Lean / 0xOsprey/shub-smale-tau9 / SubquadraticRefutation.lean
Lean reader

Research/PvsNP/Tau/SubquadraticRefutation.lean

2,101 lines49 declarationsno sorry
  1. module
  2. public import Research.PvsNP.Tau.ChirpZ
  3. public import Mathlib.Analysis.SpecialFunctions.Pow.Real
  4. @[expose] public section
  5. namespace Research.PvsNP.Tau
  6. /-!
  7. # Division-Free Schönhage–Nussbaumer FFT & Unconditional Refutation of Every Sub-Quadratic Exponent (`c < 2`)
  8. This module formalizes:
  9. 1. Negacyclic convolution `negConv N a b` over `R = ℤ[X]` (representing multiplication in
  10. `R[Y] / (Y^N + 1)`), together with exact shift-and-negate multiplication by `Y^s` (`negShift`).
  11. 2. Division-free cyclic DFT of power-of-2 size `K = 2^p` over `R[Z] / (Z^(2M) + 1)` using
  12. the exact power-of-`Z` root of unity `ω = Z^(2 * step)` (`K * step = 2 * M`), proving
  13. both Cooley–Tukey `CanBuild` gate complexity `O(p * 2^p * M)` and exact orthogonality
  14. `IDFT (DFT A) = K • A` over `ℤ[X]` without divisions.
  15. 3. Recursive Schönhage–Nussbaumer negacyclic multiplication of size `2^k` (with base case
  16. `k ≤ 3`), achieving exact uniform positive power-of-2 scaling `2^(snScale k)` in
  17. `snCost k ≤ C_sn * k * (Nat.log 2 k + 1) * 2^k` constant-free gates.
  18. 4. Combined with `ChirpZ.zMaxGe_of_chirp_engine` at `N_fft = 2 * m = 2^(k + 1)`:
  19. - `tau_subquadratic_growth`: `∃ C > 0, ∀ k ≥ 2, zMaxGe (C * k * (Nat.log 2 k + 1) * 2^k) (2 * 4^k + 1)`.
  20. - `tau_refutes_subquadratic`: for **every real exponent `c < 2`**,
  21. `¬ ∃ a : ℝ, ∀ (τ : ℕ) (P : SLP τ), P.evalPoly ≠ 0 → (intRootCount P.evalPoly : ℝ) ≤ a * ((τ : ℝ) + 1) ^ c`.
  22. -/
  23. /-! ## 1. Negacyclic Convolution and Shifts over `ℤ[X]` -/
  24. /-- Exact negacyclic convolution of length `N` over `ℤ[X]`, representing the coefficient of `Y^t`
  25. in `(∑_{i < N} a i * Y^i) * (∑_{j < N} b j * Y^j) mod (Y^N + 1)`. -/
  26. noncomputable def negConv (N : ℕ) (a b : ℕ → Polynomial ℤ) (t : ℕ) : Polynomial ℤ :=
  27. ∑ i ∈ Finset.range N, if i ≤ t then a i * b (t - i) else -(a i * b (t + N - i))
  28. /-- Multiplication by `Y^s` in `(ℤ[X])[Y] / (Y^N + 1)`, evaluated at coefficient `t < N`. -/
  29. noncomputable def negShift (N s : ℕ) (a : ℕ → Polynomial ℤ) (t : ℕ) : Polynomial ℤ :=
  30. (-1 : Polynomial ℤ) ^ (s / N) *
  31. (if s % N ≤ t then a (t - s % N) else -(a (t + N - s % N)))
  32. theorem negConv_chirp_eq (m i : ℕ) (hm : 1 ≤ m) (_hi : i < m) :
  33. negConv (2 * m) (chirpA m) (chirpB m) (i + m) =
  34. ∑ r ∈ Finset.range (m + 1), chirpA m r * chirpB m (i + m - r) := by
  35. unfold negConv
  36. have hsub : Finset.range (m + 1) ⊆ Finset.range (2 * m) := Finset.range_mono (by omega)
  37. rw [← Finset.sum_subset hsub]
  38. · refine Finset.sum_congr rfl ?_
  39. intro r hr
  40. rw [Finset.mem_range] at hr
  41. have hle : r ≤ i + m := by omega
  42. simp [hle]
  43. · intro r hr hnot
  44. rw [Finset.mem_range] at hr hnot
  45. have hgt : ¬ r ≤ m := by omega
  46. simp [chirpA, hgt]
  47. theorem negShift_zero (N : ℕ) (_hN : 0 < N) (a : ℕ → Polynomial ℤ) (t : ℕ) :
  48. negShift N 0 a t = a t := by
  49. simp [negShift, Nat.zero_div, Nat.zero_mod]
  50. theorem negShift_add_N (N s : ℕ) (hN : 0 < N) (a : ℕ → Polynomial ℤ) (t : ℕ) :
  51. negShift N (s + N) a t = -(negShift N s a t) := by
  52. unfold negShift
  53. rw [Nat.add_div_right s hN, Nat.add_mod_right, pow_succ]
  54. ring
  55. theorem negShift_add_mul_N (N s q : ℕ) (hN : 0 < N) (a : ℕ → Polynomial ℤ) (t : ℕ) :
  56. negShift N (s + q * N) a t = (-1 : Polynomial ℤ) ^ q * negShift N s a t := by
  57. induction q with
  58. | zero => simp
  59. | succ q ih =>
  60. rw [show s + (q + 1) * N = (s + q * N) + N by ring, negShift_add_N N _ hN, ih, pow_succ]
  61. ring
  62. theorem negShift_mod_two_N (N s₁ s₂ : ℕ) (hN : 0 < N)
  63. (hmod : s₁ % (2 * N) = s₂ % (2 * N)) (a : ℕ → Polynomial ℤ) (t : ℕ) :
  64. negShift N s₁ a t = negShift N s₂ a t := by
  65. have hred : ∀ s, negShift N s a t = negShift N (s % (2 * N)) a t := by
  66. intro s
  67. have hdiv := Nat.div_add_mod s (2 * N)
  68. have h1 : s = s % (2 * N) + (2 * (s / (2 * N))) * N := by linarith
  69. conv_lhs => rw [h1]
  70. rw [negShift_add_mul_N N _ _ hN, pow_mul]
  71. norm_num
  72. rw [hred s₁, hred s₂, hmod]
  73. theorem negShift_add (N s₁ s₂ : ℕ) (hN : 0 < N) (a : ℕ → Polynomial ℤ) (t : ℕ) (ht : t < N) :
  74. negShift N (s₁ + s₂) a t = negShift N s₁ (negShift N s₂ a) t := by
  75. have hdecomp : ∀ s f x,
  76. negShift N s f x = (-1 : Polynomial ℤ) ^ (s / N) * negShift N (s % N) f x := by
  77. intro s f x
  78. have hdiv := Nat.div_add_mod s N
  79. have h1 : s = s % N + (s / N) * N := by linarith
  80. conv_lhs => rw [h1]
  81. rw [negShift_add_mul_N N _ _ hN]
  82. rw [hdecomp s₁, hdecomp (s₁ + s₂)]
  83. have hlinear_smul : ∀ s (c : Polynomial ℤ) f x,
  84. negShift N s (fun y => c * f y) x = c * negShift N s f x := by
  85. intro s c f x; unfold negShift; split_ifs <;> ring
  86. have hrhs : negShift N (s₁ % N) (negShift N s₂ a) t =
  87. (-1 : Polynomial ℤ) ^ (s₂ / N) * negShift N (s₁ % N) (negShift N (s₂ % N) a) t := by
  88. have hfn : negShift N s₂ a = fun x => (-1 : Polynomial ℤ) ^ (s₂ / N) * negShift N (s₂ % N) a x := by
  89. funext x; exact hdecomp s₂ a x
  90. rw [hfn, hlinear_smul]
  91. rw [hrhs]
  92. set r₁ := s₁ % N
  93. set r₂ := s₂ % N
  94. have hr₁ : r₁ < N := Nat.mod_lt s₁ hN
  95. have hr₂ : r₂ < N := Nat.mod_lt s₂ hN
  96. have hq : (s₁ + s₂) / N = s₁ / N + s₂ / N + (r₁ + r₂) / N := by
  97. have h1 := Nat.div_add_mod s₁ N
  98. have h2 := Nat.div_add_mod s₂ N
  99. have hr1_eq : r₁ = s₁ % N := rfl
  100. have hr2_eq : r₂ = s₂ % N := rfl
  101. have h3 : s₁ + s₂ = (r₁ + r₂) + (s₁ / N + s₂ / N) * N := by linarith
  102. rw [h3, Nat.add_mul_div_right _ _ hN]
  103. omega
  104. have hr : (s₁ + s₂) % N = (r₁ + r₂) % N := Nat.add_mod s₁ s₂ N
  105. rw [hq, hr, pow_add, pow_add]
  106. have hcore :
  107. (-1 : Polynomial ℤ) ^ ((r₁ + r₂) / N) * negShift N ((r₁ + r₂) % N) a t =
  108. negShift N r₁ (negShift N r₂ a) t := by
  109. unfold negShift
  110. have hdiv1 : r₁ / N = 0 := Nat.div_eq_of_lt hr₁
  111. have hmod1 : r₁ % N = r₁ := Nat.mod_eq_of_lt hr₁
  112. have hdiv2 : r₂ / N = 0 := Nat.div_eq_of_lt hr₂
  113. have hmod2 : r₂ % N = r₂ := Nat.mod_eq_of_lt hr₂
  114. have hmod_lt : (r₁ + r₂) % N < N := Nat.mod_lt _ hN
  115. have hdiv_m : ((r₁ + r₂) % N) / N = 0 := Nat.div_eq_of_lt hmod_lt
  116. have hmod_m : ((r₁ + r₂) % N) % N = (r₁ + r₂) % N := Nat.mod_eq_of_lt hmod_lt
  117. simp only [hdiv1, hmod1, hdiv2, hmod2, hdiv_m, hmod_m, pow_zero, one_mul]
  118. by_cases hsum : r₁ + r₂ < N
  119. · have hd0 : (r₁ + r₂) / N = 0 := Nat.div_eq_of_lt hsum
  120. have hm0 : (r₁ + r₂) % N = r₁ + r₂ := Nat.mod_eq_of_lt hsum
  121. rw [hd0, hm0, pow_zero, one_mul]
  122. by_cases ht1 : r₁ ≤ t
  123. · by_cases ht2 : r₂ ≤ t - r₁
  124. · have ht12 : r₁ + r₂ ≤ t := by omega
  125. have hidx : t - (r₁ + r₂) = t - r₁ - r₂ := by omega
  126. simp [ht1, ht2, ht12, hidx]
  127. · have ht12 : ¬ (r₁ + r₂ ≤ t) := by omega
  128. have hidx : t + N - (r₁ + r₂) = t - r₁ + N - r₂ := by omega
  129. simp [ht1, ht2, ht12, hidx]
  130. · have ht12 : ¬ (r₁ + r₂ ≤ t) := by omega
  131. have ht2 : r₂ ≤ t + N - r₁ := by omega
  132. have hidx : t + N - (r₁ + r₂) = t + N - r₁ - r₂ := by omega
  133. simp [ht1, ht2, ht12, hidx]
  134. · have hlt2N : r₁ + r₂ - N < N := by omega
  135. have hsum_eq : r₁ + r₂ = (r₁ + r₂ - N) + N := by omega
  136. have hd1 : (r₁ + r₂) / N = 1 := by
  137. rw [hsum_eq, Nat.add_div_right _ hN, Nat.div_eq_of_lt hlt2N]
  138. have hm1 : (r₁ + r₂) % N = r₁ + r₂ - N := by
  139. conv_lhs => rw [hsum_eq]
  140. rw [Nat.add_mod_right, Nat.mod_eq_of_lt hlt2N]
  141. rw [hd1, hm1, pow_one]
  142. by_cases ht1 : r₁ ≤ t
  143. · have ht2 : ¬ (r₂ ≤ t - r₁) := by omega
  144. have ht12 : r₁ + r₂ - N ≤ t := by omega
  145. have hidx : t - (r₁ + r₂ - N) = t - r₁ + N - r₂ := by omega
  146. simp [ht1, ht2, ht12, hidx]
  147. · by_cases ht2 : r₂ ≤ t + N - r₁
  148. · have ht12 : r₁ + r₂ - N ≤ t := by omega
  149. have hidx : t - (r₁ + r₂ - N) = t + N - r₁ - r₂ := by omega
  150. simp [ht1, ht2, ht12, hidx]
  151. · have ht12 : ¬ (r₁ + r₂ - N ≤ t) := by omega
  152. have hidx : t + N - (r₁ + r₂ - N) = t + N - r₁ + N - r₂ := by omega
  153. simp [ht1, ht2, ht12, hidx]
  154. calc
  155. (-1 : Polynomial ℤ) ^ (s₁ / N) * (-1 : Polynomial ℤ) ^ (s₂ / N) *
  156. (-1 : Polynomial ℤ) ^ ((r₁ + r₂) / N) * negShift N ((r₁ + r₂) % N) a t
  157. = (-1 : Polynomial ℤ) ^ (s₁ / N) * (-1 : Polynomial ℤ) ^ (s₂ / N) *
  158. ((-1 : Polynomial ℤ) ^ ((r₁ + r₂) / N) * negShift N ((r₁ + r₂) % N) a t) := by ring
  159. _ = (-1 : Polynomial ℤ) ^ (s₁ / N) * ((-1 : Polynomial ℤ) ^ (s₂ / N) *
  160. negShift N r₁ (negShift N r₂ a) t) := by rw [hcore]; ring
  161. theorem negShift_add_linear (N s : ℕ) (a b : ℕ → Polynomial ℤ) (t : ℕ) :
  162. negShift N s (fun x => a x + b x) t = negShift N s a t + negShift N s b t := by
  163. unfold negShift; split_ifs <;> ring
  164. theorem negShift_neg_linear (N s : ℕ) (a : ℕ → Polynomial ℤ) (t : ℕ) :
  165. negShift N s (fun x => -(a x)) t = -(negShift N s a t) := by
  166. unfold negShift; split_ifs <;> ring
  167. theorem negShift_smul_linear (N s : ℕ) (c : Polynomial ℤ) (a : ℕ → Polynomial ℤ) (t : ℕ) :
  168. negShift N s (fun x => c * a x) t = c * negShift N s a t := by
  169. unfold negShift; split_ifs <;> ring
  170. theorem negShift_finset_sum {ι : Type*} (S : Finset ι) (N s : ℕ)
  171. (f : ι → ℕ → Polynomial ℤ) (t : ℕ) :
  172. negShift N s (fun x => ∑ i ∈ S, f i x) t = ∑ i ∈ S, negShift N s (f i) t := by
  173. classical
  174. induction S using Finset.induction_on with
  175. | empty => simp [negShift]
  176. | insert i S hi ih =>
  177. simp only [Finset.sum_insert hi]
  178. rw [negShift_add_linear, ih]
  179. theorem negConv_add_left (N : ℕ) (a₁ a₂ b : ℕ → Polynomial ℤ) (t : ℕ) :
  180. negConv N (fun i => a₁ i + a₂ i) b t = negConv N a₁ b t + negConv N a₂ b t := by
  181. unfold negConv
  182. rw [← Finset.sum_add_distrib]
  183. refine Finset.sum_congr rfl ?_
  184. intro i _; split_ifs <;> ring
  185. theorem negConv_add_right (N : ℕ) (a b₁ b₂ : ℕ → Polynomial ℤ) (t : ℕ) :
  186. negConv N a (fun j => b₁ j + b₂ j) t = negConv N a b₁ t + negConv N a b₂ t := by
  187. unfold negConv
  188. rw [← Finset.sum_add_distrib]
  189. refine Finset.sum_congr rfl ?_
  190. intro i _; split_ifs <;> ring
  191. theorem negConv_sum_left {ι : Type*} (S : Finset ι) (N : ℕ)
  192. (f : ι → ℕ → Polynomial ℤ) (b : ℕ → Polynomial ℤ) (t : ℕ) :
  193. negConv N (fun i => ∑ u ∈ S, f u i) b t = ∑ u ∈ S, negConv N (f u) b t := by
  194. classical
  195. induction S using Finset.induction_on with
  196. | empty => simp [negConv]
  197. | insert u S hu ih =>
  198. simp only [Finset.sum_insert hu]
  199. rw [negConv_add_left, ih]
  200. theorem negConv_sum_right {ι : Type*} (S : Finset ι) (N : ℕ)
  201. (a : ℕ → Polynomial ℤ) (g : ι → ℕ → Polynomial ℤ) (t : ℕ) :
  202. negConv N a (fun j => ∑ v ∈ S, g v j) t = ∑ v ∈ S, negConv N a (g v) t := by
  203. classical
  204. induction S using Finset.induction_on with
  205. | empty => simp [negConv]
  206. | insert v S hv ih =>
  207. simp only [Finset.sum_insert hv]
  208. rw [negConv_add_right, ih]
  209. theorem negConv_neg_left (N : ℕ) (a b : ℕ → Polynomial ℤ) (t : ℕ) :
  210. negConv N (fun i => -(a i)) b t = -(negConv N a b t) := by
  211. unfold negConv
  212. rw [← Finset.sum_neg_distrib]
  213. refine Finset.sum_congr rfl ?_
  214. intro i _; split_ifs <;> ring
  215. theorem negConv_neg_right (N : ℕ) (a b : ℕ → Polynomial ℤ) (t : ℕ) :
  216. negConv N a (fun j => -(b j)) t = -(negConv N a b t) := by
  217. unfold negConv
  218. rw [← Finset.sum_neg_distrib]
  219. refine Finset.sum_congr rfl ?_
  220. intro i _; split_ifs <;> ring
  221. theorem negConv_congr_left (N : ℕ) (a₁ a₂ b : ℕ → Polynomial ℤ)
  222. (h : ∀ i < N, a₁ i = a₂ i) (t : ℕ) :
  223. negConv N a₁ b t = negConv N a₂ b t := by
  224. unfold negConv
  225. refine Finset.sum_congr rfl ?_
  226. intro i hi
  227. rw [h i (Finset.mem_range.mp hi)]
  228. theorem negShift_congr (N s : ℕ) (hN : 0 < N) (a₁ a₂ : ℕ → Polynomial ℤ)
  229. (h : ∀ i < N, a₁ i = a₂ i) (t : ℕ) (ht : t < N) :
  230. negShift N s a₁ t = negShift N s a₂ t := by
  231. unfold negShift
  232. have hmod : s % N < N := Nat.mod_lt s hN
  233. split_ifs with hst
  234. · rw [h (t - s % N) (by omega)]
  235. · rw [h (t + N - s % N) (by omega)]
  236. theorem negConv_comm (N : ℕ) (a b : ℕ → Polynomial ℤ) (t : ℕ) (ht : t < N) :
  237. negConv N a b t = negConv N b a t := by
  238. unfold negConv
  239. refine Finset.sum_bij (fun i _ => if i ≤ t then t - i else t + N - i) ?_ ?_ ?_ ?_
  240. · intro i hi; rw [Finset.mem_range] at hi ⊢; split_ifs <;> omega
  241. · intro i₁ hi₁ i₂ hi₂ heq; rw [Finset.mem_range] at hi₁ hi₂; split_ifs at heq <;> omega
  242. · intro j hj; rw [Finset.mem_range] at hj
  243. by_cases hjt : j ≤ t
  244. · refine ⟨t - j, Finset.mem_range.mpr (by omega), ?_⟩
  245. have : t - j ≤ t := by omega
  246. simp [this]; omega
  247. · refine ⟨t + N - j, Finset.mem_range.mpr (by omega), ?_⟩
  248. have : ¬ (t + N - j ≤ t) := by omega
  249. simp [this]; omega
  250. · intro i hi; rw [Finset.mem_range] at hi
  251. by_cases hit : i ≤ t
  252. · have hj : t - i ≤ t := by omega
  253. have hb : t - (t - i) = i := by omega
  254. simp [hit, hj, hb, mul_comm]
  255. · have hj : ¬ (t + N - i ≤ t) := by omega
  256. have hb : t + N - (t + N - i) = i := by omega
  257. simp [hit, hj, hb, mul_comm]
  258. /-- Shifting `a` by `Y^s` commutes with `negConv N`. -/
  259. theorem negConv_negShift_left (N s : ℕ) (hN : 0 < N) (a b : ℕ → Polynomial ℤ)
  260. (t : ℕ) (ht : t < N) :
  261. negConv N (negShift N s a) b t = negShift N s (negConv N a b) t := by
  262. have hstep1 : ∀ (f : ℕ → Polynomial ℤ) (u : ℕ), u < N →
  263. negConv N (negShift N 1 f) b u = negShift N 1 (negConv N f b) u := by
  264. intro f u hu
  265. by_cases hN1 : N = 1
  266. · subst hN1
  267. have hu0 : u = 0 := by omega
  268. subst hu0
  269. simp [negConv, negShift]
  270. · have h1lt : 1 < N := by omega
  271. have hdiv1 : 1 / N = 0 := Nat.div_eq_of_lt h1lt
  272. have hmod1 : 1 % N = 1 := Nat.mod_eq_of_lt h1lt
  273. unfold negConv negShift
  274. simp only [hdiv1, hmod1, pow_zero, one_mul]
  275. have hLHS :
  276. ∑ i ∈ Finset.range N,
  277. (if i ≤ u then (if 1 ≤ i then f (i - 1) else -f (i + N - 1)) * b (u - i)
  278. else -((if 1 ≤ i then f (i - 1) else -f (i + N - 1)) * b (u + N - i))) =
  279. ∑ j ∈ Finset.range N,
  280. if 1 ≤ u then
  281. if j ≤ u - 1 then f j * b (u - 1 - j) else -(f j * b (u - 1 + N - j))
  282. else
  283. -(if j ≤ u + N - 1 then f j * b (u + N - 1 - j) else -(f j * b (u + N - 1 + N - j))) := by
  284. refine Finset.sum_bij (fun i _ => if 1 ≤ i then i - 1 else N - 1) ?_ ?_ ?_ ?_
  285. · intro i hi; rw [Finset.mem_range] at hi ⊢; split_ifs <;> omega
  286. · intro i₁ hi₁ i₂ hi₂ heq; rw [Finset.mem_range] at hi₁ hi₂; split_ifs at heq <;> omega
  287. · intro j hj; rw [Finset.mem_range] at hj
  288. by_cases hjN : j = N - 1
  289. · exact ⟨0, Finset.mem_range.mpr (by omega), by simp [hjN]⟩
  290. · exact ⟨j + 1, Finset.mem_range.mpr (by omega), by simp⟩
  291. · intro i hi
  292. rw [Finset.mem_range] at hi
  293. by_cases hi1 : 1 ≤ i
  294. · by_cases hu1 : 1 ≤ u
  295. · by_cases hiu : i ≤ u
  296. · have hj : i - 1 ≤ u - 1 := by omega
  297. have hb : u - i = u - 1 - (i - 1) := by omega
  298. simp [hi1, hu1, hiu, hj, hb]
  299. · have hj : ¬ (i - 1 ≤ u - 1) := by omega
  300. have hb : u + N - i = u - 1 + N - (i - 1) := by omega
  301. simp [hi1, hu1, hiu, hj, hb]
  302. · have hu0 : u = 0 := by omega
  303. have hiu : ¬ (i ≤ u) := by omega
  304. have hj : i - 1 ≤ u + N - 1 := by omega
  305. have hb : u + N - i = u + N - 1 - (i - 1) := by omega
  306. simp [hi1, hu1, hiu, hj, hb]
  307. · have hi0 : i = 0 := by omega
  308. subst hi0
  309. by_cases hu1 : 1 ≤ u
  310. · have hiu : 0 ≤ u := by omega
  311. have hj : ¬ (N - 1 ≤ u - 1) := by omega
  312. have hb : u - 0 = u - 1 + N - (N - 1) := by omega
  313. simp only [Nat.not_le.mpr (by omega : 0 < 1), ↓reduceIte, hiu, hu1, hj, Nat.zero_add,
  314. hb]
  315. ring
  316. · have hu0 : u = 0 := by omega
  317. subst hu0
  318. have hj : N - 1 ≤ 0 + N - 1 := by omega
  319. have hb : 0 + N - 1 - (N - 1) = 0 := by omega
  320. simp
  321. rw [hLHS]
  322. by_cases hu1 : 1 ≤ u
  323. · simp [hu1]
  324. · simp [hu1, Finset.sum_neg_distrib]
  325. induction s generalizing t with
  326. | zero =>
  327. rw [negConv_congr_left N _ a b (fun i _ => negShift_zero N hN a i), negShift_zero N hN]
  328. | succ s ih =>
  329. rw [negConv_congr_left N _ (negShift N 1 (negShift N s a)) b
  330. (fun i hi => by rw [Nat.add_comm, negShift_add N 1 s hN a i hi])]
  331. rw [hstep1 (negShift N s a) t ht]
  332. rw [negShift_congr N 1 hN _ (negShift N s (negConv N a b)) (fun i hi => ih i hi) t ht]
  333. rw [← negShift_add N 1 s hN _ t ht, Nat.add_comm]
  334. /-- Shifting `b` by `Y^s` commutes with `negConv N`. -/
  335. theorem negConv_negShift_right (N s : ℕ) (hN : 0 < N) (a b : ℕ → Polynomial ℤ)
  336. (t : ℕ) (ht : t < N) :
  337. negConv N a (negShift N s b) t = negShift N s (negConv N a b) t := by
  338. rw [negConv_comm N a _ t ht, negConv_negShift_left N s hN b a t ht]
  339. exact negShift_congr N s hN _ _ (fun i hi => negConv_comm N b a i hi) t ht
  340. /-! ## 2. `CanBuild` for `negShift` and Schoolbook Base Case (`k ≤ 3`) -/
  341. /-- Applying `negShift N s` to a length-`N` vector costs at most `N` gates (`0 - a j`). -/
  342. theorem canBuild_negShift (N s : ℕ) (hN : 0 < N) (a : ℕ → Polynomial ℤ) :
  343. CanBuild ({(0 : Polynomial ℤ)} ∪ ⋃ i < N, {a i}) N
  344. (⋃ t < N, {negShift N s a t}) := by
  345. have h := canBuild_fin_union (n := N) (d := 1)
  346. (S := {(0 : Polynomial ℤ)} ∪ ⋃ i < N, {a i})
  347. (T := fun t => {negShift N s a t}) (by
  348. intro t ht
  349. have hmod : s % N < N := Nat.mod_lt s hN
  350. set j := if s % N ≤ t then t - s % N else t + N - s % N
  351. have hj : j < N := by dsimp only [j]; split_ifs <;> omega
  352. have hval : negShift N s a t = a j ∨ negShift N s a t = -(a j) := by
  353. unfold negShift
  354. rcases Nat.even_or_odd (s / N) with ⟨m, hm⟩ | ⟨m, hm⟩
  355. · rw [hm, show m + m = 2 * m by ring, pow_mul]
  356. norm_num
  357. split_ifs with hst
  358. · left; simp [j, hst]
  359. · right; simp [j, hst]
  360. · rw [hm, pow_add, pow_mul]
  361. norm_num
  362. split_ifs with hst
  363. · right; simp [j, hst]
  364. · left; simp [j, hst]
  365. rcases hval with hpos | hneg
  366. · refine canBuild_mono_cost (by omega : 0 ≤ 1) (canBuild_subset ?_)
  367. intro x hx; rw [Set.mem_singleton_iff] at hx; subst hx
  368. rw [hpos]; right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨j, hj, rfl⟩
  369. · have hsub := canBuild_sub (0 : Polynomial ℤ) (a j)
  370. refine canBuild_mono_out (by intro x hx; rw [Set.mem_singleton_iff] at hx; subst hx; rw [hneg]; simp)
  371. (canBuild_mono_in ?_ hsub)
  372. intro x hx
  373. rcases hx with rfl | rfl
  374. · left; rfl
  375. · right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨j, hj, rfl⟩)
  376. simpa using h
  377. /-- Schoolbook negacyclic convolution of length `N` costs `3 * N * N` gates. -/
  378. theorem canBuild_negConv_schoolbook (N : ℕ) (a b : ℕ → Polynomial ℤ) :
  379. CanBuild ({(0 : Polynomial ℤ)} ∪ (⋃ i < N, {a i}) ∪ (⋃ j < N, {b j})) (3 * N * N)
  380. (⋃ t < N, {negConv N a b t}) := by
  381. have h1 := canBuild_fin_union (n := N) (d := 3 * N)
  382. (S := {(0 : Polynomial ℤ)} ∪ (⋃ i < N, {a i}) ∪ (⋃ j < N, {b j}))
  383. (T := fun t => {negConv N a b t}) (by
  384. intro t ht
  385. let term : ℕ → Polynomial ℤ := fun i =>
  386. if i ≤ t then a i * b (t - i) else -(a i * b (t + N - i))
  387. have hterms : CanBuild ({(0 : Polynomial ℤ)} ∪ (⋃ i < N, {a i}) ∪ (⋃ j < N, {b j}))
  388. (N * 2) (⋃ i < N, {term i}) := by
  389. refine canBuild_fin_union (n := N) (d := 2) (T := fun i => {term i}) ?_
  390. intro i hi
  391. by_cases hit : i ≤ t
  392. · have hm := canBuild_mul (a i) (b (t - i))
  393. refine canBuild_mono_cost (by omega : 1 ≤ 2)
  394. (canBuild_mono_out (by intro x hx; rw [Set.mem_singleton_iff] at hx; subst hx; simp [term, hit])
  395. (canBuild_mono_in ?_ hm))
  396. intro x hx
  397. rcases hx with rfl | rfl
  398. · left; right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨i, hi, rfl⟩
  399. · right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨t - i, by omega, rfl⟩
  400. · have hm := canBuild_mul (a i) (b (t + N - i))
  401. have hn := canBuild_sub (0 : Polynomial ℤ) (a i * b (t + N - i))
  402. have hmn := canBuild_trans
  403. (canBuild_mono_in (S₂ := {(0 : Polynomial ℤ), a i, b (t + N - i)})
  404. (by intro x hx; rcases hx with rfl | rfl <;> simp) hm)
  405. (by intro x hx; rcases hx with rfl | rfl <;> simp) hn
  406. refine canBuild_mono_out
  407. (by intro x hx; rw [Set.mem_singleton_iff] at hx; subst hx; right; simp [term, hit])
  408. (canBuild_mono_in ?_ hmn)
  409. intro x hx
  410. rcases hx with rfl | rfl | rfl
  411. · left; left; rfl
  412. · left; right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨i, hi, rfl⟩
  413. · right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨t + N - i, by omega, rfl⟩
  414. have hsum := canBuild_fin_sum N term
  415. have hcomb := canBuild_trans hterms (by
  416. intro x hx; rcases hx with rfl | hx'
  417. · left; left; left; rfl
  418. · right; exact hx') hsum
  419. have hcost : N * 2 + N = 3 * N := by ring
  420. rw [hcost] at hcomb
  421. exact canBuild_mono_out (fun x hx => Or.inr hx) hcomb)
  422. have hcost : N * (3 * N) = 3 * N * N := by ring
  423. rwa [hcost] at h1
  424. /-! ## 3. Division-Free Cyclic DFT over `R[Z] / (Z^L + 1)` -/
  425. /-- Exact cyclic DFT of length `K` in `R[Z] / (Z^L + 1)` using twiddle angle `ang`
  426. (multiplication by `Z^(ang * u * j)`):
  427. `ringDFT L K ang A u t = ∑_{j < K} negShift L (ang * u * j) (A j) t`. -/
  428. noncomputable def ringDFT (L K ang : ℕ) (A : ℕ → ℕ → Polynomial ℤ) (u t : ℕ) : Polynomial ℤ :=
  429. ∑ j ∈ Finset.range K, negShift L (ang * u * j) (A j) t
  430. /-- Exact orthogonality of `ringDFT` over any commutative ring (zero divisions!):
  431. if `K = 2^p` and `K * step = L` (`L > 0`), then for `0 < d < K`:
  432. `∑_{u < K} negShift L (2 * step * u * d) x t = 0` for all `t < L`. -/
  433. theorem negShift_geom_sum_zero :
  434. ∀ (p step L : ℕ), 0 < L → 2 ^ p * step = L →
  435. ∀ (d : ℕ), 0 < d → d < 2 ^ p →
  436. ∀ (x : ℕ → Polynomial ℤ) (t : ℕ), t < L →
  437. ∑ u ∈ Finset.range (2 ^ p), negShift L (2 * step * u * d) x t = 0
  438. | 0, _, _, _, _, d, hd0, hd1, _, _, _ => by simp at hd1; omega
  439. | p + 1, step, L, hL, hKS, d, hd0, hd1, x, t, ht => by
  440. have h2p : 2 ^ (p + 1) = 2 ^ p + 2 ^ p := by rw [pow_succ]; ring
  441. rw [h2p, Finset.sum_range_add, ← Finset.sum_add_distrib]
  442. have hshift_half : ∀ u : ℕ,
  443. negShift L (2 * step * (2 ^ p + u) * d) x t =
  444. (-1 : Polynomial ℤ) ^ d * negShift L (2 * step * u * d) x t := by
  445. intro u
  446. have heq : 2 * step * (2 ^ p + u) * d = 2 * step * u * d + d * L := by
  447. have : 2 * 2 ^ p * step = L := by rw [← hKS, pow_succ]; ring
  448. calc 2 * step * (2 ^ p + u) * d = 2 * step * u * d + d * (2 * 2 ^ p * step) := by ring
  449. _ = 2 * step * u * d + d * L := by rw [this]
  450. rw [heq, negShift_add_mul_N L _ d hL]
  451. rcases Nat.even_or_odd d with ⟨d', hd'⟩ | ⟨d', hd'⟩
  452. · have hsum2 :
  453. ∑ u ∈ Finset.range (2 ^ p),
  454. (negShift L (2 * step * u * d) x t + negShift L (2 * step * (2 ^ p + u) * d) x t) =
  455. 2 * ∑ u ∈ Finset.range (2 ^ p), negShift L (2 * (2 * step) * u * d') x t := by
  456. rw [Finset.mul_sum]
  457. refine Finset.sum_congr rfl ?_
  458. intro u _
  459. rw [hshift_half u, hd', show d' + d' = 2 * d' by ring, pow_mul]
  460. have harg : 2 * step * u * (2 * d') = 2 * (2 * step) * u * d' := by ring
  461. rw [harg]
  462. norm_num
  463. ring
  464. rw [hsum2]
  465. have hKS' : 2 ^ p * (2 * step) = L := by rw [← hKS, pow_succ]; ring
  466. have hzero := negShift_geom_sum_zero p (2 * step) L hL hKS' d' (by omega) (by omega) x t ht
  467. rw [hzero, mul_zero]
  468. · refine Finset.sum_eq_zero ?_
  469. intro u _
  470. rw [hshift_half u, hd', pow_add, pow_mul]
  471. norm_num
  472. /-- **Exact Inversion of Division-Free `ringDFT`:**
  473. If `K = 2^p`, `K * step = L` (`L > 0`), and `ang = 2 * step`, `angInv = 2 * step * (K - 1)`,
  474. then `ringDFT L K angInv (ringDFT L K ang A) v t = (K : Polynomial ℤ) * A v t` for all `v < K, t < L`. -/
  475. theorem ringDFT_inversion (p step L : ℕ) (hL : 0 < L) (hKS : 2 ^ p * step = L)
  476. (A : ℕ → ℕ → Polynomial ℤ) (v t : ℕ) (hv : v < 2 ^ p) (ht : t < L) :
  477. ringDFT L (2 ^ p) (2 * step * (2 ^ p - 1)) (ringDFT L (2 ^ p) (2 * step) A) v t =
  478. ((2 ^ p : ℕ) : Polynomial ℤ) * A v t := by
  479. set K := 2 ^ p
  480. have hKpos : 1 ≤ K := Nat.one_le_two_pow
  481. unfold ringDFT
  482. -- Push `negShift` inside the inner sum and swap sums over `u` and `j`
  483. have hpush :
  484. ∑ u ∈ Finset.range K,
  485. negShift L (2 * step * (K - 1) * v * u)
  486. (fun x => ∑ j ∈ Finset.range K, negShift L (2 * step * u * j) (A j) x) t =
  487. ∑ j ∈ Finset.range K,
  488. ∑ u ∈ Finset.range K,
  489. negShift L (2 * step * (K - 1) * v * u + 2 * step * u * j) (A j) t := by
  490. have h1 :
  491. ∀ u ∈ Finset.range K,
  492. negShift L (2 * step * (K - 1) * v * u)
  493. (fun x => ∑ j ∈ Finset.range K, negShift L (2 * step * u * j) (A j) x) t =
  494. ∑ j ∈ Finset.range K,
  495. negShift L (2 * step * (K - 1) * v * u + 2 * step * u * j) (A j) t := by
  496. intro u _
  497. rw [negShift_finset_sum]
  498. refine Finset.sum_congr rfl ?_
  499. intro j _
  500. rw [negShift_add L _ _ hL _ t ht]
  501. rw [Finset.sum_congr rfl h1, Finset.sum_comm]
  502. rw [hpush]
  503. -- Simplify the inner sum for each `j < K`: equals `K * A v t` if `j = v`, and `0` if `j ≠ v`
  504. have hinner :
  505. ∀ j ∈ Finset.range K,
  506. (∑ u ∈ Finset.range K,
  507. negShift L (2 * step * (K - 1) * v * u + 2 * step * u * j) (A j) t) =
  508. if j = v then ((K : ℕ) : Polynomial ℤ) * A j t else 0 := by
  509. intro j hj
  510. rw [Finset.mem_range] at hj
  511. by_cases hjv : j = v
  512. · subst hjv
  513. simp only [↓reduceIte]
  514. have hterm : ∀ u ∈ Finset.range K,
  515. negShift L (2 * step * (K - 1) * j * u + 2 * step * u * j) (A j) t = A j t := by
  516. intro u _
  517. have hmod : (2 * step * (K - 1) * j * u + 2 * step * u * j) % (2 * L) = 0 % (2 * L) := by
  518. have heq : 2 * step * (K - 1) * j * u + 2 * step * u * j = (j * u) * (2 * L) := by
  519. have hK1 : (K - 1) + 1 = K := by omega
  520. calc 2 * step * (K - 1) * j * u + 2 * step * u * j
  521. = 2 * step * ((K - 1) + 1) * j * u := by ring
  522. _ = 2 * step * K * j * u := by rw [hK1]
  523. _ = (j * u) * (2 * (K * step)) := by ring
  524. _ = (j * u) * (2 * L) := by rw [hKS]
  525. rw [heq, Nat.mul_mod_left, Nat.zero_mod]
  526. rw [negShift_mod_two_N L _ 0 hL hmod, negShift_zero L hL]
  527. rw [Finset.sum_congr rfl hterm, Finset.sum_const, Finset.card_range, nsmul_eq_mul]
  528. · simp only [hjv, ↓reduceIte]
  529. set d := (j + K - v) % K
  530. have hd0 : 0 < d := by
  531. dsimp only [d]
  532. by_cases hlt : v < j
  533. · have : (j + K - v) % K = j - v := by
  534. rw [show j + K - v = (j - v) + K by omega, Nat.add_mod_right, Nat.mod_eq_of_lt (by omega)]
  535. omega
  536. · have : (j + K - v) % K = j + K - v := Nat.mod_eq_of_lt (by omega)
  537. omega
  538. have hdK : d < K := Nat.mod_lt _ (by omega)
  539. have hmod_term : ∀ u ∈ Finset.range K,
  540. negShift L (2 * step * (K - 1) * v * u + 2 * step * u * j) (A j) t =
  541. negShift L (2 * step * u * d) (A j) t := by
  542. intro u _
  543. refine negShift_mod_two_N L _ _ hL ?_ (A j) t
  544. -- Both exponents are congruent mod `2 * L = 2 * K * step`
  545. have hmodK : ((K - 1) * v + j) % K = d := by
  546. rcases Nat.eq_zero_or_pos v with rfl | hvpos
  547. · simp [d]
  548. · have h1 : (K - 1) * v + j = (j + K - v) + (v - 1) * K := by
  549. zify [hKpos, hvpos, show v ≤ j + K by omega]; ring
  550. rw [h1, Nat.add_mul_mod_self_right]
  551. have hmul : (2 * step * u * ((K - 1) * v + j)) % (2 * L) = (2 * step * u * d) % (2 * L) := by
  552. have hdiv := Nat.div_add_mod ((K - 1) * v + j) K
  553. have h2 : (K - 1) * v + j = d + (((K - 1) * v + j) / K) * K := by linarith
  554. rw [h2, mul_add]
  555. have h3 : 2 * step * u * ((((K - 1) * v + j) / K) * K) =
  556. (u * (((K - 1) * v + j) / K)) * (2 * L) := by
  557. rw [← hKS]; ring
  558. rw [h3, Nat.add_mul_mod_self_right]
  559. have hcomm : 2 * step * (K - 1) * v * u + 2 * step * u * j =
  560. 2 * step * u * ((K - 1) * v + j) := by ring
  561. rw [hcomm, hmul]
  562. rw [Finset.sum_congr rfl hmod_term]
  563. exact negShift_geom_sum_zero p step L hL hKS d hd0 hdK (A j) t ht
  564. rw [Finset.sum_congr rfl hinner, Finset.sum_ite_eq']
  565. simp [hv]
  566. /-- **Convolution Theorem for `ringDFT`:**
  567. If `K = 2^p` and `K * step = L` (`L > 0`), then pointwise `negConv L` in the `ringDFT` domain
  568. inverts to `K` times the cyclic convolution in `R[Z]/(Z^L + 1)`. -/
  569. theorem ringDFT_convolution (p step L : ℕ) (hL : 0 < L) (hKS : 2 ^ p * step = L)
  570. (A B : ℕ → ℕ → Polynomial ℤ) (v t : ℕ) (hv : v < 2 ^ p) (ht : t < L) :
  571. ringDFT L (2 ^ p) (2 * step * (2 ^ p - 1))
  572. (fun u => negConv L (ringDFT L (2 ^ p) (2 * step) A u)
  573. (ringDFT L (2 ^ p) (2 * step) B u)) v t =
  574. ((2 ^ p : ℕ) : Polynomial ℤ) *
  575. ∑ j₁ ∈ Finset.range (2 ^ p),
  576. negConv L (A j₁) (B ((v + 2 ^ p - j₁) % 2 ^ p)) t := by
  577. set K := 2 ^ p
  578. -- Expand pointwise product as `ringDFT` of the cyclic convolution `C`!
  579. let C : ℕ → ℕ → Polynomial ℤ := fun w x =>
  580. ∑ j₁ ∈ Finset.range K, negConv L (A j₁) (B ((w + K - j₁) % K)) x
  581. have hpointwise : ∀ u < K, ∀ x < L,
  582. negConv L (ringDFT L K (2 * step) A u) (ringDFT L K (2 * step) B u) x =
  583. ringDFT L K (2 * step) C u x := by
  584. intro u _ x hx
  585. unfold ringDFT
  586. rw [negConv_sum_left]
  587. simp_rw [negConv_sum_right]
  588. have hshift :
  589. ∑ j₁ ∈ Finset.range K, ∑ j₂ ∈ Finset.range K,
  590. negConv L (negShift L (2 * step * u * j₁) (A j₁))
  591. (negShift L (2 * step * u * j₂) (B j₂)) x =
  592. ∑ j₁ ∈ Finset.range K, ∑ j₂ ∈ Finset.range K,
  593. negShift L (2 * step * u * (j₁ + j₂)) (negConv L (A j₁) (B j₂)) x := by
  594. refine Finset.sum_congr rfl ?_
  595. intro j₁ _
  596. refine Finset.sum_congr rfl ?_
  597. intro j₂ _
  598. rw [negConv_negShift_left L _ hL _ _ x hx]
  599. rw [negShift_congr L _ hL _ _ (fun y hy => negConv_negShift_right L _ hL (A j₁) (B j₂) y hy) x hx]
  600. rw [← negShift_add L _ _ hL _ x hx]
  601. congr 1
  602. ring
  603. rw [hshift]
  604. have hrhs :
  605. ∑ w ∈ Finset.range K, negShift L (2 * step * u * w) (C w) x =
  606. ∑ j₁ ∈ Finset.range K, ∑ w ∈ Finset.range K,
  607. negShift L (2 * step * u * w) (negConv L (A j₁) (B ((w + K - j₁) % K))) x := by
  608. simp_rw [C, negShift_finset_sum]
  609. exact Finset.sum_comm
  610. rw [hrhs]
  611. refine Finset.sum_congr rfl ?_
  612. intro j₁ hj₁
  613. rw [Finset.mem_range] at hj₁
  614. refine Finset.sum_bij (fun j₂ _ => (j₁ + j₂) % K) ?_ ?_ ?_ ?_
  615. · intro j₂ _; exact Finset.mem_range.mpr (Nat.mod_lt _ (by positivity))
  616. · intro a ha b hb heq
  617. rw [Finset.mem_range] at ha hb
  618. have hsub : ∀ z < K, ((j₁ + z) % K + K - j₁) % K = z := by
  619. intro z hz
  620. by_cases hlt : j₁ + z < K
  621. · rw [Nat.mod_eq_of_lt hlt, show j₁ + z + K - j₁ = z + K by omega,
  622. Nat.add_mod_right, Nat.mod_eq_of_lt hz]
  623. · have hmod_sub : (j₁ + z) % K = j₁ + z - K := by
  624. have h1 : j₁ + z = (j₁ + z - K) + K := by omega
  625. conv_lhs => rw [h1]
  626. rw [Nat.add_mod_right, Nat.mod_eq_of_lt (by omega)]
  627. rw [hmod_sub, show j₁ + z - K + K - j₁ = z by omega, Nat.mod_eq_of_lt hz]
  628. rw [← hsub a ha, ← hsub b hb, heq]
  629. · intro w hw
  630. rw [Finset.mem_range] at hw
  631. refine ⟨(w + K - j₁) % K, Finset.mem_range.mpr (Nat.mod_lt _ (by positivity)), ?_⟩
  632. by_cases hlt : j₁ ≤ w
  633. · rw [show w + K - j₁ = (w - j₁) + K by omega, Nat.add_mod_right,
  634. Nat.mod_eq_of_lt (by omega : w - j₁ < K), show j₁ + (w - j₁) = w by omega,
  635. Nat.mod_eq_of_lt hw]
  636. · rw [Nat.mod_eq_of_lt (by omega : w + K - j₁ < K),
  637. show j₁ + (w + K - j₁) = w + K by omega, Nat.add_mod_right, Nat.mod_eq_of_lt hw]
  638. · intro j₂ hj₂
  639. rw [Finset.mem_range] at hj₂
  640. have hback : ((j₁ + j₂) % K + K - j₁) % K = j₂ := by
  641. by_cases hlt : j₁ + j₂ < K
  642. · rw [Nat.mod_eq_of_lt hlt, show j₁ + j₂ + K - j₁ = j₂ + K by omega,
  643. Nat.add_mod_right, Nat.mod_eq_of_lt hj₂]
  644. · have hmod_sub : (j₁ + j₂) % K = j₁ + j₂ - K := by
  645. have h1 : j₁ + j₂ = (j₁ + j₂ - K) + K := by omega
  646. conv_lhs => rw [h1]
  647. rw [Nat.add_mod_right, Nat.mod_eq_of_lt (by omega)]
  648. rw [hmod_sub, show j₁ + j₂ - K + K - j₁ = j₂ by omega, Nat.mod_eq_of_lt hj₂]
  649. rw [hback]
  650. refine negShift_mod_two_N L _ _ hL ?_ _ x
  651. have hdiv := Nat.div_add_mod (j₁ + j₂) K
  652. have h1 : j₁ + j₂ = (j₁ + j₂) % K + ((j₁ + j₂) / K) * K := by linarith
  653. conv_lhs => rw [h1]
  654. rw [mul_add]
  655. have h2 : 2 * step * u * (((j₁ + j₂) / K) * K) = (u * ((j₁ + j₂) / K)) * (2 * L) := by
  656. rw [← hKS]; ring
  657. rw [h2, Nat.add_mul_mod_self_right]
  658. have hcongr :
  659. ringDFT L K (2 * step * (K - 1))
  660. (fun u => negConv L (ringDFT L K (2 * step) A u) (ringDFT L K (2 * step) B u)) v t =
  661. ringDFT L K (2 * step * (K - 1)) (ringDFT L K (2 * step) C) v t := by
  662. unfold ringDFT
  663. refine Finset.sum_congr rfl ?_
  664. intro u hu
  665. exact negShift_congr L _ hL _ _ (fun x hx => hpointwise u (Finset.mem_range.mp hu) x hx) t ht
  666. rw [hcongr]
  667. exact ringDFT_inversion p step L hL hKS C v t hv ht
  668. /-- Cooley–Tukey gate cost for `ringDFT` of size `2^p` on length-`L` vectors: `3 * p * 2^p * L`. -/
  669. def dftCost (p L : ℕ) : ℕ := 3 * p * 2 ^ p * L
  670. /-- **Cooley–Tukey Butterfly `CanBuild` for `ringDFT`:**
  671. Given `{0} ∪ {A j t | j < 2^p, t < L}`, computes `{ringDFT L (2^p) ang A u t | u < 2^p, t < L}`
  672. in `dftCost p L = 3 * p * 2^p * L` gates, whenever `2^p * ang` is a multiple of `2 * L`
  673. (Wait: specifically whenever `2^(p - 1) * ang ≡ L (mod 2 * L)`, i.e. `ang = 2 * step * c` with `c` odd
  674. and `2^p * step = L`!). -/
  675. theorem canBuild_ringDFT :
  676. ∀ (p step c L : ℕ), 0 < L → 2 ^ p * step = L → Odd c →
  677. ∀ (A : ℕ → ℕ → Polynomial ℤ),
  678. CanBuild
  679. ({(0 : Polynomial ℤ)} ∪ ⋃ j < 2 ^ p, ⋃ t < L, {A j t})
  680. (dftCost p L)
  681. (⋃ u < 2 ^ p, ⋃ t < L, {ringDFT L (2 ^ p) (2 * step * c) A u t})
  682. | 0, step, c, L, hL, _, _, A => by
  683. simp only [dftCost, mul_zero, zero_mul]
  684. refine canBuild_subset ?_
  685. intro x hx
  686. simp only [Set.mem_iUnion, exists_prop, Set.mem_singleton_iff] at hx
  687. rcases hx with ⟨u, hu, t, ht, rfl⟩
  688. have hu0 : u = 0 := by omega
  689. subst hu0
  690. right
  691. simp only [Set.mem_iUnion, exists_prop]
  692. refine ⟨0, by omega, t, ht, ?_⟩
  693. simp [ringDFT, negShift_zero L hL]
  694. | p + 1, step, c, L, hL, hKS, hc, A => by
  695. set K' := 2 ^ p
  696. have hKS' : K' * (2 * step) = L := by rw [← hKS, pow_succ]; ring
  697. let Aeven : ℕ → ℕ → Polynomial ℤ := fun j => A (2 * j)
  698. let Aodd : ℕ → ℕ → Polynomial ℤ := fun j => A (2 * j + 1)
  699. let E := ringDFT L K' (2 * (2 * step) * c) Aeven
  700. let O := ringDFT L K' (2 * (2 * step) * c) Aodd
  701. let T : ℕ → ℕ → Polynomial ℤ := fun u => negShift L (2 * step * c * u) (O u)
  702. set S0 : Set (Polynomial ℤ) :=
  703. {(0 : Polynomial ℤ)} ∪ ⋃ j < 2 ^ (p + 1), ⋃ t < L, {A j t}
  704. -- Step 1: Recursive DFT on Aeven and Aodd (`2 * dftCost p L` gates)
  705. have hE := canBuild_ringDFT p (2 * step) c L hL hKS' hc Aeven
  706. have hO := canBuild_ringDFT p (2 * step) c L hL hKS' hc Aodd
  707. have c1 := canBuild_mono_in (S₂ := S0) (by
  708. intro x hx; rcases hx with rfl | hx'
  709. · left; rfl
  710. · right; simp only [Set.mem_iUnion, exists_prop] at hx' ⊢
  711. rcases hx' with ⟨j, hj, t, ht, hxt⟩
  712. exact ⟨2 * j, by rw [pow_succ]; omega, t, ht, hxt⟩) hE
  713. have c2 := canBuild_trans c1 (by
  714. intro x hx; rcases hx with rfl | hx'
  715. · left; left; rfl
  716. · left; right; simp only [Set.mem_iUnion, exists_prop] at hx' ⊢
  717. rcases hx' with ⟨j, hj, t, ht, hxt⟩
  718. exact ⟨2 * j + 1, by rw [pow_succ]; omega, t, ht, hxt⟩) hO
  719. -- Step 2: Twiddle O u by `2 * step * c * u` for `u < K'` (`K' * L` gates)
  720. have hT :
  721. CanBuild ({(0 : Polynomial ℤ)} ∪ ⋃ u < K', ⋃ t < L, {O u t})
  722. (K' * L) (⋃ u < K', ⋃ t < L, {T u t}) := by
  723. refine canBuild_fin_union (n := K') (d := L) (T := fun u => ⋃ t < L, {T u t}) ?_
  724. intro u hu
  725. refine canBuild_mono_in ?_ (canBuild_negShift L (2 * step * c * u) hL (O u))
  726. intro x hx; rcases hx with rfl | hx'
  727. · left; rfl
  728. · right; simp only [Set.mem_iUnion, exists_prop] at hx' ⊢
  729. rcases hx' with ⟨t, ht, hxt⟩; exact ⟨u, hu, t, ht, hxt⟩
  730. have c3 := canBuild_trans c2 (by
  731. intro x hx; rcases hx with rfl | hx'
  732. · left; left; rfl
  733. · right; right; exact hx') hT
  734. -- Step 3: Butterflies `E u t + T u t` and `E u t - T u t` for `u < K', t < L` (`2 * K' * L` gates)
  735. have hB :
  736. CanBuild ((⋃ u < K', ⋃ t < L, {E u t}) ∪ (⋃ u < K', ⋃ t < L, {T u t}))
  737. (K' * (L * 2))
  738. (⋃ u < K', ⋃ t < L, ({E u t + T u t, E u t - T u t} : Set (Polynomial ℤ))) := by
  739. refine canBuild_fin_union (n := K') (d := L * 2)
  740. (T := fun u => ⋃ t < L, ({E u t + T u t, E u t - T u t} : Set (Polynomial ℤ))) ?_
  741. intro u hu
  742. refine canBuild_fin_union (n := L) (d := 2)
  743. (T := fun t => ({E u t + T u t, E u t - T u t} : Set (Polynomial ℤ))) ?_
  744. intro t ht
  745. have ha := canBuild_add (E u t) (T u t)
  746. have hs := canBuild_sub (E u t) (T u t)
  747. have has := canBuild_trans ha Set.subset_union_left hs
  748. refine canBuild_mono_in ?_ has
  749. intro x hx
  750. rcases hx with rfl | rfl
  751. · left; simp only [Set.mem_iUnion, exists_prop]; exact ⟨u, hu, t, ht, rfl⟩
  752. · right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨u, hu, t, ht, rfl⟩
  753. have c4 := canBuild_trans c3 (by
  754. intro x hx; rcases hx with hxE | hxT
  755. · right; left; left; exact hxE
  756. · right; right; exact hxT) hB
  757. -- Check butterfly identity:
  758. -- `ringDFT L (2*K') (2*step*c) A u t = E u t + T u t` (for `u < K'`)
  759. -- `ringDFT L (2*K') (2*step*c) A (u + K') t = E u t - T u t` (for `u < K'`)
  760. have hsplit_dft : ∀ u t, t < L →
  761. ringDFT L (2 ^ (p + 1)) (2 * step * c) A u t =
  762. E (u % K') t + negShift L (2 * step * c * u) (O (u % K')) t := by
  763. intro u t ht
  764. unfold ringDFT
  765. have h2K : 2 ^ (p + 1) = 2 * K' := by rw [pow_succ]; ring
  766. rw [h2K]
  767. -- Split `range (2 * K')` into even `2 * j` and odd `2 * j + 1`
  768. have heven_odd : ∀ f : ℕ → Polynomial ℤ,
  769. ∑ j ∈ Finset.range (2 * K'), f j =
  770. (∑ j ∈ Finset.range K', f (2 * j)) + (∑ j ∈ Finset.range K', f (2 * j + 1)) := by
  771. intro f
  772. clear h2K
  773. induction K' with
  774. | zero => simp
  775. | succ n ihn =>
  776. rw [show 2 * (n + 1) = 2 * n + 2 by ring, Finset.sum_range_succ, Finset.sum_range_succ,
  777. ihn, Finset.sum_range_succ, Finset.sum_range_succ]
  778. ring
  779. rw [heven_odd]
  780. congr 1
  781. · dsimp only [E, ringDFT, Aeven]
  782. refine Finset.sum_congr rfl ?_
  783. intro j _
  784. refine negShift_mod_two_N L _ _ hL ?_ _ t
  785. have hdiv := Nat.div_add_mod u K'
  786. have hu_eq : u = u % K' + (u / K') * K' := by linarith
  787. conv_lhs => rw [hu_eq]
  788. have h1 : 2 * step * c * (u % K' + u / K' * K') * (2 * j) =
  789. 2 * (2 * step) * c * (u % K') * j + (c * (u / K') * j) * (2 * L) := by
  790. rw [← hKS']; ring
  791. rw [h1, Nat.add_mul_mod_self_right]
  792. · have hOfn : O (u % K') = fun x =>
  793. ∑ j ∈ Finset.range K', negShift L (2 * (2 * step) * c * (u % K') * j) (Aodd j) x := rfl
  794. rw [hOfn, negShift_finset_sum]
  795. dsimp only [Aodd]
  796. refine Finset.sum_congr rfl ?_
  797. intro j _
  798. rw [← negShift_add L _ _ hL _ t ht]
  799. refine negShift_mod_two_N L _ _ hL ?_ _ t
  800. have hdiv := Nat.div_add_mod u K'
  801. have hu_eq : u = u % K' + (u / K') * K' := by linarith
  802. have h1 : 2 * step * c * u * (2 * j + 1) =
  803. 2 * step * c * u + 2 * (2 * step) * c * u * j := by ring
  804. rw [h1]
  805. conv_lhs => rw [show 2 * (2 * step) * c * u * j =
  806. 2 * (2 * step) * c * (u % K' + u / K' * K') * j by rw [← hu_eq]]
  807. have h2 : 2 * step * c * u + 2 * (2 * step) * c * (u % K' + u / K' * K') * j =
  808. (2 * step * c * u + 2 * (2 * step) * c * (u % K') * j) +
  809. (c * (u / K') * j) * (2 * L) := by
  810. rw [← hKS']; ring
  811. rw [h2, Nat.add_mul_mod_self_right]
  812. have hcost : dftCost p L + dftCost p L + K' * L + K' * (L * 2) ≤ dftCost (p + 1) L := by
  813. simp only [dftCost, K', pow_succ]
  814. have h1 : 3 * p * 2 ^ p * L + 3 * p * 2 ^ p * L + 2 ^ p * L + 2 ^ p * (L * 2) =
  815. (6 * p + 3) * (2 ^ p * L) := by ring
  816. have h2 : 3 * (p + 1) * (2 ^ p * 2) * L = (6 * p + 6) * (2 ^ p * L) := by ring
  817. rw [h1, h2]
  818. exact Nat.mul_le_mul_right (2 ^ p * L) (by omega)
  819. refine canBuild_mono_cost hcost (canBuild_mono_out ?_ c4)
  820. intro x hx
  821. simp only [Set.mem_iUnion, exists_prop, Set.mem_singleton_iff] at hx
  822. rcases hx with ⟨u, hu, t, ht, rfl⟩
  823. rw [hsplit_dft u t ht]
  824. by_cases huK : u < K'
  825. · rw [Nat.mod_eq_of_lt huK]
  826. right; simp only [Set.mem_iUnion, exists_prop]
  827. exact ⟨u, huK, t, ht, Or.inl rfl⟩
  828. · set u' := u - K'
  829. have hu' : u' < K' := by rw [pow_succ] at hu; omega
  830. have hu_eq : u = u' + K' := by omega
  831. have hmod : u % K' = u' := by rw [hu_eq, Nat.add_mod_right, Nat.mod_eq_of_lt hu']
  832. rw [hmod]
  833. have htw : negShift L (2 * step * c * u) (O u') t = -(T u' t) := by
  834. dsimp only [T]
  835. have harg : 2 * step * c * u = 2 * step * c * u' + c * L := by
  836. rw [hu_eq, ← hKS']; ring
  837. rw [harg, negShift_add_mul_N L _ c hL]
  838. rcases hc with ⟨m, rfl⟩
  839. rw [pow_add, pow_mul]
  840. norm_num
  841. rw [htw, ← sub_eq_add_neg]
  842. right; simp only [Set.mem_iUnion, exists_prop]
  843. exact ⟨u', hu', t, ht, Or.inr rfl⟩
  844. /-! ## 4. Recursive Schönhage–Nussbaumer Negacyclic Multiplication (`k ≥ 4`, Base `k ≤ 3`) -/
  845. /-- Recursive size parameter `snSub k = (k + 1) / 2 + 1` (`2 * M` exponent).
  846. For `k ≥ 4`, `snSub k < k`. -/
  847. def snSub (k : ℕ) : ℕ := (k + 1) / 2 + 1
  848. theorem snSub_lt (k : ℕ) (hk : 4 ≤ k) : snSub k < k := by
  849. unfold snSub; omega
  850. /-- Uniform positive power-of-2 scale exponent `snScale k` produced by `canBuild_snMul`. -/
  851. def snScale : ℕ → ℕ
  852. | k =>
  853. if h : k ≤ 3 then 0
  854. else snScale (snSub k) + k / 2
  855. termination_by k => k
  856. decreasing_by exact snSub_lt k (by omega)
  857. /-- Exact gate cost `snCost k` of division-free Schönhage–Nussbaumer negacyclic multiplication
  858. of length `2^k`. -/
  859. def snCost : ℕ → ℕ
  860. | k =>
  861. if h : k ≤ 3 then 3 * (2 ^ k) * (2 ^ k)
  862. else
  863. let p := k / 2
  864. let K := 2 ^ p
  865. let L := 2 ^ (snSub k)
  866. K * snCost (snSub k) + 3 * dftCost p L + 3 * K * L + 2 * 2 ^ k
  867. termination_by k => k
  868. decreasing_by exact snSub_lt k (by omega)
  869. /-- Chunk-recombination identity for Schönhage–Nussbaumer:
  870. splitting length `N = K * M` into `K` chunks of length `M` (zero-padded to `2 * M`),
  871. computing the negacyclic convolution of length `K` in `R[Z]/(Z^(2M) + 1)`, and recombining
  872. overlapping halves recovers `negConv (K * M) a b` identically. -/
  873. theorem negConv_chunk_recombine (K M : ℕ) (hK : 1 ≤ K) (hM : 1 ≤ M)
  874. (a b : ℕ → Polynomial ℤ) (q r : ℕ) (hq : q < K) (hr : r < M) :
  875. let A : ℕ → ℕ → Polynomial ℤ := fun j u => if u < M then a (j * M + u) else 0
  876. let B : ℕ → ℕ → Polynomial ℤ := fun j u => if u < M then b (j * M + u) else 0
  877. let C : ℕ → ℕ → Polynomial ℤ := fun w u =>
  878. ∑ j₁ ∈ Finset.range K,
  879. if j₁ ≤ w then negConv (2 * M) (A j₁) (B (w - j₁)) u
  880. else -(negConv (2 * M) (A j₁) (B (w + K - j₁)) u)
  881. negConv (K * M) a b (q * M + r) =
  882. C q r + (if 0 < q then C (q - 1) (r + M) else -(C (K - 1) (r + M))) := by
  883. intro A B C
  884. -- Helper: for any `j₁, j₂ < K`, compute `negConv (2*M) (A j₁) (B j₂) u` for `u = r` and `u = r + M`
  885. have hchunk_lo : ∀ j₁ j₂,
  886. negConv (2 * M) (A j₁) (B j₂) r =
  887. ∑ r₁ ∈ Finset.range M, if r₁ ≤ r then a (j₁ * M + r₁) * b (j₂ * M + (r - r₁)) else 0 := by
  888. intro j₁ j₂
  889. unfold negConv
  890. have hsub : Finset.range M ⊆ Finset.range (2 * M) := Finset.range_mono (by omega)
  891. rw [← Finset.sum_subset hsub]
  892. · refine Finset.sum_congr rfl ?_
  893. intro r₁ hr₁
  894. rw [Finset.mem_range] at hr₁
  895. by_cases hle : r₁ ≤ r
  896. · have hlt : r - r₁ < M := by omega
  897. simp [hle, A, B, hr₁, hlt]
  898. · have hnot : ¬ (r + 2 * M - r₁ < M) := by omega
  899. simp [hle, A, B, hr₁, hnot]
  900. · intro r₁ hr₁ hnot
  901. rw [Finset.mem_range] at hr₁ hnot
  902. have hge : ¬ (r₁ < M) := by omega
  903. simp [A, hge]
  904. have hchunk_hi : ∀ j₁ j₂,
  905. negConv (2 * M) (A j₁) (B j₂) (r + M) =
  906. ∑ r₁ ∈ Finset.range M, if r < r₁ then a (j₁ * M + r₁) * b (j₂ * M + (r + M - r₁)) else 0 := by
  907. intro j₁ j₂
  908. unfold negConv
  909. have hsub : Finset.range M ⊆ Finset.range (2 * M) := Finset.range_mono (by omega)
  910. rw [← Finset.sum_subset hsub]
  911. · refine Finset.sum_congr rfl ?_
  912. intro r₁ hr₁
  913. rw [Finset.mem_range] at hr₁
  914. have hle : r₁ ≤ r + M := by omega
  915. by_cases hgt : r < r₁
  916. · have hlt : r + M - r₁ < M := by omega
  917. simp [hle, hgt, A, B, hr₁, hlt]
  918. · have hnot : ¬ (r + M - r₁ < M) := by omega
  919. simp [hle, hgt, A, B, hr₁, hnot]
  920. · intro r₁ hr₁ hnot
  921. rw [Finset.mem_range] at hr₁ hnot
  922. have hge : ¬ (r₁ < M) := by omega
  923. simp [A, hge]
  924. -- Rewrite LHS as double sum over `j₁ < K, r₁ < M`
  925. have hLHS :
  926. negConv (K * M) a b (q * M + r) =
  927. ∑ j₁ ∈ Finset.range K, ∑ r₁ ∈ Finset.range M,
  928. if j₁ * M + r₁ ≤ q * M + r then
  929. a (j₁ * M + r₁) * b (q * M + r - (j₁ * M + r₁))
  930. else
  931. -(a (j₁ * M + r₁) * b (q * M + r + K * M - (j₁ * M + r₁))) := by
  932. unfold negConv
  933. rw [← Finset.sum_product']
  934. refine Finset.sum_bij (fun i _ => (i / M, i % M)) ?_ ?_ ?_ ?_
  935. · intro i hi
  936. rw [Finset.mem_range] at hi
  937. simp only [Finset.mem_product, Finset.mem_range]
  938. exact ⟨Nat.div_lt_of_lt_mul (by rwa [mul_comm]), Nat.mod_lt i (by omega)⟩
  939. · intro i₁ hi₁ i₂ hi₂ heq
  940. have h1 := congrArg Prod.fst heq
  941. have h2 := congrArg Prod.snd heq
  942. have d1 := Nat.div_add_mod i₁ M
  943. have d2 := Nat.div_add_mod i₂ M
  944. dsimp at h1 h2
  945. rw [h1, h2] at d1
  946. linarith
  947. · intro p hp
  948. simp only [Finset.mem_product, Finset.mem_range] at hp
  949. refine ⟨p.1 * M + p.2, Finset.mem_range.mpr (by nlinarith), ?_⟩
  950. ext
  951. · dsimp
  952. rw [Nat.mul_comm, Nat.mul_add_div (by omega), Nat.div_eq_of_lt hp.2, add_zero]
  953. · dsimp
  954. rw [Nat.mul_add_mod_self_right, Nat.mod_eq_of_lt hp.2]
  955. · intro i _
  956. have hi_eq : i / M * M + i % M = i := by
  957. have := Nat.div_add_mod i M; linarith
  958. simp [hi_eq]
  959. rw [hLHS]
  960. -- Push the `if 0 < q` inside the `j₁` sum on RHS and match term-by-term!
  961. have hRHS :
  962. C q r + (if 0 < q then C (q - 1) (r + M) else -(C (K - 1) (r + M))) =
  963. ∑ j₁ ∈ Finset.range K,
  964. ((if j₁ ≤ q then negConv (2 * M) (A j₁) (B (q - j₁)) r
  965. else -(negConv (2 * M) (A j₁) (B (q + K - j₁)) r)) +
  966. (if 0 < q then
  967. if j₁ ≤ q - 1 then negConv (2 * M) (A j₁) (B (q - 1 - j₁)) (r + M)
  968. else -(negConv (2 * M) (A j₁) (B (q - 1 + K - j₁)) (r + M))
  969. else
  970. -(if j₁ ≤ K - 1 then negConv (2 * M) (A j₁) (B (K - 1 - j₁)) (r + M)
  971. else -(negConv (2 * M) (A j₁) (B (K - 1 + K - j₁)) (r + M))))) := by
  972. dsimp only [C]
  973. split_ifs with hq0
  974. · rw [← Finset.sum_add_distrib]
  975. · rw [← Finset.sum_neg_distrib, ← Finset.sum_add_distrib]
  976. rw [hRHS]
  977. refine Finset.sum_congr rfl ?_
  978. intro j₁ hj₁
  979. rw [Finset.mem_range] at hj₁
  980. simp only [hchunk_lo, hchunk_hi]
  981. -- Split LHS inner sum into `r₁ ≤ r` and `r < r₁` and match each case of `j₁` vs `q`
  982. have hinner_split : ∀ (f : ℕ → Polynomial ℤ),
  983. ∑ r₁ ∈ Finset.range M, f r₁ =
  984. (∑ r₁ ∈ Finset.range M, if r₁ ≤ r then f r₁ else 0) +
  985. (∑ r₁ ∈ Finset.range M, if r < r₁ then f r₁ else 0) := by
  986. intro f
  987. rw [← Finset.sum_add_distrib]
  988. refine Finset.sum_congr rfl ?_
  989. intro r₁ _
  990. by_cases h : r₁ ≤ r
  991. · have h' : ¬ (r < r₁) := by omega
  992. simp [h, h']
  993. · have h' : r < r₁ := by omega
  994. simp [h, h']
  995. rw [hinner_split]
  996. congr 1
  997. · -- Lower half (`r₁ ≤ r`): `j₁ * M + r₁ ≤ q * M + r ↔ j₁ ≤ q`
  998. by_cases hjq : j₁ ≤ q
  999. · simp only [hjq, ↓reduceIte]
  1000. refine Finset.sum_congr rfl ?_
  1001. intro r₁ hr₁; rw [Finset.mem_range] at hr₁
  1002. by_cases hle : r₁ ≤ r
  1003. · have hmain : j₁ * M + r₁ ≤ q * M + r := by nlinarith
  1004. have hidx : q * M + r - (j₁ * M + r₁) = (q - j₁) * M + (r - r₁) := by
  1005. zify [hjq, hle, hmain]; ring
  1006. simp [hle, hmain, hidx]
  1007. · simp [hle]
  1008. · simp only [hjq, ↓reduceIte, ← Finset.sum_neg_distrib]
  1009. refine Finset.sum_congr rfl ?_
  1010. intro r₁ hr₁; rw [Finset.mem_range] at hr₁
  1011. by_cases hle : r₁ ≤ r
  1012. · have hmain : ¬ (j₁ * M + r₁ ≤ q * M + r) := by nlinarith
  1013. have hidx : q * M + r + K * M - (j₁ * M + r₁) = (q + K - j₁) * M + (r - r₁) := by
  1014. have h1 : j₁ ≤ q + K := by omega
  1015. have h2 : j₁ * M + r₁ ≤ q * M + r + K * M := by nlinarith
  1016. zify [h1, hle, h2]; ring
  1017. simp [hle, hmain, hidx]
  1018. · simp [hle]
  1019. · -- Upper half (`r < r₁`): `j₁ * M + r₁ ≤ q * M + r ↔ j₁ < q`
  1020. by_cases hq0 : 0 < q
  1021. · simp only [hq0, ↓reduceIte]
  1022. by_cases hjq : j₁ ≤ q - 1
  1023. · simp only [hjq, ↓reduceIte]
  1024. refine Finset.sum_congr rfl ?_
  1025. intro r₁ hr₁; rw [Finset.mem_range] at hr₁
  1026. by_cases hgt : r < r₁
  1027. · have hmain : j₁ * M + r₁ ≤ q * M + r := by
  1028. have : j₁ + 1 ≤ q := by omega
  1029. nlinarith
  1030. have hidx : q * M + r - (j₁ * M + r₁) = (q - 1 - j₁) * M + (r + M - r₁) := by
  1031. have h1 : j₁ + 1 ≤ q := by omega
  1032. have h2 : r₁ ≤ r + M := by omega
  1033. zify [h1, hq0, hjq, h2, hmain]; ring
  1034. simp [hgt, hmain, hidx]
  1035. · simp [hgt]
  1036. · simp only [hjq, ↓reduceIte, ← Finset.sum_neg_distrib]
  1037. refine Finset.sum_congr rfl ?_
  1038. intro r₁ hr₁; rw [Finset.mem_range] at hr₁
  1039. by_cases hgt : r < r₁
  1040. · have hmain : ¬ (j₁ * M + r₁ ≤ q * M + r) := by
  1041. have : q ≤ j₁ := by omega
  1042. nlinarith
  1043. have hidx : q * M + r + K * M - (j₁ * M + r₁) = (q - 1 + K - j₁) * M + (r + M - r₁) := by
  1044. have h1 : j₁ ≤ q - 1 + K := by omega
  1045. have h2 : r₁ ≤ r + M := by omega
  1046. have h3 : j₁ * M + r₁ ≤ q * M + r + K * M := by
  1047. have : j₁ + 1 ≤ q + K := by omega
  1048. nlinarith
  1049. zify [hq0, h1, h2, h3]; ring
  1050. simp [hgt, hmain, hidx]
  1051. · simp [hgt]
  1052. · have hq_zero : q = 0 := by omega
  1053. subst hq_zero
  1054. have hjK : j₁ ≤ K - 1 := by omega
  1055. simp only [lt_self_iff_false, ↓reduceIte, hjK, ← Finset.sum_neg_distrib]
  1056. refine Finset.sum_congr rfl ?_
  1057. intro r₁ hr₁; rw [Finset.mem_range] at hr₁
  1058. by_cases hgt : r < r₁
  1059. · have hmain : ¬ (j₁ * M + r₁ ≤ r) := by nlinarith
  1060. have hidx : r + K * M - (j₁ * M + r₁) = (K - 1 - j₁) * M + (r + M - r₁) := by
  1061. have h2 : r₁ ≤ r + M := by omega
  1062. have h3 : j₁ * M + r₁ ≤ r + K * M := by nlinarith
  1063. zify [hK, hjK, h2, h3]; ring
  1064. simp [hgt, hmain, hidx]
  1065. · simp [hgt]
  1066. /-- **Division-Free Schönhage–Nussbaumer Negacyclic Multiplication (`CanBuild`):**
  1067. For every `k : ℕ`, given `{0} ∪ {a i | i < 2^k} ∪ {b j | j < 2^k}`, computes
  1068. `{(2 : ℤ) ^ (snScale k) * negConv (2^k) a b t | t < 2^k}` in `snCost k` constant-free gates. -/
  1069. theorem canBuild_snMul :
  1070. ∀ (k : ℕ) (a b : ℕ → Polynomial ℤ),
  1071. CanBuild
  1072. ({(0 : Polynomial ℤ)} ∪ (⋃ i < 2 ^ k, {a i}) ∪ (⋃ j < 2 ^ k, {b j}))
  1073. (snCost k)
  1074. (⋃ t < 2 ^ k, {((2 : Polynomial ℤ) ^ snScale k) * negConv (2 ^ k) a b t})
  1075. | k, a, b => by
  1076. by_cases hk : k ≤ 3
  1077. · rw [snCost, snScale]
  1078. simp only [hk, ↓reduceDIte, pow_zero, one_mul]
  1079. exact canBuild_negConv_schoolbook (2 ^ k) a b
  1080. · have hk4 : 4 ≤ k := by omega
  1081. have hsub_lt : snSub k < k := snSub_lt k hk4
  1082. set p := k / 2
  1083. set K := 2 ^ p
  1084. set M := 2 ^ (k - p)
  1085. set L := 2 ^ snSub k
  1086. have hKM : K * M = 2 ^ k := by
  1087. simp only [K, M, ← pow_add]; congr 1; omega
  1088. have hL2M : L = 2 * M := by
  1089. simp only [L, M, snSub]
  1090. have : (k + 1) / 2 + 1 = (k - k / 2) + 1 := by omega
  1091. rw [this, pow_succ]; ring
  1092. have hKpos : 1 ≤ K := Nat.one_le_two_pow
  1093. have hMpos : 1 ≤ M := Nat.one_le_two_pow
  1094. have hL : 0 < L := by positivity
  1095. -- `step = L / K`: since `k - p = p` or `p + 1`, `L = 2 * M = K * 2` or `K * 4`
  1096. set step := 2 ^ (snSub k - p)
  1097. have hKS : K * step = L := by
  1098. simp only [K, step, L, ← pow_add]
  1099. congr 1; unfold snSub; omega
  1100. -- Chunk arrays `A, B` (size K, each length L = 2*M, zero-padded past M)
  1101. let A : ℕ → ℕ → Polynomial ℤ := fun j u => if u < M then a (j * M + u) else 0
  1102. let B : ℕ → ℕ → Polynomial ℤ := fun j u => if u < M then b (j * M + u) else 0
  1103. -- Twist by `ζ^j = Z^(step * j)`
  1104. let Atw : ℕ → ℕ → Polynomial ℤ := fun j => negShift L (step * j) (A j)
  1105. let Btw : ℕ → ℕ → Polynomial ℤ := fun j => negShift L (step * j) (B j)
  1106. -- Forward DFT with `ang = 2 * step * 1`
  1107. let Ahat := ringDFT L K (2 * step * 1) Atw
  1108. let Bhat := ringDFT L K (2 * step * 1) Btw
  1109. -- Pointwise scaled negacyclic products of size `L = 2^(snSub k)`
  1110. let Phat : ℕ → ℕ → Polynomial ℤ := fun u t =>
  1111. ((2 : Polynomial ℤ) ^ snScale (snSub k)) * negConv L (Ahat u) (Bhat u) t
  1112. -- Inverse DFT with `angInv = 2 * step * (K - 1)`
  1113. let Ctw := ringDFT L K (2 * step * (K - 1)) Phat
  1114. -- Untwist by `ζ^(-j) = Z^(step * (2*K - j))`
  1115. let Cscaled : ℕ → ℕ → Polynomial ℤ := fun j => negShift L (step * (2 * K - j)) (Ctw j)
  1116. -- Recombine overlapping chunks into length `2^k = K * M`
  1117. let Out : ℕ → Polynomial ℤ := fun t =>
  1118. let q := t / M
  1119. let r := t % M
  1120. Cscaled q r + (if 0 < q then Cscaled (q - 1) (r + M) else -(Cscaled (K - 1) (r + M)))
  1121. -- Prove algebraic correctness: `Out t = 2^(snScale k) * negConv (2^k) a b t` for all `t < 2^k`
  1122. have hOut_eq : ∀ t < 2 ^ k,
  1123. Out t = ((2 : Polynomial ℤ) ^ snScale k) * negConv (2 ^ k) a b t := by
  1124. intro t ht
  1125. have hsnScale : snScale k = snScale (snSub k) + p := by
  1126. conv_lhs => rw [snScale]
  1127. simp [hk, p]
  1128. -- First compute `Cscaled j u` for `j < K, u < L`
  1129. have hCscaled : ∀ j < K, ∀ u < L,
  1130. Cscaled j u =
  1131. ((2 : Polynomial ℤ) ^ (snScale (snSub k) + p)) *
  1132. ∑ j₁ ∈ Finset.range K,
  1133. if j₁ ≤ j then negConv (2 * M) (A j₁) (B (j - j₁)) u
  1134. else -(negConv (2 * M) (A j₁) (B (j + K - j₁)) u) := by
  1135. intro j hj u hu
  1136. -- Factor `2^(snScale (snSub k))` out of `Ctw` and `Cscaled`
  1137. have hPhat_eq : Phat = fun w x =>
  1138. ((2 : Polynomial ℤ) ^ snScale (snSub k)) * negConv L (Ahat w) (Bhat w) x := rfl
  1139. have hCtw_pull : ∀ w x,
  1140. Ctw w x = ((2 : Polynomial ℤ) ^ snScale (snSub k)) *
  1141. ringDFT L K (2 * step * (K - 1)) (fun z => negConv L (Ahat z) (Bhat z)) w x := by
  1142. intro w x
  1143. dsimp only [Ctw, ringDFT, Phat]
  1144. rw [Finset.mul_sum]
  1145. refine Finset.sum_congr rfl ?_
  1146. intro z _
  1147. rw [negShift_smul_linear]
  1148. dsimp only [Cscaled]
  1149. have hCtw_fn : Ctw j = fun x =>
  1150. ((2 : Polynomial ℤ) ^ snScale (snSub k)) *
  1151. ringDFT L K (2 * step * (K - 1)) (fun z => negConv L (Ahat z) (Bhat z)) j x :=
  1152. funext (hCtw_pull j)
  1153. rw [hCtw_fn, negShift_smul_linear]
  1154. -- Apply `ringDFT_convolution` to `Atw, Btw`
  1155. have hAhat_eq : Ahat = ringDFT L K (2 * step) Atw := by
  1156. simp [Ahat]
  1157. have hBhat_eq : Bhat = ringDFT L K (2 * step) Btw := by
  1158. simp [Bhat]
  1159. have hconv_fn :
  1160. ∀ x < L,
  1161. ringDFT L K (2 * step * (K - 1)) (fun z => negConv L (Ahat z) (Bhat z)) j x =
  1162. ((K : ℕ) : Polynomial ℤ) *
  1163. ∑ j₁ ∈ Finset.range K,
  1164. negConv L (Atw j₁) (Btw ((j + K - j₁) % K)) x := by
  1165. intro x hx
  1166. rw [hAhat_eq, hBhat_eq]
  1167. exact ringDFT_convolution p step L hL hKS Atw Btw j x hj hx
  1168. -- Apply `negShift L (step * (2*K - j))` to `hconv_fn`
  1169. have hsh_eq :
  1170. negShift L (step * (2 * K - j))
  1171. (ringDFT L K (2 * step * (K - 1)) (fun z => negConv L (Ahat z) (Bhat z)) j) u =
  1172. negShift L (step * (2 * K - j))
  1173. (fun x => ((K : ℕ) : Polynomial ℤ) *
  1174. ∑ j₁ ∈ Finset.range K, negConv L (Atw j₁) (Btw ((j + K - j₁) % K)) x) u :=
  1175. negShift_congr L _ hL _ _ hconv_fn u hu
  1176. rw [hsh_eq, negShift_smul_linear, negShift_finset_sum]
  1177. have hKcast : ((K : ℕ) : Polynomial ℤ) = (2 : Polynomial ℤ) ^ p := by
  1178. simp [K]
  1179. rw [hKcast, ← mul_assoc, ← pow_add]
  1180. congr 1
  1181. refine Finset.sum_congr rfl ?_
  1182. intro j₁ hj₁
  1183. rw [Finset.mem_range] at hj₁
  1184. dsimp only [Atw, Btw]
  1185. have h3shift :
  1186. negShift L (step * (2 * K - j))
  1187. (negConv L (negShift L (step * j₁) (A j₁))
  1188. (negShift L (step * ((j + K - j₁) % K)) (B ((j + K - j₁) % K)))) u =
  1189. negShift L (step * (2 * K - j) + step * j₁ + step * ((j + K - j₁) % K))
  1190. (negConv L (A j₁) (B ((j + K - j₁) % K))) u := by
  1191. have hinner_eq : ∀ x < L,
  1192. negConv L (negShift L (step * j₁) (A j₁))
  1193. (negShift L (step * ((j + K - j₁) % K)) (B ((j + K - j₁) % K))) x =
  1194. negShift L (step * j₁ + step * ((j + K - j₁) % K))
  1195. (negConv L (A j₁) (B ((j + K - j₁) % K))) x := by
  1196. intro x hx
  1197. rw [negConv_negShift_left L _ hL _ _ x hx]
  1198. rw [negShift_congr L _ hL _ _ (fun y hy => negConv_negShift_right L _ hL (A j₁) _ y hy) x hx]
  1199. rw [← negShift_add L _ _ hL _ x hx]
  1200. have houter := negShift_congr L (step * (2 * K - j)) hL _ _ hinner_eq u hu
  1201. rw [houter, ← negShift_add L _ _ hL _ u hu, add_assoc]
  1202. rw [h3shift, hL2M]
  1203. by_cases hle : j₁ ≤ j
  1204. · have hmod : (j + K - j₁) % K = j - j₁ := by
  1205. rw [show j + K - j₁ = (j - j₁) + K by omega, Nat.add_mod_right,
  1206. Nat.mod_eq_of_lt (by omega)]
  1207. simp only [hle, ↓reduceIte, hmod]
  1208. have hsum_exp : step * (2 * K - j) + step * j₁ + step * (j - j₁) = 2 * L := by
  1209. rw [← hKS]
  1210. zify [hle, show j ≤ 2 * K by omega]; ring
  1211. rw [← hL2M, hsum_exp, show 2 * L = 0 + 2 * L by ring, negShift_add_mul_N L 0 2 hL,
  1212. negShift_zero L hL]
  1213. norm_num
  1214. · have hmod : (j + K - j₁) % K = j + K - j₁ := Nat.mod_eq_of_lt (by omega)
  1215. simp only [hle, ↓reduceIte, hmod]
  1216. have hsum_exp : step * (2 * K - j) + step * j₁ + step * (j + K - j₁) = 3 * L := by
  1217. rw [← hKS]
  1218. zify [show j₁ ≤ j + K by omega, show j ≤ 2 * K by omega]; ring
  1219. rw [← hL2M, hsum_exp, show 3 * L = 0 + 3 * L by ring, negShift_add_mul_N L 0 3 hL,
  1220. negShift_zero L hL]
  1221. norm_num
  1222. have hq : t / M < K := Nat.div_lt_of_lt_mul (by rwa [mul_comm, hKM])
  1223. have hr : t % M < M := Nat.mod_lt t (by omega)
  1224. have ht_eq : t = (t / M) * M + t % M := by
  1225. have := Nat.div_add_mod t M; linarith
  1226. rw [hsnScale]
  1227. dsimp only [Out]
  1228. rw [hCscaled (t / M) hq (t % M) (by omega)]
  1229. have h2nd :
  1230. (if 0 < t / M then Cscaled (t / M - 1) (t % M + M)
  1231. else -(Cscaled (K - 1) (t % M + M))) =
  1232. ((2 : Polynomial ℤ) ^ (snScale (snSub k) + p)) *
  1233. (if 0 < t / M then
  1234. ∑ j₁ ∈ Finset.range K,
  1235. if j₁ ≤ t / M - 1 then negConv (2 * M) (A j₁) (B (t / M - 1 - j₁)) (t % M + M)
  1236. else -(negConv (2 * M) (A j₁) (B (t / M - 1 + K - j₁)) (t % M + M))
  1237. else
  1238. -(∑ j₁ ∈ Finset.range K,
  1239. if j₁ ≤ K - 1 then negConv (2 * M) (A j₁) (B (K - 1 - j₁)) (t % M + M)
  1240. else -(negConv (2 * M) (A j₁) (B (K - 1 + K - j₁)) (t % M + M)))) := by
  1241. split_ifs with hq0
  1242. · exact hCscaled (t / M - 1) (by omega) (t % M + M) (by omega)
  1243. · rw [hCscaled (K - 1) (by omega) (t % M + M) (by omega)]; ring
  1244. rw [h2nd, ← mul_add]
  1245. congr 1
  1246. conv_rhs => rw [← hKM, ht_eq]
  1247. exact (negConv_chunk_recombine K M hKpos hMpos a b (t / M) (t % M) hq hr).symm
  1248. -- Now chain the `CanBuild` steps!
  1249. set S0 : Set (Polynomial ℤ) :=
  1250. {(0 : Polynomial ℤ)} ∪ (⋃ i < 2 ^ k, {a i}) ∪ (⋃ j < 2 ^ k, {b j})
  1251. -- Step 1 (`K * L` gates): `Atw`
  1252. have hAtw : CanBuild S0 (K * L) (⋃ j < K, ⋃ u < L, {Atw j u}) := by
  1253. refine canBuild_fin_union (n := K) (d := L) (T := fun j => ⋃ u < L, {Atw j u}) ?_
  1254. intro j hj
  1255. refine canBuild_mono_in ?_ (canBuild_negShift L (step * j) hL (A j))
  1256. intro x hx; rcases hx with rfl | hx'
  1257. · left; left; rfl
  1258. · simp only [Set.mem_iUnion, exists_prop, Set.mem_singleton_iff] at hx'
  1259. rcases hx' with ⟨u, hu, rfl⟩
  1260. dsimp only [A]
  1261. split_ifs with huM
  1262. · left; right; simp only [Set.mem_iUnion, exists_prop]
  1263. exact ⟨j * M + u, by rw [← hKM]; nlinarith, rfl⟩
  1264. · left; left; rfl
  1265. -- Step 2 (`K * L` gates): `Btw`
  1266. have hBtw : CanBuild S0 (K * L) (⋃ j < K, ⋃ u < L, {Btw j u}) := by
  1267. refine canBuild_fin_union (n := K) (d := L) (T := fun j => ⋃ u < L, {Btw j u}) ?_
  1268. intro j hj
  1269. refine canBuild_mono_in ?_ (canBuild_negShift L (step * j) hL (B j))
  1270. intro x hx; rcases hx with rfl | hx'
  1271. · left; left; rfl
  1272. · simp only [Set.mem_iUnion, exists_prop, Set.mem_singleton_iff] at hx'
  1273. rcases hx' with ⟨u, hu, rfl⟩
  1274. dsimp only [B]
  1275. split_ifs with huM
  1276. · right; simp only [Set.mem_iUnion, exists_prop]
  1277. exact ⟨j * M + u, by rw [← hKM]; nlinarith, rfl⟩
  1278. · left; left; rfl
  1279. -- Step 3 (`2 * dftCost p L` gates): `Ahat` and `Bhat`
  1280. have hOdd1 : Odd (1 : ℕ) := ⟨0, by omega⟩
  1281. have hOddKm1 : Odd (K - 1) := by
  1282. have hp1 : 1 ≤ p := by omega
  1283. have hK2 : 2 ∣ K := dvd_pow_self 2 (by omega : p ≠ 0)
  1284. rcases hK2 with ⟨m, hm⟩
  1285. exact ⟨m - 1, by omega⟩
  1286. have hAhat := canBuild_ringDFT p step 1 L hL hKS hOdd1 Atw
  1287. have hBhat := canBuild_ringDFT p step 1 L hL hKS hOdd1 Btw
  1288. -- Step 4 (`K * snCost (snSub k)` gates): `K` recursive calls `Phat u`
  1289. have hPhat :
  1290. CanBuild
  1291. ({(0 : Polynomial ℤ)} ∪ (⋃ u < K, ⋃ t < L, {Ahat u t}) ∪
  1292. (⋃ u < K, ⋃ t < L, {Bhat u t}))
  1293. (K * snCost (snSub k))
  1294. (⋃ u < K, ⋃ t < L, {Phat u t}) := by
  1295. refine canBuild_fin_union (n := K) (d := snCost (snSub k))
  1296. (T := fun u => ⋃ t < L, {Phat u t}) ?_
  1297. intro u hu
  1298. refine canBuild_mono_in ?_ (canBuild_snMul (snSub k) (Ahat u) (Bhat u))
  1299. intro x hx; rcases hx with (rfl | hxA) | hxB
  1300. · left; left; rfl
  1301. · left; right; simp only [Set.mem_iUnion, exists_prop] at hxA ⊢
  1302. rcases hxA with ⟨t, ht, hxt⟩; exact ⟨u, hu, t, ht, hxt⟩
  1303. · right; simp only [Set.mem_iUnion, exists_prop] at hxB ⊢
  1304. rcases hxB with ⟨t, ht, hxt⟩; exact ⟨u, hu, t, ht, hxt⟩
  1305. -- Step 5 (`dftCost p L` gates): `Ctw`
  1306. have hCtw := canBuild_ringDFT p step (K - 1) L hL hKS hOddKm1 Phat
  1307. -- Step 6 (`K * L` gates): `Cscaled`
  1308. have hCsc :
  1309. CanBuild ({(0 : Polynomial ℤ)} ∪ ⋃ j < K, ⋃ u < L, {Ctw j u})
  1310. (K * L) (⋃ j < K, ⋃ u < L, {Cscaled j u}) := by
  1311. refine canBuild_fin_union (n := K) (d := L) (T := fun j => ⋃ u < L, {Cscaled j u}) ?_
  1312. intro j hj
  1313. refine canBuild_mono_in ?_ (canBuild_negShift L (step * (2 * K - j)) hL (Ctw j))
  1314. intro x hx; rcases hx with rfl | hx'
  1315. · left; rfl
  1316. · right; simp only [Set.mem_iUnion, exists_prop] at hx' ⊢
  1317. rcases hx' with ⟨u, hu, hxu⟩; exact ⟨j, hj, u, hu, hxu⟩
  1318. -- Step 7 (`2 * 2^k` gates): `Out t` for `t < 2^k`
  1319. have hOut :
  1320. CanBuild ({(0 : Polynomial ℤ)} ∪ ⋃ j < K, ⋃ u < L, {Cscaled j u})
  1321. (2 ^ k * 2) (⋃ t < 2 ^ k, {Out t}) := by
  1322. refine canBuild_fin_union (n := 2 ^ k) (d := 2) (T := fun t => {Out t}) ?_
  1323. intro t ht
  1324. set q := t / M
  1325. set r := t % M
  1326. have hq : q < K := Nat.div_lt_of_lt_mul (by rwa [mul_comm, hKM])
  1327. have hr : r < M := Nat.mod_lt t (by omega)
  1328. by_cases hq0 : 0 < q
  1329. · have ha := canBuild_add (Cscaled q r) (Cscaled (q - 1) (r + M))
  1330. have hOut_eq' : Out t = Cscaled q r + Cscaled (q - 1) (r + M) := by
  1331. change Cscaled q r + (if 0 < q then Cscaled (q - 1) (r + M) else -(Cscaled (K - 1) (r + M))) = _
  1332. simp only [hq0, ↓reduceIte]
  1333. refine canBuild_mono_cost (by omega : 1 ≤ 2)
  1334. (canBuild_mono_out (T₂ := {Cscaled q r + Cscaled (q - 1) (r + M)})
  1335. (by intro x hx; rw [Set.mem_singleton_iff] at hx; subst hx; rw [hOut_eq']; rfl)
  1336. (canBuild_mono_in ?_ ha))
  1337. intro x hx; rcases hx with rfl | rfl
  1338. · right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨q, hq, r, by omega, rfl⟩
  1339. · right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨q - 1, by omega, r + M, by omega, rfl⟩
  1340. · have hs := canBuild_sub (Cscaled q r) (Cscaled (K - 1) (r + M))
  1341. have hOut_eq' : Out t = Cscaled q r - Cscaled (K - 1) (r + M) := by
  1342. change Cscaled q r + (if 0 < q then Cscaled (q - 1) (r + M) else -(Cscaled (K - 1) (r + M))) = _
  1343. simp only [hq0, ↓reduceIte, sub_eq_add_neg]
  1344. refine canBuild_mono_cost (by omega : 1 ≤ 2)
  1345. (canBuild_mono_out (T₂ := {Cscaled q r - Cscaled (K - 1) (r + M)})
  1346. (by intro x hx; rw [Set.mem_singleton_iff] at hx; subst hx; rw [hOut_eq']; rfl)
  1347. (canBuild_mono_in ?_ hs))
  1348. intro x hx; rcases hx with rfl | rfl
  1349. · right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨q, hq, r, by omega, rfl⟩
  1350. · right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨K - 1, by omega, r + M, by omega, rfl⟩
  1351. -- Compose Steps 1..7
  1352. have s12 := canBuild_trans hAtw Set.subset_union_left hBtw
  1353. have s13 := canBuild_trans s12 (by
  1354. intro x hx; rcases hx with rfl | hx'
  1355. · left; left; left; rfl
  1356. · right; left; exact hx') hAhat
  1357. have s14 := canBuild_trans s13 (by
  1358. intro x hx; rcases hx with rfl | hx'
  1359. · left; left; left; rfl
  1360. · right; left; right; exact hx') hBhat
  1361. have s15 := canBuild_trans s14 (by
  1362. intro x hx; rcases hx with (rfl | hxA) | hxB
  1363. · left; left; left; rfl
  1364. · right; left; right; exact hxA
  1365. · right; right; exact hxB) hPhat
  1366. have s16 := canBuild_trans s15 (by
  1367. intro x hx; rcases hx with rfl | hx'
  1368. · left; left; left; rfl
  1369. · right; right; exact hx') hCtw
  1370. have s17 := canBuild_trans s16 (by
  1371. intro x hx; rcases hx with rfl | hx'
  1372. · left; left; left; rfl
  1373. · right; right; exact hx') hCsc
  1374. have s18 := canBuild_trans s17 (by
  1375. intro x hx; rcases hx with rfl | hx'
  1376. · left; left; left; rfl
  1377. · right; right; exact hx') hOut
  1378. have hcost :
  1379. K * L + K * L + dftCost p L + dftCost p L + K * snCost (snSub k) +
  1380. dftCost p L + K * L + 2 ^ k * 2 = snCost k := by
  1381. conv_rhs => rw [snCost]
  1382. simp only [hk, ↓reduceDIte, p, K, L]
  1383. ring
  1384. rw [hcost] at s18
  1385. refine canBuild_mono_out ?_ s18
  1386. intro x hx
  1387. simp only [Set.mem_iUnion, exists_prop, Set.mem_singleton_iff] at hx
  1388. rcases hx with ⟨t, ht, rfl⟩
  1389. right
  1390. simp only [Set.mem_iUnion, exists_prop]
  1391. exact ⟨t, ht, (hOut_eq t ht).symm⟩
  1392. termination_by k => k
  1393. decreasing_by exact snSub_lt k (by omega)
  1394. /-! ## 5. Asymptotic Bound on `snCost k` and Refutation of All Real Exponents `c < 2` -/
  1395. /-- Exact asymptotic upper bound on Schönhage–Nussbaumer gate cost:
  1396. `snCost k ≤ 200 * k * (Nat.log 2 k + 1) * 2 ^ k` for every `k ≥ 1`. -/
  1397. theorem snCost_le : ∀ k : ℕ, 1 ≤ k → snCost k ≤ 200 * k * (Nat.log 2 k + 1) * 2 ^ k := by
  1398. -- Prove the sharper shifted invariant for all `k ≥ 1`:
  1399. -- `snCost k ≤ (50 * (k - 3) * (Nat.log 2 (k - 3) + 1) + 48) * 2 ^ k`
  1400. -- Check:
  1401. -- k = 1: 3 * 2 * 2 = 12 ≤ 48 * 2 = 96
  1402. -- k = 2: 3 * 4 * 4 = 48 ≤ 48 * 4 = 192
  1403. -- k = 3: 3 * 8 * 8 = 192 ≤ 48 * 8 = 384
  1404. -- k = 4: snSub 4 = 3, p = 2, K = 4, L = 8:
  1405. -- snCost 4 = 4 * 192 + 3 * (3 * 2 * 4 * 8) + 3 * 4 * 8 + 32 = 768 + 576 + 96 + 32 = 1472
  1406. -- RHS at k = 4: (50 * 1 * 1 + 48) * 16 = 98 * 16 = 1568 ≥ 1472!
  1407. -- k ≥ 5: let k' = snSub k = (k + 1) / 2 + 1. Note K * 2^k' = 2^(k/2 + (k+1)/2 + 1) = 2 * 2^k,
  1408. -- and 2 * (k' - 3) ≤ k - 3, and Nat.log 2 (k' - 3) + 1 ≤ Nat.log 2 (k - 3)!
  1409. -- Extra work: 3 * dftCost p L + 3 * K * L + 2 * 2^k
  1410. -- = (9 * p * 2 + 3 * 2 + 2) * 2^k = (18 * p + 8) * 2^k ≤ (9 * k + 8) * 2^k.
  1411. -- Total / 2^k ≤ 2 * (50 * (k' - 3) * (log(k'-3) + 1) + 48) + 9 * k + 8
  1412. -- ≤ 50 * (k - 3) * log(k - 3) + 96 + 9 * k + 8
  1413. -- = 50 * (k - 3) * (log(k - 3) + 1) + 48 + (56 + 9 * k - 50 * (k - 3))
  1414. -- and for k ≥ 5: 56 + 9 * k ≤ 101 ≤ 100? Wait: at k = 5: 56 + 45 = 101 > 50 * 2 = 100!
  1415. -- Bump 50 → 60! At k = 5: 56 + 45 = 101 ≤ 60 * 2 = 120!
  1416. -- And for all k ≥ 5: 56 + 9 * k ≤ 60 * (k - 3) ⟺ 236 ≤ 51 * k (51 * 5 = 255 ≥ 236)!
  1417. -- And 60 * (k - 3) * (log(k-3) + 1) + 48 ≤ 200 * k * (log k + 1) for all k ≥ 1!
  1418. have hstrong : ∀ k : ℕ, 1 ≤ k →
  1419. snCost k ≤ (60 * (k - 3) * (Nat.log 2 (k - 3) + 1) + 48) * 2 ^ k := by
  1420. intro k
  1421. induction k using Nat.strongRecOn with
  1422. | ind k ih =>
  1423. intro hk1
  1424. by_cases hk3 : k ≤ 3
  1425. · rw [snCost]
  1426. simp only [hk3, ↓reduceDIte]
  1427. have hpow : 2 ^ k ≤ 8 := by
  1428. calc 2 ^ k ≤ 2 ^ 3 := Nat.pow_le_pow_right (by omega) hk3
  1429. _ = 8 := by norm_num
  1430. have h1 : 3 * 2 ^ k ≤ 60 * (k - 3) * (Nat.log 2 (k - 3) + 1) + 48 := by omega
  1431. exact Nat.mul_le_mul_right (2 ^ k) h1
  1432. · by_cases hk4 : k = 4
  1433. · subst hk4
  1434. have hval : snCost 4 = 1472 := by
  1435. rw [snCost]; simp only [show ¬ (4 ≤ 3) by omega, ↓reduceDIte, snSub, dftCost]
  1436. rw [snCost]; simp
  1437. rw [hval]
  1438. norm_num
  1439. · have hk5 : 5 ≤ k := by omega
  1440. have hlt : snSub k < k := snSub_lt k (by omega)
  1441. have hsub1 : 1 ≤ snSub k := by unfold snSub; omega
  1442. have hih := ih (snSub k) hlt hsub1
  1443. set p := k / 2
  1444. set K := 2 ^ p
  1445. set L := 2 ^ snSub k
  1446. have hKL : K * L = 2 * 2 ^ k := by
  1447. simp only [K, L, snSub, ← pow_add]
  1448. rw [show p + ((k + 1) / 2 + 1) = k + 1 by omega, pow_succ]
  1449. ring
  1450. have hdft : 3 * dftCost p L + 3 * K * L + 2 * 2 ^ k ≤ (9 * k + 8) * 2 ^ k := by
  1451. simp only [dftCost]
  1452. have h1 : 3 * (3 * p * K * L) + 3 * K * L + 2 * 2 ^ k =
  1453. (18 * p + 8) * 2 ^ k := by
  1454. have : 3 * (3 * p * K * L) + 3 * K * L = (9 * p + 3) * (K * L) := by ring
  1455. rw [this, hKL]; ring
  1456. have h2 : 18 * p + 8 ≤ 9 * k + 8 := by omega
  1457. rw [h1]
  1458. exact Nat.mul_le_mul_right (2 ^ k) h2
  1459. have hrec : K * snCost (snSub k) ≤
  1460. (2 * (60 * (snSub k - 3) * (Nat.log 2 (snSub k - 3) + 1) + 48)) * 2 ^ k := by
  1461. calc K * snCost (snSub k)
  1462. ≤ K * ((60 * (snSub k - 3) * (Nat.log 2 (snSub k - 3) + 1) + 48) * L) :=
  1463. Nat.mul_le_mul_left K hih
  1464. _ = (60 * (snSub k - 3) * (Nat.log 2 (snSub k - 3) + 1) + 48) * (K * L) := by ring
  1465. _ = (2 * (60 * (snSub k - 3) * (Nat.log 2 (snSub k - 3) + 1) + 48)) * 2 ^ k := by
  1466. rw [hKL]; ring
  1467. have hhalf : 2 * (snSub k - 3) ≤ k - 3 := by unfold snSub; omega
  1468. have hpos_sub : 1 ≤ snSub k - 3 := by unfold snSub; omega
  1469. have hlog : Nat.log 2 (snSub k - 3) + 1 ≤ Nat.log 2 (k - 3) := by
  1470. have h1 : Nat.log 2 (2 * (snSub k - 3)) = Nat.log 2 (snSub k - 3) + 1 := by
  1471. rw [mul_comm, Nat.log_mul_base (by omega) (by omega)]
  1472. rw [← h1]
  1473. exact Nat.log_mono_right hhalf
  1474. have hmain_term :
  1475. 2 * (60 * (snSub k - 3) * (Nat.log 2 (snSub k - 3) + 1)) ≤
  1476. 60 * (k - 3) * Nat.log 2 (k - 3) := by
  1477. calc 2 * (60 * (snSub k - 3) * (Nat.log 2 (snSub k - 3) + 1))
  1478. = 60 * (2 * (snSub k - 3)) * (Nat.log 2 (snSub k - 3) + 1) := by ring
  1479. _ ≤ 60 * (k - 3) * Nat.log 2 (k - 3) := by
  1480. exact Nat.mul_le_mul (Nat.mul_le_mul_left 60 hhalf) hlog
  1481. have hcoeff :
  1482. 2 * (60 * (snSub k - 3) * (Nat.log 2 (snSub k - 3) + 1) + 48) + (9 * k + 8) ≤
  1483. 60 * (k - 3) * (Nat.log 2 (k - 3) + 1) + 48 := by
  1484. have hlin : 56 + 9 * k ≤ 60 * (k - 3) := by omega
  1485. have hexp : 60 * (k - 3) * (Nat.log 2 (k - 3) + 1) =
  1486. 60 * (k - 3) * Nat.log 2 (k - 3) + 60 * (k - 3) := by ring
  1487. omega
  1488. conv_lhs => rw [snCost]
  1489. simp only [hk3, ↓reduceDIte]
  1490. calc K * snCost (snSub k) + 3 * dftCost p L + 3 * K * L + 2 * 2 ^ k
  1491. = K * snCost (snSub k) + (3 * dftCost p L + 3 * K * L + 2 * 2 ^ k) := by ring
  1492. _ ≤ (2 * (60 * (snSub k - 3) * (Nat.log 2 (snSub k - 3) + 1) + 48)) * 2 ^ k +
  1493. (9 * k + 8) * 2 ^ k := Nat.add_le_add hrec hdft
  1494. _ = (2 * (60 * (snSub k - 3) * (Nat.log 2 (snSub k - 3) + 1) + 48) + (9 * k + 8)) * 2 ^ k := by
  1495. ring
  1496. _ ≤ (60 * (k - 3) * (Nat.log 2 (k - 3) + 1) + 48) * 2 ^ k :=
  1497. Nat.mul_le_mul_right (2 ^ k) hcoeff
  1498. intro k hk1
  1499. have hs := hstrong k hk1
  1500. have hlog : Nat.log 2 (k - 3) ≤ Nat.log 2 k := Nat.log_mono_right (Nat.sub_le k 3)
  1501. have h1 : 60 * (k - 3) * (Nat.log 2 (k - 3) + 1) + 48 ≤ 200 * k * (Nat.log 2 k + 1) := by
  1502. have hmul1 : 60 * (k - 3) * (Nat.log 2 (k - 3) + 1) ≤ 60 * k * (Nat.log 2 k + 1) :=
  1503. Nat.mul_le_mul (Nat.mul_le_mul_left 60 (Nat.sub_le k 3)) (Nat.add_le_add_right hlog 1)
  1504. have h48 : 48 ≤ 140 * k * (Nat.log 2 k + 1) := by
  1505. have : 1 ≤ k * (Nat.log 2 k + 1) := Nat.mul_pos hk1 (by omega)
  1506. nlinarith
  1507. have : 60 * k * (Nat.log 2 k + 1) + 140 * k * (Nat.log 2 k + 1) =
  1508. 200 * k * (Nat.log 2 k + 1) := by ring
  1509. omega
  1510. exact hs.trans (Nat.mul_le_mul_right (2 ^ k) h1)
  1511. /-- **Unconditional Near-Quadratic Growth (`z_max(τ) = Ω(τ^2 / (log^2 τ (log log τ)^2))`):**
  1512. For every `k ≥ 2`, there exists a constant-free SLP of length `2000 * k * (Nat.log 2 k + 1) * 2 ^ k`
  1513. computing a nonzero polynomial in `ℤ[X]` with at least `2 * 4 ^ k + 1` distinct integer roots. -/
  1514. theorem tau_subquadratic_growth (k : ℕ) (hk : 2 ≤ k) :
  1515. zMaxGe (2000 * k * (Nat.log 2 k + 1) * 2 ^ k) (2 * 4 ^ k + 1) := by
  1516. set m := 2 ^ k
  1517. have hm : 1 ≤ m := Nat.one_le_two_pow
  1518. have hm2 : m * m = 4 ^ k := by
  1519. simp only [m, ← pow_add, show k + k = 2 * k by ring, pow_mul]
  1520. norm_num
  1521. have h2m : 2 * m = 2 ^ (k + 1) := by simp [m, pow_succ, mul_comm]
  1522. have hsn := canBuild_snMul (k + 1) (chirpA m) (chirpB m)
  1523. have hSpos : (0 : ℤ) < (2 : ℤ) ^ snScale (k + 1) := by positivity
  1524. have hstep :
  1525. CanBuild
  1526. ({(0 : Polynomial ℤ)} ∪ (⋃ r < m + 1, {chirpA m r}) ∪ (⋃ s < 2 * m, {chirpB m s}))
  1527. (snCost (k + 1))
  1528. (⋃ i < m, {Polynomial.C ((2 : ℤ) ^ snScale (k + 1)) *
  1529. ∑ r ∈ Finset.range (m + 1), chirpA m r * chirpB m (i + m - r)}) := by
  1530. have hin :
  1531. ({(0 : Polynomial ℤ)} ∪ (⋃ i < 2 ^ (k + 1), {chirpA m i}) ∪
  1532. (⋃ j < 2 ^ (k + 1), {chirpB m j})) ⊆
  1533. ({(0 : Polynomial ℤ)} ∪ (⋃ r < m + 1, {chirpA m r}) ∪ (⋃ s < 2 * m, {chirpB m s})) := by
  1534. intro x hx
  1535. rcases hx with (rfl | hxA) | hxB
  1536. · left; left; rfl
  1537. · simp only [Set.mem_iUnion, exists_prop, Set.mem_singleton_iff] at hxA
  1538. rcases hxA with ⟨r, _, rfl⟩
  1539. by_cases hrm : r ≤ m
  1540. · left; right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨r, by omega, rfl⟩
  1541. · left; left; simp [chirpA, hrm]
  1542. · right
  1543. simp only [Set.mem_iUnion, exists_prop, Set.mem_singleton_iff] at hxB ⊢
  1544. rcases hxB with ⟨s, hs, rfl⟩
  1545. exact ⟨s, by omega, rfl⟩
  1546. refine canBuild_mono_out ?_ (canBuild_mono_in hin hsn)
  1547. intro x hx
  1548. simp only [Set.mem_iUnion, exists_prop, Set.mem_singleton_iff] at hx ⊢
  1549. rcases hx with ⟨i, hi, rfl⟩
  1550. refine ⟨i + m, by omega, ?_⟩
  1551. rw [← h2m, negConv_chirp_eq m i hm hi]
  1552. simp [map_pow]
  1553. have hz := zMaxGe_of_chirp_engine hm ((2 : ℤ) ^ snScale (k + 1)) hSpos hstep
  1554. rw [hm2] at hz
  1555. have hle : 17 * m + snCost (k + 1) + 14 ≤ 2000 * k * (Nat.log 2 k + 1) * 2 ^ k := by
  1556. have hsn_le := snCost_le (k + 1) (by omega)
  1557. have hlog1 : Nat.log 2 (k + 1) ≤ Nat.log 2 k + 1 := by
  1558. have h1 : k + 1 ≤ k * 2 := by omega
  1559. calc Nat.log 2 (k + 1) ≤ Nat.log 2 (k * 2) := Nat.log_mono_right h1
  1560. _ = Nat.log 2 k + 1 := Nat.log_mul_base (by omega) (by omega)
  1561. have hkl : (k + 1) * (Nat.log 2 (k + 1) + 1) ≤ 4 * k * (Nat.log 2 k + 1) := by
  1562. have hk2 : k + 1 ≤ 2 * k := by omega
  1563. have hl2 : Nat.log 2 (k + 1) + 1 ≤ 2 * (Nat.log 2 k + 1) := by omega
  1564. calc (k + 1) * (Nat.log 2 (k + 1) + 1)
  1565. ≤ (2 * k) * (2 * (Nat.log 2 k + 1)) := Nat.mul_le_mul hk2 hl2
  1566. _ = 4 * k * (Nat.log 2 k + 1) := by ring
  1567. have hsn_bound : snCost (k + 1) ≤ 1600 * k * (Nat.log 2 k + 1) * 2 ^ k := by
  1568. calc snCost (k + 1)
  1569. ≤ 200 * (k + 1) * (Nat.log 2 (k + 1) + 1) * 2 ^ (k + 1) := hsn_le
  1570. _ = 400 * ((k + 1) * (Nat.log 2 (k + 1) + 1)) * 2 ^ k := by rw [pow_succ]; ring
  1571. _ ≤ 400 * (4 * k * (Nat.log 2 k + 1)) * 2 ^ k :=
  1572. Nat.mul_le_mul_right (2 ^ k) (Nat.mul_le_mul_left 400 hkl)
  1573. _ = 1600 * k * (Nat.log 2 k + 1) * 2 ^ k := by ring
  1574. have hrem : 17 * m + 14 ≤ 400 * k * (Nat.log 2 k + 1) * 2 ^ k := by
  1575. have h1 : 17 * m + 14 ≤ 31 * 2 ^ k := by omega
  1576. have h2 : 31 ≤ 400 * k * (Nat.log 2 k + 1) := by
  1577. have : 1 ≤ k * (Nat.log 2 k + 1) := Nat.mul_pos (by omega) (by omega)
  1578. nlinarith
  1579. exact h1.trans (Nat.mul_le_mul_right (2 ^ k) h2)
  1580. have hsum : 1600 * k * (Nat.log 2 k + 1) * 2 ^ k + 400 * k * (Nat.log 2 k + 1) * 2 ^ k =
  1581. 2000 * k * (Nat.log 2 k + 1) * 2 ^ k := by ring
  1582. omega
  1583. obtain ⟨d, hd⟩ := Nat.exists_eq_add_of_le hle
  1584. rw [hd]
  1585. clear hd hle
  1586. induction d with
  1587. | zero => simpa using hz
  1588. | succ d ih => exact zMaxGe_succ ih
  1589. /-- **Unconditional Refutation of Every Sub-Quadratic Exponent `c < 2` in Smale's 4th Problem:**
  1590. For every real `c < 2`, there is NO real constant `a` such that every nonzero constant-free SLP
  1591. of length `τ` satisfies `(intRootCount P.evalPoly : ℝ) ≤ a * ((τ : ℝ) + 1) ^ c`. -/
  1592. theorem tau_refutes_subquadratic (c : ℝ) (hc : c < 2) :
  1593. ¬ ∃ a : ℝ, ∀ (τ : ℕ) (P : SLP τ), P.evalPoly ≠ 0 →
  1594. (intRootCount P.evalPoly : ℝ) ≤ a * ((τ : ℝ) + 1) ^ c := by
  1595. rintro ⟨a, ha⟩
  1596. by_cases hc0 : c ≤ 0
  1597. · -- When `c ≤ 0`, `(τ + 1)^c ≤ 1`, contradicting unbounded root counts
  1598. obtain ⟨k, hk⟩ := exists_nat_gt (max a 0)
  1599. let k' := k + 2
  1600. rcases tau_subquadratic_growth k' (by omega) with ⟨P, hne, hcard⟩
  1601. have hub := ha _ P hne
  1602. have htau_ge : (1 : ℝ) ≤ ((2000 * k' * (Nat.log 2 k' + 1) * 2 ^ k' : ℕ) : ℝ) + 1 := by
  1603. linarith [Nat.cast_nonneg (α := ℝ) (2000 * k' * (Nat.log 2 k' + 1) * 2 ^ k')]
  1604. have hrpow_le : (((2000 * k' * (Nat.log 2 k' + 1) * 2 ^ k' : ℕ) : ℝ) + 1) ^ c ≤ 1 :=
  1605. Real.rpow_le_one_of_one_le_of_nonpos htau_ge hc0
  1606. have h4gt : (k : ℝ) < (2 * 4 ^ k' + 1 : ℕ) := by
  1607. have h1 : k < 2 * 4 ^ k' + 1 := by
  1608. have : k' < 2 ^ k' := Nat.lt_two_pow_self
  1609. have : 2 ^ k' ≤ 4 ^ k' := Nat.pow_le_pow_left (by omega) k'
  1610. omega
  1611. exact_mod_cast h1
  1612. have hcardR : ((2 * 4 ^ k' + 1 : ℕ) : ℝ) ≤ (intRootCount P.evalPoly : ℝ) := by exact_mod_cast hcard
  1613. have hle_a : a * (((2000 * k' * (Nat.log 2 k' + 1) * 2 ^ k' : ℕ) : ℝ) + 1) ^ c ≤ max a 0 := by
  1614. have hrpos : 0 ≤ (((2000 * k' * (Nat.log 2 k' + 1) * 2 ^ k' : ℕ) : ℝ) + 1) ^ c := by positivity
  1615. calc a * (((2000 * k' * (Nat.log 2 k' + 1) * 2 ^ k' : ℕ) : ℝ) + 1) ^ c
  1616. ≤ max a 0 * (((2000 * k' * (Nat.log 2 k' + 1) * 2 ^ k' : ℕ) : ℝ) + 1) ^ c :=
  1617. mul_le_mul_of_nonneg_right (le_max_left a 0) hrpos
  1618. _ ≤ max a 0 * 1 := mul_le_mul_of_nonneg_left hrpow_le (le_max_right a 0)
  1619. _ = max a 0 := mul_one _
  1620. linarith
  1621. · push Not at hc0
  1622. have hgap : 0 < 2 - c := by linarith
  1623. obtain ⟨M0, hM0⟩ := exists_nat_gt (1 / (2 - c))
  1624. let M := M0 + 1
  1625. have hMpos : 1 ≤ M := by omega
  1626. have hMgap : (1 : ℝ) ≤ (2 - c) * (M : ℝ) := by
  1627. have h1 : 1 / (2 - c) < (M : ℝ) := by
  1628. have : (M0 : ℝ) < (M : ℝ) := by exact_mod_cast (by omega : M0 < M)
  1629. linarith
  1630. have h2 := (div_lt_iff₀ hgap).mp h1
  1631. linarith
  1632. let B : ℝ := max a 0 * ((2001 : ℝ) * ((5 * M + 1 : ℕ) : ℝ) ^ 2) ^ 2
  1633. obtain ⟨t0, ht0⟩ := exists_nat_gt B
  1634. let t := t0 + 2
  1635. let k := 5 * M * t
  1636. have hk2 : 2 ≤ k := by
  1637. have h1 : 5 * 1 * 2 ≤ 5 * M * t :=
  1638. Nat.mul_le_mul (Nat.mul_le_mul_left 5 hMpos) (by omega)
  1639. exact le_trans (by omega : 2 ≤ 5 * 1 * 2) h1
  1640. set τ := 2000 * k * (Nat.log 2 k + 1) * 2 ^ k
  1641. rcases tau_subquadratic_growth k hk2 with ⟨P, hne, hcard⟩
  1642. have hub := ha τ P hne
  1643. have htau_nat : τ + 1 ≤ 2001 * (k + 1) ^ 2 * 2 ^ k := by
  1644. have hlog : Nat.log 2 k ≤ k := Nat.log_le_self 2 k
  1645. have h1 : τ ≤ 2000 * (k + 1) ^ 2 * 2 ^ k := by
  1646. have hkk : k * (Nat.log 2 k + 1) ≤ (k + 1) ^ 2 := by
  1647. have : k * (Nat.log 2 k + 1) ≤ (k + 1) * (k + 1) :=
  1648. Nat.mul_le_mul (by omega) (by omega)
  1649. simpa [sq] using this
  1650. calc τ = 2000 * (k * (Nat.log 2 k + 1)) * 2 ^ k := by ring
  1651. _ ≤ 2000 * (k + 1) ^ 2 * 2 ^ k :=
  1652. Nat.mul_le_mul_right (2 ^ k) (Nat.mul_le_mul_left 2000 hkk)
  1653. have h2 : 1 ≤ (k + 1) ^ 2 * 2 ^ k := by
  1654. have ha1 : 1 ≤ (k + 1) ^ 2 := by nlinarith
  1655. have hb1 : 1 ≤ 2 ^ k := Nat.one_le_two_pow
  1656. exact Nat.mul_pos ha1 hb1
  1657. linarith
  1658. have htau_R : ((τ : ℝ) + 1) ≤ ((2001 : ℝ) * ((k + 1 : ℕ) : ℝ) ^ 2) * (2 : ℝ) ^ k := by
  1659. exact_mod_cast htau_nat
  1660. have hbase_ge1 : (1 : ℝ) ≤ (2001 : ℝ) * ((k + 1 : ℕ) : ℝ) ^ 2 := by
  1661. have h1 : (1 : ℝ) ≤ ((k + 1 : ℕ) : ℝ) ^ 2 := by
  1662. exact_mod_cast (by nlinarith : 1 ≤ (k + 1) ^ 2)
  1663. nlinarith
  1664. have hrpow_bound :
  1665. ((τ : ℝ) + 1) ^ c ≤
  1666. ((2001 : ℝ) * ((k + 1 : ℕ) : ℝ) ^ 2) ^ 2 * (2 : ℝ) ^ (c * (k : ℝ)) := by
  1667. have h1 : ((τ : ℝ) + 1) ^ c ≤
  1668. (((2001 : ℝ) * ((k + 1 : ℕ) : ℝ) ^ 2) * (2 : ℝ) ^ k) ^ c :=
  1669. Real.rpow_le_rpow (by positivity) htau_R (le_of_lt hc0)
  1670. rw [Real.mul_rpow (by positivity) (by positivity)] at h1
  1671. have h2 : ((2001 : ℝ) * ((k + 1 : ℕ) : ℝ) ^ 2) ^ c ≤
  1672. ((2001 : ℝ) * ((k + 1 : ℕ) : ℝ) ^ 2) ^ (2 : ℝ) :=
  1673. Real.rpow_le_rpow_of_exponent_le hbase_ge1 (le_of_lt hc)
  1674. have h2' : ((2001 : ℝ) * ((k + 1 : ℕ) : ℝ) ^ 2) ^ (2 : ℝ) =
  1675. ((2001 : ℝ) * ((k + 1 : ℕ) : ℝ) ^ 2) ^ 2 := by norm_cast
  1676. have h3 : ((2 : ℝ) ^ k) ^ c = (2 : ℝ) ^ (c * (k : ℝ)) := by
  1677. rw [← Real.rpow_natCast (2 : ℝ) k, ← Real.rpow_mul (by norm_num : (0 : ℝ) ≤ 2), mul_comm]
  1678. rw [h2'] at h2
  1679. rw [h3] at h1
  1680. exact h1.trans (mul_le_mul_of_nonneg_right h2 (by positivity))
  1681. have hk_le : ((k + 1 : ℕ) : ℝ) ≤ ((5 * M + 1 : ℕ) : ℝ) * (t : ℝ) := by
  1682. have : k + 1 ≤ (5 * M + 1) * t := by
  1683. dsimp only [k]
  1684. have : 1 ≤ t := by omega
  1685. nlinarith
  1686. exact_mod_cast this
  1687. have hpoly_bound :
  1688. max a 0 * ((2001 : ℝ) * ((k + 1 : ℕ) : ℝ) ^ 2) ^ 2 ≤ B * (t : ℝ) ^ 4 := by
  1689. have hsq : ((2001 : ℝ) * ((k + 1 : ℕ) : ℝ) ^ 2) ^ 2 ≤
  1690. ((2001 : ℝ) * (((5 * M + 1 : ℕ) : ℝ) * (t : ℝ)) ^ 2) ^ 2 := by
  1691. gcongr
  1692. calc max a 0 * ((2001 : ℝ) * ((k + 1 : ℕ) : ℝ) ^ 2) ^ 2
  1693. ≤ max a 0 * ((2001 : ℝ) * (((5 * M + 1 : ℕ) : ℝ) * (t : ℝ)) ^ 2) ^ 2 :=
  1694. mul_le_mul_of_nonneg_left hsq (le_max_right a 0)
  1695. _ = B * (t : ℝ) ^ 4 := by dsimp only [B]; ring
  1696. have h4eq : ((2 * 4 ^ k + 1 : ℕ) : ℝ) > (2 : ℝ) ^ (2 * k) := by
  1697. have h1 : 2 ^ (2 * k) < 2 * 4 ^ k + 1 := by
  1698. have hpow : 2 ^ (2 * k) = 4 ^ k := by rw [pow_mul]; norm_num
  1699. omega
  1700. exact_mod_cast h1
  1701. have hcardR : ((2 * 4 ^ k + 1 : ℕ) : ℝ) ≤ (intRootCount P.evalPoly : ℝ) := by exact_mod_cast hcard
  1702. have hLHS : (2 : ℝ) ^ ((2 : ℝ) * (k : ℝ)) < (intRootCount P.evalPoly : ℝ) := by
  1703. have h2k : (2 : ℝ) ^ ((2 : ℝ) * (k : ℝ)) = (2 : ℝ) ^ (2 * k) := by
  1704. rw [← Real.rpow_natCast (2 : ℝ) (2 * k)]
  1705. congr 1; push_cast; ring
  1706. rw [h2k]
  1707. linarith
  1708. have hRHS : (intRootCount P.evalPoly : ℝ) ≤ B * (t : ℝ) ^ 4 * (2 : ℝ) ^ (c * (k : ℝ)) := by
  1709. calc (intRootCount P.evalPoly : ℝ)
  1710. ≤ a * ((τ : ℝ) + 1) ^ c := hub
  1711. _ ≤ max a 0 * ((τ : ℝ) + 1) ^ c :=
  1712. mul_le_mul_of_nonneg_right (le_max_left a 0) (by positivity)
  1713. _ ≤ max a 0 * (((2001 : ℝ) * ((k + 1 : ℕ) : ℝ) ^ 2) ^ 2 * (2 : ℝ) ^ (c * (k : ℝ))) :=
  1714. mul_le_mul_of_nonneg_left hrpow_bound (le_max_right a 0)
  1715. _ = (max a 0 * ((2001 : ℝ) * ((k + 1 : ℕ) : ℝ) ^ 2) ^ 2) * (2 : ℝ) ^ (c * (k : ℝ)) := by ring
  1716. _ ≤ (B * (t : ℝ) ^ 4) * (2 : ℝ) ^ (c * (k : ℝ)) :=
  1717. mul_le_mul_of_nonneg_right hpoly_bound (by positivity)
  1718. -- Divide out `(2 : ℝ) ^ (c * k) > 0`: gives `(2 : ℝ) ^ ((2 - c) * k) < B * t^4`
  1719. have h2split : (2 : ℝ) ^ ((2 : ℝ) * (k : ℝ)) =
  1720. (2 : ℝ) ^ ((2 - c) * (k : ℝ)) * (2 : ℝ) ^ (c * (k : ℝ)) := by
  1721. rw [← Real.rpow_add (by norm_num : (0 : ℝ) < 2)]
  1722. congr 1; ring
  1723. have h2ck_pos : (0 : ℝ) < (2 : ℝ) ^ (c * (k : ℝ)) := by positivity
  1724. have hdiv : (2 : ℝ) ^ ((2 - c) * (k : ℝ)) < B * (t : ℝ) ^ 4 := by
  1725. have hlt : (2 : ℝ) ^ ((2 - c) * (k : ℝ)) * (2 : ℝ) ^ (c * (k : ℝ)) <
  1726. (B * (t : ℝ) ^ 4) * (2 : ℝ) ^ (c * (k : ℝ)) := by
  1727. rw [← h2split]; linarith
  1728. exact lt_of_mul_lt_mul_right hlt (le_of_lt h2ck_pos)
  1729. -- Meanwhile `(2 - c) * k = (2 - c) * M * (5 * t) ≥ 5 * t`, and `2^(5 * t) = 2^t * (2^t)^4 > B * t^4`!
  1730. have hexp_ge : ((5 * t : ℕ) : ℝ) ≤ (2 - c) * (k : ℝ) := by
  1731. have hkR : (k : ℝ) = (M : ℝ) * ((5 * t : ℕ) : ℝ) := by
  1732. dsimp only [k]; push_cast; ring
  1733. rw [hkR, ← mul_assoc]
  1734. calc ((5 * t : ℕ) : ℝ) = 1 * ((5 * t : ℕ) : ℝ) := (one_mul _).symm
  1735. _ ≤ ((2 - c) * (M : ℝ)) * ((5 * t : ℕ) : ℝ) :=
  1736. mul_le_mul_of_nonneg_right hMgap (by positivity)
  1737. have h2_5t : (2 : ℝ) ^ (5 * t) ≤ (2 : ℝ) ^ ((2 - c) * (k : ℝ)) := by
  1738. rw [← Real.rpow_natCast (2 : ℝ) (5 * t)]
  1739. exact Real.rpow_le_rpow_of_exponent_le (by norm_num : (1 : ℝ) ≤ 2) hexp_ge
  1740. have ht_lt_2t : (t : ℝ) < (2 : ℝ) ^ t := by exact_mod_cast (Nat.lt_two_pow_self (n := t))
  1741. have hB_lt_2t : B < (2 : ℝ) ^ t := by
  1742. have : (t0 : ℝ) < (t : ℝ) := by exact_mod_cast (by omega : t0 < t)
  1743. linarith
  1744. have hB_nonneg : 0 ≤ B := by
  1745. dsimp only [B]
  1746. exact mul_nonneg (le_max_right a 0) (sq_nonneg _)
  1747. have hpow4 : (t : ℝ) ^ 4 ≤ ((2 : ℝ) ^ t) ^ 4 := by gcongr
  1748. have h2_5t_eq : (2 : ℝ) ^ (5 * t) = (2 : ℝ) ^ t * ((2 : ℝ) ^ t) ^ 4 := by
  1749. rw [← pow_mul, ← pow_add]; congr 1; ring
  1750. have hcontra : B * (t : ℝ) ^ 4 < (2 : ℝ) ^ (5 * t) := by
  1751. rw [h2_5t_eq]
  1752. calc B * (t : ℝ) ^ 4 ≤ B * ((2 : ℝ) ^ t) ^ 4 := mul_le_mul_of_nonneg_left hpow4 hB_nonneg
  1753. _ < (2 : ℝ) ^ t * ((2 : ℝ) ^ t) ^ 4 := mul_lt_mul_of_pos_right hB_lt_2t (by positivity)
  1754. linarith
  1755. /-! ## 6. Machine-Verified Concrete Finite Crossover (`z_max(1884) ≥ 2049 > 1884`) -/
  1756. /-- Exact middle product of length `n`:
  1757. `midProd n P Q i = ∑ r < n, P r * Q (i + n - 1 - r)` for `i < n` (reading `P` on `0 .. n-1`
  1758. and `Q` on `0 .. 2*n - 2`). -/
  1759. noncomputable def midProd (n : ℕ) (P Q : ℕ → Polynomial ℤ) (i : ℕ) : Polynomial ℤ :=
  1760. ∑ r ∈ Finset.range n, P r * Q (i + n - 1 - r)
  1761. /-- Exact gate cost of power-of-2 Karatsuba middle product with base case `k = 2` (`n = 4`):
  1762. `midCost 2 = 28`, `midCost (k + 1) = 3 * midCost k + 7 * 2^k - 2`.
  1763. At `k = 5` (`n = 32`): `midCost 5 = 1262`. -/
  1764. def midCost : ℕ → ℕ
  1765. | 0 => 1
  1766. | 1 => 6
  1767. | 2 => 28
  1768. | k + 1 => 3 * midCost k + 7 * 2 ^ k - 2
  1769. theorem midProd_even_lo (h : ℕ) (P Q : ℕ → Polynomial ℤ) (i : ℕ) :
  1770. let P0 := fun r => P r
  1771. let P1 := fun r => P (r + h)
  1772. let Psum := fun r => P0 r + P1 r
  1773. let Qlo := fun s => Q s
  1774. let Qmid := fun s => Q (s + h)
  1775. let Q1 := fun s => Qlo s - Qmid s
  1776. midProd (2 * h) P Q i = midProd h Psum Qmid i + midProd h P1 Q1 i := by
  1777. intro P0 P1 Psum Qlo Qmid Q1
  1778. unfold midProd
  1779. rw [show 2 * h = h + h by ring, Finset.sum_range_add]
  1780. simp only [← Finset.sum_add_distrib]
  1781. refine Finset.sum_congr rfl ?_
  1782. intro r hr
  1783. rw [Finset.mem_range] at hr
  1784. have h1 : i + (h + h) - 1 - r = i + h - 1 - r + h := by omega
  1785. have h2 : i + (h + h) - 1 - (h + r) = i + h - 1 - r := by omega
  1786. have h3 : h + r = r + h := by omega
  1787. dsimp only [Psum, P0, P1, Qmid, Q1, Qlo]
  1788. rw [h1, h2, h3]
  1789. ring
  1790. theorem midProd_even_hi (h : ℕ) (P Q : ℕ → Polynomial ℤ) (i : ℕ) :
  1791. let P0 := fun r => P r
  1792. let P1 := fun r => P (r + h)
  1793. let Psum := fun r => P0 r + P1 r
  1794. let Qmid := fun s => Q (s + h)
  1795. let Qhi := fun s => Q (s + 2 * h)
  1796. let Q2 := fun s => Qhi s - Qmid s
  1797. midProd (2 * h) P Q (i + h) = midProd h Psum Qmid i + midProd h P0 Q2 i := by
  1798. intro P0 P1 Psum Qmid Qhi Q2
  1799. unfold midProd
  1800. rw [show 2 * h = h + h by ring, Finset.sum_range_add]
  1801. simp only [← Finset.sum_add_distrib]
  1802. refine Finset.sum_congr rfl ?_
  1803. intro r hr
  1804. rw [Finset.mem_range] at hr
  1805. have h1 : i + h + (h + h) - 1 - r = i + h - 1 - r + 2 * h := by omega
  1806. have h2 : i + h + (h + h) - 1 - (h + r) = i + h - 1 - r + h := by omega
  1807. have h3 : h + r = r + h := by omega
  1808. dsimp only [Psum, P0, P1, Qmid, Q2, Qhi]
  1809. rw [h1, h2, h3]
  1810. ring
  1811. /-- Helper: sum of `4` given wires `f 0 + f 1 + f 2 + f 3` in `3` gates (no `0` wire needed!). -/
  1812. theorem canBuild_sum4 (a b c d : Polynomial ℤ) :
  1813. CanBuild ({a, b, c, d} : Set (Polynomial ℤ)) 3 {a + b + c + d} := by
  1814. have s1 := canBuild_mono_in (S₂ := {a, b, c, d})
  1815. (by intro x hx; rcases hx with rfl | rfl <;> simp) (canBuild_add a b)
  1816. have s2 := canBuild_trans s1 (by intro x hx; rcases hx with rfl | rfl <;> simp)
  1817. (canBuild_add (a + b) c)
  1818. have s3 := canBuild_trans s2 (by intro x hx; rcases hx with rfl | rfl <;> simp)
  1819. (canBuild_add (a + b + c) d)
  1820. exact canBuild_mono_out (fun x hx => Or.inr hx) s3
  1821. /-- Base case `k = 2` (`n = 4`): schoolbook `4 × 4` middle product in `28 = 4 * 7` gates. -/
  1822. theorem canBuild_midProd_four (P Q : ℕ → Polynomial ℤ) :
  1823. CanBuild ((⋃ r < 4, {P r}) ∪ (⋃ s < 7, {Q s})) 28 (⋃ i < 4, {midProd 4 P Q i}) := by
  1824. refine canBuild_fin_union (n := 4) (d := 7) (T := fun i => {midProd 4 P Q i}) ?_
  1825. intro i hi
  1826. have h4m :
  1827. CanBuild ((⋃ r < 4, {P r}) ∪ (⋃ s < 7, {Q s})) 4
  1828. (⋃ r < 4, {P r * Q (i + 3 - r)}) := by
  1829. simpa using canBuild_fin_union (n := 4) (d := 1)
  1830. (S := (⋃ r < 4, {P r}) ∪ (⋃ s < 7, {Q s}))
  1831. (T := fun r => {P r * Q (i + 3 - r)}) (by
  1832. intro r hr
  1833. refine canBuild_mono_in ?_ (canBuild_mul (P r) (Q (i + 3 - r)))
  1834. intro x hx; rcases hx with rfl | rfl
  1835. · left; simp only [Set.mem_iUnion, exists_prop]; exact ⟨r, hr, rfl⟩
  1836. · right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨i + 3 - r, by omega, rfl⟩)
  1837. have hs := canBuild_sum4 (P 0 * Q (i + 3 - 0)) (P 1 * Q (i + 3 - 1))
  1838. (P 2 * Q (i + 3 - 2)) (P 3 * Q (i + 3 - 3))
  1839. have hcomb := canBuild_trans h4m (by
  1840. intro x hx
  1841. rcases hx with rfl | rfl | rfl | rfl <;>
  1842. · right; simp only [Set.mem_iUnion, exists_prop]
  1843. first | exact ⟨0, by omega, rfl⟩ | exact ⟨1, by omega, rfl⟩
  1844. | exact ⟨2, by omega, rfl⟩ | exact ⟨3, by omega, rfl⟩) hs
  1845. refine canBuild_mono_out ?_ hcomb
  1846. intro x hx; rw [Set.mem_singleton_iff] at hx; subst hx
  1847. right; rw [Set.mem_singleton_iff]
  1848. simp [midProd, Finset.sum_range_succ, add_assoc]
  1849. /-- **Power-of-2 Karatsuba Middle Product (`CanBuild`) for `k ≥ 2`:**
  1850. Computes `{midProd (2^k) P Q i | i < 2^k}` in `midCost k` gates (`midCost 5 = 1262`). -/
  1851. theorem canBuild_karaMid :
  1852. ∀ (k : ℕ), 2 ≤ k →
  1853. ∀ (P Q : ℕ → Polynomial ℤ),
  1854. CanBuild
  1855. ((⋃ r < 2 ^ k, {P r}) ∪ (⋃ s < 2 * 2 ^ k - 1, {Q s}))
  1856. (midCost k)
  1857. (⋃ i < 2 ^ k, {midProd (2 ^ k) P Q i})
  1858. | 0, hk, _, _ => by omega
  1859. | 1, hk, _, _ => by omega
  1860. | 2, _, P, Q => by
  1861. simpa [midCost] using canBuild_midProd_four P Q
  1862. | k + 3, _, P, Q => by
  1863. set h := 2 ^ (k + 2)
  1864. have hh : 2 ^ (k + 3) = 2 * h := by simp [h, pow_succ, mul_comm]
  1865. have hpos : 1 ≤ h := Nat.one_le_two_pow
  1866. let P0 := fun r => P r
  1867. let P1 := fun r => P (r + h)
  1868. let Psum := fun r => P0 r + P1 r
  1869. let Qlo := fun s => Q s
  1870. let Qmid := fun s => Q (s + h)
  1871. let Qhi := fun s => Q (s + 2 * h)
  1872. let Q1 := fun s => Qlo s - Qmid s
  1873. let Q2 := fun s => Qhi s - Qmid s
  1874. let M0 := midProd h Psum Qmid
  1875. let M1 := midProd h P1 Q1
  1876. let M2 := midProd h P0 Q2
  1877. set S0 : Set (Polynomial ℤ) :=
  1878. (⋃ r < 2 ^ (k + 3), {P r}) ∪ (⋃ s < 2 * 2 ^ (k + 3) - 1, {Q s})
  1879. -- Step 1 (`h` gates): Psum
  1880. have hPsum : CanBuild S0 h (⋃ r < h, {Psum r}) := by
  1881. simpa using canBuild_fin_union (n := h) (d := 1) (S := S0) (T := fun r => {Psum r}) (by
  1882. intro r hr
  1883. refine canBuild_mono_in ?_ (canBuild_add (P0 r) (P1 r))
  1884. intro x hx; rcases hx with rfl | rfl
  1885. · left; simp only [Set.mem_iUnion, exists_prop]; exact ⟨r, by omega, rfl⟩
  1886. · left; simp only [Set.mem_iUnion, exists_prop]; exact ⟨r + h, by omega, rfl⟩)
  1887. -- Step 2 (`2*h - 1` gates): Q1
  1888. have hQ1 : CanBuild S0 (2 * h - 1) (⋃ s < 2 * h - 1, {Q1 s}) := by
  1889. simpa using canBuild_fin_union (n := 2 * h - 1) (d := 1) (S := S0) (T := fun s => {Q1 s}) (by
  1890. intro s hs
  1891. refine canBuild_mono_in ?_ (canBuild_sub (Qlo s) (Qmid s))
  1892. intro x hx; rcases hx with rfl | rfl
  1893. · right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨s, by omega, rfl⟩
  1894. · right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨s + h, by omega, rfl⟩)
  1895. -- Step 3 (`2*h - 1` gates): Q2
  1896. have hQ2 : CanBuild S0 (2 * h - 1) (⋃ s < 2 * h - 1, {Q2 s}) := by
  1897. simpa using canBuild_fin_union (n := 2 * h - 1) (d := 1) (S := S0) (T := fun s => {Q2 s}) (by
  1898. intro s hs
  1899. refine canBuild_mono_in ?_ (canBuild_sub (Qhi s) (Qmid s))
  1900. intro x hx; rcases hx with rfl | rfl
  1901. · right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨s + 2 * h, by omega, rfl⟩
  1902. · right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨s + h, by omega, rfl⟩)
  1903. -- Step 4 (`3 * midCost (k + 2)` gates): M0, M1, M2
  1904. have hM0 := canBuild_karaMid (k + 2) (by omega) Psum Qmid
  1905. have hM1 := canBuild_karaMid (k + 2) (by omega) P1 Q1
  1906. have hM2 := canBuild_karaMid (k + 2) (by omega) P0 Q2
  1907. have c1 := canBuild_trans hPsum Set.subset_union_left hQ1
  1908. have c2 := canBuild_trans c1 Set.subset_union_left hQ2
  1909. have c3 := canBuild_trans c2 (by
  1910. intro x hx; rcases hx with hxP | hxQ
  1911. · right; left; left; exact hxP
  1912. · left; right; simp only [Set.mem_iUnion, exists_prop] at hxQ ⊢
  1913. rcases hxQ with ⟨s, hs, hxs⟩; exact ⟨s + h, by omega, hxs⟩) hM0
  1914. have c4 := canBuild_trans c3 (by
  1915. intro x hx; rcases hx with hxP | hxQ
  1916. · left; left; simp only [Set.mem_iUnion, exists_prop] at hxP ⊢
  1917. rcases hxP with ⟨r, hr, hxr⟩; exact ⟨r + h, by omega, hxr⟩
  1918. · right; left; left; right; exact hxQ) hM1
  1919. have c5 := canBuild_trans c4 (by
  1920. intro x hx; rcases hx with hxP | hxQ
  1921. · left; left; simp only [Set.mem_iUnion, exists_prop] at hxP ⊢
  1922. rcases hxP with ⟨r, hr, hxr⟩; exact ⟨r, by omega, hxr⟩
  1923. · right; left; left; right; exact hxQ) hM2
  1924. -- Step 5 (`2 * h` gates): combine M0 + M1 and M0 + M2
  1925. have hComb1 :
  1926. CanBuild ((⋃ i < h, {M0 i}) ∪ (⋃ i < h, {M1 i})) h
  1927. (⋃ i < h, {M0 i + M1 i}) := by
  1928. simpa using canBuild_fin_union (n := h) (d := 1) (T := fun i => {M0 i + M1 i}) (by
  1929. intro i hi
  1930. refine canBuild_mono_in ?_ (canBuild_add (M0 i) (M1 i))
  1931. intro x hx; rcases hx with rfl | rfl
  1932. · left; simp only [Set.mem_iUnion, exists_prop]; exact ⟨i, hi, rfl⟩
  1933. · right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨i, hi, rfl⟩)
  1934. have hComb2 :
  1935. CanBuild ((⋃ i < h, {M0 i}) ∪ (⋃ i < h, {M2 i})) h
  1936. (⋃ i < h, {M0 i + M2 i}) := by
  1937. simpa using canBuild_fin_union (n := h) (d := 1) (T := fun i => {M0 i + M2 i}) (by
  1938. intro i hi
  1939. refine canBuild_mono_in ?_ (canBuild_add (M0 i) (M2 i))
  1940. intro x hx; rcases hx with rfl | rfl
  1941. · left; simp only [Set.mem_iUnion, exists_prop]; exact ⟨i, hi, rfl⟩
  1942. · right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨i, hi, rfl⟩)
  1943. have c6 := canBuild_trans c5 (by
  1944. intro x hx; rcases hx with hx0 | hx1
  1945. · right; left; left; right; exact hx0
  1946. · right; left; right; exact hx1) hComb1
  1947. have c7 := canBuild_trans c6 (by
  1948. intro x hx; rcases hx with hx0 | hx2
  1949. · right; left; left; left; right; exact hx0
  1950. · right; left; right; exact hx2) hComb2
  1951. have hcost :
  1952. h + (2 * h - 1) + (2 * h - 1) + midCost (k + 2) + midCost (k + 2) + midCost (k + 2) +
  1953. h + h = midCost (k + 3) := by
  1954. change _ = 3 * midCost (k + 2) + 7 * 2 ^ (k + 2) - 2
  1955. omega
  1956. rw [hcost] at c7
  1957. refine canBuild_mono_out ?_ c7
  1958. intro x hx
  1959. simp only [Set.mem_iUnion, exists_prop, Set.mem_singleton_iff] at hx
  1960. rcases hx with ⟨i, hi, rfl⟩
  1961. by_cases hih : i < h
  1962. · rw [hh, midProd_even_lo h P Q i]
  1963. left; right
  1964. simp only [Set.mem_iUnion, exists_prop]
  1965. exact ⟨i, hih, rfl⟩
  1966. · have hi_eq : i = (i - h) + h := by omega
  1967. rw [hh, hi_eq, midProd_even_hi h P Q (i - h)]
  1968. right
  1969. simp only [Set.mem_iUnion, exists_prop]
  1970. exact ⟨i - h, by omega, rfl⟩
  1971. /-- **Machine-Verified Concrete Finite Crossover (`z_max(1884) ≥ 2049 > 1884`):**
  1972. There exists a constant-free straight-line program of length `1884` over `ℤ[X]` computing a
  1973. nonzero polynomial with at least `2049` distinct integer roots (`2049 > 1884`, proving
  1974. strictly more distinct integer roots than gates at an explicit finite length). -/
  1975. theorem tau_crossover_1884_2049 : zMaxGe 1884 2049 := by
  1976. -- Use m = 32 = 2^5: `chirp_middle_product` = `midProd 32 (chirpA 32) (fun s => chirpB 32 (s + 1)) i + chirpA 32 32 * chirpB 32 i`
  1977. have hmid := canBuild_karaMid 5 (by omega) (chirpA 32) (fun s => chirpB 32 (s + 1))
  1978. set S0 : Set (Polynomial ℤ) :=
  1979. {(0 : Polynomial ℤ)} ∪ (⋃ r < 32 + 1, {chirpA 32 r}) ∪ (⋃ s < 2 * 32, {chirpB 32 s})
  1980. have h1 : CanBuild S0 (midCost 5) (⋃ i < 32, {midProd 32 (chirpA 32) (fun s => chirpB 32 (s + 1)) i}) := by
  1981. refine canBuild_mono_in ?_ hmid
  1982. intro x hx; rcases hx with hxA | hxB
  1983. · left; right; simp only [Set.mem_iUnion, exists_prop] at hxA ⊢
  1984. rcases hxA with ⟨r, hr, hxr⟩; exact ⟨r, by omega, hxr⟩
  1985. · right; simp only [Set.mem_iUnion, exists_prop] at hxB ⊢
  1986. rcases hxB with ⟨s, hs, hxs⟩; exact ⟨s + 1, by omega, hxs⟩
  1987. -- Add the `r = 32` term `chirpA 32 32 * chirpB 32 i` (`2 * 32 = 64` gates)
  1988. have h2 :
  1989. CanBuild
  1990. (S0 ∪ ⋃ i < 32, {midProd 32 (chirpA 32) (fun s => chirpB 32 (s + 1)) i})
  1991. (32 * 2)
  1992. (⋃ i < 32, {Polynomial.C (1 : ℤ) *
  1993. ∑ r ∈ Finset.range (32 + 1), chirpA 32 r * chirpB 32 (i + 32 - r)}) := by
  1994. refine canBuild_fin_union (n := 32) (d := 2)
  1995. (T := fun i => {Polynomial.C (1 : ℤ) *
  1996. ∑ r ∈ Finset.range (32 + 1), chirpA 32 r * chirpB 32 (i + 32 - r)}) ?_
  1997. intro i hi
  1998. have hm := canBuild_mul (chirpA 32 32) (chirpB 32 i)
  1999. have ha := canBuild_add (midProd 32 (chirpA 32) (fun s => chirpB 32 (s + 1)) i)
  2000. (chirpA 32 32 * chirpB 32 i)
  2001. have hma := canBuild_trans
  2002. (canBuild_mono_in (S₂ := {midProd 32 (chirpA 32) (fun s => chirpB 32 (s + 1)) i,
  2003. chirpA 32 32, chirpB 32 i}) (by intro x hx; rcases hx with rfl | rfl <;> simp) hm)
  2004. (by intro x hx; rcases hx with rfl | rfl <;> simp) ha
  2005. have heq :
  2006. midProd 32 (chirpA 32) (fun s => chirpB 32 (s + 1)) i + chirpA 32 32 * chirpB 32 i =
  2007. Polynomial.C (1 : ℤ) *
  2008. ∑ r ∈ Finset.range (32 + 1), chirpA 32 r * chirpB 32 (i + 32 - r) := by
  2009. rw [map_one, one_mul, Finset.sum_range_succ, show i + 32 - 32 = i by omega]
  2010. have hsum : midProd 32 (chirpA 32) (fun s => chirpB 32 (s + 1)) i =
  2011. ∑ r ∈ Finset.range 32, chirpA 32 r * chirpB 32 (i + 32 - r) := by
  2012. unfold midProd
  2013. refine Finset.sum_congr rfl ?_
  2014. intro r hr
  2015. rw [Finset.mem_range] at hr
  2016. have hidx : i + 32 - 1 - r + 1 = i + 32 - r := by omega
  2017. dsimp only
  2018. rw [hidx]
  2019. rw [hsum]
  2020. refine canBuild_mono_out
  2021. (T₂ := {chirpA 32 32 * chirpB 32 i} ∪
  2022. {midProd 32 (chirpA 32) (fun s => chirpB 32 (s + 1)) i + chirpA 32 32 * chirpB 32 i})
  2023. (by intro x hx; rw [Set.mem_singleton_iff] at hx; subst hx; right; rw [Set.mem_singleton_iff, heq])
  2024. (canBuild_mono_in ?_ hma)
  2025. intro x hx
  2026. rcases hx with rfl | rfl | rfl
  2027. · right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨i, hi, rfl⟩
  2028. · left; left; right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨32, by omega, rfl⟩
  2029. · left; right; simp only [Set.mem_iUnion, exists_prop]; exact ⟨i, by omega, rfl⟩
  2030. have hstep := canBuild_mono_out Set.subset_union_right (canBuild_seq h1 h2)
  2031. have hz := zMaxGe_of_chirp_engine (m := 32) (by omega) 1 (by omega) hstep
  2032. have hcost : 17 * 32 + (midCost 5 + 32 * 2) + 14 = 1884 := by decide
  2033. have hroots : 2 * (32 * 32) + 1 = 2049 := by decide
  2034. rwa [hcost, hroots] at hz
  2035. /-- Existential witness that constant-free SLPs over `ℤ[X]` strictly beat `z_max(τ) ≤ τ`. -/
  2036. theorem tau_exists_roots_gt_gates : ∃ τ r : ℕ, τ < r ∧ zMaxGe τ r :=
  2037. ⟨1884, 2049, by omega, tau_crossover_1884_2049⟩
  2038. end Research.PvsNP.Tau
  2039. end