Grace/Cumulus/NumberTheory/GaussLemma.g17
Imports
Grace.Cumulus.NumberTheory.GaussCount · Grace.Cumulus.NumberTheory.EulerCriterion · Grace.Cumulus.NumberTheory.CoprimeCancel
Declarations
- factorialList.entries_below theorem derivation
- gaussList.entries theorem derivation
- foldedList.multiplicity_out theorem derivation
- foldedList.multiplicity_in theorem derivation
- foldedList.permutes theorem derivation
- foldedList.product_factorial theorem derivation
- gaussList.product theorem derivation
- gaussList.products theorem derivation
- gauss.pow_even theorem derivation
- gauss.pow_odd theorem derivation
- gauss.lemma theorem derivation
- gauss.residue_iff theorem derivation
Source
1-- Gauss's lemma: for an odd prime p = 2h + 1 and a not divisible by p,2-- a^h ≡ 1 modulo p exactly when the Gauss set of a has even cardinality;3-- so a is a square modulo p exactly when that count is even.4-- Part of Cumulus, the Grace17 mathematics library.5-- Copyright (c) 2026 Grant Passmore.6-- Released under the Apache License 2.0. See LICENSE in the repository root.7-- Authors: Grant Passmore89module Grace.Cumulus.NumberTheory.GaussLemma10import Grace.Cumulus.NumberTheory.GaussCount11import Grace.Cumulus.NumberTheory.EulerCriterion12import Grace.Cumulus.NumberTheory.CoprimeCancel1314-- The entries of the residue list1516-- Every entry of 1..h lies below p = 2h + 1.17theorem factorialList.entries_below (p h : Nat)18 requires odd : p = 2 * h + 119 shows ∀ x, Nat(x) → 0 < multiplicity(x, factorialList(h)) → x < p20proof21 intros x xNat positive22 obtain ⟨lower, upper⟩ := factorialList.member with positive23 arith2425-- Every entry of the residue list a · 1, …, a · h mod p is in 1..p−1: an26-- entry r is below p, and a⁻¹ r mod p is some k in 1..h, so r is not 0.27theorem gaussList.entries (a p h : Nat)28 requires prime : Prime(p)29 requires odd : p = 2 * h + 130 requires notDivides : ¬(p ∣ a)31 shows ∀ r, Nat(r) → 0 < multiplicity(r, gaussList(a, p, h)) →32 0 < r ∧ r < p33proof34 intros r rNat positive35 have nonzero := Prime.nonzero with prime36 rw gaussList.value at positive37 have members := scaledList.member at a, p, factorialList(h) with nonzero38 have rBelow := members with r, rNat, positive39 constructor40 . have coprime := Prime.coprime_of_not_divides_gcd with prime, notDivides41 have inverse := modInverse.product with prime, coprime42 have entries := factorialList.entries_below with odd43 have shift := scaledList.multiplicity at a, modInverse(p, a), p, r,44 factorialList(h) with nonzero, inverse, rBelow45 rw (shift with entries) at positive46 by_contra notPositive47 have zero : r = 0 by arith48 rw zero at positive49 have vanish : modInverse(p, a) * 0 = 0 by ring50 rw [vanish, mod.zero_left] at positive51 obtain ⟨lower, upper⟩ := factorialList.member with positive52 arith53 . exact rBelow5455-- Folding the residues onto 1..h5657-- Entries of a list with entries in 1..p−1 fold into 1..h; an x outside58-- 1..h never occurs in the folded list.59theorem foldedList.multiplicity_out (p h x : Nat) (xs : List(ℕ))60 requires odd : p = 2 * h + 161 requires outside : ¬(0 < x ∧ x ≤ h)62 shows (∀ r, Nat(r) → 0 < multiplicity(r, xs) → 0 < r ∧ r < p) →63 multiplicity(x, foldedList(p, h, xs)) = 064proof65 induction xs66 | List.nil =>67 intro entries68 rw [foldedList.nil, multiplicity.nil]69 rfl70 | List.cons(r, rest) using ih =>71 intro entries72 have rHead : 0 < multiplicity(r, r :: rest)73 rw (multiplicity.cons_self at r, rest with *)74 arith75 have rNat : Nat(r) by typecheck76 obtain ⟨rLower, rUpper⟩ := entries with r, rNat, rHead77 have restEntries : ∀ y, Nat(y) → 0 < multiplicity(y, rest) →78 0 < y ∧ y < p79 intros y yNat yPositive80 have lifted := multiplicity.positive_cons at y, r, rest with yPositive81 exact entries with y, yNat, lifted82 have previous := ih with restEntries83 have foldedRest := foldedList.list at p, h, rest with *84 have split := Less.or_LessEqual at h, r85 cases split with large | small86 . have rLe : r ≤ p by arith87 have pieces := sub.add_cancel with rLe88 have different : x ≠ p - r89 intro same90 rw same at outside91 have lower : 0 < p - r by arith92 have upper : p - r ≤ h by arith93 exact outside with ⟨lower, upper⟩94 rw [foldedList.cons_large,95 (multiplicity.cons_other with different, foldedRest)]96 exact previous97 . have different : x ≠ r98 intro same99 rw same at outside100 exact outside with ⟨rLower, small⟩101 rw [foldedList.cons_small,102 (multiplicity.cons_other with different, foldedRest)]103 exact previous104105-- For x in 1..h, the folded list holds x once for each entry x and once106-- for each entry p − x of the original list.107theorem foldedList.multiplicity_in (p h x : Nat) (xs : List(ℕ))108 requires odd : p = 2 * h + 1109 requires positive : 0 < x110 requires bound : x ≤ h111 shows (∀ r, Nat(r) → 0 < multiplicity(r, xs) → 0 < r ∧ r < p) →112 multiplicity(x, foldedList(p, h, xs)) =113 multiplicity(x, xs) + multiplicity(p - x, xs)114proof115 induction xs116 | List.nil =>117 intro entries118 rw [foldedList.nil, multiplicity.nil, multiplicity.nil]119 ring120 | List.cons(r, rest) using ih =>121 intro entries122 have xLe : x ≤ p by arith123 have complement := sub.add_cancel with xLe124 have rHead : 0 < multiplicity(r, r :: rest)125 rw (multiplicity.cons_self at r, rest with *)126 arith127 have rNat : Nat(r) by typecheck128 obtain ⟨rLower, rUpper⟩ := entries with r, rNat, rHead129 have restEntries : ∀ y, Nat(y) → 0 < multiplicity(y, rest) →130 0 < y ∧ y < p131 intros y yNat yPositive132 have lifted := multiplicity.positive_cons at y, r, rest with yPositive133 exact entries with y, yNat, lifted134 have previous := ih with restEntries135 have foldedRest := foldedList.list at p, h, rest with *136 have split := Less.or_LessEqual at h, r137 cases split with large | small138 . -- A large head r folds to p − r, which is x exactly when r = p − x.139 have rLe : r ≤ p by arith140 have pieces := sub.add_cancel with rLe141 have apart : x ≠ r by arith142 rw [foldedList.cons_large, multiplicity.cons_other with apart]143 have cases := Less.trichotomy at x, p - r with *144 rcases cases with below | same | above145 . have different : x ≠ p - r by arith146 have other : p - x ≠ r by arith147 rw [multiplicity.cons_other with different,148 multiplicity.cons_other with other]149 exact previous150 . have cancel := sub.sub_cancel with rLe151 rw [same, cancel] at previous152 rw [same, cancel, multiplicity.cons_self, multiplicity.cons_self,153 previous]154 ring155 . have different : x ≠ p - r by arith156 have other : p - x ≠ r by arith157 rw [multiplicity.cons_other with different,158 multiplicity.cons_other with other]159 exact previous160 . -- A small head r stays, and is x exactly when r = x.161 have different : p - x ≠ r162 intro same163 have impossible : ¬p - x ≤ h by arith164 rw same at impossible165 exact impossible with small166 rw foldedList.cons_small167 have cases := Less.trichotomy at x, r with *168 rcases cases with below | same | above169 . have apart : x ≠ r by arith170 rw [multiplicity.cons_other with apart,171 multiplicity.cons_other with different]172 exact previous173 . rw ← same at different174 rw [← same, multiplicity.cons_self, multiplicity.cons_self,175 multiplicity.cons_other with different, previous]176 ring177 . have apart : x ≠ r by arith178 rw [multiplicity.cons_other with apart,179 multiplicity.cons_other with different]180 exact previous181182-- The folded residues of a · 1, …, a · h lie in 1..h and are distinct:183-- a k ≡ ±a j forces k ≡ ±j, and 0 < k + j ≤ 2h < p, 0 < k, j ≤ h give184-- k = j. So the folded list is a permutation of 1..h.185theorem foldedList.permutes (a p h : Nat)186 requires prime : Prime(p)187 requires odd : p = 2 * h + 1188 requires notDivides : ¬(p ∣ a)189 shows ∀ x, Nat(x) →190 multiplicity(x, foldedList(p, h, gaussList(a, p, h))) =191 multiplicity(x, factorialList(h))192proof193 intros x xNat194 have nonzero := Prime.nonzero with prime195 have entries := gaussList.entries with prime, odd, notDivides196 have inRange : (0 < x ∧ x ≤ h) ∨ ¬(0 < x ∧ x ≤ h) by tauto197 cases inRange with inside | outside198 . obtain ⟨positive, bound⟩ := inside199 have folded := foldedList.multiplicity_in at p, h, x, gaussList(a, p, h)200 with odd, positive, bound201 rw folded with entries202 -- x and p − x occur in the residue list as often as a⁻¹ x mod p and203 -- its complement occur in 1..h.204 have coprime := Prime.coprime_of_not_divides_gcd with prime, notDivides205 have inverseUnit := modInverse.unit with prime, coprime206 have inverse := modInverse.product with prime, coprime207 have listEntries := factorialList.entries_below with odd208 have xBelow : x < p by arith209 have xLe : x ≤ p by arith210 have complement := sub.add_cancel with xLe211 have complementBelow : p - x < p by arith212 rw gaussList.value213 have shiftX := scaledList.multiplicity at a, modInverse(p, a), p, x,214 factorialList(h) with nonzero, inverse, xBelow215 have shiftComplement := scaledList.multiplicity at a, modInverse(p, a), p,216 p - x, factorialList(h) with nonzero, inverse, complementBelow217 rw [shiftX with listEntries, shiftComplement with listEntries]218 -- a⁻¹ x mod p is not 0, so a⁻¹ (p − x) mod p is its complement, and219 -- exactly one of the two lies in 1..h.220 have residueBelow := mod.less at modInverse(p, a) * x, p with nonzero221 have notMultiple : (modInverse(p, a) * x) % p ≠ 0222 intro zero223 have divides := (mod.zero_iff at p, modInverse(p, a) * x224 with nonzero).forward with zero225 have cancelled := coprime.cancel with inverseUnit, divides226 have xNonzero : x ≠ 0 by arith227 have le := divides.le with cancelled, xNonzero228 arith229 have swap := mod.mul_complement at p, modInverse(p, a), x230 with nonzero, xLe, notMultiple231 rw swap232 have residueLe : (modInverse(p, a) * x) % p ≤ p by arith233 have residueComplement := sub.add_cancel with residueLe234 rw factorialList.multiplicity_in with positive, bound235 have cases := Less.or_LessEqual at h, (modInverse(p, a) * x) % p236 cases cases with large | small237 . have outside : ¬(1 ≤ (modInverse(p, a) * x) % p ∧238 (modInverse(p, a) * x) % p ≤ h)239 intro both240 obtain ⟨first, second⟩ := both241 arith242 have lower : 1 ≤ p - (modInverse(p, a) * x) % p by arith243 have upper : p - (modInverse(p, a) * x) % p ≤ h by arith244 rw [factorialList.multiplicity_out with outside,245 (factorialList.multiplicity_in with lower, upper)]246 ring247 . have lower : 1 ≤ (modInverse(p, a) * x) % p by arith248 have outside : ¬(1 ≤ p - (modInverse(p, a) * x) % p ∧249 p - (modInverse(p, a) * x) % p ≤ h)250 intro both251 obtain ⟨first, second⟩ := both252 arith253 rw [(factorialList.multiplicity_in with lower, small),254 factorialList.multiplicity_out with outside]255 ring256 . have folded := foldedList.multiplicity_out at p, h, x, gaussList(a, p, h)257 with odd, outside258 have outsideOne : ¬(1 ≤ x ∧ x ≤ h)259 intro both260 obtain ⟨first, second⟩ := both261 have positive : 0 < x by arith262 exact outside with ⟨positive, second⟩263 rw [folded with entries, factorialList.multiplicity_out with outsideOne]264 rfl265266theorem foldedList.product_factorial (a p h : Nat)267 requires prime : Prime(p)268 requires odd : p = 2 * h + 1269 requires notDivides : ¬(p ∣ a)270 shows ∏ foldedList(p, h, gaussList(a, p, h)) = h!271proof272 have permutes := foldedList.permutes with prime, odd, notDivides273 have list := factorialList.list at h274 have same := listProduct.of_multiplicities275 at foldedList(p, h, gaussList(a, p, h)) with list, permutes276 rw [same, factorialList.product]277 rfl278279-- The two products280281-- The residue list has product a^h h! modulo p.282theorem gaussList.product (a p h : Nat)283 requires prime : Prime(p)284 shows (∏ gaussList(a, p, h)) % p = (a ^ h * h!) % p285proof286 have nonzero := Prime.nonzero with prime287 have product := scaledList.product at a, p, factorialList(h) with nonzero288 rw [gaussList.value, product, factorialList.length, factorialList.product]289 rfl290291-- The two products agree with a^h h! and h! modulo p, the residue list292-- has entries below p, and its large count is the Gauss set's cardinality.293theorem gaussList.products (a p h : Nat)294 requires prime : Prime(p)295 requires odd : p = 2 * h + 1296 requires notDivides : ¬(p ∣ a)297 shows (∏ gaussList(a, p, h)) % p = (a ^ h * h!) % p ∧298 ∏ foldedList(p, h, gaussList(a, p, h)) = h! ∧299 (∀ r, Nat(r) → 0 < multiplicity(r, gaussList(a, p, h)) → r < p) ∧300 largeCount(h, gaussList(a, p, h)) = cardinality(gaussSet(a, p, h))301proof302 have entries := gaussList.entries with prime, odd, notDivides303 have belowP : ∀ r, Nat(r) → 0 < multiplicity(r, gaussList(a, p, h)) →304 r < p305 intros r rNat positive306 exact (entries with r, rNat, positive).right307 have left := gaussList.product at a, p, h with prime308 have right := foldedList.product_factorial with prime, odd, notDivides309 have count := largeCount.gaussSet at a, p, h310 exact ⟨left, right, belowP, count⟩311312-- Gauss's lemma313314-- a^h h! ≡ ±h! with the sign of the large count; cancel h!.315theorem gauss.pow_even (a p h : Nat)316 requires prime : Prime(p)317 requires odd : p = 2 * h + 1318 requires notDivides : ¬(p ∣ a)319 requires even : Even(cardinality(gaussSet(a, p, h)))320 shows a ^ h % p = 1321proof322 have nonzero := Prime.nonzero with prime323 have aboveOne := Prime.greater_than_one with prime324 obtain ⟨left, right, belowP, count⟩ :=325 gaussList.products with prime, odd, notDivides326 rw ← count at even327 have agree := foldedList.product_even with belowP, even328 rw [left, right] at agree329 have hBelow : h < p by arith330 have coprime := factorial.coprime_prime with prime, hBelow331 have same : (h! * a ^ h) % p = (h! * 1) % p332 have commute : h! * a ^ h = a ^ h * h! by ring333 have unit : h! * 1 = h! by ring334 rw [commute, unit]335 exact agree336 have cancelled := coprime.mul_cancel_mod with nonzero, coprime, same337 have oneSelf := mod.of_less at p, 1 with aboveOne338 rwa oneSelf at cancelled339340theorem gauss.pow_odd (a p h : Nat)341 requires prime : Prime(p)342 requires odd : p = 2 * h + 1343 requires notDivides : ¬(p ∣ a)344 requires oddCount : Odd(cardinality(gaussSet(a, p, h)))345 shows (a ^ h + 1) % p = 0346proof347 have nonzero := Prime.nonzero with prime348 obtain ⟨left, right, belowP, count⟩ :=349 gaussList.products with prime, odd, notDivides350 rw ← count at oddCount351 have sum := foldedList.product_odd with belowP, oddCount352 have split := mod.add at p, ∏ gaussList(a, p, h),353 ∏ foldedList(p, h, gaussList(a, p, h)) with nonzero354 rw [split, left, right] at sum355 have merge := mod.add at p, a ^ h * h!, h! with nonzero356 rw ← merge at sum357 have factor : a ^ h * h! + h! = h! * (a ^ h + 1) by ring358 rw factor at sum359 have divides := (mod.zero_iff at p, h! * (a ^ h + 1) with nonzero).forward360 with sum361 have hBelow : h < p by arith362 have coprime := factorial.coprime_prime with prime, hBelow363 have cancelled := coprime.cancel with coprime, divides364 exact (mod.zero_iff at p, a ^ h + 1 with nonzero).backward with cancelled365366theorem gauss.lemma (a p h : Nat)367 requires prime : Prime(p)368 requires odd : p = 2 * h + 1369 requires notDivides : ¬(p ∣ a)370 shows a ^ h % p = 1 ↔ Even(cardinality(gaussSet(a, p, h)))371proof372 constructor373 . intro one374 have parity := parity.exhaustive at cardinality(gaussSet(a, p, h))375 cases parity with even | oddCount376 . exact even377 . exfalso378 have zero := gauss.pow_odd with prime, odd, notDivides, oddCount379 exact euler.pow_not_both with prime, odd, one, zero380 . intro even381 exact gauss.pow_even with prime, odd, notDivides, even382383theorem gauss.residue_iff (a p h : Nat)384 requires prime : Prime(p)385 requires odd : p = 2 * h + 1386 requires notDivides : ¬(p ∣ a)387 shows QuadraticResidue(a, p) ↔ Even(cardinality(gaussSet(a, p, h)))388proof389 have criterion := euler.criterion with prime, odd, notDivides390 have gaussLemma := gauss.lemma with prime, odd, notDivides391 constructor392 . intro residue393 exact gaussLemma.forward with (criterion.forward with residue)394 . intro even395 exact criterion.backward with (gaussLemma.backward with even)396