The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V12. Number theory

Mathlib.NumberTheory.LucasLehmer

113 declarations and 2 deprecated aliases. Mathlib docs · Source

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.

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.

We show that X q has characteristic q, so that we can apply the binomial theorem.

Here and below, we introduce p' = p - 2, in order to avoid using subtraction in ℕ.

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.

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 LucasLehmer.norm_num_ext.sModNatTR.go.match_1.congr_eq_1 E1 #

∀ (motive : ℕ → ℕ → Sort u_1) (x x_1 : ℕ) (h_1 : (acc : ℕ) → motive 0 acc) (h_2 : (n acc : ℕ) → motive n.succ acc)
  (acc : ℕ),
  x = 0 →
    x_1 = acc →
      (match x, x_1 with
        | 0, acc => h_1 acc
        | n.succ, acc => h_2 n acc) ≍
        h_1 acc

theorem LucasLehmer.s.match_1.congr_eq_1 E1 #

∀ (motive : ℕ → Sort u_1) (x : ℕ) (h_1 : Unit → motive 0) (h_2 : (i : ℕ) → motive i.succ),
  x = 0 →
    (match x with
      | 0 => h_1 ()
      | i.succ => h_2 i) ≍
      h_1 ()

theorem LucasLehmer.norm_num_ext.sModNatTR.go.match_1.congr_eq_2 E1 #

∀ (motive : ℕ → ℕ → Sort u_1) (x x_1 : ℕ) (h_1 : (acc : ℕ) → motive 0 acc) (h_2 : (n acc : ℕ) → motive n.succ acc)
  (n acc : ℕ),
  x = n.succ →
    x_1 = acc →
      (match x, x_1 with
        | 0, acc => h_1 acc
        | n.succ, acc => h_2 n acc) ≍
        h_2 n acc

theorem LucasLehmer.s.match_1.congr_eq_2 E1 #

∀ (motive : ℕ → Sort u_1) (x : ℕ) (h_1 : Unit → motive 0) (h_2 : (i : ℕ) → motive i.succ) (i : ℕ),
  x = i.succ →
    (match x with
      | 0 => h_1 ()
      | i.succ => h_2 i) ≍
      h_2 i

def mersenne E1 #

ℕ → ℕ

The Mersenne numbers, 2^p - 1.

theorem strictMono_mersenne E1 #

StrictMono mersenne

theorem mersenne_lt_mersenne E0 #

∀ {p q : ℕ}, mersenne p < mersenne q ↔ p < q

theorem mersenne_le_mersenne E0 #

∀ {p q : ℕ}, mersenne p ≤ mersenne q ↔ p ≤ q

theorem mersenne_zero E0 #

mersenne 0 = 0

theorem mersenne_odd E0 #

∀ {p : ℕ}, Odd (mersenne p) ↔ p ≠ 0

theorem mersenne_pos E0 #

∀ {p : ℕ}, 0 < mersenne p ↔ 0 < p

theorem mersenne_succ E1 #

∀ (n : ℕ), mersenne (n + 1) = 2 * mersenne n + 1

theorem Nat.Prime.of_mersenne E1 #

∀ {p : ℕ}, Nat.Prime (mersenne p) → Nat.Prime p

If 2 ^ p - 1 is prime, then p is prime.

theorem Mathlib.Meta.Positivity.mersenne_pos_of_pos E1 #

∀ {p : ℕ}, 0 < p → 0 < mersenne p

Alias of the reverse direction of mersenne_pos.

def Mathlib.Meta.Positivity.evalMersenne E1 #

Mathlib.Meta.Positivity.PositivityExt

Extension for the positivity tactic: mersenne.

theorem one_lt_mersenne E0 #

∀ {p : ℕ}, 1 < mersenne p ↔ 1 < p

theorem succ_mersenne E0 #

∀ (k : ℕ), mersenne k + 1 = 2 ^ k

theorem mersenne_mod_four E1 #

∀ {n : ℕ}, 2 ≤ n → mersenne n % 4 = 3

theorem mersenne_mod_three E1 #

∀ {n : ℕ}, Odd n → 3 ≤ n → mersenne n % 3 = 1

theorem mersenne_mod_eight E1 #

∀ {n : ℕ}, 3 ≤ n → mersenne n % 8 = 7

theorem legendreSym_mersenne_two E1 #

∀ {p : ℕ} [inst : Fact (Nat.Prime (mersenne p))], 3 ≤ p → legendreSym (mersenne p) 2 = 1

If 2^p - 1 is prime then 2 is a square mod 2^p - 1.

