import Mathlib import OeisA234309.Generated.PhiTable /-! # A compositeness checker that the Lean kernel can run Everything 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 kernel The kernel evaluates `+ * / % ^ ==` on numerals with GMP, but everything else by unfolding definitions. So recursion is structural (on a step counter), and a running value is passed through `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 before calling `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] := by intro i induction i with | zero => intro a; simp [sqLoop, Nat.ModEq] | succ i ih => intro a rw [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 := by intro hp simp only [fermatBad, force_eq, Bool.and_eq_true, decide_eq_true_eq, bne_iff_ne, ne_eq] at h obtain ⟨⟨hb0, hbN⟩, hne⟩ := h set N := 2 ^ (lo + d) + 2 ^ lo - 1 with hN have 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 omega have ha : sqLoop N lo b ≡ b ^ 2 ^ lo [MOD N] := sqLoop_modEq N lo b have hc : sqLoop N d (sqLoop N lo b) ≡ b ^ 2 ^ (lo + d) [MOD N] := by refine (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] := by rw [hN1, pow_add]; exact ha.mul hc have hf : b ^ (N + 1) ≡ b * b [MOD N] := by have hcop : Nat.Coprime b N := ((Nat.Prime.coprime_iff_not_dvd hp).2 fun hd => by have := Nat.le_of_dvd hb0 hd; omega).symm have h1 : b ^ (N - 1) ≡ 1 [MOD N] := by have := Nat.ModEq.pow_totient hcop rwa [Nat.totient_prime hp] at this have h3 : b ^ (N + 1) = b ^ (N - 1) * (b * b) := by rw [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 := by unfold fermatXY at h split_ifs at h with hxy · have := not_prime_of_fermatBad h rwa [show y + (x - y) = x by omega] at this · have := not_prime_of_fermatBad h rwa [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 to base 2, `q = 1` to base 3 (needed when `x = y`: a Mersenne number `2^(x+1) - 1` with `x + 1` prime passes 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 y else if q = 1 then fermatXY 3 x y else 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 := by unfold certOK at h split_ifs at h with hq0 hq1 · exact not_prime_of_fermatXY h · exact not_prime_of_fermatXY h · intro hp simp only [force_eq, Bool.and_eq_true, decide_eq_true_eq, beq_iff_eq] at h obtain ⟨h2, h3⟩ := h have hdvd : q ∣ 2 ^ x + 2 ^ y - 1 := Nat.dvd_of_mod_eq_zero h3 rcases (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 := by simp 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 h obtain ⟨h2, hall⟩ := h rw [Nat.prime_def_le_sqrt] refine ⟨h2, fun m hm2 hms hdvd => ?_⟩ have hmm : m * m ≤ p := Nat.le_sqrt.1 hms have hm : m < 209 := by nlinarith rcases 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 := by intro m induction m using Nat.strong_induction_on with | _ m ih => intro hm rcases Nat.lt_or_ge m 2 with hlt | hge · interval_cases m · simpa using h0 · simpa using h1 · have h := hall m hge hm simp only [phiOK, force_eq, Bool.and_eq_true, beq_iff_eq] at h obtain ⟨⟨htp, hdiv⟩, hphi⟩ := h set p := spfT m with hpdef have hpdvd : p ∣ m := Nat.dvd_of_mod_eq_zero hdiv have hple : p ≤ m := Nat.le_of_dvd (by omega) hpdvd have hp : p.Prime := prime_of_trialPrime (by omega) htp have hmr : p * (m / p) = m := Nat.mul_div_cancel' hpdvd have hrlt : m / p < m := Nat.div_lt_self (by omega) hp.one_lt have 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 := 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 OeisA234309