feat(fields): fast BN254/BLS12-381/BLS12-377 scalar fields (8x32 Montgomery) - #284
Conversation
- there a custom reduction will be implemented for secp - allows for simpler proofs for montgomery
…xternal C - inverse is still Pornin's GCD algorithm, over 64 bit - 32 bit limbs are faster - removed external C, since mult didn't offer any substantial speedup
- internally, but 64 bits are still used in the algorithm at specific locations
- removing u256 with 4 64bit limbs, not needed in inv now
…odule system Module headers across the stack (public imports, @[expose] public section; meta imports for #guard tests), upstream fast-field files adopted, per-field facades rewritten, Native64x8Field.ofInt de-privated for module exposure. No proof changes. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
🤖 PR Summaryfeatu(fields): fast BN254/BLS12-381/BLS12-377 scalar fields (8x32 Montgomery)Statistics
Lean Declarations ✏️ Removed: 9 declaration(s)
✏️ Added: 236 declaration(s)
…and 86 more not listed.
📋 **Additional Analysis**The diff is part of a large contribution that splits the BLS12-377, BLS12-381, and BN254 scalar field modules into Findings from the ReviewAdherence to Style and Naming Guidelines
File and Module Layout
Documentation Standards
PR Title and Description (Process)
Deprecation Policy and Compatibility
Test Coverage
No Blocking IssuesNo violations of the hard-and-fast rules (e.g., banned syntax, incorrect file naming, missing module headers, incorrect import grouping) were found. The only concrete actionable items are the line-length violations and the missing citation entry for the inversion algorithm. 📄 **Per-File Summaries**
Last updated: 2026-08-05 11:05 UTC. |
mitschabaude
left a comment
There was a problem hiding this comment.
should there be benchmarks as well?
| /-- `acc · x^n` in Montgomery form by binary powering. -/ | ||
| def montPow (q : Limbs8) (negInv : UInt64) (acc x : Limbs8) (n : Nat) : Limbs8 := | ||
| if h : n = 0 then acc | ||
| else | ||
| montPow q negInv (if n % 2 == 1 then mul q negInv acc x else acc) | ||
| (mul q negInv x x) (n / 2) | ||
| termination_by n | ||
| decreasing_by omega |
There was a problem hiding this comment.
is there no performance overhead from this well-founded recursion?
There was a problem hiding this comment.
No, the WF overhead is specialized away by the compiler in this case, I verified in the emitted IR/C. The args are unpacked and since it's a tail call, it's compiled as a loop. Either way this is only the fallback path for inv of x=0 or if the candidate inverse from Pornin's GCD is wrong (which shouldn't be), so not the hot path.
|
There are no benchmarks in the PR right now, but maybe it's a good idea to add them, yes |
dhsorens
left a comment
There was a problem hiding this comment.
this looks great, thank you @graikos and @mitschabaude !
Fast scalar-field arithmetic for BN254, BLS12-381 and BLS12-377. Pure Lean, no externs, no
native_decide.UInt64words, so macs never overflowprecompileModulesThe early commits are the original 4x64 implementation with C extern tiers; benchmarks for external C showed no significant speedup with only
mulhiin C, and a fully external Montgomery/GCD implementation defeats the purpose. Hence, they were dropped in favor of the 8x32 implementation in pure Lean. secp256k1 is excluded (p > 2^255), itgets a custom reduction separately.