-- Euclid's lemma for a natural prime dividing an integer product, and the -- two square roots of one modulo a prime. -- 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.IntegerPrime import Grace.Cumulus.NumberTheory.IntegerDivisibility import Grace.Cumulus.NumberTheory.PerfectSquares import Grace.Cumulus.NumberTheory.CoprimeSquares -- Euclid's lemma over the integers -- Through the absolute values: p divides |x| · |y| in ℕ, so p divides one -- of them, and a natural prime divides an integer exactly when it divides -- its absolute value. theorem Prime.integer_divides_mul (p : Nat) (x y : Integer) requires prime : Prime(p) requires divides : IntegerDivides(integerOfNat(p), x * y) shows IntegerDivides(integerOfNat(p), x) ∨ IntegerDivides(integerOfNat(p), y) proof have bridge := IntegerDivides.natural_left at p, x * y have magnitudes := bridge.forward with divides have product := integerNatAbs.mul at x, y rw product at magnitudes have split := Prime.divides_mul with prime, magnitudes cases split with left | right . left exact IntegerDivides.natural_left.backward at p, x with left . right exact IntegerDivides.natural_left.backward at p, y with right -- Square roots of one and of zero modulo a prime -- x² ≡ 1 factors as (x − 1)(x + 1) ≡ 0, with the integer one written as -- integerOne: a bare numeral in an IntegerCongruent argument is the -- natural one, so this is the form the natural corollary below uses. theorem IntegerCongruent.square_one (p : Nat) (x : Integer) requires prime : Prime(p) requires square : IntegerCongruent(integerOfNat(p), x * x, integerOne) shows IntegerCongruent(integerOfNat(p), x, integerOne) ∨ IntegerCongruent(integerOfNat(p), x, -integerOne) proof unfold IntegerCongruent at square have factored : x * x - integerOne = (x - integerOne) * (x + integerOne) ring rw factored at square have split := Prime.integer_divides_mul with prime, square cases split with left | right . left unfold IntegerCongruent exact left . right unfold IntegerCongruent have shape : x - (-integerOne) = x + integerOne by ring rw shape exact right -- The same in ℕ: a natural whose square is 1 modulo p is 1 or −1 modulo p. theorem Prime.square_mod_one (p x : Nat) requires prime : Prime(p) requires square : (x * x) % p = 1 shows x % p = 1 ∨ (x + 1) % p = 0 proof have above := Prime.greater_than_one with prime have oneValue := mod.one_left with above have congruent : CongruentMod(x * x, 1, p) unfold CongruentMod rw [square, oneValue] rfl have lifted := IntegerCongruent.ofNat_iff.backward at p, x * x, 1 with congruent rw [integerOfNat.mul, integerOfNat.one] at lifted have split := IntegerCongruent.square_one with prime, lifted cases split with one | negOne . left rw ← integerOfNat.one at one have natural := IntegerCongruent.ofNat_iff.forward at p, x, 1 with one unfold CongruentMod at natural rw oneValue at natural exact natural . -- x ≡ −1 shifts to x + 1 ≡ 0, a congruence of naturals. right have reflexive := IntegerCongruent.reflexive at integerOfNat(p), integerOne have sum := IntegerCongruent.add with negOne, reflexive have cancel : -integerOne + integerOne = integerZero by ring have successor := integerOfNat.add_one at x rw [cancel, ← successor, ← integerOfNat.zero] at sum have natural := IntegerCongruent.ofNat_iff.forward at p, x + 1, 0 with sum unfold CongruentMod at natural rw mod.zero_left at natural exact natural -- A natural whose square is 0 modulo p is 0 modulo p. theorem Prime.square_mod_zero (p x : Nat) requires prime : Prime(p) requires square : (x * x) % p = 0 shows x % p = 0 proof have nonzero := Prime.nonzero with prime have characterization := mod.zero_iff at p, x * x with nonzero have divides := characterization.forward with square have split := Prime.divides_mul with prime, divides cases split with left | right . exact mod.of_divides with left . exact mod.of_divides with right -- Primes and squares -- An odd prime 2h + 1 has h ≥ 1, so p ≥ 3 and 1 < p − 1 = 2h. theorem Prime.odd_half_positive (p h : Nat) requires prime : Prime(p) requires odd : p = 2 * h + 1 shows 0 < h proof have above := Prime.greater_than_one with prime arith