Self-contained Word-RAM random tables and tabulated odd-stride generation #
This file provides two self-contained Word-RAM hashing primitives over Cslib.WordRAM.Basis.exact,
built on a shared uniform word-slice sampler (randFillLoop):
RandomTable:resampleCodepopulates an arrayZ[0 .. 2 ^ M - 1]ofm = 2 ^ Mindependent uniformM-bit words in5 * 2 ^ M + 7instructions and2 ^ Mcalls toCode.rand, inducingPMF.uniformOfFintype (Fin (2 ^ M) → Fin (2 ^ M)).lookupCodereadsZ[idx]in2deterministic instructions, andxorLookupCodeevaluates the XOR-shifted hashf(u) ^^^ Z[g(u)]in3deterministic instructions Carter and Wegman; Mitzenmacher and Upfal.TabulatedOddStride:simpleTabsplits the lowd * bbits of an input keyuintodcharacters ofb = max 1 (Nat.log 2 (max n_max 2) / 2)bits (2 ^ b ≤ max 2 n_maxwords per character table) and XORs their table entries (following the simple tabulation scheme of Zobrist 1970 and Pătraşcu and Thorup 2012).initCode dsamples thedcharacter tables viaCode.randin4 * Nat.log 2 (max n_max 2) + 5 * (d * 2 ^ b) + 30instructions (O(d * 2 ^ b + log n_max), which isO(n_max)for fixedd), andevalCode devaluatesoddStride M (simpleTab b d M T u).val = (2 * (simpleTab b d M T u).val + 1) &&& (2 ^ M - 1)in8 * d + 21deterministic instructions (O(d)). WhenM > 0, this strideh₂(u)is odd (atM = 0it is0onFin 1), and for everyMit is coprime to2 ^ M, so for any initial sloth₁(u) : Fin (2 ^ M)the linear probe sequenceprobeSeq M h₁(u) tab i = (h₁(u) + i * h₂(u)) % 2 ^ Mis a bijection onFin (2 ^ M)(bijective_probeSeq) Cormen, Leiserson, Rivest, and Stein, Section 11.4. Only this full-cycle permutation property is proved here: nok-wise or 3-wise independence theorem for simple tabulation is claimed in this module, andsimpleTabinspects only the lowd * bbits ofu.
Main definitions #
Cslib.DataStructure.HashFamily.WordRAM.Tabulation.randFillLoop,randFillComp,extractSlice: generic loop filling a contiguous memory slice with independent uniformbits-bit words.Cslib.DataStructure.HashFamily.WordRAM.Tabulation.RandomTable.resampleCode,lookupCode,xorLookupCode: 1-D random table initialization/resampling andO(1)deterministic lookup.Cslib.DataStructure.HashFamily.WordRAM.Tabulation.TabulatedOddStride.charBits,simpleTab,oddStride,probeSeq,initCode,evalCode:d-character simple tabulation and odd-stride permutation generator on the Word RAM.
Main results #
Cslib.DataStructure.HashFamily.WordRAM.Tabulation.randFillLoop_execRand,map_extractSlice_eval_randFillComp: exact execution and product uniformity ofrandFillLoop.Cslib.DataStructure.HashFamily.WordRAM.Tabulation.RandomTable.resampleCode_execRand,map_extractTable_eval_resampleComp,lookupCode_exec,xorLookupCode_exec: exact execution and uniformity ofRandomTable.Cslib.DataStructure.HashFamily.WordRAM.Tabulation.TabulatedOddStride.bijective_probeSeq,initCode_execRandWithin,evalCode_execWithin: full-cycle permutation property,O(d * 2 ^ b + log n_max)initialization, andO(d)deterministic evaluation ofTabulatedOddStride.
References #
Section 1: Uniform random word-slice filler (randFillLoop) #
Register 0 of randFillLoop: write pointer ptr.
Register 1 of randFillLoop: end pointer arrBase + len.
Register 2 of randFillLoop: random bitmask 2 ^ bits - 1.
Register 3 of randFillLoop: constant 1.
Register 4 of randFillLoop: sampled value scratch register.
Register 5 of randFillLoop: caller-preserved register.
Body of randFillLoop: samples regVal ← rand regMask, stores *regPtr ← regVal, and
advances regPtr ← regPtr + regOne.
Structured Word-RAM loop that fills [regPtr, regEnd) with independent uniform draws in
[0, regMask].
Single-iteration state update of randFillBody when Code.rand draws v.
Explicit ProbComp (Data 6 × ℕ) induced by randFillLoop over len words starting at
arrBase.
One iteration of randFillBody executes in 3 instructions and 1 rand call.
Exact ExecRand semantics of randFillLoop over len words starting at arrBase.
randFillComp bits arrBase len x makes at most len rand calls.
Pathwise postcondition of randFillComp: takes 5 * len + 2 instructions, preserves registers
regEnd, regMask, regOne, regExtra, fills [arrBase, arrBase + len) with < 2 ^ bits
words, and preserves all memory outside [arrBase, arrBase + len).
Uniformity of randFillComp: the slice extractSlice arrBase bits len z.1.mem produced by
randFillComp bits arrBase len x has the exact product uniform distribution
PMF.uniformOfFintype (Fin len → Fin (2 ^ bits)).
Section 2: RandomTable — 1-D random table for Cuckoo Hashing #
Footprint size of RandomTable M: 2 ^ M words at [base, base + 2 ^ M).
Register 0: holds the base memory address base of the table.
Register 1: holds M during resampleCode, or index idx < 2 ^ M during lookupCode.
Alias for register 1 when used as the lookup index idx < 2 ^ M.
Register 2: receives Z[idx] in lookupCode or fx ^^^ Z[idx] in xorLookupCode.
Register 3: scratch register 1.
Register 4: scratch register 2.
Register 5: scratch register 3.
Injective register permutation mapping randFillLoop's registers into RandomTable so that
regExtra maps to regBase (preserving regBase).
Deterministic setup before randFillLoop in resampleCode: computes 2 ^ M, 2 ^ M - 1,
base + 2 ^ M, and initializes the loop registers.
Closed-form machine data after setupCode.
Structured Word-RAM program that initializes or resamples Z[0 .. 2 ^ M - 1] at base using
2 ^ M calls to Code.rand on 2 ^ M - 1.
Alias for resampleCode when used for initial table construction.
Explicit ProbComp (Data 6 × ℕ) induced by resampleCode at (base, M) from x.
setupCode executes deterministically in 5 instructions when Fits w (base + 2 ^ M) and
Fits w (2 ^ M).
Exact ExecRand execution of RandomTable.resampleCode: runs in 5 * 2 ^ M + 7
instructions and 2 ^ M rand calls.
Uniformity of RandomTable.resampleComp: the extracted table extractTable base M z.1.mem
is distributed according to PMF.uniformOfFintype (Fin (2 ^ M) → Fin (2 ^ M)).
Hoare-style ExecRandWithin specification for RandomTable.resampleCode: runs in
5 * 2 ^ M + 7 steps and 2 ^ M rand calls, establishing Rep, preserving regBase, and
modifying only [base, base + 2 ^ M).
Structured Word-RAM program that reads Z[regIdx] at regBase + regIdx into regOut in 2
deterministic instructions.
Final machine data after lookupCode.
lookupCode executes in 2 deterministic instructions, preserving memory and regBase and
writing (Z idx).val into regOut.
Structured Word-RAM program that computes regOut ← regOut ^^^ Z[regIdx] in 3 deterministic
instructions (computing f(u) ^^^ Z[g(u)] when regOut holds f(u) and regIdx holds
g(u)).
Final machine data after xorLookupCode.
xorLookupCode executes in 3 deterministic instructions, preserving memory and regBase and
writing fx.val ^^^ (Z idx).val < 2 ^ M into regOut.
Section 3: TabulatedOddStride #
Character bit-width b = max 1 (⌊log₂ (max n_max 2)⌋ / 2) for d-character simple tabulation,
ensuring 1 ≤ b and 2 ^ b ≤ max 2 n_max (2 ^ (2 * b) ≤ max 4 n_max).
Because oddStride M tab is coprime to 2 ^ M, probeSeq M h₁ tab is a full-cycle
bijection (permutation) of Fin (2 ^ M) for every M, initial probe h₁, and raw table sum
tab.
Tail-recursive d-character simple tabulation accumulator from character offset j across
rem remaining characters: XORs T[(j + k) * 2 ^ b + ((u >>> (k * b)) &&& (2 ^ b - 1))] into
acc.
Memory footprint 4 + d * 2 ^ b words for a d-character simple tabulation table with
character bit-width b (4 header words (b, 2 ^ b, 2 ^ b - 1, 2 ^ M - 1) followed by
d * 2 ^ b random < 2 ^ M words).
Memory footprint 4 + d * 2 ^ (charBits n_max) words at capacity parameter n_max.
Memory representation predicate for TabulatedOddStride at [base, base + 4 + d * 2 ^ b):
header words b, 2 ^ b, 2 ^ b - 1, 2 ^ M - 1 at base + 0 .. base + 3, and table entries
(T i).val at base + 4 + i.val for i : Fin (d * 2 ^ b).
Register 0 of TabulatedOddStride: base memory address base.
Register 3 of TabulatedOddStride: table pointer scratch register.
Register 4 of TabulatedOddStride: end pointer / mask scratch register.
Register 5 of TabulatedOddStride: character bit-width b / loop bound register.
Register 6 of TabulatedOddStride: character alphabet size 2 ^ b / sampled value
register.
Register 7 of TabulatedOddStride: character mask 2 ^ b - 1 register.
Register 8 of TabulatedOddStride: general scratch register.
Register injection Fin 6 → Fin 9 mapping randFillLoop's (ptr, end, mask, one, val, extra)
to (regPtr, regB, regEnd, regTmp, regPowB, regBase).
Structured Word-RAM program that computes regB ← charBits n_max
(= max 1 (⌊log₂ (max n_max 2)⌋ / 2)) at runtime from n_max in regKey, using regPtr,
regB, regTmp as scratch registers and preserving regBase, regOut, and all memory.
State after charBitsCode starting from n_max in regKey.
charBitsCode executes deterministically in 4 * Nat.log 2 (max n_max 2) + 12 instructions,
setting regB = charBits n_max while preserving regBase, regOut, and all memory.
Deterministic 16-instruction setup prefix of initFromBCode d: writes the 4 header words
(b, 2 ^ b, 2 ^ b - 1, 2 ^ M - 1) to base + 0 .. base + 3 and sets registers for
randFillLoop.rename fillMap9 on [base + 4, base + 4 + d * 2 ^ b).
State after setupFromBCode d.
setupFromBCode d executes in 16 deterministic steps.
Structured Word-RAM program that initializes a d-character simple tabulation table from
(base, M, b) in (regBase, regOut, regB).
Explicit ProbComp (Data 9 × ℕ) produced by initFromBCode d.
Exact ExecRand semantics of initFromBCode d.
The character table extractTable base b d M z.1.mem produced by initFromBComp is uniformly
distributed over Fin (d * 2 ^ b) → Fin (2 ^ M).
Pathwise postcondition of initFromBComp: runs in 5 * (d * 2 ^ b) + 18 instructions and
establishes Rep base b d M (extractTable base b d M z.1.mem) z.1.mem, z.1.regs regBase = base,
and frame preservation outside [base, base + spaceFromB b d).
Hoare-style ExecRandWithin specification for initFromBCode d.
Explicit ProbComp (Data 9 × ℕ) produced by initCode d from (base, n_max, M).
Exact ExecRand semantics of initCode d.
Uniformity of TabulatedOddStride.initComp: the character table extracted from final
memory has the exact uniform distribution over Fin (d * 2 ^ (charBits n_max)) → Fin (2 ^ M).
Hoare-style ExecRandWithin specification for TabulatedOddStride.initCode d.
State after evalSetupCode d.
evalSetupCode d executes in 12 deterministic steps when Rep base b d M T x.mem holds.
Loop over the d character tables [base + 4, base + 4 + d * 2 ^ b).
tabLoop executes deterministically in 8 * rem + 2 instructions from character offset j
with j + rem = d, accumulating (simpleTabAux b d M T rem j u acc).val into regOut and
preserving regBase and all memory.
7-instruction finishing suffix of evalCode d: loads 2 ^ M - 1 from base + 3 and maps
acc in regOut to (2 * acc + 1) &&& (2 ^ M - 1).
Complete d-character simple tabulation odd-stride evaluation program evalCode d.
TabulatedOddStride.evalCode d correctness and complexity: for any character table
T : Fin (d * 2 ^ b) → Fin (2 ^ M) represented at base and key u with Fits w u,
evalCode d halts deterministically in 8 * d + 21 = O(d) instructions, preserving regBase
and all memory and writing (tabulatedOddStride b d M T u).val into regOut.