Documentation

CslibFrontier.DataStructure.HashFamily.WordRAM.KWise

Self-contained Word-RAM k-wise independent hashing #

This file implements self-contained Word-RAM k-wise independent hashing over Cslib.WordRAM.Basis.exact by evaluating degree-(k - 1) polynomials over the characteristic-2 finite field $\mathbb{F}_{2^{2 \cdot 3^t}} = \mathbb{F}_2[X] / (X^{2 \cdot 3^t} + X^{3^t} + 1)$ (Cslib.TrinomialField t, where t = trinomialStep U M is chosen at runtime so that $q = 2 \cdot 3^t \ge \max(\lceil \log_2 \max(U, 2) \rceil, \max(M, 1))$) and projecting to the low M bits (Cslib.Probability.trinomialPolyHash). The binary-field polynomial construction and bit-prefix projection follow Vadhan, Section 3.5.5, Construction 3.32 / Proposition 3.33 and Corollary 3.34 and Wegman and Carter, building on the universal hashing framework of Carter and Wegman, pp. 144–145 and finite-field arithmetic in Lidl and Niederreiter.

Because Basis.exact lacks a hardware carryless-multiplication instruction, initTableCode k precomputes a Four-Russians carryless multiplication table clmulTab[0 .. 2 ^ (2 * b) - 1] for $b$-bit chunks (b = chunkBits n_max = max 1 (⌊log₂ (max n_max 2)⌋ / 3)). This table uses at most $\max(4, n_{\max})$ words for all $n_{\max}$ (two_pow_two_mul_chunkBits_le) and satisfies the sublinear cube bound $(2^{2b})^3 \le n_{\max}^2$, i.e., $2^{2b} \le n_{\max}^{2/3}$, whenever $n_{\max} \ge 8$ (two_pow_two_mul_chunkBits_pow_three_le). Each $q$-bit field multiplication splits its operands into $L = \lceil q / b \rceil$ limbs of $b$ bits, accumulates the $L \times L$ chunk products from clmulTab, and reduces the resulting $< 2^{2q}$ polynomial modulo $X^{2r} + X^r + 1$ in 8 bit operations (reduceTrinomialCode). Under the closed-form bound evalSteps n_max U M k = ((11 * L + 12) * L + 30) * k + 19, each field multiplication takes $O(L^2)$ instructions, which is $O(1)$ instructions when both $U \le n_{\max}^c$ and $M \le c \log_2 n_{\max}$ for a fixed constant $c$.

The initialization is factored into two reusable stages:

  1. initTableCode k: deterministically computes $r = 3^t$, $q = 2r$, $b$, masks, and populates clmulTab[0 .. 2 ^ (2 * b) - 1] in initTableSteps n_max U M ($= O(n_{\max} + \log U + \log M)$) deterministic instructions from (base, n_max, U, M) alone.
  2. resampleCode k: samples the $k$ polynomial coefficients seed[0 .. k - 1] uniformly and independently in Fin (2 ^ q) using $k$ Code.rand calls in $5k + 10 = O(k)$ instructions while preserving clmulTab, so rehashing takes $O(k)$ instructions without rebuilding clmulTab.
  3. initCode k := .seq (initTableCode k) (resampleCode k): full one-shot initialization.
  4. evalCode k: evaluates Horner's rule for trinomialPolyHash in evalSteps n_max U M k ($= O(k \cdot L^2 + 1)$) deterministic instructions, preserving all memory and writing (trinomialPolyHash (trinomialStep U M) k U M c u).val into regOut.

Main definitions #

Main results #

References #

Section 1: Parameters, bounds, and memory layout #

Chunk bit-width b = max 1 (⌊log₂ (max n_max 2)⌋ / 3) for Four-Russians carryless multiplication, ensuring 1 ≤ b and 2 ^ (2 * b) ≤ max 4 n_max.

The Four-Russians carryless multiplication table size 2 ^ (2 * chunkBits n_max) is at most max 4 n_max for all n_max.

For 8 ≤ n_max, the Four-Russians table size 2 ^ (2 * chunkBits n_max) satisfies the sublinear cube bound (2 ^ (2 * chunkBits n_max)) ^ 3 ≤ n_max ^ 2 (i.e., 2 ^ (2 * chunkBits n_max) ≤ n_max ^ (2 / 3)).

Number of b-bit limbs L = ⌈q / b⌉ = (q + b - 1) / b needed to cover a q-bit field element.

Number of chunkBits n_max-bit limbs covering trinomialDeg U M bits.

When 0 < b, limbCountFromB b q * b is at least q.

Memory footprint 8 + k + 2 ^ (2 * b) words for k-wise independent hashing with chunk width b: 8 header words, k seed words, and 2 ^ (2 * b) carryless multiplication table entries.

