Documentation

CslibFrontier.DataStructure.Dictionary.CuckooHashing

Pagh–Rodler Cuckoo Graph Dichotomy and Walk Tail Bounds for Highly Independent Hash Pairs #

This module formalizes the cuckoo graph walk dichotomy and tail-bound analysis of Rasmus Pagh and Flemming Friche Rodler, "Cuckoo hashing" (author-hosted journal manuscript dated 8 December 2003, https://www.rasmuspagh.net/papers/cuckoo-jour.pdf, published in Journal of Algorithms 51(2):122–144, 2004) [PR04], Section 2.3 ("Analysis", manuscript pp. 6–10; Lemma 1 on manuscript p. 7, Eqs. (1)–(3) on manuscript p. 9, and the rehash analysis on manuscript p. 10). For every capacity n_max : ℕ, it proves that any independent pair of (2 * MaxLoop + 2)-wise independent hash functions into sub-tables of size 32 * max n_max 4 ≤ m with eviction cap maxEvictions n_max ≤ MaxLoop ≤ m satisfies IsCuckooHashPair with constants C_walk = 2 and C_reh = 3.

Scope and Word-RAM specialization #

This module establishes IsCuckooHashPair for uniform random hash pairs (isCuckooHashPair_uniform, connected to lazy random-oracle evaluation via uniform_interpret_eq_lazy_hashPair), for explicit (2 * MaxLoop + 2)-wise independent polynomial hash pairs (isCuckooHashPair_trinomialPolyHash), for Mikkel Thorup's double-tabulation hash family (isCuckooHashPair_doubleTabHash). It imports no Word-RAM modules. The concrete regime-switched hash pair is connected to this analysis by CslibFrontier.DataStructure.Dictionary.WordRAM.CuckooHashing.Analysis, whose isCuckooHashPair_provableFamily discharges IsCuckooHashFamily (C_walk = 3, C_reh = 4) for every n_max : ℕ whenever U ≤ max n_max 4 ^ c_exp and 32 * max n_max 4 ≤ 2 ^ M.

Source statements, assumptions, and notation mapping #

In Pagh and Rodler [PR04] (8 December 2003 manuscript, https://www.rasmuspagh.net/papers/cuckoo-jour.pdf):

Main definitions #

Main results #

References #

Section 1: Fresh-key equation chains and k-wise independent counting #

Section 1B: Index-level equation chains for alternating paths and bicyclic prefixes #

def Cslib.DataStructure.Dictionary.CuckooHashing.HasAltPath {U m : ℕ} (h₁ h₂ : Fin U → Fin m) (S : Finset (Fin U)) (u : Fin U) (ℓ : ℕ) :

Case B (HasAltPath): there exists a simple alternating path of length ℓ starting at u with all subsequent keys in S.

Instances For
    theorem Cslib.DataStructure.Dictionary.CuckooHashing.hasAltPath_iff {U m : ℕ} (h₁ h₂ : Fin U → Fin m) (S : Finset (Fin U)) (u : Fin U) (ℓ : ℕ) :
    HasAltPath h₁ h₂ S u ℓ ↔ ∃ (b : Bool) (y : Fin (ℓ + 1) → Fin U), Function.Injective y ∧ y 0 = u ∧ (∀ (i : Fin ℓ), y i.succ ∈ S) ∧ ∀ (i : Fin ℓ), if ((↑i % 2 == 1) != b) = true then h₂ (y i.succ) = h₂ (y i.castSucc) else h₁ (y i.succ) = h₁ (y i.castSucc)

    Plain characterization of HasAltPath without internal equation-chain helpers: HasAltPath holds iff there exist an initial table polarity b : Bool and an injective sequence y : Fin (ℓ + 1) → Fin U starting at y 0 = u with y i.succ ∈ S whose consecutive keys collide alternately under h₁ (false) and h₂ (true).

    @[instance_reducible]
    instance Cslib.DataStructure.Dictionary.CuckooHashing.instDecidableHasAltPath {U m : ℕ} (h₁ h₂ : Fin U → Fin m) (S : Finset (Fin U)) (u : Fin U) (ℓ : ℕ) :
    Decidable (HasAltPath h₁ h₂ S u ℓ)
    def Cslib.DataStructure.Dictionary.CuckooHashing.HasBicyclicPrefix {U m : ℕ} (h₁ h₂ : Fin U → Fin m) (S : Finset (Fin U)) (u : Fin U) (L : ℕ) :

    Case A (HasBicyclicPrefix): there exists a walk prefix of L + 1 pairwise distinct keys starting at u (y 0 = u, y 1, ..., y L ∈ S) that closes two distinct cycles, forming an independent system of L + 2 hash-collision equations across (h₁, h₂).

    Instances For
      theorem Cslib.DataStructure.Dictionary.CuckooHashing.hasBicyclicPrefix_iff {U m : ℕ} (h₁ h₂ : Fin U → Fin m) (S : Finset (Fin U)) (u : Fin U) (L : ℕ) :
      HasBicyclicPrefix h₁ h₂ S u L ↔ ∃ (p : Fin (L + 1)) (s : Fin (L + 1)) (j : Fin (L + 1)) (y : Fin (L + 1) → Fin U), (2 ≤ ↑p ∧ ↑s < ↑p ∧ ↑s % 2 = ↑p % 2 ∧ if ↑p = L then 1 ≤ ↑j ∧ ↑j < L else ↑j < L) ∧ Function.Injective y ∧ y 0 = u ∧ (∀ (i : Fin L), y i.succ ∈ S) ∧ (have endIdx := ⟨if ↑p = L then 0 else L, ⋯⟩; if ((L - ↑p + 1) % 2 == 1) = true then h₂ (y endIdx) = h₂ (y j) else h₁ (y endIdx) = h₁ (y j)) ∧ (if (↑p % 2 == 1) = true then h₂ (y p) = h₂ (y s) else h₁ (y p) = h₁ (y s)) ∧ ∀ (i : Fin L), have pred := ⟨if ↑i = ↑p then 0 else ↑i, ⋯⟩; if (if ↑i < ↑p then ↑i % 2 == 1 else (↑i - ↑p + 1) % 2 == 1) = true then h₂ (y i.succ) = h₂ (y pred) else h₁ (y i.succ) = h₁ (y pred)

      Plain characterization of HasBicyclicPrefix without internal equation-chain helpers: HasBicyclicPrefix h₁ h₂ S u L holds iff there exist cycle indices p, s, j : Fin (L + 1) and an injective sequence y : Fin (L + 1) → Fin U (y 0 = u, y i.succ ∈ S) satisfying the two cycle-closing collision equations together with the L fresh-step collision equations.

      @[instance_reducible]
      instance Cslib.DataStructure.Dictionary.CuckooHashing.instDecidableHasBicyclicPrefix {U m : ℕ} (h₁ h₂ : Fin U → Fin m) (S : Finset (Fin U)) (u : Fin U) (L : ℕ) :
      Decidable (HasBicyclicPrefix h₁ h₂ S u L)
      def Cslib.DataStructure.Dictionary.CuckooHashing.HasExcessSubset {U m : ℕ} (h₁ h₂ : Fin U → Fin m) (S : Finset (Fin U)) (u : Fin U) (maxCard : ℕ) :

      Set-level excess-≥ 1 subgraph (HasExcessSubset): there exists a subset K ⊆ insert u S containing u of size 3 ≤ K.card ≤ maxCard whose images under h₁ and h₂ have combined size at most K.card - 1.

      Instances For
        @[instance_reducible]
        instance Cslib.DataStructure.Dictionary.CuckooHashing.instDecidableHasExcessSubset {U m : ℕ} (h₁ h₂ : Fin U → Fin m) (S : Finset (Fin U)) (u : Fin U) (maxCard : ℕ) :
        Decidable (HasExcessSubset h₁ h₂ S u maxCard)

        Section 2: Half-step trace of pureEvict and Pagh–Rodler Lemma 1 (cuckooWalk_dichotomy) #

        theorem Cslib.DataStructure.Dictionary.CuckooHashing.hasBicyclicPrefix_imp_hasExcessSubset {U m r L : ℕ} {h₁ h₂ : Fin U → Fin m} {S : Finset (Fin U)} {u : Fin U} (hL2 : 2 ≤ L) (hLr : L < 2 * r) (hbic : HasBicyclicPrefix h₁ h₂ S u L) :
        HasExcessSubset h₁ h₂ S u (2 * r)

        Any bicyclic walk prefix of parameter L ∈ [2, 2 * r) yields a subset K ⊆ insert u S containing u with 3 ≤ K.card ≤ 2 * r and (K.image h₁).card + (K.image h₂).card + 1 ≤ K.card.

        theorem Cslib.DataStructure.Dictionary.CuckooHashing.cuckooWalk_dichotomy_bicyclic {U m r : ℕ} {h₁ h₂ : Fin U → Fin m} {S : Finset (Fin U)} {u : Fin U} {tbl₀ : PureTable U m} {e₀ : SlotEntry U} (hr : 1 ≤ r) (hvp : ValidPlacement h₁ h₂ S tbl₀) (hu : u ∉ S) (he₀ : e₀.key = u) (hev : pureEvict h₁ h₂ r tbl₀ e₀ = none) :
        HasAltPath h₁ h₂ S u ((2 * r + 2) / 3) ∨ ∃ L ∈ Finset.Ico 2 (2 * r), HasBicyclicPrefix h₁ h₂ S u L

        Pagh & Rodler (2004), Section 2.3, Lemma 1 (bicyclic form): If pureEvict h₁ h₂ r tbl₀ e₀ = none (1 ≤ r) from a valid placement tbl₀ of S with e₀.key = u ∉ S, then either HasAltPath h₁ h₂ S u ((2 * r + 2) / 3) holds or there is some L ∈ Finset.Ico 2 (2 * r) with HasBicyclicPrefix h₁ h₂ S u L.

        theorem Cslib.DataStructure.Dictionary.CuckooHashing.cuckooWalk_dichotomy {U m r : ℕ} {h₁ h₂ : Fin U → Fin m} {S : Finset (Fin U)} {u : Fin U} {tbl₀ : PureTable U m} {e₀ : SlotEntry U} (hr : 1 ≤ r) (hvp : ValidPlacement h₁ h₂ S tbl₀) (hu : u ∉ S) (he₀ : e₀.key = u) (hev : pureEvict h₁ h₂ r tbl₀ e₀ = none) :
        HasAltPath h₁ h₂ S u ((2 * r + 2) / 3) ∨ HasExcessSubset h₁ h₂ S u (2 * r)

        Pagh & Rodler (2004), Section 2.3, Lemma 1 (excess-subset form): If pureEvict h₁ h₂ r tbl₀ e₀ = none (1 ≤ r) from a valid placement tbl₀ of S with e₀.key = u ∉ S, then either HasAltPath h₁ h₂ S u ((2 * r + 2) / 3) (Case B) or HasExcessSubset h₁ h₂ S u (2 * r) (Case A) holds.

        theorem Cslib.DataStructure.Dictionary.CuckooHashing.lt_cuckooWalkRounds_imp_dichotomy {U m maxLoop r : ℕ} {h₁ h₂ : Fin U → Fin m} {S : Finset (Fin U)} {u : Fin U} (hr : 1 ≤ r) (hu : u ∉ S) (hlt : r < cuckooWalkRounds h₁ h₂ maxLoop S u) :
        HasAltPath h₁ h₂ S u ((2 * r + 2) / 3) ∨ ∃ L ∈ Finset.Ico 2 (2 * r), HasBicyclicPrefix h₁ h₂ S u L

        If 1 ≤ r and r < cuckooWalkRounds h₁ h₂ maxLoop S u with u ∉ S, then either HasAltPath h₁ h₂ S u ((2 * r + 2) / 3) holds or there is L ∈ Finset.Ico 2 (2 * r) with HasBicyclicPrefix h₁ h₂ S u L.

        Section 3: Seed-count tail bounds and rehash union bounds #

        Section 4: IsCuckooHashPair instances and lazy random-oracle equivalence #

        theorem Cslib.DataStructure.Dictionary.CuckooHashing.isCuckooHashPair_of_kWiseIndependent {Θ₁ : Type u_1} {Θ₂ : Type u_2} [Fintype Θ₁] [Nonempty Θ₁] [Fintype Θ₂] [Nonempty Θ₂] {U m : ℕ} (n_max MaxLoop : ℕ) {h₁ : Θ₁ → Fin U → Fin m} {h₂ : Θ₂ → Fin U → Fin m} (hk₁ : Probability.IsKWiseIndependentHash (2 * MaxLoop + 2) h₁) (hk₂ : Probability.IsKWiseIndependentHash (2 * MaxLoop + 2) h₂) (hm : 32 * max n_max 4 ≤ m) (hloop_ge : maxEvictions n_max ≤ MaxLoop) (hloop_le : MaxLoop ≤ m) :
        IsCuckooHashPair (Probability.PMF.uniformOfFintype (Θ₁ × Θ₂)) (fun (θ : Θ₁ × Θ₂) => h₁ θ.1) (fun (θ : Θ₁ × Θ₂) => h₂ θ.2) n_max MaxLoop 2 3

        Pagh & Rodler (2004), Section 2.3 (IsCuckooHashPair for (2 * MaxLoop + 2)-wise independent hash pairs): For any capacity n_max : ℕ, sub-table size m with 32 * max n_max 4 ≤ m, eviction cap MaxLoop with maxEvictions n_max ≤ MaxLoop ≤ m, and independent hash families h₁ : Θ₁ → Fin U → Fin m and h₂ : Θ₂ → Fin U → Fin m each satisfying IsKWiseIndependentHash (2 * MaxLoop + 2), the uniform product distribution on Θ₁ × Θ₂ satisfies IsCuckooHashPair with universal constants C_walk = 2 and C_reh = 3.

        theorem Cslib.DataStructure.Dictionary.CuckooHashing.isCuckooHashPair_trinomialPolyHash {t k U M : ℕ} (hU : U ≤ 2 ^ (2 * 3 ^ t)) (hM : M ≤ 2 * 3 ^ t) (n_max MaxLoop : ℕ) (hdeg : 2 * MaxLoop + 2 ≤ k) (hm : 32 * max n_max 4 ≤ 2 ^ M) (hloop_ge : maxEvictions n_max ≤ MaxLoop) (hloop_le : MaxLoop ≤ 2 ^ M) :
        IsCuckooHashPair (Probability.PMF.uniformOfFintype ((Fin k → Fin (2 ^ (2 * 3 ^ t))) × (Fin k → Fin (2 ^ (2 * 3 ^ t))))) (fun (θ : (Fin k → Fin (2 ^ (2 * 3 ^ t))) × (Fin k → Fin (2 ^ (2 * 3 ^ t)))) => Probability.trinomialPolyHash t k U M θ.1) (fun (θ : (Fin k → Fin (2 ^ (2 * 3 ^ t))) × (Fin k → Fin (2 ^ (2 * 3 ^ t)))) => Probability.trinomialPolyHash t k U M θ.2) n_max MaxLoop 2 3

        IsCuckooHashPair for two independent trinomialPolyHash polynomials: When U ≤ 2 ^ (2 * 3 ^ t), M ≤ 2 * 3 ^ t, 32 * max n_max 4 ≤ 2 ^ M, and maxEvictions n_max ≤ MaxLoop ≤ 2 ^ M, two independent degree-< k (2 * MaxLoop + 2 ≤ k) trinomial-field polynomial hashes trinomialPolyHash t k U M satisfy IsCuckooHashPair with C_walk = 2 and C_reh = 3.

        theorem Cslib.DataStructure.Dictionary.CuckooHashing.isCuckooHashPair_uniform {U m : ℕ} [NeZero m] (n_max MaxLoop : ℕ) (hm : 32 * max n_max 4 ≤ m) (hloop_ge : maxEvictions n_max ≤ MaxLoop) (hloop_le : MaxLoop ≤ m) :

        IsCuckooHashPair for a uniform random hash pair: When 32 * max n_max 4 ≤ m and maxEvictions n_max ≤ MaxLoop ≤ m, sampling (h₁, h₂) uniformly at random from (Fin U → Fin m) × (Fin U → Fin m) satisfies IsCuckooHashPair with C_walk = 2 and C_reh = 3.

        theorem Cslib.DataStructure.Dictionary.CuckooHashing.uniform_interpret_eq_lazy_hashPair {U m : ℕ} [NeZero m] {α : Type} (program : OracleComp (fun (x : Bool × Fin U) => Fin m) α) :
        (do let p ← Probability.PMF.uniformOfFintype ((Fin U → Fin m) × (Fin U → Fin m)) OracleComp.interpret (fun (q : Bool × Fin U) => PMF.pure (if q.1 = true then p.2 q.2 else p.1 q.2)) program) = PMF.map Prod.fst ((OracleComp.interpret (RandomOracle.answer fun (x : Bool × Fin U) => Probability.PMF.uniformOfFintype (Fin m)) program).run fun (x : Bool × Fin U) => none)

        Lazy random-oracle equivalence for the uniform cuckoo hash pair: Sampling (h₁, h₂) eagerly from PMF.uniformOfFintype ((Fin U → Fin m) × (Fin U → Fin m)) and interpreting queries (b, x) : Bool × Fin U via if b then h₂ x else h₁ x has the exact same output distribution as answering each distinct query (b, x) lazily on demand with an independent uniform bucket in Fin m via RandomOracle.answer.

        Section 5: High-probability k-wise independence bridge and Double Tabulation #

        theorem Cslib.DataStructure.Dictionary.CuckooHashing.isCuckooHashPair_of_whp_kWiseIndependent {Θ_top : Type u_1} {Θ_bot : Type u_2} [Fintype Θ_top] [Nonempty Θ_top] [Fintype Θ_bot] [Nonempty Θ_bot] {U m : ℕ} (n_max MaxLoop : ℕ) {H : Θ_top → Θ_bot → Fin U → Fin m} (GoodTop : Finset Θ_top) (hk : ∀ α ∈ GoodTop, Probability.IsKWiseIndependentHash (2 * MaxLoop + 2) (H α)) (hwhp : 2 * (Fintype.card Θ_top - GoodTop.card) * (max n_max 1 * max MaxLoop 1) ≤ Fintype.card Θ_top) (hm : 32 * max n_max 4 ≤ m) (hloop_ge : maxEvictions n_max ≤ MaxLoop) (hloop_le : MaxLoop ≤ m) :
        IsCuckooHashPair (Probability.PMF.uniformOfFintype ((Θ_top × Θ_bot) × Θ_top × Θ_bot)) (fun (x : (Θ_top × Θ_bot) × Θ_top × Θ_bot) => match (motive := (Θ_top × Θ_bot) × Θ_top × Θ_bot → Fin U → Fin m) x with | ((top₁, bottom₁), snd) => H top₁ bottom₁) (fun (x : (Θ_top × Θ_bot) × Θ_top × Θ_bot) => match (motive := (Θ_top × Θ_bot) × Θ_top × Θ_bot → Fin U → Fin m) x with | (fst, top₂, bottom₂) => H top₂ bottom₂) n_max MaxLoop 3 4

        Black-box high-probability k-wise independence conditioning bridge into IsCuckooHashPair: Given a two-level hash family H : Θ_top → Θ_bot → Fin U → Fin m and a good top-seed set GoodTop : Finset Θ_top such that:

        1. H α is (2 * MaxLoop + 2)-wise independent for every α ∈ GoodTop, and
        2. 2 * (|Θ_top| - |GoodTop|) * (max n_max 1 * max MaxLoop 1) ≤ |Θ_top|, under 32 * max n_max 4 ≤ m and maxEvictions n_max ≤ MaxLoop ≤ m, two independent draws from Θ_top × Θ_bot satisfy IsCuckooHashPair with constants C_walk = 3 and C_reh = 4.
        theorem Cslib.DataStructure.Dictionary.CuckooHashing.isCuckooHashPair_doubleTabHash {c b U M : ℕ} (hc : 1 ≤ c) (hU : U ≤ 2 ^ (c * b)) (n_max MaxLoop : ℕ) (hm : 32 * max n_max 4 ≤ 2 ^ M) (hloop_ge : maxEvictions n_max ≤ MaxLoop) (hloop_le : MaxLoop ≤ 2 ^ M) (hbound : 2 * (max n_max 1 * max MaxLoop 1) * Probability.doubleTabCodeBound c (8 * c) 4 (2 * MaxLoop + 2) ≤ 2 ^ (3 * b)) :
        IsCuckooHashPair (Probability.PMF.uniformOfFintype ((Probability.DoubleTabTopSeed c (8 * c) b × Probability.DoubleTabBotSeed (8 * c) b M) × Probability.DoubleTabTopSeed c (8 * c) b × Probability.DoubleTabBotSeed (8 * c) b M)) (fun (x : (Probability.DoubleTabTopSeed c (8 * c) b × Probability.DoubleTabBotSeed (8 * c) b M) × Probability.DoubleTabTopSeed c (8 * c) b × Probability.DoubleTabBotSeed (8 * c) b M) => match (motive := (Probability.DoubleTabTopSeed c (8 * c) b × Probability.DoubleTabBotSeed (8 * c) b M) × Probability.DoubleTabTopSeed c (8 * c) b × Probability.DoubleTabBotSeed (8 * c) b M → Fin U → Fin (2 ^ M)) x with | ((top₁, bottom₁), snd) => Probability.doubleTabHash c (8 * c) b U M top₁ bottom₁) (fun (x : (Probability.DoubleTabTopSeed c (8 * c) b × Probability.DoubleTabBotSeed (8 * c) b M) × Probability.DoubleTabTopSeed c (8 * c) b × Probability.DoubleTabBotSeed (8 * c) b M) => match (motive := (Probability.DoubleTabTopSeed c (8 * c) b × Probability.DoubleTabBotSeed (8 * c) b M) × Probability.DoubleTabTopSeed c (8 * c) b × Probability.DoubleTabBotSeed (8 * c) b M → Fin U → Fin (2 ^ M)) x with | (fst, top₂, bottom₂) => Probability.doubleTabHash c (8 * c) b U M top₂ bottom₂) n_max MaxLoop 3 4

        IsCuckooHashPair for Mikkel Thorup's Double Tabulation ([Thorup2013], Theorem 1(a), p. 4): When 1 ≤ c, U ≤ 2 ^ (c * b), 32 * max n_max 4 ≤ 2 ^ M, maxEvictions n_max ≤ MaxLoop ≤ 2 ^ M, and 2 * (max n_max 1 * max MaxLoop 1) * doubleTabCodeBound c (8 * c) 4 (2 * MaxLoop + 2) ≤ 2 ^ (3 * b), two independent draws from DoubleTabTopSeed c (8 * c) b × DoubleTabBotSeed (8 * c) b M satisfy IsCuckooHashPair with constants C_walk = 3 and C_reh = 4.