Skip to content

feat(fields): four-limb (4x64) Montgomery for the 256-bit fields, mul speedup - #391

Open
graikos wants to merge 7 commits into
Verified-zkEVM:mainfrom
graikos:mont-4x64
Open

graikos wants to merge 7 commits into
Verified-zkEVM:mainfrom
graikos:mont-4x64

Conversation

@graikos

@graikos graikos commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

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.

  • new Montgomery/Native64x4*.lean, mirroring the eight-limb modules: flat UInt64 CIOS, mulHi from four 32-bit partial products, adc/sbb/mac word primitives, condSubWide for full-width moduli
  • carrier is { x : Limbs4 // x.toNat < modulus }; no Bounded predicate since every limb is a full word
  • inversion is the same Pornin binary GCD: the word-sized divstep loop moves to the shared Montgomery/DivstepDefs.lean, linear combinations are five words, initU = 2^(1135 - finalRounds)
  • proven: the multiplier, the inversion certificate, and mac-width safety of the candidate. The divstep coefficient bounds move from the eight-limb proofs to Montgomery/Divstep.lean
  • all seven instances are Mont64x4Field + GcdData constants, decide-guarded
  • removes the seven eight-limb modules and ScalarFft.lean, which no field and no downstream consumer uses any more
  • bench rows relabelled Limbs4/Mont64x4, and the inversion groups renamed to fields-mont64x4-*-inv. The A/B below ran before the rename

Pinned 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 Limbs4 only inside a branch, so it is allocation-free.

Validation

Run locally on the merge with main (Lean 4.35). lake build, lake test and lake build CompPolyBench pass with no warnings. lake exe axiomsweep --check reports no sorry and no non-standard axioms. lint-style.py, check-docs-integrity.py and check-imports.sh pass. CompPolyBench --validate-only passes on all 69 groups of bench/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.

  • The .val of a fast-field element is a Limbs4 (four 64-bit limbs), not a Limbs8.
  • The parameter class is Mont64x4Field, not Mont64x8Field. The instances are renamed: instMont64x8Field → instMont64x4Field, and for secp256k1 instScalarMont64x8Field / instBaseMont64x8Field → instScalarMont64x4Field / instBaseMont64x4Field.
  • GcdData moves from Montgomery.Native64x8 to Montgomery.Native64x4. Its initU is a Limbs4, and initU_toNat uses 2 ^ (1135 - finalRounds).
  • The word-sized divstep loop moves from Montgomery.Native64x8 to Montgomery (Montgomery.gcdInner, Montgomery.gcdBitLen, Montgomery.gcdInner_natAbs_le_31), in the new modules Montgomery/DivstepDefs.lean and Montgomery/Divstep.lean.
  • The inversion bench groups are renamed from fields-mont64x8-*-inv to fields-mont64x4-*-inv.
  • The eight-limb carrier is removed: the seven Montgomery/Native64x8*.lean modules (Montgomery.Native64x8, Limbs8, Mont64x8Field) and Montgomery/ScalarFft.lean.

@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

🤖 PR Summary

feat(fields): four-limb (4x64) Montgomery for the 256-bit fields, mul speedup

Mathematical Formalization

  • New Montgomery/Native64x4Defs.lean: Defines zero-import runtime primitives for four-limb (4×64-bit, 256-bit total) Montgomery arithmetic on UInt64. Introduces:
    • Word-level helpers mulHi, adc, sbb, mac, montM.
    • Limbs4 structure (carrier) with zero, one, ofNat, toNat.
    • Limbwise addition/subtraction (addLimbs, subLimbs).
    • Modular operations (condSub, condSubWide, add, sub, neg).
    • CIOS multiplication pipeline: accumulator states State5/State6, round functions mulAccum, mulReduce, mulRound, and final mul / square.
  • New Montgomery/Native64x4.lean: Full specification and proof suite for the above definitions, including:
    • Word-level theorems (addWord_value, subWord_value, flag_le_one).
    • Carry/borrow specifications (adc_spec, sbb_spec).
    • Multiply-high and multiply-accumulate theorems (mulHi_spec, mac_spec).
    • Montgomery multiplier correctness (montM_toNat).
    • Limbs4 properties (zero_toNat, one_toNat, ofNat_toNat, toNat_lt, toNat_mod, ext_of_toNat).
    • Carry/borrow chain lemmas (carry_chain_sum, borrow_chain_sum).
    • Limbwise addition/subtraction specs (addLimbs_spec, subLimbs_spec).
    • Conditional subtraction correctness (condSub_toNat, condSub_lt, condSubWide_toNat, condSubWide_lt).
    • Modular operation specs (add_toNat, add_lt, sub_toNat, sub_lt, neg_toNat, neg_lt).
    • No sorry or admit are present.
  • New Montgomery/Native64x4Field.lean: Defines Mont64x4Field class (replacing Mont64x8Field) with carrier FastField = { x : Limbs4 // x.toNat < modulus } (no Bounded condition). Arithmetic operations (add, sub, neg, mul) updated; proofs simplified by dropping boundedness. Adds val_mul theorem.
  • New Montgomery/Native64x4InvDefs.lean: Defines Pornin binary-GCD inverse candidate for four-limb fields. Includes:
    • GcdData class (fields: finalRounds, initU, proofs).
    • Five-word helpers (mulWord, add5, sub5).
    • Linear-combination core (gcdLinearCombDiv, negMod, lincombTail, gcdLinearCombMontyRed).
    • Word-length approximation (gcdLimb, gcdNumBits, gcdApprox).
    • Outer loop gcdMainLoop, final-phase gcdFinalChunks, candidate generator gcdInvCandidate, fallback montPow.
  • New Montgomery/Native64x4Inv.lean: Correctness proof for invGcdRaw. Key theorems:
    • subLimbs_borrow_eq_one_iff (borrow ↔ numeric order).
    • mulWord_spec, add5_spec (five-word arithmetic).
    • lincombTail_lt, gcdLinearCombMontyRed_lt, gcdMainLoop_bounded, gcdFinalChunks_lt, gcdInvCandidate_lt (mac-width safety, candidate below modulus).
    • invGcdRaw_eq_inv (two versions: via invWithCandidate and directly).
    • Wrapper FastField.invGcd with invGcd_eq_inv theorem.
    • No sorry or admit introduced.

Proof Completion (sorries removed)

  • None.

Protocols / Soundness

  • All seven field instances (BN254, BLS12-381, BLS12-377, Pallas, Vesta, secp256k1 scalar, secp256k1 base) are switched to Mont64x4Field + GcdData constants, with dec_trivial guarding.
  • The eight-limb modules (Native64x8, Native64x8Defs, Native64x8Mul, Native64x8Inv, Native64x8InvDefs, Native64x8Field) are deleted; only ScalarFft.lean remains but no field instantiates them.
  • New Montgomery/DivstepDefs.lean: Zero-import gcdBitLen (branch-based binary-search bit length) and gcdInner (recursive divstep loop for single UInt64 words).
  • New Montgomery/Divstep.lean: Theorems gcdInner_zero, gcdInner_succ (unfolding lemmas), gcdInner_natAbs_le (bound on matrix entries after rounds iterations), and gcdInner_natAbs_le_31 (specialised bound for identity initial matrix, rounds ≤ 31). These are shared with the eight-limb stack for divstep coefficient bounds.

Infrastructure / CI

  • bench/ci-groups.txt: Replaces five fields-mont64x8-*-inv entries with fields-mont64x4-*-inv (bn254, bls12-381, bls12-377, secp256k1-scalar, secp256k1-base).
  • bench/CompPolyBench/Fields/Montgomery.lean: Switches benchmark harness from Mont64x8Field to Mont64x4Field, updates representation from "Mont64x8" to "Mont64x4", renames group keys.
  • bench/CompPolyBench/Fields/Arith.lean: Updates ChainRep records from "Limbs8" to "Limbs4", sinks from sinkMont64x8 to sinkMont64x4, operation calls to Native64x4.FastField variants.
  • bench/README.md: Corrected limb count and benchmark prefix.
  • docs/wiki/autoresearch.md, docs/wiki/benchmark-best-times.md, docs/wiki/serialization.md, ROADMAP.md: Updated to reflect four-limb carriers, updated file paths, benchmark names, and instance references.

Documentation

  • CompPoly/Fields/README.md: Updated listing to reflect migration to Mont64x4 architecture; added new DivstepDefs.lean, Divstep.lean, Native64x4Bytes.lean; removed ScalarFft.lean.
  • CompPoly/Fields/Pasta.lean: Module doc-comment updated from "eight-limb" to "four-limb".

Refactoring

  • Core API migration:
    • CompPoly.lean: Replaces public imports from Native64x8 to Native64x4 (including Native64x4, Native64x4Bytes, Native64x4Defs, Native64x4Field, Native64x4Inv, Native64x4InvDefs, Native64x4Mul); removes ScalarFft import entirely.
    • CompPoly/Fields/BN254/Fast.lean: Renames instMont64x8Field → instMont64x4Field; updates initU, modulusLimbs, rModModulus, r2ModModulus, montgomeryNegInv to 64-bit limb values; switches open from Native64x8 to Native64x4.
    • CompPoly/Fields/BLS12_377/Fast.lean, CompPoly/Fields/BLS12_378/Fast.lean (note: typo in original; should be BLS12_381?): Same pattern: rename instance, update constants, switch imports.
    • CompPoly/Fields/Secp256k1/Fast.lean: Same pattern for both scalar and base field instances.
    • CompPoly/Fields/Montgomery/Native64x4Bytes.lean: Renamed from Native64x8Bytes.lean; updated module import, namespace, documentation; added Georgios Raikos as co-author; toBytes_eq_toBytes_toField unchanged except namespace.
  • File deletions:
    • CompPoly/Fields/Montgomery/Native64x8.lean (full file deleted).
    • CompPoly/Fields/Montgomery/Native64x8Defs.lean (deleted).
    • CompPoly/Fields/Montgomery/Native64x8Inv.lean (deleted).
    • CompPoly/Fields/Montgomery/Native64x8InvDefs.lean (deleted).
    • CompPoly/Fields/Montgomery/Native64x8Mul.lean (deleted).
    • CompPoly/Fields/Montgomery/ScalarFft.lean (deleted).
  • Test file updates: tests/CompPolyTests/Fields/BN254/Fast.lean, BLS12_381/Fast.lean, Pasta/Fast.lean, Montgomery/Bytes.lean updated from eight-limb to four-limb API references, literal values recomputed from merged 32-bit limbs to 64-bit.

Performance (per PR body, for context):

  • Benchmarked against eight-limb build: 29 faster, 32 same, 0 slower.
  • Fast mul 0.53–0.65× (e.g., BN254 32.9 → 21.0 ns; secp256k1 base 30.9 → 16.2 ns).
  • Add 0.78–0.94×; GCD inverse 0.80× (6.0 → 4.8 µs); Fermat 0.78–0.87×.
  • Against custom secp256k1 reduction (PR feat(fields): add fast secp field arithmetic #271): mul 14 vs 22 ns, add 2.6 vs 3.4 ns, inverse 4.6 vs 33 µs.
  • Allocation-free: keeps accumulators in flat words, builds Limbs4 only inside a branch.

Statistics

Metric Count
📝 Files Changed 38
✅ Lines Added 2001
❌ Lines Removed 2250

Lean Declarations

✏️ Removed: 141 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/Montgomery/Native64x4Field.lean (2)

  • @[inline] def toLimbs8 (x : FastField modulus) : Limbs8
  • theorem val_bounded (x : FastField modulus) : x.val.Bounded

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

  • private theorem add_of_carry {S A B Q D bo : ℕ} (hchain : S + 2 ^ 256 * 1 = A + B)
  • private theorem cond_eq_mod {T Q : ℕ} (h : T < 2 * Q) :
  • 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 land_mask32 (x : ℕ) : x &&& 4294967295 = x % 4294967296
  • 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 toNat_zero : (0 : UInt64).toNat = 0
  • private theorem wide_of_head {T L t8 Q D bo : ℕ} (hdec : T = L + 2 ^ 256 * t8)
  • theorem Limbs8.ext_of_toNat {x y : Limbs8} (hx : x.Bounded) (hy : y.Bounded)
  • theorem Limbs8.toNat_lt {x : Limbs8} (hx : x.Bounded) : x.toNat < 2 ^ 256
  • theorem adcLo_lt (x y c : UInt64) : (adcLo x y c).toNat < 2 ^ 32
  • theorem adc_spec (x y c : UInt64) (hx : x.toNat < 2 ^ 32) (hy : y.toNat < 2 ^ 32)
  • theorem adc_spec_wide (x y c : UInt64) (hx : x.toNat < 2 ^ 33) (hy : y.toNat < 2 ^ 32)
  • theorem addLimbs_bounded (a b : Limbs8) : (addLimbs a b).Bounded
  • theorem addLimbs_spec (a b : Limbs8) (ha : a.Bounded) (hb : b.Bounded) :
  • theorem addLimbs_toNat (a b : Limbs8) (ha : a.Bounded) (hb : b.Bounded) :
  • theorem add_bounded (q a b : Limbs8) : (add q a b).Bounded
  • theorem add_lt (q a b : Limbs8) (hq : q.Bounded) (ha : a.Bounded) (hb : b.Bounded)
  • theorem add_toNat (q a b : Limbs8) (hq : q.Bounded) (ha : a.Bounded) (hb : b.Bounded)
  • theorem borrow_chain_sum {x0 x1 x2 x3 x4 x5 x6 x7 y0 y1 y2 y3 y4 y5 y6 y7 : ℕ}
  • theorem carry_chain_sum {x0 x1 x2 x3 x4 x5 x6 x7 y0 y1 y2 y3 y4 y5 y6 y7 : ℕ}
  • theorem condSubWide_bounded (q : Limbs8) (t : State9) (ht : t.Bounded) :
  • theorem condSubWide_lt (q : Limbs8) (t : State9) (hq : q.Bounded) (ht : t.Bounded)
  • theorem condSubWide_toNat (q : Limbs8) (t : State9) (hq : q.Bounded) (ht : t.Bounded)
  • theorem condSub_bounded (q t : Limbs8) (ht : t.Bounded) : (condSub q t).Bounded
  • theorem condSub_lt (q t : Limbs8) (hq : q.Bounded) (ht : t.Bounded)
  • theorem condSub_toNat (q t : Limbs8) (hq : q.Bounded) (ht : t.Bounded) :
  • theorem macLo_lt (t x y c : UInt64) : (macLo t x y c).toNat < 2 ^ 32
  • theorem mac_spec (t x y c : UInt64) (ht : t.toNat < 2 ^ 32) (hx : x.toNat < 2 ^ 32)
  • theorem montM_lt (s negInv : UInt64) : (montM s negInv).toNat < 2 ^ 32
  • theorem montM_toNat (s negInv : UInt64) (hs : s.toNat < 2 ^ 32) (hn : negInv.toNat < 2 ^ 32) :
  • theorem neg_bounded (q a : Limbs8) : (neg q a).Bounded
  • theorem neg_lt (q a : Limbs8) (hq : q.Bounded) (ha : a.Bounded)
  • theorem neg_toNat (q a : Limbs8) (hq : q.Bounded) (ha : a.Bounded)
  • theorem ofNat_bounded (n : ℕ) : (ofNat n).Bounded
  • theorem ofNat_toNat (n : ℕ) : (ofNat n).toNat = n % 2 ^ 256
  • theorem one_bounded : one.Bounded
  • theorem one_toNat : one.toNat = 1
  • theorem sbbLo_lt (x y b : UInt64) : (sbbLo x y b).toNat < 2 ^ 32
  • theorem sbb_spec (x y b : UInt64) (hx : x.toNat < 2 ^ 32) (hy : y.toNat < 2 ^ 32)
  • theorem subLimbs_bounded (a b : Limbs8) : (subLimbs a b).Bounded
  • theorem subLimbs_spec (a b : Limbs8) (ha : a.Bounded) (hb : b.Bounded) :
  • theorem sub_bounded (q a b : Limbs8) : (sub q a b).Bounded
  • theorem sub_lt (q a b : Limbs8) (hq : q.Bounded) (ha : a.Bounded) (hb : b.Bounded)
  • theorem sub_toNat (q a b : Limbs8) (hq : q.Bounded) (ha : a.Bounded) (hb : b.Bounded)
  • theorem sum8_lt {a0 a1 a2 a3 a4 a5 a6 a7 : ℕ} (h0 : a0 < 2 ^ 32) (h1 : a1 < 2 ^ 32)
  • theorem zero_bounded : zero.Bounded
  • theorem zero_toNat : zero.toNat = 0

CompPoly/Fields/Montgomery/Native64x8Defs.lean (28)

  • @[inline] def adcCo (x y c : UInt64) : UInt64
  • @[inline] def adcLo (x y c : UInt64) : UInt64
  • @[inline] def add (q a b : Limbs8) : Limbs8
  • @[inline] def addCarry (a b : Limbs8) : UInt64
  • @[inline] def addLimbs (a b : Limbs8) : Limbs8
  • @[inline] def condSub (q t : Limbs8) : Limbs8
  • @[inline] def condSubWide (q : Limbs8) (t : State9) : Limbs8
  • @[inline] def macHi (t x y c : UInt64) : UInt64
  • @[inline] def macLo (t x y c : UInt64) : UInt64
  • @[inline] def mask : UInt64
  • @[inline] def montM (s negInv : UInt64) : UInt64
  • @[inline] def mul (q : Limbs8) (negInv : UInt64) (a b : Limbs8) : Limbs8
  • @[inline] def mulAccum (a : Limbs8) (bi : UInt64) (t : State9) : State9
  • @[inline] def mulReduce (q : Limbs8) (negInv : UInt64) (s : State9) : State9
  • @[inline] def mulRound (q : Limbs8) (negInv : UInt64) (a : Limbs8) (bi : UInt64)
  • @[inline] def neg (q a : Limbs8) : Limbs8
  • @[inline] def ofNat (n : Nat) : Limbs8
  • @[inline] def sbbBo (x y b : UInt64) : UInt64
  • @[inline] def sbbLo (x y b : UInt64) : UInt64
  • @[inline] def square (q : Limbs8) (negInv : UInt64) (a : Limbs8) : Limbs8
  • @[inline] def sub (q a b : Limbs8) : Limbs8
  • @[inline] def subBorrow (a b : Limbs8) : UInt64
  • @[inline] def subLimbs (a b : Limbs8) : Limbs8
  • @[inline] def toLimbs8 (t : State9) : Limbs8
  • @[inline] def zero : State9
  • def Bounded (t : State9) : Prop
  • def one : Limbs8
  • def toNat (t : State9) : Nat

CompPoly/Fields/Montgomery/Native64x8Inv.lean (23)

  • @[inline] def invGcd [GcdData modulus] (x : FastField modulus) : FastField modulus
  • private def invWithCandidate (x : FastField modulus) (z : Limbs8) : FastField modulus
  • private theorem and_mask_toNat_lt (y : UInt64) : (y &&& 0xFFFFFFFF).toNat < 2 ^ 32
  • private theorem exists_eq_tuple4 {α β γ δ : Type} (p : α × β × γ × δ) :
  • private theorem exists_eq_tuple6 {α β γ δ ε ζ : Type} (p : α × β × γ × δ × ε × ζ) :
  • private theorem invWithCandidate_eq_inv (x : FastField modulus) (z : Limbs8) :
  • private theorem lincomb_stack_lt {q aS bS : Limbs8} {negInv F G : UInt64}
  • private theorem toField_hpow (x : FastField modulus) (n : ℕ) :
  • theorem gcdFinalChunks_lt {q : Limbs8} {negInv : UInt64} {finalRounds : ℕ}
  • theorem gcdInner_natAbs_le {rounds : ℕ} {a b a' b' : UInt64}
  • theorem gcdInner_natAbs_le_31 {rounds : ℕ} (h31 : rounds ≤ 31) {a b a' b' : UInt64}
  • theorem gcdInner_succ (rounds : ℕ) (a b : UInt64) (f0 g0 f1 g1 : Int) :
  • theorem gcdInner_zero (a b : UInt64) (f0 g0 f1 g1 : Int) :
  • theorem gcdInvCandidate_lt {modulus : ℕ} [P : GcdData modulus] {q x : Limbs8}
  • theorem gcdInvCandidate_lt_modulus [GcdData modulus] {x : Limbs8} (hx : x.Bounded) :
  • theorem gcdLinearCombDiv_bounded (a b : Limbs8) (f g : Int) :
  • theorem gcdLinearCombMontyRed_lt {q : Limbs8} {negInv : UInt64} {a b : Limbs8} {f g : Int}
  • theorem gcdMainLoop_bounded {q : Limbs8} {negInv : UInt64} {rounds : ℕ}
  • theorem invGcdRaw_eq_inv [GcdData modulus] (x : FastField modulus) :
  • theorem invGcd_eq_inv [GcdData modulus] (x : FastField modulus) :
  • theorem montPow_eq_inv (x : FastField modulus) :
  • theorem montPow_eq_mul_pow (acc x : FastField modulus) (n : ℕ) :
  • theorem subBorrow_eq_one_iff {a b : Limbs8} (ha : a.Bounded) (hb : b.Bounded) :

CompPoly/Fields/Montgomery/Native64x8InvDefs.lean (14)

  • @[inline] def gcdAdd9 (p q : State9) : State9
  • @[inline] def gcdApprox (val : Limbs8) (limbIdx bits : Nat) : UInt64
  • @[inline] def gcdBitLen (x : UInt64) : Nat
  • @[inline] def gcdLimb (v : Limbs8) : Nat → UInt64
  • @[inline] def gcdLinearCombDiv (a b : Limbs8) (f g : Int) : Limbs8 × Int
  • @[inline] def gcdLinearCombMontyRed (q : Limbs8) (negInv : UInt64) (a b : Limbs8)
  • @[inline] def gcdNumBits (a b : Limbs8) : Nat × Nat
  • @[inline] def gcdSub9 (p q : State9) : State9 × UInt64
  • def gcdFinalChunks (q : Limbs8) (negInv : UInt64) (finalRounds : Nat)
  • def gcdInner (rounds : Nat) (a b : UInt64) (f0 g0 f1 g1 : Int) :
  • def gcdInvCandidate (modulus : Nat) [P : GcdData modulus] (q : Limbs8)
  • def gcdMainLoop (q : Limbs8) (negInv : UInt64) (rounds : Nat) (a u b v : Limbs8) :
  • def invGcdRaw (modulus : Nat) [GcdData modulus] (q : Limbs8) (negInv : UInt64)
  • def montPow (q : Limbs8) (negInv : UInt64) (acc x : Limbs8) (n : Nat) : Limbs8

CompPoly/Fields/Montgomery/Native64x8Mul.lean (16)

  • private theorem accum_assemble {Slow Tlow T Abi k7 t8 s8 : ℕ} (hT : T = Tlow + 2 ^ 256 * t8)
  • private theorem fold8 {T1 T2 T3 T4 T5 T6 T7 T8 A Q : ℕ}
  • private theorem montM_low_zero {s negInv Q q0 w u : ℕ} (hs : s < 2 ^ 32)
  • private theorem mul_finish (q : Limbs8) (t : State9) {A M : ℕ} (hq : q.Bounded)
  • private theorem reduce_assemble {V W Slow mQ S u7 v8 u8 s8 : ℕ}
  • private theorem round_bound {T A bi m Q Tp : ℕ} (h : 2 ^ 32 * Tp = T + A * bi + m * Q)
  • theorem Limbs8.toNat_mod (x : Limbs8) (h : x.l0.toNat < 2 ^ 32) :
  • theorem State9.toNat_eq (t : State9) :
  • theorem State9.zero_bounded : State9.zero.Bounded
  • theorem State9.zero_toNat : State9.zero.toNat = 0
  • theorem mulAccum_spec (a : Limbs8) (bi : UInt64) (t : State9) (ha : a.Bounded)
  • theorem mulReduce_spec (q : Limbs8) (negInv : UInt64) (s : State9) (hq : q.Bounded)
  • theorem mulRound_spec (q a : Limbs8) (negInv bi : UInt64) (t : State9)
  • theorem mul_spec (q : Limbs8) (negInv : UInt64) (a b : Limbs8)
  • theorem mul_sum8 (b a0 a1 a2 a3 a4 a5 a6 a7 : ℕ) :
  • theorem sum8_mul (a0 a1 a2 a3 a4 a5 a6 a7 b : ℕ) :

CompPoly/Fields/Montgomery/ScalarFft.lean (2)

  • def bitreverse (n l : Nat) : Nat
  • def fft (q : Limbs8) (negInv : UInt64) (a0 : Array Limbs8) (tw : Array Limbs8)

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

  • instance instMont64x8Field : Mont64x8Field Vesta.baseFieldSize where

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

  • instance instBaseMont64x8Field : Mont64x8Field Secp256k1.baseFieldSize where
  • instance instScalarMont64x8Field : Mont64x8Field Secp256k1.scalarFieldSize where

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

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

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/Montgomery/Divstep.lean (4)

  • theorem gcdInner_natAbs_le {rounds : Nat} {a b a' b' : UInt64}
  • theorem gcdInner_natAbs_le_31 {rounds : Nat} (h31 : rounds ≤ 31) {a b a' b' : UInt64}
  • theorem gcdInner_succ (rounds : Nat) (a b : UInt64) (f0 g0 f1 g1 : Int) :
  • theorem gcdInner_zero (a b : UInt64) (f0 g0 f1 g1 : Int) :

CompPoly/Fields/Montgomery/DivstepDefs.lean (2)

  • @[inline] def gcdBitLen (x : UInt64) : Nat
  • def gcdInner (rounds : Nat) (a b : UInt64) (f0 g0 f1 g1 : Int) :

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

  • 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 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
  • theorem sbb_spec (x y b : UInt64) (hb : b.toNat ≤ 1) :
  • theorem subLimbs_spec (a b : Limbs4) :
  • theorem sub_lt (q a b : Limbs4) (haq : a.toNat < q.toNat) (hbq : b.toNat < q.toNat) :
  • theorem sub_toNat (q a b : Limbs4) (haq : a.toNat < q.toNat) (hbq : b.toNat < q.toNat) :
  • theorem toNat_lt (x : Limbs4) : x.toNat < 2 ^ 256
  • theorem toNat_mod (x : Limbs4) : x.toNat % 2 ^ 64 = x.l0.toNat
  • theorem zero_toNat : zero.toNat = 0

CompPoly/Fields/Montgomery/Native64x4Defs.lean (22)

  • @[inline] def adc (x y c : UInt64) : UInt64 × UInt64
  • @[inline] def add (q a b : Limbs4) : Limbs4
  • @[inline] def addLimbs (a b : Limbs4) : UInt64 × UInt64 × UInt64 × UInt64 × UInt64
  • @[inline] def condSub (q t : Limbs4) : Limbs4
  • @[inline] def condSubWide (q : Limbs4) (t : State5) : Limbs4
  • @[inline] def mac (t a b c : UInt64) : UInt64 × UInt64
  • @[inline] def montM (s negInv : UInt64) : UInt64
  • @[inline] def mul (q : Limbs4) (negInv : UInt64) (a b : Limbs4) : Limbs4
  • @[inline] def mulAccum (a : Limbs4) (bi : UInt64) (t : State5) : State6
  • @[inline] def mulHi (a b : UInt64) : UInt64
  • @[inline] def mulReduce (q : Limbs4) (negInv : UInt64) (s : State6) : State5
  • @[inline] def mulRound (q : Limbs4) (negInv : UInt64) (a : Limbs4) (bi : UInt64)
  • @[inline] def neg (q a : Limbs4) : Limbs4
  • @[inline] def ofNat (n : Nat) : Limbs4
  • @[inline] def sbb (x y b : UInt64) : UInt64 × UInt64
  • @[inline] def square (q : Limbs4) (negInv : UInt64) (a : Limbs4) : Limbs4
  • @[inline] def sub (q a b : Limbs4) : Limbs4
  • @[inline] def subLimbs (a b : Limbs4) : UInt64 × UInt64 × UInt64 × UInt64 × UInt64
  • @[inline] def toLimbs4 (t : State5) : Limbs4
  • @[inline] def zero : State5
  • def one : Limbs4
  • def toNat (t : State6) : Nat

CompPoly/Fields/Montgomery/Native64x4Field.lean (3)

  • @[inline] def toLimbs4 (x : FastField modulus) : Limbs4
  • private theorem ofNat_lt_of_lt {n : ℕ} (h : n < modulus) :
  • theorem val_mul (x y : FastField modulus) :

CompPoly/Fields/Montgomery/Native64x4Inv.lean (20)

  • @[inline] def invGcd [GcdData modulus] (x : FastField modulus) : FastField modulus
  • private def invWithCandidate (x : FastField modulus) (z : Limbs4) : FastField modulus
  • private theorem exists_eq_tuple4 {α β γ δ : Type} (p : α × β × γ × δ) :
  • private theorem exists_eq_tuple6 {α β γ δ ε ζ : Type} (p : α × β × γ × δ × ε × ζ) :
  • private theorem invGcdRaw_eq_invWithCandidate (x : FastField modulus) (z : Limbs4) :
  • private theorem invWithCandidate_eq_inv (x : FastField modulus) (z : Limbs4) :
  • private theorem toField_hpow (x : FastField modulus) (n : ℕ) :
  • theorem add5_spec (a0 a1 a2 a3 a4 b0 b1 b2 b3 b4 : UInt64) :
  • theorem gcdFinalChunks_lt {q : Limbs4} {negInv : UInt64} {finalRounds : ℕ}
  • theorem gcdInvCandidate_lt {modulus : ℕ} [P : GcdData modulus] {q x : Limbs4}
  • theorem gcdInvCandidate_lt_modulus [GcdData modulus] (x : Limbs4) :
  • theorem gcdLinearCombMontyRed_lt {q : Limbs4} {negInv : UInt64} {u v : Limbs4} {f g : Int}
  • theorem gcdMainLoop_bounded {q : Limbs4} {negInv : UInt64} {rounds : ℕ}
  • theorem invGcdRaw_eq_inv [GcdData modulus] (x : FastField modulus) :
  • theorem invGcd_eq_inv [GcdData modulus] (x : FastField modulus) :
  • theorem lincombTail_lt {q uS vS : Limbs4} {negInv F G : UInt64} (hq0 : 0 < q.toNat)
  • theorem montPow_eq_inv (x : FastField modulus) :
  • theorem montPow_eq_mul_pow (acc x : FastField modulus) (n : ℕ) :
  • theorem mulWord_spec (x : Limbs4) (k : UInt64) :
  • theorem subLimbs_borrow_eq_one_iff {a b : Limbs4} {d0 d1 d2 d3 bo : UInt64}

CompPoly/Fields/Montgomery/Native64x4InvDefs.lean (15)

  • @[inline] def add5 (a0 a1 a2 a3 a4 b0 b1 b2 b3 b4 : UInt64) :
  • @[inline] def gcdApprox (val : Limbs4) (limbIdx bits : Nat) : UInt64
  • @[inline] def gcdLimb (v : Limbs4) : Nat → UInt64
  • @[inline] def gcdLinearCombDiv (a b : Limbs4) (f g : Int) : Limbs4 × Int
  • @[inline] def gcdLinearCombMontyRed (q : Limbs4) (negInv : UInt64) (u v : Limbs4)
  • @[inline] def gcdNumBits (a b : Limbs4) : Nat × Nat
  • @[inline] def lincombTail (q : Limbs4) (negInv : UInt64) (uS vS : Limbs4) (F G : UInt64) :
  • @[inline] def mulWord (x : Limbs4) (k : UInt64) : UInt64 × UInt64 × UInt64 × UInt64 × UInt64
  • @[inline] def negMod (q u : Limbs4) : Limbs4
  • @[inline] def sub5 (a0 a1 a2 a3 a4 b0 b1 b2 b3 b4 : UInt64) :
  • def gcdFinalChunks (q : Limbs4) (negInv : UInt64) (finalRounds : Nat)
  • def gcdInvCandidate (modulus : Nat) [P : GcdData modulus] (q : Limbs4)
  • def gcdMainLoop (q : Limbs4) (negInv : UInt64) (rounds : Nat) (a u b v : Limbs4) :
  • def invGcdRaw (modulus : Nat) [GcdData modulus] (q : Limbs4) (negInv : UInt64)
  • def montPow (q : Limbs4) (negInv : UInt64) (acc x : Limbs4) (n : Nat) : Limbs4

CompPoly/Fields/Montgomery/Native64x4Mul.lean (12)

  • private theorem fold4 {T1 T2 T3 T4 A Q B0 B1 B2 B3 m0 m1 m2 m3 : ℕ}
  • private theorem montM_low_zero {s negInv Q q0 w u : ℕ} (hs : s < 2 ^ 64)
  • private theorem mul_finish (q : Limbs4) (t : State5) {A M : ℕ}
  • private theorem round_bound {T A bi m Q Tp : ℕ} (h : 2 ^ 64 * Tp = T + A * bi + m * Q)
  • theorem State5.toNat_eq (t : State5) :
  • theorem State5.zero_toNat : State5.zero.toNat = 0
  • theorem mulAccum_spec (a : Limbs4) (bi : UInt64) (t : State5) :
  • theorem mulReduce_spec (q : Limbs4) (negInv : UInt64) (s : State6) (hs5 : s.t5.toNat ≤ 1)
  • theorem mulRound_spec (q a : Limbs4) (negInv bi : UInt64) (t : State5)
  • theorem mul_spec (q : Limbs4) (negInv : UInt64) (a b : Limbs4)
  • theorem mul_sum4 (b a0 a1 a2 a3 : ℕ) :
  • theorem sum4_mul (a0 a1 a2 a3 b : ℕ) :

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

  • instance instMont64x4Field : Mont64x4Field Vesta.baseFieldSize where

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

  • instance instBaseMont64x4Field : Mont64x4Field Secp256k1.baseFieldSize where
  • instance instScalarMont64x4Field : Mont64x4Field Secp256k1.scalarFieldSize where

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

  • @[inline] def sinkMont64x4 {modulus : Nat} [Montgomery.Native64x4.Mont64x4Field modulus]
✏️ Affected: 23 declaration(s) (line number changed)
  • @[inline] def ofInt (modulus : ℕ) [P : Mont64x4Field modulus] (n : Int) : in CompPoly/Fields/Montgomery/Native64x4Field.lean moved from L218 to L195
  • @[inline] def ofNat (modulus : ℕ) [P : Mont64x4Field modulus] (n : ℕ) : FastField modulus in CompPoly/Fields/Montgomery/Native64x4Field.lean moved from L210 to L187
  • @[inline] def toNat (x : FastField modulus) : ℕ in CompPoly/Fields/Montgomery/Native64x4Field.lean moved from L186 to L168
  • def FastField (modulus : ℕ) [Mont64x4Field modulus] : Type in CompPoly/Fields/Montgomery/Native64x4Field.lean moved from L101 to L93
  • def one (modulus : ℕ) [P : Mont64x4Field modulus] : FastField modulus in CompPoly/Fields/Montgomery/Native64x4Field.lean moved from L128 to L118
  • def ringEquiv (modulus : ℕ) [P : Mont64x4Field modulus] : in CompPoly/Fields/Montgomery/Native64x4Field.lean moved from L507 to L482
  • def zero (modulus : ℕ) [P : Mont64x4Field modulus] : FastField modulus in CompPoly/Fields/Montgomery/Native64x4Field.lean moved from L122 to L112
  • private theorem mul_cast (x y : Limbs4) (hxq : x.toNat < modulus) : in CompPoly/Fields/Montgomery/Native64x4Field.lean moved from L273 to L255
  • theorem negInv_mul_q : P.montgomeryNegInv.toNat * P.modulusLimbs.toNat % 2 ^ 64 = 2 ^ 64 - 1 in CompPoly/Fields/Montgomery/Native64x4Field.lean moved from L79 to L71
  • private def runScalarInv (modulus : Nat) [Mont64x4Field modulus] [GcdData modulus] in bench/CompPolyBench/Fields/Montgomery.lean moved from L33 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.

📋 **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 Native64x8 modules without deprecated aliases; otherwise the contribution follows the project’s style and guidelines closely.


📄 **Per-File Summaries**
  • CompPoly.lean: The file now publicly imports CompPoly.Fields.Montgomery.Divstep and CompPoly.Fields.Montgomery.DivstepDefs. All previous public imports from CompPoly.Fields.Montgomery.Native64x8 (including Native64x8, Native64x8Bytes, Native64x8Defs, Native64x8Field, Native64x8Inv, Native64x8InvDefs, Native64x8Mul, and ScalarFft) have been replaced with their Native64x4 counterparts (Native64x4, Native64x4Bytes, Native64x4Defs, Native64x4Field, Native64x4Inv, Native64x4InvDefs, Native64x4Mul); the ScalarFft import has been removed entirely.
  • CompPoly/Fields/BLS12_377/Fast.lean: The file switches from an eight-limb (32-bit-limb) Montgomery implementation to a four-limb (64-bit-limb) Montgomery implementation for BLS12-377 scalar field arithmetic. The public import is changed from CompPoly.Fields.Montgomery.Native64x8Inv to CompPoly.Fields.Montgomery.Native64x4Inv, and the open statement is updated accordingly. The instance instGcdData is updated with a new single-limb initU value, the instance formerly named instMont64x8Field is renamed to instMont64x4Field and its modulusLimbs, rModModulus, r2ModModulus, and montgomeryNegInv fields are updated to use 64-bit limb representations. Comments and docstrings are updated to reflect the change from eight-limb to four-limb representation. No sorry or admit are added.
  • CompPoly/Fields/BLS12_381/Fast.lean: Switched the BLS12-381 scalar field fast implementation from an eight-limb (32-bit limb) Montgomery representation to a four-limb (64-bit limb) one. The public import and open directives now refer to Montgomery.Native64x4 instead of Native64x8, the instance instMont64x8Field is renamed to instMont64x4Field with type Mont64x4Field, and all associated constants (initU, modulusLimbs, rModModulus, r2ModModulus, montgomeryNegInv) are updated to 64-bit limb values. The GcdData instance's initU is similarly re-encoded as four 64-bit limbs. Module and doc comments are updated accordingly; no sorry or admit are introduced.
  • CompPoly/Fields/BN254/Fast.lean: This file switches the BN254 scalar field implementation from an eight-limb (32-bit-limb) Montgomery representation to a four-limb (64-bit-limb) Montgomery representation. The instGcdData instance now stores a single 4-limb initU value and instMont64x8Field is renamed to instMont64x4Field with its modulusLimbs, rModModulus, r2ModModulus, and montgomeryNegInv constants updated to 4-limb (64-bit-per-limb) values. The open import is changed from Montgomery.Native64x8 to Montgomery.Native64x4, the module docstring is updated, and the comments on ScalarField and ringEquiv now say "four-limb" instead of "eight-limb"; the definitions of ScalarField, ofField, and ringEquiv are unchanged in logic, just re-exposed under the new type class.
  • CompPoly/Fields/Montgomery/Divstep.lean: This new file adds three theorems about the gcdInner function defined in Montgomery/DivstepDefs. gcdInner_zero and gcdInner_succ provide unfolding lemmas for the zero‑step and the one‑step cases, respectively. gcdInner_natAbs_le proves that after rounds iterations the sum of the absolute values of each row of the transition matrix is bounded by 2^rounds * n if it initially was bounded by n. gcdInner_natAbs_le_31 specialises this to the case where the initial matrix is the identity (entries 1,0,0,1) and rounds ≤ 31, yielding the bound 2^31 that is needed for the multi‑limb inversion runtime.
  • CompPoly/Fields/Montgomery/DivstepDefs.lean: This new file CompPoly/Fields/Montgomery/DivstepDefs.lean introduces two runtime definitions for the word-sized divstep loop from Pornin's binary-GCD inversion (eprint 2020/972), designed to operate on single UInt64 words with zero imports for precompileModules. Specifically, it defines gcdBitLen — an inline function that computes the bit length of a UInt64 word (returning 0 for zero) via a branch-based binary search — and gcdInner — a recursive function that takes rounds, a, b (which must be odd), and six integers f0, g0, f1, g1; when a is odd, it conditionally swaps on a < b, subtracts b from a, updates the matrix entries accordingly, then shifts a right by one and doubles f1 and g1, and repeats for the remaining rounds, returning the final sextuple.
  • CompPoly/Fields/Montgomery/Native64x4.lean: This new file CompPoly/Fields/Montgomery/Native64x4.lean adds the full specification and proof suite for Montgomery arithmetic over four 64‑bit limbs (Limbs4). It provides word‑level helper theorems (addWord_value, subWord_value, flag_le_one), the core add‑/subtract‑with‑carry/borrow specifications (adc_spec, sbb_spec), multiply‑high and multiply‑accumulate theorems (mulHi_spec, mac_spec), a Montgomery‑multiplier lemma (montM_toNat), properties of the Limbs4 type (zero_toNat, one_toNat, ofNat_toNat, toNat_lt, toNat_mod, ext_of_toNat), carry‑ and borrow‑chain combinators (carry_chain_sum, borrow_chain_sum), auxiliary branch lemmas (private), correctness proofs for limb‑wise addition/subtraction (addLimbs_spec, subLimbs_spec), for conditional subtraction (condSub_toNat, condSub_lt, condSubWide_toNat, condSubWide_lt), and for modular addition, subtraction, and negation (add_toNat, add_lt, sub_toNat, sub_lt, neg_toNat, neg_lt). No sorry or admit appear in the diff.
  • CompPoly/Fields/Montgomery/Native64x4Bytes.lean: Renamed Native64x8Bytes.lean to Native64x4Bytes.lean, reflecting a shift from an eight-limb Montgomery carrier (32-bit limbs) to a four-limb carrier (64-bit limbs). Updated the module import, documentation, and namespace from Montgomery.Native64x8 to Montgomery.Native64x4 accordingly, and added Georgios Raikos as a co-author. The theorem toBytes_eq_toBytes_toField remains unchanged except for its namespace qualifier.
  • CompPoly/Fields/Montgomery/Native64x4Defs.lean: Adds the new file CompPoly/Fields/Montgomery/Native64x4Defs.lean, which defines runtime definitions for four-limb (256-bit) Montgomery arithmetic on 64-bit limbs with zero imports (for precompileModules). It introduces word-level helpers (mulHi, adc, sbb, mac, montM), the Limbs4 structure with zero, one, ofNat, and toNat, limbwise addition/subtraction (addLimbs, subLimbs), modular operations (condSub, add, sub, neg), and the CIOS multiplication pipeline with accumulator states State5/State6, round functions mulAccum, mulReduce, mulRound, condSubWide, and finally mul and square. No sorry or admit appear.
  • CompPoly/Fields/Montgomery/Native64x4Field.lean: This diff renames Native64x8Field.lean to Native64x4Field.lean and rewrites the entire file from an eight‑limb (32‑bit) to a four‑limb (64‑bit) Montgomery field implementation. The class Mont64x8Field becomes Mont64x4Field, the carrier FastField now uses Limbs4 and drops the Bounded condition, and all arithmetic operations (add, sub, neg, mul) as well as the per‑field constants (e.g. montgomeryNegInv now mod 2^64) are updated accordingly. The removed boundedness arguments simplify proofs, and new theorems such as val_mul are added to expose the limbs of a product. No sorry or admit are introduced.
  • CompPoly/Fields/Montgomery/Native64x4Inv.lean: This new file adds CompPoly/Fields/Montgomery/Native64x4Inv.lean, which proves the correctness of the checked inversion invGcdRaw from Native64x4InvDefs. It introduces several new theorems: subLimbs_borrow_eq_one_iff (ties the borrow bit to numeric order), mulWord_spec and add5_spec (five-word arithmetic specifications), lincombTail_lt, gcdLinearCombMontyRed_lt, gcdMainLoop_bounded, gcdFinalChunks_lt, and gcdInvCandidate_lt (all establishing that the candidate output remains below the modulus by proving mac-width safety), and the two invGcdRaw_eq_inv theorems (stating that invGcdRaw computes the field inverse, first in terms of invWithCandidate and then directly). It also adds the wrapper definition FastField.invGcd and the theorem invGcd_eq_inv showing it agrees with the field inverse. The file includes the auxiliary private definitions exists_eq_tuple4, exists_eq_tuple6, invWithCandidate, and the private theorem toField_hpow; it does not contain any sorry or admit.
  • CompPoly/Fields/Montgomery/Native64x4InvDefs.lean: Added CompPoly/Fields/Montgomery/Native64x4InvDefs.lean — a Mathlib-free module defining the Pornin binary‑GCD inverse candidate over four‑limb UInt64 words (Limbs4) together with a checked wrapper invGcdRaw. New declarations include the per‑field schedule class GcdData (with fields finalRounds, initU, and proofs), five‑word helpers (mulWord, add5, sub5), linear‑combination core functions (gcdLinearCombDiv, negMod, lincombTail, gcdLinearCombMontyRed), word‑length approximation (gcdLimb, gcdNumBits, gcdApprox), the outer loop gcdMainLoop, the final‑phase reducer gcdFinalChunks, the candidate generator gcdInvCandidate, and the fallback exponentiation montPow. The definition file is proof‑free; correctness is intended to be established in the companion proof file Montgomery/Native64x4Inv.
  • CompPoly/Fields/Montgomery/Native64x4Mul.lean: Summary unavailable — AI generation failed.
  • CompPoly/Fields/Montgomery/Native64x8.lean: The entire file CompPoly/Fields/Montgomery/Native64x8.lean was deleted. It previously contained the formal specification and correctness proofs for native Montgomery arithmetic over eight 32-bit limbs, including word-level lemmas (adc_spec, sbb_spec, mac_spec, montM_toNat), chain lemmas (carry_chain_sum, borrow_chain_sum), and field operations (add_toNat, sub_toNat, neg_toNat) on Limbs8. No sorry or admit were added (none appeared in the diff).
  • CompPoly/Fields/Montgomery/Native64x8Defs.lean: The file CompPoly/Fields/Montgomery/Native64x8Defs.lean has been deleted in its entirety. This file contained the zero-import runtime definitions for eight-limb Montgomery arithmetic (256-bit via 32-bit limbs stored in UInt64), including the Limbs8 and State9 structures, limbwise addition/subtraction (addLimbs, subLimbs), conditional subtraction (condSub, condSubWide), and the CIOS Montgomery multiplication/squaring pipeline (mul, square). Its removal consolidates all definitions and their correctness proofs into the sibling module CompPoly.Fields.Montgomery.Native64x8—any downstream consumers that relied on precompileModules importing this file must adjust to import the new sole location.
  • CompPoly/Fields/Montgomery/Native64x8Inv.lean: This pull request deletes the entire file CompPoly/Fields/Montgomery/Native64x8Inv.lean (436 lines). The file contained the Lean 4 proof of correctness for a fast GCD-based inversion algorithm (invGcdRaw) in eight-limb Montgomery fields, including theorems such as gcdInner_natAbs_le (bound on transformation coefficients), gcdLinearCombMontyRed_lt (boundedness and canonicity of the Montgomery linear combination), gcdMainLoop_bounded (invariant preservation of the main loop), gcdInvCandidate_lt (mac-width safety of the candidate), montPow_eq_inv (correctness of the Fermat powering fallback), and invGcdRaw_eq_inv (the raw inverse function computes the field inverse). It also defined the proof-carrying wrapper FastField.invGcd and the theorem FastField.invGcd_eq_inv. No replacement or relocation is indicated; the file is simply removed.
  • CompPoly/Fields/Montgomery/Native64x8InvDefs.lean: This pull request deletes the file CompPoly/Fields/Montgomery/Native64x8InvDefs.lean. The file defined the runtime components of the eight-limb binary-GCD inverse candidate (gcdInvCandidate), its checked wrapper (invGcdRaw), the supporting GcdData class, and auxiliary functions (gcdInner, gcdMainLoop, gcdFinalChunks, montPow, etc.) for a Mathlib-free implementation. Removing this file eliminates those definitions from the project.
  • CompPoly/Fields/Montgomery/Native64x8Mul.lean: The commit entirely deletes CompPoly/Fields/Montgomery/Native64x8Mul.lean. That file contained a full formal correctness proof for an eight-limb CIOS Montgomery multiplication routine (mul), including the lemmas mulAccum_spec, mulReduce_spec, mulRound_spec, the private helper theorems fold8 and mul_finish, and the top-level theorem mul_spec. Its removal means that all those specifications and their supporting results (e.g., round_bound, accum_assemble, reduce_assemble, montM_low_zero) are no longer present in the project.
  • CompPoly/Fields/Montgomery/ScalarFft.lean: The entire file CompPoly/Fields/Montgomery/ScalarFft.lean was deleted. This file contained runtime definitions for an in-place radix-2 decimation-in-time FFT over eight-limb Montgomery residues, including the bitreverse index helper and the fft function itself. Its removal suggests the FFT implementation is no longer needed or has been moved elsewhere.
  • CompPoly/Fields/Pasta.lean: Updated the module-level doc-comment in CompPoly/Fields/Pasta.lean from referring to "eight-limb Montgomery implementations" to "four-limb Montgomery implementations". This corrects the facade module's description to match the actual implementation re-exported from CompPoly/Fields/Pasta.Fast.
  • CompPoly/Fields/Pasta/Fast.lean: Summary unavailable — AI generation failed.
  • CompPoly/Fields/README.md: Updated the README file listing to reflect the migration of the fast Montgomery field implementations from an eight-limb (Mont64x8) to a four-limb (Mont64x4) architecture for BLS12-377, BLS12-381, BN254, Pasta, and Secp256k1. All corresponding fast, defs, mul, inv, and facade files are renamed or their descriptions changed accordingly. Added new files: Montgomery/DivstepDefs.lean and Montgomery/Divstep.lean containing zero-import word-sized divstep loop definitions and lemmas, and Montgomery/Native64x4Bytes.lean providing a byte codec for the four-limb carrier. Removed the Montgomery/ScalarFft.lean entry.
  • CompPoly/Fields/Secp256k1/Fast.lean: Changed the Montgomery implementations from an eight-limb (Native64x8) to a four-limb (Native64x4) representation for both the secp256k1 scalar and base fields. Updated the public import from CompPoly.Fields.Montgomery.Native64x8Inv to CompPoly.Fields.Montgomery.Native64x4Inv, changed the open module from Montgomery.Native64x8 to Montgomery.Native64x4, and renamed the instances instScalarMont64x8Field and instBaseMont64x8Field to instScalarMont64x4Field and instBaseMont64x4Field respectively. All constant values (initU, modulusLimbs, rModModulus, r2ModModulus, montgomeryNegInv) for both instScalarGcdData and instBaseGcdData and both field instances were replaced with new four-limb values. Updated docstrings to reflect the four-limb wording, but the type aliases ScalarField, BaseField and the ofScalarField, ofBaseField, scalarRingEquiv, baseRingEquiv definitions remained unchanged in signature.
  • ROADMAP.md: The ROADMAP.md document was updated to reflect a project-wide shift from an eight-limb to a four-limb Montgomery carrier for prime moduli below 2^256, with the eight-limb carrier being explicitly replaced. Corresponding references to the inversion file (Native64x8Inv.lean → Native64x4Inv.lean) and benchmark names (fields-mont64x8-*-inv → fields-mont64x4-*-inv) were changed, and the operation‑coverage listing now notes the four‑limb Montgomery multiply instead of the eight‑limb one. Two new field instantiations (Pasta/Fast.lean and Secp256k1/Fast.lean) were added to the four‑limb carrier’s list, while BN254/Fast.lean, BLS12_381/Fast.lean, and BLS12_377/Fast.lean now point to the updated implementation.
  • bench/CompPolyBench/Fields/Arith.lean: This file updates the benchmark harness for pairing scalar field and secp256k1 arithmetic to reflect a change from Native64x8 (8-limb) to Native64x4 (4-limb) Montgomery representations. All documentation comments that previously described the pairing scalar fields as "eight-limb" are now updated to "four-limb". In the six bn254Reps, bls12_381Reps, bls12_377Reps, secp256k1ScalarReps, and secp256k1BaseReps functions, each fast-field ChainRep record is changed from representation := "Limbs8" to "Limbs4" and its sink field is changed from sinkMont64x8 to sinkMont64x4. Correspondingly, in each of the ten runBn254Mul, runBls12_381Mul, runBls12_377Mul, runBn254Add, runBls12_381Add, runBls12_377Add, runSecp256k1ScalarMul, runSecp256k1ScalarAdd, runSecp256k1BaseMul, and runSecp256k1BaseAdd functions, the fast-field operation is switched from Montgomery.Native64x8.FastField.mul (or .add) to Montgomery.Native64x4.FastField.mul (or .add). The expChainRounds docstring also now refers to "four-limb scalar fields" instead of "eight-limb". No sorry or admit is added.
  • bench/CompPolyBench/Fields/Inputs.lean: Summary unavailable — AI generation failed.
  • bench/CompPolyBench/Fields/Montgomery.lean: Updated the scalar-field inversion benchmarks from the eight-limb Mont64x8Field to the four-limb Mont64x4Field representation. This involved changing the open import from Montgomery.Native64x8 to Montgomery.Native64x4, updating the typeclass constraint on runScalarInv from [Mont64x8Field modulus] to [Mont64x4Field modulus], and modifying the representation field in all benchmark record metadata from "Mont64x8" to "Mont64x4". All benchmark group keys (e.g., "fields-mont64x8-bn254-inv") were renamed to their four-limb equivalents, and the doc comment on montgomeryInvTasks was updated to reflect that these are now four-limb benchmarks.
  • bench/README.md: Updated bench/README.md to correct the limb count for field arithmetic benchmarks from "Eight-limb" to "Four-limb" and the corresponding benchmark prefix from fields-mont64x8-*-inv to fields-mont64x4-*-inv. This aligns the documentation with the actual four-limb Montgomery representation used in the benchmarks, ensuring accuracy for readers understanding the benchmark configurations.
  • bench/ci-groups.txt: The bench/ci-groups.txt file replaces five benchmark entries for field inversions (fields-mont64x8-*-inv) with their mont64x4 counterparts (fields-mont64x4-*-inv), covering the curves bn254, bls12-381, bls12-377, secp256k1-scalar, and secp256k1-base. This changes which implementation variant (likely a different word width or SIMD configuration) is included in the CI grouping.
  • docs/wiki/autoresearch.md: Updated the docs/wiki/autoresearch.md file to rename the 'Eight-limb inversion' table row to 'Four-limb inversion', and accordingly updated the associated target group names from fields-mont64x8-...-inv to fields-mont64x4-...-inv, the implementation file reference from CompPoly/Fields/Montgomery/Native64x8InvDefs.lean to CompPoly/Fields/Montgomery/Native64x4InvDefs.lean, and the related file from CompPoly/Fields/Montgomery/Native64x8Inv.lean to CompPoly/Fields/Montgomery/Native64x4Inv.lean. Also updated the 'Base-field arithmetic' row's implementation file reference from CompPoly/Fields/Montgomery/Native64x8Mul.lean to CompPoly/Fields/Montgomery/Native64x4Mul.lean. These changes reflect a refactoring of the field arithmetic implementation from 64x8-limb to 64x4-limb internal representation, keeping the table's other columns (Gate, Idea) and the overall comparison structure unchanged.
  • docs/wiki/benchmark-best-times.md: The documentation for pairing scalar fields was updated to reflect a change from eight-limb to four-limb Montgomery representation, with corresponding updates to file paths for mul and inv definitions. This corrects the technical description to match the current implementation.
  • docs/wiki/serialization.md: Corrected the table entry for the Montgomery representation used by BN254, BLS12-377/381, Pasta, and secp256k1 fields: changed the carrier name from Native64x8.FastField to Native64x4.FastField and updated the corresponding instance module path from CompPoly/Fields/Montgomery/Native64x8Bytes.lean to CompPoly/Fields/Montgomery/Native64x4Bytes.lean.
  • tests/CompPolyTests/Fields/BLS12_377/Fast.lean: Summary unavailable — AI generation failed.
  • tests/CompPolyTests/Fields/BLS12_381/Fast.lean: Updated tests/CompPolyTests/Fields/BLS12_381/Fast.lean to reflect a change from an eight-limb to a four-limb Montgomery representation. All references to Montgomery.Native64x8, Limbs8, Mont64x8Field, and related entry points (rModModulus, modulusLimbs, montgomeryNegInv, montPow) have been replaced with their Native64x4, Limbs4, Mont64x4Field, and analogous four-limb counterparts. The file comment and #guards for (0 : ScalarField).val and (1 : ScalarField).val have also been updated accordingly.
  • tests/CompPolyTests/Fields/BN254/Fast.lean: The file updates all Montgomery representation references from the 8-limb API (Montgomery.Native64x8, Limbs8, Mont64x8Field) to the corresponding 4-limb API (Montgomery.Native64x4, Limbs4, Mont64x4Field), and amends the module docstring to say "four-limb Montgomery representation" instead of "eight-limb". This reflects a redesign of the underlying CompPoly.Fields.BN254.Fast implementation, and the test file now exercises the new 4-limb representation throughout.
  • tests/CompPolyTests/Fields/Montgomery/Bytes.lean: The diff replaces all references to Native64x8Bytes with Native64x4Bytes in both meta and non-meta imports, and updates the proof invocation from Montgomery.Native64x8.FastField.toBytes_ofField n to Montgomery.Native64x4.FastField.toBytes_ofField n in the example establishing property for BN254. The other examples in the file remain unchanged.
  • tests/CompPolyTests/Fields/Pasta/Fast.lean: The test file is updated to reflect a refactor of the Pasta fast-field arithmetic from an eight‑limb (Limbs8, 32‑bit limbs) to a four‑limb (Limbs4, 64‑bit limbs) Montgomery representation. All private definitions (q, negInv, rMod, a, b, montA, montB, montAB, pq, pNegInv, pMontA, pMontB, pMontAB) now use Limbs4 instead of Limbs8, and their literal values are recomputed as pairs of the original 32‑bit limbs merged into single 64‑bit limbs. The open statement switches from _root_.Montgomery.Native64x8 to _root_.Montgomery.Native64x4, and the instance paths Vesta.Fast.instMont64x8Field and Pallas.Fast.instMont64x8Field are replaced by their instMont64x4Field counterparts. The guard comparisons against Limbs8.zero are changed to Limbs4.zero. No sorry or admit are introduced; the change is purely a mechanical adaptation of the regression checks to the new limb width.
  • tests/CompPolyTests/Fields/Secp256k1/Fast.lean: Summary unavailable — AI generation failed.

Last updated: 2026-11-06 13:59 UTC.

@graikos
graikos marked this pull request as ready for review September 30, 2026 14:52

@dhsorens dhsorens left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, unsafe or csimp. Every per-field constant is a class field with a decide default, so a wrong limb fails the build.
  • Correctness. The Field instance is transferred along toField_injective. mul_spec and condSubWide need only negInv * q ≡ -1 and a < q, so the full-width secp256k1 moduli need no extra side condition.
  • Inversion. invGcdRaw verifies the candidate (cand < q ∧ cand * x = R) and falls back to the proved Fermat montPow, so invGcd_eq_inv does not depend on the divstep schedule being right. The #guards checking that invGcd returns the candidate itself mean a broken schedule would show up in the tests, not just as a silent slowdown.
  • Codec. Native64x4Bytes matches 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 test and lake build CompPolyBench all pass with 0 warnings; axiomsweep --check, lint-style.sh and check-docs-integrity.py pass too.

To resolve in this PR:

  1. Update from main. #389 has landed, so the stack collapses. The only conflict left is CompPoly/Fields/README.md, against #392's Goldilocks rows. Taking #392's three rows resolves it: they no longer mention either Montgomery carrier, so the Mont64x4Field wording change isn't needed there.
  2. ScalarFft. It is the stated reason for keeping the eight-limb stack, but no field instantiates Limbs8 any more, so BN254.Fast.ScalarField values can no longer be passed to it. Is the downstream precompileModules consumer still using it? If it is, I'd port it to Limbs4 here. If not, say so, and (4) covers the cleanup.
  3. 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.
  4. 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).
  5. Bench keys. Rename the fields-mont64x8-*-inv keys to match the carrier (see inline).
  6. 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 in docs/wiki/autoresearch.md.

module

public import CompPoly.Fields.Montgomery.Native64x4Defs
public import CompPoly.Fields.Montgomery.Native64x8InvDefs

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@graikos

graikos commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

I dropped ScalarFft, so the 8-limbed version was removed. I edited the PR text to mention that. Also, I'm a bit confused about item 6, since CONTRIBUTING.md says the table should take numbers from the reference machine (M3), should I leave it to you?

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