theorem legendreSym_mersenne_three E1 #

∀ {p : ℕ} [inst : Fact (Nat.Prime (mersenne p))], 3 ≤ p → Odd p → legendreSym (mersenne p) 3 = -1

If 2^p - 1 is prime then 3 is not a square mod 2^p - 1.

def LucasLehmer.s E1 #

ℕ → ℤ

The recurrence s (i+1) = (s i)^2 - 2 in ℤ.

def LucasLehmer.sZMod E1 #

(p : ℕ) → ℕ → ZMod (2 ^ p - 1)

The recurrence s (i+1) = (s i)^2 - 2 in ZMod (2^p - 1).

def LucasLehmer.sMod E1 #

ℕ → ℕ → ℤ

The recurrence s (i+1) = ((s i)^2 - 2) % (2^p - 1) in ℤ.

theorem LucasLehmer.mersenne_int_pos E1 #

∀ {p : ℕ}, p ≠ 0 → 0 < 2 ^ p - 1

theorem LucasLehmer.mersenne_int_ne_zero E1 #

∀ (p : ℕ), p ≠ 0 → 2 ^ p - 1 ≠ 0

theorem LucasLehmer.sMod_nonneg E1 #

∀ (p : ℕ), p ≠ 0 → ∀ (i : ℕ), 0 ≤ LucasLehmer.sMod p i

theorem LucasLehmer.sMod_mod E1 #

∀ (p i : ℕ), LucasLehmer.sMod p i % (2 ^ p - 1) = LucasLehmer.sMod p i

theorem LucasLehmer.sMod_lt E1 #

∀ (p : ℕ), p ≠ 0 → ∀ (i : ℕ), LucasLehmer.sMod p i < 2 ^ p - 1

theorem LucasLehmer.sZMod_eq_s E1 #

∀ (p' i : ℕ), LucasLehmer.sZMod (p' + 2) i = ↑(LucasLehmer.s i)

theorem LucasLehmer.sZMod_eq_sMod E1 #

∀ (p i : ℕ), LucasLehmer.sZMod p i = ↑(LucasLehmer.sMod p i)

def LucasLehmer.lucasLehmerResidue E1 #

(p : ℕ) → ZMod (2 ^ p - 1)

The Lucas-Lehmer residue is s p (p-2) in ZMod (2^p - 1).

theorem LucasLehmer.residue_eq_zero_iff_sMod_eq_zero E1 #

∀ (p : ℕ), 1 < p → (lucasLehmerResidue p = 0 ↔ LucasLehmer.sMod p (p - 2) = 0)

def LucasLehmer.LucasLehmerTest E1 #

ℕ → Prop

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 LucasLehmer.q E1 #

ℕ → ℕ+

q is defined as the minimum factor of mersenne p, bundled as an ℕ+.

def LucasLehmer.X E1 #

ℕ → Type

We construct the ring X q as ℤ/qℤ + √3 ℤ/qℤ.

instance LucasLehmer.X.instInhabited E0 #

{q : ℕ} → Inhabited (LucasLehmer.X q)

instance LucasLehmer.X.instDecidableEq E0 #

{q : ℕ} → DecidableEq (LucasLehmer.X q)

instance LucasLehmer.X.instAddCommGroup E0 #

{q : ℕ} → AddCommGroup (LucasLehmer.X q)

theorem LucasLehmer.X.ext_iff E0 #

∀ {q : ℕ} {x y : LucasLehmer.X q}, x = y ↔ x.1 = y.1 ∧ x.2 = y.2

theorem LucasLehmer.X.ext E1 #

∀ {q : ℕ} {x y : LucasLehmer.X q}, x.1 = y.1 → x.2 = y.2 → x = y

theorem LucasLehmer.X.zero_fst E0 #

∀ {q : ℕ}, 0.1 = 0

theorem LucasLehmer.X.zero_snd E0 #

∀ {q : ℕ}, 0.2 = 0

theorem LucasLehmer.X.add_fst E0 #

∀ {q : ℕ} (x y : LucasLehmer.X q), (x + y).1 = x.1 + y.1

theorem LucasLehmer.X.add_snd E0 #

∀ {q : ℕ} (x y : LucasLehmer.X q), (x + y).2 = x.2 + y.2

theorem LucasLehmer.X.neg_fst E0 #

∀ {q : ℕ} (x : LucasLehmer.X q), (-x).1 = -x.1

theorem LucasLehmer.X.neg_snd E0 #

∀ {q : ℕ} (x : LucasLehmer.X q), (-x).2 = -x.2

