-- For an odd prime p = 2h + 1, some multiple m p with 0 < m < p is -- a² + b² + 1: the h + 1 residues x² and the h + 1 residues −1 − y² for -- x, y ≤ h cannot all be distinct among p residues. -- 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.FourSquaresModPrime import Grace.Cumulus.Cardinal import Grace.Cumulus.NumberTheory.ModularMore import Grace.Cumulus.Combinatorics.Pigeonhole -- The two residue maps on 0..h -- x ↦ x² mod p on 0..h, and y ↦ (2h − y² mod p) mod p, which is -- −1 − y² modulo p = 2h + 1. defgraph squareResidues (p h : Nat) (x ∈ succ(h)) : p := mod(mul(x, x), p) requires positive : 0 < p proof have xNat : Nat(x) := natural.member at succ(h) with member have nonzero : p ≠ 0 by arith have below := mod.less at x * x, p with nonzero have bridge := membership.of_Less at x * x % p, p with * exact bridge with below defgraph negSquareResidues (p h : Nat) (y ∈ succ(h)) : p := mod(sub(add(h, h), mod(mul(y, y), p)), p) requires positive : 0 < p proof have yNat : Nat(y) := natural.member at succ(h) with member have nonzero : p ≠ 0 by arith have below := mod.less at (h + h) - (y * y) % p, p with nonzero have bridge := membership.of_Less at ((h + h) - (y * y) % p) % p, p with * exact bridge with below -- Every residue is at most 2h, so 2h − r is again below p and the outer -- mod of negSquareResidues is the identity. theorem squareResidues.le_double (p h y : Nat) requires odd : p = 2 * h + 1 shows (y * y) % p ≤ h + h proof have nonzero : p ≠ 0 by arith have below := mod.less at y * y, p with nonzero arith theorem negSquareResidues.fold (p h y : Nat) requires odd : p = 2 * h + 1 shows ((h + h) - (y * y) % p) % p = (h + h) - (y * y) % p proof have le := sub.le_self at h + h, (y * y) % p have small : (h + h) - (y * y) % p < p by arith exact mod.of_less at p, (h + h) - (y * y) % p with small -- Both maps are injective -- With y ≤ x, x² = y² + d (x + y) for d = x − y, so p ∣ d (x + y); p is -- prime and both factors are below p, so the divisible one is zero. theorem squareResidues.eq_of_le (p h x y : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires xBound : x ≤ h requires yBound : y ≤ h requires le : y ≤ x requires same : (x * x) % p = (y * y) % p shows x = y proof obtain ⟨d, dNat, split⟩ := le have expand : x * x = y * y + d * (x + y) rw ← split ring rw expand at same have divides := (mod.add_eq_iff at p, y * y, d * (x + y)).forward with same have factor := Prime.divides_mul with prime, divides have xy : x ≤ y cases factor with dCase | sumCase . have dZero : d = 0 by_contra have bound := divides.le with dCase, this arith arith . have sumZero : x + y = 0 by_contra have bound := divides.le with sumCase, this arith arith exact LessEqual.antisymmetric with xy, le -- x² ≡ y² with x, y ≤ h gives p ∣ (x − y)(x + y) and x + y ≤ 2h < p, -- in either order of x and y. theorem squareResidues.injective (p h x y : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires xBound : x ≤ h requires yBound : y ≤ h requires same : (x * x) % p = (y * y) % p shows x = y proof have total := LessEqual.total at x, y cases total with xy | yx . have flipped : (y * y) % p = (x * x) % p by cc symm exact squareResidues.eq_of_le at p, h, y, x with prime, odd, yBound, xBound, xy, flipped . exact squareResidues.eq_of_le with prime, odd, xBound, yBound, yx, same theorem squareResidues.injective_between (p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 shows InjectiveBetween(squareResidues(p, h), succ(h), p) proof have positive : 0 < p by arith have function := squareResidues.function at p, h with positive refine ⟨function, _⟩ intros x hx y hy z hz relatesX relatesY have xNat : Nat(x) := natural.member at succ(h) with hx have yNat : Nat(y) := natural.member at succ(h) with hy have valueX := FunctionBetween.apply_value with function, hx, relatesX have valueY := FunctionBetween.apply_value with function, hy, relatesY have squareX := squareResidues.value at p, h, x with positive, hx have squareY := squareResidues.value at p, h, y with positive, hy have same : (x * x) % p = (y * y) % p by cc have xBound : x ≤ h := Less.successor_upper with (Less.of_membership with hx) have yBound : y ≤ h := Less.successor_upper with (Less.of_membership with hy) exact squareResidues.injective with prime, odd, xBound, yBound, same -- 2h − r determines the residue r, and r determines y. theorem negSquareResidues.injective (p h x y : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires xBound : x ≤ h requires yBound : y ≤ h requires same : ((h + h) - (x * x) % p) % p = ((h + h) - (y * y) % p) % p shows x = y proof have foldX := negSquareResidues.fold at p, h, x with odd have foldY := negSquareResidues.fold at p, h, y with odd rw [foldX, foldY] at same have rxLe := squareResidues.le_double at p, h, x with odd have ryLe := squareResidues.le_double at p, h, y with odd have backX := sub.sub_cancel at h + h, (x * x) % p with rxLe have backY := sub.sub_cancel at h + h, (y * y) % p with ryLe have residues : (x * x) % p = (y * y) % p by cc exact squareResidues.injective with prime, odd, xBound, yBound, residues theorem negSquareResidues.injective_between (p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 shows InjectiveBetween(negSquareResidues(p, h), succ(h), p) proof have positive : 0 < p by arith have function := negSquareResidues.function at p, h with positive refine ⟨function, _⟩ intros x hx y hy z hz relatesX relatesY have xNat : Nat(x) := natural.member at succ(h) with hx have yNat : Nat(y) := natural.member at succ(h) with hy have valueX := FunctionBetween.apply_value with function, hx, relatesX have valueY := FunctionBetween.apply_value with function, hy, relatesY have negX := negSquareResidues.value at p, h, x with positive, hx have negY := negSquareResidues.value at p, h, y with positive, hy have same : ((h + h) - (x * x) % p) % p = ((h + h) - (y * y) % p) % p by cc have xBound : x ≤ h := Less.successor_upper with (Less.of_membership with hx) have yBound : y ≤ h := Less.successor_upper with (Less.of_membership with hy) exact negSquareResidues.injective with prime, odd, xBound, yBound, same -- The images have h + 1 elements each and meet theorem squareResidues.image_count (p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 shows cardinality(functionImage(squareResidues(p, h), succ(h), p)) = h + 1 proof have injective := squareResidues.injective_between with prime, odd have finite := Finite.natural at succ(h) have count := Injective.image_count with injective, finite have one := add.right_one at h rw [count, cardinality.natural, one] rfl theorem negSquareResidues.image_count (p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 shows cardinality(functionImage(negSquareResidues(p, h), succ(h), p)) = h + 1 proof have injective := negSquareResidues.injective_between with prime, odd have finite := Finite.natural at succ(h) have count := Injective.image_count with injective, finite have one := add.right_one at h rw [count, cardinality.natural, one] rfl -- Two subsets of p of size h + 1 each meet, since 2h + 2 > p. theorem residues.meet (p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 shows ∃ x ∈ succ(h), ∃ y ∈ succ(h), squareResidues(p, h)(x) = negSquareResidues(p, h)(y) proof have countS := squareResidues.image_count with prime, odd have countT := negSquareResidues.image_count with prime, odd have finiteP := Finite.natural at p have subsetS := functionImage.subset at squareResidues(p, h), succ(h), p have subsetT := functionImage.subset at negSquareResidues(p, h), succ(h), p have finiteS := Finite.subset with finiteP, subsetS have finiteT := Finite.subset with finiteP, subsetT have union := union2.subset with subsetS, subsetT have bound := cardinality.monotone with finiteP, union by_contra have disjoint : Disjoint(functionImage(squareResidues(p, h), succ(h), p), functionImage(negSquareResidues(p, h), succ(h), p)) apply Disjoint.iff.backward intros r hS hT obtain ⟨rIn, x, hx, fx⟩ := functionImage.membership.forward with hS obtain ⟨rIn2, y, hy, gy⟩ := functionImage.membership.forward with hT have same : squareResidues(p, h)(x) = negSquareResidues(p, h)(y) by cc exact this with ⟨x, hx, y, hy, same⟩ have sum := cardinality.union_disjoint with finiteS, finiteT, disjoint rw [sum, countS, countT, cardinality.natural] at bound arith -- The multiple m p = a² + b² + 1 with 0 < m < p -- q p = x² + y² + 1 ≤ 2h² + 1 < (2h + 1)² = p² forces q < p. theorem Prime.multiple_below (p h x y q : Nat) requires odd : p = 2 * h + 1 requires aboveOne : 1 < p requires xBound : x ≤ h requires yBound : y ≤ h requires equation : x * x + y * y + 1 = p * q shows q < p proof have nonzero : p ≠ 0 by arith have xSquare := LessEqual.mul with xBound, xBound have ySquare := LessEqual.mul with yBound, yBound have expand : p * p = 4 * (h * h) + 4 * h + 1 rw odd ring have below : p * q < p * p by arith exact Less.mul_left_cancel_positive with nonzero, below -- Where the two images meet, x² + y² + 1 ≡ 0 with x, y ≤ h; the sum is -- positive and below p², so its quotient by p is the multiple. theorem Prime.multiple_sum_squares_one (p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 shows ∃ m, Nat(m) ∧ ∃ a, Nat(a) ∧ ∃ b, Nat(b) ∧ 0 < m ∧ m < p ∧ m * p = a * a + b * b + 1 proof obtain ⟨x, hx, y, hy, same⟩ := residues.meet with prime, odd have positive : 0 < p by arith have nonzero : p ≠ 0 by arith have xNat : Nat(x) := natural.member at succ(h) with hx have yNat : Nat(y) := natural.member at succ(h) with hy have squareX := squareResidues.value at p, h, x with positive, hx have negY := negSquareResidues.value at p, h, y with positive, hy have fold := negSquareResidues.fold at p, h, y with odd rw [squareX, negY, fold] at same have ryLe := squareResidues.le_double at p, h, y with odd have cancel := sub.add_cancel at h + h, (y * y) % p with ryLe have total : (x * x) % p + (y * y) % p = h + h rw same exact cancel have modSum : (x * x + y * y + 1) % p = 0 have split := mod.add at p, x * x, y * y with nonzero have shift := mod.add_mod_left at p, x * x + y * y, 1 with nonzero have inner := mod.add_mod_left at p, (x * x) % p + (y * y) % p, 1 with nonzero rw [← shift, split, inner, total] have whole : h + h + 1 = p by arith rw whole exact mod.self at p obtain ⟨q, qNat, equation⟩ := mod.equation at x * x + y * y + 1, p with nonzero rw [modSum, add.right_unit] at equation have qPositive : 0 < q by_contra notPositive have qZero : q = 0 by arith rw [qZero, mul.right_zero] at equation arith have xBound : x ≤ h := Less.successor_upper with (Less.of_membership with hx) have yBound : y ≤ h := Less.successor_upper with (Less.of_membership with hy) have aboveOne := Prime.greater_than_one with prime have qBelow := Prime.multiple_below with odd, aboveOne, xBound, yBound, equation refine ⟨q, qNat, x, xNat, y, yNat, qPositive, qBelow, _⟩ rw [mul.commutative, equation] ring