Quadratic Residues

lean4-proofnumber-theoryvisualization
a is a QR mod p    x,  x2a(modp)a \text{ is a QR mod } p \iff \exists x,\; x^2 \equiv a \pmod{p}

Statement

Let pp be an odd prime. An integer aa with pap \nmid a is called a quadratic residue (QR) mod pp if there exists xx with x2a(modp)x^2 \equiv a \pmod{p}; otherwise it is a quadratic non-residue (QNR). Exactly (p1)/2(p-1)/2 residues are QRs and (p1)/2(p-1)/2 are QNRs.

A classical characterisation (Euler's criterion): for pap \nmid a,

a(p1)/2{1(modp)a is a QR mod p1(modp)a is a QNR mod pa^{(p-1)/2} \equiv \begin{cases} 1 \pmod{p} & a \text{ is a QR mod } p \\ -1 \pmod{p} & a \text{ is a QNR mod } p \end{cases}

For a=1a = -1 the criterion gives a clean answer: 1-1 is a QR mod pp if and only if p1(mod4)p \equiv 1 \pmod{4}.

Visualization

Quadratic residues mod 7 (squares of 1,,61,\ldots,6 reduced mod 7):

xxx2mod7x^2 \bmod 7
11
24
32
42
54
61

QRs mod 7: {1,2,4}\{1, 2, 4\}. QNRs mod 7: {3,5,6}\{3, 5, 6\}.

Quadratic residues mod 13 (squares of 1,,121,\ldots,12 reduced mod 13):

xx123456789101112
x2mod13x^2 \bmod 131493121010123941

QRs mod 13: {1,3,4,9,10,12}\{1, 3, 4, 9, 10, 12\}. QNRs: {2,5,6,7,8,11}\{2, 5, 6, 7, 8, 11\}.

Note: p=131(mod4)p = 13 \equiv 1 \pmod{4}, so 112-1 \equiv 12 is a QR — confirmed above.

Proof Sketch

  1. The map xx2x \mapsto x^2 on (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times is 2-to-1 (since x2=(x)2x^2 = (-x)^2 and xxx \neq -x for pp odd), so exactly (p1)/2(p-1)/2 elements are QRs.
  2. Every element aa of (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times satisfies ap1=1a^{p-1} = 1 by Fermat's Little TheoremFermat's Little Theoremapa(modp)a^p \equiv a \pmod{p}For prime p, a^p is congruent to a mod pRead more →, so a(p1)/2a^{(p-1)/2} is a square root of 11, hence ±1\pm 1.
  3. The QRsQRs are exactly the kernel of aa(p1)/2a \mapsto a^{(p-1)/2} (the (p1)/2(p-1)/2-th power map), giving Euler's criterion.
  4. For a=1a = -1: (1)(p1)/2=1(-1)^{(p-1)/2} = 1 iff (p1)/2(p-1)/2 is even iff p1(mod4)p \equiv 1 \pmod{4}.

Connections

Quadratic residues are the language in which 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 → is stated: the Legendre symbol encodes whether aa is a QR mod pp. The count of QRs is governed by Euler's Totient FunctionEuler's Totient Functionaφ(n)1(modn)a^{\varphi(n)} \equiv 1 \pmod{n}φ(n) counts integers up to n coprime to n, and governs modular exponentiationRead more → through the index of the subgroup of squares in (Z/pZ)×(\mathbb{Z}/p\mathbb{Z})^\times.

Lean4 Proof

/-- -1 is a square in ZMod p iff p % 4 ≠ 3.
    Mathlib's `ZMod.exists_sq_eq_neg_one_iff` states this directly. -/
theorem neg_one_is_qr (p : ) [Fact p.Prime] :
    IsSquare (-1 : ZMod p)  p % 4  3 :=
  ZMod.exists_sq_eq_neg_one_iff