Memory footprint 8 + k + 2 ^ (2 * chunkBits n_max) words at capacity parameter n_max.

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.space_le (n_max k : ℕ) :
space n_max k ≤ 8 + k + max 4 n_max

space n_max k is at most 8 + k + max 4 n_max = O(n_max + k).

The width bound 2 * trinomialDeg U M ≤ w makes the field size 2 ^ trinomialDeg U M fit in a word.

Deterministic table representation predicate TableRep base b U M k mem: stores the 8 header parameters (r, q, 2 ^ r - 1, 2 ^ q - 1, 2 ^ M - 1, b, 2 ^ b - 1, L) at base + 0 .. base + 7 and the Four-Russians carryless multiplication table clmul (idx >>> b) (idx &&& (2 ^ b - 1)) at base + 8 + k + idx for idx < 2 ^ (2 * b).

Seed representation predicate SeedRep base U M k c mem: stores the k polynomial coefficients (c j).val < 2 ^ (trinomialDeg U M) at base + 8 + j.val.

def Cslib.DataStructure.HashFamily.WordRAM.KWise.Rep (base b U M k : ℕ) (c : Fin k → Fin (2 ^ Probability.trinomialDeg U M)) (mem : ℕ → ℕ) :

Full representation predicate combining TableRep and SeedRep.

Extract the k-tuple of seeds Fin k → Fin (2 ^ trinomialDeg U M) from [base + 8, base + 8 + k).

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.extractSeeds_eq_of_seedRep {base U M k : ℕ} {c : Fin k → Fin (2 ^ Probability.trinomialDeg U M)} {mem : ℕ → ℕ} (hseed : SeedRep base U M k c mem) :
extractSeeds base U M k mem = c

When SeedRep base U M k c mem holds, extractSeeds base U M k mem recovers the seed tuple c.

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.SeedRep.congr_of_footprint {base U M k : ℕ} {c : Fin k → Fin (2 ^ Probability.trinomialDeg U M)} {mem mem' : ℕ → ℕ} (hseed : SeedRep base U M k c mem) (hm : ∀ (a : ℕ), base ≤ a → a < base + 8 + k → mem' a = mem a) :
SeedRep base U M k c mem'

SeedRep base U M k c depends only on the memory values in [base, base + 8 + k).

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.extractSeeds_congr_of_footprint {base U M k : ℕ} {mem mem' : ℕ → ℕ} (hm : ∀ (a : ℕ), base ≤ a → a < base + 8 + k → mem' a = mem a) :
extractSeeds base U M k mem' = extractSeeds base U M k mem

extractSeeds base U M k depends only on the memory values in [base, base + 8 + k).

Section 2: Helper loops for initTableCode #

@[irreducible]

Final remainder reached by trinomialStepAux when repeatedly mapping B ↦ (B + 2) / 3 until ≤ 2.

Body of trinomialStepLoop on Data 4 (reg 0 = rem, reg 1 = r, reg 2 = 2, reg 3 = 3): updates rem ← (rem + 2) / 3 and r ← r * 3.

Loop while 2 < rem do [ rem ← (rem + 2) / 3; r ← r * 3 ] computing 3 ^ trinomialStepAux B.

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.trinomialStepLoop_exec {w : ℕ} (B : ℕ) {acc : ℕ} {x : WordRAM.Data 4} (hrem : x.regs 0 = B) (hacc : x.regs 1 = acc) (htwo : x.regs 2 = 2) (hthree : x.regs 3 = 3) (hfit_B2 : WordRAM.Fits w (B + 2)) (hfit_res : WordRAM.Fits w (acc * 3 ^ Probability.trinomialStepAux B)) :

trinomialStepLoop executes deterministically in 5 * trinomialStepAux B + 2 instructions, accumulating acc * 3 ^ trinomialStepAux B into register 1 and preserving all memory.

Body of clmulTabLoop on Data 18 (reg 0 = tabBase, reg 1 = idx, reg 2 = tabSize, reg 3 = b, reg 4 = 2 ^ b - 1, reg 5 = 1, reg 6..9 scratch): populates mem[tabBase + idx] ← clmul (idx >>> b) (idx &&& (2 ^ b - 1)) using previously filled entry prevIdx = ((idx >>> b) <<< b) + ((idx &&& (2 ^ b - 1)) >>> 1) and increments idx.

