Documentation

CompPoly.Fields.Montgomery.Native64x8Defs

Eight-limb Montgomery arithmetic: runtime definitions (zero-import) #

The runtime definitions of the eight-limb Montgomery arithmetic, split out of CompPoly.Fields.Montgomery.Native64x8 verbatim. All correctness statements about them live in that sibling module, which imports this one.

This module deliberately has zero imports: downstream consumers put it into precompileModules native-compilation lanes, and precompileModules compiles the entire import closure — so the runtime definitions must not pull in mathlib.

Word helpers #

@[inline]

Mask selecting the low 32 bits of a UInt64.

Instances For
    @[inline]

    Low limb of add-with-carry: (x + y + c) mod 2 ^ 32.

    Instances For
      @[inline]

      Carry-out of add-with-carry: (x + y + c) / 2 ^ 32.

      Instances For
        @[inline]

        Low limb of subtract-with-borrow: (x - y - b) mod 2 ^ 32.

        Instances For
          @[inline]

          Borrow-out of subtract-with-borrow, read off the sign bit of the 64-bit difference.

          Instances For
            @[inline]

            Low limb of multiply-accumulate: (t + x * y + c) mod 2 ^ 32.

            Instances For
              @[inline]

              High word of multiply-accumulate: (t + x * y + c) / 2 ^ 32.

              Instances For
                @[inline]

                The Montgomery multiplier of a limb: (s * negInv) mod 2 ^ 32.

                Instances For

                  Eight-limb values #

                  A 256-bit value as eight little-endian 32-bit limbs, each stored in a UInt64.

                  • l0 : UInt64

                    Limb of weight 2 ^ 0.

                  • l1 : UInt64

                    Limb of weight 2 ^ 32.

                  • l2 : UInt64

                    Limb of weight 2 ^ 64.

                  • l3 : UInt64

                    Limb of weight 2 ^ 96.

                  • l4 : UInt64

                    Limb of weight 2 ^ 128.

                  • l5 : UInt64

                    Limb of weight 2 ^ 160.

                  • l6 : UInt64

                    Limb of weight 2 ^ 192.

                  • l7 : UInt64

                    Limb of weight 2 ^ 224.

                  Instances For
                    Instances For

                      The zero value.

                      Instances For

                        The value one.

                        Instances For
                          @[inline]

                          Split a natural number into eight 32-bit limbs, discarding bits above 2 ^ 256.

                          Instances For

                            The natural number represented by the limbs: ∑ lᵢ * 2 ^ (32 * i).

                            Instances For

                              Every limb holds at most 32 significant bits.

                              Instances For

                                Limbwise addition and subtraction #

                                @[inline]

                                Limbwise add-with-carry, discarding the carry out of the top limb.

                                Instances For
                                  @[inline]

                                  Limbwise subtract-with-borrow.

                                  Instances For
                                    @[inline]

                                    Borrow out of the top limb of subLimbs.

                                    Instances For

                                      Conditional subtraction and field operations #

                                      @[inline]

                                      Subtract the modulus once if the value is at least the modulus. The borrow chain decides the branch, so no comparison is needed.

                                      Instances For
                                        @[inline]

                                        Modular addition.

                                        Instances For
                                          @[inline]

                                          Modular subtraction: on a borrow, the modulus is added back.

                                          Instances For
                                            @[inline]

                                            Modular negation.

                                            Instances For

                                              CIOS multiplication #

                                              The CIOS accumulator: eight limbs plus one head limb.

                                              • t0 : UInt64

                                                Limb of weight 2 ^ 0.

                                              • t1 : UInt64

                                                Limb of weight 2 ^ 32.

                                              • t2 : UInt64

                                                Limb of weight 2 ^ 64.

                                              • t3 : UInt64

                                                Limb of weight 2 ^ 96.

                                              • t4 : UInt64

                                                Limb of weight 2 ^ 128.

                                              • t5 : UInt64

                                                Limb of weight 2 ^ 160.

                                              • t6 : UInt64

                                                Limb of weight 2 ^ 192.

                                              • t7 : UInt64

                                                Limb of weight 2 ^ 224.

                                              • t8 : UInt64

                                                Head limb of weight 2 ^ 256.

                                              Instances For
                                                Instances For
                                                  @[inline]

                                                  The zero accumulator.

                                                  Instances For
                                                    @[inline]

                                                    The eight low limbs of the accumulator.

                                                    Instances For

                                                      The natural number represented by the accumulator.

                                                      Instances For

                                                        Every limb of the accumulator holds at most 32 significant bits.

                                                        Instances For
                                                          @[inline]

                                                          The multiply half of a CIOS round: accumulate a * bi into the accumulator. The carry out of the top limb is kept in the head limb, so no information is lost.

                                                          Instances For
                                                            @[inline]

                                                            The reduce half of a CIOS round: add the multiple montM s.t0 negInv of the modulus that cancels the low limb, then drop that limb. negInv has to be -q⁻¹ mod 2 ^ 32.

                                                            Instances For
                                                              @[inline]
                                                              def Montgomery.Native64x8.mulRound (q : Limbs8) (negInv : UInt64) (a : Limbs8) (bi : UInt64) (t : State9) :

                                                              One CIOS outer round: accumulate a * bi into the accumulator, then reduce away one limb.

                                                              Instances For
                                                                @[inline]
                                                                def Montgomery.Native64x8.mul (q : Limbs8) (negInv : UInt64) (a b : Limbs8) :

                                                                CIOS Montgomery multiplication: eight rounds followed by one conditional subtraction.

                                                                Instances For
                                                                  @[inline]

                                                                  Montgomery squaring.

                                                                  Instances For