Documentation

UniversalHashing.BinConvolution.ConvolutionDefs

Definitions for GF(2) circular convolution via NTT #

The transform below uses the standard toolkit for number-theoretic transforms: a 2³⁰-smooth prime modulus, Montgomery arithmetic for modular multiplication, and a radix-4 decimation. See [Har14] for a general treatment of fast NTT arithmetic.

The NTT-friendly prime modulus p = 3·2³⁰ + 1 = 3221225473, as a UInt64. Its 2³⁰-smooth factor p - 1 = 3·2³⁰ provides power-of-two roots of unity for the transform.

Equations
Instances For

    The same prime modulus p = 3·2³⁰ + 1 = 3221225473 as mod64, but as a UInt32.

    Equations
    Instances For

      5 is a primitive root of (ZMod p)ˣ, used to generate the NTT roots of unity.

      Equations
      Instances For

        Montgomery negated inverse p' = -p⁻¹ mod 2³² (= 3221225471), used by montMul.

        Equations
        Instances For

          Montgomery R² mod p for radix R = 2³² (= 1789569709). montMul a montR2 puts a into Montgomery form.

          Equations
          Instances For

            Montgomery R mod p = 2³² mod p (= 1073741823); the Montgomery representation of 1.

            Equations
            Instances For
              @[inline]
              def addMod32 (a b : UInt32) :
              Equations
              Instances For
                @[inline]
                def subMod32 (a b : UInt32) :
                Equations
                Instances For
                  @[inline]
                  def montMul (a b : UInt32) :
                  Equations
                  • One or more equations did not get rendered due to their size.
                  Instances For
                    @[inline]
                    def toMont (a : UInt32) :
                    Equations
                    Instances For
                      def powModAuxU64 (mod_ : UInt64) :
                      UInt64UInt64UInt64UInt64

                      Fuel-based square-and-multiply accumulator for modular exponentiation: with fuel rounds left it folds the remaining exponent bits e into the running result r, squaring base b each round. Called by powModU64 with fuel = 64, enough for any UInt64 exponent.

                      Equations
                      Instances For
                        def powModU64 (base exp mod_ : UInt64) :

                        base ^ exp mod mod_, computed by square-and-multiply via powModAuxU64.

                        Equations
                        Instances For
                          def montPow (seed wm : UInt32) :

                          Iterated Montgomery multiplication: apply montMul · wm exactly j times to seed.

                          Equations
                          Instances For
                            def rootsInner (wm : UInt32) (halfLen : ) {n : } (k i : ) :
                            Equations
                            • One or more equations did not get rendered due to their size.
                            • rootsInner wm halfLen 0 x✝¹ x✝ = x✝
                            Instances For

                              roots[2^k + j] = w_{2^(k+1)}^j · R mod mod64 (Montgomery domain)

                              Equations
                              Instances For
                                def ensureRoots.outer (n : ) (v : Vector UInt32 n) (len : UInt64) (hn : 0 < n) :
                                Equations
                                • One or more equations did not get rendered due to their size.
                                • ensureRoots.outer n v len hn 0 = v
                                Instances For
                                  def bitRevNext :

                                  Compute next index in bit-reveral order ("add 1 in bit-reversed binary")

                                  Equations
                                  Instances For
                                    def bitRevLoop {n : } (k i : ) :
                                    Equations
                                    • One or more equations did not get rendered due to their size.
                                    • bitRevLoop 0 x✝² x✝¹ x✝ = x✝¹
                                    Instances For
                                      def radix2Pass {n : } (k i : ) :
                                      Equations
                                      • One or more equations did not get rendered due to their size.
                                      • radix2Pass 0 x✝¹ x✝ = x✝
                                      Instances For
                                        @[inline]
                                        def butterfly4 {n : } (a : Vector UInt32 n) (inverse : Bool) (roots : Vector UInt32 n) (s len i2 j2 : ) :

                                        ── Radix-4 butterfly ──────────────────────────────────────────────────────── #

                                        Two-stage DIF decomposition. Stage 1 applies the same twiddle t1 to both aB (at offset s) and aD (at offset len + s) — this is intentional: in this grouping both elements occupy the "odd" slot of their respective half-group and share the same first-stage twiddle. t2 and t3 are the distinct second-stage twiddles applied after the first-stage ±-combine.

                                        Equations
                                        • One or more equations did not get rendered due to their size.
                                        Instances For
                                          def radix4Inner {n : } (inverse : Bool) (roots : Vector UInt32 n) (s len i2 : ) :
                                          Vector UInt32 nVector UInt32 n
                                          Equations
                                          Instances For
                                            def radix4Middle {n : } (inverse : Bool) (roots : Vector UInt32 n) (s len : ) :
                                            Vector UInt32 nVector UInt32 n
                                            Equations
                                            Instances For
                                              def nttInplace {n : } (arr : Vector UInt32 n) (inverse : Bool) (roots : Vector UInt32 n) :
                                              Equations
                                              • One or more equations did not get rendered due to their size.
                                              Instances For
                                                def nttInplace.go :
                                                UInt64UInt64
                                                Equations
                                                Instances For
                                                  def nttInplace.outerLoop {n : } (inverse : Bool) (roots a : Vector UInt32 n) (len : UInt64) :
                                                  Equations
                                                  • One or more equations did not get rendered due to their size.
                                                  • nttInplace.outerLoop inverse roots a len 0 = a
                                                  Instances For
                                                    def circularConvolutionGf2 {n : } (a b : BitVec n) :

                                                    GF(2) circular convolution: (a * b)[i] = ⊕ₖ (a[k] ∧ b[(i−k) mod n]). Equivalently, multiplication in GF(2)[X] / (Xⁿ − 1). Computed via radix-4 NTT with Montgomery arithmetic. Uses 2n-point NTT to avoid aliasing; folds result[i] + result[i+n] to recover the circular (period-n) convolution from the linear product.

                                                    This is only correct for n < 2 ^ 29 ≈ 2.6e8.

                                                    Equations
                                                    • One or more equations did not get rendered due to their size.
                                                    Instances For

                                                      Specification of circular convolution on bit vectors. Note that for Bool, summing is xor, while multiplication is and.

                                                      Equations
                                                      Instances For