Loop while idx < tabSize do clmulTabBody populating the Four-Russians carryless multiplication table clmulTab[0 .. 2 ^ (2 * b) - 1].

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.clmulTabLoop_exec {w tabBase b : ℕ} (hfit_end : WordRAM.Fits w (tabBase + 2 ^ (2 * b))) (rem idx : ℕ) (x : WordRAM.Data 18) :
idx + rem = 2 ^ (2 * b) → x.regs 0 = tabBase → x.regs 1 = idx → x.regs 2 = 2 ^ (2 * b) → x.regs 3 = b → x.regs 4 = 2 ^ b - 1 → x.regs 5 = 1 → (∀ i < idx, x.mem (tabBase + i) = clmul (i >>> b) (i &&& 2 ^ b - 1)) → ∃ (y : WordRAM.Data 18), WordRAM.Exec w clmulTabLoop x y (19 * rem + 2) ∧ y.regs 0 = tabBase ∧ (∀ i < 2 ^ (2 * b), y.mem (tabBase + i) = clmul (i >>> b) (i &&& 2 ^ b - 1)) ∧ ∀ (a : ℕ), a < tabBase + idx ∨ tabBase + 2 ^ (2 * b) ≤ a → y.mem a = x.mem a

clmulTabLoop executes deterministically in 19 * (2 ^ (2 * b) - idx) + 2 instructions, filling mem[tabBase + i] = clmul (i >>> b) (i &&& (2 ^ b - 1)) for all i < 2 ^ (2 * b) and preserving reg 0 = tabBase and memory outside [tabBase + idx, tabBase + 2 ^ (2 * b)).

Section 3: Table construction (initTableCode) and seed resampling (resampleCode) #

@[reducible, inline]

Register 0: holds the base memory address base.

@[reducible, inline]

Register 1: holds n_max on entry to initTableCode (and key on entry to evalCode).

@[reducible, inline]

Register 2: holds U on entry to initTableCode (and output in evalCode).

@[reducible, inline]

Register 3: holds M on entry to initTableCode.

Subprogram on Data 18 computing chunkBits n_max into register 5 and 1 into register 6 from n_max in register 1, while preserving registers 0, 1, 2, 3 and memory.

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.chunkBitsCode_exec {w base n_max U M : ℕ} {x : WordRAM.Data 18} (h0 : x.regs 0 = base) (h1 : x.regs 1 = n_max) (h2 : x.regs 2 = U) (h3 : x.regs 3 = M) (hfit_n : WordRAM.Fits w (max n_max 2)) (hfit_3 : WordRAM.Fits w 3) :
∃ (y : WordRAM.Data 18), WordRAM.Exec w chunkBitsCode x y (4 * Nat.log 2 (max n_max 2) + 13) ∧ y.regs 0 = base ∧ y.regs 2 = U ∧ y.regs 3 = M ∧ y.regs 5 = chunkBits n_max ∧ y.regs 6 = 1 ∧ y.mem = x.mem

chunkBitsCode executes deterministically in 4 * Nat.log 2 (max n_max 2) + 13 steps.

Subprogram on Data 18 computing r = 3 ^ trinomialStep U M into register 1, q = trinomialDeg U M into register 2, and 2 into register 4 from U in register 2, M in register 3, and 1 in register 6, while preserving reg 0 = base, reg 3 = M, reg 5 = b, reg 6 = 1, and memory.

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.trinomialParamsCode_exec {w base U M b : ℕ} {x : WordRAM.Data 18} (h0 : x.regs 0 = base) (h2 : x.regs 2 = U) (h3 : x.regs 3 = M) (h5 : x.regs 5 = b) (h6 : x.regs 6 = 1) (hfit_U : WordRAM.Fits w (max U 2 + 1)) (hfit_q : WordRAM.Fits w (2 ^ Probability.trinomialDeg U M)) :
∃ (y : WordRAM.Data 18), WordRAM.Exec w trinomialParamsCode x y (5 * Nat.clog 2 (max U 2) + 5 * Probability.trinomialStep U M + 18) ∧ y.regs 0 = base ∧ y.regs 1 = 3 ^ Probability.trinomialStep U M ∧ y.regs 2 = Probability.trinomialDeg U M ∧ y.regs 3 = M ∧ y.regs 4 = 2 ∧ y.regs 5 = b ∧ y.regs 6 = 1 ∧ y.mem = x.mem

trinomialParamsCode executes deterministically in 5 * Nat.clog 2 (max U 2) + 5 * trinomialStep U M + 18 steps.

First 17-instruction header-store block: writes r, q, 2 ^ r - 1, 2 ^ q - 1, 2 ^ M - 1, b to base + 0 .. base + 5.

