Documentation

CslibFrontier.DataStructure.Dictionary.WordRAM.CuckooHashing.Evict

Word-RAM Cuckoo Hashing: Memory Layout, Lookup, and Bounded Eviction #

This module defines the contiguous Word-RAM memory layout, representation invariants, allocation and initialization programs, deterministic two-probe lookup (probeKeyCode, queryCode), and bounded two-table cuckoo displacement loop (evictLoopCode) for Pagh and Rodler's cuckoo hashing dictionary [PR04] backed by the regime-switched hash family from CslibFrontier.DataStructure.Dictionary.WordRAM.CuckooHashing.HashFamily [Tho13].

Main definitions #

Main results #

References #

Section 1: Memory layout, slot encoding, and representation invariants #

Base address of the hash pair region [base + 20, t1Base base c_exp n_max).

Base address of the first cuckoo sub-table T₁ of size tableSize n_max.

Base address of the second cuckoo sub-table T₂ of size tableSize n_max.

Base address of the insertion-order key array insKeys[0 .. n_max - 1].

Base address of the insertion-order value array insVals[0 .. n_max - 1].

Total contiguous Word-RAM memory footprint [base, base + space c_exp n_max) in words.

Constant factor 20 + hashPairSpaceFactor c_exp + 514 bounding space c_exp n_max by O_{c_exp}(n_max + 1) across all n_max : ℕ.

theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.space_le (c_exp n_max : ℕ) :
space c_exp n_max ≤ spaceFactor c_exp * (n_max + 1)

Total memory footprint space c_exp n_max is O_{c_exp}(n_max + 1): at most spaceFactor c_exp * (n_max + 1) words for every n_max : ℕ.

Encode an optional SlotEntry U as a single machine word: 0 for none and e.idx + 1 for some e.

Memory representation of the two cuckoo sub-tables T₁ (at t1Addr) and T₂ (at t2Addr).

Static header constants stored at base + 0 .. 4 and base + 6 .. 10.

def Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.KeysValsRep (base c_exp n_max U V : ℕ) (ks : List (Fin U)) (s : State U) (mem : ℕ → ℕ) :

Representation of the insertion-order arrays insKeys and insVals at keysBase and valsBase realizing abstract dictionary state s : Dictionary.State U.

theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.KeysValsRep.card_dom_eq {base c_exp n_max U V : ℕ} {ks : List (Fin U)} {s : State U} {mem : ℕ → ℕ} (h : KeysValsRep base c_exp n_max U V ks s mem) :
(dom s).card = ks.length
def Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.RepWithKeys (w base c_exp n_max U V : ℕ) (ks : List (Fin U)) (s : State U) (x : WordRAM.Data 19) :

Insertion-order-indexed representation invariant RepWithKeys w base c_exp n_max U V ks s x for cuckoo hashing: stores the static header, the hash pair θ_mem = extractSeed ... x.mem, the insertion-order key-value arrays ks for s, and a valid table placement tbl obtained by replaying ks under θ_mem.

Representation invariant Rep w base c_exp n_max U V s x for cuckoo hashing.

theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.RepWithKeys.toRep {w base c_exp n_max U V : ℕ} {ks : List (Fin U)} {s : State U} {x : WordRAM.Data 19} (hrep : RepWithKeys w base c_exp n_max U V ks s x) :
Rep w base c_exp n_max U V s x
theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.Rep.bounded {w base c_exp n_max U V : ℕ} {s : State U} {x : WordRAM.Data 19} (h : Rep w base c_exp n_max U V s x) :
theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.Rep.unique {w base c_exp n_max U V : ℕ} {s₁ s₂ : State U} {x : WordRAM.Data 19} (h₁ : Rep w base c_exp n_max U V s₁ x) (h₂ : Rep w base c_exp n_max U V s₂ x) :
s₁ = s₂

A machine state x represents at most one abstract dictionary state s.

theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.RepWithKeys.supported {w base c_exp n_max U V : ℕ} {ks : List (Fin U)} {s : State U} {x x' : WordRAM.Data 19} (hreg0 : x'.regs 0 = x.regs 0) (hmem : ∀ a ∈ { base := base, length := space c_exp n_max }.footprint, x'.mem a = x.mem a) (hbdd : WordRAM.Data.Bounded w x') (hrep : RepWithKeys w base c_exp n_max U V ks s x) :
RepWithKeys w base c_exp n_max U V ks s x'
theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.Rep.supported {w base c_exp n_max U V : ℕ} {s : State U} {x x' : WordRAM.Data 19} (hreg0 : x'.regs 0 = x.regs 0) (hmem : ∀ a ∈ { base := base, length := space c_exp n_max }.footprint, x'.mem a = x.mem a) (hbdd : WordRAM.Data.Bounded w x') (hrep : Rep w base c_exp n_max U V s x) :
Rep w base c_exp n_max U V s x'

Rep depends only on reg 0 and the owned memory block [base, base + space c_exp n_max).

Section 2: allocSpan and initSetup programs and specifications #

allocSpan program: sets reg 0 = base and advances reg 1 (frontierReg) to base + space c_exp n_max.

theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.allocSpan_execWithin {w base c_exp n_max U : ℕ} {x : WordRAM.Data 19} (hfr : x.regs 1 = base) (h3 : x.regs 3 = n_max) (h4 : x.regs 4 = U) (hfit_end : WordRAM.Fits w (base + space c_exp n_max)) (hfit_ev : WordRAM.Fits w (CuckooHashing.maxEvictions n_max)) :
∃ (x₁ : WordRAM.Data 19), WordRAM.ExecWithin w (allocSpan c_exp n_max) x x₁ spanSteps ∧ x₁.mem = x.mem ∧ x₁.regs 0 = base ∧ x₁.regs 1 = base + space c_exp n_max ∧ x₁.regs 3 = n_max ∧ x₁.regs 4 = U

Complete initSetup program for cuckoo hashing: populates the header, initializes the static hash tables, samples the initial cuckoo hash pair (h₁, h₂), and restores base and frontierReg.

Couples a state distribution μ : PMF (Data 19) with its extracted in-memory seed.

@[simp]
theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.map_fst_coupleExtractedSeed (base c_exp n_max U : ℕ) (μ : PMF (WordRAM.Data 19)) :
PMF.map Prod.fst (coupleExtractedSeed base c_exp n_max U μ) = PMF.map (fun (y : WordRAM.Data 19) => extractSeed (hashBase base) c_exp n_max U (CuckooHashing.tableBits n_max) y.mem) μ
theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.mem_support_coupleExtractedSeed {base c_exp n_max U : ℕ} {μ : PMF (WordRAM.Data 19)} {p : Seed c_exp n_max U (CuckooHashing.tableBits n_max) × WordRAM.Data 19} (hp : p ∈ (coupleExtractedSeed base c_exp n_max U μ).support) :
p.1 = extractSeed (hashBase base) c_exp n_max U (CuckooHashing.tableBits n_max) p.2.mem ∧ p.2 ∈ μ.support
theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.initSetup_spec {w base c_exp n_max U V : ℕ} {x₁ : WordRAM.Data 19} (hx₁ : WordRAM.Data.Bounded w x₁) (hb : x₁.regs 0 = base) (hfr : x₁.regs 1 = base + space c_exp n_max) (h3 : x₁.regs 3 = n_max) (h4 : x₁.regs 4 = U) (hz : ∀ a ∈ { base := base, length := space c_exp n_max }.footprint, x₁.mem a = 0) (hfit_ev : WordRAM.Fits w (CuckooHashing.maxEvictions n_max)) (hfit_U : WordRAM.Fits w (max U 2 + 1)) (hfit_q : WordRAM.Fits w (2 ^ Probability.trinomialDeg U (CuckooHashing.tableBits n_max))) :
∃ (ν₀ : PMF (Seed c_exp n_max U (CuckooHashing.tableBits n_max) × WordRAM.Data 19)), WordRAM.ExecCodeDist w (initSetup c_exp n_max) x₁ (PMF.map Prod.snd ν₀) ↑(setupSteps c_exp n_max U) ∧ PMF.map Prod.fst ν₀ = seedLaw c_exp n_max U (CuckooHashing.tableBits n_max) ∧ ∀ p ∈ ν₀.support, p.1 = extractSeed (hashBase base) c_exp n_max U (CuckooHashing.tableBits n_max) p.2.mem ∧ p.2.regs 1 = base + space c_exp n_max ∧ RepWithKeys w base c_exp n_max U V [] (empty U) p.2 ∧ WordRAM.Modifies { base := base, length := space c_exp n_max }.footprint x₁.mem p.2.mem

