Documentation

HexModArith.HotLoop

structure Hex.BarrettCtx (p : Nat) [ZMod64.Bounds p] :

Barrett reduction context specialized to a Hex.ZMod64 modulus.

The stored UInt64 modulus must agree with the Nat-indexed Hex.ZMod64 modulus; the underlying HexArith context then provides the small-modulus fast path.

Instances For
    structure Hex.MontCtx (p : Nat) [ZMod64.Bounds p] :

    Montgomery reduction context specialized to a ZMod64 modulus.

    As with Hex.BarrettCtx, the executable context stores the machine-word modulus used by the underlying HexArith Montgomery code.

    • modulus : UInt64

      The executable machine-word modulus used by the underlying context.

    • modulus_eq : self.modulus.toNat = p

      The stored word modulus agrees with the Nat-indexed ZMod64 modulus.

    • toUInt64Ctx : _root_.MontCtx self.modulus

      The underlying UInt64 Montgomery context from HexArith.

    Instances For
      structure Hex.MontResidue (p : Nat) [ZMod64.Bounds p] :

      Temporary Montgomery-form residue for hot loops.

      This is intentionally distinct from Hex.ZMod64: values are still reduced into [0, p), but they represent residues in Montgomery form rather than the canonical standard representative.

      • val : UInt64

        Backing word for the Montgomery-form representative.

      • isLt : self.val.toNat < p

        The backing word remains reduced modulo p.

      Instances For

        View a Montgomery-form residue as its backing machine word.

        Equations
        Instances For

          View a Montgomery-form residue as its reduced Nat representative.

          Equations
          Instances For
            @[simp]

            The UInt64 view of a Montgomery residue is its backing word.

            @[simp]

            The Nat view of a Montgomery residue is the Nat value of its backing word.

            @[simp]

            The Nat view of a Montgomery residue is reduced modulo its indexed modulus.

            theorem Hex.MontResidue.ext {p : Nat} [ZMod64.Bounds p] {a b : MontResidue p} (h : a.val = b.val) :
            a = b

            Montgomery residues are equal when their backing words are equal.

            theorem Hex.MontResidue.ext_iff {p : Nat} [ZMod64.Bounds p] {a b : MontResidue p} :
            a = b a.val = b.val
            theorem Hex.MontResidue.ext_toNat {p : Nat} [ZMod64.Bounds p] {a b : MontResidue p} (h : a.toNat = b.toNat) :
            a = b

            Extensionality for Montgomery residues via their canonical Nat representatives.

            Two Montgomery residues are equal exactly when their canonical representatives agree.

            def Hex.BarrettCtx.ofModulus {p : Nat} [ZMod64.Bounds p] (hp : 1 < p) (hlt : p < 2 ^ 32) :

            Build a Barrett hot-loop context from the indexed small modulus.

            Equations
            Instances For
              @[simp]
              theorem Hex.BarrettCtx.ofModulus_modulus {p : Nat} [ZMod64.Bounds p] (hp : 1 < p) (hlt : p < 2 ^ 32) :

              The smart constructor stores the indexed modulus as a machine word.

              @[simp]
              theorem Hex.BarrettCtx.ofModulus_modulus_eq {p : Nat} [ZMod64.Bounds p] (hp : 1 < p) (hlt : p < 2 ^ 32) :

              The smart constructor's stored word modulus agrees with the indexed modulus.

              theorem Hex.BarrettCtx.ofModulus_toUInt64Ctx {p : Nat} [ZMod64.Bounds p] (hp : 1 < p) (hlt : p < 2 ^ 32) :

              The smart constructor delegates to the underlying UInt64 Barrett context.

              @[simp]

              The smart constructor's underlying Barrett context stores the reciprocal for the indexed modulus.

              def Hex.BarrettCtx.mulMod {p : Nat} [ZMod64.Bounds p] (ctx : BarrettCtx p) (a b : ZMod64 p) :

              Multiply two standard residues using the Barrett context and repackage the result as a Hex.ZMod64.

              Equations
              Instances For
                @[simp]
                theorem Hex.BarrettCtx.toNat_mulMod {p : Nat} [ZMod64.Bounds p] (ctx : BarrettCtx p) (a b : ZMod64 p) :
                (ctx.mulMod a b).toNat = a.toNat * b.toNat % p

                The Hex.ZMod64 Barrett wrapper computes the ordinary modular product on reduced representatives.

                @[simp]
                theorem Hex.BarrettCtx.mulMod_eq_mul {p : Nat} [ZMod64.Bounds p] (ctx : BarrettCtx p) (a b : ZMod64 p) :
                ctx.mulMod a b = a * b

                Barrett hot-loop multiplication agrees with the ordinary ZMod64 multiplication surface.

                @[simp]
                theorem Hex.BarrettCtx.mulMod_one_left {p : Nat} [ZMod64.Bounds p] (ctx : BarrettCtx p) (a : ZMod64 p) :
                ctx.mulMod 1 a = a

                Barrett hot-loop multiplication by one on the left returns the original standard residue.

                @[simp]
                theorem Hex.BarrettCtx.mulMod_zero_left {p : Nat} [ZMod64.Bounds p] (ctx : BarrettCtx p) (a : ZMod64 p) :
                ctx.mulMod 0 a = 0

                Barrett hot-loop multiplication by zero on the left returns zero.

                @[simp]
                theorem Hex.BarrettCtx.mulMod_one_right {p : Nat} [ZMod64.Bounds p] (ctx : BarrettCtx p) (a : ZMod64 p) :
                ctx.mulMod a 1 = a

                Barrett hot-loop multiplication by one on the right returns the original standard residue.

                @[simp]
                theorem Hex.BarrettCtx.mulMod_zero_right {p : Nat} [ZMod64.Bounds p] (ctx : BarrettCtx p) (a : ZMod64 p) :
                ctx.mulMod a 0 = 0

                Barrett hot-loop multiplication by zero on the right returns zero.

                theorem Hex.BarrettCtx.mulMod_comm {p : Nat} [ZMod64.Bounds p] (ctx : BarrettCtx p) (a b : ZMod64 p) :
                ctx.mulMod a b = ctx.mulMod b a

                Barrett hot-loop multiplication is commutative on standard residues.

                theorem Hex.BarrettCtx.mulMod_assoc {p : Nat} [ZMod64.Bounds p] (ctx : BarrettCtx p) (a b c : ZMod64 p) :
                ctx.mulMod (ctx.mulMod a b) c = ctx.mulMod a (ctx.mulMod b c)

                Barrett hot-loop multiplication is associative on standard residues.

                Build a Montgomery hot-loop context from the indexed modulus and the word-level odd-modulus side condition required by the underlying HexArith context.

                Equations
                Instances For
                  @[simp]

                  The smart constructor stores the canonical machine-word modulus.

                  @[simp]

                  The smart constructor's stored word modulus agrees with the indexed modulus.

                  @[simp]

                  The smart constructor delegates to the underlying UInt64 Montgomery context.

                  def Hex.MontCtx.toMont {p : Nat} [ZMod64.Bounds p] (ctx : MontCtx p) (a : ZMod64 p) :

                  Convert a standard residue into Montgomery form.

                  Equations
                  Instances For

                    Multiply two Montgomery-form residues, staying inside the Montgomery domain.

                    Equations
                    Instances For
                      def Hex.MontCtx.fromMont {p : Nat} [ZMod64.Bounds p] (ctx : MontCtx p) (a : MontResidue p) :

                      Convert a Montgomery-form loop temporary back to the standard Hex.ZMod64 representation.

                      Equations
                      Instances For

                        toMont delegates to the underlying UInt64 Montgomery conversion.

                        mulMont delegates to the underlying UInt64 Montgomery multiplication.

                        fromMont exposes the reduced Nat value computed by the underlying context.

                        @[simp]
                        theorem Hex.MontCtx.toNat_toMont {p : Nat} [ZMod64.Bounds p] (ctx : MontCtx p) (a : ZMod64 p) :

                        The Nat value of toMont is multiplication by the Montgomery radix.

                        fromMont removes one Montgomery radix factor from a Montgomery-form loop temporary.

                        @[simp]
                        theorem Hex.MontCtx.fromMont_toMont {p : Nat} [ZMod64.Bounds p] (ctx : MontCtx p) (a : ZMod64 p) :
                        ctx.fromMont (ctx.toMont a) = a

                        Converting a standard residue into Montgomery form and back is the identity.

                        @[simp]
                        theorem Hex.MontCtx.toNat_mulMont {p : Nat} [ZMod64.Bounds p] (ctx : MontCtx p) (a b : ZMod64 p) :
                        (ctx.fromMont (ctx.mulMont (ctx.toMont a) (ctx.toMont b))).toNat = a.toNat * b.toNat % p

                        Multiplying two standard residues by entering Montgomery form, multiplying, and leaving Montgomery form computes the ordinary modular product.

                        @[simp]
                        theorem Hex.MontCtx.mulMont_repr {p : Nat} [ZMod64.Bounds p] (ctx : MontCtx p) (a b : MontResidue p) :
                        (ctx.fromMont (ctx.mulMont a b)).toNat = (ctx.fromMont a).toNat * (ctx.fromMont b).toNat % p

                        Montgomery multiplication preserves the represented standard-residue product when converted back out of Montgomery form.

                        @[simp]
                        theorem Hex.MontCtx.fromMont_mulMont_toMont {p : Nat} [ZMod64.Bounds p] (ctx : MontCtx p) (a b : ZMod64 p) :
                        ctx.fromMont (ctx.mulMont (ctx.toMont a) (ctx.toMont b)) = a * b

                        Multiplying two standard residues by entering Montgomery form, multiplying, and leaving Montgomery form agrees with ordinary Hex.ZMod64 multiplication.

                        @[simp]
                        theorem Hex.MontCtx.fromMont_mulMont_toMont_one_left {p : Nat} [ZMod64.Bounds p] (ctx : MontCtx p) (a : ZMod64 p) :
                        ctx.fromMont (ctx.mulMont (ctx.toMont 1) (ctx.toMont a)) = a

                        The Montgomery round trip for a wrapped product returns the original residue when the left standard input is one.

                        @[simp]
                        theorem Hex.MontCtx.fromMont_mulMont_toMont_zero_left {p : Nat} [ZMod64.Bounds p] (ctx : MontCtx p) (a : ZMod64 p) :
                        ctx.fromMont (ctx.mulMont (ctx.toMont 0) (ctx.toMont a)) = 0

                        The Montgomery round trip for a wrapped product returns zero when the left standard input is zero.

                        @[simp]
                        theorem Hex.MontCtx.fromMont_mulMont_toMont_one_right {p : Nat} [ZMod64.Bounds p] (ctx : MontCtx p) (a : ZMod64 p) :
                        ctx.fromMont (ctx.mulMont (ctx.toMont a) (ctx.toMont 1)) = a

                        The Montgomery round trip for a wrapped product returns the original residue when the right standard input is one.

                        @[simp]
                        theorem Hex.MontCtx.fromMont_mulMont_toMont_zero_right {p : Nat} [ZMod64.Bounds p] (ctx : MontCtx p) (a : ZMod64 p) :
                        ctx.fromMont (ctx.mulMont (ctx.toMont a) (ctx.toMont 0)) = 0

                        The Montgomery round trip for a wrapped product returns zero when the right standard input is zero.

                        theorem Hex.MontCtx.fromMont_mulMont_toMont_comm {p : Nat} [ZMod64.Bounds p] (ctx : MontCtx p) (a b : ZMod64 p) :
                        ctx.fromMont (ctx.mulMont (ctx.toMont a) (ctx.toMont b)) = ctx.fromMont (ctx.mulMont (ctx.toMont b) (ctx.toMont a))

                        The Montgomery round trip for a wrapped product is commutative on standard residue inputs.

                        theorem Hex.MontCtx.fromMont_mulMont_toMont_assoc {p : Nat} [ZMod64.Bounds p] (ctx : MontCtx p) (a b c : ZMod64 p) :
                        ctx.fromMont (ctx.mulMont (ctx.toMont (ctx.fromMont (ctx.mulMont (ctx.toMont a) (ctx.toMont b)))) (ctx.toMont c)) = ctx.fromMont (ctx.mulMont (ctx.toMont a) (ctx.toMont (ctx.fromMont (ctx.mulMont (ctx.toMont b) (ctx.toMont c)))))

                        The Montgomery round trip for wrapped products is associative on standard residue inputs.