Second 17-instruction header-store and loop-prep block: writes 2 ^ b - 1 and limbCountFromB b q to base + 6, base + 7 and sets registers 0..5 for clmulTabLoop.

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.storeHeaderPart1_exec {w base r q M b : ℕ} {x : WordRAM.Data 18} (h0 : x.regs 0 = base) (h1 : x.regs 1 = r) (h2 : x.regs 2 = q) (h3 : x.regs 3 = M) (h4 : x.regs 4 = 2) (h5 : x.regs 5 = b) (h6 : x.regs 6 = 1) (hr_le : r ≤ q) (hM_le : M ≤ q) (hfit_end : WordRAM.Fits w (base + 8)) (hfit_q : WordRAM.Fits w (2 ^ q)) :
∃ (y : WordRAM.Data 18), WordRAM.Exec w storeHeaderPart1 x y 17 ∧ y.regs 0 = base ∧ y.regs 2 = q ∧ y.regs 4 = 2 ∧ y.regs 5 = b ∧ y.regs 6 = 1 ∧ y.regs 7 = base + 5 ∧ y.mem base = r ∧ y.mem (base + 1) = q ∧ y.mem (base + 2) = 2 ^ r - 1 ∧ y.mem (base + 3) = 2 ^ q - 1 ∧ y.mem (base + 4) = 2 ^ M - 1 ∧ y.mem (base + 5) = b ∧ ∀ (a : ℕ), a < base ∨ base + 6 ≤ a → y.mem a = x.mem a

storeHeaderPart1 executes deterministically in 17 steps.

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.storeHeaderPart2_exec {w base q b k : ℕ} {x : WordRAM.Data 18} (hb : 0 < b) (hq : 0 < q) (h0 : x.regs 0 = base) (h2 : x.regs 2 = q) (h4 : x.regs 4 = 2) (h5 : x.regs 5 = b) (h6 : x.regs 6 = 1) (h7 : x.regs 7 = base + 5) (hfit_space : WordRAM.Fits w (base + spaceFromB b k)) (hfit_q : WordRAM.Fits w q) :
∃ (y : WordRAM.Data 18), WordRAM.Exec w (storeHeaderPart2 k) x y 17 ∧ y.regs 0 = base + 8 + k ∧ y.regs 1 = 0 ∧ y.regs 2 = 2 ^ (2 * b) ∧ y.regs 3 = b ∧ y.regs 4 = 2 ^ b - 1 ∧ y.regs 5 = 1 ∧ y.mem (base + 6) = 2 ^ b - 1 ∧ y.mem (base + 7) = limbCountFromB b q ∧ ∀ (a : ℕ), a < base + 6 ∨ base + 8 ≤ a → y.mem a = x.mem a

storeHeaderPart2 k executes deterministically in 17 steps.

2-instruction suffix restoring regBase = base from base + 8 + k.

Complete deterministic table initialization program initTableCode k: computes (r, q, b, L) and masks from (base, n_max, U, M) in registers 0..3 and populates clmulTab[0 .. 2 ^ (2 * b) - 1].

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.initTableCode_execWithin {w base n_max U M k : ℕ} {x : WordRAM.Data 18} (h0 : x.regs 0 = base) (h1 : x.regs 1 = n_max) (h2 : x.regs 2 = U) (h3 : x.regs 3 = M) (hfit_space : WordRAM.Fits w (base + space n_max k)) (hfit_n : WordRAM.Fits w (max n_max 2)) (hfit_U : WordRAM.Fits w (max U 2 + 1)) (hfit_q : WordRAM.Fits w (2 ^ Probability.trinomialDeg U M)) :
∃ (y : WordRAM.Data 18), WordRAM.ExecWithin w (initTableCode k) x y (initTableSteps n_max U M) ∧ TableRep base (chunkBits n_max) U M k y.mem ∧ y.regs 0 = base ∧ ∀ (a : ℕ), a < base ∨ base + space n_max k ≤ a → y.mem a = x.mem a

KWise.initTableCode k correctness and complexity: from reg 0 = base, reg 1 = n_max, reg 2 = U, reg 3 = M, initTableCode k halts deterministically in initTableSteps n_max U M = O(n_max + log U + log M) instructions, establishing TableRep base (chunkBits n_max) U M k y.mem, preserving reg 0 = base, and modifying memory only within [base, base + space n_max k).

Setup prefix for resampleCode k: loads 2 ^ q - 1 from base + 3 into register 1 and initializes ptr = base + 8 (reg 2), end = base + 8 + k (reg 3), and one = 1 (reg 4).

Injective register map embedding randFillLoop into Data 18 so regExtra (5) maps to regBase (0).

Seed-resampling program resampleCode k: samples the k polynomial coefficients seed[0 .. k - 1] uniformly in Fin (2 ^ trinomialDeg U M) using k Code.rand calls in 5 * k + 10 instructions while preserving TableRep and regBase = base.

Exact ProbComp semantics of resampleCode k.

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.resampleSetupCode_exec {w base b U M k : ℕ} {x : WordRAM.Data 18} (htab : TableRep base b U M k x.mem) (h0 : x.regs 0 = base) (hfit_end : WordRAM.Fits w (base + 8 + k)) :

resampleSetupCode k executes deterministically in 8 steps.

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.resampleCode_execRand {w base b U M k : ℕ} {x : WordRAM.Data 18} (htab : TableRep base b U M k x.mem) (h0 : x.regs 0 = base) (hfit_end : WordRAM.Fits w (base + 8 + k)) (hfit_q : WordRAM.Fits w (2 ^ Probability.trinomialDeg U M)) :

