Skip to content

perf(univariate): compute each entry of a raw product as one sum - #422

Merged
alexanderlhicks merged 3 commits into
mainfrom
perf/mulraw-short-right
Nov 20, 2026
Merged

alexanderlhicks merged 3 commits into
mainfrom
perf/mulraw-short-right

Conversation

@alexanderlhicks

Copy link
Copy Markdown
Collaborator

Raw.mulRaw p q 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. When q is short, as in p * C c or p * (X - C a), the second term dominates and the cost is quadratic in p.size (#419). This PR adds Raw.mulRawFast, which builds the result in one pass and computes entry k as one sum, and connects it to mulRaw with a @[csimp] lemma.

This PR is stacked on #421, which adds the groups univariate-raw-mul-short-* and univariate-raw-mul-balanced-*. CONTRIBUTING asks for a new benchmark group in its own PR. Review the bench PR first.

Change

  • Raw.mulRawFast (CompPoly/Univariate/Raw/Ops.lean). Entry k is the sum of p[i] * q[k - i] over k + 1 - q.size ≤ i < min (k + 1) p.size, added to 0 in increasing order of i. The loop reads p and q with proved indices, so it does no bounds checks. Each pair (i, j) with i < p.size and j < q.size lands in exactly one entry, so the loop does p.size * q.size multiplications and the same number of additions. The docstring states this count from the structure of the loop. It is not a proved theorem.
  • The loop takes 0, + and * as projections of the Semiring structure. Through the default instances, code specialized at BN254.ScalarField (ZMod) called instDistribOfSemiring in every iteration of the inner loop and instMulZeroClassOfSemiring in every iteration of the outer loop. With the projections, the specialized IR has no such call. At KoalaBear.Fast.Field the specialized IR keeps unboxed u32 arithmetic.
  • The raw array is unchanged (option 1 of Raw.mulRaw does quadratic additions when the right factor is short #419). An empty p gives #[], and an empty q gives p.size - 1 zeros, so size_mulRaw and the other raw equations still hold as stated. Each product keeps the order p[i] * q[j], so no commutativity is assumed.
  • @[csimp] theorem mulRaw_eq_mulRawFast : @mulRaw = @mulRawFast. mulRaw stays the definition. The docstrings of mulRaw and mul say that compiled code runs mulRawFast.
  • The lemma sits in Raw/Ops.lean directly after mulRaw and before mul. A @[csimp] lemma does not change code that the compiler already compiled. mul, pow and powBySqUntrimmed inline or specialize mulRaw when they are compiled, so a lemma in a later module would not reach them.
  • The autoresearch target table gets a row for raw multiplication. docs/wiki/repo-map.md says where the size equations now live.

Proof

The refinement theorem closes over the lemma sets of the two sides: both arrays have the same size, and each entry equals the convolution sum ∑ i ∈ range p.size, if k < i then 0 else p.coeff i * q.coeff (k - i).

  • Implementation: size_mulRawFast and getElem_mulRawFast (both @[simp]), from the private natFold_push, natFold_add_eq_sum and sum_range_eq_sum_Ico.
  • Specification: size_mulRaw, and coeff_mulRaw (new, public, needs only [Semiring R]), from a fold induction over the rows of mulRaw that uses add_coeff? and smul_mulPowX_coeff.

The proof needs these existing lemmas in Raw/Ops.lean, so they move there. Their names and namespaces do not change, so no caller changes and no deprecated alias is needed:

Lemma From Statement
size_smul, size_mulPowX, size_addRaw, size_mulRaw Raw/Size.lean unchanged
add_coeff? Raw/Proofs.lean [Semiring Q] generalized to [AddZeroClass Q]
smul_mulPowX_coeff Raw/Proofs.lean [Semiring R] [BEq R] [LawfulBEq R] generalized to [MulZeroClass R]

Raw/Size.lean imports Raw/Ops.lean publicly and keeps size_C, size_mulRaw_le and the deprecated add_size. The note that the size facts were ported from ArkLib moves with them. All downstream proofs compile unchanged. axiomsweep reports no sorryAx. The axioms of mulRaw_eq_mulRawFast are propext, Classical.choice and Quot.sound.

The natFold_* helpers stay private. No module below Raw/Ops.lean fits them: CompPoly/Data/Array/Lemmas.lean has no big-operator import, and the repository has several similar private fold-sum lemmas (for example in Multivariate/HornerLemmas.lean and LinearAlgebra/PolynomialMatrix/StrassenCorrectness.lean). One shared module for all of them is a separate change.

Tests

tests/CompPolyTests/Univariate/RawMul.lean (new). mulRawRef repeats the defining fold of mulRaw (example : mulRawRef p q = mulRaw p q := rfl), and no @[csimp] lemma applies to it. #guard compiles its term and runs the compiler's output in the interpreter, so the checks run mulRawFast through the @[csimp] lemma. They cover:

  • empty left, empty right, both empty, and singleton factors;
  • zero and trailing-zero coefficients, including results that end in zeros;
  • a short right factor (#[c], #[-a, 1]) and a short left factor over ℤ;
  • all pairs of sizes 0 to 8 over ℕ, sizes 40 by 1 to 3 in both orders, and 24 by 24;
  • 2 × 2 matrices over ℕ, which do not commute: hand-computed products that fix the order of the factors, and all pairs of sizes 0 to 6 against mulRawRef;
  • mulRawFast called directly against mulRawRef;
  • a run_meta check that the @[csimp] map sends mulRaw to mulRawFast.

These checks do not run native code. CompPolyNativeSmoke passes, but it checks field arithmetic and does not target raw polynomial multiplication.

Mutation check: with the refinement proof replaced by sorry, 13 edits to mulRawFast were tried. Three no longer compile, because a proved index bound fails (hi := min (k + 2) …, lo := k - q.size, q[k - (lo + t) + 1]). The test module catches the factor swap, lo := k + 2 - q.size, the empty-q and empty-p changes, the lengths - 2 and + 0, dropping the first term, and removing @[csimp]. p * q + s in place of s + p * q survives; it is equivalent, because addition is commutative. The unmutated file with sorry passes.

Measurement

./scripts/bench-ab.sh run <groups> used the medium preset and five rounds per side. Both binaries ran on two hardware threads of one performance core (taskset -c 2,3). The baseline was built at 32296bd with the old fold, and the candidate at b435dc4. The benchmark commit 04fff5a differs from that baseline only in benchmark documentation. The final fix 3ec583e differs from the measured candidate only in attribution and documentation.

  • Groups: the seven univariate-raw-mul-* groups, univariate-mul-crossover-{16,64,256,1024}, univariate-mul-{koalabear,babybear} and univariate-interp-koalabear-l6.
  • Harness drift (candidate / baseline): harness-floor 1.005, harness-canary 0.981, harness-chain-floor · latency 0.992, harness-chain-floor · throughput 1.046, harness-chain-linearity · short 1.063, harness-chain-linearity · long 1.036.
  • The harness judged 69 rows. It reported 40 same, 29 faster (14 flagged SUSPECT), and 0 slower. There were no mismatches, missing rows, or unjudged rows.

The machine was shared with other Lean builds. Several unpinned runs had drift outside ±10%, and the run above has per-row spreads up to about ±30%. None of the runs had a slower row for this code.

The 16 NTT rows do not call mulRaw, so they serve as controls. They are omitted below. In this run all are same, with ratios 1.03 to 1.23. In the unpinned runs they ranged from 0.67 to 1.39. For the short-left and balanced rows classified same, the comparison detected neither a speedup nor a slowdown.

Group Row Ratio Verdict
univariate-raw-mul-short-koalabear-8 scalar-right / scalar-left 0.070 / 0.312 faster SUSPECT / faster
univariate-raw-mul-short-koalabear-8 linear-right / linear-left 0.086 / 0.213 faster SUSPECT / faster
univariate-raw-mul-short-koalabear-8 cpoly-linear-right / -left 0.177 / 0.404 faster / faster
univariate-raw-mul-short-koalabear-64 scalar-right / scalar-left 0.026 / 0.534 faster SUSPECT / faster
univariate-raw-mul-short-koalabear-64 linear-right / linear-left 0.036 / 0.404 faster SUSPECT / same
univariate-raw-mul-short-koalabear-64 cpoly-linear-right / -left 0.048 / 0.489 faster SUSPECT / faster
univariate-raw-mul-short-koalabear-1024 scalar-right / scalar-left 0.004 / 0.953 faster SUSPECT / same
univariate-raw-mul-short-koalabear-1024 linear-right / linear-left 0.006 / 0.760 faster SUSPECT / same
univariate-raw-mul-short-koalabear-1024 cpoly-linear-right / -left 0.007 / 0.851 faster SUSPECT / same
univariate-raw-mul-short-bn254-1024 scalar-right / scalar-left 0.004 / 0.629 faster SUSPECT / same
univariate-raw-mul-short-bn254-1024 linear-right / linear-left 0.005 / 0.702 faster SUSPECT / same
univariate-raw-mul-short-bn254-1024 cpoly-linear-right / -left 0.005 / 0.686 faster SUSPECT / same
univariate-raw-mul-short-int-1024 scalar-right / scalar-left 0.003 / 0.814 faster SUSPECT / same
univariate-raw-mul-short-int-1024 linear-right / linear-left 0.004 / 0.949 faster SUSPECT / same
univariate-raw-mul-short-int-1024 cpoly-linear-right / -left 0.004 / 0.879 faster SUSPECT / same
univariate-raw-mul-balanced-bn254-64 balanced / swapped 0.650 / 0.653 same / same
univariate-raw-mul-balanced-int-64 balanced / swapped 0.597 / 0.635 same / same
univariate-mul-crossover-16 / 64 / 256 / 1024 schoolbook 0.758 / 0.590 / 0.502 / 0.428 same / faster / faster / faster
univariate-mul-koalabear naive / naive-fast 0.189 / 0.420 faster / faster
univariate-mul-babybear naive / naive-fast 0.206 / 0.447 faster / faster
univariate-interp-koalabear-l6 interp-lagrange / interp-subproduct-naive 0.514 / 0.221 faster / faster

The 14 SUSPECT flags all come from the ratio falling below 0.1 on rows with the short factor on the right. Removing the quadratic term gives a reason to expect a large speedup. These checks support that explanation, with the flagged ratios still subject to measurement noise.

  • No row is mismatch, so both binaries computed the same digests. Each right-factor row also shares a digest class with the left-factor row of the same product.
  • In the candidate, the right orientation costs the same as the left orientation, in the same binary. At size 1024 the right/left ratios are 1.03, 1.06 and 0.99 on KoalaBear, 0.99, 1.00 and 0.99 on BN254, and 1.00, 0.70 and 0.73 on Int. In the baseline the same ratios are 116 to 265. The left-factor rows are not SUSPECT.
  • The candidate grows about linearly with the long factor: scalar-right grows ×5.9 from size 8 to 64 and ×14.6 from 64 to 1024. The baseline grows ×15.8 and ×88.0.
  • The specialized IR of mulRawFast at each of the three coefficient types has the multiplication and the addition inside the inner loop.

BN254 with a short left factor

An earlier version of the loop used the default instance paths. At BN254.ScalarField with a 1 × 1024 factor pair it was 1.14 to 1.29 times slower than the old fold. The specialized code built Distrib and MulZeroClass values in every iteration. Hoisting 0, + and *, or the instances, into let bindings did not help: the specializer copies a let binding that depends only on the instance back into the loop. The structure projections remove these calls from the IR. In the run above the BN254 left-factor rows have ratios 0.63 to 0.70 (same, because the runs did not separate) and the balanced BN254 rows 0.65.

Alternatives measured

  • The same loop with getD reads in place of proved indices. Against it, in two bench-ab.sh runs with drift up to 1.26 (outside ±10%), the proved-index loop had 0 slower rows. Its ratios were 0.41 to 0.65 on the KoalaBear mulRaw rows, 0.57 to 0.99 on the KoalaBear CPolynomial rows, and 0.90 to 1.15 (same) on the BN254, Int and balanced rows. Against the old fold, the getD loop read slower on the KoalaBear 1 × 1024 left rows (2.11 and 1.34) in a run with drift inside ±10%. The proved-index loop reads same on those rows.
  • With getD, two bench binaries compiled from the same mulRawFast source differed by a factor of about 2.2 on the KoalaBear 1024 × 1 and 1 × 1024 rows. The generated C of the loop was the same in both, so the difference comes from native code generation or layout. Read ratios on these rows across binaries with care.
  • Earlier candidates, measured against the old fold before any proof. All their verdicts were same, because the runs did not separate.
    • In-place accumulation into a preallocated array was slower on balanced products over KoalaBear.Fast.Field (ratio 1.08 to 1.22).
    • A fold over the shorter factor ran the original loop in every other shape.
    • Building the result with Array.ofFn was not specialized. It was slower for C c on the left of a factor of size 1024 (ratio 1.92 to 1.95).

Validation

All commands ran on this commit:

  • lake build --wfail: passes, no warnings.
  • lake test and lake build --wfail CompPolyTests: pass, no warnings.
  • lake exe axiomsweep --check: passes, 0 sorryAx, no change to scripts/axiom_baseline.json.
  • ./scripts/lint-style.sh, ./scripts/check-imports.sh: pass.
  • lake build CompPolyBenchLib CompPolyBench and lake exe CompPolyBench --medium --validate-only: pass (500 records in 135 groups).
  • CompPolyNativeSmoke with the limits in tests/README.md: passes.
  • scripts/check-docs-integrity.py: passes.
  • scripts/check-writing.py: advisory sentence-length notes only.

Not run: the CI clean rebuild and the benchmarks on the CI runner.

Follow-up, not part of this PR: coeff_mulRaw has the same right-hand side as mul_coeff_range_size in Raw/Proofs.lean and could replace the coeff_mul chain behind it.

Closes #419

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

🤖 PR Summary

Performance: compute each entry of a raw product as one sum

This PR introduces Raw.mulRawFast, a new implementation of raw polynomial multiplication that computes each entry of the result as a single sum, replacing the previous fold-over-rows algorithm. The critical connection is made via @[csimp] theorem mulRaw_eq_mulRawFast, ensuring that compiled code uses the faster routine while mulRaw remains the definitional interface.

Mathematical Formalization

  • Raw.mulRawFast (CompPoly/Univariate/Raw/Ops.lean): The core new definition. Entry k of the product is computed as the sum ∑ i, p[i] * q[k - i] over the valid index range, using a loop with proved indices (no bounds checks). The loop performs exactly p.size * q.size multiplications and the same number of additions (documented but not a proved theorem).
  • The raw array invariants are preserved: empty p yields #[], empty q yields p.size - 1 zeros, and each product keeps the order p[i] * q[j] (no commutativity assumed). The existing raw equations (size_mulRaw, etc.) continue to hold.
  • size_mulRawFast and getElem_mulRawFast (both @[simp]) are proved from private helpers (natFold_push, natFold_add_eq_sum, sum_range_eq_sum_Ico), giving size and entry lemmas for the new function.
  • coeff_mulRaw (new, public, [Semiring R]) gives the coefficient of mulRaw as the convolution sum, proved via fold induction over the rows of mulRaw using add_coeff? and smul_mulPowX_coeff.

Proof Completion (removed sorries/admits)

  • No sorry or admit placeholders are introduced in this PR. axiomsweep reports no sorryAx. The axioms of mulRaw_eq_mulRawFast are propext, Classical.choice, and Quot.sound.

Code Organization and Refactoring

  • Four theorems (size_smul, size_mulPowX, size_addRaw, size_mulRaw) and two lemmas (add_coeff?, smul_mulPowX_coeff) are moved into Raw/Ops.lean from their previous locations (Raw/Size.lean and Raw/Proofs.lean). Their names and namespaces do not change, so no downstream callers need updating. Specifically:
    • add_coeff? is generalized from [Semiring Q] to [AddZeroClass Q].
    • smul_mulPowX_coeff is generalized from [Semiring R] [BEq R] [LawfulBEq R] to [MulZeroClass R].
  • Raw/Size.lean now publicly imports Raw/Ops.lean and retains size_C, size_mulRaw_le, and the deprecated add_size. The note about the size facts being ported from ArkLib moves with them.
  • The @[csimp] lemma mulRaw_eq_mulRawFast is placed directly after mulRaw and before mul in Raw/Ops.lean. This is necessary because mul, pow, and powBySqUntrimmed inline or specialize mulRaw when compiled, so a lemma in a later module would not reach them.
  • CompPoly/Data/Array/Lemmas.lean: Adds Nat.fold_push_eq_append_ofFn (in a new Nat namespace), relating a sequential Nat.fold that pushes elements to appending an Array.ofFn.

Infrastructure / CI

  • tests/CompPolyTests/Univariate/RawMul.lean (new): Comprehensive tests using #guard (which compiles its term and runs the compiler's output, so the @[csimp] lemma is active) covering: empty factors, singleton factors, zero and trailing-zero coefficients, short factors over ℤ, all pairs of sizes 0 to 8 over ℕ, sizes 40 by 1–3 in both orders, 24×24, noncommutative 2×2 matrices over ℕ, direct calls to mulRawFast, and a run_meta check that the @[csimp] map sends mulRaw to mulRawFast. Mutation testing (13 edits) confirmed the test catches almost all plausible implementation errors.
  • tests/CompPolyTests.lean: Adds public import CompPolyTests.Univariate.RawMul.
  • docs/wiki/autoresearch.md: Adds a row for raw multiplication benchmarks.
  • docs/wiki/repo-map.md: Updated to reflect the new location of size equations.
  • CompPoly/Univariate/README.md: Updated to note that compiled code uses mulRawFast via the @[csimp] lemma.

Performance (Benchmark Results)

  • The mulRawFast loop uses projections of the Semiring structure (0, +, *) rather than default instance paths. This avoids constructing Distrib and MulZeroClass instances inside the inner loop, which was crucial for BN254.ScalarField (a ZMod type) — an earlier version using default instances was 1.14–1.29× slower than the old fold on 1 × 1024 factor pairs.
  • The benchmark (medium preset, 5 rounds, on pinned hardware threads) tested 69 rows across univariate-raw-mul-*, univariate-mul-crossover-*, univariate-mul-*, and univariate-interp-* groups. Results: 29 faster, 40 same, 0 slower, 0 mismatch. The 14 SUSPECT flags (ratio < 0.1) all occur on short-right-factor rows, where removing the quadratic term expectedly yields extreme speedups. Left-factor and balanced rows show moderate improvements or are classified same.
  • The candidate shows near-linear growth with the long factor (e.g., scalar-right grows ×5.9 from size 8→64 and ×14.6 from 64→1024), compared to quadratic growth in the baseline (×15.8 and ×88.0).
  • The specialized IR for mulRawFast at each of the three coefficient types (KoalaBear.Fast.Field, BN254.ScalarField, Int) has the multiplication and addition inside the inner loop.

No sorry or admit are introduced.


Statistics

Metric Count
📝 Files Changed 9
✅ Lines Added 436
❌ Lines Removed 67

Lean Declarations

✏️ Removed: 6 declaration(s)

CompPoly/Univariate/Raw/Proofs.lean (2)

  • lemma smul_mulPowX_coeff [LawfulBEq R] (a : R) (q : CPolynomial.Raw R) (i k : ℕ) :
  • theorem add_coeff? (p q : CPolynomial.Raw Q) (i : ℕ) :

CompPoly/Univariate/Raw/Size.lean (4)

  • theorem size_addRaw {Q : Type*} [Zero Q] [Add Q] {p q : Raw Q} :
  • theorem size_mulPowX {R : Type*} [Zero R] (i : Nat) (p : Raw R) :
  • theorem size_mulRaw {R : Type*} [Semiring R] (p q : Raw R) :
  • theorem size_smul {R : Type*} [Mul R] (x : R) (p : Raw R) :
✏️ Added: 30 declaration(s)

CompPoly/Data/Array/Lemmas.lean (1)

  • theorem fold_push_eq_append_ofFn {α : Type*} {n : ℕ} (e : (k : ℕ) → k < n → α)

CompPoly/Univariate/Raw/Ops.lean (14)

  • def mulRawFast [inst : Semiring R] (p q : CPolynomial.Raw R) : CPolynomial.Raw R
  • private theorem mulRawFast_eq_ofFn [Semiring R] {p : CPolynomial.Raw R} (q : CPolynomial.Raw R)
  • private theorem natFold_add_eq_sum [AddCommMonoid R] (n : ℕ) (g : ℕ → R) :
  • private theorem sum_range_ite_eq_sum_Ico [Semiring R] (p q : CPolynomial.Raw R) (k : ℕ) :
  • theorem add_coeff? [AddZeroClass Q] (p q : CPolynomial.Raw Q) (i : ℕ) :
  • theorem coeff_mulRaw [Semiring R] (p q : CPolynomial.Raw R) (k : ℕ) :
  • theorem getElem_mulRawFast [Semiring R] (p q : CPolynomial.Raw R) (k : ℕ)
  • theorem mulRaw_eq_mulRawFast : @mulRaw = @mulRawFast
  • theorem size_addRaw [Zero Q] [Add Q] {p q : CPolynomial.Raw Q} :
  • theorem size_mulPowX [Zero R] (i : Nat) (p : CPolynomial.Raw R) :
  • theorem size_mulRaw [Semiring R] (p q : CPolynomial.Raw R) :
  • theorem size_mulRawFast [Semiring R] (p q : CPolynomial.Raw R) :
  • theorem size_smul [Mul R] (x : R) (p : CPolynomial.Raw R) :
  • theorem smul_mulPowX_coeff [MulZeroClass R] (a : R) (q : CPolynomial.Raw R) (i k : ℕ) :

tests/CompPolyTests/Univariate/RawMul.lean (15)

  • abbrev M
  • def a : M
  • def b : M
  • def isMulRawFastCode (n : Name) : Bool
  • def matInput (n : ℕ) (shift : ℕ) : Raw M
  • def mulRawRef {R : Type*} [Semiring R] (p q : Raw R) : Raw R
  • def natInput (n : ℕ) : Raw ℕ
  • def natMul (p q : Raw ℕ) : Raw ℕ
  • def natPow (p : Raw ℕ) (n : ℕ) : Raw ℕ
  • def natPowBySq (p : Raw ℕ) (n : ℕ) : Raw ℕ
  • def zmodInput (n shift : ℕ) : Raw (ZMod 6)
  • def zmodMatInput (n shift : ℕ) : Raw (Matrix (Fin 2) (Fin 2) (ZMod 4))
  • def zmodMul (p q : Raw (ZMod 6)) : Raw (ZMod 6)
  • example {R : Type*} [Semiring R] (p q : Raw R) : mulRawRef p q = mulRaw p q
  • partial def reachableDecls (env : Environment) (root : Name) : NameSet

sorry Tracking

  • No sorrys were added, removed, or affected.

📋 **Additional Analysis**

Findings

Style and Naming Guideline Violations

  • Invalid syntax in test file (tests/CompPolyTests/Univariate/RawMul.lean):

    • public meta import is not valid Lean 4 syntax. Use import instead of meta import. This appears four times.
    • import all is not a valid module; presumably import CompPoly or similar is intended.
    • public meta section is also invalid; meta is a command, not a section modifier. Use a plain section or meta block without public.
    • These errors will prevent the file from compiling, violating the requirement that lake build must pass.
  • Removal of lemmas without deprecation (CompPoly/Univariate/Raw/Proofs.lean):

    • add_coeff? and smul_mulPowX_coeff are removed without a @[deprecated] alias. If any downstream code depended on these names from Proofs, it will break. The deprecation policy requires keeping the old name as a deprecated alias when a declaration is moved or renamed.
    • Similarly, size_smul, size_mulPowX, size_addRaw, and size_mulRaw are removed from Size.lean without deprecation aliases (except add_size which was already deprecated).
  • Potential line length violations: Some lines in the test file (e.g., #guard (List.range 9).all fun m ↦ (List.range 9).all fun n ↦) may exceed the 100-character limit. The diff does not show exact column numbers, but these lines appear long. The style guide requires .lean lines under 100 characters.

Performance Contribution Rule Checks

  • One change per PR – The PR combines the optimization (mulRawFast) with mechanical moves of lemmas and documentation updates. While these are conceptually related, the mechanical changes could have been separated. However, the primary topic is the optimization, so this is acceptable.
  • No A/B evidence in the diff – The diff does not include the required compare-*.md table or benchmark results. The PR description (not in the diff) should supply this evidence. Absent the description, we cannot verify compliance.
  • Test coverage – The test file (RawMul.lean) appropriately checks concrete inputs, zero divisors, noncommutative coefficients, and the @[csimp] application. This satisfies the testing requirements.
  • Proof structure – The @[csimp] lemma mulRaw_eq_mulRawFast is written with @[simp] lemmas for sizes and getElem, following the advice to make proofs survive future iterations.
  • Placement of csimp lemma – The lemma is correctly placed before mul and powBySqUntrimmed so that compiled definitions use mulRawFast.

Documentation Consistency

  • docs/wiki/autoresearch.md is updated to include a row for "Raw multiplication". The entry references the correct file and lemma.
  • docs/wiki/repo-map.md is updated to describe the new location of size equations (in Ops.lean rather than Size.lean). The description matches the diff.
  • CompPoly/Univariate/README.md is updated to mention mulRawFast and the @[csimp] lemma, consistent with the code.

Other Observations

  • The new theorem Nat.fold_push_eq_append_ofFn in CompPoly/Data/Array/Lemmas.lean is well-named and correctly placed.
  • The copyright and author lines are updated appropriately, including attributing the ported code from ArkLib.
  • The diff does not introduce any sorry or axiom violations; lake exe axiomsweep --check should pass if the code builds.

Summary

The most critical issues are the invalid syntax in the test file (public meta import, import all, public meta section), which would break the build, and the removal of lemmas without deprecation. The performance contribution rules are otherwise followed in substance, though the benchmark evidence is missing from the diff (presumably in the PR description). The documentation updates are consistent.


📄 **Per-File Summaries**
  • CompPoly/Data/Array/Lemmas.lean: Added theorem Nat.fold_push_eq_append_ofFn (inside a new Nat namespace) which proves that n.fold (fun k h acc ↦ acc.push (e k h)) init = init ++ Array.ofFn fun k : Fin n ↦ e k k.2. This relates a sequential Nat.fold that pushes elements depending on a proof k < n to appending an Array.ofFn constructed from the same data, using Nat.fold_succ, Array.ofFn_succ, and Array.push_append.
  • CompPoly/Univariate/README.md: Updated CompPoly/Univariate/README.md's description of Raw/Ops.lean to add a sentence noting that compiled code uses mulRawFast for mulRaw, justified by a @[csimp] lemma mulRaw_eq_mulRawFast.
  • CompPoly/Univariate/Raw/Ops.lean: This diff updates CompPoly/Univariate/Raw/Ops.lean with significant additions and modifications. It revises the module header to acknowledge a new ArkLib copyright and to document that size_smul, size_mulPowX, size_addRaw, and size_mulRaw were ported from ArkLib. The diff adds a new SizeCoeffLemmas section containing size_smul, size_mulPowX, size_addRaw, add_coeff?, smul_mulPowX_coeff, size_mulRaw, and coeff_mulRaw — theorems that compute sizes and coefficients for smul, mulPowX, addRaw, and mulRaw. It also adds a new MulRawFast section defining mulRawFast (a loop that computes each entry of raw multiplication as a single sum), along with supporting private lemmas mulRawFast_eq_ofFn, natFold_add_eq_sum, sum_range_ite_eq_sum_Ico, and the theorems size_mulRawFast and getElem_mulRawFast. The critical @[csimp] lemma mulRaw_eq_mulRawFast establishes that mulRaw equals mulRawFast, ensuring compiled code uses the faster implementation. The docstrings for mulRaw, mul, and powBySq are updated to note this compilation rewrite. No sorry or admit are introduced.
  • CompPoly/Univariate/Raw/Proofs.lean: The diff removes two lemmas: add_coeff? (which stated (addRaw p q).coeff i = p.coeff i + q.coeff i for all i, without a size restriction) and smul_mulPowX_coeff (which gave the coefficient of (smul a q).mulPowX i as if k < i then 0 else a * q.coeff (k - i)). Both declarations are no longer present in the file; any proofs that relied on them will need to be updated or replaced. No new lemmas are added.
  • CompPoly/Univariate/Raw/Size.lean: The file header now explains that CompPoly.Univariate.Raw.Ops (publicly imported here) states the size equations for smul, mulPowX, addRaw, and mulRaw; consequently the four theorems size_smul, size_mulPowX, size_addRaw, and size_mulRaw have been removed from this file. The bound size_mulRaw_le remains, and the deprecated alias add_size (pointing to the now-removed size_addRaw) is kept for compatibility.
  • docs/wiki/autoresearch.md: This documentation change adds a new table row for the raw multiplication (mulRawFast) benchmark in the autoresearch.md project wiki. The row lists relevant benchmark targets (univariate-raw-mul-short-*, univariate-raw-mul-balanced-*, univariate-mul-crossover-*), source file (CompPoly/Univariate/Raw/Ops.lean), and the mulRaw_eq_mulRawFast theorem with the @[csimp] attribute, noting this is the first loop run addressing PRs perf(univariate): compute each entry of a raw product as one sum #422 and Raw.mulRaw does quadratic additions when the right factor is short #419.
  • docs/wiki/repo-map.md: Updated the description of where representation-size facts live: the size equations for smul, mulPowX, addRaw, and mulRaw (including multiplication padding) are now in Univariate/Raw/Ops.lean, while Univariate/Raw/Size.lean publicly imports that module and adds the specific lemmas size_C and the bound size_mulRaw_le.
  • tests/CompPolyTests.lean: Added a public import CompPolyTests.Univariate.RawMul statement, bringing in a new test module for raw multiplication of univariate polynomials. This complements the existing imports for raw operations (Raw, RawSize) and likely provides tests for the basic polynomial multiplication routine. No new definitions or sorrys appear in this diff.
  • tests/CompPolyTests/Univariate/RawMul.lean: This file adds comprehensive tests for CPolynomial.Raw.mulRaw across various coefficient types including ℕ, ℤ, ZMod 6, and 2×2 matrices. It defines mulRawRef as a reference implementation matching the defining fold, then uses #guard to verify mulRaw and mulRawFast against this reference for singleton factors, zero/trailing-zero coefficients, short factors, balanced inputs, zero divisors, and noncommutative coefficients. Finally, it uses run_meta blocks to check that the @[csimp] lemma correctly maps mulRaw to mulRawFast, that compiled code of natMul, natPow, natPowBySq, and zmodMul reaches mulRawFast, and that the specialized mulRawFast for ZMod 6 does not construct Distrib or MulZeroClass instances.

Last updated: 2026-11-20 02:07 UTC.

@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Build Timing Report

  • Commit: c14b52f
  • Message: Merge 0bdde4b into 88d85f0
  • Ref: perf/mulraw-short-right
  • Comparison baseline: d3b1a9c from the previous successful PR update.
  • Measured on ubuntu-latest with /usr/bin/time -p.
  • Commands: library build (warm) lake build; test path lake test.
  • Clean build was skipped (warm/incremental CI; no toolchain or lake-manifest change). Runs automatically when lean-toolchain or lake-manifest.json changes, or via Actions → Lean Action CI → Run workflow with clean_build.
Measurement Baseline (s) Current (s) Delta (s) Status
Library build (warm) 179.93 150.27 -29.66 ok
Test path 32.23 25.64 -6.59 ok

Incremental Rebuild Signal

  • Warm-only run: default CI reuses cached oleans and rebuilds dirty modules only. No same-job clean:warm ratio.

Slowest Current Build Files (warm library build)

Showing 20 slowest current targets from the warm library build log, with comparison against the baseline warm library build log when available.

Current (s) Baseline (s) Delta (s) Path
8.40 8.90 -0.50 CompPoly/Bivariate/GuruswamiSudan/Root/Alekhnovich/Correctness.lean
7.10 8.50 -1.40 CompPoly/Univariate/ReedSolomon/GaoCorrectness.lean
7.00 7.90 -0.90 CompPoly/Univariate/NTTFast/Correctness/Radix4DIF.lean
6.80 7.30 -0.50 CompPoly/Univariate/NTT/Forward.lean
6.80 7.70 -0.90 CompPoly/Bivariate/ToPoly.lean
6.70 7.20 -0.50 CompPoly/Univariate/NTTFast/Correctness/Radix4DIT.lean
6.30 7.90 -1.60 CompPoly/Bivariate/Factor.lean
6.30 8.70 -2.40 CompPoly/LinearAlgebra/PolynomialMatrix/Approximant/ModularEquation/Completeness.lean
5.90 6.70 -0.80 CompPoly/Univariate/NTTFast/Correctness/DIF.lean
5.90 7.20 -1.30 CompPoly/Bivariate/Deriv.lean
5.90 6.80 -0.90 CompPoly/Bivariate/GuruswamiSudan/Interpolation/WitnessDivisibilityCorrectness.lean
5.70 6.80 -1.10 CompPoly/Univariate/Basic.lean
5.20 6.50 -1.30 CompPoly/Univariate/Quotient/Core.lean
5.20 4.70 +0.50 CompPoly/Bivariate/GuruswamiSudan/Interpolation/LeeOSullivan/Correctness/Completeness.lean
5.10 4.80 +0.30 CompPoly/Univariate/DivisionCorrectness.lean
4.90 5.00 -0.10 CompPoly/LinearAlgebra/PolynomialMatrix/MuldersStorjohannCorrectness/RowOps.lean
4.90 5.70 -0.80 CompPoly/Bivariate/GuruswamiSudan/PolynomialCorrectness.lean
4.70 5.00 -0.30 CompPoly/Univariate/NTTFast/Plan.lean
4.60 5.70 -1.10 CompPoly/LinearAlgebra/PolynomialMatrix/Approximant/PMBasis/Correctness.lean
4.30 4.80 -0.50 CompPoly/Univariate/Raw/Proofs.lean

alexanderlhicks added a commit that referenced this pull request Nov 19, 2026
…cement

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 <noreply@anthropic.com>
@alexanderlhicks

Copy link
Copy Markdown
Collaborator Author

This PR is now rebased onto the fix-up of #421 (347f09b). The rebased commit, cdd84cf, has the same diff as 9a9d74d. A fix-up commit, d3b1a9c, makes these changes:

  • The refinement proof closes with one simp only over the named lemmas Trim.coeff_eq_getElem, coeff_mulRaw and getElem_mulRawFast.
  • natFold_push is now the public Nat.fold_push_eq_append_ofFn in CompPoly/Data/Array/Lemmas.lean.
  • The tests add ZMod 6 (all size pairs up to 11) and 2 × 2 matrices over ZMod 4 (up to 7), two coefficient rings with zero divisors. A new check reads the compiled code of mul and fails if it does not call mulRawFast. I checked that this check fails when the @[csimp] attribute moves to the end of Raw/Ops.lean.
  • The other changes are text only: the copyright header, the module and @[csimp] docstrings, the test prose, docs/wiki/repo-map.md and the autoresearch row.

The fix-up changes no definition under CompPoly/, and 347f09b changes no timed body, so the measurements in the description still apply.

Validation at d3b1a9c, run locally:

  • lake build --wfail: passes.
  • lake test: passes with 0 warnings. lake build --wfail CompPolyTests: passes.
  • lake exe axiomsweep --check: passes.
  • ./scripts/lint-style.sh and ./scripts/check-imports.sh: pass.
  • scripts/check-docs-integrity.py: passes.
  • lake build CompPolyBenchLib CompPolyBench and lake exe CompPolyBench --medium --validate-only: pass (500 records in 135 groups).
  • The CI command for bench/ci-groups.txt, copied from .github/workflows/lean_action_ci.yml: passes (292 records in 73 groups).
  • CompPolyNativeSmoke with the limits in tests/README.md: passes.

Corrections to the description

Timing claims. The pinned run (5 rounds per side, medium preset, harness drift 0.98 to 1.06) judged 69 rows: 15 faster without a flag, 14 faster with SUSPECT, 40 same and 0 slower. Read the 15 unflagged rows first:

Group Row Ratio
univariate-raw-mul-short-koalabear-8 cpoly-linear-right 0.177
univariate-raw-mul-short-koalabear-8 scalar-left / linear-left / cpoly-linear-left 0.312 / 0.213 / 0.404
univariate-raw-mul-short-koalabear-64 scalar-left / cpoly-linear-left 0.534 / 0.489
univariate-mul-crossover-64 / -256 / -1024 schoolbook 0.590 / 0.502 / 0.428
univariate-mul-koalabear naive / naive-fast 0.189 / 0.420
univariate-mul-babybear naive / naive-fast 0.206 / 0.447
univariate-interp-koalabear-l6 interp-lagrange / interp-subproduct-naive 0.514 / 0.221

The 14 SUSPECT rows are the short-right rows with a ratio below 0.1 (0.003 to 0.086). docs/wiki/autoresearch.md says that this flag can mean that the compiler removed work from the timed path. The digest check does not exclude that, because it runs on a separate, untimed pass. Treat these ratios as diagnostics. The following facts agree with a removed quadratic term:

  • In the candidate binary, at size 1024, the right rows cost 0.70 to 1.06 times the left rows of the same product. In the baseline binary, the same ratios are 116 to 265.
  • On KoalaBear, scalar-right grows ×5.9 from size 8 to 64 and ×14.6 from 64 to 1024 in the candidate. In the baseline it grows ×15.8 and ×88.0.
  • The left rows do linear work in both binaries, and none of them is flagged.

The rows that use NTT multiplication or NTT interpolation serve as controls. There are 19 of them, not 16: 16 NTT multiplication rows and 3 NTT interpolation rows. All 19 are same, with ratios 1.03 to 1.23. That run detected no slowdown on them, but all 19 ratios are above 1. For the short-left and balanced rows that are same, the run detected neither a speedup nor a slowdown.

Measured commits. The two binaries of this run were built at 32296bd (baseline) and b435dc4 (candidate). Neither commit is published.

In those binaries the cpoly-linear-* rows have the compare key · cpoly-linear. After 347f09b the key is · linear.

Mutation check. The run had 12 mutants: 11 edits to mulRawFast and the removal of @[csimp]. Three fail to compile. The tests catch eight. One survives, p * q + s in place of s + p * q, which is equivalent because addition is commutative. The unmutated file is a control and passes. The run was at c846a7e, an unpublished commit whose library and test code are those of b435dc4.

Other corrections.

  • In the candidate, the right order of a product costs no more than the left order, not "the same". The Int ratios for linear and cpoly-linear are 0.70 and 0.73.
  • The rows that read slower for the getD loop against the old fold are scalar-left (2.11) and linear-left (1.34) of univariate-raw-mul-short-koalabear-1024. The second is a 2 × 1024 product.
  • The getD comparison runs had harness drift up to 1.26, outside ±10%. CONTRIBUTING says to repeat such runs, not to read them, so their ratios are indicative only.
  • Every verdict for the earlier candidates was same, so the runs do not show that any of them was slower. The Array.ofFn ratios of 1.92 and 1.95 are on the scalar-left row of univariate-raw-mul-short-koalabear-1024. On that row, two builds with the same mulRawFast source and different benchmark modules differed by a factor of about 2.2.
  • The @[csimp] lemma follows mulRaw and the size and coefficient lemmas, and precedes mul. mul and powBySqUntrimmed inline or specialize mulRaw. pow does not: its compiled code calls mul.
  • The implementation lemmas also include the private mulRawFast_eq_ofFn. The missing big-operator import of CompPoly/Data/Array/Lemmas.lean is a reason to keep natFold_add_eq_sum private, but not natFold_push.
  • The BN254 figures 1.14 to 1.29 come from two runs of one binary, which compared the earlier loop with a copy of the old fold. They are not a bench-ab.sh verdict. Across binaries, the same rows read same.
  • add_coeff? and smul_mulPowX_coeff keep their names and namespaces, but their instance arguments change. add_coeff? takes [AddZeroClass Q] in place of [Semiring Q]. smul_mulPowX_coeff takes [MulZeroClass R] in place of [Semiring R] [BEq R] [LawfulBEq R]. A caller that passes these instances explicitly with @ can need an edit.
  • The loop does p.size * q.size multiplications and as many additions. With a short factor on either side, this count is linear in the stored length of the long factor. The test prose now says that the output has p.size + q.size - 1 entries for a nonempty p.

alexanderlhicks and others added 3 commits November 20, 2026 01:23
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 <noreply@anthropic.com>
…cement

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 <noreply@anthropic.com>
… 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 <noreply@anthropic.com>
@alexanderlhicks
alexanderlhicks changed the base branch from bench/raw-mul-short to main November 20, 2026 02:01
@alexanderlhicks
alexanderlhicks merged commit c72a765 into main Nov 20, 2026
7 checks passed
@alexanderlhicks
alexanderlhicks deleted the perf/mulraw-short-right branch November 20, 2026 02:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Raw.mulRaw does quadratic additions when the right factor is short

1 participant