import Mathlib import OeisA234388.Generated.PhiTable /-! # A compositeness checker that the Lean kernel can run Everything 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 it on 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` asks for 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. `vPlus`, `vMinus`: the two expressions of the conjecture, and `checkRange`, which runs the checker over a range of `k` with one certificate per `k`. ## Writing for the kernel The kernel evaluates `+ * / % ^ gcd ==` on numerals with GMP, but everything else by unfolding definitions, and it remembers every intermediate term. Three rules follow, and they explain the shape 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 OeisA234388 /- ## Modular exponentiation -/ /-- `force f x = f x`, but written so that the kernel evaluates `x` to a numeral before calling `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 to be 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 := by have 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 stepK rcases Nat.mod_two_eq_zero_or_one (e / 2 ^ i) with h | h · have key : b ^ (e / 2 ^ i) = b ^ E * b ^ E := by conv_lhs => rw [show e / 2 ^ i = E + E by omega] ring simp 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 := by conv_lhs => rw [show e / 2 ^ i = E + E + 1 by omega] ring simp only [h, show ((1 : ℕ) == 1) = true from rfl, cond_true] rw [key]; exact ((Nat.mod_modEq _ _).trans hsq).mul_right b theorem loopK_eq (b m e : ℕ) : ∀ i, loopK b m e i (b ^ (e / 2 ^ i) % m) = b ^ e % m := by intro i induction 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 := by rw [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 0 theorem phiLoop_eq (m : ℕ) : ∀ i acc, phiLoop m i acc = acc + ((Finset.range i).filter (Nat.Coprime m)).card := by intro i induction i with | zero => intro acc; simp [phiLoop] | succ i ih => intro acc rw [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 := h rw [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 := h rw [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 := by rw [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 := by have hcop : Nat.Coprime 2 p := (Nat.coprime_primes Nat.prime_two hp).2 (by omega) have h := Nat.ModEq.pow_totient hcop rw [Nat.totient_prime hp] at h have : 2 ^ (p - 1) % p = 1 % p := h rw [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 := by intro hp unfold certOK at h split_ifs at h with hq · simp only [Bool.and_eq_true, decide_eq_true_eq, bne_iff_ne, ne_eq] at h exact 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 h obtain ⟨⟨h1, h2⟩, h3⟩ := h have hdvd : q ∣ N := Nat.dvd_of_mod_eq_zero h3 rcases (Nat.dvd_prime hp).1 hdvd with h | h <;> omega /-- Table lookup for Euler's totient, `m < 9144` (`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 two expressions of the conjecture, and the range checker -/ /-- `2^k (2^φ(n-k) - 1) + 1` -/ def vPlus (n k : ℕ) : ℕ := 2 ^ k * (2 ^ phiT (n - k) - 1) + 1 /-- `2^k (2^φ(n-k) - 1) - 1` -/ def vMinus (n k : ℕ) : ℕ := 2 ^ k * (2 ^ phiT (n - k) - 1) - 1 theorem vPlus_eq {n k : ℕ} (hφ : phiT (n - k) = (n - k).totient) : vPlus n k = 2 ^ k * (2 ^ (n - k).totient - 1) + 1 := by rw [vPlus, hφ] theorem vMinus_eq {n k : ℕ} (hφ : phiT (n - k) = (n - k).totient) : vMinus n k = 2 ^ k * (2 ^ (n - k).totient - 1) - 1 := by rw [vMinus, hφ] /-- 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 := by unfold checkRange at h rw [List.all_eq_true] at h have := h (k - lo) (List.mem_range.2 (by omega)) rw [show lo + (k - lo) = k by omega] at this exact ⟨_, this⟩ end OeisA234388