Skip to content

perf(poly): parallel one-point evaluation against arkworks - #398

Draft
mitschabaude-bot wants to merge 107 commits into
Verified-zkEVM:mainfrom
mitschabaude-bot:poly-eval-ark
Draft

mitschabaude-bot wants to merge 107 commits into
Verified-zkEVM:mainfrom
mitschabaude-bot:poly-eval-ark

Conversation

@mitschabaude-bot

@mitschabaude-bot mitschabaude-bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Depends on #395, which depends on #390. Review and merge those first. This branch starts at #395's tip. GitHub cannot use a fork branch as an upstream base, so this draft targets main and its diff includes both earlier PRs. Review only the new changes.

Verified multicore evaluation of one polynomial at one point, benchmarked against an existing fast implementation: arkworks' stock DensePolynomial::evaluate (ark-poly 0.5.0 with its parallel feature), and against CompPoly's existing sequential evalHorner.

  • CPolynomial.evalFast x p logWorkers splits contiguous coefficient ranges without copying, evaluates the leaves concurrently with lazy-reduction Horner kernels (KoalaBear, Goldilocks, BN254 scalar) and joins with square-and-multiply powers. evalFast_eq_evalHorner and evalFast_eq_eval prove it correct over any semiring.
  • The calling thread evaluates the lowest leaf itself and spawns the upper subtrees along the tree's lower edge. With as many CPUs as workers, a caller that only waited left one woken worker queued behind another; this change halved four-worker times (KoalaBear 1.94 → 0.95 ms at 2²⁰).
  • No leaf is smaller than the kernel's EvalKernel.minLeaf (8,192 coefficients by default, 1,024 for BN254), so small polynomials stay on the calling thread instead of paying for tasks.
  • Parallel runs warm up for 200 ms on both sides. With the usual 50 ms, idle worker cores were often still at reduced clocks when timing began, which made eight-worker evalFast measure 0.50 or 0.85 ms depending on the run.
  • --suite poly-eval runs Lean evalHorner, Lean evalFast, and ark-poly on one thread and on the selected worker count, from identical fixtures; all four results must agree before timing.
  • arkworks ships no KoalaBear or Goldilocks, so the Rust side defines them with arkworks' generic Montgomery backend (Fp64 via #[derive(MontConfig)]); those rows compare against stock arkworks code, not Plonky3-grade field arithmetic. BN254 uses ark_bn254::Fr. Binary-tower rows are gone, since arkworks cannot represent the field.

Results

One million coefficients, eight workers on the eight physical cores of a Ryzen 7 3700X (no SMT siblings). Milliseconds per evaluation; median of five paired run medians, each after 200 ms of warmup. Lean / ark-poly > 1 means arkworks is faster.

Field Lean evalHorner (1 core) Lean evalFast (8 workers) ark-poly (8 threads) Lean / ark-poly
KoalaBear 3.95 0.51 0.54 0.94×
Goldilocks 3.88 0.46 0.67 0.70×
BN254 scalar 40.94 4.03 3.34 1.21×

evalFast is 8–10× faster than sequential evalHorner and beats ark-poly on KoalaBear and Goldilocks. On BN254 it trails by 1.2×, all of it field arithmetic: Lean's sequential Horner is already 1.6× slower than arkworks' BN254 multiplication.

Measured source: 7b8d30d (the working tree differed only in an uncommitted scratch benchmark). Lean 4.34.0; Rust 1.95.0-nightly with -C target-cpu=native. Shared host, load about 0.5–2.5.

Reproduce

python3 scripts/bench-fields.py --suite poly-eval
python3 scripts/bench-fields.py --suite poly-eval --cpus 0,1,2,3
python3 scripts/bench-fields.py --suite all --validate-only

graikos and others added 30 commits September 29, 2026 07:33
…ocks

Fold the 64x64 product's middle terms without carry bookkeeping so clang emits
one widening multiply for squarings too; `mul x x` inlined into `square` used
to compile to a four-imul chain. Run the Fermat inverse and the pow ladder on
unreduced words (`mulLazy`, `squareNLazy`, `invLazy`, `powLazy`) with a single
canonicalization at the end, and make `squareN` tail recursive. Correctness
rests on word-level cast lemmas (`foldUInt128Lazy_cast`, `mulLazy_cast`,
`squareNLazy_cast`, `invLazy_cast`, `powLazy_cast`).

A/B against master on the same core: inv 311 -> 167 ns, pow 99 -> 74 ns,
mul and add unchanged.
`conditionalSubtract` was a plain def, so every KoalaBear and BabyBear `add`
paid an out-of-line call. A/B: add latency 844 -> 606 ps, throughput
876 -> 166 ps.
…nreduced words