Coupled distributional specification for initSetup, directly returning the seed-coupled initial distribution ν₀.

Section 3: Base-shifted hash wrappers, probeKeyCode, and queryCode #

Encode Option ℕ as a single machine word (0 for none and v + 1 for some v).

Decode a single machine word into Option ℕ (0 as none and v + 1 as some v).

Resamples (h₁, h₂) with reg 0 = base and restores reg 0 = base.

theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.resampleAtBaseCode_execCodeDist {w base c_exp n_max U : ℕ} {x : WordRAM.Data 19} (htab : HashPairTableRep (hashBase base) c_exp n_max U (CuckooHashing.tableBits n_max) x.mem) (h0 : x.regs 0 = base) (hfit_end : WordRAM.Fits w (base + space c_exp n_max)) (hfit_q : WordRAM.Fits w (2 ^ Probability.trinomialDeg U (CuckooHashing.tableBits n_max))) :
∃ (μ_res : PMF (WordRAM.Data 19)), WordRAM.ExecCodeDist w (resampleAtBaseCode c_exp n_max) x μ_res ↑(resampleAtBaseSteps c_exp n_max) ∧ PMF.map (fun (y : WordRAM.Data 19) => extractSeed (hashBase base) c_exp n_max U (CuckooHashing.tableBits n_max) y.mem) μ_res = seedLaw c_exp n_max U (CuckooHashing.tableBits n_max) ∧ ∀ y ∈ μ_res.support, y.regs 0 = base ∧ HashPairRep (hashBase base) c_exp n_max U (CuckooHashing.tableBits n_max) (extractSeed (hashBase base) c_exp n_max U (CuckooHashing.tableBits n_max) y.mem) y.mem ∧ ∀ (a : ℕ), a < hashBase base ∨ hashBase base + hashPairSpace c_exp n_max ≤ a → y.mem a = x.mem a

Distributional execution specification for resampleAtBaseCode.

Helper subprogram that loads mem[base + off] into reg dst using reg 15 as scratch.

theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.loadHeaderCode_exec {w base off : ℕ} (dst : Fin 19) {x : WordRAM.Data 19} (h0 : x.regs 0 = base) (hfit_off : WordRAM.Fits w off) (hfit_addr : WordRAM.Fits w (base + off)) :
WordRAM.Exec w (loadHeaderCode dst off) x (((x.set 15 off).set dst (base + off)).set dst (x.mem (base + off))) 3

Helper subprogram that stores reg src into mem[base + off] using reg 15, 16 as scratch.

theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.storeHeaderCode_exec {w base off : ℕ} (src : Fin 19) {x : WordRAM.Data 19} (h0 : x.regs 0 = base) (hne15 : src ≠ 15) (hne16 : src ≠ 16) (hfit_off : WordRAM.Fits w off) (hfit_addr : WordRAM.Fits w (base + off)) :
WordRAM.Exec w (storeHeaderCode off src) x (((x.set 15 off).set 16 (base + off)).write (base + off) (x.regs src)) 3

Helper subprogram that stores constant val into mem[base + off] using reg 15, 16, 18 as scratch.

Branches on 0 < mem[base + off] using reg 15, 16 as scratch.

Every entry in tbl comes from ks at its recorded index e.idx.

theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.pureReplay_tableIdxValid {U m : ℕ} {h₁ h₂ : Fin U → Fin m} {maxLoop : ℕ} {ks : List (Fin U)} {tbl : CuckooHashing.PureTable U m} (hnodup : ks.Nodup) (hrep : CuckooHashing.pureReplay h₁ h₂ maxLoop ks = some tbl) :

Two-probe cuckoo lookup (probeKeyCode): probes T₁[h₁(u)], and if u is not found in T₁, probes T₂[h₂(u)]. Stores foundFlag ∈ {0, 1} in mem[base + 13] and foundIdx in mem[base + 14].

theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.probeKeyCode_execWithin {w base c_exp n_max U : ℕ} {θ : Seed c_exp n_max U (CuckooHashing.tableBits n_max)} {ks : List (Fin U)} {tbl : CuckooHashing.PureTable U (2 ^ CuckooHashing.tableBits n_max)} {u : Fin U} {x : WordRAM.Data 19} (hhdr : HeaderConstsRep base c_exp n_max U x.mem) (hrep_hash : HashPairRep (hashBase base) c_exp n_max U (CuckooHashing.tableBits n_max) θ x.mem) (hslots : TableSlotsRep (t1Base base c_exp n_max) (t2Base base c_exp n_max) tbl x.mem) (htiv : TableIdxValid ks tbl) (hlen_le : ks.length ≤ n_max) (hmem_keys : ∀ (idx : ℕ) (hlt : idx < ks.length), x.mem (keysBase base c_exp n_max + idx) = ↑ks[idx]) (h0 : x.regs 0 = base) (hm11 : x.mem (base + 11) = ↑u) (hw : 2 * Probability.trinomialDeg U (CuckooHashing.tableBits n_max) ≤ w) (hfit_end : WordRAM.Fits w (base + space c_exp n_max)) :
∃ (y : WordRAM.Data 19), WordRAM.ExecWithin w (probeKeyCode c_exp n_max) x y (probeKeySteps c_exp n_max U) ∧ y.regs 0 = base ∧ (match CuckooHashing.lookupEntry (evalHash1 θ) (evalHash2 θ) tbl u with | some e => y.mem (base + 13) = 1 ∧ y.mem (base + 14) = e.idx | none => y.mem (base + 13) = 0) ∧ ∀ (a : ℕ), a ≠ base + 13 → a ≠ base + 14 → y.mem a = x.mem a

Lookup operation (queryCode): stores u.val from reg 3 into mem[base + 11], runs probeKeyCode, and writes encodeOptionNat (s u) into reg 2.

theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.queryCode_execWithin {w base c_exp n_max U V : ℕ} {ks : List (Fin U)} {s : State U} {u : Fin U} {x : WordRAM.Data 19} (hrep : RepWithKeys w base c_exp n_max U V ks s x) (h3 : x.regs 3 = ↑u) (hw : 2 * Probability.trinomialDeg U (CuckooHashing.tableBits n_max) ≤ w) (hfit_end : WordRAM.Fits w (base + space c_exp n_max)) (hfit_V : WordRAM.Fits w V) :
∃ (y : WordRAM.Data 19), WordRAM.ExecWithin w (queryCode c_exp n_max) x y (querySteps c_exp n_max U) ∧ y.regs 2 = encodeOptionNat (s u) ∧ RepWithKeys w base c_exp n_max U V ks s y ∧ extractSeed (hashBase base) c_exp n_max U (CuckooHashing.tableBits n_max) y.mem = extractSeed (hashBase base) c_exp n_max U (CuckooHashing.tableBits n_max) x.mem ∧ WordRAM.Modifies { base := base, length := space c_exp n_max }.footprint x.mem y.mem

Section 4: Bounded cuckoo eviction loop (putTableSlotCode, evictLoopCode) #

Bounded cuckoo eviction loop (evictLoopCode): executes up to remFuel = mem[base + 15] displacement rounds starting from (curKey = mem[base + 11], curIdx = mem[base + 12]) with activeFlag = mem[base + 16] = 1.

