Grace/Cumulus/NumberTheory/EulerCriterion.g17
Imports
Grace.Cumulus.NumberTheory.Wilson · Grace.Cumulus.NumberTheory.ModularMore · Grace.Cumulus.NumberTheory.CoprimeCancel
Declarations
- QuadraticResidue predicate
- QuadraticResidue.intro theorem derivation
- QuadraticResidue.mod_iff theorem derivation
- QuadraticResidue.of_congruent theorem derivation
- QuadraticResidue.mul theorem derivation
- euler.residue_pow theorem derivation
- pairPartner def
- pairPartner.value theorem derivation
- pairPartner.below theorem derivation
- pairPartner.product theorem derivation
- pairPartner.involution theorem derivation
- PairedBy predicate
- PairedBy.product theorem derivation
- factorialList.pairedBy theorem derivation
- euler.nonresidue_pow theorem derivation
- euler.pow_not_both theorem derivation
- euler.criterion theorem derivation
- euler.nonresidue_iff theorem derivation
- negOne.even_pow theorem derivation
- negOne.residue_iff theorem derivation
- negOne.square theorem derivation
Source
1-- Quadratic residues modulo a prime and Euler's criterion by the Wilson2-- pairing: for an odd prime p = 2h + 1 and a not divisible by p, a is a3-- square modulo p exactly when a^h ≡ 1, and otherwise a^h ≡ −1; the first4-- supplement, −1 is a square modulo p exactly when h is even.5-- Part of Cumulus, the Grace17 mathematics library.6-- Copyright (c) 2026 Grant Passmore.7-- Released under the Apache License 2.0. See LICENSE in the repository root.8-- Authors: Grant Passmore910module Grace.Cumulus.NumberTheory.EulerCriterion11import Grace.Cumulus.NumberTheory.Wilson12import Grace.Cumulus.NumberTheory.ModularMore13import Grace.Cumulus.NumberTheory.CoprimeCancel1415-- Quadratic residues1617-- a is a square modulo p when some natural x has x² ≡ a (mod p).18predicate QuadraticResidue (a p : Nat) :=19 ∃ x, Nat(x) ∧ CongruentMod(x * x, a, p)2021theorem QuadraticResidue.intro (a p x : Nat)22 requires square : CongruentMod(x * x, a, p)23 shows QuadraticResidue(a, p)24proof25 witness x26 exact square2728theorem QuadraticResidue.mod_iff (a p : Nat)29 shows QuadraticResidue(a % p, p) ↔ QuadraticResidue(a, p)30proof31 constructor32 . intro residue33 obtain ⟨x, xNat, square⟩ := residue34 have back := CongruentMod.mod_left at a, p35 have chain := CongruentMod.transitive with square, back36 exact QuadraticResidue.intro with chain37 . intro residue38 obtain ⟨x, xNat, square⟩ := residue39 have forward := CongruentMod.mod_right at a, p40 have chain := CongruentMod.transitive with square, forward41 exact QuadraticResidue.intro with chain4243theorem QuadraticResidue.of_congruent (a b p : Nat)44 requires congruent : CongruentMod(a, b, p)45 requires residue : QuadraticResidue(a, p)46 shows QuadraticResidue(b, p)47proof48 obtain ⟨x, xNat, square⟩ := residue49 have chain := CongruentMod.transitive with square, congruent50 exact QuadraticResidue.intro with chain5152theorem QuadraticResidue.mul (a b p : Nat)53 requires first : QuadraticResidue(a, p)54 requires second : QuadraticResidue(b, p)55 shows QuadraticResidue(a * b, p)56proof57 obtain ⟨x, xNat, squareX⟩ := first58 obtain ⟨y, yNat, squareY⟩ := second59 have product := CongruentMod.mul with squareX, squareY60 have rearrange : (x * y) * (x * y) = (x * x) * (y * y) by ring61 rw ← rearrange at product62 exact QuadraticResidue.intro with product6364-- A square has a^h ≡ 16566-- A root x of x² ≡ a is coprime to p, so a^h ≡ x^(2h) = x^(p−1) ≡ 1 by67-- Fermat.68theorem euler.residue_pow (a p h : Nat)69 requires prime : Prime(p)70 requires odd : p = 2 * h + 171 requires notDivides : ¬(p ∣ a)72 requires residue : QuadraticResidue(a, p)73 shows a ^ h % p = 174proof75 obtain ⟨x, xNat, square⟩ := residue76 have nonzero := Prime.nonzero with prime77 have xNotDivides : ¬(p ∣ x)78 intro divides79 apply notDivides80 have dividesSquare := divides.multiply_right at p, x, x with divides81 have zero := mod.of_divides with dividesSquare82 unfold CongruentMod at square83 rw zero at square84 symm at square85 exact (mod.zero_iff at p, a with nonzero).forward with square86 have fermat := fermat.little with prime, xNotDivides87 have exponent : p - 1 = 2 * h88 rw odd89 exact sub.add_one_right at 2 * h90 rw exponent at fermat91 have squareForm : (x * x) ^ h = x ^ (2 * h)92 rw [(pow.mul_base at x, x, h), (pow.double_exponent at x, h),93 (pow.two at x ^ h)]94 rfl95 have powers := CongruentMod.pow at x * x, a, h, p with square96 unfold CongruentMod at powers97 rw [squareForm, fermat] at powers98 symm at powers99 exact powers100101-- The pairing x ↦ a · x⁻¹ mod p102103-- The partner of x under a: a · x⁻¹ mod p.104def pairPartner (p a x : Nat) : Nat := mod(mul(a, modInverse(p, x)), p)105proof106 unfold pairPartner107 typecheck108109theorem pairPartner.value (p a x : Nat)110 shows pairPartner(p, a, x) = mul(a, modInverse(p, x)) % p111proof112 unfold pairPartner113 rfl114115theorem pairPartner.below (p a x : Nat)116 requires prime : Prime(p)117 shows pairPartner(p, a, x) < p118proof119 have nonzero := Prime.nonzero with prime120 rw pairPartner.value121 exact mod.less at mul(a, modInverse(p, x)), p with nonzero122123-- x times its partner is a modulo p when x is a unit.124theorem pairPartner.product (p a x : Nat)125 requires prime : Prime(p)126 requires unit : gcd(x, p) = 1127 shows (x * pairPartner(p, a, x)) % p = a % p128proof129 have nonzero := Prime.nonzero with prime130 have inverse := modInverse.product with prime, unit131 rw pairPartner.value132 have absorb := mod.mul_right at p, x, a * modInverse(p, x) with nonzero133 have rearrange : x * (a * modInverse(p, x)) = a * (x * modInverse(p, x))134 ring135 have split := mod.mul at p, a, x * modInverse(p, x) with nonzero136 have one : a % p * 1 = a % p by ring137 have idempotent := mod.mod at p, a with nonzero138 rw [absorb, rearrange, split, inverse, one, idempotent]139 rfl140141-- The partner of the partner is x: both are units, and x · y ≡ a ≡ y · z142-- cancels y.143theorem pairPartner.involution (p a x : Nat)144 requires prime : Prime(p)145 requires coprimeA : gcd(a, p) = 1146 requires positive : 0 < x147 requires below : x < p148 shows pairPartner(p, a, pairPartner(p, a, x)) = x149proof150 have nonzero := Prime.nonzero with prime151 have unit := prime.coprime_below with prime, positive, below152 have inverseUnit := modInverse.unit with prime, unit153 have partnerUnit : gcd(pairPartner(p, a, x), p) = 1154 rw pairPartner.value155 exact coprime.mul_mod with nonzero, coprimeA, inverseUnit156 have first := pairPartner.product at p, a, x with prime, unit157 have commute : x * pairPartner(p, a, x) = pairPartner(p, a, x) * x by ring158 rw commute at first159 have second := pairPartner.product at p, a, pairPartner(p, a, x)160 with prime, partnerUnit161 rw ← first at second162 have cancelled := coprime.mul_cancel_mod with nonzero, partnerUnit, second163 have secondBelow := pairPartner.below at p, a, pairPartner(p, a, x)164 with prime165 rwa [mod.of_less with secondBelow, mod.of_less with below] at cancelled166167-- Paired lists have product a^k168169-- A list is paired by a when every entry x occurs once, lies in 1..p−1,170-- differs from its partner, and the partner is an entry too.171predicate PairedBy (p a : Nat, xs : List(ℕ)) :=172 ∀ x, Nat(x) → 0 < multiplicity(x, xs) →173 (multiplicity(x, xs) = 1 ∧ 0 < x ∧ x < p ∧174 pairPartner(p, a, x) ≠ x ∧ 0 < multiplicity(pairPartner(p, a, x), xs))175176-- A paired list of length 2k has product a^k modulo p: remove an entry and177-- its partner, whose product is a modulo p, and induct on k.178theorem PairedBy.product (p a k : Nat)179 requires prime : Prime(p)180 requires coprimeA : gcd(a, p) = 1181 shows ∀ xs, List(ℕ, xs) → length{ℕ}(xs) = 2 * k → PairedBy(p, a, xs) →182 (∏ xs) % p = a ^ k % p183proof184 induction k185 | zero =>186 intros xs xsList lengthXs paired187 have shape := List.cases with xsList188 cases shape with nil | cons189 . rw [nil, listProduct.nil, pow.zero]190 rfl191 . exfalso192 obtain ⟨x, xIn, rest, restList, equal⟩ := cons193 rw [equal, (length.cons at ℕ, x, rest with *)] at lengthXs194 arith195 | j + 1 using ih =>196 intros xs xsList lengthXs paired197 have nonzero := Prime.nonzero with prime198 have shape := List.cases with xsList199 cases shape with nil | cons200 . exfalso201 rw [nil, (length.nil at ℕ)] at lengthXs202 arith203 . obtain ⟨x, xIn, rest, restList, equal⟩ := cons204 have xNat : Nat(x) by typecheck205 rw equal at lengthXs, paired206 rw equal207 unfold PairedBy at paired208 -- The head x occurs once, lies in 1..p−1, and its partner y is209 -- another entry, so y occurs in the rest.210 have xHead : 0 < multiplicity(x, x :: rest)211 rw (multiplicity.cons_self at x, rest with *)212 arith213 obtain ⟨once, xPos, xBelow, notFixed, partnerIn⟩ :=214 paired with x, xNat, xHead215 have unit := prime.coprime_below with prime, xPos, xBelow216 have product := pairPartner.product at p, a, x with prime, unit217 have involution := pairPartner.involution at p, a, x218 with prime, coprimeA, xPos, xBelow219 have restZero : multiplicity(x, rest) = 0220 rw (multiplicity.cons_self at x, rest with *) at once221 arith222 have yInRest : 0 < multiplicity(pairPartner(p, a, x), rest)223 rwa (multiplicity.cons_other at pairPartner(p, a, x), x, rest224 with notFixed) at partnerIn225 -- Removing y from the rest leaves a list zs of length 2j.226 obtain ⟨zs, zsList, productRest, lengthRest, counts⟩ :=227 multiplicity.remove_length at pairPartner(p, a, x), rest with *, yInRest228 have lengthZs : length{ℕ}(zs) = 2 * j229 unfold OneShorter at lengthRest230 rw [(length.cons at ℕ, x, rest with *), lengthRest] at lengthXs231 arith232 have yOnce : multiplicity(pairPartner(p, a, x), x :: rest) = 1233 have propsY := paired with pairPartner(p, a, x), partnerIn234 exact propsY.left235 have yZero : multiplicity(pairPartner(p, a, x), zs) = 0236 rw [(multiplicity.cons_other at pairPartner(p, a, x), x, rest237 with notFixed), (counts with pairPartner(p, a, x)),238 (multiplicity.cons_self at pairPartner(p, a, x), zs with *)] at yOnce239 arith240 -- zs is still paired: an entry q of zs is an entry of xs other than241 -- x and y, so its partner r is an entry other than x and y as well.242 have pairedZs : PairedBy(p, a, zs)243 unfold PairedBy244 intros q qNat qPositive245 have countQ := counts with q, qNat246 have consQ := multiplicity.cons at q, pairPartner(p, a, x), zs with *247 have qInRest : 0 < multiplicity(q, rest)248 rw [countQ, consQ]249 arith250 have qInXs := multiplicity.positive_cons at q, x, rest with qInRest251 obtain ⟨onceQ, qPos, qBelow, qNotFixed, qPartnerIn⟩ :=252 paired with q, qNat, qInXs253 have qInvolution := pairPartner.involution at p, a, q254 with prime, coprimeA, qPos, qBelow255 have qNotX : q ≠ x256 intro same257 rw [same, restZero] at qInRest258 arith259 have qNotY : q ≠ pairPartner(p, a, x)260 intro same261 rw [same, yZero] at qPositive262 arith263 have onceZs : multiplicity(q, zs) = 1264 rw [(multiplicity.cons_other at q, x, rest with qNotX), countQ,265 consQ, (equalNat.false_iff.backward with qNotY)] at onceQ266 have weightZero : chooseNat(Bool.false, 1, 0) = 0 by compute267 rw weightZero at onceQ268 arith269 have rNotX : pairPartner(p, a, q) ≠ x270 intro same271 apply qNotY272 rw [← qInvolution, same]273 rfl274 have rNotY : pairPartner(p, a, q) ≠ pairPartner(p, a, x)275 intro same276 apply qNotX277 rw [← qInvolution, same, involution]278 rfl279 have rInZs : 0 < multiplicity(pairPartner(p, a, q), zs)280 rwa [(multiplicity.cons_other at pairPartner(p, a, q), x, rest281 with rNotX), (counts with pairPartner(p, a, q)),282 (multiplicity.cons_other at pairPartner(p, a, q),283 pairPartner(p, a, x), zs with rNotY)] at qPartnerIn284 exact ⟨onceZs, qPos, qBelow, qNotFixed, rInZs⟩285 -- The product of xs is x · y · ∏ zs ≡ a · a^j.286 have previous := ih with zs, zsList, lengthZs, pairedZs287 rw (listProduct.cons at pairPartner(p, a, x), zs with *) at productRest288 have rearrange := mul.associative_reverse289 at pairPartner(p, a, x), ∏ zs, x with *290 have split := mod.mul at p, x * pairPartner(p, a, x), ∏ zs with nonzero291 have powerMod := mod.mul at p, a, a ^ j with nonzero292 rw [(listProduct.cons at x, rest with *), productRest, rearrange, split,293 product, previous, (pow.add_one at a, j), powerMod]294 rfl295296-- A nonsquare has a^h ≡ −1297298-- When a is not a square modulo p, the list 2h, …, 1 is paired by a: no299-- entry is its own partner, and the partner of an entry is an entry.300theorem factorialList.pairedBy (a p h : Nat)301 requires prime : Prime(p)302 requires odd : p = 2 * h + 1303 requires notDivides : ¬(p ∣ a)304 requires nonresidue : ¬QuadraticResidue(a, p)305 shows PairedBy(p, a, factorialList(2 * h))306proof307 have nonzero := Prime.nonzero with prime308 unfold PairedBy309 intros x xNat positive310 obtain ⟨lower, upper⟩ := factorialList.member at 2 * h, x with positive311 have xPos : 0 < x by arith312 have xBelow : x < p by arith313 have unit := prime.coprime_below with prime, xPos, xBelow314 have yBelow := pairPartner.below at p, a, x with prime315 have product := pairPartner.product at p, a, x with prime, unit316 -- A fixed point x would be a root of x² ≡ a.317 have notFixed : pairPartner(p, a, x) ≠ x318 intro fixed319 apply nonresidue320 rw fixed at product321 exact QuadraticResidue.intro with product322 -- The partner is not 0, since p ∤ a.323 have yPos : 0 < pairPartner(p, a, x)324 by_contra notPositive325 apply notDivides326 have zero : pairPartner(p, a, x) = 0 by arith327 rw zero at product328 have vanish : x * 0 = 0 by ring329 rw [vanish, mod.zero_left] at product330 symm at product331 exact (mod.zero_iff at p, a with nonzero).forward with product332 have yLower : 1 ≤ pairPartner(p, a, x) by arith333 have yUpper : pairPartner(p, a, x) ≤ 2 * h by arith334 have once := factorialList.multiplicity_in with lower, upper335 have yIn := factorialList.multiplicity_in with yLower, yUpper336 have yPositive : 0 < multiplicity(pairPartner(p, a, x), factorialList(2 * h))337 rw yIn338 arith339 exact ⟨once, xPos, xBelow, notFixed, yPositive⟩340341-- The product 1 · 2 ⋯ (p − 1) is a^h by the pairing and −1 by Wilson.342theorem euler.nonresidue_pow (a p h : Nat)343 requires prime : Prime(p)344 requires odd : p = 2 * h + 1345 requires notDivides : ¬(p ∣ a)346 requires nonresidue : ¬QuadraticResidue(a, p)347 shows (a ^ h + 1) % p = 0348proof349 have nonzero := Prime.nonzero with prime350 have coprimeA := Prime.coprime_of_not_divides_gcd with prime, notDivides351 have paired := factorialList.pairedBy with prime, odd, notDivides, nonresidue352 have listFact := factorialList.list at 2 * h353 have lengthFact := factorialList.length at 2 * h354 have products := PairedBy.product at p, a, h with prime, coprimeA355 have productMod := products with factorialList(2 * h), listFact, lengthFact,356 paired357 have wilson := wilson.theorem with prime358 have predecessor : p - 1 = 2 * h359 rw odd360 exact sub.add_one_right at 2 * h361 rw [predecessor, ← (factorialList.product at 2 * h)] at wilson362 have left := mod.add at p, a ^ h, 1 with nonzero363 have right := mod.add at p, ∏ factorialList(2 * h), 1 with nonzero364 rw [left, ← productMod, ← right]365 exact wilson366367-- Euler's criterion368369-- For an odd prime, a^h ≡ 1 and a^h ≡ −1 cannot both hold: p would370-- divide 2.371theorem euler.pow_not_both (a p h : Nat)372 requires prime : Prime(p)373 requires odd : p = 2 * h + 1374 requires one : a ^ h % p = 1375 shows (a ^ h + 1) % p ≠ 0376proof377 intro zero378 have nonzero := Prime.nonzero with prime379 have aboveOne := Prime.greater_than_one with prime380 obtain ⟨q, qNat, equation⟩ := mod.equation at a ^ h, p with nonzero381 rw one at equation382 have form : a ^ h + 1 = p * q + 2383 rw equation384 ring385 have dividesSum := (mod.zero_iff at p, a ^ h + 1 with nonzero).forward386 with zero387 rw form at dividesSum388 have dividesMultiple := divides.mul_left_self at p, q389 have dividesTwo := divides.add_cancel_left with dividesMultiple, dividesSum390 have twoNonzero : (2:N) ≠ 0 by arith391 have le := divides.le with dividesTwo, twoNonzero392 arith393394theorem euler.criterion (a p h : Nat)395 requires prime : Prime(p)396 requires odd : p = 2 * h + 1397 requires notDivides : ¬(p ∣ a)398 shows QuadraticResidue(a, p) ↔ a ^ h % p = 1399proof400 constructor401 . intro residue402 exact euler.residue_pow with prime, odd, notDivides, residue403 . intro one404 by_contra nonresidue405 have zero := euler.nonresidue_pow with prime, odd, notDivides, nonresidue406 exact euler.pow_not_both with prime, odd, one, zero407408theorem euler.nonresidue_iff (a p h : Nat)409 requires prime : Prime(p)410 requires odd : p = 2 * h + 1411 requires notDivides : ¬(p ∣ a)412 shows ¬QuadraticResidue(a, p) ↔ (a ^ h + 1) % p = 0413proof414 constructor415 . intro nonresidue416 exact euler.nonresidue_pow with prime, odd, notDivides, nonresidue417 . intros zero residue418 have one := euler.residue_pow with prime, odd, notDivides, residue419 exact euler.pow_not_both with prime, odd, one, zero420421-- The first supplement422423-- (2h)² is 1 modulo p = 2h + 1, so every even power of 2h is 1.424theorem negOne.even_pow (p h m : Nat)425 requires prime : Prime(p)426 requires odd : p = 2 * h + 1427 shows (2 * h) ^ (2 * m) % p = 1428proof429 have nonzero := Prime.nonzero with prime430 have aboveOne := Prime.greater_than_one with prime431 have hShape := natural.zero_or_successor at h432 cases hShape with zero | successor433 . exfalso434 arith435 . obtain ⟨n, nNat, form⟩ := successor436 have squareValue : (2 * h) * (2 * h) = p * (2 * n + 1) + 1437 rw [odd, form]438 ring439 have square := mod.unique with nonzero, squareValue, aboveOne440 have expand : (2 * h) ^ (2 * m) = ((2 * h) * (2 * h)) ^ m441 rw [(pow.double_exponent at 2 * h, m), (pow.two at (2 * h) ^ m),442 (pow.mul_base at 2 * h, 2 * h, m)]443 rfl444 rw [expand, (mod.pow at p, (2 * h) * (2 * h), m with nonzero), square,445 (pow.one_left at m)]446 exact mod.of_less with aboveOne447448-- 2h is −1 modulo p = 2h + 1, and (2h)^h ≡ (−1)^h is 1 exactly when h is449-- even.450theorem negOne.residue_iff (p h : Nat)451 requires prime : Prime(p)452 requires odd : p = 2 * h + 1453 shows QuadraticResidue(2 * h, p) ↔ Even(h)454proof455 have nonzero := Prime.nonzero with prime456 have aboveOne := Prime.greater_than_one with prime457 have notDivides : ¬(p ∣ 2 * h)458 intro divides459 have doubleNonzero : 2 * h ≠ 0 by arith460 have le := divides.le with divides, doubleNonzero461 arith462 have criterion := euler.criterion at 2 * h, p, h with prime, odd, notDivides463 have doubleBelow : 2 * h < p by arith464 have doubleSelf := mod.of_less with doubleBelow465 have parity := Even.iff_not_odd at h466 constructor467 . intro residue468 have power := criterion.forward with residue469 apply parity.backward470 intro oddH471 obtain ⟨m, mNat, form⟩ := oddH472 have exponent : h = 2 * m + 1473 rw form474 ring475 have step : (2 * h) ^ h = (2 * h) * (2 * h) ^ (2 * m)476 have expand := pow.add_one at 2 * h, 2 * m477 rw ← exponent at expand478 exact expand479 have evenPower := negOne.even_pow at p, h, m with prime, odd480 have split := mod.mul_right at p, 2 * h, (2 * h) ^ (2 * m) with nonzero481 rw [step, ← split, evenPower] at power482 have one : 2 * h * 1 = 2 * h by ring483 rw [one, doubleSelf] at power484 arith485 . intro evenH486 apply criterion.backward487 obtain ⟨m, mNat, form⟩ := (Even.iff_two_mul at h).forward with evenH488 have evenPower := negOne.even_pow at p, h, m with prime, odd489 rwa ← form at evenPower490491-- The usable form for two squares: a root of x² + 1 ≡ 0 when h is even.492theorem negOne.square (p h : Nat)493 requires prime : Prime(p)494 requires odd : p = 2 * h + 1495 requires even : Even(h)496 shows ∃ a, Nat(a) ∧ (a * a + 1) % p = 0497proof498 have nonzero := Prime.nonzero with prime499 have aboveOne := Prime.greater_than_one with prime500 have characterization := negOne.residue_iff with prime, odd501 obtain ⟨x, xNat, square⟩ := characterization.backward with even502 witness x503 unfold CongruentMod at square504 have doubleBelow : 2 * h < p by arith505 rw [(mod.add at p, x * x, 1 with nonzero), square,506 (mod.of_less with doubleBelow), (mod.of_less with aboveOne), ← odd]507 exact mod.self at p508