Self-Contained Word-RAM Double Tabulation and High-Independence Hashing ([Thorup2013]) #
This module provides a reusable Word-RAM implementation of Mikkel Thorup's double tabulation hash
family (Cslib.Probability.doubleTabHash c d b U M, [Tho13]) over Cslib.WordRAM.Basis.exact
and lifts it to Cslib.WordRAM.Basis.standard:
- Flat multi-word memory layout (
space c d b = (c + 1) * d * 2 ^ b):- The top-level table
T₁ : DoubleTabTopSeed c d b(Fin c → Fin d → Fin (2 ^ b) → Fin (2 ^ b)) occupies[base, base + c * d * 2 ^ b), storing(T₁ i j ch).valattopAddr base c b i j ch = base + (j.val * c + i.val) * 2 ^ b + ch.val(b-bit words, never packing an intermediated-tupleΨ ^ dinto a single machine word). - The bottom-level table
T₂ : DoubleTabBotSeed d b M(Fin d → Fin (2 ^ b) → Fin (2 ^ M)) occupies[base + c * d * 2 ^ b, base + (c + 1) * d * 2 ^ b), storing(T₂ j y).valatbotAddr base c d b j y = base + c * d * 2 ^ b + j.val * 2 ^ b + y.val.
- The top-level table
- Exact uniform seed resampling (
resampleCode c d b M):- Executes two sequential calls to
Tabulation.randFillLoopinresampleSteps c d b = 5 * space c d b + 13instructions (O((c + 1) * d * 2 ^ b)), sampling(T₁, T₂)from the exact uniform distributionPMF.uniformOfFintype (DoubleTabTopSeed c d b × DoubleTabBotSeed d b M).
- Executes two sequential calls to
- Register-by-register deterministic query evaluation (
evalCode c d b):- For each output coordinate
j = 0 .. d - 1, computes the intermediateb-bit charactery_j = ⨁_{i < c} T₁[i, j, x_i]via an inner loop overi = 0 .. c - 1, then looks upT₂[j, y_j]and XORs it into the output accumulator inevalSteps c d = (12 * c + 11) * d + 10deterministic instructions (O(c * d)), without modifying memory and matching(doubleTabHash c d b U M T₁ T₂ u).val.
- For each output coordinate
- Reusable
HashFamily.WordRAM.ImplandObliviousSequenceImplcertificates:implandobliviousSequenceImplpackagedoubleTabHashas a Word-RAM hash family realizingDataStructure.HashFamily.whpKWiseSpec k U (2 ^ M) δwith failure probabilityδ = doubleTabFailureProb c d b k, bounded ford = 8 * cbycard_not_isKUnique_eight_mul_lewhen1 ≤ cand2 * doubleTabCodeBound c (8 * c) 4 k ≤ 2 ^ (3 * b).provableImplandprovableObliviousSequenceImplcombineKWise(trinomialPolyHashwithδ = 0forn_max < doubleTabThreshold c_exp) andDoubleTabulation(c = 2 * max c_exp 1,d = 8 * c,b = (1 + Nat.clog 2 (max n_max 4)) / 2andδ = doubleTabFailureProb c d b k, bounded under the condition onkabove, fordoubleTabThreshold c_exp ≤ n_max). They useO_{c_exp, k}(n_max + 1)words of space for everyn_max : ℕand, wheneverU ≤ max n_max 4 ^ c_expandM ≤ max c_exp 1 * Nat.clog 2 (max n_max 4) + 5, evaluate withinprovableEvalBound c_exp kinstructions, independent ofn_max,U, andM.
Main definitions #
Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.space,Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.topAddr,Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.botAddr,Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.TopRep,Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.BotRep,Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.SeedRep: memory footprint and representation predicates for(T₁, T₂).Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.resampleCode,Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.resampleSteps,Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.evalCode,Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.evalSteps: Word-RAM resampling and query evaluation programs and step counts onData 12.Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.impl,Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.obliviousSequenceImpl,Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.provableImpl,Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.provableObliviousSequenceImpl:HashFamily.WordRAM.ImplandObliviousSequenceImplcertificates overBasis.exactandBasis.standard.
Main results #
Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.extractSeed_eq_of_seedRep: seed extraction recovers(T₁, T₂)wheneverSeedRepholds.Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.resampleCode_execRand,Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.map_extractSeed_eval_resampleComp: exactExecRandexecution and product uniformity ofresampleCode.Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.evalCode_exec,Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.evalCode_execWithin: deterministicO(c * d)evaluation ofdoubleTabHash c d b U M T₁ T₂ u.Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.doubleTabFailureProb_eight_mul_le: explicit high-probabilityk-uniqueness failure bound ford = 8 * cwhen1 ≤ cand2 * doubleTabCodeBound c (8 * c) 4 k ≤ 2 ^ (3 * b).Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.provableImpl_evalSteps_le,Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.provableSpace_le: uniformO_{c_exp, k}(1)evaluation time andO_{c_exp, k}(n_max + 1)space bounds across alln_max : ℕ, the time bound assumingU ≤ max n_max 4 ^ c_expandM ≤ max c_exp 1 * Nat.clog 2 (max n_max 4) + 5.Cslib.DataStructure.HashFamily.WordRAM.DoubleTabulation.provableImpl_evalSteps_of_le:k-independent evaluation timeevalSteps (provableC c_exp) (provableD c_exp)oncedoubleTabThreshold c_exp ≤ n_max.
References #
Section 1: Flat memory layout, seed representations, and slice equivalences #
Number of machine words c * d * 2 ^ b storing the top-level table T₁.
Number of machine words d * 2 ^ b storing the bottom-level table T₂.
Total contiguous memory footprint (c + 1) * d * 2 ^ b words for Double Tabulation at
[base, base + space c d b).
Memory representation predicate for the top-level table T₁ : DoubleTabTopSeed c d b.
Memory representation predicate for the bottom-level table T₂ : DoubleTabBotSeed d b M.
Combined memory representation predicate for (T₁, T₂) at [base, base + space c d b).
Equivalence between flat (d * 2 ^ b)-word slices and DoubleTabBotSeed d b M.
Extract the top-level seed T₁ : DoubleTabTopSeed c d b from [base, base + c * d * 2 ^ b).
Extract the bottom-level seed T₂ : DoubleTabBotSeed d b M from
[base + c * d * 2 ^ b, base + (c + 1) * d * 2 ^ b).
Extract the pair (T₁, T₂) from [base, base + space c d b).
When SeedRep base c d b M θ mem holds, extractSeed base c d b M mem recovers the seed pair
θ.
Section 2: Register layout and randomized seed resampling (resampleCode) #
Register 0: persistent base address base.
Register 1: input key u.val in evalCode.
Register 2: output accumulator acc in evalCode.
Register 3: top-table pointer / randFillLoop write pointer.
Register 4: bottom-table pointer / randFillLoop end pointer.
Register 5: bottom-table end pointer / randFillLoop bitmask.
Register 6: remaining inner-loop count remC / randFillLoop constant 1.
Register 7: shifted key curU / randFillLoop sampled value.
Register 8: inner XOR accumulator y_j.
Register 9: general scratch register for masks, addresses, and loaded entries.
Register 10: character alphabet size 2 ^ b.
Register 11: constant 0.
Setup program before the first randFillLoop call (5 instructions): sets reg 3 ← base,
reg 4 ← base + c * d * 2 ^ b, reg 5 ← 2 ^ b - 1, and reg 6 ← 1.
State after resampleSetup1Code c d b from x.
Setup program before the second randFillLoop call (4 instructions): sets
reg 3 ← base + c * d * 2 ^ b, reg 4 ← base + c * d * 2 ^ b + d * 2 ^ b, and
reg 5 ← 2 ^ M - 1.
State after resampleSetup2Code d b M from x.
Structured Word-RAM program on Data 12 that resamples both T₁ and T₂ via two sequential
Tabulation.randFillLoop calls.
Exact instruction count 5 * space c d b + 13 of resampleCode c d b M.
Explicit ProbComp (Data 12 × ℕ) induced by resampleCode c d b M.
Exact ExecRand execution of resampleCode c d b M.
Pathwise postcondition of resampleComp: executes in resampleSteps c d b instructions,
preserves reg 0 = base, establishes SeedRep base c d b M (extractSeed base c d b M z.1.mem),
and modifies only [base, base + space c d b).
Exact product uniformity of resampleComp: extractSeed base c d b M z.1.mem under
resampleComp base c d b M x has distribution
PMF.uniformOfFintype (DoubleTabTopSeed c d b × DoubleTabBotSeed d b M).
Setup before outerLoop c b (8 instructions): initializes reg 2 ← 0, reg 3 ← base,
reg 4 ← base + c * d * 2 ^ b, reg 5 ← base + c * d * 2 ^ b + d * 2 ^ b, reg 10 ← 2 ^ b, and
reg 11 ← 0.
State after evalSetupCode c d b from x.
Body of the inner loop over i = 0 .. c - 1 (10 instructions): extracts ch = reg 7 &&& (2 ^ b - 1), loads mem[reg 3 + ch], XORs into reg 8, shifts reg 7 right by b, advances
reg 3 by 2 ^ b (reg 10), and decrements reg 6.
Inner loop computing y_j = ⨁_{i < c} T₁[i, j, x_i] into reg 8.
Body of the outer loop over j = 0 .. d - 1: initializes reg 7 ← reg 1 (u.val),
reg 8 ← 0, reg 6 ← c, runs innerLoop b, loads T₂[j, y_j] from mem[reg 4 + reg 8], XORs
into reg 2, and advances reg 4 by 2 ^ b (reg 10).
Outer loop over j = 0 .. d - 1 (reg 4 < reg 5).
Complete deterministic Word-RAM evaluation program for Double Tabulation on Data 12.
Exact instruction count (12 * c + 11) * d + 10 of evalCode c d b.
Exact deterministic Exec correctness and complexity of evalCode c d b:
from SeedRep base c d b M (T₁, T₂) x.mem, x.regs 0 = base, and x.regs 1 = u.val,
evalCode c d b halts in evalSteps c d = (12 * c + 11) * d + 10 instructions with
y.regs 2 = (doubleTabHash c d b U M T₁ T₂ u).val, preserving y.regs 0 = base,
y.regs 1 = u.val, and y.mem = x.mem.
ExecWithin corollary of evalCode_exec, also recording y.Bounded w when x.Bounded w.
Section 4: Impl and ObliviousSequenceImpl certificates for Double Tabulation #
Package Double Tabulation on Data 12 into a Cslib.DataStructure.HashFamily.WordRAM.Impl
over Basis.exact with 0 initialization parameters.
Lift impl to Basis.standard.
Finset of k-unique top-level tables T₁ : DoubleTabTopSeed c d b.
Top-seed failure probability
#{T₁ | ¬ IsKUnique k (simpleTabTuple T₁)} / |DoubleTabTopSeed c d b| for doubleTabHash.
Explicit failure-probability bound for doubleTabFailureProb c (8 * c) b k
([Thorup2013], Theorem 1(a), p. 4):
when 1 ≤ c and 2 * doubleTabCodeBound c (8 * c) 4 k ≤ 2 ^ (3 * b),
doubleTabFailureProb c (8 * c) b k ≤ 2 * (doubleTabCodeBound c (8 * c) 4 k) ^ 2 / 2 ^ (6 * b).
Canonical ObliviousSequenceImpl certificate for impl realizing
DataStructure.HashFamily.whpKWiseSpec k U (2 ^ M) (doubleTabFailureProb c d b k) when
U ≤ 2 ^ (c * b).
Standard-basis lifting of obliviousSequenceImpl.
Section 5: All-n_max regime-switched provableImpl and #
Number of characters c = 2 * max c_exp 1 for polynomial universe U ≤ max n_max 4 ^ c_exp.
Intermediate tuple length d = 8 * provableC c_exp = 16 * max c_exp 1 (q = 4).
Character bit-width b = (1 + Nat.clog 2 (max n_max 4)) / 2 at capacity n_max.
The 2 ^ provableB n_max cells of one character table fit in the whole Double Tabulation
footprint space (provableC c_exp) (provableD c_exp) (provableB n_max).
Package KWise (CslibFrontier/DataStructure/HashFamily/WordRAM/KWise.lean) into an Impl on
Data 18 with 3 initialization parameters ![n_max, U, M].
Register embedding Fin 12 → Fin 18 via Fin.castLE.
Lift impl from Data 12 to Data 18 with 3 initialization parameters params so it
shares the exact register signature of kWiseImpl.
Footprint size of provableImpl c_exp k n_max U M:
KWise.space n_max k when n_max < doubleTabThreshold c_exp, and
space (provableC c_exp) (provableD c_exp) (provableB n_max) otherwise.
Failure probability δ of provableObliviousSequenceImpl c_exp k n_max U M:
0 ((((Finset.univ \ {()}).card : ℝ≥0∞) / Fintype.card Unit)) when
n_max < doubleTabThreshold c_exp, and
doubleTabFailureProb (provableC c_exp) (provableD c_exp) (provableB n_max) k otherwise. The
latter is bounded by doubleTabFailureProb_eight_mul_le when
2 * doubleTabCodeBound (provableC c_exp) (provableD c_exp) 4 k ≤ 2 ^ (3 * provableB n_max).
All-n_max regime-switched Impl (provableImpl):
uses kWiseImpl for n_max < doubleTabThreshold c_exp and impl18
with c = provableC c_exp, d = provableD c_exp, b = provableB n_max for
doubleTabThreshold c_exp ≤ n_max.
Standard-basis lifting of provableImpl.
All-n_max regime-switched ObliviousSequenceImpl (provableObliviousSequenceImpl)
realizing DataStructure.HashFamily.whpKWiseSpec k U (2 ^ M) (provableFailureProb c_exp k n_max)
whenever U ≤ max n_max 4 ^ c_exp.
Standard-basis lifting of provableObliviousSequenceImpl.
Explicit O_{c_exp, k}(1) space factor 16 + k + 16 * (provableC c_exp + 1) * provableD c_exp
bounding provableSpace c_exp k n_max by provableSpaceFactor c_exp k * (n_max + 1) across every
n_max : ℕ.
All-n_max linear space bound for provableSpace (O_{c_exp, k}(n_max + 1)):
for every c_exp, k, n_max : ℕ,
provableSpace c_exp k n_max ≤ provableSpaceFactor c_exp k * (n_max + 1).
Uniform O(c_exp) upper bound 21 * max c_exp 1 + 16 on KWise.limbCount n_max U M when
U ≤ max n_max 4 ^ c_exp and M ≤ max c_exp 1 * Nat.clog 2 (max n_max 4) + 5.
Uniform O_{c_exp, k}(1) evaluation instruction bound across all n_max : ℕ.
All-n_max constant evaluation time bound for provableImpl (O_{c_exp, k}(1)):
whenever U ≤ max n_max 4 ^ c_exp and M ≤ max c_exp 1 * Nat.clog 2 (max n_max 4) + 5,
(provableImpl ...).evalSteps ≤ provableEvalBound c_exp k, completely independent of
n_max, U, and M.
k-independent evaluation time above the threshold: once
doubleTabThreshold c_exp ≤ n_max, provableImpl evaluates by Double Tabulation in exactly
evalSteps (provableC c_exp) (provableD c_exp) instructions, whatever the independence parameter
k, capacity n_max, universe U and output width M.