module public import Research.PvsNP.Tau.GapDominanceBound public import Research.PvsNP.Tau.AddComplexityBound @[expose] public section namespace 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-spaced untapped 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 top wire `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 := by nlinarith private 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 := by unfold polyL1At apply Finset.sum_subset (fun j hj => Finset.mem_range.mpr (hN j hj)) intro j _ hnot have hc : F.coeff j = 0 := Polynomial.notMem_support_iff.mp hnot simp [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 := by unfold polyIntL1 apply Finset.sum_subset (fun j hj => Finset.mem_range.mpr (hN j hj)) intro j _ hnot have hc : f.coeff j = 0 := Polynomial.notMem_support_iff.mp hnot simp [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 := by rw [Polynomial.eval_eq_sum_range (p := F), ← Polynomial.coe_evalRingHom, map_sum] apply Finset.sum_congr rfl; intro j _ simp private 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 := by have htri : ∀ (s : Finset ℕ) (g : ℕ → ℤ), (∑ j ∈ s, g j).natAbs ≤ ∑ j ∈ s, (g j).natAbs := by intro s g induction 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 := by rw [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] omega calc (∑ 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 := by apply 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) := by apply Finset.sum_le_sum; intro j hj have hlt : j < F.natDegree := Finset.mem_range.mp hj exact 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 := by by_cases hU0 : (u.eval x).natAbs = 0 · omega · have hU1 : 1 ≤ (u.eval x).natAbs := by omega have hev := eval_polyT_eq_ws F u x rw [hzero, Finset.sum_range_succ] at hev set low := ∑ j ∈ Finset.range F.natDegree, (F.coeff j).eval x * (u.eval x) ^ j set topTerm := (F.coeff F.natDegree).eval x * (u.eval x) ^ F.natDegree have htop_eq : topTerm = -low := by linarith have hlead_pos : 1 ≤ ((F.coeff F.natDegree).eval x).natAbs := Int.natAbs_pos.mpr hlead have htop_ge : (u.eval x).natAbs ^ F.natDegree ≤ topTerm.natAbs := by calc (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 hU1 have htop_le : topTerm.natAbs ≤ (u.eval x).natAbs ^ (F.natDegree - 1) * polyL1At F x := by rw [htop_eq, Int.natAbs_neg]; exact hlow have hpow_split : (u.eval x).natAbs ^ F.natDegree = (u.eval x).natAbs ^ (F.natDegree - 1) * (u.eval x).natAbs := by conv_lhs => rw [← Nat.sub_add_cancel hD, pow_succ] rw [hpow_split] at htop_ge have 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_le exact 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 := by have hU1 : 1 ≤ (u.eval x).natAbs := by omega have hev := eval_polyT_eq_ws F u x rw [Finset.sum_range_succ] at hev set low := ∑ j ∈ Finset.range F.natDegree, (F.coeff j).eval x * (u.eval x) ^ j set topTerm := (F.coeff F.natDegree).eval x * (u.eval x) ^ F.natDegree have hlead_pos : 1 ≤ ((F.coeff F.natDegree).eval x).natAbs := Int.natAbs_pos.mpr hlead have htop_ge : (u.eval x).natAbs ^ F.natDegree ≤ topTerm.natAbs := by calc (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 := by have hrew : topTerm = (F.eval u).eval x + (-low) := by linarith calc 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 hU1 have hpow_split : (u.eval x).natAbs ^ F.natDegree = (u.eval x).natAbs ^ (F.natDegree - 1) * (u.eval x).natAbs := by conv_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 := by rw [hpow_split]; linarith have h3 : (u.eval x).natAbs ^ F.natDegree ≤ 2 * ((F.eval u).eval x).natAbs := by omega have h4 : (u.eval x).natAbs ≤ (u.eval x).natAbs ^ F.natDegree := Nat.le_self_pow (by omega) (u.eval x).natAbs omega private 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 := by have hx1 : 1 ≤ x.natAbs := by have hne : w ≠ 0 := fun h0 => by simp [h0] at hD have hpos : 1 ≤ polyIntL1 w := by rw [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) omega omega have hev : w.eval x = ∑ j ∈ Finset.range (w.natDegree + 1), w.coeff j * x ^ j := Polynomial.eval_eq_sum_range x rw [Finset.sum_range_succ] at hev set low := ∑ j ∈ Finset.range w.natDegree, w.coeff j * x ^ j set top := w.coeff w.natDegree * x ^ w.natDegree have hne : w ≠ 0 := fun h0 => by simp [h0] at hD have 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 := by calc 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 := by have hrew : top = w.eval x + (-low) := by linarith calc 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 := by intro s g induction 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 := by rw [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]; omega have hlow : low.natAbs ≤ x.natAbs ^ (w.natDegree - 1) * polyIntL1 w := by calc 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 := by apply 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) := by apply Finset.sum_le_sum; intro j hj exact 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 := by conv_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 _ hbig have h2 : 2 * low.natAbs ≤ x.natAbs ^ w.natDegree := by rw [hpow_split]; linarith have h3 : x.natAbs ^ w.natDegree ≤ 2 * (w.eval x).natAbs := by omega have h4 : x.natAbs ≤ x.natAbs ^ w.natDegree := Nat.le_self_pow (by omega) x.natAbs omega /-- 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 := by intro n induction n with | zero => simp | succ m ih => rw [Finset.range_add_one, Finset.filter_insert] have hnot : m ∉ (Finset.range m).filter p := by simp split_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 := by by_cases hw : w = 0 · subst hw; rw [zero_pow (by omega)] · have hwk : w ^ k ≠ 0 := pow_ne_zero k hw have hset : intRootSet (w ^ k) = intRootSet w := by ext x simp 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 intRootCount rw [hset] /-- `intRootSet (C c) = ∅` for every nonzero integer constant `c ≠ 0`. -/ lemma intRootSet_C_eq_empty {c : ℤ} (hc : c ≠ 0) : intRootSet (Polynomial.C c) = ∅ := by ext x simp 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 hc private lemma natDegree_C_sub_X (p : Polynomial ℤ) : (Polynomial.C p - Polynomial.X : Polynomial (Polynomial ℤ)).natDegree = 1 := by have heq : Polynomial.C p - Polynomial.X = Polynomial.C (-1 : Polynomial ℤ) * Polynomial.X + Polynomial.C p := by simp; ring rw [heq] have hm1_ne : (-1 : Polynomial ℤ) ≠ 0 := by intro h; have := congrArg (Polynomial.eval 0) h; simp at this have hdeg_left : (Polynomial.C (-1 : Polynomial ℤ) * Polynomial.X).natDegree = 1 := Polynomial.natDegree_C_mul_X _ hm1_ne have hlt : (Polynomial.C p).natDegree < (Polynomial.C (-1 : Polynomial ℤ) * Polynomial.X).natDegree := by rw [Polynomial.natDegree_C, hdeg_left]; omega rw [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 := by rw [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 => by have ht1 : t₁ = 0 := by omega have ht2 : t₂ = 0 := by omega subst ht1; subst ht2; rfl | τ + 1, .snoc P op i j, t₁, t₂, h12, h2 => by by_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 omega subst heq rw [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) := by induction τ 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 hle simp only [SLP.gateOp, hk', ↓reduceDIte] exact ih P' hle · have heq : t = τ' + 1 := by omega subst heq rw [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 := by induction τ 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 hle simp only [SLP.gateIn1, hk', ↓reduceDIte] exact ih P' hle · have heq : t = τ' + 1 := by omega subst heq rw [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 := by induction τ 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 hle simp only [SLP.gateIn2, hk', ↓reduceDIte] exact ih P' hle · have heq : t = τ' + 1 := by omega subst heq rw [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) := by unfold SLP.gateIsTopSq isTopPureSquare rw [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) where a := s.a b := s.b hab := s.hab hb := hbt hrun := fun k hk h1 h2 => (P.gateIsTopSq_prefix t ht k hk).mpr (s.hrun k (lt_of_lt_of_le hk ht) h1 h2) huntapped := fun k hk hbk => by have hu := s.huntapped k (lt_of_lt_of_le hk ht) hbk rw [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 := by intro t induction t using Nat.strong_induction_on with | ind t ih => intro hbt htc w rw [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 only set t' := w.val - 2 have ht'_lt : t' < t := by omega have hbt' : b ≤ t' := by omega have ht'_lt_c : t' < c := by omega have 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 := by unfold segNextC split_ifs with h · exact segsOrdered_adj L hord r.val h · exact (L[r.val]'r.isLt).hb lemma segNextC_le {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (r : Fin L.length) : segNextC L r ≤ τ := by unfold segNextC split_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 then let sNext := L[r.val + 1]'h ⟨(P.gateIn1 sNext.a (by have := sNext.hab; have := sNext.hb; omega)).val, by have hlt := (P.gateIn1 sNext.a (by have := sNext.hab; have := sNext.hb; omega)).isLt unfold segNextC; rw [dite_eq_left_of_eq_true (eq_true h)]; exact hlt⟩ else ⟨τ + 1, by unfold 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 integer root 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_ne let s0 := L[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⟩ 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) := by classical have hq_pos : 0 < L.length := List.length_pos_of_ne_nil hL_ne let seg : Fin L.length → UntappedSeg P := fun r => L[r.val]'r.isLt let nextC : Fin L.length → ℕ := segNextC L let wTarget : (r : Fin L.length) → Fin (nextC r + 2) := segWTarget P L let F : Fin L.length → Polynomial (Polynomial ℤ) := segFormalPoly P L hord let qlast : Fin L.length := ⟨L.length - 1, by omega⟩ let s0 : UntappedSeg P := L[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 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 hr have hqlast_not : ¬ (qlast.val + 1 < L.length) := by dsimp [qlast]; omega have hqlast_spec : (F qlast).eval (P.evalWire ⟨(seg qlast).b + 1, by have := (seg qlast).hb; omega⟩) = P.evalPoly := by have hnot : ¬ ((seg qlast).a + 2 ≤ (wTarget qlast).val ∧ (wTarget qlast).val ≤ (seg qlast).b) := by have hw : (wTarget qlast).val = τ + 1 := by dsimp only [wTarget, segWTarget]; rw [dite_eq_right_of_eq_false (eq_false hqlast_not)] have := (seg qlast).hb; omega have hspec := evalFormalT_spec P (seg qlast) (nextC qlast) (segNextC_ge L hord qlast) (segNextC_le L qlast) (wTarget qlast) hnot have hw : (wTarget qlast).val = τ + 1 := by dsimp 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).natDegree have hF_deg_pos : ∀ r : Fin L.length, 1 ≤ (F r).natDegree := by intro r by_cases hlast : r.val + 1 < L.length · by_contra h0 have hdeg0 : (F r).natDegree = 0 := by omega have hF_eq : F r = Polynomial.C ((F r).coeff 0) := Polynomial.eq_C_of_natDegree_eq_zero hdeg0 let r1 : Fin L.length := ⟨r.val + 1, hlast⟩ have hnot_inter : ¬ ((seg r).a + 2 ≤ (wTarget r).val ∧ (wTarget r).val ≤ (seg r).b) := by have hw_val : (wTarget r).val = (P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).val := by dsimp 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)).1 have hnc_le_r : nextC r ≤ τ := segNextC_le L r have hspec := evalFormalT_spec P (seg r) (nextC r) (segNextC_ge L hord r) hnc_le_r (wTarget r) hnot_inter change (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 hspec rw [hF_eq, Polynomial.eval_C] at hspec have htop_next := (seg r1).hrun (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega) le_rfl (seg r1).hab have 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 := by have hw_val : (wTarget r).val = (P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).val := by dsimp 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)) := by rw [hpr]; congr 1; exact Fin.ext hw_val rw [h1]; exact htop_next.2.2 have hrun_r := P.topSq_run_eval (seg r).hab (seg r).hb (seg r).hrun have 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 := by have hle_b : (seg r).b ≤ (seg r1).a := hseg_ord r hlast have 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) 0 have hgap_gt : nextC r - (seg r).b < (seg r).b - (seg r).a := by cases hr0 : r.val with | zero => have hh := hhead_gap hq_pos have hr_eq : r = ⟨0, hq_pos⟩ := Fin.ext hr0 rw [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 hr0 rw [hr_eq]; dsimp only [seg, nextC]; omega have hpow_lt : 2 ^ (nextC r - (seg r).b) < 2 ^ ((seg r).b - (seg r).a) := Nat.pow_lt_pow_right (by omega) hgap_gt have 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_maxDeg have 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_le rw [hrun_r.2] at hUr_le omega · have hreq : r = qlast := Fin.ext (by dsimp [qlast]; omega) subst hreq; exact htop_pos refine ⟨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 := by have htop0 := s0.hrun s0.a (by have := s0.hab; have := s0.hb; omega) le_rfl s0.hab 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 hdeg_eq : w0.natDegree = (P.prefix s0.a (by have := s0.hab; have := s0.hb; omega)).maxDeg := by dsimp only [w0]; rw [← hpr]; exact htop0.2.2 rw [hdeg_eq]; exact (P.prefix s0.a _).one_le_maxDeg -- Step 2: Backward Cauchy propagation dsimp only intro x hx have 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⟩ := hkill apply Finset.mem_union_left; apply Finset.mem_union_left; apply Finset.mem_union_left refine 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 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⟩ := by intro d induction 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) x exact 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 hd set 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]; omega have hr_lt : r.val + 1 < L.length := by omega have hfin_eq : (⟨r.val + 1, hr_lt⟩ : Fin L.length) = r1 := Fin.ext hr1_val have hnext_eq : nextC r = (seg r1).a := by dsimp 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).natAbs set Hr := wireHeight P (seg r).a (by have := (seg r).hab; have := (seg r).hb; omega) x set Rr := nextC r - (seg r).b have hHr2 : 2 ≤ Hr := two_le_wireHeight P (seg r).a _ x by_cases hsmall : U ≤ 2 * Hr ^ (2 ^ Rr) · calc U ≤ 2 * Hr ^ (2 ^ Rr) := hsmall _ ≤ Hr ^ (2 ^ Rr) * Hr ^ (2 ^ Rr) := by have : 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] · exfalso have hbig : 2 * Hr ^ (2 ^ Rr) < U := by omega have hU4 : 4 ≤ U := by have : 2 ≤ Hr ^ (2 ^ Rr) := le_trans hHr2 (Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) Hr) omega have 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 hl1 have hbig_l1 : 2 * polyL1At (F r) x < U := by omega have 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_l1 have hw_val : (wTarget r).val = (P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).val := by dsimp 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_eq have 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; rfl exact hgate_eq _ _ hseg_eq have hnot_inter : ¬ ((seg r).a + 2 ≤ (wTarget r).val ∧ (wTarget r).val ≤ (seg r).b) := by rw [hw_val] have hbr : (seg r).b ≤ (seg r1).a := by rw [← hnext_eq]; exact segNextC_ge L hord r exact ((seg r).huntapped (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega) hbr).1 have hnc_le_r : nextC r ≤ τ := segNextC_le L r have hspec := evalFormalT_spec P (seg r) (nextC r) (segNextC_ge L hord r) hnc_le_r (wTarget r) hnot_inter change (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 hspec rw [hspec] at hdom set w1_val := ((P.evalWire ⟨(wTarget r).val, by have := (wTarget r).isLt; omega⟩).eval x).natAbs have hU_le_sq : U ≤ w1_val ^ 2 := nat_half_sq_ge_ws hU4 hdom set g1 := (seg r1).b - (seg r1).a have hrun1 := P.topSq_run_eval (seg r1).hab (seg r1).hb (seg r1).hrun have 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) := by rw [hw1_eq] have := congrArg (fun p : Polynomial ℤ => (p.eval x).natAbs) hrun1.1 simpa [Int.natAbs_pow, g1] using this have hih : P_prop r1 := ihd (by omega) dsimp only [P_prop] at hih rw [hU1_eq] at hih have 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 (by have : Hr ≤ Hr ^ (2 ^ Rr) := Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) Hr omega) rw [hmax_U] at hH1_le have hH1_le' : wireHeight P (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega) x ≤ U ^ (2 ^ Rr) := by change wireHeight P (nextC r) (segNextC_le L r) x ≤ U ^ (2 ^ Rr) at hH1_le simpa only [hnext_eq] using hH1_le have hg1_ge : Rr + (nextC r1 - (seg r1).b) + 3 ≤ g1 := by have hmid := hmid_gap r.val hr_lt have 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_eq have hnc1 : segNextC L ⟨r.val + 1, hr_lt⟩ = nextC r1 := congrArg nextC hfin_eq rw [hRr_eq]; dsimp only [g1] rw [← hseg_r1, ← hnc1]; exact hmid have hchain_pow : U ^ (2 ^ (g1 - 1)) ≤ U ^ (2 ^ (Rr + (nextC r1 - (seg r1).b) + 1)) := by have hleft : U ^ (2 ^ (g1 - 1)) ≤ w1_val ^ (2 ^ g1) := by calc 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) := by congr 1 conv_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)) := by calc 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)) := by rw [← pow_add, ← Nat.add_assoc] exact le_trans hleft hright have 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_pow have 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 = 0 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 hprop0 dsimp only [P_prop] at hprop0 set R0 := s0.a set R1 := nextC ⟨0, hq_pos⟩ - s0.b set g0 := s0.b - s0.a have hg0_ge : 2 * R0 + R1 + 5 ≤ g0 := by simpa [R0, R1, g0, s0, nextC] using hhead_gap hq_pos have hrun0 := P.topSq_run_eval s0.hab s0.hb s0.hrun have hU0_eq : ((P.evalWire ⟨s0.b + 1, by have := s0.hb; omega⟩).eval x).natAbs = (w0.eval x).natAbs ^ (2 ^ g0) := by have := congrArg (fun p : Polynomial ℤ => (p.eval x).natAbs) hrun0.1 simpa [Int.natAbs_pow, w0, g0] using this rw [hU0_eq] at hprop0 have hH0_le := wireHeight_base_le P s0.a (by have := s0.hab; have := s0.hb; omega) x have hl1_w0 : polyIntL1 w0 ≤ 2 ^ (2 ^ R0) := by 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 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.symm rw [hw0_eq] exact (P.prefix s0.a _).wire_polyIntL1_le _ have hw0_le1 : (w0.eval x).natAbs ≤ 1 := by by_contra hbig_w0 have hw0_ge2 : 2 ≤ (w0.eval x).natAbs := by omega by_cases hx_big : 2 * 2 ^ (2 ^ R0) ≤ x.natAbs · have hx4 : 4 ≤ x.natAbs := by have : 2 ≤ 2 ^ (2 ^ R0) := Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) 2 omega have 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 hdom have hmax_x : max 2 x.natAbs = x.natAbs := max_eq_right (by omega) rw [hmax_x] at hH0_le have hchain_x : x.natAbs ^ (2 ^ (g0 - 1)) ≤ x.natAbs ^ (2 ^ (R0 + R1 + 1)) := by have h1 : x.natAbs ^ (2 ^ (g0 - 1)) ≤ (w0.eval x).natAbs ^ (2 ^ g0) := by calc 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) := by congr 1 conv_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)) := by calc (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 h2 have hexp_le : 2 ^ (g0 - 1) ≤ 2 ^ (R0 + R1 + 1) := (Nat.pow_le_pow_iff_right (by omega : 1 < x.natAbs)).mp hchain_x have 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) := by have : 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 _) 2 exact 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)) := by calc 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)) := by apply 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 omega calc (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)) := by calc 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)) := by rw [← pow_add]; congr 2; omega have hexp_le : 2 ^ g0 ≤ 2 ^ (2 * R0 + R1 + 2) := (Nat.pow_le_pow_iff_right (by omega : 1 < 2)).mp hchain_2 have 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 omega have hw0_ne : w0 ≠ 0 := fun h0 => by simp [h0] at hw0_deg_pos have hw0_sub_ne : w0 - 1 ≠ 0 := fun h0 => by have : w0 = 1 := sub_eq_zero.mp h0 rw [this] at hw0_deg_pos; simp at hw0_deg_pos have hw0_add_ne : w0 + 1 ≠ 0 := fun h0 => by have : w0 = -1 := eq_neg_of_add_eq_zero_left h0 rw [this] at hw0_deg_pos; simp at hw0_deg_pos rcases hcases with hm1 | h0 | hp1 · apply Finset.mem_union_right rw [intRootSet, Multiset.mem_toFinset, Polynomial.mem_roots hw0_add_ne, Polynomial.IsRoot.def] simp [hm1] · apply Finset.mem_union_left; apply Finset.mem_union_right rw [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_right rw [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-squaring multiplication** 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 hc P.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 := by rcases 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.1 have hab := s.hab rcases 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) := by rw [evalFormalT.eq_def] by_cases hv : v.val < s.b + 1 · have hva : v.val ≤ s.a + 1 := by omega left refine ⟨hva, ?_⟩ simp [hv, hva] · have hvb : v.val = s.b + 1 := by have := v.isLt; omega right refine ⟨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; omega evalFormalT 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)) := by intro hb_lt rw [evalFormalT.eq_def] have hw1 : ¬ (w.val < s.b + 1) := by omega have hw2 : ¬ (w.val = s.b + 1) := by omega rw [dite_eq_right_of_eq_false (eq_false hw1), dite_eq_right_of_eq_false (eq_false hw2)] dsimp only have ht'_eq : w.val - 2 = s.b := by omega have 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)) := by rintro t₁ _ h1 h2 hb1 hb2 rfl; rfl exact 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⟩) := by set F := evalFormalT P s.a s.b s.hab s.hb c hbc hc w by_cases hw1 : w.val < s.b + 1 · have hwa : w.val ≤ s.a + 1 := by omega have hF_eq : F = Polynomial.C (P.evalWire ⟨w.val, by omega⟩) := by dsimp 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 := by dsimp 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; omega have hbc_lt : s.b < c := by have := w.isLt; omega have hb_lt : s.b < τ := lt_of_lt_of_le hbc_lt hc have 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 hwb have hu := s.huntapped s.b hb_lt le_rfl rcases 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_step cases 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_step cases hop : P.gateOp s.b hb_lt <;> rw [hop] at hF_step <;> simp only [SLPOp.applyPolyT] at hF_step · left rw [hF_step, add_comm, Polynomial.leadingCoeff_X_add_C] exact intRootSet_C_eq_empty (c := 1) (by decide) · left rw [hF_step, leadingCoeff_C_sub_X] have hm1 : (-1 : Polynomial ℤ) = Polynomial.C (-1) := by simp rw [hm1]; exact intRootSet_C_eq_empty (c := -1) (by decide) · right refine ⟨⟨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_step cases hop : P.gateOp s.b hb_lt <;> rw [hop] at hF_step <;> simp only [SLPOp.applyPolyT] at hF_step · left rw [hF_step, Polynomial.leadingCoeff_X_add_C] exact intRootSet_C_eq_empty (c := 1) (by decide) · left rw [hF_step, sub_eq_add_neg, ← map_neg, Polynomial.leadingCoeff_X_add_C] exact intRootSet_C_eq_empty (c := 1) (by decide) · right refine ⟨⟨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_step cases hop : P.gateOp s.b hb_lt <;> rw [hop] at hF_step <;> simp only [SLPOp.applyPolyT] at hF_step · left have h2x : (Polynomial.X + Polynomial.X : Polynomial (Polynomial ℤ)) = Polynomial.C (Polynomial.C 2) * Polynomial.X := by simp only [map_ofNat]; ring rw [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 · left have hx2 : (Polynomial.X * Polynomial.X : Polynomial (Polynomial ℤ)) = Polynomial.X ^ 2 := by ring rw [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 := by by_contra h0 set F := evalFormalT P s.a s.b s.hab s.hb (s.b + 1) (Nat.le_succ _) hb_lt ⟨s.b + 2, by omega⟩ have hdeg0 : F.natDegree = 0 := by omega have 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⟩ rfl have hu := s.huntapped s.b hb_lt le_rfl have hnot_w : ¬ (s.a + 2 ≤ s.b + 2 ∧ s.b + 2 ≤ s.b) := by omega have hspec := evalFormalT_spec P s (s.b + 1) (Nat.le_succ _) hb_lt ⟨s.b + 2, by omega⟩ hnot_w change F.eval (P.evalWire ⟨s.b + 1, by omega⟩) = P.evalWire ⟨s.b + 2, by omega⟩ at hspec rcases 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_step cases 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_step rw [hF_step, Polynomial.eval_zero] at hspec exact 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_step cases 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_step rw [hF_step, Polynomial.eval_zero] at hspec exact hval_ne hspec.symm · rw [hF_step, mul_comm, Polynomial.natDegree_C_mul_X _ hq0] at hdeg0; omega · rw [hA, hB] at hF_step cases 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 := by simp only [map_ofNat]; ring have 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_step rw [hF_step, Polynomial.eval_zero] at hspec exact hval_ne hspec.symm · have hx2 : (Polynomial.X * Polynomial.X : Polynomial (Polynomial ℤ)) = Polynomial.X ^ 2 := by ring rw [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 where hne : L ≠ [] hord : SegsOrdered L hstep_mid : ∀ i (hi : i + 1 < L.length), (L[i + 1]'hi).a ≤ (L[i]'(by omega)).b + 1 hstep_last : ∀ (hpos : 0 < L.length), τ ≤ (L[L.length - 1]'(by omega)).b + 1 hhead_gap : ∀ (hpos : 0 < L.length), 2 * (L[0]'hpos).a + 6 ≤ (L[0]'hpos).b - (L[0]'hpos).a hmid_gap : ∀ i (hi : i + 1 < L.length), 5 ≤ (L[i + 1]'hi).b - (L[i + 1]'hi).a hactive : ∀ 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-squaring multiplication. -/ 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 ≤ τ := by unfold stageBound split_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 (by have hle : ∀ (i k : ℕ) (hi : i < L.length) (hk : k < L.length), i ≤ k → (L[i]'hi).b ≤ (L[k]'hk).b := by intro i k hi hk hik induction k with | zero => have hi0 : i = 0 := by omega simp 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' hk have hlt := (L[k' + 1]'hk).hab omega exact 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 := by simp [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 := by simp [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 := by simp [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) := by classical have hq_pos : 0 < L.length := List.length_pos_of_ne_nil hws.hne set a0 := (L[0]'hq_pos).a intro r induction r using Nat.strong_induction_on with | ind r ih => intro hr_le u hu hune cases r with | zero => clear hr_le have hu_a0 : u.val < a0 + 2 := by simpa [stageBound, hq_pos, a0] using hu have 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 := by have heq : P.evalWire u = (P.prefix a0 ha0_le).evalWire ⟨u.val, hu_a0⟩ := hpr.symm rw [heq] exact le_trans ((P.prefix a0 ha0_le).wireDeg_le_maxDeg _) (P.prefix a0 ha0_le).maxDeg_le_two_pow have 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 ^ a0 omega | succ r' => have hr'_lt : r' < L.length := by omega set sr := L[r']'hr'_lt set cr := stageBound L (r' + 1) have hcr_eq : cr = segNextC L ⟨r', hr'_lt⟩ := rfl have 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 := by dsimp only [cr, stageBound, sr] split_ifs with h · exact hws.hstep_mid r' h · have hr'_last : r' = L.length - 1 := by omega have hlast := hws.hstep_last hq_pos have : L[L.length - 1]'(by omega) = L[r']'hr'_lt := by simp [hr'_last] simpa [this] using hlast have hmon : nonSqMulCount P L r' ≤ nonSqMulCount P L (r' + 1) := by unfold nonSqMulCount exact 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) hune exact 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 omega have hb' : u.val - 1 ≤ τ := by have := sr.hb; omega have 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').1 have hu_eq : (⟨(u.val - 1) + 1, by have := u.isLt; omega⟩ : Fin (τ + 2)) = u := Fin.ext (by dsimp; omega) rw [hu_eq] at hpow set 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)).isLt have := sr.hab; have := sr.hb; omega⟩ have hw_in_lt : w_in.val < sr.a + 2 := (P.gateIn1 sr.a _).isLt have hw_in_ne : P.evalWire w_in ≠ 0 := by intro h0 rw [hpow, show P.evalWire w_in = 0 from h0, zero_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega)))] at hune exact hune rfl have hrc_eq : intRootCount (P.evalWire u) = intRootCount (P.evalWire w_in) := by rw [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_ne exact 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 omega have hcr_eq_succ : cr = sr.b + 1 := by omega have hbr_lt_cr : sr.b < cr := by omega have hbr_lt_τ : sr.b < τ := lt_of_lt_of_le hbr_lt_cr hcr_le set P' : SLP cr := P.prefix cr hcr_le have hP'_eval : P'.evalPoly = P.evalWire u := by unfold SLP.evalPoly rw [P.evalWire_prefix cr hcr_le ⟨cr + 1, Nat.lt_succ_self _⟩] congr 1; apply Fin.ext; dsimp; omega have hP'_ne : P'.evalPoly ≠ 0 := by rw [hP'_eval]; exact hune set L' : List (UntappedSeg P') := restrictTake L hws.hord r' hr'_lt cr hcr_le hbr_le_cr have hL'_len : L'.length = r' + 1 := restrictTake_length L hws.hord r' hr'_lt cr hcr_le hbr_le_cr have hL'_ne : L' ≠ [] := by intro h; simp [h] at hL'_len have hL'_ord : SegsOrdered L' := by apply segsOrdered_of_adj intro i hi rw [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⟩ := by intro j by_cases hj : j.val + 1 < L'.length · have hj_L : j.val + 1 < L.length := by omega unfold segNextC rw [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 := by unfold segNextC; rw [dite_eq_right_of_eq_false (eq_false hj)] have hj_eq : j.val = r' := by omega have hfin : (⟨j.val, by omega⟩ : Fin L.length) = ⟨r', hr'_lt⟩ := Fin.ext hj_eq rw [hleft, congrArg (segNextC L) hfin] exact hcr_eq have 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 := by intro hpos rw [restrictTake_getElem_a, restrictTake_getElem_b, hL'_nextC ⟨0, hpos⟩] have hstep0 : segNextC L ⟨0, hq_pos⟩ ≤ (L[0]'hq_pos).b + 1 := by unfold segNextC split_ifs with h1 · exact hws.hstep_mid 0 h1 · dsimp only at h1 have hlen1 : L.length - 1 = 0 := by omega have hlast := hws.hstep_last hq_pos simpa [hlen1] using hlast have hhg := hws.hhead_gap hq_pos have hnc0 : segNextC L ⟨(⟨0, hpos⟩ : Fin L'.length).val, by omega⟩ = segNextC L ⟨0, hq_pos⟩ := rfl rw [hnc0] omega have 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 := by intro i hi rw [restrictTake_getElem_a, restrictTake_getElem_b, restrictTake_getElem_b, hL'_nextC ⟨i + 1, hi⟩] have hi_L : i + 1 < L.length := by omega have hstep1 : (L[i + 1]'hi_L).a ≤ (L[i]'(by omega)).b + 1 := hws.hstep_mid i hi_L have hstep2 : segNextC L ⟨i + 1, hi_L⟩ ≤ (L[i + 1]'hi_L).b + 1 := by unfold segNextC split_ifs with h2 · exact hws.hstep_mid (i + 1) h2 · dsimp only at h2 have hlast := hws.hstep_last hq_pos have heq : L.length - 1 = i + 1 := by omega have : L[L.length - 1]'(by omega) = L[i + 1]'hi_L := by simp [heq] simpa [this] using hlast have hmg := hws.hmid_gap i hi_L have hnc_i : segNextC L ⟨(⟨i + 1, hi⟩ : Fin L'.length).val, by omega⟩ = segNextC L ⟨i + 1, hi_L⟩ := rfl rw [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 := by have hnot_last : ¬ ((L'.length - 1) + 1 < L'.length) := by omega have hql_idx : L'.length - 1 = r' := by omega have 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.symm have hpos_P := evalFormalT_natDegree_pos_of_active P sr hbr_lt_τ (by rw [hu_fin]; exact hune) hact_r have 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⟩ := by unfold segFormalPoly have ha_eq : (L'[L'.length - 1]'(by omega)).a = sr.a := by rw [restrictTake_getElem_a]; simp [hql_idx, sr] have hb_eq : (L'[L'.length - 1]'(by omega)).b = sr.b := by rw [restrictTake_getElem_b]; simp [hql_idx, sr] have hnc_eq : segNextC L' ⟨L'.length - 1, by omega⟩ = cr := by unfold 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 := by unfold segWTarget; rw [dite_eq_right_of_eq_false (eq_false hnot_last)] dsimp; omega have 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₂ := by rintro a₁ b₁ c₁ hab₁ hb₁ hbc₁ hc₁ w₁ _ _ _ hab₂ hb₂ hbc₂ hc₂ w₂ rfl rfl rfl hw have hweq : w₁ = w₂ := Fin.ext hw subst hweq exact 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_eq rw [hEq_F]; exact hpos_P obtain ⟨hF'_deg, hsub'⟩ := slp_gap_dominance_root_cover P' hP'_ne L' hL'_ne hL'_ord hL'_head hL'_mid htop_pos dsimp 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 := by set 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)).isLt have := s0'.hab; have := s0'.hb; omega⟩ have hw0'_deg : w0'.natDegree ≤ 2 ^ a0 := by have ha0' : s0'.a = a0 := restrictTake_getElem_a L hws.hord r' hr'_lt cr hcr_le hbr_le_cr 0 (List.length_pos_of_ne_nil hL'_ne) 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 := by dsimp 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 := by have h2' := (P'.prefix s0'.a (by have := s0'.hab; have := s0'.hb; omega)).maxDeg_le_two_pow simpa [ha0'] using h2' exact le_trans h1 h2 have hcard_w0m : (intRootSet (w0' - 1)).card ≤ 2 ^ a0 := by have h1 : (intRootSet (w0' - 1)).card ≤ (w0' - 1).natDegree := le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _) have h2 : (w0' - 1).natDegree ≤ w0'.natDegree := by simpa using Polynomial.natDegree_sub_le w0' (1 : Polynomial ℤ) omega have hcard_w0 : (intRootSet w0').card ≤ 2 ^ a0 := by have h1 : (intRootSet w0').card ≤ w0'.natDegree := le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _) omega have hcard_w0p : (intRootSet (w0' + 1)).card ≤ 2 ^ a0 := by have h1 : (intRootSet (w0' + 1)).card ≤ (w0' + 1).natDegree := le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _) have h2 : (w0' + 1).natDegree ≤ w0'.natDegree := by have h := Polynomial.natDegree_add_le w0' (1 : Polynomial ℤ) rw [Polynomial.natDegree_one] at h exact le_trans h (max_le le_rfl (Nat.zero_le _)) omega have 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 ^ a0 omega -- 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 := by intro j set sj' := L'[j.val]'j.isLt set cj := segNextC L' j have hj_lt_L : j.val < L.length := by omega set sj := L[j.val]'hj_lt_L have ha_j : sj'.a = sj.a := restrictTake_getElem_a L hws.hord r' hr'_lt cr hcr_le hbr_le_cr j.val j.isLt have hb_j : sj'.b = sj.b := restrictTake_getElem_b L hws.hord r' hr'_lt cr hcr_le hbr_le_cr j.val j.isLt have hcj_eq : cj = segNextC L ⟨j.val, hj_lt_L⟩ := hL'_nextC j have hstep_j : cj ≤ sj'.b + 1 := by rw [hcj_eq, hb_j] unfold segNextC split_ifs with h1 · exact hws.hstep_mid j.val h1 · dsimp only at h1 have hlast := hws.hstep_last hq_pos have heq : L.length - 1 = j.val := by omega have : L[L.length - 1]'(by omega) = L[j.val]'hj_lt_L := by simp [heq] simpa [this] using hlast have hnot_j : ¬ (sj'.a + 2 ≤ (segWTarget P' L' j).val ∧ (segWTarget P' L' j).val ≤ sj'.b) := by unfold segWTarget split_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; omega have 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 := by unfold intRootCount segFormalPoly; rw [hempty]; simp rw [h0]; split_ifs <;> omega · have hmul_P : isNonSqMulIdx P L j.val := by refine ⟨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_cj have hcj_le_cr : cj ≤ cr := segNextC_le L' j have hsb_lt_cr : sj.b < cr := by rw [← hb_j]; exact lt_of_lt_of_le hb_lt_cj hcj_le_cr refine ⟨hb_lt_cj', ?_, ?_⟩ · have hop1 : P'.gateOp sj.b hsb_lt_cr = .mul := by have 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; rfl rw [← 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 := by rw [← 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; rfl exact hcongr hsb_lt_cr _ hb_j.symm have 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 := by rw [← 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; rfl exact hcongr hsb_lt_cr _ hb_j.symm rw [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 := by rw [← hlc_eq] exact Polynomial.leadingCoeff_ne_zero.mpr (fun h0 => by have hdegj := hF'_deg j change 1 ≤ (evalFormalT P' sj'.a sj'.b sj'.hab sj'.hb cj _ _ _).natDegree at hdegj rw [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 := by dsimp 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) := by calc _ ≤ ∑ 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) := by rw [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)) := by set S := ∑ j ∈ (Finset.range (r' + 1)).filter (isNonSqMulIdx P L), 2 ^ ((Finset.range j).filter (isNonSqMulIdx P L)).card have hfac : ∑ j ∈ (Finset.range (r' + 1)).filter (isNonSqMulIdx P L), 3 * 2 ^ (a0 + nonSqMulCount P L j) = (3 * 2 ^ a0) * S := by rw [Finset.mul_sum] refine Finset.sum_congr rfl fun j _ => ?_ unfold nonSqMulCount rw [pow_add, mul_assoc] have hfactor : 3 * 2 ^ a0 + (3 * 2 ^ a0) * S = (3 * 2 ^ a0) * (1 + S) := by ring rw [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 nonSqMulCount rw [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) := by have hlt : τ + 1 < stageBound L L.length + 2 := by simp [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-squaring multiplications (`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 := by have h := slp_well_spaced_mul_bound P hP L hws simpa [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) := by classical by_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} := by intro x hx have 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 hr rw [he, Polynomial.eval_pow, Polynomial.eval_X] at hr exact Finset.mem_singleton.mpr (eq_zero_of_pow_eq_zero hr) have hle1 : intRootCount P.evalPoly ≤ 1 := by calc intRootCount P.evalPoly ≤ ({0} : Finset ℤ).card := Finset.card_le_card hsub _ = 1 := Finset.card_singleton 0 have 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 := by have hex_add : ∃ t : Fin τ, P.gateOp t.val t.isLt ≠ .mul ∧ ¬ P.gateIsTopSq t.val t.isLt := by have hgen : ∀ {n : ℕ} (Q : SLP n), Q.addSubCount ≠ 0 → ∃ t : Fin n, Q.gateOp t.val t.isLt ≠ .mul ∧ ¬ Q.gateIsTopSq t.val t.isLt := by intro n Q induction Q with | nil => intro h; simp [SLP.addSubCount] at h | snoc Q' op i j ih => intro hne cases op with | mul => simp only [SLP.addSubCount] at hne rcases 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.1 exact hgen P hA0 obtain ⟨t_add, ht_add_op, ht_add_nt⟩ := hex_add set 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 then if hb : (L[i]'h).b < τ then ⟨(L[i]'h).b, hb⟩ else t_add else t_add have hf_mem : ∀ i ∈ S_mul, f i ∈ S_B ∧ P.gateOp (f i).val (f i).isLt = .mul := by intro i hi have hi_lt : i < L.length := Finset.mem_range.mp (Finset.mem_filter.mp hi).1 have hmul_i : isNonSqMulIdx P L i := (Finset.mem_filter.mp hi).2 rcases 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⟩ := by simp [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 ℕ) := by intro i hi j hj hfeq have hi_lt : i < L.length := Finset.mem_range.mp (Finset.mem_filter.mp hi).1 have hj_lt : j < L.length := Finset.mem_range.mp (Finset.mem_filter.mp hj).1 rcases (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 hfeq have hb_eq : (L[i]'hi_lt).b = (L[j]'hj_lt).b := congrArg Fin.val hfeq by_contra hne have hstrict : ∀ (p q : ℕ) (hp : p < L.length) (hq : q < L.length), p < q → (L[p]'hp).b < (L[q]'hq).b := by intro p q hp hq hpq induction q with | zero => omega | succ q' ihq => by_cases hpq' : p = q' · subst hpq' have hadj := segsOrdered_adj L hws.hord p hq have hlt := (L[p + 1]'hq).hab omega · have h1 := ihq (by omega) (by omega) have hadj := segsOrdered_adj L hws.hord q' hq have hlt := (L[q' + 1]'hq).hab omega rcases 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; omega have ht_not_mem : t_add ∉ S_mul.image f := by intro hm rcases Finset.mem_image.mp hm with ⟨i, hi, hfi⟩ have hop := (hf_mem i hi).2 rw [hfi] at hop exact ht_add_op hop have hsub_B : insert t_add (S_mul.image f) ⊆ S_B := by intro t ht rcases 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).1 have hcard_eq : (S_mul.image f).card = S_mul.card := Finset.card_image_of_injOn hf_inj have hcard_B : P.nonTopSqCount = S_B.card := P.nonTopSqCount_eq_card have hle := Finset.card_le_card hsub_B rw [Finset.card_insert_of_notMem ht_not_mem, hcard_eq] at hle change S_mul.card + 1 ≤ P.nonTopSqCount omega have hmain := slp_well_spaced_mul_bound P hP L hws rw [ha0, Nat.zero_add] at hmain calc 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