KWise.resampleCode k exact randomized execution: executes with exact ProbComp semantics resampleComp base U M k x.

Uniformity of resampled seeds: under (resampleComp base U M k x).eval, the extracted seed tuple extractSeeds base U M k z.1.mem is uniformly distributed over Fin k → Fin (2 ^ trinomialDeg U M).

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.mem_support_resampleComp {base b U M k : ℕ} {x : WordRAM.Data 18} (htab : TableRep base b U M k x.mem) (h0 : x.regs 0 = base) {z : WordRAM.Data 18 × ℕ} (hz : z ∈ OracleComp.support (resampleComp base U M k x)) :
(Rep base b U M k (extractSeeds base U M k z.1.mem) z.1.mem ∧ z.1.regs 0 = base ∧ ∀ (a : ℕ), a < base + 8 ∨ base + 8 + k ≤ a → z.1.mem a = x.mem a) ∧ z.2 ≤ 5 * k + 10

Every outcome z ∈ (resampleComp base U M k x).support preserves TableRep base b U M k, establishes Rep base b U M k (extractSeeds base U M k z.1.mem) z.1.mem, sets z.1.regs 0 = base, modifies memory only within [base + 8, base + 8 + k), and takes at most 5 * k + 10 steps.

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.resampleCode_execRandWithin {w base b U M k : ℕ} {x : WordRAM.Data 18} (htab : TableRep base b U M k x.mem) (h0 : x.regs 0 = base) (hfit_end : WordRAM.Fits w (base + 8 + k)) (hfit_q : WordRAM.Fits w (2 ^ Probability.trinomialDeg U M)) :
WordRAM.ExecRandWithin w (resampleCode k) x (fun (y : WordRAM.Data 18) => Rep base b U M k (extractSeeds base U M k y.mem) y.mem ∧ y.regs 0 = base ∧ ∀ (a : ℕ), a < base + 8 ∨ base + 8 + k ≤ a → y.mem a = x.mem a) (5 * k + 10) k

KWise.resampleCode k Hoare-style specification: halts in 5 * k + 10 = O(k) steps and k rand queries, preserving TableRep base b U M k, establishing Rep base b U M k (extractSeeds base U M k z.1.mem) z.1.mem, preserving reg 0 = base, and modifying memory only within [base + 8, base + 8 + k).

Full one-shot initialization program initCode k := .seq (initTableCode k) (resampleCode k).

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.initCode_execRandWithin {w base n_max U M k : ℕ} {x : WordRAM.Data 18} (h0 : x.regs 0 = base) (h1 : x.regs 1 = n_max) (h2 : x.regs 2 = U) (h3 : x.regs 3 = M) (hfit_space : WordRAM.Fits w (base + space n_max k)) (hfit_n : WordRAM.Fits w (max n_max 2)) (hfit_U : WordRAM.Fits w (max U 2 + 1)) (hfit_q : WordRAM.Fits w (2 ^ Probability.trinomialDeg U M)) :
WordRAM.ExecRandWithin w (initCode k) x (fun (y : WordRAM.Data 18) => Rep base (chunkBits n_max) U M k (extractSeeds base U M k y.mem) y.mem ∧ y.regs 0 = base ∧ ∀ (a : ℕ), a < base ∨ base + space n_max k ≤ a → y.mem a = x.mem a) (initTableSteps n_max U M + (5 * k + 10)) k

KWise.initCode k Hoare-style specification: runs initTableCode k followed by resampleCode k in initTableSteps n_max U M + (5 * k + 10) steps and k rand calls.

Section 4: Deterministic Horner evaluation (evalCode) and k-wise independence #

@[reducible, inline]

Register 1: holds the input key u < U on entry to evalCode.

@[reducible, inline]

Register 2: holds the output hash value < 2 ^ M on exit from evalCode.

