Eight-limb Montgomery field arithmetic for 255-bit moduli (Pasta) - #274
Eight-limb Montgomery field arithmetic for 255-bit moduli (Pasta)#274mitschabaude-bot wants to merge 39 commits into
Conversation
…ds-gregor Simplify 32-bit Montgomery fields
Add `Montgomery/Native64x8`, the multi-limb sibling of `Montgomery/Native32`: prime-field arithmetic over eight 32-bit limbs carried in `UInt64` words, for moduli below `2 ^ 255`. Every operation is a straight-line chain of `@[inline]` word helpers (`adcLo`/`adcCo`, `sbbLo`/`sbbBo`, `macLo`/`macHi`, `montM`), each with an existential `toNat` specification naming its outputs. Two telescoping lemmas (`carry_chain_sum`, `borrow_chain_sum`) turn an eight-step carry or borrow chain into a single identity between recomposed values, which keeps every arithmetic side condition a small linear problem. Proved here: `condSub_toNat`, `add_toNat`, `sub_toNat`, `neg_toNat` with the matching boundedness and canonicity lemmas, all generic in the modulus limbs. `Montgomery/Native64x8Mul` proves CIOS multiplication correct: `mulAccum_spec` and `mulReduce_spec` for the two halves of a round, `mulRound_spec` for the round invariant `2 ^ 32 * t' = t + a * bi + m * q` together with the `t < 2 * q` bound, and `mul_spec` for the eight-round fold. Per-field constants live in the `Mont64x8Field` class with `decide` autoparams, instantiated for the Vesta and Pallas base fields. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Add `Montgomery/Native64x8Field`, the multi-limb analogue of
`Montgomery/Native32Field`: the bounded carrier
`FastField modulus = { x : Limbs8 // x.Bounded ∧ x.toNat < modulus }`, its
arithmetic on top of the raw eight-limb operations, and the bridge to
`ZMod modulus`.
`toField` is proved to be a ring isomorphism onto `ZMod modulus`
(`ringEquiv`), with `@[simp]` equivalences for zero, one, addition,
subtraction, negation, multiplication, squaring, powers, Fermat inversion,
division, and the numeric casts. `Field` and `NonBinaryField` instances are
transferred along it. Primality enters only through
`[Fact (Nat.Prime modulus)]`, so the raw layer and the carrier arithmetic stay
primality-free.
Supporting raw-layer additions: `Limbs8.one`, `Limbs8.ofNat` with their `toNat`
lemmas, and `Limbs8.ext_of_toNat`.
Regression tests cover the raw operations against externally computed Vesta
values and the carrier through inversion and exponentiation.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Mark `State9.zero` and `State9.toLimbs8` `@[inline]` so that the accumulator never materialises: the generated `mul` is a single straight-line function of 693 unboxed 64-bit word operations with no intermediate allocation, and adds Pallas coverage to the regression tests. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…tation Add `Fields/Pasta`, the per-field facade for the Pallas and Vesta base fields, following the KoalaBear layering: * `Pasta/Basic.lean` — the two 255-bit primes with Pratt primality certificates (ported from the certificates by Daira-Emma Hopwood, using CompPoly's own `PrattCertificate` infrastructure), their `Fact` and `Field` instances, and the 2-cycle abbreviations relating each curve's scalar field to the other's base field; * `Pasta/Fast.lean` — the `Mont64x8Field` constants for both fields and the namespaced `Pallas.Fast` / `Vesta.Fast` API over the shared eight-limb implementation, with the canonical-field bridge; * `Pasta.lean` — the facade re-exporting both. `Mont64x8Field` moves from the raw layer to `Montgomery/Native64x8Field` alongside the carrier it parameterizes, mirroring `Mont32Field` in `Montgomery/Native32Field`, and now carries `prime` like its single-word counterpart, so the bridge no longer takes a separate `Fact` argument. Tests cross-check both fast fields against canonical `ZMod` arithmetic for powers and inverses. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
🤖 PR Summary
Proven eight-limb Montgomery field arithmetic for 255-bit moduli, instantiated on the Pasta curves (Pallas/Vesta), with a zero-import runtime definitions module for native compilation. The PR also refactors the existing BabyBear and KoalaBear fast field implementations to use a shared 32-bit Montgomery library, consolidates NTT domain structures, and updates documentation and benchmarks. No new Statistics
Lean Declarations ✏️ Removed: 150 declaration(s)
✏️ Added: 320 declaration(s)
…and 170 more not listed. ✏️ Affected: 2 declaration(s) (line number changed)
📄 **Per-File Summaries**
Last updated: 2026-07-26 07:01 UTC. |
The runtime definitions (word helpers, Limbs8, add/sub/neg, State9 and the CIOS multiplication) move verbatim into the new zero-import module Native64x8Defs, so that downstream precompileModules lanes can include them without native-compiling a mathlib import closure. All theorems stay in Native64x8, which now imports the defs module. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Proven multi-limb Montgomery field arithmetic, structured for fast interpreted/compiled evaluation. The new work is the top 4 commits (97464eb..28c8931); the branch is based on
fast_kb_bb_fields(#258), so the diff includes that until it merges.Native64x8Defs/Native64x8Mul— raw eight-limb (64×8) CIOS Montgomery multiplication over straight-line accumulator code, with the fused blocks split into variable-input defs (mulAccum/mulReduce) so per-limb specs stay provable by narrowomega.Native64x8/Native64x8Field— the carrier type, ring/field structure, and thetoFieldbridge toZMod p(proved, no axioms beyond the classical trio).Pasta— both Pasta primes instantiated (negInv = 0xfffffffffor both), primality via Pratt certificates.Downstream consumer already exists: the Zcash ironwood verifier-key certificate runs its Pippenger MSM + radix-2 FFT on this arithmetic (vendored copy for now; the vendored copy gets deleted in favor of the pin once this lands).
Update: the runtime definitions (word helpers,
Limbs8,add/sub/neg,State9, and the CIOS multiplication) are now split out verbatim intoMontgomery/Native64x8Defs.lean, a module with zero imports. Rationale: downstream consumers put this module intoprecompileModulesnative-compilation lanes, andprecompileModulescompiles the entire import closure — with the defs interleaved intoNative64x8.lean, that closure includes mathlib. All theorems stay inNative64x8.lean, which imports the defs module. The ironwood consumer already uses exactly this shape (its vendored copy is a zero-import defs module).Update 2: the PR now also carries the scalar radix-2 DIT FFT over
Limbs8Montgomery elements (Montgomery/ScalarFft.lean), generic over the modulus like the rest of the defs module. It imports onlyNative64x8Defs— zero-import for the sameprecompileModulesreason — and is runtime definitions only; the correctness proofs live downstream in ironwood until upstreamed.🤖 Generated with Claude Code