-- Quadratic residues modulo a prime and Euler's criterion by the Wilson -- pairing: for an odd prime p = 2h + 1 and a not divisible by p, a is a -- square modulo p exactly when a^h ≡ 1, and otherwise a^h ≡ −1; the first -- supplement, −1 is a square modulo p exactly when h is even. -- Part of Cumulus, the Grace17 mathematics library. -- Copyright (c) 2026 Grant Passmore. -- Released under the Apache License 2.0. See LICENSE in the repository root. -- Authors: Grant Passmore module Grace.Cumulus.NumberTheory.EulerCriterion import Grace.Cumulus.NumberTheory.Wilson import Grace.Cumulus.NumberTheory.ModularMore import Grace.Cumulus.NumberTheory.CoprimeCancel -- Quadratic residues -- a is a square modulo p when some natural x has x² ≡ a (mod p). predicate QuadraticResidue (a p : Nat) := ∃ x, Nat(x) ∧ CongruentMod(x * x, a, p) theorem QuadraticResidue.intro (a p x : Nat) requires square : CongruentMod(x * x, a, p) shows QuadraticResidue(a, p) proof witness x exact square theorem QuadraticResidue.mod_iff (a p : Nat) shows QuadraticResidue(a % p, p) ↔ QuadraticResidue(a, p) proof constructor . intro residue obtain ⟨x, xNat, square⟩ := residue have back := CongruentMod.mod_left at a, p have chain := CongruentMod.transitive with square, back exact QuadraticResidue.intro with chain . intro residue obtain ⟨x, xNat, square⟩ := residue have forward := CongruentMod.mod_right at a, p have chain := CongruentMod.transitive with square, forward exact QuadraticResidue.intro with chain theorem QuadraticResidue.of_congruent (a b p : Nat) requires congruent : CongruentMod(a, b, p) requires residue : QuadraticResidue(a, p) shows QuadraticResidue(b, p) proof obtain ⟨x, xNat, square⟩ := residue have chain := CongruentMod.transitive with square, congruent exact QuadraticResidue.intro with chain theorem QuadraticResidue.mul (a b p : Nat) requires first : QuadraticResidue(a, p) requires second : QuadraticResidue(b, p) shows QuadraticResidue(a * b, p) proof obtain ⟨x, xNat, squareX⟩ := first obtain ⟨y, yNat, squareY⟩ := second have product := CongruentMod.mul with squareX, squareY have rearrange : (x * y) * (x * y) = (x * x) * (y * y) by ring rw ← rearrange at product exact QuadraticResidue.intro with product -- A square has a^h ≡ 1 -- A root x of x² ≡ a is coprime to p, so a^h ≡ x^(2h) = x^(p−1) ≡ 1 by -- Fermat. theorem euler.residue_pow (a p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires notDivides : ¬(p ∣ a) requires residue : QuadraticResidue(a, p) shows a ^ h % p = 1 proof obtain ⟨x, xNat, square⟩ := residue have nonzero := Prime.nonzero with prime have xNotDivides : ¬(p ∣ x) intro divides apply notDivides have dividesSquare := divides.multiply_right at p, x, x with divides have zero := mod.of_divides with dividesSquare unfold CongruentMod at square rw zero at square symm at square exact (mod.zero_iff at p, a with nonzero).forward with square have fermat := fermat.little with prime, xNotDivides have exponent : p - 1 = 2 * h rw odd exact sub.add_one_right at 2 * h rw exponent at fermat have squareForm : (x * x) ^ h = x ^ (2 * h) rw [(pow.mul_base at x, x, h), (pow.double_exponent at x, h), (pow.two at x ^ h)] rfl have powers := CongruentMod.pow at x * x, a, h, p with square unfold CongruentMod at powers rw [squareForm, fermat] at powers symm at powers exact powers -- The pairing x ↦ a · x⁻¹ mod p -- The partner of x under a: a · x⁻¹ mod p. def pairPartner (p a x : Nat) : Nat := mod(mul(a, modInverse(p, x)), p) proof unfold pairPartner typecheck theorem pairPartner.value (p a x : Nat) shows pairPartner(p, a, x) = mul(a, modInverse(p, x)) % p proof unfold pairPartner rfl theorem pairPartner.below (p a x : Nat) requires prime : Prime(p) shows pairPartner(p, a, x) < p proof have nonzero := Prime.nonzero with prime rw pairPartner.value exact mod.less at mul(a, modInverse(p, x)), p with nonzero -- x times its partner is a modulo p when x is a unit. theorem pairPartner.product (p a x : Nat) requires prime : Prime(p) requires unit : gcd(x, p) = 1 shows (x * pairPartner(p, a, x)) % p = a % p proof have nonzero := Prime.nonzero with prime have inverse := modInverse.product with prime, unit rw pairPartner.value have absorb := mod.mul_right at p, x, a * modInverse(p, x) with nonzero have rearrange : x * (a * modInverse(p, x)) = a * (x * modInverse(p, x)) ring have split := mod.mul at p, a, x * modInverse(p, x) with nonzero have one : a % p * 1 = a % p by ring have idempotent := mod.mod at p, a with nonzero rw [absorb, rearrange, split, inverse, one, idempotent] rfl -- The partner of the partner is x: both are units, and x · y ≡ a ≡ y · z -- cancels y. theorem pairPartner.involution (p a x : Nat) requires prime : Prime(p) requires coprimeA : gcd(a, p) = 1 requires positive : 0 < x requires below : x < p shows pairPartner(p, a, pairPartner(p, a, x)) = x proof have nonzero := Prime.nonzero with prime have unit := prime.coprime_below with prime, positive, below have inverseUnit := modInverse.unit with prime, unit have partnerUnit : gcd(pairPartner(p, a, x), p) = 1 rw pairPartner.value exact coprime.mul_mod with nonzero, coprimeA, inverseUnit have first := pairPartner.product at p, a, x with prime, unit have commute : x * pairPartner(p, a, x) = pairPartner(p, a, x) * x by ring rw commute at first have second := pairPartner.product at p, a, pairPartner(p, a, x) with prime, partnerUnit rw ← first at second have cancelled := coprime.mul_cancel_mod with nonzero, partnerUnit, second have secondBelow := pairPartner.below at p, a, pairPartner(p, a, x) with prime rwa [mod.of_less with secondBelow, mod.of_less with below] at cancelled -- Paired lists have product a^k -- A list is paired by a when every entry x occurs once, lies in 1..p−1, -- differs from its partner, and the partner is an entry too. predicate PairedBy (p a : Nat, xs : List(ℕ)) := ∀ x, Nat(x) → 0 < multiplicity(x, xs) → (multiplicity(x, xs) = 1 ∧ 0 < x ∧ x < p ∧ pairPartner(p, a, x) ≠ x ∧ 0 < multiplicity(pairPartner(p, a, x), xs)) -- A paired list of length 2k has product a^k modulo p: remove an entry and -- its partner, whose product is a modulo p, and induct on k. theorem PairedBy.product (p a k : Nat) requires prime : Prime(p) requires coprimeA : gcd(a, p) = 1 shows ∀ xs, List(ℕ, xs) → length{ℕ}(xs) = 2 * k → PairedBy(p, a, xs) → (∏ xs) % p = a ^ k % p proof induction k | zero => intros xs xsList lengthXs paired have shape := List.cases with xsList cases shape with nil | cons . rw [nil, listProduct.nil, pow.zero] rfl . exfalso obtain ⟨x, xIn, rest, restList, equal⟩ := cons rw [equal, (length.cons at ℕ, x, rest with *)] at lengthXs arith | j + 1 using ih => intros xs xsList lengthXs paired have nonzero := Prime.nonzero with prime have shape := List.cases with xsList cases shape with nil | cons . exfalso rw [nil, (length.nil at ℕ)] at lengthXs arith . obtain ⟨x, xIn, rest, restList, equal⟩ := cons have xNat : Nat(x) by typecheck rw equal at lengthXs, paired rw equal unfold PairedBy at paired -- The head x occurs once, lies in 1..p−1, and its partner y is -- another entry, so y occurs in the rest. have xHead : 0 < multiplicity(x, x :: rest) rw (multiplicity.cons_self at x, rest with *) arith obtain ⟨once, xPos, xBelow, notFixed, partnerIn⟩ := paired with x, xNat, xHead have unit := prime.coprime_below with prime, xPos, xBelow have product := pairPartner.product at p, a, x with prime, unit have involution := pairPartner.involution at p, a, x with prime, coprimeA, xPos, xBelow have restZero : multiplicity(x, rest) = 0 rw (multiplicity.cons_self at x, rest with *) at once arith have yInRest : 0 < multiplicity(pairPartner(p, a, x), rest) rwa (multiplicity.cons_other at pairPartner(p, a, x), x, rest with notFixed) at partnerIn -- Removing y from the rest leaves a list zs of length 2j. obtain ⟨zs, zsList, productRest, lengthRest, counts⟩ := multiplicity.remove_length at pairPartner(p, a, x), rest with *, yInRest have lengthZs : length{ℕ}(zs) = 2 * j unfold OneShorter at lengthRest rw [(length.cons at ℕ, x, rest with *), lengthRest] at lengthXs arith have yOnce : multiplicity(pairPartner(p, a, x), x :: rest) = 1 have propsY := paired with pairPartner(p, a, x), partnerIn exact propsY.left have yZero : multiplicity(pairPartner(p, a, x), zs) = 0 rw [(multiplicity.cons_other at pairPartner(p, a, x), x, rest with notFixed), (counts with pairPartner(p, a, x)), (multiplicity.cons_self at pairPartner(p, a, x), zs with *)] at yOnce arith -- zs is still paired: an entry q of zs is an entry of xs other than -- x and y, so its partner r is an entry other than x and y as well. have pairedZs : PairedBy(p, a, zs) unfold PairedBy intros q qNat qPositive have countQ := counts with q, qNat have consQ := multiplicity.cons at q, pairPartner(p, a, x), zs with * have qInRest : 0 < multiplicity(q, rest) rw [countQ, consQ] arith have qInXs := multiplicity.positive_cons at q, x, rest with qInRest obtain ⟨onceQ, qPos, qBelow, qNotFixed, qPartnerIn⟩ := paired with q, qNat, qInXs have qInvolution := pairPartner.involution at p, a, q with prime, coprimeA, qPos, qBelow have qNotX : q ≠ x intro same rw [same, restZero] at qInRest arith have qNotY : q ≠ pairPartner(p, a, x) intro same rw [same, yZero] at qPositive arith have onceZs : multiplicity(q, zs) = 1 rw [(multiplicity.cons_other at q, x, rest with qNotX), countQ, consQ, (equalNat.false_iff.backward with qNotY)] at onceQ have weightZero : chooseNat(Bool.false, 1, 0) = 0 by compute rw weightZero at onceQ arith have rNotX : pairPartner(p, a, q) ≠ x intro same apply qNotY rw [← qInvolution, same] rfl have rNotY : pairPartner(p, a, q) ≠ pairPartner(p, a, x) intro same apply qNotX rw [← qInvolution, same, involution] rfl have rInZs : 0 < multiplicity(pairPartner(p, a, q), zs) rwa [(multiplicity.cons_other at pairPartner(p, a, q), x, rest with rNotX), (counts with pairPartner(p, a, q)), (multiplicity.cons_other at pairPartner(p, a, q), pairPartner(p, a, x), zs with rNotY)] at qPartnerIn exact ⟨onceZs, qPos, qBelow, qNotFixed, rInZs⟩ -- The product of xs is x · y · ∏ zs ≡ a · a^j. have previous := ih with zs, zsList, lengthZs, pairedZs rw (listProduct.cons at pairPartner(p, a, x), zs with *) at productRest have rearrange := mul.associative_reverse at pairPartner(p, a, x), ∏ zs, x with * have split := mod.mul at p, x * pairPartner(p, a, x), ∏ zs with nonzero have powerMod := mod.mul at p, a, a ^ j with nonzero rw [(listProduct.cons at x, rest with *), productRest, rearrange, split, product, previous, (pow.add_one at a, j), powerMod] rfl -- A nonsquare has a^h ≡ −1 -- When a is not a square modulo p, the list 2h, …, 1 is paired by a: no -- entry is its own partner, and the partner of an entry is an entry. theorem factorialList.pairedBy (a p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires notDivides : ¬(p ∣ a) requires nonresidue : ¬QuadraticResidue(a, p) shows PairedBy(p, a, factorialList(2 * h)) proof have nonzero := Prime.nonzero with prime unfold PairedBy intros x xNat positive obtain ⟨lower, upper⟩ := factorialList.member at 2 * h, x with positive have xPos : 0 < x by arith have xBelow : x < p by arith have unit := prime.coprime_below with prime, xPos, xBelow have yBelow := pairPartner.below at p, a, x with prime have product := pairPartner.product at p, a, x with prime, unit -- A fixed point x would be a root of x² ≡ a. have notFixed : pairPartner(p, a, x) ≠ x intro fixed apply nonresidue rw fixed at product exact QuadraticResidue.intro with product -- The partner is not 0, since p ∤ a. have yPos : 0 < pairPartner(p, a, x) by_contra notPositive apply notDivides have zero : pairPartner(p, a, x) = 0 by arith rw zero at product have vanish : x * 0 = 0 by ring rw [vanish, mod.zero_left] at product symm at product exact (mod.zero_iff at p, a with nonzero).forward with product have yLower : 1 ≤ pairPartner(p, a, x) by arith have yUpper : pairPartner(p, a, x) ≤ 2 * h by arith have once := factorialList.multiplicity_in with lower, upper have yIn := factorialList.multiplicity_in with yLower, yUpper have yPositive : 0 < multiplicity(pairPartner(p, a, x), factorialList(2 * h)) rw yIn arith exact ⟨once, xPos, xBelow, notFixed, yPositive⟩ -- The product 1 · 2 ⋯ (p − 1) is a^h by the pairing and −1 by Wilson. theorem euler.nonresidue_pow (a p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires notDivides : ¬(p ∣ a) requires nonresidue : ¬QuadraticResidue(a, p) shows (a ^ h + 1) % p = 0 proof have nonzero := Prime.nonzero with prime have coprimeA := Prime.coprime_of_not_divides_gcd with prime, notDivides have paired := factorialList.pairedBy with prime, odd, notDivides, nonresidue have listFact := factorialList.list at 2 * h have lengthFact := factorialList.length at 2 * h have products := PairedBy.product at p, a, h with prime, coprimeA have productMod := products with factorialList(2 * h), listFact, lengthFact, paired have wilson := wilson.theorem with prime have predecessor : p - 1 = 2 * h rw odd exact sub.add_one_right at 2 * h rw [predecessor, ← (factorialList.product at 2 * h)] at wilson have left := mod.add at p, a ^ h, 1 with nonzero have right := mod.add at p, ∏ factorialList(2 * h), 1 with nonzero rw [left, ← productMod, ← right] exact wilson -- Euler's criterion -- For an odd prime, a^h ≡ 1 and a^h ≡ −1 cannot both hold: p would -- divide 2. theorem euler.pow_not_both (a p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires one : a ^ h % p = 1 shows (a ^ h + 1) % p ≠ 0 proof intro zero have nonzero := Prime.nonzero with prime have aboveOne := Prime.greater_than_one with prime obtain ⟨q, qNat, equation⟩ := mod.equation at a ^ h, p with nonzero rw one at equation have form : a ^ h + 1 = p * q + 2 rw equation ring have dividesSum := (mod.zero_iff at p, a ^ h + 1 with nonzero).forward with zero rw form at dividesSum have dividesMultiple := divides.mul_left_self at p, q have dividesTwo := divides.add_cancel_left with dividesMultiple, dividesSum have twoNonzero : (2:N) ≠ 0 by arith have le := divides.le with dividesTwo, twoNonzero arith theorem euler.criterion (a p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires notDivides : ¬(p ∣ a) shows QuadraticResidue(a, p) ↔ a ^ h % p = 1 proof constructor . intro residue exact euler.residue_pow with prime, odd, notDivides, residue . intro one by_contra nonresidue have zero := euler.nonresidue_pow with prime, odd, notDivides, nonresidue exact euler.pow_not_both with prime, odd, one, zero theorem euler.nonresidue_iff (a p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires notDivides : ¬(p ∣ a) shows ¬QuadraticResidue(a, p) ↔ (a ^ h + 1) % p = 0 proof constructor . intro nonresidue exact euler.nonresidue_pow with prime, odd, notDivides, nonresidue . intros zero residue have one := euler.residue_pow with prime, odd, notDivides, residue exact euler.pow_not_both with prime, odd, one, zero -- The first supplement -- (2h)² is 1 modulo p = 2h + 1, so every even power of 2h is 1. theorem negOne.even_pow (p h m : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 shows (2 * h) ^ (2 * m) % p = 1 proof have nonzero := Prime.nonzero with prime have aboveOne := Prime.greater_than_one with prime have hShape := natural.zero_or_successor at h cases hShape with zero | successor . exfalso arith . obtain ⟨n, nNat, form⟩ := successor have squareValue : (2 * h) * (2 * h) = p * (2 * n + 1) + 1 rw [odd, form] ring have square := mod.unique with nonzero, squareValue, aboveOne have expand : (2 * h) ^ (2 * m) = ((2 * h) * (2 * h)) ^ m rw [(pow.double_exponent at 2 * h, m), (pow.two at (2 * h) ^ m), (pow.mul_base at 2 * h, 2 * h, m)] rfl rw [expand, (mod.pow at p, (2 * h) * (2 * h), m with nonzero), square, (pow.one_left at m)] exact mod.of_less with aboveOne -- 2h is −1 modulo p = 2h + 1, and (2h)^h ≡ (−1)^h is 1 exactly when h is -- even. theorem negOne.residue_iff (p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 shows QuadraticResidue(2 * h, p) ↔ Even(h) proof have nonzero := Prime.nonzero with prime have aboveOne := Prime.greater_than_one with prime have notDivides : ¬(p ∣ 2 * h) intro divides have doubleNonzero : 2 * h ≠ 0 by arith have le := divides.le with divides, doubleNonzero arith have criterion := euler.criterion at 2 * h, p, h with prime, odd, notDivides have doubleBelow : 2 * h < p by arith have doubleSelf := mod.of_less with doubleBelow have parity := Even.iff_not_odd at h constructor . intro residue have power := criterion.forward with residue apply parity.backward intro oddH obtain ⟨m, mNat, form⟩ := oddH have exponent : h = 2 * m + 1 rw form ring have step : (2 * h) ^ h = (2 * h) * (2 * h) ^ (2 * m) have expand := pow.add_one at 2 * h, 2 * m rw ← exponent at expand exact expand have evenPower := negOne.even_pow at p, h, m with prime, odd have split := mod.mul_right at p, 2 * h, (2 * h) ^ (2 * m) with nonzero rw [step, ← split, evenPower] at power have one : 2 * h * 1 = 2 * h by ring rw [one, doubleSelf] at power arith . intro evenH apply criterion.backward obtain ⟨m, mNat, form⟩ := (Even.iff_two_mul at h).forward with evenH have evenPower := negOne.even_pow at p, h, m with prime, odd rwa ← form at evenPower -- The usable form for two squares: a root of x² + 1 ≡ 0 when h is even. theorem negOne.square (p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires even : Even(h) shows ∃ a, Nat(a) ∧ (a * a + 1) % p = 0 proof have nonzero := Prime.nonzero with prime have aboveOne := Prime.greater_than_one with prime have characterization := negOne.residue_iff with prime, odd obtain ⟨x, xNat, square⟩ := characterization.backward with even witness x unfold CongruentMod at square have doubleBelow : 2 * h < p by arith rw [(mod.add at p, x * x, 1 with nonzero), square, (mod.of_less with doubleBelow), (mod.of_less with aboveOne), ← odd] exact mod.self at p