Lean / Reader
Lean readerChecker.lean
Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports FormalConjectures.OEIS.«234360», OeisA234360.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 FormalConjectures.OEIS.«234360»import OeisA234360.Generated.PhiTable/-!# A compositeness checker that the Lean kernel can runEverything the counterexamples need is in this file; the files in `Generated/` only apply it.1. `powMod b e m = b ^ e % m`: binary exponentiation, written so that the kernel can evaluate iton numbers of tens of thousands of bits (`decide +kernel`; there is no `native_decide`).2. `certOK N q = true → ¬ N.Prime`: `q ≠ 0` must be a nontrivial divisor of `N`; `q = 0` asksfor the Fermat test `2 ^ (N - 1) % N ≠ 1`, sound by Fermat's little theorem.3. `phiT`: Euler's totient by table lookup, and `phiK`, the slow kernel-evaluable totient that`Generated/PhiSpec.lean` uses to prove the table correct.4. `f1`, `f3`, `ok4`: the three expressions of the conjecture, and `checkRange`, which runs thechecker over a range of `k` with one certificate per `k`.## Writing for the kernelThe kernel evaluates `+ * / % ^ gcd ==` on numerals with GMP, but everything else by unfoldingdefinitions, and it remembers every intermediate term. Three rules follow, and they explain theshape of the code below:* recursion is structural (on a step counter), never well-founded;* a running value is passed through `force`, so that it is a numeral, not a growing expression;* `Nat.log2` and friends are avoided: they unfold step by step (see `bitsK`).-/namespace OeisA234360/- ## Modular exponentiation -//-- `force f x = f x`, but written so that the kernel evaluates `x` to a numeral beforecalling `f`. This keeps accumulators from piling up as unevaluated terms. -/def force (f : ℕ → ℕ) : ℕ → ℕ| 0 => f 0| t + 1 => f (t + 1)@[simp] theorem force_eq (f : ℕ → ℕ) (x : ℕ) : force f x = f x := by cases x <;> rfl/-- One square-and-multiply step for bit `i` of the exponent `e`. The bit is read as`e / 2 ^ i % 2`: the kernel evaluates `/`, `^` and `%` on numerals directly, whereas `e >>> i`is unfolded into `i` nested halvings. -/def stepK (b m e i acc : ℕ) : ℕ :=bif e / 2 ^ i % 2 == 1 then acc * acc % m * b % m else acc * acc % m/-- Square-and-multiply from the top bit down; structurally recursive, so the kernel can run it. -/def loopK (b m e : ℕ) : ℕ → ℕ → ℕ| 0, acc => acc| i + 1, acc => force (loopK b m e i) (stepK b m e i acc)/-- Bisection for the bit length of `e` (exact when `e < 2 ^ 2 ^ j`), using only `/`, `^`, `+`on numerals. `Nat.log2` is not used because the kernel unfolds it step by step. Nothing needs tobe proved about this function: `powMod` is correct for any number of loop steps. -/def bitsK (e : ℕ) : ℕ → ℕ → ℕ| 0, F => F| j + 1, F => force (bitsK e j) (bif e / 2 ^ (F + 2 ^ j) == 0 then F else F + 2 ^ j)/-- Binary modular exponentiation that reduces inside the Lean kernel. -/def powMod (b e m : ℕ) : ℕ :=force (fun F => loopK b m e F (b ^ (e / 2 ^ F) % m)) (bitsK e 24 0 + 1)theorem stepK_eq (b m e i : ℕ) :stepK b m e i (b ^ (e / 2 ^ (i + 1)) % m) = b ^ (e / 2 ^ i) % m := byhave hE : e / 2 ^ (i + 1) = (e / 2 ^ i) / 2 := by rw [pow_succ, Nat.div_div_eq_div_mul]set E := e / 2 ^ (i + 1)have hsq : b ^ E % m * (b ^ E % m) ≡ b ^ E * b ^ E [MOD m] :=(Nat.mod_modEq _ _).mul (Nat.mod_modEq _ _)unfold stepKrcases Nat.mod_two_eq_zero_or_one (e / 2 ^ i) with h | h· have key : b ^ (e / 2 ^ i) = b ^ E * b ^ E := byconv_lhs => rw [show e / 2 ^ i = E + E by omega]ringsimp only [h, show ((0 : ℕ) == 1) = false from rfl, cond_false]rw [key]; exact hsq· have key : b ^ (e / 2 ^ i) = b ^ E * b ^ E * b := byconv_lhs => rw [show e / 2 ^ i = E + E + 1 by omega]ringsimp only [h, show ((1 : ℕ) == 1) = true from rfl, cond_true]rw [key]; exact ((Nat.mod_modEq _ _).trans hsq).mul_right btheorem loopK_eq (b m e : ℕ) : ∀ i, loopK b m e i (b ^ (e / 2 ^ i) % m) = b ^ e % m := byintro iinduction i with| zero => simp [loopK]| succ i ih => rw [loopK, force_eq, stepK_eq, ih]theorem powMod_eq (b e m : ℕ) : powMod b e m = b ^ e % m := byrw [powMod, force_eq]; exact loopK_eq b m e _/- ## Euler's totient, slowly (only used to verify the table) -//-- Count of `a < i` coprime to `m`, added to `acc`. -/def phiLoop (m : ℕ) : ℕ → ℕ → ℕ| 0, acc => acc| i + 1, acc => force (phiLoop m i) (bif Nat.gcd m i == 1 then acc + 1 else acc)/-- Euler's totient, in a form the kernel can evaluate. -/def phiK (m : ℕ) : ℕ := phiLoop m m 0theorem phiLoop_eq (m : ℕ) : ∀ i acc,phiLoop m i acc = acc + ((Finset.range i).filter (Nat.Coprime m)).card := byintro iinduction i with| zero => intro acc; simp [phiLoop]| succ i ih =>intro accrw [phiLoop, force_eq, ih, Finset.range_add_one, Finset.filter_insert]by_cases h : Nat.gcd m i = 1· have hc : Nat.Coprime m i := hrw [if_pos hc, Finset.card_insert_of_notMem (by simp)]rw [show (Nat.gcd m i == 1) = true from beq_iff_eq.2 h, cond_true]omega· have hc : ¬ Nat.Coprime m i := hrw [if_neg hc, show (Nat.gcd m i == 1) = false from beq_eq_false_iff_ne.2 h, cond_false]theorem phiK_eq (m : ℕ) : phiK m = m.totient := byrw [phiK, phiLoop_eq, Nat.totient_eq_card_coprime]; simp/- ## Compositeness certificates -//-- Fermat's little theorem for base `2`, in the form used by the checker. -/theorem two_pow_pred_mod_of_prime {p : ℕ} (hp : p.Prime) (h2 : 2 < p) : 2 ^ (p - 1) % p = 1 := byhave hcop : Nat.Coprime 2 p := (Nat.coprime_primes Nat.prime_two hp).2 (by omega)have h := Nat.ModEq.pow_totient hcoprw [Nat.totient_prime hp] at hhave : 2 ^ (p - 1) % p = 1 % p := hrw [this, Nat.mod_eq_of_lt (by omega)]/-- Certificate check for "`N` is not prime". `q ≠ 0`: `q` is a nontrivial divisor.`q = 0`: `N > 2` fails the base-2 Fermat test. -/def certOK (N q : ℕ) : Bool :=if q = 0 then decide (2 < N) && (powMod 2 (N - 1) N != 1)else decide (1 < q) && decide (q < N) && (N % q == 0)theorem not_prime_of_certOK {N q : ℕ} (h : certOK N q = true) : ¬ N.Prime := byintro hpunfold certOK at hsplit_ifs at h with hq· simp only [Bool.and_eq_true, decide_eq_true_eq, bne_iff_ne, ne_eq] at hexact h.2 (by rw [powMod_eq]; exact two_pow_pred_mod_of_prime hp h.1)· simp only [Bool.and_eq_true, decide_eq_true_eq, beq_iff_eq] at hobtain ⟨⟨h1, h2⟩, h3⟩ := hhave hdvd : q ∣ N := Nat.dvd_of_mod_eq_zero h3rcases (Nat.dvd_prime hp).1 hdvd with h | h <;> omega/-- Table lookup for Euler's totient, `m < 7588` (`Generated/PhiSpec.lean` proves`phiT m = φ(m)` there).Evaluating `phiK` for every `k` would cost the kernel about `n` steps each; a lookup costs ~200. -/def phiT (m : ℕ) : ℕ := (phiBlocks.getD (m / 128) []).getD (m % 128) 0/- ## The three expressions of the conjecture, and the range checker -//-- `(k+1)^φ(n-k) + k` -/def f1 (n k : ℕ) : ℕ := (k + 1) ^ phiT (n - k) + k/-- `k (k+1)^φ(n-k) + 1` -/def f3 (n k : ℕ) : ℕ := k * (k + 1) ^ phiT (n - k) + 1theorem f1_eq {n k : ℕ} (hφ : phiT (n - k) = (n - k).totient) :f1 n k = (k + 1) ^ (n - k).totient + k := by rw [f1, hφ]theorem f3_eq {n k : ℕ} (hφ : phiT (n - k) = (n - k).totient) :f3 n k = k * (k + 1) ^ (n - k).totient + 1 := by rw [f3, hφ]/-- For `conjecture_4`. Even `φ`: the only candidate `p = k (k+1)^(φ/2) - 1` is certifiedcomposite. Odd `φ`: `q` witnesses that `k^2 (k+1)^φ` lies strictly between two consecutive squares. -/def ok4 (n k q : ℕ) : Bool :=let t := phiT (n - k)if t % 2 = 0 then certOK (k * (k + 1) ^ (t / 2) - 1) qelselet X := k ^ 2 * (k + 1) ^ tdecide (q ^ 2 < X) && decide (X < (q + 1) ^ 2)/-- 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⟩theorem ok4_spec {n k q : ℕ} (hφ : phiT (n - k) = (n - k).totient) (h : ok4 n k q = true) :¬ ∃ p : ℕ, p.Prime ∧ (p + 1) ^ 2 = k ^ 2 * (k + 1) ^ (n - k).totient := byrintro ⟨p, hp, he⟩unfold ok4 at hsimp only at hrw [hφ] at hgeneralize (n - k).totient = t at *split_ifs at h with ht· obtain ⟨m, rfl⟩ : ∃ m, t = 2 * m := ⟨t / 2, by omega⟩rw [show 2 * m / 2 = m by omega] at hhave he' : (p + 1) ^ 2 = (k * (k + 1) ^ m) ^ 2 := by rw [he]; ringhave hsq : p + 1 = k * (k + 1) ^ m :=Nat.pow_left_injective (by norm_num : (2 : ℕ) ≠ 0) he'have : k * (k + 1) ^ m - 1 = p := by omegaexact not_prime_of_certOK h (this ▸ hp)· simp only [Bool.and_eq_true, decide_eq_true_eq] at hrw [← he] at hhave h1 : q < p + 1 := lt_of_pow_lt_pow_left₀ 2 (Nat.zero_le _) h.1have h2 : p + 1 < q + 1 := lt_of_pow_lt_pow_left₀ 2 (Nat.zero_le _) h.2omegaend OeisA234360