Compute R^2 mod p by repeated doubling in native-word arithmetic.
Equations
- MontCtx.r2Loop p 0 x✝ = x✝
- MontCtx.r2Loop p n.succ x✝ = MontCtx.r2Loop p n (MontCtx.doubleMod✝ p x✝)
Instances For
The R^2 mod p constant used to enter Montgomery form.
Equations
- MontCtx.r2OfModulus p = if p ≤ 1 then 0 else have rModP := MontCtx.r2Loop p 64 1; UInt64.ofNat (rModP.toNat * rModP.toNat % p.toNat)
Instances For
The executable r2OfModulus computes R^2 mod p for positive moduli.
Build the executable Montgomery context for an odd UInt64 modulus.
Equations
- MontCtx.mk p hp = { p_odd := hp, p' := montInv p, p'_eq := ⋯, r2 := MontCtx.r2OfModulus p, r2_eq := ⋯ }
Instances For
The oddness witness stored by MontCtx.mk.
The Montgomery inverse word stored by MontCtx.mk.
The inverse-word correctness fact specialized to MontCtx.mk.
The R^2 mod p word stored by MontCtx.mk.
The R^2 mod p correctness fact specialized to MontCtx.mk.
Every UInt64 modulus is below the Montgomery radix R = 2^64.
The Nat-level oddness fact specialized to MontCtx.mk.
Positivity of a modulus equipped with MontCtx.mk.
The radix bound for a modulus equipped with MontCtx.mk.
Convert a Montgomery residue back to the standard representation.
Equations
- ctx.fromMont a = montgomeryReduce ctx 0 a
Instances For
Multiplication by word is injective on residues modulo p: since p is odd
it is coprime to word = 2 ^ 64, so two reduced values x, y < p with
x * word ≡ y * word (mod p) are equal. This is what lets the
representative-mod-word characterisation pin down a unique mulMont value.
Public because any word-modular residue layer built on this context (for example a Montgomery-form residue ring) needs the same cancellation to prove that its additive operations preserve the represented value.
Exponentiate a Montgomery-form base by repeated squaring.
Equations
- HexArith.powMont ctx base n = HexArith.powMontBitsGo✝ ctx n (HexArith.bitLength n) 0 (ctx.toMont (UInt64.ofNat (1 % p.toNat))) base
Instances For
Word-sized odd-modulus modular exponentiation via Montgomery arithmetic.
Equations
- HexArith.powModWordOdd a n p hp = ((MontCtx.mk p hp).fromMont (HexArith.powMont (MontCtx.mk p hp) ((MontCtx.mk p hp).toMont (UInt64.ofNat (a % p.toNat))) n)).toNat
Instances For
Tail-recursive Nat fallback for modular exponentiation.
Equations
Instances For
Compiled Nat fallback, reducing after every multiplication.
Equations
- HexArith.powModBits a n p = if p = 0 then 0 else HexArith.powModNatGo n p (HexArith.bitLength n) 0 (1 % p) (a % p)
Instances For
powModBits modulo zero returns zero, matching powMod.
powModBits_eq: for a positive modulus, powModBits a n p computes a ^ n % p.
Modular exponentiation by repeated squaring, using Montgomery arithmetic for
odd UInt64 moduli and a direct Nat fallback otherwise.
This is the runtime twin of powModNat, the kernel-facing specification;
powModNat_eq_powMod is the @[csimp] equality relating them.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Modular exponentiation modulo zero returns zero.
Kernel reduction loop for powModNat, with an accumulator and decreasing fuel.
go m fuel b e acc computes (b ^ e * acc) % m when e < fuel.
Nat.rec avoids well-founded recursion, and Bool.rec avoids Decidable unfolding.
Adapted from Bhavik Mehta's powModK in PrimeCert; see the license notices above.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Kernel-facing modular exponentiation. Six-bit windows through 2^64,
four-bit windows through 2^512, three-bit windows through 2^1024, and
two-bit windows through 2^2048 reduce kernel recursion. Above that,
a reduced base below 2^64 uses two-bit windows through 2^4096, then
one-bit windows; other inputs use the binary
accumulator. Modulus zero returns zero. Compiled evaluation uses powMod,
with their equality proved below.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Modular exponentiation modulo zero returns zero.
Modular exponentiation modulo one returns zero.
Swap-oriented companion of powMod_mul_exp.