Grace/Cumulus/NumberTheory/IntegerPrime.g17
Imports
Grace.Cumulus.NumberTheory.IntegerDivisibility · Grace.Cumulus.NumberTheory.PerfectSquares · Grace.Cumulus.NumberTheory.CoprimeSquares
Declarations
- Prime.integer_divides_mul theorem derivation
- IntegerCongruent.square_one theorem derivation
- Prime.square_mod_one theorem derivation
- Prime.square_mod_zero theorem derivation
- Prime.odd_half_positive theorem derivation
Source
1-- Euclid's lemma for a natural prime dividing an integer product, and the2-- two square roots of one modulo a prime.3-- Part of Cumulus, the Grace17 mathematics library.4-- Copyright (c) 2026 Grant Passmore.5-- Released under the Apache License 2.0. See LICENSE in the repository root.6-- Authors: Grant Passmore78module Grace.Cumulus.NumberTheory.IntegerPrime9import Grace.Cumulus.NumberTheory.IntegerDivisibility10import Grace.Cumulus.NumberTheory.PerfectSquares11import Grace.Cumulus.NumberTheory.CoprimeSquares1213-- Euclid's lemma over the integers1415-- Through the absolute values: p divides |x| · |y| in ℕ, so p divides one16-- of them, and a natural prime divides an integer exactly when it divides17-- its absolute value.18theorem Prime.integer_divides_mul (p : Nat) (x y : Integer)19 requires prime : Prime(p)20 requires divides : IntegerDivides(integerOfNat(p), x * y)21 shows IntegerDivides(integerOfNat(p), x) ∨22 IntegerDivides(integerOfNat(p), y)23proof24 have bridge := IntegerDivides.natural_left at p, x * y25 have magnitudes := bridge.forward with divides26 have product := integerNatAbs.mul at x, y27 rw product at magnitudes28 have split := Prime.divides_mul with prime, magnitudes29 cases split with left | right30 . left31 exact IntegerDivides.natural_left.backward at p, x with left32 . right33 exact IntegerDivides.natural_left.backward at p, y with right3435-- Square roots of one and of zero modulo a prime3637-- x² ≡ 1 factors as (x − 1)(x + 1) ≡ 0, with the integer one written as38-- integerOne: a bare numeral in an IntegerCongruent argument is the39-- natural one, so this is the form the natural corollary below uses.40theorem IntegerCongruent.square_one (p : Nat) (x : Integer)41 requires prime : Prime(p)42 requires square : IntegerCongruent(integerOfNat(p), x * x, integerOne)43 shows IntegerCongruent(integerOfNat(p), x, integerOne) ∨44 IntegerCongruent(integerOfNat(p), x, -integerOne)45proof46 unfold IntegerCongruent at square47 have factored : x * x - integerOne = (x - integerOne) * (x + integerOne)48 ring49 rw factored at square50 have split := Prime.integer_divides_mul with prime, square51 cases split with left | right52 . left53 unfold IntegerCongruent54 exact left55 . right56 unfold IntegerCongruent57 have shape : x - (-integerOne) = x + integerOne by ring58 rw shape59 exact right6061-- The same in ℕ: a natural whose square is 1 modulo p is 1 or −1 modulo p.62theorem Prime.square_mod_one (p x : Nat)63 requires prime : Prime(p)64 requires square : (x * x) % p = 165 shows x % p = 1 ∨ (x + 1) % p = 066proof67 have above := Prime.greater_than_one with prime68 have oneValue := mod.one_left with above69 have congruent : CongruentMod(x * x, 1, p)70 unfold CongruentMod71 rw [square, oneValue]72 rfl73 have lifted := IntegerCongruent.ofNat_iff.backward at p, x * x, 174 with congruent75 rw [integerOfNat.mul, integerOfNat.one] at lifted76 have split := IntegerCongruent.square_one with prime, lifted77 cases split with one | negOne78 . left79 rw ← integerOfNat.one at one80 have natural := IntegerCongruent.ofNat_iff.forward at p, x, 1 with one81 unfold CongruentMod at natural82 rw oneValue at natural83 exact natural84 . -- x ≡ −1 shifts to x + 1 ≡ 0, a congruence of naturals.85 right86 have reflexive := IntegerCongruent.reflexive at integerOfNat(p), integerOne87 have sum := IntegerCongruent.add with negOne, reflexive88 have cancel : -integerOne + integerOne = integerZero by ring89 have successor := integerOfNat.add_one at x90 rw [cancel, ← successor, ← integerOfNat.zero] at sum91 have natural := IntegerCongruent.ofNat_iff.forward at p, x + 1, 0 with sum92 unfold CongruentMod at natural93 rw mod.zero_left at natural94 exact natural9596-- A natural whose square is 0 modulo p is 0 modulo p.97theorem Prime.square_mod_zero (p x : Nat)98 requires prime : Prime(p)99 requires square : (x * x) % p = 0100 shows x % p = 0101proof102 have nonzero := Prime.nonzero with prime103 have characterization := mod.zero_iff at p, x * x with nonzero104 have divides := characterization.forward with square105 have split := Prime.divides_mul with prime, divides106 cases split with left | right107 . exact mod.of_divides with left108 . exact mod.of_divides with right109110-- Primes and squares111112-- An odd prime 2h + 1 has h ≥ 1, so p ≥ 3 and 1 < p − 1 = 2h.113theorem Prime.odd_half_positive (p h : Nat)114 requires prime : Prime(p)115 requires odd : p = 2 * h + 1116 shows 0 < h117proof118 have above := Prime.greater_than_one with prime119 arith120