Documentation

HexModArith.WordMod

@[inline, extern lean_hex_word_mod_add]

Full-range modular addition of two residues below m.

Equations
Instances For
    @[inline, extern lean_hex_word_mod_sub]

    Full-range modular subtraction of two residues below m.

    Equations
    Instances For
      theorem Hex.toNat_addModWord (m a b : UInt64) (hm : m.toNat UInt64.word) (ha : a.toNat < m.toNat) (hb : b.toNat < m.toNat) :
      (addModWord m a b).toNat = (a.toNat + b.toNat) % m.toNat

      addModWord computes the modular sum.

      theorem Hex.addModWord_lt (m a b : UInt64) (hm : 0 < m.toNat) (hmw : m.toNat UInt64.word) (ha : a.toNat < m.toNat) (hb : b.toNat < m.toNat) :

      addModWord stays below m.

      theorem Hex.toNat_subModWord (m a b : UInt64) (hm : m.toNat UInt64.word) (ha : a.toNat < m.toNat) (hb : b.toNat < m.toNat) :
      (subModWord m a b).toNat = (a.toNat + (m.toNat - b.toNat)) % m.toNat

      subModWord computes the modular difference.

      theorem Hex.subModWord_lt (m a b : UInt64) (hm : 0 < m.toNat) (hmw : m.toNat UInt64.word) (ha : a.toNat < m.toNat) (hb : b.toNat < m.toNat) :
      structure Hex.WordMod {m : UInt64} (ctx : MontCtx m) :

      Residues modulo an odd m < 2^64, stored in Montgomery form. val is the Montgomery representative; the represented residue is ctx.fromMont val.

      The packed native polynomial kernels rely on proof erasure leaving val as the sole runtime constructor field. Any new data-bearing field requires a matching FFI update and native cross-check.

      • val : UInt64

        Montgomery-form representative.

      • isLt : self.val < m

        The representative is reduced.

      Instances For
        theorem Hex.WordMod.ext {m : UInt64} {ctx : MontCtx m} {a b : WordMod ctx} (h : a.val = b.val) :
        a = b
        theorem Hex.WordMod.ext_iff {m : UInt64} {ctx : MontCtx m} {a b : WordMod ctx} :
        a = b a.val = b.val
        @[instance_reducible]
        Equations
        @[inline]
        def Hex.WordMod.toNat {m : UInt64} {ctx : MontCtx m} (a : WordMod ctx) :

        Represented residue as a Nat in [0, m).

        Equations
        Instances For
          theorem Hex.WordMod.val_toNat_lt {m : UInt64} {ctx : MontCtx m} (a : WordMod ctx) :
          theorem Hex.WordMod.toNat_lt {m : UInt64} {ctx : MontCtx m} (a : WordMod ctx) :
          theorem Hex.WordMod.toNat_mul_word {m : UInt64} {ctx : MontCtx m} (a : WordMod ctx) :

          Multiplication by word recovers the Montgomery representative: the key input to the word-cancellation used by the additive specs.

          @[inline]
          def Hex.WordMod.ofNat {m : UInt64} {ctx : MontCtx m} (n : Nat) :

          Reduce a Nat into WordMod ctx.

          Equations
          Instances For
            @[simp]
            theorem Hex.WordMod.toNat_ofNat {m : UInt64} {ctx : MontCtx m} (n : Nat) :
            (ofNat n).toNat = n % m.toNat
            @[instance_reducible]
            instance Hex.WordMod.instZero {m : UInt64} {ctx : MontCtx m} :
            Equations
            @[instance_reducible]
            instance Hex.WordMod.instOne {m : UInt64} {ctx : MontCtx m} :
            One (WordMod ctx)
            Equations
            @[instance_reducible]
            instance Hex.WordMod.instNatCast {m : UInt64} {ctx : MontCtx m} :
            Equations
            @[inline]
            def Hex.WordMod.div {m : UInt64} {ctx : MontCtx m} (a b : WordMod ctx) :

            Division, defined only where it is used: by the leading coefficient of a monic divisor, which is 1. Dividing by 1 is the identity (div_one); other divisors return 0 and are never exercised by monic divMod.

            Equations
            Instances For
              @[instance_reducible]
              instance Hex.WordMod.instDiv {m : UInt64} {ctx : MontCtx m} :
              Div (WordMod ctx)
              Equations
              @[simp]
              theorem Hex.WordMod.div_one {m : UInt64} {ctx : MontCtx m} (a : WordMod ctx) :
              a / 1 = a
              @[simp]
              theorem Hex.WordMod.toNat_zero {m : UInt64} {ctx : MontCtx m} :
              toNat 0 = 0
              @[simp]
              theorem Hex.WordMod.toNat_one {m : UInt64} {ctx : MontCtx m} :
              toNat 1 = 1 % m.toNat
              @[inline]
              def Hex.WordMod.mul {m : UInt64} {ctx : MontCtx m} (a b : WordMod ctx) :

              Montgomery multiplication: one reduction.

              Equations
              Instances For
                @[instance_reducible]
                instance Hex.WordMod.instMul {m : UInt64} {ctx : MontCtx m} :
                Mul (WordMod ctx)
                Equations
                @[simp]
                theorem Hex.WordMod.toNat_mul {m : UInt64} {ctx : MontCtx m} (a b : WordMod ctx) :
                (a * b).toNat = a.toNat * b.toNat % m.toNat
                @[inline]
                def Hex.WordMod.add {m : UInt64} {ctx : MontCtx m} (a b : WordMod ctx) :

                Full-range modular addition.

                Equations
                Instances For
                  @[instance_reducible]
                  instance Hex.WordMod.instAdd {m : UInt64} {ctx : MontCtx m} :
                  Add (WordMod ctx)
                  Equations
                  @[inline]
                  def Hex.WordMod.sub {m : UInt64} {ctx : MontCtx m} (a b : WordMod ctx) :

                  Full-range modular subtraction.

                  Equations
                  Instances For
                    @[instance_reducible]
                    instance Hex.WordMod.instSub {m : UInt64} {ctx : MontCtx m} :
                    Sub (WordMod ctx)
                    Equations
                    @[inline]
                    def Hex.WordMod.neg {m : UInt64} {ctx : MontCtx m} (a : WordMod ctx) :

                    Modular negation, as subtraction from zero.

                    Equations
                    Instances For
                      @[instance_reducible]
                      instance Hex.WordMod.instNeg {m : UInt64} {ctx : MontCtx m} :
                      Neg (WordMod ctx)
                      Equations
                      def Hex.WordMod.pow {m : UInt64} {ctx : MontCtx m} (a : WordMod ctx) :
                      NatWordMod ctx

                      Iterated product, the natural-power operation.

                      Equations
                      Instances For
                        @[instance_reducible]
                        instance Hex.WordMod.instPowNat {m : UInt64} {ctx : MontCtx m} :
                        Equations
                        @[instance_reducible]
                        instance Hex.WordMod.instOfNat {m : UInt64} {ctx : MontCtx m} (n : Nat) :
                        OfNat (WordMod ctx) n
                        Equations
                        @[instance_reducible]
                        instance Hex.WordMod.instSMulNat {m : UInt64} {ctx : MontCtx m} :
                        Equations
                        @[instance_reducible]
                        instance Hex.WordMod.instIntCast {m : UInt64} {ctx : MontCtx m} :
                        Equations
                        @[instance_reducible]
                        instance Hex.WordMod.instSMulInt {m : UInt64} {ctx : MontCtx m} :
                        Equations
                        @[simp]
                        theorem Hex.WordMod.toNat_add {m : UInt64} {ctx : MontCtx m} (a b : WordMod ctx) :
                        (a + b).toNat = (a.toNat + b.toNat) % m.toNat
                        @[simp]
                        theorem Hex.WordMod.toNat_sub {m : UInt64} {ctx : MontCtx m} (a b : WordMod ctx) :
                        (a - b).toNat = (a.toNat + (m.toNat - b.toNat)) % m.toNat
                        @[simp]
                        theorem Hex.WordMod.toNat_neg {m : UInt64} {ctx : MontCtx m} (a : WordMod ctx) :
                        (-a).toNat = (m.toNat - a.toNat) % m.toNat
                        @[simp]
                        theorem Hex.WordMod.toNat_pow {m : UInt64} {ctx : MontCtx m} (a : WordMod ctx) (n : Nat) :
                        (a ^ n).toNat = a.toNat ^ n % m.toNat
                        @[simp]
                        theorem Hex.WordMod.toNat_nsmul {m : UInt64} {ctx : MontCtx m} (n : Nat) (a : WordMod ctx) :
                        (n a).toNat = n % m.toNat * a.toNat % m.toNat
                        @[simp]
                        theorem Hex.WordMod.toNat_neg_mul {m : UInt64} {ctx : MontCtx m} (a b : WordMod ctx) :
                        (-a * b).toNat = (m.toNat - a.toNat * b.toNat % m.toNat) % m.toNat