Lean / 0xOsprey/shub-smale-tau9 / CertSoundness.lean
Lean readerResearch/PvsNP/Tau/CertSoundness.lean
modulepublic import Research.PvsNP.Tau.Canonicalpublic import Research.PvsNP.Tau.ModularRootBoundpublic import Mathlib.Algebra.Polynomial.Taylor@[expose] public sectionnamespace Research.PvsNP.Tau/-!# Soundness of Per-Leaf Root Certificates and Evaluation-Based DeduplicationThis module formalizes the evaluation-based soundness of the leaf certificates andhash/CRT checks used by the exhaustive $\tau$-conjecture verifier:1. Generic commutative-ring SLP evaluation (`evalIn`, `outputIn`) commuting with ringhomomorphisms.2. Degree-bound propagation (`degUb`) and `intRootCount f ≤ degUb`.3. Atomic product-factor bitmask bound (`prodMask`) including `intRootCount (A + A) = intRootCount A`,constant elimination, and union bounds over atomic factors.4. Dual-number arithmetic (`Dual R`) evaluating `(f(a), f'(a)) mod p` in one pass,and the SLP-level simple-root modular certificate.5. Exact Taylor-residue certificate (`taylor_root_eq_of_factor`, `intRootCount_le_of_taylor_zmod`)handling repeated small integer roots.6. Soundness of hash-based inequality (`≠`) conclusions and CRT exact-equality-by-evaluation.-//-! ## 1. Generic Ring Evaluation and Homomorphism Commutation -//-- Apply an `SLPOp` in any ring `R`. -/def SLPOp.applyRing {R : Type*} [Ring R] : SLPOp → R → R → R| .add, a, b => a + b| .sub, a, b => a - b| .mul, a, b => a * btheorem SLPOp.map_applyRing {R S : Type*} [Ring R] [Ring S](φ : R →+* S) (op : SLPOp) (a b : R) :φ (op.applyRing a b) = op.applyRing (φ a) (φ b) := bycases op <;> simp [SLPOp.applyRing]/-- Evaluate wire `k : Fin (τ + 2)` of `P : SLP τ` in an arbitrary ring `R`with indeterminate `X` instantiated to `x : R`. -/def SLP.evalWireIn {R : Type*} [Ring R] (x : R) : {τ : ℕ} → SLP τ → Fin (τ + 2) → R| 0, .nil, k => if k.val = 0 then 1 else x| τ + 1, .snoc P op i j, k =>if h : k.val < τ + 2 thenP.evalWireIn x ⟨k.val, h⟩elseop.applyRing (P.evalWireIn x i) (P.evalWireIn x j)/-- Evaluate the output wire of `P : SLP τ` in any ring `R` at `x : R`. -/def SLP.evalIn {R : Type*} [Ring R] (x : R) {τ : ℕ} (P : SLP τ) : R :=P.evalWireIn x ⟨τ + 1, Nat.lt_succ_self _⟩theorem SLP.evalWireIn_X :∀ {τ : ℕ} (P : SLP τ) (k : Fin (τ + 2)),P.evalWireIn (Polynomial.X : Polynomial ℤ) k = P.evalWire k| 0, .nil, k => bysimp only [SLP.evalWireIn, SLP.evalWire]| τ + 1, .snoc P op i j, k => bysimp only [SLP.evalWireIn, SLP.evalWire]split_ifs with h· exact P.evalWireIn_X ⟨k.val, h⟩· rw [P.evalWireIn_X i, P.evalWireIn_X j]cases op <;> rfltheorem SLP.evalIn_X {τ : ℕ} (P : SLP τ) :P.evalIn (Polynomial.X : Polynomial ℤ) = P.evalPoly :=P.evalWireIn_X ⟨τ + 1, Nat.lt_succ_self _⟩theorem SLP.map_evalWireIn {R S : Type*} [Ring R] [Ring S](φ : R →+* S) (x : R) :∀ {τ : ℕ} (P : SLP τ) (k : Fin (τ + 2)),φ (P.evalWireIn x k) = P.evalWireIn (φ x) k| 0, .nil, k => bysimp only [SLP.evalWireIn]split_ifs <;> simp| τ + 1, .snoc P op i j, k => bysimp only [SLP.evalWireIn]split_ifs with h· exact P.map_evalWireIn φ x ⟨k.val, h⟩· rw [SLPOp.map_applyRing, P.map_evalWireIn φ x i, P.map_evalWireIn φ x j]theorem SLP.map_evalIn {R S : Type*} [Ring R] [Ring S](φ : R →+* S) (x : R) {τ : ℕ} (P : SLP τ) :φ (P.evalIn x) = P.evalIn (φ x) :=P.map_evalWireIn φ x ⟨τ + 1, Nat.lt_succ_self _⟩theorem SLP.evalIn_eq_eval₂ {R : Type*} [CommRing R] (x : R) {τ : ℕ} (P : SLP τ) :P.evalIn x = (P.evalPoly.map (Int.castRingHom R)).eval x := bylet φ : Polynomial ℤ →+* R := Polynomial.eval₂RingHom (Int.castRingHom R) xhave hφ : φ Polynomial.X = x := by simp [φ]have h := P.map_evalIn φ Polynomial.Xrw [P.evalIn_X, hφ] at hsimpa [φ, Polynomial.eval_map] using h.symmtheorem SLP.evalIn_int_eq_eval {τ : ℕ} (P : SLP τ) (a : ℤ) :P.evalIn a = P.evalPoly.eval a := bysimpa using P.evalIn_eq_eval₂ a/-- Accumulator evaluator for `List GateSpec` over any ring `R`. -/def wireValsInAux {R : Type*} [Ring R] : List R → List GateSpec → List R| acc, [] => acc| acc, g :: rest =>wireValsInAux (acc ++ [g.op.applyRing (acc.getD g.i 0) (acc.getD g.j 0)]) rest/-- Wire values of a gate list `gs` evaluated in a ring `R` at `x : R`. -/def wireValsIn {R : Type*} [Ring R] (x : R) (gs : List GateSpec) : List R :=wireValsInAux [1, x] gs/-- Output value of a gate list `gs` evaluated in a ring `R` at `x : R`. -/def outputIn {R : Type*} [Ring R] (x : R) (gs : List GateSpec) : R :=(wireValsIn x gs).getD (gs.length + 1) 0private lemma list_getD_map {R S : Type*} [Ring R] [Ring S] (φ : R →+* S)(l : List R) (i : ℕ) : (l.map φ).getD i 0 = φ (l.getD i 0) := byby_cases hi : i < l.length· simp [List.getD, hi]· simp [List.getD, hi]theorem wireValsInAux_length {R : Type*} [Ring R] (acc : List R) (gs : List GateSpec) :(wireValsInAux acc gs).length = acc.length + gs.length := byinduction gs generalizing acc with| nil => simp [wireValsInAux]| cons g rest ih =>simp only [wireValsInAux]rw [ih]simpomegatheorem wireValsIn_length {R : Type*} [Ring R] (x : R) (gs : List GateSpec) :(wireValsIn x gs).length = gs.length + 2 := byrw [wireValsIn, wireValsInAux_length]simpomegatheorem wireValsIn_X (gs : List GateSpec) :wireValsIn (Polynomial.X : Polynomial ℤ) gs = wireVals gs := byhave haux : ∀ (acc : List (Polynomial ℤ)) (gs : List GateSpec),wireValsInAux acc gs = wireValsAux acc gs := byintro acc gsinduction gs generalizing acc with| nil => rfl| cons g rest ih =>simp only [wireValsInAux, wireValsAux]have hop : g.op.applyRing (acc.getD g.i 0) (acc.getD g.j 0) =g.op.applyPoly (acc.getD g.i 0) (acc.getD g.j 0) := bycases g.op <;> rflrw [hop, ih]exact haux [1, Polynomial.X] gstheorem outputIn_X (gs : List GateSpec) :outputIn (Polynomial.X : Polynomial ℤ) gs = output gs := bysimp [outputIn, output, wireValsIn_X]theorem wireValsIn_map {R S : Type*} [Ring R] [Ring S] (φ : R →+* S)(x : R) (gs : List GateSpec) :(wireValsIn x gs).map φ = wireValsIn (φ x) gs := byhave haux : ∀ (acc : List R) (gs : List GateSpec),(wireValsInAux acc gs).map φ = wireValsInAux (acc.map φ) gs := byintro acc gsinduction gs generalizing acc with| nil => rfl| cons g rest ih =>simp only [wireValsInAux, ih, List.map_append, List.map_singleton,SLPOp.map_applyRing, list_getD_map]simpa [wireValsIn] using haux [1, x] gstheorem outputIn_map {R S : Type*} [Ring R] [Ring S] (φ : R →+* S)(x : R) (gs : List GateSpec) :φ (outputIn x gs) = outputIn (φ x) gs := byunfold outputInrw [← wireValsIn_map φ x gs, list_getD_map]theorem outputIn_eq_eval₂ {R : Type*} [CommRing R] (x : R) (gs : List GateSpec) :outputIn x gs = ((output gs).map (Int.castRingHom R)).eval x := bylet φ : Polynomial ℤ →+* R := Polynomial.eval₂RingHom (Int.castRingHom R) xhave hφ : φ Polynomial.X = x := by simp [φ]have h := outputIn_map φ Polynomial.X gsrw [outputIn_X, hφ] at hsimpa [φ, Polynomial.eval_map] using h.symm/-! ## 2. Degree-Bound Propagation -//-- Wire degree upper bound recurrence for `SLP`:`1 ↦ 0`, `X ↦ 1`, `a ± b ↦ max`, `a * b ↦ sum`. -/def SLP.wireDegUb : {τ : ℕ} → SLP τ → Fin (τ + 2) → ℕ| 0, .nil, k => if k.val = 0 then 0 else 1| τ + 1, .snoc P op i j, k =>if h : k.val < τ + 2 thenP.wireDegUb ⟨k.val, h⟩elsematch op with| .add | .sub => max (P.wireDegUb i) (P.wireDegUb j)| .mul => P.wireDegUb i + P.wireDegUb j/-- Output degree upper bound of `P : SLP τ`. -/def SLP.degUb {τ : ℕ} (P : SLP τ) : ℕ :=P.wireDegUb ⟨τ + 1, Nat.lt_succ_self _⟩private lemma wireDegUb_snoc {τ : ℕ} (P : SLP τ) (op : SLPOp) (i j : Fin (τ + 2))(k : Fin (τ + 1 + 2)) :(P.snoc op i j).wireDegUb k =if h : k.val < τ + 2 thenP.wireDegUb ⟨k.val, h⟩elsematch op with| .add | .sub => max (P.wireDegUb i) (P.wireDegUb j)| .mul => P.wireDegUb i + P.wireDegUb j := rfltheorem slp_natDegree_le_wireDegUb :∀ {τ : ℕ} (P : SLP τ) (k : Fin (τ + 2)),(P.evalWire k).natDegree ≤ P.wireDegUb k| 0, .nil, k => bysimp only [SLP.evalWire, SLP.wireDegUb]split_ifs <;> simp| τ + 1, .snoc P op i j, k => byrw [wireDegUb_snoc]by_cases h : k.val < τ + 2· simp only [SLP.evalWire, h, ↓reduceDIte]exact slp_natDegree_le_wireDegUb P ⟨k.val, h⟩· simp only [SLP.evalWire, h, ↓reduceDIte]have ihi := slp_natDegree_le_wireDegUb P ihave ihj := slp_natDegree_le_wireDegUb P jcases op with| add => exact (Polynomial.natDegree_add_le _ _).trans (max_le_max ihi ihj)| sub => exact (Polynomial.natDegree_sub_le _ _).trans (max_le_max ihi ihj)| mul => exact Polynomial.natDegree_mul_le.trans (Nat.add_le_add ihi ihj)theorem slp_natDegree_le_degUb {τ : ℕ} (P : SLP τ) :P.evalPoly.natDegree ≤ P.degUb :=slp_natDegree_le_wireDegUb P ⟨τ + 1, Nat.lt_succ_self _⟩/-- Degree upper bound root certificate for `SLP`. -/theorem slp_intRootCount_le_degUb {τ : ℕ} (P : SLP τ) (hne : P.evalPoly ≠ 0) :intRootCount P.evalPoly ≤ P.degUb :=(intRootCount_le_natDegree hne).trans (slp_natDegree_le_degUb P)/-- Degree upper bound accumulator for `List GateSpec`. -/def wireDegUbsAux : List ℕ → List GateSpec → List ℕ| acc, [] => acc| acc, g :: rest =>let d :=match g.op with| .add | .sub => max (acc.getD g.i 0) (acc.getD g.j 0)| .mul => acc.getD g.i 0 + acc.getD g.j 0wireDegUbsAux (acc ++ [d]) rest/-- Wire degree upper bounds for `gs : List GateSpec`. -/def wireDegUbs (gs : List GateSpec) : List ℕ :=wireDegUbsAux [0, 1] gs/-- Output degree upper bound for `gs : List GateSpec`. -/def degUb (gs : List GateSpec) : ℕ :=(wireDegUbs gs).getD (gs.length + 1) 0theorem wireVals_natDegree_le_wireDegUbs (gs : List GateSpec) :∀ w : ℕ, ((wireVals gs).getD w 0).natDegree ≤ (wireDegUbs gs).getD w 0 := byhave haux : ∀ (accP : List (Polynomial ℤ)) (accD : List ℕ) (gs : List GateSpec),accP.length = accD.length →(∀ w, (accP.getD w 0).natDegree ≤ accD.getD w 0) →∀ w, ((wireValsAux accP gs).getD w 0).natDegree ≤ (wireDegUbsAux accD gs).getD w 0 := byintro accP accD gsinduction gs generalizing accP accD with| nil =>intro _ h wexact h w| cons g rest ih =>intro hlen h wsimp only [wireValsAux, wireDegUbsAux]refine ih _ _ (by simp [hlen]) ?_ wintro uby_cases hu : u < accP.length· have huD : u < accD.length := by omegasimp only [List.getD, List.getElem?_append_left hu, List.getElem?_append_left huD]exact h u· have huD : accD.length ≤ u := by omegahave huP : accP.length ≤ u := by omegaby_cases heq : u = accP.length· subst heqsimp only [List.getD, List.getElem?_append_right (le_refl _),List.getElem?_append_right hlen.le, hlen, Nat.sub_self,List.getElem?_cons_zero, Option.getD_some]have hi := h g.ihave hj := h g.jcases g.op with| add => exact (Polynomial.natDegree_add_le _ _).trans (max_le_max hi hj)| sub => exact (Polynomial.natDegree_sub_le _ _).trans (max_le_max hi hj)| mul => exact Polynomial.natDegree_mul_le.trans (Nat.add_le_add hi hj)· have h1 : (accP ++ [g.op.applyPoly (accP.getD g.i 0) (accP.getD g.j 0)]).length ≤ u := bysimp; omegarw [List.getD, List.getElem?_eq_none h1]simprefine haux [1, Polynomial.X] [0, 1] gs rfl ?_intro wrcases w with _ | _ | w <;> simp [List.getD]theorem output_natDegree_le_degUb (gs : List GateSpec) :(output gs).natDegree ≤ degUb gs :=wireVals_natDegree_le_wireDegUbs gs (gs.length + 1)theorem output_intRootCount_le_degUb (gs : List GateSpec) (hne : output gs ≠ 0) :intRootCount (output gs) ≤ degUb gs :=(intRootCount_le_natDegree hne).trans (output_natDegree_le_degUb gs)/-! ## 3. Atomic Product-Mask Bound -//-- Doubling a polynomial preserves its integer root set: `intRootSet (A + A) = intRootSet A`. -/theorem intRootSet_add_self (A : Polynomial ℤ) :intRootSet (A + A) = intRootSet A := byext rsimp only [mem_intRootSet_iff, Polynomial.IsRoot, Polynomial.eval_add]have h0 : A + A = 0 ↔ A = 0 := byrw [← two_mul]exact mul_eq_zero.trans (by simp)constructor· rintro ⟨hne, hr⟩exact ⟨fun h => hne (h0.mpr h), by omega⟩· rintro ⟨hne, hr⟩exact ⟨fun h => hne (h0.mp h), by omega⟩/-- `intRootCount (A + A) = intRootCount A`. -/theorem intRootCount_add_self (A : Polynomial ℤ) :intRootCount (A + A) = intRootCount A := bysimp [intRootCount, intRootSet_add_self]/-- Nonzero constant polynomials in `ℤ[X]` have `0` integer roots. -/theorem intRootCount_C (c : ℤ) (hc : c ≠ 0) :intRootCount (Polynomial.C c) = 0 :=Nat.eq_zero_of_le_zero ((intRootCount_le_natDegree (Polynomial.C_ne_zero.mpr hc)).trans (by simp))/-- Generalized union bound: if every integer root of `f` is a root of at least one nonzeroatomic factor `w a` (`a ∈ M`) with `intRootCount (w a) ≤ ub a`, then`intRootCount f ≤ ∑ a ∈ M, ub a`. -/theorem intRootCount_le_sum_of_rootSet_subset_biUnion{f : Polynomial ℤ} {M : Finset ℕ} {w : ℕ → Polynomial ℤ} {ub : ℕ → ℕ}(hsub : intRootSet f ⊆ M.biUnion (fun a => intRootSet (w a)))(hub : ∀ a ∈ M, intRootCount (w a) ≤ ub a) :intRootCount f ≤ ∑ a ∈ M, ub a := bycalcintRootCount f = (intRootSet f).card := rfl_ ≤ (M.biUnion (fun a => intRootSet (w a))).card := Finset.card_le_card hsub_ ≤ ∑ a ∈ M, (intRootSet (w a)).card := Finset.card_biUnion_le_ = ∑ a ∈ M, intRootCount (w a) := rfl_ ≤ ∑ a ∈ M, ub a := Finset.sum_le_sum hub/-- Atomic product-factor bitmask (`Finset ℕ`) matching `prod_mask` in `tau_z9_unconditional.c`. -/def SLP.wireProdMask : {τ : ℕ} → SLP τ → Fin (τ + 2) → Finset ℕ| 0, .nil, k => if k.val = 0 then ∅ else {1}| τ + 1, .snoc P op i j, k =>if h : k.val < τ + 2 thenP.wireProdMask ⟨k.val, h⟩elseif (P.snoc op i j).degUb = 0 then∅elsematch op with| .mul => P.wireProdMask i ∪ P.wireProdMask j| .add => if i = j then P.wireProdMask i else {τ + 2}| .sub => {τ + 2}/-- Output product-factor mask of `P : SLP τ`. -/def SLP.prodMask {τ : ℕ} (P : SLP τ) : Finset ℕ :=P.wireProdMask ⟨τ + 1, Nat.lt_succ_self _⟩private lemma wireProdMask_snoc {τ : ℕ} (P : SLP τ) (op : SLPOp) (i j : Fin (τ + 2))(k : Fin (τ + 1 + 2)) :(P.snoc op i j).wireProdMask k =if h : k.val < τ + 2 then P.wireProdMask ⟨k.val, h⟩else if (P.snoc op i j).degUb = 0 then ∅else match op with| .mul => P.wireProdMask i ∪ P.wireProdMask j| .add => if i = j then P.wireProdMask i else {τ + 2}| .sub => {τ + 2} := rfltheorem slp_wireProdMask_bound {τ : ℕ} (P : SLP τ) :∀ (k : Fin (τ + 2)),(∀ a ∈ P.wireProdMask k, a < τ + 2) ∧(P.evalWire k ≠ 0 →intRootSet (P.evalWire k) ⊆(P.wireProdMask k).biUnion (fun a =>if ha : a < τ + 2 then intRootSet (P.evalWire ⟨a, ha⟩) else ∅)) := byinduction P with| nil =>intro ksimp only [SLP.wireProdMask, SLP.evalWire]rcases k with ⟨kv, hkv⟩rcases kv with _ | _ | kv· simp [intRootSet]· refine ⟨by simp, fun _ => ?_⟩intro r hrrw [Finset.mem_biUnion]exact ⟨1, Finset.mem_singleton_self 1, by simpa⟩· omega| @snoc τ P op i j ih =>intro khave lift_mem : ∀ (a : ℕ) (ha : a < τ + 2) (r : ℤ),r ∈ intRootSet (P.evalWire ⟨a, ha⟩) →r ∈ (if ha' : a < τ + 1 + 2 then intRootSet ((P.snoc op i j).evalWire ⟨a, ha'⟩) else ∅) := byintro a ha r hrrw [dite_eq_left (ha.trans (Nat.lt_succ_self _))]simpa only [SLP.evalWire, ha, ↓reduceDIte] using hrrw [wireProdMask_snoc]by_cases h : k.val < τ + 2· simp only [SLP.evalWire, h, ↓reduceDIte]rcases ih ⟨k.val, h⟩ with ⟨ha_lt, hsub⟩refine ⟨fun a ha => (ha_lt a ha).trans (Nat.lt_succ_self _), fun hne => ?_⟩refine (hsub hne).trans ?_intro r hrrcases Finset.mem_biUnion.mp hr with ⟨a, ha, hr_a⟩have ha_lt' : a < τ + 2 := ha_lt a harw [dite_eq_left ha_lt'] at hr_arw [Finset.mem_biUnion]exact ⟨a, ha, lift_mem a ha_lt' r hr_a⟩· simp only [SLP.evalWire, h, ↓reduceDIte]by_cases hdeg0 : (P.snoc op i j).degUb = 0· simp only [hdeg0, ↓reduceIte]refine ⟨by simp, fun hne => ?_⟩have hdeg_poly : ((P.snoc op i j).evalPoly).natDegree = 0 := byhave hle := slp_natDegree_le_degUb (P.snoc op i j)omegahave hpoly_eq : op.applyPoly (P.evalWire i) (P.evalWire j) = (P.snoc op i j).evalPoly := bysimp only [SLP.evalPoly, SLP.evalWire, Nat.lt_irrefl, ↓reduceDIte]have hcard0 : (intRootSet (op.applyPoly (P.evalWire i) (P.evalWire j))).card = 0 := byrw [hpoly_eq]exact Nat.eq_zero_of_le_zero ((intRootCount_le_natDegree (by rwa [← hpoly_eq])).trans hdeg_poly.le)rw [Finset.card_eq_zero.mp hcard0]simp· simp only [hdeg0, ↓reduceIte]rcases ih i with ⟨hi_lt, hi_sub⟩rcases ih j with ⟨hj_lt, hj_sub⟩cases op· -- addby_cases hij : i = j· subst hijsimp only [↓reduceIte]refine ⟨fun a ha => (hi_lt a ha).trans (Nat.lt_succ_self _), fun hne => ?_⟩have hne_i : P.evalWire i ≠ 0 := byintro h0apply hnesimp [SLPOp.applyPoly, h0]have hrw : SLPOp.add.applyPoly (P.evalWire i) (P.evalWire i) =P.evalWire i + P.evalWire i := rflrw [hrw, intRootSet_add_self]refine (hi_sub hne_i).trans ?_intro r hrrcases Finset.mem_biUnion.mp hr with ⟨a, ha, hr_a⟩have ha_lt' := hi_lt a harw [dite_eq_left ha_lt'] at hr_arw [Finset.mem_biUnion]exact ⟨a, ha, lift_mem a ha_lt' r hr_a⟩· simp only [hij, ↓reduceIte]refine ⟨by simp, fun _ r hr => ?_⟩rw [Finset.mem_biUnion]refine ⟨τ + 2, Finset.mem_singleton_self _, ?_⟩rw [dite_eq_left (Nat.lt_succ_self _)]simpa only [SLP.evalWire, Nat.lt_irrefl, ↓reduceDIte] using hr· -- subrefine ⟨by simp, fun _ r hr => ?_⟩rw [Finset.mem_biUnion]refine ⟨τ + 2, Finset.mem_singleton_self _, ?_⟩rw [dite_eq_left (Nat.lt_succ_self _)]simpa only [SLP.evalWire, Nat.lt_irrefl, ↓reduceDIte] using hr· -- mulrefine ⟨fun a ha => ?_, fun hne => ?_⟩· rcases Finset.mem_union.mp ha with ha | ha· exact (hi_lt a ha).trans (Nat.lt_succ_self _)· exact (hj_lt a ha).trans (Nat.lt_succ_self _)· have hmul : P.evalWire i * P.evalWire j ≠ 0 := hnehave hi_ne : P.evalWire i ≠ 0 := left_ne_zero_of_mul hmulhave hj_ne : P.evalWire j ≠ 0 := right_ne_zero_of_mul hmulhave hsub_mul : intRootSet (P.evalWire i * P.evalWire j) ⊆intRootSet (P.evalWire i) ∪ intRootSet (P.evalWire j) := byintro r hrrw [mem_intRootSet_iff] at hrrw [Finset.mem_union, mem_intRootSet_iff, mem_intRootSet_iff]have hm : (P.evalWire i).eval r * (P.evalWire j).eval r = 0 := bysimpa [Polynomial.IsRoot, Polynomial.eval_mul] using hr.2rcases mul_eq_zero.mp hm with h1 | h2· exact Or.inl ⟨hi_ne, h1⟩· exact Or.inr ⟨hj_ne, h2⟩refine hsub_mul.trans ?_intro r hrrcases Finset.mem_union.mp hr with h1 | h2· rcases Finset.mem_biUnion.mp (hi_sub hi_ne h1) with ⟨a, ha, hr_a⟩have ha_lt' := hi_lt a harw [dite_eq_left ha_lt'] at hr_arw [Finset.mem_biUnion]exact ⟨a, Finset.mem_union_left _ ha, lift_mem a ha_lt' r hr_a⟩· rcases Finset.mem_biUnion.mp (hj_sub hj_ne h2) with ⟨a, ha, hr_a⟩have ha_lt' := hj_lt a harw [dite_eq_left ha_lt'] at hr_arw [Finset.mem_biUnion]exact ⟨a, Finset.mem_union_right _ ha, lift_mem a ha_lt' r hr_a⟩/-- **SLP Atomic Product-Factor Union Bound:**If `ub : Fin (τ + 2) → ℕ` bounds `intRootCount` of every nonzero wire of `P : SLP τ`,then `intRootCount P.evalPoly ≤ ∑ a ∈ P.prodMask, if ha : a < τ + 2 then ub ⟨a, ha⟩ else 0`. -/theorem slp_intRootCount_le_prodMask_sum {τ : ℕ} (P : SLP τ) (ub : Fin (τ + 2) → ℕ)(hub : ∀ k : Fin (τ + 2), P.evalWire k ≠ 0 → intRootCount (P.evalWire k) ≤ ub k)(hne : P.evalPoly ≠ 0) :intRootCount P.evalPoly ≤∑ a ∈ P.prodMask, if ha : a < τ + 2 then ub ⟨a, ha⟩ else 0 := byrcases slp_wireProdMask_bound P ⟨τ + 1, Nat.lt_succ_self _⟩ with ⟨_, hsub⟩let w : ℕ → Polynomial ℤ := fun a => if ha : a < τ + 2 then P.evalWire ⟨a, ha⟩ else 0have hsub' : intRootSet P.evalPoly ⊆ P.prodMask.biUnion (fun a => intRootSet (w a)) := byrefine (hsub hne).trans ?_intro r hrrcases Finset.mem_biUnion.mp hr with ⟨a, ha, hra⟩rw [Finset.mem_biUnion]refine ⟨a, ha, ?_⟩by_cases hlt : a < τ + 2· rw [dite_eq_left hlt] at hrasimpa only [w, hlt, ↓reduceDIte] using hra· simp [hlt] at hrarefine intRootCount_le_sum_of_rootSet_subset_biUnion (w := w) hsub' ?_intro a _by_cases ha : a < τ + 2· simp only [w, ha, ↓reduceDIte]by_cases hwa : P.evalWire ⟨a, ha⟩ = 0· simp [intRootCount, intRootSet, hwa]· exact hub ⟨a, ha⟩ hwa· simp [w, ha, intRootCount, intRootSet]/-! ## 4. Dual-Number Evaluation & SLP Simple-Root Modular Certificate -//-- Dual numbers `R[ε]/(ε^2)` represented as pairs `(val, deriv)`. -/@[ext]structure Dual (R : Type*) whereval : Rderiv : Rderiving DecidableEq, Reprnamespace Dualsection CommRingvariable {R : Type*} [CommRing R]instance : Zero (Dual R) := ⟨⟨0, 0⟩⟩instance : One (Dual R) := ⟨⟨1, 0⟩⟩instance : Add (Dual R) := ⟨fun x y => ⟨x.val + y.val, x.deriv + y.deriv⟩⟩instance : Sub (Dual R) := ⟨fun x y => ⟨x.val - y.val, x.deriv - y.deriv⟩⟩instance : Neg (Dual R) := ⟨fun x => ⟨-x.val, -x.deriv⟩⟩instance : Mul (Dual R) := ⟨fun x y => ⟨x.val * y.val, x.deriv * y.val + x.val * y.deriv⟩⟩@[simp] lemma zero_val : (0 : Dual R).val = 0 := rfl@[simp] lemma zero_deriv : (0 : Dual R).deriv = 0 := rfl@[simp] lemma one_val : (1 : Dual R).val = 1 := rfl@[simp] lemma one_deriv : (1 : Dual R).deriv = 0 := rfl@[simp] lemma add_val (x y : Dual R) : (x + y).val = x.val + y.val := rfl@[simp] lemma add_deriv (x y : Dual R) : (x + y).deriv = x.deriv + y.deriv := rfl@[simp] lemma sub_val (x y : Dual R) : (x - y).val = x.val - y.val := rfl@[simp] lemma sub_deriv (x y : Dual R) : (x - y).deriv = x.deriv - y.deriv := rfl@[simp] lemma neg_val (x : Dual R) : (-x).val = -x.val := rfl@[simp] lemma neg_deriv (x : Dual R) : (-x).deriv = -x.deriv := rfl@[simp] lemma mul_val (x y : Dual R) : (x * y).val = x.val * y.val := rfl@[simp] lemma mul_deriv (x y : Dual R) : (x * y).deriv = x.deriv * y.val + x.val * y.deriv := rflinstance : CommRing (Dual R) whereadd := (· + ·)add_assoc a b c := by ext <;> dsimp <;> ringzero := 0zero_add a := by ext <;> dsimp <;> ringadd_zero a := by ext <;> dsimp <;> ringnsmul := nsmulRecneg := (-·)sub := (· - ·)sub_eq_add_neg a b := by ext <;> dsimp <;> ringzsmul := zsmulRecneg_add_cancel a := by ext <;> dsimp <;> ringadd_comm a b := by ext <;> dsimp <;> ringmul := (· * ·)left_distrib a b c := by ext <;> dsimp <;> ringright_distrib a b c := by ext <;> dsimp <;> ringzero_mul a := by ext <;> dsimp <;> ringmul_zero a := by ext <;> dsimp <;> ringmul_assoc a b c := by ext <;> dsimp <;> ringone := 1one_mul a := by ext <;> dsimp <;> ringmul_one a := by ext <;> dsimp <;> ringmul_comm a b := by ext <;> dsimp <;> ringend CommRingend Dual/-- Evaluation into `Dual R` at `a : R` (`f ↦ (f(a), f'(a))`) is a ring homomorphism. -/noncomputable def evalDualHom {R : Type*} [CommRing R] (a : R) : Polynomial R →+* Dual R wheretoFun f := ⟨f.eval a, f.derivative.eval a⟩map_one' := by ext <;> simpmap_mul' f g := by ext <;> simp [Polynomial.derivative_mul]map_zero' := by ext <;> simpmap_add' f g := by ext <;> simp/-- **Dual-Number Evaluation Theorem for `SLP`:**Evaluating `P : SLP τ` in `Dual (ZMod p)` at `⟨a, 1⟩` yields `(f_p(a), f_p'(a))`where `f_p = P.evalPoly.map (Int.castRingHom (ZMod p))`. -/theorem slp_evalIn_dual_zmod (p : ℕ) {τ : ℕ} (P : SLP τ) (a : ZMod p) :P.evalIn (⟨a, 1⟩ : Dual (ZMod p)) =⟨(P.evalPoly.map (Int.castRingHom (ZMod p))).eval a,(P.evalPoly.map (Int.castRingHom (ZMod p))).derivative.eval a⟩ := bylet φ : Polynomial ℤ →+* Dual (ZMod p) :=(evalDualHom a).comp (Polynomial.mapRingHom (Int.castRingHom (ZMod p)))have hX : φ Polynomial.X = ⟨a, 1⟩ := byext <;> simp [φ, evalDualHom]have h := P.map_evalIn φ Polynomial.Xrw [P.evalIn_X, hX] at hsimpa [φ, evalDualHom] using h.symm/-- Dual-number evaluation theorem for `List GateSpec`. -/theorem outputIn_dual_zmod (p : ℕ) (gs : List GateSpec) (a : ZMod p) :outputIn (⟨a, 1⟩ : Dual (ZMod p)) gs =⟨((output gs).map (Int.castRingHom (ZMod p))).eval a,((output gs).map (Int.castRingHom (ZMod p))).derivative.eval a⟩ := bylet φ : Polynomial ℤ →+* Dual (ZMod p) :=(evalDualHom a).comp (Polynomial.mapRingHom (Int.castRingHom (ZMod p)))have hX : φ Polynomial.X = ⟨a, 1⟩ := byext <;> simp [φ, evalDualHom]have h := outputIn_map φ Polynomial.X gsrw [outputIn_X, hX] at hsimpa [φ, evalDualHom] using h.symm/-- **SLP-Level Simple-Root Modular Certificate:**If `p` is prime, `K < p`, and dual-number evaluation of `P : SLP τ` over `ZMod p`shows at most `K` zeros and no double zeros (`val = 0 → deriv ≠ 0`), then`intRootCount P.evalPoly ≤ K` unconditionally over all of `ℤ`. -/theorem slp_intRootCount_le_of_dual_eval_zmod (p : ℕ) [Fact p.Prime] {τ : ℕ} (P : SLP τ) (K : ℕ)(hKp : K < p)(hsimple : ∀ a : ZMod p,(P.evalIn (⟨a, 1⟩ : Dual (ZMod p))).val = 0 →(P.evalIn (⟨a, 1⟩ : Dual (ZMod p))).deriv ≠ 0)(hbound : (Finset.univ.filter (fun a : ZMod p =>(P.evalIn (⟨a, 1⟩ : Dual (ZMod p))).val = 0)).card ≤ K) :intRootCount P.evalPoly ≤ K := byhave heval : ∀ a : ZMod p,(P.evalPoly.map (Int.castRingHom (ZMod p))).eval a =(P.evalIn (⟨a, 1⟩ : Dual (ZMod p))).val := byintro arw [slp_evalIn_dual_zmod]have hderiv_eq : ∀ a : ZMod p,(P.evalPoly.map (Int.castRingHom (ZMod p))).derivative.eval a =(P.evalIn (⟨a, 1⟩ : Dual (ZMod p))).deriv := byintro arw [slp_evalIn_dual_zmod]let Z := Finset.univ.filter (fun a : ZMod p =>(P.evalPoly.map (Int.castRingHom (ZMod p))).eval a = 0)have hZ_card : Z.card ≤ K := bysimpa [Z, heval] using hboundhave hne_pt : ∃ a : ZMod p, (P.evalPoly.map (Int.castRingHom (ZMod p))).eval a ≠ 0 := byby_contra hnonepush Not at hnonehave huniv : Finset.univ ⊆ Z := fun a _ => Finset.mem_filter.mpr ⟨Finset.mem_univ a, hnone a⟩have hcard : p ≤ Z.card := bycalcp = Fintype.card (ZMod p) := (ZMod.card p).symm_ = (Finset.univ : Finset (ZMod p)).card := rfl_ ≤ Z.card := Finset.card_le_card hunivomegarefine intRootCount_le_of_eval_zmod p P.evalPoly K hne_pt ?_ hZ_cardintro a harw [hderiv_eq]apply hsimple arwa [← heval]/-! ## 5. Exact Taylor-Residue Certificate -//-- **Single-Point Taylor Factor Lemma:**If `f = (X - C b)^m * g` in `ℤ[X]` and `g(b) ≢ 0 (mod p)` for prime `p`, then everyinteger root `s` of `f` satisfying `s ≡ b (mod p)` must equal `b`. -/theorem taylor_root_eq_of_factor (p : ℕ) [Fact p.Prime] (f g : Polynomial ℤ) (b : ℤ) (m : ℕ)(hfac : f = (Polynomial.X - Polynomial.C b) ^ m * g)(hgb : (g.map (Int.castRingHom (ZMod p))).eval (b : ZMod p) ≠ 0) :∀ s : ℤ, f.IsRoot s → (s : ZMod p) = (b : ZMod p) → s = b := byintro s hs hmodhave h0 : (s - b) ^ m * g.eval s = 0 := bysimpa [Polynomial.IsRoot, hfac, Polynomial.eval_mul, Polynomial.eval_pow,Polynomial.eval_sub, Polynomial.eval_X, Polynomial.eval_C] using hsrcases mul_eq_zero.mp h0 with hpow | hgs· rcases eq_or_ne (s - b) 0 with hsb | hsb· exact sub_eq_zero.mp hsb· exact absurd hpow (pow_ne_zero m hsb)· exfalsoapply hgbrw [← hmod, Polynomial.eval_map, Polynomial.eval₂_at_intCast]simp [hgs]/-- Relating vanishing low-order Taylor coefficients `f(X + b)` to divisibility by `(X - b)^m`. -/theorem factor_of_taylor_vanish (f : Polynomial ℤ) (b : ℤ) (m : ℕ)(hvan : ∀ j < m, (f.comp (Polynomial.X + Polynomial.C b)).coeff j = 0) :∃ g : Polynomial ℤ,f = (Polynomial.X - Polynomial.C b) ^ m * g ∧g.eval b = (f.comp (Polynomial.X + Polynomial.C b)).coeff m := bylet h := f.comp (Polynomial.X + Polynomial.C b)have hdvd : Polynomial.X ^ m ∣ h := Polynomial.X_pow_dvd_iff.mpr hvanrcases hdvd with ⟨q, hq⟩refine ⟨q.comp (Polynomial.X - Polynomial.C b), ?_, ?_⟩· have hcomp : ((Polynomial.X + Polynomial.C b).comp (Polynomial.X - Polynomial.C b)) = Polynomial.X := bysimp [Polynomial.add_comp, Polynomial.X_comp]calcf = f.comp Polynomial.X := (Polynomial.comp_X).symm_ = f.comp ((Polynomial.X + Polynomial.C b).comp (Polynomial.X - Polynomial.C b)) := by rw [hcomp]_ = h.comp (Polynomial.X - Polynomial.C b) := (Polynomial.comp_assoc f _ _).symm_ = (Polynomial.X ^ m * q).comp (Polynomial.X - Polynomial.C b) := by rw [hq]_ = (Polynomial.X - Polynomial.C b) ^ m * q.comp (Polynomial.X - Polynomial.C b) := bysimp [Polynomial.mul_comp, Polynomial.pow_comp, Polynomial.X_comp]· have hq0 : q.coeff 0 = h.coeff m := byhave hm : h.coeff (0 + m) = q.coeff 0 := byrw [hq, Polynomial.coeff_X_pow_mul]simpa using hm.symmrw [Polynomial.eval_comp]simp only [Polynomial.eval_sub, Polynomial.eval_X, Polynomial.eval_C, sub_self]rw [← Polynomial.coeff_zero_eq_eval_zero, hq0]/-- **Combined Taylor-Residue Modular Certificate (Factor Form):**Suppose `p` is prime, `f_p := f.map (Int.castRingHom (ZMod p)) ≠ 0`, `f_p` has no double rootoutside the residues of a finite set `S ⊂ ℤ`, and for each `b ∈ S` there exist `m ≥ 1` andcofactor `g` with `f = (X - C b)^m * g` and `g(b) ≢ 0 (mod p)`. If `f_p` has at most `K`distinct zeros in `ZMod p`, then `intRootCount f ≤ K` unconditionally over `ℤ`. -/theorem intRootCount_le_of_factor_zmod (p : ℕ) [Fact p.Prime] (f : Polynomial ℤ)(S : Finset ℤ) (K : ℕ)(_hf_p : f.map (Int.castRingHom (ZMod p)) ≠ 0)(houtside : ∀ a : ZMod p,(f.map (Int.castRingHom (ZMod p))).IsRoot a →(f.map (Int.castRingHom (ZMod p))).derivative.IsRoot a →∃ b ∈ S, (b : ZMod p) = a)(hfactor : ∀ b ∈ S, ∃ (m : ℕ) (g : Polynomial ℤ),1 ≤ m ∧ f = (Polynomial.X - Polynomial.C b) ^ m * g ∧(g.map (Int.castRingHom (ZMod p))).eval (b : ZMod p) ≠ 0)(hbound : (Finset.univ.filter (fun a : ZMod p =>(f.map (Int.castRingHom (ZMod p))).eval a = 0)).card ≤ K) :intRootCount f ≤ K := byset φ := Int.castRingHom (ZMod p)set f_p := f.map φlet Z := Finset.univ.filter (fun a : ZMod p => f_p.eval a = 0)have hmaps : ∀ r ∈ intRootSet f, (r : ZMod p) ∈ Z := byintro r hrrw [mem_intRootSet_iff] at hrrw [Finset.mem_filter]refine ⟨Finset.mem_univ _, ?_⟩rw [Polynomial.eval_map, Polynomial.eval₂_at_intCast]simp [hr.2.eq_zero]have hinj : Set.InjOn (fun r : ℤ => (r : ZMod p)) (intRootSet f) := byintro r₁ hr₁ r₂ hr₂ hmodby_contra hnehave hsub : ∀ r ∈ ({r₁, r₂} : Finset ℤ), f.IsRoot r := byintro r hrsimp only [Finset.mem_insert, Finset.mem_singleton] at hrrcases hr with rfl | rfl· exact (mem_intRootSet_iff.mp hr₁).2· exact (mem_intRootSet_iff.mp hr₂).2have hdvd_Z : (Polynomial.X - Polynomial.C r₁) * (Polynomial.X - Polynomial.C r₂) ∣ f := byhave hprod := prod_linear_dvd_of_roots f {r₁, r₂} hsubrwa [Finset.prod_pair hne] at hprodhave hsq_p : (Polynomial.X - Polynomial.C (r₁ : ZMod p)) ^ 2 ∣ f_p := byrcases hdvd_Z with ⟨q, hq⟩refine ⟨q.map φ, ?_⟩dsimp only at hmodhave h1 : f_p = ((Polynomial.X - Polynomial.C r₁) * (Polynomial.X - Polynomial.C r₂)).map φ * q.map φ := bydsimp only [f_p]rw [hq, Polynomial.map_mul]rw [h1, Polynomial.map_mul, Polynomial.map_sub, Polynomial.map_sub,Polynomial.map_X, Polynomial.map_C, Polynomial.map_C]simp only [φ, Int.coe_castRingHom]rw [hmod, sq, mul_assoc]have hroot : f_p.IsRoot (r₁ : ZMod p) := byrw [Polynomial.IsRoot, Polynomial.eval_map, Polynomial.eval₂_at_intCast]simp [(mem_intRootSet_iff.mp hr₁).2.eq_zero]have hderiv : f_p.derivative.IsRoot (r₁ : ZMod p) := byrcases hsq_p with ⟨q, hq⟩have hq' : f_p = (Polynomial.X - Polynomial.C (r₁ : ZMod p)) *((Polynomial.X - Polynomial.C (r₁ : ZMod p)) * q) := byrw [hq, sq, mul_assoc]rw [Polynomial.IsRoot, hq', Polynomial.derivative_mul]simprcases houtside (r₁ : ZMod p) hroot hderiv with ⟨b, hbS, hb_eq⟩rcases hfactor b hbS with ⟨m, g, _, hfac, hgb⟩have hr1_eq : r₁ = b :=taylor_root_eq_of_factor p f g b m hfac hgb r₁ (mem_intRootSet_iff.mp hr₁).2 hb_eq.symmhave hr2_eq : r₂ = b :=taylor_root_eq_of_factor p f g b m hfac hgb r₂ (mem_intRootSet_iff.mp hr₂).2(hmod.symm.trans hb_eq.symm)exact hne (hr1_eq.trans hr2_eq.symm)calcintRootCount f = (intRootSet f).card := rfl_ ≤ Z.card := Finset.card_le_card_of_injOn (fun r : ℤ => (r : ZMod p)) hmaps hinj_ ≤ K := hbound/-- **Combined Taylor-Residue Modular Certificate (Exact Taylor Coefficient Form):**This matches `certify_taylor_roots` in `tau_z9_unconditional.c`:if `f_p` has `≤ K < p` zeros in `ZMod p` (ensuring `f_p ≠ 0`), no double roots outsidethe residues of `S`, and for each `b ∈ S` the lowest nonzero Taylor coefficient`c_m = (f.comp (X + C b)).coeff m` has index `m ≥ 1` and `¬ ((p : ℤ) ∣ c_m)`, then`intRootCount f ≤ K`. -/theorem intRootCount_le_of_taylor_zmod (p : ℕ) [Fact p.Prime] (f : Polynomial ℤ)(S : Finset ℤ) (K : ℕ) (hKp : K < p)(houtside : ∀ a : ZMod p,(f.map (Int.castRingHom (ZMod p))).IsRoot a →(f.map (Int.castRingHom (ZMod p))).derivative.IsRoot a →∃ b ∈ S, (b : ZMod p) = a)(htaylor : ∀ b ∈ S, ∃ m : ℕ, 1 ≤ m ∧(∀ j < m, (f.comp (Polynomial.X + Polynomial.C b)).coeff j = 0) ∧¬ ((p : ℤ) ∣ (f.comp (Polynomial.X + Polynomial.C b)).coeff m))(hbound : (Finset.univ.filter (fun a : ZMod p =>(f.map (Int.castRingHom (ZMod p))).eval a = 0)).card ≤ K) :intRootCount f ≤ K := byhave hf_p : f.map (Int.castRingHom (ZMod p)) ≠ 0 := byby_contra h0have huniv : Finset.univ ⊆ Finset.univ.filter (fun a : ZMod p =>(f.map (Int.castRingHom (ZMod p))).eval a = 0) := byintro a _simp [h0]have hcard : p ≤ K := bycalcp = Fintype.card (ZMod p) := (ZMod.card p).symm_ = (Finset.univ : Finset (ZMod p)).card := rfl_ ≤ _ := Finset.card_le_card huniv_ ≤ K := hboundomegarefine intRootCount_le_of_factor_zmod p f S K hf_p houtside ?_ hboundintro b hbSrcases htaylor b hbS with ⟨m, hm1, hvan, hndvd⟩rcases factor_of_taylor_vanish f b m hvan with ⟨g, hfac, hgb⟩refine ⟨m, g, hm1, hfac, ?_⟩rw [Polynomial.eval_map, Polynomial.eval₂_at_intCast]change ((g.eval b : ℤ) : ZMod p) ≠ 0rw [hgb, ne_eq, ZMod.intCast_zmod_eq_zero_iff_dvd]exact hndvd/-- The 7 known small root centers `{0, 1, -1, 2, -2, 3, -3}` have pairwise distinctresidues modulo any prime/modulus `p ≥ 7` (in particular `p ≥ 19`). -/theorem known_centers_distinct_zmod (p : ℕ) (hp : 7 ≤ p) :∀ b₁ ∈ ({0, 1, -1, 2, -2, 3, -3} : Finset ℤ),∀ b₂ ∈ ({0, 1, -1, 2, -2, 3, -3} : Finset ℤ),(b₁ : ZMod p) = (b₂ : ZMod p) → b₁ = b₂ := byintro b₁ hb₁ b₂ hb₂ hmodhave hsub : ((b₁ - b₂ : ℤ) : ZMod p) = 0 := by push_cast; exact sub_eq_zero.mpr hmodrw [ZMod.intCast_zmod_eq_zero_iff_dvd] at hsubhave hb1' : -3 ≤ b₁ ∧ b₁ ≤ 3 := bysimp only [Finset.mem_insert, Finset.mem_singleton] at hb₁rcases hb₁ with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> omegahave hb2' : -3 ≤ b₂ ∧ b₂ ≤ 3 := bysimp only [Finset.mem_insert, Finset.mem_singleton] at hb₂rcases hb₂ with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> omegarcases hsub with ⟨k, hk⟩have hlt : (b₁ - b₂).natAbs < p := by omegaby_cases h0 : b₁ - b₂ = 0· omega· have hge : p ≤ (b₁ - b₂).natAbs := byhave hk0 : k ≠ 0 := fun h => h0 (by simp [hk, h])have hk1 : 1 ≤ k.natAbs := Int.natAbs_pos.mpr hk0have : (b₁ - b₂).natAbs = p * k.natAbs := byhave := congrArg Int.natAbs hksimpa [Int.natAbs_mul] using thisnlinarithomega/-- Evaluating an `SLP` in `ℤ[X]` at `X + C b` produces the Taylor-shifted polynomial`P.evalPoly.comp (X + C b)`. -/theorem slp_evalIn_taylor {τ : ℕ} (P : SLP τ) (b : ℤ) :P.evalIn (Polynomial.X + Polynomial.C b) =P.evalPoly.comp (Polynomial.X + Polynomial.C b) := bylet φ : Polynomial ℤ →+* Polynomial ℤ :=Polynomial.eval₂RingHom Polynomial.C (Polynomial.X + Polynomial.C b)have hX : φ Polynomial.X = Polynomial.X + Polynomial.C b := by simp [φ]have h := P.map_evalIn φ Polynomial.Xrw [P.evalIn_X, hX] at hrw [← h]rfl/-! ## 6. Exact-Equality-by-Evaluation & Hash Mismatch Soundness -//-- **Hash Mismatch Soundness (`≠`):**Because `evalIn` commutes with ring homomorphisms, any discrepancy between `P₁.evalIn x`and `P₂.evalIn x` (or `-P₂.evalIn x`) in any commutative ring `R` proves `P₁.evalPoly ≠ P₂.evalPoly`(respectively `≠ -P₂.evalPoly`). -/theorem slp_evalPoly_ne_of_evalIn_ne {R : Type*} [CommRing R] (x : R){τ₁ τ₂ : ℕ} (P₁ : SLP τ₁) (P₂ : SLP τ₂) (hne : P₁.evalIn x ≠ P₂.evalIn x) :P₁.evalPoly ≠ P₂.evalPoly := byintro heqapply hnerw [P₁.evalIn_eq_eval₂ x, P₂.evalIn_eq_eval₂ x, heq]theorem slp_evalPoly_ne_neg_of_evalIn_ne {R : Type*} [CommRing R] (x : R){τ₁ τ₂ : ℕ} (P₁ : SLP τ₁) (P₂ : SLP τ₂) (hne : P₁.evalIn x ≠ -P₂.evalIn x) :P₁.evalPoly ≠ -P₂.evalPoly := byintro heqapply hnerw [P₁.evalIn_eq_eval₂ x, P₂.evalIn_eq_eval₂ x, heq, Polynomial.map_neg, Polynomial.eval_neg]/-- **Single-Prime Evaluation Vanishing Lemma:**If `h ∈ ℤ[X]` has `natDegree ≤ D` and reduces to `0` at `D + 1` distinct residuesmodulo a prime `q`, then `h ≡ 0 (mod q)` (every coefficient of `h` is divisible by `q`). -/theorem poly_map_zmod_eq_zero_of_roots (q : ℕ) [Fact q.Prime] (h : Polynomial ℤ) (D : ℕ)(hdeg : h.natDegree ≤ D) (T : Finset (ZMod q)) (hT : D + 1 ≤ T.card)(hvan : ∀ t ∈ T, (h.map (Int.castRingHom (ZMod q))).eval t = 0) :h.map (Int.castRingHom (ZMod q)) = 0 := byset hq := h.map (Int.castRingHom (ZMod q))by_contra hnehave hsub : T ⊆ hq.roots.toFinset := byintro t htrw [Multiset.mem_toFinset, Polynomial.mem_roots hne]exact hvan t hthave hcard : D + 1 ≤ hq.natDegree := bycalcD + 1 ≤ T.card := hT_ ≤ hq.roots.toFinset.card := Finset.card_le_card hsub_ ≤ hq.roots.card := Multiset.toFinset_card_le __ ≤ hq.natDegree := Polynomial.card_roots' hqhave hdeg_le : hq.natDegree ≤ D := (Polynomial.natDegree_map_le).trans hdegomega/-- Helper: if a finite set of distinct primes each divide `c : ℤ`, then their product divides `c`. -/private lemma prod_primes_dvd_of_forall_dvd (Q : Finset ℕ) (hQ : ∀ q ∈ Q, Nat.Prime q)(c : ℤ) (hdvd : ∀ q ∈ Q, (q : ℤ) ∣ c) :((∏ q ∈ Q, q : ℕ) : ℤ) ∣ c := byclassicalinduction Q using Finset.induction_on with| empty => simp| @insert q s hqs ih =>have hq_prime : Nat.Prime q := hQ q (Finset.mem_insert_self q s)have hs_prime : ∀ r ∈ s, Nat.Prime r := fun r hr => hQ r (Finset.mem_insert_of_mem hr)have hs_dvd : ((∏ r ∈ s, r : ℕ) : ℤ) ∣ c :=ih hs_prime (fun r hr => hdvd r (Finset.mem_insert_of_mem hr))have hq_dvd : (q : ℤ) ∣ c := hdvd q (Finset.mem_insert_self q s)have hcop_aux : ∀ t : Finset ℕ, q ∉ t → (∀ r ∈ t, Nat.Prime r) → Nat.Coprime q (∏ r ∈ t, r) := byintro tinduction t using Finset.induction_on with| empty => intro _ _; simp| @insert r t hrt ihr =>intro hq_not ht_primerw [Finset.prod_insert hrt]have hr_ne : q ≠ r := fun h => hq_not (h ▸ Finset.mem_insert_self r t)have hr_prime : Nat.Prime r := ht_prime r (Finset.mem_insert_self r t)have hqr : Nat.Coprime q r :=(hq_prime.coprime_iff_not_dvd).mpr (fun h =>hr_ne ((Nat.prime_dvd_prime_iff_eq hq_prime hr_prime).mp h))have hqt : Nat.Coprime q (∏ x ∈ t, x) :=ihr (fun h => hq_not (Finset.mem_insert_of_mem h))(fun x hx => ht_prime x (Finset.mem_insert_of_mem hx))exact hqr.mul_right hqthave hcop : Nat.Coprime q (∏ r ∈ s, r) := hcop_aux s hqs hs_primehave hcopZ : IsCoprime (q : ℤ) ((∏ r ∈ s, r : ℕ) : ℤ) := hcop.isCoprimerw [Finset.prod_insert hqs, Nat.cast_mul]exact hcopZ.mul_dvd hq_dvd hs_dvd/-- **CRT Exact-Zero Certificate by Evaluation:**If `h ∈ ℤ[X]` has `natDegree ≤ D`, vanishes at `D + 1` distinct residues modulo each prime`q ∈ Q`, and every coefficient satisfies `2 * |h.coeff n| < ∏ q ∈ Q, q`, then `h = 0`. -/theorem poly_eq_zero_of_crt_eval (Q : Finset ℕ) (hQ : ∀ q ∈ Q, Nat.Prime q)(h : Polynomial ℤ) (D : ℕ) (hdeg : h.natDegree ≤ D)(hvan : ∀ q ∈ Q, ∃ T : Finset (ZMod q), D + 1 ≤ T.card ∧∀ t ∈ T, (h.map (Int.castRingHom (ZMod q))).eval t = 0)(hsmall : ∀ n ≤ D, 2 * (h.coeff n).natAbs < ∏ q ∈ Q, q) :h = 0 := byhave hmod : ∀ q ∈ Q, h.map (Int.castRingHom (ZMod q)) = 0 := byintro q hqhave : Fact q.Prime := ⟨hQ q hq⟩rcases hvan q hq with ⟨T, hT, ht⟩exact poly_map_zmod_eq_zero_of_roots q h D hdeg T hT htext nsimp only [Polynomial.coeff_zero]by_cases hn : n ≤ D· have hdvd_all : ∀ q ∈ Q, (q : ℤ) ∣ h.coeff n := byintro q hqhave hq0 := congrArg (fun p : Polynomial (ZMod q) => p.coeff n) (hmod q hq)simp only [Polynomial.coeff_map, Polynomial.coeff_zero, Int.coe_castRingHom] at hq0exact (ZMod.intCast_zmod_eq_zero_iff_dvd (h.coeff n) q).mp hq0have hprod_dvd : ((∏ q ∈ Q, q : ℕ) : ℤ) ∣ h.coeff n :=prod_primes_dvd_of_forall_dvd Q hQ (h.coeff n) hdvd_allhave hnat_dvd : (∏ q ∈ Q, q) ∣ (h.coeff n).natAbs :=Int.natCast_dvd.mp hprod_dvdhave hbound := hsmall n hnby_contra hnehave hpos : 0 < (h.coeff n).natAbs := Int.natAbs_pos.mpr hnehave hle : ∏ q ∈ Q, q ≤ (h.coeff n).natAbs := Nat.le_of_dvd hpos hnat_dvdomega· exact Polynomial.coeff_eq_zero_of_natDegree_lt (by omega)end Research.PvsNP.Tauend