-- Eisenstein's parity form of Gauss's lemma: for an odd prime p = 2h + 1 -- and an odd a not divisible by p, the Gauss count of a has the parity of -- the floor sum Σ ⌊a k / p⌋ over k = 1..h. -- 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.GaussParity import Grace.Cumulus.NumberTheory.GaussLemma import Grace.Cumulus.Cardinal import Grace.Cumulus.Nat.Division -- The weights on the half range are graphs on it with natural values: -- ⌊a k / p⌋, a k, the residue (a k) % p, the residue folded onto 1..h, -- and k itself. defgraph floorWeights (a p h : Nat) (k ∈ halfRange(h)) : ℕ := quotientNat(mul(a, k), p) proof have kNat := halfRange.member_nat with member have natural := quotientNat.natural at p, a * k exact nat.omega.forward with natural defgraph scaledWeights (a h : Nat) (k ∈ halfRange(h)) : ℕ := mul(a, k) proof have kNat := halfRange.member_nat with member have natural : Nat(a * k) by typecheck exact nat.omega.forward with natural defgraph residueWeights (a p h : Nat) (k ∈ halfRange(h)) : ℕ := mod(mul(a, k), p) proof have kNat := halfRange.member_nat with member have natural : Nat((a * k) % p) by typecheck exact nat.omega.forward with natural defgraph foldedWeights (a p h : Nat) (k ∈ halfRange(h)) : ℕ := chooseNat(isZero(sub(mod(mul(a, k), p), h)), mod(mul(a, k), p), sub(p, mod(mul(a, k), p))) proof have kNat := halfRange.member_nat with member have natural := chooseNat.natural at isZero((a * k) % p - h), (a * k) % p, p - (a * k) % p with * exact nat.omega.forward with natural defgraph identityWeights (h : Nat) (k ∈ halfRange(h)) : ℕ := k proof have kNat := halfRange.member_nat with member exact nat.omega.forward with kNat theorem floorWeights.natural (a p h : Nat) shows ∀ v ∈ halfRange(h), floorWeights(a, p, h)(v) ∈ ℕ proof intros v hv have vNat := halfRange.member_nat with hv rw floorWeights.value with hv have natural := quotientNat.natural at p, a * v exact nat.omega.forward with natural theorem scaledWeights.natural (a h : Nat) shows ∀ v ∈ halfRange(h), scaledWeights(a, h)(v) ∈ ℕ proof intros v hv have vNat := halfRange.member_nat with hv rw scaledWeights.value with hv have natural : Nat(a * v) by typecheck exact nat.omega.forward with natural theorem residueWeights.natural (a p h : Nat) shows ∀ v ∈ halfRange(h), residueWeights(a, p, h)(v) ∈ ℕ proof intros v hv have vNat := halfRange.member_nat with hv rw residueWeights.value with hv have natural : Nat((a * v) % p) by typecheck exact nat.omega.forward with natural theorem foldedWeights.natural (a p h : Nat) shows ∀ v ∈ halfRange(h), foldedWeights(a, p, h)(v) ∈ ℕ proof intros v hv have vNat := halfRange.member_nat with hv rw foldedWeights.value with hv have natural := chooseNat.natural at isZero((a * v) % p - h), (a * v) % p, p - (a * v) % p with * exact nat.omega.forward with natural theorem identityWeights.natural (h : Nat) shows ∀ v ∈ halfRange(h), identityWeights(h)(v) ∈ ℕ proof intros v hv have vNat := halfRange.member_nat with hv rw identityWeights.value with hv exact nat.omega.forward with vNat -- The folded weight of a small residue is the residue, of a large one its -- complement p − r. theorem foldedWeights.small (a p h k : Nat) requires member : k ∈ halfRange(h) requires small : (a * k) % p ≤ h shows foldedWeights(a, p, h)(k) = (a * k) % p proof have test := isZero.sub_of_le with small rw [foldedWeights.value with member, test] simp [chooseNat] theorem foldedWeights.large (a p h k : Nat) requires member : k ∈ halfRange(h) requires large : h < (a * k) % p shows foldedWeights(a, p, h)(k) = p - (a * k) % p proof have test := isZero.sub_of_Less with large rw [foldedWeights.value with member, test] simp [chooseNat] -- The three sum identities over 1..h, each an instance of natSum.linear -- with the indicator of ∅ as the zero weight and the indicator of the -- range itself as the constant weight 1. Division: Σ a k = p Σ ⌊a k / p⌋ + -- Σ (a k) % p, from quotientNat.equation pointwise. theorem natSum.division_split (a p h : Nat) requires positive : 0 < p shows natSum(halfRange(h), scaledWeights(a, h)) = add(mul(p, natSum(halfRange(h), floorWeights(a, p, h))), natSum(halfRange(h), residueWeights(a, p, h))) proof have finite := halfRange.finite at h have zeroWeights := indicatorMap.natural at ∅, halfRange(h) have agree : ∀ v ∈ halfRange(h), add(scaledWeights(a, h)(v), indicatorMap(∅, halfRange(h))(v)) = add(mul(p, floorWeights(a, p, h)(v)), residueWeights(a, p, h)(v)) intros v hv have vNat := halfRange.member_nat with hv have absent : ¬v ∈ ∅ intro inside exact set.empty with inside rw [scaledWeights.value with hv, (indicatorMap.value at ∅, halfRange(h), v with hv), floorWeights.value with hv, residueWeights.value with hv, indicator.absent with absent] have equation := quotientNat.equation at p, a * v have unit : a * v + 0 = a * v by ring rw unit exact equation have law := natSum.linear with finite, (scaledWeights.natural at a, h), zeroWeights, (floorWeights.natural at a, p, h), (residueWeights.natural at a, p, h), agree have sumNat := natSum.natural with finite, (scaledWeights.natural at a, h) rw [natSum.empty_indicator, add.right_unit] at law exact law -- Scaling: Σ a k = a Σ k. theorem natSum.scaled (a h : Nat) shows natSum(halfRange(h), scaledWeights(a, h)) = mul(a, natSum(halfRange(h), identityWeights(h))) proof have finite := halfRange.finite at h have zeroWeights := indicatorMap.natural at ∅, halfRange(h) have agree : ∀ v ∈ halfRange(h), add(scaledWeights(a, h)(v), indicatorMap(∅, halfRange(h))(v)) = add(mul(a, identityWeights(h)(v)), indicatorMap(∅, halfRange(h))(v)) intros v hv rw [scaledWeights.value with hv, identityWeights.value with hv] rfl have law := natSum.linear with finite, (scaledWeights.natural at a, h), zeroWeights, (identityWeights.natural at h), zeroWeights, agree have sumNat := natSum.natural with finite, (scaledWeights.natural at a, h) have identityNat := natSum.natural with finite, (identityWeights.natural at h) rw [natSum.empty_indicator, add.right_unit, add.right_unit] at law exact law -- Folding: over the Gauss set the residue is p minus the folded value, -- elsewhere the two agree, so Σ r_k + 2 Σ_{Gauss} r'_k = Σ r'_k + μ p. theorem natSum.fold_split (a p h : Nat) requires positive : 0 < p shows add(natSum(halfRange(h), residueWeights(a, p, h)), mul(2, natSum(gaussSet(a, p, h), foldedWeights(a, p, h)))) = add(natSum(halfRange(h), foldedWeights(a, p, h)), mul(cardinality(gaussSet(a, p, h)), p)) proof have nonzero : p ≠ 0 by arith have finite := halfRange.finite at h have gaussFinite := gaussSet.finite at a, p, h have subset := gaussSet.subset at a, p, h have restInside := difference.subset_left at halfRange(h), gaussSet(a, p, h) have restFinite := Finite.subset with finite, restInside have disjoint := Disjoint.difference_right at halfRange(h), gaussSet(a, p, h) have cover := difference.union2_of_subset with subset have residueNat := residueWeights.natural at a, p, h have foldedNat := foldedWeights.natural at a, p, h have residueGauss : ∀ v ∈ gaussSet(a, p, h), residueWeights(a, p, h)(v) ∈ ℕ intros v hv exact residueNat with v, (subset with v, hv) have foldedGauss : ∀ v ∈ gaussSet(a, p, h), foldedWeights(a, p, h)(v) ∈ ℕ intros v hv exact foldedNat with v, (subset with v, hv) have residueRest : ∀ v ∈ halfRange(h) ∖ gaussSet(a, p, h), residueWeights(a, p, h)(v) ∈ ℕ intros v hv exact residueNat with v, (restInside with v, hv) have foldedRest : ∀ v ∈ halfRange(h) ∖ gaussSet(a, p, h), foldedWeights(a, p, h)(v) ∈ ℕ intros v hv exact foldedNat with v, (restInside with v, hv) -- Both sums split over the Gauss set and its complement in 1..h. have splitResidue := natSum.union_disjoint with gaussFinite, restFinite, residueGauss, residueRest, disjoint have splitFolded := natSum.union_disjoint with gaussFinite, restFinite, foldedGauss, foldedRest, disjoint rw cover at splitResidue rw cover at splitFolded -- Off the Gauss set the residue is small and folding keeps it. have agreeRest : ∀ v ∈ halfRange(h) ∖ gaussSet(a, p, h), residueWeights(a, p, h)(v) = foldedWeights(a, p, h)(v) intros v hv have parts := difference.membership.forward with hv have inRange := parts.left have vNat := halfRange.member_nat with inRange have bounds := (halfRange.mem_iff at h, v).forward with inRange obtain ⟨vPositive, vBound⟩ := bounds have small : (a * v) % p ≤ h have cases := Less.or_LessEqual at h, (a * v) % p cases cases with large | small . exfalso have inGauss := (gaussSet.mem_iff at a, p, h, v).backward with ⟨vPositive, vBound, large⟩ exact parts.right with inGauss . exact small rw [residueWeights.value with inRange, (foldedWeights.small with inRange, small)] rfl have sameRest := natSum.congr with agreeRest -- On the Gauss set residue and folded value add to p, so the two sums -- there add to p μ. have agreeGauss : ∀ v ∈ gaussSet(a, p, h), add(residueWeights(a, p, h)(v), foldedWeights(a, p, h)(v)) = add(mul(p, indicatorMap(gaussSet(a, p, h), gaussSet(a, p, h))(v)), indicatorMap(∅, gaussSet(a, p, h))(v)) intros v hv have inRange := subset with v, hv have vNat := halfRange.member_nat with inRange have all := (gaussSet.mem_iff at a, p, h, v).forward with hv obtain ⟨vPositive, vBound, large⟩ := all have absent : ¬v ∈ ∅ intro inside exact set.empty with inside rw [residueWeights.value with inRange, (foldedWeights.large with inRange, large), (indicatorMap.value at gaussSet(a, p, h), gaussSet(a, p, h), v with hv), (indicatorMap.value at ∅, gaussSet(a, p, h), v with hv), indicator.member with hv, indicator.absent with absent] have residueBelow := mod.less at a * v, p with nonzero have residueLe : (a * v) % p ≤ p by arith have pieces := sub.add_cancel with residueLe have total : (a * v) % p + (p - (a * v) % p) = p by arith rw total ring have law := natSum.linear with gaussFinite, residueGauss, foldedGauss, (indicatorMap.natural at gaussSet(a, p, h), gaussSet(a, p, h)), (indicatorMap.natural at ∅, gaussSet(a, p, h)), agreeGauss rw [natSum.self_indicator with gaussFinite, natSum.empty_indicator] at law have residueGaussNat := natSum.natural with gaussFinite, residueGauss have foldedGaussNat := natSum.natural with gaussFinite, foldedGauss have foldedRestNat := natSum.natural with restFinite, foldedRest rw [splitResidue, splitFolded, sameRest] have commute : mul(cardinality(gaussSet(a, p, h)), p) = mul(p, cardinality(gaussSet(a, p, h))) by ring rw commute rw add.right_unit at law rw ← law arith -- The folding permutes 1..h: the folded residue of k ∈ 1..h lies in 1..h, -- and two residues folding to the same value come from the same k. theorem foldedWeights.member (a p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires notDivides : ¬(p ∣ a) shows ∀ k ∈ halfRange(h), foldedWeights(a, p, h)(k) ∈ halfRange(h) proof intros k hk have nonzero := Prime.nonzero with prime have kNat := halfRange.member_nat with hk have bounds := (halfRange.mem_iff at h, k).forward with hk obtain ⟨kPositive, kBound⟩ := bounds have coprime := Prime.coprime_of_not_divides_gcd with prime, notDivides have residueBelow := mod.less at a * k, p with nonzero -- The residue is not 0, since p ∤ a k. have residuePositive : 0 < (a * k) % p have notZero : (a * k) % p ≠ 0 intro zero have divides := (mod.zero_iff at p, a * k with nonzero).forward with zero have cancelled := coprime.cancel with coprime, divides have kNonzero : k ≠ 0 by arith have le := divides.le with cancelled, kNonzero have impossible : ¬p ≤ k by arith exact impossible with le arith have cases := Less.or_LessEqual at h, (a * k) % p cases cases with large | small . rw foldedWeights.large with hk, large have residueLe : (a * k) % p ≤ p by arith have pieces := sub.add_cancel with residueLe have lower : 0 < p - (a * k) % p by arith have upper : p - (a * k) % p ≤ h by arith exact (halfRange.mem_iff at h, p - (a * k) % p).backward with ⟨lower, upper⟩ . rw foldedWeights.small with hk, small exact (halfRange.mem_iff at h, (a * k) % p).backward with ⟨residuePositive, small⟩ theorem foldedWeights.self_map (a p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires notDivides : ¬(p ∣ a) shows foldedWeights(a, p, h) : halfRange(h) → halfRange(h) proof have function := foldedWeights.function at a, p, h have values := foldedWeights.member with prime, odd, notDivides exact FunctionBetween.codomain_of_values with function, values -- Equal folded values mean a k ≡ ±a j, so k ≡ ±j with p ∤ a; the sum -- 0 < k + j ≤ 2h < p rules out the sign −, and k, j < p give k = j. theorem foldedWeights.same (a p h k j : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires notDivides : ¬(p ∣ a) requires hk : k ∈ halfRange(h) requires hj : j ∈ halfRange(h) requires same : foldedWeights(a, p, h)(k) = foldedWeights(a, p, h)(j) shows k = j proof have nonzero := Prime.nonzero with prime have kBounds := (halfRange.mem_iff at h, k).forward with hk obtain ⟨kPositive, kBound⟩ := kBounds have jBounds := (halfRange.mem_iff at h, j).forward with hj obtain ⟨jPositive, jBound⟩ := jBounds have coprime := Prime.coprime_of_not_divides_gcd with prime, notDivides have kBelow : k < p by arith have jBelow : j < p by arith have kSelf := mod.of_less with kBelow have jSelf := mod.of_less with jBelow have kResidue := mod.less at a * k, p with nonzero have jResidue := mod.less at a * j, p with nonzero have kLe : (a * k) % p ≤ p by arith have jLe : (a * j) % p ≤ p by arith have kPieces := sub.add_cancel with kLe have jPieces := sub.add_cancel with jLe have ofEqual : (a * k) % p = (a * j) % p → k = j intro equal have cancelled := coprime.mul_cancel_mod with nonzero, coprime, equal rwa [kSelf, jSelf] at cancelled have ofSum : (a * k) % p + (a * j) % p = p → k = j intro total exfalso have split := mod.add at p, a * k, a * j with nonzero have vanish := mod.self at p rw [total, vanish] at split have factor : a * k + a * j = a * (k + j) by ring rw factor at split have divides := (mod.zero_iff at p, a * (k + j) with nonzero).forward with split have cancelled := coprime.cancel with coprime, divides have sumNonzero : k + j ≠ 0 by arith have le := divides.le with cancelled, sumNonzero have impossible : ¬p ≤ k + j by arith exact impossible with le have kCases := Less.or_LessEqual at h, (a * k) % p have jCases := Less.or_LessEqual at h, (a * j) % p cases kCases with kLarge | kSmall . rw foldedWeights.large with hk, kLarge at same cases jCases with jLarge | jSmall . rw foldedWeights.large with hj, jLarge at same apply ofEqual arith . rw foldedWeights.small with hj, jSmall at same apply ofSum arith . rw foldedWeights.small with hk, kSmall at same cases jCases with jLarge | jSmall . rw foldedWeights.large with hj, jLarge at same apply ofSum arith . rw foldedWeights.small with hj, jSmall at same exact ofEqual with same theorem foldedWeights.injective (a p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires notDivides : ¬(p ∣ a) shows InjectiveBetween(foldedWeights(a, p, h), halfRange(h), halfRange(h)) proof have function := foldedWeights.self_map with prime, odd, notDivides constructor . exact function . intros x hx y hy z hz hxz hyz have xValue := FunctionBetween.apply_value with function, hx, hxz have yValue := FunctionBetween.apply_value with function, hy, hyz have xNat := halfRange.member_nat with hx have yNat := halfRange.member_nat with hy have same : foldedWeights(a, p, h)(x) = foldedWeights(a, p, h)(y) rw [xValue, yValue] rfl exact foldedWeights.same with prime, odd, notDivides, hx, hy, same -- An injection of the finite set 1..h into itself is onto. theorem foldedWeights.bijective (a p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires notDivides : ¬(p ∣ a) shows BijectiveBetween(foldedWeights(a, p, h), halfRange(h), halfRange(h)) proof have function := foldedWeights.self_map with prime, odd, notDivides have injective := foldedWeights.injective with prime, odd, notDivides have finite := halfRange.finite at h have reflexive : halfRange(h) ⊆ halfRange(h) intros y hy exact hy have imageInside := functionImage.subset at foldedWeights(a, p, h), halfRange(h), halfRange(h) have count := cardinality.injective_image with injective, reflexive, finite have onto := cardinality.eq_subset with finite, imageInside, count have surjective := functionImage.surjective with function rw onto at surjective exact ⟨injective, surjective⟩ -- The folded residues permute 1..h, so their sum is Σ k: reindex the sum -- along the folding bijection. theorem natSum.folded_permutation (a p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires notDivides : ¬(p ∣ a) shows natSum(halfRange(h), foldedWeights(a, p, h)) = natSum(halfRange(h), identityWeights(h)) proof have finite := halfRange.finite at h have bijective := foldedWeights.bijective with prime, odd, notDivides have members := foldedWeights.member with prime, odd, notDivides have values : ∀ s ∈ halfRange(h), foldedWeights(a, p, h)(s) = identityWeights(h)(foldedWeights(a, p, h)(s)) intros s hs have inside := members with s, hs rw identityWeights.value with inside rfl have reindexed := natSum.reindex with finite, bijective, (identityWeights.natural at h), values symm exact reindexed -- Eisenstein's parity. Combining the three identities, -- (a − 1) Σ k = p (Σ ⌊a k / p⌋ + μ) − 2 Σ_{Gauss} r'_k; with a odd the -- left side is even and p is odd, so μ + Σ ⌊a k / p⌋ is even. The -- bookkeeping is done on naturals by canceling Σ r_k and Σ k from both -- sides of the combined identity. theorem gauss.parity_arithmetic (p m F count I R G : Nat) requires combined : (2 * m + 1) * I + (R + 2 * G) = (p * F + R) + (I + count * p) shows p * (F + count) = 2 * (m * I + G) proof have left : p * (F + count) + (R + I) = (p * F + R) + (I + count * p) by ring have right : 2 * (m * I + G) + (R + I) = (2 * m + 1) * I + (R + 2 * G) by ring rw [← combined, ← right] at left arith theorem gauss.parity (a p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 requires oddA : Odd(a) requires notDivides : ¬(p ∣ a) shows Even(add(cardinality(gaussSet(a, p, h)), natSum(halfRange(h), floorWeights(a, p, h)))) proof have positive : 0 < p by arith have finite := halfRange.finite at h have gaussFinite := gaussSet.finite at a, p, h have subset := gaussSet.subset at a, p, h have foldedGauss : ∀ v ∈ gaussSet(a, p, h), foldedWeights(a, p, h)(v) ∈ ℕ intros v hv exact foldedWeights.natural at a, p, h with v, (subset with v, hv) have floorNat := natSum.natural with finite, (floorWeights.natural at a, p, h) have residueNat := natSum.natural with finite, (residueWeights.natural at a, p, h) have identityNat := natSum.natural with finite, (identityWeights.natural at h) have foldedGaussNat := natSum.natural with gaussFinite, foldedGauss -- The three identities, with Σ a k written as a Σ k and the folded sum -- as Σ k. have division := natSum.division_split at a, p, h with positive have scaled := natSum.scaled at a, h have fold := natSum.fold_split at a, p, h with positive have permutation := natSum.folded_permutation with prime, odd, notDivides rw [scaled] at division rw [permutation] at fold have oddForm := (Odd.iff_two_mul_add_one at a).forward with oddA obtain ⟨m, mNat, aForm⟩ := oddForm have scaledA : mul(a, natSum(halfRange(h), identityWeights(h))) = mul(2 * m + 1, natSum(halfRange(h), identityWeights(h))) rw aForm rfl have combined : add(mul(2 * m + 1, natSum(halfRange(h), identityWeights(h))), add(natSum(halfRange(h), residueWeights(a, p, h)), mul(2, natSum(gaussSet(a, p, h), foldedWeights(a, p, h))))) = add(add(mul(p, natSum(halfRange(h), floorWeights(a, p, h))), natSum(halfRange(h), residueWeights(a, p, h))), add(natSum(halfRange(h), identityWeights(h)), mul(cardinality(gaussSet(a, p, h)), p))) rw [← scaledA, division, fold] rfl have equation : mul(p, add(natSum(halfRange(h), floorWeights(a, p, h)), cardinality(gaussSet(a, p, h)))) = mul(2, add(mul(m, natSum(halfRange(h), identityWeights(h))), natSum(gaussSet(a, p, h), foldedWeights(a, p, h)))) exact gauss.parity_arithmetic at p, m, natSum(halfRange(h), floorWeights(a, p, h)), cardinality(gaussSet(a, p, h)), natSum(halfRange(h), identityWeights(h)), natSum(halfRange(h), residueWeights(a, p, h)), natSum(gaussSet(a, p, h), foldedWeights(a, p, h)) with combined -- p times the sum is even and p is odd. have even : Even(mul(p, add(natSum(halfRange(h), floorWeights(a, p, h)), cardinality(gaussSet(a, p, h))))) rw equation exact Even.two_mul at add(mul(m, natSum(halfRange(h), identityWeights(h))), natSum(gaussSet(a, p, h), foldedWeights(a, p, h))) have split := Even.of_mul with even cases split with evenP | evenSum . exfalso have oddP : Odd(p) rw odd have hNat : Nat(h) by typecheck have form : 2 * h + 1 = 2 * h + 1 by rfl exact (Odd.iff_two_mul_add_one at 2 * h + 1).backward with ⟨h, hNat, form⟩ exact Odd.not_even with oddP, evenP . have commute : add(cardinality(gaussSet(a, p, h)), natSum(halfRange(h), floorWeights(a, p, h))) = add(natSum(halfRange(h), floorWeights(a, p, h)), cardinality(gaussSet(a, p, h))) by ring rw commute exact evenSum