instance LucasLehmer.X.instMul E0 #

{q : ℕ} → Mul (LucasLehmer.X q)

theorem LucasLehmer.X.mul_fst E0 #

∀ {q : ℕ} (x y : LucasLehmer.X q), (x * y).1 = x.1 * y.1 + 3 * x.2 * y.2

theorem LucasLehmer.X.mul_snd E0 #

∀ {q : ℕ} (x y : LucasLehmer.X q), (x * y).2 = x.1 * y.2 + x.2 * y.1

instance LucasLehmer.X.instOne E0 #

{q : ℕ} → One (LucasLehmer.X q)

theorem LucasLehmer.X.one_fst E0 #

∀ {q : ℕ}, 1.1 = 1

theorem LucasLehmer.X.one_snd E0 #

∀ {q : ℕ}, 1.2 = 0

instance LucasLehmer.X.instMonoid E0 #

{q : ℕ} → Monoid (LucasLehmer.X q)

instance LucasLehmer.X.instNatCast E0 #

{q : ℕ} → NatCast (LucasLehmer.X q)

theorem LucasLehmer.X.fst_natCast E0 #

∀ {q : ℕ} (n : ℕ), (↑n).1 = ↑n

theorem LucasLehmer.X.snd_natCast E0 #

∀ {q : ℕ} (n : ℕ), (↑n).2 = 0

theorem LucasLehmer.X.ofNat_fst E0 #

∀ {q : ℕ} (n : ℕ) [inst : n.AtLeastTwo], (OfNat.ofNat n).1 = OfNat.ofNat n

theorem LucasLehmer.X.ofNat_snd E0 #

∀ {q : ℕ} (n : ℕ) [inst : n.AtLeastTwo], (OfNat.ofNat n).2 = 0

instance LucasLehmer.X.instAddGroupWithOne E0 #

{q : ℕ} → AddGroupWithOne (LucasLehmer.X q)

theorem LucasLehmer.X.left_distrib E1 #

∀ {q : ℕ} (x y z : LucasLehmer.X q), x * (y + z) = x * y + x * z

theorem LucasLehmer.X.right_distrib E1 #

∀ {q : ℕ} (x y z : LucasLehmer.X q), (x + y) * z = x * z + y * z

instance LucasLehmer.X.instRing E0 #

{q : ℕ} → Ring (LucasLehmer.X q)

instance LucasLehmer.X.instCommRing E0 #

{q : ℕ} → CommRing (LucasLehmer.X q)

instance LucasLehmer.X.instNontrivialOfFactLtNatOfNat E0 #

∀ {q : ℕ} [Fact (1 < q)], Nontrivial (LucasLehmer.X q)

theorem LucasLehmer.X.fst_intCast E0 #

∀ {q : ℕ} (n : ℤ), (↑n).1 = ↑n

theorem LucasLehmer.X.snd_intCast E0 #

∀ {q : ℕ} (n : ℤ), (↑n).2 = 0

theorem LucasLehmer.X.coe_mul E0 #

∀ {q : ℕ} (n m : ℤ), ↑(n * m) = ↑n * ↑m

theorem LucasLehmer.X.coe_natCast E0 #

∀ {q : ℕ} (n : ℕ), ↑↑n = ↑n

def LucasLehmer.X.ω E1 #

{q : ℕ} → LucasLehmer.X q

We define ω = 2 + √3.

def LucasLehmer.X.ωb E1 #

{q : ℕ} → LucasLehmer.X q

We define ωb = 2 - √3, which is the inverse of ω.

theorem LucasLehmer.X.ω_mul_ωb E1 #

∀ {q : ℕ}, LucasLehmer.X.ω * LucasLehmer.X.ωb = 1

theorem LucasLehmer.X.ωb_mul_ω E1 #

∀ {q : ℕ}, LucasLehmer.X.ωb * LucasLehmer.X.ω = 1

theorem LucasLehmer.X.closed_form E1 #

∀ {q : ℕ} (i : ℕ), ↑(LucasLehmer.s i) = LucasLehmer.X.ω ^ 2 ^ i + LucasLehmer.X.ωb ^ 2 ^ i

A closed form for the recurrence relation.

def LucasLehmer.X.α E1 #

{q : ℕ} → LucasLehmer.X q

We define α = √3.

theorem LucasLehmer.X.α_sq E0 #

∀ {q : ℕ}, LucasLehmer.X.α ^ 2 = 3

theorem LucasLehmer.X.one_add_α_sq E0 #

