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:
initTableCode k: deterministically computes $r = 3^t$, $q = 2r$, $b$, masks, and populatesclmulTab[0 .. 2 ^ (2 * b) - 1]ininitTableSteps n_max U M($= O(n_{\max} + \log U + \log M)$) deterministic instructions from(base, n_max, U, M)alone.resampleCode k: samples the $k$ polynomial coefficientsseed[0 .. k - 1]uniformly and independently inFin (2 ^ q)using $k$Code.randcalls in $5k + 10 = O(k)$ instructions while preservingclmulTab, so rehashing takes $O(k)$ instructions without rebuildingclmulTab.initCode k := .seq (initTableCode k) (resampleCode k): full one-shot initialization.evalCode k: evaluates Horner's rule fortrinomialPolyHashinevalSteps 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).valintoregOut.
Main definitions #
Cslib.DataStructure.HashFamily.WordRAM.KWise.chunkBits,Cslib.DataStructure.HashFamily.WordRAM.KWise.limbCount,Cslib.DataStructure.HashFamily.WordRAM.KWise.space: chunk bit-width, limb count, and memory footprint.Cslib.DataStructure.HashFamily.WordRAM.KWise.TableRep,Cslib.DataStructure.HashFamily.WordRAM.KWise.SeedRep,Cslib.DataStructure.HashFamily.WordRAM.KWise.Rep: memory representation predicates separating the deterministicclmulTab/ header from the $k$ random seed words.Cslib.DataStructure.HashFamily.WordRAM.KWise.initTableCode,Cslib.DataStructure.HashFamily.WordRAM.KWise.resampleCode,Cslib.DataStructure.HashFamily.WordRAM.KWise.initCode,Cslib.DataStructure.HashFamily.WordRAM.KWise.evalCode,Cslib.DataStructure.HashFamily.WordRAM.KWise.evalHashOut: structured Word-RAM programs overBasis.exactand the induced deterministic hash output map.
Main results #
Cslib.DataStructure.HashFamily.WordRAM.KWise.two_pow_two_mul_chunkBits_le,Cslib.DataStructure.HashFamily.WordRAM.KWise.two_pow_two_mul_chunkBits_pow_three_le: coarse $\max(4, n_{\max})$ and sublinear $n_{\max}^{2/3}$ bounds on the Four-Russians table size.Cslib.DataStructure.HashFamily.WordRAM.KWise.fits_two_pow_trinomialDeg: the word-width bound2 * trinomialDeg U M ≤ wmakes2 ^ trinomialDeg U Mfit in a word.Cslib.DataStructure.HashFamily.WordRAM.KWise.extractSeeds_eq_of_seedRep,Cslib.DataStructure.HashFamily.WordRAM.KWise.SeedRep.congr_of_footprint,Cslib.DataStructure.HashFamily.WordRAM.KWise.extractSeeds_congr_of_footprint: seed-extraction inversion and memory-footprint congruence lemmas forSeedRepandextractSeeds.Cslib.DataStructure.HashFamily.WordRAM.KWise.initTableCode_execWithin: deterministicinitTableSteps n_max U M($= O(n_{\max} + \log U + \log M)$) table construction establishingTableRep.Cslib.DataStructure.HashFamily.WordRAM.KWise.resampleCode_execRand,Cslib.DataStructure.HashFamily.WordRAM.KWise.map_extractSeeds_eval_resampleComp,Cslib.DataStructure.HashFamily.WordRAM.KWise.resampleCode_execRandWithin: $(5k + 10)$-step, $k$-query seed resampling preservingTableRepand inducingPMF.uniformOfFintype (Fin k → Fin (2 ^ q)).Cslib.DataStructure.HashFamily.WordRAM.KWise.evalCode_execWithin,Cslib.DataStructure.HashFamily.WordRAM.KWise.evalHashOut_eq_trinomialPolyHash: deterministicevalSteps n_max U M k($= O(k \cdot L^2 + 1)$) Horner evaluation matchingtrinomialPolyHash.Cslib.DataStructure.HashFamily.WordRAM.KWise.map_evalHashOut_eval_resampleComp,Cslib.DataStructure.HashFamily.WordRAM.KWise.isKWiseIndependentHash_eval: composed Word-RAM $k$-wise independence theorem proving that for every set $T$ of at most $k$ keys, the restricted tuple ofevalCode koutputs afterresampleCode kis uniformly distributed on $T \to \text{Fin}(2^M)$.
References #
- S. P. Vadhan, Pseudorandomness, Section 3.5.5
- J. L. Carter and M. N. Wegman, Universal Classes of Hash Functions
- M. N. Wegman and J. L. Carter, New Hash Functions and Their Use in Authentication and Set Equality
- R. Lidl and H. Niederreiter, Finite Fields
- M. Mitzenmacher and E. Upfal, Probability and Computing
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.
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.
limbCount n_max U M * chunkBits n_max covers all trinomialDeg U M bits.
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.
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.
When SeedRep base U M k c mem holds, extractSeeds base U M k mem recovers the seed tuple
c.
SeedRep base U M k c depends only on the memory values in [base, base + 8 + k).
Section 2: Helper loops for initTableCode #
Final remainder reached by trinomialStepAux when repeatedly mapping B ↦ (B + 2) / 3 until
≤ 2.
trinomialStepRem B is always at most 2.
trinomialStepAux B is at most B.
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.
Closed-form final state of trinomialStepLoop.
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].
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) #
Register 0: holds the base memory address base.
Register 1: holds n_max on entry to initTableCode (and key on entry to evalCode).
Register 2: holds U on entry to initTableCode (and output in evalCode).
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.
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.
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.
storeHeaderPart1 executes deterministically in 17 steps.
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].
Exact step count of initTableCode k.
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).
Closed-form state after resampleSetupCode k.
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.
resampleSetupCode k executes deterministically in 8 steps.
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).
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.
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).
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.
Register 1: holds the input key u < U on entry to evalCode.
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.
Inner Four-Russians loop while 0 < rem_j do innerClmulBody.
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.
Outer Four-Russians loop while 0 < rem_i do outerClmulBody.
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.
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].
Horner evaluation loop while 0 < j do hornerBody.
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.
evalSetupCode k executes deterministically in 13 steps.
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).
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).
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).
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.
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).
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))).