`univariate-dense-goldilocks` now runs the native-word carrier beside ZMod like
the other fast fields, with rows named in the shared `-fast` scheme. Add tests
that unreduced `ofUInt64` words compare and multiply as their residues.
Compute the canonical sum as `x - (p - y)`: the subtraction borrows exactly
when `x + y < p`, so one borrow-selected correction by `2^32 - 1` yields the
reduced result, three dependent instructions instead of a carry check plus a
separate compare against `p`. `addRaw_toNat` carries the bound and the cast
lemma; the carry-based reducer and its lemmas are removed.

A/B against master on the same core: add latency 1021 -> 616 ps, throughput
381 -> 230 ps; mul, inv and pow unchanged.
# Conflicts:
#	bench/CompPolyBench/Fields/Arith.lean
… the Goldilocks fold

Two changes to the 128-bit fold behind every fast Goldilocks multiply. The borrow
of the low word against the top limb has probability about 2^-32, but a Lean `if`
compiled to a select on the critical path; moving that arm into a `@[noinline]`
function forces a predicted branch. The middle term `hi_lo * (2^32 - 1)` compiled
to a three-cycle `imul` inside loops (clang holds the constant in a register); it
is now `(hi <<< 32) - hi_lo`, two one-cycle operations. `shiftLeft32_sub_low` and
`foldTail_cast` carry the proofs; the fold lemma cases on the borrow.

A/B against master on the same core: mul latency 3077 -> 2485 ps, throughput
815 -> 739 ps, inv 314 -> 136 ns, pow 101 -> 64 ns; add unchanged.
One-sentence docstrings, `2 ^ k` literals instead of decimals, a `_toNat`
bridge for the product's high word, implicit bounds on the `addRaw` lemmas,
camelCase limb names, and README rows for the Goldilocks files.
A generated kernel runs the last four DIF layers of eight leaf blocks at once,
one block per vector lane, and writes the leaf in natural order. It replaces
the scalar leaf16 kernel and the separate leafRev pass for even leaves of at
least 2^8 words (2^16 leaf: 0.63 -> 0.48 ms). The slice tree takes the leaf
computation as a parameter; NativeLastCorrectness proves the fused leaf equal
to leafRev after stages, and runPacked/runFields with their DFT theorems
move there. 2^20 packed I/O: 14.8 -> 12.4 ms on one worker, 4.10 -> 3.47 ms
on four.
pairGo and pairInputGo now check the whole batch range once and run a
USize loop whose proof carries the remaining range, instead of checking
and converting indices on every sixteen-word batch. Packed-io NTT at one
million points: 5-7% faster on one and four workers.
inner16 now checks the range of all whole sixteen-cell batches once and
runs inner16Loop over USize indices before the scalar tail. The proof
uses new size lemmas for overwriting stores and step16. Packed-io NTT at
one million points: about 5% faster on one and four workers.
The one-point evaluation work (EvalFast, its lazy field kernels, the
matched Rust evaluator and the poly-eval benchmark suite) moves to the
poly-eval branch, to return against stock reference implementations in
its own PR. This PR keeps the proved parallel NTTs and their Plonky3
comparison.
Restores the parallel one-point evaluation work on its own branch and
replaces the hand-written Rust evaluators with ark-poly's
DensePolynomial::evaluate (parallel feature) in a Rayon pool of the
selected size; one thread gives its plain Horner path. KoalaBear and
Goldilocks use arkworks' generic Montgomery fields, since arkworks ships
neither. The binary tower rows are dropped, as arkworks cannot represent
the field.
With as many CPUs as workers, the caller was still running when the
leaves were spawned, so one woken worker usually queued behind another
and its leaf started only when the first finished. The caller now walks
the tree's lower edge, spawning the upper subtrees, and evaluates the
lowest leaf itself. Million-coefficient KoalaBear on four workers: 1.94
to 0.95 ms.
With the 50 ms warmup, idle worker cores were often still at reduced
clocks when timing began, so eight-worker evalFast measured 0.50 or
0.85 ms depending on the run's calibration. The poly-eval suite now uses
the large budget (200 ms warmup, 50 samples) in Lean and Rust.
EvalKernel gains minLeaf, the smallest range worth a task (8192
coefficients by default, 1024 for BN254), and evalFast caps its depth
so that no leaf is smaller. Small polynomials are evaluated on the
calling thread with the lazy leaf kernel.
@github-actions

github-actions Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

🤖 PR Summary

ℹ️ Some sections were omitted to fit GitHub's comment size limit.

Failed to generate AI summary. Please check the per-file summaries and statistics below.


Statistics

Metric Count
📝 Files Changed 128
✅ Lines Added 19987
❌ Lines Removed 1182

Lean Declarations

✏️ Removed: 35 declaration(s)

CompPoly/Fields/BLS12_377/Fast.lean (1)

  • instance instMont64x8Field : Mont64x8Field BLS12_377.scalarFieldSize where