Body of innerClmulLoop: looks up clmul (x_rem &&& mask_b) (y_rem &&& mask_b) in clmulTab, shifts left by shift_j, XORs into acc (reg 2), shifts y_rem right by b, advances shift_j by b, and decrements rem_j.

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.innerClmulLoop_exec {w tabBase b q x_rem sh_x : ℕ} (hb : 0 < b) (hw : 2 * q ≤ w) (hfit_tabEnd : WordRAM.Fits w (tabBase + 2 ^ (2 * b))) (hx_bound : x_rem * 2 ^ sh_x < 2 ^ q) (rem_j y_rem sh_y acc : ℕ) (s : WordRAM.Data 18) :
y_rem < 2 ^ (rem_j * b) → y_rem * 2 ^ sh_y < 2 ^ q → acc < 2 ^ (2 * q) → WordRAM.Fits w (sh_x + sh_y + rem_j * b) → s.regs 2 = acc → s.regs 4 = b → s.regs 5 = 2 ^ b - 1 → s.regs 7 = tabBase → s.regs 8 = 1 → s.regs 9 = 0 → s.regs 13 = (x_rem &&& 2 ^ b - 1) <<< b → s.regs 14 = rem_j → s.regs 15 = y_rem → s.regs 16 = sh_x + sh_y → (∀ i < 2 ^ (2 * b), s.mem (tabBase + i) = clmul (i >>> b) (i &&& 2 ^ b - 1)) → ∃ (y : WordRAM.Data 18), WordRAM.Exec w innerClmulLoop s y (11 * rem_j + 2) ∧ y.regs 2 = acc ^^^ clmul (x_rem &&& 2 ^ b - 1) y_rem <<< (sh_x + sh_y) ∧ (∀ (r : Fin 18), r ≠ 2 ∧ r ≠ 14 ∧ r ≠ 15 ∧ r ≠ 16 ∧ r ≠ 17 → y.regs r = s.regs r) ∧ y.mem = s.mem

innerClmulLoop executes deterministically in 11 * rem_j + 2 steps, XORing clmul (x_rem &&& (2 ^ b - 1)) y_rem <<< (sh_x + sh_y) into reg 2 (acc).

Body of outerClmulLoop: computes x_chunk_sh = (x_rem &&& mask_b) <<< b, initializes the inner loop over L limbs of u starting at shift shift_i, runs innerClmulLoop, and advances x_rem, shift_i, and rem_i.

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.outerClmulLoop_exec {w tabBase b q L u : ℕ} (hb : 0 < b) (hw : 2 * q ≤ w) (hfit_tabEnd : WordRAM.Fits w (tabBase + 2 ^ (2 * b))) (hu_limbs : u < 2 ^ (L * b)) (hu_bound : u < 2 ^ q) (rem_i x_rem sh_x acc : ℕ) (s : WordRAM.Data 18) :
x_rem < 2 ^ (rem_i * b) → x_rem * 2 ^ sh_x < 2 ^ q → acc < 2 ^ (2 * q) → WordRAM.Fits w (sh_x + rem_i * b + L * b) → s.regs 1 = u → s.regs 2 = acc → s.regs 4 = b → s.regs 5 = 2 ^ b - 1 → s.regs 6 = L → s.regs 7 = tabBase → s.regs 8 = 1 → s.regs 9 = 0 → s.regs 10 = rem_i → s.regs 11 = x_rem → s.regs 12 = sh_x → (∀ i < 2 ^ (2 * b), s.mem (tabBase + i) = clmul (i >>> b) (i &&& 2 ^ b - 1)) → ∃ (y : WordRAM.Data 18), WordRAM.Exec w outerClmulLoop s y ((11 * L + 12) * rem_i + 2) ∧ y.regs 2 = acc ^^^ clmul x_rem u <<< sh_x ∧ (∀ (r : Fin 18), ↑r < 10 ∧ r ≠ 2 → y.regs r = s.regs r) ∧ y.mem = s.mem

outerClmulLoop executes deterministically in (11 * L + 12) * rem_i + 2 steps, XORing clmul x_rem u <<< sh_x into reg 2 (acc).

16-instruction straight-line reduction block on Data 18: loads r, q = 2 * r, 2 ^ r - 1, and 2 ^ q - 1 from base + 0 .. base + 3 into registers 10..13 and replaces c in reg 2 with reduceTrinomialCode (trinomialStep U M) c, preserving registers 0, 1, 3..9 and memory.

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.reduceStepCode_exec {w base b U M k c : ℕ} {s : WordRAM.Data 18} (htab : TableRep base b U M k s.mem) (hw : 2 * Probability.trinomialDeg U M ≤ w) (hfit_end8 : WordRAM.Fits w (base + 8)) (hc : c < 2 ^ (2 * Probability.trinomialDeg U M)) (h0 : s.regs 0 = base) (h2 : s.regs 2 = c) (h8 : s.regs 8 = 1) :
∃ (y : WordRAM.Data 18), WordRAM.Exec w reduceStepCode s y 16 ∧ y.regs 2 = reduceTrinomialCode (Probability.trinomialStep U M) c ∧ (∀ (r : Fin 18), ↑r < 10 ∧ r ≠ 2 → y.regs r = s.regs r) ∧ y.mem = s.mem

reduceStepCode executes deterministically in 16 instructions, replacing c in reg 2 with reduceTrinomialCode (trinomialStep U M) c.

