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

Research/PvsNP/Tau/GapDominanceBound.lean

2,384 lines68 declarationsno sorry
  1. module
  2. public import Research.PvsNP.Tau.PureSquaringCollapse
  3. public import Mathlib.Data.Nat.Log
  4. @[expose] public section
  5. namespace Research.PvsNP.Tau
  6. /-!
  7. # Unconditional Elementary `o(2^τ)` Upper Bound for All Constant-Free SLPs over `ℤ`
  8. This module proves an **unconditional, completely elementary, pure-`ℤ` sub-trivial upper bound**
  9. on the number of distinct integer roots of **every** nonzero polynomial computed by a
  10. constant-free straight-line program `P : SLP τ` over `{+, -, ×}` from `{1, X}`:
  11. `slp_intRootCount_o_two_pow`:
  12. For every `τ : ℕ` and every `P : SLP τ` with `P.evalPoly ≠ 0`,
  13. `τ * (intRootCount P.evalPoly) ^ 28 ≤ 2 ^ 40 * 2 ^ (28 * τ)`
  14. (equivalently, `z_ℤ(P) ≤ 2^(10/7) · 2^τ / τ^(1/28) = o(2^τ)`).
  15. The proof uses **zero** real analysis, **zero** Khovanskii/Pfaffian theory, and **zero**
  16. `p`-adic analysis. It combines:
  17. 1. **`slp_pure_squaring_collapse_deg`** (from `PureSquaringCollapse.lean`): when the number of
  18. non-(top-degree pure-squaring) gates `k = P.nonTopSqCount` satisfies `k ≥ K`, we immediately
  19. have `(intRootCount P.evalPoly) ^ 7 ≤ 2 ^ (7 * τ - 2 * K)`.
  20. 2. **Multi-Segment Gap-Dominance Theorem (`slp_multi_segment_gap_dominance`):** when `k < K`,
  21. the `τ - k` top-degree pure-squaring gates partition into at most `3 * k + 1` untapped
  22. unbranched pure-squaring segments `[a_s, b_s)`. A pure-combinatorial demotion lemma extracts
  23. a nonempty subset of surviving segments satisfying local gap inequalities
  24. `g_0 ≥ 2 * R_0 + R_1 + 5`, `g_r ≥ R_{r-1} + R_r + 3`, and `g_r ≥ Δ`.
  25. Treating each surviving segment's output `U_r = w_r ^ (2 ^ g_r)` as a formal indeterminate
  26. `T` over `ℤ[X]` and propagating Cauchy's root bound backward across the segments forces every
  27. integer root `x` of `P.evalPoly = 0` either to kill some leading coefficient `c_r(x) = 0`
  28. (of degree `≤ 2 ^ (τ - g_r)`) or to satisfy `w_0(x) ∈ {-1, 0, 1}` (at most `3 * 2 ^ R_0`
  29. roots), yielding `intRootCount P.evalPoly ≤ (q + 3) * 2 ^ (τ - Δ)`.
  30. -/
  31. open Polynomial
  32. /-! ## 1. Prefix Extraction, Gate Accessors, and Unbranched Top-Squaring Chains -/
  33. /-- Prefix of `P : SLP τ` consisting of its first `t` gates (`t ≤ τ`). -/
  34. def SLP.prefix : {τ : ℕ} → SLP τ → (t : ℕ) → t ≤ τ → SLP t
  35. | 0, .nil, 0, _ => .nil
  36. | _ + 1, .snoc P op i j, t, ht =>
  37. if h : t ≤ _ then
  38. P.prefix t h
  39. else
  40. have heq : t = _ + 1 := by omega
  41. heq ▸ SLP.snoc P op i j
  42. /-- `P.prefix τ le_rfl = P`. -/
  43. lemma SLP.prefix_self : ∀ {τ : ℕ} (P : SLP τ), P.prefix τ le_rfl = P
  44. | 0, .nil => rfl
  45. | τ + 1, .snoc P op i j => by
  46. have hnot : ¬ (τ + 1 ≤ τ) := by omega
  47. simp [SLP.prefix, hnot]
  48. /-- `(SLP.snoc P op i j).prefix t ht = P.prefix t h` whenever `t ≤ τ`. -/
  49. lemma SLP.prefix_snoc_le {τ : ℕ} (P : SLP τ) (op : SLPOp) (i j : Fin (τ + 2))
  50. {t : ℕ} (ht : t ≤ τ) :
  51. (SLP.snoc P op i j).prefix t (Nat.le_succ_of_le ht) = P.prefix t ht := by
  52. simp [SLP.prefix, ht]
  53. /-- `P.prefix` agrees with `P` on `evalWire` for all wires `< t + 2`. -/
  54. lemma SLP.evalWire_prefix :
  55. ∀ {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t ≤ τ) (w : Fin (t + 2)),
  56. (P.prefix t ht).evalWire w = P.evalWire ⟨w.val, by omega⟩
  57. | 0, .nil, 0, _, w => rfl
  58. | τ + 1, .snoc P op i j, t, ht, w => by
  59. by_cases h : t ≤ τ
  60. · rw [SLP.prefix_snoc_le P op i j h]
  61. have hlt : w.val < τ + 2 := by omega
  62. simp only [SLP.evalWire, hlt, ↓reduceDIte]
  63. exact P.evalWire_prefix t h w
  64. · have heq : t = τ + 1 := by omega
  65. subst heq
  66. rw [SLP.prefix_self]
  67. /-- Gate operation at 0-based gate index `t < τ`. -/
  68. def SLP.gateOp : {τ : ℕ} → SLP τ → (t : ℕ) → t < τ → SLPOp
  69. | _ + 1, .snoc P op _ _, t, _ =>
  70. if h : t < _ then P.gateOp t h else op
  71. /-- First input wire index (`Fin (t + 2)`) of gate `t < τ`. -/
  72. def SLP.gateIn1 : {τ : ℕ} → SLP τ → (t : ℕ) → t < τ → Fin (t + 2)
  73. | _ + 1, .snoc P _ i _, t, ht =>
  74. if h : t < _ then P.gateIn1 t h
  75. else ⟨i.val, by have := i.isLt; omega⟩
  76. /-- Second input wire index (`Fin (t + 2)`) of gate `t < τ`. -/
  77. def SLP.gateIn2 : {τ : ℕ} → SLP τ → (t : ℕ) → t < τ → Fin (t + 2)
  78. | _ + 1, .snoc P _ _ j, t, ht =>
  79. if h : t < _ then P.gateIn2 t h
  80. else ⟨j.val, by have := j.isLt; omega⟩
  81. /-- `P.prefix (t + 1) ht` equals `SLP.snoc (P.prefix t _) (P.gateOp t _) (P.gateIn1 t _) (P.gateIn2 t _)`. -/
  82. lemma SLP.prefix_succ_eq :
  83. ∀ {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t < τ),
  84. P.prefix (t + 1) ht =
  85. SLP.snoc (P.prefix t (le_of_lt ht)) (P.gateOp t ht) (P.gateIn1 t ht) (P.gateIn2 t ht)
  86. | τ + 1, .snoc P op i j, t, ht => by
  87. by_cases h : t < τ
  88. · rw [SLP.prefix_snoc_le P op i j h, SLP.prefix_snoc_le P op i j (le_of_lt h)]
  89. simp only [SLP.gateOp, SLP.gateIn1, SLP.gateIn2, h, ↓reduceDIte]
  90. exact P.prefix_succ_eq t h
  91. · have heq : t = τ := by omega
  92. subst heq
  93. rw [SLP.prefix_self, SLP.prefix_snoc_le P op i j le_rfl, SLP.prefix_self]
  94. simp [SLP.gateOp, SLP.gateIn1, SLP.gateIn2]
  95. /-- Wire `t + 2` of `P : SLP τ` (`t < τ`) is computed by applying `gateOp t` to `gateIn1 t` and `gateIn2 t`. -/
  96. lemma SLP.evalWire_gate {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t < τ) :
  97. P.evalWire ⟨t + 2, by omega⟩ =
  98. (P.gateOp t ht).applyPoly
  99. (P.evalWire ⟨(P.gateIn1 t ht).val, by have := (P.gateIn1 t ht).isLt; omega⟩)
  100. (P.evalWire ⟨(P.gateIn2 t ht).val, by have := (P.gateIn2 t ht).isLt; omega⟩) := by
  101. have h1 := (P.evalWire_prefix (t + 1) ht ⟨t + 2, by omega⟩).symm
  102. rw [h1, P.prefix_succ_eq t ht]
  103. have hnot : ¬ (t + 2 < t + 2) := by omega
  104. simp only [SLP.evalWire, hnot, ↓reduceDIte]
  105. rw [P.evalWire_prefix t (le_of_lt ht) (P.gateIn1 t ht),
  106. P.evalWire_prefix t (le_of_lt ht) (P.gateIn2 t ht)]
  107. /-- Predicate: gate `t < τ` of `P` is a top-degree pure squaring. -/
  108. def SLP.gateIsTopSq {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t < τ) : Prop :=
  109. isTopPureSquare (P.prefix t (le_of_lt ht)) (P.gateOp t ht) (P.gateIn1 t ht) (P.gateIn2 t ht)
  110. lemma SLP.gateIsTopSq_snoc_lt {τ : ℕ} (P : SLP τ) (op : SLPOp) (i j : Fin (τ + 2))
  111. {t : ℕ} (ht : t < τ) :
  112. (SLP.snoc P op i j).gateIsTopSq t (Nat.lt_succ_of_lt ht) ↔ P.gateIsTopSq t ht := by
  113. unfold SLP.gateIsTopSq
  114. rw [SLP.prefix_snoc_le P op i j (le_of_lt ht)]
  115. simp [SLP.gateOp, SLP.gateIn1, SLP.gateIn2, ht]
  116. lemma SLP.gateIsTopSq_snoc_last {τ : ℕ} (P : SLP τ) (op : SLPOp) (i j : Fin (τ + 2)) :
  117. (SLP.snoc P op i j).gateIsTopSq τ (Nat.lt_succ_self τ) ↔ isTopPureSquare P op i j := by
  118. unfold SLP.gateIsTopSq
  119. rw [SLP.prefix_snoc_le P op i j le_rfl, SLP.prefix_self]
  120. simp [SLP.gateOp, SLP.gateIn1, SLP.gateIn2]
  121. /-- `P.nonTopSqCount` equals the number of gates `t : Fin τ` that are not top-degree pure squarings. -/
  122. lemma SLP.nonTopSqCount_eq_card :
  123. ∀ {τ : ℕ} (P : SLP τ) [DecidablePred (fun t : Fin τ => ¬ P.gateIsTopSq t.val t.isLt)],
  124. P.nonTopSqCount =
  125. (Finset.univ.filter (fun t : Fin τ => ¬ P.gateIsTopSq t.val t.isLt)).card
  126. | 0, .nil, _ => by simp [SLP.nonTopSqCount]
  127. | τ + 1, .snoc P op i j, _ => by
  128. classical
  129. have ih := P.nonTopSqCount_eq_card
  130. let S_all : Finset (Fin (τ + 1)) :=
  131. Finset.univ.filter (fun t : Fin (τ + 1) => ¬ (SLP.snoc P op i j).gateIsTopSq t.val t.isLt)
  132. let S_prev : Finset (Fin τ) :=
  133. Finset.univ.filter (fun t : Fin τ => ¬ P.gateIsTopSq t.val t.isLt)
  134. let f : Fin τ ↪ Fin (τ + 1) :=
  135. ⟨fun t => ⟨t.val, Nat.lt_succ_of_lt t.isLt⟩, fun a b h => Fin.ext (by simpa using congrArg Fin.val h)⟩
  136. let last : Fin (τ + 1) := ⟨τ, Nat.lt_succ_self τ⟩
  137. have hlast_not : last ∉ S_prev.map f := by
  138. intro hm
  139. rcases Finset.mem_map.mp hm with ⟨t, _, ht⟩
  140. have hval : t.val = τ := congrArg Fin.val ht
  141. have := t.isLt
  142. omega
  143. have hsplit : S_all =
  144. if isTopPureSquare P op i j then S_prev.map f else insert last (S_prev.map f) := by
  145. ext t
  146. by_cases hlt : t.val < τ
  147. · have hne : t ≠ last := fun h => by have := congrArg Fin.val h; dsimp [last] at this; omega
  148. have hmem_map : t ∈ S_prev.map f ↔ ¬ P.gateIsTopSq t.val hlt := by
  149. constructor
  150. · intro hm
  151. rcases Finset.mem_map.mp hm with ⟨u, hu, hut⟩
  152. have hu_eq : u.val = t.val := congrArg Fin.val hut
  153. have hu' : ¬ P.gateIsTopSq u.val u.isLt := (Finset.mem_filter.mp hu).2
  154. simpa [hu_eq] using hu'
  155. · intro hnot
  156. exact Finset.mem_map.mpr ⟨⟨t.val, hlt⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, hnot⟩, Fin.ext rfl⟩
  157. split_ifs with htop
  158. · simp only [S_all, Finset.mem_filter, Finset.mem_univ, true_and, hmem_map]
  159. exact not_congr (SLP.gateIsTopSq_snoc_lt P op i j hlt)
  160. · simp only [S_all, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert, hne,
  161. false_or, hmem_map]
  162. exact not_congr (SLP.gateIsTopSq_snoc_lt P op i j hlt)
  163. · have ht_eq : t = last := Fin.ext (by dsimp [last]; omega)
  164. subst ht_eq
  165. have htop_iff : (SLP.snoc P op i j).gateIsTopSq τ (Nat.lt_succ_self τ) ↔
  166. isTopPureSquare P op i j := SLP.gateIsTopSq_snoc_last P op i j
  167. split_ifs with htop
  168. · simp [S_all, last, htop_iff, htop, hlast_not]
  169. · simp [S_all, last, htop_iff, htop]
  170. have hcard_all : S_all.card = P.nonTopSqCount + if isTopPureSquare P op i j then 0 else 1 := by
  171. rw [hsplit, ih]
  172. split_ifs
  173. · simp [S_prev]
  174. · rw [Finset.card_insert_of_notMem hlast_not, Finset.card_map]
  175. have hcnt : (SLP.snoc P op i j).nonTopSqCount =
  176. P.nonTopSqCount + if isTopPureSquare P op i j then 0 else 1 := by
  177. by_cases h : isTopPureSquare P op i j <;> simp [SLP.nonTopSqCount, h]
  178. rw [hcnt, ← hcard_all]
  179. /-- Maximum wire degree `maxDeg` is at least `1` and at most `2 ^ τ`, and bounds all wire degrees. -/
  180. lemma SLP.one_le_maxDeg {τ : ℕ} (P : SLP τ) : 1 ≤ P.maxDeg := (P.topTwoDegs_inv).1
  181. lemma SLP.wireDeg_le_maxDeg {τ : ℕ} (P : SLP τ) (w : Fin (τ + 2)) :
  182. (P.evalWire w).natDegree ≤ P.maxDeg :=
  183. (P.topTwoDegs_inv).2.2.1 w
  184. lemma SLP.maxDeg_le_two_pow : ∀ {τ : ℕ} (P : SLP τ), P.maxDeg ≤ 2 ^ τ
  185. | 0, .nil => by simp [SLP.maxDeg, SLP.topTwoDegs]
  186. | τ + 1, .snoc P op i j => by
  187. have ih := P.maxDeg_le_two_pow
  188. have h1 : (P.evalWire i).natDegree ≤ P.maxDeg := P.wireDeg_le_maxDeg i
  189. have h2 : (P.evalWire j).natDegree ≤ P.maxDeg := P.wireDeg_le_maxDeg j
  190. have hd : (op.applyPoly (P.evalWire i) (P.evalWire j)).natDegree ≤ 2 * P.maxDeg := by
  191. cases op
  192. · exact le_trans (Polynomial.natDegree_add_le _ _) (by omega)
  193. · exact le_trans (Polynomial.natDegree_sub_le _ _) (by omega)
  194. · exact le_trans Polynomial.natDegree_mul_le (by omega)
  195. have hM : (SLP.snoc P op i j).maxDeg =
  196. max P.maxDeg (op.applyPoly (P.evalWire i) (P.evalWire j)).natDegree := by
  197. change (if P.maxDeg ≤ (op.applyPoly (P.evalWire i) (P.evalWire j)).natDegree
  198. then ((op.applyPoly (P.evalWire i) (P.evalWire j)).natDegree, P.maxDeg)
  199. else (P.maxDeg, max P.secondDeg (op.applyPoly (P.evalWire i) (P.evalWire j)).natDegree)).1 = _
  200. split_ifs with h <;> omega
  201. rw [hM, pow_succ]
  202. omega
  203. /-- Monotonicity of `maxDeg` along prefixes. -/
  204. lemma SLP.maxDeg_prefix_mono {τ : ℕ} (P : SLP τ) {t1 t2 : ℕ} (h12 : t1 ≤ t2) (ht2 : t2 ≤ τ) :
  205. (P.prefix t1 (le_trans h12 ht2)).maxDeg ≤ (P.prefix t2 ht2).maxDeg := by
  206. obtain ⟨w, hw⟩ := (P.prefix t1 (le_trans h12 ht2)).exists_wire_eq_maxDeg
  207. have hle := (P.prefix t2 ht2).wireDeg_le_maxDeg ⟨w.val, by omega⟩
  208. have heq : (P.prefix t2 ht2).evalWire ⟨w.val, by omega⟩ =
  209. (P.prefix t1 (le_trans h12 ht2)).evalWire w := by
  210. rw [P.evalWire_prefix t2 ht2 ⟨w.val, by omega⟩, P.evalWire_prefix t1 (le_trans h12 ht2) w]
  211. rw [heq] at hle
  212. rw [← hw]
  213. exact hle
  214. /-- If gate `t` is a top-degree pure squaring, then wire `t + 2` has degree `2 * D₁(t)`,
  215. `D₁(t + 1) = 2 * D₁(t)`, and EVERY wire `< t + 2` has degree strictly less than `D₁(t + 1)`. -/
  216. lemma SLP.topSq_strict_maxDeg {τ : ℕ} (P : SLP τ) {t : ℕ} (ht : t < τ)
  217. (htop : P.gateIsTopSq t ht) :
  218. (P.evalWire ⟨t + 2, by omega⟩).natDegree = 2 * (P.prefix t (le_of_lt ht)).maxDeg ∧
  219. (P.prefix (t + 1) ht).maxDeg = 2 * (P.prefix t (le_of_lt ht)).maxDeg ∧
  220. (∀ w : Fin (t + 2), (P.evalWire ⟨w.val, by omega⟩).natDegree < (P.prefix (t + 1) ht).maxDeg) := by
  221. rcases htop with ⟨hop, hij, hdeg⟩
  222. set P0 := P.prefix t (le_of_lt ht)
  223. set wi := P0.evalWire (P.gateIn1 t ht)
  224. have hD1_pos : 1 ≤ P0.maxDeg := P0.one_le_maxDeg
  225. have hwi_ne : wi ≠ 0 := by
  226. intro h0
  227. have : wi.natDegree = 0 := by simp [h0]
  228. have : P0.maxDeg = 0 := by rw [← hdeg]; exact this
  229. omega
  230. have hwire_eq : P.evalWire ⟨t + 2, by omega⟩ = wi * wi := by
  231. have h1 : P.evalWire ⟨(P.gateIn1 t ht).val, by have := (P.gateIn1 t ht).isLt; omega⟩ = wi :=
  232. (P.evalWire_prefix t (le_of_lt ht) (P.gateIn1 t ht)).symm
  233. have h2 : P.evalWire ⟨(P.gateIn2 t ht).val, by have := (P.gateIn2 t ht).isLt; omega⟩ = wi := by
  234. rw [← P.evalWire_prefix t (le_of_lt ht) (P.gateIn2 t ht), ← hij]
  235. rw [P.evalWire_gate t ht, hop]
  236. simp only [SLPOp.applyPoly, h1, h2]
  237. have hsq_deg : (P.evalWire ⟨t + 2, by omega⟩).natDegree = 2 * P0.maxDeg := by
  238. rw [hwire_eq, Polynomial.natDegree_mul hwi_ne hwi_ne]
  239. have : wi.natDegree = P0.maxDeg := hdeg
  240. omega
  241. have hM1 : (P.prefix (t + 1) ht).maxDeg = 2 * P0.maxDeg := by
  242. rw [P.prefix_succ_eq t ht]
  243. have hd_eq : ((P.gateOp t ht).applyPoly (P0.evalWire (P.gateIn1 t ht))
  244. (P0.evalWire (P.gateIn2 t ht))).natDegree = 2 * P0.maxDeg := by
  245. rw [hop, ← hij]
  246. simp only [SLPOp.applyPoly]
  247. change (wi * wi).natDegree = 2 * P0.maxDeg
  248. rw [Polynomial.natDegree_mul hwi_ne hwi_ne]
  249. have : wi.natDegree = P0.maxDeg := hdeg
  250. omega
  251. have hle : P0.maxDeg ≤ 2 * P0.maxDeg := by omega
  252. change (if P0.maxDeg ≤ ((P.gateOp t ht).applyPoly (P0.evalWire (P.gateIn1 t ht))
  253. (P0.evalWire (P.gateIn2 t ht))).natDegree
  254. then (((P.gateOp t ht).applyPoly (P0.evalWire (P.gateIn1 t ht))
  255. (P0.evalWire (P.gateIn2 t ht))).natDegree, P0.maxDeg)
  256. else _).1 = 2 * P0.maxDeg
  257. rw [hd_eq]; simp [hle]
  258. refine ⟨hsq_deg, hM1, fun w => ?_⟩
  259. rw [hM1, ← P.evalWire_prefix t (le_of_lt ht) w]
  260. have hle_w := P0.wireDeg_le_maxDeg w
  261. change (P0.evalWire w).natDegree < 2 * P0.maxDeg
  262. omega
  263. /-- **Unbranched Chain Lemma:** If both gates `t` and `t + 1` are top-degree pure squarings,
  264. then gate `t + 1` MUST square wire `t + 2` (the output of gate `t`). -/
  265. lemma SLP.topSq_next_input_eq {τ : ℕ} (P : SLP τ) {t : ℕ} (ht1 : t + 1 < τ)
  266. (htop0 : P.gateIsTopSq t (by omega)) (htop1 : P.gateIsTopSq (t + 1) ht1) :
  267. (P.gateIn1 (t + 1) ht1).val = t + 2 ∧ (P.gateIn2 (t + 1) ht1).val = t + 2 := by
  268. have hstrict := (P.topSq_strict_maxDeg (by omega) htop0).2.2
  269. rcases htop1 with ⟨_, hij, hdeg1⟩
  270. have hin_eq : (P.gateIn1 (t + 1) ht1).val = t + 2 := by
  271. by_contra hne
  272. have hlt : (P.gateIn1 (t + 1) ht1).val < t + 2 := by
  273. have := (P.gateIn1 (t + 1) ht1).isLt; omega
  274. have hlt_deg := hstrict ⟨(P.gateIn1 (t + 1) ht1).val, hlt⟩
  275. dsimp only at hlt_deg
  276. have heq_w : (P.prefix (t + 1) (le_of_lt ht1)).evalWire (P.gateIn1 (t + 1) ht1) =
  277. P.evalWire ⟨(P.gateIn1 (t + 1) ht1).val, by omega⟩ :=
  278. P.evalWire_prefix (t + 1) (le_of_lt ht1) (P.gateIn1 (t + 1) ht1)
  279. have : (P.evalWire ⟨(P.gateIn1 (t + 1) ht1).val, by omega⟩).natDegree =
  280. (P.prefix (t + 1) (le_of_lt ht1)).maxDeg := by
  281. rw [← heq_w]; exact hdeg1
  282. omega
  283. exact ⟨hin_eq, by rw [← hij]; exact hin_eq⟩
  284. /-- **Contiguous Top-Squaring Run Evaluation:** If every gate `t ∈ [a, b)` (`a < b ≤ τ`) is a
  285. top-degree pure squaring, then wire `b + 1` equals `(P.evalWire w_in) ^ (2 ^ (b - a))`
  286. where `w_in = P.gateIn1 a _`, and its degree is `2 ^ (b - a) * D₁(a)`. -/
  287. lemma SLP.topSq_run_eval {τ : ℕ} (P : SLP τ) {a b : ℕ} (hab : a < b) (hb : b ≤ τ)
  288. (hrun : ∀ t (ht : t < τ), a ≤ t → t < b → P.gateIsTopSq t ht) :
  289. P.evalWire ⟨b + 1, by omega⟩ =
  290. (P.evalWire ⟨(P.gateIn1 a (by omega)).val, by have := (P.gateIn1 a (by omega)).isLt; omega⟩) ^
  291. (2 ^ (b - a)) ∧
  292. (P.evalWire ⟨b + 1, by omega⟩).natDegree =
  293. 2 ^ (b - a) * (P.prefix a (by omega)).maxDeg := by
  294. induction b, hab using Nat.le_induction with
  295. | base =>
  296. have htop := hrun a (by omega) le_rfl (by omega)
  297. have hsq := P.topSq_strict_maxDeg (by omega) htop
  298. rcases htop with ⟨hop, hij, _⟩
  299. have heq : P.evalWire ⟨a + 1 + 1, by omega⟩ =
  300. (P.evalWire ⟨(P.gateIn1 a (by omega)).val, by have := (P.gateIn1 a (by omega)).isLt; omega⟩) ^
  301. (2 ^ (a + 1 - a)) := by
  302. have hgate := P.evalWire_gate a (by omega)
  303. have h2eq : P.evalWire ⟨(P.gateIn2 a (by omega)).val, by have := (P.gateIn2 a (by omega)).isLt; omega⟩ =
  304. P.evalWire ⟨(P.gateIn1 a (by omega)).val, by have := (P.gateIn1 a (by omega)).isLt; omega⟩ := by
  305. have hval : (P.gateIn2 a (by omega)).val = (P.gateIn1 a (by omega)).val :=
  306. congrArg Fin.val hij.symm
  307. congr 1; exact Fin.ext hval
  308. rw [hop] at hgate
  309. simp only [SLPOp.applyPoly] at hgate
  310. rw [h2eq] at hgate
  311. have hsub : a + 1 - a = 1 := by omega
  312. rw [hsub, pow_one, sq]
  313. simpa using hgate
  314. refine ⟨heq, ?_⟩
  315. have hsub : a + 1 - a = 1 := by omega
  316. rw [hsub, pow_one]
  317. exact hsq.1
  318. | succ b' hb' ih =>
  319. have hprev := ih (by omega) (fun t ht h1 h2 => hrun t ht h1 (by omega))
  320. have htop_prev := hrun (b' - 1) (by omega) (by omega) (by omega)
  321. have htop_cur := hrun b' (by omega) (by omega) (by omega)
  322. have hprev_succ : (b' - 1) + 1 = b' := by omega
  323. have hin := P.topSq_next_input_eq (t := b' - 1) (by omega) htop_prev (by simpa [hprev_succ] using htop_cur)
  324. have hgate1_congr : ∀ {t1 t2 : ℕ} (h1 : t1 < τ) (h2 : t2 < τ), t1 = t2 →
  325. (P.gateIn1 t1 h1).val = (P.gateIn1 t2 h2).val := by rintro t1 _ h1 h2 rfl; rfl
  326. have hgate2_congr : ∀ {t1 t2 : ℕ} (h1 : t1 < τ) (h2 : t2 < τ), t1 = t2 →
  327. (P.gateIn2 t1 h1).val = (P.gateIn2 t2 h2).val := by rintro t1 _ h1 h2 rfl; rfl
  328. have hin1 : (P.gateIn1 b' (by omega)).val = b' + 1 := by
  329. have h1 := hin.1
  330. rw [hgate1_congr (by omega) (by omega) hprev_succ] at h1
  331. omega
  332. have hin2 : (P.gateIn2 b' (by omega)).val = b' + 1 := by
  333. have h2 := hin.2
  334. rw [hgate2_congr (by omega) (by omega) hprev_succ] at h2
  335. omega
  336. have hop : P.gateOp b' (by omega) = .mul := htop_cur.1
  337. have hgate := P.evalWire_gate b' (by omega)
  338. rw [hop] at hgate
  339. simp only [SLPOp.applyPoly] at hgate
  340. have hwire_sq : P.evalWire ⟨b' + 1 + 1, by omega⟩ =
  341. P.evalWire ⟨b' + 1, by omega⟩ * P.evalWire ⟨b' + 1, by omega⟩ := by
  342. simpa [hin1, hin2] using hgate
  343. have hsub : b' + 1 - a = (b' - a) + 1 := by omega
  344. refine ⟨?_, ?_⟩
  345. · rw [hwire_sq, hprev.1, hsub, pow_succ, pow_mul, sq]
  346. · have hne : P.evalWire ⟨b' + 1, by omega⟩ ≠ 0 := by
  347. intro h0
  348. have : (P.evalWire ⟨b' + 1, by omega⟩).natDegree = 0 := by simp [h0]
  349. rw [hprev.2] at this
  350. have hpos1 : 1 ≤ 2 ^ (b' - a) := Nat.one_le_pow _ _ (by omega)
  351. have hpos2 : 1 ≤ (P.prefix a (by omega)).maxDeg := (P.prefix a (by omega)).one_le_maxDeg
  352. nlinarith
  353. rw [hwire_sq, Polynomial.natDegree_mul hne hne, hprev.2, hsub, pow_succ]
  354. ring
  355. /-! ## 2. Untapped Segments & Pure-Combinatorial Demotion Lemma -/
  356. /-- An untapped pure-squaring segment `[a, b)` (`a < b ≤ τ`) of `P : SLP τ`:
  357. every gate in `[a, b)` is a top-degree pure squaring, and no gate `t' ≥ b` reads any strict
  358. intermediate wire in `{a + 2, ..., b}`. -/
  359. structure UntappedSeg {τ : ℕ} (P : SLP τ) where
  360. a : ℕ
  361. b : ℕ
  362. hab : a < b
  363. hb : b ≤ τ
  364. hrun : ∀ t (ht : t < τ), a ≤ t → t < b → P.gateIsTopSq t ht
  365. huntapped : ∀ t' (ht' : t' < τ), b ≤ t' →
  366. ¬ (a + 2 ≤ (P.gateIn1 t' ht').val ∧ (P.gateIn1 t' ht').val ≤ b) ∧
  367. ¬ (a + 2 ≤ (P.gateIn2 t' ht').val ∧ (P.gateIn2 t' ht').val ≤ b)
  368. /-- A valid ordered list of disjoint untapped segments inside `[0, τ)`. -/
  369. def SegsOrdered {τ : ℕ} {P : SLP τ} : List (UntappedSeg P) → Prop
  370. | [] => True
  371. | [_] => True
  372. | s1 :: s2 :: rest => s1.b ≤ s2.a ∧ SegsOrdered (s2 :: rest)
  373. /-- Total length of a list of segments. -/
  374. def segsTotalLen {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) : ℕ :=
  375. (L.map (fun s => s.b - s.a)).sum
  376. private lemma segsOrdered_head_le {τ : ℕ} {P : SLP τ} :
  377. ∀ (s0 : UntappedSeg P) (rest : List (UntappedSeg P)),
  378. SegsOrdered (s0 :: rest) → ∀ s ∈ s0 :: rest, s0.a ≤ s.a
  379. | s0, [], _, s, hs => by simp at hs; subst hs; exact le_rfl
  380. | s0, s1 :: rest, ⟨h01, hrest⟩, s, hs => by
  381. rcases List.mem_cons.mp hs with rfl | hs'
  382. · exact le_rfl
  383. · exact le_trans (le_of_lt s0.hab) (le_trans h01 (segsOrdered_head_le s1 rest hrest s hs'))
  384. /-- Total length of ordered disjoint segments inside `[0, τ)` is at most `τ`. -/
  385. private lemma segsTotalLen_le_sub {τ : ℕ} {P : SLP τ} :
  386. ∀ (L : List (UntappedSeg P)), SegsOrdered L →
  387. ∀ m : ℕ, (∀ s ∈ L, m ≤ s.a) → segsTotalLen L ≤ τ - m
  388. | [], _, _, _ => Nat.zero_le _
  389. | [s], _, m, hm => by
  390. have := hm s (by simp)
  391. have := s.hab; have := s.hb
  392. simp [segsTotalLen]; omega
  393. | s1 :: s2 :: rest, ⟨h12, hrest⟩, m, hm => by
  394. have hm1 : m ≤ s1.a := hm s1 (by simp)
  395. have hall2 : ∀ s ∈ s2 :: rest, s1.b ≤ s.a := fun s hs =>
  396. le_trans h12 (segsOrdered_head_le s2 rest hrest s hs)
  397. have ih := segsTotalLen_le_sub (s2 :: rest) hrest s1.b hall2
  398. have := s1.hab; have := s1.hb
  399. simp only [segsTotalLen, List.map_cons, List.sum_cons] at ih ⊢
  400. omega
  401. lemma segsTotalLen_le {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (hord : SegsOrdered L) :
  402. segsTotalLen L ≤ τ := by
  403. simpa using segsTotalLen_le_sub L hord 0 (fun _ _ => Nat.zero_le _)
  404. /-- Pairwise non-overlapping ordering on a list of intervals `(a, b)`. -/
  405. private def PairsOrdered : List (ℕ × ℕ) → Prop
  406. | [] => True
  407. | [_] => True
  408. | p1 :: p2 :: rest => p1.2 ≤ p2.1 ∧ PairsOrdered (p2 :: rest)
  409. private lemma pairsOrdered_append_singleton :
  410. ∀ (L : List (ℕ × ℕ)) (q : ℕ × ℕ),
  411. PairsOrdered L → (∀ p, L.getLast? = some p → p.2 ≤ q.1) → PairsOrdered (L ++ [q])
  412. | [], _, _, _ => trivial
  413. | [p], q, _, hlast => ⟨hlast p rfl, trivial⟩
  414. | p1 :: p2 :: rest, q, ⟨h12, hrest⟩, hlast =>
  415. ⟨h12, pairsOrdered_append_singleton (p2 :: rest) q hrest (fun p hp => hlast p (by simpa using hp))⟩
  416. private lemma pairsOrdered_replace_last :
  417. ∀ (L : List (ℕ × ℕ)) (p q : ℕ × ℕ),
  418. PairsOrdered (L ++ [p]) → p.1 = q.1 → PairsOrdered (L ++ [q])
  419. | [], _, _, _, _ => trivial
  420. | [r], p, q, ⟨hrp, _⟩, hpq => ⟨by omega, trivial⟩
  421. | r1 :: r2 :: rest, p, q, ⟨h12, hrest⟩, hpq =>
  422. ⟨h12, pairsOrdered_replace_last (r2 :: rest) p q hrest hpq⟩
  423. /-- Clean strengthened interval-partition induction:
  424. `L.length ≤ bad + cut + (if L ends at n with ¬ isCut (n - 1) then 1 else 0)`. -/
  425. private lemma partition_intervals_strong (isGood isCut : ℕ → Prop)
  426. [DecidablePred isGood] [DecidablePred isCut] :
  427. ∀ n : ℕ,
  428. ∃ L : List (ℕ × ℕ),
  429. (∀ p ∈ L, p.1 < p.2 ∧ p.2 ≤ n) ∧
  430. PairsOrdered L ∧
  431. (∀ p ∈ L, ∀ t, p.1 ≤ t → t < p.2 → isGood t) ∧
  432. (∀ p ∈ L, ∀ t, p.1 ≤ t → t + 1 < p.2 → ¬ isCut t) ∧
  433. ((L.map (fun p => p.2 - p.1)).sum = ((Finset.range n).filter isGood).card) ∧
  434. (L.length ≤ ((Finset.range n).filter (fun t => ¬ isGood t)).card +
  435. ((Finset.range n).filter (fun t => isGood t ∧ isCut t)).card +
  436. if (∃ p, L.getLast? = some p ∧ p.2 = n ∧ ¬ isCut (n - 1)) then 1 else 0) := by
  437. intro n
  438. induction n with
  439. | zero =>
  440. refine ⟨[], by simp, trivial, by simp, by simp, by simp, by simp⟩
  441. | succ m ih =>
  442. obtain ⟨L, hbounds, hchain, hgood, hnocut, hsum, hlen⟩ := ih
  443. have hrange_good : ((Finset.range (m + 1)).filter isGood).card =
  444. ((Finset.range m).filter isGood).card + if isGood m then 1 else 0 := by
  445. by_cases h : isGood m <;> simp [Finset.range_add_one, Finset.filter_insert, h]
  446. have hrange_bad : ((Finset.range (m + 1)).filter (fun t => ¬ isGood t)).card =
  447. ((Finset.range m).filter (fun t => ¬ isGood t)).card + if ¬ isGood m then 1 else 0 := by
  448. by_cases h : isGood m <;> simp [Finset.range_add_one, Finset.filter_insert, h]
  449. have hrange_cut : ((Finset.range (m + 1)).filter (fun t => isGood t ∧ isCut t)).card =
  450. ((Finset.range m).filter (fun t => isGood t ∧ isCut t)).card +
  451. if isGood m ∧ isCut m then 1 else 0 := by
  452. by_cases h : isGood m ∧ isCut m <;> simp [Finset.range_add_one, Finset.filter_insert, h]
  453. by_cases hgm : isGood m
  454. · by_cases hext : ∃ p, L.getLast? = some p ∧ p.2 = m ∧ ¬ isCut (m - 1)
  455. · rcases hext with ⟨p, hlast, hp2, hnc_prev⟩
  456. have hL_ne : L ≠ [] := by intro h; simp [h] at hlast
  457. have hL_eq : L = L.dropLast ++ [p] := by
  458. have := List.dropLast_append_getLast hL_ne
  459. have hgl : L.getLast hL_ne = p := by
  460. rw [List.getLast?_eq_some_getLast hL_ne] at hlast
  461. exact Option.some.inj hlast
  462. rw [hgl] at this
  463. exact this.symm
  464. set L0 := L.dropLast
  465. have hbounds0 : ∀ q ∈ L0 ++ [p], q.1 < q.2 ∧ q.2 ≤ m := by rw [← hL_eq]; exact hbounds
  466. have hchain0 : PairsOrdered (L0 ++ [p]) := by rw [← hL_eq]; exact hchain
  467. have hgood0 : ∀ q ∈ L0 ++ [p], ∀ t, q.1 ≤ t → t < q.2 → isGood t := by rw [← hL_eq]; exact hgood
  468. have hnocut0 : ∀ q ∈ L0 ++ [p], ∀ t, q.1 ≤ t → t + 1 < q.2 → ¬ isCut t := by rw [← hL_eq]; exact hnocut
  469. have hsum0 : ((L0 ++ [p]).map (fun q => q.2 - q.1)).sum = ((Finset.range m).filter isGood).card := by
  470. rw [← hL_eq]; exact hsum
  471. refine ⟨L0 ++ [(p.1, m + 1)], ?_, ?_, ?_, ?_, ?_, ?_⟩
  472. · intro q hq
  473. rcases List.mem_append.mp hq with hq0 | hq1
  474. · have := hbounds0 q (List.mem_append_left _ hq0); omega
  475. · simp at hq1; subst hq1
  476. have := hbounds0 p (by simp); omega
  477. · exact pairsOrdered_replace_last L0 p (p.1, m + 1) hchain0 rfl
  478. · intro q hq t h1 h2
  479. rcases List.mem_append.mp hq with hq0 | hq1
  480. · exact hgood0 q (List.mem_append_left _ hq0) t h1 h2
  481. · simp at hq1; subst hq1
  482. by_cases htm : t = m
  483. · subst htm; exact hgm
  484. · exact hgood0 p (by simp) t h1 (by omega)
  485. · intro q hq t h1 h2
  486. rcases List.mem_append.mp hq with hq0 | hq1
  487. · exact hnocut0 q (List.mem_append_left _ hq0) t h1 h2
  488. · simp at hq1; subst hq1
  489. by_cases htm : t = m - 1
  490. · subst htm; exact hnc_prev
  491. · exact hnocut0 p (by simp) t h1 (by omega)
  492. · have hp_lt := (hbounds0 p (by simp)).1
  493. simp only [List.map_append, List.sum_append, List.map_singleton, List.sum_singleton] at hsum0 ⊢
  494. rw [hrange_good]; simp only [hgm, ↓reduceIte]; omega
  495. · have hlen_eq : (L0 ++ [(p.1, m + 1)]).length = L.length := by
  496. rw [hL_eq]; simp
  497. rw [hlen_eq, hrange_bad, hrange_cut]
  498. have hprev_cond : ∃ p', L.getLast? = some p' ∧ p'.2 = m ∧ ¬ isCut (m - 1) :=
  499. ⟨p, hlast, hp2, hnc_prev⟩
  500. simp only [hprev_cond, ↓reduceIte] at hlen
  501. have hnew_last : (L0 ++ [(p.1, m + 1)]).getLast? = some (p.1, m + 1) := by simp
  502. by_cases hcm : isCut m
  503. · have h2 : ¬ (∃ q, (L0 ++ [(p.1, m + 1)]).getLast? = some q ∧ q.2 = m + 1 ∧ ¬ isCut (m + 1 - 1)) := by
  504. rintro ⟨q, _, _, hnc⟩; simp at hnc; exact hnc hcm
  505. simp only [hgm, hcm, and_self, not_true_eq_false, ↓reduceIte, h2]
  506. omega
  507. · have h2 : ∃ q, (L0 ++ [(p.1, m + 1)]).getLast? = some q ∧ q.2 = m + 1 ∧ ¬ isCut (m + 1 - 1) :=
  508. ⟨(p.1, m + 1), hnew_last, rfl, by simpa using hcm⟩
  509. simp only [hgm, hcm, and_false, not_true_eq_false, ↓reduceIte, h2]
  510. omega
  511. · -- Not extendable: append (m, m + 1)
  512. refine ⟨L ++ [(m, m + 1)], ?_, ?_, ?_, ?_, ?_, ?_⟩
  513. · intro q hq
  514. rcases List.mem_append.mp hq with hq0 | hq1
  515. · have := hbounds q hq0; omega
  516. · simp at hq1; subst hq1; omega
  517. · exact pairsOrdered_append_singleton L (m, m + 1) hchain
  518. (fun x hx => (hbounds x (List.mem_of_getLast? hx)).2)
  519. · intro q hq t h1 h2
  520. rcases List.mem_append.mp hq with hq0 | hq1
  521. · exact hgood q hq0 t h1 h2
  522. · simp at hq1; subst hq1
  523. have : t = m := by omega
  524. subst this; exact hgm
  525. · intro q hq t h1 h2
  526. rcases List.mem_append.mp hq with hq0 | hq1
  527. · exact hnocut q hq0 t h1 h2
  528. · simp at hq1; subst hq1; omega
  529. · simp only [List.map_append, List.sum_append, List.map_singleton, List.sum_singleton]
  530. rw [hsum, hrange_good]; simp only [hgm, ↓reduceIte]; omega
  531. · simp only [List.length_append, List.length_singleton]
  532. rw [hrange_bad, hrange_cut]
  533. simp only [hext, ↓reduceIte] at hlen
  534. have hnew_last : (L ++ [(m, m + 1)]).getLast? = some (m, m + 1) := by simp
  535. by_cases hcm : isCut m
  536. · have h2 : ¬ (∃ q, (L ++ [(m, m + 1)]).getLast? = some q ∧ q.2 = m + 1 ∧ ¬ isCut (m + 1 - 1)) := by
  537. rintro ⟨q, _, _, hnc⟩; simp at hnc; exact hnc hcm
  538. simp only [hgm, hcm, and_self, not_true_eq_false, ↓reduceIte, h2]
  539. omega
  540. · have h2 : ∃ q, (L ++ [(m, m + 1)]).getLast? = some q ∧ q.2 = m + 1 ∧ ¬ isCut (m + 1 - 1) :=
  541. ⟨(m, m + 1), hnew_last, rfl, by simpa using hcm⟩
  542. simp only [hgm, hcm, and_false, not_true_eq_false, ↓reduceIte, h2]
  543. omega
  544. · -- Gate m is NOT good: keep L unchanged!
  545. refine ⟨L, ?_, hchain, hgood, hnocut, ?_, ?_⟩
  546. · intro q hq; have := hbounds q hq; omega
  547. · rw [hsum, hrange_good]; simp [hgm]
  548. · rw [hrange_bad, hrange_cut]
  549. have hnot_new : ¬ (∃ q, L.getLast? = some q ∧ q.2 = m + 1 ∧ ¬ isCut (m + 1 - 1)) := by
  550. rintro ⟨q, hq, hq2, _⟩
  551. have := (hbounds q (List.mem_of_getLast? hq)).2
  552. omega
  553. simp only [hgm, not_false_eq_true, false_and, ↓reduceIte, hnot_new]
  554. split_ifs at hlen <;> omega
  555. /-- **Untapped Segment Extraction Lemma:** Every `P : SLP τ` admits an ordered list `L` of
  556. disjoint untapped pure-squaring segments with `L.length ≤ 3 * P.nonTopSqCount + 1` and
  557. `segsTotalLen L = τ - P.nonTopSqCount`. -/
  558. lemma extract_untapped_segments {τ : ℕ} (P : SLP τ) :
  559. ∃ L : List (UntappedSeg P),
  560. SegsOrdered L ∧
  561. L.length ≤ 3 * P.nonTopSqCount + 1 ∧
  562. segsTotalLen L = τ - P.nonTopSqCount := by
  563. classical
  564. let isGood : ℕ → Prop := fun t => ∃ ht : t < τ, P.gateIsTopSq t ht
  565. let isCut : ℕ → Prop := fun t =>
  566. ∃ (t' : ℕ) (ht' : t' < τ), ¬ P.gateIsTopSq t' ht' ∧
  567. ((P.gateIn1 t' ht').val = t + 2 ∨ (P.gateIn2 t' ht').val = t + 2)
  568. obtain ⟨L0, hbounds, hchain, hgood, hnocut, hsum, hlen⟩ :=
  569. partition_intervals_strong isGood isCut τ
  570. -- Count bad gates = P.nonTopSqCount
  571. have hbad_card : ((Finset.range τ).filter (fun t => ¬ isGood t)).card = P.nonTopSqCount := by
  572. rw [P.nonTopSqCount_eq_card]
  573. refine Finset.card_bij (fun t ht => (⟨t, Finset.mem_range.mp (Finset.mem_filter.mp ht).1⟩ : Fin τ))
  574. ?_ ?_ ?_
  575. · intro t ht
  576. have h1 := Finset.mem_filter.mp ht
  577. have ht_lt : t < τ := Finset.mem_range.mp h1.1
  578. refine Finset.mem_filter.mpr ⟨Finset.mem_univ _, fun htop => h1.2 ⟨ht_lt, htop⟩⟩
  579. · intro a _ b _ h; exact congrArg Fin.val h
  580. · intro u hu
  581. have hnot := (Finset.mem_filter.mp hu).2
  582. refine ⟨u.val, Finset.mem_filter.mpr ⟨Finset.mem_range.mpr u.isLt, ?_⟩, Fin.ext rfl⟩
  583. rintro ⟨_, htop⟩; exact hnot htop
  584. -- Count good gates = τ - P.nonTopSqCount
  585. have hgood_card : ((Finset.range τ).filter isGood).card = τ - P.nonTopSqCount := by
  586. have hcomp := Finset.card_filter_add_card_filter_not (s := Finset.range τ) (p := isGood)
  587. rw [Finset.card_range, hbad_card] at hcomp
  588. omega
  589. -- Count cut gates ≤ 2 * P.nonTopSqCount
  590. have hcut_card : ((Finset.range τ).filter (fun t => isGood t ∧ isCut t)).card ≤
  591. 2 * P.nonTopSqCount := by
  592. let Bset : Finset (Fin τ) :=
  593. Finset.univ.filter (fun t' : Fin τ => ¬ P.gateIsTopSq t'.val t'.isLt)
  594. let Cset : Finset ℕ :=
  595. Bset.biUnion (fun t' =>
  596. ({(P.gateIn1 t'.val t'.isLt).val, (P.gateIn2 t'.val t'.isLt).val} : Finset ℕ))
  597. have hsub : ((Finset.range τ).filter (fun t => isGood t ∧ isCut t)).image (fun t => t + 2) ⊆
  598. Cset := by
  599. intro w hw
  600. rcases Finset.mem_image.mp hw with ⟨t, ht, rfl⟩
  601. rcases (Finset.mem_filter.mp ht).2.2 with ⟨t', ht', hnot, hread⟩
  602. refine Finset.mem_biUnion.mpr ⟨⟨t', ht'⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, hnot⟩, ?_⟩
  603. rcases hread with h1 | h2
  604. · simp [h1]
  605. · simp [h2]
  606. have hinj : Function.Injective (fun t : ℕ => t + 2) := fun a b h => by dsimp only at h; omega
  607. calc ((Finset.range τ).filter (fun t => isGood t ∧ isCut t)).card
  608. = (((Finset.range τ).filter (fun t => isGood t ∧ isCut t)).image (fun t => t + 2)).card :=
  609. (Finset.card_image_of_injective _ hinj).symm
  610. _ ≤ Cset.card := Finset.card_le_card hsub
  611. _ ≤ ∑ t' ∈ Bset, ({(P.gateIn1 t'.val t'.isLt).val, (P.gateIn2 t'.val t'.isLt).val} : Finset ℕ).card :=
  612. Finset.card_biUnion_le
  613. _ ≤ ∑ _t' ∈ Bset, 2 := Finset.sum_le_sum (fun _ _ => le_trans (Finset.card_insert_le _ _) (by simp))
  614. _ = 2 * Bset.card := by simp [Nat.mul_comm]
  615. _ = 2 * P.nonTopSqCount := by rw [P.nonTopSqCount_eq_card]
  616. -- Convert each (a, b) ∈ L0 into an UntappedSeg P!
  617. let hmk : ∀ p ∈ L0, UntappedSeg P := fun p hp =>
  618. { a := p.1
  619. b := p.2
  620. hab := (hbounds p hp).1
  621. hb := (hbounds p hp).2
  622. hrun := fun t ht h1 h2 => by
  623. obtain ⟨_, htop⟩ := hgood p hp t h1 h2
  624. exact htop
  625. huntapped := fun t' ht' hbt' => by
  626. -- Neither input of gate t' ≥ b can lie in [a + 2, b]!
  627. -- If t' is NOT topSq: ruled out by ¬ isCut (w - 2).
  628. -- If t' IS topSq: deg(wire w) < D₁(w - 1) ≤ D₁(t') since w - 2 < b - 1 < t'!
  629. by_cases htop' : P.gateIsTopSq t' ht'
  630. · have hne : ∀ w : Fin (t' + 2),
  631. (P.prefix t' (le_of_lt ht')).evalWire w = P.evalWire ⟨w.val, by omega⟩ →
  632. (P.evalWire ⟨w.val, by omega⟩).natDegree = (P.prefix t' (le_of_lt ht')).maxDeg →
  633. ¬ (p.1 + 2 ≤ w.val ∧ w.val ≤ p.2) := by
  634. intro w _ hdeg_w ⟨hw1, hw2⟩
  635. -- Gate w.val - 1 lies in [p.1 + 1, p.2 - 1], wait: wire w.val is created by gate t0 = w.val - 2!
  636. -- Since w.val ≤ p.2 ≤ t', gate t0 + 1 = w.val - 1 < p.2 ≤ t' is ALSO in [p.1, p.2), hence isTopSq!
  637. have ht0_lt : w.val - 1 < τ := by omega
  638. have ht0_top : P.gateIsTopSq (w.val - 1) ht0_lt := by
  639. obtain ⟨_, ht⟩ := hgood p hp (w.val - 1) (by omega) (by omega)
  640. exact ht
  641. have hstrict := (P.topSq_strict_maxDeg ht0_lt ht0_top).2.2 ⟨w.val, by omega⟩
  642. dsimp only at hstrict
  643. have hmono := P.maxDeg_prefix_mono (t1 := (w.val - 1) + 1) (t2 := t') (by omega) (le_of_lt ht')
  644. omega
  645. rcases htop' with ⟨_, hij, hdeg1⟩
  646. have h1 : ¬ (p.1 + 2 ≤ (P.gateIn1 t' ht').val ∧ (P.gateIn1 t' ht').val ≤ p.2) := by
  647. apply hne (P.gateIn1 t' ht') (P.evalWire_prefix t' (le_of_lt ht') (P.gateIn1 t' ht'))
  648. rw [← P.evalWire_prefix t' (le_of_lt ht') (P.gateIn1 t' ht')]; exact hdeg1
  649. exact ⟨h1, by rw [← hij]; exact h1⟩
  650. · constructor
  651. · rintro ⟨hw1, hw2⟩
  652. have hnc := hnocut p hp ((P.gateIn1 t' ht').val - 2) (by omega) (by omega)
  653. exact hnc ⟨t', ht', htop', Or.inl (by omega)⟩
  654. · rintro ⟨hw1, hw2⟩
  655. have hnc := hnocut p hp ((P.gateIn2 t' ht').val - 2) (by omega) (by omega)
  656. exact hnc ⟨t', ht', htop', Or.inr (by omega)⟩ }
  657. let L : List (UntappedSeg P) := L0.pmap hmk (fun _ h => h)
  658. have hord : SegsOrdered L := by
  659. have hgen : ∀ (M : List (ℕ × ℕ)) (hM : ∀ p ∈ M, p ∈ L0),
  660. PairsOrdered M →
  661. SegsOrdered (M.pmap hmk hM) := by
  662. intro M
  663. induction M with
  664. | nil => intros; trivial
  665. | cons p1 rest ih =>
  666. cases rest with
  667. | nil => intros; trivial
  668. | cons p2 rest' =>
  669. intro hM hch
  670. exact ⟨hch.1, ih (fun q hq => hM q (List.mem_cons_of_mem _ hq)) hch.2⟩
  671. exact hgen L0 (fun _ h => h) hchain
  672. refine ⟨L, hord, ?_, ?_⟩
  673. · have hlenL : L.length = L0.length := List.length_pmap
  674. rw [hlenL]
  675. split_ifs at hlen <;> omega
  676. · have hmap : L.map (fun s => s.b - s.a) = L0.map (fun p => p.2 - p.1) := by
  677. simp [L, List.map_pmap, hmk]
  678. rw [segsTotalLen, hmap, hsum, hgood_card]
  679. /-- Gap condition for a nonempty ordered list of untapped segments `L` with minimum length `Δ`. -/
  680. def ValidGapSegs {τ : ℕ} {P : SLP τ} (Δ : ℕ) (L : List (UntappedSeg P)) : Prop :=
  681. L ≠ [] ∧
  682. SegsOrdered L ∧
  683. (∀ s ∈ L, Δ ≤ s.b - s.a) ∧
  684. (∀ (s0 : UntappedSeg P) (rest : List (UntappedSeg P)), L = s0 :: rest →
  685. let nextA := match rest with | [] => τ | s1 :: _ => s1.a
  686. 2 * s0.a + (nextA - s0.b) + 5 ≤ s0.b - s0.a) ∧
  687. (∀ (i : ℕ) (hi : i + 1 < L.length),
  688. let sPrev := L[i]'(by omega)
  689. let sCur := L[i + 1]'hi
  690. let nextA := if h2 : i + 2 < L.length then (L[i + 2]'h2).a else τ
  691. (sCur.a - sPrev.b) + (nextA - sCur.b) + 3 ≤ sCur.b - sCur.a)
  692. /-- Erasing an element preserves `SegsOrdered`. -/
  693. private lemma segsOrdered_eraseIdx {τ : ℕ} {P : SLP τ} :
  694. ∀ (L : List (UntappedSeg P)), SegsOrdered L → ∀ idx : ℕ, SegsOrdered (L.eraseIdx idx)
  695. | [], _, _ => trivial
  696. | [_], _, idx => by cases idx <;> simp [SegsOrdered]
  697. | s1 :: s2 :: rest, ⟨h12, hrest⟩, 0 => hrest
  698. | s1 :: s2 :: rest, ⟨h12, hrest⟩, 1 => by
  699. cases rest with
  700. | nil => trivial
  701. | cons s3 rest' =>
  702. exact ⟨le_trans h12 (le_trans (le_of_lt s2.hab) hrest.1), hrest.2⟩
  703. | s1 :: s2 :: rest, ⟨h12, hrest⟩, idx + 2 => by
  704. change SegsOrdered (s1 :: (s2 :: rest).eraseIdx (idx + 1))
  705. have ih := segsOrdered_eraseIdx (s2 :: rest) hrest (idx + 1)
  706. cases rest with
  707. | nil =>
  708. simp [List.eraseIdx, SegsOrdered, h12]
  709. | cons s3 rest' =>
  710. exact ⟨h12, ih⟩
  711. /-- Erasing index `idx < L.length` decreases `segsTotalLen` by `(L[idx]).b - (L[idx]).a`. -/
  712. private lemma segsTotalLen_eraseIdx {τ : ℕ} {P : SLP τ} :
  713. ∀ (L : List (UntappedSeg P)) (idx : ℕ) (hidx : idx < L.length),
  714. segsTotalLen (L.eraseIdx idx) + ((L[idx]'hidx).b - (L[idx]'hidx).a) = segsTotalLen L
  715. | s :: rest, 0, _ => by
  716. simp [segsTotalLen, Nat.add_comm]
  717. | s :: rest, idx + 1, hidx => by
  718. have ih := segsTotalLen_eraseIdx rest idx (by simpa using hidx)
  719. simp only [List.eraseIdx_cons_succ, segsTotalLen, List.map_cons, List.sum_cons,
  720. List.getElem_cons_succ] at ih ⊢
  721. omega
  722. /-- Helper: `s.a` and `nextA - s.b` are bounded by `R_tot = τ - segsTotalLen L`! -/
  723. private lemma gap_le_Rtot_head {τ : ℕ} {P : SLP τ} (s0 : UntappedSeg P)
  724. (rest : List (UntappedSeg P)) (hord : SegsOrdered (s0 :: rest)) :
  725. let nextA := match rest with | [] => τ | s1 :: _ => s1.a
  726. s0.a + (nextA - s0.b) ≤ τ - segsTotalLen (s0 :: rest) := by
  727. have hle_tot := segsTotalLen_le (s0 :: rest) hord
  728. cases rest with
  729. | nil =>
  730. simp only [segsTotalLen, List.map_singleton, List.sum_singleton]
  731. have := s0.hab; have := s0.hb; omega
  732. | cons s1 rest' =>
  733. have hall : ∀ s ∈ s1 :: rest', s1.a ≤ s.a := segsOrdered_head_le s1 rest' hord.2
  734. have hsub := segsTotalLen_le_sub (s1 :: rest') hord.2 s1.a hall
  735. have h01 : s0.b ≤ s1.a := hord.1
  736. have := s0.hab; have := s1.hab; have := s1.hb
  737. simp only [segsTotalLen, List.map_cons, List.sum_cons] at hsub hle_tot ⊢
  738. omega
  739. /-- Strengthened version of `gap_le_Rtot_mid` that carries `L.head.a` cleanly through induction. -/
  740. private lemma gap_le_Rtot_mid_strong {τ : ℕ} {P : SLP τ} :
  741. ∀ (L : List (UntappedSeg P)), SegsOrdered L →
  742. ∀ (i : ℕ) (hi : i + 1 < L.length),
  743. let sFirst := L[0]'(by omega)
  744. let sPrev := L[i]'(by omega)
  745. let sCur := L[i + 1]'hi
  746. let nextA := if h2 : i + 2 < L.length then (L[i + 2]'h2).a else τ
  747. sFirst.a + (sCur.a - sPrev.b) + (nextA - sCur.b) ≤ τ - segsTotalLen L := by
  748. intro L
  749. induction L with
  750. | nil => intro _ i hi; simp at hi
  751. | cons s0 rest ih =>
  752. intro hord i hi
  753. cases i with
  754. | zero =>
  755. cases rest with
  756. | nil => simp at hi
  757. | cons s1 rest' =>
  758. have hhead := gap_le_Rtot_head s1 rest' hord.2
  759. have h01 : s0.b ≤ s1.a := hord.1
  760. have := s0.hab
  761. cases rest' with
  762. | nil =>
  763. have hnot : ¬ (2 < 2) := by omega
  764. simp only [List.length_cons, List.length_nil, hnot, ↓reduceDIte,
  765. List.getElem_cons_zero, List.getElem_cons_succ, segsTotalLen,
  766. List.map_cons, List.map_nil, List.sum_cons, List.sum_nil]
  767. have := s1.hab; have := s1.hb; omega
  768. | cons s2 rest'' =>
  769. have hlt2 : 2 < (s0 :: s1 :: s2 :: rest'').length := by simp
  770. simp only [hlt2, ↓reduceDIte, List.getElem_cons_succ, List.getElem_cons_zero]
  771. simp only [segsTotalLen, List.map_cons, List.sum_cons] at hhead ⊢
  772. omega
  773. | succ i' =>
  774. cases rest with
  775. | nil => simp at hi
  776. | cons s1 rest' =>
  777. have hrec := ih hord.2 i' (by simpa using hi)
  778. have h01 : s0.b ≤ s1.a := hord.1
  779. have := s0.hab
  780. have hiff : (i' + 1 + 2 < (s0 :: s1 :: rest').length) ↔ (i' + 2 < (s1 :: rest').length) := by
  781. simp
  782. simp only [List.getElem_cons_zero, List.getElem_cons_succ] at hrec ⊢
  783. split_ifs at hrec ⊢ with h1 h2
  784. · simp only [segsTotalLen, List.map_cons, List.sum_cons] at hrec ⊢
  785. omega
  786. · omega
  787. · omega
  788. · simp only [segsTotalLen, List.map_cons, List.sum_cons] at hrec ⊢
  789. omega
  790. /-- **Pure-Combinatorial Demotion Lemma:** Given any ordered list `L` of untapped segments of `P`
  791. and threshold `Δ`, either some sublist `L'` satisfies `ValidGapSegs Δ L'` with
  792. `L'.length ≤ L.length`, OR `τ + Δ + 2 ≤ 3 ^ L.length * (τ - segsTotalLen L + Δ + 2)`. -/
  793. lemma demotion_exists_valid_gaps {τ : ℕ} {P : SLP τ} (Δ : ℕ) :
  794. ∀ (n : ℕ) (L : List (UntappedSeg P)),
  795. L.length = n → SegsOrdered L →
  796. (∃ L' : List (UntappedSeg P), ValidGapSegs Δ L' ∧ L'.length ≤ L.length) ∨
  797. τ + Δ + 2 ≤ 3 ^ n * (τ - segsTotalLen L + Δ + 2) := by
  798. intro n
  799. induction n with
  800. | zero =>
  801. intro L hlen _
  802. right
  803. have hnil : L = [] := List.eq_nil_of_length_eq_zero hlen
  804. subst hnil
  805. simp [segsTotalLen]
  806. | succ n' ih =>
  807. intro L hlen hord
  808. by_cases hvalid : ValidGapSegs Δ L
  809. · exact Or.inl ⟨L, hvalid, le_rfl⟩
  810. · -- Find a violating index idx < L.length whose length g ≤ 2 * R_tot + Δ + 4
  811. have hL_ne : L ≠ [] := fun h => by simp [h] at hlen
  812. have hex_bad : ∃ (idx : ℕ) (hidx : idx < L.length),
  813. (L[idx]'hidx).b - (L[idx]'hidx).a ≤ 2 * (τ - segsTotalLen L) + Δ + 4 := by
  814. by_contra hnone
  815. push Not at hnone
  816. apply hvalid
  817. refine ⟨hL_ne, hord, ?_, ?_, ?_⟩
  818. · intro s hs
  819. obtain ⟨idx, hidx, rfl⟩ := List.getElem_of_mem hs
  820. have := hnone idx hidx
  821. omega
  822. · intro s0 rest hLeq
  823. subst hLeq
  824. have hbig := hnone 0 (by simp)
  825. have hhead := gap_le_Rtot_head s0 rest hord
  826. simp only [List.getElem_cons_zero] at hbig
  827. cases rest <;> dsimp only at hhead ⊢ <;> omega
  828. · intro i hi
  829. have hbig := hnone (i + 1) hi
  830. have hmid := gap_le_Rtot_mid_strong L hord i hi
  831. omega
  832. obtain ⟨idx, hidx, hg_le⟩ := hex_bad
  833. let L' := L.eraseIdx idx
  834. have hlen' : L'.length = n' := by
  835. rw [List.length_eraseIdx_of_lt hidx, hlen]; omega
  836. have hord' : SegsOrdered L' := segsOrdered_eraseIdx L hord idx
  837. have hsum' : segsTotalLen L' + ((L[idx]'hidx).b - (L[idx]'hidx).a) = segsTotalLen L :=
  838. segsTotalLen_eraseIdx L idx hidx
  839. have hle_L := segsTotalLen_le L hord
  840. rcases ih L' hlen' hord' with ⟨L'', hv'', hl''⟩ | hbound'
  841. · exact Or.inl ⟨L'', hv'', by omega⟩
  842. · right
  843. have hR' : τ - segsTotalLen L' + Δ + 2 ≤ 3 * (τ - segsTotalLen L + Δ + 2) := by
  844. have := (L[idx]'hidx).hab
  845. omega
  846. calc τ + Δ + 2
  847. ≤ 3 ^ n' * (τ - segsTotalLen L' + Δ + 2) := hbound'
  848. _ ≤ 3 ^ n' * (3 * (τ - segsTotalLen L + Δ + 2)) := Nat.mul_le_mul_left _ hR'
  849. _ = 3 ^ (n' + 1) * (τ - segsTotalLen L + Δ + 2) := by rw [pow_succ]; ring
  850. /-! ## 3. Formal Substitution `F(x, T) ∈ (Polynomial ℤ)[X]` & The 4 Block Properties -/
  851. /-- Apply `SLPOp` to two elements of `Polynomial (Polynomial ℤ)`. -/
  852. noncomputable def SLPOp.applyPolyT :
  853. SLPOp → Polynomial (Polynomial ℤ) → Polynomial (Polynomial ℤ) → Polynomial (Polynomial ℤ)
  854. | .add, A, B => A + B
  855. | .sub, A, B => A - B
  856. | .mul, A, B => A * B
  857. /-- Evaluate `P` in `Polynomial (Polynomial ℤ)` up to gate `t` (`b ≤ t ≤ τ`), replacing wire `b + 1`
  858. by the indeterminate `T = Polynomial.X`, keeping all wires `≤ a + 1` as `Polynomial.C (P.evalWire i)`,
  859. and setting unread strict intermediate wires `a + 2 .. b` to `0`. -/
  860. noncomputable def evalFormalT {τ : ℕ} (P : SLP τ) (a b : ℕ) (hab : a < b) (hb : b ≤ τ)
  861. (t : ℕ) (_hbt : b ≤ t) (ht : t ≤ τ) (w : Fin (t + 2)) : Polynomial (Polynomial ℤ) :=
  862. if hw : w.val < b + 1 then
  863. if hwa : w.val ≤ a + 1 then
  864. Polynomial.C (P.evalWire ⟨w.val, by omega⟩)
  865. else
  866. 0
  867. else if hwb : w.val = b + 1 then
  868. Polynomial.X
  869. else
  870. -- w.val ≥ b + 2, so w.val = t' + 2 with b ≤ t' < t
  871. have hprev_lt : w.val - 2 < τ := by omega
  872. have hbt' : b ≤ w.val - 2 := by omega
  873. let t' := w.val - 2
  874. let in1 := P.gateIn1 t' hprev_lt
  875. let in2 := P.gateIn2 t' hprev_lt
  876. (P.gateOp t' hprev_lt).applyPolyT
  877. (evalFormalT P a b hab hb t' hbt' (le_of_lt hprev_lt) in1)
  878. (evalFormalT P a b hab hb t' hbt' (le_of_lt hprev_lt) in2)
  879. termination_by t
  880. decreasing_by
  881. all_goals omega
  882. /-- Property 1 (Exact Specialization): evaluating `T := P.evalWire (b + 1)` recovers `P.evalWire w`
  883. for every wire `w` that is not a strict intermediate wire of an untapped segment `seg`. -/
  884. lemma evalFormalT_spec {τ : ℕ} (P : SLP τ) (seg : UntappedSeg P) :
  885. ∀ (t : ℕ) (hbt : seg.b ≤ t) (ht : t ≤ τ) (w : Fin (t + 2)),
  886. ¬ (seg.a + 2 ≤ w.val ∧ w.val ≤ seg.b) →
  887. (evalFormalT P seg.a seg.b seg.hab seg.hb t hbt ht w).eval
  888. (P.evalWire ⟨seg.b + 1, by omega⟩) = P.evalWire ⟨w.val, by omega⟩ := by
  889. intro t
  890. induction t using Nat.strong_induction_on with
  891. | ind t ih =>
  892. intro hbt ht w hnot_inter
  893. rw [evalFormalT.eq_def]
  894. split_ifs with hw hwa hwb
  895. · simp
  896. · exfalso; exact hnot_inter ⟨by omega, by omega⟩
  897. · simp [hwb]
  898. · set t' := w.val - 2
  899. have ht'_lt_t : t' < t := by omega
  900. have hbt' : seg.b ≤ t' := by omega
  901. have ht'_lt_τ : t' < τ := by omega
  902. have hun := seg.huntapped t' ht'_lt_τ hbt'
  903. have ih1 := ih t' ht'_lt_t hbt' (le_of_lt ht'_lt_τ) (P.gateIn1 t' ht'_lt_τ) hun.1
  904. have ih2 := ih t' ht'_lt_t hbt' (le_of_lt ht'_lt_τ) (P.gateIn2 t' ht'_lt_τ) hun.2
  905. have hw_eq : w.val = t' + 2 := by omega
  906. have hgate := P.evalWire_gate t' ht'_lt_τ
  907. have hpoly_eval : ∀ (op : SLPOp) (A B : Polynomial (Polynomial ℤ)) (U : Polynomial ℤ),
  908. (op.applyPolyT A B).eval U = op.applyPoly (A.eval U) (B.eval U) := by
  909. intro op A B U
  910. cases op <;> simp [SLPOp.applyPolyT, SLPOp.applyPoly]
  911. rw [hpoly_eval, ih1, ih2]
  912. simpa [hw_eq] using hgate.symm
  913. /-- Property 2 (`x`-Degree Bound): every `T`-coefficient of `evalFormalT` at horizon `t` has
  914. `natDegree ≤ 2 ^ (t - b) * D₁(a)`. -/
  915. lemma evalFormalT_coeff_natDegree_le {τ : ℕ} (P : SLP τ) (a b : ℕ) (hab : a < b) (hb : b ≤ τ) :
  916. ∀ (t : ℕ) (hbt : b ≤ t) (ht : t ≤ τ) (w : Fin (t + 2)) (j : ℕ),
  917. ((evalFormalT P a b hab hb t hbt ht w).coeff j).natDegree ≤
  918. 2 ^ (t - b) * (P.prefix a (by omega)).maxDeg := by
  919. intro t
  920. induction t using Nat.strong_induction_on with
  921. | ind t ih =>
  922. intro hbt ht w j
  923. rw [evalFormalT.eq_def]
  924. split_ifs with hw hwa hwb
  925. · rw [Polynomial.coeff_C]
  926. split_ifs with hj
  927. · have h1 : (P.evalWire ⟨w.val, by omega⟩).natDegree ≤ (P.prefix a (by omega)).maxDeg := by
  928. rw [← P.evalWire_prefix a (by omega) ⟨w.val, by omega⟩]
  929. exact (P.prefix a (by omega)).wireDeg_le_maxDeg ⟨w.val, by omega⟩
  930. have h2 : 1 ≤ 2 ^ (t - b) := Nat.one_le_pow _ _ (by omega)
  931. nlinarith
  932. · simp
  933. · simp
  934. · rw [Polynomial.coeff_X]
  935. split_ifs <;> simp
  936. · set t' := w.val - 2
  937. have ht'_lt_t : t' < t := by omega
  938. have hbt' : b ≤ t' := by omega
  939. have ht'_lt_τ : t' < τ := by omega
  940. have hpow_le : 2 * 2 ^ (t' - b) ≤ 2 ^ (t - b) := by
  941. have : (t' - b) + 1 ≤ t - b := by omega
  942. have := Nat.pow_le_pow_right (by omega : 1 ≤ 2) this
  943. rwa [pow_succ, Nat.mul_comm] at this
  944. set A := evalFormalT P a b hab hb t' hbt' (le_of_lt ht'_lt_τ) (P.gateIn1 t' ht'_lt_τ)
  945. set B := evalFormalT P a b hab hb t' hbt' (le_of_lt ht'_lt_τ) (P.gateIn2 t' ht'_lt_τ)
  946. have hA : ∀ k, (A.coeff k).natDegree ≤ 2 ^ (t' - b) * (P.prefix a (by omega)).maxDeg :=
  947. ih t' ht'_lt_t hbt' (le_of_lt ht'_lt_τ) (P.gateIn1 t' ht'_lt_τ)
  948. have hB : ∀ k, (B.coeff k).natDegree ≤ 2 ^ (t' - b) * (P.prefix a (by omega)).maxDeg :=
  949. ih t' ht'_lt_t hbt' (le_of_lt ht'_lt_τ) (P.gateIn2 t' ht'_lt_τ)
  950. change (((P.gateOp t' ht'_lt_τ).applyPolyT A B).coeff j).natDegree ≤
  951. 2 ^ (t - b) * (P.prefix a (by omega)).maxDeg
  952. cases P.gateOp t' ht'_lt_τ with
  953. | add =>
  954. simp only [SLPOp.applyPolyT, Polynomial.coeff_add]
  955. have := le_trans (Polynomial.natDegree_add_le _ _) (max_le (hA j) (hB j))
  956. nlinarith
  957. | sub =>
  958. simp only [SLPOp.applyPolyT, Polynomial.coeff_sub]
  959. have := le_trans (Polynomial.natDegree_sub_le _ _) (max_le (hA j) (hB j))
  960. nlinarith
  961. | mul =>
  962. simp only [SLPOp.applyPolyT, Polynomial.coeff_mul]
  963. have hsum_deg : ∀ (s : Finset (ℕ × ℕ)) (f : ℕ × ℕ → Polynomial ℤ) (M : ℕ),
  964. (∀ p ∈ s, (f p).natDegree ≤ M) → (∑ p ∈ s, f p).natDegree ≤ M := by
  965. intro s f M
  966. induction s using Finset.induction_on with
  967. | empty => intro _; simp
  968. | @insert u s' hu ihs =>
  969. intro hM
  970. rw [Finset.sum_insert hu]
  971. exact le_trans (Polynomial.natDegree_add_le _ _)
  972. (max_le (hM u (Finset.mem_insert_self _ _))
  973. (ihs (fun p hp => hM p (Finset.mem_insert_of_mem hp))))
  974. have hsum : (∑ x ∈ Finset.antidiagonal j, A.coeff x.1 * B.coeff x.2).natDegree ≤
  975. 2 * (2 ^ (t' - b) * (P.prefix a (by omega)).maxDeg) := by
  976. apply hsum_deg
  977. intro p _
  978. calc (A.coeff p.1 * B.coeff p.2).natDegree
  979. ≤ (A.coeff p.1).natDegree + (B.coeff p.2).natDegree := Polynomial.natDegree_mul_le
  980. _ ≤ 2 ^ (t' - b) * (P.prefix a (by omega)).maxDeg +
  981. 2 ^ (t' - b) * (P.prefix a (by omega)).maxDeg := Nat.add_le_add (hA p.1) (hB p.2)
  982. _ = 2 * (2 ^ (t' - b) * (P.prefix a (by omega)).maxDeg) := by ring
  983. have hmul_le : 2 * (2 ^ (t' - b) * (P.prefix a (by omega)).maxDeg) ≤
  984. 2 ^ (t - b) * (P.prefix a (by omega)).maxDeg := by
  985. rw [← Nat.mul_assoc]
  986. exact Nat.mul_le_mul_right _ hpow_le
  987. exact le_trans hsum hmul_le
  988. /-- `ℓ₁`-norm of `F(x, T)` at an integer `x : ℤ`: `∑_j |(F.coeff j).eval x|`. -/
  989. noncomputable def polyL1At (F : Polynomial (Polynomial ℤ)) (x : ℤ) : ℕ :=
  990. ∑ j ∈ F.support, ((F.coeff j).eval x).natAbs
  991. private lemma polyL1At_eq_sum_range (F : Polynomial (Polynomial ℤ)) (x : ℤ) {N : ℕ}
  992. (hN : ∀ j ∈ F.support, j < N) :
  993. polyL1At F x = ∑ j ∈ Finset.range N, ((F.coeff j).eval x).natAbs := by
  994. unfold polyL1At
  995. apply Finset.sum_subset (fun j hj => Finset.mem_range.mpr (hN j hj))
  996. intro j _ hj
  997. have : F.coeff j = 0 := Polynomial.notMem_support_iff.mp hj
  998. simp [this]
  999. private lemma polyL1At_add_le (A B : Polynomial (Polynomial ℤ)) (x : ℤ) :
  1000. polyL1At (A + B) x ≤ polyL1At A x + polyL1At B x := by
  1001. set N := max (A.natDegree + 1) (B.natDegree + 1)
  1002. have hA : ∀ j ∈ A.support, j < N := fun j hj => by
  1003. have := Polynomial.le_natDegree_of_mem_supp j hj; omega
  1004. have hB : ∀ j ∈ B.support, j < N := fun j hj => by
  1005. have := Polynomial.le_natDegree_of_mem_supp j hj; omega
  1006. have hAB : ∀ j ∈ (A + B).support, j < N := fun j hj => by
  1007. have h1 := Polynomial.le_natDegree_of_mem_supp j hj
  1008. have h2 := Polynomial.natDegree_add_le A B
  1009. omega
  1010. rw [polyL1At_eq_sum_range (A + B) x hAB, polyL1At_eq_sum_range A x hA,
  1011. polyL1At_eq_sum_range B x hB, ← Finset.sum_add_distrib]
  1012. apply Finset.sum_le_sum
  1013. intro j _
  1014. simp only [Polynomial.coeff_add, Polynomial.eval_add]
  1015. exact Int.natAbs_add_le _ _
  1016. private lemma polyL1At_sub_le (A B : Polynomial (Polynomial ℤ)) (x : ℤ) :
  1017. polyL1At (A - B) x ≤ polyL1At A x + polyL1At B x := by
  1018. set N := max (A.natDegree + 1) (B.natDegree + 1)
  1019. have hA : ∀ j ∈ A.support, j < N := fun j hj => by
  1020. have := Polynomial.le_natDegree_of_mem_supp j hj; omega
  1021. have hB : ∀ j ∈ B.support, j < N := fun j hj => by
  1022. have := Polynomial.le_natDegree_of_mem_supp j hj; omega
  1023. have hAB : ∀ j ∈ (A - B).support, j < N := fun j hj => by
  1024. have h1 := Polynomial.le_natDegree_of_mem_supp j hj
  1025. have h2 := Polynomial.natDegree_sub_le A B
  1026. omega
  1027. rw [polyL1At_eq_sum_range (A - B) x hAB, polyL1At_eq_sum_range A x hA,
  1028. polyL1At_eq_sum_range B x hB, ← Finset.sum_add_distrib]
  1029. apply Finset.sum_le_sum
  1030. intro j _
  1031. simp only [Polynomial.coeff_sub, Polynomial.eval_sub]
  1032. simpa [sub_eq_add_neg, Int.natAbs_neg] using Int.natAbs_add_le ((A.coeff j).eval x) (-((B.coeff j).eval x))
  1033. private lemma polyL1At_mul_le (A B : Polynomial (Polynomial ℤ)) (x : ℤ) :
  1034. polyL1At (A * B) x ≤ polyL1At A x * polyL1At B x := by
  1035. set Na := A.natDegree + 1
  1036. set Nb := B.natDegree + 1
  1037. have hA : ∀ j ∈ A.support, j < Na := fun j hj => by
  1038. have := Polynomial.le_natDegree_of_mem_supp j hj; omega
  1039. have hB : ∀ j ∈ B.support, j < Nb := fun j hj => by
  1040. have := Polynomial.le_natDegree_of_mem_supp j hj; omega
  1041. have hAB : ∀ j ∈ (A * B).support, j < Na + Nb := fun j hj => by
  1042. have h1 := Polynomial.le_natDegree_of_mem_supp j hj
  1043. have h2 := Polynomial.natDegree_mul_le (p := A) (q := B)
  1044. omega
  1045. rw [polyL1At_eq_sum_range (A * B) x hAB, polyL1At_eq_sum_range A x hA,
  1046. polyL1At_eq_sum_range B x hB, Finset.sum_mul_sum]
  1047. have htri : ∀ (s : Finset (ℕ × ℕ)) (f : ℕ × ℕ → ℤ),
  1048. (∑ p ∈ s, f p).natAbs ≤ ∑ p ∈ s, (f p).natAbs := by
  1049. intro s f
  1050. induction s using Finset.induction_on with
  1051. | empty => simp
  1052. | @insert u s' hu ihs =>
  1053. rw [Finset.sum_insert hu, Finset.sum_insert hu]
  1054. exact le_trans (Int.natAbs_add_le _ _) (Nat.add_le_add_left ihs _)
  1055. calc ∑ j ∈ Finset.range (Na + Nb), (((A * B).coeff j).eval x).natAbs
  1056. ≤ ∑ j ∈ Finset.range (Na + Nb),
  1057. ∑ p ∈ Finset.antidiagonal j, ((A.coeff p.1).eval x).natAbs * ((B.coeff p.2).eval x).natAbs := by
  1058. apply Finset.sum_le_sum
  1059. intro j _
  1060. rw [Polynomial.coeff_mul, ← Polynomial.coe_evalRingHom, map_sum]
  1061. calc (∑ p ∈ Finset.antidiagonal j, (Polynomial.evalRingHom x) (A.coeff p.1 * B.coeff p.2)).natAbs
  1062. ≤ ∑ p ∈ Finset.antidiagonal j, ((Polynomial.evalRingHom x) (A.coeff p.1 * B.coeff p.2)).natAbs :=
  1063. htri _ _
  1064. _ = ∑ p ∈ Finset.antidiagonal j, ((A.coeff p.1).eval x).natAbs * ((B.coeff p.2).eval x).natAbs := by
  1065. apply Finset.sum_congr rfl; intro p _
  1066. simp [Int.natAbs_mul]
  1067. _ ≤ ∑ p ∈ Finset.range Na ×ˢ Finset.range Nb,
  1068. ((A.coeff p.1).eval x).natAbs * ((B.coeff p.2).eval x).natAbs := by
  1069. -- Every term with p.1 ≥ Na or p.2 ≥ Nb vanishes!
  1070. rw [← Finset.sum_biUnion]
  1071. · apply Finset.sum_le_sum_of_ne_zero
  1072. intro p hp hne
  1073. have hp1 : p.1 < Na := by
  1074. by_contra h
  1075. have h0 : A.coeff p.1 = 0 := Polynomial.coeff_eq_zero_of_natDegree_lt (by omega)
  1076. simp [h0] at hne
  1077. have hp2 : p.2 < Nb := by
  1078. by_contra h
  1079. have h0 : B.coeff p.2 = 0 := Polynomial.coeff_eq_zero_of_natDegree_lt (by omega)
  1080. simp [h0] at hne
  1081. exact Finset.mem_product.mpr ⟨Finset.mem_range.mpr hp1, Finset.mem_range.mpr hp2⟩
  1082. · intro i _ j _ hij
  1083. exact Finset.disjoint_left.mpr fun p hpi hpj =>
  1084. hij ((Finset.mem_antidiagonal.mp hpi).symm.trans (Finset.mem_antidiagonal.mp hpj))
  1085. _ = ∑ i ∈ Finset.range Na, ∑ j ∈ Finset.range Nb,
  1086. ((A.coeff i).eval x).natAbs * ((B.coeff j).eval x).natAbs := Finset.sum_product _ _ _
  1087. /-- Maximum wire magnitude (at least `2`) of `P` up to gate `t` (`t ≤ τ`) at integer `x`. -/
  1088. noncomputable def wireHeight {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t ≤ τ) (x : ℤ) : ℕ :=
  1089. max 2 (Finset.univ.sup (fun i : Fin (t + 2) => ((P.prefix t ht).evalWire i).eval x |>.natAbs))
  1090. lemma two_le_wireHeight {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t ≤ τ) (x : ℤ) :
  1091. 2 ≤ wireHeight P t ht x := le_max_left _ _
  1092. lemma wire_natAbs_le_wireHeight {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t ≤ τ) (x : ℤ)
  1093. (w : Fin (t + 2)) :
  1094. ((P.evalWire ⟨w.val, by omega⟩).eval x).natAbs ≤ wireHeight P t ht x := by
  1095. rw [← P.evalWire_prefix t ht w]
  1096. exact le_trans (Finset.le_sup (f := fun i : Fin (t + 2) => ((P.prefix t ht).evalWire i).eval x |>.natAbs)
  1097. (Finset.mem_univ w)) (le_max_right _ _)
  1098. /-- Property 3 (`ℓ₁`-Norm Bound): `polyL1At (evalFormalT ... w) x ≤ (wireHeight P a _ x) ^ (2 ^ (t - b))`. -/
  1099. lemma evalFormalT_l1_le {τ : ℕ} (P : SLP τ) (a b : ℕ) (hab : a < b) (hb : b ≤ τ) (x : ℤ) :
  1100. ∀ (t : ℕ) (hbt : b ≤ t) (ht : t ≤ τ) (w : Fin (t + 2)),
  1101. polyL1At (evalFormalT P a b hab hb t hbt ht w) x ≤
  1102. (wireHeight P a (by omega) x) ^ (2 ^ (t - b)) := by
  1103. intro t
  1104. induction t using Nat.strong_induction_on with
  1105. | ind t ih =>
  1106. intro hbt ht w
  1107. set H := wireHeight P a (by omega) x
  1108. have hH2 : 2 ≤ H := two_le_wireHeight P a (by omega) x
  1109. have hpow1 : 1 ≤ 2 ^ (t - b) := Nat.one_le_pow _ _ (by omega)
  1110. have hH_le_pow : H ≤ H ^ (2 ^ (t - b)) := Nat.le_self_pow (by omega) H
  1111. rw [evalFormalT.eq_def]
  1112. split_ifs with hw hwa hwb
  1113. · have hC : polyL1At (Polynomial.C (P.evalWire ⟨w.val, by omega⟩)) x ≤
  1114. ((P.evalWire ⟨w.val, by omega⟩).eval x).natAbs := by
  1115. unfold polyL1At
  1116. by_cases h0 : P.evalWire ⟨w.val, by omega⟩ = 0
  1117. · simp [h0]
  1118. · rw [Polynomial.support_C h0]; simp
  1119. exact le_trans hC (le_trans (wire_natAbs_le_wireHeight P a (by omega) x ⟨w.val, by omega⟩) hH_le_pow)
  1120. · simp [polyL1At]
  1121. · have hX : polyL1At (Polynomial.X : Polynomial (Polynomial ℤ)) x = 1 := by
  1122. unfold polyL1At
  1123. rw [Polynomial.support_X]
  1124. simp
  1125. rw [hX]; omega
  1126. · set t' := w.val - 2
  1127. have ht'_lt_t : t' < t := by omega
  1128. have hbt' : b ≤ t' := by omega
  1129. have ht'_lt_τ : t' < τ := by omega
  1130. set A := evalFormalT P a b hab hb t' hbt' (le_of_lt ht'_lt_τ) (P.gateIn1 t' ht'_lt_τ)
  1131. set B := evalFormalT P a b hab hb t' hbt' (le_of_lt ht'_lt_τ) (P.gateIn2 t' ht'_lt_τ)
  1132. have hA : polyL1At A x ≤ H ^ (2 ^ (t' - b)) :=
  1133. ih t' ht'_lt_t hbt' (le_of_lt ht'_lt_τ) (P.gateIn1 t' ht'_lt_τ)
  1134. have hB : polyL1At B x ≤ H ^ (2 ^ (t' - b)) :=
  1135. ih t' ht'_lt_t hbt' (le_of_lt ht'_lt_τ) (P.gateIn2 t' ht'_lt_τ)
  1136. have h2_le_Hpow : 2 ≤ H ^ (2 ^ (t' - b)) :=
  1137. le_trans hH2 (Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) H)
  1138. have hsq_le : H ^ (2 ^ (t' - b)) * H ^ (2 ^ (t' - b)) ≤ H ^ (2 ^ (t - b)) := by
  1139. rw [← pow_add, ← two_mul]
  1140. apply Nat.pow_le_pow_right (by omega)
  1141. have : (t' - b) + 1 ≤ t - b := by omega
  1142. have := Nat.pow_le_pow_right (by omega : 1 ≤ 2) this
  1143. rwa [pow_succ, Nat.mul_comm] at this
  1144. change polyL1At ((P.gateOp t' ht'_lt_τ).applyPolyT A B) x ≤ H ^ (2 ^ (t - b))
  1145. cases P.gateOp t' ht'_lt_τ with
  1146. | add =>
  1147. simp only [SLPOp.applyPolyT]
  1148. have h1 := polyL1At_add_le A B x
  1149. have h2 : H ^ (2 ^ (t' - b)) + H ^ (2 ^ (t' - b)) ≤ H ^ (2 ^ (t' - b)) * H ^ (2 ^ (t' - b)) := by
  1150. nlinarith
  1151. exact le_trans h1 (le_trans (Nat.add_le_add hA hB) (le_trans h2 hsq_le))
  1152. | sub =>
  1153. simp only [SLPOp.applyPolyT]
  1154. have h1 := polyL1At_sub_le A B x
  1155. have h2 : H ^ (2 ^ (t' - b)) + H ^ (2 ^ (t' - b)) ≤ H ^ (2 ^ (t' - b)) * H ^ (2 ^ (t' - b)) := by
  1156. nlinarith
  1157. exact le_trans h1 (le_trans (Nat.add_le_add hA hB) (le_trans h2 hsq_le))
  1158. | mul =>
  1159. simp only [SLPOp.applyPolyT]
  1160. exact le_trans (polyL1At_mul_le A B x) (le_trans (Nat.mul_le_mul hA hB) hsq_le)
  1161. /-- Property 4 (Next-Level Height Bound): for any untapped segment `seg` and any `c` with
  1162. `seg.b ≤ c ≤ τ`, `wireHeight P c _ x ≤ (max (wireHeight P seg.a _ x) |U(x)|) ^ (2 ^ (c - seg.b))`. -/
  1163. lemma wireHeight_step_le {τ : ℕ} (P : SLP τ) (seg : UntappedSeg P) (x : ℤ) :
  1164. ∀ (c : ℕ) (hbc : seg.b ≤ c) (hc : c ≤ τ),
  1165. wireHeight P c hc x ≤
  1166. (max (wireHeight P seg.a (by have := seg.hab; have := seg.hb; omega) x)
  1167. ((P.evalWire ⟨seg.b + 1, by have := seg.hb; omega⟩).eval x).natAbs) ^ (2 ^ (c - seg.b)) := by
  1168. intro c hbc hc
  1169. set M := max (wireHeight P seg.a (by have := seg.hab; have := seg.hb; omega) x)
  1170. ((P.evalWire ⟨seg.b + 1, by have := seg.hb; omega⟩).eval x).natAbs
  1171. have hM2 : 2 ≤ M := le_trans (two_le_wireHeight P seg.a (by have := seg.hab; have := seg.hb; omega) x) (le_max_left _ _)
  1172. have hpow1 : 1 ≤ 2 ^ (c - seg.b) := Nat.one_le_pow _ _ (by omega)
  1173. have hM_le_pow : M ≤ M ^ (2 ^ (c - seg.b)) := Nat.le_self_pow (by omega) M
  1174. -- Prove by strong induction on w.val < c + 2 that |wire w(x)| ≤ M ^ (2 ^ (max w.val (seg.b + 1) - (seg.b + 1)))
  1175. have hwire : ∀ (k : ℕ) (hk : k < c + 2),
  1176. ((P.evalWire ⟨k, by omega⟩).eval x).natAbs ≤
  1177. M ^ (2 ^ (k - (seg.b + 1))) := by
  1178. intro k
  1179. induction k using Nat.strong_induction_on with
  1180. | ind k ihk =>
  1181. intro hk
  1182. by_cases hle_a : k ≤ seg.a + 1
  1183. · have hsub : k - (seg.b + 1) = 0 := by have := seg.hab; omega
  1184. rw [hsub, pow_zero, pow_one]
  1185. exact le_trans (wire_natAbs_le_wireHeight P seg.a (by have := seg.hab; have := seg.hb; omega) x ⟨k, by omega⟩) (le_max_left _ _)
  1186. · by_cases hle_b : k ≤ seg.b
  1187. · -- Strict intermediate wire of seg: equals w_in ^ (2 ^ (k - seg.a - 1))!
  1188. -- Note |w_in(x)| ^ (2^(k - seg.a - 1)) ≤ max 1 |w_in(x)| ^ (2^(seg.b - seg.a)) = max 1 |U(x)| ≤ M!
  1189. have hsub : k - (seg.b + 1) = 0 := by omega
  1190. rw [hsub, pow_zero, pow_one]
  1191. have hrun_k := P.topSq_run_eval (a := seg.a) (b := k - 1) (by omega) (by omega)
  1192. (fun t ht h1 h2 => seg.hrun t ht h1 (by omega))
  1193. have hrun_b := P.topSq_run_eval seg.hab seg.hb seg.hrun
  1194. set win := P.evalWire ⟨(P.gateIn1 seg.a (by omega)).val, by have := (P.gateIn1 seg.a (by omega)).isLt; omega⟩
  1195. have hk_eq : k - 1 + 1 = k := by omega
  1196. have hval_k : ((P.evalWire ⟨k, by omega⟩).eval x).natAbs =
  1197. (win.eval x).natAbs ^ (2 ^ (k - 1 - seg.a)) := by
  1198. have := congrArg (fun p : Polynomial ℤ => (p.eval x).natAbs) hrun_k.1
  1199. simpa [hk_eq, Int.natAbs_pow, win] using this
  1200. have hval_b : ((P.evalWire ⟨seg.b + 1, by omega⟩).eval x).natAbs =
  1201. (win.eval x).natAbs ^ (2 ^ (seg.b - seg.a)) := by
  1202. have := congrArg (fun p : Polynomial ℤ => (p.eval x).natAbs) hrun_b.1
  1203. simpa [Int.natAbs_pow, win] using this
  1204. rw [hval_k]
  1205. by_cases hw0 : (win.eval x).natAbs = 0
  1206. · rw [hw0, zero_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega)))]; omega
  1207. · have h1 : (win.eval x).natAbs ^ (2 ^ (k - 1 - seg.a)) ≤
  1208. (win.eval x).natAbs ^ (2 ^ (seg.b - seg.a)) :=
  1209. Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) (by omega))
  1210. rw [← hval_b] at h1
  1211. exact le_trans h1 (le_max_right _ _)
  1212. · by_cases heq_b : k = seg.b + 1
  1213. · subst heq_b
  1214. simp only [Nat.sub_self, pow_zero, pow_one]
  1215. exact le_max_right _ _
  1216. · -- k ≥ seg.b + 2: gate t' = k - 2
  1217. set t' := k - 2
  1218. have ht'_lt : t' < τ := by omega
  1219. have hgate := P.evalWire_gate t' ht'_lt
  1220. have hk_eq : t' + 2 = k := by omega
  1221. have hin1_lt : (P.gateIn1 t' ht'_lt).val < k := by have := (P.gateIn1 t' ht'_lt).isLt; omega
  1222. have hin2_lt : (P.gateIn2 t' ht'_lt).val < k := by have := (P.gateIn2 t' ht'_lt).isLt; omega
  1223. have h1 := ihk (P.gateIn1 t' ht'_lt).val hin1_lt (by omega)
  1224. have h2 := ihk (P.gateIn2 t' ht'_lt).val hin2_lt (by omega)
  1225. have he1 : (P.gateIn1 t' ht'_lt).val - (seg.b + 1) ≤ k - (seg.b + 1) - 1 := by omega
  1226. have he2 : (P.gateIn2 t' ht'_lt).val - (seg.b + 1) ≤ k - (seg.b + 1) - 1 := by omega
  1227. have h1' : ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x).natAbs ≤
  1228. M ^ (2 ^ (k - (seg.b + 1) - 1)) :=
  1229. le_trans h1 (Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) he1))
  1230. have h2' : ((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x).natAbs ≤
  1231. M ^ (2 ^ (k - (seg.b + 1) - 1)) :=
  1232. le_trans h2 (Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) he2))
  1233. have hsq : M ^ (2 ^ (k - (seg.b + 1) - 1)) * M ^ (2 ^ (k - (seg.b + 1) - 1)) =
  1234. M ^ (2 ^ (k - (seg.b + 1))) := by
  1235. rw [← pow_add, ← two_mul]
  1236. congr 1
  1237. conv_rhs => rw [← Nat.sub_add_cancel (by omega : 1 ≤ k - (seg.b + 1)), pow_succ, Nat.mul_comm]
  1238. have h2_le : 2 ≤ M ^ (2 ^ (k - (seg.b + 1) - 1)) :=
  1239. le_trans hM2 (Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) M)
  1240. have heval : (P.evalWire ⟨k, by omega⟩).eval x =
  1241. (P.gateOp t' ht'_lt).applyInt
  1242. ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x)
  1243. ((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x) := by
  1244. rw [SLPOp.applyInt_eq_eval]
  1245. simpa [hk_eq] using congrArg (fun p : Polynomial ℤ => p.eval x) hgate
  1246. rw [heval]
  1247. cases P.gateOp t' ht'_lt with
  1248. | add =>
  1249. simp only [SLPOp.applyInt]
  1250. have := Int.natAbs_add_le
  1251. ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x)
  1252. ((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x)
  1253. nlinarith
  1254. | sub =>
  1255. simp only [SLPOp.applyInt]
  1256. have := Int.natAbs_add_le
  1257. ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x)
  1258. (-((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x))
  1259. rw [Int.natAbs_neg] at this
  1260. have hsub_eq : ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x) -
  1261. ((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x) =
  1262. ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x) +
  1263. (-((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x)) := by ring
  1264. rw [hsub_eq]; nlinarith
  1265. | mul =>
  1266. simp only [SLPOp.applyInt, Int.natAbs_mul]
  1267. rw [← hsq]; exact Nat.mul_le_mul h1' h2'
  1268. unfold wireHeight
  1269. apply max_le (le_trans hM2 hM_le_pow)
  1270. apply Finset.sup_le
  1271. intro i _
  1272. rw [P.evalWire_prefix c hc i]
  1273. have hi_le : i.val - (seg.b + 1) ≤ c - seg.b := by have := i.isLt; omega
  1274. exact le_trans (hwire i.val i.isLt)
  1275. (Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) hi_le))
  1276. /-- Base height bound before segment `a`: `wireHeight P a _ x ≤ (max 2 |x|) ^ (2 ^ a)`. -/
  1277. lemma wireHeight_base_le {τ : ℕ} (P : SLP τ) (a : ℕ) (ha : a ≤ τ) (x : ℤ) :
  1278. wireHeight P a ha x ≤ (max 2 x.natAbs) ^ (2 ^ a) := by
  1279. set M := max 2 x.natAbs
  1280. have hM2 : 2 ≤ M := le_max_left _ _
  1281. have hwire : ∀ (k : ℕ) (hk : k < a + 2),
  1282. ((P.evalWire ⟨k, by omega⟩).eval x).natAbs ≤ M ^ (2 ^ (k - 1)) := by
  1283. intro k
  1284. induction k using Nat.strong_induction_on with
  1285. | ind k ihk =>
  1286. intro hk
  1287. match k with
  1288. | 0 =>
  1289. have h0 : P.evalWire ⟨0, by omega⟩ = 1 := P.evalWire_zero
  1290. rw [h0]; simp; omega
  1291. | 1 =>
  1292. have h1 : P.evalWire ⟨1, by omega⟩ = Polynomial.X := by
  1293. have hpr := P.evalWire_prefix 0 (Nat.zero_le τ) ⟨1, by omega⟩
  1294. have hnil : P.prefix 0 (Nat.zero_le τ) = .nil := by cases P.prefix 0 (Nat.zero_le τ); rfl
  1295. rw [hnil] at hpr
  1296. simpa [SLP.evalWire] using hpr.symm
  1297. rw [h1]; simp [M]
  1298. | t' + 2 =>
  1299. have ht'_lt : t' < τ := by omega
  1300. have hgate := P.evalWire_gate t' ht'_lt
  1301. have hin1_lt : (P.gateIn1 t' ht'_lt).val < t' + 2 := (P.gateIn1 t' ht'_lt).isLt
  1302. have hin2_lt : (P.gateIn2 t' ht'_lt).val < t' + 2 := (P.gateIn2 t' ht'_lt).isLt
  1303. have h1 := ihk (P.gateIn1 t' ht'_lt).val hin1_lt (by omega)
  1304. have h2 := ihk (P.gateIn2 t' ht'_lt).val hin2_lt (by omega)
  1305. have h1' : ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x).natAbs ≤ M ^ (2 ^ t') :=
  1306. le_trans h1 (Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) (by omega)))
  1307. have h2' : ((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x).natAbs ≤ M ^ (2 ^ t') :=
  1308. le_trans h2 (Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) (by omega)))
  1309. have hsq : M ^ (2 ^ t') * M ^ (2 ^ t') = M ^ (2 ^ (t' + 1)) := by
  1310. rw [← pow_add, ← two_mul, pow_succ, Nat.mul_comm]
  1311. have h2_le : 2 ≤ M ^ (2 ^ t') :=
  1312. le_trans hM2 (Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) M)
  1313. have heval : (P.evalWire ⟨t' + 2, by omega⟩).eval x =
  1314. (P.gateOp t' ht'_lt).applyInt
  1315. ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x)
  1316. ((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x) := by
  1317. rw [SLPOp.applyInt_eq_eval]
  1318. exact congrArg (fun p : Polynomial ℤ => p.eval x) hgate
  1319. rw [heval]
  1320. have hsub : t' + 2 - 1 = t' + 1 := by omega
  1321. rw [hsub]
  1322. cases P.gateOp t' ht'_lt with
  1323. | add =>
  1324. simp only [SLPOp.applyInt]
  1325. have := Int.natAbs_add_le
  1326. ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x)
  1327. ((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x)
  1328. nlinarith
  1329. | sub =>
  1330. simp only [SLPOp.applyInt]
  1331. have := Int.natAbs_add_le
  1332. ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x)
  1333. (-((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x))
  1334. rw [Int.natAbs_neg] at this
  1335. have hsub_eq : ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x) -
  1336. ((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x) =
  1337. ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x) +
  1338. (-((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x)) := by ring
  1339. rw [hsub_eq]; nlinarith
  1340. | mul =>
  1341. simp only [SLPOp.applyInt, Int.natAbs_mul]
  1342. rw [← hsq]; exact Nat.mul_le_mul h1' h2'
  1343. unfold wireHeight
  1344. apply max_le (le_trans hM2 (Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) M))
  1345. apply Finset.sup_le
  1346. intro i _
  1347. rw [P.evalWire_prefix a ha i]
  1348. have hi_le : i.val - 1 ≤ a := by have := i.isLt; omega
  1349. exact le_trans (hwire i.val i.isLt)
  1350. (Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) hi_le))
  1351. /-- Coefficient `ℓ₁`-norm of `f ∈ ℤ[X]`: `∑ j ∈ f.support, (f.coeff j).natAbs`. -/
  1352. noncomputable def polyIntL1 (f : Polynomial ℤ) : ℕ :=
  1353. ∑ j ∈ f.support, (f.coeff j).natAbs
  1354. private lemma polyIntL1_eq_sum_range (f : Polynomial ℤ) {N : ℕ} (hN : ∀ j ∈ f.support, j < N) :
  1355. polyIntL1 f = ∑ j ∈ Finset.range N, (f.coeff j).natAbs := by
  1356. unfold polyIntL1
  1357. apply Finset.sum_subset (fun j hj => Finset.mem_range.mpr (hN j hj))
  1358. intro j _ hj
  1359. have : f.coeff j = 0 := Polynomial.notMem_support_iff.mp hj
  1360. simp [this]
  1361. private lemma polyIntL1_add_le (f g : Polynomial ℤ) :
  1362. polyIntL1 (f + g) ≤ polyIntL1 f + polyIntL1 g := by
  1363. set N := max (f.natDegree + 1) (g.natDegree + 1)
  1364. have hf : ∀ j ∈ f.support, j < N := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega
  1365. have hg : ∀ j ∈ g.support, j < N := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega
  1366. have hfg : ∀ j ∈ (f + g).support, j < N := fun j hj => by
  1367. have h1 := Polynomial.le_natDegree_of_mem_supp j hj
  1368. have h2 := Polynomial.natDegree_add_le f g; omega
  1369. rw [polyIntL1_eq_sum_range (f + g) hfg, polyIntL1_eq_sum_range f hf,
  1370. polyIntL1_eq_sum_range g hg, ← Finset.sum_add_distrib]
  1371. apply Finset.sum_le_sum; intro j _
  1372. simp only [Polynomial.coeff_add]
  1373. exact Int.natAbs_add_le _ _
  1374. private lemma polyIntL1_sub_le (f g : Polynomial ℤ) :
  1375. polyIntL1 (f - g) ≤ polyIntL1 f + polyIntL1 g := by
  1376. set N := max (f.natDegree + 1) (g.natDegree + 1)
  1377. have hf : ∀ j ∈ f.support, j < N := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega
  1378. have hg : ∀ j ∈ g.support, j < N := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega
  1379. have hfg : ∀ j ∈ (f - g).support, j < N := fun j hj => by
  1380. have h1 := Polynomial.le_natDegree_of_mem_supp j hj
  1381. have h2 := Polynomial.natDegree_sub_le f g; omega
  1382. rw [polyIntL1_eq_sum_range (f - g) hfg, polyIntL1_eq_sum_range f hf,
  1383. polyIntL1_eq_sum_range g hg, ← Finset.sum_add_distrib]
  1384. apply Finset.sum_le_sum; intro j _
  1385. simp only [Polynomial.coeff_sub]
  1386. simpa [sub_eq_add_neg, Int.natAbs_neg] using Int.natAbs_add_le (f.coeff j) (-(g.coeff j))
  1387. private lemma polyIntL1_mul_le (f g : Polynomial ℤ) :
  1388. polyIntL1 (f * g) ≤ polyIntL1 f * polyIntL1 g := by
  1389. set Nf := f.natDegree + 1
  1390. set Ng := g.natDegree + 1
  1391. have hf : ∀ j ∈ f.support, j < Nf := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega
  1392. have hg : ∀ j ∈ g.support, j < Ng := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega
  1393. have hfg : ∀ j ∈ (f * g).support, j < Nf + Ng := fun j hj => by
  1394. have h1 := Polynomial.le_natDegree_of_mem_supp j hj
  1395. have h2 := Polynomial.natDegree_mul_le (p := f) (q := g); omega
  1396. rw [polyIntL1_eq_sum_range (f * g) hfg, polyIntL1_eq_sum_range f hf,
  1397. polyIntL1_eq_sum_range g hg, Finset.sum_mul_sum]
  1398. have htri : ∀ (s : Finset (ℕ × ℕ)) (h : ℕ × ℕ → ℤ),
  1399. (∑ p ∈ s, h p).natAbs ≤ ∑ p ∈ s, (h p).natAbs := by
  1400. intro s h
  1401. induction s using Finset.induction_on with
  1402. | empty => simp
  1403. | @insert u s' hu ihs =>
  1404. rw [Finset.sum_insert hu, Finset.sum_insert hu]
  1405. exact le_trans (Int.natAbs_add_le _ _) (Nat.add_le_add_left ihs _)
  1406. calc ∑ j ∈ Finset.range (Nf + Ng), ((f * g).coeff j).natAbs
  1407. ≤ ∑ j ∈ Finset.range (Nf + Ng),
  1408. ∑ p ∈ Finset.antidiagonal j, (f.coeff p.1).natAbs * (g.coeff p.2).natAbs := by
  1409. apply Finset.sum_le_sum; intro j _
  1410. rw [Polynomial.coeff_mul]
  1411. calc (∑ p ∈ Finset.antidiagonal j, f.coeff p.1 * g.coeff p.2).natAbs
  1412. ≤ ∑ p ∈ Finset.antidiagonal j, (f.coeff p.1 * g.coeff p.2).natAbs := htri _ _
  1413. _ = ∑ p ∈ Finset.antidiagonal j, (f.coeff p.1).natAbs * (g.coeff p.2).natAbs := by
  1414. apply Finset.sum_congr rfl; intro p _; exact Int.natAbs_mul _ _
  1415. _ ≤ ∑ p ∈ Finset.range Nf ×ˢ Finset.range Ng, (f.coeff p.1).natAbs * (g.coeff p.2).natAbs := by
  1416. rw [← Finset.sum_biUnion]
  1417. · apply Finset.sum_le_sum_of_ne_zero
  1418. intro p _ hne
  1419. have hp1 : p.1 < Nf := by
  1420. by_contra h
  1421. have h0 : f.coeff p.1 = 0 := Polynomial.coeff_eq_zero_of_natDegree_lt (by omega)
  1422. simp [h0] at hne
  1423. have hp2 : p.2 < Ng := by
  1424. by_contra h
  1425. have h0 : g.coeff p.2 = 0 := Polynomial.coeff_eq_zero_of_natDegree_lt (by omega)
  1426. simp [h0] at hne
  1427. exact Finset.mem_product.mpr ⟨Finset.mem_range.mpr hp1, Finset.mem_range.mpr hp2⟩
  1428. · intro i _ j _ hij
  1429. exact Finset.disjoint_left.mpr fun p hpi hpj =>
  1430. hij ((Finset.mem_antidiagonal.mp hpi).symm.trans (Finset.mem_antidiagonal.mp hpj))
  1431. _ = ∑ i ∈ Finset.range Nf, ∑ j ∈ Finset.range Ng,
  1432. (f.coeff i).natAbs * (g.coeff j).natAbs := Finset.sum_product _ _ _
  1433. /-- Every wire of `P : SLP τ` has coefficient `ℓ₁`-norm at most `2 ^ (2 ^ τ)`. -/
  1434. lemma SLP.wire_polyIntL1_le :
  1435. ∀ {τ : ℕ} (P : SLP τ) (w : Fin (τ + 2)), polyIntL1 (P.evalWire w) ≤ 2 ^ (2 ^ τ)
  1436. | 0, .nil, w => by
  1437. simp only [SLP.evalWire]
  1438. split_ifs
  1439. · have : polyIntL1 (1 : Polynomial ℤ) = 1 := by
  1440. unfold polyIntL1
  1441. rw [← Polynomial.C_1, Polynomial.support_C (by norm_num : (1 : ℤ) ≠ 0)]
  1442. simp
  1443. rw [this]; omega
  1444. · have : polyIntL1 (Polynomial.X : Polynomial ℤ) = 1 := by
  1445. unfold polyIntL1; rw [Polynomial.support_X]; simp
  1446. rw [this]; omega
  1447. | τ + 1, .snoc P op i j, w => by
  1448. have ih1 := P.wire_polyIntL1_le i
  1449. have ih2 := P.wire_polyIntL1_le j
  1450. have hsq : 2 ^ (2 ^ τ) * 2 ^ (2 ^ τ) = 2 ^ (2 ^ (τ + 1)) := by
  1451. rw [← pow_add, ← two_mul, pow_succ, Nat.mul_comm]
  1452. have h2_le : 2 ≤ 2 ^ (2 ^ τ) :=
  1453. Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) 2
  1454. simp only [SLP.evalWire]
  1455. split_ifs with hw
  1456. · exact le_trans (P.wire_polyIntL1_le ⟨w.val, hw⟩)
  1457. (Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) (by omega)))
  1458. · cases op with
  1459. | add =>
  1460. simp only [SLPOp.applyPoly]
  1461. have := polyIntL1_add_le (P.evalWire i) (P.evalWire j)
  1462. nlinarith
  1463. | sub =>
  1464. simp only [SLPOp.applyPoly]
  1465. have := polyIntL1_sub_le (P.evalWire i) (P.evalWire j)
  1466. nlinarith
  1467. | mul =>
  1468. simp only [SLPOp.applyPoly]
  1469. rw [← hsq]; exact le_trans (polyIntL1_mul_le _ _) (Nat.mul_le_mul ih1 ih2)
  1470. /-! ## 4. Cauchy Bounds, Backward Propagation & Multi-Segment Gap-Dominance Theorem -/
  1471. private lemma eval_polyT_eq (F : Polynomial (Polynomial ℤ)) (u : Polynomial ℤ) (x : ℤ) :
  1472. (F.eval u).eval x = ∑ j ∈ Finset.range (F.natDegree + 1), (F.coeff j).eval x * (u.eval x) ^ j := by
  1473. rw [Polynomial.eval_eq_sum_range (p := F), ← Polynomial.coe_evalRingHom, map_sum]
  1474. apply Finset.sum_congr rfl; intro j _
  1475. simp
  1476. /-- Triangle bound on lower terms `j < D` of `F(x, U)`:
  1477. `|∑_{j < D} c_j(x) * U^j| ≤ |U|^(D - 1) * polyL1At F x` whenever `1 ≤ D` and `1 ≤ |U|`. -/
  1478. private lemma lower_terms_bound (F : Polynomial (Polynomial ℤ)) (x U : ℤ)
  1479. (hD : 1 ≤ F.natDegree) (hU : 1 ≤ U.natAbs) :
  1480. (∑ j ∈ Finset.range F.natDegree, (F.coeff j).eval x * U ^ j).natAbs ≤
  1481. U.natAbs ^ (F.natDegree - 1) * polyL1At F x := by
  1482. have htri : ∀ (s : Finset ℕ) (g : ℕ → ℤ),
  1483. (∑ j ∈ s, g j).natAbs ≤ ∑ j ∈ s, (g j).natAbs := by
  1484. intro s g
  1485. induction s using Finset.induction_on with
  1486. | empty => simp
  1487. | @insert u s' hu ihs =>
  1488. rw [Finset.sum_insert hu, Finset.sum_insert hu]
  1489. exact le_trans (Int.natAbs_add_le _ _) (Nat.add_le_add_left ihs _)
  1490. have hsum_le : ∑ j ∈ Finset.range F.natDegree, ((F.coeff j).eval x).natAbs ≤ polyL1At F x := by
  1491. rw [polyL1At_eq_sum_range F x (N := F.natDegree + 1) (fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega)]
  1492. rw [Finset.sum_range_succ]; omega
  1493. calc (∑ j ∈ Finset.range F.natDegree, (F.coeff j).eval x * U ^ j).natAbs
  1494. ≤ ∑ j ∈ Finset.range F.natDegree, ((F.coeff j).eval x * U ^ j).natAbs := htri _ _
  1495. _ = ∑ j ∈ Finset.range F.natDegree, ((F.coeff j).eval x).natAbs * U.natAbs ^ j := by
  1496. apply Finset.sum_congr rfl; intro j _
  1497. rw [Int.natAbs_mul, Int.natAbs_pow]
  1498. _ ≤ ∑ j ∈ Finset.range F.natDegree, ((F.coeff j).eval x).natAbs * U.natAbs ^ (F.natDegree - 1) := by
  1499. apply Finset.sum_le_sum; intro j hj
  1500. have hj_le : j ≤ F.natDegree - 1 := by have := Finset.mem_range.mp hj; omega
  1501. exact Nat.mul_le_mul_left _ (Nat.pow_le_pow_right hU hj_le)
  1502. _ = (∑ j ∈ Finset.range F.natDegree, ((F.coeff j).eval x).natAbs) * U.natAbs ^ (F.natDegree - 1) :=
  1503. (Finset.sum_mul _ _ _).symm
  1504. _ ≤ polyL1At F x * U.natAbs ^ (F.natDegree - 1) := Nat.mul_le_mul_right _ hsum_le
  1505. _ = U.natAbs ^ (F.natDegree - 1) * polyL1At F x := Nat.mul_comm _ _
  1506. /-- **Cauchy Root Bound for `F(x, U) = 0`:** If `F.natDegree ≥ 1`, `(F.leadingCoeff).eval x ≠ 0`,
  1507. and `(F.eval u).eval x = 0`, then `|u(x)| ≤ polyL1At F x`. -/
  1508. private lemma cauchy_root_bound_polyT (F : Polynomial (Polynomial ℤ)) (u : Polynomial ℤ) (x : ℤ)
  1509. (hD : 1 ≤ F.natDegree) (hlead : (F.leadingCoeff).eval x ≠ 0)
  1510. (hzero : (F.eval u).eval x = 0) :
  1511. (u.eval x).natAbs ≤ polyL1At F x := by
  1512. set U := u.eval x
  1513. by_cases hU0 : U.natAbs = 0
  1514. · omega
  1515. have hU1 : 1 ≤ U.natAbs := by omega
  1516. have hsum := eval_polyT_eq F u x
  1517. rw [hzero, Finset.sum_range_succ] at hsum
  1518. set low := ∑ j ∈ Finset.range F.natDegree, (F.coeff j).eval x * U ^ j
  1519. have heq : (F.coeff F.natDegree).eval x * U ^ F.natDegree = -low := by linarith
  1520. have hlead_pos : 1 ≤ ((F.coeff F.natDegree).eval x).natAbs := Int.natAbs_pos.mpr hlead
  1521. have hpow_split : U.natAbs ^ F.natDegree = U.natAbs ^ (F.natDegree - 1) * U.natAbs := by
  1522. conv_lhs => rw [← Nat.sub_add_cancel hD, pow_succ]
  1523. have hlow := lower_terms_bound F x U hD hU1
  1524. have h1 : U.natAbs ^ F.natDegree ≤ low.natAbs := by
  1525. calc U.natAbs ^ F.natDegree
  1526. = 1 * U.natAbs ^ F.natDegree := (Nat.one_mul _).symm
  1527. _ ≤ ((F.coeff F.natDegree).eval x).natAbs * U.natAbs ^ F.natDegree :=
  1528. Nat.mul_le_mul_right _ hlead_pos
  1529. _ = ((F.coeff F.natDegree).eval x * U ^ F.natDegree).natAbs := by
  1530. rw [Int.natAbs_mul, Int.natAbs_pow]
  1531. _ = (-low).natAbs := by rw [heq]
  1532. _ = low.natAbs := Int.natAbs_neg _
  1533. rw [hpow_split] at h1
  1534. have hpos : 0 < U.natAbs ^ (F.natDegree - 1) := Nat.pow_pos hU1
  1535. exact Nat.le_of_mul_le_mul_left (le_trans h1 hlow) hpos
  1536. /-- **Cauchy Dominance Lower Bound:** If `F.natDegree ≥ 1`, `(F.leadingCoeff).eval x ≠ 0`, and
  1537. `2 * polyL1At F x < |u(x)|`, then `|u(x)| ≤ 2 * |(F.eval u).eval x|`. -/
  1538. private lemma cauchy_dominance_polyT (F : Polynomial (Polynomial ℤ)) (u : Polynomial ℤ) (x : ℤ)
  1539. (hD : 1 ≤ F.natDegree) (hlead : (F.leadingCoeff).eval x ≠ 0)
  1540. (hbig : 2 * polyL1At F x < (u.eval x).natAbs) :
  1541. (u.eval x).natAbs ≤ 2 * ((F.eval u).eval x).natAbs := by
  1542. set U := u.eval x
  1543. have hU1 : 1 ≤ U.natAbs := by omega
  1544. have hsum := eval_polyT_eq F u x
  1545. rw [Finset.sum_range_succ] at hsum
  1546. set low := ∑ j ∈ Finset.range F.natDegree, (F.coeff j).eval x * U ^ j
  1547. set top := (F.coeff F.natDegree).eval x * U ^ F.natDegree
  1548. have hlead_pos : 1 ≤ ((F.coeff F.natDegree).eval x).natAbs := Int.natAbs_pos.mpr hlead
  1549. have htop_ge : U.natAbs ^ F.natDegree ≤ top.natAbs := by
  1550. calc U.natAbs ^ F.natDegree
  1551. = 1 * U.natAbs ^ F.natDegree := (Nat.one_mul _).symm
  1552. _ ≤ ((F.coeff F.natDegree).eval x).natAbs * U.natAbs ^ F.natDegree :=
  1553. Nat.mul_le_mul_right _ hlead_pos
  1554. _ = top.natAbs := by simp [top, Int.natAbs_mul, Int.natAbs_pow]
  1555. have htri : top.natAbs ≤ ((F.eval u).eval x).natAbs + low.natAbs := by
  1556. have hrew : top = (F.eval u).eval x + (-low) := by linarith
  1557. calc top.natAbs = ((F.eval u).eval x + (-low)).natAbs := by rw [← hrew]
  1558. _ ≤ ((F.eval u).eval x).natAbs + (-low).natAbs := Int.natAbs_add_le _ _
  1559. _ = ((F.eval u).eval x).natAbs + low.natAbs := by rw [Int.natAbs_neg]
  1560. have hlow := lower_terms_bound F x U hD hU1
  1561. have hpow_split : U.natAbs ^ F.natDegree = U.natAbs ^ (F.natDegree - 1) * U.natAbs := by
  1562. conv_lhs => rw [← Nat.sub_add_cancel hD, pow_succ]
  1563. have h1 : U.natAbs ^ (F.natDegree - 1) * (2 * polyL1At F x) ≤ U.natAbs ^ (F.natDegree - 1) * U.natAbs :=
  1564. Nat.mul_le_mul_left _ (le_of_lt hbig)
  1565. have h2 : 2 * low.natAbs ≤ U.natAbs ^ F.natDegree := by
  1566. rw [hpow_split]; linarith
  1567. have h3 : U.natAbs ^ F.natDegree ≤ 2 * ((F.eval u).eval x).natAbs := by omega
  1568. have h4 : U.natAbs ≤ U.natAbs ^ F.natDegree := Nat.le_self_pow (by omega) U.natAbs
  1569. omega
  1570. /-- Cauchy dominance for a single univariate polynomial `w ∈ ℤ[X]` (`deg w ≥ 1`):
  1571. if `2 * polyIntL1 w ≤ |x|`, then `|x| ≤ 2 * |w(x)|`. -/
  1572. private lemma cauchy_dominance_univar (w : Polynomial ℤ) (x : ℤ)
  1573. (hD : 1 ≤ w.natDegree) (hbig : 2 * polyIntL1 w ≤ x.natAbs) :
  1574. x.natAbs ≤ 2 * (w.eval x).natAbs := by
  1575. have hx1 : 1 ≤ x.natAbs := by
  1576. have hne : w ≠ 0 := fun h0 => by simp [h0] at hD
  1577. have hlead : 1 ≤ (w.coeff w.natDegree).natAbs := Int.natAbs_pos.mpr (Polynomial.leadingCoeff_ne_zero.mpr hne)
  1578. have hl1 : 1 ≤ polyIntL1 w := by
  1579. rw [polyIntL1_eq_sum_range w (N := w.natDegree + 1) (fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega)]
  1580. rw [Finset.sum_range_succ]; omega
  1581. omega
  1582. have hsum : w.eval x = (∑ j ∈ Finset.range w.natDegree, w.coeff j * x ^ j) +
  1583. w.coeff w.natDegree * x ^ w.natDegree := by
  1584. rw [Polynomial.eval_eq_sum_range, Finset.sum_range_succ]
  1585. set low := ∑ j ∈ Finset.range w.natDegree, w.coeff j * x ^ j
  1586. set top := w.coeff w.natDegree * x ^ w.natDegree
  1587. have hne : w ≠ 0 := fun h0 => by simp [h0] at hD
  1588. have hlead_pos : 1 ≤ (w.coeff w.natDegree).natAbs :=
  1589. Int.natAbs_pos.mpr (Polynomial.leadingCoeff_ne_zero.mpr hne)
  1590. have htop_ge : x.natAbs ^ w.natDegree ≤ top.natAbs := by
  1591. calc x.natAbs ^ w.natDegree
  1592. = 1 * x.natAbs ^ w.natDegree := (Nat.one_mul _).symm
  1593. _ ≤ (w.coeff w.natDegree).natAbs * x.natAbs ^ w.natDegree := Nat.mul_le_mul_right _ hlead_pos
  1594. _ = top.natAbs := by simp [top, Int.natAbs_mul, Int.natAbs_pow]
  1595. have htri : top.natAbs ≤ (w.eval x).natAbs + low.natAbs := by
  1596. have hrew : top = w.eval x + (-low) := by linarith
  1597. calc top.natAbs = (w.eval x + (-low)).natAbs := by rw [← hrew]
  1598. _ ≤ (w.eval x).natAbs + (-low).natAbs := Int.natAbs_add_le _ _
  1599. _ = (w.eval x).natAbs + low.natAbs := by rw [Int.natAbs_neg]
  1600. have htri_sum : ∀ (s : Finset ℕ) (g : ℕ → ℤ),
  1601. (∑ j ∈ s, g j).natAbs ≤ ∑ j ∈ s, (g j).natAbs := by
  1602. intro s g
  1603. induction s using Finset.induction_on with
  1604. | empty => simp
  1605. | @insert u s' hu ihs =>
  1606. rw [Finset.sum_insert hu, Finset.sum_insert hu]
  1607. exact le_trans (Int.natAbs_add_le _ _) (Nat.add_le_add_left ihs _)
  1608. have hlow_sum : ∑ j ∈ Finset.range w.natDegree, (w.coeff j).natAbs ≤ polyIntL1 w := by
  1609. rw [polyIntL1_eq_sum_range w (N := w.natDegree + 1) (fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega)]
  1610. rw [Finset.sum_range_succ]; omega
  1611. have hlow : low.natAbs ≤ x.natAbs ^ (w.natDegree - 1) * polyIntL1 w := by
  1612. calc low.natAbs
  1613. ≤ ∑ j ∈ Finset.range w.natDegree, (w.coeff j * x ^ j).natAbs := htri_sum _ _
  1614. _ = ∑ j ∈ Finset.range w.natDegree, (w.coeff j).natAbs * x.natAbs ^ j := by
  1615. apply Finset.sum_congr rfl; intro j _; rw [Int.natAbs_mul, Int.natAbs_pow]
  1616. _ ≤ ∑ j ∈ Finset.range w.natDegree, (w.coeff j).natAbs * x.natAbs ^ (w.natDegree - 1) := by
  1617. apply Finset.sum_le_sum; intro j hj
  1618. exact Nat.mul_le_mul_left _ (Nat.pow_le_pow_right hx1 (by have := Finset.mem_range.mp hj; omega))
  1619. _ = (∑ j ∈ Finset.range w.natDegree, (w.coeff j).natAbs) * x.natAbs ^ (w.natDegree - 1) :=
  1620. (Finset.sum_mul _ _ _).symm
  1621. _ ≤ polyIntL1 w * x.natAbs ^ (w.natDegree - 1) := Nat.mul_le_mul_right _ hlow_sum
  1622. _ = x.natAbs ^ (w.natDegree - 1) * polyIntL1 w := Nat.mul_comm _ _
  1623. have hpow_split : x.natAbs ^ w.natDegree = x.natAbs ^ (w.natDegree - 1) * x.natAbs := by
  1624. conv_lhs => rw [← Nat.sub_add_cancel hD, pow_succ]
  1625. have h1 : x.natAbs ^ (w.natDegree - 1) * (2 * polyIntL1 w) ≤ x.natAbs ^ (w.natDegree - 1) * x.natAbs :=
  1626. Nat.mul_le_mul_left _ hbig
  1627. have h2 : 2 * low.natAbs ≤ x.natAbs ^ w.natDegree := by rw [hpow_split]; linarith
  1628. have h3 : x.natAbs ^ w.natDegree ≤ 2 * (w.eval x).natAbs := by omega
  1629. have h4 : x.natAbs ≤ x.natAbs ^ w.natDegree := Nat.le_self_pow (by omega) x.natAbs
  1630. omega
  1631. /-- Pure-`ℕ` arithmetic helper: if `5 ≤ u`, `u ^ 2 ^ (g - 1) ≤ u ^ 2 ^ e` is impossible when `e + 2 ≤ g`,
  1632. and `u ≤ 2 * v` implies `u ^ (2 ^ (g - 1)) ≤ v ^ (2 ^ g)` (since `4 ≤ u` implies `u ≤ (u / 2)^2`,
  1633. actually `u * 4 ≤ u ^ 2`, so `(2 * v)^2 ≥ u^2 ≥ 4 * u` => `v^2 ≥ u`!). -/
  1634. private lemma nat_half_sq_ge {u v : ℕ} (hu : 4 ≤ u) (huv : u ≤ 2 * v) : u ≤ v ^ 2 := by
  1635. nlinarith
  1636. /-- **Multi-Segment Gap-Dominance Theorem:**
  1637. If `P : SLP τ` (`P.evalPoly ≠ 0`) admits a list `L` satisfying `ValidGapSegs Δ L`, then
  1638. `intRootCount P.evalPoly ≤ (L.length + 3) * 2 ^ (τ - Δ)`. -/
  1639. theorem slp_multi_segment_gap_dominance {τ Δ : ℕ} (P : SLP τ) (hP : P.evalPoly ≠ 0)
  1640. (L : List (UntappedSeg P)) (hvalid : ValidGapSegs Δ L) :
  1641. intRootCount P.evalPoly ≤ (L.length + 3) * 2 ^ (τ - Δ) := by
  1642. classical
  1643. rcases hvalid with ⟨hL_ne, hord, hΔ, hhead_gap, hmid_gap⟩
  1644. have hq_pos : 1 ≤ L.length := by cases L <;> simp at hL_ne ⊢
  1645. -- Helper: for r : Fin L.length, define seg r, nextA r, target wire wTarget r, and F r : Polynomial (Polynomial ℤ).
  1646. let seg : Fin L.length → UntappedSeg P := fun r => L[r.val]'r.isLt
  1647. have hseg_ord : ∀ (r : Fin L.length) (hr : r.val + 1 < L.length),
  1648. (seg r).b ≤ (seg ⟨r.val + 1, hr⟩).a := by
  1649. have hgen : ∀ (M : List (UntappedSeg P)), SegsOrdered M →
  1650. ∀ (i : ℕ) (hi : i + 1 < M.length), (M[i]'(by omega)).b ≤ (M[i + 1]'hi).a := by
  1651. intro M
  1652. induction M with
  1653. | nil => intro _ i hi; simp at hi
  1654. | cons s0 rest ih =>
  1655. intro ho i hi
  1656. cases i with
  1657. | zero =>
  1658. cases rest with
  1659. | nil => simp at hi
  1660. | cons s1 rest' => exact ho.1
  1661. | succ i' =>
  1662. cases rest with
  1663. | nil => simp at hi
  1664. | cons s1 rest' => exact ih ho.2 i' (by simpa using hi)
  1665. intro r hr
  1666. exact hgen L hord r.val hr
  1667. let nextC : Fin L.length → ℕ := fun r =>
  1668. if h : r.val + 1 < L.length then (seg ⟨r.val + 1, h⟩).a else τ
  1669. have hnextC_ge : ∀ r : Fin L.length, (seg r).b ≤ nextC r := by
  1670. intro r; dsimp only [nextC]
  1671. split_ifs with h
  1672. · exact hseg_ord r h
  1673. · exact (seg r).hb
  1674. have hnextC_le : ∀ r : Fin L.length, nextC r ≤ τ := by
  1675. intro r; dsimp only [nextC]
  1676. split_ifs with h
  1677. · exact le_of_lt (lt_of_lt_of_le (seg ⟨r.val + 1, h⟩).hab (seg ⟨r.val + 1, h⟩).hb)
  1678. · exact le_rfl
  1679. let wTarget : (r : Fin L.length) → Fin (nextC r + 2) := fun r =>
  1680. if h : r.val + 1 < L.length then
  1681. let sNext := seg ⟨r.val + 1, h⟩
  1682. ⟨(P.gateIn1 sNext.a (by have := sNext.hab; have := sNext.hb; omega)).val, by
  1683. have hlt := (P.gateIn1 sNext.a (by have := sNext.hab; have := sNext.hb; omega)).isLt
  1684. dsimp only [nextC]; rw [dite_eq_left_of_eq_true (eq_true h)]; exact hlt⟩
  1685. else
  1686. ⟨τ + 1, by
  1687. dsimp only [nextC]; rw [dite_eq_right_of_eq_false (eq_false h)]; omega⟩
  1688. let F : Fin L.length → Polynomial (Polynomial ℤ) := fun r =>
  1689. evalFormalT P (seg r).a (seg r).b (seg r).hab (seg r).hb
  1690. (nextC r) (hnextC_ge r) (hnextC_le r) (wTarget r)
  1691. -- Check top segment (last index qlast)
  1692. let qlast : Fin L.length := ⟨L.length - 1, by omega⟩
  1693. have hqlast_not : ¬ (qlast.val + 1 < L.length) := by dsimp [qlast]; omega
  1694. have hqlast_C : nextC qlast = τ := by
  1695. dsimp only [nextC]; rw [dite_eq_right_of_eq_false (eq_false hqlast_not)]
  1696. have hqlast_spec : (F qlast).eval (P.evalWire ⟨(seg qlast).b + 1, by have := (seg qlast).hb; omega⟩) =
  1697. P.evalPoly := by
  1698. have hnot : ¬ ((seg qlast).a + 2 ≤ (wTarget qlast).val ∧ (wTarget qlast).val ≤ (seg qlast).b) := by
  1699. have hw : (wTarget qlast).val = τ + 1 := by
  1700. dsimp only [wTarget]; rw [dite_eq_right_of_eq_false (eq_false hqlast_not)]
  1701. have := (seg qlast).hb; omega
  1702. have hspec := evalFormalT_spec P (seg qlast) (nextC qlast) (hnextC_ge qlast) (hnextC_le qlast)
  1703. (wTarget qlast) hnot
  1704. have hw : (wTarget qlast).val = τ + 1 := by
  1705. dsimp only [wTarget]; rw [dite_eq_right_of_eq_false (eq_false hqlast_not)]
  1706. simpa [F, SLP.evalPoly, hw] using hspec
  1707. -- Case 0: (F qlast).natDegree = 0 -> P.evalPoly = (F qlast).coeff 0 has degree ≤ 2 ^ (τ - Δ)!
  1708. by_cases htop0 : (F qlast).natDegree = 0
  1709. · have hF_eq : F qlast = Polynomial.C ((F qlast).coeff 0) :=
  1710. Polynomial.eq_C_of_natDegree_eq_zero htop0
  1711. have hP_eq : P.evalPoly = (F qlast).coeff 0 := by
  1712. calc P.evalPoly = (F qlast).eval (P.evalWire ⟨(seg qlast).b + 1, by have := (seg qlast).hb; omega⟩) := hqlast_spec.symm
  1713. _ = (Polynomial.C ((F qlast).coeff 0)).eval _ := by conv_lhs => rw [hF_eq]
  1714. _ = (F qlast).coeff 0 := Polynomial.eval_C
  1715. have hdeg_le : P.evalPoly.natDegree ≤ 2 ^ (τ - Δ) := by
  1716. rw [hP_eq]
  1717. have h1 := evalFormalT_coeff_natDegree_le P (seg qlast).a (seg qlast).b (seg qlast).hab
  1718. (seg qlast).hb (nextC qlast) (hnextC_ge qlast) (hnextC_le qlast) (wTarget qlast) 0
  1719. change ((F qlast).coeff 0).natDegree ≤ 2 ^ (nextC qlast - (seg qlast).b) * (P.prefix (seg qlast).a _).maxDeg at h1
  1720. rw [hqlast_C] at h1
  1721. have h2 := (P.prefix (seg qlast).a (by have := (seg qlast).hab; have := (seg qlast).hb; omega)).maxDeg_le_two_pow
  1722. have h3 : 2 ^ (τ - (seg qlast).b) * 2 ^ (seg qlast).a = 2 ^ ((τ - (seg qlast).b) + (seg qlast).a) :=
  1723. (pow_add 2 _ _).symm
  1724. have h4 : Δ ≤ (seg qlast).b - (seg qlast).a := hΔ (seg qlast) (List.getElem_mem _)
  1725. have h5 := (seg qlast).hab; have h6 := (seg qlast).hb
  1726. have h7 : (τ - (seg qlast).b) + (seg qlast).a ≤ τ - Δ := by omega
  1727. calc ((F qlast).coeff 0).natDegree
  1728. ≤ 2 ^ (τ - (seg qlast).b) * (P.prefix (seg qlast).a _).maxDeg := h1
  1729. _ ≤ 2 ^ (τ - (seg qlast).b) * 2 ^ (seg qlast).a := Nat.mul_le_mul_left _ h2
  1730. _ = 2 ^ ((τ - (seg qlast).b) + (seg qlast).a) := h3
  1731. _ ≤ 2 ^ (τ - Δ) := Nat.pow_le_pow_right (by omega) h7
  1732. have hroots : intRootCount P.evalPoly ≤ P.evalPoly.natDegree :=
  1733. le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _)
  1734. calc intRootCount P.evalPoly
  1735. ≤ 2 ^ (τ - Δ) := le_trans hroots hdeg_le
  1736. _ = 1 * 2 ^ (τ - Δ) := (Nat.one_mul _).symm
  1737. _ ≤ (L.length + 3) * 2 ^ (τ - Δ) := Nat.mul_le_mul_right _ (by omega)
  1738. -- Main Case: (F qlast).natDegree ≥ 1.
  1739. -- Prove that EVERY r : Fin L.length has (F r).natDegree ≥ 1!
  1740. have hF_deg_pos : ∀ r : Fin L.length, 1 ≤ (F r).natDegree := by
  1741. intro r
  1742. by_cases hlast : r.val + 1 < L.length
  1743. · by_contra h0
  1744. have hdeg0 : (F r).natDegree = 0 := by omega
  1745. have hF_eq : F r = Polynomial.C ((F r).coeff 0) := Polynomial.eq_C_of_natDegree_eq_zero hdeg0
  1746. let r1 : Fin L.length := ⟨r.val + 1, hlast⟩
  1747. have hnot_inter : ¬ ((seg r).a + 2 ≤ (wTarget r).val ∧ (wTarget r).val ≤ (seg r).b) := by
  1748. have hw_val : (wTarget r).val =
  1749. (P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).val := by
  1750. dsimp only [wTarget]; rw [dite_eq_left_of_eq_true (eq_true hlast)]
  1751. rw [hw_val]
  1752. exact ((seg r).huntapped (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)
  1753. (hseg_ord r hlast)).1
  1754. have hspec := evalFormalT_spec P (seg r) (nextC r) (hnextC_ge r) (hnextC_le r) (wTarget r) hnot_inter
  1755. change (F r).eval (P.evalWire ⟨(seg r).b + 1, by have := (seg r).hb; omega⟩) =
  1756. P.evalWire ⟨(wTarget r).val, by have := (wTarget r).isLt; have := hnextC_le r; omega⟩ at hspec
  1757. rw [hF_eq, Polynomial.eval_C] at hspec
  1758. -- On the one hand, deg(wTarget r) = D₁(a_{r+1}) ≥ deg(U_r) = 2^(g_r) * D₁(a_r)
  1759. have htop_next := (seg r1).hrun (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)
  1760. le_rfl (seg r1).hab
  1761. have hdeg_wt : (P.evalWire ⟨(wTarget r).val, by have := (wTarget r).isLt; have := hnextC_le r; omega⟩).natDegree =
  1762. (P.prefix (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).maxDeg := by
  1763. have hw_val : (wTarget r).val =
  1764. (P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).val := by
  1765. dsimp only [wTarget]; rw [dite_eq_left_of_eq_true (eq_true hlast)]
  1766. have hpr := P.evalWire_prefix (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)
  1767. (P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega))
  1768. have h1 : P.evalWire ⟨(wTarget r).val, by have := (wTarget r).isLt; have := hnextC_le r; omega⟩ =
  1769. (P.prefix (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).evalWire
  1770. (P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)) := by
  1771. rw [hpr]; congr 1; exact Fin.ext hw_val
  1772. rw [h1]; exact htop_next.2.2
  1773. have hrun_r := P.topSq_run_eval (seg r).hab (seg r).hb (seg r).hrun
  1774. have hUr_le : (P.evalWire ⟨(seg r).b + 1, by have := (seg r).hb; omega⟩).natDegree ≤
  1775. (P.prefix (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).maxDeg := by
  1776. have hle_b : (seg r).b ≤ (seg r1).a := hseg_ord r hlast
  1777. have hpr := P.evalWire_prefix (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)
  1778. ⟨(seg r).b + 1, by omega⟩
  1779. rw [← hpr]
  1780. exact (P.prefix (seg r1).a _).wireDeg_le_maxDeg ⟨(seg r).b + 1, by omega⟩
  1781. -- On the other hand, deg((F r).coeff 0) ≤ 2 ^ (nextC r - b_r) * D₁(a_r)
  1782. have hcoeff_le := evalFormalT_coeff_natDegree_le P (seg r).a (seg r).b (seg r).hab (seg r).hb
  1783. (nextC r) (hnextC_ge r) (hnextC_le r) (wTarget r) 0
  1784. -- And g_r > nextC r - b_r by the gap condition!
  1785. have hgap_gt : nextC r - (seg r).b < (seg r).b - (seg r).a := by
  1786. cases hr0 : r.val with
  1787. | zero =>
  1788. cases hL_eq : L with
  1789. | nil => exact absurd hL_eq hL_ne
  1790. | cons s0 rest =>
  1791. have hh := hhead_gap s0 rest hL_eq
  1792. have hs0 : seg r = s0 := by simp [seg, hr0, hL_eq]
  1793. cases rest with
  1794. | nil => simp [hL_eq, hr0] at hlast
  1795. | cons s1 rest' =>
  1796. have hs1 : seg ⟨r.val + 1, hlast⟩ = s1 := by simp [seg, hr0, hL_eq]
  1797. have hnc : nextC r = s1.a := by
  1798. dsimp only [nextC]; rw [dite_eq_left_of_eq_true (eq_true hlast), hs1]
  1799. rw [hnc, hs0]; dsimp at hh; omega
  1800. | succ m =>
  1801. have hm := hmid_gap m (by omega)
  1802. have hsc : seg r = L[m + 1]'(by omega) := by simp [seg, hr0]
  1803. have hnc : nextC r = (L[m + 2]'(by omega)).a := by
  1804. dsimp only [nextC]; rw [dite_eq_left_of_eq_true (eq_true hlast)]
  1805. simp [seg, hr0]
  1806. have hlt2 : m + 2 < L.length := by omega
  1807. simp only [hlt2, ↓reduceDIte] at hm
  1808. rw [hnc, hsc]; omega
  1809. have hpow_lt : 2 ^ (nextC r - (seg r).b) < 2 ^ ((seg r).b - (seg r).a) :=
  1810. Nat.pow_lt_pow_right (by omega) hgap_gt
  1811. have hD1_pos : 1 ≤ (P.prefix (seg r).a (by have := (seg r).hab; have := (seg r).hb; omega)).maxDeg :=
  1812. (P.prefix (seg r).a _).one_le_maxDeg
  1813. have hstrict : 2 ^ (nextC r - (seg r).b) * (P.prefix (seg r).a _).maxDeg <
  1814. 2 ^ ((seg r).b - (seg r).a) * (P.prefix (seg r).a _).maxDeg :=
  1815. Nat.mul_lt_mul_of_pos_right hpow_lt (by omega)
  1816. rw [hspec] at hcoeff_le
  1817. rw [hrun_r.2] at hUr_le
  1818. omega
  1819. · have hreq : r = qlast := Fin.ext (by dsimp [qlast]; omega)
  1820. subst hreq; omega
  1821. -- Leading coefficient of F r is nonzero and has degree ≤ 2 ^ (τ - Δ) for EVERY r : Fin L.length!
  1822. have hlead_ne : ∀ r : Fin L.length, (F r).leadingCoeff ≠ 0 := fun r =>
  1823. Polynomial.leadingCoeff_ne_zero.mpr (fun h0 => by have := hF_deg_pos r; simp [h0] at this)
  1824. have hlead_deg : ∀ r : Fin L.length, (F r).leadingCoeff.natDegree ≤ 2 ^ (τ - Δ) := by
  1825. intro r
  1826. have h1 := evalFormalT_coeff_natDegree_le P (seg r).a (seg r).b (seg r).hab (seg r).hb
  1827. (nextC r) (hnextC_ge r) (hnextC_le r) (wTarget r) (F r).natDegree
  1828. have h2 := (P.prefix (seg r).a (by have := (seg r).hab; have := (seg r).hb; omega)).maxDeg_le_two_pow
  1829. have h4 : Δ ≤ (seg r).b - (seg r).a := hΔ (seg r) (List.getElem_mem _)
  1830. have h5 := (seg r).hab; have h6 := hnextC_ge r; have h7 := hnextC_le r
  1831. have hexp : (nextC r - (seg r).b) + (seg r).a ≤ τ - Δ := by omega
  1832. calc (F r).leadingCoeff.natDegree
  1833. ≤ 2 ^ (nextC r - (seg r).b) * (P.prefix (seg r).a _).maxDeg := h1
  1834. _ ≤ 2 ^ (nextC r - (seg r).b) * 2 ^ (seg r).a := Nat.mul_le_mul_left _ h2
  1835. _ = 2 ^ ((nextC r - (seg r).b) + (seg r).a) := (pow_add 2 _ _).symm
  1836. _ ≤ 2 ^ (τ - Δ) := Nat.pow_le_pow_right (by omega) hexp
  1837. -- First segment's input polynomial w0 = P.evalWire (gateIn1 a_0)
  1838. let s0 : UntappedSeg P := seg ⟨0, hq_pos⟩
  1839. let w0 : Polynomial ℤ :=
  1840. P.evalWire ⟨(P.gateIn1 s0.a (by have := s0.hab; have := s0.hb; omega)).val,
  1841. by have := (P.gateIn1 s0.a (by have := s0.hab; have := s0.hb; omega)).isLt; have := s0.hab; have := s0.hb; omega⟩
  1842. have hw0_deg_pos : 1 ≤ w0.natDegree := by
  1843. have htop0 := s0.hrun s0.a (by have := s0.hab; have := s0.hb; omega) le_rfl s0.hab
  1844. have hpr := P.evalWire_prefix s0.a (by have := s0.hab; have := s0.hb; omega)
  1845. (P.gateIn1 s0.a (by have := s0.hab; have := s0.hb; omega))
  1846. have hdeg_eq : w0.natDegree = (P.prefix s0.a (by have := s0.hab; have := s0.hb; omega)).maxDeg := by
  1847. dsimp only [w0]; rw [← hpr]; exact htop0.2.2
  1848. rw [hdeg_eq]; exact (P.prefix s0.a _).one_le_maxDeg
  1849. have hw0_deg_le : w0.natDegree ≤ 2 ^ (τ - Δ) := by
  1850. have hpr := P.evalWire_prefix s0.a (by have := s0.hab; have := s0.hb; omega)
  1851. (P.gateIn1 s0.a (by have := s0.hab; have := s0.hb; omega))
  1852. have h1 : w0.natDegree ≤ (P.prefix s0.a (by have := s0.hab; have := s0.hb; omega)).maxDeg := by
  1853. dsimp only [w0]; rw [← hpr]; exact (P.prefix s0.a _).wireDeg_le_maxDeg _
  1854. have h2 := (P.prefix s0.a (by have := s0.hab; have := s0.hb; omega)).maxDeg_le_two_pow
  1855. have h4 : Δ ≤ s0.b - s0.a := hΔ s0 (List.getElem_mem _)
  1856. have h5 := s0.hab; have h6 := s0.hb
  1857. exact le_trans (le_trans h1 h2) (Nat.pow_le_pow_right (by omega) (by omega))
  1858. -- Build the finite cover of all integer roots of P.evalPoly:
  1859. -- (⋃_{r : Fin L.length} intRootSet (F r).leadingCoeff) ∪ intRootSet (w0 - 1) ∪ intRootSet w0 ∪ intRootSet (w0 + 1)
  1860. have hsub : intRootSet P.evalPoly ⊆
  1861. (Finset.univ.biUnion (fun r : Fin L.length => intRootSet (F r).leadingCoeff)) ∪
  1862. intRootSet (w0 - 1) ∪ intRootSet w0 ∪ intRootSet (w0 + 1) := by
  1863. intro x hx
  1864. have hPx : P.evalPoly.eval x = 0 :=
  1865. Polynomial.IsRoot.def.mp ((Polynomial.mem_roots hP).mp (Multiset.mem_toFinset.mp hx))
  1866. by_cases hkill : ∃ r : Fin L.length, (F r).leadingCoeff.eval x = 0
  1867. · obtain ⟨r, hr0⟩ := hkill
  1868. apply Finset.mem_union_left; apply Finset.mem_union_left; apply Finset.mem_union_left
  1869. refine Finset.mem_biUnion.mpr ⟨r, Finset.mem_univ _, ?_⟩
  1870. rw [intRootSet, Multiset.mem_toFinset, Polynomial.mem_roots (hlead_ne r), Polynomial.IsRoot.def]
  1871. exact hr0
  1872. · push Not at hkill
  1873. -- Backward propagation: for EVERY r : Fin L.length (from L.length - 1 down to 0),
  1874. -- |U_r(x)| ≤ (wireHeight P (seg r).a _ x) ^ (2 ^ (nextC r - (seg r).b + 1))!
  1875. let P_prop : Fin L.length → Prop := fun r =>
  1876. ((P.evalWire ⟨(seg r).b + 1, by have := (seg r).hb; omega⟩).eval x).natAbs ≤
  1877. (wireHeight P (seg r).a (by have := (seg r).hab; have := (seg r).hb; omega) x) ^
  1878. (2 ^ (nextC r - (seg r).b + 1))
  1879. have hprop : ∀ (d : ℕ) (hd : d < L.length), P_prop ⟨L.length - 1 - d, by omega⟩ := by
  1880. intro d
  1881. induction d with
  1882. | zero =>
  1883. intro _
  1884. have hr_eq : (⟨L.length - 1 - 0, by omega⟩ : Fin L.length) = qlast := Fin.ext (by simp [qlast])
  1885. rw [hr_eq]
  1886. dsimp only [P_prop]
  1887. have hcauchy := cauchy_root_bound_polyT (F qlast)
  1888. (P.evalWire ⟨(seg qlast).b + 1, by have := (seg qlast).hb; omega⟩) x
  1889. (hF_deg_pos qlast) (hkill qlast) (by rw [hqlast_spec, hPx])
  1890. have hl1 := evalFormalT_l1_le P (seg qlast).a (seg qlast).b (seg qlast).hab (seg qlast).hb x
  1891. (nextC qlast) (hnextC_ge qlast) (hnextC_le qlast) (wTarget qlast)
  1892. have hH2 := two_le_wireHeight P (seg qlast).a (by have := (seg qlast).hab; have := (seg qlast).hb; omega) x
  1893. exact le_trans (le_trans hcauchy hl1)
  1894. (Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) (by omega)))
  1895. | succ d' ihd =>
  1896. intro hd
  1897. set r : Fin L.length := ⟨L.length - 1 - (d' + 1), by omega⟩
  1898. set r1 : Fin L.length := ⟨L.length - 1 - d', by omega⟩
  1899. dsimp only [P_prop]
  1900. have hr1_val : r.val + 1 = r1.val := by simp [r, r1]; omega
  1901. have hr_lt : r.val + 1 < L.length := by omega
  1902. have hfin_eq : (⟨r.val + 1, hr_lt⟩ : Fin L.length) = r1 := Fin.ext hr1_val
  1903. have hnext_eq : nextC r = (seg r1).a := by
  1904. dsimp only [nextC]; rw [dite_eq_left_of_eq_true (eq_true hr_lt), hfin_eq]
  1905. set U := ((P.evalWire ⟨(seg r).b + 1, by have := (seg r).hb; omega⟩).eval x).natAbs
  1906. set Hr := wireHeight P (seg r).a (by have := (seg r).hab; have := (seg r).hb; omega) x
  1907. set Rr := nextC r - (seg r).b
  1908. have hHr2 : 2 ≤ Hr := two_le_wireHeight P (seg r).a _ x
  1909. -- Either U ≤ 2 * Hr ^ (2 ^ Rr) (in which case U ≤ Hr ^ (2 ^ (Rr + 1)) immediately!),
  1910. -- or 2 * Hr ^ (2 ^ Rr) < U (derive contradiction from ihd!).
  1911. by_cases hsmall : U ≤ 2 * Hr ^ (2 ^ Rr)
  1912. · calc U ≤ 2 * Hr ^ (2 ^ Rr) := hsmall
  1913. _ ≤ Hr ^ (2 ^ Rr) * Hr ^ (2 ^ Rr) := by
  1914. have : 2 ≤ Hr ^ (2 ^ Rr) :=
  1915. le_trans hHr2 (Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) Hr)
  1916. exact Nat.mul_le_mul_right _ this
  1917. _ = Hr ^ (2 ^ (Rr + 1)) := by rw [← pow_add, ← two_mul, pow_succ, Nat.mul_comm]
  1918. · exfalso
  1919. have hbig : 2 * Hr ^ (2 ^ Rr) < U := by omega
  1920. have hU4 : 4 ≤ U := by
  1921. have : 2 ≤ Hr ^ (2 ^ Rr) :=
  1922. le_trans hHr2 (Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) Hr)
  1923. omega
  1924. have hl1 := evalFormalT_l1_le P (seg r).a (seg r).b (seg r).hab (seg r).hb x
  1925. (nextC r) (hnextC_ge r) (hnextC_le r) (wTarget r)
  1926. change polyL1At (F r) x ≤ Hr ^ (2 ^ Rr) at hl1
  1927. have hbig_l1 : 2 * polyL1At (F r) x < U := by omega
  1928. have hdom := cauchy_dominance_polyT (F r)
  1929. (P.evalWire ⟨(seg r).b + 1, by have := (seg r).hb; omega⟩) x
  1930. (hF_deg_pos r) (hkill r) hbig_l1
  1931. -- Identify (F r).eval U_r as w_{r+1} (the input to seg r1)!
  1932. have hw_val : (wTarget r).val =
  1933. (P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).val := by
  1934. dsimp only [wTarget]; rw [dite_eq_left_of_eq_true (eq_true hr_lt)]
  1935. have hseg_eq : seg ⟨r.val + 1, hr_lt⟩ = seg r1 := congrArg seg hfin_eq
  1936. have hgate_eq : ∀ {s1 s2 : UntappedSeg P} (h1 : s1.a < τ) (h2 : s2.a < τ), s1 = s2 →
  1937. (P.gateIn1 s1.a h1).val = (P.gateIn1 s2.a h2).val := by rintro s1 _ h1 h2 rfl; rfl
  1938. exact hgate_eq _ _ hseg_eq
  1939. have hnot_inter : ¬ ((seg r).a + 2 ≤ (wTarget r).val ∧ (wTarget r).val ≤ (seg r).b) := by
  1940. rw [hw_val]
  1941. have hbr : (seg r).b ≤ (seg r1).a := by rw [← hnext_eq]; exact hnextC_ge r
  1942. exact ((seg r).huntapped (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega) hbr).1
  1943. have hspec := evalFormalT_spec P (seg r) (nextC r) (hnextC_ge r) (hnextC_le r) (wTarget r) hnot_inter
  1944. rw [hspec] at hdom
  1945. set w1_val := ((P.evalWire ⟨(wTarget r).val, by have := (wTarget r).isLt; have := hnextC_le r; omega⟩).eval x).natAbs
  1946. -- U ≤ 2 * w1_val and 4 ≤ U => U ≤ w1_val ^ 2!
  1947. have hU_le_sq : U ≤ w1_val ^ 2 := nat_half_sq_ge hU4 hdom
  1948. -- Now relate w1_val ^ (2 ^ g_{r+1}) = |U_{r+1}(x)|!
  1949. set g1 := (seg r1).b - (seg r1).a
  1950. have hrun1 := P.topSq_run_eval (seg r1).hab (seg r1).hb (seg r1).hrun
  1951. have hw1_eq : w1_val = ((P.evalWire ⟨(P.gateIn1 (seg r1).a
  1952. (by have := (seg r1).hab; have := (seg r1).hb; omega)).val,
  1953. 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 := by
  1954. exact congrArg (fun k : Fin (τ + 2) => ((P.evalWire k).eval x).natAbs) (Fin.ext hw_val)
  1955. have hU1_eq : ((P.evalWire ⟨(seg r1).b + 1, by have := (seg r1).hb; omega⟩).eval x).natAbs =
  1956. w1_val ^ (2 ^ g1) := by
  1957. rw [hw1_eq]
  1958. have := congrArg (fun p : Polynomial ℤ => (p.eval x).natAbs) hrun1.1
  1959. simpa [Int.natAbs_pow, g1] using this
  1960. -- By ihd, w1_val ^ (2 ^ g1) ≤ (wireHeight P (seg r1).a _ x) ^ (2 ^ (R_{r+1} + 1))
  1961. have hih : P_prop r1 := ihd (by omega)
  1962. dsimp only [P_prop] at hih
  1963. rw [hU1_eq] at hih
  1964. -- By Property 4 (wireHeight_step_le), wireHeight P (seg r1).a _ x ≤ (max Hr U) ^ (2 ^ Rr) = U ^ (2 ^ Rr)!
  1965. have hH1_le := wireHeight_step_le P (seg r) x (nextC r) (hnextC_ge r) (hnextC_le r)
  1966. have hmax_U : max Hr U = U := max_eq_right (by
  1967. have : Hr ≤ Hr ^ (2 ^ Rr) := Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) Hr
  1968. omega)
  1969. rw [hmax_U] at hH1_le
  1970. have hH1_le' : wireHeight P (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega) x ≤ U ^ (2 ^ Rr) := by
  1971. change wireHeight P (nextC r) (hnextC_le r) x ≤ U ^ (2 ^ Rr) at hH1_le
  1972. simpa only [hnext_eq] using hH1_le
  1973. -- Combine: U ^ (2 ^ (g1 - 1)) ≤ (w1_val ^ 2) ^ (2 ^ (g1 - 1)) = w1_val ^ (2 ^ g1) ≤ U ^ (2 ^ (Rr + R_{r+1} + 1))!
  1974. have hg1_ge : Rr + (nextC r1 - (seg r1).b) + 3 ≤ g1 := by
  1975. have hmid := hmid_gap r.val (by omega)
  1976. have hr_seg : L[r.val]'(by omega) = seg r := rfl
  1977. have hr1_seg : L[r.val + 1]'(by omega) = seg r1 := by simp [seg, hr1_val]
  1978. have hRr_eq : Rr = (seg r1).a - (seg r).b := by rw [← hnext_eq]
  1979. have hnc1 : nextC r1 = if h2 : r.val + 2 < L.length then (L[r.val + 2]'h2).a else τ := by
  1980. dsimp only [nextC]
  1981. split_ifs with h1 h2
  1982. · have hidx_eq : r1.val + 1 = r.val + 2 := by omega
  1983. simp only [seg]
  1984. have hget : ∀ {i j : ℕ} (hi : i < L.length) (hj : j < L.length), i = j →
  1985. L[i]'hi = L[j]'hj := by rintro i _ hi hj rfl; rfl
  1986. exact congrArg UntappedSeg.a (hget _ _ hidx_eq)
  1987. · omega
  1988. · omega
  1989. · rfl
  1990. rw [hRr_eq, hnc1]
  1991. simpa [hr_seg, hr1_seg, g1] using hmid
  1992. have hchain_pow : U ^ (2 ^ (g1 - 1)) ≤ U ^ (2 ^ (Rr + (nextC r1 - (seg r1).b) + 1)) := by
  1993. have hleft : U ^ (2 ^ (g1 - 1)) ≤ w1_val ^ (2 ^ g1) := by
  1994. calc U ^ (2 ^ (g1 - 1))
  1995. ≤ (w1_val ^ 2) ^ (2 ^ (g1 - 1)) := Nat.pow_le_pow_left hU_le_sq _
  1996. _ = w1_val ^ (2 * 2 ^ (g1 - 1)) := (pow_mul _ _ _).symm
  1997. _ = w1_val ^ (2 ^ g1) := by
  1998. congr 1
  1999. conv_rhs => rw [← Nat.sub_add_cancel (by omega : 1 ≤ g1), pow_succ, Nat.mul_comm]
  2000. have hright : w1_val ^ (2 ^ g1) ≤ U ^ (2 ^ (Rr + (nextC r1 - (seg r1).b) + 1)) := by
  2001. calc w1_val ^ (2 ^ g1)
  2002. ≤ (wireHeight P (seg r1).a _ x) ^ (2 ^ (nextC r1 - (seg r1).b + 1)) := hih
  2003. _ ≤ (U ^ (2 ^ Rr)) ^ (2 ^ (nextC r1 - (seg r1).b + 1)) :=
  2004. Nat.pow_le_pow_left hH1_le' _
  2005. _ = U ^ (2 ^ Rr * 2 ^ (nextC r1 - (seg r1).b + 1)) := (pow_mul _ _ _).symm
  2006. _ = U ^ (2 ^ (Rr + (nextC r1 - (seg r1).b) + 1)) := by
  2007. rw [← pow_add, ← Nat.add_assoc]
  2008. exact le_trans hleft hright
  2009. have hexp_le : 2 ^ (g1 - 1) ≤ 2 ^ (Rr + (nextC r1 - (seg r1).b) + 1) :=
  2010. (Nat.pow_le_pow_iff_right (by omega : 1 < U)).mp hchain_pow
  2011. have hexp_lt : 2 ^ (Rr + (nextC r1 - (seg r1).b) + 1) < 2 ^ (g1 - 1) :=
  2012. Nat.pow_lt_pow_right (by omega) (by omega)
  2013. omega
  2014. -- Now evaluate hprop at d = L.length - 1 (which gives r = 0, i.e. s0)!
  2015. have hprop0 := hprop (L.length - 1) (by omega)
  2016. have hr0_eq : (⟨L.length - 1 - (L.length - 1), by omega⟩ : Fin L.length) = ⟨0, hq_pos⟩ :=
  2017. Fin.ext (by simp)
  2018. rw [hr0_eq] at hprop0
  2019. dsimp only [P_prop] at hprop0
  2020. -- Extract the head gap condition: 2 * R0 + R1 + 5 ≤ g0
  2021. set R0 := s0.a
  2022. set R1 := nextC ⟨0, hq_pos⟩ - s0.b
  2023. set g0 := s0.b - s0.a
  2024. have hg0_ge : 2 * R0 + R1 + 5 ≤ g0 := by
  2025. cases hL_eq : L with
  2026. | nil => exact absurd hL_eq hL_ne
  2027. | cons a0 rest =>
  2028. have hh := hhead_gap a0 rest hL_eq
  2029. have ha0 : s0 = a0 := by simp [s0, seg, hL_eq]
  2030. cases rest with
  2031. | nil =>
  2032. have hnc : nextC ⟨0, hq_pos⟩ = τ := by
  2033. dsimp only [nextC]; have hnot : ¬ (0 + 1 < L.length) := by simp [hL_eq]
  2034. rw [dite_eq_right_of_eq_false (eq_false hnot)]
  2035. dsimp only [R0, R1, g0]; rw [hnc, ha0]; exact hh
  2036. | cons a1 rest' =>
  2037. have hlt1 : 0 + 1 < L.length := by simp [hL_eq]
  2038. have hnc : nextC ⟨0, hq_pos⟩ = a1.a := by
  2039. dsimp only [nextC]; rw [dite_eq_left_of_eq_true (eq_true hlt1)]
  2040. simp [seg, hL_eq]
  2041. dsimp only [R0, R1, g0]; rw [hnc, ha0]; exact hh
  2042. -- Rewrite |U_0(x)| = |w0(x)| ^ (2 ^ g0)
  2043. have hrun0 := P.topSq_run_eval s0.hab s0.hb s0.hrun
  2044. have hU0_eq : ((P.evalWire ⟨s0.b + 1, by have := s0.hb; omega⟩).eval x).natAbs =
  2045. (w0.eval x).natAbs ^ (2 ^ g0) := by
  2046. have := congrArg (fun p : Polynomial ℤ => (p.eval x).natAbs) hrun0.1
  2047. simpa [Int.natAbs_pow, w0, g0] using this
  2048. rw [hU0_eq] at hprop0
  2049. -- Bound H0 ≤ (max 2 |x|) ^ (2 ^ R0) and polyIntL1 w0 ≤ 2 ^ (2 ^ R0)
  2050. have hH0_le := wireHeight_base_le P s0.a (by have := s0.hab; have := s0.hb; omega) x
  2051. have hl1_w0 : polyIntL1 w0 ≤ 2 ^ (2 ^ R0) := by
  2052. have hpr := P.evalWire_prefix s0.a (by have := s0.hab; have := s0.hb; omega)
  2053. (P.gateIn1 s0.a (by have := s0.hab; have := s0.hb; omega))
  2054. have hw0_eq : w0 = (P.prefix s0.a (by have := s0.hab; have := s0.hb; omega)).evalWire
  2055. (P.gateIn1 s0.a (by have := s0.hab; have := s0.hb; omega)) := hpr.symm
  2056. rw [hw0_eq]
  2057. exact (P.prefix s0.a _).wire_polyIntL1_le _
  2058. -- Deduce (w0.eval x).natAbs ≤ 1!
  2059. have hw0_le1 : (w0.eval x).natAbs ≤ 1 := by
  2060. by_contra hbig_w0
  2061. have hw0_ge2 : 2 ≤ (w0.eval x).natAbs := by omega
  2062. by_cases hx_big : 2 * 2 ^ (2 ^ R0) ≤ x.natAbs
  2063. · -- Case A: |x| ≥ 2 * 2^(2^R0) ≥ 4, so |x| ≤ 2 * |w0(x)| => |x| ≤ |w0(x)|^2!
  2064. have hx4 : 4 ≤ x.natAbs := by
  2065. have : 2 ≤ 2 ^ (2 ^ R0) :=
  2066. Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) 2
  2067. omega
  2068. have hdom := cauchy_dominance_univar w0 x hw0_deg_pos (by omega)
  2069. have hx_sq : x.natAbs ≤ (w0.eval x).natAbs ^ 2 := nat_half_sq_ge hx4 hdom
  2070. have hmax_x : max 2 x.natAbs = x.natAbs := max_eq_right (by omega)
  2071. rw [hmax_x] at hH0_le
  2072. have hchain_x : x.natAbs ^ (2 ^ (g0 - 1)) ≤ x.natAbs ^ (2 ^ (R0 + R1 + 1)) := by
  2073. have h1 : x.natAbs ^ (2 ^ (g0 - 1)) ≤ (w0.eval x).natAbs ^ (2 ^ g0) := by
  2074. calc x.natAbs ^ (2 ^ (g0 - 1))
  2075. ≤ ((w0.eval x).natAbs ^ 2) ^ (2 ^ (g0 - 1)) := Nat.pow_le_pow_left hx_sq _
  2076. _ = (w0.eval x).natAbs ^ (2 * 2 ^ (g0 - 1)) := (pow_mul _ _ _).symm
  2077. _ = (w0.eval x).natAbs ^ (2 ^ g0) := by
  2078. congr 1
  2079. conv_rhs => rw [← Nat.sub_add_cancel (by omega : 1 ≤ g0), pow_succ, Nat.mul_comm]
  2080. have h2 : (w0.eval x).natAbs ^ (2 ^ g0) ≤ x.natAbs ^ (2 ^ (R0 + R1 + 1)) := by
  2081. calc (w0.eval x).natAbs ^ (2 ^ g0)
  2082. ≤ (wireHeight P s0.a _ x) ^ (2 ^ (R1 + 1)) := hprop0
  2083. _ ≤ (x.natAbs ^ (2 ^ R0)) ^ (2 ^ (R1 + 1)) := Nat.pow_le_pow_left hH0_le _
  2084. _ = x.natAbs ^ (2 ^ R0 * 2 ^ (R1 + 1)) := (pow_mul _ _ _).symm
  2085. _ = x.natAbs ^ (2 ^ (R0 + R1 + 1)) := by rw [← pow_add, ← Nat.add_assoc]
  2086. exact le_trans h1 h2
  2087. have hexp_le : 2 ^ (g0 - 1) ≤ 2 ^ (R0 + R1 + 1) :=
  2088. (Nat.pow_le_pow_iff_right (by omega : 1 < x.natAbs)).mp hchain_x
  2089. have hexp_lt : 2 ^ (R0 + R1 + 1) < 2 ^ (g0 - 1) :=
  2090. Nat.pow_lt_pow_right (by omega) (by omega)
  2091. omega
  2092. · -- Case B: |x| < 2 * 2^(2^R0) = 2^(2^R0 + 1), so H0 ≤ 2^(2^(2*R0 + 1))!
  2093. have hmax_le : max 2 x.natAbs ≤ 2 ^ (2 ^ R0 + 1) := by
  2094. have h1 : 2 ≤ 2 ^ (2 ^ R0 + 1) :=
  2095. Nat.le_self_pow (Nat.succ_ne_zero _) 2
  2096. have h2 : 2 * 2 ^ (2 ^ R0) = 2 ^ (2 ^ R0 + 1) := by rw [pow_succ, Nat.mul_comm]
  2097. omega
  2098. have hH0_2 : wireHeight P s0.a (by have := s0.hab; have := s0.hb; omega) x ≤
  2099. 2 ^ (2 ^ (2 * R0 + 1)) := by
  2100. calc wireHeight P s0.a _ x
  2101. ≤ (max 2 x.natAbs) ^ (2 ^ R0) := hH0_le
  2102. _ ≤ (2 ^ (2 ^ R0 + 1)) ^ (2 ^ R0) := Nat.pow_le_pow_left hmax_le _
  2103. _ = 2 ^ ((2 ^ R0 + 1) * 2 ^ R0) := (pow_mul _ _ _).symm
  2104. _ ≤ 2 ^ (2 ^ (2 * R0 + 1)) := by
  2105. apply Nat.pow_le_pow_right (by omega)
  2106. have h1 : 2 ^ R0 + 1 ≤ 2 * 2 ^ R0 := by
  2107. have : 1 ≤ 2 ^ R0 := Nat.one_le_pow _ _ (by omega); omega
  2108. calc (2 ^ R0 + 1) * 2 ^ R0
  2109. ≤ (2 * 2 ^ R0) * 2 ^ R0 := Nat.mul_le_mul_right _ h1
  2110. _ = 2 * (2 ^ R0 * 2 ^ R0) := by ring
  2111. _ = 2 ^ (2 * R0 + 1) := by rw [← pow_add, ← two_mul, pow_succ, Nat.mul_comm]
  2112. have hchain_2 : 2 ^ (2 ^ g0) ≤ 2 ^ (2 ^ (2 * R0 + R1 + 2)) := by
  2113. calc 2 ^ (2 ^ g0)
  2114. ≤ (w0.eval x).natAbs ^ (2 ^ g0) := Nat.pow_le_pow_left hw0_ge2 _
  2115. _ ≤ (wireHeight P s0.a _ x) ^ (2 ^ (R1 + 1)) := hprop0
  2116. _ ≤ (2 ^ (2 ^ (2 * R0 + 1))) ^ (2 ^ (R1 + 1)) := Nat.pow_le_pow_left hH0_2 _
  2117. _ = 2 ^ (2 ^ (2 * R0 + 1) * 2 ^ (R1 + 1)) := (pow_mul _ _ _).symm
  2118. _ = 2 ^ (2 ^ (2 * R0 + R1 + 2)) := by rw [← pow_add]; congr 2; omega
  2119. have hexp_le : 2 ^ g0 ≤ 2 ^ (2 * R0 + R1 + 2) :=
  2120. (Nat.pow_le_pow_iff_right (by omega : 1 < 2)).mp hchain_2
  2121. have hexp_lt : 2 ^ (2 * R0 + R1 + 2) < 2 ^ g0 :=
  2122. Nat.pow_lt_pow_right (by omega) (by omega)
  2123. omega
  2124. -- Since |w0(x)| ≤ 1, w0(x) ∈ {-1, 0, 1}!
  2125. have hw0_ne : w0 ≠ 0 := fun h0 => by simp [h0] at hw0_deg_pos
  2126. have hw0_sub1_ne : w0 - 1 ≠ 0 := fun h0 => by
  2127. have : w0 = 1 := sub_eq_zero.mp h0
  2128. rw [this] at hw0_deg_pos; simp at hw0_deg_pos
  2129. have hw0_add1_ne : w0 + 1 ≠ 0 := fun h0 => by
  2130. have : w0 = -1 := eq_neg_of_add_eq_zero_left h0
  2131. rw [this] at hw0_deg_pos; simp at hw0_deg_pos
  2132. have hcases : w0.eval x = 1 ∨ w0.eval x = 0 ∨ w0.eval x = -1 := by omega
  2133. rcases hcases with h1 | h0 | hm1
  2134. · apply Finset.mem_union_left; apply Finset.mem_union_left; apply Finset.mem_union_right
  2135. rw [intRootSet, Multiset.mem_toFinset, Polynomial.mem_roots hw0_sub1_ne, Polynomial.IsRoot.def]
  2136. simp [h1]
  2137. · apply Finset.mem_union_left; apply Finset.mem_union_right
  2138. rw [intRootSet, Multiset.mem_toFinset, Polynomial.mem_roots hw0_ne, Polynomial.IsRoot.def]
  2139. exact h0
  2140. · apply Finset.mem_union_right
  2141. rw [intRootSet, Multiset.mem_toFinset, Polynomial.mem_roots hw0_add1_ne, Polynomial.IsRoot.def]
  2142. simp [hm1]
  2143. -- Bound the cardinality of the cover!
  2144. have hcard_poly : ∀ q : Polynomial ℤ, intRootCount q ≤ q.natDegree := fun q =>
  2145. le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _)
  2146. have hdeg_sub1 : (w0 - 1).natDegree ≤ 2 ^ (τ - Δ) :=
  2147. le_trans (Polynomial.natDegree_sub_le _ _) (max_le hw0_deg_le (by simp))
  2148. have hdeg_add1 : (w0 + 1).natDegree ≤ 2 ^ (τ - Δ) :=
  2149. le_trans (Polynomial.natDegree_add_le _ _) (max_le hw0_deg_le (by simp))
  2150. calc intRootCount P.evalPoly
  2151. ≤ ((Finset.univ.biUnion (fun r : Fin L.length => intRootSet (F r).leadingCoeff)) ∪
  2152. intRootSet (w0 - 1) ∪ intRootSet w0 ∪ intRootSet (w0 + 1)).card :=
  2153. Finset.card_le_card hsub
  2154. _ ≤ (Finset.univ.biUnion (fun r : Fin L.length => intRootSet (F r).leadingCoeff)).card +
  2155. intRootCount (w0 - 1) + intRootCount w0 + intRootCount (w0 + 1) := by
  2156. have h1 := Finset.card_union_le
  2157. ((Finset.univ.biUnion (fun r : Fin L.length => intRootSet (F r).leadingCoeff)) ∪
  2158. intRootSet (w0 - 1) ∪ intRootSet w0) (intRootSet (w0 + 1))
  2159. have h2 := Finset.card_union_le
  2160. ((Finset.univ.biUnion (fun r : Fin L.length => intRootSet (F r).leadingCoeff)) ∪
  2161. intRootSet (w0 - 1)) (intRootSet w0)
  2162. have h3 := Finset.card_union_le
  2163. (Finset.univ.biUnion (fun r : Fin L.length => intRootSet (F r).leadingCoeff))
  2164. (intRootSet (w0 - 1))
  2165. simp only [intRootCount] at *
  2166. omega
  2167. _ ≤ (∑ r : Fin L.length, intRootCount (F r).leadingCoeff) +
  2168. 2 ^ (τ - Δ) + 2 ^ (τ - Δ) + 2 ^ (τ - Δ) := by
  2169. have hbi : (Finset.univ.biUnion (fun r : Fin L.length => intRootSet (F r).leadingCoeff)).card ≤
  2170. ∑ r : Fin L.length, intRootCount (F r).leadingCoeff := Finset.card_biUnion_le
  2171. have h1 := le_trans (hcard_poly (w0 - 1)) hdeg_sub1
  2172. have h2 := le_trans (hcard_poly w0) hw0_deg_le
  2173. have h3 := le_trans (hcard_poly (w0 + 1)) hdeg_add1
  2174. omega
  2175. _ ≤ (∑ _r : Fin L.length, 2 ^ (τ - Δ)) + 3 * 2 ^ (τ - Δ) := by
  2176. have hsum := Finset.sum_le_sum (s := (Finset.univ : Finset (Fin L.length)))
  2177. (fun r _ => le_trans (hcard_poly (F r).leadingCoeff) (hlead_deg r))
  2178. omega
  2179. _ = (L.length + 3) * 2 ^ (τ - Δ) := by
  2180. simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, smul_eq_mul]
  2181. ring
  2182. /-! ## 5. Main Unconditional `o(2^τ)` Theorem for All Constant-Free SLPs -/
  2183. private lemma three_pow_le_two_pow (K : ℕ) : 3 ^ (3 * K + 1) ≤ 2 ^ (5 * K + 2) := by
  2184. have h1 : (3 ^ 3) ^ K ≤ (2 ^ 5) ^ K := Nat.pow_le_pow_left (by decide) K
  2185. rw [← pow_mul, ← pow_mul] at h1
  2186. calc 3 ^ (3 * K + 1) = 3 ^ (3 * K) * 3 := pow_succ _ _
  2187. _ ≤ 2 ^ (5 * K) * 4 := Nat.mul_le_mul h1 (by decide)
  2188. _ = 2 ^ (5 * K + 2) := by rw [pow_add]; norm_num
  2189. private lemma two_pow_quarter_ge_double : ∀ m : ℕ, 40 ≤ m → 2 * m ≤ 2 ^ (m / 4) := by
  2190. intro m hm
  2191. have hbase : ∀ q : ℕ, 10 ≤ q → 8 * q + 6 ≤ 2 ^ q := by
  2192. intro q hq
  2193. induction q, hq using Nat.le_induction with
  2194. | base => decide
  2195. | succ q' hq' ih =>
  2196. rw [pow_succ]; omega
  2197. have hq : 10 ≤ m / 4 := by omega
  2198. have h1 := hbase (m / 4) hq
  2199. omega
  2200. /-- **Unconditional Elementary `o(2^τ)` Theorem for Smale's 4th Problem:**
  2201. For EVERY `τ : ℕ` and EVERY constant-free straight-line program `P : SLP τ` with `P.evalPoly ≠ 0`:
  2202. `τ * (intRootCount P.evalPoly) ^ 28 ≤ 2 ^ 40 * 2 ^ (28 * τ)`.
  2203. Equivalently, `intRootCount P.evalPoly ≤ 2^(10/7) * 2^τ / τ^(1/28) = o(2^τ)` unconditionally! -/
  2204. theorem slp_intRootCount_o_two_pow {τ : ℕ} (P : SLP τ) (hP : P.evalPoly ≠ 0) :
  2205. τ * (intRootCount P.evalPoly) ^ 28 ≤ 2 ^ 40 * 2 ^ (28 * τ) := by
  2206. have htriv : intRootCount P.evalPoly ≤ 2 ^ τ := by
  2207. calc intRootCount P.evalPoly
  2208. ≤ P.evalPoly.natDegree := le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _)
  2209. _ ≤ P.maxDeg := P.wireDeg_le_maxDeg ⟨τ + 1, Nat.lt_succ_self _⟩
  2210. _ ≤ 2 ^ τ := P.maxDeg_le_two_pow
  2211. by_cases hsmall : τ < 2 ^ 40
  2212. · calc τ * (intRootCount P.evalPoly) ^ 28
  2213. ≤ 2 ^ 40 * (2 ^ τ) ^ 28 := Nat.mul_le_mul (le_of_lt hsmall) (Nat.pow_le_pow_left htriv 28)
  2214. _ = 2 ^ 40 * 2 ^ (28 * τ) := by rw [← pow_mul, Nat.mul_comm τ 28]
  2215. · have hτ_big : 2 ^ 40 ≤ τ := by omega
  2216. set m := Nat.log 2 τ
  2217. have hm40 : 40 ≤ m := (Nat.le_log_iff_pow_le (by omega) (by omega)).mpr hτ_big
  2218. have h2m_le : 2 ^ m ≤ τ := Nat.pow_log_le_self 2 (by omega)
  2219. have hτ_lt : τ < 2 ^ (m + 1) := Nat.lt_pow_succ_log_self (by omega) τ
  2220. set K := m / 8
  2221. by_cases hk_high : K ≤ P.nonTopSqCount
  2222. · -- Case 1: High non-top-squaring count -> Pure-Squaring Collapse wins!
  2223. have hdeg7 : (P.evalPoly.natDegree) ^ 7 ≤ 2 ^ (7 * τ - 2 * P.nonTopSqCount) :=
  2224. slp_pure_squaring_collapse_deg P
  2225. have hz_le_deg : intRootCount P.evalPoly ≤ P.evalPoly.natDegree :=
  2226. le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _)
  2227. have hz7 : (intRootCount P.evalPoly) ^ 7 ≤ 2 ^ (7 * τ - 2 * K) :=
  2228. le_trans (Nat.pow_le_pow_left hz_le_deg 7)
  2229. (le_trans hdeg7 (Nat.pow_le_pow_right (by omega) (by omega)))
  2230. have hz28 : (intRootCount P.evalPoly) ^ 28 ≤ 2 ^ (28 * τ - 8 * K) := by
  2231. have h1 : ((intRootCount P.evalPoly) ^ 7) ^ 4 ≤ (2 ^ (7 * τ - 2 * K)) ^ 4 :=
  2232. Nat.pow_le_pow_left hz7 4
  2233. rw [← pow_mul, ← pow_mul] at h1
  2234. have hK_le : 2 * K ≤ 7 * τ := by
  2235. have : m ≤ 2 ^ m := Nat.le_of_lt Nat.lt_two_pow_self
  2236. omega
  2237. have hexp : (7 * τ - 2 * K) * 4 = 28 * τ - 8 * K := by omega
  2238. rwa [hexp] at h1
  2239. have hm_le_8K : m + 1 ≤ 8 * K + 8 := by omega
  2240. have hτ_le : τ ≤ 2 ^ (8 * K + 8) :=
  2241. le_trans (le_of_lt hτ_lt) (Nat.pow_le_pow_right (by omega) hm_le_8K)
  2242. have h8K_le : 8 * K ≤ 28 * τ := by
  2243. have : m ≤ 2 ^ m := Nat.le_of_lt Nat.lt_two_pow_self
  2244. omega
  2245. calc τ * (intRootCount P.evalPoly) ^ 28
  2246. ≤ 2 ^ (8 * K + 8) * 2 ^ (28 * τ - 8 * K) := Nat.mul_le_mul hτ_le hz28
  2247. _ = 2 ^ ((8 * K + 8) + (28 * τ - 8 * K)) := (pow_add 2 _ _).symm
  2248. _ = 2 ^ (8 + 28 * τ) := by congr 1; omega
  2249. _ = 2 ^ 8 * 2 ^ (28 * τ) := pow_add 2 8 (28 * τ)
  2250. _ ≤ 2 ^ 40 * 2 ^ (28 * τ) := Nat.mul_le_mul_right _ (by decide)
  2251. · -- Case 2: Low non-top-squaring count (k < K) -> Multi-Segment Gap-Dominance wins!
  2252. have hk_lt : P.nonTopSqCount < K := by omega
  2253. obtain ⟨L0, hord0, hlen0, htot0⟩ := extract_untapped_segments P
  2254. set Δ := 2 ^ (m / 4)
  2255. have h2m4 : 2 * m ≤ Δ := two_pow_quarter_ge_double m hm40
  2256. have hΔ_le_τ : Δ ≤ τ :=
  2257. le_trans (Nat.pow_le_pow_right (by omega) (by omega : m / 4 ≤ m)) h2m_le
  2258. -- Show the demotion failure branch is impossible!
  2259. have hdem_strict : 3 ^ L0.length * (τ - segsTotalLen L0 + Δ + 2) < τ + Δ + 2 := by
  2260. have hR0 : τ - segsTotalLen L0 = P.nonTopSqCount := by
  2261. have := P.nonTopSqCount_le
  2262. omega
  2263. rw [hR0]
  2264. have hL0_le : L0.length ≤ 3 * K + 1 := by omega
  2265. have h3pow : 3 ^ L0.length ≤ 2 ^ (5 * K + 2) :=
  2266. le_trans (Nat.pow_le_pow_right (by omega) hL0_le) (three_pow_le_two_pow K)
  2267. have hfac : P.nonTopSqCount + Δ + 2 ≤ 2 * Δ := by omega
  2268. have hmul_le : 3 ^ L0.length * (P.nonTopSqCount + Δ + 2) ≤ 2 ^ (5 * K + 2 + (m / 4 + 1)) := by
  2269. calc 3 ^ L0.length * (P.nonTopSqCount + Δ + 2)
  2270. ≤ 2 ^ (5 * K + 2) * (2 * Δ) := Nat.mul_le_mul h3pow hfac
  2271. _ = 2 ^ (5 * K + 2) * 2 ^ (m / 4 + 1) := by congr 1; rw [pow_succ, Nat.mul_comm]
  2272. _ = 2 ^ (5 * K + 2 + (m / 4 + 1)) := (pow_add 2 _ _).symm
  2273. have hexp_lt : 5 * K + 2 + (m / 4 + 1) < m := by omega
  2274. have hpow_lt : 2 ^ (5 * K + 2 + (m / 4 + 1)) < 2 ^ m := Nat.pow_lt_pow_right (by omega) hexp_lt
  2275. omega
  2276. rcases demotion_exists_valid_gaps Δ L0.length L0 rfl hord0 with ⟨L', hv', hlen'⟩ | hfail
  2277. · have hz_gap := slp_multi_segment_gap_dominance P hP L' hv'
  2278. have hL'_le : L'.length + 3 ≤ 2 ^ m := by
  2279. have : m ≤ 2 ^ m := Nat.le_of_lt Nat.lt_two_pow_self
  2280. have : L'.length + 3 ≤ 3 * K + 4 := by omega
  2281. have : 3 * K + 4 ≤ m := by omega
  2282. omega
  2283. have hz_le : intRootCount P.evalPoly ≤ 2 ^ m * 2 ^ (τ - Δ) :=
  2284. le_trans hz_gap (Nat.mul_le_mul_right _ hL'_le)
  2285. have hz28 : (intRootCount P.evalPoly) ^ 28 ≤ 2 ^ (28 * m) * 2 ^ (28 * (τ - Δ)) := by
  2286. calc (intRootCount P.evalPoly) ^ 28
  2287. ≤ (2 ^ m * 2 ^ (τ - Δ)) ^ 28 := Nat.pow_le_pow_left hz_le 28
  2288. _ = (2 ^ m) ^ 28 * (2 ^ (τ - Δ)) ^ 28 := mul_pow _ _ _
  2289. _ = 2 ^ (28 * m) * 2 ^ (28 * (τ - Δ)) := by
  2290. rw [← pow_mul, ← pow_mul, Nat.mul_comm m 28, Nat.mul_comm (τ - Δ) 28]
  2291. have hexp_ok : m + 1 + 28 * m + 28 * (τ - Δ) ≤ 28 * τ := by omega
  2292. calc τ * (intRootCount P.evalPoly) ^ 28
  2293. ≤ 2 ^ (m + 1) * (2 ^ (28 * m) * 2 ^ (28 * (τ - Δ))) :=
  2294. Nat.mul_le_mul (le_of_lt hτ_lt) hz28
  2295. _ = 2 ^ (m + 1 + 28 * m + 28 * (τ - Δ)) := by
  2296. rw [← pow_add, ← pow_add]; congr 1; omega
  2297. _ ≤ 2 ^ (28 * τ) := Nat.pow_le_pow_right (by omega) hexp_ok
  2298. _ = 1 * 2 ^ (28 * τ) := (Nat.one_mul _).symm
  2299. _ ≤ 2 ^ 40 * 2 ^ (28 * τ) := Nat.mul_le_mul_right _ (by decide)
  2300. · exfalso; omega
  2301. end Research.PvsNP.Tau