Universe-Reduced k-Wise Independent Hashing with Bit-Packed Tables #
This module provides a reusable Word-RAM implementation of the two-stage universe-reduced k-wise
independent hash family (Cslib.Probability.reducedPolyHash U w₀ b b' k) over
Cslib.WordRAM.Basis.exact and lifts it to Cslib.WordRAM.Basis.standard:
- Two-stage construction without any universe restriction (
U ≤ 2 ^ w₀,0 < w₀ ≤ w):- Stage 1 (overflow-free multiply-shift reduction): reduces any
w₀-bit keyu : Fin U(0 < w₀ ≤ w) toR = min w₀ (3 * b)bits viamultiplyShiftReduced U w₀ (3 * b) α uusingoddMulModWord(39deterministic instructions, no2 * w₀ ≤ wrestriction). - Stage 2 (limb-aligned trinomial polynomial hash with bit-packed Four-Russians table):
evaluates a degree-
< kpolynomial overGF(2)[X] / (X^{2·3^t} + X^{3^t} + 1)on the reduced key usingL = reducedLimbCount t ≤ 54limbs ofc = reducedChunkBits t = 3 ^ (t - 3)bits and a bit-packed Four-Russians carryless multiplication table ofreducedTableBits t ≤ max 8 (2 ^ (b / 2)) = o(2 ^ b)bits (b' ≤ 3 * b).
- Stage 1 (overflow-free multiply-shift reduction): reduces any
- Compact
O(k) + o(2 ^ b) / w-word memory layout (space w w₀ b b' k):[base, base + 1 + k * L)stores the top seedα : Fin (2 ^ (w₀ - 1))(1word) and thek * Lc-bit limbs of the polynomial coefficientsβ : Fin k → Fin (2 ^ (2 * 3 ^ t))(1 + k * L ≤ 1 + 54 * k = O(k)words).[base + 1 + k * L, base + space w w₀ b b' k)stores the bit-packed carryless multiplication table intableWords w w₀ b b' = reducedTableWords t wwords.
O(1)worst-case evaluation time for fixedk(evalCode w w₀ b b' k):- Conditional
k-wise independence andHashFamily.WordRAM.Implpackaging:- Conditioned on any top seed
α ∈ reducedGoodTop U w₀ (3 * b) Sthat is injective onS : Finset (Fin U)(failure probability≤ (#S * (#S - 1)) / 2 ^ min w₀ (3 * b)), the restricted family onSis exactk-wise independent (IsKWiseIndependentHash k).
- Conditioned on any top seed
Main definitions #
Cslib.DataStructure.HashFamily.WordRAM.ReducedKWise.seedWords,Cslib.DataStructure.HashFamily.WordRAM.ReducedKWise.tableWords,Cslib.DataStructure.HashFamily.WordRAM.ReducedKWise.space: memory footprint sizes.Cslib.DataStructure.HashFamily.WordRAM.ReducedKWise.TableRep,Cslib.DataStructure.HashFamily.WordRAM.ReducedKWise.SeedRep,Cslib.DataStructure.HashFamily.WordRAM.ReducedKWise.Rep: memory representation predicates.Cslib.DataStructure.HashFamily.WordRAM.ReducedKWise.initTableCode,Cslib.DataStructure.HashFamily.WordRAM.ReducedKWise.resampleCode,Cslib.DataStructure.HashFamily.WordRAM.ReducedKWise.evalCode: Word-RAM programs onData 240.Cslib.DataStructure.HashFamily.WordRAM.ReducedKWise.impl,Cslib.DataStructure.HashFamily.WordRAM.ReducedKWise.implStandard:HashFamily.WordRAM.Implcertificates.
Main results #
Cslib.DataStructure.HashFamily.WordRAM.ReducedKWise.space_le:space w w₀ b b' k ≤ 2 + 54 * k + 2 * max 8 (2 ^ (b / 2)) / wwhenb' ≤ 3 * b.Cslib.DataStructure.HashFamily.WordRAM.ReducedKWise.initTableCode_execWithin: deterministic table initialization.Cslib.DataStructure.HashFamily.WordRAM.ReducedKWise.resampleCode_execRand,Cslib.DataStructure.HashFamily.WordRAM.ReducedKWise.map_extractSeed_eval_resampleComp: exact product uniformity of seed resampling.Cslib.DataStructure.HashFamily.WordRAM.ReducedKWise.evalCode_execWithin,Cslib.DataStructure.HashFamily.WordRAM.ReducedKWise.evalSteps_le: deterministic evaluation in≤ 47250 * k + 591instructions.
References #
Section 1: Memory layout, representation predicates, and seed extraction #
Number of seed words 1 + k * reducedLimbCount (reducedTrinomialStep w₀ b b') (1 word for the
multiply-shift seed α and k * L words for the c-bit limbs of the k polynomial
coefficients).
Number of words reducedTableWords (reducedTrinomialStep w₀ b b') w occupied by the bit-packed
Four-Russians carryless multiplication table.
Total memory footprint seedWords w₀ b b' k + tableWords w w₀ b b' in words.
Base address base + seedWords w₀ b b' k of the bit-packed carryless multiplication table.
Address base + 1 + (m.val + L * j.val) of the m-th limb of polynomial coefficient j.
Bit offset (idx &&& (2 ^ reducedPackShift t w - 1)) * (2 * reducedChunkBits t) of a packed
carryless-multiplication table slot fits below the word width w.
Compact space bound: whenever w₀ ≤ w, b' ≤ 3 * b, b' ≤ w, and 2 ≤ w,
space w w₀ b b' k uses at most 2 + 54 * k + 2 * max 8 (2 ^ (b / 2)) / w words
(O(k) + o(2 ^ b) / w).
Whenever 2 * reducedChunkBits t ≤ w and Fits w (base + space w w₀ b b' k), we have strict
inequality 2 * reducedChunkBits t < w (and hence 2 ^ (2 * reducedChunkBits t) < 2 ^ w).
Deterministic precomputed table invariant at tabBase base w₀ b b' k: every table word is
< 2 ^ (2 ^ p * (2 * c)) and every 2 * c-bit slice idx < 2 ^ (2 * c) stores
clmul (idx >>> c) (idx &&& (2 ^ c - 1)).
Seed representation invariant linking θ : ReducedPolySeed w₀ b b' k to memory at
[base, base + seedWords w₀ b b' k).
Equivalence (Fin 1 → Fin (2 ^ (w₀ - 1))) ≃ ReducedTopSeed w₀.
Equivalence between flat k * L-word slices of c-bit limbs and
ReducedBotSeed w₀ b b' k.
Extract the top-level multiply-shift seed α : ReducedTopSeed w₀ from mem base.
Extract the bottom-level polynomial seed β : ReducedBotSeed w₀ b b' k from
[base + 1, base + 1 + k * L).
Extract the full seed (α, β) : ReducedPolySeed w₀ b b' k from memory.
Extracting a seed from memory satisfying SeedRep recovers the original seed θ.
Register layout on Data 240 #
Reduced-key limb register 20 + m % 54 (0 ≤ m < L ≤ 54).
Horner accumulator limb register 74 + m % 54 (0 ≤ m < L ≤ 54).
Carryless product limb register 128 + d % 108 (0 ≤ d < 2 * L ≤ 108).
TableRep depends only on memory in [base, base + space w w₀ b b' k).
SeedRep depends only on memory in [base, base + space w w₀ b b' k).
Section 2: Bit-packed Four-Russians table construction (initTableCode) #
11-instruction setup before clmulTabLoop on Data 240.
Closed-form state after initTableSetupCode w w₀ b b' k.
29-instruction body of clmulTabLoop on Data 240, populating slice idx &&& (2 ^ p - 1) of
mem[tabBase + (idx >>> p)] with clmul (idx >>> c) (idx &&& (2 ^ c - 1)).
Loop while idx < 2 ^ (2 * c) do clmulTabBody populating the bit-packed carryless
multiplication table.
Full table initialization program on Data 240.
Exact instruction count 31 * 2 ^ (2 * c) + 13 of initTableCode w w₀ b b' k.
Modifying the bit-packed slice (idx >>> p, idx &&& (2 ^ p - 1)) by clearing all bits at or
above sh = (idx &&& (2 ^ p - 1)) * (2 * c) and XORing in val <<< sh (val < 2 ^ (2 * c))
preserves getLimb (2 * c) at every strictly earlier entry j < idx.
Updating word tBase + wIdx to a value below B preserves the < B bound on every word
tBase + w' with w' ≤ wIdx, assuming all strictly earlier words already satisfy < B.
After masking bits at or above slot (idx &&& (2 ^ p - 1)) * (2 * c) of word
tBase + (idx >>> p), reading the packed limb at prevIdx = ((idx >>> c) <<< c) + (v >>> 1)
returns clmul (idx >>> c) ((idx &&& (2 ^ c - 1)) >>> 1) whenever all strictly earlier entries
< idx hold their carryless products.
Deterministic execution of initTableCode w w₀ b b' k: establishes TableRep, preserves
reg 0 = base, and modifies only [tabBase base w₀ b b' k, base + space w w₀ b b' k).
ExecWithin corollary of initTableCode_exec.
Section 3: Exact uniform seed resampling (resampleCode) #
5-instruction setup for the top-seed Tabulation.randFillLoop call on Data 240.
State after resampleSetup1Code w₀.
4-instruction setup for the bottom-seed Tabulation.randFillLoop call on Data 240.
State after resampleSetup2Code w₀ b b' k.
Structured Word-RAM program on Data 240 that resamples both α and β via two sequential
Tabulation.randFillLoop calls.
Exact instruction count 5 * seedWords w₀ b b' k + 13 of resampleCode w₀ b b' k.
Explicit ProbComp (Data 240 × ℕ) induced by resampleCode w₀ b b' k.
Exact ExecRand execution of resampleCode w₀ b b' k.
Pathwise postcondition of resampleComp: executes in resampleSteps w₀ b b' k instructions,
preserves reg 0 = base and TableRep, establishes SeedRep, and modifies only
[base, base + seedWords w₀ b b' k).
Exact product uniformity of resampleComp: extractSeed base w₀ b b' k z.1.mem under
resampleComp base w₀ b b' k x has distribution
PMF.uniformOfFintype (ReducedPolySeed w₀ b b' k).
Ascending sequential composition of n blocks f 0, f 1, ..., f (n - 1).
Part 1a of Stage 1 (12 instructions): loads α = x.mem (x.regs 0), sets half-width masks and
shifts in regs 5..10, and splits α and x' = x.regs 1 &&& (2 ^ m - 1) into regs 12..15.
Part 1b of Stage 1 (18 instructions): combines the half-words in regs 12..15 into
halfMulMod m α x' in reg 3.
Part 1c of Stage 1 (9 instructions): finishes oddMulModWord w₀ α u and shifts right by
w₀ - min w₀ (3 * b) into reg 3.
Stage 1 of evalCode (39 instructions): computes the multiply-shift reduced key
oddMulModWord w₀ α u >>> (w₀ - min w₀ (3 * b)) into reg 3.
Stage 1 execution: computes
oddMulModWord w₀ (x.mem (x.regs 0)) (x.regs 1) >>> (w₀ - min w₀ (3 * b))
into reg 3 in 39 steps, preserving reg 0 and memory.
Step m of Stage 2 (5 instructions): extracts limb m of r_val from reg 3 into xReg m,
shifts reg 3 right by c, and zeroes accReg m.
Stage 2b of evalCode (8 instructions): loads table/chunk constants into regs 4..9 and
reg 18.
Register invariant preserved across all Stage 3 Horner operations.
Invariant combining HornerConsts and the current accumulator limbs in accReg.
Inner step (i, m) (13 instructions): looks up clmul (accLimb i) (xLimb m) in the
bit-packed Four-Russians table and XORs its low and high c-bit halves into prodReg (i + m) and
prodReg (i + m + 1).
Row i of limb-by-limb carryless multiplication.
Full L × L limb-by-limb carryless multiplication into prodReg 0 .. prodReg (2 * L - 1).
Step m < 2 * L₀ of trinomial reduction and seed addition (6 instructions):
loads limb m of coefficient coeffIdx from memory, reduces the product limbs in prodReg
modulo X ^ (2 * 3 ^ t) + X ^ (3 ^ t) + 1, and writes the XOR into accReg m.
Trinomial reduction and seed addition across all 2 * L₀ = L limbs.
Step j < k of Horner polynomial evaluation: multiplies accReg by xReg into prodReg,
reduces modulo the trinomial, and adds coefficient k - 1 - j.
Stage 3 of evalCode: k Horner steps evaluating polynomialHash.
Step count of a single hornerStep t k j.
Step m < L of Stage 4 (5 instructions): masks and shifts accReg m into reg 2 to
reconstruct the low b' bits of the polynomial evaluation.
Stage 4 of evalCode: packs the low b' bits of accReg 0 .. accReg (L - 1) into reg 2.
Complete deterministic Word-RAM hash evaluation program evalCode w w₀ b b' k on Data 240:
reduces reg 1 via Stage 1 (reduceKeyCode), unpacks limbs and sets up Horner constants via
Stage 2 (initLimbsCode, hornerSetupCode), runs k Horner steps via Stage 3 (hornerCode),
and packs the low b' bits into reg 2 via Stage 4 (packOutCode).
Exact instruction count of evalCode w w₀ b b' k.
Parameter-independent O(k) upper bound 47250 * k + 591 on evalSteps w₀ b b' k.
evalSteps w₀ b b' k is O(k) (evalSteps_le):
for all w₀, b, b', k : ℕ, evalSteps w₀ b b' k ≤ evalBound k = 47250 * k + 591.
Deterministic execution of evalCode w w₀ b b' k:
evaluates (reducedPolyHash U w₀ b b' k θ u).val into reg 2 in evalSteps w₀ b b' k
instructions while preserving reg 0 = base and all memory.
ExecWithin wrapper for evalCode_exec.
Section 5: Packaging as HashFamily.WordRAM.Impl #
Package ReducedKWise into a reusable HashFamily.WordRAM.Impl certificate over
Basis.exact on Data 240.
Standard-basis lifting of impl.