Body of hornerLoop on Data 18: decrements j (reg 3), multiplies acc (reg 2) by u (reg 1) in TrinomialField (trinomialStep U M) via outerClmulLoop and reduceStepCode, and adds seed[j] from mem[base + 8 + j].

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.hornerLoop_exec {w base b U M k : ℕ} {c : Fin k → Fin (2 ^ Probability.trinomialDeg U M)} (u : Fin U) (hb : 0 < b) (hw : 2 * Probability.trinomialDeg U M ≤ w) (hfit_space : WordRAM.Fits w (base + spaceFromB b k)) :
let t := Probability.trinomialStep U M; have q := Probability.trinomialDeg U M; have L := limbCountFromB b q; have c_field := fun (j : Fin k) => (TrinomialField.equivFin t).symm (c j); have x_field := (AdjoinRoot.mk (trinomial t)) (polyOfCode ↑u); ∀ (m : ℕ) (hm : m ≤ k) (acc : TrinomialField t) (s : WordRAM.Data 18), Rep base b U M k c s.mem → s.regs 0 = base → s.regs 1 = ↑u → s.regs 2 = ↑((TrinomialField.equivFin t) acc) → s.regs 3 = m → s.regs 4 = b → s.regs 5 = 2 ^ b - 1 → s.regs 6 = L → s.regs 7 = base + 8 + k → s.regs 8 = 1 → s.regs 9 = 0 → ∃ (y : WordRAM.Data 18), WordRAM.Exec w hornerLoop s y (((11 * L + 12) * L + 30) * m + 2) ∧ y.regs 0 = base ∧ y.regs 1 = ↑u ∧ y.regs 2 = ↑((TrinomialField.equivFin t) (acc * x_field ^ m + ∑ i : Fin m, c_field (Fin.castLE hm i) * x_field ^ ↑i)) ∧ y.mem = s.mem

hornerLoop executes deterministically in ((11 * L + 12) * L + 30) * m + 2 instructions, updating reg 2 from (equivFin t acc).val to (equivFin t (acc * x ^ m + ∑ i : Fin m, c_field (Fin.castLE hm i) * x ^ (i : ℕ))).val.

13-instruction setup block for evalCode k: initializes reg 8 = 1, reg 9 = 0, reg 2 = 0, reg 3 = k, loads b, 2 ^ b - 1, L from base + 5 .. base + 7 into reg 4..6, and sets reg 7 = base + 8 + k.

4-instruction suffix for evalCode k: loads 2 ^ M - 1 from base + 4 and masks reg 2.

Deterministic evaluation program evalCode k: given reg 0 = base and reg 1 = u.val, evaluates trinomialPolyHash (trinomialStep U M) k U M c u into reg 2 (regOut) in evalStepsFromB b U M k instructions while preserving reg 0 = base, reg 1 = u.val, and all memory.

Exact instruction count of evalCode k for chunk width b, universe U, output bits M, and independence parameter k.

Exact instruction count of evalCode k for capacity n_max, universe U, output bits M, and independence parameter k.

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.evalSetupCode_exec {w base b U M k : ℕ} {x : WordRAM.Data 18} (htab : TableRep base b U M k x.mem) (h0 : x.regs 0 = base) (hfit_end : WordRAM.Fits w (base + 8 + k)) :
∃ (y : WordRAM.Data 18), WordRAM.Exec w (evalSetupCode k) x y 13 ∧ y.regs 0 = base ∧ y.regs 1 = x.regs 1 ∧ y.regs 2 = 0 ∧ y.regs 3 = k ∧ y.regs 4 = b ∧ y.regs 5 = 2 ^ b - 1 ∧ y.regs 6 = limbCountFromB b (Probability.trinomialDeg U M) ∧ y.regs 7 = base + 8 + k ∧ y.regs 8 = 1 ∧ y.regs 9 = 0 ∧ y.mem = x.mem

evalSetupCode k executes deterministically in 13 steps.

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.evalCode_execWithin_fromB {w base b U M k : ℕ} {c : Fin k → Fin (2 ^ Probability.trinomialDeg U M)} (u : Fin U) {x : WordRAM.Data 18} (hb : 0 < b) (hrep : Rep base b U M k c x.mem) (h0 : x.regs 0 = base) (h1 : x.regs 1 = ↑u) (hw : 2 * Probability.trinomialDeg U M ≤ w) (hfit_space : WordRAM.Fits w (base + spaceFromB b k)) :
∃ (y : WordRAM.Data 18), WordRAM.ExecWithin w (evalCode k) x y (evalStepsFromB b U M k) ∧ y.regs 0 = base ∧ y.regs 1 = ↑u ∧ y.regs 2 = ↑(Probability.trinomialPolyHash (Probability.trinomialStep U M) k U M c u) ∧ y.mem = x.mem

