Lean / 0xOsprey/shub-smale-tau9 / CertSoundness.lean
Lean reader

Research/PvsNP/Tau/CertSoundness.lean

984 lines77 declarationsno sorry
  1. module
  2. public import Research.PvsNP.Tau.Canonical
  3. public import Research.PvsNP.Tau.ModularRootBound
  4. public import Mathlib.Algebra.Polynomial.Taylor
  5. @[expose] public section
  6. namespace Research.PvsNP.Tau
  7. /-!
  8. # Soundness of Per-Leaf Root Certificates and Evaluation-Based Deduplication
  9. This module formalizes the evaluation-based soundness of the leaf certificates and
  10. hash/CRT checks used by the exhaustive $\tau$-conjecture verifier:
  11. 1. Generic commutative-ring SLP evaluation (`evalIn`, `outputIn`) commuting with ring
  12. homomorphisms.
  13. 2. Degree-bound propagation (`degUb`) and `intRootCount f ≤ degUb`.
  14. 3. Atomic product-factor bitmask bound (`prodMask`) including `intRootCount (A + A) = intRootCount A`,
  15. constant elimination, and union bounds over atomic factors.
  16. 4. Dual-number arithmetic (`Dual R`) evaluating `(f(a), f'(a)) mod p` in one pass,
  17. and the SLP-level simple-root modular certificate.
  18. 5. Exact Taylor-residue certificate (`taylor_root_eq_of_factor`, `intRootCount_le_of_taylor_zmod`)
  19. handling repeated small integer roots.
  20. 6. Soundness of hash-based inequality (`≠`) conclusions and CRT exact-equality-by-evaluation.
  21. -/
  22. /-! ## 1. Generic Ring Evaluation and Homomorphism Commutation -/
  23. /-- Apply an `SLPOp` in any ring `R`. -/
  24. def SLPOp.applyRing {R : Type*} [Ring R] : SLPOp → R → R → R
  25. | .add, a, b => a + b
  26. | .sub, a, b => a - b
  27. | .mul, a, b => a * b
  28. theorem SLPOp.map_applyRing {R S : Type*} [Ring R] [Ring S]
  29. (φ : R →+* S) (op : SLPOp) (a b : R) :
  30. φ (op.applyRing a b) = op.applyRing (φ a) (φ b) := by
  31. cases op <;> simp [SLPOp.applyRing]
  32. /-- Evaluate wire `k : Fin (τ + 2)` of `P : SLP τ` in an arbitrary ring `R`
  33. with indeterminate `X` instantiated to `x : R`. -/
  34. def SLP.evalWireIn {R : Type*} [Ring R] (x : R) : {τ : ℕ} → SLP τ → Fin (τ + 2) → R
  35. | 0, .nil, k => if k.val = 0 then 1 else x
  36. | τ + 1, .snoc P op i j, k =>
  37. if h : k.val < τ + 2 then
  38. P.evalWireIn x ⟨k.val, h⟩
  39. else
  40. op.applyRing (P.evalWireIn x i) (P.evalWireIn x j)
  41. /-- Evaluate the output wire of `P : SLP τ` in any ring `R` at `x : R`. -/
  42. def SLP.evalIn {R : Type*} [Ring R] (x : R) {τ : ℕ} (P : SLP τ) : R :=
  43. P.evalWireIn x ⟨τ + 1, Nat.lt_succ_self _⟩
  44. theorem SLP.evalWireIn_X :
  45. ∀ {τ : ℕ} (P : SLP τ) (k : Fin (τ + 2)),
  46. P.evalWireIn (Polynomial.X : Polynomial ℤ) k = P.evalWire k
  47. | 0, .nil, k => by
  48. simp only [SLP.evalWireIn, SLP.evalWire]
  49. | τ + 1, .snoc P op i j, k => by
  50. simp only [SLP.evalWireIn, SLP.evalWire]
  51. split_ifs with h
  52. · exact P.evalWireIn_X ⟨k.val, h⟩
  53. · rw [P.evalWireIn_X i, P.evalWireIn_X j]
  54. cases op <;> rfl
  55. theorem SLP.evalIn_X {τ : ℕ} (P : SLP τ) :
  56. P.evalIn (Polynomial.X : Polynomial ℤ) = P.evalPoly :=
  57. P.evalWireIn_X ⟨τ + 1, Nat.lt_succ_self _⟩
  58. theorem SLP.map_evalWireIn {R S : Type*} [Ring R] [Ring S]
  59. (φ : R →+* S) (x : R) :
  60. ∀ {τ : ℕ} (P : SLP τ) (k : Fin (τ + 2)),
  61. φ (P.evalWireIn x k) = P.evalWireIn (φ x) k
  62. | 0, .nil, k => by
  63. simp only [SLP.evalWireIn]
  64. split_ifs <;> simp
  65. | τ + 1, .snoc P op i j, k => by
  66. simp only [SLP.evalWireIn]
  67. split_ifs with h
  68. · exact P.map_evalWireIn φ x ⟨k.val, h⟩
  69. · rw [SLPOp.map_applyRing, P.map_evalWireIn φ x i, P.map_evalWireIn φ x j]
  70. theorem SLP.map_evalIn {R S : Type*} [Ring R] [Ring S]
  71. (φ : R →+* S) (x : R) {τ : ℕ} (P : SLP τ) :
  72. φ (P.evalIn x) = P.evalIn (φ x) :=
  73. P.map_evalWireIn φ x ⟨τ + 1, Nat.lt_succ_self _⟩
  74. theorem SLP.evalIn_eq_eval₂ {R : Type*} [CommRing R] (x : R) {τ : ℕ} (P : SLP τ) :
  75. P.evalIn x = (P.evalPoly.map (Int.castRingHom R)).eval x := by
  76. let φ : Polynomial ℤ →+* R := Polynomial.eval₂RingHom (Int.castRingHom R) x
  77. have hφ : φ Polynomial.X = x := by simp [φ]
  78. have h := P.map_evalIn φ Polynomial.X
  79. rw [P.evalIn_X, hφ] at h
  80. simpa [φ, Polynomial.eval_map] using h.symm
  81. theorem SLP.evalIn_int_eq_eval {τ : ℕ} (P : SLP τ) (a : ℤ) :
  82. P.evalIn a = P.evalPoly.eval a := by
  83. simpa using P.evalIn_eq_eval₂ a
  84. /-- Accumulator evaluator for `List GateSpec` over any ring `R`. -/
  85. def wireValsInAux {R : Type*} [Ring R] : List R → List GateSpec → List R
  86. | acc, [] => acc
  87. | acc, g :: rest =>
  88. wireValsInAux (acc ++ [g.op.applyRing (acc.getD g.i 0) (acc.getD g.j 0)]) rest
  89. /-- Wire values of a gate list `gs` evaluated in a ring `R` at `x : R`. -/
  90. def wireValsIn {R : Type*} [Ring R] (x : R) (gs : List GateSpec) : List R :=
  91. wireValsInAux [1, x] gs
  92. /-- Output value of a gate list `gs` evaluated in a ring `R` at `x : R`. -/
  93. def outputIn {R : Type*} [Ring R] (x : R) (gs : List GateSpec) : R :=
  94. (wireValsIn x gs).getD (gs.length + 1) 0
  95. private lemma list_getD_map {R S : Type*} [Ring R] [Ring S] (φ : R →+* S)
  96. (l : List R) (i : ℕ) : (l.map φ).getD i 0 = φ (l.getD i 0) := by
  97. by_cases hi : i < l.length
  98. · simp [List.getD, hi]
  99. · simp [List.getD, hi]
  100. theorem wireValsInAux_length {R : Type*} [Ring R] (acc : List R) (gs : List GateSpec) :
  101. (wireValsInAux acc gs).length = acc.length + gs.length := by
  102. induction gs generalizing acc with
  103. | nil => simp [wireValsInAux]
  104. | cons g rest ih =>
  105. simp only [wireValsInAux]
  106. rw [ih]
  107. simp
  108. omega
  109. theorem wireValsIn_length {R : Type*} [Ring R] (x : R) (gs : List GateSpec) :
  110. (wireValsIn x gs).length = gs.length + 2 := by
  111. rw [wireValsIn, wireValsInAux_length]
  112. simp
  113. omega
  114. theorem wireValsIn_X (gs : List GateSpec) :
  115. wireValsIn (Polynomial.X : Polynomial ℤ) gs = wireVals gs := by
  116. have haux : ∀ (acc : List (Polynomial ℤ)) (gs : List GateSpec),
  117. wireValsInAux acc gs = wireValsAux acc gs := by
  118. intro acc gs
  119. induction gs generalizing acc with
  120. | nil => rfl
  121. | cons g rest ih =>
  122. simp only [wireValsInAux, wireValsAux]
  123. have hop : g.op.applyRing (acc.getD g.i 0) (acc.getD g.j 0) =
  124. g.op.applyPoly (acc.getD g.i 0) (acc.getD g.j 0) := by
  125. cases g.op <;> rfl
  126. rw [hop, ih]
  127. exact haux [1, Polynomial.X] gs
  128. theorem outputIn_X (gs : List GateSpec) :
  129. outputIn (Polynomial.X : Polynomial ℤ) gs = output gs := by
  130. simp [outputIn, output, wireValsIn_X]
  131. theorem wireValsIn_map {R S : Type*} [Ring R] [Ring S] (φ : R →+* S)
  132. (x : R) (gs : List GateSpec) :
  133. (wireValsIn x gs).map φ = wireValsIn (φ x) gs := by
  134. have haux : ∀ (acc : List R) (gs : List GateSpec),
  135. (wireValsInAux acc gs).map φ = wireValsInAux (acc.map φ) gs := by
  136. intro acc gs
  137. induction gs generalizing acc with
  138. | nil => rfl
  139. | cons g rest ih =>
  140. simp only [wireValsInAux, ih, List.map_append, List.map_singleton,
  141. SLPOp.map_applyRing, list_getD_map]
  142. simpa [wireValsIn] using haux [1, x] gs
  143. theorem outputIn_map {R S : Type*} [Ring R] [Ring S] (φ : R →+* S)
  144. (x : R) (gs : List GateSpec) :
  145. φ (outputIn x gs) = outputIn (φ x) gs := by
  146. unfold outputIn
  147. rw [← wireValsIn_map φ x gs, list_getD_map]
  148. theorem outputIn_eq_eval₂ {R : Type*} [CommRing R] (x : R) (gs : List GateSpec) :
  149. outputIn x gs = ((output gs).map (Int.castRingHom R)).eval x := by
  150. let φ : Polynomial ℤ →+* R := Polynomial.eval₂RingHom (Int.castRingHom R) x
  151. have hφ : φ Polynomial.X = x := by simp [φ]
  152. have h := outputIn_map φ Polynomial.X gs
  153. rw [outputIn_X, hφ] at h
  154. simpa [φ, Polynomial.eval_map] using h.symm
  155. /-! ## 2. Degree-Bound Propagation -/
  156. /-- Wire degree upper bound recurrence for `SLP`:
  157. `1 ↦ 0`, `X ↦ 1`, `a ± b ↦ max`, `a * b ↦ sum`. -/
  158. def SLP.wireDegUb : {τ : ℕ} → SLP τ → Fin (τ + 2) → ℕ
  159. | 0, .nil, k => if k.val = 0 then 0 else 1
  160. | τ + 1, .snoc P op i j, k =>
  161. if h : k.val < τ + 2 then
  162. P.wireDegUb ⟨k.val, h⟩
  163. else
  164. match op with
  165. | .add | .sub => max (P.wireDegUb i) (P.wireDegUb j)
  166. | .mul => P.wireDegUb i + P.wireDegUb j
  167. /-- Output degree upper bound of `P : SLP τ`. -/
  168. def SLP.degUb {τ : ℕ} (P : SLP τ) : ℕ :=
  169. P.wireDegUb ⟨τ + 1, Nat.lt_succ_self _⟩
  170. private lemma wireDegUb_snoc {τ : ℕ} (P : SLP τ) (op : SLPOp) (i j : Fin (τ + 2))
  171. (k : Fin (τ + 1 + 2)) :
  172. (P.snoc op i j).wireDegUb k =
  173. if h : k.val < τ + 2 then
  174. P.wireDegUb ⟨k.val, h⟩
  175. else
  176. match op with
  177. | .add | .sub => max (P.wireDegUb i) (P.wireDegUb j)
  178. | .mul => P.wireDegUb i + P.wireDegUb j := rfl
  179. theorem slp_natDegree_le_wireDegUb :
  180. ∀ {τ : ℕ} (P : SLP τ) (k : Fin (τ + 2)),
  181. (P.evalWire k).natDegree ≤ P.wireDegUb k
  182. | 0, .nil, k => by
  183. simp only [SLP.evalWire, SLP.wireDegUb]
  184. split_ifs <;> simp
  185. | τ + 1, .snoc P op i j, k => by
  186. rw [wireDegUb_snoc]
  187. by_cases h : k.val < τ + 2
  188. · simp only [SLP.evalWire, h, ↓reduceDIte]
  189. exact slp_natDegree_le_wireDegUb P ⟨k.val, h⟩
  190. · simp only [SLP.evalWire, h, ↓reduceDIte]
  191. have ihi := slp_natDegree_le_wireDegUb P i
  192. have ihj := slp_natDegree_le_wireDegUb P j
  193. cases op with
  194. | add => exact (Polynomial.natDegree_add_le _ _).trans (max_le_max ihi ihj)
  195. | sub => exact (Polynomial.natDegree_sub_le _ _).trans (max_le_max ihi ihj)
  196. | mul => exact Polynomial.natDegree_mul_le.trans (Nat.add_le_add ihi ihj)
  197. theorem slp_natDegree_le_degUb {τ : ℕ} (P : SLP τ) :
  198. P.evalPoly.natDegree ≤ P.degUb :=
  199. slp_natDegree_le_wireDegUb P ⟨τ + 1, Nat.lt_succ_self _⟩
  200. /-- Degree upper bound root certificate for `SLP`. -/
  201. theorem slp_intRootCount_le_degUb {τ : ℕ} (P : SLP τ) (hne : P.evalPoly ≠ 0) :
  202. intRootCount P.evalPoly ≤ P.degUb :=
  203. (intRootCount_le_natDegree hne).trans (slp_natDegree_le_degUb P)
  204. /-- Degree upper bound accumulator for `List GateSpec`. -/
  205. def wireDegUbsAux : List ℕ → List GateSpec → List ℕ
  206. | acc, [] => acc
  207. | acc, g :: rest =>
  208. let d :=
  209. match g.op with
  210. | .add | .sub => max (acc.getD g.i 0) (acc.getD g.j 0)
  211. | .mul => acc.getD g.i 0 + acc.getD g.j 0
  212. wireDegUbsAux (acc ++ [d]) rest
  213. /-- Wire degree upper bounds for `gs : List GateSpec`. -/
  214. def wireDegUbs (gs : List GateSpec) : List ℕ :=
  215. wireDegUbsAux [0, 1] gs
  216. /-- Output degree upper bound for `gs : List GateSpec`. -/
  217. def degUb (gs : List GateSpec) : ℕ :=
  218. (wireDegUbs gs).getD (gs.length + 1) 0
  219. theorem wireVals_natDegree_le_wireDegUbs (gs : List GateSpec) :
  220. ∀ w : ℕ, ((wireVals gs).getD w 0).natDegree ≤ (wireDegUbs gs).getD w 0 := by
  221. have haux : ∀ (accP : List (Polynomial ℤ)) (accD : List ℕ) (gs : List GateSpec),
  222. accP.length = accD.length →
  223. (∀ w, (accP.getD w 0).natDegree ≤ accD.getD w 0) →
  224. ∀ w, ((wireValsAux accP gs).getD w 0).natDegree ≤ (wireDegUbsAux accD gs).getD w 0 := by
  225. intro accP accD gs
  226. induction gs generalizing accP accD with
  227. | nil =>
  228. intro _ h w
  229. exact h w
  230. | cons g rest ih =>
  231. intro hlen h w
  232. simp only [wireValsAux, wireDegUbsAux]
  233. refine ih _ _ (by simp [hlen]) ?_ w
  234. intro u
  235. by_cases hu : u < accP.length
  236. · have huD : u < accD.length := by omega
  237. simp only [List.getD, List.getElem?_append_left hu, List.getElem?_append_left huD]
  238. exact h u
  239. · have huD : accD.length ≤ u := by omega
  240. have huP : accP.length ≤ u := by omega
  241. by_cases heq : u = accP.length
  242. · subst heq
  243. simp only [List.getD, List.getElem?_append_right (le_refl _),
  244. List.getElem?_append_right hlen.le, hlen, Nat.sub_self,
  245. List.getElem?_cons_zero, Option.getD_some]
  246. have hi := h g.i
  247. have hj := h g.j
  248. cases g.op with
  249. | add => exact (Polynomial.natDegree_add_le _ _).trans (max_le_max hi hj)
  250. | sub => exact (Polynomial.natDegree_sub_le _ _).trans (max_le_max hi hj)
  251. | mul => exact Polynomial.natDegree_mul_le.trans (Nat.add_le_add hi hj)
  252. · have h1 : (accP ++ [g.op.applyPoly (accP.getD g.i 0) (accP.getD g.j 0)]).length ≤ u := by
  253. simp; omega
  254. rw [List.getD, List.getElem?_eq_none h1]
  255. simp
  256. refine haux [1, Polynomial.X] [0, 1] gs rfl ?_
  257. intro w
  258. rcases w with _ | _ | w <;> simp [List.getD]
  259. theorem output_natDegree_le_degUb (gs : List GateSpec) :
  260. (output gs).natDegree ≤ degUb gs :=
  261. wireVals_natDegree_le_wireDegUbs gs (gs.length + 1)
  262. theorem output_intRootCount_le_degUb (gs : List GateSpec) (hne : output gs ≠ 0) :
  263. intRootCount (output gs) ≤ degUb gs :=
  264. (intRootCount_le_natDegree hne).trans (output_natDegree_le_degUb gs)
  265. /-! ## 3. Atomic Product-Mask Bound -/
  266. /-- Doubling a polynomial preserves its integer root set: `intRootSet (A + A) = intRootSet A`. -/
  267. theorem intRootSet_add_self (A : Polynomial ℤ) :
  268. intRootSet (A + A) = intRootSet A := by
  269. ext r
  270. simp only [mem_intRootSet_iff, Polynomial.IsRoot, Polynomial.eval_add]
  271. have h0 : A + A = 0 ↔ A = 0 := by
  272. rw [← two_mul]
  273. exact mul_eq_zero.trans (by simp)
  274. constructor
  275. · rintro ⟨hne, hr⟩
  276. exact ⟨fun h => hne (h0.mpr h), by omega⟩
  277. · rintro ⟨hne, hr⟩
  278. exact ⟨fun h => hne (h0.mp h), by omega⟩
  279. /-- `intRootCount (A + A) = intRootCount A`. -/
  280. theorem intRootCount_add_self (A : Polynomial ℤ) :
  281. intRootCount (A + A) = intRootCount A := by
  282. simp [intRootCount, intRootSet_add_self]
  283. /-- Nonzero constant polynomials in `ℤ[X]` have `0` integer roots. -/
  284. theorem intRootCount_C (c : ℤ) (hc : c ≠ 0) :
  285. intRootCount (Polynomial.C c) = 0 :=
  286. Nat.eq_zero_of_le_zero ((intRootCount_le_natDegree (Polynomial.C_ne_zero.mpr hc)).trans (by simp))
  287. /-- Generalized union bound: if every integer root of `f` is a root of at least one nonzero
  288. atomic factor `w a` (`a ∈ M`) with `intRootCount (w a) ≤ ub a`, then
  289. `intRootCount f ≤ ∑ a ∈ M, ub a`. -/
  290. theorem intRootCount_le_sum_of_rootSet_subset_biUnion
  291. {f : Polynomial ℤ} {M : Finset ℕ} {w : ℕ → Polynomial ℤ} {ub : ℕ → ℕ}
  292. (hsub : intRootSet f ⊆ M.biUnion (fun a => intRootSet (w a)))
  293. (hub : ∀ a ∈ M, intRootCount (w a) ≤ ub a) :
  294. intRootCount f ≤ ∑ a ∈ M, ub a := by
  295. calc
  296. intRootCount f = (intRootSet f).card := rfl
  297. _ ≤ (M.biUnion (fun a => intRootSet (w a))).card := Finset.card_le_card hsub
  298. _ ≤ ∑ a ∈ M, (intRootSet (w a)).card := Finset.card_biUnion_le
  299. _ = ∑ a ∈ M, intRootCount (w a) := rfl
  300. _ ≤ ∑ a ∈ M, ub a := Finset.sum_le_sum hub
  301. /-- Atomic product-factor bitmask (`Finset ℕ`) matching `prod_mask` in `tau_z9_unconditional.c`. -/
  302. def SLP.wireProdMask : {τ : ℕ} → SLP τ → Fin (τ + 2) → Finset ℕ
  303. | 0, .nil, k => if k.val = 0 then ∅ else {1}
  304. | τ + 1, .snoc P op i j, k =>
  305. if h : k.val < τ + 2 then
  306. P.wireProdMask ⟨k.val, h⟩
  307. else
  308. if (P.snoc op i j).degUb = 0 then
  309. ∅
  310. else
  311. match op with
  312. | .mul => P.wireProdMask i ∪ P.wireProdMask j
  313. | .add => if i = j then P.wireProdMask i else {τ + 2}
  314. | .sub => {τ + 2}
  315. /-- Output product-factor mask of `P : SLP τ`. -/
  316. def SLP.prodMask {τ : ℕ} (P : SLP τ) : Finset ℕ :=
  317. P.wireProdMask ⟨τ + 1, Nat.lt_succ_self _⟩
  318. private lemma wireProdMask_snoc {τ : ℕ} (P : SLP τ) (op : SLPOp) (i j : Fin (τ + 2))
  319. (k : Fin (τ + 1 + 2)) :
  320. (P.snoc op i j).wireProdMask k =
  321. if h : k.val < τ + 2 then P.wireProdMask ⟨k.val, h⟩
  322. else if (P.snoc op i j).degUb = 0 then ∅
  323. else match op with
  324. | .mul => P.wireProdMask i ∪ P.wireProdMask j
  325. | .add => if i = j then P.wireProdMask i else {τ + 2}
  326. | .sub => {τ + 2} := rfl
  327. theorem slp_wireProdMask_bound {τ : ℕ} (P : SLP τ) :
  328. ∀ (k : Fin (τ + 2)),
  329. (∀ a ∈ P.wireProdMask k, a < τ + 2) ∧
  330. (P.evalWire k ≠ 0 →
  331. intRootSet (P.evalWire k) ⊆
  332. (P.wireProdMask k).biUnion (fun a =>
  333. if ha : a < τ + 2 then intRootSet (P.evalWire ⟨a, ha⟩) else ∅)) := by
  334. induction P with
  335. | nil =>
  336. intro k
  337. simp only [SLP.wireProdMask, SLP.evalWire]
  338. rcases k with ⟨kv, hkv⟩
  339. rcases kv with _ | _ | kv
  340. · simp [intRootSet]
  341. · refine ⟨by simp, fun _ => ?_⟩
  342. intro r hr
  343. rw [Finset.mem_biUnion]
  344. exact ⟨1, Finset.mem_singleton_self 1, by simpa⟩
  345. · omega
  346. | @snoc τ P op i j ih =>
  347. intro k
  348. have lift_mem : ∀ (a : ℕ) (ha : a < τ + 2) (r : ℤ),
  349. r ∈ intRootSet (P.evalWire ⟨a, ha⟩) →
  350. r ∈ (if ha' : a < τ + 1 + 2 then intRootSet ((P.snoc op i j).evalWire ⟨a, ha'⟩) else ∅) := by
  351. intro a ha r hr
  352. rw [dite_eq_left (ha.trans (Nat.lt_succ_self _))]
  353. simpa only [SLP.evalWire, ha, ↓reduceDIte] using hr
  354. rw [wireProdMask_snoc]
  355. by_cases h : k.val < τ + 2
  356. · simp only [SLP.evalWire, h, ↓reduceDIte]
  357. rcases ih ⟨k.val, h⟩ with ⟨ha_lt, hsub⟩
  358. refine ⟨fun a ha => (ha_lt a ha).trans (Nat.lt_succ_self _), fun hne => ?_⟩
  359. refine (hsub hne).trans ?_
  360. intro r hr
  361. rcases Finset.mem_biUnion.mp hr with ⟨a, ha, hr_a⟩
  362. have ha_lt' : a < τ + 2 := ha_lt a ha
  363. rw [dite_eq_left ha_lt'] at hr_a
  364. rw [Finset.mem_biUnion]
  365. exact ⟨a, ha, lift_mem a ha_lt' r hr_a⟩
  366. · simp only [SLP.evalWire, h, ↓reduceDIte]
  367. by_cases hdeg0 : (P.snoc op i j).degUb = 0
  368. · simp only [hdeg0, ↓reduceIte]
  369. refine ⟨by simp, fun hne => ?_⟩
  370. have hdeg_poly : ((P.snoc op i j).evalPoly).natDegree = 0 := by
  371. have hle := slp_natDegree_le_degUb (P.snoc op i j)
  372. omega
  373. have hpoly_eq : op.applyPoly (P.evalWire i) (P.evalWire j) = (P.snoc op i j).evalPoly := by
  374. simp only [SLP.evalPoly, SLP.evalWire, Nat.lt_irrefl, ↓reduceDIte]
  375. have hcard0 : (intRootSet (op.applyPoly (P.evalWire i) (P.evalWire j))).card = 0 := by
  376. rw [hpoly_eq]
  377. exact Nat.eq_zero_of_le_zero ((intRootCount_le_natDegree (by rwa [← hpoly_eq])).trans hdeg_poly.le)
  378. rw [Finset.card_eq_zero.mp hcard0]
  379. simp
  380. · simp only [hdeg0, ↓reduceIte]
  381. rcases ih i with ⟨hi_lt, hi_sub⟩
  382. rcases ih j with ⟨hj_lt, hj_sub⟩
  383. cases op
  384. · -- add
  385. by_cases hij : i = j
  386. · subst hij
  387. simp only [↓reduceIte]
  388. refine ⟨fun a ha => (hi_lt a ha).trans (Nat.lt_succ_self _), fun hne => ?_⟩
  389. have hne_i : P.evalWire i ≠ 0 := by
  390. intro h0
  391. apply hne
  392. simp [SLPOp.applyPoly, h0]
  393. have hrw : SLPOp.add.applyPoly (P.evalWire i) (P.evalWire i) =
  394. P.evalWire i + P.evalWire i := rfl
  395. rw [hrw, intRootSet_add_self]
  396. refine (hi_sub hne_i).trans ?_
  397. intro r hr
  398. rcases Finset.mem_biUnion.mp hr with ⟨a, ha, hr_a⟩
  399. have ha_lt' := hi_lt a ha
  400. rw [dite_eq_left ha_lt'] at hr_a
  401. rw [Finset.mem_biUnion]
  402. exact ⟨a, ha, lift_mem a ha_lt' r hr_a⟩
  403. · simp only [hij, ↓reduceIte]
  404. refine ⟨by simp, fun _ r hr => ?_⟩
  405. rw [Finset.mem_biUnion]
  406. refine ⟨τ + 2, Finset.mem_singleton_self _, ?_⟩
  407. rw [dite_eq_left (Nat.lt_succ_self _)]
  408. simpa only [SLP.evalWire, Nat.lt_irrefl, ↓reduceDIte] using hr
  409. · -- sub
  410. refine ⟨by simp, fun _ r hr => ?_⟩
  411. rw [Finset.mem_biUnion]
  412. refine ⟨τ + 2, Finset.mem_singleton_self _, ?_⟩
  413. rw [dite_eq_left (Nat.lt_succ_self _)]
  414. simpa only [SLP.evalWire, Nat.lt_irrefl, ↓reduceDIte] using hr
  415. · -- mul
  416. refine ⟨fun a ha => ?_, fun hne => ?_⟩
  417. · rcases Finset.mem_union.mp ha with ha | ha
  418. · exact (hi_lt a ha).trans (Nat.lt_succ_self _)
  419. · exact (hj_lt a ha).trans (Nat.lt_succ_self _)
  420. · have hmul : P.evalWire i * P.evalWire j ≠ 0 := hne
  421. have hi_ne : P.evalWire i ≠ 0 := left_ne_zero_of_mul hmul
  422. have hj_ne : P.evalWire j ≠ 0 := right_ne_zero_of_mul hmul
  423. have hsub_mul : intRootSet (P.evalWire i * P.evalWire j) ⊆
  424. intRootSet (P.evalWire i) ∪ intRootSet (P.evalWire j) := by
  425. intro r hr
  426. rw [mem_intRootSet_iff] at hr
  427. rw [Finset.mem_union, mem_intRootSet_iff, mem_intRootSet_iff]
  428. have hm : (P.evalWire i).eval r * (P.evalWire j).eval r = 0 := by
  429. simpa [Polynomial.IsRoot, Polynomial.eval_mul] using hr.2
  430. rcases mul_eq_zero.mp hm with h1 | h2
  431. · exact Or.inl ⟨hi_ne, h1⟩
  432. · exact Or.inr ⟨hj_ne, h2⟩
  433. refine hsub_mul.trans ?_
  434. intro r hr
  435. rcases Finset.mem_union.mp hr with h1 | h2
  436. · rcases Finset.mem_biUnion.mp (hi_sub hi_ne h1) with ⟨a, ha, hr_a⟩
  437. have ha_lt' := hi_lt a ha
  438. rw [dite_eq_left ha_lt'] at hr_a
  439. rw [Finset.mem_biUnion]
  440. exact ⟨a, Finset.mem_union_left _ ha, lift_mem a ha_lt' r hr_a⟩
  441. · rcases Finset.mem_biUnion.mp (hj_sub hj_ne h2) with ⟨a, ha, hr_a⟩
  442. have ha_lt' := hj_lt a ha
  443. rw [dite_eq_left ha_lt'] at hr_a
  444. rw [Finset.mem_biUnion]
  445. exact ⟨a, Finset.mem_union_right _ ha, lift_mem a ha_lt' r hr_a⟩
  446. /-- **SLP Atomic Product-Factor Union Bound:**
  447. If `ub : Fin (τ + 2) → ℕ` bounds `intRootCount` of every nonzero wire of `P : SLP τ`,
  448. then `intRootCount P.evalPoly ≤ ∑ a ∈ P.prodMask, if ha : a < τ + 2 then ub ⟨a, ha⟩ else 0`. -/
  449. theorem slp_intRootCount_le_prodMask_sum {τ : ℕ} (P : SLP τ) (ub : Fin (τ + 2) → ℕ)
  450. (hub : ∀ k : Fin (τ + 2), P.evalWire k ≠ 0 → intRootCount (P.evalWire k) ≤ ub k)
  451. (hne : P.evalPoly ≠ 0) :
  452. intRootCount P.evalPoly ≤
  453. ∑ a ∈ P.prodMask, if ha : a < τ + 2 then ub ⟨a, ha⟩ else 0 := by
  454. rcases slp_wireProdMask_bound P ⟨τ + 1, Nat.lt_succ_self _⟩ with ⟨_, hsub⟩
  455. let w : ℕ → Polynomial ℤ := fun a => if ha : a < τ + 2 then P.evalWire ⟨a, ha⟩ else 0
  456. have hsub' : intRootSet P.evalPoly ⊆ P.prodMask.biUnion (fun a => intRootSet (w a)) := by
  457. refine (hsub hne).trans ?_
  458. intro r hr
  459. rcases Finset.mem_biUnion.mp hr with ⟨a, ha, hra⟩
  460. rw [Finset.mem_biUnion]
  461. refine ⟨a, ha, ?_⟩
  462. by_cases hlt : a < τ + 2
  463. · rw [dite_eq_left hlt] at hra
  464. simpa only [w, hlt, ↓reduceDIte] using hra
  465. · simp [hlt] at hra
  466. refine intRootCount_le_sum_of_rootSet_subset_biUnion (w := w) hsub' ?_
  467. intro a _
  468. by_cases ha : a < τ + 2
  469. · simp only [w, ha, ↓reduceDIte]
  470. by_cases hwa : P.evalWire ⟨a, ha⟩ = 0
  471. · simp [intRootCount, intRootSet, hwa]
  472. · exact hub ⟨a, ha⟩ hwa
  473. · simp [w, ha, intRootCount, intRootSet]
  474. /-! ## 4. Dual-Number Evaluation & SLP Simple-Root Modular Certificate -/
  475. /-- Dual numbers `R[ε]/(ε^2)` represented as pairs `(val, deriv)`. -/
  476. @[ext]
  477. structure Dual (R : Type*) where
  478. val : R
  479. deriv : R
  480. deriving DecidableEq, Repr
  481. namespace Dual
  482. section CommRing
  483. variable {R : Type*} [CommRing R]
  484. instance : Zero (Dual R) := ⟨⟨0, 0⟩⟩
  485. instance : One (Dual R) := ⟨⟨1, 0⟩⟩
  486. instance : Add (Dual R) := ⟨fun x y => ⟨x.val + y.val, x.deriv + y.deriv⟩⟩
  487. instance : Sub (Dual R) := ⟨fun x y => ⟨x.val - y.val, x.deriv - y.deriv⟩⟩
  488. instance : Neg (Dual R) := ⟨fun x => ⟨-x.val, -x.deriv⟩⟩
  489. instance : Mul (Dual R) := ⟨fun x y => ⟨x.val * y.val, x.deriv * y.val + x.val * y.deriv⟩⟩
  490. @[simp] lemma zero_val : (0 : Dual R).val = 0 := rfl
  491. @[simp] lemma zero_deriv : (0 : Dual R).deriv = 0 := rfl
  492. @[simp] lemma one_val : (1 : Dual R).val = 1 := rfl
  493. @[simp] lemma one_deriv : (1 : Dual R).deriv = 0 := rfl
  494. @[simp] lemma add_val (x y : Dual R) : (x + y).val = x.val + y.val := rfl
  495. @[simp] lemma add_deriv (x y : Dual R) : (x + y).deriv = x.deriv + y.deriv := rfl
  496. @[simp] lemma sub_val (x y : Dual R) : (x - y).val = x.val - y.val := rfl
  497. @[simp] lemma sub_deriv (x y : Dual R) : (x - y).deriv = x.deriv - y.deriv := rfl
  498. @[simp] lemma neg_val (x : Dual R) : (-x).val = -x.val := rfl
  499. @[simp] lemma neg_deriv (x : Dual R) : (-x).deriv = -x.deriv := rfl
  500. @[simp] lemma mul_val (x y : Dual R) : (x * y).val = x.val * y.val := rfl
  501. @[simp] lemma mul_deriv (x y : Dual R) : (x * y).deriv = x.deriv * y.val + x.val * y.deriv := rfl
  502. instance : CommRing (Dual R) where
  503. add := (· + ·)
  504. add_assoc a b c := by ext <;> dsimp <;> ring
  505. zero := 0
  506. zero_add a := by ext <;> dsimp <;> ring
  507. add_zero a := by ext <;> dsimp <;> ring
  508. nsmul := nsmulRec
  509. neg := (-·)
  510. sub := (· - ·)
  511. sub_eq_add_neg a b := by ext <;> dsimp <;> ring
  512. zsmul := zsmulRec
  513. neg_add_cancel a := by ext <;> dsimp <;> ring
  514. add_comm a b := by ext <;> dsimp <;> ring
  515. mul := (· * ·)
  516. left_distrib a b c := by ext <;> dsimp <;> ring
  517. right_distrib a b c := by ext <;> dsimp <;> ring
  518. zero_mul a := by ext <;> dsimp <;> ring
  519. mul_zero a := by ext <;> dsimp <;> ring
  520. mul_assoc a b c := by ext <;> dsimp <;> ring
  521. one := 1
  522. one_mul a := by ext <;> dsimp <;> ring
  523. mul_one a := by ext <;> dsimp <;> ring
  524. mul_comm a b := by ext <;> dsimp <;> ring
  525. end CommRing
  526. end Dual
  527. /-- Evaluation into `Dual R` at `a : R` (`f ↦ (f(a), f'(a))`) is a ring homomorphism. -/
  528. noncomputable def evalDualHom {R : Type*} [CommRing R] (a : R) : Polynomial R →+* Dual R where
  529. toFun f := ⟨f.eval a, f.derivative.eval a⟩
  530. map_one' := by ext <;> simp
  531. map_mul' f g := by ext <;> simp [Polynomial.derivative_mul]
  532. map_zero' := by ext <;> simp
  533. map_add' f g := by ext <;> simp
  534. /-- **Dual-Number Evaluation Theorem for `SLP`:**
  535. Evaluating `P : SLP τ` in `Dual (ZMod p)` at `⟨a, 1⟩` yields `(f_p(a), f_p'(a))`
  536. where `f_p = P.evalPoly.map (Int.castRingHom (ZMod p))`. -/
  537. theorem slp_evalIn_dual_zmod (p : ℕ) {τ : ℕ} (P : SLP τ) (a : ZMod p) :
  538. P.evalIn (⟨a, 1⟩ : Dual (ZMod p)) =
  539. ⟨(P.evalPoly.map (Int.castRingHom (ZMod p))).eval a,
  540. (P.evalPoly.map (Int.castRingHom (ZMod p))).derivative.eval a⟩ := by
  541. let φ : Polynomial ℤ →+* Dual (ZMod p) :=
  542. (evalDualHom a).comp (Polynomial.mapRingHom (Int.castRingHom (ZMod p)))
  543. have hX : φ Polynomial.X = ⟨a, 1⟩ := by
  544. ext <;> simp [φ, evalDualHom]
  545. have h := P.map_evalIn φ Polynomial.X
  546. rw [P.evalIn_X, hX] at h
  547. simpa [φ, evalDualHom] using h.symm
  548. /-- Dual-number evaluation theorem for `List GateSpec`. -/
  549. theorem outputIn_dual_zmod (p : ℕ) (gs : List GateSpec) (a : ZMod p) :
  550. outputIn (⟨a, 1⟩ : Dual (ZMod p)) gs =
  551. ⟨((output gs).map (Int.castRingHom (ZMod p))).eval a,
  552. ((output gs).map (Int.castRingHom (ZMod p))).derivative.eval a⟩ := by
  553. let φ : Polynomial ℤ →+* Dual (ZMod p) :=
  554. (evalDualHom a).comp (Polynomial.mapRingHom (Int.castRingHom (ZMod p)))
  555. have hX : φ Polynomial.X = ⟨a, 1⟩ := by
  556. ext <;> simp [φ, evalDualHom]
  557. have h := outputIn_map φ Polynomial.X gs
  558. rw [outputIn_X, hX] at h
  559. simpa [φ, evalDualHom] using h.symm
  560. /-- **SLP-Level Simple-Root Modular Certificate:**
  561. If `p` is prime, `K < p`, and dual-number evaluation of `P : SLP τ` over `ZMod p`
  562. shows at most `K` zeros and no double zeros (`val = 0 → deriv ≠ 0`), then
  563. `intRootCount P.evalPoly ≤ K` unconditionally over all of `ℤ`. -/
  564. theorem slp_intRootCount_le_of_dual_eval_zmod (p : ℕ) [Fact p.Prime] {τ : ℕ} (P : SLP τ) (K : ℕ)
  565. (hKp : K < p)
  566. (hsimple : ∀ a : ZMod p,
  567. (P.evalIn (⟨a, 1⟩ : Dual (ZMod p))).val = 0 →
  568. (P.evalIn (⟨a, 1⟩ : Dual (ZMod p))).deriv ≠ 0)
  569. (hbound : (Finset.univ.filter (fun a : ZMod p =>
  570. (P.evalIn (⟨a, 1⟩ : Dual (ZMod p))).val = 0)).card ≤ K) :
  571. intRootCount P.evalPoly ≤ K := by
  572. have heval : ∀ a : ZMod p,
  573. (P.evalPoly.map (Int.castRingHom (ZMod p))).eval a =
  574. (P.evalIn (⟨a, 1⟩ : Dual (ZMod p))).val := by
  575. intro a
  576. rw [slp_evalIn_dual_zmod]
  577. have hderiv_eq : ∀ a : ZMod p,
  578. (P.evalPoly.map (Int.castRingHom (ZMod p))).derivative.eval a =
  579. (P.evalIn (⟨a, 1⟩ : Dual (ZMod p))).deriv := by
  580. intro a
  581. rw [slp_evalIn_dual_zmod]
  582. let Z := Finset.univ.filter (fun a : ZMod p =>
  583. (P.evalPoly.map (Int.castRingHom (ZMod p))).eval a = 0)
  584. have hZ_card : Z.card ≤ K := by
  585. simpa [Z, heval] using hbound
  586. have hne_pt : ∃ a : ZMod p, (P.evalPoly.map (Int.castRingHom (ZMod p))).eval a ≠ 0 := by
  587. by_contra hnone
  588. push Not at hnone
  589. have huniv : Finset.univ ⊆ Z := fun a _ => Finset.mem_filter.mpr ⟨Finset.mem_univ a, hnone a⟩
  590. have hcard : p ≤ Z.card := by
  591. calc
  592. p = Fintype.card (ZMod p) := (ZMod.card p).symm
  593. _ = (Finset.univ : Finset (ZMod p)).card := rfl
  594. _ ≤ Z.card := Finset.card_le_card huniv
  595. omega
  596. refine intRootCount_le_of_eval_zmod p P.evalPoly K hne_pt ?_ hZ_card
  597. intro a ha
  598. rw [hderiv_eq]
  599. apply hsimple a
  600. rwa [← heval]
  601. /-! ## 5. Exact Taylor-Residue Certificate -/
  602. /-- **Single-Point Taylor Factor Lemma:**
  603. If `f = (X - C b)^m * g` in `ℤ[X]` and `g(b) ≢ 0 (mod p)` for prime `p`, then every
  604. integer root `s` of `f` satisfying `s ≡ b (mod p)` must equal `b`. -/
  605. theorem taylor_root_eq_of_factor (p : ℕ) [Fact p.Prime] (f g : Polynomial ℤ) (b : ℤ) (m : ℕ)
  606. (hfac : f = (Polynomial.X - Polynomial.C b) ^ m * g)
  607. (hgb : (g.map (Int.castRingHom (ZMod p))).eval (b : ZMod p) ≠ 0) :
  608. ∀ s : ℤ, f.IsRoot s → (s : ZMod p) = (b : ZMod p) → s = b := by
  609. intro s hs hmod
  610. have h0 : (s - b) ^ m * g.eval s = 0 := by
  611. simpa [Polynomial.IsRoot, hfac, Polynomial.eval_mul, Polynomial.eval_pow,
  612. Polynomial.eval_sub, Polynomial.eval_X, Polynomial.eval_C] using hs
  613. rcases mul_eq_zero.mp h0 with hpow | hgs
  614. · rcases eq_or_ne (s - b) 0 with hsb | hsb
  615. · exact sub_eq_zero.mp hsb
  616. · exact absurd hpow (pow_ne_zero m hsb)
  617. · exfalso
  618. apply hgb
  619. rw [← hmod, Polynomial.eval_map, Polynomial.eval₂_at_intCast]
  620. simp [hgs]
  621. /-- Relating vanishing low-order Taylor coefficients `f(X + b)` to divisibility by `(X - b)^m`. -/
  622. theorem factor_of_taylor_vanish (f : Polynomial ℤ) (b : ℤ) (m : ℕ)
  623. (hvan : ∀ j < m, (f.comp (Polynomial.X + Polynomial.C b)).coeff j = 0) :
  624. ∃ g : Polynomial ℤ,
  625. f = (Polynomial.X - Polynomial.C b) ^ m * g ∧
  626. g.eval b = (f.comp (Polynomial.X + Polynomial.C b)).coeff m := by
  627. let h := f.comp (Polynomial.X + Polynomial.C b)
  628. have hdvd : Polynomial.X ^ m ∣ h := Polynomial.X_pow_dvd_iff.mpr hvan
  629. rcases hdvd with ⟨q, hq⟩
  630. refine ⟨q.comp (Polynomial.X - Polynomial.C b), ?_, ?_⟩
  631. · have hcomp : ((Polynomial.X + Polynomial.C b).comp (Polynomial.X - Polynomial.C b)) = Polynomial.X := by
  632. simp [Polynomial.add_comp, Polynomial.X_comp]
  633. calc
  634. f = f.comp Polynomial.X := (Polynomial.comp_X).symm
  635. _ = f.comp ((Polynomial.X + Polynomial.C b).comp (Polynomial.X - Polynomial.C b)) := by rw [hcomp]
  636. _ = h.comp (Polynomial.X - Polynomial.C b) := (Polynomial.comp_assoc f _ _).symm
  637. _ = (Polynomial.X ^ m * q).comp (Polynomial.X - Polynomial.C b) := by rw [hq]
  638. _ = (Polynomial.X - Polynomial.C b) ^ m * q.comp (Polynomial.X - Polynomial.C b) := by
  639. simp [Polynomial.mul_comp, Polynomial.pow_comp, Polynomial.X_comp]
  640. · have hq0 : q.coeff 0 = h.coeff m := by
  641. have hm : h.coeff (0 + m) = q.coeff 0 := by
  642. rw [hq, Polynomial.coeff_X_pow_mul]
  643. simpa using hm.symm
  644. rw [Polynomial.eval_comp]
  645. simp only [Polynomial.eval_sub, Polynomial.eval_X, Polynomial.eval_C, sub_self]
  646. rw [← Polynomial.coeff_zero_eq_eval_zero, hq0]
  647. /-- **Combined Taylor-Residue Modular Certificate (Factor Form):**
  648. Suppose `p` is prime, `f_p := f.map (Int.castRingHom (ZMod p)) ≠ 0`, `f_p` has no double root
  649. outside the residues of a finite set `S ⊂ ℤ`, and for each `b ∈ S` there exist `m ≥ 1` and
  650. cofactor `g` with `f = (X - C b)^m * g` and `g(b) ≢ 0 (mod p)`. If `f_p` has at most `K`
  651. distinct zeros in `ZMod p`, then `intRootCount f ≤ K` unconditionally over `ℤ`. -/
  652. theorem intRootCount_le_of_factor_zmod (p : ℕ) [Fact p.Prime] (f : Polynomial ℤ)
  653. (S : Finset ℤ) (K : ℕ)
  654. (_hf_p : f.map (Int.castRingHom (ZMod p)) ≠ 0)
  655. (houtside : ∀ a : ZMod p,
  656. (f.map (Int.castRingHom (ZMod p))).IsRoot a →
  657. (f.map (Int.castRingHom (ZMod p))).derivative.IsRoot a →
  658. ∃ b ∈ S, (b : ZMod p) = a)
  659. (hfactor : ∀ b ∈ S, ∃ (m : ℕ) (g : Polynomial ℤ),
  660. 1 ≤ m ∧ f = (Polynomial.X - Polynomial.C b) ^ m * g ∧
  661. (g.map (Int.castRingHom (ZMod p))).eval (b : ZMod p) ≠ 0)
  662. (hbound : (Finset.univ.filter (fun a : ZMod p =>
  663. (f.map (Int.castRingHom (ZMod p))).eval a = 0)).card ≤ K) :
  664. intRootCount f ≤ K := by
  665. set φ := Int.castRingHom (ZMod p)
  666. set f_p := f.map φ
  667. let Z := Finset.univ.filter (fun a : ZMod p => f_p.eval a = 0)
  668. have hmaps : ∀ r ∈ intRootSet f, (r : ZMod p) ∈ Z := by
  669. intro r hr
  670. rw [mem_intRootSet_iff] at hr
  671. rw [Finset.mem_filter]
  672. refine ⟨Finset.mem_univ _, ?_⟩
  673. rw [Polynomial.eval_map, Polynomial.eval₂_at_intCast]
  674. simp [hr.2.eq_zero]
  675. have hinj : Set.InjOn (fun r : ℤ => (r : ZMod p)) (intRootSet f) := by
  676. intro r₁ hr₁ r₂ hr₂ hmod
  677. by_contra hne
  678. have hsub : ∀ r ∈ ({r₁, r₂} : Finset ℤ), f.IsRoot r := by
  679. intro r hr
  680. simp only [Finset.mem_insert, Finset.mem_singleton] at hr
  681. rcases hr with rfl | rfl
  682. · exact (mem_intRootSet_iff.mp hr₁).2
  683. · exact (mem_intRootSet_iff.mp hr₂).2
  684. have hdvd_Z : (Polynomial.X - Polynomial.C r₁) * (Polynomial.X - Polynomial.C r₂) ∣ f := by
  685. have hprod := prod_linear_dvd_of_roots f {r₁, r₂} hsub
  686. rwa [Finset.prod_pair hne] at hprod
  687. have hsq_p : (Polynomial.X - Polynomial.C (r₁ : ZMod p)) ^ 2 ∣ f_p := by
  688. rcases hdvd_Z with ⟨q, hq⟩
  689. refine ⟨q.map φ, ?_⟩
  690. dsimp only at hmod
  691. have h1 : f_p = ((Polynomial.X - Polynomial.C r₁) * (Polynomial.X - Polynomial.C r₂)).map φ * q.map φ := by
  692. dsimp only [f_p]
  693. rw [hq, Polynomial.map_mul]
  694. rw [h1, Polynomial.map_mul, Polynomial.map_sub, Polynomial.map_sub,
  695. Polynomial.map_X, Polynomial.map_C, Polynomial.map_C]
  696. simp only [φ, Int.coe_castRingHom]
  697. rw [hmod, sq, mul_assoc]
  698. have hroot : f_p.IsRoot (r₁ : ZMod p) := by
  699. rw [Polynomial.IsRoot, Polynomial.eval_map, Polynomial.eval₂_at_intCast]
  700. simp [(mem_intRootSet_iff.mp hr₁).2.eq_zero]
  701. have hderiv : f_p.derivative.IsRoot (r₁ : ZMod p) := by
  702. rcases hsq_p with ⟨q, hq⟩
  703. have hq' : f_p = (Polynomial.X - Polynomial.C (r₁ : ZMod p)) *
  704. ((Polynomial.X - Polynomial.C (r₁ : ZMod p)) * q) := by
  705. rw [hq, sq, mul_assoc]
  706. rw [Polynomial.IsRoot, hq', Polynomial.derivative_mul]
  707. simp
  708. rcases houtside (r₁ : ZMod p) hroot hderiv with ⟨b, hbS, hb_eq⟩
  709. rcases hfactor b hbS with ⟨m, g, _, hfac, hgb⟩
  710. have hr1_eq : r₁ = b :=
  711. taylor_root_eq_of_factor p f g b m hfac hgb r₁ (mem_intRootSet_iff.mp hr₁).2 hb_eq.symm
  712. have hr2_eq : r₂ = b :=
  713. taylor_root_eq_of_factor p f g b m hfac hgb r₂ (mem_intRootSet_iff.mp hr₂).2
  714. (hmod.symm.trans hb_eq.symm)
  715. exact hne (hr1_eq.trans hr2_eq.symm)
  716. calc
  717. intRootCount f = (intRootSet f).card := rfl
  718. _ ≤ Z.card := Finset.card_le_card_of_injOn (fun r : ℤ => (r : ZMod p)) hmaps hinj
  719. _ ≤ K := hbound
  720. /-- **Combined Taylor-Residue Modular Certificate (Exact Taylor Coefficient Form):**
  721. This matches `certify_taylor_roots` in `tau_z9_unconditional.c`:
  722. if `f_p` has `≤ K < p` zeros in `ZMod p` (ensuring `f_p ≠ 0`), no double roots outside
  723. the residues of `S`, and for each `b ∈ S` the lowest nonzero Taylor coefficient
  724. `c_m = (f.comp (X + C b)).coeff m` has index `m ≥ 1` and `¬ ((p : ℤ) ∣ c_m)`, then
  725. `intRootCount f ≤ K`. -/
  726. theorem intRootCount_le_of_taylor_zmod (p : ℕ) [Fact p.Prime] (f : Polynomial ℤ)
  727. (S : Finset ℤ) (K : ℕ) (hKp : K < p)
  728. (houtside : ∀ a : ZMod p,
  729. (f.map (Int.castRingHom (ZMod p))).IsRoot a →
  730. (f.map (Int.castRingHom (ZMod p))).derivative.IsRoot a →
  731. ∃ b ∈ S, (b : ZMod p) = a)
  732. (htaylor : ∀ b ∈ S, ∃ m : ℕ, 1 ≤ m ∧
  733. (∀ j < m, (f.comp (Polynomial.X + Polynomial.C b)).coeff j = 0) ∧
  734. ¬ ((p : ℤ) ∣ (f.comp (Polynomial.X + Polynomial.C b)).coeff m))
  735. (hbound : (Finset.univ.filter (fun a : ZMod p =>
  736. (f.map (Int.castRingHom (ZMod p))).eval a = 0)).card ≤ K) :
  737. intRootCount f ≤ K := by
  738. have hf_p : f.map (Int.castRingHom (ZMod p)) ≠ 0 := by
  739. by_contra h0
  740. have huniv : Finset.univ ⊆ Finset.univ.filter (fun a : ZMod p =>
  741. (f.map (Int.castRingHom (ZMod p))).eval a = 0) := by
  742. intro a _
  743. simp [h0]
  744. have hcard : p ≤ K := by
  745. calc
  746. p = Fintype.card (ZMod p) := (ZMod.card p).symm
  747. _ = (Finset.univ : Finset (ZMod p)).card := rfl
  748. _ ≤ _ := Finset.card_le_card huniv
  749. _ ≤ K := hbound
  750. omega
  751. refine intRootCount_le_of_factor_zmod p f S K hf_p houtside ?_ hbound
  752. intro b hbS
  753. rcases htaylor b hbS with ⟨m, hm1, hvan, hndvd⟩
  754. rcases factor_of_taylor_vanish f b m hvan with ⟨g, hfac, hgb⟩
  755. refine ⟨m, g, hm1, hfac, ?_⟩
  756. rw [Polynomial.eval_map, Polynomial.eval₂_at_intCast]
  757. change ((g.eval b : ℤ) : ZMod p) ≠ 0
  758. rw [hgb, ne_eq, ZMod.intCast_zmod_eq_zero_iff_dvd]
  759. exact hndvd
  760. /-- The 7 known small root centers `{0, 1, -1, 2, -2, 3, -3}` have pairwise distinct
  761. residues modulo any prime/modulus `p ≥ 7` (in particular `p ≥ 19`). -/
  762. theorem known_centers_distinct_zmod (p : ℕ) (hp : 7 ≤ p) :
  763. ∀ b₁ ∈ ({0, 1, -1, 2, -2, 3, -3} : Finset ℤ),
  764. ∀ b₂ ∈ ({0, 1, -1, 2, -2, 3, -3} : Finset ℤ),
  765. (b₁ : ZMod p) = (b₂ : ZMod p) → b₁ = b₂ := by
  766. intro b₁ hb₁ b₂ hb₂ hmod
  767. have hsub : ((b₁ - b₂ : ℤ) : ZMod p) = 0 := by push_cast; exact sub_eq_zero.mpr hmod
  768. rw [ZMod.intCast_zmod_eq_zero_iff_dvd] at hsub
  769. have hb1' : -3 ≤ b₁ ∧ b₁ ≤ 3 := by
  770. simp only [Finset.mem_insert, Finset.mem_singleton] at hb₁
  771. rcases hb₁ with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> omega
  772. have hb2' : -3 ≤ b₂ ∧ b₂ ≤ 3 := by
  773. simp only [Finset.mem_insert, Finset.mem_singleton] at hb₂
  774. rcases hb₂ with rfl | rfl | rfl | rfl | rfl | rfl | rfl <;> omega
  775. rcases hsub with ⟨k, hk⟩
  776. have hlt : (b₁ - b₂).natAbs < p := by omega
  777. by_cases h0 : b₁ - b₂ = 0
  778. · omega
  779. · have hge : p ≤ (b₁ - b₂).natAbs := by
  780. have hk0 : k ≠ 0 := fun h => h0 (by simp [hk, h])
  781. have hk1 : 1 ≤ k.natAbs := Int.natAbs_pos.mpr hk0
  782. have : (b₁ - b₂).natAbs = p * k.natAbs := by
  783. have := congrArg Int.natAbs hk
  784. simpa [Int.natAbs_mul] using this
  785. nlinarith
  786. omega
  787. /-- Evaluating an `SLP` in `ℤ[X]` at `X + C b` produces the Taylor-shifted polynomial
  788. `P.evalPoly.comp (X + C b)`. -/
  789. theorem slp_evalIn_taylor {τ : ℕ} (P : SLP τ) (b : ℤ) :
  790. P.evalIn (Polynomial.X + Polynomial.C b) =
  791. P.evalPoly.comp (Polynomial.X + Polynomial.C b) := by
  792. let φ : Polynomial ℤ →+* Polynomial ℤ :=
  793. Polynomial.eval₂RingHom Polynomial.C (Polynomial.X + Polynomial.C b)
  794. have hX : φ Polynomial.X = Polynomial.X + Polynomial.C b := by simp [φ]
  795. have h := P.map_evalIn φ Polynomial.X
  796. rw [P.evalIn_X, hX] at h
  797. rw [← h]
  798. rfl
  799. /-! ## 6. Exact-Equality-by-Evaluation & Hash Mismatch Soundness -/
  800. /-- **Hash Mismatch Soundness (`≠`):**
  801. Because `evalIn` commutes with ring homomorphisms, any discrepancy between `P₁.evalIn x`
  802. and `P₂.evalIn x` (or `-P₂.evalIn x`) in any commutative ring `R` proves `P₁.evalPoly ≠ P₂.evalPoly`
  803. (respectively `≠ -P₂.evalPoly`). -/
  804. theorem slp_evalPoly_ne_of_evalIn_ne {R : Type*} [CommRing R] (x : R)
  805. {τ₁ τ₂ : ℕ} (P₁ : SLP τ₁) (P₂ : SLP τ₂) (hne : P₁.evalIn x ≠ P₂.evalIn x) :
  806. P₁.evalPoly ≠ P₂.evalPoly := by
  807. intro heq
  808. apply hne
  809. rw [P₁.evalIn_eq_eval₂ x, P₂.evalIn_eq_eval₂ x, heq]
  810. theorem slp_evalPoly_ne_neg_of_evalIn_ne {R : Type*} [CommRing R] (x : R)
  811. {τ₁ τ₂ : ℕ} (P₁ : SLP τ₁) (P₂ : SLP τ₂) (hne : P₁.evalIn x ≠ -P₂.evalIn x) :
  812. P₁.evalPoly ≠ -P₂.evalPoly := by
  813. intro heq
  814. apply hne
  815. rw [P₁.evalIn_eq_eval₂ x, P₂.evalIn_eq_eval₂ x, heq, Polynomial.map_neg, Polynomial.eval_neg]
  816. /-- **Single-Prime Evaluation Vanishing Lemma:**
  817. If `h ∈ ℤ[X]` has `natDegree ≤ D` and reduces to `0` at `D + 1` distinct residues
  818. modulo a prime `q`, then `h ≡ 0 (mod q)` (every coefficient of `h` is divisible by `q`). -/
  819. theorem poly_map_zmod_eq_zero_of_roots (q : ℕ) [Fact q.Prime] (h : Polynomial ℤ) (D : ℕ)
  820. (hdeg : h.natDegree ≤ D) (T : Finset (ZMod q)) (hT : D + 1 ≤ T.card)
  821. (hvan : ∀ t ∈ T, (h.map (Int.castRingHom (ZMod q))).eval t = 0) :
  822. h.map (Int.castRingHom (ZMod q)) = 0 := by
  823. set hq := h.map (Int.castRingHom (ZMod q))
  824. by_contra hne
  825. have hsub : T ⊆ hq.roots.toFinset := by
  826. intro t ht
  827. rw [Multiset.mem_toFinset, Polynomial.mem_roots hne]
  828. exact hvan t ht
  829. have hcard : D + 1 ≤ hq.natDegree := by
  830. calc
  831. D + 1 ≤ T.card := hT
  832. _ ≤ hq.roots.toFinset.card := Finset.card_le_card hsub
  833. _ ≤ hq.roots.card := Multiset.toFinset_card_le _
  834. _ ≤ hq.natDegree := Polynomial.card_roots' hq
  835. have hdeg_le : hq.natDegree ≤ D := (Polynomial.natDegree_map_le).trans hdeg
  836. omega
  837. /-- Helper: if a finite set of distinct primes each divide `c : ℤ`, then their product divides `c`. -/
  838. private lemma prod_primes_dvd_of_forall_dvd (Q : Finset ℕ) (hQ : ∀ q ∈ Q, Nat.Prime q)
  839. (c : ℤ) (hdvd : ∀ q ∈ Q, (q : ℤ) ∣ c) :
  840. ((∏ q ∈ Q, q : ℕ) : ℤ) ∣ c := by
  841. classical
  842. induction Q using Finset.induction_on with
  843. | empty => simp
  844. | @insert q s hqs ih =>
  845. have hq_prime : Nat.Prime q := hQ q (Finset.mem_insert_self q s)
  846. have hs_prime : ∀ r ∈ s, Nat.Prime r := fun r hr => hQ r (Finset.mem_insert_of_mem hr)
  847. have hs_dvd : ((∏ r ∈ s, r : ℕ) : ℤ) ∣ c :=
  848. ih hs_prime (fun r hr => hdvd r (Finset.mem_insert_of_mem hr))
  849. have hq_dvd : (q : ℤ) ∣ c := hdvd q (Finset.mem_insert_self q s)
  850. have hcop_aux : ∀ t : Finset ℕ, q ∉ t → (∀ r ∈ t, Nat.Prime r) → Nat.Coprime q (∏ r ∈ t, r) := by
  851. intro t
  852. induction t using Finset.induction_on with
  853. | empty => intro _ _; simp
  854. | @insert r t hrt ihr =>
  855. intro hq_not ht_prime
  856. rw [Finset.prod_insert hrt]
  857. have hr_ne : q ≠ r := fun h => hq_not (h ▸ Finset.mem_insert_self r t)
  858. have hr_prime : Nat.Prime r := ht_prime r (Finset.mem_insert_self r t)
  859. have hqr : Nat.Coprime q r :=
  860. (hq_prime.coprime_iff_not_dvd).mpr (fun h =>
  861. hr_ne ((Nat.prime_dvd_prime_iff_eq hq_prime hr_prime).mp h))
  862. have hqt : Nat.Coprime q (∏ x ∈ t, x) :=
  863. ihr (fun h => hq_not (Finset.mem_insert_of_mem h))
  864. (fun x hx => ht_prime x (Finset.mem_insert_of_mem hx))
  865. exact hqr.mul_right hqt
  866. have hcop : Nat.Coprime q (∏ r ∈ s, r) := hcop_aux s hqs hs_prime
  867. have hcopZ : IsCoprime (q : ℤ) ((∏ r ∈ s, r : ℕ) : ℤ) := hcop.isCoprime
  868. rw [Finset.prod_insert hqs, Nat.cast_mul]
  869. exact hcopZ.mul_dvd hq_dvd hs_dvd
  870. /-- **CRT Exact-Zero Certificate by Evaluation:**
  871. If `h ∈ ℤ[X]` has `natDegree ≤ D`, vanishes at `D + 1` distinct residues modulo each prime
  872. `q ∈ Q`, and every coefficient satisfies `2 * |h.coeff n| < ∏ q ∈ Q, q`, then `h = 0`. -/
  873. theorem poly_eq_zero_of_crt_eval (Q : Finset ℕ) (hQ : ∀ q ∈ Q, Nat.Prime q)
  874. (h : Polynomial ℤ) (D : ℕ) (hdeg : h.natDegree ≤ D)
  875. (hvan : ∀ q ∈ Q, ∃ T : Finset (ZMod q), D + 1 ≤ T.card ∧
  876. ∀ t ∈ T, (h.map (Int.castRingHom (ZMod q))).eval t = 0)
  877. (hsmall : ∀ n ≤ D, 2 * (h.coeff n).natAbs < ∏ q ∈ Q, q) :
  878. h = 0 := by
  879. have hmod : ∀ q ∈ Q, h.map (Int.castRingHom (ZMod q)) = 0 := by
  880. intro q hq
  881. have : Fact q.Prime := ⟨hQ q hq⟩
  882. rcases hvan q hq with ⟨T, hT, ht⟩
  883. exact poly_map_zmod_eq_zero_of_roots q h D hdeg T hT ht
  884. ext n
  885. simp only [Polynomial.coeff_zero]
  886. by_cases hn : n ≤ D
  887. · have hdvd_all : ∀ q ∈ Q, (q : ℤ) ∣ h.coeff n := by
  888. intro q hq
  889. have hq0 := congrArg (fun p : Polynomial (ZMod q) => p.coeff n) (hmod q hq)
  890. simp only [Polynomial.coeff_map, Polynomial.coeff_zero, Int.coe_castRingHom] at hq0
  891. exact (ZMod.intCast_zmod_eq_zero_iff_dvd (h.coeff n) q).mp hq0
  892. have hprod_dvd : ((∏ q ∈ Q, q : ℕ) : ℤ) ∣ h.coeff n :=
  893. prod_primes_dvd_of_forall_dvd Q hQ (h.coeff n) hdvd_all
  894. have hnat_dvd : (∏ q ∈ Q, q) ∣ (h.coeff n).natAbs :=
  895. Int.natCast_dvd.mp hprod_dvd
  896. have hbound := hsmall n hn
  897. by_contra hne
  898. have hpos : 0 < (h.coeff n).natAbs := Int.natAbs_pos.mpr hne
  899. have hle : ∏ q ∈ Q, q ≤ (h.coeff n).natAbs := Nat.le_of_dvd hpos hnat_dvd
  900. omega
  901. · exact Polynomial.coeff_eq_zero_of_natDegree_lt (by omega)
  902. end Research.PvsNP.Tau
  903. end