CompPoly/Fields/BLS12_381/Fast.lean (1)

  • instance instMont64x8Field : Mont64x8Field BLS12_381.scalarFieldSize where

CompPoly/Fields/BN254/Fast.lean (1)

  • instance instMont64x8Field : Mont64x8Field BN254.scalarFieldSize where

CompPoly/Fields/Goldilocks/Fast.lean (2)

  • private theorem mul_assoc_field (x y z : Field) : (x * y) * z = x * (y * z)
  • private theorem pow_succ (x : Field) (n : Nat) : pow x (n + 1) = pow x n * x

CompPoly/Fields/Goldilocks/FastDefs.lean (1)

  • def reduceAddWithCarryRaw (lo : UInt64) (carry : Bool) : UInt64

CompPoly/Fields/Goldilocks/FastReduction.lean (6)

  • theorem addWithCarry_bound (x y : UInt64)
  • theorem addWithCarry_value (x y : UInt64) :
  • theorem reduceAddWithCarryRaw_cast (lo : UInt64) (carry : Bool)
  • theorem reduceAddWithCarryRaw_lt (lo : UInt64) (carry : Bool)
  • theorem wideMul_high_nat
  • theorem wideMul_high_toNat

CompPoly/Fields/Montgomery/Native64x8.lean (1)

  • private theorem carry_top_zero {S A B Q c : ℕ} (h : S + 2 ^ 256 * c = A + B)

CompPoly/Fields/Montgomery/Native64x8Field.lean (1)

  • theorem two_mul_q_lt : 2 * P.modulusLimbs.toNat < 2 ^ 256

CompPoly/Fields/Pasta/Fast.lean (1)

  • instance instMont64x8Field : Mont64x8Field Vesta.baseFieldSize where

CompPoly/Fields/Secp256k1.lean (7)

  • abbrev BaseField
  • abbrev ScalarField
  • def BASE_FIELD_CARD : Nat
  • def SCALAR_FIELD_CARD : Nat
  • example :
  • theorem BaseField_is_prime : Nat.Prime BASE_FIELD_CARD
  • theorem ScalarField_is_prime : Nat.Prime SCALAR_FIELD_CARD

bench/CompPolyBench/Fields/Binary/Tower.lean (7)

  • @[inline] private def towerSampler {E : Type} (xs : Array E) (one : E) : Nat → E × E
  • @[specialize] private def runTowerGroup (groupKey title method : String)
  • def checksumConcreteBt128 (x : ConcreteBTField 7) : Nat
  • def checksumFastBT128 (x : Fast.FastBT128) : Nat
  • private def runTowerInv (preset : BenchPreset) (gen : StdGen) :
  • private def runTowerMul (preset : BenchPreset) (gen : StdGen) :
  • private def towerShape : String

bench/CompPolyBench/Fields/Binary/Tower/Scalar.lean (5)

  • private def runTowerInv64 (preset : BenchPreset) (gen : StdGen) :
  • private def runTowerMul64 (preset : BenchPreset) (gen : StdGen) :
  • private def runTowerMul8 (preset : BenchPreset) (gen : StdGen) :
  • private def towerWordPool (bits : Nat) (gen : StdGen) : Array UInt64 × StdGen
  • private def towerWordRep (suffix representation field : String) (pool : Array UInt64) :

bench/CompPolyBench/Fields/Inputs.lean (1)

  • @[inline] def sinkMont64x8 {modulus : Nat} [Montgomery.Native64x8.Mont64x8Field modulus]
✏️ Added: 944 declaration(s)

CompPoly/Data/Array/Involution.lean (9)

  • @[inline] def permuteInvolution (order : Array Nat) (a : Array α) : Array α
  • @[inline] private def involutionStep (f : Nat → Nat) (a : Array α) (i : Nat) : Array α
  • @[simp] private theorem size_involutionStep (f : Nat → Nat) (a : Array α) (i : Nat) :
  • @[simp] theorem size_permuteInvolution (order : Array Nat) (a : Array α) :
  • private theorem getD_swap (a : Array α) (i j k : Nat) (d : α)
  • private theorem permuteInvolution_eq_fold (order : Array Nat) (a : Array α) :
  • private theorem prefix_invariant (f : Nat → Nat) (a : Array α) (d : α)
  • private theorem size_fold (f : Nat → Nat) (is : List Nat) (a : Array α) :
  • theorem permuteInvolution_eq_map (order : Array Nat) (a : Array α) (d : α)

