Repository navigation
Conversation
🤖 PR Summaryfeat(fields): four-limb (4x64) Montgomery for the 256-bit fields, mul speedup Mathematical Formalization
Proof Completion (sorries removed)
Protocols / Soundness
Infrastructure / CI
Documentation
Refactoring
Performance (per PR body, for context):
Statistics
Lean Declarations ✏️ Removed: 141 declaration(s)
✏️ Added: 121 declaration(s)
✏️ Affected: 23 declaration(s) (line number changed)
📋 **Additional Analysis**The diff replaces the eight-limb (Native64x8) Montgomery implementation with a four-limb (Native64x4) version across the CompPoly library. The change is internally consistent and updates all imports, tests, benchmarks, and documentation. The main potential issue is the deletion of the old 📄 **Per-File Summaries**
Last updated: 2026-11-06 13:59 UTC. |
dhsorens
left a comment
There was a problem hiding this comment.
Thanks, this is a really nice speedup, and the design makes the soundness story easy to check.
What I checked:
- TCB. The additions contain no
native_decide,bv_decide,implemented_by,extern,unsafeorcsimp. Every per-field constant is a class field with adecidedefault, so a wrong limb fails the build. - Correctness. The
Fieldinstance is transferred alongtoField_injective.mul_specandcondSubWideneed onlynegInv * q ≡ -1anda < q, so the full-width secp256k1 moduli need no extra side condition. - Inversion.
invGcdRawverifies the candidate (cand < q ∧ cand * x = R) and falls back to the proved FermatmontPow, soinvGcd_eq_invdoes not depend on the divstep schedule being right. The#guards checking thatinvGcdreturns the candidate itself mean a broken schedule would show up in the tests, not just as a silent slowdown. - Codec.
Native64x4Bytesmatches the eight-limb lemmas (CanonicalNat/ByteCodec,toBytes_ofField). - Locally on Lean 4.35. I merged current
main(with #389 and #392) into this branch.lake build,lake testandlake build CompPolyBenchall pass with 0 warnings;axiomsweep --check,lint-style.shandcheck-docs-integrity.pypass too.
To resolve in this PR:
- Update from
main. #389 has landed, so the stack collapses. The only conflict left isCompPoly/Fields/README.md, against #392's Goldilocks rows. Taking #392's three rows resolves it: they no longer mention either Montgomery carrier, so theMont64x4Fieldwording change isn't needed there. ScalarFft. It is the stated reason for keeping the eight-limb stack, but no field instantiatesLimbs8any more, soBN254.Fast.ScalarFieldvalues can no longer be passed to it. Is the downstreamprecompileModulesconsumer still using it? If it is, I'd port it toLimbs4here. If not, say so, and (4) covers the cleanup.- Downstream breakage. Please note in the description that this changes the representation behind every 256-bit fast field (
.val : Limbs8→Limbs4) and renames the instances (instMont64x8Field→instMont64x4Field). There are no deprecation aliases, which I think is fine for these internals, but downstream users should hear about it. - Shared divstep. Move the shared divstep code out of the eight-limb modules, then delete or keep those modules depending on the answer to (2) (see inline).
- Bench keys. Rename the
fields-mont64x8-*-invkeys to match the carrier (see inline). - Best times. Record the new mul/add/inverse numbers for the 256-bit fields in
docs/wiki/benchmark-best-times.md. This is an optimisation pass, and the record is part of the loop indocs/wiki/autoresearch.md.
| module | ||
|
|
||
| public import CompPoly.Fields.Montgomery.Native64x4Defs | ||
| public import CompPoly.Fields.Montgomery.Native64x8InvDefs |
There was a problem hiding this comment.
This import, and the matching one of Native64x8Inv in Native64x4Inv.lean, means the four-limb inverse depends on the eight-limb stack only for gcdInner / gcdBitLen and gcdInner_natAbs_le_31. Those are word-level divstep code that doesn't depend on the limb count. Could you move them into a shared module (e.g. Montgomery/Divstep.lean) here? Then the eight-limb modules exist only for ScalarFft, and once that's settled (see the review comment), we can either delete them in this PR or keep them knowing exactly what they're for.
There was a problem hiding this comment.
Moved to Montgomery/DivstepDefs.lean and Montgomery/Divstep.lean, the eight-limb modules are deleted (see item 4).
| /-- Run the secp256k1 scalar-field inversion benchmark. -/ | ||
| private def runSecp256k1ScalarInv (preset : BenchPreset) (gen : StdGen) : | ||
| IO (BenchGroup × StdGen) := do | ||
| runScalarInv Secp256k1.scalarFieldSize "fields-mont64x8-secp256k1-scalar-inv" |
There was a problem hiding this comment.
The A/B in the description is already measured, so the keys no longer need to match main. Could you rename them to match the carrier (e.g. fields-mont64x4-*-inv) in this PR? As it is, the key names one carrier and the representation column another. The same keys appear in bench/ci-groups.txt, the inversion row of bench/README.md, the four-limb inversion row of docs/wiki/autoresearch.md, and the docs/wiki/benchmark-best-times.md rows. Those rows should get the new numbers anyway (see the review comment).
There was a problem hiding this comment.
Renamed to fields-mont64x4-*-inv everywhere except benchmark-best-times.md, which changes with the new numbers (item 6).
Resolve CompPoly/Fields/README.md by taking Verified-zkEVM#392's Goldilocks rows.
Move `gcdBitLen` and `gcdInner` to the zero-import `Montgomery/DivstepDefs.lean`, and their unfolding lemmas and the coefficient bound to `Montgomery/Divstep.lean`. The four-limb inversion modules import these instead of the eight-limb stack. Delete the seven `Montgomery/Native64x8*.lean` modules and `Montgomery/ScalarFft.lean`. No field instantiates `Limbs8`, and the `precompileModules` consumer no longer uses `ScalarFft`. Rename the inversion bench groups from `fields-mont64x8-*-inv` to `fields-mont64x4-*-inv`. The rows in `benchmark-best-times.md` keep the old keys until the reference machine re-runs them.
|
I dropped |
Replaces the eight-limb Montgomery carrier of BN254, BLS12-381, BLS12-377, Pallas, Vesta and both secp256k1 fields with a four-limb 64-bit-radix one. Pure Lean, no externs, no
native_decide.Montgomery/Native64x4*.lean, mirroring the eight-limb modules: flatUInt64CIOS,mulHifrom four 32-bit partial products,adc/sbb/macword primitives,condSubWidefor full-width moduli{ x : Limbs4 // x.toNat < modulus }; noBoundedpredicate since every limb is a full wordMontgomery/DivstepDefs.lean, linear combinations are five words,initU = 2^(1135 - finalRounds)Montgomery/Divstep.leanMont64x4Field+GcdDataconstants, decide-guardedScalarFft.lean, which no field and no downstream consumer uses any moreLimbs4/Mont64x4, and the inversion groups renamed tofields-mont64x4-*-inv. The A/B below ran before the renamePinned A/B against the eight-limb build, 61 rows: 29 faster, 32 same, 0 slower. Fast mul 0.53-0.65x (BN254 32.9 → 21.0 ns, secp256k1 base 30.9 → 16.2 ns), add 0.78-0.94x, GCD inverse 0.80x (6.0 → 4.8 µs), Fermat 0.78-0.87x, on every field. Against the secp256k1 custom reduction of #271 in the same standalone harness: mul 14 vs 22 ns, add 2.6 vs 3.4 ns, inverse 4.6 vs 33 µs.
The earlier 4x64 attempt lost to 8x32 because its 128-bit intermediates were boxed; this one keeps every accumulator in flat words and builds a
Limbs4only inside a branch, so it is allocation-free.Validation
Run locally on the merge with
main(Lean 4.35).lake build,lake testandlake build CompPolyBenchpass with no warnings.lake exe axiomsweep --checkreports nosorryand no non-standard axioms.lint-style.py,check-docs-integrity.pyandcheck-imports.shpass.CompPolyBench --validate-onlypasses on all 69 groups ofbench/ci-groups.txt.Breaking changes for downstream users
This PR changes the representation behind every 256-bit fast field: the BN254, BLS12-381 and BLS12-377 scalar fields, Pallas, Vesta, and both secp256k1 fields. The PR adds no deprecation aliases.
.valof a fast-field element is aLimbs4(four 64-bit limbs), not aLimbs8.Mont64x4Field, notMont64x8Field. The instances are renamed:instMont64x8Field→instMont64x4Field, and for secp256k1instScalarMont64x8Field/instBaseMont64x8Field→instScalarMont64x4Field/instBaseMont64x4Field.GcdDatamoves fromMontgomery.Native64x8toMontgomery.Native64x4. ItsinitUis aLimbs4, andinitU_toNatuses2 ^ (1135 - finalRounds).Montgomery.Native64x8toMontgomery(Montgomery.gcdInner,Montgomery.gcdBitLen,Montgomery.gcdInner_natAbs_le_31), in the new modulesMontgomery/DivstepDefs.leanandMontgomery/Divstep.lean.fields-mont64x8-*-invtofields-mont64x4-*-inv.Montgomery/Native64x8*.leanmodules (Montgomery.Native64x8,Limbs8,Mont64x8Field) andMontgomery/ScalarFft.lean.