module public import Research.PvsNP.Tau.PureSquaringCollapse public import Mathlib.Data.Nat.Log @[expose] public section namespace Research.PvsNP.Tau /-! # Unconditional Elementary `o(2^τ)` Upper Bound for All Constant-Free SLPs over `ℤ` This module proves an **unconditional, completely elementary, pure-`ℤ` sub-trivial upper bound** on the number of distinct integer roots of **every** nonzero polynomial computed by a constant-free straight-line program `P : SLP τ` over `{+, -, ×}` from `{1, X}`: `slp_intRootCount_o_two_pow`: For every `τ : ℕ` and every `P : SLP τ` with `P.evalPoly ≠ 0`, `τ * (intRootCount P.evalPoly) ^ 28 ≤ 2 ^ 40 * 2 ^ (28 * τ)` (equivalently, `z_ℤ(P) ≤ 2^(10/7) · 2^τ / τ^(1/28) = o(2^τ)`). The proof uses **zero** real analysis, **zero** Khovanskii/Pfaffian theory, and **zero** `p`-adic analysis. It combines: 1. **`slp_pure_squaring_collapse_deg`** (from `PureSquaringCollapse.lean`): when the number of non-(top-degree pure-squaring) gates `k = P.nonTopSqCount` satisfies `k ≥ K`, we immediately have `(intRootCount P.evalPoly) ^ 7 ≤ 2 ^ (7 * τ - 2 * K)`. 2. **Multi-Segment Gap-Dominance Theorem (`slp_multi_segment_gap_dominance`):** when `k < K`, the `τ - k` top-degree pure-squaring gates partition into at most `3 * k + 1` untapped unbranched pure-squaring segments `[a_s, b_s)`. A pure-combinatorial demotion lemma extracts a nonempty subset of surviving segments satisfying local gap inequalities `g_0 ≥ 2 * R_0 + R_1 + 5`, `g_r ≥ R_{r-1} + R_r + 3`, and `g_r ≥ Δ`. Treating each surviving segment's output `U_r = w_r ^ (2 ^ g_r)` as a formal indeterminate `T` over `ℤ[X]` and propagating Cauchy's root bound backward across the segments forces every integer root `x` of `P.evalPoly = 0` either to kill some leading coefficient `c_r(x) = 0` (of degree `≤ 2 ^ (τ - g_r)`) or to satisfy `w_0(x) ∈ {-1, 0, 1}` (at most `3 * 2 ^ R_0` roots), yielding `intRootCount P.evalPoly ≤ (q + 3) * 2 ^ (τ - Δ)`. -/ open Polynomial /-! ## 1. Prefix Extraction, Gate Accessors, and Unbranched Top-Squaring Chains -/ /-- Prefix of `P : SLP τ` consisting of its first `t` gates (`t ≤ τ`). -/ def SLP.prefix : {τ : ℕ} → SLP τ → (t : ℕ) → t ≤ τ → SLP t | 0, .nil, 0, _ => .nil | _ + 1, .snoc P op i j, t, ht => if h : t ≤ _ then P.prefix t h else have heq : t = _ + 1 := by omega heq ▸ SLP.snoc P op i j /-- `P.prefix τ le_rfl = P`. -/ lemma SLP.prefix_self : ∀ {τ : ℕ} (P : SLP τ), P.prefix τ le_rfl = P | 0, .nil => rfl | τ + 1, .snoc P op i j => by have hnot : ¬ (τ + 1 ≤ τ) := by omega simp [SLP.prefix, hnot] /-- `(SLP.snoc P op i j).prefix t ht = P.prefix t h` whenever `t ≤ τ`. -/ lemma SLP.prefix_snoc_le {τ : ℕ} (P : SLP τ) (op : SLPOp) (i j : Fin (τ + 2)) {t : ℕ} (ht : t ≤ τ) : (SLP.snoc P op i j).prefix t (Nat.le_succ_of_le ht) = P.prefix t ht := by simp [SLP.prefix, ht] /-- `P.prefix` agrees with `P` on `evalWire` for all wires `< t + 2`. -/ lemma SLP.evalWire_prefix : ∀ {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t ≤ τ) (w : Fin (t + 2)), (P.prefix t ht).evalWire w = P.evalWire ⟨w.val, by omega⟩ | 0, .nil, 0, _, w => rfl | τ + 1, .snoc P op i j, t, ht, w => by by_cases h : t ≤ τ · rw [SLP.prefix_snoc_le P op i j h] have hlt : w.val < τ + 2 := by omega simp only [SLP.evalWire, hlt, ↓reduceDIte] exact P.evalWire_prefix t h w · have heq : t = τ + 1 := by omega subst heq rw [SLP.prefix_self] /-- Gate operation at 0-based gate index `t < τ`. -/ def SLP.gateOp : {τ : ℕ} → SLP τ → (t : ℕ) → t < τ → SLPOp | _ + 1, .snoc P op _ _, t, _ => if h : t < _ then P.gateOp t h else op /-- First input wire index (`Fin (t + 2)`) of gate `t < τ`. -/ def SLP.gateIn1 : {τ : ℕ} → SLP τ → (t : ℕ) → t < τ → Fin (t + 2) | _ + 1, .snoc P _ i _, t, ht => if h : t < _ then P.gateIn1 t h else ⟨i.val, by have := i.isLt; omega⟩ /-- Second input wire index (`Fin (t + 2)`) of gate `t < τ`. -/ def SLP.gateIn2 : {τ : ℕ} → SLP τ → (t : ℕ) → t < τ → Fin (t + 2) | _ + 1, .snoc P _ _ j, t, ht => if h : t < _ then P.gateIn2 t h else ⟨j.val, by have := j.isLt; omega⟩ /-- `P.prefix (t + 1) ht` equals `SLP.snoc (P.prefix t _) (P.gateOp t _) (P.gateIn1 t _) (P.gateIn2 t _)`. -/ lemma SLP.prefix_succ_eq : ∀ {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t < τ), P.prefix (t + 1) ht = SLP.snoc (P.prefix t (le_of_lt ht)) (P.gateOp t ht) (P.gateIn1 t ht) (P.gateIn2 t ht) | τ + 1, .snoc P op i j, t, ht => by by_cases h : t < τ · rw [SLP.prefix_snoc_le P op i j h, SLP.prefix_snoc_le P op i j (le_of_lt h)] simp only [SLP.gateOp, SLP.gateIn1, SLP.gateIn2, h, ↓reduceDIte] exact P.prefix_succ_eq t h · have heq : t = τ := by omega subst heq rw [SLP.prefix_self, SLP.prefix_snoc_le P op i j le_rfl, SLP.prefix_self] simp [SLP.gateOp, SLP.gateIn1, SLP.gateIn2] /-- Wire `t + 2` of `P : SLP τ` (`t < τ`) is computed by applying `gateOp t` to `gateIn1 t` and `gateIn2 t`. -/ lemma SLP.evalWire_gate {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t < τ) : P.evalWire ⟨t + 2, by omega⟩ = (P.gateOp t ht).applyPoly (P.evalWire ⟨(P.gateIn1 t ht).val, by have := (P.gateIn1 t ht).isLt; omega⟩) (P.evalWire ⟨(P.gateIn2 t ht).val, by have := (P.gateIn2 t ht).isLt; omega⟩) := by have h1 := (P.evalWire_prefix (t + 1) ht ⟨t + 2, by omega⟩).symm rw [h1, P.prefix_succ_eq t ht] have hnot : ¬ (t + 2 < t + 2) := by omega simp only [SLP.evalWire, hnot, ↓reduceDIte] rw [P.evalWire_prefix t (le_of_lt ht) (P.gateIn1 t ht), P.evalWire_prefix t (le_of_lt ht) (P.gateIn2 t ht)] /-- Predicate: gate `t < τ` of `P` is a top-degree pure squaring. -/ def SLP.gateIsTopSq {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t < τ) : Prop := isTopPureSquare (P.prefix t (le_of_lt ht)) (P.gateOp t ht) (P.gateIn1 t ht) (P.gateIn2 t ht) lemma SLP.gateIsTopSq_snoc_lt {τ : ℕ} (P : SLP τ) (op : SLPOp) (i j : Fin (τ + 2)) {t : ℕ} (ht : t < τ) : (SLP.snoc P op i j).gateIsTopSq t (Nat.lt_succ_of_lt ht) ↔ P.gateIsTopSq t ht := by unfold SLP.gateIsTopSq rw [SLP.prefix_snoc_le P op i j (le_of_lt ht)] simp [SLP.gateOp, SLP.gateIn1, SLP.gateIn2, ht] lemma SLP.gateIsTopSq_snoc_last {τ : ℕ} (P : SLP τ) (op : SLPOp) (i j : Fin (τ + 2)) : (SLP.snoc P op i j).gateIsTopSq τ (Nat.lt_succ_self τ) ↔ isTopPureSquare P op i j := by unfold SLP.gateIsTopSq rw [SLP.prefix_snoc_le P op i j le_rfl, SLP.prefix_self] simp [SLP.gateOp, SLP.gateIn1, SLP.gateIn2] /-- `P.nonTopSqCount` equals the number of gates `t : Fin τ` that are not top-degree pure squarings. -/ lemma SLP.nonTopSqCount_eq_card : ∀ {τ : ℕ} (P : SLP τ) [DecidablePred (fun t : Fin τ => ¬ P.gateIsTopSq t.val t.isLt)], P.nonTopSqCount = (Finset.univ.filter (fun t : Fin τ => ¬ P.gateIsTopSq t.val t.isLt)).card | 0, .nil, _ => by simp [SLP.nonTopSqCount] | τ + 1, .snoc P op i j, _ => by classical have ih := P.nonTopSqCount_eq_card let S_all : Finset (Fin (τ + 1)) := Finset.univ.filter (fun t : Fin (τ + 1) => ¬ (SLP.snoc P op i j).gateIsTopSq t.val t.isLt) let S_prev : Finset (Fin τ) := Finset.univ.filter (fun t : Fin τ => ¬ P.gateIsTopSq t.val t.isLt) let f : Fin τ ↪ Fin (τ + 1) := ⟨fun t => ⟨t.val, Nat.lt_succ_of_lt t.isLt⟩, fun a b h => Fin.ext (by simpa using congrArg Fin.val h)⟩ let last : Fin (τ + 1) := ⟨τ, Nat.lt_succ_self τ⟩ have hlast_not : last ∉ S_prev.map f := by intro hm rcases Finset.mem_map.mp hm with ⟨t, _, ht⟩ have hval : t.val = τ := congrArg Fin.val ht have := t.isLt omega have hsplit : S_all = if isTopPureSquare P op i j then S_prev.map f else insert last (S_prev.map f) := by ext t by_cases hlt : t.val < τ · have hne : t ≠ last := fun h => by have := congrArg Fin.val h; dsimp [last] at this; omega have hmem_map : t ∈ S_prev.map f ↔ ¬ P.gateIsTopSq t.val hlt := by constructor · intro hm rcases Finset.mem_map.mp hm with ⟨u, hu, hut⟩ have hu_eq : u.val = t.val := congrArg Fin.val hut have hu' : ¬ P.gateIsTopSq u.val u.isLt := (Finset.mem_filter.mp hu).2 simpa [hu_eq] using hu' · intro hnot exact Finset.mem_map.mpr ⟨⟨t.val, hlt⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, hnot⟩, Fin.ext rfl⟩ split_ifs with htop · simp only [S_all, Finset.mem_filter, Finset.mem_univ, true_and, hmem_map] exact not_congr (SLP.gateIsTopSq_snoc_lt P op i j hlt) · simp only [S_all, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_insert, hne, false_or, hmem_map] exact not_congr (SLP.gateIsTopSq_snoc_lt P op i j hlt) · have ht_eq : t = last := Fin.ext (by dsimp [last]; omega) subst ht_eq have htop_iff : (SLP.snoc P op i j).gateIsTopSq τ (Nat.lt_succ_self τ) ↔ isTopPureSquare P op i j := SLP.gateIsTopSq_snoc_last P op i j split_ifs with htop · simp [S_all, last, htop_iff, htop, hlast_not] · simp [S_all, last, htop_iff, htop] have hcard_all : S_all.card = P.nonTopSqCount + if isTopPureSquare P op i j then 0 else 1 := by rw [hsplit, ih] split_ifs · simp [S_prev] · rw [Finset.card_insert_of_notMem hlast_not, Finset.card_map] have hcnt : (SLP.snoc P op i j).nonTopSqCount = P.nonTopSqCount + if isTopPureSquare P op i j then 0 else 1 := by by_cases h : isTopPureSquare P op i j <;> simp [SLP.nonTopSqCount, h] rw [hcnt, ← hcard_all] /-- Maximum wire degree `maxDeg` is at least `1` and at most `2 ^ τ`, and bounds all wire degrees. -/ lemma SLP.one_le_maxDeg {τ : ℕ} (P : SLP τ) : 1 ≤ P.maxDeg := (P.topTwoDegs_inv).1 lemma SLP.wireDeg_le_maxDeg {τ : ℕ} (P : SLP τ) (w : Fin (τ + 2)) : (P.evalWire w).natDegree ≤ P.maxDeg := (P.topTwoDegs_inv).2.2.1 w lemma SLP.maxDeg_le_two_pow : ∀ {τ : ℕ} (P : SLP τ), P.maxDeg ≤ 2 ^ τ | 0, .nil => by simp [SLP.maxDeg, SLP.topTwoDegs] | τ + 1, .snoc P op i j => by have ih := P.maxDeg_le_two_pow have h1 : (P.evalWire i).natDegree ≤ P.maxDeg := P.wireDeg_le_maxDeg i have h2 : (P.evalWire j).natDegree ≤ P.maxDeg := P.wireDeg_le_maxDeg j have hd : (op.applyPoly (P.evalWire i) (P.evalWire j)).natDegree ≤ 2 * P.maxDeg := by cases op · exact le_trans (Polynomial.natDegree_add_le _ _) (by omega) · exact le_trans (Polynomial.natDegree_sub_le _ _) (by omega) · exact le_trans Polynomial.natDegree_mul_le (by omega) have hM : (SLP.snoc P op i j).maxDeg = max P.maxDeg (op.applyPoly (P.evalWire i) (P.evalWire j)).natDegree := by change (if P.maxDeg ≤ (op.applyPoly (P.evalWire i) (P.evalWire j)).natDegree then ((op.applyPoly (P.evalWire i) (P.evalWire j)).natDegree, P.maxDeg) else (P.maxDeg, max P.secondDeg (op.applyPoly (P.evalWire i) (P.evalWire j)).natDegree)).1 = _ split_ifs with h <;> omega rw [hM, pow_succ] omega /-- Monotonicity of `maxDeg` along prefixes. -/ lemma SLP.maxDeg_prefix_mono {τ : ℕ} (P : SLP τ) {t1 t2 : ℕ} (h12 : t1 ≤ t2) (ht2 : t2 ≤ τ) : (P.prefix t1 (le_trans h12 ht2)).maxDeg ≤ (P.prefix t2 ht2).maxDeg := by obtain ⟨w, hw⟩ := (P.prefix t1 (le_trans h12 ht2)).exists_wire_eq_maxDeg have hle := (P.prefix t2 ht2).wireDeg_le_maxDeg ⟨w.val, by omega⟩ have heq : (P.prefix t2 ht2).evalWire ⟨w.val, by omega⟩ = (P.prefix t1 (le_trans h12 ht2)).evalWire w := by rw [P.evalWire_prefix t2 ht2 ⟨w.val, by omega⟩, P.evalWire_prefix t1 (le_trans h12 ht2) w] rw [heq] at hle rw [← hw] exact hle /-- If gate `t` is a top-degree pure squaring, then wire `t + 2` has degree `2 * D₁(t)`, `D₁(t + 1) = 2 * D₁(t)`, and EVERY wire `< t + 2` has degree strictly less than `D₁(t + 1)`. -/ lemma SLP.topSq_strict_maxDeg {τ : ℕ} (P : SLP τ) {t : ℕ} (ht : t < τ) (htop : P.gateIsTopSq t ht) : (P.evalWire ⟨t + 2, by omega⟩).natDegree = 2 * (P.prefix t (le_of_lt ht)).maxDeg ∧ (P.prefix (t + 1) ht).maxDeg = 2 * (P.prefix t (le_of_lt ht)).maxDeg ∧ (∀ w : Fin (t + 2), (P.evalWire ⟨w.val, by omega⟩).natDegree < (P.prefix (t + 1) ht).maxDeg) := by rcases htop with ⟨hop, hij, hdeg⟩ set P0 := P.prefix t (le_of_lt ht) set wi := P0.evalWire (P.gateIn1 t ht) have hD1_pos : 1 ≤ P0.maxDeg := P0.one_le_maxDeg have hwi_ne : wi ≠ 0 := by intro h0 have : wi.natDegree = 0 := by simp [h0] have : P0.maxDeg = 0 := by rw [← hdeg]; exact this omega have hwire_eq : P.evalWire ⟨t + 2, by omega⟩ = wi * wi := by have h1 : P.evalWire ⟨(P.gateIn1 t ht).val, by have := (P.gateIn1 t ht).isLt; omega⟩ = wi := (P.evalWire_prefix t (le_of_lt ht) (P.gateIn1 t ht)).symm have h2 : P.evalWire ⟨(P.gateIn2 t ht).val, by have := (P.gateIn2 t ht).isLt; omega⟩ = wi := by rw [← P.evalWire_prefix t (le_of_lt ht) (P.gateIn2 t ht), ← hij] rw [P.evalWire_gate t ht, hop] simp only [SLPOp.applyPoly, h1, h2] have hsq_deg : (P.evalWire ⟨t + 2, by omega⟩).natDegree = 2 * P0.maxDeg := by rw [hwire_eq, Polynomial.natDegree_mul hwi_ne hwi_ne] have : wi.natDegree = P0.maxDeg := hdeg omega have hM1 : (P.prefix (t + 1) ht).maxDeg = 2 * P0.maxDeg := by rw [P.prefix_succ_eq t ht] have hd_eq : ((P.gateOp t ht).applyPoly (P0.evalWire (P.gateIn1 t ht)) (P0.evalWire (P.gateIn2 t ht))).natDegree = 2 * P0.maxDeg := by rw [hop, ← hij] simp only [SLPOp.applyPoly] change (wi * wi).natDegree = 2 * P0.maxDeg rw [Polynomial.natDegree_mul hwi_ne hwi_ne] have : wi.natDegree = P0.maxDeg := hdeg omega have hle : P0.maxDeg ≤ 2 * P0.maxDeg := by omega change (if P0.maxDeg ≤ ((P.gateOp t ht).applyPoly (P0.evalWire (P.gateIn1 t ht)) (P0.evalWire (P.gateIn2 t ht))).natDegree then (((P.gateOp t ht).applyPoly (P0.evalWire (P.gateIn1 t ht)) (P0.evalWire (P.gateIn2 t ht))).natDegree, P0.maxDeg) else _).1 = 2 * P0.maxDeg rw [hd_eq]; simp [hle] refine ⟨hsq_deg, hM1, fun w => ?_⟩ rw [hM1, ← P.evalWire_prefix t (le_of_lt ht) w] have hle_w := P0.wireDeg_le_maxDeg w change (P0.evalWire w).natDegree < 2 * P0.maxDeg omega /-- **Unbranched Chain Lemma:** If both gates `t` and `t + 1` are top-degree pure squarings, then gate `t + 1` MUST square wire `t + 2` (the output of gate `t`). -/ lemma SLP.topSq_next_input_eq {τ : ℕ} (P : SLP τ) {t : ℕ} (ht1 : t + 1 < τ) (htop0 : P.gateIsTopSq t (by omega)) (htop1 : P.gateIsTopSq (t + 1) ht1) : (P.gateIn1 (t + 1) ht1).val = t + 2 ∧ (P.gateIn2 (t + 1) ht1).val = t + 2 := by have hstrict := (P.topSq_strict_maxDeg (by omega) htop0).2.2 rcases htop1 with ⟨_, hij, hdeg1⟩ have hin_eq : (P.gateIn1 (t + 1) ht1).val = t + 2 := by by_contra hne have hlt : (P.gateIn1 (t + 1) ht1).val < t + 2 := by have := (P.gateIn1 (t + 1) ht1).isLt; omega have hlt_deg := hstrict ⟨(P.gateIn1 (t + 1) ht1).val, hlt⟩ dsimp only at hlt_deg have heq_w : (P.prefix (t + 1) (le_of_lt ht1)).evalWire (P.gateIn1 (t + 1) ht1) = P.evalWire ⟨(P.gateIn1 (t + 1) ht1).val, by omega⟩ := P.evalWire_prefix (t + 1) (le_of_lt ht1) (P.gateIn1 (t + 1) ht1) have : (P.evalWire ⟨(P.gateIn1 (t + 1) ht1).val, by omega⟩).natDegree = (P.prefix (t + 1) (le_of_lt ht1)).maxDeg := by rw [← heq_w]; exact hdeg1 omega exact ⟨hin_eq, by rw [← hij]; exact hin_eq⟩ /-- **Contiguous Top-Squaring Run Evaluation:** If every gate `t ∈ [a, b)` (`a < b ≤ τ`) is a top-degree pure squaring, then wire `b + 1` equals `(P.evalWire w_in) ^ (2 ^ (b - a))` where `w_in = P.gateIn1 a _`, and its degree is `2 ^ (b - a) * D₁(a)`. -/ lemma SLP.topSq_run_eval {τ : ℕ} (P : SLP τ) {a b : ℕ} (hab : a < b) (hb : b ≤ τ) (hrun : ∀ t (ht : t < τ), a ≤ t → t < b → P.gateIsTopSq t ht) : P.evalWire ⟨b + 1, by omega⟩ = (P.evalWire ⟨(P.gateIn1 a (by omega)).val, by have := (P.gateIn1 a (by omega)).isLt; omega⟩) ^ (2 ^ (b - a)) ∧ (P.evalWire ⟨b + 1, by omega⟩).natDegree = 2 ^ (b - a) * (P.prefix a (by omega)).maxDeg := by induction b, hab using Nat.le_induction with | base => have htop := hrun a (by omega) le_rfl (by omega) have hsq := P.topSq_strict_maxDeg (by omega) htop rcases htop with ⟨hop, hij, _⟩ have heq : P.evalWire ⟨a + 1 + 1, by omega⟩ = (P.evalWire ⟨(P.gateIn1 a (by omega)).val, by have := (P.gateIn1 a (by omega)).isLt; omega⟩) ^ (2 ^ (a + 1 - a)) := by have hgate := P.evalWire_gate a (by omega) have h2eq : P.evalWire ⟨(P.gateIn2 a (by omega)).val, by have := (P.gateIn2 a (by omega)).isLt; omega⟩ = P.evalWire ⟨(P.gateIn1 a (by omega)).val, by have := (P.gateIn1 a (by omega)).isLt; omega⟩ := by have hval : (P.gateIn2 a (by omega)).val = (P.gateIn1 a (by omega)).val := congrArg Fin.val hij.symm congr 1; exact Fin.ext hval rw [hop] at hgate simp only [SLPOp.applyPoly] at hgate rw [h2eq] at hgate have hsub : a + 1 - a = 1 := by omega rw [hsub, pow_one, sq] simpa using hgate refine ⟨heq, ?_⟩ have hsub : a + 1 - a = 1 := by omega rw [hsub, pow_one] exact hsq.1 | succ b' hb' ih => have hprev := ih (by omega) (fun t ht h1 h2 => hrun t ht h1 (by omega)) have htop_prev := hrun (b' - 1) (by omega) (by omega) (by omega) have htop_cur := hrun b' (by omega) (by omega) (by omega) have hprev_succ : (b' - 1) + 1 = b' := by omega have hin := P.topSq_next_input_eq (t := b' - 1) (by omega) htop_prev (by simpa [hprev_succ] using htop_cur) have hgate1_congr : ∀ {t1 t2 : ℕ} (h1 : t1 < τ) (h2 : t2 < τ), t1 = t2 → (P.gateIn1 t1 h1).val = (P.gateIn1 t2 h2).val := by rintro t1 _ h1 h2 rfl; rfl have hgate2_congr : ∀ {t1 t2 : ℕ} (h1 : t1 < τ) (h2 : t2 < τ), t1 = t2 → (P.gateIn2 t1 h1).val = (P.gateIn2 t2 h2).val := by rintro t1 _ h1 h2 rfl; rfl have hin1 : (P.gateIn1 b' (by omega)).val = b' + 1 := by have h1 := hin.1 rw [hgate1_congr (by omega) (by omega) hprev_succ] at h1 omega have hin2 : (P.gateIn2 b' (by omega)).val = b' + 1 := by have h2 := hin.2 rw [hgate2_congr (by omega) (by omega) hprev_succ] at h2 omega have hop : P.gateOp b' (by omega) = .mul := htop_cur.1 have hgate := P.evalWire_gate b' (by omega) rw [hop] at hgate simp only [SLPOp.applyPoly] at hgate have hwire_sq : P.evalWire ⟨b' + 1 + 1, by omega⟩ = P.evalWire ⟨b' + 1, by omega⟩ * P.evalWire ⟨b' + 1, by omega⟩ := by simpa [hin1, hin2] using hgate have hsub : b' + 1 - a = (b' - a) + 1 := by omega refine ⟨?_, ?_⟩ · rw [hwire_sq, hprev.1, hsub, pow_succ, pow_mul, sq] · have hne : P.evalWire ⟨b' + 1, by omega⟩ ≠ 0 := by intro h0 have : (P.evalWire ⟨b' + 1, by omega⟩).natDegree = 0 := by simp [h0] rw [hprev.2] at this have hpos1 : 1 ≤ 2 ^ (b' - a) := Nat.one_le_pow _ _ (by omega) have hpos2 : 1 ≤ (P.prefix a (by omega)).maxDeg := (P.prefix a (by omega)).one_le_maxDeg nlinarith rw [hwire_sq, Polynomial.natDegree_mul hne hne, hprev.2, hsub, pow_succ] ring /-! ## 2. Untapped Segments & Pure-Combinatorial Demotion Lemma -/ /-- An untapped pure-squaring segment `[a, b)` (`a < b ≤ τ`) of `P : SLP τ`: every gate in `[a, b)` is a top-degree pure squaring, and no gate `t' ≥ b` reads any strict intermediate wire in `{a + 2, ..., b}`. -/ structure UntappedSeg {τ : ℕ} (P : SLP τ) where a : ℕ b : ℕ hab : a < b hb : b ≤ τ hrun : ∀ t (ht : t < τ), a ≤ t → t < b → P.gateIsTopSq t ht huntapped : ∀ t' (ht' : t' < τ), b ≤ t' → ¬ (a + 2 ≤ (P.gateIn1 t' ht').val ∧ (P.gateIn1 t' ht').val ≤ b) ∧ ¬ (a + 2 ≤ (P.gateIn2 t' ht').val ∧ (P.gateIn2 t' ht').val ≤ b) /-- A valid ordered list of disjoint untapped segments inside `[0, τ)`. -/ def SegsOrdered {τ : ℕ} {P : SLP τ} : List (UntappedSeg P) → Prop | [] => True | [_] => True | s1 :: s2 :: rest => s1.b ≤ s2.a ∧ SegsOrdered (s2 :: rest) /-- Total length of a list of segments. -/ def segsTotalLen {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) : ℕ := (L.map (fun s => s.b - s.a)).sum private lemma segsOrdered_head_le {τ : ℕ} {P : SLP τ} : ∀ (s0 : UntappedSeg P) (rest : List (UntappedSeg P)), SegsOrdered (s0 :: rest) → ∀ s ∈ s0 :: rest, s0.a ≤ s.a | s0, [], _, s, hs => by simp at hs; subst hs; exact le_rfl | s0, s1 :: rest, ⟨h01, hrest⟩, s, hs => by rcases List.mem_cons.mp hs with rfl | hs' · exact le_rfl · exact le_trans (le_of_lt s0.hab) (le_trans h01 (segsOrdered_head_le s1 rest hrest s hs')) /-- Total length of ordered disjoint segments inside `[0, τ)` is at most `τ`. -/ private lemma segsTotalLen_le_sub {τ : ℕ} {P : SLP τ} : ∀ (L : List (UntappedSeg P)), SegsOrdered L → ∀ m : ℕ, (∀ s ∈ L, m ≤ s.a) → segsTotalLen L ≤ τ - m | [], _, _, _ => Nat.zero_le _ | [s], _, m, hm => by have := hm s (by simp) have := s.hab; have := s.hb simp [segsTotalLen]; omega | s1 :: s2 :: rest, ⟨h12, hrest⟩, m, hm => by have hm1 : m ≤ s1.a := hm s1 (by simp) have hall2 : ∀ s ∈ s2 :: rest, s1.b ≤ s.a := fun s hs => le_trans h12 (segsOrdered_head_le s2 rest hrest s hs) have ih := segsTotalLen_le_sub (s2 :: rest) hrest s1.b hall2 have := s1.hab; have := s1.hb simp only [segsTotalLen, List.map_cons, List.sum_cons] at ih ⊢ omega lemma segsTotalLen_le {τ : ℕ} {P : SLP τ} (L : List (UntappedSeg P)) (hord : SegsOrdered L) : segsTotalLen L ≤ τ := by simpa using segsTotalLen_le_sub L hord 0 (fun _ _ => Nat.zero_le _) /-- Pairwise non-overlapping ordering on a list of intervals `(a, b)`. -/ private def PairsOrdered : List (ℕ × ℕ) → Prop | [] => True | [_] => True | p1 :: p2 :: rest => p1.2 ≤ p2.1 ∧ PairsOrdered (p2 :: rest) private lemma pairsOrdered_append_singleton : ∀ (L : List (ℕ × ℕ)) (q : ℕ × ℕ), PairsOrdered L → (∀ p, L.getLast? = some p → p.2 ≤ q.1) → PairsOrdered (L ++ [q]) | [], _, _, _ => trivial | [p], q, _, hlast => ⟨hlast p rfl, trivial⟩ | p1 :: p2 :: rest, q, ⟨h12, hrest⟩, hlast => ⟨h12, pairsOrdered_append_singleton (p2 :: rest) q hrest (fun p hp => hlast p (by simpa using hp))⟩ private lemma pairsOrdered_replace_last : ∀ (L : List (ℕ × ℕ)) (p q : ℕ × ℕ), PairsOrdered (L ++ [p]) → p.1 = q.1 → PairsOrdered (L ++ [q]) | [], _, _, _, _ => trivial | [r], p, q, ⟨hrp, _⟩, hpq => ⟨by omega, trivial⟩ | r1 :: r2 :: rest, p, q, ⟨h12, hrest⟩, hpq => ⟨h12, pairsOrdered_replace_last (r2 :: rest) p q hrest hpq⟩ /-- Clean strengthened interval-partition induction: `L.length ≤ bad + cut + (if L ends at n with ¬ isCut (n - 1) then 1 else 0)`. -/ private lemma partition_intervals_strong (isGood isCut : ℕ → Prop) [DecidablePred isGood] [DecidablePred isCut] : ∀ n : ℕ, ∃ L : List (ℕ × ℕ), (∀ p ∈ L, p.1 < p.2 ∧ p.2 ≤ n) ∧ PairsOrdered L ∧ (∀ p ∈ L, ∀ t, p.1 ≤ t → t < p.2 → isGood t) ∧ (∀ p ∈ L, ∀ t, p.1 ≤ t → t + 1 < p.2 → ¬ isCut t) ∧ ((L.map (fun p => p.2 - p.1)).sum = ((Finset.range n).filter isGood).card) ∧ (L.length ≤ ((Finset.range n).filter (fun t => ¬ isGood t)).card + ((Finset.range n).filter (fun t => isGood t ∧ isCut t)).card + if (∃ p, L.getLast? = some p ∧ p.2 = n ∧ ¬ isCut (n - 1)) then 1 else 0) := by intro n induction n with | zero => refine ⟨[], by simp, trivial, by simp, by simp, by simp, by simp⟩ | succ m ih => obtain ⟨L, hbounds, hchain, hgood, hnocut, hsum, hlen⟩ := ih have hrange_good : ((Finset.range (m + 1)).filter isGood).card = ((Finset.range m).filter isGood).card + if isGood m then 1 else 0 := by by_cases h : isGood m <;> simp [Finset.range_add_one, Finset.filter_insert, h] have hrange_bad : ((Finset.range (m + 1)).filter (fun t => ¬ isGood t)).card = ((Finset.range m).filter (fun t => ¬ isGood t)).card + if ¬ isGood m then 1 else 0 := by by_cases h : isGood m <;> simp [Finset.range_add_one, Finset.filter_insert, h] have hrange_cut : ((Finset.range (m + 1)).filter (fun t => isGood t ∧ isCut t)).card = ((Finset.range m).filter (fun t => isGood t ∧ isCut t)).card + if isGood m ∧ isCut m then 1 else 0 := by by_cases h : isGood m ∧ isCut m <;> simp [Finset.range_add_one, Finset.filter_insert, h] by_cases hgm : isGood m · by_cases hext : ∃ p, L.getLast? = some p ∧ p.2 = m ∧ ¬ isCut (m - 1) · rcases hext with ⟨p, hlast, hp2, hnc_prev⟩ have hL_ne : L ≠ [] := by intro h; simp [h] at hlast have hL_eq : L = L.dropLast ++ [p] := by have := List.dropLast_append_getLast hL_ne have hgl : L.getLast hL_ne = p := by rw [List.getLast?_eq_some_getLast hL_ne] at hlast exact Option.some.inj hlast rw [hgl] at this exact this.symm set L0 := L.dropLast have hbounds0 : ∀ q ∈ L0 ++ [p], q.1 < q.2 ∧ q.2 ≤ m := by rw [← hL_eq]; exact hbounds have hchain0 : PairsOrdered (L0 ++ [p]) := by rw [← hL_eq]; exact hchain have hgood0 : ∀ q ∈ L0 ++ [p], ∀ t, q.1 ≤ t → t < q.2 → isGood t := by rw [← hL_eq]; exact hgood have hnocut0 : ∀ q ∈ L0 ++ [p], ∀ t, q.1 ≤ t → t + 1 < q.2 → ¬ isCut t := by rw [← hL_eq]; exact hnocut have hsum0 : ((L0 ++ [p]).map (fun q => q.2 - q.1)).sum = ((Finset.range m).filter isGood).card := by rw [← hL_eq]; exact hsum refine ⟨L0 ++ [(p.1, m + 1)], ?_, ?_, ?_, ?_, ?_, ?_⟩ · intro q hq rcases List.mem_append.mp hq with hq0 | hq1 · have := hbounds0 q (List.mem_append_left _ hq0); omega · simp at hq1; subst hq1 have := hbounds0 p (by simp); omega · exact pairsOrdered_replace_last L0 p (p.1, m + 1) hchain0 rfl · intro q hq t h1 h2 rcases List.mem_append.mp hq with hq0 | hq1 · exact hgood0 q (List.mem_append_left _ hq0) t h1 h2 · simp at hq1; subst hq1 by_cases htm : t = m · subst htm; exact hgm · exact hgood0 p (by simp) t h1 (by omega) · intro q hq t h1 h2 rcases List.mem_append.mp hq with hq0 | hq1 · exact hnocut0 q (List.mem_append_left _ hq0) t h1 h2 · simp at hq1; subst hq1 by_cases htm : t = m - 1 · subst htm; exact hnc_prev · exact hnocut0 p (by simp) t h1 (by omega) · have hp_lt := (hbounds0 p (by simp)).1 simp only [List.map_append, List.sum_append, List.map_singleton, List.sum_singleton] at hsum0 ⊢ rw [hrange_good]; simp only [hgm, ↓reduceIte]; omega · have hlen_eq : (L0 ++ [(p.1, m + 1)]).length = L.length := by rw [hL_eq]; simp rw [hlen_eq, hrange_bad, hrange_cut] have hprev_cond : ∃ p', L.getLast? = some p' ∧ p'.2 = m ∧ ¬ isCut (m - 1) := ⟨p, hlast, hp2, hnc_prev⟩ simp only [hprev_cond, ↓reduceIte] at hlen have hnew_last : (L0 ++ [(p.1, m + 1)]).getLast? = some (p.1, m + 1) := by simp by_cases hcm : isCut m · have h2 : ¬ (∃ q, (L0 ++ [(p.1, m + 1)]).getLast? = some q ∧ q.2 = m + 1 ∧ ¬ isCut (m + 1 - 1)) := by rintro ⟨q, _, _, hnc⟩; simp at hnc; exact hnc hcm simp only [hgm, hcm, and_self, not_true_eq_false, ↓reduceIte, h2] omega · have h2 : ∃ q, (L0 ++ [(p.1, m + 1)]).getLast? = some q ∧ q.2 = m + 1 ∧ ¬ isCut (m + 1 - 1) := ⟨(p.1, m + 1), hnew_last, rfl, by simpa using hcm⟩ simp only [hgm, hcm, and_false, not_true_eq_false, ↓reduceIte, h2] omega · -- Not extendable: append (m, m + 1) refine ⟨L ++ [(m, m + 1)], ?_, ?_, ?_, ?_, ?_, ?_⟩ · intro q hq rcases List.mem_append.mp hq with hq0 | hq1 · have := hbounds q hq0; omega · simp at hq1; subst hq1; omega · exact pairsOrdered_append_singleton L (m, m + 1) hchain (fun x hx => (hbounds x (List.mem_of_getLast? hx)).2) · intro q hq t h1 h2 rcases List.mem_append.mp hq with hq0 | hq1 · exact hgood q hq0 t h1 h2 · simp at hq1; subst hq1 have : t = m := by omega subst this; exact hgm · intro q hq t h1 h2 rcases List.mem_append.mp hq with hq0 | hq1 · exact hnocut q hq0 t h1 h2 · simp at hq1; subst hq1; omega · simp only [List.map_append, List.sum_append, List.map_singleton, List.sum_singleton] rw [hsum, hrange_good]; simp only [hgm, ↓reduceIte]; omega · simp only [List.length_append, List.length_singleton] rw [hrange_bad, hrange_cut] simp only [hext, ↓reduceIte] at hlen have hnew_last : (L ++ [(m, m + 1)]).getLast? = some (m, m + 1) := by simp by_cases hcm : isCut m · have h2 : ¬ (∃ q, (L ++ [(m, m + 1)]).getLast? = some q ∧ q.2 = m + 1 ∧ ¬ isCut (m + 1 - 1)) := by rintro ⟨q, _, _, hnc⟩; simp at hnc; exact hnc hcm simp only [hgm, hcm, and_self, not_true_eq_false, ↓reduceIte, h2] omega · have h2 : ∃ q, (L ++ [(m, m + 1)]).getLast? = some q ∧ q.2 = m + 1 ∧ ¬ isCut (m + 1 - 1) := ⟨(m, m + 1), hnew_last, rfl, by simpa using hcm⟩ simp only [hgm, hcm, and_false, not_true_eq_false, ↓reduceIte, h2] omega · -- Gate m is NOT good: keep L unchanged! refine ⟨L, ?_, hchain, hgood, hnocut, ?_, ?_⟩ · intro q hq; have := hbounds q hq; omega · rw [hsum, hrange_good]; simp [hgm] · rw [hrange_bad, hrange_cut] have hnot_new : ¬ (∃ q, L.getLast? = some q ∧ q.2 = m + 1 ∧ ¬ isCut (m + 1 - 1)) := by rintro ⟨q, hq, hq2, _⟩ have := (hbounds q (List.mem_of_getLast? hq)).2 omega simp only [hgm, not_false_eq_true, false_and, ↓reduceIte, hnot_new] split_ifs at hlen <;> omega /-- **Untapped Segment Extraction Lemma:** Every `P : SLP τ` admits an ordered list `L` of disjoint untapped pure-squaring segments with `L.length ≤ 3 * P.nonTopSqCount + 1` and `segsTotalLen L = τ - P.nonTopSqCount`. -/ lemma extract_untapped_segments {τ : ℕ} (P : SLP τ) : ∃ L : List (UntappedSeg P), SegsOrdered L ∧ L.length ≤ 3 * P.nonTopSqCount + 1 ∧ segsTotalLen L = τ - P.nonTopSqCount := by classical let isGood : ℕ → Prop := fun t => ∃ ht : t < τ, P.gateIsTopSq t ht let isCut : ℕ → Prop := fun t => ∃ (t' : ℕ) (ht' : t' < τ), ¬ P.gateIsTopSq t' ht' ∧ ((P.gateIn1 t' ht').val = t + 2 ∨ (P.gateIn2 t' ht').val = t + 2) obtain ⟨L0, hbounds, hchain, hgood, hnocut, hsum, hlen⟩ := partition_intervals_strong isGood isCut τ -- Count bad gates = P.nonTopSqCount have hbad_card : ((Finset.range τ).filter (fun t => ¬ isGood t)).card = P.nonTopSqCount := by rw [P.nonTopSqCount_eq_card] refine Finset.card_bij (fun t ht => (⟨t, Finset.mem_range.mp (Finset.mem_filter.mp ht).1⟩ : Fin τ)) ?_ ?_ ?_ · intro t ht have h1 := Finset.mem_filter.mp ht have ht_lt : t < τ := Finset.mem_range.mp h1.1 refine Finset.mem_filter.mpr ⟨Finset.mem_univ _, fun htop => h1.2 ⟨ht_lt, htop⟩⟩ · intro a _ b _ h; exact congrArg Fin.val h · intro u hu have hnot := (Finset.mem_filter.mp hu).2 refine ⟨u.val, Finset.mem_filter.mpr ⟨Finset.mem_range.mpr u.isLt, ?_⟩, Fin.ext rfl⟩ rintro ⟨_, htop⟩; exact hnot htop -- Count good gates = τ - P.nonTopSqCount have hgood_card : ((Finset.range τ).filter isGood).card = τ - P.nonTopSqCount := by have hcomp := Finset.card_filter_add_card_filter_not (s := Finset.range τ) (p := isGood) rw [Finset.card_range, hbad_card] at hcomp omega -- Count cut gates ≤ 2 * P.nonTopSqCount have hcut_card : ((Finset.range τ).filter (fun t => isGood t ∧ isCut t)).card ≤ 2 * P.nonTopSqCount := by let Bset : Finset (Fin τ) := Finset.univ.filter (fun t' : Fin τ => ¬ P.gateIsTopSq t'.val t'.isLt) let Cset : Finset ℕ := Bset.biUnion (fun t' => ({(P.gateIn1 t'.val t'.isLt).val, (P.gateIn2 t'.val t'.isLt).val} : Finset ℕ)) have hsub : ((Finset.range τ).filter (fun t => isGood t ∧ isCut t)).image (fun t => t + 2) ⊆ Cset := by intro w hw rcases Finset.mem_image.mp hw with ⟨t, ht, rfl⟩ rcases (Finset.mem_filter.mp ht).2.2 with ⟨t', ht', hnot, hread⟩ refine Finset.mem_biUnion.mpr ⟨⟨t', ht'⟩, Finset.mem_filter.mpr ⟨Finset.mem_univ _, hnot⟩, ?_⟩ rcases hread with h1 | h2 · simp [h1] · simp [h2] have hinj : Function.Injective (fun t : ℕ => t + 2) := fun a b h => by dsimp only at h; omega calc ((Finset.range τ).filter (fun t => isGood t ∧ isCut t)).card = (((Finset.range τ).filter (fun t => isGood t ∧ isCut t)).image (fun t => t + 2)).card := (Finset.card_image_of_injective _ hinj).symm _ ≤ Cset.card := Finset.card_le_card hsub _ ≤ ∑ t' ∈ Bset, ({(P.gateIn1 t'.val t'.isLt).val, (P.gateIn2 t'.val t'.isLt).val} : Finset ℕ).card := Finset.card_biUnion_le _ ≤ ∑ _t' ∈ Bset, 2 := Finset.sum_le_sum (fun _ _ => le_trans (Finset.card_insert_le _ _) (by simp)) _ = 2 * Bset.card := by simp [Nat.mul_comm] _ = 2 * P.nonTopSqCount := by rw [P.nonTopSqCount_eq_card] -- Convert each (a, b) ∈ L0 into an UntappedSeg P! let hmk : ∀ p ∈ L0, UntappedSeg P := fun p hp => { a := p.1 b := p.2 hab := (hbounds p hp).1 hb := (hbounds p hp).2 hrun := fun t ht h1 h2 => by obtain ⟨_, htop⟩ := hgood p hp t h1 h2 exact htop huntapped := fun t' ht' hbt' => by -- Neither input of gate t' ≥ b can lie in [a + 2, b]! -- If t' is NOT topSq: ruled out by ¬ isCut (w - 2). -- If t' IS topSq: deg(wire w) < D₁(w - 1) ≤ D₁(t') since w - 2 < b - 1 < t'! by_cases htop' : P.gateIsTopSq t' ht' · have hne : ∀ w : Fin (t' + 2), (P.prefix t' (le_of_lt ht')).evalWire w = P.evalWire ⟨w.val, by omega⟩ → (P.evalWire ⟨w.val, by omega⟩).natDegree = (P.prefix t' (le_of_lt ht')).maxDeg → ¬ (p.1 + 2 ≤ w.val ∧ w.val ≤ p.2) := by intro w _ hdeg_w ⟨hw1, hw2⟩ -- Gate w.val - 1 lies in [p.1 + 1, p.2 - 1], wait: wire w.val is created by gate t0 = w.val - 2! -- Since w.val ≤ p.2 ≤ t', gate t0 + 1 = w.val - 1 < p.2 ≤ t' is ALSO in [p.1, p.2), hence isTopSq! have ht0_lt : w.val - 1 < τ := by omega have ht0_top : P.gateIsTopSq (w.val - 1) ht0_lt := by obtain ⟨_, ht⟩ := hgood p hp (w.val - 1) (by omega) (by omega) exact ht have hstrict := (P.topSq_strict_maxDeg ht0_lt ht0_top).2.2 ⟨w.val, by omega⟩ dsimp only at hstrict have hmono := P.maxDeg_prefix_mono (t1 := (w.val - 1) + 1) (t2 := t') (by omega) (le_of_lt ht') omega rcases htop' with ⟨_, hij, hdeg1⟩ have h1 : ¬ (p.1 + 2 ≤ (P.gateIn1 t' ht').val ∧ (P.gateIn1 t' ht').val ≤ p.2) := by apply hne (P.gateIn1 t' ht') (P.evalWire_prefix t' (le_of_lt ht') (P.gateIn1 t' ht')) rw [← P.evalWire_prefix t' (le_of_lt ht') (P.gateIn1 t' ht')]; exact hdeg1 exact ⟨h1, by rw [← hij]; exact h1⟩ · constructor · rintro ⟨hw1, hw2⟩ have hnc := hnocut p hp ((P.gateIn1 t' ht').val - 2) (by omega) (by omega) exact hnc ⟨t', ht', htop', Or.inl (by omega)⟩ · rintro ⟨hw1, hw2⟩ have hnc := hnocut p hp ((P.gateIn2 t' ht').val - 2) (by omega) (by omega) exact hnc ⟨t', ht', htop', Or.inr (by omega)⟩ } let L : List (UntappedSeg P) := L0.pmap hmk (fun _ h => h) have hord : SegsOrdered L := by have hgen : ∀ (M : List (ℕ × ℕ)) (hM : ∀ p ∈ M, p ∈ L0), PairsOrdered M → SegsOrdered (M.pmap hmk hM) := by intro M induction M with | nil => intros; trivial | cons p1 rest ih => cases rest with | nil => intros; trivial | cons p2 rest' => intro hM hch exact ⟨hch.1, ih (fun q hq => hM q (List.mem_cons_of_mem _ hq)) hch.2⟩ exact hgen L0 (fun _ h => h) hchain refine ⟨L, hord, ?_, ?_⟩ · have hlenL : L.length = L0.length := List.length_pmap rw [hlenL] split_ifs at hlen <;> omega · have hmap : L.map (fun s => s.b - s.a) = L0.map (fun p => p.2 - p.1) := by simp [L, List.map_pmap, hmk] rw [segsTotalLen, hmap, hsum, hgood_card] /-- Gap condition for a nonempty ordered list of untapped segments `L` with minimum length `Δ`. -/ def ValidGapSegs {τ : ℕ} {P : SLP τ} (Δ : ℕ) (L : List (UntappedSeg P)) : Prop := L ≠ [] ∧ SegsOrdered L ∧ (∀ s ∈ L, Δ ≤ s.b - s.a) ∧ (∀ (s0 : UntappedSeg P) (rest : List (UntappedSeg P)), L = s0 :: rest → let nextA := match rest with | [] => τ | s1 :: _ => s1.a 2 * s0.a + (nextA - s0.b) + 5 ≤ s0.b - s0.a) ∧ (∀ (i : ℕ) (hi : i + 1 < L.length), let sPrev := L[i]'(by omega) let sCur := L[i + 1]'hi let nextA := if h2 : i + 2 < L.length then (L[i + 2]'h2).a else τ (sCur.a - sPrev.b) + (nextA - sCur.b) + 3 ≤ sCur.b - sCur.a) /-- Erasing an element preserves `SegsOrdered`. -/ private lemma segsOrdered_eraseIdx {τ : ℕ} {P : SLP τ} : ∀ (L : List (UntappedSeg P)), SegsOrdered L → ∀ idx : ℕ, SegsOrdered (L.eraseIdx idx) | [], _, _ => trivial | [_], _, idx => by cases idx <;> simp [SegsOrdered] | s1 :: s2 :: rest, ⟨h12, hrest⟩, 0 => hrest | s1 :: s2 :: rest, ⟨h12, hrest⟩, 1 => by cases rest with | nil => trivial | cons s3 rest' => exact ⟨le_trans h12 (le_trans (le_of_lt s2.hab) hrest.1), hrest.2⟩ | s1 :: s2 :: rest, ⟨h12, hrest⟩, idx + 2 => by change SegsOrdered (s1 :: (s2 :: rest).eraseIdx (idx + 1)) have ih := segsOrdered_eraseIdx (s2 :: rest) hrest (idx + 1) cases rest with | nil => simp [List.eraseIdx, SegsOrdered, h12] | cons s3 rest' => exact ⟨h12, ih⟩ /-- Erasing index `idx < L.length` decreases `segsTotalLen` by `(L[idx]).b - (L[idx]).a`. -/ private lemma segsTotalLen_eraseIdx {τ : ℕ} {P : SLP τ} : ∀ (L : List (UntappedSeg P)) (idx : ℕ) (hidx : idx < L.length), segsTotalLen (L.eraseIdx idx) + ((L[idx]'hidx).b - (L[idx]'hidx).a) = segsTotalLen L | s :: rest, 0, _ => by simp [segsTotalLen, Nat.add_comm] | s :: rest, idx + 1, hidx => by have ih := segsTotalLen_eraseIdx rest idx (by simpa using hidx) simp only [List.eraseIdx_cons_succ, segsTotalLen, List.map_cons, List.sum_cons, List.getElem_cons_succ] at ih ⊢ omega /-- Helper: `s.a` and `nextA - s.b` are bounded by `R_tot = τ - segsTotalLen L`! -/ private lemma gap_le_Rtot_head {τ : ℕ} {P : SLP τ} (s0 : UntappedSeg P) (rest : List (UntappedSeg P)) (hord : SegsOrdered (s0 :: rest)) : let nextA := match rest with | [] => τ | s1 :: _ => s1.a s0.a + (nextA - s0.b) ≤ τ - segsTotalLen (s0 :: rest) := by have hle_tot := segsTotalLen_le (s0 :: rest) hord cases rest with | nil => simp only [segsTotalLen, List.map_singleton, List.sum_singleton] have := s0.hab; have := s0.hb; omega | cons s1 rest' => have hall : ∀ s ∈ s1 :: rest', s1.a ≤ s.a := segsOrdered_head_le s1 rest' hord.2 have hsub := segsTotalLen_le_sub (s1 :: rest') hord.2 s1.a hall have h01 : s0.b ≤ s1.a := hord.1 have := s0.hab; have := s1.hab; have := s1.hb simp only [segsTotalLen, List.map_cons, List.sum_cons] at hsub hle_tot ⊢ omega /-- Strengthened version of `gap_le_Rtot_mid` that carries `L.head.a` cleanly through induction. -/ private lemma gap_le_Rtot_mid_strong {τ : ℕ} {P : SLP τ} : ∀ (L : List (UntappedSeg P)), SegsOrdered L → ∀ (i : ℕ) (hi : i + 1 < L.length), let sFirst := L[0]'(by omega) let sPrev := L[i]'(by omega) let sCur := L[i + 1]'hi let nextA := if h2 : i + 2 < L.length then (L[i + 2]'h2).a else τ sFirst.a + (sCur.a - sPrev.b) + (nextA - sCur.b) ≤ τ - segsTotalLen L := by intro L induction L with | nil => intro _ i hi; simp at hi | cons s0 rest ih => intro hord i hi cases i with | zero => cases rest with | nil => simp at hi | cons s1 rest' => have hhead := gap_le_Rtot_head s1 rest' hord.2 have h01 : s0.b ≤ s1.a := hord.1 have := s0.hab cases rest' with | nil => have hnot : ¬ (2 < 2) := by omega simp only [List.length_cons, List.length_nil, hnot, ↓reduceDIte, List.getElem_cons_zero, List.getElem_cons_succ, segsTotalLen, List.map_cons, List.map_nil, List.sum_cons, List.sum_nil] have := s1.hab; have := s1.hb; omega | cons s2 rest'' => have hlt2 : 2 < (s0 :: s1 :: s2 :: rest'').length := by simp simp only [hlt2, ↓reduceDIte, List.getElem_cons_succ, List.getElem_cons_zero] simp only [segsTotalLen, List.map_cons, List.sum_cons] at hhead ⊢ omega | succ i' => cases rest with | nil => simp at hi | cons s1 rest' => have hrec := ih hord.2 i' (by simpa using hi) have h01 : s0.b ≤ s1.a := hord.1 have := s0.hab have hiff : (i' + 1 + 2 < (s0 :: s1 :: rest').length) ↔ (i' + 2 < (s1 :: rest').length) := by simp simp only [List.getElem_cons_zero, List.getElem_cons_succ] at hrec ⊢ split_ifs at hrec ⊢ with h1 h2 · simp only [segsTotalLen, List.map_cons, List.sum_cons] at hrec ⊢ omega · omega · omega · simp only [segsTotalLen, List.map_cons, List.sum_cons] at hrec ⊢ omega /-- **Pure-Combinatorial Demotion Lemma:** Given any ordered list `L` of untapped segments of `P` and threshold `Δ`, either some sublist `L'` satisfies `ValidGapSegs Δ L'` with `L'.length ≤ L.length`, OR `τ + Δ + 2 ≤ 3 ^ L.length * (τ - segsTotalLen L + Δ + 2)`. -/ lemma demotion_exists_valid_gaps {τ : ℕ} {P : SLP τ} (Δ : ℕ) : ∀ (n : ℕ) (L : List (UntappedSeg P)), L.length = n → SegsOrdered L → (∃ L' : List (UntappedSeg P), ValidGapSegs Δ L' ∧ L'.length ≤ L.length) ∨ τ + Δ + 2 ≤ 3 ^ n * (τ - segsTotalLen L + Δ + 2) := by intro n induction n with | zero => intro L hlen _ right have hnil : L = [] := List.eq_nil_of_length_eq_zero hlen subst hnil simp [segsTotalLen] | succ n' ih => intro L hlen hord by_cases hvalid : ValidGapSegs Δ L · exact Or.inl ⟨L, hvalid, le_rfl⟩ · -- Find a violating index idx < L.length whose length g ≤ 2 * R_tot + Δ + 4 have hL_ne : L ≠ [] := fun h => by simp [h] at hlen have hex_bad : ∃ (idx : ℕ) (hidx : idx < L.length), (L[idx]'hidx).b - (L[idx]'hidx).a ≤ 2 * (τ - segsTotalLen L) + Δ + 4 := by by_contra hnone push Not at hnone apply hvalid refine ⟨hL_ne, hord, ?_, ?_, ?_⟩ · intro s hs obtain ⟨idx, hidx, rfl⟩ := List.getElem_of_mem hs have := hnone idx hidx omega · intro s0 rest hLeq subst hLeq have hbig := hnone 0 (by simp) have hhead := gap_le_Rtot_head s0 rest hord simp only [List.getElem_cons_zero] at hbig cases rest <;> dsimp only at hhead ⊢ <;> omega · intro i hi have hbig := hnone (i + 1) hi have hmid := gap_le_Rtot_mid_strong L hord i hi omega obtain ⟨idx, hidx, hg_le⟩ := hex_bad let L' := L.eraseIdx idx have hlen' : L'.length = n' := by rw [List.length_eraseIdx_of_lt hidx, hlen]; omega have hord' : SegsOrdered L' := segsOrdered_eraseIdx L hord idx have hsum' : segsTotalLen L' + ((L[idx]'hidx).b - (L[idx]'hidx).a) = segsTotalLen L := segsTotalLen_eraseIdx L idx hidx have hle_L := segsTotalLen_le L hord rcases ih L' hlen' hord' with ⟨L'', hv'', hl''⟩ | hbound' · exact Or.inl ⟨L'', hv'', by omega⟩ · right have hR' : τ - segsTotalLen L' + Δ + 2 ≤ 3 * (τ - segsTotalLen L + Δ + 2) := by have := (L[idx]'hidx).hab omega calc τ + Δ + 2 ≤ 3 ^ n' * (τ - segsTotalLen L' + Δ + 2) := hbound' _ ≤ 3 ^ n' * (3 * (τ - segsTotalLen L + Δ + 2)) := Nat.mul_le_mul_left _ hR' _ = 3 ^ (n' + 1) * (τ - segsTotalLen L + Δ + 2) := by rw [pow_succ]; ring /-! ## 3. Formal Substitution `F(x, T) ∈ (Polynomial ℤ)[X]` & The 4 Block Properties -/ /-- Apply `SLPOp` to two elements of `Polynomial (Polynomial ℤ)`. -/ noncomputable def SLPOp.applyPolyT : SLPOp → Polynomial (Polynomial ℤ) → Polynomial (Polynomial ℤ) → Polynomial (Polynomial ℤ) | .add, A, B => A + B | .sub, A, B => A - B | .mul, A, B => A * B /-- Evaluate `P` in `Polynomial (Polynomial ℤ)` up to gate `t` (`b ≤ t ≤ τ`), replacing wire `b + 1` by the indeterminate `T = Polynomial.X`, keeping all wires `≤ a + 1` as `Polynomial.C (P.evalWire i)`, and setting unread strict intermediate wires `a + 2 .. b` to `0`. -/ noncomputable def evalFormalT {τ : ℕ} (P : SLP τ) (a b : ℕ) (hab : a < b) (hb : b ≤ τ) (t : ℕ) (_hbt : b ≤ t) (ht : t ≤ τ) (w : Fin (t + 2)) : Polynomial (Polynomial ℤ) := if hw : w.val < b + 1 then if hwa : w.val ≤ a + 1 then Polynomial.C (P.evalWire ⟨w.val, by omega⟩) else 0 else if hwb : w.val = b + 1 then Polynomial.X else -- w.val ≥ b + 2, so w.val = t' + 2 with b ≤ t' < t have hprev_lt : w.val - 2 < τ := by omega have hbt' : b ≤ w.val - 2 := by omega let t' := w.val - 2 let in1 := P.gateIn1 t' hprev_lt let in2 := P.gateIn2 t' hprev_lt (P.gateOp t' hprev_lt).applyPolyT (evalFormalT P a b hab hb t' hbt' (le_of_lt hprev_lt) in1) (evalFormalT P a b hab hb t' hbt' (le_of_lt hprev_lt) in2) termination_by t decreasing_by all_goals omega /-- Property 1 (Exact Specialization): evaluating `T := P.evalWire (b + 1)` recovers `P.evalWire w` for every wire `w` that is not a strict intermediate wire of an untapped segment `seg`. -/ lemma evalFormalT_spec {τ : ℕ} (P : SLP τ) (seg : UntappedSeg P) : ∀ (t : ℕ) (hbt : seg.b ≤ t) (ht : t ≤ τ) (w : Fin (t + 2)), ¬ (seg.a + 2 ≤ w.val ∧ w.val ≤ seg.b) → (evalFormalT P seg.a seg.b seg.hab seg.hb t hbt ht w).eval (P.evalWire ⟨seg.b + 1, by omega⟩) = P.evalWire ⟨w.val, by omega⟩ := by intro t induction t using Nat.strong_induction_on with | ind t ih => intro hbt ht w hnot_inter rw [evalFormalT.eq_def] split_ifs with hw hwa hwb · simp · exfalso; exact hnot_inter ⟨by omega, by omega⟩ · simp [hwb] · set t' := w.val - 2 have ht'_lt_t : t' < t := by omega have hbt' : seg.b ≤ t' := by omega have ht'_lt_τ : t' < τ := by omega have hun := seg.huntapped t' ht'_lt_τ hbt' have ih1 := ih t' ht'_lt_t hbt' (le_of_lt ht'_lt_τ) (P.gateIn1 t' ht'_lt_τ) hun.1 have ih2 := ih t' ht'_lt_t hbt' (le_of_lt ht'_lt_τ) (P.gateIn2 t' ht'_lt_τ) hun.2 have hw_eq : w.val = t' + 2 := by omega have hgate := P.evalWire_gate t' ht'_lt_τ have hpoly_eval : ∀ (op : SLPOp) (A B : Polynomial (Polynomial ℤ)) (U : Polynomial ℤ), (op.applyPolyT A B).eval U = op.applyPoly (A.eval U) (B.eval U) := by intro op A B U cases op <;> simp [SLPOp.applyPolyT, SLPOp.applyPoly] rw [hpoly_eval, ih1, ih2] simpa [hw_eq] using hgate.symm /-- Property 2 (`x`-Degree Bound): every `T`-coefficient of `evalFormalT` at horizon `t` has `natDegree ≤ 2 ^ (t - b) * D₁(a)`. -/ lemma evalFormalT_coeff_natDegree_le {τ : ℕ} (P : SLP τ) (a b : ℕ) (hab : a < b) (hb : b ≤ τ) : ∀ (t : ℕ) (hbt : b ≤ t) (ht : t ≤ τ) (w : Fin (t + 2)) (j : ℕ), ((evalFormalT P a b hab hb t hbt ht w).coeff j).natDegree ≤ 2 ^ (t - b) * (P.prefix a (by omega)).maxDeg := by intro t induction t using Nat.strong_induction_on with | ind t ih => intro hbt ht w j rw [evalFormalT.eq_def] split_ifs with hw hwa hwb · rw [Polynomial.coeff_C] split_ifs with hj · have h1 : (P.evalWire ⟨w.val, by omega⟩).natDegree ≤ (P.prefix a (by omega)).maxDeg := by rw [← P.evalWire_prefix a (by omega) ⟨w.val, by omega⟩] exact (P.prefix a (by omega)).wireDeg_le_maxDeg ⟨w.val, by omega⟩ have h2 : 1 ≤ 2 ^ (t - b) := Nat.one_le_pow _ _ (by omega) nlinarith · simp · simp · rw [Polynomial.coeff_X] split_ifs <;> simp · set t' := w.val - 2 have ht'_lt_t : t' < t := by omega have hbt' : b ≤ t' := by omega have ht'_lt_τ : t' < τ := by omega have hpow_le : 2 * 2 ^ (t' - b) ≤ 2 ^ (t - b) := by have : (t' - b) + 1 ≤ t - b := by omega have := Nat.pow_le_pow_right (by omega : 1 ≤ 2) this rwa [pow_succ, Nat.mul_comm] at this set A := evalFormalT P a b hab hb t' hbt' (le_of_lt ht'_lt_τ) (P.gateIn1 t' ht'_lt_τ) set B := evalFormalT P a b hab hb t' hbt' (le_of_lt ht'_lt_τ) (P.gateIn2 t' ht'_lt_τ) have hA : ∀ k, (A.coeff k).natDegree ≤ 2 ^ (t' - b) * (P.prefix a (by omega)).maxDeg := ih t' ht'_lt_t hbt' (le_of_lt ht'_lt_τ) (P.gateIn1 t' ht'_lt_τ) have hB : ∀ k, (B.coeff k).natDegree ≤ 2 ^ (t' - b) * (P.prefix a (by omega)).maxDeg := ih t' ht'_lt_t hbt' (le_of_lt ht'_lt_τ) (P.gateIn2 t' ht'_lt_τ) change (((P.gateOp t' ht'_lt_τ).applyPolyT A B).coeff j).natDegree ≤ 2 ^ (t - b) * (P.prefix a (by omega)).maxDeg cases P.gateOp t' ht'_lt_τ with | add => simp only [SLPOp.applyPolyT, Polynomial.coeff_add] have := le_trans (Polynomial.natDegree_add_le _ _) (max_le (hA j) (hB j)) nlinarith | sub => simp only [SLPOp.applyPolyT, Polynomial.coeff_sub] have := le_trans (Polynomial.natDegree_sub_le _ _) (max_le (hA j) (hB j)) nlinarith | mul => simp only [SLPOp.applyPolyT, Polynomial.coeff_mul] have hsum_deg : ∀ (s : Finset (ℕ × ℕ)) (f : ℕ × ℕ → Polynomial ℤ) (M : ℕ), (∀ p ∈ s, (f p).natDegree ≤ M) → (∑ p ∈ s, f p).natDegree ≤ M := by intro s f M induction s using Finset.induction_on with | empty => intro _; simp | @insert u s' hu ihs => intro hM rw [Finset.sum_insert hu] exact le_trans (Polynomial.natDegree_add_le _ _) (max_le (hM u (Finset.mem_insert_self _ _)) (ihs (fun p hp => hM p (Finset.mem_insert_of_mem hp)))) have hsum : (∑ x ∈ Finset.antidiagonal j, A.coeff x.1 * B.coeff x.2).natDegree ≤ 2 * (2 ^ (t' - b) * (P.prefix a (by omega)).maxDeg) := by apply hsum_deg intro p _ calc (A.coeff p.1 * B.coeff p.2).natDegree ≤ (A.coeff p.1).natDegree + (B.coeff p.2).natDegree := Polynomial.natDegree_mul_le _ ≤ 2 ^ (t' - b) * (P.prefix a (by omega)).maxDeg + 2 ^ (t' - b) * (P.prefix a (by omega)).maxDeg := Nat.add_le_add (hA p.1) (hB p.2) _ = 2 * (2 ^ (t' - b) * (P.prefix a (by omega)).maxDeg) := by ring have hmul_le : 2 * (2 ^ (t' - b) * (P.prefix a (by omega)).maxDeg) ≤ 2 ^ (t - b) * (P.prefix a (by omega)).maxDeg := by rw [← Nat.mul_assoc] exact Nat.mul_le_mul_right _ hpow_le exact le_trans hsum hmul_le /-- `ℓ₁`-norm of `F(x, T)` at an integer `x : ℤ`: `∑_j |(F.coeff j).eval x|`. -/ noncomputable def polyL1At (F : Polynomial (Polynomial ℤ)) (x : ℤ) : ℕ := ∑ j ∈ F.support, ((F.coeff j).eval x).natAbs private lemma polyL1At_eq_sum_range (F : Polynomial (Polynomial ℤ)) (x : ℤ) {N : ℕ} (hN : ∀ j ∈ F.support, j < N) : polyL1At F x = ∑ j ∈ Finset.range N, ((F.coeff j).eval x).natAbs := by unfold polyL1At apply Finset.sum_subset (fun j hj => Finset.mem_range.mpr (hN j hj)) intro j _ hj have : F.coeff j = 0 := Polynomial.notMem_support_iff.mp hj simp [this] private lemma polyL1At_add_le (A B : Polynomial (Polynomial ℤ)) (x : ℤ) : polyL1At (A + B) x ≤ polyL1At A x + polyL1At B x := by set N := max (A.natDegree + 1) (B.natDegree + 1) have hA : ∀ j ∈ A.support, j < N := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega have hB : ∀ j ∈ B.support, j < N := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega have hAB : ∀ j ∈ (A + B).support, j < N := fun j hj => by have h1 := Polynomial.le_natDegree_of_mem_supp j hj have h2 := Polynomial.natDegree_add_le A B omega rw [polyL1At_eq_sum_range (A + B) x hAB, polyL1At_eq_sum_range A x hA, polyL1At_eq_sum_range B x hB, ← Finset.sum_add_distrib] apply Finset.sum_le_sum intro j _ simp only [Polynomial.coeff_add, Polynomial.eval_add] exact Int.natAbs_add_le _ _ private lemma polyL1At_sub_le (A B : Polynomial (Polynomial ℤ)) (x : ℤ) : polyL1At (A - B) x ≤ polyL1At A x + polyL1At B x := by set N := max (A.natDegree + 1) (B.natDegree + 1) have hA : ∀ j ∈ A.support, j < N := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega have hB : ∀ j ∈ B.support, j < N := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega have hAB : ∀ j ∈ (A - B).support, j < N := fun j hj => by have h1 := Polynomial.le_natDegree_of_mem_supp j hj have h2 := Polynomial.natDegree_sub_le A B omega rw [polyL1At_eq_sum_range (A - B) x hAB, polyL1At_eq_sum_range A x hA, polyL1At_eq_sum_range B x hB, ← Finset.sum_add_distrib] apply Finset.sum_le_sum intro j _ simp only [Polynomial.coeff_sub, Polynomial.eval_sub] simpa [sub_eq_add_neg, Int.natAbs_neg] using Int.natAbs_add_le ((A.coeff j).eval x) (-((B.coeff j).eval x)) private lemma polyL1At_mul_le (A B : Polynomial (Polynomial ℤ)) (x : ℤ) : polyL1At (A * B) x ≤ polyL1At A x * polyL1At B x := by set Na := A.natDegree + 1 set Nb := B.natDegree + 1 have hA : ∀ j ∈ A.support, j < Na := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega have hB : ∀ j ∈ B.support, j < Nb := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega have hAB : ∀ j ∈ (A * B).support, j < Na + Nb := fun j hj => by have h1 := Polynomial.le_natDegree_of_mem_supp j hj have h2 := Polynomial.natDegree_mul_le (p := A) (q := B) omega rw [polyL1At_eq_sum_range (A * B) x hAB, polyL1At_eq_sum_range A x hA, polyL1At_eq_sum_range B x hB, Finset.sum_mul_sum] have htri : ∀ (s : Finset (ℕ × ℕ)) (f : ℕ × ℕ → ℤ), (∑ p ∈ s, f p).natAbs ≤ ∑ p ∈ s, (f p).natAbs := by intro s f 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 _) calc ∑ j ∈ Finset.range (Na + Nb), (((A * B).coeff j).eval x).natAbs ≤ ∑ j ∈ Finset.range (Na + Nb), ∑ p ∈ Finset.antidiagonal j, ((A.coeff p.1).eval x).natAbs * ((B.coeff p.2).eval x).natAbs := by apply Finset.sum_le_sum intro j _ rw [Polynomial.coeff_mul, ← Polynomial.coe_evalRingHom, map_sum] calc (∑ p ∈ Finset.antidiagonal j, (Polynomial.evalRingHom x) (A.coeff p.1 * B.coeff p.2)).natAbs ≤ ∑ p ∈ Finset.antidiagonal j, ((Polynomial.evalRingHom x) (A.coeff p.1 * B.coeff p.2)).natAbs := htri _ _ _ = ∑ p ∈ Finset.antidiagonal j, ((A.coeff p.1).eval x).natAbs * ((B.coeff p.2).eval x).natAbs := by apply Finset.sum_congr rfl; intro p _ simp [Int.natAbs_mul] _ ≤ ∑ p ∈ Finset.range Na ×ˢ Finset.range Nb, ((A.coeff p.1).eval x).natAbs * ((B.coeff p.2).eval x).natAbs := by -- Every term with p.1 ≥ Na or p.2 ≥ Nb vanishes! rw [← Finset.sum_biUnion] · apply Finset.sum_le_sum_of_ne_zero intro p hp hne have hp1 : p.1 < Na := by by_contra h have h0 : A.coeff p.1 = 0 := Polynomial.coeff_eq_zero_of_natDegree_lt (by omega) simp [h0] at hne have hp2 : p.2 < Nb := by by_contra h have h0 : B.coeff p.2 = 0 := Polynomial.coeff_eq_zero_of_natDegree_lt (by omega) simp [h0] at hne exact Finset.mem_product.mpr ⟨Finset.mem_range.mpr hp1, Finset.mem_range.mpr hp2⟩ · intro i _ j _ hij exact Finset.disjoint_left.mpr fun p hpi hpj => hij ((Finset.mem_antidiagonal.mp hpi).symm.trans (Finset.mem_antidiagonal.mp hpj)) _ = ∑ i ∈ Finset.range Na, ∑ j ∈ Finset.range Nb, ((A.coeff i).eval x).natAbs * ((B.coeff j).eval x).natAbs := Finset.sum_product _ _ _ /-- Maximum wire magnitude (at least `2`) of `P` up to gate `t` (`t ≤ τ`) at integer `x`. -/ noncomputable def wireHeight {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t ≤ τ) (x : ℤ) : ℕ := max 2 (Finset.univ.sup (fun i : Fin (t + 2) => ((P.prefix t ht).evalWire i).eval x |>.natAbs)) lemma two_le_wireHeight {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t ≤ τ) (x : ℤ) : 2 ≤ wireHeight P t ht x := le_max_left _ _ lemma wire_natAbs_le_wireHeight {τ : ℕ} (P : SLP τ) (t : ℕ) (ht : t ≤ τ) (x : ℤ) (w : Fin (t + 2)) : ((P.evalWire ⟨w.val, by omega⟩).eval x).natAbs ≤ wireHeight P t ht x := by rw [← P.evalWire_prefix t ht w] exact le_trans (Finset.le_sup (f := fun i : Fin (t + 2) => ((P.prefix t ht).evalWire i).eval x |>.natAbs) (Finset.mem_univ w)) (le_max_right _ _) /-- Property 3 (`ℓ₁`-Norm Bound): `polyL1At (evalFormalT ... w) x ≤ (wireHeight P a _ x) ^ (2 ^ (t - b))`. -/ lemma evalFormalT_l1_le {τ : ℕ} (P : SLP τ) (a b : ℕ) (hab : a < b) (hb : b ≤ τ) (x : ℤ) : ∀ (t : ℕ) (hbt : b ≤ t) (ht : t ≤ τ) (w : Fin (t + 2)), polyL1At (evalFormalT P a b hab hb t hbt ht w) x ≤ (wireHeight P a (by omega) x) ^ (2 ^ (t - b)) := by intro t induction t using Nat.strong_induction_on with | ind t ih => intro hbt ht w set H := wireHeight P a (by omega) x have hH2 : 2 ≤ H := two_le_wireHeight P a (by omega) x have hpow1 : 1 ≤ 2 ^ (t - b) := Nat.one_le_pow _ _ (by omega) have hH_le_pow : H ≤ H ^ (2 ^ (t - b)) := Nat.le_self_pow (by omega) H rw [evalFormalT.eq_def] split_ifs with hw hwa hwb · have hC : polyL1At (Polynomial.C (P.evalWire ⟨w.val, by omega⟩)) x ≤ ((P.evalWire ⟨w.val, by omega⟩).eval x).natAbs := by unfold polyL1At by_cases h0 : P.evalWire ⟨w.val, by omega⟩ = 0 · simp [h0] · rw [Polynomial.support_C h0]; simp exact le_trans hC (le_trans (wire_natAbs_le_wireHeight P a (by omega) x ⟨w.val, by omega⟩) hH_le_pow) · simp [polyL1At] · have hX : polyL1At (Polynomial.X : Polynomial (Polynomial ℤ)) x = 1 := by unfold polyL1At rw [Polynomial.support_X] simp rw [hX]; omega · set t' := w.val - 2 have ht'_lt_t : t' < t := by omega have hbt' : b ≤ t' := by omega have ht'_lt_τ : t' < τ := by omega set A := evalFormalT P a b hab hb t' hbt' (le_of_lt ht'_lt_τ) (P.gateIn1 t' ht'_lt_τ) set B := evalFormalT P a b hab hb t' hbt' (le_of_lt ht'_lt_τ) (P.gateIn2 t' ht'_lt_τ) have hA : polyL1At A x ≤ H ^ (2 ^ (t' - b)) := ih t' ht'_lt_t hbt' (le_of_lt ht'_lt_τ) (P.gateIn1 t' ht'_lt_τ) have hB : polyL1At B x ≤ H ^ (2 ^ (t' - b)) := ih t' ht'_lt_t hbt' (le_of_lt ht'_lt_τ) (P.gateIn2 t' ht'_lt_τ) have h2_le_Hpow : 2 ≤ H ^ (2 ^ (t' - b)) := le_trans hH2 (Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) H) have hsq_le : H ^ (2 ^ (t' - b)) * H ^ (2 ^ (t' - b)) ≤ H ^ (2 ^ (t - b)) := by rw [← pow_add, ← two_mul] apply Nat.pow_le_pow_right (by omega) have : (t' - b) + 1 ≤ t - b := by omega have := Nat.pow_le_pow_right (by omega : 1 ≤ 2) this rwa [pow_succ, Nat.mul_comm] at this change polyL1At ((P.gateOp t' ht'_lt_τ).applyPolyT A B) x ≤ H ^ (2 ^ (t - b)) cases P.gateOp t' ht'_lt_τ with | add => simp only [SLPOp.applyPolyT] have h1 := polyL1At_add_le A B x have h2 : H ^ (2 ^ (t' - b)) + H ^ (2 ^ (t' - b)) ≤ H ^ (2 ^ (t' - b)) * H ^ (2 ^ (t' - b)) := by nlinarith exact le_trans h1 (le_trans (Nat.add_le_add hA hB) (le_trans h2 hsq_le)) | sub => simp only [SLPOp.applyPolyT] have h1 := polyL1At_sub_le A B x have h2 : H ^ (2 ^ (t' - b)) + H ^ (2 ^ (t' - b)) ≤ H ^ (2 ^ (t' - b)) * H ^ (2 ^ (t' - b)) := by nlinarith exact le_trans h1 (le_trans (Nat.add_le_add hA hB) (le_trans h2 hsq_le)) | mul => simp only [SLPOp.applyPolyT] exact le_trans (polyL1At_mul_le A B x) (le_trans (Nat.mul_le_mul hA hB) hsq_le) /-- Property 4 (Next-Level Height Bound): for any untapped segment `seg` and any `c` with `seg.b ≤ c ≤ τ`, `wireHeight P c _ x ≤ (max (wireHeight P seg.a _ x) |U(x)|) ^ (2 ^ (c - seg.b))`. -/ lemma wireHeight_step_le {τ : ℕ} (P : SLP τ) (seg : UntappedSeg P) (x : ℤ) : ∀ (c : ℕ) (hbc : seg.b ≤ c) (hc : c ≤ τ), wireHeight P c hc x ≤ (max (wireHeight P seg.a (by have := seg.hab; have := seg.hb; omega) x) ((P.evalWire ⟨seg.b + 1, by have := seg.hb; omega⟩).eval x).natAbs) ^ (2 ^ (c - seg.b)) := by intro c hbc hc set M := max (wireHeight P seg.a (by have := seg.hab; have := seg.hb; omega) x) ((P.evalWire ⟨seg.b + 1, by have := seg.hb; omega⟩).eval x).natAbs have hM2 : 2 ≤ M := le_trans (two_le_wireHeight P seg.a (by have := seg.hab; have := seg.hb; omega) x) (le_max_left _ _) have hpow1 : 1 ≤ 2 ^ (c - seg.b) := Nat.one_le_pow _ _ (by omega) have hM_le_pow : M ≤ M ^ (2 ^ (c - seg.b)) := Nat.le_self_pow (by omega) M -- Prove by strong induction on w.val < c + 2 that |wire w(x)| ≤ M ^ (2 ^ (max w.val (seg.b + 1) - (seg.b + 1))) have hwire : ∀ (k : ℕ) (hk : k < c + 2), ((P.evalWire ⟨k, by omega⟩).eval x).natAbs ≤ M ^ (2 ^ (k - (seg.b + 1))) := by intro k induction k using Nat.strong_induction_on with | ind k ihk => intro hk by_cases hle_a : k ≤ seg.a + 1 · have hsub : k - (seg.b + 1) = 0 := by have := seg.hab; omega rw [hsub, pow_zero, pow_one] exact le_trans (wire_natAbs_le_wireHeight P seg.a (by have := seg.hab; have := seg.hb; omega) x ⟨k, by omega⟩) (le_max_left _ _) · by_cases hle_b : k ≤ seg.b · -- Strict intermediate wire of seg: equals w_in ^ (2 ^ (k - seg.a - 1))! -- Note |w_in(x)| ^ (2^(k - seg.a - 1)) ≤ max 1 |w_in(x)| ^ (2^(seg.b - seg.a)) = max 1 |U(x)| ≤ M! have hsub : k - (seg.b + 1) = 0 := by omega rw [hsub, pow_zero, pow_one] have hrun_k := P.topSq_run_eval (a := seg.a) (b := k - 1) (by omega) (by omega) (fun t ht h1 h2 => seg.hrun t ht h1 (by omega)) have hrun_b := P.topSq_run_eval seg.hab seg.hb seg.hrun set win := P.evalWire ⟨(P.gateIn1 seg.a (by omega)).val, by have := (P.gateIn1 seg.a (by omega)).isLt; omega⟩ have hk_eq : k - 1 + 1 = k := by omega have hval_k : ((P.evalWire ⟨k, by omega⟩).eval x).natAbs = (win.eval x).natAbs ^ (2 ^ (k - 1 - seg.a)) := by have := congrArg (fun p : Polynomial ℤ => (p.eval x).natAbs) hrun_k.1 simpa [hk_eq, Int.natAbs_pow, win] using this have hval_b : ((P.evalWire ⟨seg.b + 1, by omega⟩).eval x).natAbs = (win.eval x).natAbs ^ (2 ^ (seg.b - seg.a)) := by have := congrArg (fun p : Polynomial ℤ => (p.eval x).natAbs) hrun_b.1 simpa [Int.natAbs_pow, win] using this rw [hval_k] by_cases hw0 : (win.eval x).natAbs = 0 · rw [hw0, zero_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega)))]; omega · have h1 : (win.eval x).natAbs ^ (2 ^ (k - 1 - seg.a)) ≤ (win.eval x).natAbs ^ (2 ^ (seg.b - seg.a)) := Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) (by omega)) rw [← hval_b] at h1 exact le_trans h1 (le_max_right _ _) · by_cases heq_b : k = seg.b + 1 · subst heq_b simp only [Nat.sub_self, pow_zero, pow_one] exact le_max_right _ _ · -- k ≥ seg.b + 2: gate t' = k - 2 set t' := k - 2 have ht'_lt : t' < τ := by omega have hgate := P.evalWire_gate t' ht'_lt have hk_eq : t' + 2 = k := by omega have hin1_lt : (P.gateIn1 t' ht'_lt).val < k := by have := (P.gateIn1 t' ht'_lt).isLt; omega have hin2_lt : (P.gateIn2 t' ht'_lt).val < k := by have := (P.gateIn2 t' ht'_lt).isLt; omega have h1 := ihk (P.gateIn1 t' ht'_lt).val hin1_lt (by omega) have h2 := ihk (P.gateIn2 t' ht'_lt).val hin2_lt (by omega) have he1 : (P.gateIn1 t' ht'_lt).val - (seg.b + 1) ≤ k - (seg.b + 1) - 1 := by omega have he2 : (P.gateIn2 t' ht'_lt).val - (seg.b + 1) ≤ k - (seg.b + 1) - 1 := by omega have h1' : ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x).natAbs ≤ M ^ (2 ^ (k - (seg.b + 1) - 1)) := le_trans h1 (Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) he1)) have h2' : ((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x).natAbs ≤ M ^ (2 ^ (k - (seg.b + 1) - 1)) := le_trans h2 (Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) he2)) have hsq : M ^ (2 ^ (k - (seg.b + 1) - 1)) * M ^ (2 ^ (k - (seg.b + 1) - 1)) = M ^ (2 ^ (k - (seg.b + 1))) := by rw [← pow_add, ← two_mul] congr 1 conv_rhs => rw [← Nat.sub_add_cancel (by omega : 1 ≤ k - (seg.b + 1)), pow_succ, Nat.mul_comm] have h2_le : 2 ≤ M ^ (2 ^ (k - (seg.b + 1) - 1)) := le_trans hM2 (Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) M) have heval : (P.evalWire ⟨k, by omega⟩).eval x = (P.gateOp t' ht'_lt).applyInt ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x) ((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x) := by rw [SLPOp.applyInt_eq_eval] simpa [hk_eq] using congrArg (fun p : Polynomial ℤ => p.eval x) hgate rw [heval] cases P.gateOp t' ht'_lt with | add => simp only [SLPOp.applyInt] have := Int.natAbs_add_le ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x) ((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x) nlinarith | sub => simp only [SLPOp.applyInt] have := Int.natAbs_add_le ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x) (-((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x)) rw [Int.natAbs_neg] at this have hsub_eq : ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x) - ((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x) = ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x) + (-((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x)) := by ring rw [hsub_eq]; nlinarith | mul => simp only [SLPOp.applyInt, Int.natAbs_mul] rw [← hsq]; exact Nat.mul_le_mul h1' h2' unfold wireHeight apply max_le (le_trans hM2 hM_le_pow) apply Finset.sup_le intro i _ rw [P.evalWire_prefix c hc i] have hi_le : i.val - (seg.b + 1) ≤ c - seg.b := by have := i.isLt; omega exact le_trans (hwire i.val i.isLt) (Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) hi_le)) /-- Base height bound before segment `a`: `wireHeight P a _ x ≤ (max 2 |x|) ^ (2 ^ a)`. -/ lemma wireHeight_base_le {τ : ℕ} (P : SLP τ) (a : ℕ) (ha : a ≤ τ) (x : ℤ) : wireHeight P a ha x ≤ (max 2 x.natAbs) ^ (2 ^ a) := by set M := max 2 x.natAbs have hM2 : 2 ≤ M := le_max_left _ _ have hwire : ∀ (k : ℕ) (hk : k < a + 2), ((P.evalWire ⟨k, by omega⟩).eval x).natAbs ≤ M ^ (2 ^ (k - 1)) := by intro k induction k using Nat.strong_induction_on with | ind k ihk => intro hk match k with | 0 => have h0 : P.evalWire ⟨0, by omega⟩ = 1 := P.evalWire_zero rw [h0]; simp; omega | 1 => have h1 : P.evalWire ⟨1, by omega⟩ = Polynomial.X := by have hpr := P.evalWire_prefix 0 (Nat.zero_le τ) ⟨1, by omega⟩ have hnil : P.prefix 0 (Nat.zero_le τ) = .nil := by cases P.prefix 0 (Nat.zero_le τ); rfl rw [hnil] at hpr simpa [SLP.evalWire] using hpr.symm rw [h1]; simp [M] | t' + 2 => have ht'_lt : t' < τ := by omega have hgate := P.evalWire_gate t' ht'_lt have hin1_lt : (P.gateIn1 t' ht'_lt).val < t' + 2 := (P.gateIn1 t' ht'_lt).isLt have hin2_lt : (P.gateIn2 t' ht'_lt).val < t' + 2 := (P.gateIn2 t' ht'_lt).isLt have h1 := ihk (P.gateIn1 t' ht'_lt).val hin1_lt (by omega) have h2 := ihk (P.gateIn2 t' ht'_lt).val hin2_lt (by omega) have h1' : ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x).natAbs ≤ M ^ (2 ^ t') := le_trans h1 (Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) (by omega))) have h2' : ((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x).natAbs ≤ M ^ (2 ^ t') := le_trans h2 (Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) (by omega))) have hsq : M ^ (2 ^ t') * M ^ (2 ^ t') = M ^ (2 ^ (t' + 1)) := by rw [← pow_add, ← two_mul, pow_succ, Nat.mul_comm] have h2_le : 2 ≤ M ^ (2 ^ t') := le_trans hM2 (Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) M) have heval : (P.evalWire ⟨t' + 2, by omega⟩).eval x = (P.gateOp t' ht'_lt).applyInt ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x) ((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x) := by rw [SLPOp.applyInt_eq_eval] exact congrArg (fun p : Polynomial ℤ => p.eval x) hgate rw [heval] have hsub : t' + 2 - 1 = t' + 1 := by omega rw [hsub] cases P.gateOp t' ht'_lt with | add => simp only [SLPOp.applyInt] have := Int.natAbs_add_le ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x) ((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x) nlinarith | sub => simp only [SLPOp.applyInt] have := Int.natAbs_add_le ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x) (-((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x)) rw [Int.natAbs_neg] at this have hsub_eq : ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x) - ((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x) = ((P.evalWire ⟨(P.gateIn1 t' ht'_lt).val, by omega⟩).eval x) + (-((P.evalWire ⟨(P.gateIn2 t' ht'_lt).val, by omega⟩).eval x)) := by ring rw [hsub_eq]; nlinarith | mul => simp only [SLPOp.applyInt, Int.natAbs_mul] rw [← hsq]; exact Nat.mul_le_mul h1' h2' unfold wireHeight apply max_le (le_trans hM2 (Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) M)) apply Finset.sup_le intro i _ rw [P.evalWire_prefix a ha i] have hi_le : i.val - 1 ≤ a := by have := i.isLt; omega exact le_trans (hwire i.val i.isLt) (Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) hi_le)) /-- Coefficient `ℓ₁`-norm of `f ∈ ℤ[X]`: `∑ j ∈ f.support, (f.coeff j).natAbs`. -/ noncomputable def polyIntL1 (f : Polynomial ℤ) : ℕ := ∑ j ∈ f.support, (f.coeff j).natAbs private lemma polyIntL1_eq_sum_range (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 _ hj have : f.coeff j = 0 := Polynomial.notMem_support_iff.mp hj simp [this] private lemma polyIntL1_add_le (f g : Polynomial ℤ) : polyIntL1 (f + g) ≤ polyIntL1 f + polyIntL1 g := by set N := max (f.natDegree + 1) (g.natDegree + 1) have hf : ∀ j ∈ f.support, j < N := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega have hg : ∀ j ∈ g.support, j < N := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega have hfg : ∀ j ∈ (f + g).support, j < N := fun j hj => by have h1 := Polynomial.le_natDegree_of_mem_supp j hj have h2 := Polynomial.natDegree_add_le f g; omega rw [polyIntL1_eq_sum_range (f + g) hfg, polyIntL1_eq_sum_range f hf, polyIntL1_eq_sum_range g hg, ← Finset.sum_add_distrib] apply Finset.sum_le_sum; intro j _ simp only [Polynomial.coeff_add] exact Int.natAbs_add_le _ _ private lemma polyIntL1_sub_le (f g : Polynomial ℤ) : polyIntL1 (f - g) ≤ polyIntL1 f + polyIntL1 g := by set N := max (f.natDegree + 1) (g.natDegree + 1) have hf : ∀ j ∈ f.support, j < N := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega have hg : ∀ j ∈ g.support, j < N := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega have hfg : ∀ j ∈ (f - g).support, j < N := fun j hj => by have h1 := Polynomial.le_natDegree_of_mem_supp j hj have h2 := Polynomial.natDegree_sub_le f g; omega rw [polyIntL1_eq_sum_range (f - g) hfg, polyIntL1_eq_sum_range f hf, polyIntL1_eq_sum_range g hg, ← Finset.sum_add_distrib] apply Finset.sum_le_sum; intro j _ simp only [Polynomial.coeff_sub] simpa [sub_eq_add_neg, Int.natAbs_neg] using Int.natAbs_add_le (f.coeff j) (-(g.coeff j)) private lemma polyIntL1_mul_le (f g : Polynomial ℤ) : polyIntL1 (f * g) ≤ polyIntL1 f * polyIntL1 g := by set Nf := f.natDegree + 1 set Ng := g.natDegree + 1 have hf : ∀ j ∈ f.support, j < Nf := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega have hg : ∀ j ∈ g.support, j < Ng := fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega have hfg : ∀ j ∈ (f * g).support, j < Nf + Ng := fun j hj => by have h1 := Polynomial.le_natDegree_of_mem_supp j hj have h2 := Polynomial.natDegree_mul_le (p := f) (q := g); omega rw [polyIntL1_eq_sum_range (f * g) hfg, polyIntL1_eq_sum_range f hf, polyIntL1_eq_sum_range g hg, Finset.sum_mul_sum] have htri : ∀ (s : Finset (ℕ × ℕ)) (h : ℕ × ℕ → ℤ), (∑ p ∈ s, h p).natAbs ≤ ∑ p ∈ s, (h p).natAbs := by intro s h 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 _) calc ∑ j ∈ Finset.range (Nf + Ng), ((f * g).coeff j).natAbs ≤ ∑ j ∈ Finset.range (Nf + Ng), ∑ p ∈ Finset.antidiagonal j, (f.coeff p.1).natAbs * (g.coeff p.2).natAbs := by apply Finset.sum_le_sum; intro j _ rw [Polynomial.coeff_mul] calc (∑ p ∈ Finset.antidiagonal j, f.coeff p.1 * g.coeff p.2).natAbs ≤ ∑ p ∈ Finset.antidiagonal j, (f.coeff p.1 * g.coeff p.2).natAbs := htri _ _ _ = ∑ p ∈ Finset.antidiagonal j, (f.coeff p.1).natAbs * (g.coeff p.2).natAbs := by apply Finset.sum_congr rfl; intro p _; exact Int.natAbs_mul _ _ _ ≤ ∑ p ∈ Finset.range Nf ×ˢ Finset.range Ng, (f.coeff p.1).natAbs * (g.coeff p.2).natAbs := by rw [← Finset.sum_biUnion] · apply Finset.sum_le_sum_of_ne_zero intro p _ hne have hp1 : p.1 < Nf := by by_contra h have h0 : f.coeff p.1 = 0 := Polynomial.coeff_eq_zero_of_natDegree_lt (by omega) simp [h0] at hne have hp2 : p.2 < Ng := by by_contra h have h0 : g.coeff p.2 = 0 := Polynomial.coeff_eq_zero_of_natDegree_lt (by omega) simp [h0] at hne exact Finset.mem_product.mpr ⟨Finset.mem_range.mpr hp1, Finset.mem_range.mpr hp2⟩ · intro i _ j _ hij exact Finset.disjoint_left.mpr fun p hpi hpj => hij ((Finset.mem_antidiagonal.mp hpi).symm.trans (Finset.mem_antidiagonal.mp hpj)) _ = ∑ i ∈ Finset.range Nf, ∑ j ∈ Finset.range Ng, (f.coeff i).natAbs * (g.coeff j).natAbs := Finset.sum_product _ _ _ /-- Every wire of `P : SLP τ` has coefficient `ℓ₁`-norm at most `2 ^ (2 ^ τ)`. -/ lemma SLP.wire_polyIntL1_le : ∀ {τ : ℕ} (P : SLP τ) (w : Fin (τ + 2)), polyIntL1 (P.evalWire w) ≤ 2 ^ (2 ^ τ) | 0, .nil, w => by simp only [SLP.evalWire] split_ifs · have : polyIntL1 (1 : Polynomial ℤ) = 1 := by unfold polyIntL1 rw [← Polynomial.C_1, Polynomial.support_C (by norm_num : (1 : ℤ) ≠ 0)] simp rw [this]; omega · have : polyIntL1 (Polynomial.X : Polynomial ℤ) = 1 := by unfold polyIntL1; rw [Polynomial.support_X]; simp rw [this]; omega | τ + 1, .snoc P op i j, w => by have ih1 := P.wire_polyIntL1_le i have ih2 := P.wire_polyIntL1_le j have hsq : 2 ^ (2 ^ τ) * 2 ^ (2 ^ τ) = 2 ^ (2 ^ (τ + 1)) := by rw [← pow_add, ← two_mul, pow_succ, Nat.mul_comm] have h2_le : 2 ≤ 2 ^ (2 ^ τ) := Nat.le_self_pow (Nat.ne_of_gt (Nat.one_le_pow _ _ (by omega))) 2 simp only [SLP.evalWire] split_ifs with hw · exact le_trans (P.wire_polyIntL1_le ⟨w.val, hw⟩) (Nat.pow_le_pow_right (by omega) (Nat.pow_le_pow_right (by omega) (by omega))) · cases op with | add => simp only [SLPOp.applyPoly] have := polyIntL1_add_le (P.evalWire i) (P.evalWire j) nlinarith | sub => simp only [SLPOp.applyPoly] have := polyIntL1_sub_le (P.evalWire i) (P.evalWire j) nlinarith | mul => simp only [SLPOp.applyPoly] rw [← hsq]; exact le_trans (polyIntL1_mul_le _ _) (Nat.mul_le_mul ih1 ih2) /-! ## 4. Cauchy Bounds, Backward Propagation & Multi-Segment Gap-Dominance Theorem -/ private lemma eval_polyT_eq (F : Polynomial (Polynomial ℤ)) (u : Polynomial ℤ) (x : ℤ) : (F.eval u).eval x = ∑ j ∈ Finset.range (F.natDegree + 1), (F.coeff j).eval x * (u.eval x) ^ j := by rw [Polynomial.eval_eq_sum_range (p := F), ← Polynomial.coe_evalRingHom, map_sum] apply Finset.sum_congr rfl; intro j _ simp /-- Triangle bound on lower terms `j < D` of `F(x, U)`: `|∑_{j < D} c_j(x) * U^j| ≤ |U|^(D - 1) * polyL1At F x` whenever `1 ≤ D` and `1 ≤ |U|`. -/ private lemma lower_terms_bound (F : Polynomial (Polynomial ℤ)) (x U : ℤ) (hD : 1 ≤ F.natDegree) (hU : 1 ≤ U.natAbs) : (∑ j ∈ Finset.range F.natDegree, (F.coeff j).eval x * U ^ j).natAbs ≤ U.natAbs ^ (F.natDegree - 1) * polyL1At F x := 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 u s' hu ihs => rw [Finset.sum_insert hu, Finset.sum_insert hu] exact le_trans (Int.natAbs_add_le _ _) (Nat.add_le_add_left ihs _) have hsum_le : ∑ j ∈ Finset.range F.natDegree, ((F.coeff j).eval x).natAbs ≤ polyL1At F x := by rw [polyL1At_eq_sum_range F x (N := F.natDegree + 1) (fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega)] 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 hj_le : j ≤ F.natDegree - 1 := by have := Finset.mem_range.mp hj; omega exact Nat.mul_le_mul_left _ (Nat.pow_le_pow_right hU hj_le) _ = (∑ j ∈ Finset.range F.natDegree, ((F.coeff j).eval x).natAbs) * U.natAbs ^ (F.natDegree - 1) := (Finset.sum_mul _ _ _).symm _ ≤ polyL1At F x * U.natAbs ^ (F.natDegree - 1) := Nat.mul_le_mul_right _ hsum_le _ = U.natAbs ^ (F.natDegree - 1) * polyL1At F x := Nat.mul_comm _ _ /-- **Cauchy Root Bound for `F(x, U) = 0`:** If `F.natDegree ≥ 1`, `(F.leadingCoeff).eval x ≠ 0`, and `(F.eval u).eval x = 0`, then `|u(x)| ≤ polyL1At F x`. -/ private lemma cauchy_root_bound_polyT (F : Polynomial (Polynomial ℤ)) (u : Polynomial ℤ) (x : ℤ) (hD : 1 ≤ F.natDegree) (hlead : (F.leadingCoeff).eval x ≠ 0) (hzero : (F.eval u).eval x = 0) : (u.eval x).natAbs ≤ polyL1At F x := by set U := u.eval x by_cases hU0 : U.natAbs = 0 · omega have hU1 : 1 ≤ U.natAbs := by omega have hsum := eval_polyT_eq F u x rw [hzero, Finset.sum_range_succ] at hsum set low := ∑ j ∈ Finset.range F.natDegree, (F.coeff j).eval x * U ^ j have heq : (F.coeff F.natDegree).eval x * U ^ F.natDegree = -low := by linarith have hlead_pos : 1 ≤ ((F.coeff F.natDegree).eval x).natAbs := Int.natAbs_pos.mpr hlead have hpow_split : U.natAbs ^ F.natDegree = U.natAbs ^ (F.natDegree - 1) * U.natAbs := by conv_lhs => rw [← Nat.sub_add_cancel hD, pow_succ] have hlow := lower_terms_bound F x U hD hU1 have h1 : U.natAbs ^ F.natDegree ≤ low.natAbs := by calc U.natAbs ^ F.natDegree = 1 * U.natAbs ^ F.natDegree := (Nat.one_mul _).symm _ ≤ ((F.coeff F.natDegree).eval x).natAbs * U.natAbs ^ F.natDegree := Nat.mul_le_mul_right _ hlead_pos _ = ((F.coeff F.natDegree).eval x * U ^ F.natDegree).natAbs := by rw [Int.natAbs_mul, Int.natAbs_pow] _ = (-low).natAbs := by rw [heq] _ = low.natAbs := Int.natAbs_neg _ rw [hpow_split] at h1 have hpos : 0 < U.natAbs ^ (F.natDegree - 1) := Nat.pow_pos hU1 exact Nat.le_of_mul_le_mul_left (le_trans h1 hlow) hpos /-- **Cauchy Dominance Lower Bound:** If `F.natDegree ≥ 1`, `(F.leadingCoeff).eval x ≠ 0`, and `2 * polyL1At F x < |u(x)|`, then `|u(x)| ≤ 2 * |(F.eval u).eval x|`. -/ private lemma cauchy_dominance_polyT (F : Polynomial (Polynomial ℤ)) (u : Polynomial ℤ) (x : ℤ) (hD : 1 ≤ F.natDegree) (hlead : (F.leadingCoeff).eval x ≠ 0) (hbig : 2 * polyL1At F x < (u.eval x).natAbs) : (u.eval x).natAbs ≤ 2 * ((F.eval u).eval x).natAbs := by set U := u.eval x have hU1 : 1 ≤ U.natAbs := by omega have hsum := eval_polyT_eq F u x rw [Finset.sum_range_succ] at hsum set low := ∑ j ∈ Finset.range F.natDegree, (F.coeff j).eval x * U ^ j set top := (F.coeff F.natDegree).eval x * U ^ F.natDegree have hlead_pos : 1 ≤ ((F.coeff F.natDegree).eval x).natAbs := Int.natAbs_pos.mpr hlead have htop_ge : U.natAbs ^ F.natDegree ≤ top.natAbs := by calc U.natAbs ^ F.natDegree = 1 * U.natAbs ^ F.natDegree := (Nat.one_mul _).symm _ ≤ ((F.coeff F.natDegree).eval x).natAbs * U.natAbs ^ F.natDegree := Nat.mul_le_mul_right _ hlead_pos _ = top.natAbs := by simp [top, Int.natAbs_mul, Int.natAbs_pow] have htri : top.natAbs ≤ ((F.eval u).eval x).natAbs + low.natAbs := by have hrew : top = (F.eval u).eval x + (-low) := by linarith calc top.natAbs = ((F.eval u).eval x + (-low)).natAbs := by rw [← hrew] _ ≤ ((F.eval u).eval x).natAbs + (-low).natAbs := Int.natAbs_add_le _ _ _ = ((F.eval u).eval x).natAbs + low.natAbs := by rw [Int.natAbs_neg] have hlow := lower_terms_bound F x U hD hU1 have hpow_split : U.natAbs ^ F.natDegree = U.natAbs ^ (F.natDegree - 1) * U.natAbs := by conv_lhs => rw [← Nat.sub_add_cancel hD, pow_succ] have h1 : U.natAbs ^ (F.natDegree - 1) * (2 * polyL1At F x) ≤ U.natAbs ^ (F.natDegree - 1) * U.natAbs := Nat.mul_le_mul_left _ (le_of_lt hbig) have h2 : 2 * low.natAbs ≤ U.natAbs ^ F.natDegree := by rw [hpow_split]; linarith have h3 : U.natAbs ^ F.natDegree ≤ 2 * ((F.eval u).eval x).natAbs := by omega have h4 : U.natAbs ≤ U.natAbs ^ F.natDegree := Nat.le_self_pow (by omega) U.natAbs omega /-- Cauchy dominance for a single univariate polynomial `w ∈ ℤ[X]` (`deg w ≥ 1`): if `2 * polyIntL1 w ≤ |x|`, then `|x| ≤ 2 * |w(x)|`. -/ private lemma cauchy_dominance_univar (w : Polynomial ℤ) (x : ℤ) (hD : 1 ≤ w.natDegree) (hbig : 2 * polyIntL1 w ≤ x.natAbs) : x.natAbs ≤ 2 * (w.eval x).natAbs := by have hx1 : 1 ≤ x.natAbs := by have hne : w ≠ 0 := fun h0 => by simp [h0] at hD have hlead : 1 ≤ (w.coeff w.natDegree).natAbs := Int.natAbs_pos.mpr (Polynomial.leadingCoeff_ne_zero.mpr hne) have hl1 : 1 ≤ polyIntL1 w := by rw [polyIntL1_eq_sum_range w (N := w.natDegree + 1) (fun j hj => by have := Polynomial.le_natDegree_of_mem_supp j hj; omega)] rw [Finset.sum_range_succ]; omega omega have hsum : w.eval x = (∑ j ∈ Finset.range w.natDegree, w.coeff j * x ^ j) + w.coeff w.natDegree * x ^ w.natDegree := by rw [Polynomial.eval_eq_sum_range, Finset.sum_range_succ] 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 [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 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-`ℕ` arithmetic helper: if `5 ≤ u`, `u ^ 2 ^ (g - 1) ≤ u ^ 2 ^ e` is impossible when `e + 2 ≤ g`, and `u ≤ 2 * v` implies `u ^ (2 ^ (g - 1)) ≤ v ^ (2 ^ g)` (since `4 ≤ u` implies `u ≤ (u / 2)^2`, actually `u * 4 ≤ u ^ 2`, so `(2 * v)^2 ≥ u^2 ≥ 4 * u` => `v^2 ≥ u`!). -/ private lemma nat_half_sq_ge {u v : ℕ} (hu : 4 ≤ u) (huv : u ≤ 2 * v) : u ≤ v ^ 2 := by nlinarith /-- **Multi-Segment Gap-Dominance Theorem:** If `P : SLP τ` (`P.evalPoly ≠ 0`) admits a list `L` satisfying `ValidGapSegs Δ L`, then `intRootCount P.evalPoly ≤ (L.length + 3) * 2 ^ (τ - Δ)`. -/ theorem slp_multi_segment_gap_dominance {τ Δ : ℕ} (P : SLP τ) (hP : P.evalPoly ≠ 0) (L : List (UntappedSeg P)) (hvalid : ValidGapSegs Δ L) : intRootCount P.evalPoly ≤ (L.length + 3) * 2 ^ (τ - Δ) := by classical rcases hvalid with ⟨hL_ne, hord, hΔ, hhead_gap, hmid_gap⟩ have hq_pos : 1 ≤ L.length := by cases L <;> simp at hL_ne ⊢ -- Helper: for r : Fin L.length, define seg r, nextA r, target wire wTarget r, and F r : Polynomial (Polynomial ℤ). let seg : Fin L.length → UntappedSeg P := fun r => L[r.val]'r.isLt have hseg_ord : ∀ (r : Fin L.length) (hr : r.val + 1 < L.length), (seg r).b ≤ (seg ⟨r.val + 1, hr⟩).a := by have hgen : ∀ (M : List (UntappedSeg P)), SegsOrdered M → ∀ (i : ℕ) (hi : i + 1 < M.length), (M[i]'(by omega)).b ≤ (M[i + 1]'hi).a := by intro M induction M with | nil => intro _ i hi; simp at hi | cons s0 rest ih => intro ho i hi cases i with | zero => cases rest with | nil => simp at hi | cons s1 rest' => exact ho.1 | succ i' => cases rest with | nil => simp at hi | cons s1 rest' => exact ih ho.2 i' (by simpa using hi) intro r hr exact hgen L hord r.val hr let nextC : Fin L.length → ℕ := fun r => if h : r.val + 1 < L.length then (seg ⟨r.val + 1, h⟩).a else τ have hnextC_ge : ∀ r : Fin L.length, (seg r).b ≤ nextC r := by intro r; dsimp only [nextC] split_ifs with h · exact hseg_ord r h · exact (seg r).hb have hnextC_le : ∀ r : Fin L.length, nextC r ≤ τ := by intro r; dsimp only [nextC] split_ifs with h · exact le_of_lt (lt_of_lt_of_le (seg ⟨r.val + 1, h⟩).hab (seg ⟨r.val + 1, h⟩).hb) · exact le_rfl let wTarget : (r : Fin L.length) → Fin (nextC r + 2) := fun r => if h : r.val + 1 < L.length then let sNext := seg ⟨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 dsimp only [nextC]; rw [dite_eq_left_of_eq_true (eq_true h)]; exact hlt⟩ else ⟨τ + 1, by dsimp only [nextC]; rw [dite_eq_right_of_eq_false (eq_false h)]; omega⟩ let F : Fin L.length → Polynomial (Polynomial ℤ) := fun r => evalFormalT P (seg r).a (seg r).b (seg r).hab (seg r).hb (nextC r) (hnextC_ge r) (hnextC_le r) (wTarget r) -- Check top segment (last index qlast) let qlast : Fin L.length := ⟨L.length - 1, by omega⟩ have hqlast_not : ¬ (qlast.val + 1 < L.length) := by dsimp [qlast]; omega have hqlast_C : nextC qlast = τ := by dsimp only [nextC]; rw [dite_eq_right_of_eq_false (eq_false hqlast_not)] have hqlast_spec : (F qlast).eval (P.evalWire ⟨(seg qlast).b + 1, by have := (seg qlast).hb; omega⟩) = P.evalPoly := 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]; 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) (hnextC_ge qlast) (hnextC_le qlast) (wTarget qlast) hnot have hw : (wTarget qlast).val = τ + 1 := by dsimp only [wTarget]; rw [dite_eq_right_of_eq_false (eq_false hqlast_not)] simpa [F, SLP.evalPoly, hw] using hspec -- Case 0: (F qlast).natDegree = 0 -> P.evalPoly = (F qlast).coeff 0 has degree ≤ 2 ^ (τ - Δ)! by_cases htop0 : (F qlast).natDegree = 0 · have hF_eq : F qlast = Polynomial.C ((F qlast).coeff 0) := Polynomial.eq_C_of_natDegree_eq_zero htop0 have hP_eq : P.evalPoly = (F qlast).coeff 0 := by calc P.evalPoly = (F qlast).eval (P.evalWire ⟨(seg qlast).b + 1, by have := (seg qlast).hb; omega⟩) := hqlast_spec.symm _ = (Polynomial.C ((F qlast).coeff 0)).eval _ := by conv_lhs => rw [hF_eq] _ = (F qlast).coeff 0 := Polynomial.eval_C have hdeg_le : P.evalPoly.natDegree ≤ 2 ^ (τ - Δ) := by rw [hP_eq] have h1 := evalFormalT_coeff_natDegree_le P (seg qlast).a (seg qlast).b (seg qlast).hab (seg qlast).hb (nextC qlast) (hnextC_ge qlast) (hnextC_le qlast) (wTarget qlast) 0 change ((F qlast).coeff 0).natDegree ≤ 2 ^ (nextC qlast - (seg qlast).b) * (P.prefix (seg qlast).a _).maxDeg at h1 rw [hqlast_C] at h1 have h2 := (P.prefix (seg qlast).a (by have := (seg qlast).hab; have := (seg qlast).hb; omega)).maxDeg_le_two_pow have h3 : 2 ^ (τ - (seg qlast).b) * 2 ^ (seg qlast).a = 2 ^ ((τ - (seg qlast).b) + (seg qlast).a) := (pow_add 2 _ _).symm have h4 : Δ ≤ (seg qlast).b - (seg qlast).a := hΔ (seg qlast) (List.getElem_mem _) have h5 := (seg qlast).hab; have h6 := (seg qlast).hb have h7 : (τ - (seg qlast).b) + (seg qlast).a ≤ τ - Δ := by omega calc ((F qlast).coeff 0).natDegree ≤ 2 ^ (τ - (seg qlast).b) * (P.prefix (seg qlast).a _).maxDeg := h1 _ ≤ 2 ^ (τ - (seg qlast).b) * 2 ^ (seg qlast).a := Nat.mul_le_mul_left _ h2 _ = 2 ^ ((τ - (seg qlast).b) + (seg qlast).a) := h3 _ ≤ 2 ^ (τ - Δ) := Nat.pow_le_pow_right (by omega) h7 have hroots : intRootCount P.evalPoly ≤ P.evalPoly.natDegree := le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _) calc intRootCount P.evalPoly ≤ 2 ^ (τ - Δ) := le_trans hroots hdeg_le _ = 1 * 2 ^ (τ - Δ) := (Nat.one_mul _).symm _ ≤ (L.length + 3) * 2 ^ (τ - Δ) := Nat.mul_le_mul_right _ (by omega) -- Main Case: (F qlast).natDegree ≥ 1. -- Prove that EVERY r : Fin L.length has (F r).natDegree ≥ 1! have hF_deg_pos : ∀ r : Fin L.length, 1 ≤ (F r).natDegree := 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]; 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 hspec := evalFormalT_spec P (seg r) (nextC r) (hnextC_ge r) (hnextC_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; have := hnextC_le r; omega⟩ at hspec rw [hF_eq, Polynomial.eval_C] at hspec -- On the one hand, deg(wTarget r) = D₁(a_{r+1}) ≥ deg(U_r) = 2^(g_r) * D₁(a_r) have htop_next := (seg r1).hrun (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega) le_rfl (seg r1).hab have hdeg_wt : (P.evalWire ⟨(wTarget r).val, by have := (wTarget r).isLt; have := hnextC_le r; omega⟩).natDegree = (P.prefix (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).maxDeg := 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]; rw [dite_eq_left_of_eq_true (eq_true hlast)] have hpr := P.evalWire_prefix (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega) (P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)) have h1 : P.evalWire ⟨(wTarget r).val, by have := (wTarget r).isLt; have := hnextC_le r; omega⟩ = (P.prefix (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).evalWire (P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)) := 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⟩ -- On the other hand, deg((F r).coeff 0) ≤ 2 ^ (nextC r - b_r) * D₁(a_r) have hcoeff_le := evalFormalT_coeff_natDegree_le P (seg r).a (seg r).b (seg r).hab (seg r).hb (nextC r) (hnextC_ge r) (hnextC_le r) (wTarget r) 0 -- And g_r > nextC r - b_r by the gap condition! have hgap_gt : nextC r - (seg r).b < (seg r).b - (seg r).a := by cases hr0 : r.val with | zero => cases hL_eq : L with | nil => exact absurd hL_eq hL_ne | cons s0 rest => have hh := hhead_gap s0 rest hL_eq have hs0 : seg r = s0 := by simp [seg, hr0, hL_eq] cases rest with | nil => simp [hL_eq, hr0] at hlast | cons s1 rest' => have hs1 : seg ⟨r.val + 1, hlast⟩ = s1 := by simp [seg, hr0, hL_eq] have hnc : nextC r = s1.a := by dsimp only [nextC]; rw [dite_eq_left_of_eq_true (eq_true hlast), hs1] rw [hnc, hs0]; dsimp at hh; omega | succ m => have hm := hmid_gap m (by omega) have hsc : seg r = L[m + 1]'(by omega) := by simp [seg, hr0] have hnc : nextC r = (L[m + 2]'(by omega)).a := by dsimp only [nextC]; rw [dite_eq_left_of_eq_true (eq_true hlast)] simp [seg, hr0] have hlt2 : m + 2 < L.length := by omega simp only [hlt2, ↓reduceDIte] at hm rw [hnc, hsc]; 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; omega -- Leading coefficient of F r is nonzero and has degree ≤ 2 ^ (τ - Δ) for EVERY r : Fin L.length! have hlead_ne : ∀ r : Fin L.length, (F r).leadingCoeff ≠ 0 := fun r => Polynomial.leadingCoeff_ne_zero.mpr (fun h0 => by have := hF_deg_pos r; simp [h0] at this) have hlead_deg : ∀ r : Fin L.length, (F r).leadingCoeff.natDegree ≤ 2 ^ (τ - Δ) := by intro r have h1 := evalFormalT_coeff_natDegree_le P (seg r).a (seg r).b (seg r).hab (seg r).hb (nextC r) (hnextC_ge r) (hnextC_le r) (wTarget r) (F r).natDegree have h2 := (P.prefix (seg r).a (by have := (seg r).hab; have := (seg r).hb; omega)).maxDeg_le_two_pow have h4 : Δ ≤ (seg r).b - (seg r).a := hΔ (seg r) (List.getElem_mem _) have h5 := (seg r).hab; have h6 := hnextC_ge r; have h7 := hnextC_le r have hexp : (nextC r - (seg r).b) + (seg r).a ≤ τ - Δ := by omega calc (F r).leadingCoeff.natDegree ≤ 2 ^ (nextC r - (seg r).b) * (P.prefix (seg r).a _).maxDeg := h1 _ ≤ 2 ^ (nextC r - (seg r).b) * 2 ^ (seg r).a := Nat.mul_le_mul_left _ h2 _ = 2 ^ ((nextC r - (seg r).b) + (seg r).a) := (pow_add 2 _ _).symm _ ≤ 2 ^ (τ - Δ) := Nat.pow_le_pow_right (by omega) hexp -- First segment's input polynomial w0 = P.evalWire (gateIn1 a_0) let s0 : UntappedSeg P := seg ⟨0, hq_pos⟩ let w0 : Polynomial ℤ := P.evalWire ⟨(P.gateIn1 s0.a (by have := s0.hab; have := s0.hb; omega)).val, by have := (P.gateIn1 s0.a (by have := s0.hab; have := s0.hb; omega)).isLt; have := s0.hab; have := s0.hb; omega⟩ have hw0_deg_pos : 1 ≤ w0.natDegree := 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 have hw0_deg_le : w0.natDegree ≤ 2 ^ (τ - Δ) := 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 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_le_two_pow have h4 : Δ ≤ s0.b - s0.a := hΔ s0 (List.getElem_mem _) have h5 := s0.hab; have h6 := s0.hb exact le_trans (le_trans h1 h2) (Nat.pow_le_pow_right (by omega) (by omega)) -- Build the finite cover of all integer roots of P.evalPoly: -- (⋃_{r : Fin L.length} intRootSet (F r).leadingCoeff) ∪ intRootSet (w0 - 1) ∪ intRootSet w0 ∪ intRootSet (w0 + 1) have hsub : intRootSet P.evalPoly ⊆ (Finset.univ.biUnion (fun r : Fin L.length => intRootSet (F r).leadingCoeff)) ∪ intRootSet (w0 - 1) ∪ intRootSet w0 ∪ intRootSet (w0 + 1) := by 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 -- Backward propagation: for EVERY r : Fin L.length (from L.length - 1 down to 0), -- |U_r(x)| ≤ (wireHeight P (seg r).a _ x) ^ (2 ^ (nextC r - (seg r).b + 1))! let P_prop : Fin L.length → Prop := fun r => ((P.evalWire ⟨(seg r).b + 1, by have := (seg r).hb; omega⟩).eval x).natAbs ≤ (wireHeight P (seg r).a (by have := (seg r).hab; have := (seg r).hb; omega) x) ^ (2 ^ (nextC r - (seg r).b + 1)) have hprop : ∀ (d : ℕ) (hd : d < L.length), P_prop ⟨L.length - 1 - d, by omega⟩ := 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 (F qlast) (P.evalWire ⟨(seg qlast).b + 1, by have := (seg qlast).hb; omega⟩) x (hF_deg_pos qlast) (hkill qlast) (by rw [hqlast_spec, hPx]) have hl1 := evalFormalT_l1_le P (seg qlast).a (seg qlast).b (seg qlast).hab (seg qlast).hb x (nextC qlast) (hnextC_ge qlast) (hnextC_le qlast) (wTarget qlast) have hH2 := two_le_wireHeight P (seg qlast).a (by have := (seg qlast).hab; have := (seg qlast).hb; omega) 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]; rw [dite_eq_left_of_eq_true (eq_true hr_lt), hfin_eq] set U := ((P.evalWire ⟨(seg r).b + 1, by have := (seg r).hb; omega⟩).eval x).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 -- Either U ≤ 2 * Hr ^ (2 ^ Rr) (in which case U ≤ Hr ^ (2 ^ (Rr + 1)) immediately!), -- or 2 * Hr ^ (2 ^ Rr) < U (derive contradiction from ihd!). by_cases hsmall : U ≤ 2 * Hr ^ (2 ^ Rr) · calc U ≤ 2 * Hr ^ (2 ^ Rr) := hsmall _ ≤ Hr ^ (2 ^ Rr) * Hr ^ (2 ^ Rr) := 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) (hnextC_ge r) (hnextC_le 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 (F r) (P.evalWire ⟨(seg r).b + 1, by have := (seg r).hb; omega⟩) x (hF_deg_pos r) (hkill r) hbig_l1 -- Identify (F r).eval U_r as w_{r+1} (the input to seg r1)! have hw_val : (wTarget r).val = (P.gateIn1 (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega)).val := by dsimp only [wTarget]; rw [dite_eq_left_of_eq_true (eq_true hr_lt)] have hseg_eq : seg ⟨r.val + 1, hr_lt⟩ = seg r1 := congrArg seg hfin_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 hnextC_ge r exact ((seg r).huntapped (seg r1).a (by have := (seg r1).hab; have := (seg r1).hb; omega) hbr).1 have hspec := evalFormalT_spec P (seg r) (nextC r) (hnextC_ge r) (hnextC_le r) (wTarget r) hnot_inter rw [hspec] at hdom set w1_val := ((P.evalWire ⟨(wTarget r).val, by have := (wTarget r).isLt; have := hnextC_le r; omega⟩).eval x).natAbs -- U ≤ 2 * w1_val and 4 ≤ U => U ≤ w1_val ^ 2! have hU_le_sq : U ≤ w1_val ^ 2 := nat_half_sq_ge hU4 hdom -- Now relate w1_val ^ (2 ^ g_{r+1}) = |U_{r+1}(x)|! set g1 := (seg r1).b - (seg r1).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 := by exact 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 -- By ihd, w1_val ^ (2 ^ g1) ≤ (wireHeight P (seg r1).a _ x) ^ (2 ^ (R_{r+1} + 1)) have hih : P_prop r1 := ihd (by omega) dsimp only [P_prop] at hih rw [hU1_eq] at hih -- By Property 4 (wireHeight_step_le), wireHeight P (seg r1).a _ x ≤ (max Hr U) ^ (2 ^ Rr) = U ^ (2 ^ Rr)! have hH1_le := wireHeight_step_le P (seg r) x (nextC r) (hnextC_ge r) (hnextC_le r) have hmax_U : max Hr U = U := max_eq_right (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) (hnextC_le r) x ≤ U ^ (2 ^ Rr) at hH1_le simpa only [hnext_eq] using hH1_le -- Combine: U ^ (2 ^ (g1 - 1)) ≤ (w1_val ^ 2) ^ (2 ^ (g1 - 1)) = w1_val ^ (2 ^ g1) ≤ U ^ (2 ^ (Rr + R_{r+1} + 1))! have hg1_ge : Rr + (nextC r1 - (seg r1).b) + 3 ≤ g1 := by have hmid := hmid_gap r.val (by omega) have hr_seg : L[r.val]'(by omega) = seg r := rfl have hr1_seg : L[r.val + 1]'(by omega) = seg r1 := by simp [seg, hr1_val] have hRr_eq : Rr = (seg r1).a - (seg r).b := by rw [← hnext_eq] have hnc1 : nextC r1 = if h2 : r.val + 2 < L.length then (L[r.val + 2]'h2).a else τ := by dsimp only [nextC] split_ifs with h1 h2 · have hidx_eq : r1.val + 1 = r.val + 2 := by omega simp only [seg] have hget : ∀ {i j : ℕ} (hi : i < L.length) (hj : j < L.length), i = j → L[i]'hi = L[j]'hj := by rintro i _ hi hj rfl; rfl exact congrArg UntappedSeg.a (hget _ _ hidx_eq) · omega · omega · rfl rw [hRr_eq, hnc1] simpa [hr_seg, hr1_seg, g1] using 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 -- Now evaluate hprop at d = L.length - 1 (which gives r = 0, i.e. s0)! have hprop0 := hprop (L.length - 1) (by omega) have hr0_eq : (⟨L.length - 1 - (L.length - 1), by omega⟩ : Fin L.length) = ⟨0, hq_pos⟩ := Fin.ext (by simp) rw [hr0_eq] at hprop0 dsimp only [P_prop] at hprop0 -- Extract the head gap condition: 2 * R0 + R1 + 5 ≤ g0 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 cases hL_eq : L with | nil => exact absurd hL_eq hL_ne | cons a0 rest => have hh := hhead_gap a0 rest hL_eq have ha0 : s0 = a0 := by simp [s0, seg, hL_eq] cases rest with | nil => have hnc : nextC ⟨0, hq_pos⟩ = τ := by dsimp only [nextC]; have hnot : ¬ (0 + 1 < L.length) := by simp [hL_eq] rw [dite_eq_right_of_eq_false (eq_false hnot)] dsimp only [R0, R1, g0]; rw [hnc, ha0]; exact hh | cons a1 rest' => have hlt1 : 0 + 1 < L.length := by simp [hL_eq] have hnc : nextC ⟨0, hq_pos⟩ = a1.a := by dsimp only [nextC]; rw [dite_eq_left_of_eq_true (eq_true hlt1)] simp [seg, hL_eq] dsimp only [R0, R1, g0]; rw [hnc, ha0]; exact hh -- Rewrite |U_0(x)| = |w0(x)| ^ (2 ^ g0) have hrun0 := P.topSq_run_eval s0.hab s0.hb s0.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 -- Bound H0 ≤ (max 2 |x|) ^ (2 ^ R0) and polyIntL1 w0 ≤ 2 ^ (2 ^ R0) have hH0_le := wireHeight_base_le P s0.a (by have := s0.hab; have := s0.hb; omega) 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 _ -- Deduce (w0.eval x).natAbs ≤ 1! 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 · -- Case A: |x| ≥ 2 * 2^(2^R0) ≥ 4, so |x| ≤ 2 * |w0(x)| => |x| ≤ |w0(x)|^2! 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 w0 x hw0_deg_pos (by omega) have hx_sq : x.natAbs ≤ (w0.eval x).natAbs ^ 2 := nat_half_sq_ge 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 · -- Case B: |x| < 2 * 2^(2^R0) = 2^(2^R0 + 1), so H0 ≤ 2^(2^(2*R0 + 1))! have hmax_le : max 2 x.natAbs ≤ 2 ^ (2 ^ R0 + 1) := by have h1 : 2 ≤ 2 ^ (2 ^ R0 + 1) := Nat.le_self_pow (Nat.succ_ne_zero _) 2 have h2 : 2 * 2 ^ (2 ^ R0) = 2 ^ (2 ^ R0 + 1) := by rw [pow_succ, Nat.mul_comm] omega have hH0_2 : wireHeight P s0.a (by have := s0.hab; have := s0.hb; omega) x ≤ 2 ^ (2 ^ (2 * R0 + 1)) := by calc wireHeight P s0.a _ x ≤ (max 2 x.natAbs) ^ (2 ^ R0) := hH0_le _ ≤ (2 ^ (2 ^ R0 + 1)) ^ (2 ^ R0) := Nat.pow_le_pow_left hmax_le _ _ = 2 ^ ((2 ^ R0 + 1) * 2 ^ R0) := (pow_mul _ _ _).symm _ ≤ 2 ^ (2 ^ (2 * R0 + 1)) := by apply Nat.pow_le_pow_right (by omega) have h1 : 2 ^ R0 + 1 ≤ 2 * 2 ^ R0 := by have : 1 ≤ 2 ^ R0 := Nat.one_le_pow _ _ (by omega); omega calc (2 ^ R0 + 1) * 2 ^ R0 ≤ (2 * 2 ^ R0) * 2 ^ R0 := Nat.mul_le_mul_right _ h1 _ = 2 * (2 ^ R0 * 2 ^ R0) := by ring _ = 2 ^ (2 * R0 + 1) := by rw [← pow_add, ← two_mul, pow_succ, Nat.mul_comm] have hchain_2 : 2 ^ (2 ^ g0) ≤ 2 ^ (2 ^ (2 * R0 + R1 + 2)) := 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_2 _ _ = 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 -- Since |w0(x)| ≤ 1, w0(x) ∈ {-1, 0, 1}! have hw0_ne : w0 ≠ 0 := fun h0 => by simp [h0] at hw0_deg_pos have hw0_sub1_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_add1_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 have hcases : w0.eval x = 1 ∨ w0.eval x = 0 ∨ w0.eval x = -1 := by omega rcases hcases with h1 | h0 | hm1 · apply Finset.mem_union_left; apply Finset.mem_union_left; apply Finset.mem_union_right rw [intRootSet, Multiset.mem_toFinset, Polynomial.mem_roots hw0_sub1_ne, Polynomial.IsRoot.def] simp [h1] · 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_right rw [intRootSet, Multiset.mem_toFinset, Polynomial.mem_roots hw0_add1_ne, Polynomial.IsRoot.def] simp [hm1] -- Bound the cardinality of the cover! have hcard_poly : ∀ q : Polynomial ℤ, intRootCount q ≤ q.natDegree := fun q => le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _) have hdeg_sub1 : (w0 - 1).natDegree ≤ 2 ^ (τ - Δ) := le_trans (Polynomial.natDegree_sub_le _ _) (max_le hw0_deg_le (by simp)) have hdeg_add1 : (w0 + 1).natDegree ≤ 2 ^ (τ - Δ) := le_trans (Polynomial.natDegree_add_le _ _) (max_le hw0_deg_le (by simp)) calc intRootCount P.evalPoly ≤ ((Finset.univ.biUnion (fun r : Fin L.length => intRootSet (F r).leadingCoeff)) ∪ intRootSet (w0 - 1) ∪ intRootSet w0 ∪ intRootSet (w0 + 1)).card := Finset.card_le_card hsub _ ≤ (Finset.univ.biUnion (fun r : Fin L.length => intRootSet (F r).leadingCoeff)).card + intRootCount (w0 - 1) + intRootCount w0 + intRootCount (w0 + 1) := by have h1 := Finset.card_union_le ((Finset.univ.biUnion (fun r : Fin L.length => intRootSet (F r).leadingCoeff)) ∪ intRootSet (w0 - 1) ∪ intRootSet w0) (intRootSet (w0 + 1)) have h2 := Finset.card_union_le ((Finset.univ.biUnion (fun r : Fin L.length => intRootSet (F r).leadingCoeff)) ∪ intRootSet (w0 - 1)) (intRootSet w0) have h3 := Finset.card_union_le (Finset.univ.biUnion (fun r : Fin L.length => intRootSet (F r).leadingCoeff)) (intRootSet (w0 - 1)) simp only [intRootCount] at * omega _ ≤ (∑ r : Fin L.length, intRootCount (F r).leadingCoeff) + 2 ^ (τ - Δ) + 2 ^ (τ - Δ) + 2 ^ (τ - Δ) := by have hbi : (Finset.univ.biUnion (fun r : Fin L.length => intRootSet (F r).leadingCoeff)).card ≤ ∑ r : Fin L.length, intRootCount (F r).leadingCoeff := Finset.card_biUnion_le have h1 := le_trans (hcard_poly (w0 - 1)) hdeg_sub1 have h2 := le_trans (hcard_poly w0) hw0_deg_le have h3 := le_trans (hcard_poly (w0 + 1)) hdeg_add1 omega _ ≤ (∑ _r : Fin L.length, 2 ^ (τ - Δ)) + 3 * 2 ^ (τ - Δ) := by have hsum := Finset.sum_le_sum (s := (Finset.univ : Finset (Fin L.length))) (fun r _ => le_trans (hcard_poly (F r).leadingCoeff) (hlead_deg r)) omega _ = (L.length + 3) * 2 ^ (τ - Δ) := by simp only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, smul_eq_mul] ring /-! ## 5. Main Unconditional `o(2^τ)` Theorem for All Constant-Free SLPs -/ private lemma three_pow_le_two_pow (K : ℕ) : 3 ^ (3 * K + 1) ≤ 2 ^ (5 * K + 2) := by have h1 : (3 ^ 3) ^ K ≤ (2 ^ 5) ^ K := Nat.pow_le_pow_left (by decide) K rw [← pow_mul, ← pow_mul] at h1 calc 3 ^ (3 * K + 1) = 3 ^ (3 * K) * 3 := pow_succ _ _ _ ≤ 2 ^ (5 * K) * 4 := Nat.mul_le_mul h1 (by decide) _ = 2 ^ (5 * K + 2) := by rw [pow_add]; norm_num private lemma two_pow_quarter_ge_double : ∀ m : ℕ, 40 ≤ m → 2 * m ≤ 2 ^ (m / 4) := by intro m hm have hbase : ∀ q : ℕ, 10 ≤ q → 8 * q + 6 ≤ 2 ^ q := by intro q hq induction q, hq using Nat.le_induction with | base => decide | succ q' hq' ih => rw [pow_succ]; omega have hq : 10 ≤ m / 4 := by omega have h1 := hbase (m / 4) hq omega /-- **Unconditional Elementary `o(2^τ)` Theorem for Smale's 4th Problem:** For EVERY `τ : ℕ` and EVERY constant-free straight-line program `P : SLP τ` with `P.evalPoly ≠ 0`: `τ * (intRootCount P.evalPoly) ^ 28 ≤ 2 ^ 40 * 2 ^ (28 * τ)`. Equivalently, `intRootCount P.evalPoly ≤ 2^(10/7) * 2^τ / τ^(1/28) = o(2^τ)` unconditionally! -/ theorem slp_intRootCount_o_two_pow {τ : ℕ} (P : SLP τ) (hP : P.evalPoly ≠ 0) : τ * (intRootCount P.evalPoly) ^ 28 ≤ 2 ^ 40 * 2 ^ (28 * τ) := by have htriv : intRootCount P.evalPoly ≤ 2 ^ τ := by calc intRootCount P.evalPoly ≤ P.evalPoly.natDegree := le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _) _ ≤ P.maxDeg := P.wireDeg_le_maxDeg ⟨τ + 1, Nat.lt_succ_self _⟩ _ ≤ 2 ^ τ := P.maxDeg_le_two_pow by_cases hsmall : τ < 2 ^ 40 · calc τ * (intRootCount P.evalPoly) ^ 28 ≤ 2 ^ 40 * (2 ^ τ) ^ 28 := Nat.mul_le_mul (le_of_lt hsmall) (Nat.pow_le_pow_left htriv 28) _ = 2 ^ 40 * 2 ^ (28 * τ) := by rw [← pow_mul, Nat.mul_comm τ 28] · have hτ_big : 2 ^ 40 ≤ τ := by omega set m := Nat.log 2 τ have hm40 : 40 ≤ m := (Nat.le_log_iff_pow_le (by omega) (by omega)).mpr hτ_big have h2m_le : 2 ^ m ≤ τ := Nat.pow_log_le_self 2 (by omega) have hτ_lt : τ < 2 ^ (m + 1) := Nat.lt_pow_succ_log_self (by omega) τ set K := m / 8 by_cases hk_high : K ≤ P.nonTopSqCount · -- Case 1: High non-top-squaring count -> Pure-Squaring Collapse wins! have hdeg7 : (P.evalPoly.natDegree) ^ 7 ≤ 2 ^ (7 * τ - 2 * P.nonTopSqCount) := slp_pure_squaring_collapse_deg P have hz_le_deg : intRootCount P.evalPoly ≤ P.evalPoly.natDegree := le_trans (Multiset.toFinset_card_le _) (Polynomial.card_roots' _) have hz7 : (intRootCount P.evalPoly) ^ 7 ≤ 2 ^ (7 * τ - 2 * K) := le_trans (Nat.pow_le_pow_left hz_le_deg 7) (le_trans hdeg7 (Nat.pow_le_pow_right (by omega) (by omega))) have hz28 : (intRootCount P.evalPoly) ^ 28 ≤ 2 ^ (28 * τ - 8 * K) := by have h1 : ((intRootCount P.evalPoly) ^ 7) ^ 4 ≤ (2 ^ (7 * τ - 2 * K)) ^ 4 := Nat.pow_le_pow_left hz7 4 rw [← pow_mul, ← pow_mul] at h1 have hK_le : 2 * K ≤ 7 * τ := by have : m ≤ 2 ^ m := Nat.le_of_lt Nat.lt_two_pow_self omega have hexp : (7 * τ - 2 * K) * 4 = 28 * τ - 8 * K := by omega rwa [hexp] at h1 have hm_le_8K : m + 1 ≤ 8 * K + 8 := by omega have hτ_le : τ ≤ 2 ^ (8 * K + 8) := le_trans (le_of_lt hτ_lt) (Nat.pow_le_pow_right (by omega) hm_le_8K) have h8K_le : 8 * K ≤ 28 * τ := by have : m ≤ 2 ^ m := Nat.le_of_lt Nat.lt_two_pow_self omega calc τ * (intRootCount P.evalPoly) ^ 28 ≤ 2 ^ (8 * K + 8) * 2 ^ (28 * τ - 8 * K) := Nat.mul_le_mul hτ_le hz28 _ = 2 ^ ((8 * K + 8) + (28 * τ - 8 * K)) := (pow_add 2 _ _).symm _ = 2 ^ (8 + 28 * τ) := by congr 1; omega _ = 2 ^ 8 * 2 ^ (28 * τ) := pow_add 2 8 (28 * τ) _ ≤ 2 ^ 40 * 2 ^ (28 * τ) := Nat.mul_le_mul_right _ (by decide) · -- Case 2: Low non-top-squaring count (k < K) -> Multi-Segment Gap-Dominance wins! have hk_lt : P.nonTopSqCount < K := by omega obtain ⟨L0, hord0, hlen0, htot0⟩ := extract_untapped_segments P set Δ := 2 ^ (m / 4) have h2m4 : 2 * m ≤ Δ := two_pow_quarter_ge_double m hm40 have hΔ_le_τ : Δ ≤ τ := le_trans (Nat.pow_le_pow_right (by omega) (by omega : m / 4 ≤ m)) h2m_le -- Show the demotion failure branch is impossible! have hdem_strict : 3 ^ L0.length * (τ - segsTotalLen L0 + Δ + 2) < τ + Δ + 2 := by have hR0 : τ - segsTotalLen L0 = P.nonTopSqCount := by have := P.nonTopSqCount_le omega rw [hR0] have hL0_le : L0.length ≤ 3 * K + 1 := by omega have h3pow : 3 ^ L0.length ≤ 2 ^ (5 * K + 2) := le_trans (Nat.pow_le_pow_right (by omega) hL0_le) (three_pow_le_two_pow K) have hfac : P.nonTopSqCount + Δ + 2 ≤ 2 * Δ := by omega have hmul_le : 3 ^ L0.length * (P.nonTopSqCount + Δ + 2) ≤ 2 ^ (5 * K + 2 + (m / 4 + 1)) := by calc 3 ^ L0.length * (P.nonTopSqCount + Δ + 2) ≤ 2 ^ (5 * K + 2) * (2 * Δ) := Nat.mul_le_mul h3pow hfac _ = 2 ^ (5 * K + 2) * 2 ^ (m / 4 + 1) := by congr 1; rw [pow_succ, Nat.mul_comm] _ = 2 ^ (5 * K + 2 + (m / 4 + 1)) := (pow_add 2 _ _).symm have hexp_lt : 5 * K + 2 + (m / 4 + 1) < m := by omega have hpow_lt : 2 ^ (5 * K + 2 + (m / 4 + 1)) < 2 ^ m := Nat.pow_lt_pow_right (by omega) hexp_lt omega rcases demotion_exists_valid_gaps Δ L0.length L0 rfl hord0 with ⟨L', hv', hlen'⟩ | hfail · have hz_gap := slp_multi_segment_gap_dominance P hP L' hv' have hL'_le : L'.length + 3 ≤ 2 ^ m := by have : m ≤ 2 ^ m := Nat.le_of_lt Nat.lt_two_pow_self have : L'.length + 3 ≤ 3 * K + 4 := by omega have : 3 * K + 4 ≤ m := by omega omega have hz_le : intRootCount P.evalPoly ≤ 2 ^ m * 2 ^ (τ - Δ) := le_trans hz_gap (Nat.mul_le_mul_right _ hL'_le) have hz28 : (intRootCount P.evalPoly) ^ 28 ≤ 2 ^ (28 * m) * 2 ^ (28 * (τ - Δ)) := by calc (intRootCount P.evalPoly) ^ 28 ≤ (2 ^ m * 2 ^ (τ - Δ)) ^ 28 := Nat.pow_le_pow_left hz_le 28 _ = (2 ^ m) ^ 28 * (2 ^ (τ - Δ)) ^ 28 := mul_pow _ _ _ _ = 2 ^ (28 * m) * 2 ^ (28 * (τ - Δ)) := by rw [← pow_mul, ← pow_mul, Nat.mul_comm m 28, Nat.mul_comm (τ - Δ) 28] have hexp_ok : m + 1 + 28 * m + 28 * (τ - Δ) ≤ 28 * τ := by omega calc τ * (intRootCount P.evalPoly) ^ 28 ≤ 2 ^ (m + 1) * (2 ^ (28 * m) * 2 ^ (28 * (τ - Δ))) := Nat.mul_le_mul (le_of_lt hτ_lt) hz28 _ = 2 ^ (m + 1 + 28 * m + 28 * (τ - Δ)) := by rw [← pow_add, ← pow_add]; congr 1; omega _ ≤ 2 ^ (28 * τ) := Nat.pow_le_pow_right (by omega) hexp_ok _ = 1 * 2 ^ (28 * τ) := (Nat.one_mul _).symm _ ≤ 2 ^ 40 * 2 ^ (28 * τ) := Nat.mul_le_mul_right _ (by decide) · exfalso; omega end Research.PvsNP.Tau