KWise.evalCode k correctness and complexity (general b): from Rep base b U M k c x.mem, x.regs 0 = base, and x.regs 1 = u.val (u : Fin U), evalCode k halts deterministically in evalStepsFromB b U M k = O(k * L ^ 2 + 1) instructions, writing (trinomialPolyHash (trinomialStep U M) k U M c u).val into reg 2 (regOut) and preserving reg 0 = base, reg 1 = u.val, and all memory (y.mem = x.mem).

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.evalCode_execWithin {w base n_max U M k : ℕ} {c : Fin k → Fin (2 ^ Probability.trinomialDeg U M)} (u : Fin U) {x : WordRAM.Data 18} (hrep : Rep base (chunkBits n_max) U M k c x.mem) (h0 : x.regs 0 = base) (h1 : x.regs 1 = ↑u) (hw : 2 * Probability.trinomialDeg U M ≤ w) (hfit_space : WordRAM.Fits w (base + space n_max k)) :
∃ (y : WordRAM.Data 18), WordRAM.ExecWithin w (evalCode k) x y (evalSteps n_max U M k) ∧ y.regs 0 = base ∧ y.regs 1 = ↑u ∧ y.regs 2 = ↑(Probability.trinomialPolyHash (Probability.trinomialStep U M) k U M c u) ∧ y.mem = x.mem

KWise.evalCode k correctness and complexity: from Rep base (chunkBits n_max) U M k c x.mem, x.regs 0 = base, and x.regs 1 = u.val (u : Fin U), evalCode k halts deterministically in evalSteps n_max U M k = O(k * L ^ 2 + 1) instructions, writing (trinomialPolyHash (trinomialStep U M) k U M c u).val into reg 2 (regOut) and preserving reg 0 = base, reg 1 = u.val, and all memory (y.mem = x.mem).

noncomputable def Cslib.DataStructure.HashFamily.WordRAM.KWise.evalHashOut (w base k U M : ℕ) (y : WordRAM.Data 18) (u : Fin U) :
Fin (2 ^ M)

Output of deterministic evalCode k execution at word size w from state y with reg 0 ← base and reg 1 ← u.val, reduced modulo 2 ^ M into Fin (2 ^ M).

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.evalHashOut_eq_trinomialPolyHash {w base n_max U M k : ℕ} {c : Fin k → Fin (2 ^ Probability.trinomialDeg U M)} {y : WordRAM.Data 18} (hrep : Rep base (chunkBits n_max) U M k c y.mem) (hw : 2 * Probability.trinomialDeg U M ≤ w) (hfit_space : WordRAM.Fits w (base + space n_max k)) (u : Fin U) :

On any machine state y satisfying Rep base (chunkBits n_max) U M k c y.mem, the deterministic evalCode k output evalHashOut w base k U M y u equals trinomialPolyHash (trinomialStep U M) k U M c u.

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.map_evalHashOut_eval_resampleComp {w base n_max U M k : ℕ} {x : WordRAM.Data 18} (htab : TableRep base (chunkBits n_max) U M k x.mem) (h0 : x.regs 0 = base) (hw : 2 * Probability.trinomialDeg U M ≤ w) (hfit_space : WordRAM.Fits w (base + space n_max k)) :

Under resampleComp base U M k x, the Word-RAM hash function evalHashOut w base k U M z.1 has the exact distribution of trinomialPolyHash (trinomialStep U M) k U M c for a uniform seed c : Fin k → Fin (2 ^ trinomialDeg U M).

For any k-wise independent hash family h : ι → X → Y and any set T : Finset X of at most k points, restricting h i to T under a uniform seed i ← PMF.uniformOfFintype ι yields the uniform distribution PMF.uniformOfFintype (T → Y).

theorem Cslib.DataStructure.HashFamily.WordRAM.KWise.isKWiseIndependentHash_eval {w base n_max U M k : ℕ} {x : WordRAM.Data 18} (htab : TableRep base (chunkBits n_max) U M k x.mem) (h0 : x.regs 0 = base) (hw : 2 * Probability.trinomialDeg U M ≤ w) (hfit_space : WordRAM.Fits w (base + space n_max k)) {T : Finset (Fin U)} (hT : T.card ≤ k) :
PMF.map (fun (z : WordRAM.Data 18 × ℕ) => T.restrict (evalHashOut w base k U M z.1)) (resampleComp base U M k x).eval = Probability.PMF.uniformOfFintype (↥T → Fin (2 ^ M))

Composed Word-RAM k-wise independence of resampleCode and evalCode (isKWiseIndependentHash_eval): from any machine state x satisfying TableRep base (chunkBits n_max) U M k x.mem and x.regs 0 = base, under the resampling distribution (resampleComp base U M k x).eval of resampleCode k, for every set T : Finset (Fin U) of at most k keys (T.card ≤ k), the restricted tuple of deterministic evalCode k outputs T.restrict (evalHashOut w base k U M z.1) is uniformly distributed on T → Fin (2 ^ M) (PMF.uniformOfFintype (T → Fin (2 ^ M))).