Grace/Cumulus/NumberTheory/QuadraticReciprocity.g17
Imports
Grace.Cumulus.NumberTheory.LatticeCount · Grace.Cumulus.NumberTheory.GaussLemma · Grace.Cumulus.NumberTheory.EulerCriterion
Declarations
- quadratic_reciprocity theorem derivation
- quadratic_reciprocity.one_mod_four theorem derivation
- quadratic_reciprocity.three_mod_four theorem derivation
Source
1-- The law of quadratic reciprocity: for distinct odd primes p = 2hp + 12-- and q = 2hq + 1, q is a square modulo p exactly when p is a square3-- modulo q, unless both are 3 modulo 4, when exactly one is; that is, the4-- two residue statements agree exactly when hp · hq is even.5-- Part of Cumulus, the Grace17 mathematics library.6-- Copyright (c) 2026 Grant Passmore.7-- Released under the Apache License 2.0. See LICENSE in the repository root.8-- Authors: Grant Passmore910module Grace.Cumulus.NumberTheory.QuadraticReciprocity11import Grace.Cumulus.NumberTheory.LatticeCount12import Grace.Cumulus.NumberTheory.GaussLemma13import Grace.Cumulus.NumberTheory.EulerCriterion1415-- The law1617-- Each residue statement is the parity of a Gauss count (gauss.residue_iff),18-- each count has the parity of a floor sum (gauss.parity, with q odd and19-- p ∤ q), and the two floor sums add to hp · hq (lattice.count).20theorem quadratic_reciprocity (p q hp hq : Nat)21 requires primeP : Prime(p)22 requires primeQ : Prime(q)23 requires distinct : p ≠ q24 requires oddP : p = 2 * hp + 125 requires oddQ : q = 2 * hq + 126 shows (QuadraticResidue(q, p) ↔ QuadraticResidue(p, q)) ↔ Even(hp * hq)27proof28 have distinctQ : q ≠ p by cc29 have notDividesQ := Prime.not_divides_distinct with primeP, primeQ, distinct30 have notDividesP := Prime.not_divides_distinct with primeQ, primeP, distinctQ31 have hpNat : Nat(hp) by typecheck32 have hqNat : Nat(hq) by typecheck33 have oddPrimeP : Odd(p)34 exact (Odd.iff_two_mul_add_one at p).backward with ⟨hp, hpNat, oddP⟩35 have oddPrimeQ : Odd(q)36 exact (Odd.iff_two_mul_add_one at q).backward with ⟨hq, hqNat, oddQ⟩37 have residueQ := gauss.residue_iff at q, p, hp with primeP, oddP, notDividesQ38 have residueP := gauss.residue_iff at p, q, hq with primeQ, oddQ, notDividesP39 have parityQ := gauss.parity at q, p, hp with primeP, oddP, oddPrimeQ,40 notDividesQ41 have parityP := gauss.parity at p, q, hq with primeQ, oddQ, oddPrimeP,42 notDividesP43 have count := lattice.count with primeP, primeQ, distinct, oddP, oddQ44 have sumQ := natSum.natural with (halfRange.finite at hp),45 (floorWeights.natural at q, p, hp)46 have sumP := natSum.natural with (halfRange.finite at hq),47 (floorWeights.natural at p, q, hq)48 have splitQ := (Even.add_iff at cardinality(gaussSet(q, p, hp)),49 natSum(halfRange(hp), floorWeights(q, p, hp))).forward with parityQ50 have splitP := (Even.add_iff at cardinality(gaussSet(p, q, hq)),51 natSum(halfRange(hq), floorWeights(p, q, hq))).forward with parityP52 have splitCount := Even.add_iff53 at natSum(halfRange(hp), floorWeights(q, p, hp)),54 natSum(halfRange(hq), floorWeights(p, q, hq))55 rw count at splitCount56 tauto5758theorem quadratic_reciprocity.one_mod_four (p q hp hq : Nat)59 requires primeP : Prime(p)60 requires primeQ : Prime(q)61 requires distinct : p ≠ q62 requires oddP : p = 2 * hp + 163 requires oddQ : q = 2 * hq + 164 requires pOne : p % 4 = 165 shows QuadraticResidue(q, p) ↔ QuadraticResidue(p, q)66proof67 have law := quadratic_reciprocity with primeP, primeQ, distinct, oddP, oddQ68 have evenHalf := (mod.four_eq_one_iff with oddP).forward with pOne69 have even := Even.mul_left at hp, hq with evenHalf70 exact law.backward with even7172theorem quadratic_reciprocity.three_mod_four (p q hp hq : Nat)73 requires primeP : Prime(p)74 requires primeQ : Prime(q)75 requires distinct : p ≠ q76 requires oddP : p = 2 * hp + 177 requires oddQ : q = 2 * hq + 178 requires pThree : p % 4 = 379 requires qThree : q % 4 = 380 shows QuadraticResidue(q, p) ↔ ¬QuadraticResidue(p, q)81proof82 have law := quadratic_reciprocity with primeP, primeQ, distinct, oddP, oddQ83 have oddHalfP := (mod.four_eq_three_iff with oddP).forward with pThree84 have oddHalfQ := (mod.four_eq_three_iff with oddQ).forward with qThree85 have oddProduct := Odd.mul with oddHalfP, oddHalfQ86 have notEven := Odd.not_even with oddProduct87 tauto88