Lean / 0xOsprey/shub-smale-tau9 / GapDominanceBound.lean
Lean readerResearch/PvsNP/Tau/GapDominanceBound.lean
modulepublic import Research.PvsNP.Tau.PureSquaringCollapsepublic import Mathlib.Data.Nat.Log@[expose] public sectionnamespace Research.PvsNP.Tau/-!# Unconditional Elementary `o(2^τ)` Upper Bound for All Constant-Free SLPs over `ℤ`This module proves an **unconditional, completely elementary, pure-`ℤ` sub-trivial upper bound**on the number of distinct integer roots of **every** nonzero polynomial computed by aconstant-free straight-line program `P : SLP τ` over `{+, -, ×}` from `{1, X}`:`slp_intRootCount_o_two_pow`:For every `τ : ℕ` and every `P : SLP τ` with `P.evalPoly ≠ 0`,`τ * (intRootCount P.evalPoly) ^ 28 ≤ 2 ^ 40 * 2 ^ (28 * τ)`(equivalently, `z_ℤ(P) ≤ 2^(10/7) · 2^τ / τ^(1/28) = o(2^τ)`).The proof uses **zero** real analysis, **zero** Khovanskii/Pfaffian theory, and **zero**`p`-adic analysis. It combines:1. **`slp_pure_squaring_collapse_deg`** (from `PureSquaringCollapse.lean`): when the number ofnon-(top-degree pure-squaring) gates `k = P.nonTopSqCount` satisfies `k ≥ K`, we immediatelyhave `(intRootCount P.evalPoly) ^ 7 ≤ 2 ^ (7 * τ - 2 * K)`.2. **Multi-Segment Gap-Dominance Theorem (`slp_multi_segment_gap_dominance`):** when `k < K`,the `τ - k` top-degree pure-squaring gates partition into at most `3 * k + 1` untappedunbranched pure-squaring segments `[a_s, b_s)`. A pure-combinatorial demotion lemma extractsa nonempty subset of surviving segments satisfying local gap inequalities`g_0 ≥ 2 * R_0 + R_1 + 5`, `g_r ≥ R_{r-1} + R_r + 3`, and `g_r ≥ Δ`.Treating each surviving segment's output `U_r = w_r ^ (2 ^ g_r)` as a formal indeterminate`T` over `ℤ[X]` and propagating Cauchy's root bound backward across the segments forces everyinteger root `x` of `P.evalPoly = 0` either to kill some leading coefficient `c_r(x) = 0`(of degree `≤ 2 ^ (τ - g_r)`) or to satisfy `w_0(x) ∈ {-1, 0, 1}` (at most `3 * 2 ^ R_0`roots), yielding `intRootCount P.evalPoly ≤ (q + 3) * 2 ^ (τ - Δ)`.-/open Polynomial/-! ## 1. Prefix Extraction, Gate Accessors, and Unbranched Top-Squaring Chains -//-- Prefix of `P : SLP τ` consisting of its first `t` gates (`t ≤ τ`). -/def SLP.prefix : {τ : ℕ} → SLP τ → (t : ℕ) → t ≤ τ → SLP t| 0, .nil, 0, _ => .nil| _ + 1, .snoc P op i j, t, ht =>if h : t ≤ _ thenP.prefix t helsehave heq : t = _ + 1 := by omegaheq ▸ SLP.snoc P op i j/-- `P.prefix τ le_rfl = P`. -/lemma SLP.prefix_self : ∀ {τ : ℕ} (P : SLP τ), P.prefix τ le_rfl = P| 0, .nil => rfl| τ + 1, .snoc P op i j => byhave hnot : ¬ (τ + 1 ≤ τ) := by omegasimp [SLP.prefix, hnot]/-- `(SLP.snoc P op i j).prefix t ht = P.prefix t h` whenever `t ≤ τ`. -/lemma SLP.prefix_snoc_le {τ : ℕ} (P : SLP τ) (op : SLPOp) (i j : Fin (τ + 2)){t : ℕ} (ht : t ≤ τ) :(SLP.snoc P op i j).prefix t (Nat.le_succ_of_le ht) = P.prefix t ht := bysimp [SLP.prefix, ht]/-- `P.prefix` agrees with `P` on `evalWire` for all wires `< t + 2`. -/lemma SLP.evalWire_prefix :∀ {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t ≤ τ) (w : Fin (t + 2)),(P.prefix t ht).evalWire w = P.evalWire ⟨w.val, by omega⟩| 0, .nil, 0, _, w => rfl| τ + 1, .snoc P op i j, t, ht, w => byby_cases h : t ≤ τ· rw [SLP.prefix_snoc_le P op i j h]have hlt : w.val < τ + 2 := by omegasimp only [SLP.evalWire, hlt, ↓reduceDIte]exact P.evalWire_prefix t h w· have heq : t = τ + 1 := by omegasubst heqrw [SLP.prefix_self]/-- Gate operation at 0-based gate index `t < τ`. -/def SLP.gateOp : {τ : ℕ} → SLP τ → (t : ℕ) → t < τ → SLPOp| _ + 1, .snoc P op _ _, t, _ =>if h : t < _ then P.gateOp t h else op/-- First input wire index (`Fin (t + 2)`) of gate `t < τ`. -/def SLP.gateIn1 : {τ : ℕ} → SLP τ → (t : ℕ) → t < τ → Fin (t + 2)| _ + 1, .snoc P _ i _, t, ht =>if h : t < _ then P.gateIn1 t helse ⟨i.val, by have := i.isLt; omega⟩/-- Second input wire index (`Fin (t + 2)`) of gate `t < τ`. -/def SLP.gateIn2 : {τ : ℕ} → SLP τ → (t : ℕ) → t < τ → Fin (t + 2)| _ + 1, .snoc P _ _ j, t, ht =>if h : t < _ then P.gateIn2 t helse ⟨j.val, by have := j.isLt; omega⟩/-- `P.prefix (t + 1) ht` equals `SLP.snoc (P.prefix t _) (P.gateOp t _) (P.gateIn1 t _) (P.gateIn2 t _)`. -/lemma SLP.prefix_succ_eq :∀ {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t < τ),P.prefix (t + 1) ht =SLP.snoc (P.prefix t (le_of_lt ht)) (P.gateOp t ht) (P.gateIn1 t ht) (P.gateIn2 t ht)| τ + 1, .snoc P op i j, t, ht => byby_cases h : t < τ· rw [SLP.prefix_snoc_le P op i j h, SLP.prefix_snoc_le P op i j (le_of_lt h)]simp only [SLP.gateOp, SLP.gateIn1, SLP.gateIn2, h, ↓reduceDIte]exact P.prefix_succ_eq t h· have heq : t = τ := by omegasubst heqrw [SLP.prefix_self, SLP.prefix_snoc_le P op i j le_rfl, SLP.prefix_self]simp [SLP.gateOp, SLP.gateIn1, SLP.gateIn2]/-- Wire `t + 2` of `P : SLP τ` (`t < τ`) is computed by applying `gateOp t` to `gateIn1 t` and `gateIn2 t`. -/lemma SLP.evalWire_gate {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t < τ) :P.evalWire ⟨t + 2, by omega⟩ =(P.gateOp t ht).applyPoly(P.evalWire ⟨(P.gateIn1 t ht).val, by have := (P.gateIn1 t ht).isLt; omega⟩)(P.evalWire ⟨(P.gateIn2 t ht).val, by have := (P.gateIn2 t ht).isLt; omega⟩) := byhave h1 := (P.evalWire_prefix (t + 1) ht ⟨t + 2, by omega⟩).symmrw [h1, P.prefix_succ_eq t ht]have hnot : ¬ (t + 2 < t + 2) := by omegasimp only [SLP.evalWire, hnot, ↓reduceDIte]rw [P.evalWire_prefix t (le_of_lt ht) (P.gateIn1 t ht),P.evalWire_prefix t (le_of_lt ht) (P.gateIn2 t ht)]/-- Predicate: gate `t < τ` of `P` is a top-degree pure squaring. -/def SLP.gateIsTopSq {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t < τ) : Prop :=isTopPureSquare (P.prefix t (le_of_lt ht)) (P.gateOp t ht) (P.gateIn1 t ht) (P.gateIn2 t ht)lemma SLP.gateIsTopSq_snoc_lt {τ : ℕ} (P : SLP τ) (op : SLPOp) (i j : Fin (τ + 2)){t : ℕ} (ht : t < τ) :(SLP.snoc P op i j).gateIsTopSq t (Nat.lt_succ_of_lt ht) ↔ P.gateIsTopSq t ht := byunfold SLP.gateIsTopSqrw [SLP.prefix_snoc_le P op i j (le_of_lt ht)]simp [SLP.gateOp, SLP.gateIn1, SLP.gateIn2, ht]lemma SLP.gateIsTopSq_snoc_last {τ : ℕ} (P : SLP τ) (op : SLPOp) (i j : Fin (τ + 2)) :(SLP.snoc P op i j).gateIsTopSq τ (Nat.lt_succ_self τ) ↔ isTopPureSquare P op i j := byunfold SLP.gateIsTopSqrw [SLP.prefix_snoc_le P op i j le_rfl, SLP.prefix_self]simp [SLP.gateOp, SLP.gateIn1, SLP.gateIn2]/-- `P.nonTopSqCount` equals the number of gates `t : Fin τ` that are not top-degree pure squarings. -/lemma SLP.nonTopSqCount_eq_card :∀ {τ : ℕ} (P : SLP τ) [DecidablePred (fun t : Fin τ => ¬ P.gateIsTopSq t.val t.isLt)],P.nonTopSqCount =(Finset.univ.filter (fun t : Fin τ => ¬ P.gateIsTopSq t.val t.isLt)).card| 0, .nil, _ => by simp [SLP.nonTopSqCount]| τ + 1, .snoc P op i j, _ => byclassicalhave ih := P.nonTopSqCount_eq_cardlet S_all : Finset (Fin (τ + 1)) :=Finset.univ.filter (fun t : Fin (τ + 1) => ¬ (SLP.snoc P op i j).gateIsTopSq t.val t.isLt)let S_prev : Finset (Fin τ) :=Finset.univ.filter (fun t : Fin τ => ¬ P.gateIsTopSq t.val t.isLt)let f : Fin τ ↪ Fin (τ + 1) :=⟨fun t => ⟨t.val, Nat.lt_succ_of_lt t.isLt⟩, fun a b h => Fin.ext (by simpa using congrArg Fin.val h)⟩let last : Fin (τ + 1) := ⟨τ, Nat.lt_succ_self τ⟩have hlast_not : last ∉ S_prev.map f := byintro hmrcases Finset.mem_map.mp hm with ⟨t, _, ht⟩have hval : t.val = τ := congrArg Fin.val hthave := t.isLtomegahave hsplit : S_all =if isTopPureSquare P op i j then S_prev.map f else insert last (S_prev.map f) := byext tby_cases hlt : t.val < τ· have hne : t ≠ last := fun h => by have := congrArg Fin.val h; dsimp [last] at this; omegahave hmem_map : t ∈ S_prev.map f ↔ ¬ P.gateIsTopSq t.val hlt := byconstructor· intro hmrcases Finset.mem_map.mp hm with ⟨u, hu, hut⟩have hu_eq : u.val = t.val := congrArg Fin.val huthave hu' : ¬ P.gateIsTopSq u.val u.isLt := (Finset.mem_filter.mp hu).2simpa [hu_eq] using hu'· intro hnotexact Finset.mem_map.mpr ⟨⟨t.val, hlt⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, hnot⟩, Fin.ext rfl⟩split_ifs with htop· simp only [S_all, Finset.mem_filter, Finset.mem_univ, true_and, hmem_map]exact not_congr (SLP.gateIsTopSq_snoc_lt P op i j hlt)· simp only [S_all, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert, hne,false_or, hmem_map]exact not_congr (SLP.gateIsTopSq_snoc_lt P op i j hlt)· have ht_eq : t = last := Fin.ext (by dsimp [last]; omega)subst ht_eqhave htop_iff : (SLP.snoc P op i j).gateIsTopSq τ (Nat.lt_succ_self τ) ↔isTopPureSquare P op i j := SLP.gateIsTopSq_snoc_last P op i jsplit_ifs with htop· simp [S_all, last, htop_iff, htop, hlast_not]· simp [S_all, last, htop_iff, htop]have hcard_all : S_all.card = P.nonTopSqCount + if isTopPureSquare P op i j then 0 else 1 := byrw [hsplit, ih]split_ifs· simp [S_prev]· rw [Finset.card_insert_of_notMem hlast_not, Finset.card_map]have hcnt : (SLP.snoc P op i j).nonTopSqCount =P.nonTopSqCount + if isTopPureSquare P op i j then 0 else 1 := byby_cases h : isTopPureSquare P op i j <;> simp [SLP.nonTopSqCount, h]rw [hcnt, ← hcard_all]/-- Maximum wire degree `maxDeg` is at least `1` and at most `2 ^ τ`, and bounds all wire degrees. -/lemma SLP.one_le_maxDeg {τ : ℕ} (P : SLP τ) : 1 ≤ P.maxDeg := (P.topTwoDegs_inv).1lemma SLP.wireDeg_le_maxDeg {τ : ℕ} (P : SLP τ) (w : Fin (τ + 2)) :(P.evalWire w).natDegree ≤ P.maxDeg :=(P.topTwoDegs_inv).2.2.1 wlemma SLP.maxDeg_le_two_pow : ∀ {τ : ℕ} (P : SLP τ), P.maxDeg ≤ 2 ^ τ| 0, .nil => by simp [SLP.maxDeg, SLP.topTwoDegs]| τ + 1, .snoc P op i j => byhave ih := P.maxDeg_le_two_powhave h1 : (P.evalWire i).natDegree ≤ P.maxDeg := P.wireDeg_le_maxDeg ihave h2 : (P.evalWire j).natDegree ≤ P.maxDeg := P.wireDeg_le_maxDeg jhave hd : (op.applyPoly (P.evalWire i) (P.evalWire j)).natDegree ≤ 2 * P.maxDeg := bycases op· exact le_trans (Polynomial.natDegree_add_le _ _) (by omega)· exact le_trans (Polynomial.natDegree_sub_le _ _) (by omega)· exact le_trans Polynomial.natDegree_mul_le (by omega)have hM : (SLP.snoc P op i j).maxDeg =max P.maxDeg (op.applyPoly (P.evalWire i) (P.evalWire j)).natDegree := bychange (if P.maxDeg ≤ (op.applyPoly (P.evalWire i) (P.evalWire j)).natDegreethen ((op.applyPoly (P.evalWire i) (P.evalWire j)).natDegree, P.maxDeg)else (P.maxDeg, max P.secondDeg (op.applyPoly (P.evalWire i) (P.evalWire j)).natDegree)).1 = _split_ifs with h <;> omegarw [hM, pow_succ]omega/-- Monotonicity of `maxDeg` along prefixes. -/lemma SLP.maxDeg_prefix_mono {τ : ℕ} (P : SLP τ) {t1 t2 : ℕ} (h12 : t1 ≤ t2) (ht2 : t2 ≤ τ) :(P.prefix t1 (le_trans h12 ht2)).maxDeg ≤ (P.prefix t2 ht2).maxDeg := byobtain ⟨w, hw⟩ := (P.prefix t1 (le_trans h12 ht2)).exists_wire_eq_maxDeghave hle := (P.prefix t2 ht2).wireDeg_le_maxDeg ⟨w.val, by omega⟩have heq : (P.prefix t2 ht2).evalWire ⟨w.val, by omega⟩ =(P.prefix t1 (le_trans h12 ht2)).evalWire w := byrw [P.evalWire_prefix t2 ht2 ⟨w.val, by omega⟩, P.evalWire_prefix t1 (le_trans h12 ht2) w]rw [heq] at hlerw [← hw]exact hle/-- If gate `t` is a top-degree pure squaring, then wire `t + 2` has degree `2 * D₁(t)`,`D₁(t + 1) = 2 * D₁(t)`, and EVERY wire `< t + 2` has degree strictly less than `D₁(t + 1)`. -/lemma SLP.topSq_strict_maxDeg {τ : ℕ} (P : SLP τ) {t : ℕ} (ht : t < τ)(htop : P.gateIsTopSq t ht) :(P.evalWire ⟨t + 2, by omega⟩).natDegree = 2 * (P.prefix t (le_of_lt ht)).maxDeg ∧(P.prefix (t + 1) ht).maxDeg = 2 * (P.prefix t (le_of_lt ht)).maxDeg ∧(∀ w : Fin (t + 2), (P.evalWire ⟨w.val, by omega⟩).natDegree < (P.prefix (t + 1) ht).maxDeg) := byrcases htop with ⟨hop, hij, hdeg⟩set P0 := P.prefix t (le_of_lt ht)set wi := P0.evalWire (P.gateIn1 t ht)have hD1_pos : 1 ≤ P0.maxDeg := P0.one_le_maxDeghave hwi_ne : wi ≠ 0 := byintro h0have : wi.natDegree = 0 := by simp [h0]have : P0.maxDeg = 0 := by rw [← hdeg]; exact thisomegahave hwire_eq : P.evalWire ⟨t + 2, by omega⟩ = wi * wi := byhave h1 : P.evalWire ⟨(P.gateIn1 t ht).val, by have := (P.gateIn1 t ht).isLt; omega⟩ = wi :=(P.evalWire_prefix t (le_of_lt ht) (P.gateIn1 t ht)).symmhave h2 : P.evalWire ⟨(P.gateIn2 t ht).val, by have := (P.gateIn2 t ht).isLt; omega⟩ = wi := byrw [← P.evalWire_prefix t (le_of_lt ht) (P.gateIn2 t ht), ← hij]rw [P.evalWire_gate t ht, hop]simp only [SLPOp.applyPoly, h1, h2]have hsq_deg : (P.evalWire ⟨t + 2, by omega⟩).natDegree = 2 * P0.maxDeg := byrw [hwire_eq, Polynomial.natDegree_mul hwi_ne hwi_ne]have : wi.natDegree = P0.maxDeg := hdegomegahave hM1 : (P.prefix (t + 1) ht).maxDeg = 2 * P0.maxDeg := byrw [P.prefix_succ_eq t ht]have hd_eq : ((P.gateOp t ht).applyPoly (P0.evalWire (P.gateIn1 t ht))(P0.evalWire (P.gateIn2 t ht))).natDegree = 2 * P0.maxDeg := byrw [hop, ← hij]simp only [SLPOp.applyPoly]change (wi * wi).natDegree = 2 * P0.maxDegrw [Polynomial.natDegree_mul hwi_ne hwi_ne]have : wi.natDegree = P0.maxDeg := hdegomegahave hle : P0.maxDeg ≤ 2 * P0.maxDeg := by omegachange (if P0.maxDeg ≤ ((P.gateOp t ht).applyPoly (P0.evalWire (P.gateIn1 t ht))(P0.evalWire (P.gateIn2 t ht))).natDegreethen (((P.gateOp t ht).applyPoly (P0.evalWire (P.gateIn1 t ht))(P0.evalWire (P.gateIn2 t ht))).natDegree, P0.maxDeg)else _).1 = 2 * P0.maxDegrw [hd_eq]; simp [hle]refine ⟨hsq_deg, hM1, fun w => ?_⟩rw [hM1, ← P.evalWire_prefix t (le_of_lt ht) w]have hle_w := P0.wireDeg_le_maxDeg wchange (P0.evalWire w).natDegree < 2 * P0.maxDegomega/-- **Unbranched Chain Lemma:** If both gates `t` and `t + 1` are top-degree pure squarings,then gate `t + 1` MUST square wire `t + 2` (the output of gate `t`). -/lemma SLP.topSq_next_input_eq {τ : ℕ} (P : SLP τ) {t : ℕ} (ht1 : t + 1 < τ)(htop0 : P.gateIsTopSq t (by omega)) (htop1 : P.gateIsTopSq (t + 1) ht1) :(P.gateIn1 (t + 1) ht1).val = t + 2 ∧ (P.gateIn2 (t + 1) ht1).val = t + 2 := byhave hstrict := (P.topSq_strict_maxDeg (by omega) htop0).2.2rcases htop1 with ⟨_, hij, hdeg1⟩have hin_eq : (P.gateIn1 (t + 1) ht1).val = t + 2 := byby_contra hnehave hlt : (P.gateIn1 (t + 1) ht1).val < t + 2 := byhave := (P.gateIn1 (t + 1) ht1).isLt; omegahave hlt_deg := hstrict ⟨(P.gateIn1 (t + 1) ht1).val, hlt⟩dsimp only at hlt_deghave heq_w : (P.prefix (t + 1) (le_of_lt ht1)).evalWire (P.gateIn1 (t + 1) ht1) =P.evalWire ⟨(P.gateIn1 (t + 1) ht1).val, by omega⟩ :=P.evalWire_prefix (t + 1) (le_of_lt ht1) (P.gateIn1 (t + 1) ht1)have : (P.evalWire ⟨(P.gateIn1 (t + 1) ht1).val, by omega⟩).natDegree =(P.prefix (t + 1) (le_of_lt ht1)).maxDeg := byrw [← heq_w]; exact hdeg1omegaexact ⟨hin_eq, by rw [← hij]; exact hin_eq⟩/-- **Contiguous Top-Squaring Run Evaluation:** If every gate `t ∈ [a, b)` (`a < b ≤ τ`) is atop-degree pure squaring, then wire `b + 1` equals `(P.evalWire w_in) ^ (2 ^ (b - a))`where `w_in = P.gateIn1 a _`, and its degree is `2 ^ (b - a) * D₁(a)`. -/lemma SLP.topSq_run_eval {τ : ℕ} (P : SLP τ) {a b : ℕ} (hab : a < b) (hb : b ≤ τ)(hrun : ∀ t (ht : t < τ), a ≤ t → t < b → P.gateIsTopSq t ht) :P.evalWire ⟨b + 1, by omega⟩ =(P.evalWire ⟨(P.gateIn1 a (by omega)).val, by have := (P.gateIn1 a (by omega)).isLt; omega⟩) ^(2 ^ (b - a)) ∧(P.evalWire ⟨b + 1, by omega⟩).natDegree =2 ^ (b - a) * (P.prefix a (by omega)).maxDeg := byinduction b, hab using Nat.le_induction with| base =>have htop := hrun a (by omega) le_rfl (by omega)have hsq := P.topSq_strict_maxDeg (by omega) htoprcases htop with ⟨hop, hij, _⟩have heq : P.evalWire ⟨a + 1 + 1, by omega⟩ =(P.evalWire ⟨(P.gateIn1 a (by omega)).val, by have := (P.gateIn1 a (by omega)).isLt; omega⟩) ^(2 ^ (a + 1 - a)) := byhave hgate := P.evalWire_gate a (by omega)have h2eq : P.evalWire ⟨(P.gateIn2 a (by omega)).val, by have := (P.gateIn2 a (by omega)).isLt; omega⟩ =P.evalWire ⟨(P.gateIn1 a (by omega)).val, by have := (P.gateIn1 a (by omega)).isLt; omega⟩ := byhave hval : (P.gateIn2 a (by omega)).val = (P.gateIn1 a (by omega)).val :=congrArg Fin.val hij.symmcongr 1; exact Fin.ext hvalrw [hop] at hgatesimp only [SLPOp.applyPoly] at hgaterw [h2eq] at hgatehave hsub : a + 1 - a = 1 := by omegarw [hsub, pow_one, sq]simpa using hgaterefine ⟨heq, ?_⟩have hsub : a + 1 - a = 1 := by omegarw [hsub, pow_one]exact hsq.1| succ b' hb' ih =>have hprev := ih (by omega) (fun t ht h1 h2 => hrun t ht h1 (by omega))have htop_prev := hrun (b' - 1) (by omega) (by omega) (by omega)have htop_cur := hrun b' (by omega) (by omega) (by omega)have hprev_succ : (b' - 1) + 1 = b' := by omegahave hin := P.topSq_next_input_eq (t := b' - 1) (by omega) htop_prev (by simpa [hprev_succ] using htop_cur)have hgate1_congr : ∀ {t1 t2 : ℕ} (h1 : t1 < τ) (h2 : t2 < τ), t1 = t2 →(P.gateIn1 t1 h1).val = (P.gateIn1 t2 h2).val := by rintro t1 _ h1 h2 rfl; rflhave hgate2_congr : ∀ {t1 t2 : ℕ} (h1 : t1 < τ) (h2 : t2 < τ), t1 = t2 →(P.gateIn2 t1 h1).val = (P.gateIn2 t2 h2).val := by rintro t1 _ h1 h2 rfl; rflhave hin1 : (P.gateIn1 b' (by omega)).val = b' + 1 := byhave h1 := hin.1rw [hgate1_congr (by omega) (by omega) hprev_succ] at h1omegahave hin2 : (P.gateIn2 b' (by omega)).val = b' + 1 := byhave h2 := hin.2rw [hgate2_congr (by omega) (by omega) hprev_succ] at h2omegahave hop : P.gateOp b' (by omega) = .mul := htop_cur.1have hgate := P.evalWire_gate b' (by omega)rw [hop] at hgatesimp only [SLPOp.applyPoly] at hgatehave hwire_sq : P.evalWire ⟨b' + 1 + 1, by omega⟩ =P.evalWire ⟨b' + 1, by omega⟩ * P.evalWire ⟨b' + 1, by omega⟩ := bysimpa [hin1, hin2] using hgatehave hsub : b' + 1 - a = (b' - a) + 1 := by omegarefine ⟨?_, ?_⟩· rw [hwire_sq, hprev.1, hsub, pow_succ, pow_mul, sq]· have hne : P.evalWire ⟨b' + 1, by omega⟩ ≠ 0 := byintro h0have : (P.evalWire ⟨b' + 1, by omega⟩).natDegree = 0 := by simp [h0]rw [hprev.2] at thishave hpos1 : 1 ≤ 2 ^ (b' - a) := Nat.one_le_pow _ _ (by omega)have hpos2 : 1 ≤ (P.prefix a (by omega)).maxDeg := (P.prefix a (by omega)).one_le_maxDegnlinarithrw [hwire_sq, Polynomial.natDegree_mul hne hne, hprev.2, hsub, pow_succ]ring/-! ## 2. Untapped Segments & Pure-Combinatorial Demotion Lemma -//-- An untapped pure-squaring segment `[a, b)` (`a < b ≤ τ`) of `P : SLP τ`:every gate in `[a, b)` is a top-degree pure squaring, and no gate `t' ≥ b` reads any strictintermediate wire in `{a + 2, ..., b}`. -/structure UntappedSeg {τ : ℕ} (P : SLP τ) wherea : ℕb : ℕhab : a < bhb : b ≤ τhrun : ∀ t (ht : t < τ), a ≤ t → t < b → P.gateIsTopSq t hthuntapped : ∀ t' (ht' : t' < τ), b ≤ t' →¬ (a + 2 ≤ (P.gateIn1 t' ht').val ∧ (P.gateIn1 t' ht').val ≤ b) ∧¬ (a + 2 ≤ (P.gateIn2 t' ht').val ∧ (P.gateIn2 t' ht').val ≤ b)/-- A valid ordered list of disjoint untapped segments inside `[0, τ)`. -/def SegsOrdered {τ : ℕ} {P : SLP τ} : List (UntappedSeg P) → Prop| [] => True| [_] => True| s1 :: s2 :: rest => s1.b ≤ s2.a ∧ SegsOrdered (s2 :: rest)/-- Total length of a list of segments. -/def segsTotalLen {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) : ℕ :=(L.map (fun s => s.b - s.a)).sumprivate lemma segsOrdered_head_le {τ : ℕ} {P : SLP τ} :∀ (s0 : UntappedSeg P) (rest : List (UntappedSeg P)),SegsOrdered (s0 :: rest) → ∀ s ∈ s0 :: rest, s0.a ≤ s.a| s0, [], _, s, hs => by simp at hs; subst hs; exact le_rfl| s0, s1 :: rest, ⟨h01, hrest⟩, s, hs => byrcases List.mem_cons.mp hs with rfl | hs'· exact le_rfl· exact le_trans (le_of_lt s0.hab) (le_trans h01 (segsOrdered_head_le s1 rest hrest s hs'))/-- Total length of ordered disjoint segments inside `[0, τ)` is at most `τ`. -/private lemma segsTotalLen_le_sub {τ : ℕ} {P : SLP τ} :∀ (L : List (UntappedSeg P)), SegsOrdered L →∀ m : ℕ, (∀ s ∈ L, m ≤ s.a) → segsTotalLen L ≤ τ - m| [], _, _, _ => Nat.zero_le _| [s], _, m, hm => byhave := hm s (by simp)have := s.hab; have := s.hbsimp [segsTotalLen]; omega| s1 :: s2 :: rest, ⟨h12, hrest⟩, m, hm => byhave hm1 : m ≤ s1.a := hm s1 (by simp)have hall2 : ∀ s ∈ s2 :: rest, s1.b ≤ s.a := fun s hs =>le_trans h12 (segsOrdered_head_le s2 rest hrest s hs)have ih := segsTotalLen_le_sub (s2 :: rest) hrest s1.b hall2have := s1.hab; have := s1.hbsimp only [segsTotalLen, List.map_cons, List.sum_cons] at ih ⊢omegalemma segsTotalLen_le {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (hord : SegsOrdered L) :segsTotalLen L ≤ τ := bysimpa using segsTotalLen_le_sub L hord 0 (fun _ _ => Nat.zero_le _)/-- Pairwise non-overlapping ordering on a list of intervals `(a, b)`. -/private def PairsOrdered : List (ℕ × ℕ) → Prop| [] => True| [_] => True| p1 :: p2 :: rest => p1.2 ≤ p2.1 ∧ PairsOrdered (p2 :: rest)private lemma pairsOrdered_append_singleton :∀ (L : List (ℕ × ℕ)) (q : ℕ × ℕ),PairsOrdered L → (∀ p, L.getLast? = some p → p.2 ≤ q.1) → PairsOrdered (L ++ [q])| [], _, _, _ => trivial| [p], q, _, hlast => ⟨hlast p rfl, trivial⟩| p1 :: p2 :: rest, q, ⟨h12, hrest⟩, hlast =>⟨h12, pairsOrdered_append_singleton (p2 :: rest) q hrest (fun p hp => hlast p (by simpa using hp))⟩private lemma pairsOrdered_replace_last :∀ (L : List (ℕ × ℕ)) (p q : ℕ × ℕ),PairsOrdered (L ++ [p]) → p.1 = q.1 → PairsOrdered (L ++ [q])| [], _, _, _, _ => trivial| [r], p, q, ⟨hrp, _⟩, hpq => ⟨by omega, trivial⟩| r1 :: r2 :: rest, p, q, ⟨h12, hrest⟩, hpq =>⟨h12, pairsOrdered_replace_last (r2 :: rest) p q hrest hpq⟩/-- Clean strengthened interval-partition induction:`L.length ≤ bad + cut + (if L ends at n with ¬ isCut (n - 1) then 1 else 0)`. -/private lemma partition_intervals_strong (isGood isCut : ℕ → Prop)[DecidablePred isGood] [DecidablePred isCut] :∀ n : ℕ,∃ L : List (ℕ × ℕ),(∀ p ∈ L, p.1 < p.2 ∧ p.2 ≤ n) ∧PairsOrdered L ∧(∀ p ∈ L, ∀ t, p.1 ≤ t → t < p.2 → isGood t) ∧(∀ p ∈ L, ∀ t, p.1 ≤ t → t + 1 < p.2 → ¬ isCut t) ∧((L.map (fun p => p.2 - p.1)).sum = ((Finset.range n).filter isGood).card) ∧(L.length ≤ ((Finset.range n).filter (fun t => ¬ isGood t)).card +((Finset.range n).filter (fun t => isGood t ∧ isCut t)).card +if (∃ p, L.getLast? = some p ∧ p.2 = n ∧ ¬ isCut (n - 1)) then 1 else 0) := byintro ninduction n with| zero =>refine ⟨[], by simp, trivial, by simp, by simp, by simp, by simp⟩| succ m ih =>obtain ⟨L, hbounds, hchain, hgood, hnocut, hsum, hlen⟩ := ihhave hrange_good : ((Finset.range (m + 1)).filter isGood).card =((Finset.range m).filter isGood).card + if isGood m then 1 else 0 := byby_cases h : isGood m <;> simp [Finset.range_add_one, Finset.filter_insert, h]have hrange_bad : ((Finset.range (m + 1)).filter (fun t => ¬ isGood t)).card =((Finset.range m).filter (fun t => ¬ isGood t)).card + if ¬ isGood m then 1 else 0 := byby_cases h : isGood m <;> simp [Finset.range_add_one, Finset.filter_insert, h]have hrange_cut : ((Finset.range (m + 1)).filter (fun t => isGood t ∧ isCut t)).card =((Finset.range m).filter (fun t => isGood t ∧ isCut t)).card +if isGood m ∧ isCut m then 1 else 0 := byby_cases h : isGood m ∧ isCut m <;> simp [Finset.range_add_one, Finset.filter_insert, h]by_cases hgm : isGood m· by_cases hext : ∃ p, L.getLast? = some p ∧ p.2 = m ∧ ¬ isCut (m - 1)· rcases hext with ⟨p, hlast, hp2, hnc_prev⟩have hL_ne : L ≠ [] := by intro h; simp [h] at hlasthave hL_eq : L = L.dropLast ++ [p] := byhave := List.dropLast_append_getLast hL_nehave hgl : L.getLast hL_ne = p := byrw [List.getLast?_eq_some_getLast hL_ne] at hlastexact Option.some.inj hlastrw [hgl] at thisexact this.symmset L0 := L.dropLasthave hbounds0 : ∀ q ∈ L0 ++ [p], q.1 < q.2 ∧ q.2 ≤ m := by rw [← hL_eq]; exact hboundshave hchain0 : PairsOrdered (L0 ++ [p]) := by rw [← hL_eq]; exact hchainhave hgood0 : ∀ q ∈ L0 ++ [p], ∀ t, q.1 ≤ t → t < q.2 → isGood t := by rw [← hL_eq]; exact hgoodhave hnocut0 : ∀ q ∈ L0 ++ [p], ∀ t, q.1 ≤ t → t + 1 < q.2 → ¬ isCut t := by rw [← hL_eq]; exact hnocuthave hsum0 : ((L0 ++ [p]).map (fun q => q.2 - q.1)).sum = ((Finset.range m).filter isGood).card := byrw [← hL_eq]; exact hsumrefine ⟨L0 ++ [(p.1, m + 1)], ?_, ?_, ?_, ?_, ?_, ?_⟩· intro q hqrcases List.mem_append.mp hq with hq0 | hq1· have := hbounds0 q (List.mem_append_left _ hq0); omega· simp at hq1; subst hq1have := hbounds0 p (by simp); omega· exact pairsOrdered_replace_last L0 p (p.1, m + 1) hchain0 rfl· intro q hq t h1 h2rcases List.mem_append.mp hq with hq0 | hq1· exact hgood0 q (List.mem_append_left _ hq0) t h1 h2· simp at hq1; subst hq1by_cases htm : t = m· subst htm; exact hgm· exact hgood0 p (by simp) t h1 (by omega)· intro q hq t h1 h2rcases List.mem_append.mp hq with hq0 | hq1· exact hnocut0 q (List.mem_append_left _ hq0) t h1 h2· simp at hq1; subst hq1by_cases htm : t = m - 1· subst htm; exact hnc_prev· exact hnocut0 p (by simp) t h1 (by omega)· have hp_lt := (hbounds0 p (by simp)).1simp only [List.map_append, List.sum_append, List.map_singleton, List.sum_singleton] at hsum0 ⊢rw [hrange_good]; simp only [hgm, ↓reduceIte]; omega· have hlen_eq : (L0 ++ [(p.1, m + 1)]).length = L.length := byrw [hL_eq]; simprw [hlen_eq, hrange_bad, hrange_cut]have hprev_cond : ∃ p', L.getLast? = some p' ∧ p'.2 = m ∧ ¬ isCut (m - 1) :=⟨p, hlast, hp2, hnc_prev⟩simp only [hprev_cond, ↓reduceIte] at hlenhave hnew_last : (L0 ++ [(p.1, m + 1)]).getLast? = some (p.1, m + 1) := by simpby_cases hcm : isCut m· have h2 : ¬ (∃ q, (L0 ++ [(p.1, m + 1)]).getLast? = some q ∧ q.2 = m + 1 ∧ ¬ isCut (m + 1 - 1)) := byrintro ⟨q, _, _, hnc⟩; simp at hnc; exact hnc hcmsimp only [hgm, hcm, and_self, not_true_eq_false, ↓reduceIte, h2]omega· have h2 : ∃ q, (L0 ++ [(p.1, m + 1)]).getLast? = some q ∧ q.2 = m + 1 ∧ ¬ isCut (m + 1 - 1) :=⟨(p.1, m + 1), hnew_last, rfl, by simpa using hcm⟩simp only [hgm, hcm, and_false, not_true_eq_false, ↓reduceIte, h2]omega· -- Not extendable: append (m, m + 1)refine ⟨L ++ [(m, m + 1)], ?_, ?_, ?_, ?_, ?_, ?_⟩· intro q hqrcases List.mem_append.mp hq with hq0 | hq1· have := hbounds q hq0; omega· simp at hq1; subst hq1; omega· exact pairsOrdered_append_singleton L (m, m + 1) hchain(fun x hx => (hbounds x (List.mem_of_getLast? hx)).2)· intro q hq t h1 h2rcases List.mem_append.mp hq with hq0 | hq1· exact hgood q hq0 t h1 h2· simp at hq1; subst hq1have : t = m := by omegasubst this; exact hgm· intro q hq t h1 h2rcases List.mem_append.mp hq with hq0 | hq1· exact hnocut q hq0 t h1 h2· simp at hq1; subst hq1; omega· simp only [List.map_append, List.sum_append, List.map_singleton, List.sum_singleton]rw [hsum, hrange_good]; simp only [hgm, ↓reduceIte]; omega· simp only [List.length_append, List.length_singleton]rw [hrange_bad, hrange_cut]simp only [hext, ↓reduceIte] at hlenhave hnew_last : (L ++ [(m, m + 1)]).getLast? = some (m, m + 1) := by simpby_cases hcm : isCut m· have h2 : ¬ (∃ q, (L ++ [(m, m + 1)]).getLast? = some q ∧ q.2 = m + 1 ∧ ¬ isCut (m + 1 - 1)) := byrintro ⟨q, _, _, hnc⟩; simp at hnc; exact hnc hcmsimp only [hgm, hcm, and_self, not_true_eq_false, ↓reduceIte, h2]omega· have h2 : ∃ q, (L ++ [(m, m + 1)]).getLast? = some q ∧ q.2 = m + 1 ∧ ¬ isCut (m + 1 - 1) :=⟨(m, m + 1), hnew_last, rfl, by simpa using hcm⟩simp only [hgm, hcm, and_false, not_true_eq_false, ↓reduceIte, h2]omega· -- Gate m is NOT good: keep L unchanged!refine ⟨L, ?_, hchain, hgood, hnocut, ?_, ?_⟩· intro q hq; have := hbounds q hq; omega· rw [hsum, hrange_good]; simp [hgm]· rw [hrange_bad, hrange_cut]have hnot_new : ¬ (∃ q, L.getLast? = some q ∧ q.2 = m + 1 ∧ ¬ isCut (m + 1 - 1)) := byrintro ⟨q, hq, hq2, _⟩have := (hbounds q (List.mem_of_getLast? hq)).2omegasimp only [hgm, not_false_eq_true, false_and, ↓reduceIte, hnot_new]split_ifs at hlen <;> omega/-- **Untapped Segment Extraction Lemma:** Every `P : SLP τ` admits an ordered list `L` ofdisjoint untapped pure-squaring segments with `L.length ≤ 3 * P.nonTopSqCount + 1` and`segsTotalLen L = τ - P.nonTopSqCount`. -/lemma extract_untapped_segments {τ : ℕ} (P : SLP τ) :∃ L : List (UntappedSeg P),SegsOrdered L ∧L.length ≤ 3 * P.nonTopSqCount + 1 ∧segsTotalLen L = τ - P.nonTopSqCount := byclassicallet isGood : ℕ → Prop := fun t => ∃ ht : t < τ, P.gateIsTopSq t htlet isCut : ℕ → Prop := fun t =>∃ (t' : ℕ) (ht' : t' < τ), ¬ P.gateIsTopSq t' ht' ∧((P.gateIn1 t' ht').val = t + 2 ∨ (P.gateIn2 t' ht').val = t + 2)obtain ⟨L0, hbounds, hchain, hgood, hnocut, hsum, hlen⟩ :=partition_intervals_strong isGood isCut τ-- Count bad gates = P.nonTopSqCounthave hbad_card : ((Finset.range τ).filter (fun t => ¬ isGood t)).card = P.nonTopSqCount := byrw [P.nonTopSqCount_eq_card]refine Finset.card_bij (fun t ht => (⟨t, Finset.mem_range.mp (Finset.mem_filter.mp ht).1⟩ : Fin τ))?_ ?_ ?_· intro t hthave h1 := Finset.mem_filter.mp hthave ht_lt : t < τ := Finset.mem_range.mp h1.1refine Finset.mem_filter.mpr ⟨Finset.mem_univ _, fun htop => h1.2 ⟨ht_lt, htop⟩⟩· intro a _ b _ h; exact congrArg Fin.val h· intro u huhave hnot := (Finset.mem_filter.mp hu).2refine ⟨u.val, Finset.mem_filter.mpr ⟨Finset.mem_range.mpr u.isLt, ?_⟩, Fin.ext rfl⟩rintro ⟨_, htop⟩; exact hnot htop-- Count good gates = τ - P.nonTopSqCounthave hgood_card : ((Finset.range τ).filter isGood).card = τ - P.nonTopSqCount := byhave hcomp := Finset.card_filter_add_card_filter_not (s := Finset.range τ) (p := isGood)rw [Finset.card_range, hbad_card] at hcompomega-- Count cut gates ≤ 2 * P.nonTopSqCounthave hcut_card : ((Finset.range τ).filter (fun t => isGood t ∧ isCut t)).card ≤2 * P.nonTopSqCount := bylet Bset : Finset (Fin τ) :=Finset.univ.filter (fun t' : Fin τ => ¬ P.gateIsTopSq t'.val t'.isLt)let Cset : Finset ℕ :=Bset.biUnion (fun t' =>({(P.gateIn1 t'.val t'.isLt).val, (P.gateIn2 t'.val t'.isLt).val} : Finset ℕ))have hsub : ((Finset.range τ).filter (fun t => isGood t ∧ isCut t)).image (fun t => t + 2) ⊆Cset := byintro w hwrcases Finset.mem_image.mp hw with ⟨t, ht, rfl⟩rcases (Finset.mem_filter.mp ht).2.2 with ⟨t', ht', hnot, hread⟩refine Finset.mem_biUnion.mpr ⟨⟨t', ht'⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, hnot⟩, ?_⟩rcases hread with h1 | h2· simp [h1]· simp [h2]have hinj : Function.Injective (fun t : ℕ => t + 2) := fun a b h => by dsimp only at h; omegacalc ((Finset.range τ).filter (fun t => isGood t ∧ isCut t)).card= (((Finset.range τ).filter (fun t => isGood t ∧ isCut t)).image (fun t => t + 2)).card :=(Finset.card_image_of_injective _ hinj).symm_ ≤ Cset.card := Finset.card_le_card hsub_ ≤ ∑ t' ∈ Bset, ({(P.gateIn1 t'.val t'.isLt).val, (P.gateIn2 t'.val t'.isLt).val} : Finset ℕ).card :=Finset.card_biUnion_le_ ≤ ∑ _t' ∈ Bset, 2 := Finset.sum_le_sum (fun _ _ => le_trans (Finset.card_insert_le _ _) (by simp))_ = 2 * Bset.card := by simp [Nat.mul_comm]_ = 2 * P.nonTopSqCount := by rw [P.nonTopSqCount_eq_card]-- Convert each (a, b) ∈ L0 into an UntappedSeg P!let hmk : ∀ p ∈ L0, UntappedSeg P := fun p hp =>{ a := p.1b := p.2hab := (hbounds p hp).1hb := (hbounds p hp).2hrun := fun t ht h1 h2 => byobtain ⟨_, htop⟩ := hgood p hp t h1 h2exact htophuntapped := fun t' ht' hbt' => by-- Neither input of gate t' ≥ b can lie in [a + 2, b]!-- If t' is NOT topSq: ruled out by ¬ isCut (w - 2).-- If t' IS topSq: deg(wire w) < D₁(w - 1) ≤ D₁(t') since w - 2 < b - 1 < t'!by_cases htop' : P.gateIsTopSq t' ht'· have hne : ∀ w : Fin (t' + 2),(P.prefix t' (le_of_lt ht')).evalWire w = P.evalWire ⟨w.val, by omega⟩ →(P.evalWire ⟨w.val, by omega⟩).natDegree = (P.prefix t' (le_of_lt ht')).maxDeg →¬ (p.1 + 2 ≤ w.val ∧ w.val ≤ p.2) := byintro w _ hdeg_w ⟨hw1, hw2⟩-- Gate w.val - 1 lies in [p.1 + 1, p.2 - 1], wait: wire w.val is created by gate t0 = w.val - 2!-- Since w.val ≤ p.2 ≤ t', gate t0 + 1 = w.val - 1 < p.2 ≤ t' is ALSO in [p.1, p.2), hence isTopSq!have ht0_lt : w.val - 1 < τ := by omegahave ht0_top : P.gateIsTopSq (w.val - 1) ht0_lt := byobtain ⟨_, ht⟩ := hgood p hp (w.val - 1) (by omega) (by omega)exact hthave hstrict := (P.topSq_strict_maxDeg ht0_lt ht0_top).2.2 ⟨w.val, by omega⟩dsimp only at hstricthave hmono := P.maxDeg_prefix_mono (t1 := (w.val - 1) + 1) (t2 := t') (by omega) (le_of_lt ht')omegarcases htop' with ⟨_, hij, hdeg1⟩have h1 : ¬ (p.1 + 2 ≤ (P.gateIn1 t' ht').val ∧ (P.gateIn1 t' ht').val ≤ p.2) := byapply hne (P.gateIn1 t' ht') (P.evalWire_prefix t' (le_of_lt ht') (P.gateIn1 t' ht'))rw [← P.evalWire_prefix t' (le_of_lt ht') (P.gateIn1 t' ht')]; exact hdeg1exact ⟨h1, by rw [← hij]; exact h1⟩· constructor· rintro ⟨hw1, hw2⟩have hnc := hnocut p hp ((P.gateIn1 t' ht').val - 2) (by omega) (by omega)exact hnc ⟨t', ht', htop', Or.inl (by omega)⟩· rintro ⟨hw1, hw2⟩have hnc := hnocut p hp ((P.gateIn2 t' ht').val - 2) (by omega) (by omega)exact hnc ⟨t', ht', htop', Or.inr (by omega)⟩ }let L : List (UntappedSeg P) := L0.pmap hmk (fun _ h => h)have hord : SegsOrdered L := byhave hgen : ∀ (M : List (ℕ × ℕ)) (hM : ∀ p ∈ M, p ∈ L0),PairsOrdered M →SegsOrdered (M.pmap hmk hM) := byintro Minduction M with| nil => intros; trivial| cons p1 rest ih =>cases rest with| nil => intros; trivial| cons p2 rest' =>intro hM hchexact ⟨hch.1, ih (fun q hq => hM q (List.mem_cons_of_mem _ hq)) hch.2⟩exact hgen L0 (fun _ h => h) hchainrefine ⟨L, hord, ?_, ?_⟩· have hlenL : L.length = L0.length := List.length_pmaprw [hlenL]split_ifs at hlen <;> omega· have hmap : L.map (fun s => s.b - s.a) = L0.map (fun p => p.2 - p.1) := bysimp [L, List.map_pmap, hmk]rw [segsTotalLen, hmap, hsum, hgood_card]/-- Gap condition for a nonempty ordered list of untapped segments `L` with minimum length `Δ`. -/def ValidGapSegs {τ : ℕ} {P : SLP τ} (Δ : ℕ) (L : List (UntappedSeg P)) : Prop :=L ≠ [] ∧SegsOrdered L ∧(∀ s ∈ L, Δ ≤ s.b - s.a) ∧(∀ (s0 : UntappedSeg P) (rest : List (UntappedSeg P)), L = s0 :: rest →let nextA := match rest with | [] => τ | s1 :: _ => s1.a2 * s0.a + (nextA - s0.b) + 5 ≤ s0.b - s0.a) ∧(∀ (i : ℕ) (hi : i + 1 < L.length),let sPrev := L[i]'(by omega)let sCur := L[i + 1]'hilet nextA := if h2 : i + 2 < L.length then (L[i + 2]'h2).a else τ(sCur.a - sPrev.b) + (nextA - sCur.b) + 3 ≤ sCur.b - sCur.a)/-- Erasing an element preserves `SegsOrdered`. -/private lemma segsOrdered_eraseIdx {τ : ℕ} {P : SLP τ} :∀ (L : List (UntappedSeg P)), SegsOrdered L → ∀ idx : ℕ, SegsOrdered (L.eraseIdx idx)| [], _, _ => trivial| [_], _, idx => by cases idx <;> simp [SegsOrdered]| s1 :: s2 :: rest, ⟨h12, hrest⟩, 0 => hrest| s1 :: s2 :: rest, ⟨h12, hrest⟩, 1 => bycases rest with| nil => trivial| cons s3 rest' =>exact ⟨le_trans h12 (le_trans (le_of_lt s2.hab) hrest.1), hrest.2⟩| s1 :: s2 :: rest, ⟨h12, hrest⟩, idx + 2 => bychange SegsOrdered (s1 :: (s2 :: rest).eraseIdx (idx + 1))have ih := segsOrdered_eraseIdx (s2 :: rest) hrest (idx + 1)cases rest with| nil =>simp [List.eraseIdx, SegsOrdered, h12]| cons s3 rest' =>exact ⟨h12, ih⟩/-- Erasing index `idx < L.length` decreases `segsTotalLen` by `(L[idx]).b - (L[idx]).a`. -/private lemma segsTotalLen_eraseIdx {τ : ℕ} {P : SLP τ} :∀ (L : List (UntappedSeg P)) (idx : ℕ) (hidx : idx < L.length),segsTotalLen (L.eraseIdx idx) + ((L[idx]'hidx).b - (L[idx]'hidx).a) = segsTotalLen L| s :: rest, 0, _ => bysimp [segsTotalLen, Nat.add_comm]| s :: rest, idx + 1, hidx => byhave ih := segsTotalLen_eraseIdx rest idx (by simpa using hidx)simp only [List.eraseIdx_cons_succ, segsTotalLen, List.map_cons, List.sum_cons,List.getElem_cons_succ] at ih ⊢omega/-- Helper: `s.a` and `nextA - s.b` are bounded by `R_tot = τ - segsTotalLen L`! -/private lemma gap_le_Rtot_head {τ : ℕ} {P : SLP τ} (s0 : UntappedSeg P)(rest : List (UntappedSeg P)) (hord : SegsOrdered (s0 :: rest)) :let nextA := match rest with | [] => τ | s1 :: _ => s1.as0.a + (nextA - s0.b) ≤ τ - segsTotalLen (s0 :: rest) := byhave hle_tot := segsTotalLen_le (s0 :: rest) hordcases rest with| nil =>simp only [segsTotalLen, List.map_singleton, List.sum_singleton]have := s0.hab; have := s0.hb; omega| cons s1 rest' =>have hall : ∀ s ∈ s1 :: rest', s1.a ≤ s.a := segsOrdered_head_le s1 rest' hord.2have hsub := segsTotalLen_le_sub (s1 :: rest') hord.2 s1.a hallhave h01 : s0.b ≤ s1.a := hord.1have := s0.hab; have := s1.hab; have := s1.hbsimp only [segsTotalLen, List.map_cons, List.sum_cons] at hsub hle_tot ⊢omega/-- Strengthened version of `gap_le_Rtot_mid` that carries `L.head.a` cleanly through induction. -/private lemma gap_le_Rtot_mid_strong {τ : ℕ} {P : SLP τ} :∀ (L : List (UntappedSeg P)), SegsOrdered L →∀ (i : ℕ) (hi : i + 1 < L.length),let sFirst := L[0]'(by omega)let sPrev := L[i]'(by omega)let sCur := L[i + 1]'hilet nextA := if h2 : i + 2 < L.length then (L[i + 2]'h2).a else τsFirst.a + (sCur.a - sPrev.b) + (nextA - sCur.b) ≤ τ - segsTotalLen L := byintro Linduction L with| nil => intro _ i hi; simp at hi| cons s0 rest ih =>intro hord i hicases i with| zero =>cases rest with| nil => simp at hi| cons s1 rest' =>have hhead := gap_le_Rtot_head s1 rest' hord.2have h01 : s0.b ≤ s1.a := hord.1have := s0.habcases rest' with| nil =>have hnot : ¬ (2 < 2) := by omegasimp only [List.length_cons, List.length_nil, hnot, ↓reduceDIte,List.getElem_cons_zero, List.getElem_cons_succ, segsTotalLen,List.map_cons, List.map_nil, List.sum_cons, List.sum_nil]have := s1.hab; have := s1.hb; omega| cons s2 rest'' =>have hlt2 : 2 < (s0 :: s1 :: s2 :: rest'').length := by simpsimp only [hlt2, ↓reduceDIte, List.getElem_cons_succ, List.getElem_cons_zero]simp only [segsTotalLen, List.map_cons, List.sum_cons] at hhead ⊢omega| succ i' =>cases rest with| nil => simp at hi| cons s1 rest' =>have hrec := ih hord.2 i' (by simpa using hi)have h01 : s0.b ≤ s1.a := hord.1have := s0.habhave hiff : (i' + 1 + 2 < (s0 :: s1 :: rest').length) ↔ (i' + 2 < (s1 :: rest').length) := bysimpsimp only [List.getElem_cons_zero, List.getElem_cons_succ] at hrec ⊢split_ifs at hrec ⊢ with h1 h2· simp only [segsTotalLen, List.map_cons, List.sum_cons] at hrec ⊢omega· omega· omega· simp only [segsTotalLen, List.map_cons, List.sum_cons] at hrec ⊢omega/-- **Pure-Combinatorial Demotion Lemma:** Given any ordered list `L` of untapped segments of `P`and threshold `Δ`, either some sublist `L'` satisfies `ValidGapSegs Δ L'` with`L'.length ≤ L.length`, OR `τ + Δ + 2 ≤ 3 ^ L.length * (τ - segsTotalLen L + Δ + 2)`. -/lemma demotion_exists_valid_gaps {τ : ℕ} {P : SLP τ} (Δ : ℕ) :∀ (n : ℕ) (L : List (UntappedSeg P)),L.length = n → SegsOrdered L →(∃ L' : List (UntappedSeg P), ValidGapSegs Δ L' ∧ L'.length ≤ L.length) ∨τ + Δ + 2 ≤ 3 ^ n * (τ - segsTotalLen L + Δ + 2) := byintro ninduction n with| zero =>intro L hlen _righthave hnil : L = [] := List.eq_nil_of_length_eq_zero hlensubst hnilsimp [segsTotalLen]| succ n' ih =>intro L hlen hordby_cases hvalid : ValidGapSegs Δ L· exact Or.inl ⟨L, hvalid, le_rfl⟩· -- Find a violating index idx < L.length whose length g ≤ 2 * R_tot + Δ + 4have hL_ne : L ≠ [] := fun h => by simp [h] at hlenhave hex_bad : ∃ (idx : ℕ) (hidx : idx < L.length),(L[idx]'hidx).b - (L[idx]'hidx).a ≤ 2 * (τ - segsTotalLen L) + Δ + 4 := byby_contra hnonepush Not at hnoneapply hvalidrefine ⟨hL_ne, hord, ?_, ?_, ?_⟩· intro s hsobtain ⟨idx, hidx, rfl⟩ := List.getElem_of_mem hshave := hnone idx hidxomega· intro s0 rest hLeqsubst hLeqhave hbig := hnone 0 (by simp)have hhead := gap_le_Rtot_head s0 rest hordsimp only [List.getElem_cons_zero] at hbigcases rest <;> dsimp only at hhead ⊢ <;> omega· intro i hihave hbig := hnone (i + 1) hihave hmid := gap_le_Rtot_mid_strong L hord i hiomegaobtain ⟨idx, hidx, hg_le⟩ := hex_badlet L' := L.eraseIdx idxhave hlen' : L'.length = n' := byrw [List.length_eraseIdx_of_lt hidx, hlen]; omegahave hord' : SegsOrdered L' := segsOrdered_eraseIdx L hord idxhave hsum' : segsTotalLen L' + ((L[idx]'hidx).b - (L[idx]'hidx).a) = segsTotalLen L :=segsTotalLen_eraseIdx L idx hidxhave hle_L := segsTotalLen_le L hordrcases ih L' hlen' hord' with ⟨L'', hv'', hl''⟩ | hbound'· exact Or.inl ⟨L'', hv'', by omega⟩· righthave hR' : τ - segsTotalLen L' + Δ + 2 ≤ 3 * (τ - segsTotalLen L + Δ + 2) := byhave := (L[idx]'hidx).habomegacalc τ + Δ + 2≤ 3 ^ n' * (τ - segsTotalLen L' + Δ + 2) := hbound'_ ≤ 3 ^ n' * (3 * (τ - segsTotalLen L + Δ + 2)) := Nat.mul_le_mul_left _ hR'_ = 3 ^ (n' + 1) * (τ - segsTotalLen L + Δ + 2) := by rw [pow_succ]; ring/-! ## 3. Formal Substitution `F(x, T) ∈ (Polynomial ℤ)[X]` & The 4 Block Properties -//-- Apply `SLPOp` to two elements of `Polynomial (Polynomial ℤ)`. -/noncomputable def SLPOp.applyPolyT :SLPOp → Polynomial (Polynomial ℤ) → Polynomial (Polynomial ℤ) → Polynomial (Polynomial ℤ)| .add, A, B => A + B| .sub, A, B => A - B| .mul, A, B => A * B/-- Evaluate `P` in `Polynomial (Polynomial ℤ)` up to gate `t` (`b ≤ t ≤ τ`), replacing wire `b + 1`by the indeterminate `T = Polynomial.X`, keeping all wires `≤ a + 1` as `Polynomial.C (P.evalWire i)`,and setting unread strict intermediate wires `a + 2 .. b` to `0`. -/noncomputable def evalFormalT {τ : ℕ} (P : SLP τ) (a b : ℕ) (hab : a < b) (hb : b ≤ τ)(t : ℕ) (_hbt : b ≤ t) (ht : t ≤ τ) (w : Fin (t + 2)) : Polynomial (Polynomial ℤ) :=if hw : w.val < b + 1 thenif hwa : w.val ≤ a + 1 thenPolynomial.C (P.evalWire ⟨w.val, by omega⟩)else0else if hwb : w.val = b + 1 thenPolynomial.Xelse-- w.val ≥ b + 2, so w.val = t' + 2 with b ≤ t' < thave hprev_lt : w.val - 2 < τ := by omegahave hbt' : b ≤ w.val - 2 := by omegalet t' := w.val - 2let in1 := P.gateIn1 t' hprev_ltlet in2 := P.gateIn2 t' hprev_lt(P.gateOp t' hprev_lt).applyPolyT(evalFormalT P a b hab hb t' hbt' (le_of_lt hprev_lt) in1)(evalFormalT P a b hab hb t' hbt' (le_of_lt hprev_lt) in2)termination_by tdecreasing_byall_goals omega/-- Property 1 (Exact Specialization): evaluating `T := P.evalWire (b + 1)` recovers `P.evalWire w`for every wire `w` that is not a strict intermediate wire of an untapped segment `seg`. -/lemma evalFormalT_spec {τ : ℕ} (P : SLP τ) (seg : UntappedSeg P) :∀ (t : ℕ) (hbt : seg.b ≤ t) (ht : t ≤ τ) (w : Fin (t + 2)),¬ (seg.a + 2 ≤ w.val ∧ w.val ≤ seg.b) →(evalFormalT P seg.a seg.b seg.hab seg.hb t hbt ht w).eval(P.evalWire ⟨seg.b + 1, by omega⟩) = P.evalWire ⟨w.val, by omega⟩ := byintro tinduction t using Nat.strong_induction_on with| ind t ih =>intro hbt ht w hnot_interrw [evalFormalT.eq_def]split_ifs with hw hwa hwb· simp· exfalso; exact hnot_inter ⟨by omega, by omega⟩· simp [hwb]· set t' := w.val - 2have ht'_lt_t : t' < t := by omegahave hbt' : seg.b ≤ t' := by omegahave ht'_lt_τ : t' < τ := by omegahave hun := seg.huntapped t' ht'_lt_τ hbt'have ih1 := ih t' ht'_lt_t hbt' (le_of_lt ht'_lt_τ) (P.gateIn1 t' ht'_lt_τ) hun.1have ih2 := ih t' ht'_lt_t hbt' (le_of_lt ht'_lt_τ) (P.gateIn2 t' ht'_lt_τ) hun.2have hw_eq : w.val = t' + 2 := by omegahave hgate := P.evalWire_gate t' ht'_lt_τhave hpoly_eval : ∀ (op : SLPOp) (A B : Polynomial (Polynomial ℤ)) (U : Polynomial ℤ),(op.applyPolyT A B).eval U = op.applyPoly (A.eval U) (B.eval U) := byintro op A B Ucases op <;> simp [SLPOp.applyPolyT, SLPOp.applyPoly]rw [hpoly_eval, ih1, ih2]simpa [hw_eq] using hgate.symm/-- Property 2 (`x`-Degree Bound): every `T`-coefficient of `evalFormalT` at horizon `t` has`natDegree ≤ 2 ^ (t - b) * D₁(a)`. -/lemma evalFormalT_coeff_natDegree_le {τ : ℕ} (P : SLP τ) (a b : ℕ) (hab : a < b) (hb : b ≤ τ) :∀ (t : ℕ) (hbt : b ≤ t) (ht : t ≤ τ) (w : Fin (t + 2)) (j : ℕ),((evalFormalT P a b hab hb t hbt ht w).coeff j).natDegree ≤2 ^ (t - b) * (P.prefix a (by omega)).maxDeg := byintro tinduction t using Nat.strong_induction_on with| ind t ih =>intro hbt ht w jrw [evalFormalT.eq_def]split_ifs with hw hwa hwb· rw [Polynomial.coeff_C]split_ifs with hj· have h1 : (P.evalWire ⟨w.val, by omega⟩).natDegree ≤ (P.prefix a (by omega)).maxDeg := byrw [← P.evalWire_prefix a (by omega) ⟨w.val, by omega⟩]exact (P.prefix a (by omega)).wireDeg_le_maxDeg ⟨w.val, by omega⟩have h2 : 1 ≤ 2 ^ (t - b) := Nat.one_le_pow _ _ (by omega)nlinarith· simp· simp· rw [Polynomial.coeff_X]split_ifs <;> simp· set t' := w.val - 2have ht'_lt_t : t' < t := by omegahave hbt' : b ≤ t' := by omegahave ht'_lt_τ : t' < τ := by omegahave hpow_le : 2 * 2 ^ (t' - b) ≤ 2 ^ (t - b) := byhave : (t' - b) + 1 ≤ t - b := by omegahave := Nat.pow_le_pow_right (by omega : 1 ≤ 2) thisrwa [pow_succ, Nat.mul_comm] at thisset A := evalFormalT P a b hab hb t' hbt' (le_of_lt ht'_lt_τ) (P.gateIn1 t' ht'_lt_τ)set B := evalFormalT P a b hab hb t' hbt' (le_of_lt ht'_lt_τ) (P.gateIn2 t' ht'_lt_τ)have hA : ∀ k, (A.coeff k).natDegree ≤ 2 ^ (t' - b) * (P.prefix a (by omega)).maxDeg :=ih t' ht'_lt_t hbt' (le_of_lt ht'_lt_τ) (P.gateIn1 t' ht'_lt_τ)have hB : ∀ k, (B.coeff k).natDegree ≤ 2 ^ (t' - b) * (P.prefix a (by omega)).maxDeg :=ih t' ht'_lt_t hbt' (le_of_lt ht'_lt_τ) (P.gateIn2 t' ht'_lt_τ)change (((P.gateOp t' ht'_lt_τ).applyPolyT A B).coeff j).natDegree ≤2 ^ (t - b) * (P.prefix a (by omega)).maxDegcases P.gateOp t' ht'_lt_τ with| add =>simp only [SLPOp.applyPolyT, Polynomial.coeff_add]have := le_trans (Polynomial.natDegree_add_le _ _) (max_le (hA j) (hB j))nlinarith| sub =>simp only [SLPOp.applyPolyT, Polynomial.coeff_sub]have := le_trans (Polynomial.natDegree_sub_le _ _) (max_le (hA j) (hB j))nlinarith| mul =>simp only [SLPOp.applyPolyT, Polynomial.coeff_mul]have hsum_deg : ∀ (s : Finset (ℕ × ℕ)) (f : ℕ × ℕ → Polynomial ℤ) (M : ℕ),(∀ p ∈ s, (f p).natDegree ≤ M) → (∑ p ∈ s, f p).natDegree ≤ M := byintro s f Minduction s using Finset.induction_on with| empty => intro _; simp| @insert u s' hu ihs =>intro hMrw [Finset.sum_insert hu]exact le_trans (Polynomial.natDegree_add_le _ _)(max_le (hM u (Finset.mem_insert_self _ _))(ihs (fun p hp => hM p (Finset.mem_insert_of_mem hp))))have hsum : (∑ x ∈ Finset.antidiagonal j, A.coeff x.1 * B.coeff x.2).natDegree ≤2 * (2 ^ (t' - b) * (P.prefix a (by omega)).maxDeg) := byapply hsum_degintro p _calc (A.coeff p.1 * B.coeff p.2).natDegree≤ (A.coeff p.1).natDegree + (B.coeff p.2).natDegree := Polynomial.natDegree_mul_le_ ≤ 2 ^ (t' - b) * (P.prefix a (by omega)).maxDeg +2 ^ (t' - b) * (P.prefix a (by omega)).maxDeg := Nat.add_le_add (hA p.1) (hB p.2)_ = 2 * (2 ^ (t' - b) * (P.prefix a (by omega)).maxDeg) := by ringhave hmul_le : 2 * (2 ^ (t' - b) * (P.prefix a (by omega)).maxDeg) ≤2 ^ (t - b) * (P.prefix a (by omega)).maxDeg := byrw [← Nat.mul_assoc]exact Nat.mul_le_mul_right _ hpow_leexact le_trans hsum hmul_le/-- `ℓ₁`-norm of `F(x, T)` at an integer `x : ℤ`: `∑_j |(F.coeff j).eval x|`. -/noncomputable def polyL1At (F : Polynomial (Polynomial ℤ)) (x : ℤ) : ℕ :=∑ j ∈ F.support, ((F.coeff j).eval x).natAbsprivate lemma polyL1At_eq_sum_range (F : Polynomial (Polynomial ℤ)) (x : ℤ) {N : ℕ}(hN : ∀ j ∈ F.support, j < N) :polyL1At F x = ∑ j ∈ Finset.range N, ((F.coeff j).eval x).natAbs := byunfold polyL1Atapply Finset.sum_subset (fun j hj => Finset.mem_range.mpr (hN j hj))intro j _ hjhave : F.coeff j = 0 := Polynomial.notMem_support_iff.mp hjsimp [this]private lemma polyL1At_add_le (A B : Polynomial (Polynomial ℤ)) (x : ℤ) :polyL1At (A + B) x ≤ polyL1At A x + polyL1At B x := byset N := max (A.natDegree + 1) (B.natDegree + 1)have hA : ∀ j ∈ A.support, j < N := fun j hj => byhave := Polynomial.le_natDegree_of_mem_supp j hj; omegahave hB : ∀ j ∈ B.support, j < N := fun j hj => byhave := Polynomial.le_natDegree_of_mem_supp j hj; omegahave hAB : ∀ j ∈ (A + B).support, j < N := fun j hj => byhave h1 := Polynomial.le_natDegree_of_mem_supp j hjhave h2 := Polynomial.natDegree_add_le A Bomegarw [polyL1At_eq_sum_range (A + B) x hAB, polyL1At_eq_sum_range A x hA,polyL1At_eq_sum_range B x hB, ← Finset.sum_add_distrib]apply Finset.sum_le_sumintro j _simp only [Polynomial.coeff_add, Polynomial.eval_add]exact Int.natAbs_add_le _ _private lemma polyL1At_sub_le (A B : Polynomial (Polynomial ℤ)) (x : ℤ) :polyL1At (A - B) x ≤ polyL1At A x + polyL1At B x := byset N := max (A.natDegree + 1) (B.natDegree + 1)have hA : ∀ j ∈ A.support, j < N := fun j hj => byhave := Polynomial.le_natDegree_of_mem_supp j hj; omegahave hB : ∀ j ∈ B.support, j < N := fun j hj => byhave := Polynomial.le_natDegree_of_mem_supp j hj; omegahave hAB : ∀ j ∈ (A - B).support, j < N := fun j hj => byhave h1 := Polynomial.le_natDegree_of_mem_supp j hjhave h2 := Polynomial.natDegree_sub_le A Bomegarw [polyL1At_eq_sum_range (A - B) x hAB, polyL1At_eq_sum_range A x hA,polyL1At_eq_sum_range B x hB, ← Finset.sum_add_distrib]apply Finset.sum_le_sumintro j _simp only [Polynomial.coeff_sub, Polynomial.eval_sub]simpa [sub_eq_add_neg, Int.natAbs_neg] using Int.natAbs_add_le ((A.coeff j).eval x) (-((B.coeff j).eval x))private lemma polyL1At_mul_le (A B : Polynomial (Polynomial ℤ)) (x : ℤ) :polyL1At (A * B) x ≤ polyL1At A x * polyL1At B x := byset Na := A.natDegree + 1set Nb := B.natDegree + 1have hA : ∀ j ∈ A.support, j < Na := fun j hj => byhave := Polynomial.le_natDegree_of_mem_supp j hj; omegahave hB : ∀ j ∈ B.support, j < Nb := fun j hj => byhave := Polynomial.le_natDegree_of_mem_supp j hj; omegahave hAB : ∀ j ∈ (A * B).support, j < Na + Nb := fun j hj => byhave h1 := Polynomial.le_natDegree_of_mem_supp j hjhave h2 := Polynomial.natDegree_mul_le (p := A) (q := B)omegarw [polyL1At_eq_sum_range (A * B) x hAB, polyL1At_eq_sum_range A x hA,polyL1At_eq_sum_range B x hB, Finset.sum_mul_sum]have htri : ∀ (s : Finset (ℕ × ℕ)) (f : ℕ × ℕ → ℤ),(∑ p ∈ s, f p).natAbs ≤ ∑ p ∈ s, (f p).natAbs := byintro s finduction s using Finset.induction_on with| empty => simp| @insert u s' hu ihs =>rw [Finset.sum_insert hu, Finset.sum_insert hu]exact le_trans (Int.natAbs_add_le _ _) (Nat.add_le_add_left ihs _)calc ∑ j ∈ Finset.range (Na + Nb), (((A * B).coeff j).eval x).natAbs≤ ∑ j ∈ Finset.range (Na + Nb),∑ p ∈ Finset.antidiagonal j, ((A.coeff p.1).eval x).natAbs * ((B.coeff p.2).eval x).natAbs := byapply Finset.sum_le_sumintro j _rw [Polynomial.coeff_mul, ← Polynomial.coe_evalRingHom, map_sum]calc (∑ p ∈ Finset.antidiagonal j, (Polynomial.evalRingHom x) (A.coeff p.1 * B.coeff p.2)).natAbs≤ ∑ p ∈ Finset.antidiagonal j, ((Polynomial.evalRingHom x) (A.coeff p.1 * B.coeff p.2)).natAbs :=htri _ __ = ∑ p ∈ Finset.antidiagonal j, ((A.coeff p.1).eval x).natAbs * ((B.coeff p.2).eval x).natAbs := byapply Finset.sum_congr rfl; intro p _simp [Int.natAbs_mul]_ ≤ ∑ p ∈ Finset.range Na ×ˢ Finset.range Nb,((A.coeff p.1).eval x).natAbs * ((B.coeff p.2).eval x).natAbs := by-- Every term with p.1 ≥ Na or p.2 ≥ Nb vanishes!rw [← Finset.sum_biUnion]· apply Finset.sum_le_sum_of_ne_zerointro p hp hnehave hp1 : p.1 < Na := byby_contra hhave h0 : A.coeff p.1 = 0 := Polynomial.coeff_eq_zero_of_natDegree_lt (by omega)simp [h0] at hnehave hp2 : p.2 < Nb := byby_contra hhave h0 : B.coeff p.2 = 0 := Polynomial.coeff_eq_zero_of_natDegree_lt (by omega)simp [h0] at hneexact Finset.mem_product.mpr ⟨Finset.mem_range.mpr hp1, Finset.mem_range.mpr hp2⟩· intro i _ j _ hijexact Finset.disjoint_left.mpr fun p hpi hpj =>hij ((Finset.mem_antidiagonal.mp hpi).symm.trans (Finset.mem_antidiagonal.mp hpj))_ = ∑ i ∈ Finset.range Na, ∑ j ∈ Finset.range Nb,((A.coeff i).eval x).natAbs * ((B.coeff j).eval x).natAbs := Finset.sum_product _ _ _/-- Maximum wire magnitude (at least `2`) of `P` up to gate `t` (`t ≤ τ`) at integer `x`. -/noncomputable def wireHeight {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t ≤ τ) (x : ℤ) : ℕ :=max 2 (Finset.univ.sup (fun i : Fin (t + 2) => ((P.prefix t ht).evalWire i).eval x |>.natAbs))lemma two_le_wireHeight {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t ≤ τ) (x : ℤ) :2 ≤ wireHeight P t ht x := le_max_left _ _lemma wire_natAbs_le_wireHeight {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t ≤ τ) (x : ℤ)(w : Fin (t + 2)) :((P.evalWire ⟨w.val, by omega⟩).eval x).natAbs ≤ wireHeight P t ht x := byrw [← P.evalWire_prefix t ht w]exact le_trans (Finset.le_sup (f := fun i : Fin (t + 2) => ((P.prefix t ht).evalWire i).eval x |>.natAbs)(Finset.mem_univ w)) (le_max_right _ _)/-- Property 3 (`ℓ₁`-Norm Bound): `polyL1At (evalFormalT ... w) x ≤ (wireHeight P a _ x) ^ (2 ^ (t - b))`. -/lemma evalFormalT_l1_le {τ : ℕ} (P : SLP τ) (a b : ℕ) (hab : a < b) (hb : b ≤ τ) (x : ℤ) :∀ (t : ℕ) (hbt : b ≤ t) (ht : t ≤ τ) (w : Fin (t + 2)),polyL1At (evalFormalT P a b hab hb t hbt ht w) x ≤(wireHeight P a (by omega) x) ^ (2 ^ (t - b)) := byintro tinduction t using Nat.strong_induction_on with| ind t ih =>intro hbt ht wset H := wireHeight P a (by omega) xhave hH2 : 2 ≤ H := two_le_wireHeight P a (by omega) xhave hpow1 : 1 ≤ 2 ^ (t - b) := Nat.one_le_pow _ _ (by omega)have hH_le_pow : H ≤ H ^ (2 ^ (t - b)) := Nat.le_self_pow (by omega) Hrw [evalFormalT.eq_def]split_ifs with hw hwa hwb· have hC : polyL1At (Polynomial.C (P.evalWire ⟨w.val, by omega⟩)) x ≤((P.evalWire ⟨w.val, by omega⟩).eval x).natAbs := byunfold polyL1Atby_cases h0 : P.evalWire ⟨w.val, by omega⟩ = 0· simp [h0]· rw [Polynomial.support_C h0]; simpexact le_trans hC (le_trans (wire_natAbs_le_wireHeight P a (by omega) x ⟨w.val, by omega⟩) hH_le_pow)· simp [polyL1At]· have hX : polyL1At (Polynomial.X : Polynomial (Polynomial ℤ)) x = 1 := byunfold polyL1Atrw [Polynomial.support_X]simprw [hX]; omega· set t' := w.val - 2have ht'_lt_t : t' < t := by omegahave hbt' : b ≤ t' := by omegahave ht'_lt_τ : t' < τ := by omegaset A := evalFormalT P a b hab hb t' hbt' (le_of_lt ht'_lt_τ) (P.gateIn1 t' ht'_lt_τ)set B := evalFormalT P a b hab hb t' hbt' (le_of_lt ht'_lt_τ) (P.gateIn2 t' ht'_lt_τ)have hA : polyL1At A x ≤ H ^ (2 ^ (t' - b)) :=ih t' ht'_lt_t hbt' (le_of_lt ht'_lt_τ) (P.gateIn1 t' ht'_lt_τ)have hB : polyL1At B x ≤ H ^ (2 ^ (t' - b)) :=ih t' ht'_lt_t hbt' (le_of_lt ht'_lt_τ) (P.gateIn2 t' ht'_lt_τ)have h2_le_Hpow : 2 ≤ H ^ (2 ^ (t' - b)) :=le_trans hH2 (Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) H)have hsq_le : H ^ (2 ^ (t' - b)) * H ^ (2 ^ (t' - b)) ≤ H ^ (2 ^ (t - b)) := byrw [← pow_add, ← two_mul]apply Nat.pow_le_pow_right (by omega)have : (t' - b) + 1 ≤ t - b := by omegahave := Nat.pow_le_pow_right (by omega : 1 ≤ 2) thisrwa [pow_succ, Nat.mul_comm] at thischange polyL1At ((P.gateOp t' ht'_lt_τ).applyPolyT A B) x ≤ H ^ (2 ^ (t - b))cases P.gateOp t' ht'_lt_τ with| add =>simp only [SLPOp.applyPolyT]have h1 := polyL1At_add_le A B xhave h2 : H ^ (2 ^ (t' - b)) + H ^ (2 ^ (t' - b)) ≤ H ^ (2 ^ (t' - b)) * H ^ (2 ^ (t' - b)) := bynlinarithexact le_trans h1 (le_trans (Nat.add_le_add hA hB) (le_trans h2 hsq_le))| sub =>simp only [SLPOp.applyPolyT]have h1 := polyL1At_sub_le A B xhave h2 : H ^ (2 ^ (t' - b)) + H ^ (2 ^ (t' - b)) ≤ H ^ (2 ^ (t' - b)) * H ^ (2 ^ (t' - b)) := bynlinarithexact le_trans h1 (le_trans (Nat.add_le_add hA hB) (le_trans h2 hsq_le))| mul =>simp only [SLPOp.applyPolyT]exact le_trans (polyL1At_mul_le A B x) (le_trans (Nat.mul_le_mul hA hB) hsq_le)/-- Property 4 (Next-Level Height Bound): for any untapped segment `seg` and any `c` with`seg.b ≤ c ≤ τ`, `wireHeight P c _ x ≤ (max (wireHeight P seg.a _ x) |U(x)|) ^ (2 ^ (c - seg.b))`. -/lemma wireHeight_step_le {τ : ℕ} (P : SLP τ) (seg : UntappedSeg P) (x : ℤ) :∀ (c : ℕ) (hbc : seg.b ≤ c) (hc : c ≤ τ),wireHeight P c hc x ≤(max (wireHeight P seg.a (by have := seg.hab; have := seg.hb; omega) x)((P.evalWire ⟨seg.b + 1, by have := seg.hb; omega⟩).eval x).natAbs) ^ (2 ^ (c - seg.b)) := byintro c hbc hcset M := max (wireHeight P seg.a (by have := seg.hab; have := seg.hb; omega) x)((P.evalWire ⟨seg.b + 1, by have := seg.hb; omega⟩).eval x).natAbshave hM2 : 2 ≤ M := le_trans (two_le_wireHeight P seg.a (by have := seg.hab; have := seg.hb; omega) x) (le_max_left _ _)have hpow1 : 1 ≤ 2 ^ (c - seg.b) := Nat.one_le_pow _ _ (by omega)have hM_le_pow : M ≤ M ^ (2 ^ (c - seg.b)) := Nat.le_self_pow (by omega) M-- Prove by strong induction on w.val < c + 2 that |wire w(x)| ≤ M ^ (2 ^ (max w.val (seg.b + 1) - (seg.b + 1)))have hwire : ∀ (k : ℕ) (hk : k < c + 2),((P.evalWire ⟨k, by omega⟩).eval x).natAbs ≤M ^ (2 ^ (k - (seg.b + 1))) := byintro kinduction k using Nat.strong_induction_on with| ind k ihk =>intro hkby_cases hle_a : k ≤ seg.a + 1· have hsub : k - (seg.b + 1) = 0 := by have := seg.hab; omegarw [hsub, pow_zero, pow_one]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 _ _)· by_cases hle_b : k ≤ seg.b· -- Strict intermediate wire of seg: equals w_in ^ (2 ^ (k - seg.a - 1))!-- Note |w_in(x)| ^ (2^(k - seg.a - 1)) ≤ max 1 |w_in(x)| ^ (2^(seg.b - seg.a)) = max 1 |U(x)| ≤ M!have hsub : k - (seg.b + 1) = 0 := by omegarw [hsub, pow_zero, pow_one]have hrun_k := P.topSq_run_eval (a := seg.a) (b := k - 1) (by omega) (by omega)(fun t ht h1 h2 => seg.hrun t ht h1 (by omega))have hrun_b := P.topSq_run_eval seg.hab seg.hb seg.hrunset win := P.evalWire ⟨(P.gateIn1 seg.a (by omega)).val, by have := (P.gateIn1 seg.a (by omega)).isLt; omega⟩have hk_eq : k - 1 + 1 = k := by omegahave hval_k : ((P.evalWire ⟨k, by omega⟩).eval x).natAbs =(win.eval x).natAbs ^ (2 ^ (k - 1 - seg.a)) := byhave := congrArg (fun p : Polynomial ℤ => (p.eval x).natAbs) hrun_k.1simpa [hk_eq, Int.natAbs_pow, win] using thishave hval_b : ((P.evalWire ⟨seg.b + 1, by omega⟩).eval x).natAbs =(win.eval x).natAbs ^ (2 ^ (seg.b - seg.a)) := byhave := congrArg (fun p : Polynomial ℤ => (p.eval x).natAbs) hrun_b.1simpa [Int.natAbs_pow, win] using thisrw [hval_k]by_cases hw0 : (win.eval x).natAbs = 0· rw [hw0, zero_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega)))]; omega· have h1 : (win.eval x).natAbs ^ (2 ^ (k - 1 - seg.a)) ≤(win.eval x).natAbs ^ (2 ^ (seg.b - seg.a)) :=Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) (by omega))rw [← hval_b] at h1exact le_trans h1 (le_max_right _ _)· by_cases heq_b : k = seg.b + 1· subst heq_bsimp only [Nat.sub_self, pow_zero, pow_one]exact le_max_right _ _· -- k ≥ seg.b + 2: gate t' = k - 2set t' := k - 2have ht'_lt : t' < τ := by omegahave hgate := P.evalWire_gate t' ht'_lthave hk_eq : t' + 2 = k := by omegahave hin1_lt : (P.gateIn1 t' ht'_lt).val < k := by have := (P.gateIn1 t' ht'_lt).isLt; omegahave hin2_lt : (P.gateIn2 t' ht'_lt).val < k := by have := (P.gateIn2 t' ht'_lt).isLt; omegahave h1 := ihk (P.gateIn1 t' ht'_lt).val hin1_lt (by omega)have h2 := ihk (P.gateIn2 t' ht'_lt).val hin2_lt (by omega)have he1 : (P.gateIn1 t' ht'_lt).val - (seg.b + 1) ≤ k - (seg.b + 1) - 1 := by omegahave he2 : (P.gateIn2 t' ht'_lt).val - (seg.b + 1) ≤ k - (seg.b + 1) - 1 := by omegahave h1' : ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x).natAbs ≤M ^ (2 ^ (k - (seg.b + 1) - 1)) :=le_trans h1 (Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) he1))have h2' : ((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x).natAbs ≤M ^ (2 ^ (k - (seg.b + 1) - 1)) :=le_trans h2 (Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) he2))have hsq : M ^ (2 ^ (k - (seg.b + 1) - 1)) * M ^ (2 ^ (k - (seg.b + 1) - 1)) =M ^ (2 ^ (k - (seg.b + 1))) := byrw [← pow_add, ← two_mul]congr 1conv_rhs => rw [← Nat.sub_add_cancel (by omega : 1 ≤ k - (seg.b + 1)), pow_succ, Nat.mul_comm]have h2_le : 2 ≤ M ^ (2 ^ (k - (seg.b + 1) - 1)) :=le_trans hM2 (Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) M)have heval : (P.evalWire ⟨k, by omega⟩).eval x =(P.gateOp t' ht'_lt).applyInt((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x)((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x) := byrw [SLPOp.applyInt_eq_eval]simpa [hk_eq] using congrArg (fun p : Polynomial ℤ => p.eval x) hgaterw [heval]cases P.gateOp t' ht'_lt with| add =>simp only [SLPOp.applyInt]have := Int.natAbs_add_le((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x)((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x)nlinarith| sub =>simp only [SLPOp.applyInt]have := Int.natAbs_add_le((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x)(-((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x))rw [Int.natAbs_neg] at thishave hsub_eq : ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x) -((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x) =((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x) +(-((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x)) := by ringrw [hsub_eq]; nlinarith| mul =>simp only [SLPOp.applyInt, Int.natAbs_mul]rw [← hsq]; exact Nat.mul_le_mul h1' h2'unfold wireHeightapply max_le (le_trans hM2 hM_le_pow)apply Finset.sup_leintro i _rw [P.evalWire_prefix c hc i]have hi_le : i.val - (seg.b + 1) ≤ c - seg.b := by have := i.isLt; omegaexact le_trans (hwire i.val i.isLt)(Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) hi_le))/-- Base height bound before segment `a`: `wireHeight P a _ x ≤ (max 2 |x|) ^ (2 ^ a)`. -/lemma wireHeight_base_le {τ : ℕ} (P : SLP τ) (a : ℕ) (ha : a ≤ τ) (x : ℤ) :wireHeight P a ha x ≤ (max 2 x.natAbs) ^ (2 ^ a) := byset M := max 2 x.natAbshave hM2 : 2 ≤ M := le_max_left _ _have hwire : ∀ (k : ℕ) (hk : k < a + 2),((P.evalWire ⟨k, by omega⟩).eval x).natAbs ≤ M ^ (2 ^ (k - 1)) := byintro kinduction k using Nat.strong_induction_on with| ind k ihk =>intro hkmatch k with| 0 =>have h0 : P.evalWire ⟨0, by omega⟩ = 1 := P.evalWire_zerorw [h0]; simp; omega| 1 =>have h1 : P.evalWire ⟨1, by omega⟩ = Polynomial.X := byhave hpr := P.evalWire_prefix 0 (Nat.zero_le τ) ⟨1, by omega⟩have hnil : P.prefix 0 (Nat.zero_le τ) = .nil := by cases P.prefix 0 (Nat.zero_le τ); rflrw [hnil] at hprsimpa [SLP.evalWire] using hpr.symmrw [h1]; simp [M]| t' + 2 =>have ht'_lt : t' < τ := by omegahave hgate := P.evalWire_gate t' ht'_lthave hin1_lt : (P.gateIn1 t' ht'_lt).val < t' + 2 := (P.gateIn1 t' ht'_lt).isLthave hin2_lt : (P.gateIn2 t' ht'_lt).val < t' + 2 := (P.gateIn2 t' ht'_lt).isLthave h1 := ihk (P.gateIn1 t' ht'_lt).val hin1_lt (by omega)have h2 := ihk (P.gateIn2 t' ht'_lt).val hin2_lt (by omega)have h1' : ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x).natAbs ≤ M ^ (2 ^ t') :=le_trans h1 (Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) (by omega)))have h2' : ((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x).natAbs ≤ M ^ (2 ^ t') :=le_trans h2 (Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) (by omega)))have hsq : M ^ (2 ^ t') * M ^ (2 ^ t') = M ^ (2 ^ (t' + 1)) := byrw [← pow_add, ← two_mul, pow_succ, Nat.mul_comm]have h2_le : 2 ≤ M ^ (2 ^ t') :=le_trans hM2 (Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) M)have heval : (P.evalWire ⟨t' + 2, by omega⟩).eval x =(P.gateOp t' ht'_lt).applyInt((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x)((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x) := byrw [SLPOp.applyInt_eq_eval]exact congrArg (fun p : Polynomial ℤ => p.eval x) hgaterw [heval]have hsub : t' + 2 - 1 = t' + 1 := by omegarw [hsub]cases P.gateOp t' ht'_lt with| add =>simp only [SLPOp.applyInt]have := Int.natAbs_add_le((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x)((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x)nlinarith| sub =>simp only [SLPOp.applyInt]have := Int.natAbs_add_le((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x)(-((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x))rw [Int.natAbs_neg] at thishave hsub_eq : ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x) -((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x) =((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x) +(-((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x)) := by ringrw [hsub_eq]; nlinarith| mul =>simp only [SLPOp.applyInt, Int.natAbs_mul]rw [← hsq]; exact Nat.mul_le_mul h1' h2'unfold wireHeightapply max_le (le_trans hM2 (Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) M))apply Finset.sup_leintro i _rw [P.evalWire_prefix a ha i]have hi_le : i.val - 1 ≤ a := by have := i.isLt; omegaexact le_trans (hwire i.val i.isLt)(Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) hi_le))/-- Coefficient `ℓ₁`-norm of `f ∈ ℤ[X]`: `∑ j ∈ f.support, (f.coeff j).natAbs`. -/noncomputable def polyIntL1 (f : Polynomial ℤ) : ℕ :=∑ j ∈ f.support, (f.coeff j).natAbsprivate lemma polyIntL1_eq_sum_range (f : Polynomial ℤ) {N : ℕ} (hN : ∀ j ∈ f.support, j < N) :polyIntL1 f = ∑ j ∈ Finset.range N, (f.coeff j).natAbs := byunfold polyIntL1apply Finset.sum_subset (fun j hj => Finset.mem_range.mpr (hN j hj))intro j _ hjhave : f.coeff j = 0 := Polynomial.notMem_support_iff.mp hjsimp [this]private lemma polyIntL1_add_le (f g : Polynomial ℤ) :polyIntL1 (f + g) ≤ polyIntL1 f + polyIntL1 g := byset N := max (f.natDegree + 1) (g.natDegree + 1)have hf : ∀ j ∈ f.support, j < N := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omegahave hg : ∀ j ∈ g.support, j < N := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omegahave hfg : ∀ j ∈ (f + g).support, j < N := fun j hj => byhave h1 := Polynomial.le_natDegree_of_mem_supp j hjhave h2 := Polynomial.natDegree_add_le f g; omegarw [polyIntL1_eq_sum_range (f + g) hfg, polyIntL1_eq_sum_range f hf,polyIntL1_eq_sum_range g hg, ← Finset.sum_add_distrib]apply Finset.sum_le_sum; intro j _simp only [Polynomial.coeff_add]exact Int.natAbs_add_le _ _private lemma polyIntL1_sub_le (f g : Polynomial ℤ) :polyIntL1 (f - g) ≤ polyIntL1 f + polyIntL1 g := byset N := max (f.natDegree + 1) (g.natDegree + 1)have hf : ∀ j ∈ f.support, j < N := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omegahave hg : ∀ j ∈ g.support, j < N := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omegahave hfg : ∀ j ∈ (f - g).support, j < N := fun j hj => byhave h1 := Polynomial.le_natDegree_of_mem_supp j hjhave h2 := Polynomial.natDegree_sub_le f g; omegarw [polyIntL1_eq_sum_range (f - g) hfg, polyIntL1_eq_sum_range f hf,polyIntL1_eq_sum_range g hg, ← Finset.sum_add_distrib]apply Finset.sum_le_sum; intro j _simp only [Polynomial.coeff_sub]simpa [sub_eq_add_neg, Int.natAbs_neg] using Int.natAbs_add_le (f.coeff j) (-(g.coeff j))private lemma polyIntL1_mul_le (f g : Polynomial ℤ) :polyIntL1 (f * g) ≤ polyIntL1 f * polyIntL1 g := byset Nf := f.natDegree + 1set Ng := g.natDegree + 1have hf : ∀ j ∈ f.support, j < Nf := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omegahave hg : ∀ j ∈ g.support, j < Ng := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omegahave hfg : ∀ j ∈ (f * g).support, j < Nf + Ng := fun j hj => byhave h1 := Polynomial.le_natDegree_of_mem_supp j hjhave h2 := Polynomial.natDegree_mul_le (p := f) (q := g); omegarw [polyIntL1_eq_sum_range (f * g) hfg, polyIntL1_eq_sum_range f hf,polyIntL1_eq_sum_range g hg, Finset.sum_mul_sum]have htri : ∀ (s : Finset (ℕ × ℕ)) (h : ℕ × ℕ → ℤ),(∑ p ∈ s, h p).natAbs ≤ ∑ p ∈ s, (h p).natAbs := byintro s hinduction s using Finset.induction_on with| empty => simp| @insert u s' hu ihs =>rw [Finset.sum_insert hu, Finset.sum_insert hu]exact le_trans (Int.natAbs_add_le _ _) (Nat.add_le_add_left ihs _)calc ∑ j ∈ Finset.range (Nf + Ng), ((f * g).coeff j).natAbs≤ ∑ j ∈ Finset.range (Nf + Ng),∑ p ∈ Finset.antidiagonal j, (f.coeff p.1).natAbs * (g.coeff p.2).natAbs := byapply Finset.sum_le_sum; intro j _rw [Polynomial.coeff_mul]calc (∑ p ∈ Finset.antidiagonal j, f.coeff p.1 * g.coeff p.2).natAbs≤ ∑ p ∈ Finset.antidiagonal j, (f.coeff p.1 * g.coeff p.2).natAbs := htri _ __ = ∑ p ∈ Finset.antidiagonal j, (f.coeff p.1).natAbs * (g.coeff p.2).natAbs := byapply Finset.sum_congr rfl; intro p _; exact Int.natAbs_mul _ __ ≤ ∑ p ∈ Finset.range Nf ×ˢ Finset.range Ng, (f.coeff p.1).natAbs * (g.coeff p.2).natAbs := byrw [← Finset.sum_biUnion]· apply Finset.sum_le_sum_of_ne_zerointro p _ hnehave hp1 : p.1 < Nf := byby_contra hhave h0 : f.coeff p.1 = 0 := Polynomial.coeff_eq_zero_of_natDegree_lt (by omega)simp [h0] at hnehave hp2 : p.2 < Ng := byby_contra hhave h0 : g.coeff p.2 = 0 := Polynomial.coeff_eq_zero_of_natDegree_lt (by omega)simp [h0] at hneexact Finset.mem_product.mpr ⟨Finset.mem_range.mpr hp1, Finset.mem_range.mpr hp2⟩· intro i _ j _ hijexact Finset.disjoint_left.mpr fun p hpi hpj =>hij ((Finset.mem_antidiagonal.mp hpi).symm.trans (Finset.mem_antidiagonal.mp hpj))_ = ∑ i ∈ Finset.range Nf, ∑ j ∈ Finset.range Ng,(f.coeff i).natAbs * (g.coeff j).natAbs := Finset.sum_product _ _ _/-- Every wire of `P : SLP τ` has coefficient `ℓ₁`-norm at most `2 ^ (2 ^ τ)`. -/lemma SLP.wire_polyIntL1_le :∀ {τ : ℕ} (P : SLP τ) (w : Fin (τ + 2)), polyIntL1 (P.evalWire w) ≤ 2 ^ (2 ^ τ)| 0, .nil, w => bysimp only [SLP.evalWire]split_ifs· have : polyIntL1 (1 : Polynomial ℤ) = 1 := byunfold polyIntL1rw [← Polynomial.C_1, Polynomial.support_C (by norm_num : (1 : ℤ) ≠ 0)]simprw [this]; omega· have : polyIntL1 (Polynomial.X : Polynomial ℤ) = 1 := byunfold polyIntL1; rw [Polynomial.support_X]; simprw [this]; omega| τ + 1, .snoc P op i j, w => byhave ih1 := P.wire_polyIntL1_le ihave ih2 := P.wire_polyIntL1_le jhave hsq : 2 ^ (2 ^ τ) * 2 ^ (2 ^ τ) = 2 ^ (2 ^ (τ + 1)) := byrw [← pow_add, ← two_mul, pow_succ, Nat.mul_comm]have h2_le : 2 ≤ 2 ^ (2 ^ τ) :=Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) 2simp only [SLP.evalWire]split_ifs with hw· exact le_trans (P.wire_polyIntL1_le ⟨w.val, hw⟩)(Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) (by omega)))· cases op with| add =>simp only [SLPOp.applyPoly]have := polyIntL1_add_le (P.evalWire i) (P.evalWire j)nlinarith| sub =>simp only [SLPOp.applyPoly]have := polyIntL1_sub_le (P.evalWire i) (P.evalWire j)nlinarith| mul =>simp only [SLPOp.applyPoly]rw [← hsq]; exact le_trans (polyIntL1_mul_le _ _) (Nat.mul_le_mul ih1 ih2)/-! ## 4. Cauchy Bounds, Backward Propagation & Multi-Segment Gap-Dominance Theorem -/private lemma eval_polyT_eq (F : Polynomial (Polynomial ℤ)) (u : Polynomial ℤ) (x : ℤ) :(F.eval u).eval x = ∑ j ∈ Finset.range (F.natDegree + 1), (F.coeff j).eval x * (u.eval x) ^ j := byrw [Polynomial.eval_eq_sum_range (p := F), ← Polynomial.coe_evalRingHom, map_sum]apply Finset.sum_congr rfl; intro j _simp/-- Triangle bound on lower terms `j < D` of `F(x, U)`:`|∑_{j < D} c_j(x) * U^j| ≤ |U|^(D - 1) * polyL1At F x` whenever `1 ≤ D` and `1 ≤ |U|`. -/private lemma lower_terms_bound (F : Polynomial (Polynomial ℤ)) (x U : ℤ)(hD : 1 ≤ F.natDegree) (hU : 1 ≤ U.natAbs) :(∑ j ∈ Finset.range F.natDegree, (F.coeff j).eval x * U ^ j).natAbs ≤U.natAbs ^ (F.natDegree - 1) * polyL1At F x := byhave htri : ∀ (s : Finset ℕ) (g : ℕ → ℤ),(∑ j ∈ s, g j).natAbs ≤ ∑ j ∈ s, (g j).natAbs := byintro s ginduction s using Finset.induction_on with| empty => simp| @insert u s' hu ihs =>rw [Finset.sum_insert hu, Finset.sum_insert hu]exact le_trans (Int.natAbs_add_le _ _) (Nat.add_le_add_left ihs _)have hsum_le : ∑ j ∈ Finset.range F.natDegree, ((F.coeff j).eval x).natAbs ≤ polyL1At F x := byrw [polyL1At_eq_sum_range F x (N := F.natDegree + 1) (fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega)]rw [Finset.sum_range_succ]; omegacalc (∑ j ∈ Finset.range F.natDegree, (F.coeff j).eval x * U ^ j).natAbs≤ ∑ j ∈ Finset.range F.natDegree, ((F.coeff j).eval x * U ^ j).natAbs := htri _ __ = ∑ j ∈ Finset.range F.natDegree, ((F.coeff j).eval x).natAbs * U.natAbs ^ j := byapply Finset.sum_congr rfl; intro j _rw [Int.natAbs_mul, Int.natAbs_pow]_ ≤ ∑ j ∈ Finset.range F.natDegree, ((F.coeff j).eval x).natAbs * U.natAbs ^ (F.natDegree - 1) := byapply Finset.sum_le_sum; intro j hjhave hj_le : j ≤ F.natDegree - 1 := by have := Finset.mem_range.mp hj; omegaexact Nat.mul_le_mul_left _ (Nat.pow_le_pow_right hU hj_le)_ = (∑ j ∈ Finset.range F.natDegree, ((F.coeff j).eval x).natAbs) * U.natAbs ^ (F.natDegree - 1) :=(Finset.sum_mul _ _ _).symm_ ≤ polyL1At F x * U.natAbs ^ (F.natDegree - 1) := Nat.mul_le_mul_right _ hsum_le_ = U.natAbs ^ (F.natDegree - 1) * polyL1At F x := Nat.mul_comm _ _/-- **Cauchy Root Bound for `F(x, U) = 0`:** If `F.natDegree ≥ 1`, `(F.leadingCoeff).eval x ≠ 0`,and `(F.eval u).eval x = 0`, then `|u(x)| ≤ polyL1At F x`. -/private lemma cauchy_root_bound_polyT (F : Polynomial (Polynomial ℤ)) (u : Polynomial ℤ) (x : ℤ)(hD : 1 ≤ F.natDegree) (hlead : (F.leadingCoeff).eval x ≠ 0)(hzero : (F.eval u).eval x = 0) :(u.eval x).natAbs ≤ polyL1At F x := byset U := u.eval xby_cases hU0 : U.natAbs = 0· omegahave hU1 : 1 ≤ U.natAbs := by omegahave hsum := eval_polyT_eq F u xrw [hzero, Finset.sum_range_succ] at hsumset low := ∑ j ∈ Finset.range F.natDegree, (F.coeff j).eval x * U ^ jhave heq : (F.coeff F.natDegree).eval x * U ^ F.natDegree = -low := by linarithhave hlead_pos : 1 ≤ ((F.coeff F.natDegree).eval x).natAbs := Int.natAbs_pos.mpr hleadhave hpow_split : U.natAbs ^ F.natDegree = U.natAbs ^ (F.natDegree - 1) * U.natAbs := byconv_lhs => rw [← Nat.sub_add_cancel hD, pow_succ]have hlow := lower_terms_bound F x U hD hU1have h1 : U.natAbs ^ F.natDegree ≤ low.natAbs := bycalc U.natAbs ^ F.natDegree= 1 * U.natAbs ^ F.natDegree := (Nat.one_mul _).symm_ ≤ ((F.coeff F.natDegree).eval x).natAbs * U.natAbs ^ F.natDegree :=Nat.mul_le_mul_right _ hlead_pos_ = ((F.coeff F.natDegree).eval x * U ^ F.natDegree).natAbs := byrw [Int.natAbs_mul, Int.natAbs_pow]_ = (-low).natAbs := by rw [heq]_ = low.natAbs := Int.natAbs_neg _rw [hpow_split] at h1have hpos : 0 < U.natAbs ^ (F.natDegree - 1) := Nat.pow_pos hU1exact Nat.le_of_mul_le_mul_left (le_trans h1 hlow) hpos/-- **Cauchy Dominance Lower Bound:** If `F.natDegree ≥ 1`, `(F.leadingCoeff).eval x ≠ 0`, and`2 * polyL1At F x < |u(x)|`, then `|u(x)| ≤ 2 * |(F.eval u).eval x|`. -/private lemma cauchy_dominance_polyT (F : Polynomial (Polynomial ℤ)) (u : Polynomial ℤ) (x : ℤ)(hD : 1 ≤ F.natDegree) (hlead : (F.leadingCoeff).eval x ≠ 0)(hbig : 2 * polyL1At F x < (u.eval x).natAbs) :(u.eval x).natAbs ≤ 2 * ((F.eval u).eval x).natAbs := byset U := u.eval xhave hU1 : 1 ≤ U.natAbs := by omegahave hsum := eval_polyT_eq F u xrw [Finset.sum_range_succ] at hsumset low := ∑ j ∈ Finset.range F.natDegree, (F.coeff j).eval x * U ^ jset top := (F.coeff F.natDegree).eval x * U ^ F.natDegreehave hlead_pos : 1 ≤ ((F.coeff F.natDegree).eval x).natAbs := Int.natAbs_pos.mpr hleadhave htop_ge : U.natAbs ^ F.natDegree ≤ top.natAbs := bycalc U.natAbs ^ F.natDegree= 1 * U.natAbs ^ F.natDegree := (Nat.one_mul _).symm_ ≤ ((F.coeff F.natDegree).eval x).natAbs * U.natAbs ^ F.natDegree :=Nat.mul_le_mul_right _ hlead_pos_ = top.natAbs := by simp [top, Int.natAbs_mul, Int.natAbs_pow]have htri : top.natAbs ≤ ((F.eval u).eval x).natAbs + low.natAbs := byhave hrew : top = (F.eval u).eval x + (-low) := by linarithcalc top.natAbs = ((F.eval u).eval x + (-low)).natAbs := by rw [← hrew]_ ≤ ((F.eval u).eval x).natAbs + (-low).natAbs := Int.natAbs_add_le _ __ = ((F.eval u).eval x).natAbs + low.natAbs := by rw [Int.natAbs_neg]have hlow := lower_terms_bound F x U hD hU1have hpow_split : U.natAbs ^ F.natDegree = U.natAbs ^ (F.natDegree - 1) * U.natAbs := byconv_lhs => rw [← Nat.sub_add_cancel hD, pow_succ]have h1 : U.natAbs ^ (F.natDegree - 1) * (2 * polyL1At F x) ≤ U.natAbs ^ (F.natDegree - 1) * U.natAbs :=Nat.mul_le_mul_left _ (le_of_lt hbig)have h2 : 2 * low.natAbs ≤ U.natAbs ^ F.natDegree := byrw [hpow_split]; linarithhave h3 : U.natAbs ^ F.natDegree ≤ 2 * ((F.eval u).eval x).natAbs := by omegahave h4 : U.natAbs ≤ U.natAbs ^ F.natDegree := Nat.le_self_pow (by omega) U.natAbsomega/-- Cauchy dominance for a single univariate polynomial `w ∈ ℤ[X]` (`deg w ≥ 1`):if `2 * polyIntL1 w ≤ |x|`, then `|x| ≤ 2 * |w(x)|`. -/private lemma cauchy_dominance_univar (w : Polynomial ℤ) (x : ℤ)(hD : 1 ≤ w.natDegree) (hbig : 2 * polyIntL1 w ≤ x.natAbs) :x.natAbs ≤ 2 * (w.eval x).natAbs := byhave hx1 : 1 ≤ x.natAbs := byhave hne : w ≠ 0 := fun h0 => by simp [h0] at hDhave hlead : 1 ≤ (w.coeff w.natDegree).natAbs := Int.natAbs_pos.mpr (Polynomial.leadingCoeff_ne_zero.mpr hne)have hl1 : 1 ≤ polyIntL1 w := byrw [polyIntL1_eq_sum_range w (N := w.natDegree + 1) (fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega)]rw [Finset.sum_range_succ]; omegaomegahave hsum : w.eval x = (∑ j ∈ Finset.range w.natDegree, w.coeff j * x ^ j) +w.coeff w.natDegree * x ^ w.natDegree := byrw [Polynomial.eval_eq_sum_range, Finset.sum_range_succ]set low := ∑ j ∈ Finset.range w.natDegree, w.coeff j * x ^ jset top := w.coeff w.natDegree * x ^ w.natDegreehave hne : w ≠ 0 := fun h0 => by simp [h0] at hDhave hlead_pos : 1 ≤ (w.coeff w.natDegree).natAbs :=Int.natAbs_pos.mpr (Polynomial.leadingCoeff_ne_zero.mpr hne)have htop_ge : x.natAbs ^ w.natDegree ≤ top.natAbs := bycalc x.natAbs ^ w.natDegree= 1 * x.natAbs ^ w.natDegree := (Nat.one_mul _).symm_ ≤ (w.coeff w.natDegree).natAbs * x.natAbs ^ w.natDegree := Nat.mul_le_mul_right _ hlead_pos_ = top.natAbs := by simp [top, Int.natAbs_mul, Int.natAbs_pow]have htri : top.natAbs ≤ (w.eval x).natAbs + low.natAbs := byhave hrew : top = w.eval x + (-low) := by linarithcalc top.natAbs = (w.eval x + (-low)).natAbs := by rw [← hrew]_ ≤ (w.eval x).natAbs + (-low).natAbs := Int.natAbs_add_le _ __ = (w.eval x).natAbs + low.natAbs := by rw [Int.natAbs_neg]have htri_sum : ∀ (s : Finset ℕ) (g : ℕ → ℤ),(∑ j ∈ s, g j).natAbs ≤ ∑ j ∈ s, (g j).natAbs := byintro s ginduction s using Finset.induction_on with| empty => simp| @insert u s' hu ihs =>rw [Finset.sum_insert hu, Finset.sum_insert hu]exact le_trans (Int.natAbs_add_le _ _) (Nat.add_le_add_left ihs _)have hlow_sum : ∑ j ∈ Finset.range w.natDegree, (w.coeff j).natAbs ≤ polyIntL1 w := byrw [polyIntL1_eq_sum_range w (N := w.natDegree + 1) (fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega)]rw [Finset.sum_range_succ]; omegahave hlow : low.natAbs ≤ x.natAbs ^ (w.natDegree - 1) * polyIntL1 w := bycalc low.natAbs≤ ∑ j ∈ Finset.range w.natDegree, (w.coeff j * x ^ j).natAbs := htri_sum _ __ = ∑ j ∈ Finset.range w.natDegree, (w.coeff j).natAbs * x.natAbs ^ j := byapply Finset.sum_congr rfl; intro j _; rw [Int.natAbs_mul, Int.natAbs_pow]_ ≤ ∑ j ∈ Finset.range w.natDegree, (w.coeff j).natAbs * x.natAbs ^ (w.natDegree - 1) := byapply Finset.sum_le_sum; intro j hjexact Nat.mul_le_mul_left _ (Nat.pow_le_pow_right hx1 (by have := Finset.mem_range.mp hj; omega))_ = (∑ j ∈ Finset.range w.natDegree, (w.coeff j).natAbs) * x.natAbs ^ (w.natDegree - 1) :=(Finset.sum_mul _ _ _).symm_ ≤ polyIntL1 w * x.natAbs ^ (w.natDegree - 1) := Nat.mul_le_mul_right _ hlow_sum_ = x.natAbs ^ (w.natDegree - 1) * polyIntL1 w := Nat.mul_comm _ _have hpow_split : x.natAbs ^ w.natDegree = x.natAbs ^ (w.natDegree - 1) * x.natAbs := byconv_lhs => rw [← Nat.sub_add_cancel hD, pow_succ]have h1 : x.natAbs ^ (w.natDegree - 1) * (2 * polyIntL1 w) ≤ x.natAbs ^ (w.natDegree - 1) * x.natAbs :=Nat.mul_le_mul_left _ hbighave h2 : 2 * low.natAbs ≤ x.natAbs ^ w.natDegree := by rw [hpow_split]; linarithhave h3 : x.natAbs ^ w.natDegree ≤ 2 * (w.eval x).natAbs := by omegahave h4 : x.natAbs ≤ x.natAbs ^ w.natDegree := Nat.le_self_pow (by omega) x.natAbsomega/-- Pure-`ℕ` arithmetic helper: if `5 ≤ u`, `u ^ 2 ^ (g - 1) ≤ u ^ 2 ^ e` is impossible when `e + 2 ≤ g`,and `u ≤ 2 * v` implies `u ^ (2 ^ (g - 1)) ≤ v ^ (2 ^ g)` (since `4 ≤ u` implies `u ≤ (u / 2)^2`,actually `u * 4 ≤ u ^ 2`, so `(2 * v)^2 ≥ u^2 ≥ 4 * u` => `v^2 ≥ u`!). -/private lemma nat_half_sq_ge {u v : ℕ} (hu : 4 ≤ u) (huv : u ≤ 2 * v) : u ≤ v ^ 2 := bynlinarith/-- **Multi-Segment Gap-Dominance Theorem:**If `P : SLP τ` (`P.evalPoly ≠ 0`) admits a list `L` satisfying `ValidGapSegs Δ L`, then`intRootCount P.evalPoly ≤ (L.length + 3) * 2 ^ (τ - Δ)`. -/theorem slp_multi_segment_gap_dominance {τ Δ : ℕ} (P : SLP τ) (hP : P.evalPoly ≠ 0)(L : List (UntappedSeg P)) (hvalid : ValidGapSegs Δ L) :intRootCount P.evalPoly ≤ (L.length + 3) * 2 ^ (τ - Δ) := byclassicalrcases hvalid with ⟨hL_ne, hord, hΔ, hhead_gap, hmid_gap⟩have hq_pos : 1 ≤ L.length := by cases L <;> simp at hL_ne ⊢-- Helper: for r : Fin L.length, define seg r, nextA r, target wire wTarget r, and F r : Polynomial (Polynomial ℤ).let seg : Fin L.length → UntappedSeg P := fun r => L[r.val]'r.isLthave hseg_ord : ∀ (r : Fin L.length) (hr : r.val + 1 < L.length),(seg r).b ≤ (seg ⟨r.val + 1, hr⟩).a := byhave hgen : ∀ (M : List (UntappedSeg P)), SegsOrdered M →∀ (i : ℕ) (hi : i + 1 < M.length), (M[i]'(by omega)).b ≤ (M[i + 1]'hi).a := byintro Minduction M with| nil => intro _ i hi; simp at hi| cons s0 rest ih =>intro ho i hicases i with| zero =>cases rest with| nil => simp at hi| cons s1 rest' => exact ho.1| succ i' =>cases rest with| nil => simp at hi| cons s1 rest' => exact ih ho.2 i' (by simpa using hi)intro r hrexact hgen L hord r.val hrlet nextC : Fin L.length → ℕ := fun r =>if h : r.val + 1 < L.length then (seg ⟨r.val + 1, h⟩).a else τhave hnextC_ge : ∀ r : Fin L.length, (seg r).b ≤ nextC r := byintro r; dsimp only [nextC]split_ifs with h· exact hseg_ord r h· exact (seg r).hbhave hnextC_le : ∀ r : Fin L.length, nextC r ≤ τ := byintro r; dsimp only [nextC]split_ifs with h· exact le_of_lt (lt_of_lt_of_le (seg ⟨r.val + 1, h⟩).hab (seg ⟨r.val + 1, h⟩).hb)· exact le_rfllet wTarget : (r : Fin L.length) → Fin (nextC r + 2) := fun r =>if h : r.val + 1 < L.length thenlet sNext := seg ⟨r.val + 1, h⟩⟨(P.gateIn1 sNext.a (by have := sNext.hab; have := sNext.hb; omega)).val, byhave hlt := (P.gateIn1 sNext.a (by have := sNext.hab; have := sNext.hb; omega)).isLtdsimp only [nextC]; rw [dite_eq_left_of_eq_true (eq_true h)]; exact hlt⟩else⟨τ + 1, bydsimp only [nextC]; rw [dite_eq_right_of_eq_false (eq_false h)]; omega⟩let F : Fin L.length → Polynomial (Polynomial ℤ) := fun r =>evalFormalT P (seg r).a (seg r).b (seg r).hab (seg r).hb(nextC r) (hnextC_ge r) (hnextC_le r) (wTarget r)-- Check top segment (last index qlast)let qlast : Fin L.length := ⟨L.length - 1, by omega⟩have hqlast_not : ¬ (qlast.val + 1 < L.length) := by dsimp [qlast]; omegahave hqlast_C : nextC qlast = τ := bydsimp only [nextC]; rw [dite_eq_right_of_eq_false (eq_false hqlast_not)]have hqlast_spec : (F qlast).eval (P.evalWire ⟨(seg qlast).b + 1, by have := (seg qlast).hb; omega⟩) =P.evalPoly := byhave hnot : ¬ ((seg qlast).a + 2 ≤ (wTarget qlast).val ∧ (wTarget qlast).val ≤ (seg qlast).b) := byhave hw : (wTarget qlast).val = τ + 1 := bydsimp only [wTarget]; rw [dite_eq_right_of_eq_false (eq_false hqlast_not)]have := (seg qlast).hb; omegahave hspec := evalFormalT_spec P (seg qlast) (nextC qlast) (hnextC_ge qlast) (hnextC_le qlast)(wTarget qlast) hnothave hw : (wTarget qlast).val = τ + 1 := bydsimp only [wTarget]; rw [dite_eq_right_of_eq_false (eq_false hqlast_not)]simpa [F, SLP.evalPoly, hw] using hspec-- Case 0: (F qlast).natDegree = 0 -> P.evalPoly = (F qlast).coeff 0 has degree ≤ 2 ^ (τ - Δ)!by_cases htop0 : (F qlast).natDegree = 0· have hF_eq : F qlast = Polynomial.C ((F qlast).coeff 0) :=Polynomial.eq_C_of_natDegree_eq_zero htop0have hP_eq : P.evalPoly = (F qlast).coeff 0 := bycalc P.evalPoly = (F qlast).eval (P.evalWire ⟨(seg qlast).b + 1, by have := (seg qlast).hb; omega⟩) := hqlast_spec.symm_ = (Polynomial.C ((F qlast).coeff 0)).eval _ := by conv_lhs => rw [hF_eq]_ = (F qlast).coeff 0 := Polynomial.eval_Chave hdeg_le : P.evalPoly.natDegree ≤ 2 ^ (τ - Δ) := byrw [hP_eq]have h1 := evalFormalT_coeff_natDegree_le P (seg qlast).a (seg qlast).b (seg qlast).hab(seg qlast).hb (nextC qlast) (hnextC_ge qlast) (hnextC_le qlast) (wTarget qlast) 0change ((F qlast).coeff 0).natDegree ≤ 2 ^ (nextC qlast - (seg qlast).b) * (P.prefix (seg qlast).a _).maxDeg at h1rw [hqlast_C] at h1have h2 := (P.prefix (seg qlast).a (by have := (seg qlast).hab; have := (seg qlast).hb; omega)).maxDeg_le_two_powhave h3 : 2 ^ (τ - (seg qlast).b) * 2 ^ (seg qlast).a = 2 ^ ((τ - (seg qlast).b) + (seg qlast).a) :=(pow_add 2 _ _).symmhave h4 : Δ ≤ (seg qlast).b - (seg qlast).a := hΔ (seg qlast) (List.getElem_mem _)have h5 := (seg qlast).hab; have h6 := (seg qlast).hbhave h7 : (τ - (seg qlast).b) + (seg qlast).a ≤ τ - Δ := by omegacalc ((F qlast).coeff 0).natDegree≤ 2 ^ (τ - (seg qlast).b) * (P.prefix (seg qlast).a _).maxDeg := h1_ ≤ 2 ^ (τ - (seg qlast).b) * 2 ^ (seg qlast).a := Nat.mul_le_mul_left _ h2_ = 2 ^ ((τ - (seg qlast).b) + (seg qlast).a) := h3_ ≤ 2 ^ (τ - Δ) := Nat.pow_le_pow_right (by omega) h7have hroots : intRootCount P.evalPoly ≤ P.evalPoly.natDegree :=le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _)calc intRootCount P.evalPoly≤ 2 ^ (τ - Δ) := le_trans hroots hdeg_le_ = 1 * 2 ^ (τ - Δ) := (Nat.one_mul _).symm_ ≤ (L.length + 3) * 2 ^ (τ - Δ) := Nat.mul_le_mul_right _ (by omega)-- Main Case: (F qlast).natDegree ≥ 1.-- Prove that EVERY r : Fin L.length has (F r).natDegree ≥ 1!have hF_deg_pos : ∀ r : Fin L.length, 1 ≤ (F r).natDegree := byintro rby_cases hlast : r.val + 1 < L.length· by_contra h0have hdeg0 : (F r).natDegree = 0 := by omegahave hF_eq : F r = Polynomial.C ((F r).coeff 0) := Polynomial.eq_C_of_natDegree_eq_zero hdeg0let r1 : Fin L.length := ⟨r.val + 1, hlast⟩have hnot_inter : ¬ ((seg r).a + 2 ≤ (wTarget r).val ∧ (wTarget r).val ≤ (seg r).b) := byhave hw_val : (wTarget r).val =(P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).val := bydsimp only [wTarget]; rw [dite_eq_left_of_eq_true (eq_true hlast)]rw [hw_val]exact ((seg r).huntapped (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)(hseg_ord r hlast)).1have hspec := evalFormalT_spec P (seg r) (nextC r) (hnextC_ge r) (hnextC_le r) (wTarget r) hnot_interchange (F r).eval (P.evalWire ⟨(seg r).b + 1, by have := (seg r).hb; omega⟩) =P.evalWire ⟨(wTarget r).val, by have := (wTarget r).isLt; have := hnextC_le r; omega⟩ at hspecrw [hF_eq, Polynomial.eval_C] at hspec-- On the one hand, deg(wTarget r) = D₁(a_{r+1}) ≥ deg(U_r) = 2^(g_r) * D₁(a_r)have htop_next := (seg r1).hrun (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)le_rfl (seg r1).habhave hdeg_wt : (P.evalWire ⟨(wTarget r).val, by have := (wTarget r).isLt; have := hnextC_le r; omega⟩).natDegree =(P.prefix (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).maxDeg := byhave hw_val : (wTarget r).val =(P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).val := bydsimp only [wTarget]; rw [dite_eq_left_of_eq_true (eq_true hlast)]have hpr := P.evalWire_prefix (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)(P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega))have h1 : P.evalWire ⟨(wTarget r).val, by have := (wTarget r).isLt; have := hnextC_le r; omega⟩ =(P.prefix (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).evalWire(P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)) := byrw [hpr]; congr 1; exact Fin.ext hw_valrw [h1]; exact htop_next.2.2have hrun_r := P.topSq_run_eval (seg r).hab (seg r).hb (seg r).hrunhave hUr_le : (P.evalWire ⟨(seg r).b + 1, by have := (seg r).hb; omega⟩).natDegree ≤(P.prefix (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).maxDeg := byhave hle_b : (seg r).b ≤ (seg r1).a := hseg_ord r hlasthave hpr := P.evalWire_prefix (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)⟨(seg r).b + 1, by omega⟩rw [← hpr]exact (P.prefix (seg r1).a _).wireDeg_le_maxDeg ⟨(seg r).b + 1, by omega⟩-- On the other hand, deg((F r).coeff 0) ≤ 2 ^ (nextC r - b_r) * D₁(a_r)have hcoeff_le := evalFormalT_coeff_natDegree_le P (seg r).a (seg r).b (seg r).hab (seg r).hb(nextC r) (hnextC_ge r) (hnextC_le r) (wTarget r) 0-- And g_r > nextC r - b_r by the gap condition!have hgap_gt : nextC r - (seg r).b < (seg r).b - (seg r).a := bycases hr0 : r.val with| zero =>cases hL_eq : L with| nil => exact absurd hL_eq hL_ne| cons s0 rest =>have hh := hhead_gap s0 rest hL_eqhave hs0 : seg r = s0 := by simp [seg, hr0, hL_eq]cases rest with| nil => simp [hL_eq, hr0] at hlast| cons s1 rest' =>have hs1 : seg ⟨r.val + 1, hlast⟩ = s1 := by simp [seg, hr0, hL_eq]have hnc : nextC r = s1.a := bydsimp only [nextC]; rw [dite_eq_left_of_eq_true (eq_true hlast), hs1]rw [hnc, hs0]; dsimp at hh; omega| succ m =>have hm := hmid_gap m (by omega)have hsc : seg r = L[m + 1]'(by omega) := by simp [seg, hr0]have hnc : nextC r = (L[m + 2]'(by omega)).a := bydsimp only [nextC]; rw [dite_eq_left_of_eq_true (eq_true hlast)]simp [seg, hr0]have hlt2 : m + 2 < L.length := by omegasimp only [hlt2, ↓reduceDIte] at hmrw [hnc, hsc]; omegahave hpow_lt : 2 ^ (nextC r - (seg r).b) < 2 ^ ((seg r).b - (seg r).a) :=Nat.pow_lt_pow_right (by omega) hgap_gthave hD1_pos : 1 ≤ (P.prefix (seg r).a (by have := (seg r).hab; have := (seg r).hb; omega)).maxDeg :=(P.prefix (seg r).a _).one_le_maxDeghave hstrict : 2 ^ (nextC r - (seg r).b) * (P.prefix (seg r).a _).maxDeg <2 ^ ((seg r).b - (seg r).a) * (P.prefix (seg r).a _).maxDeg :=Nat.mul_lt_mul_of_pos_right hpow_lt (by omega)rw [hspec] at hcoeff_lerw [hrun_r.2] at hUr_leomega· have hreq : r = qlast := Fin.ext (by dsimp [qlast]; omega)subst hreq; omega-- Leading coefficient of F r is nonzero and has degree ≤ 2 ^ (τ - Δ) for EVERY r : Fin L.length!have hlead_ne : ∀ r : Fin L.length, (F r).leadingCoeff ≠ 0 := fun r =>Polynomial.leadingCoeff_ne_zero.mpr (fun h0 => by have := hF_deg_pos r; simp [h0] at this)have hlead_deg : ∀ r : Fin L.length, (F r).leadingCoeff.natDegree ≤ 2 ^ (τ - Δ) := byintro rhave h1 := evalFormalT_coeff_natDegree_le P (seg r).a (seg r).b (seg r).hab (seg r).hb(nextC r) (hnextC_ge r) (hnextC_le r) (wTarget r) (F r).natDegreehave h2 := (P.prefix (seg r).a (by have := (seg r).hab; have := (seg r).hb; omega)).maxDeg_le_two_powhave h4 : Δ ≤ (seg r).b - (seg r).a := hΔ (seg r) (List.getElem_mem _)have h5 := (seg r).hab; have h6 := hnextC_ge r; have h7 := hnextC_le rhave hexp : (nextC r - (seg r).b) + (seg r).a ≤ τ - Δ := by omegacalc (F r).leadingCoeff.natDegree≤ 2 ^ (nextC r - (seg r).b) * (P.prefix (seg r).a _).maxDeg := h1_ ≤ 2 ^ (nextC r - (seg r).b) * 2 ^ (seg r).a := Nat.mul_le_mul_left _ h2_ = 2 ^ ((nextC r - (seg r).b) + (seg r).a) := (pow_add 2 _ _).symm_ ≤ 2 ^ (τ - Δ) := Nat.pow_le_pow_right (by omega) hexp-- First segment's input polynomial w0 = P.evalWire (gateIn1 a_0)let s0 : UntappedSeg P := seg ⟨0, hq_pos⟩let w0 : Polynomial ℤ :=P.evalWire ⟨(P.gateIn1 s0.a (by have := s0.hab; have := s0.hb; omega)).val,by have := (P.gateIn1 s0.a (by have := s0.hab; have := s0.hb; omega)).isLt; have := s0.hab; have := s0.hb; omega⟩have hw0_deg_pos : 1 ≤ w0.natDegree := byhave htop0 := s0.hrun s0.a (by have := s0.hab; have := s0.hb; omega) le_rfl s0.habhave hpr := P.evalWire_prefix s0.a (by have := s0.hab; have := s0.hb; omega)(P.gateIn1 s0.a (by have := s0.hab; have := s0.hb; omega))have hdeg_eq : w0.natDegree = (P.prefix s0.a (by have := s0.hab; have := s0.hb; omega)).maxDeg := bydsimp only [w0]; rw [← hpr]; exact htop0.2.2rw [hdeg_eq]; exact (P.prefix s0.a _).one_le_maxDeghave hw0_deg_le : w0.natDegree ≤ 2 ^ (τ - Δ) := byhave hpr := P.evalWire_prefix s0.a (by have := s0.hab; have := s0.hb; omega)(P.gateIn1 s0.a (by have := s0.hab; have := s0.hb; omega))have h1 : w0.natDegree ≤ (P.prefix s0.a (by have := s0.hab; have := s0.hb; omega)).maxDeg := bydsimp only [w0]; rw [← hpr]; exact (P.prefix s0.a _).wireDeg_le_maxDeg _have h2 := (P.prefix s0.a (by have := s0.hab; have := s0.hb; omega)).maxDeg_le_two_powhave h4 : Δ ≤ s0.b - s0.a := hΔ s0 (List.getElem_mem _)have h5 := s0.hab; have h6 := s0.hbexact le_trans (le_trans h1 h2) (Nat.pow_le_pow_right (by omega) (by omega))-- Build the finite cover of all integer roots of P.evalPoly:-- (⋃_{r : Fin L.length} intRootSet (F r).leadingCoeff) ∪ intRootSet (w0 - 1) ∪ intRootSet w0 ∪ intRootSet (w0 + 1)have hsub : intRootSet P.evalPoly ⊆(Finset.univ.biUnion (fun r : Fin L.length => intRootSet (F r).leadingCoeff)) ∪intRootSet (w0 - 1) ∪ intRootSet w0 ∪ intRootSet (w0 + 1) := byintro x hxhave hPx : P.evalPoly.eval x = 0 :=Polynomial.IsRoot.def.mp ((Polynomial.mem_roots hP).mp (Multiset.mem_toFinset.mp hx))by_cases hkill : ∃ r : Fin L.length, (F r).leadingCoeff.eval x = 0· obtain ⟨r, hr0⟩ := hkillapply Finset.mem_union_left; apply Finset.mem_union_left; apply Finset.mem_union_leftrefine Finset.mem_biUnion.mpr ⟨r, Finset.mem_univ _, ?_⟩rw [intRootSet, Multiset.mem_toFinset, Polynomial.mem_roots (hlead_ne r), Polynomial.IsRoot.def]exact hr0· push Not at hkill-- Backward propagation: for EVERY r : Fin L.length (from L.length - 1 down to 0),-- |U_r(x)| ≤ (wireHeight P (seg r).a _ x) ^ (2 ^ (nextC r - (seg r).b + 1))!let P_prop : Fin L.length → Prop := fun r =>((P.evalWire ⟨(seg r).b + 1, by have := (seg r).hb; omega⟩).eval x).natAbs ≤(wireHeight P (seg r).a (by have := (seg r).hab; have := (seg r).hb; omega) x) ^(2 ^ (nextC r - (seg r).b + 1))have hprop : ∀ (d : ℕ) (hd : d < L.length), P_prop ⟨L.length - 1 - d, by omega⟩ := byintro dinduction d with| zero =>intro _have hr_eq : (⟨L.length - 1 - 0, by omega⟩ : Fin L.length) = qlast := Fin.ext (by simp [qlast])rw [hr_eq]dsimp only [P_prop]have hcauchy := cauchy_root_bound_polyT (F qlast)(P.evalWire ⟨(seg qlast).b + 1, by have := (seg qlast).hb; omega⟩) x(hF_deg_pos qlast) (hkill qlast) (by rw [hqlast_spec, hPx])have hl1 := evalFormalT_l1_le P (seg qlast).a (seg qlast).b (seg qlast).hab (seg qlast).hb x(nextC qlast) (hnextC_ge qlast) (hnextC_le qlast) (wTarget qlast)have hH2 := two_le_wireHeight P (seg qlast).a (by have := (seg qlast).hab; have := (seg qlast).hb; omega) xexact le_trans (le_trans hcauchy hl1)(Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) (by omega)))| succ d' ihd =>intro hdset r : Fin L.length := ⟨L.length - 1 - (d' + 1), by omega⟩set r1 : Fin L.length := ⟨L.length - 1 - d', by omega⟩dsimp only [P_prop]have hr1_val : r.val + 1 = r1.val := by simp [r, r1]; omegahave hr_lt : r.val + 1 < L.length := by omegahave hfin_eq : (⟨r.val + 1, hr_lt⟩ : Fin L.length) = r1 := Fin.ext hr1_valhave hnext_eq : nextC r = (seg r1).a := bydsimp only [nextC]; rw [dite_eq_left_of_eq_true (eq_true hr_lt), hfin_eq]set U := ((P.evalWire ⟨(seg r).b + 1, by have := (seg r).hb; omega⟩).eval x).natAbsset Hr := wireHeight P (seg r).a (by have := (seg r).hab; have := (seg r).hb; omega) xset Rr := nextC r - (seg r).bhave hHr2 : 2 ≤ Hr := two_le_wireHeight P (seg r).a _ x-- Either U ≤ 2 * Hr ^ (2 ^ Rr) (in which case U ≤ Hr ^ (2 ^ (Rr + 1)) immediately!),-- or 2 * Hr ^ (2 ^ Rr) < U (derive contradiction from ihd!).by_cases hsmall : U ≤ 2 * Hr ^ (2 ^ Rr)· calc U ≤ 2 * Hr ^ (2 ^ Rr) := hsmall_ ≤ Hr ^ (2 ^ Rr) * Hr ^ (2 ^ Rr) := byhave : 2 ≤ Hr ^ (2 ^ Rr) :=le_trans hHr2 (Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) Hr)exact Nat.mul_le_mul_right _ this_ = Hr ^ (2 ^ (Rr + 1)) := by rw [← pow_add, ← two_mul, pow_succ, Nat.mul_comm]· exfalsohave hbig : 2 * Hr ^ (2 ^ Rr) < U := by omegahave hU4 : 4 ≤ U := byhave : 2 ≤ Hr ^ (2 ^ Rr) :=le_trans hHr2 (Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) Hr)omegahave hl1 := evalFormalT_l1_le P (seg r).a (seg r).b (seg r).hab (seg r).hb x(nextC r) (hnextC_ge r) (hnextC_le r) (wTarget r)change polyL1At (F r) x ≤ Hr ^ (2 ^ Rr) at hl1have hbig_l1 : 2 * polyL1At (F r) x < U := by omegahave hdom := cauchy_dominance_polyT (F r)(P.evalWire ⟨(seg r).b + 1, by have := (seg r).hb; omega⟩) x(hF_deg_pos r) (hkill r) hbig_l1-- Identify (F r).eval U_r as w_{r+1} (the input to seg r1)!have hw_val : (wTarget r).val =(P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).val := bydsimp only [wTarget]; rw [dite_eq_left_of_eq_true (eq_true hr_lt)]have hseg_eq : seg ⟨r.val + 1, hr_lt⟩ = seg r1 := congrArg seg hfin_eqhave hgate_eq : ∀ {s1 s2 : UntappedSeg P} (h1 : s1.a < τ) (h2 : s2.a < τ), s1 = s2 →(P.gateIn1 s1.a h1).val = (P.gateIn1 s2.a h2).val := by rintro s1 _ h1 h2 rfl; rflexact hgate_eq _ _ hseg_eqhave hnot_inter : ¬ ((seg r).a + 2 ≤ (wTarget r).val ∧ (wTarget r).val ≤ (seg r).b) := byrw [hw_val]have hbr : (seg r).b ≤ (seg r1).a := by rw [← hnext_eq]; exact hnextC_ge rexact ((seg r).huntapped (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega) hbr).1have hspec := evalFormalT_spec P (seg r) (nextC r) (hnextC_ge r) (hnextC_le r) (wTarget r) hnot_interrw [hspec] at hdomset w1_val := ((P.evalWire ⟨(wTarget r).val, by have := (wTarget r).isLt; have := hnextC_le r; omega⟩).eval x).natAbs-- U ≤ 2 * w1_val and 4 ≤ U => U ≤ w1_val ^ 2!have hU_le_sq : U ≤ w1_val ^ 2 := nat_half_sq_ge hU4 hdom-- Now relate w1_val ^ (2 ^ g_{r+1}) = |U_{r+1}(x)|!set g1 := (seg r1).b - (seg r1).ahave hrun1 := P.topSq_run_eval (seg r1).hab (seg r1).hb (seg r1).hrunhave hw1_eq : w1_val = ((P.evalWire ⟨(P.gateIn1 (seg r1).a(by have := (seg r1).hab; have := (seg r1).hb; omega)).val,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 := byexact congrArg (fun k : Fin (τ + 2) => ((P.evalWire k).eval x).natAbs) (Fin.ext hw_val)have hU1_eq : ((P.evalWire ⟨(seg r1).b + 1, by have := (seg r1).hb; omega⟩).eval x).natAbs =w1_val ^ (2 ^ g1) := byrw [hw1_eq]have := congrArg (fun p : Polynomial ℤ => (p.eval x).natAbs) hrun1.1simpa [Int.natAbs_pow, g1] using this-- By ihd, w1_val ^ (2 ^ g1) ≤ (wireHeight P (seg r1).a _ x) ^ (2 ^ (R_{r+1} + 1))have hih : P_prop r1 := ihd (by omega)dsimp only [P_prop] at hihrw [hU1_eq] at hih-- By Property 4 (wireHeight_step_le), wireHeight P (seg r1).a _ x ≤ (max Hr U) ^ (2 ^ Rr) = U ^ (2 ^ Rr)!have hH1_le := wireHeight_step_le P (seg r) x (nextC r) (hnextC_ge r) (hnextC_le r)have hmax_U : max Hr U = U := max_eq_right (byhave : Hr ≤ Hr ^ (2 ^ Rr) := Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) Hromega)rw [hmax_U] at hH1_lehave hH1_le' : wireHeight P (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega) x ≤ U ^ (2 ^ Rr) := bychange wireHeight P (nextC r) (hnextC_le r) x ≤ U ^ (2 ^ Rr) at hH1_lesimpa only [hnext_eq] using hH1_le-- Combine: U ^ (2 ^ (g1 - 1)) ≤ (w1_val ^ 2) ^ (2 ^ (g1 - 1)) = w1_val ^ (2 ^ g1) ≤ U ^ (2 ^ (Rr + R_{r+1} + 1))!have hg1_ge : Rr + (nextC r1 - (seg r1).b) + 3 ≤ g1 := byhave hmid := hmid_gap r.val (by omega)have hr_seg : L[r.val]'(by omega) = seg r := rflhave hr1_seg : L[r.val + 1]'(by omega) = seg r1 := by simp [seg, hr1_val]have hRr_eq : Rr = (seg r1).a - (seg r).b := by rw [← hnext_eq]have hnc1 : nextC r1 = if h2 : r.val + 2 < L.length then (L[r.val + 2]'h2).a else τ := bydsimp only [nextC]split_ifs with h1 h2· have hidx_eq : r1.val + 1 = r.val + 2 := by omegasimp only [seg]have hget : ∀ {i j : ℕ} (hi : i < L.length) (hj : j < L.length), i = j →L[i]'hi = L[j]'hj := by rintro i _ hi hj rfl; rflexact congrArg UntappedSeg.a (hget _ _ hidx_eq)· omega· omega· rflrw [hRr_eq, hnc1]simpa [hr_seg, hr1_seg, g1] using hmidhave hchain_pow : U ^ (2 ^ (g1 - 1)) ≤ U ^ (2 ^ (Rr + (nextC r1 - (seg r1).b) + 1)) := byhave hleft : U ^ (2 ^ (g1 - 1)) ≤ w1_val ^ (2 ^ g1) := bycalc U ^ (2 ^ (g1 - 1))≤ (w1_val ^ 2) ^ (2 ^ (g1 - 1)) := Nat.pow_le_pow_left hU_le_sq __ = w1_val ^ (2 * 2 ^ (g1 - 1)) := (pow_mul _ _ _).symm_ = w1_val ^ (2 ^ g1) := bycongr 1conv_rhs => rw [← Nat.sub_add_cancel (by omega : 1 ≤ g1), pow_succ, Nat.mul_comm]have hright : w1_val ^ (2 ^ g1) ≤ U ^ (2 ^ (Rr + (nextC r1 - (seg r1).b) + 1)) := bycalc w1_val ^ (2 ^ g1)≤ (wireHeight P (seg r1).a _ x) ^ (2 ^ (nextC r1 - (seg r1).b + 1)) := hih_ ≤ (U ^ (2 ^ Rr)) ^ (2 ^ (nextC r1 - (seg r1).b + 1)) :=Nat.pow_le_pow_left hH1_le' __ = U ^ (2 ^ Rr * 2 ^ (nextC r1 - (seg r1).b + 1)) := (pow_mul _ _ _).symm_ = U ^ (2 ^ (Rr + (nextC r1 - (seg r1).b) + 1)) := byrw [← pow_add, ← Nat.add_assoc]exact le_trans hleft hrighthave hexp_le : 2 ^ (g1 - 1) ≤ 2 ^ (Rr + (nextC r1 - (seg r1).b) + 1) :=(Nat.pow_le_pow_iff_right (by omega : 1 < U)).mp hchain_powhave hexp_lt : 2 ^ (Rr + (nextC r1 - (seg r1).b) + 1) < 2 ^ (g1 - 1) :=Nat.pow_lt_pow_right (by omega) (by omega)omega-- Now evaluate hprop at d = L.length - 1 (which gives r = 0, i.e. s0)!have hprop0 := hprop (L.length - 1) (by omega)have hr0_eq : (⟨L.length - 1 - (L.length - 1), by omega⟩ : Fin L.length) = ⟨0, hq_pos⟩ :=Fin.ext (by simp)rw [hr0_eq] at hprop0dsimp only [P_prop] at hprop0-- Extract the head gap condition: 2 * R0 + R1 + 5 ≤ g0set R0 := s0.aset R1 := nextC ⟨0, hq_pos⟩ - s0.bset g0 := s0.b - s0.ahave hg0_ge : 2 * R0 + R1 + 5 ≤ g0 := bycases hL_eq : L with| nil => exact absurd hL_eq hL_ne| cons a0 rest =>have hh := hhead_gap a0 rest hL_eqhave ha0 : s0 = a0 := by simp [s0, seg, hL_eq]cases rest with| nil =>have hnc : nextC ⟨0, hq_pos⟩ = τ := bydsimp only [nextC]; have hnot : ¬ (0 + 1 < L.length) := by simp [hL_eq]rw [dite_eq_right_of_eq_false (eq_false hnot)]dsimp only [R0, R1, g0]; rw [hnc, ha0]; exact hh| cons a1 rest' =>have hlt1 : 0 + 1 < L.length := by simp [hL_eq]have hnc : nextC ⟨0, hq_pos⟩ = a1.a := bydsimp only [nextC]; rw [dite_eq_left_of_eq_true (eq_true hlt1)]simp [seg, hL_eq]dsimp only [R0, R1, g0]; rw [hnc, ha0]; exact hh-- Rewrite |U_0(x)| = |w0(x)| ^ (2 ^ g0)have hrun0 := P.topSq_run_eval s0.hab s0.hb s0.hrunhave hU0_eq : ((P.evalWire ⟨s0.b + 1, by have := s0.hb; omega⟩).eval x).natAbs =(w0.eval x).natAbs ^ (2 ^ g0) := byhave := congrArg (fun p : Polynomial ℤ => (p.eval x).natAbs) hrun0.1simpa [Int.natAbs_pow, w0, g0] using thisrw [hU0_eq] at hprop0-- Bound H0 ≤ (max 2 |x|) ^ (2 ^ R0) and polyIntL1 w0 ≤ 2 ^ (2 ^ R0)have hH0_le := wireHeight_base_le P s0.a (by have := s0.hab; have := s0.hb; omega) xhave hl1_w0 : polyIntL1 w0 ≤ 2 ^ (2 ^ R0) := byhave hpr := P.evalWire_prefix s0.a (by have := s0.hab; have := s0.hb; omega)(P.gateIn1 s0.a (by have := s0.hab; have := s0.hb; omega))have hw0_eq : w0 = (P.prefix s0.a (by have := s0.hab; have := s0.hb; omega)).evalWire(P.gateIn1 s0.a (by have := s0.hab; have := s0.hb; omega)) := hpr.symmrw [hw0_eq]exact (P.prefix s0.a _).wire_polyIntL1_le _-- Deduce (w0.eval x).natAbs ≤ 1!have hw0_le1 : (w0.eval x).natAbs ≤ 1 := byby_contra hbig_w0have hw0_ge2 : 2 ≤ (w0.eval x).natAbs := by omegaby_cases hx_big : 2 * 2 ^ (2 ^ R0) ≤ x.natAbs· -- Case A: |x| ≥ 2 * 2^(2^R0) ≥ 4, so |x| ≤ 2 * |w0(x)| => |x| ≤ |w0(x)|^2!have hx4 : 4 ≤ x.natAbs := byhave : 2 ≤ 2 ^ (2 ^ R0) :=Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) 2omegahave hdom := cauchy_dominance_univar w0 x hw0_deg_pos (by omega)have hx_sq : x.natAbs ≤ (w0.eval x).natAbs ^ 2 := nat_half_sq_ge hx4 hdomhave hmax_x : max 2 x.natAbs = x.natAbs := max_eq_right (by omega)rw [hmax_x] at hH0_lehave hchain_x : x.natAbs ^ (2 ^ (g0 - 1)) ≤ x.natAbs ^ (2 ^ (R0 + R1 + 1)) := byhave h1 : x.natAbs ^ (2 ^ (g0 - 1)) ≤ (w0.eval x).natAbs ^ (2 ^ g0) := bycalc x.natAbs ^ (2 ^ (g0 - 1))≤ ((w0.eval x).natAbs ^ 2) ^ (2 ^ (g0 - 1)) := Nat.pow_le_pow_left hx_sq __ = (w0.eval x).natAbs ^ (2 * 2 ^ (g0 - 1)) := (pow_mul _ _ _).symm_ = (w0.eval x).natAbs ^ (2 ^ g0) := bycongr 1conv_rhs => rw [← Nat.sub_add_cancel (by omega : 1 ≤ g0), pow_succ, Nat.mul_comm]have h2 : (w0.eval x).natAbs ^ (2 ^ g0) ≤ x.natAbs ^ (2 ^ (R0 + R1 + 1)) := bycalc (w0.eval x).natAbs ^ (2 ^ g0)≤ (wireHeight P s0.a _ x) ^ (2 ^ (R1 + 1)) := hprop0_ ≤ (x.natAbs ^ (2 ^ R0)) ^ (2 ^ (R1 + 1)) := Nat.pow_le_pow_left hH0_le __ = x.natAbs ^ (2 ^ R0 * 2 ^ (R1 + 1)) := (pow_mul _ _ _).symm_ = x.natAbs ^ (2 ^ (R0 + R1 + 1)) := by rw [← pow_add, ← Nat.add_assoc]exact le_trans h1 h2have hexp_le : 2 ^ (g0 - 1) ≤ 2 ^ (R0 + R1 + 1) :=(Nat.pow_le_pow_iff_right (by omega : 1 < x.natAbs)).mp hchain_xhave hexp_lt : 2 ^ (R0 + R1 + 1) < 2 ^ (g0 - 1) :=Nat.pow_lt_pow_right (by omega) (by omega)omega· -- Case B: |x| < 2 * 2^(2^R0) = 2^(2^R0 + 1), so H0 ≤ 2^(2^(2*R0 + 1))!have hmax_le : max 2 x.natAbs ≤ 2 ^ (2 ^ R0 + 1) := byhave h1 : 2 ≤ 2 ^ (2 ^ R0 + 1) :=Nat.le_self_pow (Nat.succ_ne_zero _) 2have h2 : 2 * 2 ^ (2 ^ R0) = 2 ^ (2 ^ R0 + 1) := by rw [pow_succ, Nat.mul_comm]omegahave hH0_2 : wireHeight P s0.a (by have := s0.hab; have := s0.hb; omega) x ≤2 ^ (2 ^ (2 * R0 + 1)) := bycalc wireHeight P s0.a _ x≤ (max 2 x.natAbs) ^ (2 ^ R0) := hH0_le_ ≤ (2 ^ (2 ^ R0 + 1)) ^ (2 ^ R0) := Nat.pow_le_pow_left hmax_le __ = 2 ^ ((2 ^ R0 + 1) * 2 ^ R0) := (pow_mul _ _ _).symm_ ≤ 2 ^ (2 ^ (2 * R0 + 1)) := byapply Nat.pow_le_pow_right (by omega)have h1 : 2 ^ R0 + 1 ≤ 2 * 2 ^ R0 := byhave : 1 ≤ 2 ^ R0 := Nat.one_le_pow _ _ (by omega); omegacalc (2 ^ R0 + 1) * 2 ^ R0≤ (2 * 2 ^ R0) * 2 ^ R0 := Nat.mul_le_mul_right _ h1_ = 2 * (2 ^ R0 * 2 ^ R0) := by ring_ = 2 ^ (2 * R0 + 1) := by rw [← pow_add, ← two_mul, pow_succ, Nat.mul_comm]have hchain_2 : 2 ^ (2 ^ g0) ≤ 2 ^ (2 ^ (2 * R0 + R1 + 2)) := bycalc 2 ^ (2 ^ g0)≤ (w0.eval x).natAbs ^ (2 ^ g0) := Nat.pow_le_pow_left hw0_ge2 __ ≤ (wireHeight P s0.a _ x) ^ (2 ^ (R1 + 1)) := hprop0_ ≤ (2 ^ (2 ^ (2 * R0 + 1))) ^ (2 ^ (R1 + 1)) := Nat.pow_le_pow_left hH0_2 __ = 2 ^ (2 ^ (2 * R0 + 1) * 2 ^ (R1 + 1)) := (pow_mul _ _ _).symm_ = 2 ^ (2 ^ (2 * R0 + R1 + 2)) := by rw [← pow_add]; congr 2; omegahave hexp_le : 2 ^ g0 ≤ 2 ^ (2 * R0 + R1 + 2) :=(Nat.pow_le_pow_iff_right (by omega : 1 < 2)).mp hchain_2have hexp_lt : 2 ^ (2 * R0 + R1 + 2) < 2 ^ g0 :=Nat.pow_lt_pow_right (by omega) (by omega)omega-- Since |w0(x)| ≤ 1, w0(x) ∈ {-1, 0, 1}!have hw0_ne : w0 ≠ 0 := fun h0 => by simp [h0] at hw0_deg_poshave hw0_sub1_ne : w0 - 1 ≠ 0 := fun h0 => byhave : w0 = 1 := sub_eq_zero.mp h0rw [this] at hw0_deg_pos; simp at hw0_deg_poshave hw0_add1_ne : w0 + 1 ≠ 0 := fun h0 => byhave : w0 = -1 := eq_neg_of_add_eq_zero_left h0rw [this] at hw0_deg_pos; simp at hw0_deg_poshave hcases : w0.eval x = 1 ∨ w0.eval x = 0 ∨ w0.eval x = -1 := by omegarcases hcases with h1 | h0 | hm1· apply Finset.mem_union_left; apply Finset.mem_union_left; apply Finset.mem_union_rightrw [intRootSet, Multiset.mem_toFinset, Polynomial.mem_roots hw0_sub1_ne, Polynomial.IsRoot.def]simp [h1]· apply Finset.mem_union_left; apply Finset.mem_union_rightrw [intRootSet, Multiset.mem_toFinset, Polynomial.mem_roots hw0_ne, Polynomial.IsRoot.def]exact h0· apply Finset.mem_union_rightrw [intRootSet, Multiset.mem_toFinset, Polynomial.mem_roots hw0_add1_ne, Polynomial.IsRoot.def]simp [hm1]-- Bound the cardinality of the cover!have hcard_poly : ∀ q : Polynomial ℤ, intRootCount q ≤ q.natDegree := fun q =>le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _)have hdeg_sub1 : (w0 - 1).natDegree ≤ 2 ^ (τ - Δ) :=le_trans (Polynomial.natDegree_sub_le _ _) (max_le hw0_deg_le (by simp))have hdeg_add1 : (w0 + 1).natDegree ≤ 2 ^ (τ - Δ) :=le_trans (Polynomial.natDegree_add_le _ _) (max_le hw0_deg_le (by simp))calc intRootCount P.evalPoly≤ ((Finset.univ.biUnion (fun r : Fin L.length => intRootSet (F r).leadingCoeff)) ∪intRootSet (w0 - 1) ∪ intRootSet w0 ∪ intRootSet (w0 + 1)).card :=Finset.card_le_card hsub_ ≤ (Finset.univ.biUnion (fun r : Fin L.length => intRootSet (F r).leadingCoeff)).card +intRootCount (w0 - 1) + intRootCount w0 + intRootCount (w0 + 1) := byhave h1 := Finset.card_union_le((Finset.univ.biUnion (fun r : Fin L.length => intRootSet (F r).leadingCoeff)) ∪intRootSet (w0 - 1) ∪ intRootSet w0) (intRootSet (w0 + 1))have h2 := Finset.card_union_le((Finset.univ.biUnion (fun r : Fin L.length => intRootSet (F r).leadingCoeff)) ∪intRootSet (w0 - 1)) (intRootSet w0)have h3 := Finset.card_union_le(Finset.univ.biUnion (fun r : Fin L.length => intRootSet (F r).leadingCoeff))(intRootSet (w0 - 1))simp only [intRootCount] at *omega_ ≤ (∑ r : Fin L.length, intRootCount (F r).leadingCoeff) +2 ^ (τ - Δ) + 2 ^ (τ - Δ) + 2 ^ (τ - Δ) := byhave hbi : (Finset.univ.biUnion (fun r : Fin L.length => intRootSet (F r).leadingCoeff)).card ≤∑ r : Fin L.length, intRootCount (F r).leadingCoeff := Finset.card_biUnion_lehave h1 := le_trans (hcard_poly (w0 - 1)) hdeg_sub1have h2 := le_trans (hcard_poly w0) hw0_deg_lehave h3 := le_trans (hcard_poly (w0 + 1)) hdeg_add1omega_ ≤ (∑ _r : Fin L.length, 2 ^ (τ - Δ)) + 3 * 2 ^ (τ - Δ) := byhave hsum := Finset.sum_le_sum (s := (Finset.univ : Finset (Fin L.length)))(fun r _ => le_trans (hcard_poly (F r).leadingCoeff) (hlead_deg r))omega_ = (L.length + 3) * 2 ^ (τ - Δ) := bysimp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, smul_eq_mul]ring/-! ## 5. Main Unconditional `o(2^τ)` Theorem for All Constant-Free SLPs -/private lemma three_pow_le_two_pow (K : ℕ) : 3 ^ (3 * K + 1) ≤ 2 ^ (5 * K + 2) := byhave h1 : (3 ^ 3) ^ K ≤ (2 ^ 5) ^ K := Nat.pow_le_pow_left (by decide) Krw [← pow_mul, ← pow_mul] at h1calc 3 ^ (3 * K + 1) = 3 ^ (3 * K) * 3 := pow_succ _ __ ≤ 2 ^ (5 * K) * 4 := Nat.mul_le_mul h1 (by decide)_ = 2 ^ (5 * K + 2) := by rw [pow_add]; norm_numprivate lemma two_pow_quarter_ge_double : ∀ m : ℕ, 40 ≤ m → 2 * m ≤ 2 ^ (m / 4) := byintro m hmhave hbase : ∀ q : ℕ, 10 ≤ q → 8 * q + 6 ≤ 2 ^ q := byintro q hqinduction q, hq using Nat.le_induction with| base => decide| succ q' hq' ih =>rw [pow_succ]; omegahave hq : 10 ≤ m / 4 := by omegahave h1 := hbase (m / 4) hqomega/-- **Unconditional Elementary `o(2^τ)` Theorem for Smale's 4th Problem:**For EVERY `τ : ℕ` and EVERY constant-free straight-line program `P : SLP τ` with `P.evalPoly ≠ 0`:`τ * (intRootCount P.evalPoly) ^ 28 ≤ 2 ^ 40 * 2 ^ (28 * τ)`.Equivalently, `intRootCount P.evalPoly ≤ 2^(10/7) * 2^τ / τ^(1/28) = o(2^τ)` unconditionally! -/theorem slp_intRootCount_o_two_pow {τ : ℕ} (P : SLP τ) (hP : P.evalPoly ≠ 0) :τ * (intRootCount P.evalPoly) ^ 28 ≤ 2 ^ 40 * 2 ^ (28 * τ) := byhave htriv : intRootCount P.evalPoly ≤ 2 ^ τ := bycalc intRootCount P.evalPoly≤ P.evalPoly.natDegree := le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _)_ ≤ P.maxDeg := P.wireDeg_le_maxDeg ⟨τ + 1, Nat.lt_succ_self _⟩_ ≤ 2 ^ τ := P.maxDeg_le_two_powby_cases hsmall : τ < 2 ^ 40· calc τ * (intRootCount P.evalPoly) ^ 28≤ 2 ^ 40 * (2 ^ τ) ^ 28 := Nat.mul_le_mul (le_of_lt hsmall) (Nat.pow_le_pow_left htriv 28)_ = 2 ^ 40 * 2 ^ (28 * τ) := by rw [← pow_mul, Nat.mul_comm τ 28]· have hτ_big : 2 ^ 40 ≤ τ := by omegaset m := Nat.log 2 τhave hm40 : 40 ≤ m := (Nat.le_log_iff_pow_le (by omega) (by omega)).mpr hτ_bighave h2m_le : 2 ^ m ≤ τ := Nat.pow_log_le_self 2 (by omega)have hτ_lt : τ < 2 ^ (m + 1) := Nat.lt_pow_succ_log_self (by omega) τset K := m / 8by_cases hk_high : K ≤ P.nonTopSqCount· -- Case 1: High non-top-squaring count -> Pure-Squaring Collapse wins!have hdeg7 : (P.evalPoly.natDegree) ^ 7 ≤ 2 ^ (7 * τ - 2 * P.nonTopSqCount) :=slp_pure_squaring_collapse_deg Phave hz_le_deg : intRootCount P.evalPoly ≤ P.evalPoly.natDegree :=le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _)have hz7 : (intRootCount P.evalPoly) ^ 7 ≤ 2 ^ (7 * τ - 2 * K) :=le_trans (Nat.pow_le_pow_left hz_le_deg 7)(le_trans hdeg7 (Nat.pow_le_pow_right (by omega) (by omega)))have hz28 : (intRootCount P.evalPoly) ^ 28 ≤ 2 ^ (28 * τ - 8 * K) := byhave h1 : ((intRootCount P.evalPoly) ^ 7) ^ 4 ≤ (2 ^ (7 * τ - 2 * K)) ^ 4 :=Nat.pow_le_pow_left hz7 4rw [← pow_mul, ← pow_mul] at h1have hK_le : 2 * K ≤ 7 * τ := byhave : m ≤ 2 ^ m := Nat.le_of_lt Nat.lt_two_pow_selfomegahave hexp : (7 * τ - 2 * K) * 4 = 28 * τ - 8 * K := by omegarwa [hexp] at h1have hm_le_8K : m + 1 ≤ 8 * K + 8 := by omegahave hτ_le : τ ≤ 2 ^ (8 * K + 8) :=le_trans (le_of_lt hτ_lt) (Nat.pow_le_pow_right (by omega) hm_le_8K)have h8K_le : 8 * K ≤ 28 * τ := byhave : m ≤ 2 ^ m := Nat.le_of_lt Nat.lt_two_pow_selfomegacalc τ * (intRootCount P.evalPoly) ^ 28≤ 2 ^ (8 * K + 8) * 2 ^ (28 * τ - 8 * K) := Nat.mul_le_mul hτ_le hz28_ = 2 ^ ((8 * K + 8) + (28 * τ - 8 * K)) := (pow_add 2 _ _).symm_ = 2 ^ (8 + 28 * τ) := by congr 1; omega_ = 2 ^ 8 * 2 ^ (28 * τ) := pow_add 2 8 (28 * τ)_ ≤ 2 ^ 40 * 2 ^ (28 * τ) := Nat.mul_le_mul_right _ (by decide)· -- Case 2: Low non-top-squaring count (k < K) -> Multi-Segment Gap-Dominance wins!have hk_lt : P.nonTopSqCount < K := by omegaobtain ⟨L0, hord0, hlen0, htot0⟩ := extract_untapped_segments Pset Δ := 2 ^ (m / 4)have h2m4 : 2 * m ≤ Δ := two_pow_quarter_ge_double m hm40have hΔ_le_τ : Δ ≤ τ :=le_trans (Nat.pow_le_pow_right (by omega) (by omega : m / 4 ≤ m)) h2m_le-- Show the demotion failure branch is impossible!have hdem_strict : 3 ^ L0.length * (τ - segsTotalLen L0 + Δ + 2) < τ + Δ + 2 := byhave hR0 : τ - segsTotalLen L0 = P.nonTopSqCount := byhave := P.nonTopSqCount_leomegarw [hR0]have hL0_le : L0.length ≤ 3 * K + 1 := by omegahave h3pow : 3 ^ L0.length ≤ 2 ^ (5 * K + 2) :=le_trans (Nat.pow_le_pow_right (by omega) hL0_le) (three_pow_le_two_pow K)have hfac : P.nonTopSqCount + Δ + 2 ≤ 2 * Δ := by omegahave hmul_le : 3 ^ L0.length * (P.nonTopSqCount + Δ + 2) ≤ 2 ^ (5 * K + 2 + (m / 4 + 1)) := bycalc 3 ^ L0.length * (P.nonTopSqCount + Δ + 2)≤ 2 ^ (5 * K + 2) * (2 * Δ) := Nat.mul_le_mul h3pow hfac_ = 2 ^ (5 * K + 2) * 2 ^ (m / 4 + 1) := by congr 1; rw [pow_succ, Nat.mul_comm]_ = 2 ^ (5 * K + 2 + (m / 4 + 1)) := (pow_add 2 _ _).symmhave hexp_lt : 5 * K + 2 + (m / 4 + 1) < m := by omegahave hpow_lt : 2 ^ (5 * K + 2 + (m / 4 + 1)) < 2 ^ m := Nat.pow_lt_pow_right (by omega) hexp_ltomegarcases demotion_exists_valid_gaps Δ L0.length L0 rfl hord0 with ⟨L', hv', hlen'⟩ | hfail· have hz_gap := slp_multi_segment_gap_dominance P hP L' hv'have hL'_le : L'.length + 3 ≤ 2 ^ m := byhave : m ≤ 2 ^ m := Nat.le_of_lt Nat.lt_two_pow_selfhave : L'.length + 3 ≤ 3 * K + 4 := by omegahave : 3 * K + 4 ≤ m := by omegaomegahave hz_le : intRootCount P.evalPoly ≤ 2 ^ m * 2 ^ (τ - Δ) :=le_trans hz_gap (Nat.mul_le_mul_right _ hL'_le)have hz28 : (intRootCount P.evalPoly) ^ 28 ≤ 2 ^ (28 * m) * 2 ^ (28 * (τ - Δ)) := bycalc (intRootCount P.evalPoly) ^ 28≤ (2 ^ m * 2 ^ (τ - Δ)) ^ 28 := Nat.pow_le_pow_left hz_le 28_ = (2 ^ m) ^ 28 * (2 ^ (τ - Δ)) ^ 28 := mul_pow _ _ __ = 2 ^ (28 * m) * 2 ^ (28 * (τ - Δ)) := byrw [← pow_mul, ← pow_mul, Nat.mul_comm m 28, Nat.mul_comm (τ - Δ) 28]have hexp_ok : m + 1 + 28 * m + 28 * (τ - Δ) ≤ 28 * τ := by omegacalc τ * (intRootCount P.evalPoly) ^ 28≤ 2 ^ (m + 1) * (2 ^ (28 * m) * 2 ^ (28 * (τ - Δ))) :=Nat.mul_le_mul (le_of_lt hτ_lt) hz28_ = 2 ^ (m + 1 + 28 * m + 28 * (τ - Δ)) := byrw [← pow_add, ← pow_add]; congr 1; omega_ ≤ 2 ^ (28 * τ) := Nat.pow_le_pow_right (by omega) hexp_ok_ = 1 * 2 ^ (28 * τ) := (Nat.one_mul _).symm_ ≤ 2 ^ 40 * 2 ^ (28 * τ) := Nat.mul_le_mul_right _ (by decide)· exfalso; omegaend Research.PvsNP.Tau