feat(fields): add fast Mersenne31 arithmetic - #257
Conversation
🤖 PR SummaryOverviewThis PR restructures the Mersenne31 field implementation and adds a fast, Restructuring
Mathematical Formalization
Fast Arithmetic Implementation
No Statistics
Lean Declarations ✏️ Removed: 3 declaration(s)
✏️ Added: 108 declaration(s)
📋 **Additional Analysis**The diff implements a significant refactoring/improvement (replacing the single 📄 **Per-File Summaries**
Last updated: 2026-07-20 08:30 UTC. |
MavenRain
left a comment
There was a problem hiding this comment.
A couple of things:
- Consider adding tests for
Mersenne31.Fast - Mersenne.lean is still referenced in the docs. Consider updating.
- Consider extracting the four
haveblocks duplicated betweenreduceUInt64_castandreduceUInt64Raw_ltinto a private lemma.
dhsorens
left a comment
There was a problem hiding this comment.
Summary
Solid Fast track: Mersenne reduction (not Montgomery) is the right call for 2^31-1, and the toField_* / ringEquiv / inv addition-chain proofs are in good shape.
Blockers before merge: green CI, rebase onto current main, and a #guard test file matching BabyBear/KoalaBear Fast.
Inline notes are the actionable items.
| if hxy : x.val ≥ y.val then | ||
| ⟨x.val - y.val, by | ||
| have hy_le_x : y.val.toNat ≤ x.val.toNat := by | ||
| simpa [UInt32.le_iff_toNat_le] using hxy |
There was a problem hiding this comment.
Build break. CI fails here: simpa leaves hxy : y.val ≤ x.val where the goal wants y.val.toNat ≤ x.val.toNat.
Please rewrite with an explicit UInt32.le_iff_toNat_le (or avoid simpa). This also leaves toField_sub with a sorry warning.
| -/ | ||
|
|
||
| namespace Mersenne31 | ||
| namespace Basic |
There was a problem hiding this comment.
Namespace. Prefer namespace Mersenne31 only (like BabyBear/KoalaBear), so the public type is Mersenne31.Field / Mersenne31.fieldSize, not Mersenne31.Basic.Field.
Old Mersenne.lean used Mersenne31.Field — keep that name.
|
|
||
| /-- The Mersenne31 prime `2^31 - 1` as a native `UInt32`. -/ | ||
| @[inline] | ||
| def modulus : UInt32 := 2147483647 |
There was a problem hiding this comment.
Nit: use hex for native constants, matching siblings (0x7FFFFFFF / 0x7FFFFFFF for modulus64). Decimal 2147483647 is harder to scan as a Mersenne modulus.
| ⟨reduceUInt64Raw x, reduceUInt64Raw_lt x h⟩ | ||
|
|
||
| /-- Bounded `UInt64` reduction preserves the represented canonical field element. -/ | ||
| private theorem reduceUInt64_cast (x : UInt64) |
There was a problem hiding this comment.
Dup: the bound/have blocks here are copy-pasted from reduceUInt64Raw_lt above. Extract a private lemma for the split-sum bounds and reuse it in both places.
| /-! | ||
| # Mersenne31 prime field `2^{31} - 1` | ||
|
|
||
| Facade module for the Mersenne31 field. It re-exports the canonical `ZMod` model |
There was a problem hiding this comment.
Follow-ups (not in this file, but needed for the PR):
- Add
tests/CompPolyTests/Fields/Mersenne31/Fast.leanmirroring Koala/Baby#guardcoverage (zero/one, wrap, ±, mul, pow, inv, int powers). - Update
CompPoly/Fields/README.md(Mersenne.lean→Mersenne31/). - Rebase onto current main (post-feat(fields): add fast KoalaBear/BabyBear arithmetic #258 Montgomery + Lean 4.32).
- Optional: thin deprecated
CompPoly/Fields/Mersenne.leanre-export so old imports keep working.
No description provided.