Lean / Reader
Lean reader

Checker.lean

206 lines19 declarationsno sorryimports Mathlib OeisA234309.Generated.PhiTable

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.

  1. import Mathlib
  2. import OeisA234309.Generated.PhiTable
  3. /-!
  4. # A compositeness checker that the Lean kernel can run
  5. Everything the counterexample needs is in this file; the files in `Generated/` only apply it.
  6. The numbers are `N = 2^x + 2^y - 1`. A certificate for "`N` is not prime" is a number `q`:
  7. * `q ≥ 2`: `q` is a nontrivial divisor of `N`;
  8. * `q = 0` or `1`: `N` fails the Fermat test to base `b = 2` or `3`. Since `N + 1 = 2^x + 2^y`,
  9. Fermat's little theorem for a prime `N > b` gives `b^(2^x) * b^(2^y) = b^(N+1) ≡ b^2 (mod N)`.
  10. The left side needs only repeated squaring (`sqLoop`), with no reading of exponent bits.
  11. ## Writing for the kernel
  12. The kernel evaluates `+ * / % ^ ==` on numerals with GMP, but everything else by unfolding
  13. definitions. So recursion is structural (on a step counter), and a running value is passed
  14. through `force`, which makes it a numeral before the next step instead of a growing expression.
  15. -/
  16. namespace OeisA234309
  17. /-- `force f x = f x`, but written so that the kernel evaluates `x` to a numeral before
  18. calling `f`. -/
  19. def force {α : Type} (f : ℕ → α) : ℕ → α
  20. | 0 => f 0
  21. | t + 1 => f (t + 1)
  22. @[simp] theorem force_eq {α : Type} (f : ℕ → α) (x : ℕ) : force f x = f x := by cases x <;> rfl
  23. /- ## Repeated squaring -/
  24. /-- `a ↦ a^2 % m`, `i` times. -/
  25. def sqLoop (m : ℕ) : ℕ → ℕ → ℕ
  26. | 0, a => a
  27. | i + 1, a => force (sqLoop m i) (a * a % m)
  28. theorem sqLoop_modEq (m : ℕ) : ∀ i a, sqLoop m i a ≡ a ^ 2 ^ i [MOD m] := by
  29. intro i
  30. induction i with
  31. | zero => intro a; simp [sqLoop, Nat.ModEq]
  32. | succ i ih =>
  33. intro a
  34. rw [sqLoop, force_eq]
  35. refine (ih _).trans ?_
  36. have h : a * a % m ≡ a * a [MOD m] := Nat.mod_modEq _ _
  37. calc (a * a % m) ^ 2 ^ i ≡ (a * a) ^ 2 ^ i [MOD m] := h.pow _
  38. _ = a ^ 2 ^ (i + 1) := by rw [← pow_two, ← pow_mul, pow_succ, mul_comm]
  39. /- ## The Fermat test for `2^(lo+d) + 2^lo - 1` -/
  40. /-- `true` means: `N = 2^(lo+d) + 2^lo - 1` is greater than `b > 0` and `b^(N+1)` is not `b^2`
  41. mod `N`. Here `b^(N+1) = b^(2^lo) * b^(2^(lo+d))` is computed by `lo + d` squarings. -/
  42. def fermatBad (b lo d : ℕ) : Bool :=
  43. force (fun N =>
  44. force (fun a =>
  45. force (fun c => decide (0 < b) && decide (b < N) && (a * c % N != b * b % N)) (sqLoop N d a))
  46. (sqLoop N lo b))
  47. (2 ^ (lo + d) + 2 ^ lo - 1)
  48. theorem not_prime_of_fermatBad {b lo d : ℕ} (h : fermatBad b lo d = true) :
  49. ¬ (2 ^ (lo + d) + 2 ^ lo - 1).Prime := by
  50. intro hp
  51. simp only [fermatBad, force_eq, Bool.and_eq_true, decide_eq_true_eq, bne_iff_ne, ne_eq] at h
  52. obtain ⟨⟨hb0, hbN⟩, hne⟩ := h
  53. set N := 2 ^ (lo + d) + 2 ^ lo - 1 with hN
  54. have hpos1 : 0 < 2 ^ (lo + d) := Nat.pow_pos (by norm_num)
  55. have hpos2 : 0 < 2 ^ lo := Nat.pow_pos (by norm_num)
  56. have hN1 : N + 1 = 2 ^ lo + 2 ^ (lo + d) := by omega
  57. have ha : sqLoop N lo b ≡ b ^ 2 ^ lo [MOD N] := sqLoop_modEq N lo b
  58. have hc : sqLoop N d (sqLoop N lo b) ≡ b ^ 2 ^ (lo + d) [MOD N] := by
  59. refine (sqLoop_modEq N d _).trans ?_
  60. calc sqLoop N lo b ^ 2 ^ d ≡ (b ^ 2 ^ lo) ^ 2 ^ d [MOD N] := ha.pow _
  61. _ = b ^ 2 ^ (lo + d) := by rw [← pow_mul, ← pow_add]
  62. have hprod : sqLoop N lo b * sqLoop N d (sqLoop N lo b) ≡ b ^ (N + 1) [MOD N] := by
  63. rw [hN1, pow_add]; exact ha.mul hc
  64. have hf : b ^ (N + 1) ≡ b * b [MOD N] := by
  65. have hcop : Nat.Coprime b N :=
  66. ((Nat.Prime.coprime_iff_not_dvd hp).2 fun hd => by
  67. have := Nat.le_of_dvd hb0 hd; omega).symm
  68. have h1 : b ^ (N - 1) ≡ 1 [MOD N] := by
  69. have := Nat.ModEq.pow_totient hcop
  70. rwa [Nat.totient_prime hp] at this
  71. have h3 : b ^ (N + 1) = b ^ (N - 1) * (b * b) := by
  72. rw [show N + 1 = (N - 1) + 2 by omega, pow_add, pow_two]
  73. rw [h3]
  74. simpa using h1.mul_right (b * b)
  75. exact hne (hprod.trans hf)
  76. /- ## Compositeness certificates for `2^x + 2^y - 1` -/
  77. /-- The Fermat test to base `b` for `2^x + 2^y - 1`, whichever of `x`, `y` is larger. -/
  78. def fermatXY (b x y : ℕ) : Bool :=
  79. if y ≤ x then fermatBad b y (x - y) else fermatBad b x (y - x)
  80. theorem not_prime_of_fermatXY {b x y : ℕ} (h : fermatXY b x y = true) :
  81. ¬ (2 ^ x + 2 ^ y - 1).Prime := by
  82. unfold fermatXY at h
  83. split_ifs at h with hxy
  84. · have := not_prime_of_fermatBad h
  85. rwa [show y + (x - y) = x by omega] at this
  86. · have := not_prime_of_fermatBad h
  87. rwa [show x + (y - x) = y by omega, add_comm (2 ^ y)] at this
  88. /-- Certificate check for "`2^x + 2^y - 1` is not prime": `q = 0` asks for the Fermat test to
  89. base 2, `q = 1` to base 3 (needed when `x = y`: a Mersenne number `2^(x+1) - 1` with `x + 1` prime
  90. passes the base-2 test), and any other `q` must be a nontrivial divisor. -/
  91. def certOK (x y q : ℕ) : Bool :=
  92. if q = 0 then fermatXY 2 x y
  93. else if q = 1 then fermatXY 3 x y
  94. else force (fun N => decide (q < N) && (N % q == 0)) (2 ^ x + 2 ^ y - 1)
  95. theorem not_prime_of_certOK {x y q : ℕ} (h : certOK x y q = true) :
  96. ¬ (2 ^ x + 2 ^ y - 1).Prime := by
  97. unfold certOK at h
  98. split_ifs at h with hq0 hq1
  99. · exact not_prime_of_fermatXY h
  100. · exact not_prime_of_fermatXY h
  101. · intro hp
  102. simp only [force_eq, Bool.and_eq_true, decide_eq_true_eq, beq_iff_eq] at h
  103. obtain ⟨h2, h3⟩ := h
  104. have hdvd : q ∣ 2 ^ x + 2 ^ y - 1 := Nat.dvd_of_mod_eq_zero h3
  105. rcases (Nat.dvd_prime hp).1 hdvd with h | h <;> omega
  106. /- ## Euler's totient by table, checked by a recurrence -/
  107. /-- Table lookup for Euler's totient (`Generated/PhiSpec.lean` proves `phiT m = φ(m)`). -/
  108. def phiT (m : ℕ) : ℕ := (phiBlocks.getD (m / 128) []).getD (m % 128) 0
  109. /-- Table lookup for a prime factor of `m`. -/
  110. def spfT (m : ℕ) : ℕ := (spfBlocks.getD (m / 128) []).getD (m % 128) 0
  111. /-- Trial division: no `d` with `2 ≤ d`, `d * d ≤ p` divides `p` (complete for `p < 209^2`). -/
  112. def trialPrime (p : ℕ) : Bool :=
  113. decide (2 ≤ p) && (List.range 209).all fun d => decide (d < 2) || decide (p < d * d) || (p % d != 0)
  114. theorem prime_of_trialPrime {p : ℕ} (hb : p < 209 * 209) (h : trialPrime p = true) : p.Prime := by
  115. simp only [trialPrime, Bool.and_eq_true, decide_eq_true_eq, List.all_eq_true, List.mem_range,
  116. Bool.or_eq_true, bne_iff_ne, ne_eq] at h
  117. obtain ⟨h2, hall⟩ := h
  118. rw [Nat.prime_def_le_sqrt]
  119. refine ⟨h2, fun m hm2 hms hdvd => ?_⟩
  120. have hmm : m * m ≤ p := Nat.le_sqrt.1 hms
  121. have hm : m < 209 := by nlinarith
  122. rcases hall m hm with (h | h) | h
  123. · omega
  124. · omega
  125. · exact h (Nat.mod_eq_zero_of_dvd hdvd)
  126. /-- The recurrence that pins down the table at `m ≥ 2`: with `p = spfT m` a prime factor and
  127. `r = m / p`, `φ(m) = p φ(r)` if `p ∣ r`, and `(p - 1) φ(r)` otherwise. -/
  128. def phiOK (m : ℕ) : Bool :=
  129. force (fun p => trialPrime p && (m % p == 0) &&
  130. (phiT m == bif m / p % p == 0 then p * phiT (m / p) else (p - 1) * phiT (m / p))) (spfT m)
  131. theorem phiT_eq_of_phiOK {B : ℕ} (hB : B ≤ 209 * 209) (h0 : phiT 0 = 0) (h1 : phiT 1 = 1)
  132. (hall : ∀ m, 2 ≤ m → m < B → phiOK m = true) : ∀ m, m < B → phiT m = m.totient := by
  133. intro m
  134. induction m using Nat.strong_induction_on with
  135. | _ m ih =>
  136. intro hm
  137. rcases Nat.lt_or_ge m 2 with hlt | hge
  138. · interval_cases m
  139. · simpa using h0
  140. · simpa using h1
  141. · have h := hall m hge hm
  142. simp only [phiOK, force_eq, Bool.and_eq_true, beq_iff_eq] at h
  143. obtain ⟨⟨htp, hdiv⟩, hphi⟩ := h
  144. set p := spfT m with hpdef
  145. have hpdvd : p ∣ m := Nat.dvd_of_mod_eq_zero hdiv
  146. have hple : p ≤ m := Nat.le_of_dvd (by omega) hpdvd
  147. have hp : p.Prime := prime_of_trialPrime (by omega) htp
  148. have hmr : p * (m / p) = m := Nat.mul_div_cancel' hpdvd
  149. have hrlt : m / p < m := Nat.div_lt_self (by omega) hp.one_lt
  150. have hr := ih (m / p) hrlt (by omega)
  151. rw [hphi, hr]
  152. by_cases hd : m / p % p = 0
  153. · rw [show (m / p % p == 0) = true from beq_iff_eq.2 hd, cond_true]
  154. conv_rhs => rw [← hmr]
  155. exact (Nat.totient_mul_of_prime_of_dvd hp (Nat.dvd_of_mod_eq_zero hd)).symm
  156. · rw [show (m / p % p == 0) = false from beq_eq_false_iff_ne.2 hd, cond_false]
  157. conv_rhs => rw [← hmr]
  158. exact (Nat.totient_mul_of_prime_of_not_dvd hp
  159. (fun hdvd => hd (Nat.mod_eq_zero_of_dvd hdvd))).symm
  160. /- ## The range checker -/
  161. /-- The certificate check at `n`, `k`, with the totients read from the table. -/
  162. def certK (n k q : ℕ) : Bool := certOK (phiT k) (phiT (n - k)) q
  163. /-- Check `k = lo + i` for `i < m`, with `cert[i]` the certificate for that `k`. -/
  164. def checkRange (g : ℕ → ℕ → Bool) (lo m : ℕ) (cert : List ℕ) : Bool :=
  165. (List.range m).all fun i => g (lo + i) (cert.getD i 0)
  166. theorem checkRange_spec {g : ℕ → ℕ → Bool} {lo m : ℕ} {cert : List ℕ}
  167. (h : checkRange g lo m cert = true) {k : ℕ} (h1 : lo ≤ k) (h2 : k < lo + m) :
  168. ∃ q, g k q = true := by
  169. unfold checkRange at h
  170. rw [List.all_eq_true] at h
  171. have := h (k - lo) (List.mem_range.2 (by omega))
  172. rw [show lo + (k - lo) = k by omega] at this
  173. exact ⟨_, this⟩
  174. end OeisA234309