Legendre Symbol

lean4-proofnumber-theoryvisualization
(ap)=a(p1)/2modp\left(\frac{a}{p}\right) = a^{(p-1)/2} \bmod p

Statement

For an odd prime pp and integer aa, the Legendre symbol (ap)\left(\frac{a}{p}\right) (also written legendreSym  p  a\text{legendreSym}\; p\; a in Lean) is defined as

(ap)={0pa1a is a QR mod p1a is a QNR mod p\left(\frac{a}{p}\right) = \begin{cases} 0 & p \mid a \\ 1 & a \text{ is a QR mod } p \\ -1 & a \text{ is a QNR mod } p \end{cases}

Euler's criterion gives a computable formula: for pap \nmid a,

(ap)a(p1)/2(modp)\left(\frac{a}{p}\right) \equiv a^{(p-1)/2} \pmod{p}

In Mathlib this is legendreSym.eq_pow: the integer value of legendreSym  p  a\text{legendreSym}\; p\; a cast into Z/pZ\mathbb{Z}/p\mathbb{Z} equals (amodp)p/2(a \bmod p)^{p/2}.

Visualization

Legendre symbol (a7)\left(\frac{a}{7}\right) for p=7p = 7, a=1,,6a = 1,\ldots,6:

aaa3mod7a^3 \bmod 7(a7)\left(\frac{a}{7}\right)
11+1+1
21+1+1 (2 is QR: 32=923^2=9\equiv 2)
36 1\equiv -11-1 (3 is QNR)
41+1+1 (4 =22= 2^2)
56 1\equiv -11-1 (5 is QNR)
66 1\equiv -11-1 (6 is QNR)

Check: QRs mod 7 are {1,2,4}\{1, 2, 4\} — three values, which is (71)/2=3(7-1)/2 = 3. The product of all six symbols is (1)3=1(-1)^3 = -1.

Proof Sketch

  1. The group (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times is cyclic of order p1p-1 by Primitive RootsPrimitive Rootsg is a primitive root mod p    {g0,g1,,gp2}=(Z/pZ)×g \text{ is a primitive root mod } p \iff \{g^0, g^1, \ldots, g^{p-2}\} = (\mathbb{Z}/p\mathbb{Z})^\timesA primitive root mod p is a generator of the cyclic group (Z/pZ)*, existing for every prime pRead more →. Let gg be a generator; write a=gka = g^k.
  2. aa is a QR iff kk is even. The map aa(p1)/2a \mapsto a^{(p-1)/2} sends gkg^k to gk(p1)/2g^{k(p-1)/2}. Since gp1=1g^{p-1} = 1, this is 11 when kk is even and 1-1 when kk is odd.
  3. This coincides with the Legendre symbol, establishing Euler's criterion.
  4. Multiplicativity (abp)=(ap)(bp)\left(\frac{ab}{p}\right) = \left(\frac{a}{p}\right)\left(\frac{b}{p}\right) follows from the same power-map argument.

Connections

The Legendre symbol is the building block of Quadratic ReciprocityQuadratic Reciprocity(pq)(qp)=(1)p12q12\left(\frac{p}{q}\right)\left(\frac{q}{p}\right) = (-1)^{\frac{p-1}{2}\cdot\frac{q-1}{2}}Relationship between solvability of quadratic congruences for two odd primesRead more →, which relates (pq)\left(\frac{p}{q}\right) and (qp)\left(\frac{q}{p}\right) for distinct odd primes. The generalisation to composite moduli is the Jacobi SymbolJacobi Symbol(an)=i(api)ei\left(\frac{a}{n}\right) = \prod_{i} \left(\frac{a}{p_i}\right)^{e_i}The Jacobi symbol generalises the Legendre symbol to composite odd moduliRead more →.

Lean4 Proof

/-- Euler's criterion in Mathlib: the cast of `legendreSym p a` into `ZMod p`
    equals `(a : ZMod p) ^ (p / 2)`. -/
theorem legendre_euler_criterion (p : ) [Fact p.Prime] (a : ) :
    (legendreSym p a : ZMod p) = (a : ZMod p) ^ (p / 2) :=
  legendreSym.eq_pow p a