Multiplies two 64-bit unsigned integers and reduces the result modulo modulus without losing the
high half of the product. When modulus is 0, the result is the wrapped product.
This function is overridden at runtime with an efficient implementation.
Instances For
Raises a 64-bit unsigned integer to a natural-number power modulo modulus. When modulus is 0,
the result is the wrapped power.
Instances For
Returns the multiplicative inverse of a modulo modulus, or none if the inverse does not
exist. When modulus is 0, the result is none. For a nonzero modulus, an inverse exists exactly
when a and modulus are coprime.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Basic properties #
Every operation reduces its result modulo modulus, except that a modulus of 0 means "do not
reduce", which for a 64-bit result is reduction modulo 2 ^ 64. wrapMod names that distinction
so both cases can be handled uniformly.
Correctness of invMod? #
invMod?.go is the extended Euclidean algorithm carrying a single coefficient sequence. Writing
a for the value being inverted, it maintains oldS * a ≡ oldR and s * a ≡ r modulo modulus,
while Nat.gcd oldR r stays fixed. At exit r = 0, so the first component is the gcd and the
second is a Bézout coefficient for it.
invMod? returns exactly the reduced multiplicative inverse. The bound x < modulus is needed:
without it the equation alone would also admit representatives shifted by modulus.
Runtime implementations #
The specification of powMod computes the full natural-number power before reducing it, while
invMod? uses natural-number arithmetic for its Euclidean loop. The definitions below consume
exponents 64 bits at a time and keep the modular arithmetic in machine words. They are installed
with @[csimp], so the compiled behaviour is proved equal to the definitions above.
Multiplies result by base ^ exponent modulo modulus, consuming the bits of the word-sized
exponent from the least significant end. result is returned unchanged when exponent is 0,
so the accumulator passed in must already be reduced modulo modulus.
Equations
- One or more equations did not get rendered due to their size.
Instances For
Performs steps squaring steps, consuming that many bits of exponent. Returns the repeatedly
squared base alongside the accumulated result. Neither is reduced modulo modulus unless a step
runs, so steps = 0 returns both arguments unchanged and the accumulator passed in must already
be reduced.
Equations
Instances For
Efficient implementation of UInt64.powMod. A Nat exponent is consumed 64 bits at a time, so
arbitrarily large exponents only pay for one Nat division per word rather than per bit.
Equations
- base.powModImpl exponent modulus = if modulus = 1 then 0 else UInt64.powModImpl.go modulus base exponent 1
Instances For
Efficient implementation of UInt64.invMod?, using a word-sized extended Euclidean loop.
Equations
- a.invModImpl modulus = if modulus = 0 then none else UInt64.invModImpl.go modulus (a % modulus) modulus (1 % modulus) 0
Instances For
Equations
- One or more equations did not get rendered due to their size.