Documentation

Init.Data.UInt.Modular

@[implicit_reducible, extern lean_uint64_mul_mod]
def UInt64.mulMod (a b modulus : UInt64) :

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.

Equations
Instances For
    noncomputable def UInt64.powMod (base : UInt64) (exponent : Nat) (modulus : UInt64) :

    Raises a 64-bit unsigned integer to a natural-number power modulo modulus. When modulus is 0, the result is the wrapped power.

    Equations
    Instances For
      noncomputable def UInt64.invMod? (a modulus : UInt64) :

      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
        @[irreducible]
        noncomputable def UInt64.invMod?.go (modulus : UInt64) (oldR r oldS s : Nat) :
        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.

          @[simp]
          theorem UInt64.toNat_mulMod {a b modulus : UInt64} (h : modulus ≠ 0) :
          (a.mulMod b modulus).toNat = a.toNat * b.toNat % modulus.toNat
          theorem UInt64.mulMod_lt {a b modulus : UInt64} (h : modulus ≠ 0) :
          a.mulMod b modulus < modulus
          @[simp]
          theorem UInt64.toNat_mulMod_zero {a b : UInt64} :
          (a.mulMod b 0).toNat = a.toNat * b.toNat % 2 ^ 64
          @[simp]
          theorem UInt64.toNat_powMod {base : UInt64} {e : Nat} {modulus : UInt64} (h : modulus ≠ 0) :
          (base.powMod e modulus).toNat = base.toNat ^ e % modulus.toNat
          @[simp]
          theorem UInt64.toNat_powMod_zero {base : UInt64} {e : Nat} :
          (base.powMod e 0).toNat = base.toNat ^ e % 2 ^ 64
          theorem UInt64.powMod_lt {base : UInt64} {e : Nat} {modulus : UInt64} (h : modulus ≠ 0) :
          base.powMod e modulus < modulus

          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.

          @[simp]
          theorem UInt64.isSome_invMod? {a modulus : UInt64} (hm : modulus ≠ 0) :
          (a.invMod? modulus).isSome = true ↔ a.toNat.gcd modulus.toNat = 1
          theorem UInt64.lt_of_invMod?_eq_some {a modulus x : UInt64} (h : a.invMod? modulus = some x) :
          x < modulus
          theorem UInt64.mulMod_of_invMod?_eq_some {a modulus x : UInt64} (h : a.invMod? modulus = some x) :
          a.mulMod x modulus = 1 % modulus

          The value returned by invMod? really is a multiplicative inverse. Note that for modulus = 1 the right-hand side is 0, matching the convention that everything is invertible modulo one.

          theorem UInt64.invMod?_eq_some_iff {a modulus x : UInt64} :
          a.invMod? modulus = some x ↔ x < modulus ∧ a.mulMod x modulus = 1 % modulus

          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.

          @[irreducible]
          def UInt64.powModWord (base exponent modulus result : UInt64) :

          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
            def UInt64.powModChunk (modulus : UInt64) :

            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
              def UInt64.powModImpl (base : UInt64) (exponent : Nat) (modulus : UInt64) :

              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
              Instances For
                @[irreducible]
                def UInt64.powModImpl.go (modulus base : UInt64) (exponent : Nat) (result : UInt64) :
                Equations
                • One or more equations did not get rendered due to their size.
                Instances For

                  Efficient implementation of UInt64.invMod?, using a word-sized extended Euclidean loop.

                  Equations
                  Instances For
                    @[irreducible]
                    def UInt64.invModImpl.go (modulus oldR r oldS s : UInt64) :
                    Equations
                    • One or more equations did not get rendered due to their size.
                    Instances For

                      Correctness of the runtime implementations #