From 3c0d34279915f05213d7283e9208ca4d7e58c5c6 Mon Sep 17 00:00:00 2001 From: Alexander Hicks <25369263+alexanderlhicks@users.noreply.github.com> Date: Thu, 19 Nov 2026 14:00:30 +0000 Subject: [PATCH 1/3] perf(univariate): compute each entry of a raw product as one sum Raw.mulRaw folds over the coefficients of p and adds the shifted row p[i] * q to the accumulator. Row i has q.size + i entries, so a nonempty p costs p.size * q.size + p.size * (p.size - 1) / 2 additions. A short right factor, as in p * C c or p * (X - C a), costs a number of additions that is quadratic in p.size (#419). The new Raw.mulRawFast builds the result in one pass. Entry k is the sum of p[i] * q[k - i] over the indices i that land in it, so the loop does p.size * q.size multiplications and as many additions. It reads the coefficients with proved indices. The raw array is the array of mulRaw: an empty p gives an empty array, and an empty q gives p.size - 1 zeros. Each product keeps the order p[i] * q[j], so no commutativity is needed. The loop takes 0, + and * as projections of the Semiring structure. The default instance paths construct Distrib and MulZeroClass values, and code specialized at ZMod constructed them in every iteration. The @[csimp] lemma mulRaw_eq_mulRawFast makes compiled code use the new loop. The lemma is in Raw/Ops.lean before mul, because mul and powBySqUntrimmed inline or specialize mulRaw when they are compiled. mulRaw stays the definition. The proof computes each entry of both arrays as the same convolution sum. To give the proof the lemmas it needs, size_smul, size_mulPowX, size_addRaw and size_mulRaw move from Raw/Size.lean to Raw/Ops.lean, and add_coeff? and smul_mulPowX_coeff move from Raw/Proofs.lean to Raw/Ops.lean. Their names do not change. add_coeff? now needs only AddZeroClass and smul_mulPowX_coeff only MulZeroClass. The new test module compares the compiled code with a copy of the definition of mulRaw. The inputs include empty and trailing-zero arrays, short factors on each side, and 2 x 2 matrices over Nat, which do not commute. Closes #419 Co-Authored-By: Claude Opus 5.5 --- CompPoly/Univariate/README.md | 2 +- CompPoly/Univariate/Raw/Ops.lean | 200 ++++++++++++++++++++- CompPoly/Univariate/Raw/Proofs.lean | 15 -- CompPoly/Univariate/Raw/Size.lean | 46 +---- docs/wiki/autoresearch.md | 1 + docs/wiki/repo-map.md | 5 +- tests/CompPolyTests.lean | 1 + tests/CompPolyTests/Univariate/RawMul.lean | 119 ++++++++++++ 8 files changed, 326 insertions(+), 63 deletions(-) create mode 100644 tests/CompPolyTests/Univariate/RawMul.lean diff --git a/CompPoly/Univariate/README.md b/CompPoly/Univariate/README.md index a38a9aff..79959626 100644 --- a/CompPoly/Univariate/README.md +++ b/CompPoly/Univariate/README.md @@ -19,7 +19,7 @@ on top (root finding and Reed-Solomon coding). - **Raw.lean** — Umbrella import for the raw array-backed representation and its split implementation files. - **Raw/Core.lean** — Raw datatype and core operations (`coeff`, `C`, `X`, `monomial`, `trim`, `degree`, `eval`). -- **Raw/Ops.lean** — Raw algebraic operations and related operational lemmas. +- **Raw/Ops.lean** — Raw algebraic operations and related operational lemmas. Compiled code computes `mulRaw` with `mulRawFast`, through the `@[csimp]` lemma `mulRaw_eq_mulRawFast`. - **Raw/Division.lean** — Raw polynomial division APIs (`divByMonic`, `modByMonic`, `div`, `mod`). Only `[BEq R]` is shared at section scope; the monic-division definitions need just `[CommRing R]`. - **Raw/Modular.lean** — Raw modular arithmetic. - **Raw/Context.lean** — Raw-level algorithm dictionaries backing the canonical contexts. diff --git a/CompPoly/Univariate/Raw/Ops.lean b/CompPoly/Univariate/Raw/Ops.lean index a16721ef..a60ccf03 100644 --- a/CompPoly/Univariate/Raw/Ops.lean +++ b/CompPoly/Univariate/Raw/Ops.lean @@ -1,8 +1,9 @@ /- Copyright (c) 2025 CompPoly. All rights reserved. +Copyright (c) 2026 ArkLib Contributors (the size lemmas of `SizeCoeffLemmas`). Released under Apache 2.0 license as described in the file LICENSE. Authors: Quang Dao, Gregor Mitscha-Baude, Derek Sorensen, Desmond Coles, - Natalie Klaus, Dimitris Mitsios + Natalie Klaus, Dimitris Mitsios, Alexander Hicks -/ module @@ -110,15 +111,208 @@ end MulPowXDefs The partial sums are combined with the untrimmed `addRaw`, so the result may carry trailing zeros and should be trimmed for canonical form. Deferring the trim to the - end (see `mul`) avoids an `O(size)` scan after every partial sum. -/ + end (see `mul`) avoids an `O(size)` scan after every partial sum. + + Compiled code computes `mulRaw` with `mulRawFast` (see `mulRaw_eq_mulRawFast`). -/ @[inline, specialize] def mulRaw [Semiring R] (p q : CPolynomial.Raw R) : CPolynomial.Raw R := p.zipIdx.foldl (fun acc ⟨a, i⟩ => acc.addRaw <| (smul a q).mulPowX i) (mk #[]) +section SizeCoeffLemmas + +/-! ### Sizes and coefficients of the raw operations + +The sizes count the stored coefficients, including trailing zeros. `size_smul`, `size_mulPowX`, +`size_addRaw` and `size_mulRaw` were ported from ArkLib's raw-size adapter under Apache 2.0. +The file header records their copyright. -/ + +/-- Scalar multiplication preserves the stored coefficient count. -/ +@[simp] +theorem size_smul [Mul R] (x : R) (p : CPolynomial.Raw R) : + (smul x p).size = p.size := by + simp only [smul, Array.size_map] + +/-- Multiplication by `X^i` retains all of the prepended zero coefficients. -/ +@[simp] +theorem size_mulPowX [Zero R] (i : Nat) (p : CPolynomial.Raw R) : + (mulPowX i p).size = i + p.size := by + simp only [mulPowX, Array.size_append, Array.size_replicate] + +/-- Padded raw addition stores the larger input size. -/ +@[simp] +theorem size_addRaw [Zero Q] [Add Q] {p q : CPolynomial.Raw Q} : + (addRaw p q).size = max p.size q.size := by + simp only [addRaw, Array.size_zipWith, Array.size_rightpad] + omega + +/-- Coefficient `i` of `addRaw p q` is the sum of coefficient `i` of `p` and of `q`. -/ +theorem add_coeff? [AddZeroClass Q] (p q : CPolynomial.Raw Q) (i : ℕ) : + (addRaw p q).coeff i = p.coeff i + q.coeff i := by + by_cases hi : i < (addRaw p q).size + · rw [coeff, ← Array.getElem_eq_getD (h := hi)] + simp only [addRaw, mk, Array.getElem_zipWith, Array.rightpad_getElem_eq_getD] + · rw [size_addRaw, not_lt, max_le_iff] at hi + simp [coeff, Array.getD_eq_getD_getElem?, hi.1, hi.2] + +/-- Coefficient `k` of `smul a q` shifted by `i` places: `0` below `i`, and `a * q.coeff (k - i)` +from `i` on. -/ +theorem smul_mulPowX_coeff [MulZeroClass R] (a : R) (q : CPolynomial.Raw R) (i k : ℕ) : + ((smul a q).mulPowX i).coeff k = if k < i then 0 else a * q.coeff (k - i) := by + simp only [mulPowX, smul, mk, coeff, Array.getD_eq_getD_getElem?, Array.getElem?_append, + Array.size_replicate, Array.getElem?_replicate, Array.getElem?_map] + split_ifs + · rfl + · cases q[k - i]? <;> simp + +/-- Raw multiplication retains every shifted row, including padding from an empty right array. -/ +theorem size_mulRaw [Semiring R] (p q : CPolynomial.Raw R) : + (mulRaw p q).size = if p.size = 0 then 0 else q.size + p.size - 1 := by + unfold mulRaw + have h := Array.foldl_induction (as := p.zipIdx) + (motive := fun i (acc : CPolynomial.Raw R) ↦ acc.size = if i = 0 then 0 else q.size + i - 1) + (init := mk #[]) (by rfl) + (f := fun acc ai ↦ acc.addRaw ((smul ai.1 q).mulPowX ai.2)) ?_ + · simpa only [Array.size_zipIdx] using h + · intro i acc ih + have hz : (p.zipIdx)[i].2 = i.val := by + change ((p.zipIdx)[i.val]'i.isLt).2 = i.val + rw [Array.getElem_zipIdx] + exact Nat.zero_add _ + simp only [size_addRaw, size_mulPowX, size_smul] + rw [hz, ih] + by_cases hi : i.val = 0 + · simp [hi] + · simp [hi] + omega + +/-- Coefficient `k` of `mulRaw p q` is the convolution sum of the coefficients of `p` and `q`. -/ +theorem coeff_mulRaw [Semiring R] (p q : CPolynomial.Raw R) (k : ℕ) : + (mulRaw p q).coeff k = + ∑ i ∈ Finset.range p.size, if k < i then 0 else p.coeff i * q.coeff (k - i) := by + unfold mulRaw + have h := Array.foldl_induction (as := p.zipIdx) + (motive := fun n (acc : CPolynomial.Raw R) ↦ ∀ k, acc.coeff k = + ∑ i ∈ Finset.range n, if k < i then 0 else p.coeff i * q.coeff (k - i)) + (init := mk #[]) (fun k ↦ by simp) + (f := fun acc ai ↦ acc.addRaw ((smul ai.1 q).mulPowX ai.2)) ?_ + · simpa only [Array.size_zipIdx] using h k + · intro i acc ih k + have hz : (p.zipIdx)[i] = (p.coeff i, i.val) := by + change (p.zipIdx)[i.val]'i.isLt = _ + rw [Array.getElem_zipIdx, zero_add, Array.getElem_eq_getD 0] + rw [hz, add_coeff?, ih, smul_mulPowX_coeff, Finset.sum_range_succ] + +end SizeCoeffLemmas + +section MulRawFast + +/-! ### Compiled raw multiplication + +`mulRawFast` computes the same array as `mulRaw`. The `@[csimp]` lemma `mulRaw_eq_mulRawFast` +makes compiled code use it. The lemma comes before `mul` and the other definitions in this file +that use `mulRaw`, because the compiler inlines or specializes `mulRaw` into them when it compiles +them, and a later `@[csimp]` lemma does not change that code. -/ + +/-- Raw multiplication that computes each entry of the result as one sum. + + The result is empty when `p` is empty. Otherwise it has `q.size + p.size - 1` entries, as in + `mulRaw`. Entry `k` is the sum of the products `p[i] * q[k - i]` over the indices `i` with + `k + 1 - q.size ≤ i < min (k + 1) p.size`, added to `0` in increasing order of `i`. Each pair + `(i, j)` with `i < p.size` and `j < q.size` occurs in exactly one entry, so the loop makes + `p.size * q.size` multiplications and the same number of additions. -/ +@[specialize] +def mulRawFast [inst : Semiring R] (p q : CPolynomial.Raw R) : CPolynomial.Raw R := + -- Structure projections of `inst`. The default instances construct `Distrib R` and + -- `MulZeroClass R` values. Code specialized at a carrier whose instance the compiler cannot + -- unfold, such as `ZMod n`, would construct them in every iteration of the loop. + letI : Zero R := inst.toAddCommMonoid.toAddMonoid.toZero + letI : Add R := inst.toAddCommMonoid.toAddMonoid.toAdd + letI : Mul R := inst.toMonoid.toMul + if p.size = 0 then mk #[] else + (q.size + p.size - 1).fold (fun k _ acc ↦ + let lo := k + 1 - q.size + let hi := min (k + 1) p.size + acc.push ((hi - lo).fold + (fun t ht s ↦ s + p[lo + t]'(by omega) * q[k - (lo + t)]'(by omega)) 0)) + (Array.emptyWithCapacity (q.size + p.size - 1)) + +/-- A `Nat.fold` that pushes `e k` at step `k` appends `e 0, …, e (n - 1)`. -/ +private theorem natFold_push {n : ℕ} (e : ℕ → R) (init : Array R) : + n.fold (fun k _ acc ↦ acc.push (e k)) init = init ++ Array.ofFn fun k : Fin n ↦ e k := by + induction n with + | zero => simp + | succ n ih => rw [Nat.fold_succ, ih, Array.ofFn_succ, Array.push_append]; rfl + +/-- For a nonempty `p`, `mulRawFast p q` is the array of the entry sums. -/ +private theorem mulRawFast_eq_ofFn [Semiring R] {p : CPolynomial.Raw R} (q : CPolynomial.Raw R) + (hp : p.size ≠ 0) : + mulRawFast p q = Array.ofFn fun k : Fin (q.size + p.size - 1) ↦ + (min (k.val + 1) p.size - (k.val + 1 - q.size)).fold (fun t _ s ↦ + s + p.getD (k.val + 1 - q.size + t) 0 * q.getD (k.val - (k.val + 1 - q.size + t)) 0) 0 := by + simp only [mulRawFast, hp, ↓reduceIte] + rw [natFold_push, Array.emptyWithCapacity_eq, Array.empty_append] + congr! 3 with k + funext t ht s + rw [Array.getElem_eq_getD 0, Array.getElem_eq_getD 0] + +/-- A `Nat.fold` that adds `g t` at step `t`, from `0`, computes the sum of the `g t`. -/ +private theorem natFold_add_eq_sum [AddCommMonoid R] (n : ℕ) (g : ℕ → R) : + n.fold (fun t _ acc ↦ acc + g t) 0 = ∑ t ∈ Finset.range n, g t := by + induction n with + | zero => simp + | succ n ih => rw [Nat.fold_succ, ih, Finset.sum_range_succ] + +/-- The convolution sum over `i < p.size` is the sum over the indices `i` whose products land in +entry `k`. -/ +private theorem sum_range_eq_sum_Ico [Semiring R] (p q : CPolynomial.Raw R) (k : ℕ) : + (∑ i ∈ Finset.range p.size, if k < i then 0 else p.coeff i * q.coeff (k - i)) = + ∑ i ∈ Finset.Ico (k + 1 - q.size) (min (k + 1) p.size), p.coeff i * q.coeff (k - i) := by + have hsub : Finset.Ico (k + 1 - q.size) (min (k + 1) p.size) ⊆ Finset.range p.size := + fun i hi ↦ by simp only [Finset.mem_Ico, Finset.mem_range] at hi ⊢; omega + rw [← Finset.sum_subset hsub] + · refine Finset.sum_congr rfl fun i hi ↦ ?_ + simp only [Finset.mem_Ico] at hi + simp [show ¬k < i by omega] + · intro i hi hi' + simp only [Finset.mem_Ico, Finset.mem_range, not_and_or, not_le, not_lt] at hi hi' + split_ifs with hk + · rfl + · have : q.size ≤ k - i := by omega + simp [this] + +/-- `mulRawFast p q` has the size of `mulRaw p q`. -/ +@[simp] +theorem size_mulRawFast [Semiring R] (p q : CPolynomial.Raw R) : + (mulRawFast p q).size = if p.size = 0 then 0 else q.size + p.size - 1 := by + by_cases hp : p.size = 0 + · simp [mulRawFast, hp] + · simp [mulRawFast_eq_ofFn q hp, hp] + +/-- Entry `k` of `mulRawFast p q` is the convolution sum of the coefficients of `p` and `q`. -/ +@[simp] +theorem getElem_mulRawFast [Semiring R] (p q : CPolynomial.Raw R) (k : ℕ) + (hk : k < (mulRawFast p q).size) : + (mulRawFast p q)[k] = + ∑ i ∈ Finset.range p.size, if k < i then 0 else p.coeff i * q.coeff (k - i) := by + have hp : p.size ≠ 0 := by rw [size_mulRawFast] at hk; split_ifs at hk <;> omega + simp only [mulRawFast_eq_ofFn q hp, Array.getElem_ofFn] + rw [natFold_add_eq_sum, sum_range_eq_sum_Ico, Finset.sum_Ico_eq_sum_range] + +/-- Compiled code computes `mulRaw` with `mulRawFast`. -/ +@[csimp] +theorem mulRaw_eq_mulRawFast : @mulRaw = @mulRawFast := by + funext R _ p q + refine Array.ext (by simp only [size_mulRaw, size_mulRawFast]) fun k h _ ↦ ?_ + simp only [Array.getElem_eq_getD 0 (h := h), getElem_mulRawFast] + exact coeff_mulRaw p q k + +end MulRawFast + /-- Multiplication using the naive `O(n²)` algorithm: `Σᵢ (aᵢ * q) * X^i`. Trims only once, at the end of the convolution fold, rather than after every - partial sum (`mulRaw` does the untrimmed accumulation). -/ + partial sum (`mulRaw` does the untrimmed accumulation). Compiled code computes the product with + `mulRawFast` (see `mulRaw_eq_mulRawFast`). -/ @[inline, specialize] def mul [Semiring R] [BEq R] (p q : CPolynomial.Raw R) : CPolynomial.Raw R := (mulRaw p q).trim diff --git a/CompPoly/Univariate/Raw/Proofs.lean b/CompPoly/Univariate/Raw/Proofs.lean index acb64b1a..d992d152 100644 --- a/CompPoly/Univariate/Raw/Proofs.lean +++ b/CompPoly/Univariate/Raw/Proofs.lean @@ -78,16 +78,6 @@ theorem add_coeff {p q : CPolynomial.Raw Q} {i : ℕ} (hi : i < (addRaw p q).siz by_cases hi' : i < p.size <;> by_cases hi'' : i < q.size <;> simp_all [Array.rightpad_getElem_eq_getD] -theorem add_coeff? (p q : CPolynomial.Raw Q) (i : ℕ) : - (addRaw p q).coeff i = p.coeff i + q.coeff i := by - rcases (Nat.lt_or_ge i (addRaw p q).size) with h_lt | h_ge - · rw [← add_coeff h_lt] - exact (Array.getElem_eq_getD (h := h_lt) 0).symm - have h_lt' : i ≥ max p.size q.size := by simpa only [size_addRaw] using h_ge - have h_p : i ≥ p.size := by omega - have h_q : i ≥ q.size := by omega - simp [h_p, h_q] - lemma add_coeff_trimmed [LawfulBEq R] (p q : CPolynomial.Raw R) (i : ℕ) : (p + q).coeff i = p.coeff i + q.coeff i := by have h_add : p + q = (p.addRaw q).trim := by rfl @@ -731,11 +721,6 @@ lemma smul_monomial_one_trim [DecidableEq R] [LawfulBEq R] (trim_replicate_zero (R := R) (n + 1)) · exact Trim.push_trim (Array.replicate n 0) r h -lemma smul_mulPowX_coeff [LawfulBEq R] (a : R) (q : CPolynomial.Raw R) (i k : ℕ) : - ((smul a q).mulPowX i).coeff k = if k < i then 0 else a * q.coeff (k - i) := by - convert mulPowX_coeff' (smul a q) i k using 1 - rw [ smul_coeff ] - lemma mul_coeff_list [LawfulBEq R] (p q : CPolynomial.Raw R) (k : ℕ) : (p * q).coeff k = (p.zipIdx.toList.map (fun ⟨a, i⟩ => if k < i then 0 else a * q.coeff (k - i))).sum := by diff --git a/CompPoly/Univariate/Raw/Size.lean b/CompPoly/Univariate/Raw/Size.lean index f2aae310..6b0ca307 100644 --- a/CompPoly/Univariate/Raw/Size.lean +++ b/CompPoly/Univariate/Raw/Size.lean @@ -13,8 +13,10 @@ public import CompPoly.Univariate.Raw.Ops These bounds concern stored coefficient arrays, including trailing zero coefficients. Raw multiplication retains the padding of its shifted rows even when the right input is empty. -The scalar, shift and product facts were ported from ArkLib's raw-size adapter under Apache 2.0; -its original copyright attribution is retained. +The size equations of `smul`, `mulPowX`, `addRaw` and `mulRaw` are in +`CompPoly.Univariate.Raw.Ops`, next to the operations, and this module imports them publicly. +The product bound was ported from ArkLib's raw-size adapter under Apache 2.0; its original +copyright attribution is retained. -/ public section @@ -26,50 +28,10 @@ namespace CompPoly.CPolynomial.Raw theorem size_C {R : Type*} (x : R) : (C x).size = 1 := by rfl -/-- Scalar multiplication preserves the stored coefficient count. -/ -@[simp] -theorem size_smul {R : Type*} [Mul R] (x : R) (p : Raw R) : - (smul x p).size = p.size := by - simp only [smul, Array.size_map] - -/-- Multiplication by `X^i` retains all of the prepended zero coefficients. -/ -@[simp] -theorem size_mulPowX {R : Type*} [Zero R] (i : Nat) (p : Raw R) : - (mulPowX i p).size = i + p.size := by - simp only [mulPowX, Array.size_append, Array.size_replicate] - -/-- Padded raw addition stores the larger input size. -/ -@[simp] -theorem size_addRaw {Q : Type*} [Zero Q] [Add Q] {p q : Raw Q} : - (addRaw p q).size = max p.size q.size := by - simp only [addRaw, Array.size_zipWith, Array.size_rightpad] - omega - /-- Compatibility name for the public theorem previously provided by `Raw.Proofs`. -/ @[deprecated (since := "2026-11-03")] alias add_size := size_addRaw -/-- Raw multiplication retains every shifted row, including padding from an empty right array. -/ -theorem size_mulRaw {R : Type*} [Semiring R] (p q : Raw R) : - (mulRaw p q).size = if p.size = 0 then 0 else q.size + p.size - 1 := by - unfold mulRaw - have h := Array.foldl_induction (as := p.zipIdx) - (motive := fun i (acc : Raw R) ↦ acc.size = if i = 0 then 0 else q.size + i - 1) - (init := mk #[]) (by rfl) - (f := fun acc ai ↦ acc.addRaw ((smul ai.1 q).mulPowX ai.2)) ?_ - · simpa only [Array.size_zipIdx] using h - · intro i acc ih - have hz : (p.zipIdx)[i].2 = i.val := by - change ((p.zipIdx)[i.val]'i.isLt).2 = i.val - rw [Array.getElem_zipIdx] - exact Nat.zero_add _ - simp only [size_addRaw, size_mulPowX, size_smul] - rw [hz, ih] - by_cases hi : i.val = 0 - · simp [hi] - · simp [hi] - omega - /-- The raw product's stored size is at most the sum of the input sizes minus one. -/ theorem size_mulRaw_le {R : Type*} [Semiring R] (p q : Raw R) : (mulRaw p q).size ≤ p.size + q.size - 1 := by diff --git a/docs/wiki/autoresearch.md b/docs/wiki/autoresearch.md index 6867b3eb..e9a36bb5 100644 --- a/docs/wiki/autoresearch.md +++ b/docs/wiki/autoresearch.md @@ -196,6 +196,7 @@ its rows there. | Base-field arithmetic | `fields-{koalabear,babybear,mersenne31,goldilocks}-{mul,add,inv,pow}`, `fields-{bn254,bls12-381,bls12-377}-{mul,add}`, `fields-secp256k1-{scalar,base}-{mul,add}` | `CompPoly/Fields/Montgomery/Native32Field.lean`, `CompPoly/Fields/Goldilocks/Fast.lean`, `CompPoly/Fields/Mersenne31/Fast.lean`, `CompPoly/Fields/Montgomery/Native64x8Mul.lean` | `toField_*`, `ringEquiv`, `instField` | two to four digest-cross-checked rows per group; the best-behaved targets | | Eight-limb inversion | `fields-mont64x8-{bn254,bls12-381,bls12-377,secp256k1-scalar,secp256k1-base}-inv` | `CompPoly/Fields/Montgomery/Native64x8InvDefs.lean` | `invGcdRaw_eq_inv` and its bounds chain in `CompPoly/Fields/Montgomery/Native64x8Inv.lean` | three algorithms in one group, one digest class | | Many-polynomial evaluation | `univariate-many-one-point-koalabear`, `univariate-dense-*` | `CompPoly/Univariate/ManyEval/Basic.lean`, `CompPoly/Univariate/Raw/Ops.lean` | `evalManyHorner_eq_map_eval`, `evalManySharedPowers_eq_map_eval`, `eval₂_horner_eq_eval₂` | plain array loops with short refinement proofs | +| Raw multiplication | `univariate-raw-mul-short-*`, `univariate-raw-mul-balanced-*`, `univariate-mul-crossover-*` | `CompPoly/Univariate/Raw/Ops.lean` (`mulRawFast`) | `mulRaw_eq_mulRawFast` (`@[csimp]`) | first loop run (#419): one sum per output entry, read with proved indices, so a short right factor costs linear time. Next: the inner `Nat.fold` counts down, so every term recomputes its index from the remaining count | | Tower scalar arithmetic | `fields-tower-bt{8,64}-mul`, `fields-tower-bt64-inv-word`, `fields-tower-bt128-{mul,inv}` | `CompPoly/Fields/Binary/Tower/FastDefs.lean` | `mul64T_eq_mul64`, `inv64T_eq_inv64`, `toConcrete_*` in `CompPoly/Fields/Binary/Tower/Fast.lean` | table twins already close to optimal; low headroom | | Additive NTT | `additive-ntt-btf3-l2-r2`, `additive-ntt-btf3-l4-r2`, `additive-ntt-btf4-l7-r2` | `CompPoly/Fields/Binary/AdditiveNTT/Impl.lean` | `computableAdditiveNTTFast_eq_computableAdditiveNTT` | the `btf4` group is fast-only; rely on the build | | NTTFast stages | `ntt-{koalabear,babybear}-l{8,10,12,14,16}`, `ntt-plan-koalabear` | `CompPoly/Univariate/NTTFast/Plan.lean` | `CompPoly/Univariate/NTTFast/Correctness/Pipeline.lean` | largest surface and largest proof burden; last | diff --git a/docs/wiki/repo-map.md b/docs/wiki/repo-map.md index ae2ffaa0..ea294b50 100644 --- a/docs/wiki/repo-map.md +++ b/docs/wiki/repo-map.md @@ -50,8 +50,9 @@ scripts/ repo utilities and validation helpers `NTT/Coset.lean` and `NTTFast/Coset.lean` on its cosets, and `BatchEval/Interpolation.lean` on arbitrary nodes. - Bounding stored coefficient buffers: `Univariate/Raw/Size.lean` gives raw operation sizes, - including the padding retained by multiplication. These are representation-size facts, - independent of canonical degree and execution cost. + including the padding retained by multiplication. The size equations of the operations + themselves are in `Univariate/Raw/Ops.lean`, and `Size.lean` exports them. These are + representation-size facts, independent of canonical degree and execution cost. - Working on root-of-unity NTT evaluation, interpolation, or multiplication: start in `CompPoly/Univariate/NTT/` for shared domains/specifications and `CompPoly/Univariate/NTTFast/` for planned optimized transforms. diff --git a/tests/CompPolyTests.lean b/tests/CompPolyTests.lean index 39a5564c..f7bdc87d 100644 --- a/tests/CompPolyTests.lean +++ b/tests/CompPolyTests.lean @@ -110,6 +110,7 @@ public import CompPolyTests.Univariate.NTT.Forward public import CompPolyTests.Univariate.NTT.Interpolation public import CompPolyTests.Univariate.NTT.Inverse public import CompPolyTests.Univariate.Raw +public import CompPolyTests.Univariate.RawMul public import CompPolyTests.Univariate.RawSize public import CompPolyTests.Univariate.Roots.Enumeration public import CompPolyTests.Univariate.Roots.FiniteField diff --git a/tests/CompPolyTests/Univariate/RawMul.lean b/tests/CompPolyTests/Univariate/RawMul.lean new file mode 100644 index 00000000..b22300ac --- /dev/null +++ b/tests/CompPolyTests/Univariate/RawMul.lean @@ -0,0 +1,119 @@ +/- +Copyright (c) 2026 CompPoly Contributors. All rights reserved. +Released under Apache 2.0 license as described in the file LICENSE. +Authors: Alexander Hicks +-/ +module + +import all CompPoly.Univariate.Raw.Ops +public meta import CompPoly.Univariate.Raw.Ops +public meta import Mathlib.LinearAlgebra.Matrix.Notation + +/-! +# Raw multiplication tests + +Checks of `CPolynomial.Raw.mulRaw` on concrete inputs. `#guard` compiles its term and runs the +compiler's output in the interpreter. The compiler applies the `@[csimp]` lemma +`mulRaw_eq_mulRawFast`, so these checks compute `mulRaw` with `mulRawFast`. `mulRawRef` repeats +the defining fold of `mulRaw`. No `@[csimp]` lemma applies to it, so a comparison with +`mulRawRef` checks the compiled code against the definition of `mulRaw`. + +The inputs cover empty and singleton arrays, zero and trailing-zero coefficients, a short factor +on each side, and balanced factors. The coefficients in the last group are `2 × 2` matrices over +`ℕ`, which do not commute, so these checks also fix the order of the factors in each product. +-/ + +public meta section + +open CompPoly CPolynomial CPolynomial.Raw + +namespace CompPolyTests.Univariate.RawMul + +/-- The defining fold of `mulRaw`. No `@[csimp]` lemma applies to this copy. -/ +def mulRawRef {R : Type*} [Semiring R] (p q : Raw R) : Raw R := + p.zipIdx.foldl (fun acc ⟨a, i⟩ => acc.addRaw <| (smul a q).mulPowX i) (mk #[]) + +example {R : Type*} [Semiring R] (p q : Raw R) : mulRawRef p q = mulRaw p q := rfl + +/-! ### Empty and singleton factors -/ + +#guard mulRaw (#[] : Raw ℕ) #[] == #[] +#guard mulRaw (#[] : Raw ℕ) #[1, 2, 3] == #[] +#guard mulRaw (#[1, 2, 3] : Raw ℕ) #[] == #[0, 0] +#guard mulRaw (#[1] : Raw ℕ) #[] == #[] +#guard mulRaw (#[3] : Raw ℕ) #[5] == #[15] +#guard mulRaw (#[2] : Raw ℕ) #[1, 2, 3] == #[2, 4, 6] +#guard mulRaw (#[1, 2, 3] : Raw ℕ) #[2] == #[2, 4, 6] + +/-! ### Zero and trailing-zero coefficients + +The product keeps `p.size + q.size - 1` entries, so zero entries at the end stay. -/ + +#guard mulRaw (#[0, 0] : Raw ℕ) #[0, 0, 0] == #[0, 0, 0, 0] +#guard mulRaw (#[1, 0] : Raw ℕ) #[1, 1, 0] == #[1, 1, 0, 0] +#guard mulRaw (#[0, 2] : Raw ℕ) #[0, 3] == #[0, 0, 6] +#guard mulRaw (#[1, 2] : Raw ℕ) #[3, 4] == #[3, 10, 8] + +/-! ### A short factor on each side + +`#[c]` is `C c` and `#[-a, 1]` is `X - C a`. -/ + +#guard mulRaw (#[1, -2, 0, 5] : Raw ℤ) #[3] == #[3, -6, 0, 15] +#guard mulRaw (#[1, -2, 0, 5] : Raw ℤ) #[-2, 1] == #[-2, 5, -2, -10, 5] +#guard mulRaw (#[-2, 1] : Raw ℤ) #[1, -2, 0, 5] == #[-2, 5, -2, -10, 5] +#guard mulRaw (#[3] : Raw ℤ) #[1, -2, 0, 5] == #[3, -6, 0, 15] + +/-- The array of length `n` whose entry `i` is `i + 1`, or `0` when `i % 3 = 2`. -/ +def natInput (n : ℕ) : Raw ℕ := Array.ofFn fun i : Fin n => if i.val % 3 = 2 then 0 else i.val + 1 + +#guard (List.range 9).all fun m => (List.range 9).all fun n => + mulRaw (natInput m) (natInput n) == mulRawRef (natInput m) (natInput n) +#guard [1, 2, 3].all fun n => + mulRaw (natInput 40) (natInput n) == mulRawRef (natInput 40) (natInput n) && + mulRaw (natInput n) (natInput 40) == mulRawRef (natInput n) (natInput 40) +#guard mulRaw (natInput 24) (natInput 24) == mulRawRef (natInput 24) (natInput 24) + +/-! ### Noncommutative coefficients -/ + +/-- A `2 × 2` matrix over `ℕ`. -/ +abbrev M := Matrix (Fin 2) (Fin 2) ℕ + +/-- A matrix that does not commute with `b`. -/ +def a : M := !![1, 2; 3, 4] + +/-- The swap matrix. -/ +def b : M := !![0, 1; 1, 0] + +#guard a * b != b * a +#guard mulRaw #[a] #[b] == #[a * b] +#guard mulRaw #[a] #[b] == #[!![2, 1; 4, 3]] +#guard mulRaw #[a, b] #[b, a] == #[a * b, a * a + b * b, b * a] +#guard mulRaw #[a, b, a] #[b] == #[a * b, b * b, a * b] +#guard mulRaw #[a, b, a] #[b] != #[b * a, b * b, b * a] +#guard mulRaw #[b] #[a, b, a] == #[b * a, b * b, b * a] + +/-- Matrix inputs of length `n`, cycling through six matrices that include `0` and `1`. -/ +def matInput (n : ℕ) (shift : ℕ) : Raw M := + Array.ofFn fun i : Fin n => #[a, b, 0, a * b, 1, !![0, 0; 5, 0]].getD ((i.val + shift) % 6) 0 + +#guard (List.range 7).all fun m => (List.range 7).all fun n => + mulRaw (matInput m 0) (matInput n 3) == mulRawRef (matInput m 0) (matInput n 3) +#guard [1, 2].all fun n => + mulRaw (matInput 20 0) (matInput n 1) == mulRawRef (matInput 20 0) (matInput n 1) && + mulRaw (matInput n 1) (matInput 20 0) == mulRawRef (matInput n 1) (matInput 20 0) + +/-! ### `mulRawFast` called directly -/ + +#guard (List.range 9).all fun m => (List.range 9).all fun n => + mulRawFast (natInput m) (natInput n) == mulRawRef (natInput m) (natInput n) +#guard (List.range 7).all fun m => (List.range 7).all fun n => + mulRawFast (matInput m 0) (matInput n 3) == mulRawRef (matInput m 0) (matInput n 3) + +-- The checks above pass without the `@[csimp]` lemma too; this fails if it is removed. +open Lean in +run_meta do + let map := (Compiler.CSimp.ext.getState (← getEnv)).map + unless (map.find? ``mulRaw).map (·.toDeclName) == some ``mulRawFast do + throwError "no @[csimp] lemma replaces mulRaw with mulRawFast" + +end CompPolyTests.Univariate.RawMul From 056f071dc02ef4340f873f231a78033bd73ab43f Mon Sep 17 00:00:00 2001 From: Alexander Hicks <25369263+alexanderlhicks@users.noreply.github.com> Date: Thu, 19 Nov 2026 22:45:02 +0000 Subject: [PATCH 2/3] fix(univariate): name the proof lemmas of mulRawFast and test its placement MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The refinement proof of mulRaw_eq_mulRawFast now closes with one simp only over named lemmas: Trim.coeff_eq_getElem, coeff_mulRaw and getElem_mulRawFast. The proof no longer depends on coeff unfolding to getD. The private natFold_push is now the public Nat.fold_push_eq_append_ofFn in CompPoly/Data/Array/Lemmas.lean. It is a fact about arrays only. natFold_add_eq_sum stays private, because that module has no big-operator import. The test module gains new checks. ZMod 6 and 2 x 2 matrices over ZMod 4 test coefficient types with zero divisors. An IR check reads the compiled code of mul and fails if it does not call mulRawFast. The test prose qualifies the output length with "for a nonempty p", and the test functions use the arrow ↦. The other changes are text only. The header of Raw/Ops.lean has one copyright line, which keeps the ArkLib attribution. The module docstring names the size lemmas and the csimp lemma. The csimp docstring states the claim of the lemma. The repo map says where the size equations are. The autoresearch row cites #422 and states the operation count of the loop. Compatibility: add_coeff? and smul_mulPowX_coeff keep their names and namespaces. The first commit of this PR changed their instance arguments. The lemma add_coeff? takes [AddZeroClass Q] in place of [Semiring Q]. The lemma smul_mulPowX_coeff takes [MulZeroClass R] in place of [Semiring R] [BEq R] [LawfulBEq R]. The new statements are more general. A caller that passes these instances explicitly with @ can need an edit. Co-Authored-By: Claude Opus 5.5 --- CompPoly/Data/Array/Lemmas.lean | 12 +++++ CompPoly/Univariate/Raw/Ops.lean | 30 +++++------ docs/wiki/autoresearch.md | 2 +- docs/wiki/repo-map.md | 9 ++-- tests/CompPolyTests/Univariate/RawMul.lean | 63 +++++++++++++++++----- 5 files changed, 82 insertions(+), 34 deletions(-) diff --git a/CompPoly/Data/Array/Lemmas.lean b/CompPoly/Data/Array/Lemmas.lean index f6427158..43cba3fb 100644 --- a/CompPoly/Data/Array/Lemmas.lean +++ b/CompPoly/Data/Array/Lemmas.lean @@ -268,3 +268,15 @@ lemma popWhile_nil_or_last_false (p : α → Bool) (as : Array α) · simp_all +decide [ Array.getLast ] end Array + +namespace Nat + +/-- A `Nat.fold` that pushes `e k` at step `k` appends `e 0, …, e (n - 1)` to its initial +array. -/ +theorem fold_push_eq_append_ofFn {α : Type*} {n : ℕ} (e : ℕ → α) (init : Array α) : + n.fold (fun k _ acc ↦ acc.push (e k)) init = init ++ Array.ofFn fun k : Fin n ↦ e k := by + induction n with + | zero => simp + | succ n ih => rw [Nat.fold_succ, ih, Array.ofFn_succ, Array.push_append]; rfl + +end Nat diff --git a/CompPoly/Univariate/Raw/Ops.lean b/CompPoly/Univariate/Raw/Ops.lean index a60ccf03..3e5ca9ac 100644 --- a/CompPoly/Univariate/Raw/Ops.lean +++ b/CompPoly/Univariate/Raw/Ops.lean @@ -1,6 +1,5 @@ /- -Copyright (c) 2025 CompPoly. All rights reserved. -Copyright (c) 2026 ArkLib Contributors (the size lemmas of `SizeCoeffLemmas`). +Copyright (c) 2025 CompPoly, 2026 ArkLib Contributors. All rights reserved. Released under Apache 2.0 license as described in the file LICENSE. Authors: Quang Dao, Gregor Mitscha-Baude, Derek Sorensen, Desmond Coles, Natalie Klaus, Dimitris Mitsios, Alexander Hicks @@ -13,7 +12,10 @@ public import CompPoly.Univariate.Raw.Core /-! # Raw Univariate Polynomial Operations -Operations and evaluation lemmas for raw computable univariate polynomials. +Operations and evaluation lemmas for raw computable univariate polynomials. The file also gives +the sizes and the coefficients of the raw operations. It defines `mulRawFast`, a loop for `mulRaw` +that computes each entry of the result as one sum. The `@[csimp]` lemma `mulRaw_eq_mulRawFast` +makes compiled code compute `mulRaw` with `mulRawFast`. -/ public section @@ -210,8 +212,8 @@ section MulRawFast `mulRawFast` computes the same array as `mulRaw`. The `@[csimp]` lemma `mulRaw_eq_mulRawFast` makes compiled code use it. The lemma comes before `mul` and the other definitions in this file -that use `mulRaw`, because the compiler inlines or specializes `mulRaw` into them when it compiles -them, and a later `@[csimp]` lemma does not change that code. -/ +that use `mulRaw`. When the compiler compiles such a definition, it inlines or specializes `mulRaw` +into the code. A `@[csimp]` lemma later in the file would not change that code. -/ /-- Raw multiplication that computes each entry of the result as one sum. @@ -236,21 +238,15 @@ def mulRawFast [inst : Semiring R] (p q : CPolynomial.Raw R) : CPolynomial.Raw R (fun t ht s ↦ s + p[lo + t]'(by omega) * q[k - (lo + t)]'(by omega)) 0)) (Array.emptyWithCapacity (q.size + p.size - 1)) -/-- A `Nat.fold` that pushes `e k` at step `k` appends `e 0, …, e (n - 1)`. -/ -private theorem natFold_push {n : ℕ} (e : ℕ → R) (init : Array R) : - n.fold (fun k _ acc ↦ acc.push (e k)) init = init ++ Array.ofFn fun k : Fin n ↦ e k := by - induction n with - | zero => simp - | succ n ih => rw [Nat.fold_succ, ih, Array.ofFn_succ, Array.push_append]; rfl - /-- For a nonempty `p`, `mulRawFast p q` is the array of the entry sums. -/ private theorem mulRawFast_eq_ofFn [Semiring R] {p : CPolynomial.Raw R} (q : CPolynomial.Raw R) (hp : p.size ≠ 0) : mulRawFast p q = Array.ofFn fun k : Fin (q.size + p.size - 1) ↦ (min (k.val + 1) p.size - (k.val + 1 - q.size)).fold (fun t _ s ↦ - s + p.getD (k.val + 1 - q.size + t) 0 * q.getD (k.val - (k.val + 1 - q.size + t)) 0) 0 := by + s + p.getD (k.val + 1 - q.size + t) 0 * + q.getD (k.val - (k.val + 1 - q.size + t)) 0) 0 := by simp only [mulRawFast, hp, ↓reduceIte] - rw [natFold_push, Array.emptyWithCapacity_eq, Array.empty_append] + rw [Nat.fold_push_eq_append_ofFn, Array.emptyWithCapacity_eq, Array.empty_append] congr! 3 with k funext t ht s rw [Array.getElem_eq_getD 0, Array.getElem_eq_getD 0] @@ -298,13 +294,13 @@ theorem getElem_mulRawFast [Semiring R] (p q : CPolynomial.Raw R) (k : ℕ) simp only [mulRawFast_eq_ofFn q hp, Array.getElem_ofFn] rw [natFold_add_eq_sum, sum_range_eq_sum_Ico, Finset.sum_Ico_eq_sum_range] -/-- Compiled code computes `mulRaw` with `mulRawFast`. -/ +/-- `mulRaw` equals `mulRawFast`. As a `@[csimp]` lemma, it makes compiled code use +`mulRawFast`. -/ @[csimp] theorem mulRaw_eq_mulRawFast : @mulRaw = @mulRawFast := by funext R _ p q refine Array.ext (by simp only [size_mulRaw, size_mulRawFast]) fun k h _ ↦ ?_ - simp only [Array.getElem_eq_getD 0 (h := h), getElem_mulRawFast] - exact coeff_mulRaw p q k + simp only [← Trim.coeff_eq_getElem h, coeff_mulRaw, getElem_mulRawFast] end MulRawFast diff --git a/docs/wiki/autoresearch.md b/docs/wiki/autoresearch.md index e9a36bb5..82cddce6 100644 --- a/docs/wiki/autoresearch.md +++ b/docs/wiki/autoresearch.md @@ -196,7 +196,7 @@ its rows there. | Base-field arithmetic | `fields-{koalabear,babybear,mersenne31,goldilocks}-{mul,add,inv,pow}`, `fields-{bn254,bls12-381,bls12-377}-{mul,add}`, `fields-secp256k1-{scalar,base}-{mul,add}` | `CompPoly/Fields/Montgomery/Native32Field.lean`, `CompPoly/Fields/Goldilocks/Fast.lean`, `CompPoly/Fields/Mersenne31/Fast.lean`, `CompPoly/Fields/Montgomery/Native64x8Mul.lean` | `toField_*`, `ringEquiv`, `instField` | two to four digest-cross-checked rows per group; the best-behaved targets | | Eight-limb inversion | `fields-mont64x8-{bn254,bls12-381,bls12-377,secp256k1-scalar,secp256k1-base}-inv` | `CompPoly/Fields/Montgomery/Native64x8InvDefs.lean` | `invGcdRaw_eq_inv` and its bounds chain in `CompPoly/Fields/Montgomery/Native64x8Inv.lean` | three algorithms in one group, one digest class | | Many-polynomial evaluation | `univariate-many-one-point-koalabear`, `univariate-dense-*` | `CompPoly/Univariate/ManyEval/Basic.lean`, `CompPoly/Univariate/Raw/Ops.lean` | `evalManyHorner_eq_map_eval`, `evalManySharedPowers_eq_map_eval`, `eval₂_horner_eq_eval₂` | plain array loops with short refinement proofs | -| Raw multiplication | `univariate-raw-mul-short-*`, `univariate-raw-mul-balanced-*`, `univariate-mul-crossover-*` | `CompPoly/Univariate/Raw/Ops.lean` (`mulRawFast`) | `mulRaw_eq_mulRawFast` (`@[csimp]`) | first loop run (#419): one sum per output entry, read with proved indices, so a short right factor costs linear time. Next: the inner `Nat.fold` counts down, so every term recomputes its index from the remaining count | +| Raw multiplication | `univariate-raw-mul-short-*`, `univariate-raw-mul-balanced-*`, `univariate-mul-crossover-*` | `CompPoly/Univariate/Raw/Ops.lean` (`mulRawFast`) | `mulRaw_eq_mulRawFast` (`@[csimp]`) | first loop run (#422, for #419): each output entry is one sum, read with proved indices. The loop does `p.size * q.size` multiplications and as many additions, so with a short factor on either side the count is linear in the stored length of the long factor. Next: the inner `Nat.fold` counts down, so every term recomputes its index from the remaining count | | Tower scalar arithmetic | `fields-tower-bt{8,64}-mul`, `fields-tower-bt64-inv-word`, `fields-tower-bt128-{mul,inv}` | `CompPoly/Fields/Binary/Tower/FastDefs.lean` | `mul64T_eq_mul64`, `inv64T_eq_inv64`, `toConcrete_*` in `CompPoly/Fields/Binary/Tower/Fast.lean` | table twins already close to optimal; low headroom | | Additive NTT | `additive-ntt-btf3-l2-r2`, `additive-ntt-btf3-l4-r2`, `additive-ntt-btf4-l7-r2` | `CompPoly/Fields/Binary/AdditiveNTT/Impl.lean` | `computableAdditiveNTTFast_eq_computableAdditiveNTT` | the `btf4` group is fast-only; rely on the build | | NTTFast stages | `ntt-{koalabear,babybear}-l{8,10,12,14,16}`, `ntt-plan-koalabear` | `CompPoly/Univariate/NTTFast/Plan.lean` | `CompPoly/Univariate/NTTFast/Correctness/Pipeline.lean` | largest surface and largest proof burden; last | diff --git a/docs/wiki/repo-map.md b/docs/wiki/repo-map.md index ea294b50..fcace8de 100644 --- a/docs/wiki/repo-map.md +++ b/docs/wiki/repo-map.md @@ -49,10 +49,11 @@ scripts/ repo utilities and validation helpers `NTT/Interpolation.lean` and `NTTFast/Interpolation.lean` on an NTT domain, `NTT/Coset.lean` and `NTTFast/Coset.lean` on its cosets, and `BatchEval/Interpolation.lean` on arbitrary nodes. -- Bounding stored coefficient buffers: `Univariate/Raw/Size.lean` gives raw operation sizes, - including the padding retained by multiplication. The size equations of the operations - themselves are in `Univariate/Raw/Ops.lean`, and `Size.lean` exports them. These are - representation-size facts, independent of canonical degree and execution cost. +- Bounding stored coefficient buffers: the size equations of `smul`, `mulPowX`, `addRaw` and + `mulRaw`, including the padding that multiplication retains, are in `Univariate/Raw/Ops.lean`. + `Univariate/Raw/Size.lean` imports that module publicly and adds `size_C` and bounds such as + `size_mulRaw_le`. These are representation-size facts, independent of canonical degree and + execution cost. - Working on root-of-unity NTT evaluation, interpolation, or multiplication: start in `CompPoly/Univariate/NTT/` for shared domains/specifications and `CompPoly/Univariate/NTTFast/` for planned optimized transforms. diff --git a/tests/CompPolyTests/Univariate/RawMul.lean b/tests/CompPolyTests/Univariate/RawMul.lean index b22300ac..6b9f19be 100644 --- a/tests/CompPolyTests/Univariate/RawMul.lean +++ b/tests/CompPolyTests/Univariate/RawMul.lean @@ -7,6 +7,8 @@ module import all CompPoly.Univariate.Raw.Ops public meta import CompPoly.Univariate.Raw.Ops +public meta import Lean.Compiler.IR.EmitUtil +public meta import Mathlib.Data.ZMod.Defs public meta import Mathlib.LinearAlgebra.Matrix.Notation /-! @@ -19,8 +21,13 @@ the defining fold of `mulRaw`. No `@[csimp]` lemma applies to it, so a compariso `mulRawRef` checks the compiled code against the definition of `mulRaw`. The inputs cover empty and singleton arrays, zero and trailing-zero coefficients, a short factor -on each side, and balanced factors. The coefficients in the last group are `2 × 2` matrices over -`ℕ`, which do not commute, so these checks also fix the order of the factors in each product. +on each side, and balanced factors. The section on zero divisors uses `ZMod 6` and `2 × 2` +matrices over `ZMod 4`. The section on noncommutative coefficients uses `2 × 2` matrices over +`ℕ`. The matrices over `ℕ` do not commute, so these checks also fix the order of the factors in +each product. + +Two last checks read the `@[csimp]` map and the compiled code of `mul`. They fail if compiled code +does not compute `mulRaw` with `mulRawFast`. -/ public meta section @@ -47,7 +54,8 @@ example {R : Type*} [Semiring R] (p q : Raw R) : mulRawRef p q = mulRaw p q := r /-! ### Zero and trailing-zero coefficients -The product keeps `p.size + q.size - 1` entries, so zero entries at the end stay. -/ +For a nonempty `p`, the product keeps `p.size + q.size - 1` entries, so zero entries at the end +stay. -/ #guard mulRaw (#[0, 0] : Raw ℕ) #[0, 0, 0] == #[0, 0, 0, 0] #guard mulRaw (#[1, 0] : Raw ℕ) #[1, 1, 0] == #[1, 1, 0, 0] @@ -64,15 +72,34 @@ The product keeps `p.size + q.size - 1` entries, so zero entries at the end stay #guard mulRaw (#[3] : Raw ℤ) #[1, -2, 0, 5] == #[3, -6, 0, 15] /-- The array of length `n` whose entry `i` is `i + 1`, or `0` when `i % 3 = 2`. -/ -def natInput (n : ℕ) : Raw ℕ := Array.ofFn fun i : Fin n => if i.val % 3 = 2 then 0 else i.val + 1 +def natInput (n : ℕ) : Raw ℕ := Array.ofFn fun i : Fin n ↦ if i.val % 3 = 2 then 0 else i.val + 1 -#guard (List.range 9).all fun m => (List.range 9).all fun n => +#guard (List.range 9).all fun m ↦ (List.range 9).all fun n ↦ mulRaw (natInput m) (natInput n) == mulRawRef (natInput m) (natInput n) -#guard [1, 2, 3].all fun n => +#guard [1, 2, 3].all fun n ↦ mulRaw (natInput 40) (natInput n) == mulRawRef (natInput 40) (natInput n) && mulRaw (natInput n) (natInput 40) == mulRawRef (natInput n) (natInput 40) #guard mulRaw (natInput 24) (natInput 24) == mulRawRef (natInput 24) (natInput 24) +/-! ### Zero divisors + +In `ZMod 6` and in matrices over `ZMod 4`, a product of nonzero entries can be zero. -/ + +/-- Inputs over `ZMod 6` of length `n`. Entry `i` is `5 * i + shift`. -/ +def zmodInput (n shift : ℕ) : Raw (ZMod 6) := + Array.ofFn fun i : Fin n ↦ ((5 * i.val + shift : ℕ) : ZMod 6) + +#guard (List.range 12).all fun m ↦ (List.range 12).all fun n ↦ + mulRaw (zmodInput m 1) (zmodInput n 2) == mulRawRef (zmodInput m 1) (zmodInput n 2) + +/-- Upper triangular `2 × 2` matrices over `ZMod 4` of length `n`. -/ +def zmodMatInput (n shift : ℕ) : Raw (Matrix (Fin 2) (Fin 2) (ZMod 4)) := + Array.ofFn fun i : Fin n ↦ !![((i.val + shift : ℕ) : ZMod 4), 2; 0, ((3 * i.val : ℕ) : ZMod 4)] + +#guard (List.range 8).all fun m ↦ (List.range 8).all fun n ↦ + mulRaw (zmodMatInput m 0) (zmodMatInput n 1) == + mulRawRef (zmodMatInput m 0) (zmodMatInput n 1) + /-! ### Noncommutative coefficients -/ /-- A `2 × 2` matrix over `ℕ`. -/ @@ -94,26 +121,38 @@ def b : M := !![0, 1; 1, 0] /-- Matrix inputs of length `n`, cycling through six matrices that include `0` and `1`. -/ def matInput (n : ℕ) (shift : ℕ) : Raw M := - Array.ofFn fun i : Fin n => #[a, b, 0, a * b, 1, !![0, 0; 5, 0]].getD ((i.val + shift) % 6) 0 + Array.ofFn fun i : Fin n ↦ #[a, b, 0, a * b, 1, !![0, 0; 5, 0]].getD ((i.val + shift) % 6) 0 -#guard (List.range 7).all fun m => (List.range 7).all fun n => +#guard (List.range 7).all fun m ↦ (List.range 7).all fun n ↦ mulRaw (matInput m 0) (matInput n 3) == mulRawRef (matInput m 0) (matInput n 3) -#guard [1, 2].all fun n => +#guard [1, 2].all fun n ↦ mulRaw (matInput 20 0) (matInput n 1) == mulRawRef (matInput 20 0) (matInput n 1) && mulRaw (matInput n 1) (matInput 20 0) == mulRawRef (matInput n 1) (matInput 20 0) /-! ### `mulRawFast` called directly -/ -#guard (List.range 9).all fun m => (List.range 9).all fun n => +#guard (List.range 9).all fun m ↦ (List.range 9).all fun n ↦ mulRawFast (natInput m) (natInput n) == mulRawRef (natInput m) (natInput n) -#guard (List.range 7).all fun m => (List.range 7).all fun n => +#guard (List.range 7).all fun m ↦ (List.range 7).all fun n ↦ mulRawFast (matInput m 0) (matInput n 3) == mulRawRef (matInput m 0) (matInput n 3) --- The checks above pass without the `@[csimp]` lemma too; this fails if it is removed. +/-! ### The `@[csimp]` lemma and its placement + +The checks above pass without the `@[csimp]` lemma too. The first check below fails if the lemma +is removed. The second fails if `mul` is compiled before the lemma, because then the compiled code +of `mul` keeps the fold of `mulRaw`. -/ + open Lean in run_meta do let map := (Compiler.CSimp.ext.getState (← getEnv)).map unless (map.find? ``mulRaw).map (·.toDeclName) == some ``mulRawFast do throwError "no @[csimp] lemma replaces mulRaw with mulRawFast" +open Lean in +run_meta do + let env ← getEnv + let some decl := IR.findEnvDecl env ``mul | throwError "no compiled code for `mul`" + unless (IR.collectUsedDecls env [decl]).any (``mulRawFast).isPrefixOf do + throwError "the compiled code of `mul` does not call `mulRawFast`" + end CompPolyTests.Univariate.RawMul From 0bdde4b29b8a0cd36d288d953895f53e59ff50cc Mon Sep 17 00:00:00 2001 From: Alexander Hicks <25369263+alexanderlhicks@users.noreply.github.com> Date: Fri, 20 Nov 2026 01:23:02 +0000 Subject: [PATCH 3/3] fix(univariate): test the compiled callers of mulRawFast and its ZMod code The placement test now walks the compiled code transitively from test definitions that use mul, pow and powBySq. Each must reach code of mulRawFast. Closed terms do not count, because the compiler shares them between unrelated declarations. A new test fails if the code of mulRaw specialized at ZMod 6 calls instDistribOfSemiring or instMulZeroClassOfSemiring. The comment at the letI lines of mulRawFast names this test. New checks compare mulRaw with its defining fold at 70 x 33 and at 300 x 2. The four products with an empty factor that RawSize.lean already checks are removed from RawMul.lean. Nat.fold_push_eq_append_ofFn now lets the pushed element depend on the bound proof. The private sum_range_eq_sum_Ico is now sum_range_ite_eq_sum_Ico. The docstrings of Raw/Ops.lean and Raw/Size.lean name the lemmas that each file states. The provenance of the ported size lemmas moves to the module docstring of Raw/Ops.lean. The size_mulRawFast and powBySqUntrimmed docstrings, the test docstrings, the repo map and the autoresearch row are corrected. The autoresearch row records the 0 + x addition in each entry as a next step. Co-Authored-By: Claude Opus 5.5 --- CompPoly/Data/Array/Lemmas.lean | 14 ++-- CompPoly/Univariate/Raw/Ops.lean | 32 +++++---- CompPoly/Univariate/Raw/Size.lean | 8 +-- docs/wiki/autoresearch.md | 2 +- docs/wiki/repo-map.md | 2 +- tests/CompPolyTests/Univariate/RawMul.lean | 82 ++++++++++++++++++---- 6 files changed, 99 insertions(+), 41 deletions(-) diff --git a/CompPoly/Data/Array/Lemmas.lean b/CompPoly/Data/Array/Lemmas.lean index 43cba3fb..a9350bca 100644 --- a/CompPoly/Data/Array/Lemmas.lean +++ b/CompPoly/Data/Array/Lemmas.lean @@ -271,12 +271,14 @@ end Array namespace Nat -/-- A `Nat.fold` that pushes `e k` at step `k` appends `e 0, …, e (n - 1)` to its initial -array. -/ -theorem fold_push_eq_append_ofFn {α : Type*} {n : ℕ} (e : ℕ → α) (init : Array α) : - n.fold (fun k _ acc ↦ acc.push (e k)) init = init ++ Array.ofFn fun k : Fin n ↦ e k := by - induction n with +/-- A `Nat.fold` that pushes `e k h` at step `k` appends `e 0 _, …, e (n - 1) _` to its initial +array. The pushed element can depend on the proof `h : k < n`. -/ +theorem fold_push_eq_append_ofFn {α : Type*} {n : ℕ} (e : (k : ℕ) → k < n → α) + (init : Array α) : + n.fold (fun k h acc ↦ acc.push (e k h)) init = init ++ Array.ofFn fun k : Fin n ↦ e k k.2 := by + induction n generalizing init with | zero => simp - | succ n ih => rw [Nat.fold_succ, ih, Array.ofFn_succ, Array.push_append]; rfl + | succ n ih => + rw [Nat.fold_succ, ih (fun k h ↦ e k (by omega)), Array.ofFn_succ, Array.push_append]; rfl end Nat diff --git a/CompPoly/Univariate/Raw/Ops.lean b/CompPoly/Univariate/Raw/Ops.lean index 3e5ca9ac..36dd5392 100644 --- a/CompPoly/Univariate/Raw/Ops.lean +++ b/CompPoly/Univariate/Raw/Ops.lean @@ -12,10 +12,14 @@ public import CompPoly.Univariate.Raw.Core /-! # Raw Univariate Polynomial Operations -Operations and evaluation lemmas for raw computable univariate polynomials. The file also gives -the sizes and the coefficients of the raw operations. It defines `mulRawFast`, a loop for `mulRaw` -that computes each entry of the result as one sum. The `@[csimp]` lemma `mulRaw_eq_mulRawFast` -makes compiled code compute `mulRaw` with `mulRawFast`. +Operations and evaluation lemmas for raw computable univariate polynomials. The file also states +the sizes of `smul`, `mulPowX`, `addRaw` and `mulRaw`, and the coefficients of `addRaw`, of a +shifted `smul` and of `mulRaw`. It defines `mulRawFast`, a loop for `mulRaw` that computes each +entry of the result as one sum. The `@[csimp]` lemma `mulRaw_eq_mulRawFast` makes compiled code +compute `mulRaw` with `mulRawFast`. + +`size_smul`, `size_mulPowX`, `size_addRaw` and `size_mulRaw` were ported from ArkLib's raw-size +adapter under Apache 2.0. The file header records their copyright. -/ public section @@ -122,11 +126,9 @@ def mulRaw [Semiring R] (p q : CPolynomial.Raw R) : CPolynomial.Raw R := section SizeCoeffLemmas -/-! ### Sizes and coefficients of the raw operations +/-! ### Sizes and coefficients of the operations in `mulRaw` -The sizes count the stored coefficients, including trailing zeros. `size_smul`, `size_mulPowX`, -`size_addRaw` and `size_mulRaw` were ported from ArkLib's raw-size adapter under Apache 2.0. -The file header records their copyright. -/ +The sizes count the stored coefficients, including trailing zeros. -/ /-- Scalar multiplication preserves the stored coefficient count. -/ @[simp] @@ -226,7 +228,8 @@ into the code. A `@[csimp]` lemma later in the file would not change that code. def mulRawFast [inst : Semiring R] (p q : CPolynomial.Raw R) : CPolynomial.Raw R := -- Structure projections of `inst`. The default instances construct `Distrib R` and -- `MulZeroClass R` values. Code specialized at a carrier whose instance the compiler cannot - -- unfold, such as `ZMod n`, would construct them in every iteration of the loop. + -- unfold, such as `ZMod n`, would construct them in every iteration of the loop. The proofs + -- do not need these lines. A check in `CompPolyTests.Univariate.RawMul` fails without them. letI : Zero R := inst.toAddCommMonoid.toAddMonoid.toZero letI : Add R := inst.toAddCommMonoid.toAddMonoid.toAdd letI : Mul R := inst.toMonoid.toMul @@ -248,7 +251,7 @@ private theorem mulRawFast_eq_ofFn [Semiring R] {p : CPolynomial.Raw R} (q : CPo simp only [mulRawFast, hp, ↓reduceIte] rw [Nat.fold_push_eq_append_ofFn, Array.emptyWithCapacity_eq, Array.empty_append] congr! 3 with k - funext t ht s + funext t _ s rw [Array.getElem_eq_getD 0, Array.getElem_eq_getD 0] /-- A `Nat.fold` that adds `g t` at step `t`, from `0`, computes the sum of the `g t`. -/ @@ -260,7 +263,7 @@ private theorem natFold_add_eq_sum [AddCommMonoid R] (n : ℕ) (g : ℕ → R) : /-- The convolution sum over `i < p.size` is the sum over the indices `i` whose products land in entry `k`. -/ -private theorem sum_range_eq_sum_Ico [Semiring R] (p q : CPolynomial.Raw R) (k : ℕ) : +private theorem sum_range_ite_eq_sum_Ico [Semiring R] (p q : CPolynomial.Raw R) (k : ℕ) : (∑ i ∈ Finset.range p.size, if k < i then 0 else p.coeff i * q.coeff (k - i)) = ∑ i ∈ Finset.Ico (k + 1 - q.size) (min (k + 1) p.size), p.coeff i * q.coeff (k - i) := by have hsub : Finset.Ico (k + 1 - q.size) (min (k + 1) p.size) ⊆ Finset.range p.size := @@ -276,7 +279,7 @@ private theorem sum_range_eq_sum_Ico [Semiring R] (p q : CPolynomial.Raw R) (k : · have : q.size ≤ k - i := by omega simp [this] -/-- `mulRawFast p q` has the size of `mulRaw p q`. -/ +/-- `mulRawFast p q` is empty when `p` is empty. Otherwise it has `q.size + p.size - 1` entries. -/ @[simp] theorem size_mulRawFast [Semiring R] (p q : CPolynomial.Raw R) : (mulRawFast p q).size = if p.size = 0 then 0 else q.size + p.size - 1 := by @@ -292,7 +295,7 @@ theorem getElem_mulRawFast [Semiring R] (p q : CPolynomial.Raw R) (k : ℕ) ∑ i ∈ Finset.range p.size, if k < i then 0 else p.coeff i * q.coeff (k - i) := by have hp : p.size ≠ 0 := by rw [size_mulRawFast] at hk; split_ifs at hk <;> omega simp only [mulRawFast_eq_ofFn q hp, Array.getElem_ofFn] - rw [natFold_add_eq_sum, sum_range_eq_sum_Ico, Finset.sum_Ico_eq_sum_range] + rw [natFold_add_eq_sum, sum_range_ite_eq_sum_Ico, Finset.sum_Ico_eq_sum_range] /-- `mulRaw` equals `mulRawFast`. As a `@[csimp]` lemma, it makes compiled code use `mulRawFast`. -/ @@ -321,7 +324,8 @@ def pow [Semiring R] [BEq R] (p : CPolynomial.Raw R) (n : Nat) : CPolynomial.Raw /-- Repeated-squaring core that accumulates with the untrimmed `mulRaw`, deferring the canonicalization trim to the outer `powBySq` wrapper. The output may carry -trailing zeros and is not in canonical form. +trailing zeros and is not in canonical form. Compiled code computes each product with +`mulRawFast`. For $ n > 0 $, computes $ p ^ n $ by recursing on $ n / 2 $: * If $ n $ is even: $ (p ^ {n/2})^2 $ diff --git a/CompPoly/Univariate/Raw/Size.lean b/CompPoly/Univariate/Raw/Size.lean index 6b0ca307..eb18750c 100644 --- a/CompPoly/Univariate/Raw/Size.lean +++ b/CompPoly/Univariate/Raw/Size.lean @@ -13,10 +13,10 @@ public import CompPoly.Univariate.Raw.Ops These bounds concern stored coefficient arrays, including trailing zero coefficients. Raw multiplication retains the padding of its shifted rows even when the right input is empty. -The size equations of `smul`, `mulPowX`, `addRaw` and `mulRaw` are in -`CompPoly.Univariate.Raw.Ops`, next to the operations, and this module imports them publicly. -The product bound was ported from ArkLib's raw-size adapter under Apache 2.0; its original -copyright attribution is retained. +`CompPoly.Univariate.Raw.Ops` states the size equations of `smul`, `mulPowX`, `addRaw` and +`mulRaw`, next to the operations. This module imports it publicly. The product bound +`size_mulRaw_le` was ported from ArkLib's raw-size adapter under Apache 2.0. The file header keeps +its original copyright attribution. -/ public section diff --git a/docs/wiki/autoresearch.md b/docs/wiki/autoresearch.md index 82cddce6..176ec746 100644 --- a/docs/wiki/autoresearch.md +++ b/docs/wiki/autoresearch.md @@ -196,7 +196,7 @@ its rows there. | Base-field arithmetic | `fields-{koalabear,babybear,mersenne31,goldilocks}-{mul,add,inv,pow}`, `fields-{bn254,bls12-381,bls12-377}-{mul,add}`, `fields-secp256k1-{scalar,base}-{mul,add}` | `CompPoly/Fields/Montgomery/Native32Field.lean`, `CompPoly/Fields/Goldilocks/Fast.lean`, `CompPoly/Fields/Mersenne31/Fast.lean`, `CompPoly/Fields/Montgomery/Native64x8Mul.lean` | `toField_*`, `ringEquiv`, `instField` | two to four digest-cross-checked rows per group; the best-behaved targets | | Eight-limb inversion | `fields-mont64x8-{bn254,bls12-381,bls12-377,secp256k1-scalar,secp256k1-base}-inv` | `CompPoly/Fields/Montgomery/Native64x8InvDefs.lean` | `invGcdRaw_eq_inv` and its bounds chain in `CompPoly/Fields/Montgomery/Native64x8Inv.lean` | three algorithms in one group, one digest class | | Many-polynomial evaluation | `univariate-many-one-point-koalabear`, `univariate-dense-*` | `CompPoly/Univariate/ManyEval/Basic.lean`, `CompPoly/Univariate/Raw/Ops.lean` | `evalManyHorner_eq_map_eval`, `evalManySharedPowers_eq_map_eval`, `eval₂_horner_eq_eval₂` | plain array loops with short refinement proofs | -| Raw multiplication | `univariate-raw-mul-short-*`, `univariate-raw-mul-balanced-*`, `univariate-mul-crossover-*` | `CompPoly/Univariate/Raw/Ops.lean` (`mulRawFast`) | `mulRaw_eq_mulRawFast` (`@[csimp]`) | first loop run (#422, for #419): each output entry is one sum, read with proved indices. The loop does `p.size * q.size` multiplications and as many additions, so with a short factor on either side the count is linear in the stored length of the long factor. Next: the inner `Nat.fold` counts down, so every term recomputes its index from the remaining count | +| Raw multiplication | `univariate-raw-mul-short-*`, `univariate-raw-mul-balanced-*`, `univariate-mul-crossover-*` | `CompPoly/Univariate/Raw/Ops.lean` (`mulRawFast`) | `mulRaw_eq_mulRawFast` (`@[csimp]`) | first loop run (#422, for #419): the loop computes each output entry as one sum and reads the inputs with proved indices. It does `p.size * q.size` multiplications and as many additions. With a short factor on either side, that count is linear in the stored length of the long factor. Next: the inner `Nat.fold` counts down, so every term recomputes its index from the remaining count. Each sum also starts from `0`, so every entry does one addition `0 + x`. Seed each sum with its first product to remove it | | Tower scalar arithmetic | `fields-tower-bt{8,64}-mul`, `fields-tower-bt64-inv-word`, `fields-tower-bt128-{mul,inv}` | `CompPoly/Fields/Binary/Tower/FastDefs.lean` | `mul64T_eq_mul64`, `inv64T_eq_inv64`, `toConcrete_*` in `CompPoly/Fields/Binary/Tower/Fast.lean` | table twins already close to optimal; low headroom | | Additive NTT | `additive-ntt-btf3-l2-r2`, `additive-ntt-btf3-l4-r2`, `additive-ntt-btf4-l7-r2` | `CompPoly/Fields/Binary/AdditiveNTT/Impl.lean` | `computableAdditiveNTTFast_eq_computableAdditiveNTT` | the `btf4` group is fast-only; rely on the build | | NTTFast stages | `ntt-{koalabear,babybear}-l{8,10,12,14,16}`, `ntt-plan-koalabear` | `CompPoly/Univariate/NTTFast/Plan.lean` | `CompPoly/Univariate/NTTFast/Correctness/Pipeline.lean` | largest surface and largest proof burden; last | diff --git a/docs/wiki/repo-map.md b/docs/wiki/repo-map.md index fcace8de..ad4c29c7 100644 --- a/docs/wiki/repo-map.md +++ b/docs/wiki/repo-map.md @@ -51,7 +51,7 @@ scripts/ repo utilities and validation helpers `BatchEval/Interpolation.lean` on arbitrary nodes. - Bounding stored coefficient buffers: the size equations of `smul`, `mulPowX`, `addRaw` and `mulRaw`, including the padding that multiplication retains, are in `Univariate/Raw/Ops.lean`. - `Univariate/Raw/Size.lean` imports that module publicly and adds `size_C` and bounds such as + `Univariate/Raw/Size.lean` imports that module publicly and adds `size_C` and the bound `size_mulRaw_le`. These are representation-size facts, independent of canonical degree and execution cost. - Working on root-of-unity NTT evaluation, interpolation, or multiplication: diff --git a/tests/CompPolyTests/Univariate/RawMul.lean b/tests/CompPolyTests/Univariate/RawMul.lean index 6b9f19be..2290a54c 100644 --- a/tests/CompPolyTests/Univariate/RawMul.lean +++ b/tests/CompPolyTests/Univariate/RawMul.lean @@ -26,8 +26,9 @@ matrices over `ZMod 4`. The section on noncommutative coefficients uses `2 × 2` `ℕ`. The matrices over `ℕ` do not commute, so these checks also fix the order of the factors in each product. -Two last checks read the `@[csimp]` map and the compiled code of `mul`. They fail if compiled code -does not compute `mulRaw` with `mulRawFast`. +The last checks read the `@[csimp]` map and the compiled code. They fail if the compiled code of +`mul`, `pow` or `powBySq` does not reach `mulRawFast`. They also fail if the code of `mulRawFast` +specialized at `ZMod 6` constructs `Distrib` or `MulZeroClass` instances. -/ public meta section @@ -42,12 +43,10 @@ def mulRawRef {R : Type*} [Semiring R] (p q : Raw R) : Raw R := example {R : Type*} [Semiring R] (p q : Raw R) : mulRawRef p q = mulRaw p q := rfl -/-! ### Empty and singleton factors -/ +/-! ### Singleton factors + +`CompPolyTests.Univariate.RawSize` checks the fixed products with an empty factor. -/ -#guard mulRaw (#[] : Raw ℕ) #[] == #[] -#guard mulRaw (#[] : Raw ℕ) #[1, 2, 3] == #[] -#guard mulRaw (#[1, 2, 3] : Raw ℕ) #[] == #[0, 0] -#guard mulRaw (#[1] : Raw ℕ) #[] == #[] #guard mulRaw (#[3] : Raw ℕ) #[5] == #[15] #guard mulRaw (#[2] : Raw ℕ) #[1, 2, 3] == #[2, 4, 6] #guard mulRaw (#[1, 2, 3] : Raw ℕ) #[2] == #[2, 4, 6] @@ -80,10 +79,12 @@ def natInput (n : ℕ) : Raw ℕ := Array.ofFn fun i : Fin n ↦ if i.val % 3 = mulRaw (natInput 40) (natInput n) == mulRawRef (natInput 40) (natInput n) && mulRaw (natInput n) (natInput 40) == mulRawRef (natInput n) (natInput 40) #guard mulRaw (natInput 24) (natInput 24) == mulRawRef (natInput 24) (natInput 24) +#guard mulRaw (natInput 70) (natInput 33) == mulRawRef (natInput 70) (natInput 33) +#guard mulRaw (natInput 300) (natInput 2) == mulRawRef (natInput 300) (natInput 2) /-! ### Zero divisors -In `ZMod 6` and in matrices over `ZMod 4`, a product of nonzero entries can be zero. -/ +In `ZMod 6` and in matrices over `ZMod 4`, a product of two nonzero coefficients can be zero. -/ /-- Inputs over `ZMod 6` of length `n`. Entry `i` is `5 * i + shift`. -/ def zmodInput (n shift : ℕ) : Raw (ZMod 6) := @@ -92,7 +93,8 @@ def zmodInput (n shift : ℕ) : Raw (ZMod 6) := #guard (List.range 12).all fun m ↦ (List.range 12).all fun n ↦ mulRaw (zmodInput m 1) (zmodInput n 2) == mulRawRef (zmodInput m 1) (zmodInput n 2) -/-- Upper triangular `2 × 2` matrices over `ZMod 4` of length `n`. -/ +/-- An array of `n` upper triangular `2 × 2` matrices over `ZMod 4`. Entry `i` is +`!![i + shift, 2; 0, 3 * i]`. -/ def zmodMatInput (n shift : ℕ) : Raw (Matrix (Fin 2) (Fin 2) (ZMod 4)) := Array.ofFn fun i : Fin n ↦ !![((i.val + shift : ℕ) : ZMod 4), 2; 0, ((3 * i.val : ℕ) : ZMod 4)] @@ -102,7 +104,7 @@ def zmodMatInput (n shift : ℕ) : Raw (Matrix (Fin 2) (Fin 2) (ZMod 4)) := /-! ### Noncommutative coefficients -/ -/-- A `2 × 2` matrix over `ℕ`. -/ +/-- The type of `2 × 2` matrices over `ℕ`. -/ abbrev M := Matrix (Fin 2) (Fin 2) ℕ /-- A matrix that does not commute with `b`. -/ @@ -139,8 +141,45 @@ def matInput (n : ℕ) (shift : ℕ) : Raw M := /-! ### The `@[csimp]` lemma and its placement The checks above pass without the `@[csimp]` lemma too. The first check below fails if the lemma -is removed. The second fails if `mul` is compiled before the lemma, because then the compiled code -of `mul` keeps the fold of `mulRaw`. -/ +is removed. The other checks walk the compiled code of the definitions below. If `mul` or +`powBySqUntrimmed` is compiled before the lemma, its code keeps the fold of `mulRaw` and does not +reach `mulRawFast`. `pow` calls `mul`. -/ + +/-- `mul` at `ℕ`. -/ +def natMul (p q : Raw ℕ) : Raw ℕ := mul p q + +/-- `pow` at `ℕ`. -/ +def natPow (p : Raw ℕ) (n : ℕ) : Raw ℕ := pow p n + +/-- `powBySq` at `ℕ`. -/ +def natPowBySq (p : Raw ℕ) (n : ℕ) : Raw ℕ := powBySq p n + +/-- `mulRaw` at `ZMod 6`, a carrier whose instances the compiler does not unfold. -/ +def zmodMul (p q : Raw (ZMod 6)) : Raw (ZMod 6) := mulRaw p q + +open Lean in +/-- The compiled declarations that the code of `root` reaches through calls, `root` included. -/ +partial def reachableDecls (env : Environment) (root : Name) : NameSet := + go {} [root] +where + /-- Adds the declarations in `todo` and the declarations that they reach to `seen`. -/ + go (seen : NameSet) : List Name → NameSet + | [] => seen + | n :: todo => + if seen.contains n then go seen todo + else + let used := match IR.findEnvDecl env n with + | some decl => IR.collectUsedDecls env [decl] + | none => #[] + go (seen.insert n) (used.toList ++ todo) + +open Lean in +/-- Whether `n` names code of `mulRawFast` or of one of its specializations. A closed term does +not count. The compiler shares closed terms between unrelated declarations. -/ +def isMulRawFastCode (n : Name) : Bool := + (``mulRawFast).isPrefixOf n && match n with + | .str _ s => !s.startsWith "_closed" + | _ => true open Lean in run_meta do @@ -151,8 +190,21 @@ run_meta do open Lean in run_meta do let env ← getEnv - let some decl := IR.findEnvDecl env ``mul | throwError "no compiled code for `mul`" - unless (IR.collectUsedDecls env [decl]).any (``mulRawFast).isPrefixOf do - throwError "the compiled code of `mul` does not call `mulRawFast`" + for root in [``natMul, ``natPow, ``natPowBySq, ``zmodMul] do + unless (reachableDecls env root).any isMulRawFastCode do + throwError "the compiled code of `{root}` does not reach `mulRawFast`" + +/-! The `letI` lines in `mulRawFast` take `0`, `+` and `*` as projections of the `Semiring` +instance. Without them, the code specialized at `ZMod 6` constructs a `Distrib` and a +`MulZeroClass` instance in every iteration. The proofs and the checks above pass without these +lines. -/ + +open Lean in +run_meta do + let env ← getEnv + let built := [``instDistribOfSemiring, ``instMulZeroClassOfSemiring] + for n in reachableDecls env ``zmodMul do + if n.components.any built.contains then + throwError "the compiled code of `zmodMul` calls `{n}`" end CompPolyTests.Univariate.RawMul