Lean / Reader
Lean readerChecker.lean
Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports OeisA234309.Generated.PhiTable, which it does not have: you can read it there, but it will not check. Build the project itself to check it.
import Mathlibimport OeisA234309.Generated.PhiTable/-!# A compositeness checker that the Lean kernel can runEverything the counterexample needs is in this file; the files in `Generated/` only apply it.The numbers are `N = 2^x + 2^y - 1`. A certificate for "`N` is not prime" is a number `q`:* `q ≥ 2`: `q` is a nontrivial divisor of `N`;* `q = 0` or `1`: `N` fails the Fermat test to base `b = 2` or `3`. Since `N + 1 = 2^x + 2^y`,Fermat's little theorem for a prime `N > b` gives `b^(2^x) * b^(2^y) = b^(N+1) ≡ b^2 (mod N)`.The left side needs only repeated squaring (`sqLoop`), with no reading of exponent bits.## Writing for the kernelThe kernel evaluates `+ * / % ^ ==` on numerals with GMP, but everything else by unfoldingdefinitions. So recursion is structural (on a step counter), and a running value is passedthrough `force`, which makes it a numeral before the next step instead of a growing expression.-/namespace OeisA234309/-- `force f x = f x`, but written so that the kernel evaluates `x` to a numeral beforecalling `f`. -/def force {α : Type} (f : ℕ → α) : ℕ → α| 0 => f 0| t + 1 => f (t + 1)@[simp] theorem force_eq {α : Type} (f : ℕ → α) (x : ℕ) : force f x = f x := by cases x <;> rfl/- ## Repeated squaring -//-- `a ↦ a^2 % m`, `i` times. -/def sqLoop (m : ℕ) : ℕ → ℕ → ℕ| 0, a => a| i + 1, a => force (sqLoop m i) (a * a % m)theorem sqLoop_modEq (m : ℕ) : ∀ i a, sqLoop m i a ≡ a ^ 2 ^ i [MOD m] := byintro iinduction i with| zero => intro a; simp [sqLoop, Nat.ModEq]| succ i ih =>intro arw [sqLoop, force_eq]refine (ih _).trans ?_have h : a * a % m ≡ a * a [MOD m] := Nat.mod_modEq _ _calc (a * a % m) ^ 2 ^ i ≡ (a * a) ^ 2 ^ i [MOD m] := h.pow __ = a ^ 2 ^ (i + 1) := by rw [← pow_two, ← pow_mul, pow_succ, mul_comm]/- ## The Fermat test for `2^(lo+d) + 2^lo - 1` -//-- `true` means: `N = 2^(lo+d) + 2^lo - 1` is greater than `b > 0` and `b^(N+1)` is not `b^2`mod `N`. Here `b^(N+1) = b^(2^lo) * b^(2^(lo+d))` is computed by `lo + d` squarings. -/def fermatBad (b lo d : ℕ) : Bool :=force (fun N =>force (fun a =>force (fun c => decide (0 < b) && decide (b < N) && (a * c % N != b * b % N)) (sqLoop N d a))(sqLoop N lo b))(2 ^ (lo + d) + 2 ^ lo - 1)theorem not_prime_of_fermatBad {b lo d : ℕ} (h : fermatBad b lo d = true) :¬ (2 ^ (lo + d) + 2 ^ lo - 1).Prime := byintro hpsimp only [fermatBad, force_eq, Bool.and_eq_true, decide_eq_true_eq, bne_iff_ne, ne_eq] at hobtain ⟨⟨hb0, hbN⟩, hne⟩ := hset N := 2 ^ (lo + d) + 2 ^ lo - 1 with hNhave hpos1 : 0 < 2 ^ (lo + d) := Nat.pow_pos (by norm_num)have hpos2 : 0 < 2 ^ lo := Nat.pow_pos (by norm_num)have hN1 : N + 1 = 2 ^ lo + 2 ^ (lo + d) := by omegahave ha : sqLoop N lo b ≡ b ^ 2 ^ lo [MOD N] := sqLoop_modEq N lo bhave hc : sqLoop N d (sqLoop N lo b) ≡ b ^ 2 ^ (lo + d) [MOD N] := byrefine (sqLoop_modEq N d _).trans ?_calc sqLoop N lo b ^ 2 ^ d ≡ (b ^ 2 ^ lo) ^ 2 ^ d [MOD N] := ha.pow __ = b ^ 2 ^ (lo + d) := by rw [← pow_mul, ← pow_add]have hprod : sqLoop N lo b * sqLoop N d (sqLoop N lo b) ≡ b ^ (N + 1) [MOD N] := byrw [hN1, pow_add]; exact ha.mul hchave hf : b ^ (N + 1) ≡ b * b [MOD N] := byhave hcop : Nat.Coprime b N :=((Nat.Prime.coprime_iff_not_dvd hp).2 fun hd => byhave := Nat.le_of_dvd hb0 hd; omega).symmhave h1 : b ^ (N - 1) ≡ 1 [MOD N] := byhave := Nat.ModEq.pow_totient hcoprwa [Nat.totient_prime hp] at thishave h3 : b ^ (N + 1) = b ^ (N - 1) * (b * b) := byrw [show N + 1 = (N - 1) + 2 by omega, pow_add, pow_two]rw [h3]simpa using h1.mul_right (b * b)exact hne (hprod.trans hf)/- ## Compositeness certificates for `2^x + 2^y - 1` -//-- The Fermat test to base `b` for `2^x + 2^y - 1`, whichever of `x`, `y` is larger. -/def fermatXY (b x y : ℕ) : Bool :=if y ≤ x then fermatBad b y (x - y) else fermatBad b x (y - x)theorem not_prime_of_fermatXY {b x y : ℕ} (h : fermatXY b x y = true) :¬ (2 ^ x + 2 ^ y - 1).Prime := byunfold fermatXY at hsplit_ifs at h with hxy· have := not_prime_of_fermatBad hrwa [show y + (x - y) = x by omega] at this· have := not_prime_of_fermatBad hrwa [show x + (y - x) = y by omega, add_comm (2 ^ y)] at this/-- Certificate check for "`2^x + 2^y - 1` is not prime": `q = 0` asks for the Fermat test tobase 2, `q = 1` to base 3 (needed when `x = y`: a Mersenne number `2^(x+1) - 1` with `x + 1` primepasses the base-2 test), and any other `q` must be a nontrivial divisor. -/def certOK (x y q : ℕ) : Bool :=if q = 0 then fermatXY 2 x yelse if q = 1 then fermatXY 3 x yelse force (fun N => decide (q < N) && (N % q == 0)) (2 ^ x + 2 ^ y - 1)theorem not_prime_of_certOK {x y q : ℕ} (h : certOK x y q = true) :¬ (2 ^ x + 2 ^ y - 1).Prime := byunfold certOK at hsplit_ifs at h with hq0 hq1· exact not_prime_of_fermatXY h· exact not_prime_of_fermatXY h· intro hpsimp only [force_eq, Bool.and_eq_true, decide_eq_true_eq, beq_iff_eq] at hobtain ⟨h2, h3⟩ := hhave hdvd : q ∣ 2 ^ x + 2 ^ y - 1 := Nat.dvd_of_mod_eq_zero h3rcases (Nat.dvd_prime hp).1 hdvd with h | h <;> omega/- ## Euler's totient by table, checked by a recurrence -//-- Table lookup for Euler's totient (`Generated/PhiSpec.lean` proves `phiT m = φ(m)`). -/def phiT (m : ℕ) : ℕ := (phiBlocks.getD (m / 128) []).getD (m % 128) 0/-- Table lookup for a prime factor of `m`. -/def spfT (m : ℕ) : ℕ := (spfBlocks.getD (m / 128) []).getD (m % 128) 0/-- Trial division: no `d` with `2 ≤ d`, `d * d ≤ p` divides `p` (complete for `p < 209^2`). -/def trialPrime (p : ℕ) : Bool :=decide (2 ≤ p) && (List.range 209).all fun d => decide (d < 2) || decide (p < d * d) || (p % d != 0)theorem prime_of_trialPrime {p : ℕ} (hb : p < 209 * 209) (h : trialPrime p = true) : p.Prime := bysimp only [trialPrime, Bool.and_eq_true, decide_eq_true_eq, List.all_eq_true, List.mem_range,Bool.or_eq_true, bne_iff_ne, ne_eq] at hobtain ⟨h2, hall⟩ := hrw [Nat.prime_def_le_sqrt]refine ⟨h2, fun m hm2 hms hdvd => ?_⟩have hmm : m * m ≤ p := Nat.le_sqrt.1 hmshave hm : m < 209 := by nlinarithrcases hall m hm with (h | h) | h· omega· omega· exact h (Nat.mod_eq_zero_of_dvd hdvd)/-- The recurrence that pins down the table at `m ≥ 2`: with `p = spfT m` a prime factor and`r = m / p`, `φ(m) = p φ(r)` if `p ∣ r`, and `(p - 1) φ(r)` otherwise. -/def phiOK (m : ℕ) : Bool :=force (fun p => trialPrime p && (m % p == 0) &&(phiT m == bif m / p % p == 0 then p * phiT (m / p) else (p - 1) * phiT (m / p))) (spfT m)theorem phiT_eq_of_phiOK {B : ℕ} (hB : B ≤ 209 * 209) (h0 : phiT 0 = 0) (h1 : phiT 1 = 1)(hall : ∀ m, 2 ≤ m → m < B → phiOK m = true) : ∀ m, m < B → phiT m = m.totient := byintro minduction m using Nat.strong_induction_on with| _ m ih =>intro hmrcases Nat.lt_or_ge m 2 with hlt | hge· interval_cases m· simpa using h0· simpa using h1· have h := hall m hge hmsimp only [phiOK, force_eq, Bool.and_eq_true, beq_iff_eq] at hobtain ⟨⟨htp, hdiv⟩, hphi⟩ := hset p := spfT m with hpdefhave hpdvd : p ∣ m := Nat.dvd_of_mod_eq_zero hdivhave hple : p ≤ m := Nat.le_of_dvd (by omega) hpdvdhave hp : p.Prime := prime_of_trialPrime (by omega) htphave hmr : p * (m / p) = m := Nat.mul_div_cancel' hpdvdhave hrlt : m / p < m := Nat.div_lt_self (by omega) hp.one_lthave hr := ih (m / p) hrlt (by omega)rw [hphi, hr]by_cases hd : m / p % p = 0· rw [show (m / p % p == 0) = true from beq_iff_eq.2 hd, cond_true]conv_rhs => rw [← hmr]exact (Nat.totient_mul_of_prime_of_dvd hp (Nat.dvd_of_mod_eq_zero hd)).symm· rw [show (m / p % p == 0) = false from beq_eq_false_iff_ne.2 hd, cond_false]conv_rhs => rw [← hmr]exact (Nat.totient_mul_of_prime_of_not_dvd hp(fun hdvd => hd (Nat.mod_eq_zero_of_dvd hdvd))).symm/- ## The range checker -//-- The certificate check at `n`, `k`, with the totients read from the table. -/def certK (n k q : ℕ) : Bool := certOK (phiT k) (phiT (n - k)) q/-- Check `k = lo + i` for `i < m`, with `cert[i]` the certificate for that `k`. -/def checkRange (g : ℕ → ℕ → Bool) (lo m : ℕ) (cert : List ℕ) : Bool :=(List.range m).all fun i => g (lo + i) (cert.getD i 0)theorem checkRange_spec {g : ℕ → ℕ → Bool} {lo m : ℕ} {cert : List ℕ}(h : checkRange g lo m cert = true) {k : ℕ} (h1 : lo ≤ k) (h2 : k < lo + m) :∃ q, g k q = true := byunfold checkRange at hrw [List.all_eq_true] at hhave := h (k - lo) (List.mem_range.2 (by omega))rw [show lo + (k - lo) = k by omega] at thisexact ⟨_, this⟩end OeisA234309