Legendre and Jacobi Symbols
The Legendre symbol packs "is a square mod ?" into a number: , or . 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 :
The second formula is Euler's criterion. It makes the symbol multiplicative:
So it is enough to know the symbol of , of 2 and of odd primes .
The supplements
- : iff .
- : iff .
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 column is exactly for .
The Jacobi symbol
For odd (primes, possibly repeated), define
Caution: does not mean is a square modulo . For example , yet 2 is not a square modulo 15. But 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:
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 bit operations and never factors .
Solovay-Strassen primality test
For a prime , Euler's criterion says for all . For an odd composite , 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.
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'smpz_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 modulo .
Exercises
Exercise 1: Multiplicativity
Compute from and .
Answer
.
Solution
, so . The squares modulo 11 are 1, 3, 4, 5, 9, so . The product is : 6 is not a square modulo 11.
Exercise 2: Jacobi is not squareness
Show that but has no solution.
Answer
, but 2 is not a square modulo 3.
Solution
(squares mod 3: only 1) and (squares mod 5: 1, 4). The Jacobi symbol is , but a solution modulo 15 would give a solution modulo 3, which does not exist.
Exercise 3: Distribution (code)
For , count the units with Jacobi symbol and how many of those are actual squares.
Answer
24 units have symbol ; only 6 are squares.
Solution
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
; half have symbol . A square must be a residue modulo 3, 5 and 7 simultaneously, a fraction of the units: 6.
Test yourself
If for the Jacobi symbol, then…
- is a square mod
- is not a square mod
- is prime
- nothing
exactly when is…
- 1 or 7
- 3 or 5
- 1 or 3
- 5 or 7
The Jacobi symbol can be computed…
- only after factoring
- with a Euclid-like algorithm, without factoring
- only for prime
- only with floating point