-- The law of quadratic reciprocity: for distinct odd primes p = 2hp + 1 -- and q = 2hq + 1, q is a square modulo p exactly when p is a square -- modulo q, unless both are 3 modulo 4, when exactly one is; that is, the -- two residue statements agree exactly when hp · hq is even. -- 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.QuadraticReciprocity import Grace.Cumulus.NumberTheory.LatticeCount import Grace.Cumulus.NumberTheory.GaussLemma import Grace.Cumulus.NumberTheory.EulerCriterion -- The law -- Each residue statement is the parity of a Gauss count (gauss.residue_iff), -- each count has the parity of a floor sum (gauss.parity, with q odd and -- p ∤ q), and the two floor sums add to hp · hq (lattice.count). theorem quadratic_reciprocity (p q hp hq : Nat) requires primeP : Prime(p) requires primeQ : Prime(q) requires distinct : p ≠ q requires oddP : p = 2 * hp + 1 requires oddQ : q = 2 * hq + 1 shows (QuadraticResidue(q, p) ↔ QuadraticResidue(p, q)) ↔ Even(hp * hq) proof have distinctQ : q ≠ p by cc have notDividesQ := Prime.not_divides_distinct with primeP, primeQ, distinct have notDividesP := Prime.not_divides_distinct with primeQ, primeP, distinctQ have hpNat : Nat(hp) by typecheck have hqNat : Nat(hq) by typecheck have oddPrimeP : Odd(p) exact (Odd.iff_two_mul_add_one at p).backward with ⟨hp, hpNat, oddP⟩ have oddPrimeQ : Odd(q) exact (Odd.iff_two_mul_add_one at q).backward with ⟨hq, hqNat, oddQ⟩ have residueQ := gauss.residue_iff at q, p, hp with primeP, oddP, notDividesQ have residueP := gauss.residue_iff at p, q, hq with primeQ, oddQ, notDividesP have parityQ := gauss.parity at q, p, hp with primeP, oddP, oddPrimeQ, notDividesQ have parityP := gauss.parity at p, q, hq with primeQ, oddQ, oddPrimeP, notDividesP have count := lattice.count with primeP, primeQ, distinct, oddP, oddQ have sumQ := natSum.natural with (halfRange.finite at hp), (floorWeights.natural at q, p, hp) have sumP := natSum.natural with (halfRange.finite at hq), (floorWeights.natural at p, q, hq) have splitQ := (Even.add_iff at cardinality(gaussSet(q, p, hp)), natSum(halfRange(hp), floorWeights(q, p, hp))).forward with parityQ have splitP := (Even.add_iff at cardinality(gaussSet(p, q, hq)), natSum(halfRange(hq), floorWeights(p, q, hq))).forward with parityP have splitCount := Even.add_iff at natSum(halfRange(hp), floorWeights(q, p, hp)), natSum(halfRange(hq), floorWeights(p, q, hq)) rw count at splitCount tauto theorem quadratic_reciprocity.one_mod_four (p q hp hq : Nat) requires primeP : Prime(p) requires primeQ : Prime(q) requires distinct : p ≠ q requires oddP : p = 2 * hp + 1 requires oddQ : q = 2 * hq + 1 requires pOne : p % 4 = 1 shows QuadraticResidue(q, p) ↔ QuadraticResidue(p, q) proof have law := quadratic_reciprocity with primeP, primeQ, distinct, oddP, oddQ have evenHalf := (mod.four_eq_one_iff with oddP).forward with pOne have even := Even.mul_left at hp, hq with evenHalf exact law.backward with even theorem quadratic_reciprocity.three_mod_four (p q hp hq : Nat) requires primeP : Prime(p) requires primeQ : Prime(q) requires distinct : p ≠ q requires oddP : p = 2 * hp + 1 requires oddQ : q = 2 * hq + 1 requires pThree : p % 4 = 3 requires qThree : q % 4 = 3 shows QuadraticResidue(q, p) ↔ ¬QuadraticResidue(p, q) proof have law := quadratic_reciprocity with primeP, primeQ, distinct, oddP, oddQ have oddHalfP := (mod.four_eq_three_iff with oddP).forward with pThree have oddHalfQ := (mod.four_eq_three_iff with oddQ).forward with qThree have oddProduct := Odd.mul with oddHalfP, oddHalfQ have notEven := Odd.not_even with oddProduct tauto