-- Sums of four squares in ℕ and ℤ, Euler's four-square identity, and -- closure of sums of four squares under products. -- 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.FourSquareIdentity import Grace.Cumulus.Int import Grace.Cumulus.Nat -- Sums of four squares, in ℕ and in ℤ predicate SumOfFourSquares (n : Nat) := ∃ a, Nat(a) ∧ ∃ b, Nat(b) ∧ ∃ c, Nat(c) ∧ ∃ d, Nat(d) ∧ n = a * a + b * b + c * c + d * d predicate IntegerSumOfFourSquares (z : Set) := ∃ a ∈ ℤ, ∃ b ∈ ℤ, ∃ c ∈ ℤ, ∃ d ∈ ℤ, z = a * a + b * b + c * c + d * d theorem SumOfFourSquares.intro (a b c d : Nat) shows SumOfFourSquares(a * a + b * b + c * c + d * d) proof have aNat : Nat(a) by typecheck have bNat : Nat(b) by typecheck have cNat : Nat(c) by typecheck have dNat : Nat(d) by typecheck exact ⟨a, aNat, b, bNat, c, cNat, d, dNat, rfl⟩ theorem SumOfFourSquares.zero shows SumOfFourSquares(0) proof have same : 0 * 0 + 0 * 0 + 0 * 0 + 0 * 0 = 0 by ring have sum := SumOfFourSquares.intro at 0, 0, 0, 0 rwa same at sum theorem SumOfFourSquares.one shows SumOfFourSquares(1) proof have same : 1 * 1 + 0 * 0 + 0 * 0 + 0 * 0 = 1 by ring have sum := SumOfFourSquares.intro at 1, 0, 0, 0 rwa same at sum theorem SumOfFourSquares.two shows SumOfFourSquares(2) proof have same : 1 * 1 + 1 * 1 + 0 * 0 + 0 * 0 = 2 by ring have sum := SumOfFourSquares.intro at 1, 1, 0, 0 rwa same at sum theorem IntegerSumOfFourSquares.intro (a b c d : Integer) shows IntegerSumOfFourSquares(a * a + b * b + c * c + d * d) proof have aIn : a ∈ ℤ by settle with Integer have bIn : b ∈ ℤ by settle with Integer have cIn : c ∈ ℤ by settle with Integer have dIn : d ∈ ℤ by settle with Integer exact ⟨a, aIn, b, bIn, c, cIn, d, dIn, rfl⟩ -- Passing between ℕ and ℤ theorem IntegerSumOfFourSquares.of_natural (n : Nat) requires sum : SumOfFourSquares(n) shows IntegerSumOfFourSquares(integerOfNat(n)) proof obtain ⟨a, aNat, b, bNat, c, cNat, d, dNat, shape⟩ := sum have lifted : integerOfNat(n) = integerOfNat(a) * integerOfNat(a) + integerOfNat(b) * integerOfNat(b) + integerOfNat(c) * integerOfNat(c) + integerOfNat(d) * integerOfNat(d) rw shape simp [integerOfNat.add, integerOfNat.mul] rw lifted exact IntegerSumOfFourSquares.intro at integerOfNat(a), integerOfNat(b), integerOfNat(c), integerOfNat(d) -- A square is the square of the absolute value, whichever the sign. -- An integer sum of four squares that is a natural is a sum of the four -- squares of the absolute values. theorem SumOfFourSquares.of_integer (n : Nat) requires sum : IntegerSumOfFourSquares(integerOfNat(n)) shows SumOfFourSquares(n) proof obtain ⟨a, aIn, b, bIn, c, cIn, d, dIn, shape⟩ := sum have aSquare := Integer.square_natAbs at a have bSquare := Integer.square_natAbs at b have cSquare := Integer.square_natAbs at c have dSquare := Integer.square_natAbs at d rw [aSquare, bSquare, cSquare, dSquare] at shape rw [← integerOfNat.add, ← integerOfNat.add, ← integerOfNat.add] at shape have same := integerOfNat.injective_iff.forward with shape rw same exact SumOfFourSquares.intro at integerNatAbs(a), integerNatAbs(b), integerNatAbs(c), integerNatAbs(d) -- Euler's identity and closure under products theorem euler.four_square_identity (a b c d e f g k : Integer) shows (a * a + b * b + c * c + d * d) * (e * e + f * f + g * g + k * k) = (a * e + b * f + c * g + d * k) * (a * e + b * f + c * g + d * k) + (a * f - b * e + c * k - d * g) * (a * f - b * e + c * k - d * g) + (a * g - b * k - c * e + d * f) * (a * g - b * k - c * e + d * f) + (a * k + b * g - c * f - d * e) * (a * k + b * g - c * f - d * e) proof ring theorem IntegerSumOfFourSquares.mul (x y : Integer) requires first : IntegerSumOfFourSquares(x) requires second : IntegerSumOfFourSquares(y) shows IntegerSumOfFourSquares(x * y) proof obtain ⟨a, aIn, b, bIn, c, cIn, d, dIn, xShape⟩ := first obtain ⟨e, eIn, f, fIn, g, gIn, k, kIn, yShape⟩ := second rw [xShape, yShape, euler.four_square_identity] exact IntegerSumOfFourSquares.intro at a * e + b * f + c * g + d * k, a * f - b * e + c * k - d * g, a * g - b * k - c * e + d * f, a * k + b * g - c * f - d * e -- The product of two sums of four squares, through ℤ. theorem SumOfFourSquares.mul (m n : Nat) requires first : SumOfFourSquares(m) requires second : SumOfFourSquares(n) shows SumOfFourSquares(m * n) proof have liftM := IntegerSumOfFourSquares.of_natural with first have liftN := IntegerSumOfFourSquares.of_natural with second have product := IntegerSumOfFourSquares.mul with liftM, liftN rw integerMulValue.ofNat at product exact SumOfFourSquares.of_integer with product