CompPoly/Data/ByteArray/Pack.lean (12)

  • @[simp] theorem size_setUInt32LE! (a : ByteArray) (off : Nat) (v : UInt32) :
  • @[simp] theorem size_usetUInt32LE (a : ByteArray) (i : USize) (v : UInt32) (h) :
  • def getUInt32LE : (a : @& ByteArray) → (i : @& Nat) → (h : i + 4 ≤ a.size
  • def getUInt32LE! (a : @& ByteArray) (off : @& Nat) : UInt32
  • def setUInt32LE : (a : ByteArray) → (i : @& Nat) → (v : UInt32) →
  • def setUInt32LE! (a : ByteArray) (off : @& Nat) (v : UInt32) : ByteArray
  • def ugetUInt32LE : (a : @& ByteArray) → (i : USize) →
  • def usetUInt32LE : (a : ByteArray) → (i : USize) → (v : UInt32) →
  • theorem get!_eq_getElem! (a : ByteArray) (i : Nat) : a.get! i = a[i]!
  • theorem getElem!_setUInt32LE!_of_outside (a : ByteArray) (o j : Nat) (v : UInt32)
  • theorem getUInt32LE!_setUInt32LE!_of_disjoint (a : ByteArray) (o₁ o₂ : Nat) (v : UInt32)
  • theorem getUInt32LE!_setUInt32LE!_self (a : ByteArray) (off : Nat) (v : UInt32)

CompPoly/Data/ByteArray/Packed.lean (75)

  • @[inline, reducible] def ofWord32 {R : Type u} (toWord : R → UInt32) (ofWord : UInt32 → R)
  • @[inline] def getAt (b : ByteArray) (k : USize) (hk : k.toNat < b.size / width R)
  • @[inline] def loadWord (b : ByteArray) (i : USize) (h : i.toNat + 4 ≤ b.size)
  • @[inline] def setAt (b : ByteArray) (k : USize) (x : R) (hk : k.toNat < b.size / width R)
  • @[inline] def setIf (n : Nat) (hn : n < USize.size) (b : ByteArray) (k : USize) (x : R) :
  • @[inline] def storeWord (b : ByteArray) (i : USize) (v : UInt32) (h : i.toNat + 4 ≤ b.size)
  • @[simp] theorem decode_encode (a : Array R) : decode (encode a) = a
  • @[simp] theorem encode_empty : encode (#[] : Array R) = ByteArray.empty
  • @[simp] theorem size_alloc (n : Nat) : (alloc R n).size = width R * n
  • @[simp] theorem size_alloc_div (n : Nat) : (alloc R n).size / width R = n
  • @[simp] theorem size_encode (a : Array R) : (encode a).size = width R * a.size
  • @[simp] theorem size_encode_uint32 (W : Array UInt32) : (encode W).size = 4 * W.size
  • @[simp] theorem size_setAt (b : ByteArray) (k : USize) (x : R) (hk) (hu) :
  • @[simp] theorem size_uset (b : ByteArray) (i : USize) (v : UInt8) (h : i.toNat < b.size) :
  • @[simp] theorem size_zeros (n : Nat) : (zeros n).size = n
  • @[simp] theorem width_uint32 : width UInt32 = 4
  • def alloc (R : Type u) [PackedRepr R] (n : Nat) : ByteArray
  • def bytesAt (b : ByteArray) (off w : Nat) : Vector UInt8 w
  • def decode (b : ByteArray) : Array R
  • def encode (a : Array R) : ByteArray
  • def fillZeros (target : Nat) : Nat → ByteArray → ByteArray
  • def get (b : ByteArray) (j : Nat) : R
  • def ofWordBytes (v : Vector UInt8 4) : UInt32
  • def replaceAt (b : ByteArray) (off : Nat) (values : ByteArray) : ByteArray
  • def replicate (n : Nat) (x : R) : ByteArray
  • def replicateGo (n : Nat) (block acc : ByteArray) : ByteArray
  • def wordBytes (v : UInt32) : Vector UInt8 4
  • def zeroBlock : ByteArray
  • def zeros (n : Nat) : ByteArray
  • private theorem drop_flatMap (l : List R) (i : Nat) :
  • private theorem length_flatMap (l : List R) :
  • private theorem take_flatMap (l : List R) (i : Nat) :
  • theorem bytesAt_encode (a : Array R) (i : Nat) (hi : i < a.size) :
  • theorem copySlice_encode (a o : Array R) (i c : Nat) :
  • theorem copySlice_replicate (k m len : Nat) (hl : len ≤ k) :
  • theorem encode_append (a b : Array R) : encode (a ++ b) = encode a ++ encode b
  • theorem encode_extract (a : Array R) (first last : Nat) :
  • theorem encode_ofWord32 {R : Type u} (toWord : R → UInt32) (ofWord : UInt32 → R)
  • theorem fillZeros_replicate (target : Nat) :
  • theorem get!_replaceAt (b v : ByteArray) (off i : Nat) (h : off + v.size ≤ b.size) :
  • theorem getAt_eq (b : ByteArray) (k : USize) (hk) (hu) :
  • theorem getElem_decode (b : ByteArray) (i : Nat) (hi : i < (decode (R
  • theorem getElem_decode_eq_get (b : ByteArray) (j : Nat) (hj : j < (decode (R
  • theorem getUInt32LE!_encode (W : Array UInt32) (k : Nat) :
  • theorem getUInt32LE!_eq_ofWordBytes (b : ByteArray) (off : Nat) (h : off + 4 ≤ b.size) :
  • theorem get_encode (a : Array R) (j : Nat) (hj : j < a.size) : get (encode a) j = a[j]
  • theorem get_setAt (b : ByteArray) (k : USize) (x : R) (hk) (hu) (j : Nat) :
  • theorem loadWord_eq (b : ByteArray) (i : USize) (h hu) :
  • theorem ofWordBytes_wordBytes (v : UInt32) : ofWordBytes (wordBytes v) = v
  • theorem read_encode (a : Array R) (off : USize) (i : Nat) (ho : off.toNat = width R * i)
  • theorem replaceAt_empty (b : ByteArray) (off : Nat) (h : off ≤ b.size) :
  • theorem replaceAt_encode (a v : Array R) (i : Nat) :
  • theorem replaceAt_encode_uint32 (W V : Array UInt32) (i : Nat) :
  • theorem replaceAt_replaceAt (b p q : ByteArray) (off : Nat)
  • theorem replaceAt_self (b : ByteArray) (off w : Nat) (h : off + w ≤ b.size) :
  • theorem replicateGo_encode (x : R) : ∀ (n m a : Nat),
  • theorem replicate_eq (n : Nat) (x : R) : replicate n x = encode (Array.replicate n x)
  • theorem setAt_encode (a : Array R) (k : USize) (x : R) (hk) (hu) :
  • theorem setIf_encode (a : Array R) (k : USize) (x : R) (hn : width R * a.size < USize.size) :
  • theorem setIf_encode_of_eq (a : Array R) (k : USize) (x : R) (m : Nat) (hm : m < USize.size)
  • theorem setUInt32LE!_encode (W : Array UInt32) (k : Nat) (v : UInt32) :
  • theorem setUInt32LE!_eq_replaceAt (b : ByteArray) (off : Nat) (v : UInt32)
  • theorem setUInt32LE!_eq_replaceAt_encode (b : ByteArray) (off : Nat) (v : UInt32)
  • theorem set_eq_splice (a : Array R) (i : Nat) (x : R) (hi : i < a.size) :
  • theorem size_decode (b : ByteArray) : (decode (R
  • theorem size_replaceAt (b values : ByteArray) (off : Nat) (h : off + values.size ≤ b.size) :
  • theorem slot_toNat (k : USize) (n : Nat) (hk : k.toNat < n / width R) (hu : n < USize.size) :
  • theorem storeWord_eq (b : ByteArray) (i : USize) (v : UInt32) (h hu) :
  • theorem toNat_add_byte (i : USize) (n d : Nat) (h : i.toNat + 4 ≤ n) (hu : n < USize.size)
  • theorem toUInt8_shiftRight (v : UInt32) (s : UInt32) (k : Nat) (hs : s.toNat = 8 * k)
  • theorem uget_eq_get! (b : ByteArray) (i : USize) (h : i.toNat < b.size) :
  • theorem uset_eq_set! (b : ByteArray) (i : USize) (v : UInt8) (h : i.toNat < b.size) :
  • theorem wordBytes_ofWordBytes (v : Vector UInt8 4) : wordBytes (ofWordBytes v) = v
  • theorem write_encode (a : Array R) (off : USize) (i : Nat) (x : R)
  • theorem zeros_eq (n : Nat) : zeros n = ⟨Array.replicate n 0⟩

CompPoly/Data/Bytes/UInt32.lean (2)

  • @[simp] theorem ByteCodec.width_uint32 : ByteCodec.width UInt32 = 4
  • theorem ByteCodec.toBytes_uint32 (x : UInt32) :

CompPoly/Fields/BLS12_377/Fast.lean (1)

  • instance instMont64x4Field : Mont64x4Field BLS12_377.scalarFieldSize where

CompPoly/Fields/BLS12_381/Fast.lean (1)

  • instance instMont64x4Field : Mont64x4Field BLS12_381.scalarFieldSize where

CompPoly/Fields/BN254/Fast.lean (1)

  • instance instMont64x4Field : Mont64x4Field BN254.scalarFieldSize where

CompPoly/Fields/Goldilocks/FastDefs.lean (7)

  • def addRaw (x y : UInt64) : UInt64
  • def foldUInt128Borrow (lo hiHi hiLo : UInt64) : UInt64
  • def foldUInt128Lazy (lo hi : UInt64) : UInt64
  • def invLazy (x : UInt64) : UInt64
  • def mulLazy (x y : UInt64) : UInt64
  • def powLazy (acc x : UInt64) (n : Nat) : UInt64
  • def squareNLazy (x : UInt64) : Nat → UInt64

CompPoly/Fields/Goldilocks/FastReduction.lean (12)

  • private theorem foldTail_cast (lo hiHi hiLo t0 : UInt64) (hhiLo : hiLo.toNat < 2 ^ 32)
  • private theorem wideMul_hi_nat (p00 p01 p10 p11 : Nat) :
  • theorem addRaw_cast {x y : UInt64} (hx : x.toNat < Goldilocks.fieldSize)
  • theorem addRaw_lt {x y : UInt64} (hx : x.toNat < Goldilocks.fieldSize)
  • theorem addRaw_toNat {x y : UInt64} (hx : x.toNat < Goldilocks.fieldSize)
  • theorem foldUInt128Lazy_cast (lo hi : UInt64) :
  • theorem invLazy_cast (x : UInt64) :
  • theorem mulLazy_cast (x y : UInt64) :
  • theorem powLazy_cast (acc x : UInt64) (n : Nat) :
  • theorem shiftLeft32_sub_low (hi : UInt64) :
  • theorem squareNLazy_cast (x : UInt64) (n : Nat) :
  • theorem wideMul_snd_toNat (x y : UInt64) :

CompPoly/Fields/Montgomery/Native64x4.lean (37)

  • private theorem addWord_value (a b : UInt64) :
  • private theorem add_of_carry {S A B Q D bo : ℕ} (hchain : S + 2 ^ 256 * 1 = A + B)
  • private theorem cond_of_borrow_one {M Q T : ℕ} (h : M + Q = T + 2 ^ 256) (hM : M < 2 ^ 256) :
  • private theorem cond_of_borrow_zero {M Q T : ℕ} (h : M + Q = T) (hM : M < 2 ^ 256) :
  • private theorem flag_le_one (p : Prop) [Decidable p] :
  • private theorem flag_or_value (p q : Prop) [Decidable p] [Decidable q]
  • private theorem high32_value (x : UInt64) : (x >>> 32).toNat = x.toNat / 2 ^ 32
  • private theorem low32_value (x : UInt64) : (x &&& 0xffffffff).toNat = x.toNat % 2 ^ 32
  • private theorem subWord_value (a b : UInt64) :
  • private theorem sub_of_borrow_one {A B Q D E c : ℕ} (hD : D + B = A + 2 ^ 256 * 1)
  • private theorem sub_of_borrow_zero {A B Q D : ℕ} (h : D + B = A + 2 ^ 256 * 0)
  • private theorem wide_of_head {T L t4 Q D bo : ℕ} (hdec : T = L + 2 ^ 256 * t4)
  • theorem adc_spec (x y c : UInt64) (hc : c.toNat ≤ 1) :
  • theorem addLimbs_spec (a b : Limbs4) :
  • theorem add_lt (q a b : Limbs4) (haq : a.toNat < q.toNat) (hbq : b.toNat < q.toNat) :
  • theorem add_toNat (q a b : Limbs4) (haq : a.toNat < q.toNat) (hbq : b.toNat < q.toNat) :
  • theorem borrow_chain_sum {x0 x1 x2 x3 y0 y1 y2 y3 m0 m1 m2 m3 bin b0 b1 b2 b3 : ℕ}
  • theorem carry_chain_sum {x0 x1 x2 x3 y0 y1 y2 y3 s0 s1 s2 s3 cin c0 c1 c2 c3 : ℕ}
  • theorem condSubWide_lt (q : Limbs4) (t : State5) (h : t.toNat < 2 * q.toNat) :
  • theorem condSubWide_toNat (q : Limbs4) (t : State5) (h : t.toNat < 2 * q.toNat) :
  • theorem condSub_lt (q t : Limbs4) (h : t.toNat < 2 * q.toNat) :
  • theorem condSub_toNat (q t : Limbs4) :
  • theorem ext_of_toNat {x y : Limbs4} (h : x.toNat = y.toNat) : x = y
  • theorem mac_spec (t a b c : UInt64) :
  • theorem montM_toNat (s negInv : UInt64) :
  • theorem mulHi_spec (a b : UInt64) :
  • theorem neg_lt (q a : Limbs4) (haq : a.toNat < q.toNat) : (neg q a).toNat < q.toNat
  • theorem neg_toNat (q a : Limbs4) (haq : a.toNat < q.toNat) :
  • theorem ofNat_toNat (n : ℕ) : (ofNat n).toNat = n % 2 ^ 256
  • theorem one_toNat : one.toNat = 1

…and 794 more not listed.

✏️ Affected: 21 declaration(s) (line number changed)
  • theorem addLimbs_toNat (a b : Limbs8) (ha : a.Bounded) (hb : b.Bounded) : in CompPoly/Fields/Montgomery/Native64x8.lean moved from L278 to L319
  • theorem add_bounded (q a b : Limbs8) : (add q a b).Bounded in CompPoly/Fields/Montgomery/Native64x8.lean moved from L372 to L422
  • @[inline] def add (q a b : Limbs8) : Limbs8 in CompPoly/Fields/Montgomery/Native64x8Defs.lean moved from L149 to L178
  • @[specialize] def gcdFinalChunks (q : Limbs8) (negInv : UInt64) (finalRounds : Nat) in CompPoly/Fields/Montgomery/Native64x8InvDefs.lean moved from L194 to L215
  • @[specialize] def gcdInvCandidate (modulus : Nat) [P : GcdData modulus] (q : Limbs8) in CompPoly/Fields/Montgomery/Native64x8InvDefs.lean moved from L207 to L230
  • @[specialize] def gcdMainLoop (q : Limbs8) (negInv : UInt64) (rounds : Nat) (a u b v : Limbs8) in CompPoly/Fields/Montgomery/Native64x8InvDefs.lean moved from L174 to L193
  • def towerScalarTasks : List BenchTask in bench/CompPolyBench/Fields/Binary/Tower/Scalar.lean moved from L93 to L101
  • private def runScalarInv (modulus : Nat) [Mont64x4Field modulus] [GcdData modulus] in bench/CompPolyBench/Fields/Montgomery.lean moved from L32 to L33
  • private def a : Limbs4 in tests/CompPolyTests/Fields/Pasta/Fast.lean moved from L31 to L31
  • private def b : Limbs4 in tests/CompPolyTests/Fields/Pasta/Fast.lean moved from L35 to L34
  • private def montA : Limbs4 in tests/CompPolyTests/Fields/Pasta/Fast.lean moved from L39 to L37
  • private def montAB : Limbs4 in tests/CompPolyTests/Fields/Pasta/Fast.lean moved from L47 to L43
  • private def montB : Limbs4 in tests/CompPolyTests/Fields/Pasta/Fast.lean moved from L43 to L40
  • private def negInv : UInt64 in tests/CompPolyTests/Fields/Pasta/Fast.lean moved from L25 to L25
  • private def pMontA : Limbs4 in tests/CompPolyTests/Fields/Pasta/Fast.lean moved from L84 to L79
  • private def pMontAB : Limbs4 in tests/CompPolyTests/Fields/Pasta/Fast.lean moved from L92 to L85
  • private def pMontB : Limbs4 in tests/CompPolyTests/Fields/Pasta/Fast.lean moved from L88 to L82
  • private def pNegInv : UInt64 in tests/CompPolyTests/Fields/Pasta/Fast.lean moved from L82 to L77
  • private def pq : Limbs4 in tests/CompPolyTests/Fields/Pasta/Fast.lean moved from L80 to L75
  • private def q : Limbs4 in tests/CompPolyTests/Fields/Pasta/Fast.lean moved from L23 to L23
  • private def rMod : Limbs4 in tests/CompPolyTests/Fields/Pasta/Fast.lean moved from L27 to L27

sorry Tracking

  • No sorrys were added, removed, or affected.

Coverage Notes

  • AI file summarization partially analyzed 2 file(s) because their individual diffs exceeded the per-file size budget. Statistics and Lean signal tracking still cover the full PR.
Partially Analyzed Files
  • CompPoly/Univariate/NTTFast/ColumnsCorrectness.lean (+1499/-0)
  • docs/wiki/benchmarking.md (+474/-4)
* Additional-instructions analysis was skipped because the full diff exceeded the analysis size budget, and partial results would be misleading.

Last updated: 2026-10-13 03:05 UTC.

Idle worker cores take a few hundred milliseconds of load before the
frequency governor raises their clocks; the 50 ms warmup often ended
before that. The NTT suite now uses the large budget (200 ms warmup,
50 samples) in Lean and Rust; the Rust harness gains Budget presets
matching Lean's.
…rray word accessors

The packed NTT now reads and writes words with `ByteArray.ugetUInt32LE` and
`ByteArray.usetUInt32LE` from lean4#14053, backported in `CompPoly.Data.ByteArray.Pack` with the
same names, signatures, Lean definitions and lemmas. `Native.readRaw` and the `Native.storeWords`
extern are gone: `storeWords` is a Lean batch of word stores, appends grow the buffer with
`ByteArray.copySlice`, and checked reads are `readAt`. `storeWords_eq` and the read lemmas prove
them equal to the previous storage semantics, so the refinement proofs carry over.

The backport's C differs from the PR's: loads and stores use a fixed-size `memcpy`, and a store
states that the array is still unshared, so a run of stores keeps one sharing check. With the
PR's byte-shift C the million-point transform took 79 ms instead of 23 ms; without the
assumption, 23 ms instead of 13.

Range checks run on machine words; `storeWords` reads the append offset before the growth
consumes the buffer, which would otherwise copy it on every batch.
The binary split tree, the sliced pair tree, the transpose and the reversed leaves now store
each 16-word batch at a cursor into a buffer pre-sized with zeros, instead of growing the
output by appending. Zero buffers are filled by appending copies of a separate 4 KiB zero
block into a reserved destination, so the destination stays exclusive.
The transpose extends its output by one zero block right before each tile overwrites it, so
the zeros are still in cache. encode overwrites a zero buffer word by word instead of appending
one word at a time; this restores the small transforms that end in an encode.
storeWords loses its append mode, which only push, push16 and the old
generate used. generate now overwrites a zero buffer word by word, and
encode is generate over the field array. Removed: push, push16, zeroBytes,
the natural-index write16, Storage.wordAt and lemmas no longer referenced.
The native smoke test keeps only the accessor checks: storeWords and read
are Lean definitions proved against Storage.pack.
PackedRepr R stores every element in width bytes, with toBytes/ofBytes
as the specification and fast read/write at byte offsets proved equal to
it; PackedRepr.encode/decode view a ByteArray as an Array R. UInt32 and
every Native32 FastField are one-word instances on the backported
ByteArray word accessors.

The packed NTT's Storage.pack and packFields become PackedRepr.encode at
UInt32 and at the field, and the executable encoder is
Native.encodeFields. The column-parallel NTT drops Word32Repr and the
byte-by-byte ByteWords layer for PackedRepr.getAt/setAt with element-count
invariants, so it is generic over the element width. Zero buffers are
ByteArray.zeros. Data/Bytes/Words.lean and Packed/Storage.lean are gone.
CompPoly is extern-free by design; only the packed NTT kernels opt into the
backported ByteArray word accessors. The one-word PackedRepr instance read
and wrote through those accessors, which put them under the extern-free
column-parallel NTT too. It now loads and stores the four bytes with
ByteArray.uget/uset, proved equal to the accessors' Lean definitions, and
the column-parallel NTT runs at its speed before the PackedRepr refactor.
Plan.forward/inverse took field arrays through a separate input path:
field-array split kernels, their models and refinement proofs, and a
field-input task tree. Consumers hold packed words (PackedRepr.encode, or
Native.encodeFields and Native.unpack for fast conversion), so the plan now
exposes only forwardPacked/inversePacked. The packed decode path shared by
small and custom-depth transforms is Native.runDecoded.

The benchmark's field-array variant goes with the API; packed-io remains.
NaturalPlan.forwardPacked/inversePacked take and return packed elements
(PackedRepr), like the packed plan; forwardPacked_eq and inversePacked_eq
prove forwardPacked (encode a) = encode (forward a) and the same for the
inverse.

Even sizes from 2^16 run Columns.transformPacked: column tasks decode their
own columns from the packed buffer, and sixteen tasks each interleave a
contiguous range of rows into packed words, joined by one copy. The proofs
reuse the array transform: a generic simulation lemma over the interleave
loop relates packed stores to array stores and offset chunks to slices of
the whole interleave. Other sizes unpack with Columns.decodeAll, run the
array transform and pack with Columns.encodeAll.

PackedRepr gains replicate (filled by doubling copies) and setIf, a guarded
store whose machine-size bound is proved once per buffer. The benchmark's
variants are now packed and externless, both with packed words in and out.
The column transform now handles odd sizes: a leaf finishes with the radix-two
layer (lastPair), which commutes with cutting the array into leaves. So every
size from 2^15 with more than one worker takes the column path, and smaller
transforms or a single worker run the sequential transform as one leaf. The
segment path for odd sizes (Parallel.chunksTask) and the inverse pass lists it
needed are gone.

Odd sizes on several workers get 2-4x faster (2^19 on 8 workers: 17.7 ms to
4.2 ms); other shapes are unchanged. The NTT suite validates 2^15 and 2^17
against Plonky3.
The packed plan now runs the extern-free plan's pipeline on packed
words: from 2^15 elements and with more than one worker, column tasks
gather their columns and run the top two radix-four passes with
`inner16`, sixteen leaf tasks finish with `leafNatural`, and
`naturalLeaves` interleaves; otherwise one leaf. `Packed/Columns.lean`
holds the pipeline, `Packed/ColumnsCorrectness.lean` its proof, which
reuses the extern-free column and leaf lemmas.

The split tree, slice tree, tiled decoder, `Plan.defaultDepth` and the
in-stage normalization they needed are deleted. The spec files that
remain are merged into `Packed/DifSpec.lean`. Both plans use sixteen
column tasks at every worker count, which keeps a task's rows and
twiddles in L2 with four workers.

Forward, ms, previous packed plan → this: CPUs 0-7 2^12 0.079 → 0.020,
2^16 0.410 → 0.246, 2^20 2.53 → 2.42; CPUs 0-3 2^18 0.687 → 0.672,
2^20 3.54 → 3.23; CPU 0 2^20 10.8 → 7.69.
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.

2 participants