-- Gauss's lemma: for an odd prime p = 2h + 1 and a not divisible by p, -- a^h ≡ 1 modulo p exactly when the Gauss set of a has even cardinality; -- so a is a square modulo p exactly when that count 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.GaussLemma import Grace.Cumulus.NumberTheory.GaussCount import Grace.Cumulus.NumberTheory.EulerCriterion import Grace.Cumulus.NumberTheory.CoprimeCancel -- The entries of the residue list -- Every entry of 1..h lies below p = 2h + 1. theorem factorialList.entries_below (p h : Nat) requires odd : p = 2 * h + 1 shows ∀ x, Nat(x) → 0 < multiplicity(x, factorialList(h)) → x < p proof intros x xNat positive obtain ⟨lower, upper⟩ := factorialList.member with positive arith -- Every entry of the residue list a · 1, …, a · h mod p is in 1..p−1: an -- entry r is below p, and a⁻¹ r mod p is some k in 1..h, so r is not 0. theorem gaussList.entries (a p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires notDivides : ¬(p ∣ a) shows ∀ r, Nat(r) → 0 < multiplicity(r, gaussList(a, p, h)) → 0 < r ∧ r < p proof intros r rNat positive have nonzero := Prime.nonzero with prime rw gaussList.value at positive have members := scaledList.member at a, p, factorialList(h) with nonzero have rBelow := members with r, rNat, positive constructor . have coprime := Prime.coprime_of_not_divides_gcd with prime, notDivides have inverse := modInverse.product with prime, coprime have entries := factorialList.entries_below with odd have shift := scaledList.multiplicity at a, modInverse(p, a), p, r, factorialList(h) with nonzero, inverse, rBelow rw (shift with entries) at positive by_contra notPositive have zero : r = 0 by arith rw zero at positive have vanish : modInverse(p, a) * 0 = 0 by ring rw [vanish, mod.zero_left] at positive obtain ⟨lower, upper⟩ := factorialList.member with positive arith . exact rBelow -- Folding the residues onto 1..h -- Entries of a list with entries in 1..p−1 fold into 1..h; an x outside -- 1..h never occurs in the folded list. theorem foldedList.multiplicity_out (p h x : Nat) (xs : List(ℕ)) requires odd : p = 2 * h + 1 requires outside : ¬(0 < x ∧ x ≤ h) shows (∀ r, Nat(r) → 0 < multiplicity(r, xs) → 0 < r ∧ r < p) → multiplicity(x, foldedList(p, h, xs)) = 0 proof induction xs | List.nil => intro entries rw [foldedList.nil, multiplicity.nil] rfl | List.cons(r, rest) using ih => intro entries have rHead : 0 < multiplicity(r, r :: rest) rw (multiplicity.cons_self at r, rest with *) arith have rNat : Nat(r) by typecheck obtain ⟨rLower, rUpper⟩ := entries with r, rNat, rHead have restEntries : ∀ y, Nat(y) → 0 < multiplicity(y, rest) → 0 < y ∧ y < p intros y yNat yPositive have lifted := multiplicity.positive_cons at y, r, rest with yPositive exact entries with y, yNat, lifted have previous := ih with restEntries have foldedRest := foldedList.list at p, h, rest with * have split := Less.or_LessEqual at h, r cases split with large | small . have rLe : r ≤ p by arith have pieces := sub.add_cancel with rLe have different : x ≠ p - r intro same rw same at outside have lower : 0 < p - r by arith have upper : p - r ≤ h by arith exact outside with ⟨lower, upper⟩ rw [foldedList.cons_large, (multiplicity.cons_other with different, foldedRest)] exact previous . have different : x ≠ r intro same rw same at outside exact outside with ⟨rLower, small⟩ rw [foldedList.cons_small, (multiplicity.cons_other with different, foldedRest)] exact previous -- For x in 1..h, the folded list holds x once for each entry x and once -- for each entry p − x of the original list. theorem foldedList.multiplicity_in (p h x : Nat) (xs : List(ℕ)) requires odd : p = 2 * h + 1 requires positive : 0 < x requires bound : x ≤ h shows (∀ r, Nat(r) → 0 < multiplicity(r, xs) → 0 < r ∧ r < p) → multiplicity(x, foldedList(p, h, xs)) = multiplicity(x, xs) + multiplicity(p - x, xs) proof induction xs | List.nil => intro entries rw [foldedList.nil, multiplicity.nil, multiplicity.nil] ring | List.cons(r, rest) using ih => intro entries have xLe : x ≤ p by arith have complement := sub.add_cancel with xLe have rHead : 0 < multiplicity(r, r :: rest) rw (multiplicity.cons_self at r, rest with *) arith have rNat : Nat(r) by typecheck obtain ⟨rLower, rUpper⟩ := entries with r, rNat, rHead have restEntries : ∀ y, Nat(y) → 0 < multiplicity(y, rest) → 0 < y ∧ y < p intros y yNat yPositive have lifted := multiplicity.positive_cons at y, r, rest with yPositive exact entries with y, yNat, lifted have previous := ih with restEntries have foldedRest := foldedList.list at p, h, rest with * have split := Less.or_LessEqual at h, r cases split with large | small . -- A large head r folds to p − r, which is x exactly when r = p − x. have rLe : r ≤ p by arith have pieces := sub.add_cancel with rLe have apart : x ≠ r by arith rw [foldedList.cons_large, multiplicity.cons_other with apart] have cases := Less.trichotomy at x, p - r with * rcases cases with below | same | above . have different : x ≠ p - r by arith have other : p - x ≠ r by arith rw [multiplicity.cons_other with different, multiplicity.cons_other with other] exact previous . have cancel := sub.sub_cancel with rLe rw [same, cancel] at previous rw [same, cancel, multiplicity.cons_self, multiplicity.cons_self, previous] ring . have different : x ≠ p - r by arith have other : p - x ≠ r by arith rw [multiplicity.cons_other with different, multiplicity.cons_other with other] exact previous . -- A small head r stays, and is x exactly when r = x. have different : p - x ≠ r intro same have impossible : ¬p - x ≤ h by arith rw same at impossible exact impossible with small rw foldedList.cons_small have cases := Less.trichotomy at x, r with * rcases cases with below | same | above . have apart : x ≠ r by arith rw [multiplicity.cons_other with apart, multiplicity.cons_other with different] exact previous . rw ← same at different rw [← same, multiplicity.cons_self, multiplicity.cons_self, multiplicity.cons_other with different, previous] ring . have apart : x ≠ r by arith rw [multiplicity.cons_other with apart, multiplicity.cons_other with different] exact previous -- The folded residues of a · 1, …, a · h lie in 1..h and are distinct: -- a k ≡ ±a j forces k ≡ ±j, and 0 < k + j ≤ 2h < p, 0 < k, j ≤ h give -- k = j. So the folded list is a permutation of 1..h. theorem foldedList.permutes (a p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires notDivides : ¬(p ∣ a) shows ∀ x, Nat(x) → multiplicity(x, foldedList(p, h, gaussList(a, p, h))) = multiplicity(x, factorialList(h)) proof intros x xNat have nonzero := Prime.nonzero with prime have entries := gaussList.entries with prime, odd, notDivides have inRange : (0 < x ∧ x ≤ h) ∨ ¬(0 < x ∧ x ≤ h) by tauto cases inRange with inside | outside . obtain ⟨positive, bound⟩ := inside have folded := foldedList.multiplicity_in at p, h, x, gaussList(a, p, h) with odd, positive, bound rw folded with entries -- x and p − x occur in the residue list as often as a⁻¹ x mod p and -- its complement occur in 1..h. have coprime := Prime.coprime_of_not_divides_gcd with prime, notDivides have inverseUnit := modInverse.unit with prime, coprime have inverse := modInverse.product with prime, coprime have listEntries := factorialList.entries_below with odd have xBelow : x < p by arith have xLe : x ≤ p by arith have complement := sub.add_cancel with xLe have complementBelow : p - x < p by arith rw gaussList.value have shiftX := scaledList.multiplicity at a, modInverse(p, a), p, x, factorialList(h) with nonzero, inverse, xBelow have shiftComplement := scaledList.multiplicity at a, modInverse(p, a), p, p - x, factorialList(h) with nonzero, inverse, complementBelow rw [shiftX with listEntries, shiftComplement with listEntries] -- a⁻¹ x mod p is not 0, so a⁻¹ (p − x) mod p is its complement, and -- exactly one of the two lies in 1..h. have residueBelow := mod.less at modInverse(p, a) * x, p with nonzero have notMultiple : (modInverse(p, a) * x) % p ≠ 0 intro zero have divides := (mod.zero_iff at p, modInverse(p, a) * x with nonzero).forward with zero have cancelled := coprime.cancel with inverseUnit, divides have xNonzero : x ≠ 0 by arith have le := divides.le with cancelled, xNonzero arith have swap := mod.mul_complement at p, modInverse(p, a), x with nonzero, xLe, notMultiple rw swap have residueLe : (modInverse(p, a) * x) % p ≤ p by arith have residueComplement := sub.add_cancel with residueLe rw factorialList.multiplicity_in with positive, bound have cases := Less.or_LessEqual at h, (modInverse(p, a) * x) % p cases cases with large | small . have outside : ¬(1 ≤ (modInverse(p, a) * x) % p ∧ (modInverse(p, a) * x) % p ≤ h) intro both obtain ⟨first, second⟩ := both arith have lower : 1 ≤ p - (modInverse(p, a) * x) % p by arith have upper : p - (modInverse(p, a) * x) % p ≤ h by arith rw [factorialList.multiplicity_out with outside, (factorialList.multiplicity_in with lower, upper)] ring . have lower : 1 ≤ (modInverse(p, a) * x) % p by arith have outside : ¬(1 ≤ p - (modInverse(p, a) * x) % p ∧ p - (modInverse(p, a) * x) % p ≤ h) intro both obtain ⟨first, second⟩ := both arith rw [(factorialList.multiplicity_in with lower, small), factorialList.multiplicity_out with outside] ring . have folded := foldedList.multiplicity_out at p, h, x, gaussList(a, p, h) with odd, outside have outsideOne : ¬(1 ≤ x ∧ x ≤ h) intro both obtain ⟨first, second⟩ := both have positive : 0 < x by arith exact outside with ⟨positive, second⟩ rw [folded with entries, factorialList.multiplicity_out with outsideOne] rfl theorem foldedList.product_factorial (a p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires notDivides : ¬(p ∣ a) shows ∏ foldedList(p, h, gaussList(a, p, h)) = h! proof have permutes := foldedList.permutes with prime, odd, notDivides have list := factorialList.list at h have same := listProduct.of_multiplicities at foldedList(p, h, gaussList(a, p, h)) with list, permutes rw [same, factorialList.product] rfl -- The two products -- The residue list has product a^h h! modulo p. theorem gaussList.product (a p h : Nat) requires prime : Prime(p) shows (∏ gaussList(a, p, h)) % p = (a ^ h * h!) % p proof have nonzero := Prime.nonzero with prime have product := scaledList.product at a, p, factorialList(h) with nonzero rw [gaussList.value, product, factorialList.length, factorialList.product] rfl -- The two products agree with a^h h! and h! modulo p, the residue list -- has entries below p, and its large count is the Gauss set's cardinality. theorem gaussList.products (a p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires notDivides : ¬(p ∣ a) shows (∏ gaussList(a, p, h)) % p = (a ^ h * h!) % p ∧ ∏ foldedList(p, h, gaussList(a, p, h)) = h! ∧ (∀ r, Nat(r) → 0 < multiplicity(r, gaussList(a, p, h)) → r < p) ∧ largeCount(h, gaussList(a, p, h)) = cardinality(gaussSet(a, p, h)) proof have entries := gaussList.entries with prime, odd, notDivides have belowP : ∀ r, Nat(r) → 0 < multiplicity(r, gaussList(a, p, h)) → r < p intros r rNat positive exact (entries with r, rNat, positive).right have left := gaussList.product at a, p, h with prime have right := foldedList.product_factorial with prime, odd, notDivides have count := largeCount.gaussSet at a, p, h exact ⟨left, right, belowP, count⟩ -- Gauss's lemma -- a^h h! ≡ ±h! with the sign of the large count; cancel h!. theorem gauss.pow_even (a p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires notDivides : ¬(p ∣ a) requires even : Even(cardinality(gaussSet(a, p, h))) shows a ^ h % p = 1 proof have nonzero := Prime.nonzero with prime have aboveOne := Prime.greater_than_one with prime obtain ⟨left, right, belowP, count⟩ := gaussList.products with prime, odd, notDivides rw ← count at even have agree := foldedList.product_even with belowP, even rw [left, right] at agree have hBelow : h < p by arith have coprime := factorial.coprime_prime with prime, hBelow have same : (h! * a ^ h) % p = (h! * 1) % p have commute : h! * a ^ h = a ^ h * h! by ring have unit : h! * 1 = h! by ring rw [commute, unit] exact agree have cancelled := coprime.mul_cancel_mod with nonzero, coprime, same have oneSelf := mod.of_less at p, 1 with aboveOne rwa oneSelf at cancelled theorem gauss.pow_odd (a p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires notDivides : ¬(p ∣ a) requires oddCount : Odd(cardinality(gaussSet(a, p, h))) shows (a ^ h + 1) % p = 0 proof have nonzero := Prime.nonzero with prime obtain ⟨left, right, belowP, count⟩ := gaussList.products with prime, odd, notDivides rw ← count at oddCount have sum := foldedList.product_odd with belowP, oddCount have split := mod.add at p, ∏ gaussList(a, p, h), ∏ foldedList(p, h, gaussList(a, p, h)) with nonzero rw [split, left, right] at sum have merge := mod.add at p, a ^ h * h!, h! with nonzero rw ← merge at sum have factor : a ^ h * h! + h! = h! * (a ^ h + 1) by ring rw factor at sum have divides := (mod.zero_iff at p, h! * (a ^ h + 1) with nonzero).forward with sum have hBelow : h < p by arith have coprime := factorial.coprime_prime with prime, hBelow have cancelled := coprime.cancel with coprime, divides exact (mod.zero_iff at p, a ^ h + 1 with nonzero).backward with cancelled theorem gauss.lemma (a p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires notDivides : ¬(p ∣ a) shows a ^ h % p = 1 ↔ Even(cardinality(gaussSet(a, p, h))) proof constructor . intro one have parity := parity.exhaustive at cardinality(gaussSet(a, p, h)) cases parity with even | oddCount . exact even . exfalso have zero := gauss.pow_odd with prime, odd, notDivides, oddCount exact euler.pow_not_both with prime, odd, one, zero . intro even exact gauss.pow_even with prime, odd, notDivides, even theorem gauss.residue_iff (a p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires notDivides : ¬(p ∣ a) shows QuadraticResidue(a, p) ↔ Even(cardinality(gaussSet(a, p, h))) proof have criterion := euler.criterion with prime, odd, notDivides have gaussLemma := gauss.lemma with prime, odd, notDivides constructor . intro residue exact gaussLemma.forward with (criterion.forward with residue) . intro even exact criterion.backward with (gaussLemma.backward with even)