Skip to content

feat(bz): word-sized Montgomery residue ring and CLD kernel#8697

Open
kim-em wants to merge 13 commits into
mainfrom
issue-8691
Open

feat(bz): word-sized Montgomery residue ring and CLD kernel#8697
kim-em wants to merge 13 commits into
mainfrom
issue-8691

Conversation

@kim-em

@kim-em kim-em commented Jul 9, 2026

Copy link
Copy Markdown
Owner

This PR adds the word-sized Montgomery arithmetic layer for the Berlekamp-Zassenhaus lattice tier, issue #8691 "perf(bz): word-sized modulus arithmetic for the lattice tier's lift and CLD stages", in two proven-and-tested steps.

Hex.WordMod ctx is a residue ring modulo an odd m < 2^64 stored in Montgomery form on top of HexArith.MontCtx. Multiplication is a single mulMont reduction; addition and subtraction are full-range modular operations built from UInt64.addCarry/subBorrow, so the whole odd-m < 2^64 range is supported (unlike ZMod64, which caps at 2^31). Every operation carries a proven toNat characterisation modulo m (toNat_mul from MontCtx.mulMont_repr; toNat_add/toNat_sub/toNat_neg via a word-cancellation lemma, MontCtx.cancel_word_mod_of_lt, which this PR exposes from the Montgomery context). No sorry, no axiom.

Hex.cldQuotientModWord? then computes the CLD quotient (f · g') / g mod p^a for a monic g over WordMod whenever the guard Odd (p^a) ∧ p^a < 2^64 holds, declining (returning none) otherwise; powLtWord? checks p^a < 2^64 with early exit so a huge exponent never materialises a bignum. Conformance #guards pin it byte-identical to the bignum cldQuotientMod on the SD3 and √2+√3 cores at moduli 7^5, 11^13, 5^9, and confirm the guard declines on 11^400.

Before building this I measured the premise: at the actual SD4 recovery point the modulus is 11^13 (about 45 bits, not near 2^64 as the issue framed it, because #8395 early-termination stops the doubling schedule short), and a native-Montgomery prototype of cldQuotientMod there is byte-identical to the current Int/DensePoly path and 1.8x faster (the ~90-bit intermediate products in the Int path heap-allocate as GMP; Montgomery keeps them in words). Full measurement and the @[csimp] routing plan are on the issue.

Follow-up (its own PR): prove cldQuotientMod = cldQuotientModWord?-under-guard in the *Mathlib layer (via the DensePoly.toPolynomial ring-equiv, a WordMod →+* ZMod (p^a) hom, and monic-division uniqueness), then wire the fast path with @[csimp] and re-measure. The runtime win lands there; this PR is the proven, conformance-checked foundation. The kernel is deliberately in its own module so the eventual @[csimp] in Lattice.lean imports it without a cycle.

🤖 Prepared with Claude Code

Kim Morrison and others added 3 commits July 9, 2026 04:11
Measured the SD4 recovery modulus (11^13, ~45 bits, not near 2^64) and a
1.8x byte-identical native-Montgomery cldQuotientMod prototype; posted
findings + re-scope recommendation on the issue rather than landing a
proof-only WordPolyMod layer as dead code.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…< 2^64

Add `Hex.WordMod ctx`, residues modulo an odd `m < 2^64` stored in
Montgomery form on top of `HexArith.MontCtx`. Multiplication is a single
`mulMont` reduction; addition and subtraction are full-range modular
operations built from `UInt64.addCarry`/`subBorrow`, so the whole odd
`m < 2^64` range is supported (unlike `ZMod64`, capped at 2^31). Every
operation has a proven `toNat` characterisation modulo m; the additive specs
use `MontCtx.cancel_word_mod_of_lt`, now exposed from the Montgomery context.

This is the scalar layer (Phase 1) for the word-sized poly-mod-p^a arithmetic
of the Berlekamp-Zassenhaus lattice tier (#8691), measured byte-identical and
1.8x faster than the Int/bignum path at the SD4 recovery modulus.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…ientMod

Add `Hex.cldQuotientModWord?`, the CLD quotient `(f · g') / g mod p^a` for a
monic `g`, computed over `WordMod` (single-reduction Montgomery poly
arithmetic) whenever the guard `Odd (p^a) ∧ p^a < 2^64` holds and declining
otherwise. `powLtWord?` checks `p^a < 2^64` with early exit so a huge exponent
never materialises a bignum. Give `WordMod` the `NatCast` and (monic-only)
`Div` instances the dense-poly `derivative`/`divMod` need.

Conformance `#guard`s in HexBerlekampZassenhaus.Conformance pin the kernel
byte-identical to the bignum `cldQuotientMod` (SD3/√2+√3 cores, moduli 7^5,
11^13, 5^9) and confirm the guard declines on 11^400. The correspondence proof
and `@[csimp]` wiring are the next phases (#8691).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@kim-em kim-em changed the title feat(modarith): WordMod, a proven Montgomery residue ring over odd m < 2^64 feat(bz): word-sized Montgomery residue ring and CLD kernel Jul 9, 2026
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Kim Morrison and others added 9 commits July 9, 2026 05:25
Add `HexModArithMathlib.WordMod.toZMod : Hex.WordMod ctx → ZMod m.toNat` with
its injectivity and the homomorphism laws (`toZMod_zero/one/add/mul/neg/sub`),
mirroring the existing `ZMod64` bridge. This is the Phase-2 foundation for the
byte-identical CLD correspondence (#8691): the next steps bundle a Mathlib
`CommRing (WordMod ctx)` and `≃+* ZMod` through this map, then transport the
word-sized and bignum CLD quotients into `Polynomial (ZMod (p^a))` to prove them
equal under the guard.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Give the executable `WordMod ctx` a Mathlib `CommRing` structure, pulled back
along the injective `toZMod` via `Function.Injective.commRing` (adding the
`SMul ℕ/ℤ`, `Pow ℕ`, `IntCast` data with immediate transport proofs), and
package `toZMod` as `toZModRingHom : WordMod ctx →+* ZMod m.toNat`. This is the
algebraic backbone for the Phase-2 CLD correspondence (#8691): the remaining
step transports both quotients through `DensePoly.toPolynomial` + `Polynomial.map`
into `Polynomial (ZMod (p^a))` and proves them equal via monic-division
uniqueness.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…roof

Add the coefficient casts `cZ`/`cW` from executable integer / `WordMod`
polynomials into `ZMod (p^a)[X]` with their homomorphism laws, the
canonical-representative uniqueness lemma `eq_of_cZ_eq`, and the central
`map_divMod_monic`: monic dense-polynomial division commutes with any
coefficient ring hom (via `Polynomial.div_modByMonic_unique`). These are the
reusable pieces the byte-identical `cldQuotientMod = cldQuotientModWord?`
equality (#8691, Phase 2) assembles from.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Add the intCast-of-intModNat cast lemma, the cW-of-toW-mapped = cZ bridge, the
reduceModPow-vanishing lemma, and derivative-commute for both casts. With the
transport lemma these reduce the byte-identical CLD equality (#8691) to
numerator/divisor agreement in ZMod (p^a)[X] plus monic-division uniqueness.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Refactor powLtWord?'s inner loop into a named @[expose] powLtWordAux, prove it
computes p^a and stays below 2^64 (powLtWord?_eq), and add toPoly_degree_lt
transporting the dense remainder-degree bound into Polynomial.degree. These are
the last supporting lemmas before assembling the cldQuotientMod equality (#8691).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…embly

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

1 participant