Lean / Reader
Lean reader

Checker.lean

178 lines23 declarations1 × sorry / admit / native_decideimports Mathlib OeisA234388.Generated.PhiTable

Lean 4 Web has Mathlib, so it can check files that import only Mathlib. This one also imports OeisA234388.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 OeisA234388.Generated.PhiTable
  3. /-!
  4. # A compositeness checker that the Lean kernel can run
  5. Everything the counterexamples need is in this file; the files in `Generated/` only apply it.
  6. 1. `powMod b e m = b ^ e % m`: binary exponentiation, written so that the kernel can evaluate it
  7. on numbers of tens of thousands of bits (`decide +kernel`; there is no `native_decide`).
  8. 2. `certOK N q = true → ¬ N.Prime`: `q ≠ 0` must be a nontrivial divisor of `N`; `q = 0` asks
  9. for the Fermat test `2 ^ (N - 1) % N ≠ 1`, sound by Fermat's little theorem.
  10. 3. `phiT`: Euler's totient by table lookup, and `phiK`, the slow kernel-evaluable totient that
  11. `Generated/PhiSpec.lean` uses to prove the table correct.
  12. 4. `vPlus`, `vMinus`: the two expressions of the conjecture, and `checkRange`, which runs the
  13. checker over a range of `k` with one certificate per `k`.
  14. ## Writing for the kernel
  15. The kernel evaluates `+ * / % ^ gcd ==` on numerals with GMP, but everything else by unfolding
  16. definitions, and it remembers every intermediate term. Three rules follow, and they explain the
  17. shape of the code below:
  18. * recursion is structural (on a step counter), never well-founded;
  19. * a running value is passed through `force`, so that it is a numeral, not a growing expression;
  20. * `Nat.log2` and friends are avoided: they unfold step by step (see `bitsK`).
  21. -/
  22. namespace OeisA234388
  23. /- ## Modular exponentiation -/
  24. /-- `force f x = f x`, but written so that the kernel evaluates `x` to a numeral before
  25. calling `f`. This keeps accumulators from piling up as unevaluated terms. -/
  26. def force (f : ℕ → ℕ) : ℕ → ℕ
  27. | 0 => f 0
  28. | t + 1 => f (t + 1)
  29. @[simp] theorem force_eq (f : ℕ → ℕ) (x : ℕ) : force f x = f x := by cases x <;> rfl
  30. /-- One square-and-multiply step for bit `i` of the exponent `e`. The bit is read as
  31. `e / 2 ^ i % 2`: the kernel evaluates `/`, `^` and `%` on numerals directly, whereas `e >>> i`
  32. is unfolded into `i` nested halvings. -/
  33. def stepK (b m e i acc : ℕ) : ℕ :=
  34. bif e / 2 ^ i % 2 == 1 then acc * acc % m * b % m else acc * acc % m
  35. /-- Square-and-multiply from the top bit down; structurally recursive, so the kernel can run it. -/
  36. def loopK (b m e : ℕ) : ℕ → ℕ → ℕ
  37. | 0, acc => acc
  38. | i + 1, acc => force (loopK b m e i) (stepK b m e i acc)
  39. /-- Bisection for the bit length of `e` (exact when `e < 2 ^ 2 ^ j`), using only `/`, `^`, `+`
  40. on numerals. `Nat.log2` is not used because the kernel unfolds it step by step. Nothing needs to
  41. be proved about this function: `powMod` is correct for any number of loop steps. -/
  42. def bitsK (e : ℕ) : ℕ → ℕ → ℕ
  43. | 0, F => F
  44. | j + 1, F => force (bitsK e j) (bif e / 2 ^ (F + 2 ^ j) == 0 then F else F + 2 ^ j)
  45. /-- Binary modular exponentiation that reduces inside the Lean kernel. -/
  46. def powMod (b e m : ℕ) : ℕ :=
  47. force (fun F => loopK b m e F (b ^ (e / 2 ^ F) % m)) (bitsK e 24 0 + 1)
  48. theorem stepK_eq (b m e i : ℕ) :
  49. stepK b m e i (b ^ (e / 2 ^ (i + 1)) % m) = b ^ (e / 2 ^ i) % m := by
  50. have hE : e / 2 ^ (i + 1) = (e / 2 ^ i) / 2 := by rw [pow_succ, Nat.div_div_eq_div_mul]
  51. set E := e / 2 ^ (i + 1)
  52. have hsq : b ^ E % m * (b ^ E % m) ≡ b ^ E * b ^ E [MOD m] :=
  53. (Nat.mod_modEq _ _).mul (Nat.mod_modEq _ _)
  54. unfold stepK
  55. rcases Nat.mod_two_eq_zero_or_one (e / 2 ^ i) with h | h
  56. · have key : b ^ (e / 2 ^ i) = b ^ E * b ^ E := by
  57. conv_lhs => rw [show e / 2 ^ i = E + E by omega]
  58. ring
  59. simp only [h, show ((0 : ℕ) == 1) = false from rfl, cond_false]
  60. rw [key]; exact hsq
  61. · have key : b ^ (e / 2 ^ i) = b ^ E * b ^ E * b := by
  62. conv_lhs => rw [show e / 2 ^ i = E + E + 1 by omega]
  63. ring
  64. simp only [h, show ((1 : ℕ) == 1) = true from rfl, cond_true]
  65. rw [key]; exact ((Nat.mod_modEq _ _).trans hsq).mul_right b
  66. theorem loopK_eq (b m e : ℕ) : ∀ i, loopK b m e i (b ^ (e / 2 ^ i) % m) = b ^ e % m := by
  67. intro i
  68. induction i with
  69. | zero => simp [loopK]
  70. | succ i ih => rw [loopK, force_eq, stepK_eq, ih]
  71. theorem powMod_eq (b e m : ℕ) : powMod b e m = b ^ e % m := by
  72. rw [powMod, force_eq]; exact loopK_eq b m e _
  73. /- ## Euler's totient, slowly (only used to verify the table) -/
  74. /-- Count of `a < i` coprime to `m`, added to `acc`. -/
  75. def phiLoop (m : ℕ) : ℕ → ℕ → ℕ
  76. | 0, acc => acc
  77. | i + 1, acc => force (phiLoop m i) (bif Nat.gcd m i == 1 then acc + 1 else acc)
  78. /-- Euler's totient, in a form the kernel can evaluate. -/
  79. def phiK (m : ℕ) : ℕ := phiLoop m m 0
  80. theorem phiLoop_eq (m : ℕ) : ∀ i acc,
  81. phiLoop m i acc = acc + ((Finset.range i).filter (Nat.Coprime m)).card := by
  82. intro i
  83. induction i with
  84. | zero => intro acc; simp [phiLoop]
  85. | succ i ih =>
  86. intro acc
  87. rw [phiLoop, force_eq, ih, Finset.range_add_one, Finset.filter_insert]
  88. by_cases h : Nat.gcd m i = 1
  89. · have hc : Nat.Coprime m i := h
  90. rw [if_pos hc, Finset.card_insert_of_notMem (by simp)]
  91. rw [show (Nat.gcd m i == 1) = true from beq_iff_eq.2 h, cond_true]
  92. omega
  93. · have hc : ¬ Nat.Coprime m i := h
  94. rw [if_neg hc, show (Nat.gcd m i == 1) = false from beq_eq_false_iff_ne.2 h, cond_false]
  95. theorem phiK_eq (m : ℕ) : phiK m = m.totient := by
  96. rw [phiK, phiLoop_eq, Nat.totient_eq_card_coprime]; simp
  97. /- ## Compositeness certificates -/
  98. /-- Fermat's little theorem for base `2`, in the form used by the checker. -/
  99. theorem two_pow_pred_mod_of_prime {p : ℕ} (hp : p.Prime) (h2 : 2 < p) : 2 ^ (p - 1) % p = 1 := by
  100. have hcop : Nat.Coprime 2 p := (Nat.coprime_primes Nat.prime_two hp).2 (by omega)
  101. have h := Nat.ModEq.pow_totient hcop
  102. rw [Nat.totient_prime hp] at h
  103. have : 2 ^ (p - 1) % p = 1 % p := h
  104. rw [this, Nat.mod_eq_of_lt (by omega)]
  105. /-- Certificate check for "`N` is not prime". `q ≠ 0`: `q` is a nontrivial divisor.
  106. `q = 0`: `N > 2` fails the base-2 Fermat test. -/
  107. def certOK (N q : ℕ) : Bool :=
  108. if q = 0 then decide (2 < N) && (powMod 2 (N - 1) N != 1)
  109. else decide (1 < q) && decide (q < N) && (N % q == 0)
  110. theorem not_prime_of_certOK {N q : ℕ} (h : certOK N q = true) : ¬ N.Prime := by
  111. intro hp
  112. unfold certOK at h
  113. split_ifs at h with hq
  114. · simp only [Bool.and_eq_true, decide_eq_true_eq, bne_iff_ne, ne_eq] at h
  115. exact h.2 (by rw [powMod_eq]; exact two_pow_pred_mod_of_prime hp h.1)
  116. · simp only [Bool.and_eq_true, decide_eq_true_eq, beq_iff_eq] at h
  117. obtain ⟨⟨h1, h2⟩, h3⟩ := h
  118. have hdvd : q ∣ N := Nat.dvd_of_mod_eq_zero h3
  119. rcases (Nat.dvd_prime hp).1 hdvd with h | h <;> omega
  120. /-- Table lookup for Euler's totient, `m < 9144` (`Generated/PhiSpec.lean` proves
  121. `phiT m = φ(m)` there). Evaluating `phiK` for every `k` would cost the kernel about `n` steps
  122. each; a lookup costs ~200. -/
  123. def phiT (m : ℕ) : ℕ := (phiBlocks.getD (m / 128) []).getD (m % 128) 0
  124. /- ## The two expressions of the conjecture, and the range checker -/
  125. /-- `2^k (2^φ(n-k) - 1) + 1` -/
  126. def vPlus (n k : ℕ) : ℕ := 2 ^ k * (2 ^ phiT (n - k) - 1) + 1
  127. /-- `2^k (2^φ(n-k) - 1) - 1` -/
  128. def vMinus (n k : ℕ) : ℕ := 2 ^ k * (2 ^ phiT (n - k) - 1) - 1
  129. theorem vPlus_eq {n k : ℕ} (hφ : phiT (n - k) = (n - k).totient) :
  130. vPlus n k = 2 ^ k * (2 ^ (n - k).totient - 1) + 1 := by rw [vPlus, hφ]
  131. theorem vMinus_eq {n k : ℕ} (hφ : phiT (n - k) = (n - k).totient) :
  132. vMinus n k = 2 ^ k * (2 ^ (n - k).totient - 1) - 1 := by rw [vMinus, hφ]
  133. /-- Check `k = lo + i` for `i < m`, with `cert[i]` the certificate for that `k`. -/
  134. def checkRange (g : ℕ → ℕ → Bool) (lo m : ℕ) (cert : List ℕ) : Bool :=
  135. (List.range m).all fun i => g (lo + i) (cert.getD i 0)
  136. theorem checkRange_spec {g : ℕ → ℕ → Bool} {lo m : ℕ} {cert : List ℕ}
  137. (h : checkRange g lo m cert = true) {k : ℕ} (h1 : lo ≤ k) (h2 : k < lo + m) :
  138. ∃ q, g k q = true := by
  139. unfold checkRange at h
  140. rw [List.all_eq_true] at h
  141. have := h (k - lo) (List.mem_range.2 (by omega))
  142. rw [show lo + (k - lo) = k by omega] at this
  143. exact ⟨_, this⟩
  144. end OeisA234388