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

Research/PvsNP/Tau/WellSpacedSLPs.lean

1,634 lines47 declarationsno sorry
  1. module
  2. public import Research.PvsNP.Tau.GapDominanceBound
  3. public import Research.PvsNP.Tau.AddComplexityBound
  4. @[expose] public section
  5. namespace Research.PvsNP.Tau
  6. /-!
  7. # Well-Spaced General `{+, -, ×}` Straight-Line Programs over `ℤ`
  8. This module formalizes the **Well-Spaced General `{+, -, ×}` SLP Theorem** over `ℤ`:
  9. for any constant-free DAG `P : SLP τ` (`P.evalPoly ≠ 0`) admitting a list `L` of well-spaced
  10. untapped top-degree pure-squaring segments `[a_0, b_0), ..., [a_{q-1}, b_{q-1})` (gaps `R_r ≤ 1`,
  11. `g_0 ≥ 2 a_0 + 6`, `g_r ≥ 5` for `r ≥ 1`, and each isolated trailing gate `b_r` reading the top
  12. wire `b_r + 1`), we prove:
  13. 1. **`slp_well_spaced_mul_bound`** (Sharp Multiplication-Only Exponential Bound):
  14. ```
  15. intRootCount P.evalPoly ≤ 3 * 2 ^ (a_0 + nonSqMulCount P L L.length)
  16. ```
  17. In particular, well-spaced additions, subtractions, and pure squarings are **completely free**:
  18. only isolated non-squaring multiplications `U_r * w_u` (`u < a_r + 2`) can double the bound!
  19. 2. **`slp_well_spaced_add_only_le_three`**:
  20. When `a_0 = 0` and there are no isolated non-squaring multiplications (`nonSqMulCount = 0`),
  21. every nonzero well-spaced `{+, -}`-plus-squaring DAG (with **arbitrarily many** additions,
  22. subtractions, and DAG taps to earlier wires) satisfies
  23. ```
  24. intRootCount P.evalPoly ≤ 3.
  25. ```
  26. 3. **`slp_well_spaced_intRootCount_le`**:
  27. When `a_0 = 0`, every nonzero well-spaced general `{+, -, ×}` SLP satisfies
  28. ```
  29. intRootCount P.evalPoly ≤ 2 ^ (P.nonTopSqCount + 1),
  30. ```
  31. beating Khovanskii's `2 ^ O(|B| ^ 2)` to single-exponential `2 ^ (|B| + 1)` over `ℤ`.
  32. -/
  33. open Polynomial
  34. /-! ## 1. Elementary Helpers (Cauchy Bounds, Prefix Transfers, & Geometric Sum) -/
  35. private lemma nat_half_sq_ge_ws {u v : ℕ} (hu : 4 ≤ u) (huv : u ≤ 2 * v) : u ≤ v ^ 2 := by
  36. nlinarith
  37. private lemma polyL1At_eq_sum_range_ws (F : Polynomial (Polynomial ℤ)) (x : ℤ) {N : ℕ}
  38. (hN : ∀ j ∈ F.support, j < N) :
  39. polyL1At F x = ∑ j ∈ Finset.range N, ((F.coeff j).eval x).natAbs := by
  40. unfold polyL1At
  41. apply Finset.sum_subset (fun j hj => Finset.mem_range.mpr (hN j hj))
  42. intro j _ hnot
  43. have hc : F.coeff j = 0 := Polynomial.notMem_support_iff.mp hnot
  44. simp [hc]
  45. private lemma polyIntL1_eq_sum_range_ws (f : Polynomial ℤ) {N : ℕ}
  46. (hN : ∀ j ∈ f.support, j < N) :
  47. polyIntL1 f = ∑ j ∈ Finset.range N, (f.coeff j).natAbs := by
  48. unfold polyIntL1
  49. apply Finset.sum_subset (fun j hj => Finset.mem_range.mpr (hN j hj))
  50. intro j _ hnot
  51. have hc : f.coeff j = 0 := Polynomial.notMem_support_iff.mp hnot
  52. simp [hc]
  53. private lemma eval_polyT_eq_ws (F : Polynomial (Polynomial ℤ)) (u : Polynomial ℤ) (x : ℤ) :
  54. (F.eval u).eval x = ∑ j ∈ Finset.range (F.natDegree + 1), (F.coeff j).eval x * (u.eval x) ^ j := by
  55. rw [Polynomial.eval_eq_sum_range (p := F), ← Polynomial.coe_evalRingHom, map_sum]
  56. apply Finset.sum_congr rfl; intro j _
  57. simp
  58. private lemma lower_terms_bound_ws (F : Polynomial (Polynomial ℤ)) (x U : ℤ)
  59. (hD : 1 ≤ F.natDegree) (hU : 1 ≤ U.natAbs) :
  60. (∑ j ∈ Finset.range F.natDegree, (F.coeff j).eval x * U ^ j).natAbs ≤
  61. U.natAbs ^ (F.natDegree - 1) * polyL1At F x := by
  62. have htri : ∀ (s : Finset ℕ) (g : ℕ → ℤ),
  63. (∑ j ∈ s, g j).natAbs ≤ ∑ j ∈ s, (g j).natAbs := by
  64. intro s g
  65. induction s using Finset.induction_on with
  66. | empty => simp
  67. | @insert a s' ha ih =>
  68. rw [Finset.sum_insert ha, Finset.sum_insert ha]
  69. exact le_trans (Int.natAbs_add_le _ _) (Nat.add_le_add_left ih _)
  70. have hsum_le : ∑ j ∈ Finset.range F.natDegree, ((F.coeff j).eval x).natAbs ≤ polyL1At F x := by
  71. rw [polyL1At_eq_sum_range_ws F x (N := F.natDegree + 1)
  72. (fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega)]
  73. rw [Finset.sum_range_succ]
  74. omega
  75. calc (∑ j ∈ Finset.range F.natDegree, (F.coeff j).eval x * U ^ j).natAbs
  76. ≤ ∑ j ∈ Finset.range F.natDegree, ((F.coeff j).eval x * U ^ j).natAbs := htri _ _
  77. _ = ∑ j ∈ Finset.range F.natDegree, ((F.coeff j).eval x).natAbs * U.natAbs ^ j := by
  78. apply Finset.sum_congr rfl; intro j _
  79. rw [Int.natAbs_mul, Int.natAbs_pow]
  80. _ ≤ ∑ j ∈ Finset.range F.natDegree, ((F.coeff j).eval x).natAbs * U.natAbs ^ (F.natDegree - 1) := by
  81. apply Finset.sum_le_sum; intro j hj
  82. have hlt : j < F.natDegree := Finset.mem_range.mp hj
  83. exact Nat.mul_le_mul_left _ (Nat.pow_le_pow_right hU (by omega))
  84. _ = (∑ j ∈ Finset.range F.natDegree, ((F.coeff j).eval x).natAbs) * U.natAbs ^ (F.natDegree - 1) :=
  85. (Finset.sum_mul _ _ _).symm
  86. _ ≤ polyL1At F x * U.natAbs ^ (F.natDegree - 1) := Nat.mul_le_mul_right _ hsum_le
  87. _ = U.natAbs ^ (F.natDegree - 1) * polyL1At F x := Nat.mul_comm _ _
  88. private lemma cauchy_root_bound_polyT_ws (F : Polynomial (Polynomial ℤ)) (u : Polynomial ℤ) (x : ℤ)
  89. (hD : 1 ≤ F.natDegree) (hlead : F.leadingCoeff.eval x ≠ 0) (hzero : (F.eval u).eval x = 0) :
  90. (u.eval x).natAbs ≤ polyL1At F x := by
  91. by_cases hU0 : (u.eval x).natAbs = 0
  92. · omega
  93. · have hU1 : 1 ≤ (u.eval x).natAbs := by omega
  94. have hev := eval_polyT_eq_ws F u x
  95. rw [hzero, Finset.sum_range_succ] at hev
  96. set low := ∑ j ∈ Finset.range F.natDegree, (F.coeff j).eval x * (u.eval x) ^ j
  97. set topTerm := (F.coeff F.natDegree).eval x * (u.eval x) ^ F.natDegree
  98. have htop_eq : topTerm = -low := by linarith
  99. have hlead_pos : 1 ≤ ((F.coeff F.natDegree).eval x).natAbs :=
  100. Int.natAbs_pos.mpr hlead
  101. have htop_ge : (u.eval x).natAbs ^ F.natDegree ≤ topTerm.natAbs := by
  102. calc (u.eval x).natAbs ^ F.natDegree
  103. = 1 * (u.eval x).natAbs ^ F.natDegree := (Nat.one_mul _).symm
  104. _ ≤ ((F.coeff F.natDegree).eval x).natAbs * (u.eval x).natAbs ^ F.natDegree :=
  105. Nat.mul_le_mul_right _ hlead_pos
  106. _ = topTerm.natAbs := by dsimp only [topTerm]; rw [Int.natAbs_mul, Int.natAbs_pow]
  107. have hlow : low.natAbs ≤ (u.eval x).natAbs ^ (F.natDegree - 1) * polyL1At F x :=
  108. lower_terms_bound_ws F x (u.eval x) hD hU1
  109. have htop_le : topTerm.natAbs ≤ (u.eval x).natAbs ^ (F.natDegree - 1) * polyL1At F x := by
  110. rw [htop_eq, Int.natAbs_neg]; exact hlow
  111. have hpow_split : (u.eval x).natAbs ^ F.natDegree =
  112. (u.eval x).natAbs ^ (F.natDegree - 1) * (u.eval x).natAbs := by
  113. conv_lhs => rw [← Nat.sub_add_cancel hD, pow_succ]
  114. rw [hpow_split] at htop_ge
  115. have hprod : (u.eval x).natAbs ^ (F.natDegree - 1) * (u.eval x).natAbs ≤
  116. (u.eval x).natAbs ^ (F.natDegree - 1) * polyL1At F x := le_trans htop_ge htop_le
  117. exact Nat.le_of_mul_le_mul_left hprod (Nat.pow_pos (by omega))
  118. private lemma cauchy_dominance_polyT_ws (F : Polynomial (Polynomial ℤ)) (u : Polynomial ℤ) (x : ℤ)
  119. (hD : 1 ≤ F.natDegree) (hlead : F.leadingCoeff.eval x ≠ 0)
  120. (hbig : 2 * polyL1At F x < (u.eval x).natAbs) :
  121. (u.eval x).natAbs ≤ 2 * ((F.eval u).eval x).natAbs := by
  122. have hU1 : 1 ≤ (u.eval x).natAbs := by omega
  123. have hev := eval_polyT_eq_ws F u x
  124. rw [Finset.sum_range_succ] at hev
  125. set low := ∑ j ∈ Finset.range F.natDegree, (F.coeff j).eval x * (u.eval x) ^ j
  126. set topTerm := (F.coeff F.natDegree).eval x * (u.eval x) ^ F.natDegree
  127. have hlead_pos : 1 ≤ ((F.coeff F.natDegree).eval x).natAbs :=
  128. Int.natAbs_pos.mpr hlead
  129. have htop_ge : (u.eval x).natAbs ^ F.natDegree ≤ topTerm.natAbs := by
  130. calc (u.eval x).natAbs ^ F.natDegree
  131. = 1 * (u.eval x).natAbs ^ F.natDegree := (Nat.one_mul _).symm
  132. _ ≤ ((F.coeff F.natDegree).eval x).natAbs * (u.eval x).natAbs ^ F.natDegree :=
  133. Nat.mul_le_mul_right _ hlead_pos
  134. _ = topTerm.natAbs := by dsimp only [topTerm]; rw [Int.natAbs_mul, Int.natAbs_pow]
  135. have htri : topTerm.natAbs ≤ ((F.eval u).eval x).natAbs + low.natAbs := by
  136. have hrew : topTerm = (F.eval u).eval x + (-low) := by linarith
  137. calc topTerm.natAbs = ((F.eval u).eval x + (-low)).natAbs := by rw [← hrew]
  138. _ ≤ ((F.eval u).eval x).natAbs + (-low).natAbs := Int.natAbs_add_le _ _
  139. _ = ((F.eval u).eval x).natAbs + low.natAbs := by rw [Int.natAbs_neg]
  140. have hlow := lower_terms_bound_ws F x (u.eval x) hD hU1
  141. have hpow_split : (u.eval x).natAbs ^ F.natDegree =
  142. (u.eval x).natAbs ^ (F.natDegree - 1) * (u.eval x).natAbs := by
  143. conv_lhs => rw [← Nat.sub_add_cancel hD, pow_succ]
  144. have h1 : (u.eval x).natAbs ^ (F.natDegree - 1) * (2 * polyL1At F x) ≤
  145. (u.eval x).natAbs ^ (F.natDegree - 1) * (u.eval x).natAbs :=
  146. Nat.mul_le_mul_left _ (by omega)
  147. have h2 : 2 * low.natAbs ≤ (u.eval x).natAbs ^ F.natDegree := by
  148. rw [hpow_split]; linarith
  149. have h3 : (u.eval x).natAbs ^ F.natDegree ≤ 2 * ((F.eval u).eval x).natAbs := by omega
  150. have h4 : (u.eval x).natAbs ≤ (u.eval x).natAbs ^ F.natDegree :=
  151. Nat.le_self_pow (by omega) (u.eval x).natAbs
  152. omega
  153. private lemma cauchy_dominance_univar_ws (w : Polynomial ℤ) (x : ℤ)
  154. (hD : 1 ≤ w.natDegree) (hbig : 2 * polyIntL1 w ≤ x.natAbs) :
  155. x.natAbs ≤ 2 * (w.eval x).natAbs := by
  156. have hx1 : 1 ≤ x.natAbs := by
  157. have hne : w ≠ 0 := fun h0 => by simp [h0] at hD
  158. have hpos : 1 ≤ polyIntL1 w := by
  159. rw [polyIntL1_eq_sum_range_ws w (N := w.natDegree + 1)
  160. (fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega)]
  161. rw [Finset.sum_range_succ]
  162. have hlc : 1 ≤ (w.coeff w.natDegree).natAbs :=
  163. Int.natAbs_pos.mpr (Polynomial.leadingCoeff_ne_zero.mpr hne)
  164. omega
  165. omega
  166. have hev : w.eval x = ∑ j ∈ Finset.range (w.natDegree + 1), w.coeff j * x ^ j :=
  167. Polynomial.eval_eq_sum_range x
  168. rw [Finset.sum_range_succ] at hev
  169. set low := ∑ j ∈ Finset.range w.natDegree, w.coeff j * x ^ j
  170. set top := w.coeff w.natDegree * x ^ w.natDegree
  171. have hne : w ≠ 0 := fun h0 => by simp [h0] at hD
  172. have hlead_pos : 1 ≤ (w.coeff w.natDegree).natAbs :=
  173. Int.natAbs_pos.mpr (Polynomial.leadingCoeff_ne_zero.mpr hne)
  174. have htop_ge : x.natAbs ^ w.natDegree ≤ top.natAbs := by
  175. calc x.natAbs ^ w.natDegree
  176. = 1 * x.natAbs ^ w.natDegree := (Nat.one_mul _).symm
  177. _ ≤ (w.coeff w.natDegree).natAbs * x.natAbs ^ w.natDegree := Nat.mul_le_mul_right _ hlead_pos
  178. _ = top.natAbs := by simp only [top, Int.natAbs_mul, Int.natAbs_pow]
  179. have htri : top.natAbs ≤ (w.eval x).natAbs + low.natAbs := by
  180. have hrew : top = w.eval x + (-low) := by linarith
  181. calc top.natAbs = (w.eval x + (-low)).natAbs := by rw [← hrew]
  182. _ ≤ (w.eval x).natAbs + (-low).natAbs := Int.natAbs_add_le _ _
  183. _ = (w.eval x).natAbs + low.natAbs := by rw [Int.natAbs_neg]
  184. have htri_sum : ∀ (s : Finset ℕ) (g : ℕ → ℤ),
  185. (∑ j ∈ s, g j).natAbs ≤ ∑ j ∈ s, (g j).natAbs := by
  186. intro s g
  187. induction s using Finset.induction_on with
  188. | empty => simp
  189. | @insert u s' hu ihs =>
  190. rw [Finset.sum_insert hu, Finset.sum_insert hu]
  191. exact le_trans (Int.natAbs_add_le _ _) (Nat.add_le_add_left ihs _)
  192. have hlow_sum : ∑ j ∈ Finset.range w.natDegree, (w.coeff j).natAbs ≤ polyIntL1 w := by
  193. rw [polyIntL1_eq_sum_range_ws w (N := w.natDegree + 1)
  194. (fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega)]
  195. rw [Finset.sum_range_succ]; omega
  196. have hlow : low.natAbs ≤ x.natAbs ^ (w.natDegree - 1) * polyIntL1 w := by
  197. calc low.natAbs
  198. ≤ ∑ j ∈ Finset.range w.natDegree, (w.coeff j * x ^ j).natAbs := htri_sum _ _
  199. _ = ∑ j ∈ Finset.range w.natDegree, (w.coeff j).natAbs * x.natAbs ^ j := by
  200. apply Finset.sum_congr rfl; intro j _; rw [Int.natAbs_mul, Int.natAbs_pow]
  201. _ ≤ ∑ j ∈ Finset.range w.natDegree, (w.coeff j).natAbs * x.natAbs ^ (w.natDegree - 1) := by
  202. apply Finset.sum_le_sum; intro j hj
  203. exact Nat.mul_le_mul_left _ (Nat.pow_le_pow_right hx1 (by have := Finset.mem_range.mp hj; omega))
  204. _ = (∑ j ∈ Finset.range w.natDegree, (w.coeff j).natAbs) * x.natAbs ^ (w.natDegree - 1) :=
  205. (Finset.sum_mul _ _ _).symm
  206. _ ≤ polyIntL1 w * x.natAbs ^ (w.natDegree - 1) := Nat.mul_le_mul_right _ hlow_sum
  207. _ = x.natAbs ^ (w.natDegree - 1) * polyIntL1 w := Nat.mul_comm _ _
  208. have hpow_split : x.natAbs ^ w.natDegree = x.natAbs ^ (w.natDegree - 1) * x.natAbs := by
  209. conv_lhs => rw [← Nat.sub_add_cancel hD, pow_succ]
  210. have h1 : x.natAbs ^ (w.natDegree - 1) * (2 * polyIntL1 w) ≤ x.natAbs ^ (w.natDegree - 1) * x.natAbs :=
  211. Nat.mul_le_mul_left _ hbig
  212. have h2 : 2 * low.natAbs ≤ x.natAbs ^ w.natDegree := by rw [hpow_split]; linarith
  213. have h3 : x.natAbs ^ w.natDegree ≤ 2 * (w.eval x).natAbs := by omega
  214. have h4 : x.natAbs ≤ x.natAbs ^ w.natDegree := Nat.le_self_pow (by omega) x.natAbs
  215. omega
  216. /-- Pure-`ℕ` telescoping geometric sum for filtered subsets of `range n`:
  217. `1 + ∑_{j < n, p j} 2 ^ |{i < j : p i}| = 2 ^ |{i < n : p i}|`. -/
  218. lemma one_add_sum_two_pow_filter (p : ℕ → Prop) [DecidablePred p] :
  219. ∀ n : ℕ,
  220. 1 + ∑ j ∈ (Finset.range n).filter p, 2 ^ ((Finset.range j).filter p).card =
  221. 2 ^ ((Finset.range n).filter p).card := by
  222. intro n
  223. induction n with
  224. | zero => simp
  225. | succ m ih =>
  226. rw [Finset.range_add_one, Finset.filter_insert]
  227. have hnot : m ∉ (Finset.range m).filter p := by simp
  228. split_ifs with hm
  229. · rw [Finset.sum_insert hnot, Finset.card_insert_of_notMem hnot, pow_succ]
  230. omega
  231. · exact ih
  232. /-- `intRootCount (w ^ k) = intRootCount w` for any `w : Polynomial ℤ` and `k ≥ 1`. -/
  233. lemma intRootCount_pow_eq {w : Polynomial ℤ} {k : ℕ} (hk : 1 ≤ k) :
  234. intRootCount (w ^ k) = intRootCount w := by
  235. by_cases hw : w = 0
  236. · subst hw; rw [zero_pow (by omega)]
  237. · have hwk : w ^ k ≠ 0 := pow_ne_zero k hw
  238. have hset : intRootSet (w ^ k) = intRootSet w := by
  239. ext x
  240. simp only [intRootSet, Multiset.mem_toFinset, Polynomial.mem_roots hwk,
  241. Polynomial.mem_roots hw, Polynomial.IsRoot.def, Polynomial.eval_pow]
  242. exact ⟨fun h => eq_zero_of_pow_eq_zero h, fun h => by rw [h, zero_pow (by omega)]⟩
  243. unfold intRootCount
  244. rw [hset]
  245. /-- `intRootSet (C c) = ∅` for every nonzero integer constant `c ≠ 0`. -/
  246. lemma intRootSet_C_eq_empty {c : ℤ} (hc : c ≠ 0) :
  247. intRootSet (Polynomial.C c) = ∅ := by
  248. ext x
  249. simp only [intRootSet, Multiset.mem_toFinset, Polynomial.mem_roots (Polynomial.C_ne_zero.mpr hc),
  250. Polynomial.IsRoot.def, Polynomial.eval_C, Finset.notMem_empty, iff_false]
  251. exact hc
  252. private lemma natDegree_C_sub_X (p : Polynomial ℤ) :
  253. (Polynomial.C p - Polynomial.X : Polynomial (Polynomial ℤ)).natDegree = 1 := by
  254. have heq : Polynomial.C p - Polynomial.X = Polynomial.C (-1 : Polynomial ℤ) * Polynomial.X + Polynomial.C p := by
  255. simp; ring
  256. rw [heq]
  257. have hm1_ne : (-1 : Polynomial ℤ) ≠ 0 := by intro h; have := congrArg (Polynomial.eval 0) h; simp at this
  258. have hdeg_left : (Polynomial.C (-1 : Polynomial ℤ) * Polynomial.X).natDegree = 1 :=
  259. Polynomial.natDegree_C_mul_X _ hm1_ne
  260. have hlt : (Polynomial.C p).natDegree < (Polynomial.C (-1 : Polynomial ℤ) * Polynomial.X).natDegree := by
  261. rw [Polynomial.natDegree_C, hdeg_left]; omega
  262. rw [Polynomial.natDegree_add_eq_left_of_natDegree_lt hlt, hdeg_left]
  263. private lemma leadingCoeff_C_sub_X (p : Polynomial ℤ) :
  264. (Polynomial.C p - Polynomial.X : Polynomial (Polynomial ℤ)).leadingCoeff = -1 := by
  265. rw [Polynomial.leadingCoeff, natDegree_C_sub_X, Polynomial.coeff_sub, Polynomial.coeff_C,
  266. Polynomial.coeff_X]
  267. simp
  268. /-! ## 2. Prefix Compatibility for Gates and `UntappedSeg` -/
  269. lemma SLP.prefix_prefix_eq :
  270. ∀ {τ : ℕ} (P : SLP τ) (t₁ t₂ : ℕ) (h12 : t₁ ≤ t₂) (h2 : t₂ ≤ τ),
  271. (P.prefix t₂ h2).prefix t₁ h12 = P.prefix t₁ (le_trans h12 h2)
  272. | 0, .nil, t₁, t₂, h12, h2 => by
  273. have ht1 : t₁ = 0 := by omega
  274. have ht2 : t₂ = 0 := by omega
  275. subst ht1; subst ht2; rfl
  276. | τ + 1, .snoc P op i j, t₁, t₂, h12, h2 => by
  277. by_cases ht2 : t₂ ≤ τ
  278. · rw [SLP.prefix_snoc_le P op i j ht2, SLP.prefix_snoc_le P op i j (le_trans h12 ht2)]
  279. exact P.prefix_prefix_eq t₁ t₂ h12 ht2
  280. · have heq : t₂ = τ + 1 := by omega
  281. subst heq
  282. rw [SLP.prefix_self]
  283. lemma SLP.gateOp_prefix {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t ≤ τ) (k : ℕ) (hk : k < t) :
  284. (P.prefix t ht).gateOp k hk = P.gateOp k (lt_of_lt_of_le hk ht) := by
  285. induction τ with
  286. | zero => omega
  287. | succ τ' ih =>
  288. cases P with
  289. | snoc P' op i j =>
  290. by_cases hle : t ≤ τ'
  291. · rw [SLP.prefix_snoc_le P' op i j hle]
  292. have hk' : k < τ' := lt_of_lt_of_le hk hle
  293. simp only [SLP.gateOp, hk', ↓reduceDIte]
  294. exact ih P' hle
  295. · have heq : t = τ' + 1 := by omega
  296. subst heq
  297. rw [SLP.prefix_self]
  298. lemma SLP.gateIn1_prefix_val {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t ≤ τ) (k : ℕ) (hk : k < t) :
  299. ((P.prefix t ht).gateIn1 k hk).val = (P.gateIn1 k (lt_of_lt_of_le hk ht)).val := by
  300. induction τ with
  301. | zero => omega
  302. | succ τ' ih =>
  303. cases P with
  304. | snoc P' op i j =>
  305. by_cases hle : t ≤ τ'
  306. · rw [SLP.prefix_snoc_le P' op i j hle]
  307. have hk' : k < τ' := lt_of_lt_of_le hk hle
  308. simp only [SLP.gateIn1, hk', ↓reduceDIte]
  309. exact ih P' hle
  310. · have heq : t = τ' + 1 := by omega
  311. subst heq
  312. rw [SLP.prefix_self]
  313. lemma SLP.gateIn2_prefix_val {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t ≤ τ) (k : ℕ) (hk : k < t) :
  314. ((P.prefix t ht).gateIn2 k hk).val = (P.gateIn2 k (lt_of_lt_of_le hk ht)).val := by
  315. induction τ with
  316. | zero => omega
  317. | succ τ' ih =>
  318. cases P with
  319. | snoc P' op i j =>
  320. by_cases hle : t ≤ τ'
  321. · rw [SLP.prefix_snoc_le P' op i j hle]
  322. have hk' : k < τ' := lt_of_lt_of_le hk hle
  323. simp only [SLP.gateIn2, hk', ↓reduceDIte]
  324. exact ih P' hle
  325. · have heq : t = τ' + 1 := by omega
  326. subst heq
  327. rw [SLP.prefix_self]
  328. lemma SLP.gateIsTopSq_prefix {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t ≤ τ) (k : ℕ) (hk : k < t) :
  329. (P.prefix t ht).gateIsTopSq k hk ↔ P.gateIsTopSq k (lt_of_lt_of_le hk ht) := by
  330. unfold SLP.gateIsTopSq isTopPureSquare
  331. rw [P.prefix_prefix_eq k t (le_of_lt hk) ht, P.gateOp_prefix t ht k hk]
  332. have h1 : (P.prefix t ht).gateIn1 k hk = P.gateIn1 k (lt_of_lt_of_le hk ht) :=
  333. Fin.ext (P.gateIn1_prefix_val t ht k hk)
  334. have h2 : (P.prefix t ht).gateIn2 k hk = P.gateIn2 k (lt_of_lt_of_le hk ht) :=
  335. Fin.ext (P.gateIn2_prefix_val t ht k hk)
  336. rw [h1, h2]
  337. /-- Restrict an `UntappedSeg P` with `s.b ≤ t` to the prefix `P.prefix t ht`. -/
  338. def UntappedSeg.restrictPrefix {τ : ℕ} {P : SLP τ} (s : UntappedSeg P)
  339. (t : ℕ) (ht : t ≤ τ) (hbt : s.b ≤ t) : UntappedSeg (P.prefix t ht) where
  340. a := s.a
  341. b := s.b
  342. hab := s.hab
  343. hb := hbt
  344. hrun := fun k hk h1 h2 => (P.gateIsTopSq_prefix t ht k hk).mpr (s.hrun k (lt_of_lt_of_le hk ht) h1 h2)
  345. huntapped := fun k hk hbk => by
  346. have hu := s.huntapped k (lt_of_lt_of_le hk ht) hbk
  347. rw [P.gateIn1_prefix_val t ht k hk, P.gateIn2_prefix_val t ht k hk]
  348. exact hu
  349. /-- `evalFormalT` on `P.prefix c hc` agrees with `evalFormalT` on `P`. -/
  350. lemma evalFormalT_prefix_eq {τ : ℕ} (P : SLP τ) (a b : ℕ) (hab : a < b)
  351. (c : ℕ) (hbc : b ≤ c) (hc : c ≤ τ) :
  352. ∀ (t : ℕ) (hbt : b ≤ t) (htc : t ≤ c) (w : Fin (t + 2)),
  353. evalFormalT (P.prefix c hc) a b hab hbc t hbt htc w =
  354. evalFormalT P a b hab (le_trans hbc hc) t hbt (le_trans htc hc) w := by
  355. intro t
  356. induction t using Nat.strong_induction_on with
  357. | ind t ih =>
  358. intro hbt htc w
  359. rw [evalFormalT.eq_def, evalFormalT.eq_def]
  360. split_ifs with hw hwa hwb
  361. · rw [P.evalWire_prefix c hc ⟨w.val, by omega⟩]
  362. · rfl
  363. · rfl
  364. · dsimp only
  365. set t' := w.val - 2
  366. have ht'_lt : t' < t := by omega
  367. have hbt' : b ≤ t' := by omega
  368. have ht'_lt_c : t' < c := by omega
  369. have h1_eq : (P.prefix c hc).gateIn1 t' ht'_lt_c = P.gateIn1 t' (lt_of_lt_of_le ht'_lt_c hc) :=
  370. Fin.ext (P.gateIn1_prefix_val c hc t' ht'_lt_c)
  371. have h2_eq : (P.prefix c hc).gateIn2 t' ht'_lt_c = P.gateIn2 t' (lt_of_lt_of_le ht'_lt_c hc) :=
  372. Fin.ext (P.gateIn2_prefix_val c hc t' ht'_lt_c)
  373. rw [P.gateOp_prefix c hc t' ht'_lt_c, h1_eq, h2_eq]
  374. rw [ih t' ht'_lt hbt' (le_of_lt ht'_lt_c) _, ih t' ht'_lt hbt' (le_of_lt ht'_lt_c) _]
  375. /-! ## 3. Multi-Segment Gap-Dominance Root-Set Cover -/
  376. /-- Helper: next boundary gate index after segment `r` in a list `L`. -/
  377. def segNextC {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (r : Fin L.length) : ℕ :=
  378. if h : r.val + 1 < L.length then (L[r.val + 1]'h).a else τ
  379. lemma segsOrdered_adj {τ : ℕ} {P : SLP τ} :
  380. ∀ (L : List (UntappedSeg P)), SegsOrdered L →
  381. ∀ (i : ℕ) (hi : i + 1 < L.length), (L[i]'(by omega)).b ≤ (L[i + 1]'hi).a
  382. | [], _, _, hi => by simp at hi
  383. | [_], _, _, hi => by simp at hi
  384. | s0 :: s1 :: rest, ho, 0, _ => ho.1
  385. | _ :: s1 :: rest, ho, i + 1, hi =>
  386. segsOrdered_adj (s1 :: rest) ho.2 i (by simpa using hi)
  387. lemma segNextC_ge {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (hord : SegsOrdered L)
  388. (r : Fin L.length) : (L[r.val]'r.isLt).b ≤ segNextC L r := by
  389. unfold segNextC
  390. split_ifs with h
  391. · exact segsOrdered_adj L hord r.val h
  392. · exact (L[r.val]'r.isLt).hb
  393. lemma segNextC_le {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (r : Fin L.length) :
  394. segNextC L r ≤ τ := by
  395. unfold segNextC
  396. split_ifs with h
  397. · exact le_of_lt (lt_of_lt_of_le (L[r.val + 1]'h).hab (L[r.val + 1]'h).hb)
  398. · exact le_rfl
  399. /-- Target wire after segment `r` in `L` (input to segment `r + 1`, or output wire `τ + 1`). -/
  400. def segWTarget {τ : ℕ} (P : SLP τ) (L : List (UntappedSeg P)) (r : Fin L.length) :
  401. Fin (segNextC L r + 2) :=
  402. if h : r.val + 1 < L.length then
  403. let sNext := L[r.val + 1]'h
  404. ⟨(P.gateIn1 sNext.a (by have := sNext.hab; have := sNext.hb; omega)).val, by
  405. have hlt := (P.gateIn1 sNext.a (by have := sNext.hab; have := sNext.hb; omega)).isLt
  406. unfold segNextC; rw [dite_eq_left_of_eq_true (eq_true h)]; exact hlt⟩
  407. else
  408. ⟨τ + 1, by
  409. unfold segNextC; rw [dite_eq_right_of_eq_false (eq_false h)]; omega⟩
  410. /-- Formal polynomial `F_r(x, T) ∈ (Polynomial ℤ)[X]` for segment `r` of `L`. -/
  411. noncomputable def segFormalPoly {τ : ℕ} (P : SLP τ) (L : List (UntappedSeg P)) (hord : SegsOrdered L)
  412. (r : Fin L.length) : Polynomial (Polynomial ℤ) :=
  413. evalFormalT P (L[r.val]'r.isLt).a (L[r.val]'r.isLt).b (L[r.val]'r.isLt).hab (L[r.val]'r.isLt).hb
  414. (segNextC L r) (segNextC_ge L hord r) (segNextC_le L r) (segWTarget P L r)
  415. /-- **Multi-Segment Gap-Dominance Root-Set Cover:**
  416. Given a nonempty ordered list `L` of untapped pure-squaring segments of `P : SLP τ` (`P.evalPoly ≠ 0`)
  417. satisfying the local gap inequalities, if the top segment's formal polynomial `segFormalPoly P L hord qlast`
  418. has `natDegree ≥ 1`, then **every** `segFormalPoly P L hord r` has `natDegree ≥ 1`, and every integer
  419. root of `P.evalPoly` either kills some leading coefficient `(segFormalPoly P L hord r).leadingCoeff`
  420. or satisfies `w_0(x) ∈ {-1, 0, 1}`! -/
  421. theorem slp_gap_dominance_root_cover {τ : ℕ} (P : SLP τ) (hP : P.evalPoly ≠ 0)
  422. (L : List (UntappedSeg P)) (hL_ne : L ≠ []) (hord : SegsOrdered L)
  423. (hhead_gap : ∀ (hpos : 0 < L.length),
  424. 2 * (L[0]'hpos).a + (segNextC L ⟨0, hpos⟩ - (L[0]'hpos).b) + 5 ≤
  425. (L[0]'hpos).b - (L[0]'hpos).a)
  426. (hmid_gap : ∀ (r : ℕ) (hr : r + 1 < L.length),
  427. ((L[r + 1]'hr).a - (L[r]'(by omega)).b) +
  428. (segNextC L ⟨r + 1, hr⟩ - (L[r + 1]'hr).b) + 3 ≤
  429. (L[r + 1]'hr).b - (L[r + 1]'hr).a)
  430. (htop_pos : 1 ≤ (segFormalPoly P L hord ⟨L.length - 1, List.length_pos_of_ne_nil hL_ne |> fun h => by omega⟩).natDegree) :
  431. (∀ r : Fin L.length, 1 ≤ (segFormalPoly P L hord r).natDegree) ∧
  432. let hq_pos : 0 < L.length := List.length_pos_of_ne_nil hL_ne
  433. let s0 := L[0]'hq_pos
  434. let w0 : Polynomial ℤ :=
  435. P.evalWire ⟨(P.gateIn1 s0.a (by have := s0.hab; have := s0.hb; omega)).val,
  436. by have := (P.gateIn1 s0.a (by have := s0.hab; have := s0.hb; omega)).isLt
  437. have := s0.hab; have := s0.hb; omega⟩
  438. intRootSet P.evalPoly ⊆
  439. (Finset.univ.biUnion (fun r : Fin L.length => intRootSet (segFormalPoly P L hord r).leadingCoeff)) ∪
  440. intRootSet (w0 - 1) ∪ intRootSet w0 ∪ intRootSet (w0 + 1) := by
  441. classical
  442. have hq_pos : 0 < L.length := List.length_pos_of_ne_nil hL_ne
  443. let seg : Fin L.length → UntappedSeg P := fun r => L[r.val]'r.isLt
  444. let nextC : Fin L.length → ℕ := segNextC L
  445. let wTarget : (r : Fin L.length) → Fin (nextC r + 2) := segWTarget P L
  446. let F : Fin L.length → Polynomial (Polynomial ℤ) := segFormalPoly P L hord
  447. let qlast : Fin L.length := ⟨L.length - 1, by omega⟩
  448. let s0 : UntappedSeg P := L[0]'hq_pos
  449. let w0 : Polynomial ℤ :=
  450. P.evalWire ⟨(P.gateIn1 s0.a (by have := s0.hab; have := s0.hb; omega)).val,
  451. by have := (P.gateIn1 s0.a (by have := s0.hab; have := s0.hb; omega)).isLt
  452. have := s0.hab; have := s0.hb; omega⟩
  453. have hseg_ord : ∀ (r : Fin L.length) (hr : r.val + 1 < L.length),
  454. (seg r).b ≤ (seg ⟨r.val + 1, hr⟩).a := fun r hr => segsOrdered_adj L hord r.val hr
  455. have hqlast_not : ¬ (qlast.val + 1 < L.length) := by dsimp [qlast]; omega
  456. have hqlast_spec : (F qlast).eval (P.evalWire ⟨(seg qlast).b + 1, by have := (seg qlast).hb; omega⟩) =
  457. P.evalPoly := by
  458. have hnot : ¬ ((seg qlast).a + 2 ≤ (wTarget qlast).val ∧ (wTarget qlast).val ≤ (seg qlast).b) := by
  459. have hw : (wTarget qlast).val = τ + 1 := by
  460. dsimp only [wTarget, segWTarget]; rw [dite_eq_right_of_eq_false (eq_false hqlast_not)]
  461. have := (seg qlast).hb; omega
  462. have hspec := evalFormalT_spec P (seg qlast) (nextC qlast) (segNextC_ge L hord qlast)
  463. (segNextC_le L qlast) (wTarget qlast) hnot
  464. have hw : (wTarget qlast).val = τ + 1 := by
  465. dsimp only [wTarget, segWTarget]; rw [dite_eq_right_of_eq_false (eq_false hqlast_not)]
  466. simpa [F, segFormalPoly, SLP.evalPoly, hw] using hspec
  467. -- Step 1: Every r : Fin L.length has 1 ≤ (F r).natDegree
  468. have hF_deg_pos : ∀ r : Fin L.length, 1 ≤ (F r).natDegree := by
  469. intro r
  470. by_cases hlast : r.val + 1 < L.length
  471. · by_contra h0
  472. have hdeg0 : (F r).natDegree = 0 := by omega
  473. have hF_eq : F r = Polynomial.C ((F r).coeff 0) := Polynomial.eq_C_of_natDegree_eq_zero hdeg0
  474. let r1 : Fin L.length := ⟨r.val + 1, hlast⟩
  475. have hnot_inter : ¬ ((seg r).a + 2 ≤ (wTarget r).val ∧ (wTarget r).val ≤ (seg r).b) := by
  476. have hw_val : (wTarget r).val =
  477. (P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).val := by
  478. dsimp only [wTarget, segWTarget]; rw [dite_eq_left_of_eq_true (eq_true hlast)]
  479. rw [hw_val]
  480. exact ((seg r).huntapped (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)
  481. (hseg_ord r hlast)).1
  482. have hnc_le_r : nextC r ≤ τ := segNextC_le L r
  483. have hspec := evalFormalT_spec P (seg r) (nextC r) (segNextC_ge L hord r)
  484. hnc_le_r (wTarget r) hnot_inter
  485. change (F r).eval (P.evalWire ⟨(seg r).b + 1, by have := (seg r).hb; omega⟩) =
  486. P.evalWire ⟨(wTarget r).val, by have := (wTarget r).isLt; omega⟩ at hspec
  487. rw [hF_eq, Polynomial.eval_C] at hspec
  488. have htop_next := (seg r1).hrun (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)
  489. le_rfl (seg r1).hab
  490. have hdeg_wt : (P.evalWire ⟨(wTarget r).val, by have := (wTarget r).isLt; omega⟩).natDegree =
  491. (P.prefix (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).maxDeg := by
  492. have hw_val : (wTarget r).val =
  493. (P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).val := by
  494. dsimp only [wTarget, segWTarget]; rw [dite_eq_left_of_eq_true (eq_true hlast)]
  495. have hpr := P.evalWire_prefix (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)
  496. (P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega))
  497. have h1 : P.evalWire ⟨(wTarget r).val, by have := (wTarget r).isLt; omega⟩ =
  498. (P.prefix (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).evalWire
  499. (P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)) := by
  500. rw [hpr]; congr 1; exact Fin.ext hw_val
  501. rw [h1]; exact htop_next.2.2
  502. have hrun_r := P.topSq_run_eval (seg r).hab (seg r).hb (seg r).hrun
  503. have hUr_le : (P.evalWire ⟨(seg r).b + 1, by have := (seg r).hb; omega⟩).natDegree ≤
  504. (P.prefix (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).maxDeg := by
  505. have hle_b : (seg r).b ≤ (seg r1).a := hseg_ord r hlast
  506. have hpr := P.evalWire_prefix (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)
  507. ⟨(seg r).b + 1, by omega⟩
  508. rw [← hpr]
  509. exact (P.prefix (seg r1).a _).wireDeg_le_maxDeg ⟨(seg r).b + 1, by omega⟩
  510. have hcoeff_le : ((F r).coeff 0).natDegree ≤ 2 ^ (nextC r - (seg r).b) * (P.prefix (seg r).a (by have := (seg r).hab; have := (seg r).hb; omega)).maxDeg :=
  511. evalFormalT_coeff_natDegree_le P (seg r).a (seg r).b (seg r).hab (seg r).hb
  512. (nextC r) (segNextC_ge L hord r) hnc_le_r (wTarget r) 0
  513. have hgap_gt : nextC r - (seg r).b < (seg r).b - (seg r).a := by
  514. cases hr0 : r.val with
  515. | zero =>
  516. have hh := hhead_gap hq_pos
  517. have hr_eq : r = ⟨0, hq_pos⟩ := Fin.ext hr0
  518. rw [hr_eq]; dsimp only [seg, nextC]; omega
  519. | succ m =>
  520. have hm := hmid_gap m (by omega)
  521. have hr_eq : r = ⟨m + 1, by omega⟩ := Fin.ext hr0
  522. rw [hr_eq]; dsimp only [seg, nextC]; omega
  523. have hpow_lt : 2 ^ (nextC r - (seg r).b) < 2 ^ ((seg r).b - (seg r).a) :=
  524. Nat.pow_lt_pow_right (by omega) hgap_gt
  525. have hD1_pos : 1 ≤ (P.prefix (seg r).a (by have := (seg r).hab; have := (seg r).hb; omega)).maxDeg :=
  526. (P.prefix (seg r).a _).one_le_maxDeg
  527. have hstrict : 2 ^ (nextC r - (seg r).b) * (P.prefix (seg r).a _).maxDeg <
  528. 2 ^ ((seg r).b - (seg r).a) * (P.prefix (seg r).a _).maxDeg :=
  529. Nat.mul_lt_mul_of_pos_right hpow_lt (by omega)
  530. rw [hspec] at hcoeff_le
  531. rw [hrun_r.2] at hUr_le
  532. omega
  533. · have hreq : r = qlast := Fin.ext (by dsimp [qlast]; omega)
  534. subst hreq; exact htop_pos
  535. refine ⟨hF_deg_pos, ?_⟩
  536. have hlead_ne : ∀ r : Fin L.length, (F r).leadingCoeff ≠ 0 := fun r =>
  537. Polynomial.leadingCoeff_ne_zero.mpr (fun h0 => by have := hF_deg_pos r; simp [h0] at this)
  538. have hw0_deg_pos : 1 ≤ w0.natDegree := by
  539. have htop0 := s0.hrun s0.a (by have := s0.hab; have := s0.hb; omega) le_rfl s0.hab
  540. have hpr := P.evalWire_prefix s0.a (by have := s0.hab; have := s0.hb; omega)
  541. (P.gateIn1 s0.a (by have := s0.hab; have := s0.hb; omega))
  542. have hdeg_eq : w0.natDegree = (P.prefix s0.a (by have := s0.hab; have := s0.hb; omega)).maxDeg := by
  543. dsimp only [w0]; rw [← hpr]; exact htop0.2.2
  544. rw [hdeg_eq]; exact (P.prefix s0.a _).one_le_maxDeg
  545. -- Step 2: Backward Cauchy propagation
  546. dsimp only
  547. intro x hx
  548. have hPx : P.evalPoly.eval x = 0 :=
  549. Polynomial.IsRoot.def.mp ((Polynomial.mem_roots hP).mp (Multiset.mem_toFinset.mp hx))
  550. by_cases hkill : ∃ r : Fin L.length, (F r).leadingCoeff.eval x = 0
  551. · obtain ⟨r, hr0⟩ := hkill
  552. apply Finset.mem_union_left; apply Finset.mem_union_left; apply Finset.mem_union_left
  553. refine Finset.mem_biUnion.mpr ⟨r, Finset.mem_univ _, ?_⟩
  554. rw [intRootSet, Multiset.mem_toFinset, Polynomial.mem_roots (hlead_ne r), Polynomial.IsRoot.def]
  555. exact hr0
  556. · push Not at hkill
  557. let P_prop : Fin L.length → Prop := fun r =>
  558. ((P.evalWire ⟨(seg r).b + 1, by have := (seg r).hb; omega⟩).eval x).natAbs ≤
  559. (wireHeight P (seg r).a (by have := (seg r).hab; have := (seg r).hb; omega) x) ^
  560. (2 ^ (nextC r - (seg r).b + 1))
  561. have hprop : ∀ (d : ℕ) (hd : d < L.length), P_prop ⟨L.length - 1 - d, by omega⟩ := by
  562. intro d
  563. induction d with
  564. | zero =>
  565. intro _
  566. have hr_eq : (⟨L.length - 1 - 0, by omega⟩ : Fin L.length) = qlast := Fin.ext (by simp [qlast])
  567. rw [hr_eq]
  568. dsimp only [P_prop]
  569. have hcauchy := cauchy_root_bound_polyT_ws (F qlast)
  570. (P.evalWire ⟨(seg qlast).b + 1, by have := (seg qlast).hb; omega⟩) x
  571. (hF_deg_pos qlast) (hkill qlast) (by rw [hqlast_spec, hPx])
  572. have hl1 := evalFormalT_l1_le P (seg qlast).a (seg qlast).b (seg qlast).hab (seg qlast).hb x
  573. (nextC qlast) (segNextC_ge L hord qlast) (segNextC_le L qlast) (wTarget qlast)
  574. have hH2 := two_le_wireHeight P (seg qlast).a (by have := (seg qlast).hab; have := (seg qlast).hb; omega) x
  575. exact le_trans (le_trans hcauchy hl1)
  576. (Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) (by omega)))
  577. | succ d' ihd =>
  578. intro hd
  579. set r : Fin L.length := ⟨L.length - 1 - (d' + 1), by omega⟩
  580. set r1 : Fin L.length := ⟨L.length - 1 - d', by omega⟩
  581. dsimp only [P_prop]
  582. have hr1_val : r.val + 1 = r1.val := by simp [r, r1]; omega
  583. have hr_lt : r.val + 1 < L.length := by omega
  584. have hfin_eq : (⟨r.val + 1, hr_lt⟩ : Fin L.length) = r1 := Fin.ext hr1_val
  585. have hnext_eq : nextC r = (seg r1).a := by
  586. dsimp only [nextC, segNextC]; rw [dite_eq_left_of_eq_true (eq_true hr_lt)]
  587. exact congrArg UntappedSeg.a (congrArg seg hfin_eq)
  588. set U := ((P.evalWire ⟨(seg r).b + 1, by have := (seg r).hb; omega⟩).eval x).natAbs
  589. set Hr := wireHeight P (seg r).a (by have := (seg r).hab; have := (seg r).hb; omega) x
  590. set Rr := nextC r - (seg r).b
  591. have hHr2 : 2 ≤ Hr := two_le_wireHeight P (seg r).a _ x
  592. by_cases hsmall : U ≤ 2 * Hr ^ (2 ^ Rr)
  593. · calc U ≤ 2 * Hr ^ (2 ^ Rr) := hsmall
  594. _ ≤ Hr ^ (2 ^ Rr) * Hr ^ (2 ^ Rr) := by
  595. have : 2 ≤ Hr ^ (2 ^ Rr) :=
  596. le_trans hHr2 (Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) Hr)
  597. exact Nat.mul_le_mul_right _ this
  598. _ = Hr ^ (2 ^ (Rr + 1)) := by rw [← pow_add, ← two_mul, pow_succ, Nat.mul_comm]
  599. · exfalso
  600. have hbig : 2 * Hr ^ (2 ^ Rr) < U := by omega
  601. have hU4 : 4 ≤ U := by
  602. have : 2 ≤ Hr ^ (2 ^ Rr) :=
  603. le_trans hHr2 (Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) Hr)
  604. omega
  605. have hl1 := evalFormalT_l1_le P (seg r).a (seg r).b (seg r).hab (seg r).hb x
  606. (nextC r) (segNextC_ge L hord r) (segNextC_le L r) (wTarget r)
  607. change polyL1At (F r) x ≤ Hr ^ (2 ^ Rr) at hl1
  608. have hbig_l1 : 2 * polyL1At (F r) x < U := by omega
  609. have hdom := cauchy_dominance_polyT_ws (F r)
  610. (P.evalWire ⟨(seg r).b + 1, by have := (seg r).hb; omega⟩) x
  611. (hF_deg_pos r) (hkill r) hbig_l1
  612. have hw_val : (wTarget r).val =
  613. (P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).val := by
  614. dsimp only [wTarget, segWTarget]; rw [dite_eq_left_of_eq_true (eq_true hr_lt)]
  615. have hseg_eq : seg ⟨r.val + 1, hr_lt⟩ = seg r1 := congrArg seg hfin_eq
  616. have hgate_eq : ∀ {s1 s2 : UntappedSeg P} (h1 : s1.a < τ) (h2 : s2.a < τ), s1 = s2 →
  617. (P.gateIn1 s1.a h1).val = (P.gateIn1 s2.a h2).val := by rintro s1 _ h1 h2 rfl; rfl
  618. exact hgate_eq _ _ hseg_eq
  619. have hnot_inter : ¬ ((seg r).a + 2 ≤ (wTarget r).val ∧ (wTarget r).val ≤ (seg r).b) := by
  620. rw [hw_val]
  621. have hbr : (seg r).b ≤ (seg r1).a := by rw [← hnext_eq]; exact segNextC_ge L hord r
  622. exact ((seg r).huntapped (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega) hbr).1
  623. have hnc_le_r : nextC r ≤ τ := segNextC_le L r
  624. have hspec := evalFormalT_spec P (seg r) (nextC r) (segNextC_ge L hord r)
  625. hnc_le_r (wTarget r) hnot_inter
  626. change (F r).eval (P.evalWire ⟨(seg r).b + 1, by have := (seg r).hb; omega⟩) =
  627. P.evalWire ⟨(wTarget r).val, by have := (wTarget r).isLt; omega⟩ at hspec
  628. rw [hspec] at hdom
  629. set w1_val := ((P.evalWire ⟨(wTarget r).val, by have := (wTarget r).isLt; omega⟩).eval x).natAbs
  630. have hU_le_sq : U ≤ w1_val ^ 2 := nat_half_sq_ge_ws hU4 hdom
  631. set g1 := (seg r1).b - (seg r1).a
  632. have hrun1 := P.topSq_run_eval (seg r1).hab (seg r1).hb (seg r1).hrun
  633. have hw1_eq : w1_val = ((P.evalWire ⟨(P.gateIn1 (seg r1).a
  634. (by have := (seg r1).hab; have := (seg r1).hb; omega)).val,
  635. by have := (P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).isLt; have := (seg r1).hab; have := (seg r1).hb; omega⟩).eval x).natAbs :=
  636. congrArg (fun k : Fin (τ + 2) => ((P.evalWire k).eval x).natAbs) (Fin.ext hw_val)
  637. have hU1_eq : ((P.evalWire ⟨(seg r1).b + 1, by have := (seg r1).hb; omega⟩).eval x).natAbs =
  638. w1_val ^ (2 ^ g1) := by
  639. rw [hw1_eq]
  640. have := congrArg (fun p : Polynomial ℤ => (p.eval x).natAbs) hrun1.1
  641. simpa [Int.natAbs_pow, g1] using this
  642. have hih : P_prop r1 := ihd (by omega)
  643. dsimp only [P_prop] at hih
  644. rw [hU1_eq] at hih
  645. have hH1_le := wireHeight_step_le P (seg r) x (nextC r) (segNextC_ge L hord r) (segNextC_le L r)
  646. have hmax_U : max Hr U = U := max_eq_right (by
  647. have : Hr ≤ Hr ^ (2 ^ Rr) := Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) Hr
  648. omega)
  649. rw [hmax_U] at hH1_le
  650. have hH1_le' : wireHeight P (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega) x ≤ U ^ (2 ^ Rr) := by
  651. change wireHeight P (nextC r) (segNextC_le L r) x ≤ U ^ (2 ^ Rr) at hH1_le
  652. simpa only [hnext_eq] using hH1_le
  653. have hg1_ge : Rr + (nextC r1 - (seg r1).b) + 3 ≤ g1 := by
  654. have hmid := hmid_gap r.val hr_lt
  655. have hRr_eq : Rr = (seg r1).a - (seg r).b := by rw [← hnext_eq]
  656. have hseg_r1 : L[r.val + 1]'hr_lt = seg r1 := congrArg seg hfin_eq
  657. have hnc1 : segNextC L ⟨r.val + 1, hr_lt⟩ = nextC r1 := congrArg nextC hfin_eq
  658. rw [hRr_eq]; dsimp only [g1]
  659. rw [← hseg_r1, ← hnc1]; exact hmid
  660. have hchain_pow : U ^ (2 ^ (g1 - 1)) ≤ U ^ (2 ^ (Rr + (nextC r1 - (seg r1).b) + 1)) := by
  661. have hleft : U ^ (2 ^ (g1 - 1)) ≤ w1_val ^ (2 ^ g1) := by
  662. calc U ^ (2 ^ (g1 - 1))
  663. ≤ (w1_val ^ 2) ^ (2 ^ (g1 - 1)) := Nat.pow_le_pow_left hU_le_sq _
  664. _ = w1_val ^ (2 * 2 ^ (g1 - 1)) := (pow_mul _ _ _).symm
  665. _ = w1_val ^ (2 ^ g1) := by
  666. congr 1
  667. conv_rhs => rw [← Nat.sub_add_cancel (by omega : 1 ≤ g1), pow_succ, Nat.mul_comm]
  668. have hright : w1_val ^ (2 ^ g1) ≤ U ^ (2 ^ (Rr + (nextC r1 - (seg r1).b) + 1)) := by
  669. calc w1_val ^ (2 ^ g1)
  670. ≤ (wireHeight P (seg r1).a _ x) ^ (2 ^ (nextC r1 - (seg r1).b + 1)) := hih
  671. _ ≤ (U ^ (2 ^ Rr)) ^ (2 ^ (nextC r1 - (seg r1).b + 1)) :=
  672. Nat.pow_le_pow_left hH1_le' _
  673. _ = U ^ (2 ^ Rr * 2 ^ (nextC r1 - (seg r1).b + 1)) := (pow_mul _ _ _).symm
  674. _ = U ^ (2 ^ (Rr + (nextC r1 - (seg r1).b) + 1)) := by
  675. rw [← pow_add, ← Nat.add_assoc]
  676. exact le_trans hleft hright
  677. have hexp_le : 2 ^ (g1 - 1) ≤ 2 ^ (Rr + (nextC r1 - (seg r1).b) + 1) :=
  678. (Nat.pow_le_pow_iff_right (by omega : 1 < U)).mp hchain_pow
  679. have hexp_lt : 2 ^ (Rr + (nextC r1 - (seg r1).b) + 1) < 2 ^ (g1 - 1) :=
  680. Nat.pow_lt_pow_right (by omega) (by omega)
  681. omega
  682. -- Evaluate at r = 0
  683. have hprop0 := hprop (L.length - 1) (by omega)
  684. have hr0_eq : (⟨L.length - 1 - (L.length - 1), by omega⟩ : Fin L.length) = ⟨0, hq_pos⟩ :=
  685. Fin.ext (by simp)
  686. rw [hr0_eq] at hprop0
  687. dsimp only [P_prop] at hprop0
  688. set R0 := s0.a
  689. set R1 := nextC ⟨0, hq_pos⟩ - s0.b
  690. set g0 := s0.b - s0.a
  691. have hg0_ge : 2 * R0 + R1 + 5 ≤ g0 := by
  692. simpa [R0, R1, g0, s0, nextC] using hhead_gap hq_pos
  693. have hrun0 := P.topSq_run_eval s0.hab s0.hb s0.hrun
  694. have hU0_eq : ((P.evalWire ⟨s0.b + 1, by have := s0.hb; omega⟩).eval x).natAbs =
  695. (w0.eval x).natAbs ^ (2 ^ g0) := by
  696. have := congrArg (fun p : Polynomial ℤ => (p.eval x).natAbs) hrun0.1
  697. simpa [Int.natAbs_pow, w0, g0] using this
  698. rw [hU0_eq] at hprop0
  699. have hH0_le := wireHeight_base_le P s0.a (by have := s0.hab; have := s0.hb; omega) x
  700. have hl1_w0 : polyIntL1 w0 ≤ 2 ^ (2 ^ R0) := by
  701. have hpr := P.evalWire_prefix s0.a (by have := s0.hab; have := s0.hb; omega)
  702. (P.gateIn1 s0.a (by have := s0.hab; have := s0.hb; omega))
  703. have hw0_eq : w0 = (P.prefix s0.a (by have := s0.hab; have := s0.hb; omega)).evalWire
  704. (P.gateIn1 s0.a (by have := s0.hab; have := s0.hb; omega)) := hpr.symm
  705. rw [hw0_eq]
  706. exact (P.prefix s0.a _).wire_polyIntL1_le _
  707. have hw0_le1 : (w0.eval x).natAbs ≤ 1 := by
  708. by_contra hbig_w0
  709. have hw0_ge2 : 2 ≤ (w0.eval x).natAbs := by omega
  710. by_cases hx_big : 2 * 2 ^ (2 ^ R0) ≤ x.natAbs
  711. · have hx4 : 4 ≤ x.natAbs := by
  712. have : 2 ≤ 2 ^ (2 ^ R0) :=
  713. Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) 2
  714. omega
  715. have hdom := cauchy_dominance_univar_ws w0 x hw0_deg_pos
  716. (le_trans (Nat.mul_le_mul_left 2 hl1_w0) hx_big)
  717. have hx_sq : x.natAbs ≤ (w0.eval x).natAbs ^ 2 := nat_half_sq_ge_ws hx4 hdom
  718. have hmax_x : max 2 x.natAbs = x.natAbs := max_eq_right (by omega)
  719. rw [hmax_x] at hH0_le
  720. have hchain_x : x.natAbs ^ (2 ^ (g0 - 1)) ≤ x.natAbs ^ (2 ^ (R0 + R1 + 1)) := by
  721. have h1 : x.natAbs ^ (2 ^ (g0 - 1)) ≤ (w0.eval x).natAbs ^ (2 ^ g0) := by
  722. calc x.natAbs ^ (2 ^ (g0 - 1))
  723. ≤ ((w0.eval x).natAbs ^ 2) ^ (2 ^ (g0 - 1)) := Nat.pow_le_pow_left hx_sq _
  724. _ = (w0.eval x).natAbs ^ (2 * 2 ^ (g0 - 1)) := (pow_mul _ _ _).symm
  725. _ = (w0.eval x).natAbs ^ (2 ^ g0) := by
  726. congr 1
  727. conv_rhs => rw [← Nat.sub_add_cancel (by omega : 1 ≤ g0), pow_succ, Nat.mul_comm]
  728. have h2 : (w0.eval x).natAbs ^ (2 ^ g0) ≤ x.natAbs ^ (2 ^ (R0 + R1 + 1)) := by
  729. calc (w0.eval x).natAbs ^ (2 ^ g0)
  730. ≤ (wireHeight P s0.a _ x) ^ (2 ^ (R1 + 1)) := hprop0
  731. _ ≤ (x.natAbs ^ (2 ^ R0)) ^ (2 ^ (R1 + 1)) := Nat.pow_le_pow_left hH0_le _
  732. _ = x.natAbs ^ (2 ^ R0 * 2 ^ (R1 + 1)) := (pow_mul _ _ _).symm
  733. _ = x.natAbs ^ (2 ^ (R0 + R1 + 1)) := by rw [← pow_add, ← Nat.add_assoc]
  734. exact le_trans h1 h2
  735. have hexp_le : 2 ^ (g0 - 1) ≤ 2 ^ (R0 + R1 + 1) :=
  736. (Nat.pow_le_pow_iff_right (by omega : 1 < x.natAbs)).mp hchain_x
  737. have hexp_lt : 2 ^ (R0 + R1 + 1) < 2 ^ (g0 - 1) :=
  738. Nat.pow_lt_pow_right (by omega) (by omega)
  739. omega
  740. · have hmax_le : max 2 x.natAbs ≤ 2 ^ (2 ^ R0 + 1) := by
  741. have : 2 * 2 ^ (2 ^ R0) = 2 ^ (2 ^ R0 + 1) := by rw [pow_succ, Nat.mul_comm]
  742. have h2le : 2 ≤ 2 ^ (2 ^ R0 + 1) :=
  743. Nat.le_self_pow (Nat.succ_ne_zero _) 2
  744. exact max_le h2le (by omega)
  745. have hH0_le2 : wireHeight P s0.a (by have := s0.hab; have := s0.hb; omega) x ≤ 2 ^ (2 ^ (2 * R0 + 1)) := by
  746. calc wireHeight P s0.a (by have := s0.hab; have := s0.hb; omega) x
  747. ≤ (max 2 x.natAbs) ^ (2 ^ R0) := hH0_le
  748. _ ≤ (2 ^ (2 ^ R0 + 1)) ^ (2 ^ R0) := Nat.pow_le_pow_left hmax_le _
  749. _ = 2 ^ ((2 ^ R0 + 1) * 2 ^ R0) := (pow_mul _ _ _).symm
  750. _ ≤ 2 ^ (2 ^ (2 * R0 + 1)) := by
  751. apply Nat.pow_le_pow_right (by omega)
  752. have h2pos : 1 ≤ 2 ^ R0 := Nat.one_le_pow _ _ (by omega)
  753. have hadd : 2 ^ R0 + 1 ≤ 2 * 2 ^ R0 := by omega
  754. calc (2 ^ R0 + 1) * 2 ^ R0
  755. ≤ (2 * 2 ^ R0) * 2 ^ R0 := Nat.mul_le_mul_right _ hadd
  756. _ = 2 * (2 ^ R0 * 2 ^ R0) := by ring
  757. _ = 2 ^ (2 * R0 + 1) := by rw [← pow_add, ← two_mul, pow_succ, Nat.mul_comm]
  758. have hchain_2 : 2 ^ (2 ^ g0) ≤ 2 ^ (2 ^ (2 * R0 + R1 + 2)) := by
  759. calc 2 ^ (2 ^ g0)
  760. ≤ (w0.eval x).natAbs ^ (2 ^ g0) := Nat.pow_le_pow_left hw0_ge2 _
  761. _ ≤ (wireHeight P s0.a _ x) ^ (2 ^ (R1 + 1)) := hprop0
  762. _ ≤ (2 ^ (2 ^ (2 * R0 + 1))) ^ (2 ^ (R1 + 1)) := Nat.pow_le_pow_left hH0_le2 _
  763. _ = 2 ^ (2 ^ (2 * R0 + 1) * 2 ^ (R1 + 1)) := (pow_mul _ _ _).symm
  764. _ = 2 ^ (2 ^ (2 * R0 + R1 + 2)) := by
  765. rw [← pow_add]; congr 2; omega
  766. have hexp_le : 2 ^ g0 ≤ 2 ^ (2 * R0 + R1 + 2) :=
  767. (Nat.pow_le_pow_iff_right (by omega : 1 < 2)).mp hchain_2
  768. have hexp_lt : 2 ^ (2 * R0 + R1 + 2) < 2 ^ g0 :=
  769. Nat.pow_lt_pow_right (by omega) (by omega)
  770. omega
  771. -- Therefore w0.eval x ∈ {-1, 0, 1}
  772. have hcases : w0.eval x = -1 ∨ w0.eval x = 0 ∨ w0.eval x = 1 := by omega
  773. have hw0_ne : w0 ≠ 0 := fun h0 => by simp [h0] at hw0_deg_pos
  774. have hw0_sub_ne : w0 - 1 ≠ 0 := fun h0 => by
  775. have : w0 = 1 := sub_eq_zero.mp h0
  776. rw [this] at hw0_deg_pos; simp at hw0_deg_pos
  777. have hw0_add_ne : w0 + 1 ≠ 0 := fun h0 => by
  778. have : w0 = -1 := eq_neg_of_add_eq_zero_left h0
  779. rw [this] at hw0_deg_pos; simp at hw0_deg_pos
  780. rcases hcases with hm1 | h0 | hp1
  781. · apply Finset.mem_union_right
  782. rw [intRootSet, Multiset.mem_toFinset, Polynomial.mem_roots hw0_add_ne, Polynomial.IsRoot.def]
  783. simp [hm1]
  784. · apply Finset.mem_union_left; apply Finset.mem_union_right
  785. rw [intRootSet, Multiset.mem_toFinset, Polynomial.mem_roots hw0_ne, Polynomial.IsRoot.def]
  786. exact h0
  787. · apply Finset.mem_union_left; apply Finset.mem_union_left; apply Finset.mem_union_right
  788. rw [intRootSet, Multiset.mem_toFinset, Polynomial.mem_roots hw0_sub_ne, Polynomial.IsRoot.def]
  789. simp [hp1]
  790. /-! ## 4. Leading-Coefficient Classification at Gap `R_r ≤ 1` -/
  791. /-- Predicate: segment `s` has a trailing gate at `s.b < c` which is a **non-squaring
  792. multiplication** of the segment output wire `s.b + 1` with an earlier wire `≤ s.a + 1`. -/
  793. def segIsNonSqMul {τ : ℕ} (P : SLP τ) (s : UntappedSeg P) (c : ℕ) (hc : c ≤ τ) : Prop :=
  794. ∃ (hbc : s.b < c),
  795. let hb_lt : s.b < τ := lt_of_lt_of_le hbc hc
  796. P.gateOp s.b hb_lt = .mul ∧
  797. (((P.gateIn1 s.b hb_lt).val = s.b + 1 ∧ (P.gateIn2 s.b hb_lt).val ≤ s.a + 1) ∨
  798. ((P.gateIn2 s.b hb_lt).val = s.b + 1 ∧ (P.gateIn1 s.b hb_lt).val ≤ s.a + 1))
  799. /-- Every `segIsNonSqMul` gate `s.b` is a `.mul` gate with distinct inputs (`gateIn1 ≠ gateIn2`),
  800. and hence strictly a **non-top-squaring** gate (`¬ P.gateIsTopSq s.b _`). -/
  801. lemma segIsNonSqMul_not_topSq {τ : ℕ} {P : SLP τ} {s : UntappedSeg P} {c : ℕ} {hc : c ≤ τ}
  802. (h : segIsNonSqMul P s c hc) :
  803. ∃ (hb_lt : s.b < τ), P.gateOp s.b hb_lt = .mul ∧ ¬ P.gateIsTopSq s.b hb_lt := by
  804. rcases h with ⟨hbc, hop, hcases⟩
  805. refine ⟨lt_of_lt_of_le hbc hc, hop, fun htop => ?_⟩
  806. have heq_in : (P.gateIn1 s.b (lt_of_lt_of_le hbc hc)).val =
  807. (P.gateIn2 s.b (lt_of_lt_of_le hbc hc)).val := congrArg Fin.val htop.2.1
  808. have hab := s.hab
  809. rcases hcases with ⟨h1, h2⟩ | ⟨h2, h1⟩ <;> omega
  810. /-- Value of `evalFormalT` at a wire `v < b + 2` (which is not an intermediate wire of `s`). -/
  811. private lemma evalFormalT_at_b {τ : ℕ} (P : SLP τ) (s : UntappedSeg P) (v : Fin (s.b + 2))
  812. (hnot : ¬ (s.a + 2 ≤ v.val ∧ v.val ≤ s.b)) :
  813. (v.val ≤ s.a + 1 ∧
  814. evalFormalT P s.a s.b s.hab s.hb s.b le_rfl s.hb v =
  815. Polynomial.C (P.evalWire ⟨v.val, by have := v.isLt; have := s.hb; omega⟩)) ∨
  816. (v.val = s.b + 1 ∧
  817. evalFormalT P s.a s.b s.hab s.hb s.b le_rfl s.hb v = Polynomial.X) := by
  818. rw [evalFormalT.eq_def]
  819. by_cases hv : v.val < s.b + 1
  820. · have hva : v.val ≤ s.a + 1 := by omega
  821. left
  822. refine ⟨hva, ?_⟩
  823. simp [hv, hva]
  824. · have hvb : v.val = s.b + 1 := by have := v.isLt; omega
  825. right
  826. refine ⟨hvb, ?_⟩
  827. simp [hvb]
  828. /-- Step equation for `evalFormalT` at `w.val = s.b + 2` (`c = s.b + 1`). -/
  829. private lemma evalFormalT_step_b_add_two {τ : ℕ} (P : SLP τ) (s : UntappedSeg P)
  830. (c : ℕ) (hbc : s.b ≤ c) (hc : c ≤ τ) (w : Fin (c + 2)) (hwb : w.val = s.b + 2) :
  831. let hb_lt : s.b < τ := by have := w.isLt; omega
  832. evalFormalT P s.a s.b s.hab s.hb c hbc hc w =
  833. (P.gateOp s.b hb_lt).applyPolyT
  834. (evalFormalT P s.a s.b s.hab s.hb s.b le_rfl s.hb (P.gateIn1 s.b hb_lt))
  835. (evalFormalT P s.a s.b s.hab s.hb s.b le_rfl s.hb (P.gateIn2 s.b hb_lt)) := by
  836. intro hb_lt
  837. rw [evalFormalT.eq_def]
  838. have hw1 : ¬ (w.val < s.b + 1) := by omega
  839. have hw2 : ¬ (w.val = s.b + 1) := by omega
  840. rw [dite_eq_right_of_eq_false (eq_false hw1), dite_eq_right_of_eq_false (eq_false hw2)]
  841. dsimp only
  842. have ht'_eq : w.val - 2 = s.b := by omega
  843. have hcongr : ∀ {t₁ t₂ : ℕ} (h1 : t₁ < τ) (h2 : t₂ < τ) (hb1 : s.b ≤ t₁) (hb2 : s.b ≤ t₂),
  844. t₁ = t₂ →
  845. (P.gateOp t₁ h1).applyPolyT
  846. (evalFormalT P s.a s.b s.hab s.hb t₁ hb1 (le_of_lt h1) (P.gateIn1 t₁ h1))
  847. (evalFormalT P s.a s.b s.hab s.hb t₁ hb1 (le_of_lt h1) (P.gateIn2 t₁ h1)) =
  848. (P.gateOp t₂ h2).applyPolyT
  849. (evalFormalT P s.a s.b s.hab s.hb t₂ hb2 (le_of_lt h2) (P.gateIn1 t₂ h2))
  850. (evalFormalT P s.a s.b s.hab s.hb t₂ hb2 (le_of_lt h2) (P.gateIn2 t₂ h2)) := by
  851. rintro t₁ _ h1 h2 hb1 hb2 rfl; rfl
  852. exact hcongr _ hb_lt _ le_rfl ht'_eq
  853. /-- **Exact Leading-Coefficient Classification at `c ≤ s.b + 1`:**
  854. For any untapped segment `s` and `s.b ≤ c ≤ s.b + 1`, if `F = evalFormalT ... c ... w` has
  855. `1 ≤ F.natDegree` (with `w` not an intermediate wire of `s`), then:
  856. - Either `intRootSet F.leadingCoeff = ∅` (leading coefficient in `{-1, 1, 2}`), OR
  857. - `segIsNonSqMul P s c hc` holds AND `F.leadingCoeff = P.evalWire u` for some wire `u < s.a + 2`! -/
  858. lemma evalFormalT_leadingCoeff_of_step_le_one {τ : ℕ} (P : SLP τ) (s : UntappedSeg P)
  859. (c : ℕ) (hbc : s.b ≤ c) (hc : c ≤ τ) (hstep : c ≤ s.b + 1)
  860. (w : Fin (c + 2)) (hnot : ¬ (s.a + 2 ≤ w.val ∧ w.val ≤ s.b))
  861. (hdeg : 1 ≤ (evalFormalT P s.a s.b s.hab s.hb c hbc hc w).natDegree) :
  862. intRootSet (evalFormalT P s.a s.b s.hab s.hb c hbc hc w).leadingCoeff = ∅ ∨
  863. (segIsNonSqMul P s c hc ∧
  864. ∃ u : Fin (s.a + 2),
  865. (evalFormalT P s.a s.b s.hab s.hb c hbc hc w).leadingCoeff =
  866. P.evalWire ⟨u.val, by have := u.isLt; have := s.hab; have := s.hb; omega⟩) := by
  867. set F := evalFormalT P s.a s.b s.hab s.hb c hbc hc w
  868. by_cases hw1 : w.val < s.b + 1
  869. · have hwa : w.val ≤ s.a + 1 := by omega
  870. have hF_eq : F = Polynomial.C (P.evalWire ⟨w.val, by omega⟩) := by
  871. dsimp only [F]; rw [evalFormalT.eq_def]; simp [hw1, hwa]
  872. rw [hF_eq, Polynomial.natDegree_C] at hdeg; omega
  873. · by_cases hw2 : w.val = s.b + 1
  874. · have hF_eq : F = Polynomial.X := by
  875. dsimp only [F]; rw [evalFormalT.eq_def]; simp [hw2]
  876. left; rw [hF_eq, Polynomial.leadingCoeff_X]
  877. exact intRootSet_C_eq_empty (c := 1) (by decide)
  878. · have hwb : w.val = s.b + 2 := by have := w.isLt; omega
  879. have hbc_lt : s.b < c := by have := w.isLt; omega
  880. have hb_lt : s.b < τ := lt_of_lt_of_le hbc_lt hc
  881. have hF_step : F = (P.gateOp s.b hb_lt).applyPolyT
  882. (evalFormalT P s.a s.b s.hab s.hb s.b le_rfl s.hb (P.gateIn1 s.b hb_lt))
  883. (evalFormalT P s.a s.b s.hab s.hb s.b le_rfl s.hb (P.gateIn2 s.b hb_lt)) :=
  884. evalFormalT_step_b_add_two P s c hbc hc w hwb
  885. have hu := s.huntapped s.b hb_lt le_rfl
  886. rcases evalFormalT_at_b P s (P.gateIn1 s.b hb_lt) hu.1 with ⟨h1a, hA⟩ | ⟨h1b, hA⟩ <;>
  887. rcases evalFormalT_at_b P s (P.gateIn2 s.b hb_lt) hu.2 with ⟨h2a, hB⟩ | ⟨h2b, hB⟩
  888. · rw [hA, hB] at hF_step
  889. cases hop : P.gateOp s.b hb_lt <;> rw [hop] at hF_step <;>
  890. simp only [SLPOp.applyPolyT] at hF_step
  891. · rw [← Polynomial.C_add] at hF_step; rw [hF_step, Polynomial.natDegree_C] at hdeg; omega
  892. · rw [← Polynomial.C_sub] at hF_step; rw [hF_step, Polynomial.natDegree_C] at hdeg; omega
  893. · rw [← Polynomial.C_mul] at hF_step; rw [hF_step, Polynomial.natDegree_C] at hdeg; omega
  894. · set p := P.evalWire ⟨(P.gateIn1 s.b hb_lt).val, by have := (P.gateIn1 s.b hb_lt).isLt; omega⟩
  895. rw [hA, hB] at hF_step
  896. cases hop : P.gateOp s.b hb_lt <;> rw [hop] at hF_step <;>
  897. simp only [SLPOp.applyPolyT] at hF_step
  898. · left
  899. rw [hF_step, add_comm, Polynomial.leadingCoeff_X_add_C]
  900. exact intRootSet_C_eq_empty (c := 1) (by decide)
  901. · left
  902. rw [hF_step, leadingCoeff_C_sub_X]
  903. have hm1 : (-1 : Polynomial ℤ) = Polynomial.C (-1) := by simp
  904. rw [hm1]; exact intRootSet_C_eq_empty (c := -1) (by decide)
  905. · right
  906. refine ⟨⟨hbc_lt, hop, Or.inr ⟨h2b, h1a⟩⟩, ⟨(P.gateIn1 s.b hb_lt).val, by omega⟩, ?_⟩
  907. rw [hF_step, Polynomial.leadingCoeff_C_mul_X]
  908. · set q := P.evalWire ⟨(P.gateIn2 s.b hb_lt).val, by have := (P.gateIn2 s.b hb_lt).isLt; omega⟩
  909. rw [hA, hB] at hF_step
  910. cases hop : P.gateOp s.b hb_lt <;> rw [hop] at hF_step <;>
  911. simp only [SLPOp.applyPolyT] at hF_step
  912. · left
  913. rw [hF_step, Polynomial.leadingCoeff_X_add_C]
  914. exact intRootSet_C_eq_empty (c := 1) (by decide)
  915. · left
  916. rw [hF_step, sub_eq_add_neg, ← map_neg, Polynomial.leadingCoeff_X_add_C]
  917. exact intRootSet_C_eq_empty (c := 1) (by decide)
  918. · right
  919. refine ⟨⟨hbc_lt, hop, Or.inl ⟨h1b, h2a⟩⟩, ⟨(P.gateIn2 s.b hb_lt).val, by omega⟩, ?_⟩
  920. rw [hF_step, mul_comm, Polynomial.leadingCoeff_C_mul_X]
  921. · rw [hA, hB] at hF_step
  922. cases hop : P.gateOp s.b hb_lt <;> rw [hop] at hF_step <;>
  923. simp only [SLPOp.applyPolyT] at hF_step
  924. · left
  925. have h2x : (Polynomial.X + Polynomial.X : Polynomial (Polynomial ℤ)) =
  926. Polynomial.C (Polynomial.C 2) * Polynomial.X := by
  927. simp only [map_ofNat]; ring
  928. rw [hF_step, h2x, Polynomial.leadingCoeff_C_mul_X]
  929. exact intRootSet_C_eq_empty (c := 2) (by decide)
  930. · rw [sub_self] at hF_step; rw [hF_step, Polynomial.natDegree_zero] at hdeg; omega
  931. · left
  932. have hx2 : (Polynomial.X * Polynomial.X : Polynomial (Polynomial ℤ)) = Polynomial.X ^ 2 := by ring
  933. rw [hF_step, hx2, Polynomial.leadingCoeff_X_pow]
  934. exact intRootSet_C_eq_empty (c := 1) (by decide)
  935. /-- If `c = s.b + 1`, `P.evalWire (s.b + 2) ≠ 0`, and gate `s.b` uses the top wire `s.b + 1`,
  936. then `evalFormalT` at wire `s.b + 2` automatically has `natDegree ≥ 1`! -/
  937. lemma evalFormalT_natDegree_pos_of_active {τ : ℕ} (P : SLP τ) (s : UntappedSeg P)
  938. (hb_lt : s.b < τ) (hval_ne : P.evalWire ⟨s.b + 2, by omega⟩ ≠ 0)
  939. (hact : (P.gateIn1 s.b hb_lt).val = s.b + 1 ∨ (P.gateIn2 s.b hb_lt).val = s.b + 1) :
  940. 1 ≤ (evalFormalT P s.a s.b s.hab s.hb (s.b + 1) (Nat.le_succ _) hb_lt ⟨s.b + 2, by omega⟩).natDegree := by
  941. by_contra h0
  942. set F := evalFormalT P s.a s.b s.hab s.hb (s.b + 1) (Nat.le_succ _) hb_lt ⟨s.b + 2, by omega⟩
  943. have hdeg0 : F.natDegree = 0 := by omega
  944. have hF_step : F = (P.gateOp s.b hb_lt).applyPolyT
  945. (evalFormalT P s.a s.b s.hab s.hb s.b le_rfl s.hb (P.gateIn1 s.b hb_lt))
  946. (evalFormalT P s.a s.b s.hab s.hb s.b le_rfl s.hb (P.gateIn2 s.b hb_lt)) :=
  947. evalFormalT_step_b_add_two P s (s.b + 1) (Nat.le_succ _) hb_lt ⟨s.b + 2, by omega⟩ rfl
  948. have hu := s.huntapped s.b hb_lt le_rfl
  949. have hnot_w : ¬ (s.a + 2 ≤ s.b + 2 ∧ s.b + 2 ≤ s.b) := by omega
  950. have hspec := evalFormalT_spec P s (s.b + 1) (Nat.le_succ _) hb_lt ⟨s.b + 2, by omega⟩ hnot_w
  951. change F.eval (P.evalWire ⟨s.b + 1, by omega⟩) = P.evalWire ⟨s.b + 2, by omega⟩ at hspec
  952. rcases evalFormalT_at_b P s (P.gateIn1 s.b hb_lt) hu.1 with ⟨h1a, hA⟩ | ⟨h1b, hA⟩ <;>
  953. rcases evalFormalT_at_b P s (P.gateIn2 s.b hb_lt) hu.2 with ⟨h2a, hB⟩ | ⟨h2b, hB⟩
  954. · have := s.hab; rcases hact with h | h <;> omega
  955. · set p := P.evalWire ⟨(P.gateIn1 s.b hb_lt).val, by have := (P.gateIn1 s.b hb_lt).isLt; omega⟩
  956. rw [hA, hB] at hF_step
  957. cases hop : P.gateOp s.b hb_lt <;> rw [hop] at hF_step <;>
  958. simp only [SLPOp.applyPolyT] at hF_step
  959. · rw [hF_step, add_comm, Polynomial.natDegree_X_add_C] at hdeg0; omega
  960. · rw [hF_step, natDegree_C_sub_X] at hdeg0; omega
  961. · by_cases hp0 : p = 0
  962. · rw [hp0, map_zero, zero_mul] at hF_step
  963. rw [hF_step, Polynomial.eval_zero] at hspec
  964. exact hval_ne hspec.symm
  965. · rw [hF_step, Polynomial.natDegree_C_mul_X _ hp0] at hdeg0; omega
  966. · set q := P.evalWire ⟨(P.gateIn2 s.b hb_lt).val, by have := (P.gateIn2 s.b hb_lt).isLt; omega⟩
  967. rw [hA, hB] at hF_step
  968. cases hop : P.gateOp s.b hb_lt <;> rw [hop] at hF_step <;>
  969. simp only [SLPOp.applyPolyT] at hF_step
  970. · rw [hF_step, Polynomial.natDegree_X_add_C] at hdeg0; omega
  971. · rw [hF_step, sub_eq_add_neg, ← map_neg, Polynomial.natDegree_X_add_C] at hdeg0; omega
  972. · by_cases hq0 : q = 0
  973. · rw [hq0, map_zero, mul_zero] at hF_step
  974. rw [hF_step, Polynomial.eval_zero] at hspec
  975. exact hval_ne hspec.symm
  976. · rw [hF_step, mul_comm, Polynomial.natDegree_C_mul_X _ hq0] at hdeg0; omega
  977. · rw [hA, hB] at hF_step
  978. cases hop : P.gateOp s.b hb_lt <;> rw [hop] at hF_step <;>
  979. simp only [SLPOp.applyPolyT] at hF_step
  980. · have h2x : (Polynomial.X + Polynomial.X : Polynomial (Polynomial ℤ)) =
  981. Polynomial.C (Polynomial.C 2) * Polynomial.X := by
  982. simp only [map_ofNat]; ring
  983. have h2ne : (Polynomial.C (2 : ℤ) : Polynomial ℤ) ≠ 0 := Polynomial.C_ne_zero.mpr (by decide)
  984. rw [hF_step, h2x, Polynomial.natDegree_C_mul_X _ h2ne] at hdeg0; omega
  985. · rw [sub_self] at hF_step
  986. rw [hF_step, Polynomial.eval_zero] at hspec
  987. exact hval_ne hspec.symm
  988. · have hx2 : (Polynomial.X * Polynomial.X : Polynomial (Polynomial ℤ)) = Polynomial.X ^ 2 := by ring
  989. rw [hF_step, hx2, Polynomial.natDegree_X_pow] at hdeg0; omega
  990. /-! ## 5. Well-Spaced SLPs & Inductive Exponential-in-`m_mul` / `2 ^ (|B| + 1)` Bounds -/
  991. /-- A list `L` of untapped pure-squaring segments of `P : SLP τ` is **well-spaced** if:
  992. 1. `L` is nonempty and ordered (`s_0.b ≤ s_1.a`, etc.);
  993. 2. Every inter-segment gap and trailing gap has at most 1 gate (`a_{i+1} ≤ b_i + 1`, `τ ≤ b_{last} + 1`);
  994. 3. The first pure-squaring run has length `g_0 ≥ 2 * a_0 + 6`, and every subsequent run has `g_i ≥ 5`;
  995. 4. Whenever an isolated gate exists at `b_i < nextC i`, it actively reads the segment output wire `b_i + 1`. -/
  996. structure WellSpacedSegs {τ : ℕ} (P : SLP τ) (L : List (UntappedSeg P)) : Prop where
  997. hne : L ≠ []
  998. hord : SegsOrdered L
  999. hstep_mid : ∀ i (hi : i + 1 < L.length), (L[i + 1]'hi).a ≤ (L[i]'(by omega)).b + 1
  1000. hstep_last : ∀ (hpos : 0 < L.length), τ ≤ (L[L.length - 1]'(by omega)).b + 1
  1001. hhead_gap : ∀ (hpos : 0 < L.length), 2 * (L[0]'hpos).a + 6 ≤ (L[0]'hpos).b - (L[0]'hpos).a
  1002. hmid_gap : ∀ i (hi : i + 1 < L.length), 5 ≤ (L[i + 1]'hi).b - (L[i + 1]'hi).a
  1003. hactive : ∀ i (hi : i < L.length) (hb_lt : (L[i]'hi).b < segNextC L ⟨i, hi⟩),
  1004. (P.gateIn1 (L[i]'hi).b (lt_of_lt_of_le hb_lt (segNextC_le L ⟨i, hi⟩))).val = (L[i]'hi).b + 1 ∨
  1005. (P.gateIn2 (L[i]'hi).b (lt_of_lt_of_le hb_lt (segNextC_le L ⟨i, hi⟩))).val = (L[i]'hi).b + 1
  1006. /-- Predicate: index `i < L.length` has an isolated non-squaring multiplication at gate `(L[i]).b`. -/
  1007. def isNonSqMulIdx {τ : ℕ} (P : SLP τ) (L : List (UntappedSeg P)) (i : ℕ) : Prop :=
  1008. ∃ (hi : i < L.length),
  1009. segIsNonSqMul P (L[i]'hi) (segNextC L ⟨i, hi⟩) (segNextC_le L ⟨i, hi⟩)
  1010. /-- Number of segments among the first `r` segments of `L` whose trailing gate is a non-squaring
  1011. multiplication. -/
  1012. noncomputable def nonSqMulCount {τ : ℕ} (P : SLP τ) (L : List (UntappedSeg P)) (r : ℕ) : ℕ :=
  1013. letI := Classical.propDecidable
  1014. ((Finset.range r).filter (isNonSqMulIdx P L)).card
  1015. /-- Gate boundary index at stage `r ≤ L.length`: `(L[r]).a` if `r < L.length`, else `τ`. -/
  1016. def stageBound {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (r : ℕ) : ℕ :=
  1017. if h : r < L.length then (L[r]'h).a else τ
  1018. lemma stageBound_le {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (r : ℕ) :
  1019. stageBound L r ≤ τ := by
  1020. unfold stageBound
  1021. split_ifs with h
  1022. · exact le_of_lt (lt_of_lt_of_le (L[r]'h).hab (L[r]'h).hb)
  1023. · exact le_rfl
  1024. /-- Helper: restrict the first `r + 1` segments of `L` to `P.prefix c hc` (where `c = segNextC L r`). -/
  1025. private def restrictTake {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (hord : SegsOrdered L)
  1026. (r : ℕ) (hr : r < L.length) (c : ℕ) (hc : c ≤ τ) (hbc : (L[r]'hr).b ≤ c) :
  1027. List (UntappedSeg (P.prefix c hc)) :=
  1028. (List.finRange (r + 1)).map fun j =>
  1029. (L[j.val]'(by omega)).restrictPrefix c hc (by
  1030. have hle : ∀ (i k : ℕ) (hi : i < L.length) (hk : k < L.length), i ≤ k →
  1031. (L[i]'hi).b ≤ (L[k]'hk).b := by
  1032. intro i k hi hk hik
  1033. induction k with
  1034. | zero =>
  1035. have hi0 : i = 0 := by omega
  1036. simp only [hi0]; exact le_rfl
  1037. | succ k' ihk =>
  1038. by_cases heq : i = k' + 1
  1039. · simp only [heq]; exact le_rfl
  1040. · have hprev := ihk (by omega) (by omega)
  1041. have hadj := segsOrdered_adj L hord k' hk
  1042. have hlt := (L[k' + 1]'hk).hab
  1043. omega
  1044. exact le_trans (hle j.val r (by omega) hr (by omega)) hbc)
  1045. private lemma restrictTake_length {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (hord : SegsOrdered L)
  1046. (r : ℕ) (hr : r < L.length) (c : ℕ) (hc : c ≤ τ) (hbc : (L[r]'hr).b ≤ c) :
  1047. (restrictTake L hord r hr c hc hbc).length = r + 1 := by
  1048. simp [restrictTake]
  1049. private lemma restrictTake_getElem_a {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (hord : SegsOrdered L)
  1050. (r : ℕ) (hr : r < L.length) (c : ℕ) (hc : c ≤ τ) (hbc : (L[r]'hr).b ≤ c)
  1051. (i : ℕ) (hi : i < (restrictTake L hord r hr c hc hbc).length) :
  1052. ((restrictTake L hord r hr c hc hbc)[i]'hi).a = (L[i]'(by rw [restrictTake_length] at hi; omega)).a := by
  1053. simp [restrictTake, UntappedSeg.restrictPrefix]
  1054. private lemma restrictTake_getElem_b {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (hord : SegsOrdered L)
  1055. (r : ℕ) (hr : r < L.length) (c : ℕ) (hc : c ≤ τ) (hbc : (L[r]'hr).b ≤ c)
  1056. (i : ℕ) (hi : i < (restrictTake L hord r hr c hc hbc).length) :
  1057. ((restrictTake L hord r hr c hc hbc)[i]'hi).b = (L[i]'(by rw [restrictTake_length] at hi; omega)).b := by
  1058. simp [restrictTake, UntappedSeg.restrictPrefix]
  1059. private lemma segsOrdered_of_adj {τ : ℕ} {P : SLP τ} :
  1060. ∀ (M : List (UntappedSeg P)),
  1061. (∀ (i : ℕ) (hi : i + 1 < M.length), (M[i]'(by omega)).b ≤ (M[i + 1]'hi).a) →
  1062. SegsOrdered M
  1063. | [], _ => trivial
  1064. | [_], _ => trivial
  1065. | s0 :: s1 :: rest, h =>
  1066. ⟨h 0 (by simp), segsOrdered_of_adj (s1 :: rest) (fun i hi => h (i + 1) (by simpa using hi))⟩
  1067. /-- **Inductive Wire-Level Well-Spaced Bound:**
  1068. For every stage `r ≤ L.length` and every nonzero wire `u` before `stageBound L r + 2`,
  1069. `intRootCount (P.evalWire u) ≤ 3 * 2 ^ (a_0 + nonSqMulCount P L r)`. -/
  1070. theorem slp_well_spaced_wire_bound {τ : ℕ} (P : SLP τ) (L : List (UntappedSeg P))
  1071. (hws : WellSpacedSegs P L) :
  1072. ∀ (r : ℕ) (_hr_le : r ≤ L.length) (u : Fin (τ + 2)),
  1073. u.val < stageBound L r + 2 →
  1074. P.evalWire u ≠ 0 →
  1075. intRootCount (P.evalWire u) ≤
  1076. 3 * 2 ^ ((L[0]'(List.length_pos_of_ne_nil hws.hne)).a + nonSqMulCount P L r) := by
  1077. classical
  1078. have hq_pos : 0 < L.length := List.length_pos_of_ne_nil hws.hne
  1079. set a0 := (L[0]'hq_pos).a
  1080. intro r
  1081. induction r using Nat.strong_induction_on with
  1082. | ind r ih =>
  1083. intro hr_le u hu hune
  1084. cases r with
  1085. | zero =>
  1086. clear hr_le
  1087. have hu_a0 : u.val < a0 + 2 := by
  1088. simpa [stageBound, hq_pos, a0] using hu
  1089. have ha0_le : a0 ≤ τ := le_of_lt (lt_of_lt_of_le (L[0]'hq_pos).hab (L[0]'hq_pos).hb)
  1090. have hpr := P.evalWire_prefix a0 ha0_le ⟨u.val, hu_a0⟩
  1091. have hdeg : (P.evalWire u).natDegree ≤ 2 ^ a0 := by
  1092. have heq : P.evalWire u = (P.prefix a0 ha0_le).evalWire ⟨u.val, hu_a0⟩ := hpr.symm
  1093. rw [heq]
  1094. exact le_trans ((P.prefix a0 ha0_le).wireDeg_le_maxDeg _) (P.prefix a0 ha0_le).maxDeg_le_two_pow
  1095. have hrc : intRootCount (P.evalWire u) ≤ (P.evalWire u).natDegree :=
  1096. le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _)
  1097. have hm0 : nonSqMulCount P L 0 = 0 := by simp [nonSqMulCount]
  1098. rw [hm0, Nat.add_zero]
  1099. change intRootCount (P.evalWire u) ≤ 3 * 2 ^ a0
  1100. omega
  1101. | succ r' =>
  1102. have hr'_lt : r' < L.length := by omega
  1103. set sr := L[r']'hr'_lt
  1104. set cr := stageBound L (r' + 1)
  1105. have hcr_eq : cr = segNextC L ⟨r', hr'_lt⟩ := rfl
  1106. have hcr_le : cr ≤ τ := stageBound_le L (r' + 1)
  1107. have hbr_le_cr : sr.b ≤ cr := segNextC_ge L hws.hord ⟨r', hr'_lt⟩
  1108. have hstep_cr : cr ≤ sr.b + 1 := by
  1109. dsimp only [cr, stageBound, sr]
  1110. split_ifs with h
  1111. · exact hws.hstep_mid r' h
  1112. · have hr'_last : r' = L.length - 1 := by omega
  1113. have hlast := hws.hstep_last hq_pos
  1114. have : L[L.length - 1]'(by omega) = L[r']'hr'_lt := by simp [hr'_last]
  1115. simpa [this] using hlast
  1116. have hmon : nonSqMulCount P L r' ≤ nonSqMulCount P L (r' + 1) := by
  1117. unfold nonSqMulCount
  1118. exact Finset.card_le_card (Finset.filter_subset_filter _ (Finset.range_mono (Nat.le_succ r')))
  1119. by_cases hu_early : u.val < sr.a + 2
  1120. · have hprev := ih r' (Nat.lt_succ_self r') (le_of_lt hr'_lt) u
  1121. (by simpa [stageBound, hr'_lt, sr] using hu_early) hune
  1122. exact le_trans hprev (Nat.mul_le_mul_left 3 (Nat.pow_le_pow_right (by omega) (Nat.add_le_add_left hmon a0)))
  1123. · by_cases hu_run : u.val ≤ sr.b + 1
  1124. · have hab' : sr.a < u.val - 1 := by omega
  1125. have hb' : u.val - 1 ≤ τ := by have := sr.hb; omega
  1126. have hrun' : ∀ t (ht : t < τ), sr.a ≤ t → t < u.val - 1 → P.gateIsTopSq t ht :=
  1127. fun t ht h1 h2 => sr.hrun t ht h1 (by omega)
  1128. have hpow := (P.topSq_run_eval hab' hb' hrun').1
  1129. have hu_eq : (⟨(u.val - 1) + 1, by have := u.isLt; omega⟩ : Fin (τ + 2)) = u := Fin.ext (by dsimp; omega)
  1130. rw [hu_eq] at hpow
  1131. set w_in : Fin (τ + 2) :=
  1132. ⟨(P.gateIn1 sr.a (by have := sr.hab; have := sr.hb; omega)).val,
  1133. by have := (P.gateIn1 sr.a (by have := sr.hab; have := sr.hb; omega)).isLt
  1134. have := sr.hab; have := sr.hb; omega⟩
  1135. have hw_in_lt : w_in.val < sr.a + 2 := (P.gateIn1 sr.a _).isLt
  1136. have hw_in_ne : P.evalWire w_in ≠ 0 := by
  1137. intro h0
  1138. rw [hpow, show P.evalWire w_in = 0 from h0, zero_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega)))] at hune
  1139. exact hune rfl
  1140. have hrc_eq : intRootCount (P.evalWire u) = intRootCount (P.evalWire w_in) := by
  1141. rw [hpow]
  1142. exact intRootCount_pow_eq (Nat.one_le_pow _ _ (by omega))
  1143. rw [hrc_eq]
  1144. have hprev := ih r' (Nat.lt_succ_self r') (le_of_lt hr'_lt) w_in
  1145. (by simpa [stageBound, hr'_lt, sr] using hw_in_lt) hw_in_ne
  1146. exact le_trans hprev (Nat.mul_le_mul_left 3 (Nat.pow_le_pow_right (by omega) (Nat.add_le_add_left hmon a0)))
  1147. · -- Main Case: u.val = sr.b + 2, cr = sr.b + 1, P.evalWire u = (P.prefix cr hcr_le).evalPoly!
  1148. have hu_eq_b2 : u.val = sr.b + 2 := by omega
  1149. have hcr_eq_succ : cr = sr.b + 1 := by omega
  1150. have hbr_lt_cr : sr.b < cr := by omega
  1151. have hbr_lt_τ : sr.b < τ := lt_of_lt_of_le hbr_lt_cr hcr_le
  1152. set P' : SLP cr := P.prefix cr hcr_le
  1153. have hP'_eval : P'.evalPoly = P.evalWire u := by
  1154. unfold SLP.evalPoly
  1155. rw [P.evalWire_prefix cr hcr_le ⟨cr + 1, Nat.lt_succ_self _⟩]
  1156. congr 1; apply Fin.ext; dsimp; omega
  1157. have hP'_ne : P'.evalPoly ≠ 0 := by rw [hP'_eval]; exact hune
  1158. set L' : List (UntappedSeg P') := restrictTake L hws.hord r' hr'_lt cr hcr_le hbr_le_cr
  1159. have hL'_len : L'.length = r' + 1 := restrictTake_length L hws.hord r' hr'_lt cr hcr_le hbr_le_cr
  1160. have hL'_ne : L' ≠ [] := by intro h; simp [h] at hL'_len
  1161. have hL'_ord : SegsOrdered L' := by
  1162. apply segsOrdered_of_adj
  1163. intro i hi
  1164. rw [restrictTake_getElem_b, restrictTake_getElem_a]
  1165. exact segsOrdered_adj L hws.hord i (by omega)
  1166. have hL'_nextC : ∀ (j : Fin L'.length),
  1167. segNextC L' j = segNextC L ⟨j.val, by omega⟩ := by
  1168. intro j
  1169. by_cases hj : j.val + 1 < L'.length
  1170. · have hj_L : j.val + 1 < L.length := by omega
  1171. unfold segNextC
  1172. rw [dite_eq_left_of_eq_true (eq_true hj), dite_eq_left_of_eq_true (eq_true hj_L)]
  1173. exact restrictTake_getElem_a L hws.hord r' hr'_lt cr hcr_le hbr_le_cr (j.val + 1) hj
  1174. · have hleft : segNextC L' j = cr := by
  1175. unfold segNextC; rw [dite_eq_right_of_eq_false (eq_false hj)]
  1176. have hj_eq : j.val = r' := by omega
  1177. have hfin : (⟨j.val, by omega⟩ : Fin L.length) = ⟨r', hr'_lt⟩ := Fin.ext hj_eq
  1178. rw [hleft, congrArg (segNextC L) hfin]
  1179. exact hcr_eq
  1180. have hL'_head : ∀ (hpos : 0 < L'.length),
  1181. 2 * (L'[0]'hpos).a + (segNextC L' ⟨0, hpos⟩ - (L'[0]'hpos).b) + 5 ≤
  1182. (L'[0]'hpos).b - (L'[0]'hpos).a := by
  1183. intro hpos
  1184. rw [restrictTake_getElem_a, restrictTake_getElem_b, hL'_nextC ⟨0, hpos⟩]
  1185. have hstep0 : segNextC L ⟨0, hq_pos⟩ ≤ (L[0]'hq_pos).b + 1 := by
  1186. unfold segNextC
  1187. split_ifs with h1
  1188. · exact hws.hstep_mid 0 h1
  1189. · dsimp only at h1
  1190. have hlen1 : L.length - 1 = 0 := by omega
  1191. have hlast := hws.hstep_last hq_pos
  1192. simpa [hlen1] using hlast
  1193. have hhg := hws.hhead_gap hq_pos
  1194. have hnc0 : segNextC L ⟨(⟨0, hpos⟩ : Fin L'.length).val, by omega⟩ = segNextC L ⟨0, hq_pos⟩ := rfl
  1195. rw [hnc0]
  1196. omega
  1197. have hL'_mid : ∀ (i : ℕ) (hi : i + 1 < L'.length),
  1198. ((L'[i + 1]'hi).a - (L'[i]'(by omega)).b) +
  1199. (segNextC L' ⟨i + 1, hi⟩ - (L'[i + 1]'hi).b) + 3 ≤
  1200. (L'[i + 1]'hi).b - (L'[i + 1]'hi).a := by
  1201. intro i hi
  1202. rw [restrictTake_getElem_a, restrictTake_getElem_b, restrictTake_getElem_b,
  1203. hL'_nextC ⟨i + 1, hi⟩]
  1204. have hi_L : i + 1 < L.length := by omega
  1205. have hstep1 : (L[i + 1]'hi_L).a ≤ (L[i]'(by omega)).b + 1 := hws.hstep_mid i hi_L
  1206. have hstep2 : segNextC L ⟨i + 1, hi_L⟩ ≤ (L[i + 1]'hi_L).b + 1 := by
  1207. unfold segNextC
  1208. split_ifs with h2
  1209. · exact hws.hstep_mid (i + 1) h2
  1210. · dsimp only at h2
  1211. have hlast := hws.hstep_last hq_pos
  1212. have heq : L.length - 1 = i + 1 := by omega
  1213. have : L[L.length - 1]'(by omega) = L[i + 1]'hi_L := by simp [heq]
  1214. simpa [this] using hlast
  1215. have hmg := hws.hmid_gap i hi_L
  1216. have hnc_i : segNextC L ⟨(⟨i + 1, hi⟩ : Fin L'.length).val, by omega⟩ = segNextC L ⟨i + 1, hi_L⟩ := rfl
  1217. rw [hnc_i]
  1218. omega
  1219. -- Verify the top-segment formal polynomial has natDegree ≥ 1 (via hactive!)
  1220. have htop_pos : 1 ≤ (segFormalPoly P' L' hL'_ord
  1221. ⟨L'.length - 1, List.length_pos_of_ne_nil hL'_ne |> fun h => by omega⟩).natDegree := by
  1222. have hnot_last : ¬ ((L'.length - 1) + 1 < L'.length) := by omega
  1223. have hql_idx : L'.length - 1 = r' := by omega
  1224. have hact_r := hws.hactive r' hr'_lt (by simpa [hcr_eq] using hbr_lt_cr)
  1225. have hu_fin : (⟨sr.b + 2, by omega⟩ : Fin (τ + 2)) = u := Fin.ext hu_eq_b2.symm
  1226. have hpos_P := evalFormalT_natDegree_pos_of_active P sr hbr_lt_τ
  1227. (by rw [hu_fin]; exact hune) hact_r
  1228. have hEq_F :
  1229. segFormalPoly P' L' hL'_ord ⟨L'.length - 1, by omega⟩ =
  1230. evalFormalT P sr.a sr.b sr.hab sr.hb (sr.b + 1) (Nat.le_succ _) hbr_lt_τ ⟨sr.b + 2, by omega⟩ := by
  1231. unfold segFormalPoly
  1232. have ha_eq : (L'[L'.length - 1]'(by omega)).a = sr.a := by
  1233. rw [restrictTake_getElem_a]; simp [hql_idx, sr]
  1234. have hb_eq : (L'[L'.length - 1]'(by omega)).b = sr.b := by
  1235. rw [restrictTake_getElem_b]; simp [hql_idx, sr]
  1236. have hnc_eq : segNextC L' ⟨L'.length - 1, by omega⟩ = cr := by
  1237. unfold segNextC; rw [dite_eq_right_of_eq_false (eq_false hnot_last)]
  1238. have hw_eq : (segWTarget P' L' ⟨L'.length - 1, by omega⟩).val = sr.b + 2 := by
  1239. unfold segWTarget; rw [dite_eq_right_of_eq_false (eq_false hnot_last)]
  1240. dsimp; omega
  1241. have hgen : ∀ {a₁ b₁ c₁ : ℕ} {hab₁ : a₁ < b₁} {hb₁ : b₁ ≤ cr} {hbc₁ : b₁ ≤ c₁} {hc₁ : c₁ ≤ cr}
  1242. {w₁ : Fin (c₁ + 2)} {a₂ b₂ c₂ : ℕ} {hab₂ : a₂ < b₂} {hb₂ : b₂ ≤ τ} {hbc₂ : b₂ ≤ c₂}
  1243. {hc₂ : c₂ ≤ τ} {w₂ : Fin (c₂ + 2)},
  1244. a₁ = a₂ → b₁ = b₂ → c₁ = c₂ → w₁.val = w₂.val →
  1245. evalFormalT P' a₁ b₁ hab₁ hb₁ c₁ hbc₁ hc₁ w₁ =
  1246. evalFormalT P a₂ b₂ hab₂ hb₂ c₂ hbc₂ hc₂ w₂ := by
  1247. rintro a₁ b₁ c₁ hab₁ hb₁ hbc₁ hc₁ w₁ _ _ _ hab₂ hb₂ hbc₂ hc₂ w₂ rfl rfl rfl hw
  1248. have hweq : w₁ = w₂ := Fin.ext hw
  1249. subst hweq
  1250. exact evalFormalT_prefix_eq P a₁ b₁ hab₁ cr hb₁ hcr_le c₁ hbc₁ hc₁ w₁
  1251. exact hgen ha_eq hb_eq (hnc_eq.trans hcr_eq_succ) hw_eq
  1252. rw [hEq_F]; exact hpos_P
  1253. obtain ⟨hF'_deg, hsub'⟩ := slp_gap_dominance_root_cover P' hP'_ne L' hL'_ne hL'_ord hL'_head hL'_mid htop_pos
  1254. dsimp only at hsub'
  1255. rw [← hP'_eval]
  1256. -- Bound intRootCount P'.evalPoly from hsub'
  1257. have hcard_le : intRootCount P'.evalPoly ≤
  1258. (∑ j : Fin L'.length, intRootCount (segFormalPoly P' L' hL'_ord j).leadingCoeff) +
  1259. 3 * 2 ^ a0 := by
  1260. set s0' := L'[0]'(List.length_pos_of_ne_nil hL'_ne)
  1261. set w0' : Polynomial ℤ :=
  1262. P'.evalWire ⟨(P'.gateIn1 s0'.a (by have := s0'.hab; have := s0'.hb; omega)).val,
  1263. by have := (P'.gateIn1 s0'.a (by have := s0'.hab; have := s0'.hb; omega)).isLt
  1264. have := s0'.hab; have := s0'.hb; omega⟩
  1265. have hw0'_deg : w0'.natDegree ≤ 2 ^ a0 := by
  1266. have ha0' : s0'.a = a0 := restrictTake_getElem_a L hws.hord r' hr'_lt cr hcr_le hbr_le_cr 0 (List.length_pos_of_ne_nil hL'_ne)
  1267. have hpr := P'.evalWire_prefix s0'.a (by have := s0'.hab; have := s0'.hb; omega)
  1268. (P'.gateIn1 s0'.a (by have := s0'.hab; have := s0'.hb; omega))
  1269. have h1 : w0'.natDegree ≤ (P'.prefix s0'.a (by have := s0'.hab; have := s0'.hb; omega)).maxDeg := by
  1270. dsimp only [w0']; rw [← hpr]; exact (P'.prefix s0'.a _).wireDeg_le_maxDeg _
  1271. have h2 : (P'.prefix s0'.a (by have := s0'.hab; have := s0'.hb; omega)).maxDeg ≤ 2 ^ a0 := by
  1272. have h2' := (P'.prefix s0'.a (by have := s0'.hab; have := s0'.hb; omega)).maxDeg_le_two_pow
  1273. simpa [ha0'] using h2'
  1274. exact le_trans h1 h2
  1275. have hcard_w0m : (intRootSet (w0' - 1)).card ≤ 2 ^ a0 := by
  1276. have h1 : (intRootSet (w0' - 1)).card ≤ (w0' - 1).natDegree :=
  1277. le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _)
  1278. have h2 : (w0' - 1).natDegree ≤ w0'.natDegree := by
  1279. simpa using Polynomial.natDegree_sub_le w0' (1 : Polynomial ℤ)
  1280. omega
  1281. have hcard_w0 : (intRootSet w0').card ≤ 2 ^ a0 := by
  1282. have h1 : (intRootSet w0').card ≤ w0'.natDegree :=
  1283. le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _)
  1284. omega
  1285. have hcard_w0p : (intRootSet (w0' + 1)).card ≤ 2 ^ a0 := by
  1286. have h1 : (intRootSet (w0' + 1)).card ≤ (w0' + 1).natDegree :=
  1287. le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _)
  1288. have h2 : (w0' + 1).natDegree ≤ w0'.natDegree := by
  1289. have h := Polynomial.natDegree_add_le w0' (1 : Polynomial ℤ)
  1290. rw [Polynomial.natDegree_one] at h
  1291. exact le_trans h (max_le le_rfl (Nat.zero_le _))
  1292. omega
  1293. have hc1 : (intRootSet P'.evalPoly).card ≤
  1294. ((Finset.univ.biUnion (fun j : Fin L'.length => intRootSet (segFormalPoly P' L' hL'_ord j).leadingCoeff)) ∪
  1295. intRootSet (w0' - 1) ∪ intRootSet w0' ∪ intRootSet (w0' + 1)).card :=
  1296. Finset.card_le_card hsub'
  1297. have hc2 := Finset.card_union_le
  1298. ((Finset.univ.biUnion (fun j : Fin L'.length => intRootSet (segFormalPoly P' L' hL'_ord j).leadingCoeff)) ∪
  1299. intRootSet (w0' - 1) ∪ intRootSet w0')
  1300. (intRootSet (w0' + 1))
  1301. have hc3 := Finset.card_union_le
  1302. ((Finset.univ.biUnion (fun j : Fin L'.length => intRootSet (segFormalPoly P' L' hL'_ord j).leadingCoeff)) ∪
  1303. intRootSet (w0' - 1))
  1304. (intRootSet w0')
  1305. have hc4 := Finset.card_union_le
  1306. (Finset.univ.biUnion (fun j : Fin L'.length => intRootSet (segFormalPoly P' L' hL'_ord j).leadingCoeff))
  1307. (intRootSet (w0' - 1))
  1308. have hc5 := Finset.card_biUnion_le (s := (Finset.univ : Finset (Fin L'.length)))
  1309. (t := fun j : Fin L'.length => intRootSet (segFormalPoly P' L' hL'_ord j).leadingCoeff)
  1310. change (intRootSet P'.evalPoly).card ≤
  1311. (∑ j : Fin L'.length, (intRootSet (segFormalPoly P' L' hL'_ord j).leadingCoeff).card) + 3 * 2 ^ a0
  1312. omega
  1313. -- Bound each summand j : Fin L'.length via evalFormalT_leadingCoeff_of_step_le_one + IH!
  1314. have hterm : ∀ j : Fin L'.length,
  1315. intRootCount (segFormalPoly P' L' hL'_ord j).leadingCoeff ≤
  1316. if isNonSqMulIdx P L j.val then 3 * 2 ^ (a0 + nonSqMulCount P L j.val) else 0 := by
  1317. intro j
  1318. set sj' := L'[j.val]'j.isLt
  1319. set cj := segNextC L' j
  1320. have hj_lt_L : j.val < L.length := by omega
  1321. set sj := L[j.val]'hj_lt_L
  1322. have ha_j : sj'.a = sj.a := restrictTake_getElem_a L hws.hord r' hr'_lt cr hcr_le hbr_le_cr j.val j.isLt
  1323. have hb_j : sj'.b = sj.b := restrictTake_getElem_b L hws.hord r' hr'_lt cr hcr_le hbr_le_cr j.val j.isLt
  1324. have hcj_eq : cj = segNextC L ⟨j.val, hj_lt_L⟩ := hL'_nextC j
  1325. have hstep_j : cj ≤ sj'.b + 1 := by
  1326. rw [hcj_eq, hb_j]
  1327. unfold segNextC
  1328. split_ifs with h1
  1329. · exact hws.hstep_mid j.val h1
  1330. · dsimp only at h1
  1331. have hlast := hws.hstep_last hq_pos
  1332. have heq : L.length - 1 = j.val := by omega
  1333. have : L[L.length - 1]'(by omega) = L[j.val]'hj_lt_L := by simp [heq]
  1334. simpa [this] using hlast
  1335. have hnot_j : ¬ (sj'.a + 2 ≤ (segWTarget P' L' j).val ∧ (segWTarget P' L' j).val ≤ sj'.b) := by
  1336. unfold segWTarget
  1337. split_ifs with h
  1338. · exact (sj'.huntapped (L'[j.val + 1]'h).a (by have := (L'[j.val + 1]'h).hab; have := (L'[j.val + 1]'h).hb; omega)
  1339. (segsOrdered_adj L' hL'_ord j.val h)).1
  1340. · have := sj'.hb; dsimp; omega
  1341. have hcls := evalFormalT_leadingCoeff_of_step_le_one P' sj' cj
  1342. (segNextC_ge L' hL'_ord j) (segNextC_le L' j) hstep_j (segWTarget P' L' j) hnot_j (hF'_deg j)
  1343. rcases hcls with hempty | ⟨hmul', v, hlc_eq⟩
  1344. · have h0 : intRootCount (segFormalPoly P' L' hL'_ord j).leadingCoeff = 0 := by
  1345. unfold intRootCount segFormalPoly; rw [hempty]; simp
  1346. rw [h0]; split_ifs <;> omega
  1347. · have hmul_P : isNonSqMulIdx P L j.val := by
  1348. refine ⟨hj_lt_L, ?_⟩
  1349. rcases hmul' with ⟨hb_lt_cj, hop', hcases'⟩
  1350. have hb_lt_cj' : sj.b < segNextC L ⟨j.val, hj_lt_L⟩ := by rw [← hcj_eq, ← hb_j]; exact hb_lt_cj
  1351. have hcj_le_cr : cj ≤ cr := segNextC_le L' j
  1352. have hsb_lt_cr : sj.b < cr := by rw [← hb_j]; exact lt_of_lt_of_le hb_lt_cj hcj_le_cr
  1353. refine ⟨hb_lt_cj', ?_, ?_⟩
  1354. · have hop1 : P'.gateOp sj.b hsb_lt_cr = .mul := by
  1355. have hcongr : ∀ {k₁ k₂ : ℕ} (h1 : k₁ < cr) (h2 : k₂ < cr), k₁ = k₂ →
  1356. P'.gateOp k₁ h1 = P'.gateOp k₂ h2 := by rintro k₁ _ h1 h2 rfl; rfl
  1357. rw [← hcongr _ hsb_lt_cr hb_j]; exact hop'
  1358. rwa [P.gateOp_prefix cr hcr_le sj.b hsb_lt_cr] at hop1
  1359. · have h1_eq : (P.gateIn1 sj.b (lt_of_lt_of_le hsb_lt_cr hcr_le)).val =
  1360. (P'.gateIn1 sj'.b (lt_of_lt_of_le hb_lt_cj hcj_le_cr)).val := by
  1361. rw [← P.gateIn1_prefix_val cr hcr_le sj.b hsb_lt_cr]
  1362. have hcongr : ∀ {k₁ k₂ : ℕ} (h1 : k₁ < cr) (h2 : k₂ < cr), k₁ = k₂ →
  1363. (P'.gateIn1 k₁ h1).val = (P'.gateIn1 k₂ h2).val := by rintro k₁ _ h1 h2 rfl; rfl
  1364. exact hcongr hsb_lt_cr _ hb_j.symm
  1365. have h2_eq : (P.gateIn2 sj.b (lt_of_lt_of_le hsb_lt_cr hcr_le)).val =
  1366. (P'.gateIn2 sj'.b (lt_of_lt_of_le hb_lt_cj hcj_le_cr)).val := by
  1367. rw [← P.gateIn2_prefix_val cr hcr_le sj.b hsb_lt_cr]
  1368. have hcongr : ∀ {k₁ k₂ : ℕ} (h1 : k₁ < cr) (h2 : k₂ < cr), k₁ = k₂ →
  1369. (P'.gateIn2 k₁ h1).val = (P'.gateIn2 k₂ h2).val := by rintro k₁ _ h1 h2 rfl; rfl
  1370. exact hcongr hsb_lt_cr _ hb_j.symm
  1371. rw [h1_eq, h2_eq, ← ha_j, ← hb_j]
  1372. exact hcases'
  1373. simp only [hmul_P, ↓reduceIte]
  1374. change intRootCount (evalFormalT P' sj'.a sj'.b sj'.hab sj'.hb cj _ _ _).leadingCoeff ≤ _
  1375. rw [hlc_eq]
  1376. have hlc_ne : P'.evalWire ⟨v.val, by have := v.isLt; have := sj'.hab; have := sj'.hb; omega⟩ ≠ 0 := by
  1377. rw [← hlc_eq]
  1378. exact Polynomial.leadingCoeff_ne_zero.mpr (fun h0 => by
  1379. have hdegj := hF'_deg j
  1380. change 1 ≤ (evalFormalT P' sj'.a sj'.b sj'.hab sj'.hb cj _ _ _).natDegree at hdegj
  1381. rw [h0] at hdegj; simp at hdegj)
  1382. set v' : Fin (τ + 2) := ⟨v.val, by have := v.isLt; have := sj'.hab; have := sj'.hb; omega⟩
  1383. have hv_eq : P'.evalWire ⟨v.val, by have := v.isLt; have := sj'.hab; have := sj'.hb; omega⟩ =
  1384. P.evalWire v' := P.evalWire_prefix cr hcr_le _
  1385. rw [hv_eq] at hlc_ne ⊢
  1386. have hv'_lt : v'.val < stageBound L j.val + 2 := by
  1387. dsimp only [v', stageBound]
  1388. rw [dite_eq_left_of_eq_true (eq_true hj_lt_L)]
  1389. calc v.val < sj'.a + 2 := v.isLt
  1390. _ = (L[j.val]'hj_lt_L).a + 2 := by rw [ha_j]
  1391. exact ih j.val (by omega) (le_of_lt hj_lt_L) v' hv'_lt hlc_ne
  1392. -- Sum the bounds using one_add_sum_two_pow_filter!
  1393. have hsum_le :
  1394. (∑ j : Fin L'.length, intRootCount (segFormalPoly P' L' hL'_ord j).leadingCoeff) ≤
  1395. ∑ j ∈ (Finset.range (r' + 1)).filter (isNonSqMulIdx P L),
  1396. 3 * 2 ^ (a0 + nonSqMulCount P L j) := by
  1397. calc _ ≤ ∑ j : Fin L'.length,
  1398. if isNonSqMulIdx P L j.val then 3 * 2 ^ (a0 + nonSqMulCount P L j.val) else 0 :=
  1399. Finset.sum_le_sum (fun j _ => hterm j)
  1400. _ = ∑ j ∈ Finset.range L'.length,
  1401. if isNonSqMulIdx P L j then 3 * 2 ^ (a0 + nonSqMulCount P L j) else 0 :=
  1402. Fin.sum_univ_eq_sum_range (fun j => if isNonSqMulIdx P L j then 3 * 2 ^ (a0 + nonSqMulCount P L j) else 0) L'.length
  1403. _ = ∑ j ∈ (Finset.range (r' + 1)).filter (isNonSqMulIdx P L),
  1404. 3 * 2 ^ (a0 + nonSqMulCount P L j) := by
  1405. rw [hL'_len, Finset.sum_filter]
  1406. have hgeom :
  1407. 3 * 2 ^ a0 +
  1408. ∑ j ∈ (Finset.range (r' + 1)).filter (isNonSqMulIdx P L),
  1409. 3 * 2 ^ (a0 + nonSqMulCount P L j) =
  1410. 3 * 2 ^ (a0 + nonSqMulCount P L (r' + 1)) := by
  1411. set S := ∑ j ∈ (Finset.range (r' + 1)).filter (isNonSqMulIdx P L),
  1412. 2 ^ ((Finset.range j).filter (isNonSqMulIdx P L)).card
  1413. have hfac :
  1414. ∑ j ∈ (Finset.range (r' + 1)).filter (isNonSqMulIdx P L),
  1415. 3 * 2 ^ (a0 + nonSqMulCount P L j) =
  1416. (3 * 2 ^ a0) * S := by
  1417. rw [Finset.mul_sum]
  1418. refine Finset.sum_congr rfl fun j _ => ?_
  1419. unfold nonSqMulCount
  1420. rw [pow_add, mul_assoc]
  1421. have hfactor : 3 * 2 ^ a0 + (3 * 2 ^ a0) * S = (3 * 2 ^ a0) * (1 + S) := by ring
  1422. rw [hfac, hfactor]
  1423. have htele : 1 + S = 2 ^ ((Finset.range (r' + 1)).filter (isNonSqMulIdx P L)).card :=
  1424. one_add_sum_two_pow_filter (isNonSqMulIdx P L) (r' + 1)
  1425. rw [htele]
  1426. unfold nonSqMulCount
  1427. rw [pow_add, mul_assoc]
  1428. omega
  1429. /-- **Well-Spaced Sharp Multiplication-Count Bound (`3 * 2 ^ (a_0 + m_mul)`):**
  1430. For every nonzero constant-free `P : SLP τ` admitting a well-spaced segment list `L`,
  1431. `intRootCount P.evalPoly ≤ 3 * 2 ^ (a_0 + nonSqMulCount P L L.length)`.
  1432. Additions, subtractions, and pure squarings do **not** contribute to the exponent! -/
  1433. theorem slp_well_spaced_mul_bound {τ : ℕ} (P : SLP τ) (hP : P.evalPoly ≠ 0)
  1434. (L : List (UntappedSeg P)) (hws : WellSpacedSegs P L) :
  1435. intRootCount P.evalPoly ≤
  1436. 3 * 2 ^ ((L[0]'(List.length_pos_of_ne_nil hws.hne)).a + nonSqMulCount P L L.length) := by
  1437. have hlt : τ + 1 < stageBound L L.length + 2 := by
  1438. simp [stageBound]
  1439. exact slp_well_spaced_wire_bound P L hws L.length le_rfl ⟨τ + 1, Nat.lt_succ_self _⟩ hlt hP
  1440. /-- **Well-Spaced Addition/Subtraction-Only Bound (`z_ℤ(P) ≤ 3`):**
  1441. For every nonzero well-spaced SLP starting from `a_0 = 0` with no isolated non-squaring
  1442. multiplications (`nonSqMulCount = 0`) — allowing **arbitrarily many** additions, subtractions,
  1443. pure squarings, and arbitrary DAG taps to earlier wires —
  1444. `intRootCount P.evalPoly ≤ 3`. -/
  1445. theorem slp_well_spaced_add_only_le_three {τ : ℕ} (P : SLP τ) (hP : P.evalPoly ≠ 0)
  1446. (L : List (UntappedSeg P)) (hws : WellSpacedSegs P L)
  1447. (ha0 : (L[0]'(List.length_pos_of_ne_nil hws.hne)).a = 0)
  1448. (hm0 : nonSqMulCount P L L.length = 0) :
  1449. intRootCount P.evalPoly ≤ 3 := by
  1450. have h := slp_well_spaced_mul_bound P hP L hws
  1451. simpa [ha0, hm0] using h
  1452. /-- **Well-Spaced General `{+, -, ×}` SLP Theorem (`z_ℤ(P) ≤ 2 ^ (|B| + 1)`):**
  1453. Every nonzero constant-free `P : SLP τ` admitting a well-spaced segment list `L` with `a_0 = 0`
  1454. satisfies
  1455. `intRootCount P.evalPoly ≤ 2 ^ (P.nonTopSqCount + 1)`. -/
  1456. theorem slp_well_spaced_intRootCount_le {τ : ℕ} (P : SLP τ) (hP : P.evalPoly ≠ 0)
  1457. (L : List (UntappedSeg P)) (hws : WellSpacedSegs P L)
  1458. (ha0 : (L[0]'(List.length_pos_of_ne_nil hws.hne)).a = 0) :
  1459. intRootCount P.evalPoly ≤ 2 ^ (P.nonTopSqCount + 1) := by
  1460. classical
  1461. by_cases hA0 : P.addSubCount = 0
  1462. · rcases slp_addSubCount_zero_wire P hA0 ⟨τ + 1, Nat.lt_succ_self _⟩ with ⟨e, he⟩
  1463. have hsub : intRootSet P.evalPoly ⊆ {0} := by
  1464. intro x hx
  1465. have hr : P.evalPoly.eval x = 0 :=
  1466. Polynomial.IsRoot.def.mp ((Polynomial.mem_roots hP).mp (Multiset.mem_toFinset.mp hx))
  1467. change (P.evalWire ⟨τ + 1, Nat.lt_succ_self _⟩).eval x = 0 at hr
  1468. rw [he, Polynomial.eval_pow, Polynomial.eval_X] at hr
  1469. exact Finset.mem_singleton.mpr (eq_zero_of_pow_eq_zero hr)
  1470. have hle1 : intRootCount P.evalPoly ≤ 1 := by
  1471. calc intRootCount P.evalPoly ≤ ({0} : Finset ℤ).card := Finset.card_le_card hsub
  1472. _ = 1 := Finset.card_singleton 0
  1473. have hpow : 1 ≤ 2 ^ (P.nonTopSqCount + 1) := Nat.one_le_pow _ _ (by omega)
  1474. omega
  1475. · have hmul_lt_B : nonSqMulCount P L L.length + 1 ≤ P.nonTopSqCount := by
  1476. have hex_add : ∃ t : Fin τ, P.gateOp t.val t.isLt ≠ .mul ∧ ¬ P.gateIsTopSq t.val t.isLt := by
  1477. have hgen : ∀ {n : ℕ} (Q : SLP n), Q.addSubCount ≠ 0 →
  1478. ∃ t : Fin n, Q.gateOp t.val t.isLt ≠ .mul ∧ ¬ Q.gateIsTopSq t.val t.isLt := by
  1479. intro n Q
  1480. induction Q with
  1481. | nil => intro h; simp [SLP.addSubCount] at h
  1482. | snoc Q' op i j ih =>
  1483. intro hne
  1484. cases op with
  1485. | mul =>
  1486. simp only [SLP.addSubCount] at hne
  1487. rcases ih hne with ⟨t, hop, hnt⟩
  1488. refine ⟨⟨t.val, Nat.lt_succ_of_lt t.isLt⟩, ?_, ?_⟩
  1489. · simp [SLP.gateOp, t.isLt, hop]
  1490. · rwa [SLP.gateIsTopSq_snoc_lt]
  1491. | add =>
  1492. refine ⟨⟨_, Nat.lt_succ_self _⟩, ?_, ?_⟩
  1493. · simp [SLP.gateOp]
  1494. · rw [SLP.gateIsTopSq_snoc_last]; intro htop; cases htop.1
  1495. | sub =>
  1496. refine ⟨⟨_, Nat.lt_succ_self _⟩, ?_, ?_⟩
  1497. · simp [SLP.gateOp]
  1498. · rw [SLP.gateIsTopSq_snoc_last]; intro htop; cases htop.1
  1499. exact hgen P hA0
  1500. obtain ⟨t_add, ht_add_op, ht_add_nt⟩ := hex_add
  1501. set S_mul := (Finset.range L.length).filter (isNonSqMulIdx P L)
  1502. set S_B := Finset.univ.filter (fun t : Fin τ => ¬ P.gateIsTopSq t.val t.isLt)
  1503. let f : ℕ → Fin τ := fun i =>
  1504. if h : i < L.length then
  1505. if hb : (L[i]'h).b < τ then ⟨(L[i]'h).b, hb⟩ else t_add
  1506. else t_add
  1507. have hf_mem : ∀ i ∈ S_mul, f i ∈ S_B ∧ P.gateOp (f i).val (f i).isLt = .mul := by
  1508. intro i hi
  1509. have hi_lt : i < L.length := Finset.mem_range.mp (Finset.mem_filter.mp hi).1
  1510. have hmul_i : isNonSqMulIdx P L i := (Finset.mem_filter.mp hi).2
  1511. rcases hmul_i with ⟨_, hseg⟩
  1512. rcases segIsNonSqMul_not_topSq hseg with ⟨hb_lt, hop, hnt⟩
  1513. have hfi : f i = ⟨(L[i]'hi_lt).b, hb_lt⟩ := by
  1514. simp [f, hi_lt, hb_lt]
  1515. rw [hfi]
  1516. exact ⟨Finset.mem_filter.mpr ⟨Finset.mem_univ _, hnt⟩, hop⟩
  1517. have hf_inj : Set.InjOn f (S_mul : Set ℕ) := by
  1518. intro i hi j hj hfeq
  1519. have hi_lt : i < L.length := Finset.mem_range.mp (Finset.mem_filter.mp hi).1
  1520. have hj_lt : j < L.length := Finset.mem_range.mp (Finset.mem_filter.mp hj).1
  1521. rcases (Finset.mem_filter.mp hi).2 with ⟨_, hsi⟩
  1522. rcases (Finset.mem_filter.mp hj).2 with ⟨_, hsj⟩
  1523. rcases segIsNonSqMul_not_topSq hsi with ⟨hbi, _, _⟩
  1524. rcases segIsNonSqMul_not_topSq hsj with ⟨hbj, _, _⟩
  1525. have hfi : f i = ⟨(L[i]'hi_lt).b, hbi⟩ := by simp [f, hi_lt, hbi]
  1526. have hfj : f j = ⟨(L[j]'hj_lt).b, hbj⟩ := by simp [f, hj_lt, hbj]
  1527. rw [hfi, hfj] at hfeq
  1528. have hb_eq : (L[i]'hi_lt).b = (L[j]'hj_lt).b := congrArg Fin.val hfeq
  1529. by_contra hne
  1530. have hstrict : ∀ (p q : ℕ) (hp : p < L.length) (hq : q < L.length), p < q →
  1531. (L[p]'hp).b < (L[q]'hq).b := by
  1532. intro p q hp hq hpq
  1533. induction q with
  1534. | zero => omega
  1535. | succ q' ihq =>
  1536. by_cases hpq' : p = q'
  1537. · subst hpq'
  1538. have hadj := segsOrdered_adj L hws.hord p hq
  1539. have hlt := (L[p + 1]'hq).hab
  1540. omega
  1541. · have h1 := ihq (by omega) (by omega)
  1542. have hadj := segsOrdered_adj L hws.hord q' hq
  1543. have hlt := (L[q' + 1]'hq).hab
  1544. omega
  1545. rcases lt_or_gt_of_ne hne with hlt | hgt
  1546. · have := hstrict i j hi_lt hj_lt hlt; omega
  1547. · have := hstrict j i hj_lt hi_lt hgt; omega
  1548. have ht_not_mem : t_add ∉ S_mul.image f := by
  1549. intro hm
  1550. rcases Finset.mem_image.mp hm with ⟨i, hi, hfi⟩
  1551. have hop := (hf_mem i hi).2
  1552. rw [hfi] at hop
  1553. exact ht_add_op hop
  1554. have hsub_B : insert t_add (S_mul.image f) ⊆ S_B := by
  1555. intro t ht
  1556. rcases Finset.mem_insert.mp ht with rfl | him
  1557. · exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, ht_add_nt⟩
  1558. · rcases Finset.mem_image.mp him with ⟨i, hi, rfl⟩
  1559. exact (hf_mem i hi).1
  1560. have hcard_eq : (S_mul.image f).card = S_mul.card := Finset.card_image_of_injOn hf_inj
  1561. have hcard_B : P.nonTopSqCount = S_B.card := P.nonTopSqCount_eq_card
  1562. have hle := Finset.card_le_card hsub_B
  1563. rw [Finset.card_insert_of_notMem ht_not_mem, hcard_eq] at hle
  1564. change S_mul.card + 1 ≤ P.nonTopSqCount
  1565. omega
  1566. have hmain := slp_well_spaced_mul_bound P hP L hws
  1567. rw [ha0, Nat.zero_add] at hmain
  1568. calc intRootCount P.evalPoly
  1569. ≤ 3 * 2 ^ nonSqMulCount P L L.length := hmain
  1570. _ ≤ 4 * 2 ^ nonSqMulCount P L L.length := Nat.mul_le_mul_right _ (by omega)
  1571. _ = 2 ^ (nonSqMulCount P L L.length + 2) := by rw [pow_succ, pow_succ]; ring
  1572. _ ≤ 2 ^ (P.nonTopSqCount + 1) := Nat.pow_le_pow_right (by omega) (by omega)
  1573. end Research.PvsNP.Tau