-- Fermat's descent for four squares: a multiple m p of a prime with -- 1 < m < p that is a sum of four squares yields a smaller positive -- multiple that is, so every prime is a sum of four squares. -- 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.FourSquaresDescent import Grace.Cumulus.NumberTheory.FourSquareIdentity import Grace.Cumulus.NumberTheory.FourSquaresModPrime import Grace.Cumulus.NumberTheory.IntegerRemainder -- Euler's identity modulo m -- A sum of four integer squares is the embedded sum of the squares of the -- absolute values. theorem fourSquares.sum_ofNat (e f g k : Integer) shows e * e + f * f + g * g + k * k = integerOfNat( integerNatAbs(e) * integerNatAbs(e) + integerNatAbs(f) * integerNatAbs(f) + integerNatAbs(g) * integerNatAbs(g) + integerNatAbs(k) * integerNatAbs(k)) proof have eSquare := Integer.square_natAbs at e have fSquare := Integer.square_natAbs at f have gSquare := Integer.square_natAbs at g have kSquare := Integer.square_natAbs at k rw [eSquare, fSquare, gSquare, kSquare] simp [integerOfNat.add] -- When a, b, c, d are congruent to e, f, g, k modulo m and m divides -- a² + b² + c² + d², m divides each cross term of Euler's identity: the -- first is congruent to a² + b² + c² + d², the others to 0. theorem fourSquares.cross_terms (m : Nat) (a b c d e f g k : Integer) requires sum : IntegerDivides(integerOfNat(m), a * a + b * b + c * c + d * d) requires congruentA : IntegerCongruent(integerOfNat(m), a, e) requires congruentB : IntegerCongruent(integerOfNat(m), b, f) requires congruentC : IntegerCongruent(integerOfNat(m), c, g) requires congruentD : IntegerCongruent(integerOfNat(m), d, k) shows IntegerDivides(integerOfNat(m), a * e + b * f + c * g + d * k) ∧ IntegerDivides(integerOfNat(m), a * f - b * e + c * k - d * g) ∧ IntegerDivides(integerOfNat(m), a * g - b * k - c * e + d * f) ∧ IntegerDivides(integerOfNat(m), a * k + b * g - c * f - d * e) proof obtain ⟨q, qInt, hq⟩ := sum obtain ⟨qa, qaInt, ha⟩ := congruentA obtain ⟨qb, qbInt, hb⟩ := congruentB obtain ⟨qc, qcInt, hc⟩ := congruentC obtain ⟨qd, qdInt, hd⟩ := congruentD refine ⟨?_, ?_, ?_, ?_⟩ . apply IntegerDivides.intro at integerOfNat(m), a * e + b * f + c * g + d * k, q - a * qa - b * qb - c * qc - d * qd linear_combination hq - a * ha - b * hb - c * hc - d * hd . apply IntegerDivides.intro at integerOfNat(m), a * f - b * e + c * k - d * g, b * qa - a * qb + d * qc - c * qd linear_combination b * ha - a * hb + d * hc - c * hd . apply IntegerDivides.intro at integerOfNat(m), a * g - b * k - c * e + d * f, c * qa - a * qc + b * qd - d * qb linear_combination c * ha - a * hc + b * hd - d * hb . apply IntegerDivides.intro at integerOfNat(m), a * k + b * g - c * f - d * e, d * qa - a * qd + c * qb - b * qc linear_combination d * ha - a * hd + c * hb - b * hc -- Dividing Euler's identity by m² when m divides every cross term. theorem fourSquares.quotient (m : Nat) (a b c d e f g k : Integer) requires sum : IntegerDivides(integerOfNat(m), a * a + b * b + c * c + d * d) requires congruentA : IntegerCongruent(integerOfNat(m), a, e) requires congruentB : IntegerCongruent(integerOfNat(m), b, f) requires congruentC : IntegerCongruent(integerOfNat(m), c, g) requires congruentD : IntegerCongruent(integerOfNat(m), d, k) shows ∃ z ∈ ℤ, IntegerSumOfFourSquares(z) ∧ (a * a + b * b + c * c + d * d) * (e * e + f * f + g * g + k * k) = integerOfNat(m * m) * z proof have terms := fourSquares.cross_terms at m, a, b, c, d, e, f, g, k with * obtain ⟨⟨w, wInt, hw⟩, ⟨x, xInt, hx⟩, ⟨y, yInt, hy⟩, ⟨z, zInt, hz⟩⟩ := terms have squares := IntegerSumOfFourSquares.intro at w, x, y, z have member : w * w + x * x + y * y + z * z ∈ ℤ by typecheck refine ⟨w * w + x * x + y * y + z * z, member, squares, ?_⟩ rw euler.four_square_identity rw [← hw, ← hx, ← hy, ← hz, integerOfNat.mul] ring -- The least absolute residues: their squares sum to at most m² -- Four naturals each at most s/4 sum to at most s. theorem fourSquares.sum_bound (s x y z w : Nat) requires boundX : x * 4 ≤ s requires boundY : y * 4 ≤ s requires boundZ : z * 4 ≤ s requires boundW : w * 4 ≤ s shows x + y + z + w ≤ s proof have first : (x + y) * 4 ≤ s + s by arith have second : (z + w) * 4 ≤ s + s by arith have scaled : (x + y + z + w) * 4 ≤ s * 4 by arith have fourNonzero : 4 ≠ 0 intro zero arith exact LessEqual.mul_right_cancel_positive at x + y + z + w, s, 4 with fourNonzero, scaled -- Four residues of absolute value at most m/2 have squares summing to at -- most m². theorem fourSquares.residue_bound (m : Nat) (e f g k : Integer) requires boundE : 2 * integerNatAbs(e) ≤ m requires boundF : 2 * integerNatAbs(f) ≤ m requires boundG : 2 * integerNatAbs(g) ≤ m requires boundK : 2 * integerNatAbs(k) ≤ m shows integerNatAbs(e * e + f * f + g * g + k * k) ≤ m * m proof rw [fourSquares.sum_ofNat, integerNatAbs.ofNat] have squareE := integerNatAbs.square_bound at e, m with boundE have squareF := integerNatAbs.square_bound at f, m with boundF have squareG := integerNatAbs.square_bound at g, m with boundG have squareK := integerNatAbs.square_bound at k, m with boundK rw integerNatAbs.mul at squareE, squareF, squareG, squareK exact fourSquares.sum_bound with squareE, squareF, squareG, squareK -- The extreme cases: the squares sum to 0 or to m² -- In an extreme case, residues bounded by m/2 whose squares sum to 0 or -- m² each have square 0 or exactly m²/4. theorem fourSquares.extreme_squares (m : Nat) (e f g k : Integer) requires boundE : 2 * integerNatAbs(e) ≤ m requires boundF : 2 * integerNatAbs(f) ≤ m requires boundG : 2 * integerNatAbs(g) ≤ m requires boundK : 2 * integerNatAbs(k) ≤ m requires extreme : e * e + f * f + g * g + k * k = 0 ∨ e * e + f * f + g * g + k * k = integerOfNat(m * m) shows (integerNatAbs(e) * integerNatAbs(e) = 0 ∨ integerNatAbs(e) * integerNatAbs(e) * 4 = m * m) ∧ (integerNatAbs(f) * integerNatAbs(f) = 0 ∨ integerNatAbs(f) * integerNatAbs(f) * 4 = m * m) ∧ (integerNatAbs(g) * integerNatAbs(g) = 0 ∨ integerNatAbs(g) * integerNatAbs(g) * 4 = m * m) ∧ (integerNatAbs(k) * integerNatAbs(k) = 0 ∨ integerNatAbs(k) * integerNatAbs(k) * 4 = m * m) proof have squareE := integerNatAbs.square_bound at e, m with boundE have squareF := integerNatAbs.square_bound at f, m with boundF have squareG := integerNatAbs.square_bound at g, m with boundG have squareK := integerNatAbs.square_bound at k, m with boundK rw integerNatAbs.mul at squareE, squareF, squareG, squareK rw fourSquares.sum_ofNat at extreme cases extreme with zero | full . have total := integerOfNat.eq_zero_iff.forward with zero refine ⟨?_, ?_, ?_, ?_⟩ . left; arith . left; arith . left; arith . left; arith . have total := integerOfNat.injective with full refine ⟨?_, ?_, ?_, ?_⟩ . right; arith . right; arith . right; arith . right; arith -- A residue whose square is 0 or m²/4 is 0 or ±m/2, so m divides twice it. theorem fourSquares.residue_double (m : Nat) (e : Integer) requires extreme : integerNatAbs(e) * integerNatAbs(e) = 0 ∨ integerNatAbs(e) * integerNatAbs(e) * 4 = m * m shows IntegerDivides(integerOfNat(m), e + e) proof obtain ⟨n, nNat, sign⟩ := Integer.signed_natural at e have same := integerNatAbs.eq_of_sign at e, n with sign have eSquare := Integer.square_natAbs at e rw same at extreme, eSquare cases extreme with zero | half . rw [zero, integerOfNat.zero] at eSquare have eZero := integerMulValue.square_eq_zero at e with eSquare rw eZero apply IntegerDivides.intro at integerOfNat(m), integerZero + integerZero, integerZero ring . have expanded : n * n * 4 = n * n + n * n + n * n + n * n by ring rw expanded at half have lifted : integerOfNat(n * n + n * n + n * n + n * n) = integerOfNat(m * m) rw half rw integerOfNat.mul at lifted simp [integerOfNat.add] at lifted have product : (e + e - integerOfNat(m)) * (e + e + integerOfNat(m)) = 0 linear_combination eSquare + eSquare + eSquare + eSquare + lifted have split := integerMulValue.eq_zero with product cases split with plus | minus . apply IntegerDivides.intro at integerOfNat(m), e + e, integerOne linear_combination -plus . apply IntegerDivides.intro at integerOfNat(m), e + e, -integerOne linear_combination -minus -- The extreme cases: residues all zero, or all of absolute value exactly -- m/2; either way m² divides the original sum of squares. theorem fourSquares.extreme (m : Nat) (a b c d e f g k : Integer) requires boundE : 2 * integerNatAbs(e) ≤ m requires boundF : 2 * integerNatAbs(f) ≤ m requires boundG : 2 * integerNatAbs(g) ≤ m requires boundK : 2 * integerNatAbs(k) ≤ m requires congruentA : IntegerCongruent(integerOfNat(m), a, e) requires congruentB : IntegerCongruent(integerOfNat(m), b, f) requires congruentC : IntegerCongruent(integerOfNat(m), c, g) requires congruentD : IntegerCongruent(integerOfNat(m), d, k) requires extreme : e * e + f * f + g * g + k * k = 0 ∨ e * e + f * f + g * g + k * k = integerOfNat(m * m) shows IntegerDivides(integerOfNat(m * m), a * a + b * b + c * c + d * d) proof -- Each residue has square 0 or m²/4, so m divides each doubled residue, -- and m² divides each piece of a² = (a − e)² + (a − e)(2e) + e². have parts := fourSquares.extreme_squares with * obtain ⟨partE, partF, partG, partK⟩ := parts have doubleE := fourSquares.residue_double at m, e with partE have doubleF := fourSquares.residue_double at m, f with partF have doubleG := fourSquares.residue_double at m, g with partG have doubleK := fourSquares.residue_double at m, k with partK obtain ⟨qa, qaInt, ha⟩ := congruentA obtain ⟨qb, qbInt, hb⟩ := congruentB obtain ⟨qc, qcInt, hc⟩ := congruentC obtain ⟨qd, qdInt, hd⟩ := congruentD obtain ⟨ue, ueInt, hue⟩ := doubleE obtain ⟨uf, ufInt, huf⟩ := doubleF obtain ⟨ug, ugInt, hug⟩ := doubleG obtain ⟨uk, ukInt, huk⟩ := doubleK have aShape : a = e + integerOfNat(m) * qa by linear_combination -ha have bShape : b = f + integerOfNat(m) * qb by linear_combination -hb have cShape : c = g + integerOfNat(m) * qc by linear_combination -hc have dShape : d = k + integerOfNat(m) * qd by linear_combination -hd rw [aShape, bShape, cShape, dShape, integerOfNat.mul] cases extreme with zero | full . refine ⟨qa * qa + qb * qb + qc * qc + qd * qd + (qa * ue + qb * uf + qc * ug + qd * uk), ?_, ?_⟩ . typecheck . linear_combination integerOfNat(m) * qa * hue + integerOfNat(m) * qb * huf + integerOfNat(m) * qc * hug + integerOfNat(m) * qd * huk - zero . rw integerOfNat.mul at full refine ⟨qa * qa + qb * qb + qc * qc + qd * qd + (qa * ue + qb * uf + qc * ug + qd * uk) + 1, ?_, ?_⟩ . typecheck . linear_combination integerOfNat(m) * qa * hue + integerOfNat(m) * qb * huf + integerOfNat(m) * qc * hug + integerOfNat(m) * qd * huk - full -- m² dividing m p with p prime forces m ∣ p, so m = 1 or m = p. theorem fourSquares.no_extreme (p m : Nat) requires prime : Prime(p) requires aboveOne : 1 < m requires below : m < p shows ¬(m * m ∣ m * p) proof intro divides have mNonzero : m ≠ 0 by arith have cancel := divides.mul_cancel_left at m, m, p with mNonzero have dividesP := cancel.forward with divides have one := Prime.divisor at p, m with * rw one at aboveOne arith -- The descent step -- Take least absolute residues e, …, k of a, …, d modulo m; their squares -- sum to m r with 0 < r < m by the bound and the extreme cases, and -- Euler's identity divided by m² writes r p as a sum of four squares. theorem fourSquares.descent_step (p m : Nat) requires prime : Prime(p) requires aboveOne : 1 < m requires below : m < p requires sum : SumOfFourSquares(m * p) shows ∃ r, Nat(r) ∧ 0 < r ∧ r < m ∧ SumOfFourSquares(r * p) proof obtain ⟨a, aNat, b, bNat, c, cNat, d, dNat, equation⟩ := sum have mPositive : 0 < m by arith have mNonzero : m ≠ 0 by arith -- The sum of squares in ℤ is m p, so m divides it and m² does not. have sumNat : integerOfNat(a) * integerOfNat(a) + integerOfNat(b) * integerOfNat(b) + integerOfNat(c) * integerOfNat(c) + integerOfNat(d) * integerOfNat(d) = integerOfNat(m * p) rw equation simp [integerOfNat.add, integerOfNat.mul] have divides : IntegerDivides(integerOfNat(m), integerOfNat(a) * integerOfNat(a) + integerOfNat(b) * integerOfNat(b) + integerOfNat(c) * integerOfNat(c) + integerOfNat(d) * integerOfNat(d)) rw [sumNat, integerOfNat.mul] exact IntegerDivides.mul_left_self at integerOfNat(m), integerOfNat(p) have noSquare := fourSquares.no_extreme at p, m with * have squareDivides : ¬IntegerDivides(integerOfNat(m * m), integerOfNat(a) * integerOfNat(a) + integerOfNat(b) * integerOfNat(b) + integerOfNat(c) * integerOfNat(c) + integerOfNat(d) * integerOfNat(d)) rw sumNat intro extreme have natural := IntegerDivides.ofNat_iff.forward with extreme exact noSquare with natural -- The least absolute residues. obtain ⟨e, eInt, congruentA, boundE⟩ := IntegerCongruent.least_absolute at integerOfNat(a), m with mPositive obtain ⟨f, fInt, congruentB, boundF⟩ := IntegerCongruent.least_absolute at integerOfNat(b), m with mPositive obtain ⟨g, gInt, congruentC, boundG⟩ := IntegerCongruent.least_absolute at integerOfNat(c), m with mPositive obtain ⟨k, kInt, congruentD, boundK⟩ := IntegerCongruent.least_absolute at integerOfNat(d), m with mPositive -- Their squares sum to a natural that m divides: m r, with r ≤ m. have residueSum := fourSquares.sum_ofNat at e, f, g, k have squaresA := IntegerCongruent.mul with congruentA, congruentA have squaresB := IntegerCongruent.mul with congruentB, congruentB have squaresC := IntegerCongruent.mul with congruentC, congruentC have squaresD := IntegerCongruent.mul with congruentD, congruentD have sumAB := IntegerCongruent.add with squaresA, squaresB have sumABC := IntegerCongruent.add with sumAB, squaresC have sumCongruent := IntegerCongruent.add with sumABC, squaresD have sumZero := IntegerCongruent.zero_right_iff.backward with divides have backward := IntegerCongruent.symmetric with sumCongruent have residueZero := IntegerCongruent.transitive with backward, sumZero have residueDivides := IntegerCongruent.zero_right_iff.forward with residueZero rw residueSum at residueDivides have natural := IntegerDivides.ofNat_iff.forward with residueDivides obtain ⟨r, rNat, shape⟩ := divides.definition.forward with natural have bound := fourSquares.residue_bound at m, e, f, g, k with * rw [residueSum, integerNatAbs.ofNat, ← shape] at bound have rBound := LessEqual.mul_left_cancel_positive at r, m, m with mNonzero, bound rw ← shape at residueSum -- The extreme cases r = 0 and r = m would make m² divide m p. have notExtreme : ¬(e * e + f * f + g * g + k * k = 0 ∨ e * e + f * f + g * g + k * k = integerOfNat(m * m)) intro extreme have squareSum := fourSquares.extreme at m, integerOfNat(a), integerOfNat(b), integerOfNat(c), integerOfNat(d), e, f, g, k with * exact squareDivides with squareSum have rPositive : 0 < r by_contra zero have rZero : r = 0 by arith rw [rZero, mul.right_zero, integerOfNat.zero] at residueSum apply notExtreme left exact residueSum have rBelow : r < m by_contra notBelow have same : r = m by arith rw same at residueSum apply notExtreme right exact residueSum -- Euler's identity divided by m² writes r p as a sum of four squares. have quotient := fourSquares.quotient at m, integerOfNat(a), integerOfNat(b), integerOfNat(c), integerOfNat(d), e, f, g, k with * obtain ⟨z, zInt, zSquares, product⟩ := quotient rw [sumNat, residueSum] at product have key : integerOfNat(m * m) * integerOfNat(r * p) = integerOfNat(m * p) * integerOfNat(m * r) rw [← integerOfNat.mul, ← integerOfNat.mul] have same : m * m * (r * p) = m * p * (m * r) by ring rw same rw ← key at product have nonzero : integerOfNat(m * m) ≠ 0 intro zero have squareZero := integerOfNat.eq_zero_iff.forward with zero have squareNonzero := mul.nonzero at m, m with mNonzero, mNonzero exact squareNonzero with squareZero have same := integerMulValue.left_cancel at integerOfNat(r * p), z, integerOfNat(m * m) with nonzero, product rw ← same at zSquares have result := SumOfFourSquares.of_integer at r * p with zSquares refine ⟨r, rNat, rPositive, rBelow, result⟩ -- Every prime is a sum of four squares -- The positive multiples of p that are sums of four squares. def fourSquareMultiples (p : Nat) : Set := { m ∈ ℕ | 0 < m ∧ SumOfFourSquares(mul(m, p)) } -- A prime is 2 or of the form 2 h + 1. theorem Prime.two_or_odd_half (p : Nat) requires prime : Prime(p) shows p = 2 ∨ ∃ h, Nat(h) ∧ p = 2 * h + 1 proof have parity := parity.exhaustive at p cases parity with even | odd . left have evenShape := Even.iff_two_mul at p obtain ⟨h, hNat, shape⟩ := evenShape.forward with even symm at shape have twoDivides := divides.intro at 2, p, h with shape have above := Prime.greater_than_one with prime have twoLe : 2 ≤ p by arith have split := LessEqual.cases at 2, p with twoLe cases split with less | same . exfalso have one := Prime.divisor at p, 2 with * arith . rw ← same . right have oddShape := Odd.iff_two_mul_add_one at p exact oddShape.forward with odd -- The least positive m with m p a sum of four squares is 1: the odd case -- is seeded by Prime.multiple_sum_squares_one, and 2 = 1 + 1 + 0 + 0. theorem lagrange.prime (p : Nat) requires prime : Prime(p) shows SumOfFourSquares(p) proof have split := Prime.two_or_odd_half at p with prime cases split with two | odd . rw two exact SumOfFourSquares.two . obtain ⟨h, hNat, shape⟩ := odd obtain ⟨m, mNat, a, aNat, b, bNat, mPositive, mBelow, equation⟩ := Prime.multiple_sum_squares_one at p, h with prime, shape -- m is a positive multiple of p that is a sum of four squares. have seedSum : SumOfFourSquares(mul(m, p)) have same : m * p = a * a + b * b + 1 * 1 + 0 * 0 rw equation ring rw same exact SumOfFourSquares.intro at a, b, 1, 0 have mMember := nat.omega.forward with mNat have seed : m ∈ fourSquareMultiples(p) := ⟨mMember, mPositive, seedSum⟩ have inhabited : Nonempty(fourSquareMultiples(p)) := ⟨m, seed⟩ have naturals : fourSquareMultiples(p) ⊆ ℕ by simp have least := leastNatural.member at fourSquareMultiples(p) with * obtain ⟨leastMember, leastPositive, leastSum⟩ := fourSquareMultiples.membership.forward with least have leastBound := leastNatural.least at fourSquareMultiples(p), m with * -- The least multiple is not above 1, since the descent would go lower. have notAbove : ¬(1 < leastNatural(fourSquareMultiples(p))) intro above have belowP : leastNatural(fourSquareMultiples(p)) < p by arith obtain ⟨r, rNat, rPositive, rBelow, rSum⟩ := fourSquares.descent_step at p, leastNatural(fourSquareMultiples(p)) with prime, above, belowP, leastSum have rMember := nat.omega.forward with rNat have rIn : r ∈ fourSquareMultiples(p) := ⟨rMember, rPositive, rSum⟩ have rBound := leastNatural.least at fourSquareMultiples(p), r with * arith have one : leastNatural(fourSquareMultiples(p)) = 1 by arith rw [one, mul.left_unit] at leastSum exact leastSum