TExcelTAD
Home › Number Theory & Cryptography › Legendre and Jacobi Symbols

Legendre and Jacobi Symbols

The Legendre symbol packs "is aa a square mod pp?" into a number: +1+1, −1-1 or 00. Its generalization, the Jacobi symbol, can be computed with a gcd-like algorithm without factoring and without exponentiation, and it gave the first probabilistic primality test, Solovay-Strassen.

The Legendre symbol

For an odd prime pp:

(ap)={−0p∣a−1a is a nonzero QR mod p−1a is a QNR mod pand(ap)≡a(p−1)/2(modp)\left(\frac{a}{p}\right) = \begin{cases} \phantom{-}0 & p \mid a \\ \phantom{-}1 & a \text{ is a nonzero QR mod } p \\ -1 & a \text{ is a QNR mod } p \end{cases} \qquad\text{and}\qquad \left(\frac{a}{p}\right) \equiv a^{(p-1)/2} \pmod{p}

The second formula is Euler's criterion. It makes the symbol multiplicative:

(abp)=(ap)(bp)\left(\frac{ab}{p}\right) = \left(\frac{a}{p}\right)\left(\frac{b}{p}\right)

So it is enough to know the symbol of −1-1, of 2 and of odd primes qq.

The supplements

  • (−1p)=(−1)(p−1)/2\left(\frac{-1}{p}\right) = (-1)^{(p-1)/2}: +1+1 iff p≡1(mod4)p \equiv 1 \pmod 4.
  • (2p)=(−1)(p2−1)/8\left(\frac{2}{p}\right) = (-1)^{(p^2-1)/8}: +1+1 iff p≡±1(mod8)p \equiv \pm 1 \pmod 8.
python · try it yourself
def legendre(a, p):
    t = pow(a, (p - 1) // 2, p)
    return -1 if t == p - 1 else t

for p in (7, 11, 13, 17, 19, 23, 29, 31):
    print(f"p={p:2}  p mod 8={p % 8}  (2/p)={legendre(2, p):2}  (-1/p)={legendre(p - 1, p):2}")
p= 7  p mod 8=7  (2/p)= 1  (-1/p)=-1
p=11  p mod 8=3  (2/p)=-1  (-1/p)=-1
p=13  p mod 8=5  (2/p)=-1  (-1/p)= 1
p=17  p mod 8=1  (2/p)= 1  (-1/p)= 1
p=19  p mod 8=3  (2/p)=-1  (-1/p)=-1
p=23  p mod 8=7  (2/p)= 1  (-1/p)=-1
p=29  p mod 8=5  (2/p)=-1  (-1/p)= 1
p=31  p mod 8=7  (2/p)= 1  (-1/p)=-1

The (2/p)(2/p) column is +1+1 exactly for p≡1,7(mod8)p \equiv 1, 7 \pmod 8.

The Jacobi symbol

For odd n=p1p2⋯pkn = p_1 p_2 \cdots p_k (primes, possibly repeated), define

(an)=(ap1)(ap2)⋯(apk)\left(\frac{a}{n}\right) = \left(\frac{a}{p_1}\right)\left(\frac{a}{p_2}\right)\cdots\left(\frac{a}{p_k}\right)

Caution: (an)=1\left(\frac{a}{n}\right) = 1 does not mean aa is a square modulo nn. For example (215)=(23)(25)=(−1)(−1)=1\left(\frac{2}{15}\right) = \left(\frac{2}{3}\right)\left(\frac{2}{5}\right) = (-1)(-1) = 1, yet 2 is not a square modulo 15. But −1-1 does mean "not a square".

The magic is that, thanks to quadratic reciprocity, the Jacobi symbol obeys the same flipping rules as the Legendre symbol, and can be computed like Euclid's algorithm:

python · try it yourself
def jacobi(a, n):
    assert n > 0 and n % 2 == 1
    a %= n
    result = 1
    while a:
        while a % 2 == 0:              # pull out factors of 2 using (2/n)
            a //= 2
            if n % 8 in (3, 5):
                result = -result
        a, n = n, a                    # reciprocity: flip
        if a % 4 == 3 and n % 4 == 3:
            result = -result
        a %= n
    return result if n == 1 else 0

from sympy import jacobi_symbol
print(jacobi(2, 15), jacobi(1001, 9907), jacobi(19, 45), jacobi(5, 21))
print(all(jacobi(a, n) == jacobi_symbol(a, n) for n in range(1, 300, 2) for a in range(0, 60)))
1 -1 1 1
True

The algorithm runs in O(log⁡2n)O(\log^2 n) bit operations and never factors nn.

Solovay-Strassen primality test

For a prime nn, Euler's criterion says a(n−1)/2≡(an)(modn)a^{(n-1)/2} \equiv \left(\frac{a}{n}\right) \pmod n for all aa. For an odd composite nn, at least half of the units violate it (there are no "Carmichael numbers" for this test). So one random base catches a composite with probability at least 1/2.

python · try it yourself
def jacobi(a, n):
    a %= n; r = 1
    while a:
        while a % 2 == 0:
            a //= 2
            if n % 8 in (3, 5): r = -r
        a, n = n, a
        if a % 4 == 3 and n % 4 == 3: r = -r
        a %= n
    return r if n == 1 else 0

def euler_witness(a, n):
    j = jacobi(a, n) % n               # map -1 to n - 1
    return j == 0 or pow(a, (n - 1) // 2, n) != j

for n in (561, 1105, 1729, 1_000_003):
    witnesses = sum(euler_witness(a, n) for a in range(2, 200))
    print(n, "witnesses among bases 2..199:", witnesses)
561 witnesses among bases 2..199: 171
1105 witnesses among bases 2..199: 161
1729 witnesses among bases 2..199: 117
1000003 witnesses among bases 2..199: 0

Miller-Rabin is stronger: every strong liar is also an Euler liar, and its error per round is at most 1/4 instead of 1/2. So it replaced Solovay-Strassen in practice.

In software

  • sympy.jacobi_symbol, GMP's mpz_jacobi; the Baillie-PSW primality test (used by many libraries) needs a Jacobi symbol to choose its Lucas parameters.
  • The Goldwasser-Micali cryptosystem, the first provably secure probabilistic encryption scheme, encrypts single bits as residues or non-residues with Jacobi symbol +1+1 modulo n=pqn = pq.

Exercises

Exercise 1: Multiplicativity

Compute (611)\left(\frac{6}{11}\right) from (211)\left(\frac{2}{11}\right) and (311)\left(\frac{3}{11}\right).

Answer

−1-1.

Solution

11≡3(mod8)11 \equiv 3 \pmod 8, so (211)=−1\left(\frac{2}{11}\right) = -1. The squares modulo 11 are 1, 3, 4, 5, 9, so (311)=1\left(\frac{3}{11}\right) = 1. The product is −1-1: 6 is not a square modulo 11.

Exercise 2: Jacobi is not squareness

Show that (215)=1\left(\frac{2}{15}\right) = 1 but x2≡2(mod15)x^2 \equiv 2 \pmod{15} has no solution.

Answer

(−1)(−1)=1(-1)(-1) = 1, but 2 is not a square modulo 3.

Solution

(23)=−1\left(\frac{2}{3}\right) = -1 (squares mod 3: only 1) and (25)=−1\left(\frac{2}{5}\right) = -1 (squares mod 5: 1, 4). The Jacobi symbol is +1+1, but a solution modulo 15 would give a solution modulo 3, which does not exist.

Exercise 3: Distribution (code)

For n=3⋅5⋅7=105n = 3 \cdot 5 \cdot 7 = 105, count the units with Jacobi symbol +1+1 and how many of those are actual squares.

Answer

24 units have symbol +1+1; only 6 are squares.

Solution
python · try it yourself
from math import gcd
from sympy import jacobi_symbol
n = 105
units = [a for a in range(1, n) if gcd(a, n) == 1]
plus = [a for a in units if jacobi_symbol(a, n) == 1]
squares = sorted({x * x % n for x in units})
print(len(units), len(plus), len(squares))
48 24 6

φ(105)=48\varphi(105) = 48; half have symbol +1+1. A square must be a residue modulo 3, 5 and 7 simultaneously, a fraction 1/81/8 of the units: 6.

Test yourself

If (an)=−1\left(\frac{a}{n}\right) = -1 for the Jacobi symbol, then…

  1. aa is a square mod nn
  2. aa is not a square mod nn
  3. nn is prime
  4. nothing

(2p)=1\left(\frac{2}{p}\right) = 1 exactly when p mod 8p \bmod 8 is…

  1. 1 or 7
  2. 3 or 5
  3. 1 or 3
  4. 5 or 7

The Jacobi symbol can be computed…

  1. only after factoring nn
  2. with a Euclid-like algorithm, without factoring
  3. only for prime nn
  4. only with floating point