∀ {q : ℕ}, (1 + LucasLehmer.X.α) ^ 2 = 2 * LucasLehmer.X.ω

theorem LucasLehmer.X.α_pow E1 #

∀ {q : ℕ} (i : ℕ), LucasLehmer.X.α ^ (2 * i + 1) = 3 ^ i * LucasLehmer.X.α

instance LucasLehmer.X.instCharP E0 #

∀ {q : ℕ}, CharP (LucasLehmer.X q) q

instance LucasLehmer.X.instCoeZMod E0 #

{q : ℕ} → Coe (ZMod q) (LucasLehmer.X q)

theorem LucasLehmer.X.one_add_α_pow_q E1 #

∀ {q : ℕ} [inst : Fact (Nat.Prime q)], Odd q → legendreSym q 3 = -1 → (1 + LucasLehmer.X.α) ^ q = 1 - LucasLehmer.X.α

If 3 is not a square mod q then (1 + α) ^ q = 1 - α

theorem LucasLehmer.X.one_add_α_pow_q_succ E1 #

∀ {q : ℕ} [inst : Fact (Nat.Prime q)], Odd q → legendreSym q 3 = -1 → (1 + LucasLehmer.X.α) ^ (q + 1) = -2

If 3 is not a square then (1 + α) ^ (q + 1) = -2.

theorem LucasLehmer.X.two_mul_ω_pow E1 #

∀ {q : ℕ} [inst : Fact (Nat.Prime q)], Odd q → legendreSym q 3 = -1 → (2 * LucasLehmer.X.ω) ^ ((q + 1) / 2) = -2

If 3 is not a square then (2 * ω) ^ ((q + 1) / 2) = -2.

theorem LucasLehmer.X.pow_ω E1 #

∀ {q : ℕ} [inst : Fact (Nat.Prime q)],
  Odd q → legendreSym q 3 = -1 → legendreSym q 2 = 1 → LucasLehmer.X.ω ^ ((q + 1) / 2) = -1

If 3 is not a square and 2 is square then $\omega^{(q+1)/2}=-1$.

theorem LucasLehmer.X.ω_pow_trace E1 #

∀ {q : ℕ} [inst : Fact (Nat.Prime q)],
  Odd q →
    legendreSym q 3 = -1 →
      legendreSym q 2 = 1 → 4 ∣ q + 1 → LucasLehmer.X.ω ^ ((q + 1) / 4) + LucasLehmer.X.ωb ^ ((q + 1) / 4) = 0

The final evaluation needed to establish the Lucas-Lehmer necessity.

instance LucasLehmer.X.instFintype E0 #

{q : ℕ} → [NeZero q] → Fintype (LucasLehmer.X q)

theorem LucasLehmer.X.card_eq E0 #

∀ {q : ℕ} [inst : NeZero q], Fintype.card (LucasLehmer.X q) = q ^ 2

The cardinality of X is q^2.

theorem LucasLehmer.X.card_units_lt E1 #

∀ {q : ℕ} [inst : NeZero q], 1 < q → Fintype.card (LucasLehmer.X q)ˣ < q ^ 2

There are strictly fewer than q^2 units, since 0 is not a unit.

theorem LucasLehmer.two_lt_q E1 #

∀ (p' : ℕ), 2 < LucasLehmer.q (p' + 2)

If 1 < p, then q p, the smallest prime factor of mersenne p, is more than 2.

theorem LucasLehmer.ω_pow_formula E1 #

