Quadratic Reciprocity
Is 3 a square modulo 1009? Is 1009 a square modulo 3? These look like unrelated questions, yet Gauss proved they almost always have the same answer. The law of quadratic reciprocity, which he called the "golden theorem" and proved eight times, is what makes the Jacobi symbol computable like a gcd.
The law
For distinct odd primes and :
In words: the two symbols are equal unless both and are , in which case they are opposite.
from sympy import primerange, legendre_symbol
ok, n = True, 0
for p in primerange(3, 200):
for q in primerange(3, 200):
if p < q:
lhs = legendre_symbol(p, q) * legendre_symbol(q, p)
rhs = (-1) ** (((p - 1) // 2) * ((q - 1) // 2))
ok &= lhs == rhs
n += 1
print(n, "pairs checked, law holds:", ok)
print("(3/1009) =", legendre_symbol(3, 1009), " (1009/3) =", legendre_symbol(1009, 3), " 1009 mod 4 =", 1009 % 4)
print("(7/11) =", legendre_symbol(7, 11), " (11/7) =", legendre_symbol(11, 7))990 pairs checked, law holds: True
(3/1009) = 1 (1009/3) = 1 1009 mod 4 = 1
(7/11) = -1 (11/7) = 1
Since the two symbols agree; is a square modulo 3, so 3 is a square modulo 1009. For 7 and 11, both , the symbols are opposite.
Computing a symbol by hand
Reciprocity plus the supplements reduce any Legendre symbol quickly. Example, (383 is prime):
So : 219 is a square modulo 383.
p, a = 383, 219
print(pow(a, (p - 1) // 2, p), [x for x in range(p) if x * x % p == a])1 [169, 214]
Which primes is 3 a square modulo?
Reciprocity converts "is a square mod " (a question about ) into "is a square mod " (a question about ). For : iff . For : iff .
from sympy import primerange, legendre_symbol
for q, m in ((3, 12), (5, 5)):
classes = sorted({p % m for p in primerange(7, 2000) if legendre_symbol(q, p) == 1})
print(f"({q}/p) = 1 exactly for p mod {m} in {classes}")(3/p) = 1 exactly for p mod 12 in [1, 11]
(5/p) = 1 exactly for p mod 5 in [1, 4]
Gauss's lemma (idea of a proof)
One classic proof counts: where is the number of values whose remainder mod exceeds (Gauss's lemma). Eisenstein turned that count into lattice points under the line , and counting the points of a rectangle in two ways yields the law.
def gauss_lemma(a, p):
mu = sum(1 for k in range(1, (p - 1) // 2 + 1) if (k * a) % p > p / 2)
return (-1) ** mu
from sympy import legendre_symbol
print(all(gauss_lemma(a, p) == legendre_symbol(a, p) for p in (7, 11, 13, 101, 383) for a in range(1, p)))True
In software
- Reciprocity is the engine of the Jacobi-symbol algorithm used by Baillie-PSW primality tests and by Goldwasser-Micali.
- Generalized reciprocity laws describe how primes split in number fields, the mathematics behind the number field sieve that factors RSA moduli.
Exercises
Exercise 1: Flip
Use reciprocity to compute .
Answer
.
Solution
, so . Indeed .
Exercise 2: Both
Compute via reciprocity.
Answer
.
Solution
, so . The squares modulo 7 are 1, 2, 4.
Exercise 3: When is a square? (code)
Find a rule for which primes have as a square, and check it in code.
Answer
Exactly when .
Solution
, and the sign factors from reciprocity cancel, leaving , which is iff .
from sympy import primerange, legendre_symbol
print(all((legendre_symbol(-3 % p, p) == 1) == (p % 3 == 1) for p in primerange(5, 5000)))True
This is why primes admit curves like with an efficient endomorphism, used by secp256k1 (Bitcoin's curve).
Test yourself
For odd primes , and are opposite exactly when…
- both are
- both are
- one is 2
- never
Reciprocity is useful in computation because it…
- finds square roots
- lets you swap and reduce the arguments like Euclid's algorithm
- factors
- proves primality
Is 13 a square modulo 17?
- yes
- no