Step bound per displacement round in evictLoopCode.

theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.evictLoopCode_execWithin {w base c_exp n_max U fuel : ℕ} {θ : Seed c_exp n_max U (CuckooHashing.tableBits n_max)} {ks : List (Fin U)} {tbl : CuckooHashing.PureTable U (2 ^ CuckooHashing.tableBits n_max)} {e : CuckooHashing.SlotEntry U} {x : WordRAM.Data 19} (hhdr : HeaderConstsRep base c_exp n_max U x.mem) (hrep_hash : HashPairRep (hashBase base) c_exp n_max U (CuckooHashing.tableBits n_max) θ x.mem) (hslots : TableSlotsRep (t1Base base c_exp n_max) (t2Base base c_exp n_max) tbl x.mem) (htiv : TableIdxValid ks tbl) (he : e.idx < ks.length ∧ ks[e.idx]? = some e.key) (hlen_le : ks.length ≤ n_max) (hmem_keys : ∀ (idx : ℕ) (hlt : idx < ks.length), x.mem (keysBase base c_exp n_max + idx) = ↑ks[idx]) (h0 : x.regs 0 = base) (hm11 : x.mem (base + 11) = ↑e.key) (hm12 : x.mem (base + 12) = e.idx) (hm15 : x.mem (base + 15) = fuel) (hm16 : x.mem (base + 16) = 1) (hfit_fuel : WordRAM.Fits w fuel) (hw : 2 * Probability.trinomialDeg U (CuckooHashing.tableBits n_max) ≤ w) (hfit_end : WordRAM.Fits w (base + space c_exp n_max)) :
∃ (y : WordRAM.Data 19), WordRAM.ExecWithin w (evictLoopCode c_exp n_max) x y (CuckooHashing.evictRounds (evalHash1 θ) (evalHash2 θ) fuel tbl e * evictIterSteps c_exp n_max U + 10) ∧ y.regs 0 = base ∧ HeaderConstsRep base c_exp n_max U y.mem ∧ HashPairRep (hashBase base) c_exp n_max U (CuckooHashing.tableBits n_max) θ y.mem ∧ (match CuckooHashing.pureEvict (evalHash1 θ) (evalHash2 θ) fuel tbl e with | some tbl' => y.mem (base + 16) = 0 ∧ TableSlotsRep (t1Base base c_exp n_max) (t2Base base c_exp n_max) tbl' y.mem ∧ TableIdxValid ks tbl' | none => y.mem (base + 16) = 1) ∧ ∀ (a : ℕ), a ≠ base + 11 → a ≠ base + 12 → a ≠ base + 15 → a ≠ base + 16 → a < t1Base base c_exp n_max ∨ keysBase base c_exp n_max ≤ a → y.mem a = x.mem a

Deterministic execution specification for evictLoopCode, bounding steps in terms of evictRounds and relating the post-state to pureEvict.

Step-bound inequalities for probeKeyCode, queryCode, and evictLoopCode #

theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.probeKeySteps_le {c_exp n_max U : ℕ} (hU : U ≤ max n_max 4 ^ c_exp) :
probeKeySteps c_exp n_max U ≤ 2 * (evalHashBound c_exp + 35) + 12

O(c_exp⁴) = O_{c_exp}(1) worst-case step bound for queryCode c_exp n_max, independent of n_max and U whenever U ≤ max n_max 4 ^ c_exp.

theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.querySteps_le {c_exp n_max U : ℕ} (hU : U ≤ max n_max 4 ^ c_exp) :
querySteps c_exp n_max U ≤ queryStepsBound c_exp
theorem Cslib.DataStructure.Dictionary.WordRAM.CuckooHashing.evictIterSteps_le_add {c_exp n_max U : ℕ} (hU : U ≤ max n_max 4 ^ c_exp) :
evictIterSteps c_exp n_max U ≤ 2 * (evalHashBound c_exp + 54) + 59