Lean / 0xOsprey/shub-smale-tau9 / WellSpacedSLPs.lean
Lean readerResearch/PvsNP/Tau/WellSpacedSLPs.lean
modulepublic import Research.PvsNP.Tau.GapDominanceBoundpublic import Research.PvsNP.Tau.AddComplexityBound@[expose] public sectionnamespace Research.PvsNP.Tau/-!# Well-Spaced General `{+, -, ×}` Straight-Line Programs over `ℤ`This module formalizes the **Well-Spaced General `{+, -, ×}` SLP Theorem** over `ℤ`:for any constant-free DAG `P : SLP τ` (`P.evalPoly ≠ 0`) admitting a list `L` of well-spaceduntapped top-degree pure-squaring segments `[a_0, b_0), ..., [a_{q-1}, b_{q-1})` (gaps `R_r ≤ 1`,`g_0 ≥ 2 a_0 + 6`, `g_r ≥ 5` for `r ≥ 1`, and each isolated trailing gate `b_r` reading the topwire `b_r + 1`), we prove:1. **`slp_well_spaced_mul_bound`** (Sharp Multiplication-Only Exponential Bound):```intRootCount P.evalPoly ≤ 3 * 2 ^ (a_0 + nonSqMulCount P L L.length)```In particular, well-spaced additions, subtractions, and pure squarings are **completely free**:only isolated non-squaring multiplications `U_r * w_u` (`u < a_r + 2`) can double the bound!2. **`slp_well_spaced_add_only_le_three`**:When `a_0 = 0` and there are no isolated non-squaring multiplications (`nonSqMulCount = 0`),every nonzero well-spaced `{+, -}`-plus-squaring DAG (with **arbitrarily many** additions,subtractions, and DAG taps to earlier wires) satisfies```intRootCount P.evalPoly ≤ 3.```3. **`slp_well_spaced_intRootCount_le`**:When `a_0 = 0`, every nonzero well-spaced general `{+, -, ×}` SLP satisfies```intRootCount P.evalPoly ≤ 2 ^ (P.nonTopSqCount + 1),```beating Khovanskii's `2 ^ O(|B| ^ 2)` to single-exponential `2 ^ (|B| + 1)` over `ℤ`.-/open Polynomial/-! ## 1. Elementary Helpers (Cauchy Bounds, Prefix Transfers, & Geometric Sum) -/private lemma nat_half_sq_ge_ws {u v : ℕ} (hu : 4 ≤ u) (huv : u ≤ 2 * v) : u ≤ v ^ 2 := bynlinarithprivate lemma polyL1At_eq_sum_range_ws (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 _ hnothave hc : F.coeff j = 0 := Polynomial.notMem_support_iff.mp hnotsimp [hc]private lemma polyIntL1_eq_sum_range_ws (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 _ hnothave hc : f.coeff j = 0 := Polynomial.notMem_support_iff.mp hnotsimp [hc]private lemma eval_polyT_eq_ws (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 _simpprivate lemma lower_terms_bound_ws (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 a s' ha ih =>rw [Finset.sum_insert ha, Finset.sum_insert ha]exact le_trans (Int.natAbs_add_le _ _) (Nat.add_le_add_left ih _)have hsum_le : ∑ j ∈ Finset.range F.natDegree, ((F.coeff j).eval x).natAbs ≤ polyL1At F x := byrw [polyL1At_eq_sum_range_ws 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 hlt : j < F.natDegree := Finset.mem_range.mp hjexact Nat.mul_le_mul_left _ (Nat.pow_le_pow_right hU (by omega))_ = (∑ 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 _ _private lemma cauchy_root_bound_polyT_ws (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 := byby_cases hU0 : (u.eval x).natAbs = 0· omega· have hU1 : 1 ≤ (u.eval x).natAbs := by omegahave hev := eval_polyT_eq_ws F u xrw [hzero, Finset.sum_range_succ] at hevset low := ∑ j ∈ Finset.range F.natDegree, (F.coeff j).eval x * (u.eval x) ^ jset topTerm := (F.coeff F.natDegree).eval x * (u.eval x) ^ F.natDegreehave htop_eq : topTerm = -low := by linarithhave hlead_pos : 1 ≤ ((F.coeff F.natDegree).eval x).natAbs :=Int.natAbs_pos.mpr hleadhave htop_ge : (u.eval x).natAbs ^ F.natDegree ≤ topTerm.natAbs := bycalc (u.eval x).natAbs ^ F.natDegree= 1 * (u.eval x).natAbs ^ F.natDegree := (Nat.one_mul _).symm_ ≤ ((F.coeff F.natDegree).eval x).natAbs * (u.eval x).natAbs ^ F.natDegree :=Nat.mul_le_mul_right _ hlead_pos_ = topTerm.natAbs := by dsimp only [topTerm]; rw [Int.natAbs_mul, Int.natAbs_pow]have hlow : low.natAbs ≤ (u.eval x).natAbs ^ (F.natDegree - 1) * polyL1At F x :=lower_terms_bound_ws F x (u.eval x) hD hU1have htop_le : topTerm.natAbs ≤ (u.eval x).natAbs ^ (F.natDegree - 1) * polyL1At F x := byrw [htop_eq, Int.natAbs_neg]; exact hlowhave hpow_split : (u.eval x).natAbs ^ F.natDegree =(u.eval x).natAbs ^ (F.natDegree - 1) * (u.eval x).natAbs := byconv_lhs => rw [← Nat.sub_add_cancel hD, pow_succ]rw [hpow_split] at htop_gehave hprod : (u.eval x).natAbs ^ (F.natDegree - 1) * (u.eval x).natAbs ≤(u.eval x).natAbs ^ (F.natDegree - 1) * polyL1At F x := le_trans htop_ge htop_leexact Nat.le_of_mul_le_mul_left hprod (Nat.pow_pos (by omega))private lemma cauchy_dominance_polyT_ws (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 := byhave hU1 : 1 ≤ (u.eval x).natAbs := by omegahave hev := eval_polyT_eq_ws F u xrw [Finset.sum_range_succ] at hevset low := ∑ j ∈ Finset.range F.natDegree, (F.coeff j).eval x * (u.eval x) ^ jset topTerm := (F.coeff F.natDegree).eval x * (u.eval x) ^ F.natDegreehave hlead_pos : 1 ≤ ((F.coeff F.natDegree).eval x).natAbs :=Int.natAbs_pos.mpr hleadhave htop_ge : (u.eval x).natAbs ^ F.natDegree ≤ topTerm.natAbs := bycalc (u.eval x).natAbs ^ F.natDegree= 1 * (u.eval x).natAbs ^ F.natDegree := (Nat.one_mul _).symm_ ≤ ((F.coeff F.natDegree).eval x).natAbs * (u.eval x).natAbs ^ F.natDegree :=Nat.mul_le_mul_right _ hlead_pos_ = topTerm.natAbs := by dsimp only [topTerm]; rw [Int.natAbs_mul, Int.natAbs_pow]have htri : topTerm.natAbs ≤ ((F.eval u).eval x).natAbs + low.natAbs := byhave hrew : topTerm = (F.eval u).eval x + (-low) := by linarithcalc topTerm.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_ws F x (u.eval x) hD hU1have hpow_split : (u.eval x).natAbs ^ F.natDegree =(u.eval x).natAbs ^ (F.natDegree - 1) * (u.eval x).natAbs := byconv_lhs => rw [← Nat.sub_add_cancel hD, pow_succ]have h1 : (u.eval x).natAbs ^ (F.natDegree - 1) * (2 * polyL1At F x) ≤(u.eval x).natAbs ^ (F.natDegree - 1) * (u.eval x).natAbs :=Nat.mul_le_mul_left _ (by omega)have h2 : 2 * low.natAbs ≤ (u.eval x).natAbs ^ F.natDegree := byrw [hpow_split]; linarithhave h3 : (u.eval x).natAbs ^ F.natDegree ≤ 2 * ((F.eval u).eval x).natAbs := by omegahave h4 : (u.eval x).natAbs ≤ (u.eval x).natAbs ^ F.natDegree :=Nat.le_self_pow (by omega) (u.eval x).natAbsomegaprivate lemma cauchy_dominance_univar_ws (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 hpos : 1 ≤ polyIntL1 w := byrw [polyIntL1_eq_sum_range_ws w (N := w.natDegree + 1)(fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega)]rw [Finset.sum_range_succ]have hlc : 1 ≤ (w.coeff w.natDegree).natAbs :=Int.natAbs_pos.mpr (Polynomial.leadingCoeff_ne_zero.mpr hne)omegaomegahave hev : w.eval x = ∑ j ∈ Finset.range (w.natDegree + 1), w.coeff j * x ^ j :=Polynomial.eval_eq_sum_range xrw [Finset.sum_range_succ] at hevset 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 only [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_ws 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-`ℕ` telescoping geometric sum for filtered subsets of `range n`:`1 + ∑_{j < n, p j} 2 ^ |{i < j : p i}| = 2 ^ |{i < n : p i}|`. -/lemma one_add_sum_two_pow_filter (p : ℕ → Prop) [DecidablePred p] :∀ n : ℕ,1 + ∑ j ∈ (Finset.range n).filter p, 2 ^ ((Finset.range j).filter p).card =2 ^ ((Finset.range n).filter p).card := byintro ninduction n with| zero => simp| succ m ih =>rw [Finset.range_add_one, Finset.filter_insert]have hnot : m ∉ (Finset.range m).filter p := by simpsplit_ifs with hm· rw [Finset.sum_insert hnot, Finset.card_insert_of_notMem hnot, pow_succ]omega· exact ih/-- `intRootCount (w ^ k) = intRootCount w` for any `w : Polynomial ℤ` and `k ≥ 1`. -/lemma intRootCount_pow_eq {w : Polynomial ℤ} {k : ℕ} (hk : 1 ≤ k) :intRootCount (w ^ k) = intRootCount w := byby_cases hw : w = 0· subst hw; rw [zero_pow (by omega)]· have hwk : w ^ k ≠ 0 := pow_ne_zero k hwhave hset : intRootSet (w ^ k) = intRootSet w := byext xsimp only [intRootSet, Multiset.mem_toFinset, Polynomial.mem_roots hwk,Polynomial.mem_roots hw, Polynomial.IsRoot.def, Polynomial.eval_pow]exact ⟨fun h => eq_zero_of_pow_eq_zero h, fun h => by rw [h, zero_pow (by omega)]⟩unfold intRootCountrw [hset]/-- `intRootSet (C c) = ∅` for every nonzero integer constant `c ≠ 0`. -/lemma intRootSet_C_eq_empty {c : ℤ} (hc : c ≠ 0) :intRootSet (Polynomial.C c) = ∅ := byext xsimp only [intRootSet, Multiset.mem_toFinset, Polynomial.mem_roots (Polynomial.C_ne_zero.mpr hc),Polynomial.IsRoot.def, Polynomial.eval_C, Finset.notMem_empty, iff_false]exact hcprivate lemma natDegree_C_sub_X (p : Polynomial ℤ) :(Polynomial.C p - Polynomial.X : Polynomial (Polynomial ℤ)).natDegree = 1 := byhave heq : Polynomial.C p - Polynomial.X = Polynomial.C (-1 : Polynomial ℤ) * Polynomial.X + Polynomial.C p := bysimp; ringrw [heq]have hm1_ne : (-1 : Polynomial ℤ) ≠ 0 := by intro h; have := congrArg (Polynomial.eval 0) h; simp at thishave hdeg_left : (Polynomial.C (-1 : Polynomial ℤ) * Polynomial.X).natDegree = 1 :=Polynomial.natDegree_C_mul_X _ hm1_nehave hlt : (Polynomial.C p).natDegree < (Polynomial.C (-1 : Polynomial ℤ) * Polynomial.X).natDegree := byrw [Polynomial.natDegree_C, hdeg_left]; omegarw [Polynomial.natDegree_add_eq_left_of_natDegree_lt hlt, hdeg_left]private lemma leadingCoeff_C_sub_X (p : Polynomial ℤ) :(Polynomial.C p - Polynomial.X : Polynomial (Polynomial ℤ)).leadingCoeff = -1 := byrw [Polynomial.leadingCoeff, natDegree_C_sub_X, Polynomial.coeff_sub, Polynomial.coeff_C,Polynomial.coeff_X]simp/-! ## 2. Prefix Compatibility for Gates and `UntappedSeg` -/lemma SLP.prefix_prefix_eq :∀ {τ : ℕ} (P : SLP τ) (t₁ t₂ : ℕ) (h12 : t₁ ≤ t₂) (h2 : t₂ ≤ τ),(P.prefix t₂ h2).prefix t₁ h12 = P.prefix t₁ (le_trans h12 h2)| 0, .nil, t₁, t₂, h12, h2 => byhave ht1 : t₁ = 0 := by omegahave ht2 : t₂ = 0 := by omegasubst ht1; subst ht2; rfl| τ + 1, .snoc P op i j, t₁, t₂, h12, h2 => byby_cases ht2 : t₂ ≤ τ· rw [SLP.prefix_snoc_le P op i j ht2, SLP.prefix_snoc_le P op i j (le_trans h12 ht2)]exact P.prefix_prefix_eq t₁ t₂ h12 ht2· have heq : t₂ = τ + 1 := by omegasubst heqrw [SLP.prefix_self]lemma SLP.gateOp_prefix {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t ≤ τ) (k : ℕ) (hk : k < t) :(P.prefix t ht).gateOp k hk = P.gateOp k (lt_of_lt_of_le hk ht) := byinduction τ with| zero => omega| succ τ' ih =>cases P with| snoc P' op i j =>by_cases hle : t ≤ τ'· rw [SLP.prefix_snoc_le P' op i j hle]have hk' : k < τ' := lt_of_lt_of_le hk hlesimp only [SLP.gateOp, hk', ↓reduceDIte]exact ih P' hle· have heq : t = τ' + 1 := by omegasubst heqrw [SLP.prefix_self]lemma SLP.gateIn1_prefix_val {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t ≤ τ) (k : ℕ) (hk : k < t) :((P.prefix t ht).gateIn1 k hk).val = (P.gateIn1 k (lt_of_lt_of_le hk ht)).val := byinduction τ with| zero => omega| succ τ' ih =>cases P with| snoc P' op i j =>by_cases hle : t ≤ τ'· rw [SLP.prefix_snoc_le P' op i j hle]have hk' : k < τ' := lt_of_lt_of_le hk hlesimp only [SLP.gateIn1, hk', ↓reduceDIte]exact ih P' hle· have heq : t = τ' + 1 := by omegasubst heqrw [SLP.prefix_self]lemma SLP.gateIn2_prefix_val {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t ≤ τ) (k : ℕ) (hk : k < t) :((P.prefix t ht).gateIn2 k hk).val = (P.gateIn2 k (lt_of_lt_of_le hk ht)).val := byinduction τ with| zero => omega| succ τ' ih =>cases P with| snoc P' op i j =>by_cases hle : t ≤ τ'· rw [SLP.prefix_snoc_le P' op i j hle]have hk' : k < τ' := lt_of_lt_of_le hk hlesimp only [SLP.gateIn2, hk', ↓reduceDIte]exact ih P' hle· have heq : t = τ' + 1 := by omegasubst heqrw [SLP.prefix_self]lemma SLP.gateIsTopSq_prefix {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t ≤ τ) (k : ℕ) (hk : k < t) :(P.prefix t ht).gateIsTopSq k hk ↔ P.gateIsTopSq k (lt_of_lt_of_le hk ht) := byunfold SLP.gateIsTopSq isTopPureSquarerw [P.prefix_prefix_eq k t (le_of_lt hk) ht, P.gateOp_prefix t ht k hk]have h1 : (P.prefix t ht).gateIn1 k hk = P.gateIn1 k (lt_of_lt_of_le hk ht) :=Fin.ext (P.gateIn1_prefix_val t ht k hk)have h2 : (P.prefix t ht).gateIn2 k hk = P.gateIn2 k (lt_of_lt_of_le hk ht) :=Fin.ext (P.gateIn2_prefix_val t ht k hk)rw [h1, h2]/-- Restrict an `UntappedSeg P` with `s.b ≤ t` to the prefix `P.prefix t ht`. -/def UntappedSeg.restrictPrefix {τ : ℕ} {P : SLP τ} (s : UntappedSeg P)(t : ℕ) (ht : t ≤ τ) (hbt : s.b ≤ t) : UntappedSeg (P.prefix t ht) wherea := s.ab := s.bhab := s.habhb := hbthrun := fun k hk h1 h2 => (P.gateIsTopSq_prefix t ht k hk).mpr (s.hrun k (lt_of_lt_of_le hk ht) h1 h2)huntapped := fun k hk hbk => byhave hu := s.huntapped k (lt_of_lt_of_le hk ht) hbkrw [P.gateIn1_prefix_val t ht k hk, P.gateIn2_prefix_val t ht k hk]exact hu/-- `evalFormalT` on `P.prefix c hc` agrees with `evalFormalT` on `P`. -/lemma evalFormalT_prefix_eq {τ : ℕ} (P : SLP τ) (a b : ℕ) (hab : a < b)(c : ℕ) (hbc : b ≤ c) (hc : c ≤ τ) :∀ (t : ℕ) (hbt : b ≤ t) (htc : t ≤ c) (w : Fin (t + 2)),evalFormalT (P.prefix c hc) a b hab hbc t hbt htc w =evalFormalT P a b hab (le_trans hbc hc) t hbt (le_trans htc hc) w := byintro tinduction t using Nat.strong_induction_on with| ind t ih =>intro hbt htc wrw [evalFormalT.eq_def, evalFormalT.eq_def]split_ifs with hw hwa hwb· rw [P.evalWire_prefix c hc ⟨w.val, by omega⟩]· rfl· rfl· dsimp onlyset t' := w.val - 2have ht'_lt : t' < t := by omegahave hbt' : b ≤ t' := by omegahave ht'_lt_c : t' < c := by omegahave h1_eq : (P.prefix c hc).gateIn1 t' ht'_lt_c = P.gateIn1 t' (lt_of_lt_of_le ht'_lt_c hc) :=Fin.ext (P.gateIn1_prefix_val c hc t' ht'_lt_c)have h2_eq : (P.prefix c hc).gateIn2 t' ht'_lt_c = P.gateIn2 t' (lt_of_lt_of_le ht'_lt_c hc) :=Fin.ext (P.gateIn2_prefix_val c hc t' ht'_lt_c)rw [P.gateOp_prefix c hc t' ht'_lt_c, h1_eq, h2_eq]rw [ih t' ht'_lt hbt' (le_of_lt ht'_lt_c) _, ih t' ht'_lt hbt' (le_of_lt ht'_lt_c) _]/-! ## 3. Multi-Segment Gap-Dominance Root-Set Cover -//-- Helper: next boundary gate index after segment `r` in a list `L`. -/def segNextC {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (r : Fin L.length) : ℕ :=if h : r.val + 1 < L.length then (L[r.val + 1]'h).a else τlemma segsOrdered_adj {τ : ℕ} {P : SLP τ} :∀ (L : List (UntappedSeg P)), SegsOrdered L →∀ (i : ℕ) (hi : i + 1 < L.length), (L[i]'(by omega)).b ≤ (L[i + 1]'hi).a| [], _, _, hi => by simp at hi| [_], _, _, hi => by simp at hi| s0 :: s1 :: rest, ho, 0, _ => ho.1| _ :: s1 :: rest, ho, i + 1, hi =>segsOrdered_adj (s1 :: rest) ho.2 i (by simpa using hi)lemma segNextC_ge {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (hord : SegsOrdered L)(r : Fin L.length) : (L[r.val]'r.isLt).b ≤ segNextC L r := byunfold segNextCsplit_ifs with h· exact segsOrdered_adj L hord r.val h· exact (L[r.val]'r.isLt).hblemma segNextC_le {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (r : Fin L.length) :segNextC L r ≤ τ := byunfold segNextCsplit_ifs with h· exact le_of_lt (lt_of_lt_of_le (L[r.val + 1]'h).hab (L[r.val + 1]'h).hb)· exact le_rfl/-- Target wire after segment `r` in `L` (input to segment `r + 1`, or output wire `τ + 1`). -/def segWTarget {τ : ℕ} (P : SLP τ) (L : List (UntappedSeg P)) (r : Fin L.length) :Fin (segNextC L r + 2) :=if h : r.val + 1 < L.length thenlet sNext := L[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)).isLtunfold segNextC; rw [dite_eq_left_of_eq_true (eq_true h)]; exact hlt⟩else⟨τ + 1, byunfold segNextC; rw [dite_eq_right_of_eq_false (eq_false h)]; omega⟩/-- Formal polynomial `F_r(x, T) ∈ (Polynomial ℤ)[X]` for segment `r` of `L`. -/noncomputable def segFormalPoly {τ : ℕ} (P : SLP τ) (L : List (UntappedSeg P)) (hord : SegsOrdered L)(r : Fin L.length) : Polynomial (Polynomial ℤ) :=evalFormalT P (L[r.val]'r.isLt).a (L[r.val]'r.isLt).b (L[r.val]'r.isLt).hab (L[r.val]'r.isLt).hb(segNextC L r) (segNextC_ge L hord r) (segNextC_le L r) (segWTarget P L r)/-- **Multi-Segment Gap-Dominance Root-Set Cover:**Given a nonempty ordered list `L` of untapped pure-squaring segments of `P : SLP τ` (`P.evalPoly ≠ 0`)satisfying the local gap inequalities, if the top segment's formal polynomial `segFormalPoly P L hord qlast`has `natDegree ≥ 1`, then **every** `segFormalPoly P L hord r` has `natDegree ≥ 1`, and every integerroot of `P.evalPoly` either kills some leading coefficient `(segFormalPoly P L hord r).leadingCoeff`or satisfies `w_0(x) ∈ {-1, 0, 1}`! -/theorem slp_gap_dominance_root_cover {τ : ℕ} (P : SLP τ) (hP : P.evalPoly ≠ 0)(L : List (UntappedSeg P)) (hL_ne : L ≠ []) (hord : SegsOrdered L)(hhead_gap : ∀ (hpos : 0 < L.length),2 * (L[0]'hpos).a + (segNextC L ⟨0, hpos⟩ - (L[0]'hpos).b) + 5 ≤(L[0]'hpos).b - (L[0]'hpos).a)(hmid_gap : ∀ (r : ℕ) (hr : r + 1 < L.length),((L[r + 1]'hr).a - (L[r]'(by omega)).b) +(segNextC L ⟨r + 1, hr⟩ - (L[r + 1]'hr).b) + 3 ≤(L[r + 1]'hr).b - (L[r + 1]'hr).a)(htop_pos : 1 ≤ (segFormalPoly P L hord ⟨L.length - 1, List.length_pos_of_ne_nil hL_ne |> fun h => by omega⟩).natDegree) :(∀ r : Fin L.length, 1 ≤ (segFormalPoly P L hord r).natDegree) ∧let hq_pos : 0 < L.length := List.length_pos_of_ne_nil hL_nelet s0 := L[0]'hq_poslet 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)).isLthave := s0.hab; have := s0.hb; omega⟩intRootSet P.evalPoly ⊆(Finset.univ.biUnion (fun r : Fin L.length => intRootSet (segFormalPoly P L hord r).leadingCoeff)) ∪intRootSet (w0 - 1) ∪ intRootSet w0 ∪ intRootSet (w0 + 1) := byclassicalhave hq_pos : 0 < L.length := List.length_pos_of_ne_nil hL_nelet seg : Fin L.length → UntappedSeg P := fun r => L[r.val]'r.isLtlet nextC : Fin L.length → ℕ := segNextC Llet wTarget : (r : Fin L.length) → Fin (nextC r + 2) := segWTarget P Llet F : Fin L.length → Polynomial (Polynomial ℤ) := segFormalPoly P L hordlet qlast : Fin L.length := ⟨L.length - 1, by omega⟩let s0 : UntappedSeg P := L[0]'hq_poslet 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)).isLthave := s0.hab; have := s0.hb; omega⟩have hseg_ord : ∀ (r : Fin L.length) (hr : r.val + 1 < L.length),(seg r).b ≤ (seg ⟨r.val + 1, hr⟩).a := fun r hr => segsOrdered_adj L hord r.val hrhave hqlast_not : ¬ (qlast.val + 1 < L.length) := by dsimp [qlast]; omegahave 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, segWTarget]; rw [dite_eq_right_of_eq_false (eq_false hqlast_not)]have := (seg qlast).hb; omegahave hspec := evalFormalT_spec P (seg qlast) (nextC qlast) (segNextC_ge L hord qlast)(segNextC_le L qlast) (wTarget qlast) hnothave hw : (wTarget qlast).val = τ + 1 := bydsimp only [wTarget, segWTarget]; rw [dite_eq_right_of_eq_false (eq_false hqlast_not)]simpa [F, segFormalPoly, SLP.evalPoly, hw] using hspec-- Step 1: Every r : Fin L.length has 1 ≤ (F r).natDegreehave 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, segWTarget]; 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 hnc_le_r : nextC r ≤ τ := segNextC_le L rhave hspec := evalFormalT_spec P (seg r) (nextC r) (segNextC_ge L hord r)hnc_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; omega⟩ at hspecrw [hF_eq, Polynomial.eval_C] at hspechave 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; 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, segWTarget]; 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; 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⟩have hcoeff_le : ((F r).coeff 0).natDegree ≤ 2 ^ (nextC r - (seg r).b) * (P.prefix (seg r).a (by have := (seg r).hab; have := (seg r).hb; omega)).maxDeg :=evalFormalT_coeff_natDegree_le P (seg r).a (seg r).b (seg r).hab (seg r).hb(nextC r) (segNextC_ge L hord r) hnc_le_r (wTarget r) 0have hgap_gt : nextC r - (seg r).b < (seg r).b - (seg r).a := bycases hr0 : r.val with| zero =>have hh := hhead_gap hq_poshave hr_eq : r = ⟨0, hq_pos⟩ := Fin.ext hr0rw [hr_eq]; dsimp only [seg, nextC]; omega| succ m =>have hm := hmid_gap m (by omega)have hr_eq : r = ⟨m + 1, by omega⟩ := Fin.ext hr0rw [hr_eq]; dsimp only [seg, nextC]; 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; exact htop_posrefine ⟨hF_deg_pos, ?_⟩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 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_maxDeg-- Step 2: Backward Cauchy propagationdsimp onlyintro 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 hkilllet 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_ws (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) (segNextC_ge L hord qlast) (segNextC_le L 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, segNextC]; rw [dite_eq_left_of_eq_true (eq_true hr_lt)]exact congrArg UntappedSeg.a (congrArg seg 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 _ xby_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) (segNextC_ge L hord r) (segNextC_le L 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_ws (F r)(P.evalWire ⟨(seg r).b + 1, by have := (seg r).hb; omega⟩) x(hF_deg_pos r) (hkill r) hbig_l1have hw_val : (wTarget r).val =(P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).val := bydsimp only [wTarget, segWTarget]; 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 segNextC_ge L hord rexact ((seg r).huntapped (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega) hbr).1have hnc_le_r : nextC r ≤ τ := segNextC_le L rhave hspec := evalFormalT_spec P (seg r) (nextC r) (segNextC_ge L hord r)hnc_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; omega⟩ at hspecrw [hspec] at hdomset w1_val := ((P.evalWire ⟨(wTarget r).val, by have := (wTarget r).isLt; omega⟩).eval x).natAbshave hU_le_sq : U ≤ w1_val ^ 2 := nat_half_sq_ge_ws hU4 hdomset 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 :=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 thishave hih : P_prop r1 := ihd (by omega)dsimp only [P_prop] at hihrw [hU1_eq] at hihhave hH1_le := wireHeight_step_le P (seg r) x (nextC r) (segNextC_ge L hord r) (segNextC_le L 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) (segNextC_le L r) x ≤ U ^ (2 ^ Rr) at hH1_lesimpa only [hnext_eq] using hH1_lehave hg1_ge : Rr + (nextC r1 - (seg r1).b) + 3 ≤ g1 := byhave hmid := hmid_gap r.val hr_lthave hRr_eq : Rr = (seg r1).a - (seg r).b := by rw [← hnext_eq]have hseg_r1 : L[r.val + 1]'hr_lt = seg r1 := congrArg seg hfin_eqhave hnc1 : segNextC L ⟨r.val + 1, hr_lt⟩ = nextC r1 := congrArg nextC hfin_eqrw [hRr_eq]; dsimp only [g1]rw [← hseg_r1, ← hnc1]; exact 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-- Evaluate at r = 0have 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 hprop0set R0 := s0.aset R1 := nextC ⟨0, hq_pos⟩ - s0.bset g0 := s0.b - s0.ahave hg0_ge : 2 * R0 + R1 + 5 ≤ g0 := bysimpa [R0, R1, g0, s0, nextC] using hhead_gap hq_poshave 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 hprop0have 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 _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· 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_ws w0 x hw0_deg_pos(le_trans (Nat.mul_le_mul_left 2 hl1_w0) hx_big)have hx_sq : x.natAbs ≤ (w0.eval x).natAbs ^ 2 := nat_half_sq_ge_ws 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· have hmax_le : max 2 x.natAbs ≤ 2 ^ (2 ^ R0 + 1) := byhave : 2 * 2 ^ (2 ^ R0) = 2 ^ (2 ^ R0 + 1) := by rw [pow_succ, Nat.mul_comm]have h2le : 2 ≤ 2 ^ (2 ^ R0 + 1) :=Nat.le_self_pow (Nat.succ_ne_zero _) 2exact max_le h2le (by omega)have hH0_le2 : wireHeight P s0.a (by have := s0.hab; have := s0.hb; omega) x ≤ 2 ^ (2 ^ (2 * R0 + 1)) := bycalc wireHeight P s0.a (by have := s0.hab; have := s0.hb; omega) 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 h2pos : 1 ≤ 2 ^ R0 := Nat.one_le_pow _ _ (by omega)have hadd : 2 ^ R0 + 1 ≤ 2 * 2 ^ R0 := by omegacalc (2 ^ R0 + 1) * 2 ^ R0≤ (2 * 2 ^ R0) * 2 ^ R0 := Nat.mul_le_mul_right _ hadd_ = 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_le2 __ = 2 ^ (2 ^ (2 * R0 + 1) * 2 ^ (R1 + 1)) := (pow_mul _ _ _).symm_ = 2 ^ (2 ^ (2 * R0 + R1 + 2)) := byrw [← 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-- Therefore w0.eval x ∈ {-1, 0, 1}have hcases : w0.eval x = -1 ∨ w0.eval x = 0 ∨ w0.eval x = 1 := by omegahave hw0_ne : w0 ≠ 0 := fun h0 => by simp [h0] at hw0_deg_poshave hw0_sub_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_add_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_posrcases hcases with hm1 | h0 | hp1· apply Finset.mem_union_rightrw [intRootSet, Multiset.mem_toFinset, Polynomial.mem_roots hw0_add_ne, Polynomial.IsRoot.def]simp [hm1]· 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_left; apply Finset.mem_union_left; apply Finset.mem_union_rightrw [intRootSet, Multiset.mem_toFinset, Polynomial.mem_roots hw0_sub_ne, Polynomial.IsRoot.def]simp [hp1]/-! ## 4. Leading-Coefficient Classification at Gap `R_r ≤ 1` -//-- Predicate: segment `s` has a trailing gate at `s.b < c` which is a **non-squaringmultiplication** of the segment output wire `s.b + 1` with an earlier wire `≤ s.a + 1`. -/def segIsNonSqMul {τ : ℕ} (P : SLP τ) (s : UntappedSeg P) (c : ℕ) (hc : c ≤ τ) : Prop :=∃ (hbc : s.b < c),let hb_lt : s.b < τ := lt_of_lt_of_le hbc hcP.gateOp s.b hb_lt = .mul ∧(((P.gateIn1 s.b hb_lt).val = s.b + 1 ∧ (P.gateIn2 s.b hb_lt).val ≤ s.a + 1) ∨((P.gateIn2 s.b hb_lt).val = s.b + 1 ∧ (P.gateIn1 s.b hb_lt).val ≤ s.a + 1))/-- Every `segIsNonSqMul` gate `s.b` is a `.mul` gate with distinct inputs (`gateIn1 ≠ gateIn2`),and hence strictly a **non-top-squaring** gate (`¬ P.gateIsTopSq s.b _`). -/lemma segIsNonSqMul_not_topSq {τ : ℕ} {P : SLP τ} {s : UntappedSeg P} {c : ℕ} {hc : c ≤ τ}(h : segIsNonSqMul P s c hc) :∃ (hb_lt : s.b < τ), P.gateOp s.b hb_lt = .mul ∧ ¬ P.gateIsTopSq s.b hb_lt := byrcases h with ⟨hbc, hop, hcases⟩refine ⟨lt_of_lt_of_le hbc hc, hop, fun htop => ?_⟩have heq_in : (P.gateIn1 s.b (lt_of_lt_of_le hbc hc)).val =(P.gateIn2 s.b (lt_of_lt_of_le hbc hc)).val := congrArg Fin.val htop.2.1have hab := s.habrcases hcases with ⟨h1, h2⟩ | ⟨h2, h1⟩ <;> omega/-- Value of `evalFormalT` at a wire `v < b + 2` (which is not an intermediate wire of `s`). -/private lemma evalFormalT_at_b {τ : ℕ} (P : SLP τ) (s : UntappedSeg P) (v : Fin (s.b + 2))(hnot : ¬ (s.a + 2 ≤ v.val ∧ v.val ≤ s.b)) :(v.val ≤ s.a + 1 ∧evalFormalT P s.a s.b s.hab s.hb s.b le_rfl s.hb v =Polynomial.C (P.evalWire ⟨v.val, by have := v.isLt; have := s.hb; omega⟩)) ∨(v.val = s.b + 1 ∧evalFormalT P s.a s.b s.hab s.hb s.b le_rfl s.hb v = Polynomial.X) := byrw [evalFormalT.eq_def]by_cases hv : v.val < s.b + 1· have hva : v.val ≤ s.a + 1 := by omegaleftrefine ⟨hva, ?_⟩simp [hv, hva]· have hvb : v.val = s.b + 1 := by have := v.isLt; omegarightrefine ⟨hvb, ?_⟩simp [hvb]/-- Step equation for `evalFormalT` at `w.val = s.b + 2` (`c = s.b + 1`). -/private lemma evalFormalT_step_b_add_two {τ : ℕ} (P : SLP τ) (s : UntappedSeg P)(c : ℕ) (hbc : s.b ≤ c) (hc : c ≤ τ) (w : Fin (c + 2)) (hwb : w.val = s.b + 2) :let hb_lt : s.b < τ := by have := w.isLt; omegaevalFormalT P s.a s.b s.hab s.hb c hbc hc w =(P.gateOp s.b hb_lt).applyPolyT(evalFormalT P s.a s.b s.hab s.hb s.b le_rfl s.hb (P.gateIn1 s.b hb_lt))(evalFormalT P s.a s.b s.hab s.hb s.b le_rfl s.hb (P.gateIn2 s.b hb_lt)) := byintro hb_ltrw [evalFormalT.eq_def]have hw1 : ¬ (w.val < s.b + 1) := by omegahave hw2 : ¬ (w.val = s.b + 1) := by omegarw [dite_eq_right_of_eq_false (eq_false hw1), dite_eq_right_of_eq_false (eq_false hw2)]dsimp onlyhave ht'_eq : w.val - 2 = s.b := by omegahave hcongr : ∀ {t₁ t₂ : ℕ} (h1 : t₁ < τ) (h2 : t₂ < τ) (hb1 : s.b ≤ t₁) (hb2 : s.b ≤ t₂),t₁ = t₂ →(P.gateOp t₁ h1).applyPolyT(evalFormalT P s.a s.b s.hab s.hb t₁ hb1 (le_of_lt h1) (P.gateIn1 t₁ h1))(evalFormalT P s.a s.b s.hab s.hb t₁ hb1 (le_of_lt h1) (P.gateIn2 t₁ h1)) =(P.gateOp t₂ h2).applyPolyT(evalFormalT P s.a s.b s.hab s.hb t₂ hb2 (le_of_lt h2) (P.gateIn1 t₂ h2))(evalFormalT P s.a s.b s.hab s.hb t₂ hb2 (le_of_lt h2) (P.gateIn2 t₂ h2)) := byrintro t₁ _ h1 h2 hb1 hb2 rfl; rflexact hcongr _ hb_lt _ le_rfl ht'_eq/-- **Exact Leading-Coefficient Classification at `c ≤ s.b + 1`:**For any untapped segment `s` and `s.b ≤ c ≤ s.b + 1`, if `F = evalFormalT ... c ... w` has`1 ≤ F.natDegree` (with `w` not an intermediate wire of `s`), then:- Either `intRootSet F.leadingCoeff = ∅` (leading coefficient in `{-1, 1, 2}`), OR- `segIsNonSqMul P s c hc` holds AND `F.leadingCoeff = P.evalWire u` for some wire `u < s.a + 2`! -/lemma evalFormalT_leadingCoeff_of_step_le_one {τ : ℕ} (P : SLP τ) (s : UntappedSeg P)(c : ℕ) (hbc : s.b ≤ c) (hc : c ≤ τ) (hstep : c ≤ s.b + 1)(w : Fin (c + 2)) (hnot : ¬ (s.a + 2 ≤ w.val ∧ w.val ≤ s.b))(hdeg : 1 ≤ (evalFormalT P s.a s.b s.hab s.hb c hbc hc w).natDegree) :intRootSet (evalFormalT P s.a s.b s.hab s.hb c hbc hc w).leadingCoeff = ∅ ∨(segIsNonSqMul P s c hc ∧∃ u : Fin (s.a + 2),(evalFormalT P s.a s.b s.hab s.hb c hbc hc w).leadingCoeff =P.evalWire ⟨u.val, by have := u.isLt; have := s.hab; have := s.hb; omega⟩) := byset F := evalFormalT P s.a s.b s.hab s.hb c hbc hc wby_cases hw1 : w.val < s.b + 1· have hwa : w.val ≤ s.a + 1 := by omegahave hF_eq : F = Polynomial.C (P.evalWire ⟨w.val, by omega⟩) := bydsimp only [F]; rw [evalFormalT.eq_def]; simp [hw1, hwa]rw [hF_eq, Polynomial.natDegree_C] at hdeg; omega· by_cases hw2 : w.val = s.b + 1· have hF_eq : F = Polynomial.X := bydsimp only [F]; rw [evalFormalT.eq_def]; simp [hw2]left; rw [hF_eq, Polynomial.leadingCoeff_X]exact intRootSet_C_eq_empty (c := 1) (by decide)· have hwb : w.val = s.b + 2 := by have := w.isLt; omegahave hbc_lt : s.b < c := by have := w.isLt; omegahave hb_lt : s.b < τ := lt_of_lt_of_le hbc_lt hchave hF_step : F = (P.gateOp s.b hb_lt).applyPolyT(evalFormalT P s.a s.b s.hab s.hb s.b le_rfl s.hb (P.gateIn1 s.b hb_lt))(evalFormalT P s.a s.b s.hab s.hb s.b le_rfl s.hb (P.gateIn2 s.b hb_lt)) :=evalFormalT_step_b_add_two P s c hbc hc w hwbhave hu := s.huntapped s.b hb_lt le_rflrcases evalFormalT_at_b P s (P.gateIn1 s.b hb_lt) hu.1 with ⟨h1a, hA⟩ | ⟨h1b, hA⟩ <;>rcases evalFormalT_at_b P s (P.gateIn2 s.b hb_lt) hu.2 with ⟨h2a, hB⟩ | ⟨h2b, hB⟩· rw [hA, hB] at hF_stepcases hop : P.gateOp s.b hb_lt <;> rw [hop] at hF_step <;>simp only [SLPOp.applyPolyT] at hF_step· rw [← Polynomial.C_add] at hF_step; rw [hF_step, Polynomial.natDegree_C] at hdeg; omega· rw [← Polynomial.C_sub] at hF_step; rw [hF_step, Polynomial.natDegree_C] at hdeg; omega· rw [← Polynomial.C_mul] at hF_step; rw [hF_step, Polynomial.natDegree_C] at hdeg; omega· set p := P.evalWire ⟨(P.gateIn1 s.b hb_lt).val, by have := (P.gateIn1 s.b hb_lt).isLt; omega⟩rw [hA, hB] at hF_stepcases hop : P.gateOp s.b hb_lt <;> rw [hop] at hF_step <;>simp only [SLPOp.applyPolyT] at hF_step· leftrw [hF_step, add_comm, Polynomial.leadingCoeff_X_add_C]exact intRootSet_C_eq_empty (c := 1) (by decide)· leftrw [hF_step, leadingCoeff_C_sub_X]have hm1 : (-1 : Polynomial ℤ) = Polynomial.C (-1) := by simprw [hm1]; exact intRootSet_C_eq_empty (c := -1) (by decide)· rightrefine ⟨⟨hbc_lt, hop, Or.inr ⟨h2b, h1a⟩⟩, ⟨(P.gateIn1 s.b hb_lt).val, by omega⟩, ?_⟩rw [hF_step, Polynomial.leadingCoeff_C_mul_X]· set q := P.evalWire ⟨(P.gateIn2 s.b hb_lt).val, by have := (P.gateIn2 s.b hb_lt).isLt; omega⟩rw [hA, hB] at hF_stepcases hop : P.gateOp s.b hb_lt <;> rw [hop] at hF_step <;>simp only [SLPOp.applyPolyT] at hF_step· leftrw [hF_step, Polynomial.leadingCoeff_X_add_C]exact intRootSet_C_eq_empty (c := 1) (by decide)· leftrw [hF_step, sub_eq_add_neg, ← map_neg, Polynomial.leadingCoeff_X_add_C]exact intRootSet_C_eq_empty (c := 1) (by decide)· rightrefine ⟨⟨hbc_lt, hop, Or.inl ⟨h1b, h2a⟩⟩, ⟨(P.gateIn2 s.b hb_lt).val, by omega⟩, ?_⟩rw [hF_step, mul_comm, Polynomial.leadingCoeff_C_mul_X]· rw [hA, hB] at hF_stepcases hop : P.gateOp s.b hb_lt <;> rw [hop] at hF_step <;>simp only [SLPOp.applyPolyT] at hF_step· lefthave h2x : (Polynomial.X + Polynomial.X : Polynomial (Polynomial ℤ)) =Polynomial.C (Polynomial.C 2) * Polynomial.X := bysimp only [map_ofNat]; ringrw [hF_step, h2x, Polynomial.leadingCoeff_C_mul_X]exact intRootSet_C_eq_empty (c := 2) (by decide)· rw [sub_self] at hF_step; rw [hF_step, Polynomial.natDegree_zero] at hdeg; omega· lefthave hx2 : (Polynomial.X * Polynomial.X : Polynomial (Polynomial ℤ)) = Polynomial.X ^ 2 := by ringrw [hF_step, hx2, Polynomial.leadingCoeff_X_pow]exact intRootSet_C_eq_empty (c := 1) (by decide)/-- If `c = s.b + 1`, `P.evalWire (s.b + 2) ≠ 0`, and gate `s.b` uses the top wire `s.b + 1`,then `evalFormalT` at wire `s.b + 2` automatically has `natDegree ≥ 1`! -/lemma evalFormalT_natDegree_pos_of_active {τ : ℕ} (P : SLP τ) (s : UntappedSeg P)(hb_lt : s.b < τ) (hval_ne : P.evalWire ⟨s.b + 2, by omega⟩ ≠ 0)(hact : (P.gateIn1 s.b hb_lt).val = s.b + 1 ∨ (P.gateIn2 s.b hb_lt).val = s.b + 1) :1 ≤ (evalFormalT P s.a s.b s.hab s.hb (s.b + 1) (Nat.le_succ _) hb_lt ⟨s.b + 2, by omega⟩).natDegree := byby_contra h0set F := evalFormalT P s.a s.b s.hab s.hb (s.b + 1) (Nat.le_succ _) hb_lt ⟨s.b + 2, by omega⟩have hdeg0 : F.natDegree = 0 := by omegahave hF_step : F = (P.gateOp s.b hb_lt).applyPolyT(evalFormalT P s.a s.b s.hab s.hb s.b le_rfl s.hb (P.gateIn1 s.b hb_lt))(evalFormalT P s.a s.b s.hab s.hb s.b le_rfl s.hb (P.gateIn2 s.b hb_lt)) :=evalFormalT_step_b_add_two P s (s.b + 1) (Nat.le_succ _) hb_lt ⟨s.b + 2, by omega⟩ rflhave hu := s.huntapped s.b hb_lt le_rflhave hnot_w : ¬ (s.a + 2 ≤ s.b + 2 ∧ s.b + 2 ≤ s.b) := by omegahave hspec := evalFormalT_spec P s (s.b + 1) (Nat.le_succ _) hb_lt ⟨s.b + 2, by omega⟩ hnot_wchange F.eval (P.evalWire ⟨s.b + 1, by omega⟩) = P.evalWire ⟨s.b + 2, by omega⟩ at hspecrcases evalFormalT_at_b P s (P.gateIn1 s.b hb_lt) hu.1 with ⟨h1a, hA⟩ | ⟨h1b, hA⟩ <;>rcases evalFormalT_at_b P s (P.gateIn2 s.b hb_lt) hu.2 with ⟨h2a, hB⟩ | ⟨h2b, hB⟩· have := s.hab; rcases hact with h | h <;> omega· set p := P.evalWire ⟨(P.gateIn1 s.b hb_lt).val, by have := (P.gateIn1 s.b hb_lt).isLt; omega⟩rw [hA, hB] at hF_stepcases hop : P.gateOp s.b hb_lt <;> rw [hop] at hF_step <;>simp only [SLPOp.applyPolyT] at hF_step· rw [hF_step, add_comm, Polynomial.natDegree_X_add_C] at hdeg0; omega· rw [hF_step, natDegree_C_sub_X] at hdeg0; omega· by_cases hp0 : p = 0· rw [hp0, map_zero, zero_mul] at hF_steprw [hF_step, Polynomial.eval_zero] at hspecexact hval_ne hspec.symm· rw [hF_step, Polynomial.natDegree_C_mul_X _ hp0] at hdeg0; omega· set q := P.evalWire ⟨(P.gateIn2 s.b hb_lt).val, by have := (P.gateIn2 s.b hb_lt).isLt; omega⟩rw [hA, hB] at hF_stepcases hop : P.gateOp s.b hb_lt <;> rw [hop] at hF_step <;>simp only [SLPOp.applyPolyT] at hF_step· rw [hF_step, Polynomial.natDegree_X_add_C] at hdeg0; omega· rw [hF_step, sub_eq_add_neg, ← map_neg, Polynomial.natDegree_X_add_C] at hdeg0; omega· by_cases hq0 : q = 0· rw [hq0, map_zero, mul_zero] at hF_steprw [hF_step, Polynomial.eval_zero] at hspecexact hval_ne hspec.symm· rw [hF_step, mul_comm, Polynomial.natDegree_C_mul_X _ hq0] at hdeg0; omega· rw [hA, hB] at hF_stepcases hop : P.gateOp s.b hb_lt <;> rw [hop] at hF_step <;>simp only [SLPOp.applyPolyT] at hF_step· have h2x : (Polynomial.X + Polynomial.X : Polynomial (Polynomial ℤ)) =Polynomial.C (Polynomial.C 2) * Polynomial.X := bysimp only [map_ofNat]; ringhave h2ne : (Polynomial.C (2 : ℤ) : Polynomial ℤ) ≠ 0 := Polynomial.C_ne_zero.mpr (by decide)rw [hF_step, h2x, Polynomial.natDegree_C_mul_X _ h2ne] at hdeg0; omega· rw [sub_self] at hF_steprw [hF_step, Polynomial.eval_zero] at hspecexact hval_ne hspec.symm· have hx2 : (Polynomial.X * Polynomial.X : Polynomial (Polynomial ℤ)) = Polynomial.X ^ 2 := by ringrw [hF_step, hx2, Polynomial.natDegree_X_pow] at hdeg0; omega/-! ## 5. Well-Spaced SLPs & Inductive Exponential-in-`m_mul` / `2 ^ (|B| + 1)` Bounds -//-- A list `L` of untapped pure-squaring segments of `P : SLP τ` is **well-spaced** if:1. `L` is nonempty and ordered (`s_0.b ≤ s_1.a`, etc.);2. Every inter-segment gap and trailing gap has at most 1 gate (`a_{i+1} ≤ b_i + 1`, `τ ≤ b_{last} + 1`);3. The first pure-squaring run has length `g_0 ≥ 2 * a_0 + 6`, and every subsequent run has `g_i ≥ 5`;4. Whenever an isolated gate exists at `b_i < nextC i`, it actively reads the segment output wire `b_i + 1`. -/structure WellSpacedSegs {τ : ℕ} (P : SLP τ) (L : List (UntappedSeg P)) : Prop wherehne : L ≠ []hord : SegsOrdered Lhstep_mid : ∀ i (hi : i + 1 < L.length), (L[i + 1]'hi).a ≤ (L[i]'(by omega)).b + 1hstep_last : ∀ (hpos : 0 < L.length), τ ≤ (L[L.length - 1]'(by omega)).b + 1hhead_gap : ∀ (hpos : 0 < L.length), 2 * (L[0]'hpos).a + 6 ≤ (L[0]'hpos).b - (L[0]'hpos).ahmid_gap : ∀ i (hi : i + 1 < L.length), 5 ≤ (L[i + 1]'hi).b - (L[i + 1]'hi).ahactive : ∀ i (hi : i < L.length) (hb_lt : (L[i]'hi).b < segNextC L ⟨i, hi⟩),(P.gateIn1 (L[i]'hi).b (lt_of_lt_of_le hb_lt (segNextC_le L ⟨i, hi⟩))).val = (L[i]'hi).b + 1 ∨(P.gateIn2 (L[i]'hi).b (lt_of_lt_of_le hb_lt (segNextC_le L ⟨i, hi⟩))).val = (L[i]'hi).b + 1/-- Predicate: index `i < L.length` has an isolated non-squaring multiplication at gate `(L[i]).b`. -/def isNonSqMulIdx {τ : ℕ} (P : SLP τ) (L : List (UntappedSeg P)) (i : ℕ) : Prop :=∃ (hi : i < L.length),segIsNonSqMul P (L[i]'hi) (segNextC L ⟨i, hi⟩) (segNextC_le L ⟨i, hi⟩)/-- Number of segments among the first `r` segments of `L` whose trailing gate is a non-squaringmultiplication. -/noncomputable def nonSqMulCount {τ : ℕ} (P : SLP τ) (L : List (UntappedSeg P)) (r : ℕ) : ℕ :=letI := Classical.propDecidable((Finset.range r).filter (isNonSqMulIdx P L)).card/-- Gate boundary index at stage `r ≤ L.length`: `(L[r]).a` if `r < L.length`, else `τ`. -/def stageBound {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (r : ℕ) : ℕ :=if h : r < L.length then (L[r]'h).a else τlemma stageBound_le {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (r : ℕ) :stageBound L r ≤ τ := byunfold stageBoundsplit_ifs with h· exact le_of_lt (lt_of_lt_of_le (L[r]'h).hab (L[r]'h).hb)· exact le_rfl/-- Helper: restrict the first `r + 1` segments of `L` to `P.prefix c hc` (where `c = segNextC L r`). -/private def restrictTake {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (hord : SegsOrdered L)(r : ℕ) (hr : r < L.length) (c : ℕ) (hc : c ≤ τ) (hbc : (L[r]'hr).b ≤ c) :List (UntappedSeg (P.prefix c hc)) :=(List.finRange (r + 1)).map fun j =>(L[j.val]'(by omega)).restrictPrefix c hc (byhave hle : ∀ (i k : ℕ) (hi : i < L.length) (hk : k < L.length), i ≤ k →(L[i]'hi).b ≤ (L[k]'hk).b := byintro i k hi hk hikinduction k with| zero =>have hi0 : i = 0 := by omegasimp only [hi0]; exact le_rfl| succ k' ihk =>by_cases heq : i = k' + 1· simp only [heq]; exact le_rfl· have hprev := ihk (by omega) (by omega)have hadj := segsOrdered_adj L hord k' hkhave hlt := (L[k' + 1]'hk).habomegaexact le_trans (hle j.val r (by omega) hr (by omega)) hbc)private lemma restrictTake_length {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (hord : SegsOrdered L)(r : ℕ) (hr : r < L.length) (c : ℕ) (hc : c ≤ τ) (hbc : (L[r]'hr).b ≤ c) :(restrictTake L hord r hr c hc hbc).length = r + 1 := bysimp [restrictTake]private lemma restrictTake_getElem_a {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (hord : SegsOrdered L)(r : ℕ) (hr : r < L.length) (c : ℕ) (hc : c ≤ τ) (hbc : (L[r]'hr).b ≤ c)(i : ℕ) (hi : i < (restrictTake L hord r hr c hc hbc).length) :((restrictTake L hord r hr c hc hbc)[i]'hi).a = (L[i]'(by rw [restrictTake_length] at hi; omega)).a := bysimp [restrictTake, UntappedSeg.restrictPrefix]private lemma restrictTake_getElem_b {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (hord : SegsOrdered L)(r : ℕ) (hr : r < L.length) (c : ℕ) (hc : c ≤ τ) (hbc : (L[r]'hr).b ≤ c)(i : ℕ) (hi : i < (restrictTake L hord r hr c hc hbc).length) :((restrictTake L hord r hr c hc hbc)[i]'hi).b = (L[i]'(by rw [restrictTake_length] at hi; omega)).b := bysimp [restrictTake, UntappedSeg.restrictPrefix]private lemma segsOrdered_of_adj {τ : ℕ} {P : SLP τ} :∀ (M : List (UntappedSeg P)),(∀ (i : ℕ) (hi : i + 1 < M.length), (M[i]'(by omega)).b ≤ (M[i + 1]'hi).a) →SegsOrdered M| [], _ => trivial| [_], _ => trivial| s0 :: s1 :: rest, h =>⟨h 0 (by simp), segsOrdered_of_adj (s1 :: rest) (fun i hi => h (i + 1) (by simpa using hi))⟩/-- **Inductive Wire-Level Well-Spaced Bound:**For every stage `r ≤ L.length` and every nonzero wire `u` before `stageBound L r + 2`,`intRootCount (P.evalWire u) ≤ 3 * 2 ^ (a_0 + nonSqMulCount P L r)`. -/theorem slp_well_spaced_wire_bound {τ : ℕ} (P : SLP τ) (L : List (UntappedSeg P))(hws : WellSpacedSegs P L) :∀ (r : ℕ) (_hr_le : r ≤ L.length) (u : Fin (τ + 2)),u.val < stageBound L r + 2 →P.evalWire u ≠ 0 →intRootCount (P.evalWire u) ≤3 * 2 ^ ((L[0]'(List.length_pos_of_ne_nil hws.hne)).a + nonSqMulCount P L r) := byclassicalhave hq_pos : 0 < L.length := List.length_pos_of_ne_nil hws.hneset a0 := (L[0]'hq_pos).aintro rinduction r using Nat.strong_induction_on with| ind r ih =>intro hr_le u hu hunecases r with| zero =>clear hr_lehave hu_a0 : u.val < a0 + 2 := bysimpa [stageBound, hq_pos, a0] using huhave ha0_le : a0 ≤ τ := le_of_lt (lt_of_lt_of_le (L[0]'hq_pos).hab (L[0]'hq_pos).hb)have hpr := P.evalWire_prefix a0 ha0_le ⟨u.val, hu_a0⟩have hdeg : (P.evalWire u).natDegree ≤ 2 ^ a0 := byhave heq : P.evalWire u = (P.prefix a0 ha0_le).evalWire ⟨u.val, hu_a0⟩ := hpr.symmrw [heq]exact le_trans ((P.prefix a0 ha0_le).wireDeg_le_maxDeg _) (P.prefix a0 ha0_le).maxDeg_le_two_powhave hrc : intRootCount (P.evalWire u) ≤ (P.evalWire u).natDegree :=le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _)have hm0 : nonSqMulCount P L 0 = 0 := by simp [nonSqMulCount]rw [hm0, Nat.add_zero]change intRootCount (P.evalWire u) ≤ 3 * 2 ^ a0omega| succ r' =>have hr'_lt : r' < L.length := by omegaset sr := L[r']'hr'_ltset cr := stageBound L (r' + 1)have hcr_eq : cr = segNextC L ⟨r', hr'_lt⟩ := rflhave hcr_le : cr ≤ τ := stageBound_le L (r' + 1)have hbr_le_cr : sr.b ≤ cr := segNextC_ge L hws.hord ⟨r', hr'_lt⟩have hstep_cr : cr ≤ sr.b + 1 := bydsimp only [cr, stageBound, sr]split_ifs with h· exact hws.hstep_mid r' h· have hr'_last : r' = L.length - 1 := by omegahave hlast := hws.hstep_last hq_poshave : L[L.length - 1]'(by omega) = L[r']'hr'_lt := by simp [hr'_last]simpa [this] using hlasthave hmon : nonSqMulCount P L r' ≤ nonSqMulCount P L (r' + 1) := byunfold nonSqMulCountexact Finset.card_le_card (Finset.filter_subset_filter _ (Finset.range_mono (Nat.le_succ r')))by_cases hu_early : u.val < sr.a + 2· have hprev := ih r' (Nat.lt_succ_self r') (le_of_lt hr'_lt) u(by simpa [stageBound, hr'_lt, sr] using hu_early) huneexact le_trans hprev (Nat.mul_le_mul_left 3 (Nat.pow_le_pow_right (by omega) (Nat.add_le_add_left hmon a0)))· by_cases hu_run : u.val ≤ sr.b + 1· have hab' : sr.a < u.val - 1 := by omegahave hb' : u.val - 1 ≤ τ := by have := sr.hb; omegahave hrun' : ∀ t (ht : t < τ), sr.a ≤ t → t < u.val - 1 → P.gateIsTopSq t ht :=fun t ht h1 h2 => sr.hrun t ht h1 (by omega)have hpow := (P.topSq_run_eval hab' hb' hrun').1have hu_eq : (⟨(u.val - 1) + 1, by have := u.isLt; omega⟩ : Fin (τ + 2)) = u := Fin.ext (by dsimp; omega)rw [hu_eq] at hpowset w_in : Fin (τ + 2) :=⟨(P.gateIn1 sr.a (by have := sr.hab; have := sr.hb; omega)).val,by have := (P.gateIn1 sr.a (by have := sr.hab; have := sr.hb; omega)).isLthave := sr.hab; have := sr.hb; omega⟩have hw_in_lt : w_in.val < sr.a + 2 := (P.gateIn1 sr.a _).isLthave hw_in_ne : P.evalWire w_in ≠ 0 := byintro h0rw [hpow, show P.evalWire w_in = 0 from h0, zero_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega)))] at huneexact hune rflhave hrc_eq : intRootCount (P.evalWire u) = intRootCount (P.evalWire w_in) := byrw [hpow]exact intRootCount_pow_eq (Nat.one_le_pow _ _ (by omega))rw [hrc_eq]have hprev := ih r' (Nat.lt_succ_self r') (le_of_lt hr'_lt) w_in(by simpa [stageBound, hr'_lt, sr] using hw_in_lt) hw_in_neexact le_trans hprev (Nat.mul_le_mul_left 3 (Nat.pow_le_pow_right (by omega) (Nat.add_le_add_left hmon a0)))· -- Main Case: u.val = sr.b + 2, cr = sr.b + 1, P.evalWire u = (P.prefix cr hcr_le).evalPoly!have hu_eq_b2 : u.val = sr.b + 2 := by omegahave hcr_eq_succ : cr = sr.b + 1 := by omegahave hbr_lt_cr : sr.b < cr := by omegahave hbr_lt_τ : sr.b < τ := lt_of_lt_of_le hbr_lt_cr hcr_leset P' : SLP cr := P.prefix cr hcr_lehave hP'_eval : P'.evalPoly = P.evalWire u := byunfold SLP.evalPolyrw [P.evalWire_prefix cr hcr_le ⟨cr + 1, Nat.lt_succ_self _⟩]congr 1; apply Fin.ext; dsimp; omegahave hP'_ne : P'.evalPoly ≠ 0 := by rw [hP'_eval]; exact huneset L' : List (UntappedSeg P') := restrictTake L hws.hord r' hr'_lt cr hcr_le hbr_le_crhave hL'_len : L'.length = r' + 1 := restrictTake_length L hws.hord r' hr'_lt cr hcr_le hbr_le_crhave hL'_ne : L' ≠ [] := by intro h; simp [h] at hL'_lenhave hL'_ord : SegsOrdered L' := byapply segsOrdered_of_adjintro i hirw [restrictTake_getElem_b, restrictTake_getElem_a]exact segsOrdered_adj L hws.hord i (by omega)have hL'_nextC : ∀ (j : Fin L'.length),segNextC L' j = segNextC L ⟨j.val, by omega⟩ := byintro jby_cases hj : j.val + 1 < L'.length· have hj_L : j.val + 1 < L.length := by omegaunfold segNextCrw [dite_eq_left_of_eq_true (eq_true hj), dite_eq_left_of_eq_true (eq_true hj_L)]exact restrictTake_getElem_a L hws.hord r' hr'_lt cr hcr_le hbr_le_cr (j.val + 1) hj· have hleft : segNextC L' j = cr := byunfold segNextC; rw [dite_eq_right_of_eq_false (eq_false hj)]have hj_eq : j.val = r' := by omegahave hfin : (⟨j.val, by omega⟩ : Fin L.length) = ⟨r', hr'_lt⟩ := Fin.ext hj_eqrw [hleft, congrArg (segNextC L) hfin]exact hcr_eqhave hL'_head : ∀ (hpos : 0 < L'.length),2 * (L'[0]'hpos).a + (segNextC L' ⟨0, hpos⟩ - (L'[0]'hpos).b) + 5 ≤(L'[0]'hpos).b - (L'[0]'hpos).a := byintro hposrw [restrictTake_getElem_a, restrictTake_getElem_b, hL'_nextC ⟨0, hpos⟩]have hstep0 : segNextC L ⟨0, hq_pos⟩ ≤ (L[0]'hq_pos).b + 1 := byunfold segNextCsplit_ifs with h1· exact hws.hstep_mid 0 h1· dsimp only at h1have hlen1 : L.length - 1 = 0 := by omegahave hlast := hws.hstep_last hq_possimpa [hlen1] using hlasthave hhg := hws.hhead_gap hq_poshave hnc0 : segNextC L ⟨(⟨0, hpos⟩ : Fin L'.length).val, by omega⟩ = segNextC L ⟨0, hq_pos⟩ := rflrw [hnc0]omegahave hL'_mid : ∀ (i : ℕ) (hi : i + 1 < L'.length),((L'[i + 1]'hi).a - (L'[i]'(by omega)).b) +(segNextC L' ⟨i + 1, hi⟩ - (L'[i + 1]'hi).b) + 3 ≤(L'[i + 1]'hi).b - (L'[i + 1]'hi).a := byintro i hirw [restrictTake_getElem_a, restrictTake_getElem_b, restrictTake_getElem_b,hL'_nextC ⟨i + 1, hi⟩]have hi_L : i + 1 < L.length := by omegahave hstep1 : (L[i + 1]'hi_L).a ≤ (L[i]'(by omega)).b + 1 := hws.hstep_mid i hi_Lhave hstep2 : segNextC L ⟨i + 1, hi_L⟩ ≤ (L[i + 1]'hi_L).b + 1 := byunfold segNextCsplit_ifs with h2· exact hws.hstep_mid (i + 1) h2· dsimp only at h2have hlast := hws.hstep_last hq_poshave heq : L.length - 1 = i + 1 := by omegahave : L[L.length - 1]'(by omega) = L[i + 1]'hi_L := by simp [heq]simpa [this] using hlasthave hmg := hws.hmid_gap i hi_Lhave hnc_i : segNextC L ⟨(⟨i + 1, hi⟩ : Fin L'.length).val, by omega⟩ = segNextC L ⟨i + 1, hi_L⟩ := rflrw [hnc_i]omega-- Verify the top-segment formal polynomial has natDegree ≥ 1 (via hactive!)have htop_pos : 1 ≤ (segFormalPoly P' L' hL'_ord⟨L'.length - 1, List.length_pos_of_ne_nil hL'_ne |> fun h => by omega⟩).natDegree := byhave hnot_last : ¬ ((L'.length - 1) + 1 < L'.length) := by omegahave hql_idx : L'.length - 1 = r' := by omegahave hact_r := hws.hactive r' hr'_lt (by simpa [hcr_eq] using hbr_lt_cr)have hu_fin : (⟨sr.b + 2, by omega⟩ : Fin (τ + 2)) = u := Fin.ext hu_eq_b2.symmhave hpos_P := evalFormalT_natDegree_pos_of_active P sr hbr_lt_τ(by rw [hu_fin]; exact hune) hact_rhave hEq_F :segFormalPoly P' L' hL'_ord ⟨L'.length - 1, by omega⟩ =evalFormalT P sr.a sr.b sr.hab sr.hb (sr.b + 1) (Nat.le_succ _) hbr_lt_τ ⟨sr.b + 2, by omega⟩ := byunfold segFormalPolyhave ha_eq : (L'[L'.length - 1]'(by omega)).a = sr.a := byrw [restrictTake_getElem_a]; simp [hql_idx, sr]have hb_eq : (L'[L'.length - 1]'(by omega)).b = sr.b := byrw [restrictTake_getElem_b]; simp [hql_idx, sr]have hnc_eq : segNextC L' ⟨L'.length - 1, by omega⟩ = cr := byunfold segNextC; rw [dite_eq_right_of_eq_false (eq_false hnot_last)]have hw_eq : (segWTarget P' L' ⟨L'.length - 1, by omega⟩).val = sr.b + 2 := byunfold segWTarget; rw [dite_eq_right_of_eq_false (eq_false hnot_last)]dsimp; omegahave hgen : ∀ {a₁ b₁ c₁ : ℕ} {hab₁ : a₁ < b₁} {hb₁ : b₁ ≤ cr} {hbc₁ : b₁ ≤ c₁} {hc₁ : c₁ ≤ cr}{w₁ : Fin (c₁ + 2)} {a₂ b₂ c₂ : ℕ} {hab₂ : a₂ < b₂} {hb₂ : b₂ ≤ τ} {hbc₂ : b₂ ≤ c₂}{hc₂ : c₂ ≤ τ} {w₂ : Fin (c₂ + 2)},a₁ = a₂ → b₁ = b₂ → c₁ = c₂ → w₁.val = w₂.val →evalFormalT P' a₁ b₁ hab₁ hb₁ c₁ hbc₁ hc₁ w₁ =evalFormalT P a₂ b₂ hab₂ hb₂ c₂ hbc₂ hc₂ w₂ := byrintro a₁ b₁ c₁ hab₁ hb₁ hbc₁ hc₁ w₁ _ _ _ hab₂ hb₂ hbc₂ hc₂ w₂ rfl rfl rfl hwhave hweq : w₁ = w₂ := Fin.ext hwsubst hweqexact evalFormalT_prefix_eq P a₁ b₁ hab₁ cr hb₁ hcr_le c₁ hbc₁ hc₁ w₁exact hgen ha_eq hb_eq (hnc_eq.trans hcr_eq_succ) hw_eqrw [hEq_F]; exact hpos_Pobtain ⟨hF'_deg, hsub'⟩ := slp_gap_dominance_root_cover P' hP'_ne L' hL'_ne hL'_ord hL'_head hL'_mid htop_posdsimp only at hsub'rw [← hP'_eval]-- Bound intRootCount P'.evalPoly from hsub'have hcard_le : intRootCount P'.evalPoly ≤(∑ j : Fin L'.length, intRootCount (segFormalPoly P' L' hL'_ord j).leadingCoeff) +3 * 2 ^ a0 := byset s0' := L'[0]'(List.length_pos_of_ne_nil hL'_ne)set 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)).isLthave := s0'.hab; have := s0'.hb; omega⟩have hw0'_deg : w0'.natDegree ≤ 2 ^ a0 := byhave ha0' : s0'.a = a0 := restrictTake_getElem_a L hws.hord r' hr'_lt cr hcr_le hbr_le_cr 0 (List.length_pos_of_ne_nil hL'_ne)have 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 ≤ 2 ^ a0 := byhave h2' := (P'.prefix s0'.a (by have := s0'.hab; have := s0'.hb; omega)).maxDeg_le_two_powsimpa [ha0'] using h2'exact le_trans h1 h2have hcard_w0m : (intRootSet (w0' - 1)).card ≤ 2 ^ a0 := byhave h1 : (intRootSet (w0' - 1)).card ≤ (w0' - 1).natDegree :=le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _)have h2 : (w0' - 1).natDegree ≤ w0'.natDegree := bysimpa using Polynomial.natDegree_sub_le w0' (1 : Polynomial ℤ)omegahave hcard_w0 : (intRootSet w0').card ≤ 2 ^ a0 := byhave h1 : (intRootSet w0').card ≤ w0'.natDegree :=le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _)omegahave hcard_w0p : (intRootSet (w0' + 1)).card ≤ 2 ^ a0 := byhave h1 : (intRootSet (w0' + 1)).card ≤ (w0' + 1).natDegree :=le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _)have h2 : (w0' + 1).natDegree ≤ w0'.natDegree := byhave h := Polynomial.natDegree_add_le w0' (1 : Polynomial ℤ)rw [Polynomial.natDegree_one] at hexact le_trans h (max_le le_rfl (Nat.zero_le _))omegahave hc1 : (intRootSet P'.evalPoly).card ≤((Finset.univ.biUnion (fun j : Fin L'.length => intRootSet (segFormalPoly P' L' hL'_ord j).leadingCoeff)) ∪intRootSet (w0' - 1) ∪ intRootSet w0' ∪ intRootSet (w0' + 1)).card :=Finset.card_le_card hsub'have hc2 := Finset.card_union_le((Finset.univ.biUnion (fun j : Fin L'.length => intRootSet (segFormalPoly P' L' hL'_ord j).leadingCoeff)) ∪intRootSet (w0' - 1) ∪ intRootSet w0')(intRootSet (w0' + 1))have hc3 := Finset.card_union_le((Finset.univ.biUnion (fun j : Fin L'.length => intRootSet (segFormalPoly P' L' hL'_ord j).leadingCoeff)) ∪intRootSet (w0' - 1))(intRootSet w0')have hc4 := Finset.card_union_le(Finset.univ.biUnion (fun j : Fin L'.length => intRootSet (segFormalPoly P' L' hL'_ord j).leadingCoeff))(intRootSet (w0' - 1))have hc5 := Finset.card_biUnion_le (s := (Finset.univ : Finset (Fin L'.length)))(t := fun j : Fin L'.length => intRootSet (segFormalPoly P' L' hL'_ord j).leadingCoeff)change (intRootSet P'.evalPoly).card ≤(∑ j : Fin L'.length, (intRootSet (segFormalPoly P' L' hL'_ord j).leadingCoeff).card) + 3 * 2 ^ a0omega-- Bound each summand j : Fin L'.length via evalFormalT_leadingCoeff_of_step_le_one + IH!have hterm : ∀ j : Fin L'.length,intRootCount (segFormalPoly P' L' hL'_ord j).leadingCoeff ≤if isNonSqMulIdx P L j.val then 3 * 2 ^ (a0 + nonSqMulCount P L j.val) else 0 := byintro jset sj' := L'[j.val]'j.isLtset cj := segNextC L' jhave hj_lt_L : j.val < L.length := by omegaset sj := L[j.val]'hj_lt_Lhave ha_j : sj'.a = sj.a := restrictTake_getElem_a L hws.hord r' hr'_lt cr hcr_le hbr_le_cr j.val j.isLthave hb_j : sj'.b = sj.b := restrictTake_getElem_b L hws.hord r' hr'_lt cr hcr_le hbr_le_cr j.val j.isLthave hcj_eq : cj = segNextC L ⟨j.val, hj_lt_L⟩ := hL'_nextC jhave hstep_j : cj ≤ sj'.b + 1 := byrw [hcj_eq, hb_j]unfold segNextCsplit_ifs with h1· exact hws.hstep_mid j.val h1· dsimp only at h1have hlast := hws.hstep_last hq_poshave heq : L.length - 1 = j.val := by omegahave : L[L.length - 1]'(by omega) = L[j.val]'hj_lt_L := by simp [heq]simpa [this] using hlasthave hnot_j : ¬ (sj'.a + 2 ≤ (segWTarget P' L' j).val ∧ (segWTarget P' L' j).val ≤ sj'.b) := byunfold segWTargetsplit_ifs with h· exact (sj'.huntapped (L'[j.val + 1]'h).a (by have := (L'[j.val + 1]'h).hab; have := (L'[j.val + 1]'h).hb; omega)(segsOrdered_adj L' hL'_ord j.val h)).1· have := sj'.hb; dsimp; omegahave hcls := evalFormalT_leadingCoeff_of_step_le_one P' sj' cj(segNextC_ge L' hL'_ord j) (segNextC_le L' j) hstep_j (segWTarget P' L' j) hnot_j (hF'_deg j)rcases hcls with hempty | ⟨hmul', v, hlc_eq⟩· have h0 : intRootCount (segFormalPoly P' L' hL'_ord j).leadingCoeff = 0 := byunfold intRootCount segFormalPoly; rw [hempty]; simprw [h0]; split_ifs <;> omega· have hmul_P : isNonSqMulIdx P L j.val := byrefine ⟨hj_lt_L, ?_⟩rcases hmul' with ⟨hb_lt_cj, hop', hcases'⟩have hb_lt_cj' : sj.b < segNextC L ⟨j.val, hj_lt_L⟩ := by rw [← hcj_eq, ← hb_j]; exact hb_lt_cjhave hcj_le_cr : cj ≤ cr := segNextC_le L' jhave hsb_lt_cr : sj.b < cr := by rw [← hb_j]; exact lt_of_lt_of_le hb_lt_cj hcj_le_crrefine ⟨hb_lt_cj', ?_, ?_⟩· have hop1 : P'.gateOp sj.b hsb_lt_cr = .mul := byhave hcongr : ∀ {k₁ k₂ : ℕ} (h1 : k₁ < cr) (h2 : k₂ < cr), k₁ = k₂ →P'.gateOp k₁ h1 = P'.gateOp k₂ h2 := by rintro k₁ _ h1 h2 rfl; rflrw [← hcongr _ hsb_lt_cr hb_j]; exact hop'rwa [P.gateOp_prefix cr hcr_le sj.b hsb_lt_cr] at hop1· have h1_eq : (P.gateIn1 sj.b (lt_of_lt_of_le hsb_lt_cr hcr_le)).val =(P'.gateIn1 sj'.b (lt_of_lt_of_le hb_lt_cj hcj_le_cr)).val := byrw [← P.gateIn1_prefix_val cr hcr_le sj.b hsb_lt_cr]have hcongr : ∀ {k₁ k₂ : ℕ} (h1 : k₁ < cr) (h2 : k₂ < cr), k₁ = k₂ →(P'.gateIn1 k₁ h1).val = (P'.gateIn1 k₂ h2).val := by rintro k₁ _ h1 h2 rfl; rflexact hcongr hsb_lt_cr _ hb_j.symmhave h2_eq : (P.gateIn2 sj.b (lt_of_lt_of_le hsb_lt_cr hcr_le)).val =(P'.gateIn2 sj'.b (lt_of_lt_of_le hb_lt_cj hcj_le_cr)).val := byrw [← P.gateIn2_prefix_val cr hcr_le sj.b hsb_lt_cr]have hcongr : ∀ {k₁ k₂ : ℕ} (h1 : k₁ < cr) (h2 : k₂ < cr), k₁ = k₂ →(P'.gateIn2 k₁ h1).val = (P'.gateIn2 k₂ h2).val := by rintro k₁ _ h1 h2 rfl; rflexact hcongr hsb_lt_cr _ hb_j.symmrw [h1_eq, h2_eq, ← ha_j, ← hb_j]exact hcases'simp only [hmul_P, ↓reduceIte]change intRootCount (evalFormalT P' sj'.a sj'.b sj'.hab sj'.hb cj _ _ _).leadingCoeff ≤ _rw [hlc_eq]have hlc_ne : P'.evalWire ⟨v.val, by have := v.isLt; have := sj'.hab; have := sj'.hb; omega⟩ ≠ 0 := byrw [← hlc_eq]exact Polynomial.leadingCoeff_ne_zero.mpr (fun h0 => byhave hdegj := hF'_deg jchange 1 ≤ (evalFormalT P' sj'.a sj'.b sj'.hab sj'.hb cj _ _ _).natDegree at hdegjrw [h0] at hdegj; simp at hdegj)set v' : Fin (τ + 2) := ⟨v.val, by have := v.isLt; have := sj'.hab; have := sj'.hb; omega⟩have hv_eq : P'.evalWire ⟨v.val, by have := v.isLt; have := sj'.hab; have := sj'.hb; omega⟩ =P.evalWire v' := P.evalWire_prefix cr hcr_le _rw [hv_eq] at hlc_ne ⊢have hv'_lt : v'.val < stageBound L j.val + 2 := bydsimp only [v', stageBound]rw [dite_eq_left_of_eq_true (eq_true hj_lt_L)]calc v.val < sj'.a + 2 := v.isLt_ = (L[j.val]'hj_lt_L).a + 2 := by rw [ha_j]exact ih j.val (by omega) (le_of_lt hj_lt_L) v' hv'_lt hlc_ne-- Sum the bounds using one_add_sum_two_pow_filter!have hsum_le :(∑ j : Fin L'.length, intRootCount (segFormalPoly P' L' hL'_ord j).leadingCoeff) ≤∑ j ∈ (Finset.range (r' + 1)).filter (isNonSqMulIdx P L),3 * 2 ^ (a0 + nonSqMulCount P L j) := bycalc _ ≤ ∑ j : Fin L'.length,if isNonSqMulIdx P L j.val then 3 * 2 ^ (a0 + nonSqMulCount P L j.val) else 0 :=Finset.sum_le_sum (fun j _ => hterm j)_ = ∑ j ∈ Finset.range L'.length,if isNonSqMulIdx P L j then 3 * 2 ^ (a0 + nonSqMulCount P L j) else 0 :=Fin.sum_univ_eq_sum_range (fun j => if isNonSqMulIdx P L j then 3 * 2 ^ (a0 + nonSqMulCount P L j) else 0) L'.length_ = ∑ j ∈ (Finset.range (r' + 1)).filter (isNonSqMulIdx P L),3 * 2 ^ (a0 + nonSqMulCount P L j) := byrw [hL'_len, Finset.sum_filter]have hgeom :3 * 2 ^ a0 +∑ j ∈ (Finset.range (r' + 1)).filter (isNonSqMulIdx P L),3 * 2 ^ (a0 + nonSqMulCount P L j) =3 * 2 ^ (a0 + nonSqMulCount P L (r' + 1)) := byset S := ∑ j ∈ (Finset.range (r' + 1)).filter (isNonSqMulIdx P L),2 ^ ((Finset.range j).filter (isNonSqMulIdx P L)).cardhave hfac :∑ j ∈ (Finset.range (r' + 1)).filter (isNonSqMulIdx P L),3 * 2 ^ (a0 + nonSqMulCount P L j) =(3 * 2 ^ a0) * S := byrw [Finset.mul_sum]refine Finset.sum_congr rfl fun j _ => ?_unfold nonSqMulCountrw [pow_add, mul_assoc]have hfactor : 3 * 2 ^ a0 + (3 * 2 ^ a0) * S = (3 * 2 ^ a0) * (1 + S) := by ringrw [hfac, hfactor]have htele : 1 + S = 2 ^ ((Finset.range (r' + 1)).filter (isNonSqMulIdx P L)).card :=one_add_sum_two_pow_filter (isNonSqMulIdx P L) (r' + 1)rw [htele]unfold nonSqMulCountrw [pow_add, mul_assoc]omega/-- **Well-Spaced Sharp Multiplication-Count Bound (`3 * 2 ^ (a_0 + m_mul)`):**For every nonzero constant-free `P : SLP τ` admitting a well-spaced segment list `L`,`intRootCount P.evalPoly ≤ 3 * 2 ^ (a_0 + nonSqMulCount P L L.length)`.Additions, subtractions, and pure squarings do **not** contribute to the exponent! -/theorem slp_well_spaced_mul_bound {τ : ℕ} (P : SLP τ) (hP : P.evalPoly ≠ 0)(L : List (UntappedSeg P)) (hws : WellSpacedSegs P L) :intRootCount P.evalPoly ≤3 * 2 ^ ((L[0]'(List.length_pos_of_ne_nil hws.hne)).a + nonSqMulCount P L L.length) := byhave hlt : τ + 1 < stageBound L L.length + 2 := bysimp [stageBound]exact slp_well_spaced_wire_bound P L hws L.length le_rfl ⟨τ + 1, Nat.lt_succ_self _⟩ hlt hP/-- **Well-Spaced Addition/Subtraction-Only Bound (`z_ℤ(P) ≤ 3`):**For every nonzero well-spaced SLP starting from `a_0 = 0` with no isolated non-squaringmultiplications (`nonSqMulCount = 0`) — allowing **arbitrarily many** additions, subtractions,pure squarings, and arbitrary DAG taps to earlier wires —`intRootCount P.evalPoly ≤ 3`. -/theorem slp_well_spaced_add_only_le_three {τ : ℕ} (P : SLP τ) (hP : P.evalPoly ≠ 0)(L : List (UntappedSeg P)) (hws : WellSpacedSegs P L)(ha0 : (L[0]'(List.length_pos_of_ne_nil hws.hne)).a = 0)(hm0 : nonSqMulCount P L L.length = 0) :intRootCount P.evalPoly ≤ 3 := byhave h := slp_well_spaced_mul_bound P hP L hwssimpa [ha0, hm0] using h/-- **Well-Spaced General `{+, -, ×}` SLP Theorem (`z_ℤ(P) ≤ 2 ^ (|B| + 1)`):**Every nonzero constant-free `P : SLP τ` admitting a well-spaced segment list `L` with `a_0 = 0`satisfies`intRootCount P.evalPoly ≤ 2 ^ (P.nonTopSqCount + 1)`. -/theorem slp_well_spaced_intRootCount_le {τ : ℕ} (P : SLP τ) (hP : P.evalPoly ≠ 0)(L : List (UntappedSeg P)) (hws : WellSpacedSegs P L)(ha0 : (L[0]'(List.length_pos_of_ne_nil hws.hne)).a = 0) :intRootCount P.evalPoly ≤ 2 ^ (P.nonTopSqCount + 1) := byclassicalby_cases hA0 : P.addSubCount = 0· rcases slp_addSubCount_zero_wire P hA0 ⟨τ + 1, Nat.lt_succ_self _⟩ with ⟨e, he⟩have hsub : intRootSet P.evalPoly ⊆ {0} := byintro x hxhave hr : P.evalPoly.eval x = 0 :=Polynomial.IsRoot.def.mp ((Polynomial.mem_roots hP).mp (Multiset.mem_toFinset.mp hx))change (P.evalWire ⟨τ + 1, Nat.lt_succ_self _⟩).eval x = 0 at hrrw [he, Polynomial.eval_pow, Polynomial.eval_X] at hrexact Finset.mem_singleton.mpr (eq_zero_of_pow_eq_zero hr)have hle1 : intRootCount P.evalPoly ≤ 1 := bycalc intRootCount P.evalPoly ≤ ({0} : Finset ℤ).card := Finset.card_le_card hsub_ = 1 := Finset.card_singleton 0have hpow : 1 ≤ 2 ^ (P.nonTopSqCount + 1) := Nat.one_le_pow _ _ (by omega)omega· have hmul_lt_B : nonSqMulCount P L L.length + 1 ≤ P.nonTopSqCount := byhave hex_add : ∃ t : Fin τ, P.gateOp t.val t.isLt ≠ .mul ∧ ¬ P.gateIsTopSq t.val t.isLt := byhave hgen : ∀ {n : ℕ} (Q : SLP n), Q.addSubCount ≠ 0 →∃ t : Fin n, Q.gateOp t.val t.isLt ≠ .mul ∧ ¬ Q.gateIsTopSq t.val t.isLt := byintro n Qinduction Q with| nil => intro h; simp [SLP.addSubCount] at h| snoc Q' op i j ih =>intro hnecases op with| mul =>simp only [SLP.addSubCount] at hnercases ih hne with ⟨t, hop, hnt⟩refine ⟨⟨t.val, Nat.lt_succ_of_lt t.isLt⟩, ?_, ?_⟩· simp [SLP.gateOp, t.isLt, hop]· rwa [SLP.gateIsTopSq_snoc_lt]| add =>refine ⟨⟨_, Nat.lt_succ_self _⟩, ?_, ?_⟩· simp [SLP.gateOp]· rw [SLP.gateIsTopSq_snoc_last]; intro htop; cases htop.1| sub =>refine ⟨⟨_, Nat.lt_succ_self _⟩, ?_, ?_⟩· simp [SLP.gateOp]· rw [SLP.gateIsTopSq_snoc_last]; intro htop; cases htop.1exact hgen P hA0obtain ⟨t_add, ht_add_op, ht_add_nt⟩ := hex_addset S_mul := (Finset.range L.length).filter (isNonSqMulIdx P L)set S_B := Finset.univ.filter (fun t : Fin τ => ¬ P.gateIsTopSq t.val t.isLt)let f : ℕ → Fin τ := fun i =>if h : i < L.length thenif hb : (L[i]'h).b < τ then ⟨(L[i]'h).b, hb⟩ else t_addelse t_addhave hf_mem : ∀ i ∈ S_mul, f i ∈ S_B ∧ P.gateOp (f i).val (f i).isLt = .mul := byintro i hihave hi_lt : i < L.length := Finset.mem_range.mp (Finset.mem_filter.mp hi).1have hmul_i : isNonSqMulIdx P L i := (Finset.mem_filter.mp hi).2rcases hmul_i with ⟨_, hseg⟩rcases segIsNonSqMul_not_topSq hseg with ⟨hb_lt, hop, hnt⟩have hfi : f i = ⟨(L[i]'hi_lt).b, hb_lt⟩ := bysimp [f, hi_lt, hb_lt]rw [hfi]exact ⟨Finset.mem_filter.mpr ⟨Finset.mem_univ _, hnt⟩, hop⟩have hf_inj : Set.InjOn f (S_mul : Set ℕ) := byintro i hi j hj hfeqhave hi_lt : i < L.length := Finset.mem_range.mp (Finset.mem_filter.mp hi).1have hj_lt : j < L.length := Finset.mem_range.mp (Finset.mem_filter.mp hj).1rcases (Finset.mem_filter.mp hi).2 with ⟨_, hsi⟩rcases (Finset.mem_filter.mp hj).2 with ⟨_, hsj⟩rcases segIsNonSqMul_not_topSq hsi with ⟨hbi, _, _⟩rcases segIsNonSqMul_not_topSq hsj with ⟨hbj, _, _⟩have hfi : f i = ⟨(L[i]'hi_lt).b, hbi⟩ := by simp [f, hi_lt, hbi]have hfj : f j = ⟨(L[j]'hj_lt).b, hbj⟩ := by simp [f, hj_lt, hbj]rw [hfi, hfj] at hfeqhave hb_eq : (L[i]'hi_lt).b = (L[j]'hj_lt).b := congrArg Fin.val hfeqby_contra hnehave hstrict : ∀ (p q : ℕ) (hp : p < L.length) (hq : q < L.length), p < q →(L[p]'hp).b < (L[q]'hq).b := byintro p q hp hq hpqinduction q with| zero => omega| succ q' ihq =>by_cases hpq' : p = q'· subst hpq'have hadj := segsOrdered_adj L hws.hord p hqhave hlt := (L[p + 1]'hq).habomega· have h1 := ihq (by omega) (by omega)have hadj := segsOrdered_adj L hws.hord q' hqhave hlt := (L[q' + 1]'hq).habomegarcases lt_or_gt_of_ne hne with hlt | hgt· have := hstrict i j hi_lt hj_lt hlt; omega· have := hstrict j i hj_lt hi_lt hgt; omegahave ht_not_mem : t_add ∉ S_mul.image f := byintro hmrcases Finset.mem_image.mp hm with ⟨i, hi, hfi⟩have hop := (hf_mem i hi).2rw [hfi] at hopexact ht_add_op hophave hsub_B : insert t_add (S_mul.image f) ⊆ S_B := byintro t htrcases Finset.mem_insert.mp ht with rfl | him· exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, ht_add_nt⟩· rcases Finset.mem_image.mp him with ⟨i, hi, rfl⟩exact (hf_mem i hi).1have hcard_eq : (S_mul.image f).card = S_mul.card := Finset.card_image_of_injOn hf_injhave hcard_B : P.nonTopSqCount = S_B.card := P.nonTopSqCount_eq_cardhave hle := Finset.card_le_card hsub_Brw [Finset.card_insert_of_notMem ht_not_mem, hcard_eq] at hlechange S_mul.card + 1 ≤ P.nonTopSqCountomegahave hmain := slp_well_spaced_mul_bound P hP L hwsrw [ha0, Nat.zero_add] at hmaincalc intRootCount P.evalPoly≤ 3 * 2 ^ nonSqMulCount P L L.length := hmain_ ≤ 4 * 2 ^ nonSqMulCount P L L.length := Nat.mul_le_mul_right _ (by omega)_ = 2 ^ (nonSqMulCount P L L.length + 2) := by rw [pow_succ, pow_succ]; ring_ ≤ 2 ^ (P.nonTopSqCount + 1) := Nat.pow_le_pow_right (by omega) (by omega)end Research.PvsNP.Tau