Datasets:

Modalities:
Text
Languages:
English
Libraries:
Datasets
License:
Zhangir Azerbayev
squashed?
4365a98
raw
history blame
17 kB
/-
Copyright (c) 2020 Scott Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Scott Morrison, Ainsley Pahljina
-/
import data.nat.parity
import data.pnat.interval
import data.zmod.basic
import group_theory.order_of_element
import ring_theory.fintype
import tactic.interval_cases
import tactic.ring_exp
/-!
# The Lucas-Lehmer test for Mersenne primes.
We define `lucas_lehmer_residue : Ξ  p : β„•, zmod (2^p - 1)`, and
prove `lucas_lehmer_residue p = 0 β†’ prime (mersenne p)`.
We construct a tactic `lucas_lehmer.run_test`, which iteratively certifies the arithmetic
required to calculate the residue, and enables us to prove
```
example : prime (mersenne 127) :=
lucas_lehmer_sufficiency _ (by norm_num) (by lucas_lehmer.run_test)
```
## TODO
- Show reverse implication.
- Speed up the calculations using `n ≑ (n % 2^p) + (n / 2^p) [MOD 2^p - 1]`.
- Find some bigger primes!
## History
This development began as a student project by Ainsley Pahljina,
and was then cleaned up for mathlib by Scott Morrison.
The tactic for certified computation of Lucas-Lehmer residues was provided by Mario Carneiro.
-/
/-- The Mersenne numbers, 2^p - 1. -/
def mersenne (p : β„•) : β„• := 2^p - 1
lemma mersenne_pos {p : β„•} (h : 0 < p) : 0 < mersenne p :=
begin
dsimp [mersenne],
calc 0 < 2^1 - 1 : by norm_num
... ≀ 2^p - 1 : nat.pred_le_pred (nat.pow_le_pow_of_le_right (nat.succ_pos 1) h)
end
@[simp]
lemma succ_mersenne (k : β„•) : mersenne k + 1 = 2 ^ k :=
begin
rw [mersenne, tsub_add_cancel_of_le],
exact one_le_pow_of_one_le (by norm_num) k
end
namespace lucas_lehmer
open nat
/-!
We now define three(!) different versions of the recurrence
`s (i+1) = (s i)^2 - 2`.
These versions take values either in `β„€`, in `zmod (2^p - 1)`, or
in `β„€` but applying `% (2^p - 1)` at each step.
They are each useful at different points in the proof,
so we take a moment setting up the lemmas relating them.
-/
/-- The recurrence `s (i+1) = (s i)^2 - 2` in `β„€`. -/
def s : β„• β†’ β„€
| 0 := 4
| (i+1) := (s i)^2 - 2
/-- The recurrence `s (i+1) = (s i)^2 - 2` in `zmod (2^p - 1)`. -/
def s_zmod (p : β„•) : β„• β†’ zmod (2^p - 1)
| 0 := 4
| (i+1) := (s_zmod i)^2 - 2
/-- The recurrence `s (i+1) = ((s i)^2 - 2) % (2^p - 1)` in `β„€`. -/
def s_mod (p : β„•) : β„• β†’ β„€
| 0 := 4 % (2^p - 1)
| (i+1) := ((s_mod i)^2 - 2) % (2^p - 1)
lemma mersenne_int_ne_zero (p : β„•) (w : 0 < p) : (2^p - 1 : β„€) β‰  0 :=
begin
apply ne_of_gt, simp only [gt_iff_lt, sub_pos],
exact_mod_cast nat.one_lt_two_pow p w,
end
lemma s_mod_nonneg (p : β„•) (w : 0 < p) (i : β„•) : 0 ≀ s_mod p i :=
begin
cases i; dsimp [s_mod],
{ exact sup_eq_left.mp rfl },
{ apply int.mod_nonneg, exact mersenne_int_ne_zero p w },
end
lemma s_mod_mod (p i : β„•) : s_mod p i % (2^p - 1) = s_mod p i :=
by cases i; simp [s_mod]
lemma s_mod_lt (p : β„•) (w : 0 < p) (i : β„•) : s_mod p i < 2^p - 1 :=
begin
rw ←s_mod_mod,
convert int.mod_lt _ _,
{ refine (abs_of_nonneg _).symm,
simp only [sub_nonneg, ge_iff_le],
exact_mod_cast nat.one_le_two_pow p, },
{ exact mersenne_int_ne_zero p w, },
end
lemma s_zmod_eq_s (p' : β„•) (i : β„•) : s_zmod (p'+2) i = (s i : zmod (2^(p'+2) - 1)):=
begin
induction i with i ih,
{ dsimp [s, s_zmod], norm_num, },
{ push_cast [s, s_zmod, ih] },
end
-- These next two don't make good `norm_cast` lemmas.
lemma int.coe_nat_pow_pred (b p : β„•) (w : 0 < b) : ((b^p - 1 : β„•) : β„€) = (b^p - 1 : β„€) :=
begin
have : 1 ≀ b^p := nat.one_le_pow p b w,
norm_cast
end
lemma int.coe_nat_two_pow_pred (p : β„•) : ((2^p - 1 : β„•) : β„€) = (2^p - 1 : β„€) :=
int.coe_nat_pow_pred 2 p dec_trivial
lemma s_zmod_eq_s_mod (p : β„•) (i : β„•) : s_zmod p i = (s_mod p i : zmod (2^p - 1)) :=
by induction i; push_cast [←int.coe_nat_two_pow_pred p, s_mod, s_zmod, *]
/-- The Lucas-Lehmer residue is `s p (p-2)` in `zmod (2^p - 1)`. -/
def lucas_lehmer_residue (p : β„•) : zmod (2^p - 1) := s_zmod p (p-2)
lemma residue_eq_zero_iff_s_mod_eq_zero (p : β„•) (w : 1 < p) :
lucas_lehmer_residue p = 0 ↔ s_mod p (p-2) = 0 :=
begin
dsimp [lucas_lehmer_residue],
rw s_zmod_eq_s_mod p,
split,
{ -- We want to use that fact that `0 ≀ s_mod p (p-2) < 2^p - 1`
-- and `lucas_lehmer_residue p = 0 β†’ 2^p - 1 ∣ s_mod p (p-2)`.
intro h,
simp [zmod.int_coe_zmod_eq_zero_iff_dvd] at h,
apply int.eq_zero_of_dvd_of_nonneg_of_lt _ _ h; clear h,
apply s_mod_nonneg _ (nat.lt_of_succ_lt w),
exact s_mod_lt _ (nat.lt_of_succ_lt w) (p-2) },
{ intro h, rw h, simp, },
end
/--
A Mersenne number `2^p-1` is prime if and only if
the Lucas-Lehmer residue `s p (p-2) % (2^p - 1)` is zero.
-/
@[derive decidable_pred]
def lucas_lehmer_test (p : β„•) : Prop := lucas_lehmer_residue p = 0
/-- `q` is defined as the minimum factor of `mersenne p`, bundled as an `β„•+`. -/
def q (p : β„•) : β„•+ := ⟨nat.min_fac (mersenne p), nat.min_fac_pos (mersenne p)⟩
local attribute [instance]
lemma fact_pnat_pos (q : β„•+) : fact (0 < (q : β„•)) := ⟨q.2⟩
/-- We construct the ring `X q` as β„€/qβ„€ + √3 β„€/qβ„€. -/
-- It would be nice to define this as (β„€/qβ„€)[x] / (x^2 - 3),
-- obtaining the ring structure for free,
-- but that seems to be more trouble than it's worth;
-- if it were easy to make the definition,
-- cardinality calculations would be somewhat more involved, too.
@[derive [add_comm_group, decidable_eq, fintype, inhabited]]
def X (q : β„•+) : Type := (zmod q) Γ— (zmod q)
namespace X
variable {q : β„•+}
@[ext]
lemma ext {x y : X q} (h₁ : x.1 = y.1) (hβ‚‚ : x.2 = y.2) : x = y :=
begin
cases x, cases y,
congr; assumption
end
@[simp] lemma add_fst (x y : X q) : (x + y).1 = x.1 + y.1 := rfl
@[simp] lemma add_snd (x y : X q) : (x + y).2 = x.2 + y.2 := rfl
@[simp] lemma neg_fst (x : X q) : (-x).1 = -x.1 := rfl
@[simp] lemma neg_snd (x : X q) : (-x).2 = -x.2 := rfl
instance : has_mul (X q) :=
{ mul := Ξ» x y, (x.1*y.1 + 3*x.2*y.2, x.1*y.2 + x.2*y.1) }
@[simp] lemma mul_fst (x y : X q) : (x * y).1 = x.1 * y.1 + 3 * x.2 * y.2 := rfl
@[simp] lemma mul_snd (x y : X q) : (x * y).2 = x.1 * y.2 + x.2 * y.1 := rfl
instance : has_one (X q) :=
{ one := ⟨1,0⟩ }
@[simp] lemma one_fst : (1 : X q).1 = 1 := rfl
@[simp] lemma one_snd : (1 : X q).2 = 0 := rfl
@[simp] lemma bit0_fst (x : X q) : (bit0 x).1 = bit0 x.1 := rfl
@[simp] lemma bit0_snd (x : X q) : (bit0 x).2 = bit0 x.2 := rfl
@[simp] lemma bit1_fst (x : X q) : (bit1 x).1 = bit1 x.1 := rfl
@[simp] lemma bit1_snd (x : X q) : (bit1 x).2 = bit0 x.2 := by { dsimp [bit1], simp, }
instance : monoid (X q) :=
{ mul_assoc := Ξ» x y z, by { ext; { dsimp, ring }, },
one := ⟨1,0⟩,
one_mul := Ξ» x, by { ext; simp, },
mul_one := Ξ» x, by { ext; simp, },
..(infer_instance : has_mul (X q)) }
instance : add_group_with_one (X q) :=
{ nat_cast := λ n, ⟨n, 0⟩,
nat_cast_zero := by simp,
nat_cast_succ := by simp [nat.cast, monoid.one],
int_cast := λ n, ⟨n, 0⟩,
int_cast_of_nat := Ξ» n, by simp; refl,
int_cast_neg_succ_of_nat := Ξ» n, by ext; simp; refl,
.. X.monoid, .. X.add_comm_group _ }
lemma left_distrib (x y z : X q) : x * (y + z) = x * y + x * z :=
by { ext; { dsimp, ring }, }
lemma right_distrib (x y z : X q) : (x + y) * z = x * z + y * z :=
by { ext; { dsimp, ring }, }
instance : ring (X q) :=
{ left_distrib := left_distrib,
right_distrib := right_distrib,
.. X.add_group_with_one,
..(infer_instance : add_comm_group (X q)),
..(infer_instance : monoid (X q)) }
instance : comm_ring (X q) :=
{ mul_comm := Ξ» x y, by { ext; { dsimp, ring }, },
..(infer_instance : ring (X q))}
instance [fact (1 < (q : β„•))] : nontrivial (X q) :=
⟨⟨0, 1, λ h, by { injection h with h1 _, exact zero_ne_one h1 } ⟩⟩
@[simp] lemma nat_coe_fst (n : β„•) : (n : X q).fst = (n : zmod q) := rfl
@[simp] lemma nat_coe_snd (n : β„•) : (n : X q).snd = (0 : zmod q) := rfl
@[simp] lemma int_coe_fst (n : β„€) : (n : X q).fst = (n : zmod q) := rfl
@[simp] lemma int_coe_snd (n : β„€) : (n : X q).snd = (0 : zmod q) := rfl
@[norm_cast]
lemma coe_mul (n m : β„€) : ((n * m : β„€) : X q) = (n : X q) * (m : X q) :=
by { ext; simp; ring }
@[norm_cast]
lemma coe_nat (n : β„•) : ((n : β„€) : X q) = (n : X q) :=
by { ext; simp, }
/-- The cardinality of `X` is `q^2`. -/
lemma X_card : fintype.card (X q) = q^2 :=
begin
dsimp [X],
rw [fintype.card_prod, zmod.card q],
ring,
end
/-- There are strictly fewer than `q^2` units, since `0` is not a unit. -/
lemma units_card (w : 1 < q) : fintype.card ((X q)Λ£) < q^2 :=
begin
haveI : fact (1 < (q:β„•)) := ⟨w⟩,
convert card_units_lt (X q),
rw X_card,
end
/-- We define `Ο‰ = 2 + √3`. -/
def Ο‰ : X q := (2, 1)
/-- We define `Ο‰b = 2 - √3`, which is the inverse of `Ο‰`. -/
def Ο‰b : X q := (2, -1)
lemma Ο‰_mul_Ο‰b (q : β„•+) : (Ο‰ : X q) * Ο‰b = 1 :=
begin
dsimp [Ο‰, Ο‰b],
ext; simp; ring,
end
lemma Ο‰b_mul_Ο‰ (q : β„•+) : (Ο‰b : X q) * Ο‰ = 1 :=
begin
dsimp [Ο‰, Ο‰b],
ext; simp; ring,
end
/-- A closed form for the recurrence relation. -/
lemma closed_form (i : β„•) : (s i : X q) = (Ο‰ : X q)^(2^i) + (Ο‰b : X q)^(2^i) :=
begin
induction i with i ih,
{ dsimp [s, Ο‰, Ο‰b],
ext; { simp; refl, }, },
{ calc (s (i + 1) : X q) = ((s i)^2 - 2 : β„€) : rfl
... = ((s i : X q)^2 - 2) : by push_cast
... = (Ο‰^(2^i) + Ο‰b^(2^i))^2 - 2 : by rw ih
... = (Ο‰^(2^i))^2 + (Ο‰b^(2^i))^2 + 2*(Ο‰b^(2^i)*Ο‰^(2^i)) - 2 : by ring
... = (Ο‰^(2^i))^2 + (Ο‰b^(2^i))^2 :
by rw [←mul_pow Ο‰b Ο‰, Ο‰b_mul_Ο‰, one_pow, mul_one, add_sub_cancel]
... = Ο‰^(2^(i+1)) + Ο‰b^(2^(i+1)) : by rw [←pow_mul, ←pow_mul, pow_succ'] }
end
end X
open X
/-!
Here and below, we introduce `p' = p - 2`, in order to avoid using subtraction in `β„•`.
-/
/-- If `1 < p`, then `q p`, the smallest prime factor of `mersenne p`, is more than 2. -/
lemma two_lt_q (p' : β„•) : 2 < q (p'+2) := begin
by_contradiction H,
simp at H,
interval_cases q (p'+2); clear H,
{ -- If q = 1, we get a contradiction from 2^p = 2
dsimp [q] at h, injection h with h', clear h,
simp [mersenne] at h',
exact lt_irrefl 2
(calc 2 ≀ p'+2 : nat.le_add_left _ _
... < 2^(p'+2) : nat.lt_two_pow _
... = 2 : nat.pred_inj (nat.one_le_two_pow _) dec_trivial h'), },
{ -- If q = 2, we get a contradiction from 2 ∣ 2^p - 1
dsimp [q] at h, injection h with h', clear h,
rw [mersenne, pnat.one_coe, nat.min_fac_eq_two_iff, pow_succ] at h',
exact nat.two_not_dvd_two_mul_sub_one (nat.one_le_two_pow _) h', }
end
theorem Ο‰_pow_formula (p' : β„•) (h : lucas_lehmer_residue (p'+2) = 0) :
βˆƒ (k : β„€), (Ο‰ : X (q (p'+2)))^(2^(p'+1)) =
k * (mersenne (p'+2)) * ((Ο‰ : X (q (p'+2)))^(2^p')) - 1 :=
begin
dsimp [lucas_lehmer_residue] at h,
rw s_zmod_eq_s p' at h,
simp [zmod.int_coe_zmod_eq_zero_iff_dvd] at h,
cases h with k h,
use k,
replace h := congr_arg (Ξ» (n : β„€), (n : X (q (p'+2)))) h, -- coercion from β„€ to X q
dsimp at h,
rw closed_form at h,
replace h := congr_arg (Ξ» x, Ο‰^2^p' * x) h,
dsimp at h,
have t : 2^p' + 2^p' = 2^(p'+1) := by ring_exp,
rw [mul_add, ←pow_add Ο‰, t, ←mul_pow Ο‰ Ο‰b (2^p'), Ο‰_mul_Ο‰b, one_pow] at h,
rw [mul_comm, coe_mul] at h,
rw [mul_comm _ (k : X (q (p'+2)))] at h,
replace h := eq_sub_of_add_eq h,
have : 1 ≀ 2 ^ (p' + 2) := nat.one_le_pow _ _ dec_trivial,
exact_mod_cast h,
end
/-- `q` is the minimum factor of `mersenne p`, so `M p = 0` in `X q`. -/
theorem mersenne_coe_X (p : β„•) : (mersenne p : X (q p)) = 0 :=
begin
ext; simp [mersenne, q, zmod.nat_coe_zmod_eq_zero_iff_dvd, -pow_pos],
apply nat.min_fac_dvd,
end
theorem Ο‰_pow_eq_neg_one (p' : β„•) (h : lucas_lehmer_residue (p'+2) = 0) :
(Ο‰ : X (q (p'+2)))^(2^(p'+1)) = -1 :=
begin
cases Ο‰_pow_formula p' h with k w,
rw [mersenne_coe_X] at w,
simpa using w,
end
theorem Ο‰_pow_eq_one (p' : β„•) (h : lucas_lehmer_residue (p'+2) = 0) :
(Ο‰ : X (q (p'+2)))^(2^(p'+2)) = 1 :=
calc (Ο‰ : X (q (p'+2)))^2^(p'+2)
= (Ο‰^(2^(p'+1)))^2 : by rw [←pow_mul, ←pow_succ']
... = (-1)^2 : by rw Ο‰_pow_eq_neg_one p' h
... = 1 : by simp
/-- `Ο‰` as an element of the group of units. -/
def Ο‰_unit (p : β„•) : units (X (q p)) :=
{ val := Ο‰,
inv := Ο‰b,
val_inv := by simp [Ο‰_mul_Ο‰b],
inv_val := by simp [Ο‰b_mul_Ο‰], }
@[simp] lemma Ο‰_unit_coe (p : β„•) : (Ο‰_unit p : X (q p)) = Ο‰ := rfl
/-- The order of `Ο‰` in the unit group is exactly `2^p`. -/
theorem order_Ο‰ (p' : β„•) (h : lucas_lehmer_residue (p'+2) = 0) :
order_of (Ο‰_unit (p'+2)) = 2^(p'+2) :=
begin
apply nat.eq_prime_pow_of_dvd_least_prime_pow, -- the order of Ο‰ divides 2^p
{ norm_num, },
{ intro o,
have Ο‰_pow := order_of_dvd_iff_pow_eq_one.1 o,
replace Ο‰_pow := congr_arg (units.coe_hom (X (q (p'+2))) :
units (X (q (p'+2))) β†’ X (q (p'+2))) Ο‰_pow,
simp at Ο‰_pow,
have h : (1 : zmod (q (p'+2))) = -1 :=
congr_arg (prod.fst) ((Ο‰_pow.symm).trans (Ο‰_pow_eq_neg_one p' h)),
haveI : fact (2 < (q (p'+2) : β„•)) := ⟨two_lt_q _⟩,
apply zmod.neg_one_ne_one h.symm, },
{ apply order_of_dvd_iff_pow_eq_one.2,
apply units.ext,
push_cast,
exact Ο‰_pow_eq_one p' h, }
end
lemma order_ineq (p' : β„•) (h : lucas_lehmer_residue (p'+2) = 0) : 2^(p'+2) < (q (p'+2) : β„•)^2 :=
calc 2^(p'+2) = order_of (Ο‰_unit (p'+2)) : (order_Ο‰ p' h).symm
... ≀ fintype.card ((X _)Λ£) : order_of_le_card_univ
... < (q (p'+2) : β„•)^2 : units_card (nat.lt_of_succ_lt (two_lt_q _))
end lucas_lehmer
export lucas_lehmer (lucas_lehmer_test lucas_lehmer_residue)
open lucas_lehmer
theorem lucas_lehmer_sufficiency (p : β„•) (w : 1 < p) : lucas_lehmer_test p β†’ (mersenne p).prime :=
begin
let p' := p - 2,
have z : p = p' + 2 := (tsub_eq_iff_eq_add_of_le w.nat_succ_le).mp rfl,
have w : 1 < p' + 2 := (nat.lt_of_sub_eq_succ rfl),
contrapose,
intros a t,
rw z at a,
rw z at t,
have h₁ := order_ineq p' t,
have hβ‚‚ := nat.min_fac_sq_le_self (mersenne_pos (nat.lt_of_succ_lt w)) a,
have h := lt_of_lt_of_le h₁ hβ‚‚,
exact not_lt_of_ge (nat.sub_le _ _) h,
end
-- Here we calculate the residue, very inefficiently, using `dec_trivial`. We can do much better.
example : (mersenne 5).prime := lucas_lehmer_sufficiency 5 (by norm_num) dec_trivial
-- Next we use `norm_num` to calculate each `s p i`.
namespace lucas_lehmer
open tactic
lemma s_mod_succ {p a i b c}
(h1 : (2^p - 1 : β„€) = a)
(h2 : s_mod p i = b)
(h3 : (b * b - 2) % a = c) :
s_mod p (i+1) = c :=
by { dsimp [s_mod, mersenne], rw [h1, h2, sq, h3] }
/--
Given a goal of the form `lucas_lehmer_test p`,
attempt to do the calculation using `norm_num` to certify each step.
-/
meta def run_test : tactic unit :=
do `(lucas_lehmer_test %%p) ← target,
`[dsimp [lucas_lehmer_test]],
`[rw lucas_lehmer.residue_eq_zero_iff_s_mod_eq_zero, swap, norm_num],
p ← eval_expr β„• p,
-- Calculate the candidate Mersenne prime
let M : β„€ := 2^p - 1,
t ← to_expr ``(2^%%`(p) - 1 = %%`(M)),
v ← to_expr ``(by norm_num : 2^%%`(p) - 1 = %%`(M)),
w ← assertv `w t v,
-- base case
t ← to_expr ``(s_mod %%`(p) 0 = 4),
v ← to_expr ``(by norm_num [lucas_lehmer.s_mod] : s_mod %%`(p) 0 = 4),
h ← assertv `h t v,
-- step case, repeated p-2 times
iterate_exactly (p-2) `[replace h := lucas_lehmer.s_mod_succ w h (by { norm_num, refl })],
-- now close the goal
h ← get_local `h,
exact h
end lucas_lehmer
/-- We verify that the tactic works to prove `127.prime`. -/
example : (mersenne 7).prime := lucas_lehmer_sufficiency _ (by norm_num) (by lucas_lehmer.run_test).
/-!
This implementation works successfully to prove `(2^127 - 1).prime`,
and all the Mersenne primes up to this point appear in [archive/examples/mersenne_primes.lean].
`(2^127 - 1).prime` takes about 5 minutes to run (depending on your CPU!),
and unfortunately the next Mersenne prime `(2^521 - 1)`,
which was the first "computer era" prime,
is out of reach with the current implementation.
There's still low hanging fruit available to do faster computations
based on the formula
```
n ≑ (n % 2^p) + (n / 2^p) [MOD 2^p - 1]
```
and the fact that `% 2^p` and `/ 2^p` can be very efficient on the binary representation.
Someone should do this, too!
-/
lemma modeq_mersenne (n k : β„•) : k ≑ ((k / 2^n) + (k % 2^n)) [MOD 2^n - 1] :=
-- See https://leanprover.zulipchat.com/#narrow/stream/113489-new-members/topic/help.20finding.20a.20lemma/near/177698446
begin
conv in k { rw ← nat.div_add_mod k (2^n) },
refine nat.modeq.add_right _ _,
conv { congr, skip, skip, rw ← one_mul (k/2^n) },
exact (nat.modeq_sub $ nat.succ_le_of_lt $ pow_pos zero_lt_two _).mul_right _,
end
-- It's hard to know what the limiting factor for large Mersenne primes would be.
-- In the purely computational world, I think it's the squaring operation in `s`.