TExcelTAD
Home › Number Theory & Cryptography › Quadratic Reciprocity

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 pp and qq:

(pq)(qp)=(−1)p−12⋅q−12\left(\frac{p}{q}\right)\left(\frac{q}{p}\right) = (-1)^{\frac{p-1}{2} \cdot \frac{q-1}{2}}

In words: the two symbols are equal unless both pp and qq are ≡3(mod4)\equiv 3 \pmod 4, in which case they are opposite.

python · try it yourself
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 1009≡1(mod4)1009 \equiv 1 \pmod 4 the two symbols agree; 1009≡1(mod3)1009 \equiv 1 \pmod 3 is a square modulo 3, so 3 is a square modulo 1009. For 7 and 11, both ≡3(mod4)\equiv 3 \pmod 4, the symbols are opposite.

Computing a symbol by hand

Reciprocity plus the supplements reduce any Legendre symbol quickly. Example, (219383)\left(\frac{219}{383}\right) (383 is prime):

(219383)=(3383)(73383)219=3⋅73(3383)=−(3833)=−(23)=13,383≡3(mod4)(73383)=(38373)=(1873)=(273)=173≡1(mod4), 18=2⋅32, 73≡1(mod8)\begin{aligned} \left(\frac{219}{383}\right) &= \left(\frac{3}{383}\right)\left(\frac{73}{383}\right) && 219 = 3 \cdot 73 \\ \left(\frac{3}{383}\right) &= -\left(\frac{383}{3}\right) = -\left(\frac{2}{3}\right) = 1 && 3, 383 \equiv 3 \pmod 4 \\ \left(\frac{73}{383}\right) &= \left(\frac{383}{73}\right) = \left(\frac{18}{73}\right) = \left(\frac{2}{73}\right) = 1 && 73 \equiv 1 \pmod 4,\ 18 = 2 \cdot 3^2,\ 73 \equiv 1 \pmod 8 \end{aligned}

So (219383)=1\left(\frac{219}{383}\right) = 1: 219 is a square modulo 383.

python · try it yourself
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 qq a square mod pp" (a question about pp) into "is pp a square mod qq" (a question about p mod qp \bmod q). For q=3q = 3: (3p)=1\left(\frac{3}{p}\right) = 1 iff p≡±1(mod12)p \equiv \pm 1 \pmod{12}. For q=5q = 5: (5p)=1\left(\frac{5}{p}\right) = 1 iff p≡±1(mod5)p \equiv \pm 1 \pmod 5.

python · try it yourself
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: (ap)=(−1)μ\left(\frac{a}{p}\right) = (-1)^\mu where μ\mu is the number of values a,2a,…,p−12aa, 2a, \ldots, \frac{p-1}{2}a whose remainder mod pp exceeds p/2p/2 (Gauss's lemma). Eisenstein turned that count into lattice points under the line y=qpxy = \frac{q}{p}x, and counting the points of a rectangle in two ways yields the law.

python · try it yourself
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 (519)\left(\frac{5}{19}\right).

Answer

+1+1.

Solution

5≡1(mod4)5 \equiv 1 \pmod 4, so (519)=(195)=(45)=1\left(\frac{5}{19}\right) = \left(\frac{19}{5}\right) = \left(\frac{4}{5}\right) = 1. Indeed 92=81=4⋅19+59^2 = 81 = 4 \cdot 19 + 5.

Exercise 2: Both ≡3(mod4)\equiv 3 \pmod 4

Compute (37)\left(\frac{3}{7}\right) via reciprocity.

Answer

−1-1.

Solution

3≡7≡3(mod4)3 \equiv 7 \equiv 3 \pmod 4, so (37)=−(73)=−(13)=−1\left(\frac{3}{7}\right) = -\left(\frac{7}{3}\right) = -\left(\frac{1}{3}\right) = -1. The squares modulo 7 are 1, 2, 4.

Exercise 3: When is −3-3 a square? (code)

Find a rule for which primes p>3p > 3 have −3-3 as a square, and check it in code.

Answer

Exactly when p≡1(mod3)p \equiv 1 \pmod 3.

Solution

(−3p)=(−1p)(3p)\left(\frac{-3}{p}\right) = \left(\frac{-1}{p}\right)\left(\frac{3}{p}\right), and the sign factors from reciprocity cancel, leaving (p3)\left(\frac{p}{3}\right), which is +1+1 iff p≡1(mod3)p \equiv 1 \pmod 3.

python · try it yourself
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 p≡1(mod3)p \equiv 1 \pmod 3 admit curves like y2=x3+by^2 = x^3 + b with an efficient endomorphism, used by secp256k1 (Bitcoin's curve).

Test yourself

For odd primes p≠qp \ne q, (pq)\left(\frac{p}{q}\right) and (qp)\left(\frac{q}{p}\right) are opposite exactly when…

  1. both are ≡1(mod4)\equiv 1 \pmod 4
  2. both are ≡3(mod4)\equiv 3 \pmod 4
  3. one is 2
  4. never

Reciprocity is useful in computation because it…

  1. finds square roots
  2. lets you swap and reduce the arguments like Euclid's algorithm
  3. factors nn
  4. proves primality

Is 13 a square modulo 17?

  1. yes
  2. no