forked from leanprover-community/mathlib4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLucasLehmer.lean
More file actions
753 lines (610 loc) · 26.1 KB
/
LucasLehmer.lean
File metadata and controls
753 lines (610 loc) · 26.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/-
Copyright (c) 2020 Kim Morrison. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro, Alastair Irving, Kim Morrison, Ainsley Pahljina
-/
module
public import Mathlib.NumberTheory.Fermat
public import Mathlib.RingTheory.Fintype
/-!
# The Lucas-Lehmer test for Mersenne primes
We define `lucasLehmerResidue : Π p : ℕ, ZMod (2^p - 1)`, and
prove `lucasLehmerResidue p = 0 ↔ Prime (mersenne p)`.
We construct a `norm_num` extension to calculate this residue to certify primality of Mersenne
primes using `lucas_lehmer_sufficiency`.
## TODO
- 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 Kim Morrison.
The tactic for certified computation of Lucas-Lehmer residues was provided by Mario Carneiro.
This tactic was ported by Thomas Murrills to Lean 4, and then it was converted to a `norm_num`
extension and made to use kernel reductions by Kyle Miller.
-/
@[expose] public section
/-- The Mersenne numbers, 2^p - 1. -/
def mersenne (p : ℕ) : ℕ :=
2 ^ p - 1
theorem strictMono_mersenne : StrictMono mersenne := fun m n h ↦
(Nat.sub_lt_sub_iff_right <| Nat.one_le_pow _ _ two_pos).2 <| by gcongr; norm_num1
@[simp, gcongr]
theorem mersenne_lt_mersenne {p q : ℕ} : mersenne p < mersenne q ↔ p < q :=
strictMono_mersenne.lt_iff_lt
@[simp, gcongr]
theorem mersenne_le_mersenne {p q : ℕ} : mersenne p ≤ mersenne q ↔ p ≤ q :=
strictMono_mersenne.le_iff_le
@[simp] theorem mersenne_zero : mersenne 0 = 0 := rfl
@[simp] lemma mersenne_odd : ∀ {p : ℕ}, Odd (mersenne p) ↔ p ≠ 0
| 0 => by simp
| p + 1 => by
simpa using Nat.Even.sub_odd (one_le_pow₀ one_le_two)
(even_two.pow_of_ne_zero p.succ_ne_zero) odd_one
@[simp] theorem mersenne_pos {p : ℕ} : 0 < mersenne p ↔ 0 < p := mersenne_lt_mersenne (p := 0)
lemma mersenne_succ (n : ℕ) : mersenne (n + 1) = 2 * mersenne n + 1 := by
dsimp [mersenne]
have := Nat.one_le_pow n 2 two_pos
lia
/-- If `2 ^ p - 1` is prime, then `p` is prime. -/
lemma Nat.Prime.of_mersenne {p : ℕ} (h : (mersenne p).Prime) : Nat.Prime p := by
apply Nat.prime_of_pow_sub_one_prime _ h |>.2
rintro rfl
apply Nat.not_prime_one h
namespace Mathlib.Meta.Positivity
open Lean Meta Qq Function
alias ⟨_, mersenne_pos_of_pos⟩ := mersenne_pos
/-- Extension for the `positivity` tactic: `mersenne`. -/
@[positivity mersenne _]
meta def evalMersenne : PositivityExt where eval {u α} _zα _pα e := do
match u, α, e with
| 0, ~q(ℕ), ~q(mersenne $a) =>
let ra ← core q(inferInstance) q(inferInstance) a
assertInstancesCommute
match ra with
| .positive pa => pure (.positive q(mersenne_pos_of_pos $pa))
| _ => pure (.nonnegative q(Nat.zero_le (mersenne $a)))
| _, _, _ => throwError "not mersenne"
end Mathlib.Meta.Positivity
@[simp]
theorem one_lt_mersenne {p : ℕ} : 1 < mersenne p ↔ 1 < p :=
mersenne_lt_mersenne (p := 1)
@[simp]
theorem succ_mersenne (k : ℕ) : mersenne k + 1 = 2 ^ k := by
rw [mersenne, tsub_add_cancel_of_le]
exact one_le_pow₀ (by simp)
lemma mersenne_mod_four {n : ℕ} (h : 2 ≤ n) : mersenne n % 4 = 3 := by
induction n, h using Nat.le_induction with
| base => rfl
| succ _ _ _ => rw [mersenne_succ]; lia
lemma mersenne_mod_three {n : ℕ} (odd : Odd n) (h : 3 ≤ n) : mersenne n % 3 = 1 := by
obtain ⟨k, rfl⟩ := odd
replace h : 1 ≤ k := by lia
induction k, h using Nat.le_induction with
| base => rfl
| succ j _ _ =>
rw [mersenne_succ, show 2 * (j + 1) = 2 * j + 1 + 1 by lia, mersenne_succ]
lia
lemma mersenne_mod_eight {n : ℕ} (h : 3 ≤ n) : mersenne n % 8 = 7 := by
induction n, h using Nat.le_induction with
| base => rfl
| succ _ _ _ => rw [mersenne_succ]; lia
/-- If `2^p - 1` is prime then 2 is a square mod `2^p - 1`. -/
lemma legendreSym_mersenne_two {p : ℕ} [Fact (mersenne p).Prime] (hp : 3 ≤ p) :
legendreSym (mersenne p) 2 = 1 := by
have := mersenne_mod_eight hp
rw [legendreSym.at_two (by lia), ZMod.χ₈_nat_eq_if_mod_eight]
lia
/-- If `2^p - 1` is prime then 3 is not a square mod `2^p - 1`. -/
lemma legendreSym_mersenne_three {p : ℕ} [Fact (mersenne p).Prime] (hp : 3 ≤ p) (odd : Odd p) :
legendreSym (mersenne p) 3 = -1 := by
rw [(by rfl : (3 : ℤ) = (3 : ℕ)), legendreSym.quadratic_reciprocity_three_mod_four (by norm_num)
(mersenne_mod_four (by lia)),
legendreSym.mod]
rw_mod_cast [mersenne_mod_three odd hp]
simp
namespace LucasLehmer
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 sZMod (p : ℕ) : ℕ → ZMod (2 ^ p - 1)
| 0 => 4
| i + 1 => sZMod p i ^ 2 - 2
/-- The recurrence `s (i+1) = ((s i)^2 - 2) % (2^p - 1)` in `ℤ`. -/
def sMod (p : ℕ) : ℕ → ℤ
| 0 => 4 % (2 ^ p - 1)
| i + 1 => (sMod p i ^ 2 - 2) % (2 ^ p - 1)
theorem mersenne_int_pos {p : ℕ} (hp : p ≠ 0) : (0 : ℤ) < 2 ^ p - 1 :=
sub_pos.2 <| mod_cast Nat.one_lt_two_pow hp
theorem mersenne_int_ne_zero (p : ℕ) (hp : p ≠ 0) : (2 ^ p - 1 : ℤ) ≠ 0 :=
(mersenne_int_pos hp).ne'
theorem sMod_nonneg (p : ℕ) (hp : p ≠ 0) (i : ℕ) : 0 ≤ sMod p i := by
cases i <;> dsimp [sMod]
· exact sup_eq_right.mp rfl
· apply Int.emod_nonneg
exact mersenne_int_ne_zero p hp
theorem sMod_mod (p i : ℕ) : sMod p i % (2 ^ p - 1) = sMod p i := by cases i <;> simp [sMod]
theorem sMod_lt (p : ℕ) (hp : p ≠ 0) (i : ℕ) : sMod p i < 2 ^ p - 1 := by
rw [← sMod_mod]
refine (Int.emod_lt_abs _ (mersenne_int_ne_zero p hp)).trans_eq ?_
exact abs_of_nonneg (mersenne_int_pos hp).le
theorem sZMod_eq_s (p' : ℕ) (i : ℕ) : sZMod (p' + 2) i = (s i : ZMod (2 ^ (p' + 2) - 1)) := by
induction i with
| zero => dsimp [s, sZMod]; simp
| succ i ih => push_cast [s, sZMod, ih]; rfl
theorem sZMod_eq_sMod (p : ℕ) (i : ℕ) : sZMod p i = (sMod p i : ZMod (2 ^ p - 1)) := by
induction i <;> push_cast [← Int.coe_nat_two_pow_pred p, sMod, sZMod, *] <;> rfl
/-- The Lucas-Lehmer residue is `s p (p-2)` in `ZMod (2^p - 1)`. -/
def lucasLehmerResidue (p : ℕ) : ZMod (2 ^ p - 1) :=
sZMod p (p - 2)
theorem residue_eq_zero_iff_sMod_eq_zero (p : ℕ) (w : 1 < p) :
lucasLehmerResidue p = 0 ↔ sMod p (p - 2) = 0 := by
dsimp [lucasLehmerResidue]
rw [sZMod_eq_sMod p]
constructor
· -- 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
apply Int.eq_zero_of_dvd_of_nonneg_of_lt _ _
(by simpa [ZMod.intCast_zmod_eq_zero_iff_dvd] using h) <;> clear h
· exact sMod_nonneg _ (by positivity) _
· exact sMod_lt _ (by positivity) _
· intro h
rw [h]
simp
/-- **Lucas-Lehmer Test**: 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.
-/
def LucasLehmerTest (p : ℕ) : Prop :=
lucasLehmerResidue p = 0
/-- `q` is defined as the minimum factor of `mersenne p`, bundled as an `ℕ+`. -/
def q (p : ℕ) : ℕ+ :=
⟨Nat.minFac (mersenne p), Nat.minFac_pos (mersenne p)⟩
-- 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.
/-- We construct the ring `X q` as ℤ/qℤ + √3 ℤ/qℤ. -/
def X (q : ℕ) : Type :=
ZMod q × ZMod q
namespace X
variable {q : ℕ}
instance : Inhabited (X q) := inferInstanceAs (Inhabited (ZMod q × ZMod q))
instance : DecidableEq (X q) := inferInstanceAs (DecidableEq (ZMod q × ZMod q))
instance : AddCommGroup (X q) := inferInstanceAs (AddCommGroup (ZMod q × ZMod q))
@[ext]
theorem ext {x y : X q} (h₁ : x.1 = y.1) (h₂ : x.2 = y.2) : x = y := by
cases x; cases y; congr
@[simp] theorem zero_fst : (0 : X q).1 = 0 := rfl
@[simp] theorem zero_snd : (0 : X q).2 = 0 := rfl
@[simp]
theorem add_fst (x y : X q) : (x + y).1 = x.1 + y.1 :=
rfl
@[simp]
theorem add_snd (x y : X q) : (x + y).2 = x.2 + y.2 :=
rfl
@[simp]
theorem neg_fst (x : X q) : (-x).1 = -x.1 :=
rfl
@[simp]
theorem neg_snd (x : X q) : (-x).2 = -x.2 :=
rfl
instance : Mul (X q) where mul x y := (x.1 * y.1 + 3 * x.2 * y.2, x.1 * y.2 + x.2 * y.1)
@[simp]
theorem mul_fst (x y : X q) : (x * y).1 = x.1 * y.1 + 3 * x.2 * y.2 :=
rfl
@[simp]
theorem mul_snd (x y : X q) : (x * y).2 = x.1 * y.2 + x.2 * y.1 :=
rfl
instance : One (X q) where one := ⟨1, 0⟩
@[simp]
theorem one_fst : (1 : X q).1 = 1 :=
rfl
@[simp]
theorem one_snd : (1 : X q).2 = 0 :=
rfl
instance : Monoid (X q) :=
{ (inferInstance : Mul (X q)), (inferInstance : One (X q)) with
mul_assoc := fun x y z => by ext <;> dsimp <;> ring
one_mul := fun x => by ext <;> simp
mul_one := fun x => by ext <;> simp }
instance : NatCast (X q) where
natCast := fun n => ⟨n, 0⟩
@[simp] theorem fst_natCast (n : ℕ) : (n : X q).fst = (n : ZMod q) := rfl
@[simp] theorem snd_natCast (n : ℕ) : (n : X q).snd = (0 : ZMod q) := rfl
@[simp] theorem ofNat_fst (n : ℕ) [n.AtLeastTwo] :
(ofNat(n) : X q).fst = OfNat.ofNat n :=
rfl
@[simp] theorem ofNat_snd (n : ℕ) [n.AtLeastTwo] :
(ofNat(n) : X q).snd = 0 :=
rfl
instance : AddGroupWithOne (X q) :=
{ (inferInstance : Monoid (X q)), (inferInstance : AddCommGroup (X q)),
(inferInstance : NatCast (X q)) with
natCast_zero := by ext <;> simp
natCast_succ := fun _ ↦ by ext <;> simp
intCast := fun n => ⟨n, 0⟩
intCast_ofNat := fun n => by ext <;> simp
intCast_negSucc := fun n => by ext <;> simp }
theorem left_distrib (x y z : X q) : x * (y + z) = x * y + x * z := by
ext <;> dsimp <;> ring
theorem right_distrib (x y z : X q) : (x + y) * z = x * z + y * z := by
ext <;> dsimp <;> ring
instance : Ring (X q) :=
{ (inferInstance : AddGroupWithOne (X q)), (inferInstance : AddCommGroup (X q)),
(inferInstance : Monoid (X q)) with
left_distrib := left_distrib
right_distrib := right_distrib
mul_zero := fun _ ↦ by ext <;> simp
zero_mul := fun _ ↦ by ext <;> simp }
instance : CommRing (X q) :=
{ (inferInstance : Ring (X q)) with
mul_comm := fun _ _ ↦ by ext <;> dsimp <;> ring }
instance [Fact (1 < (q : ℕ))] : Nontrivial (X q) :=
⟨⟨0, 1, ne_of_apply_ne Prod.fst zero_ne_one⟩⟩
@[simp]
theorem fst_intCast (n : ℤ) : (n : X q).fst = (n : ZMod q) :=
rfl
@[simp]
theorem snd_intCast (n : ℤ) : (n : X q).snd = (0 : ZMod q) :=
rfl
@[norm_cast]
theorem coe_mul (n m : ℤ) : ((n * m : ℤ) : X q) = (n : X q) * (m : X q) := by ext <;> simp
@[norm_cast]
theorem coe_natCast (n : ℕ) : ((n : ℤ) : X q) = (n : X q) := by ext <;> simp
/-- 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)
theorem ω_mul_ωb : (ω : X q) * ωb = 1 := by
dsimp [ω, ωb]
ext <;> simp; ring
theorem ωb_mul_ω : (ωb : X q) * ω = 1 := by
rw [mul_comm, ω_mul_ωb]
/-- A closed form for the recurrence relation. -/
theorem closed_form (i : ℕ) : (s i : X q) = (ω : X q) ^ 2 ^ i + (ωb : X q) ^ 2 ^ i := by
induction i with
| zero =>
dsimp [s, ω, ωb]
ext <;> norm_num
| succ i ih =>
calc
(s (i + 1) : X q) = (s i ^ 2 - 2 : ℤ) := rfl
_ = (s i : X q) ^ 2 - 2 := by push_cast; rfl
_ = (ω ^ 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_right]
_ = ω ^ 2 ^ (i + 1) + ωb ^ 2 ^ (i + 1) := by rw [← pow_mul, ← pow_mul, _root_.pow_succ]
/-- We define `α = √3`. -/
def α : X q := (0, 1)
@[simp] lemma α_sq : (α ^ 2 : X q) = 3 := by
ext <;> simp [α, sq]
@[simp] lemma one_add_α_sq : ((1 + α) ^ 2 : X q) = 2 * ω := by
ext <;> simp [α, ω, sq] <;> norm_num
lemma α_pow (i : ℕ) : (α : X q) ^ (2 * i + 1) = 3 ^ i * α := by
rw [pow_succ, pow_mul, α_sq]
/-! We show that `X q` has characteristic `q`, so that we can apply the binomial theorem. -/
instance : CharP (X q) q where
cast_eq_zero_iff x := by
convert ZMod.natCast_eq_zero_iff _ _
exact ⟨congr_arg Prod.fst, fun hx ↦ ext hx (by simp)⟩
instance : Coe (ZMod ↑q) (X q) where
coe := ZMod.castHom dvd_rfl (X q)
/-- If `3` is not a square mod `q` then `(1 + α) ^ q = 1 - α` -/
lemma one_add_α_pow_q [Fact q.Prime] (odd : Odd q) (leg3 : legendreSym q 3 = -1) :
(1 + α : X q) ^ q = 1 - α := by
obtain ⟨k, rfl⟩ := odd
let q := 2 * k + 1
have : (3 ^ k : ZMod q) = -1 := by
simpa [leg3, mul_add_div, eq_comm] using legendreSym.eq_pow (2 * k + 1) 3
rw [add_pow_expChar, α_pow, show (3 : X q) = (3 : ZMod q) by rw [map_ofNat], ← map_pow, this,
map_neg]
simp [sub_eq_add_neg]
/-- If `3` is not a square then `(1 + α) ^ (q + 1) = -2`. -/
lemma one_add_α_pow_q_succ [Fact q.Prime] (odd : Odd q) (leg3 : legendreSym q 3 = -1) :
(1 + α : X q) ^ (q + 1) = -2 := by
rw [pow_succ, one_add_α_pow_q odd leg3, mul_comm, ← _root_.sq_sub_sq, α_sq]
norm_num
/-- If `3` is not a square then `(2 * ω) ^ ((q + 1) / 2) = -2`. -/
lemma two_mul_ω_pow [Fact q.Prime] (odd : Odd q) (leg3 : legendreSym q 3 = -1) :
(2 * ω : X q) ^ ((q + 1) / 2) = -2 := by
rw [← one_add_α_sq, ← pow_mul]
have : 2 * ((q + 1) / 2) = q + 1 := by
apply Nat.mul_div_cancel'
rw [← even_iff_two_dvd]
exact Odd.add_one odd
rw [this, one_add_α_pow_q_succ odd leg3]
/-- If 3 is not a square and 2 is square then $\omega^{(q+1)/2}=-1$. -/
lemma pow_ω [Fact q.Prime] (odd : Odd q)
(leg3 : legendreSym q 3 = -1)
(leg2 : legendreSym q 2 = 1) :
(ω : X q) ^ ((q + 1) / 2) = -1 := by
have pow2 : (2 : ZMod q) ^ ((q + 1) / 2) = 2 := by
obtain ⟨_, _⟩ := odd
rw [(by lia : (q + 1) / 2 = q / 2 + 1), pow_succ]
have leg := legendreSym.eq_pow q 2
have : (2 : ZMod q) = ((2 : ℤ) : ZMod q) := by norm_cast
rw [this, ← leg, leg2]
ring
have := two_mul_ω_pow odd leg3
rw [mul_pow] at this
have coe : (2 : X q) = (2 : ZMod q) := by rw [map_ofNat]
rw [coe, ← map_pow, pow2, ← coe,
(by ring : (-2 : X q) = 2 * -1)] at this
refine (IsUnit.of_mul_eq_one (M := X q) ↑((q + 1) / 2) ?_).mul_left_cancel this
norm_cast
simp [Nat.mul_div_cancel' odd.add_one.two_dvd]
/-- The final evaluation needed to establish the Lucas-Lehmer necessity. -/
lemma ω_pow_trace [Fact q.Prime] (odd : Odd q)
(leg3 : legendreSym q 3 = -1)
(leg2 : legendreSym q 2 = 1)
(hq4 : 4 ∣ q + 1) :
(ω : X q) ^ ((q + 1) / 4) + ωb ^ ((q + 1) / 4) = 0 := by
have : (ω : X q) ^ ((q + 1) / 2) * ωb ^ ((q + 1) / 4) = -ωb ^ ((q + 1) / 4) := by
rw [pow_ω odd leg3 leg2]
ring
have div4 : (q + 1) / 2 = (q + 1) / 4 + (q + 1) / 4 := by rcases hq4 with ⟨k, hk⟩; lia
rw [div4, pow_add, mul_assoc, ← mul_pow, ω_mul_ωb, one_pow, mul_one] at this
rw [this]
ring
variable [NeZero q]
set_option backward.inferInstanceAs.wrap false in
instance : Fintype (X q) := inferInstanceAs (Fintype (ZMod q × ZMod q))
/-- The cardinality of `X` is `q^2`. -/
theorem card_eq : Fintype.card (X q) = q ^ 2 := by
dsimp [X]
rw [Fintype.card_prod, ZMod.card q, sq]
/-- There are strictly fewer than `q^2` units, since `0` is not a unit. -/
nonrec theorem card_units_lt (w : 1 < q) : Fintype.card (X q)ˣ < q ^ 2 := by
have : Fact (1 < (q : ℕ)) := ⟨w⟩
convert card_units_lt (X q)
rw [card_eq]
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. -/
theorem two_lt_q (p' : ℕ) : 2 < q (p' + 2) := by
refine (minFac_prime (one_lt_mersenne.2 ?_).ne').two_le.lt_of_ne' ?_
· exact le_add_left _ _
· rw [Ne, minFac_eq_two_iff, mersenne, Nat.pow_succ']
exact Nat.two_not_dvd_two_mul_sub_one Nat.one_le_two_pow
theorem ω_pow_formula (p' : ℕ) (h : lucasLehmerResidue (p' + 2) = 0) :
∃ k : ℤ,
(ω : X (q (p' + 2))) ^ 2 ^ (p' + 1) =
k * mersenne (p' + 2) * (ω : X (q (p' + 2))) ^ 2 ^ p' - 1 := by
dsimp [lucasLehmerResidue] at h
rw [sZMod_eq_s p'] at h
replace h : 2 ^ (p' + 2) - 1 ∣ s p' := by simpa [ZMod.intCast_zmod_eq_zero_iff_dvd] using h
obtain ⟨k, h⟩ := h
use k
replace h := congr_arg (fun n : ℤ => (n : X (q (p' + 2)))) h
-- coercion from ℤ to X q
dsimp at h
rw [closed_form] at h
replace h := congr_arg (fun x => ω ^ 2 ^ p' * x) h
dsimp at h
have t : 2 ^ p' + 2 ^ p' = 2 ^ (p' + 1) := by ring
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 _ _ (by decide)
exact mod_cast h
set_option backward.isDefEq.respectTransparency false in
-- TODO: fix non-terminal simp (acting on two goals with different simp sets)
set_option linter.flexible false in
/-- `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 := by
ext <;> simp [mersenne, q, ZMod.natCast_eq_zero_iff, -pow_pos]
apply Nat.minFac_dvd
theorem ω_pow_eq_neg_one (p' : ℕ) (h : lucasLehmerResidue (p' + 2) = 0) :
(ω : X (q (p' + 2))) ^ 2 ^ (p' + 1) = -1 := by
obtain ⟨k, w⟩ := ω_pow_formula p' h
rw [mersenne_coe_X] at w
simpa using w
theorem ω_pow_eq_one (p' : ℕ) (h : lucasLehmerResidue (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, ← Nat.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)) where
val := ω
inv := ωb
val_inv := ω_mul_ωb
inv_val := ωb_mul_ω
@[simp]
theorem ωUnit_coe (p : ℕ) : (ωUnit p : X (q p)) = ω :=
rfl
/-- The order of `ω` in the unit group is exactly `2^p`. -/
theorem order_ω (p' : ℕ) (h : lucasLehmerResidue (p' + 2) = 0) :
orderOf (ωUnit (p' + 2)) = 2 ^ (p' + 2) := by
apply Nat.eq_prime_pow_of_dvd_least_prime_pow
-- the order of ω divides 2^p
· exact Nat.prime_two
· intro o
have ω_pow :=
congr_arg (Units.coeHom (X (q (p' + 2))) : Units (X (q (p' + 2))) → X (q (p' + 2))) <|
orderOf_dvd_iff_pow_eq_one.1 o
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 orderOf_dvd_iff_pow_eq_one.2
apply Units.ext
push_cast
exact ω_pow_eq_one p' h
theorem order_ineq (p' : ℕ) (h : lucasLehmerResidue (p' + 2) = 0) :
2 ^ (p' + 2) < (q (p' + 2) : ℕ) ^ 2 :=
calc
2 ^ (p' + 2) = orderOf (ωUnit (p' + 2)) := (order_ω p' h).symm
_ ≤ Fintype.card (X (q (p' + 2)))ˣ := orderOf_le_card_univ
_ < q (p' + 2) ^ 2 := card_units_lt (Nat.lt_of_succ_lt (two_lt_q _))
end LucasLehmer
export LucasLehmer (LucasLehmerTest lucasLehmerResidue)
open LucasLehmer
theorem lucas_lehmer_sufficiency (p : ℕ) (w : 1 < p) : LucasLehmerTest p → (mersenne p).Prime := by
set p' := p - 2 with hp'
clear_value p'
obtain rfl : p = p' + 2 := by lia
have w : 1 < p' + 2 := Nat.lt_of_sub_eq_succ rfl
contrapose
intro a t
have h₁ := order_ineq p' t
have h₂ := Nat.minFac_sq_le_self (mersenne_pos.2 (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
set_option backward.isDefEq.respectTransparency false in
/-- If `2^p - 1` is prime then the Lucas-Lehmer test holds, `s (p - 2) % (2^p - 1) = 0`. -/
theorem lucas_lehmer_necessity (p : ℕ) (w : 3 ≤ p) (hp : (mersenne p).Prime) :
LucasLehmerTest p := by
have : Fact (mersenne p).Prime := ⟨‹_›⟩
set p' := p - 2 with hp'
clear_value p'
obtain rfl : p = p' + 2 := by lia
dsimp [LucasLehmerTest, lucasLehmerResidue]
rw [sZMod_eq_s p', ← X.fst_intCast, X.closed_form, add_tsub_cancel_right]
have := X.ω_pow_trace (q := mersenne (p' + 2)) (by simp)
(legendreSym_mersenne_three w <| hp.of_mersenne.odd_of_ne_two (by lia))
(legendreSym_mersenne_two w) (by simp [pow_add])
rw [succ_mersenne, pow_add, show 2 ^ 2 = 4 by norm_num, mul_div_cancel_right₀ _ (by norm_num)]
at this
simp [this]
namespace LucasLehmer
/-!
### `norm_num` extension
Next we define a `norm_num` extension that calculates `LucasLehmerTest p` for `1 < p`.
It makes use of a version of `sMod` that is specifically written to be reducible by the
Lean 4 kernel, which has the capability of efficiently reducing natural number expressions.
With this reduction in hand, it's a simple matter of applying the lemma
`LucasLehmer.residue_eq_zero_iff_sMod_eq_zero`.
See `Archive/Examples/MersennePrimes.lean` for certifications of all Mersenne primes
up through `mersenne 4423`.
-/
namespace norm_num_ext
open Qq Lean Elab.Tactic Mathlib.Meta.NormNum
/-- Version of `sMod` that is `ℕ`-valued. One should have `q = 2 ^ p - 1`.
This can be reduced by the kernel. -/
def sModNat (q : ℕ) : ℕ → ℕ
| 0 => 4 % q
| i + 1 => (sModNat q i ^ 2 + (q - 2)) % q
theorem sModNat_eq_sMod (p k : ℕ) (hp : 2 ≤ p) : (sModNat (2 ^ p - 1) k : ℤ) = sMod p k := by
induction k with
| zero => grind [sModNat, sMod]
| succ =>
have : 2 ^ 2 ≤ 2 ^ p := Nat.pow_le_pow_right (by lia) hp
grind [sModNat, sMod, Int.emod_eq_add_self_emod]
/-- Tail-recursive version of `sModNat`. -/
meta def sModNatTR (q k : ℕ) : ℕ :=
go k (4 % q)
where
/-- Helper function for `sMod''`. -/
go : ℕ → ℕ → ℕ
| 0, acc => acc
| n + 1, acc => go n ((acc ^ 2 + (q - 2)) % q)
termination_by structural x => x
/--
Generalization of `sModNat` with arbitrary base case,
useful for proving `sModNatTR` and `sModNat` agree.
-/
def sModNat_aux (b q : ℕ) : ℕ → ℕ
| 0 => b
| i + 1 => (sModNat_aux b q i ^ 2 + (q - 2)) % q
theorem sModNat_aux_eq (q k : ℕ) : sModNat_aux (4 % q) q k = sModNat q k := by
induction k with
| zero => rfl
| succ k ih => rw [sModNat_aux, ih, sModNat, ← ih]
theorem sModNatTR_eq_sModNat (q i : ℕ) : sModNatTR q i = sModNat q i := by
rw [sModNatTR, helper, sModNat_aux_eq]
where
helper b q k : sModNatTR.go q k b = sModNat_aux b q k := by
induction k generalizing b with
| zero => rfl
| succ k ih =>
rw [sModNatTR.go, ih, sModNat_aux]
clear ih
induction k with
| zero => rfl
| succ k ih =>
rw [sModNat_aux, ih, sModNat_aux]
lemma testTrueHelper (p : ℕ) (hp : Nat.blt 1 p = true) (h : sModNatTR (2 ^ p - 1) (p - 2) = 0) :
LucasLehmerTest p := by
rw [Nat.blt_eq] at hp
rw [LucasLehmerTest, LucasLehmer.residue_eq_zero_iff_sMod_eq_zero p hp, ← sModNat_eq_sMod p _ hp,
← sModNatTR_eq_sModNat, h]
rfl
lemma testFalseHelper (p : ℕ) (hp : Nat.blt 1 p = true)
(h : Nat.ble 1 (sModNatTR (2 ^ p - 1) (p - 2))) : ¬ LucasLehmerTest p := by
rw [Nat.blt_eq] at hp
rw [Nat.ble_eq, Nat.succ_le_iff, Nat.pos_iff_ne_zero] at h
rw [LucasLehmerTest, LucasLehmer.residue_eq_zero_iff_sMod_eq_zero p hp, ← sModNat_eq_sMod p _ hp,
← sModNatTR_eq_sModNat]
simpa using h
theorem isNat_lucasLehmerTest : {p np : ℕ} →
IsNat p np → LucasLehmerTest np → LucasLehmerTest p
| _, _, ⟨rfl⟩, h => h
theorem isNat_not_lucasLehmerTest : {p np : ℕ} →
IsNat p np → ¬ LucasLehmerTest np → ¬ LucasLehmerTest p
| _, _, ⟨rfl⟩, h => h
/-- Calculate `LucasLehmer.LucasLehmerTest p` for `2 ≤ p` by using kernel reduction for the
`sMod'` function. -/
@[norm_num LucasLehmer.LucasLehmerTest (_ : ℕ)]
meta def evalLucasLehmerTest : NormNumExt where eval {_ _} e := do
let .app _ (p : Q(ℕ)) ← Meta.whnfR e | failure
let ⟨ep, hp⟩ ← deriveNat p _
let np := ep.natLit!
unless 1 < np do
failure
haveI' h1ltp : Nat.blt 1 $ep =Q true := ⟨⟩
if sModNatTR (2 ^ np - 1) (np - 2) = 0 then
haveI' hs : sModNatTR (2 ^ $ep - 1) ($ep - 2) =Q 0 := ⟨⟩
have pf : Q(LucasLehmerTest $ep) := q(testTrueHelper $ep $h1ltp $hs)
have pf' : Q(LucasLehmerTest $p) := q(isNat_lucasLehmerTest $hp $pf)
return .isTrue pf'
else
haveI' hs : Nat.ble 1 (sModNatTR (2 ^ $ep - 1) ($ep - 2)) =Q true := ⟨⟩
have pf : Q(¬ LucasLehmerTest $ep) := q(testFalseHelper $ep $h1ltp $hs)
have pf' : Q(¬ LucasLehmerTest $p) := q(isNat_not_lucasLehmerTest $hp $pf)
return .isFalse pf'
end norm_num_ext
end LucasLehmer
/-!
This implementation works successfully to prove `(2^4423 - 1).Prime`,
and all the Mersenne primes up to this point appear in `Archive/Examples/MersennePrimes.lean`.
These can be calculated nearly instantly, and `(2^9689 - 1).Prime` only fails due to deep
recursion.
(Note by kmill: the following notes were for the Lean 3 version. They seem like they could still
be useful, so I'm leaving them here.)
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!
-/
theorem 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
calc
k = 2 ^ n * (k / 2 ^ n) + k % 2 ^ n := (Nat.div_add_mod k (2 ^ n)).symm
_ ≡ 1 * (k / 2 ^ n) + k % 2 ^ n [MOD 2 ^ n - 1] :=
((Nat.modEq_sub <| Nat.succ_le_of_lt <| pow_pos zero_lt_two _).mul_right _).add_right _
_ = k / 2 ^ n + k % 2 ^ n := by rw [one_mul]
-- 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`.