diff --git a/CompPoly/Data/Array/Lemmas.lean b/CompPoly/Data/Array/Lemmas.lean index f6427158..a9350bca 100644 --- a/CompPoly/Data/Array/Lemmas.lean +++ b/CompPoly/Data/Array/Lemmas.lean @@ -268,3 +268,17 @@ 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 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 (fun k h ↦ e k (by omega)), Array.ofFn_succ, Array.push_append]; rfl + +end Nat 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..36dd5392 100644 --- a/CompPoly/Univariate/Raw/Ops.lean +++ b/CompPoly/Univariate/Raw/Ops.lean @@ -1,8 +1,8 @@ /- -Copyright (c) 2025 CompPoly. All rights reserved. +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 + Natalie Klaus, Dimitris Mitsios, Alexander Hicks -/ module @@ -12,7 +12,14 @@ 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 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 @@ -110,15 +117,201 @@ 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 operations in `mulRaw` + +The sizes count the stored coefficients, including trailing zeros. -/ + +/-- 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`. 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. + + 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. 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 + 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)) + +/-- 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 [Nat.fold_push_eq_append_ofFn, Array.emptyWithCapacity_eq, Array.empty_append] + congr! 3 with k + 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`. -/ +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_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 := + 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` 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 + 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_ite_eq_sum_Ico, Finset.sum_Ico_eq_sum_range] + +/-- `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 [← Trim.coeff_eq_getElem h, coeff_mulRaw, getElem_mulRawFast] + +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 @@ -131,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/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..eb18750c 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. +`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 @@ -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..176ec746 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 (#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 ae2ffaa0..ad4c29c7 100644 --- a/docs/wiki/repo-map.md +++ b/docs/wiki/repo-map.md @@ -49,9 +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. 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 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: 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..2290a54c --- /dev/null +++ b/tests/CompPolyTests/Univariate/RawMul.lean @@ -0,0 +1,210 @@ +/- +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 Lean.Compiler.IR.EmitUtil +public meta import Mathlib.Data.ZMod.Defs +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 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. + +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 + +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 + +/-! ### Singleton factors + +`CompPolyTests.Univariate.RawSize` checks the fixed products with an empty factor. -/ + +#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 + +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] +#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) +#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 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) := + 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) + +/-- 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)] + +#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 -/ + +/-- The type of `2 × 2` matrices 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 `@[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 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 + 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 + 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