Grace/Cumulus/NumberTheory/FourSquaresModPrime.g17
Imports
Grace.Cumulus.Cardinal · Grace.Cumulus.NumberTheory.ModularMore · Grace.Cumulus.Combinatorics.Pigeonhole
Declarations
- squareResidues defgraph
- negSquareResidues defgraph
- squareResidues.le_double theorem derivation
- negSquareResidues.fold theorem derivation
- squareResidues.eq_of_le theorem derivation
- squareResidues.injective theorem derivation
- squareResidues.injective_between theorem derivation
- negSquareResidues.injective theorem derivation
- negSquareResidues.injective_between theorem derivation
- squareResidues.image_count theorem derivation
- negSquareResidues.image_count theorem derivation
- residues.meet theorem derivation
- Prime.multiple_below theorem derivation
- Prime.multiple_sum_squares_one theorem derivation
Source
1-- For an odd prime p = 2h + 1, some multiple m p with 0 < m < p is2-- a² + b² + 1: the h + 1 residues x² and the h + 1 residues −1 − y² for3-- x, y ≤ h cannot all be distinct among p residues.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.FourSquaresModPrime10import Grace.Cumulus.Cardinal11import Grace.Cumulus.NumberTheory.ModularMore12import Grace.Cumulus.Combinatorics.Pigeonhole1314-- The two residue maps on 0..h1516-- x ↦ x² mod p on 0..h, and y ↦ (2h − y² mod p) mod p, which is17-- −1 − y² modulo p = 2h + 1.18defgraph squareResidues (p h : Nat) (x ∈ succ(h)) : p := mod(mul(x, x), p)19requires positive : 0 < p20proof21 have xNat : Nat(x) := natural.member at succ(h) with member22 have nonzero : p ≠ 0 by arith23 have below := mod.less at x * x, p with nonzero24 have bridge := membership.of_Less at x * x % p, p with *25 exact bridge with below2627defgraph negSquareResidues (p h : Nat) (y ∈ succ(h)) : p :=28 mod(sub(add(h, h), mod(mul(y, y), p)), p)29requires positive : 0 < p30proof31 have yNat : Nat(y) := natural.member at succ(h) with member32 have nonzero : p ≠ 0 by arith33 have below := mod.less at (h + h) - (y * y) % p, p with nonzero34 have bridge := membership.of_Less at ((h + h) - (y * y) % p) % p, p with *35 exact bridge with below3637-- Every residue is at most 2h, so 2h − r is again below p and the outer38-- mod of negSquareResidues is the identity.39theorem squareResidues.le_double (p h y : Nat)40 requires odd : p = 2 * h + 141 shows (y * y) % p ≤ h + h42proof43 have nonzero : p ≠ 0 by arith44 have below := mod.less at y * y, p with nonzero45 arith4647theorem negSquareResidues.fold (p h y : Nat)48 requires odd : p = 2 * h + 149 shows ((h + h) - (y * y) % p) % p = (h + h) - (y * y) % p50proof51 have le := sub.le_self at h + h, (y * y) % p52 have small : (h + h) - (y * y) % p < p by arith53 exact mod.of_less at p, (h + h) - (y * y) % p with small5455-- Both maps are injective5657-- With y ≤ x, x² = y² + d (x + y) for d = x − y, so p ∣ d (x + y); p is58-- prime and both factors are below p, so the divisible one is zero.59theorem squareResidues.eq_of_le (p h x y : Nat)60 requires prime : Prime(p)61 requires odd : p = 2 * h + 162 requires xBound : x ≤ h63 requires yBound : y ≤ h64 requires le : y ≤ x65 requires same : (x * x) % p = (y * y) % p66 shows x = y67proof68 obtain ⟨d, dNat, split⟩ := le69 have expand : x * x = y * y + d * (x + y)70 rw ← split71 ring72 rw expand at same73 have divides := (mod.add_eq_iff at p, y * y, d * (x + y)).forward with same74 have factor := Prime.divides_mul with prime, divides75 have xy : x ≤ y76 cases factor with dCase | sumCase77 . have dZero : d = 078 by_contra79 have bound := divides.le with dCase, this80 arith81 arith82 . have sumZero : x + y = 083 by_contra84 have bound := divides.le with sumCase, this85 arith86 arith87 exact LessEqual.antisymmetric with xy, le8889-- x² ≡ y² with x, y ≤ h gives p ∣ (x − y)(x + y) and x + y ≤ 2h < p,90-- in either order of x and y.91theorem squareResidues.injective (p h x y : Nat)92 requires prime : Prime(p)93 requires odd : p = 2 * h + 194 requires xBound : x ≤ h95 requires yBound : y ≤ h96 requires same : (x * x) % p = (y * y) % p97 shows x = y98proof99 have total := LessEqual.total at x, y100 cases total with xy | yx101 . have flipped : (y * y) % p = (x * x) % p by cc102 symm103 exact squareResidues.eq_of_le at p, h, y, x104 with prime, odd, yBound, xBound, xy, flipped105 . exact squareResidues.eq_of_le with prime, odd, xBound, yBound, yx, same106107theorem squareResidues.injective_between (p h : Nat)108 requires prime : Prime(p)109 requires odd : p = 2 * h + 1110 shows InjectiveBetween(squareResidues(p, h), succ(h), p)111proof112 have positive : 0 < p by arith113 have function := squareResidues.function at p, h with positive114 refine ⟨function, _⟩115 intros x hx y hy z hz relatesX relatesY116 have xNat : Nat(x) := natural.member at succ(h) with hx117 have yNat : Nat(y) := natural.member at succ(h) with hy118 have valueX := FunctionBetween.apply_value with function, hx, relatesX119 have valueY := FunctionBetween.apply_value with function, hy, relatesY120 have squareX := squareResidues.value at p, h, x with positive, hx121 have squareY := squareResidues.value at p, h, y with positive, hy122 have same : (x * x) % p = (y * y) % p by cc123 have xBound : x ≤ h :=124 Less.successor_upper with (Less.of_membership with hx)125 have yBound : y ≤ h :=126 Less.successor_upper with (Less.of_membership with hy)127 exact squareResidues.injective with prime, odd, xBound, yBound, same128129-- 2h − r determines the residue r, and r determines y.130theorem negSquareResidues.injective (p h x y : Nat)131 requires prime : Prime(p)132 requires odd : p = 2 * h + 1133 requires xBound : x ≤ h134 requires yBound : y ≤ h135 requires same : ((h + h) - (x * x) % p) % p = ((h + h) - (y * y) % p) % p136 shows x = y137proof138 have foldX := negSquareResidues.fold at p, h, x with odd139 have foldY := negSquareResidues.fold at p, h, y with odd140 rw [foldX, foldY] at same141 have rxLe := squareResidues.le_double at p, h, x with odd142 have ryLe := squareResidues.le_double at p, h, y with odd143 have backX := sub.sub_cancel at h + h, (x * x) % p with rxLe144 have backY := sub.sub_cancel at h + h, (y * y) % p with ryLe145 have residues : (x * x) % p = (y * y) % p by cc146 exact squareResidues.injective with prime, odd, xBound, yBound, residues147148theorem negSquareResidues.injective_between (p h : Nat)149 requires prime : Prime(p)150 requires odd : p = 2 * h + 1151 shows InjectiveBetween(negSquareResidues(p, h), succ(h), p)152proof153 have positive : 0 < p by arith154 have function := negSquareResidues.function at p, h with positive155 refine ⟨function, _⟩156 intros x hx y hy z hz relatesX relatesY157 have xNat : Nat(x) := natural.member at succ(h) with hx158 have yNat : Nat(y) := natural.member at succ(h) with hy159 have valueX := FunctionBetween.apply_value with function, hx, relatesX160 have valueY := FunctionBetween.apply_value with function, hy, relatesY161 have negX := negSquareResidues.value at p, h, x with positive, hx162 have negY := negSquareResidues.value at p, h, y with positive, hy163 have same : ((h + h) - (x * x) % p) % p = ((h + h) - (y * y) % p) % p164 by cc165 have xBound : x ≤ h :=166 Less.successor_upper with (Less.of_membership with hx)167 have yBound : y ≤ h :=168 Less.successor_upper with (Less.of_membership with hy)169 exact negSquareResidues.injective with prime, odd, xBound, yBound, same170171-- The images have h + 1 elements each and meet172173theorem squareResidues.image_count (p h : Nat)174 requires prime : Prime(p)175 requires odd : p = 2 * h + 1176 shows cardinality(functionImage(squareResidues(p, h), succ(h), p)) = h + 1177proof178 have injective := squareResidues.injective_between with prime, odd179 have finite := Finite.natural at succ(h)180 have count := Injective.image_count with injective, finite181 have one := add.right_one at h182 rw [count, cardinality.natural, one]183 rfl184185theorem negSquareResidues.image_count (p h : Nat)186 requires prime : Prime(p)187 requires odd : p = 2 * h + 1188 shows cardinality(functionImage(negSquareResidues(p, h), succ(h), p)) = h + 1189proof190 have injective := negSquareResidues.injective_between with prime, odd191 have finite := Finite.natural at succ(h)192 have count := Injective.image_count with injective, finite193 have one := add.right_one at h194 rw [count, cardinality.natural, one]195 rfl196197-- Two subsets of p of size h + 1 each meet, since 2h + 2 > p.198theorem residues.meet (p h : Nat)199 requires prime : Prime(p)200 requires odd : p = 2 * h + 1201 shows ∃ x ∈ succ(h), ∃ y ∈ succ(h),202 squareResidues(p, h)(x) = negSquareResidues(p, h)(y)203proof204 have countS := squareResidues.image_count with prime, odd205 have countT := negSquareResidues.image_count with prime, odd206 have finiteP := Finite.natural at p207 have subsetS := functionImage.subset at squareResidues(p, h), succ(h), p208 have subsetT := functionImage.subset at negSquareResidues(p, h), succ(h), p209 have finiteS := Finite.subset with finiteP, subsetS210 have finiteT := Finite.subset with finiteP, subsetT211 have union := union2.subset with subsetS, subsetT212 have bound := cardinality.monotone with finiteP, union213 by_contra214 have disjoint : Disjoint(functionImage(squareResidues(p, h), succ(h), p),215 functionImage(negSquareResidues(p, h), succ(h), p))216 apply Disjoint.iff.backward217 intros r hS hT218 obtain ⟨rIn, x, hx, fx⟩ := functionImage.membership.forward with hS219 obtain ⟨rIn2, y, hy, gy⟩ := functionImage.membership.forward with hT220 have same : squareResidues(p, h)(x) = negSquareResidues(p, h)(y) by cc221 exact this with ⟨x, hx, y, hy, same⟩222 have sum := cardinality.union_disjoint with finiteS, finiteT, disjoint223 rw [sum, countS, countT, cardinality.natural] at bound224 arith225226-- The multiple m p = a² + b² + 1 with 0 < m < p227228-- q p = x² + y² + 1 ≤ 2h² + 1 < (2h + 1)² = p² forces q < p.229theorem Prime.multiple_below (p h x y q : Nat)230 requires odd : p = 2 * h + 1231 requires aboveOne : 1 < p232 requires xBound : x ≤ h233 requires yBound : y ≤ h234 requires equation : x * x + y * y + 1 = p * q235 shows q < p236proof237 have nonzero : p ≠ 0 by arith238 have xSquare := LessEqual.mul with xBound, xBound239 have ySquare := LessEqual.mul with yBound, yBound240 have expand : p * p = 4 * (h * h) + 4 * h + 1241 rw odd242 ring243 have below : p * q < p * p by arith244 exact Less.mul_left_cancel_positive with nonzero, below245246-- Where the two images meet, x² + y² + 1 ≡ 0 with x, y ≤ h; the sum is247-- positive and below p², so its quotient by p is the multiple.248theorem Prime.multiple_sum_squares_one (p h : Nat)249 requires prime : Prime(p)250 requires odd : p = 2 * h + 1251 shows ∃ m, Nat(m) ∧ ∃ a, Nat(a) ∧ ∃ b, Nat(b) ∧252 0 < m ∧ m < p ∧ m * p = a * a + b * b + 1253proof254 obtain ⟨x, hx, y, hy, same⟩ := residues.meet with prime, odd255 have positive : 0 < p by arith256 have nonzero : p ≠ 0 by arith257 have xNat : Nat(x) := natural.member at succ(h) with hx258 have yNat : Nat(y) := natural.member at succ(h) with hy259 have squareX := squareResidues.value at p, h, x with positive, hx260 have negY := negSquareResidues.value at p, h, y with positive, hy261 have fold := negSquareResidues.fold at p, h, y with odd262 rw [squareX, negY, fold] at same263 have ryLe := squareResidues.le_double at p, h, y with odd264 have cancel := sub.add_cancel at h + h, (y * y) % p with ryLe265 have total : (x * x) % p + (y * y) % p = h + h266 rw same267 exact cancel268 have modSum : (x * x + y * y + 1) % p = 0269 have split := mod.add at p, x * x, y * y with nonzero270 have shift := mod.add_mod_left at p, x * x + y * y, 1 with nonzero271 have inner := mod.add_mod_left at p, (x * x) % p + (y * y) % p, 1272 with nonzero273 rw [← shift, split, inner, total]274 have whole : h + h + 1 = p by arith275 rw whole276 exact mod.self at p277 obtain ⟨q, qNat, equation⟩ := mod.equation at x * x + y * y + 1, p278 with nonzero279 rw [modSum, add.right_unit] at equation280 have qPositive : 0 < q281 by_contra notPositive282 have qZero : q = 0 by arith283 rw [qZero, mul.right_zero] at equation284 arith285 have xBound : x ≤ h :=286 Less.successor_upper with (Less.of_membership with hx)287 have yBound : y ≤ h :=288 Less.successor_upper with (Less.of_membership with hy)289 have aboveOne := Prime.greater_than_one with prime290 have qBelow := Prime.multiple_below291 with odd, aboveOne, xBound, yBound, equation292 refine ⟨q, qNat, x, xNat, y, yNat, qPositive, qBelow, _⟩293 rw [mul.commutative, equation]294 ring295