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
Nat-level modular exponentiation by repeated squaring, with the same
zero-modulus convention as powMod.
This is the kernel-facing specification of modular exponentiation: it and its
recursion are @[expose], so proof terms that replay it (decide-style
certificate checkers) reduce in the kernel. powMod is its runtime twin via
powModNat_eq_powMod, so compiled callers of powModNat still take the
Montgomery path for odd word-sized moduli.
Equations
- HexArith.powModNat a n p = if p = 0 then 0 else HexArith.powModNatGo n p (HexArith.bitLength n) 0 (1 % p) (a % p)
Instances For
powModNat_eq: for a positive modulus, powModNat 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.
Modular exponentiation modulo one returns zero.
Swap-oriented companion of powMod_mul_exp.