∀ (p' : ℕ),
  lucasLehmerResidue (p' + 2) = 0 →
    ∃ k, LucasLehmer.X.ω ^ 2 ^ (p' + 1) = ↑k * ↑(mersenne (p' + 2)) * LucasLehmer.X.ω ^ 2 ^ p' - 1

theorem LucasLehmer.mersenne_coe_X E1 #

∀ (p : ℕ), ↑(mersenne p) = 0

q is the minimum factor of mersenne p, so M p = 0 in X q.

theorem LucasLehmer.ω_pow_eq_neg_one E1 #

∀ (p' : ℕ), lucasLehmerResidue (p' + 2) = 0 → LucasLehmer.X.ω ^ 2 ^ (p' + 1) = -1

theorem LucasLehmer.ω_pow_eq_one E1 #

∀ (p' : ℕ), lucasLehmerResidue (p' + 2) = 0 → LucasLehmer.X.ω ^ 2 ^ (p' + 2) = 1

def LucasLehmer.ωUnit E1 #

(p : ℕ) → (LucasLehmer.X ↑(LucasLehmer.q p))ˣ

ω as an element of the group of units.

theorem LucasLehmer.ωUnit_coe E0 #

∀ (p : ℕ), ↑(LucasLehmer.ωUnit p) = LucasLehmer.X.ω

theorem LucasLehmer.order_ω E1 #

∀ (p' : ℕ), lucasLehmerResidue (p' + 2) = 0 → orderOf (LucasLehmer.ωUnit (p' + 2)) = 2 ^ (p' + 2)

The order of ω in the unit group is exactly 2^p.

theorem LucasLehmer.order_ineq E1 #

∀ (p' : ℕ), lucasLehmerResidue (p' + 2) = 0 → 2 ^ (p' + 2) < ↑(LucasLehmer.q (p' + 2)) ^ 2

theorem lucas_lehmer_sufficiency E3 #

∀ (p : ℕ), 1 < p → LucasLehmerTest p → Nat.Prime (mersenne p)

Lucas–Lehmer primality test: sufficiency direction.

theorem lucas_lehmer_necessity E1 #

∀ (p : ℕ), 3 ≤ p → Nat.Prime (mersenne p) → LucasLehmerTest p

If 2^p - 1 is prime then the Lucas-Lehmer test holds, s (p - 2) % (2^p - 1) = 0.

def LucasLehmer.norm_num_ext.sModNat E1 #

ℕ → ℕ → ℕ

Version of sMod that is ℕ-valued. One should have q = 2 ^ p - 1. This can be reduced by the kernel.

theorem LucasLehmer.norm_num_ext.sModNat_eq_sMod E1 #

∀ (p k : ℕ), 2 ≤ p → ↑(LucasLehmer.norm_num_ext.sModNat (2 ^ p - 1) k) = LucasLehmer.sMod p k

def LucasLehmer.norm_num_ext.sModNatTR E1 #

ℕ → ℕ → ℕ

Tail-recursive version of sModNat.

def LucasLehmer.norm_num_ext.sModNatTR.go E1 #

ℕ → ℕ → ℕ → ℕ

Helper function for sMod''.

def LucasLehmer.norm_num_ext.sModNatAux E0 #

ℕ → ℕ → ℕ → ℕ

Generalization of sModNat with arbitrary base case, useful for proving sModNatTR and sModNat agree.

theorem LucasLehmer.norm_num_ext.sModNatAux_eq E0 #

∀ (q k : ℕ), LucasLehmer.norm_num_ext.sModNatAux (4 % q) q k = LucasLehmer.norm_num_ext.sModNat q k

theorem LucasLehmer.norm_num_ext.sModNatTR_eq_sModNat E1 #

∀ (q i : ℕ), LucasLehmer.norm_num_ext.sModNatTR q i = LucasLehmer.norm_num_ext.sModNat q i

theorem LucasLehmer.norm_num_ext.testTrueHelper E1 #

∀ (p : ℕ), Nat.blt 1 p = true → LucasLehmer.norm_num_ext.sModNatTR (2 ^ p - 1) (p - 2) = 0 → LucasLehmerTest p

theorem LucasLehmer.norm_num_ext.testFalseHelper E1 #

∀ (p : ℕ),
  Nat.blt 1 p = true → Nat.ble 1 (LucasLehmer.norm_num_ext.sModNatTR (2 ^ p - 1) (p - 2)) = true → ¬LucasLehmerTest p

theorem LucasLehmer.norm_num_ext.isNat_lucasLehmerTest E1 #

∀ {p np : ℕ}, Mathlib.Meta.NormNum.IsNat p np → LucasLehmerTest np → LucasLehmerTest p

theorem LucasLehmer.norm_num_ext.isNat_not_lucasLehmerTest E1 #

∀ {p np : ℕ}, Mathlib.Meta.NormNum.IsNat p np → ¬LucasLehmerTest np → ¬LucasLehmerTest p

def LucasLehmer.norm_num_ext.evalLucasLehmerTest E1 #

Mathlib.Meta.NormNum.NormNumExt

Calculate LucasLehmer.LucasLehmerTest p for 2 ≤ p by using kernel reduction for the sMod' function.

theorem modEq_mersenne E1 #

∀ (n k : ℕ), k ≡ k / 2 ^ n + k % 2 ^ n [MOD 2 ^ n - 1]

Deprecated aliases

NameNew nameSince
LucasLehmer.norm_num_ext.sModNat_auxLucasLehmer.norm_num_ext.sModNatAux2026-06-06
LucasLehmer.norm_num_ext.sModNat_aux_eqLucasLehmer.norm_num_ext.sModNatAux_eq2026-06-06