Documentation

CslibFrontier.Crypto.Computational.RainbowTable

Rainbow and Hellman tables for two-phase hash inversion #

Two-phase table precomputation and online inversion of a hash function h : P → Y from [BS23], Section 18.7 (pp. 742–746; Fig. 18.13 on p. 744), modeled as oracle programs over h and an endpoint lookup oracle.

Given column reduction functions g : ℕ → Y → P defining f_j(pw) := g j (h pw) on 0-indexed columns j = 0, ..., τ - 1 (corresponding to the book's g_1, ..., g_τ):

Deterministic per-run request-trace counts (OracleComp.queriesWith) for both rainbowAttack and hellmanAttack are proved in CslibFrontier.Crypto.Computational.RainbowTable.RunCost.

This formalization differs from the book's presentation as follows:

  1. 0-indexed columns. Reduction functions are indexed 0, ..., τ - 1 rather than 1, ..., τ; loop step k ∈ {0, ..., τ - 1} tests column col = τ - 1 - k (i = τ - k in for i = τ downto 1).
  2. Explicit starting list. Sampling ℓ random starting points pw_1, ..., pw_ℓ ∈ P is factored out into the parameter starts : List P.
  3. Endpoint-keyed table and abstract lookup oracle. rainbowPreprocess stores (z_i, pw_i) rather than (pw_i, z_i) so that List.lookup implements table lookup directly, as in Cslib.Crypto.dictionaryTable. Online inversion accesses the table through the right-hand oracle P → Option P of TableAttackSpec P Y, separating hash evaluations (TableAttackSpec.hashCost) from table lookups (TableAttackSpec.lookupCost) without claiming constant machine time for List.lookup.
  4. Failure output. Inversion returns Option P (none for "fail").
  5. Limitations. Soundness and two-phase inversion on covered chains are proved pointwise for arbitrary h and g. Hellman's multiple-table remedy (m small independent tables with distinct reduction functions searched separately by A₁, p. 745), the book's heuristic success-probability approximations modeling h and g_j as random functions (pp. 743–746), and machine-level running-time bounds are not formalized here (#5752).

Main definitions #

Main results #

References #

Rainbow chains #

def Cslib.Crypto.rainbowChainPure {P Y : Type} (h : P → Y) (g : ℕ → Y → P) :
ℕ → ℕ → P → P

Pure evaluation of len columns of a rainbow chain starting at column start from pw ([BS23], Section 18.7, Fig. 18.13b, p. 744; p. 745): column j applies f_j(x) = g j (h x). Columns are 0-indexed (0, ..., τ - 1 for the book's g_1, ..., g_τ).

@[simp]
theorem Cslib.Crypto.rainbowChainPure_zero {P Y : Type} (h : P → Y) (g : ℕ → Y → P) (start : ℕ) (pw : P) :
rainbowChainPure h g start 0 pw = pw

Zero columns leave the password unchanged.

@[simp]
theorem Cslib.Crypto.rainbowChainPure_succ {P Y : Type} (h : P → Y) (g : ℕ → Y → P) (start len : ℕ) (pw : P) :
rainbowChainPure h g start (len + 1) pw = rainbowChainPure h g (start + 1) len (g start (h pw))

Applying len + 1 columns from start applies column start then len columns from start + 1.

theorem Cslib.Crypto.rainbowChainPure_add {P Y : Type} (h : P → Y) (g : ℕ → Y → P) (start m n : ℕ) (pw : P) :
rainbowChainPure h g start (m + n) pw = rainbowChainPure h g (start + m) n (rainbowChainPure h g start m pw)

Splitting a rainbow chain of length m + n into m columns from start followed by n columns from start + m.

@[simp]
theorem Cslib.Crypto.rainbowChainPure_const {P Y : Type} (h : P → Y) (g₀ : Y → P) (start len : ℕ) (pw : P) :
rainbowChainPure h (fun (x : ℕ) => g₀) start len pw = (fun (x : P) => g₀ (h x))^[len] pw

With a single column-independent reduction g₀, rainbowChainPure is (g₀ ∘ h)^[len] (Hellman's chain, [BS23], Section 18.7, p. 743; Fig. 18.13a, p. 744).

def Cslib.Crypto.rainbowChain {P Y : Type} (g : ℕ → Y → P) :
ℕ → ℕ → P → OracleComp (fun (x : P) => Y) P

Oracle evaluation of len columns of a rainbow chain starting at column start from pw, querying h : P → Y once per column ([BS23], Section 18.7, pp. 745–746).

@[simp]
theorem Cslib.Crypto.evalPure_rainbowChain {P Y : Type} (h : P → Y) (g : ℕ → Y → P) (start len : ℕ) (pw : P) :
OracleComp.evalPure h (rainbowChain g start len pw) = rainbowChainPure h g start len pw

Against h, rainbowChain computes rainbowChainPure h.

theorem Cslib.Crypto.isQueryBounded_rainbowChain {P Y : Type} (g : ℕ → Y → P) (start len : ℕ) (pw : P) :
(rainbowChain g start len pw).IsQueryBounded len

Evaluating len columns makes at most len hash queries on every path.

@[simp]
theorem Cslib.Crypto.length_queriesWith_rainbowChain {P Y : Type} (h : P → Y) (g : ℕ → Y → P) (start len : ℕ) (pw : P) :
(OracleComp.queriesWith h (rainbowChain g start len pw)).length = len

Along any deterministic run against h, rainbowChain makes len hash queries.

Preprocessing phase A₀ #

def Cslib.Crypto.rainbowPreprocess {P Y : Type} (g : ℕ → Y → P) (τ : ℕ) (starts : List P) :
OracleComp (fun (x : P) => Y) (List (P × P))

Preprocessing algorithm A₀ for a rainbow table of chain length τ from starting points starts ([BS23], Section 18.7, pp. 743, 745; Fig. 18.13, p. 744): computes the endpoint z_i = f_{τ-1}(⋯ f_0(pw_i) ⋯) of each pw_i in starts and stores (z_i, pw_i) keyed by endpoint for List.lookup.

theorem Cslib.Crypto.evalPure_rainbowPreprocess {P Y : Type} (h : P → Y) (g : ℕ → Y → P) (τ : ℕ) (starts : List P) :
OracleComp.evalPure h (rainbowPreprocess g τ starts) = List.map (fun (pw₀ : P) => (rainbowChainPure h g 0 τ pw₀, pw₀)) starts

Against h, rainbowPreprocess pairs each starting point's τ-step endpoint with that starting point.

theorem Cslib.Crypto.isQueryBounded_rainbowPreprocess {P Y : Type} (g : ℕ → Y → P) (τ : ℕ) (starts : List P) :
(rainbowPreprocess g τ starts).IsQueryBounded (starts.length * τ)

Cost of A₀. Preprocessing starts.length chains of length τ makes at most starts.length * τ hash queries on every path ([BS23], Section 18.7, pp. 743, 745).

theorem Cslib.Crypto.lookup_evalPure_rainbowPreprocess {P Y : Type} [DecidableEq P] (h : P → Y) (g : ℕ → Y → P) (τ : ℕ) (starts : List P) (z : P) :
List.lookup z (OracleComp.evalPure h (rainbowPreprocess g τ starts)) = List.find? (fun (pw₀ : P) => rainbowChainPure h g 0 τ pw₀ == z) starts

Looking an endpoint z up in the preprocessed table finds the first starting point in starts whose τ-step chain ends at z.

theorem Cslib.Crypto.lookup_evalPure_rainbowPreprocess_eq_some_iff {P Y : Type} [DecidableEq P] {h : P → Y} {g : ℕ → Y → P} {τ : ℕ} {starts : List P} {z pw₀ : P} :
List.lookup z (OracleComp.evalPure h (rainbowPreprocess g τ starts)) = some pw₀ ↔ rainbowChainPure h g 0 τ pw₀ = z ∧ ∃ (before : List P) (after : List P), starts = before ++ pw₀ :: after ∧ ∀ pw₁ ∈ before, rainbowChainPure h g 0 τ pw₁ ≠ z

Endpoint lookup in A₀'s table. List.lookup z on the preprocessed table returns some pw₀ if and only if pw₀'s τ-step chain ends at z and no earlier element of starts has endpoint z.

theorem Cslib.Crypto.lookup_evalPure_rainbowPreprocess_of_nodup {P Y : Type} [DecidableEq P] {h : P → Y} {g : ℕ → Y → P} {τ : ℕ} {starts : List P} {pw₀ : P} (hmem : pw₀ ∈ starts) (hnodup : (List.map (rainbowChainPure h g 0 τ) starts).Nodup) :
List.lookup (rainbowChainPure h g 0 τ pw₀) (OracleComp.evalPure h (rainbowPreprocess g τ starts)) = some pw₀

When the τ-step endpoints of starts are distinct—as guaranteed when A₀ discards chains with duplicate endpoints ([BS23], Section 18.7, p. 745)—every pw₀ ∈ starts is recovered from its endpoint by List.lookup.

Online inversion interface #

@[reducible, inline]

Summed oracle interface for online table-assisted inversion ([BS23], Section 18.7, p. 742): hash evaluations P → Y on the left and endpoint-to-start table lookups P → Option P on the right.

Per-call cost charging 1 per hash query (left) and 0 per table lookup (right).

Per-call cost charging 0 per hash query (left) and 1 per table lookup (right).

@[simp]

A left (hash) query has hashCost 1.

@[simp]

A right (table lookup) query has hashCost 0.

@[simp]

A left (hash) query has lookupCost 0.

@[simp]

A right (table lookup) query has lookupCost 1.

Online rainbow-table inversion A₁ (p. 746) #

def Cslib.Crypto.rainbowAttackStep {P Y : Type} [DecidableEq Y] (g : ℕ → Y → P) (τ : ℕ) (y : Y) (k : ℕ) :

Step k (0 ≤ k < τ, testing column col = τ - 1 - k, i.e., loop index i = τ - k in for i = τ downto 1) of online rainbow-table inversion A₁ ([BS23], Section 18.7, p. 746): computes the candidate endpoint z from g (τ - 1 - k) y across k columns, looks z up in the table, and on some pw₀ reconstructs pw across τ - 1 - k columns and checks h(pw) = y.

def Cslib.Crypto.rainbowAttack {P Y : Type} [DecidableEq Y] (g : ℕ → Y → P) (τ : ℕ) (y : Y) :

Online rainbow-table inversion algorithm A₁ ([BS23], Section 18.7, p. 746): searches steps k = 0, ..., τ - 1 (i = τ down to 1) in order and stops at the first verified preimage of y.

theorem Cslib.Crypto.evalPure_rainbowAttackStep {P Y : Type} [DecidableEq Y] (h : P → Y) (lookup : P → Option P) (g : ℕ → Y → P) (τ : ℕ) (y : Y) (k : ℕ) :
OracleComp.evalPure (OracleComp.sumPure h lookup) (rainbowAttackStep g τ y k) = match lookup (rainbowChainPure h g (τ - k) k (g (τ - 1 - k) y)) with | none => none | some pw₀ => have pw := rainbowChainPure h g 0 (τ - 1 - k) pw₀; if h pw = y then some pw else none

Deterministic evaluation of one step of rainbowAttack.

theorem Cslib.Crypto.evalPure_rainbowAttack {P Y : Type} [DecidableEq Y] (h : P → Y) (lookup : P → Option P) (g : ℕ → Y → P) (τ : ℕ) (y : Y) :
OracleComp.evalPure (OracleComp.sumPure h lookup) (rainbowAttack g τ y) = List.findSome? (fun (k : ℕ) => match lookup (rainbowChainPure h g (τ - k) k (g (τ - 1 - k) y)) with | none => none | some pw₀ => have pw := rainbowChainPure h g 0 (τ - 1 - k) pw₀; if h pw = y then some pw else none) (List.range τ)

Deterministic evaluation of rainbowAttack against h and lookup.

theorem Cslib.Crypto.rainbowAttack_sound {P Y : Type} [DecidableEq Y] {h : P → Y} {lookup : P → Option P} {g : ℕ → Y → P} {τ : ℕ} {y : Y} {pw : P} (heval : OracleComp.evalPure (OracleComp.sumPure h lookup) (rainbowAttack g τ y) = some pw) :
h pw = y

Soundness of rainbow-table inversion ([BS23], Section 18.7, line 5 of Algorithm A₁, p. 746): every password output by rainbowAttack satisfies h pw = y; false alarms from chain merges or reduction collisions are rejected.

theorem Cslib.Crypto.exists_evalPure_rainbowAttack_of_lookup_eq_some {P Y : Type} [DecidableEq Y] {h : P → Y} {lookup : P → Option P} {g : ℕ → Y → P} {τ col : ℕ} {pw₀ : P} {y : Y} (hcol : col < τ) (hy : h (rainbowChainPure h g 0 col pw₀) = y) (hlookup : lookup (rainbowChainPure h g 0 τ pw₀) = some pw₀) :
∃ (pw' : P), OracleComp.evalPure (OracleComp.sumPure h lookup) (rainbowAttack g τ y) = some pw' ∧ h pw' = y

Completeness of rainbow-table inversion on covered points ([BS23], Section 18.7, pp. 745–746): if y is the hash of the column-col value (col < τ) of a chain starting at pw₀ whose τ-step endpoint maps to some pw₀ under lookup, then rainbowAttack outputs a valid preimage of y.

theorem Cslib.Crypto.evalPure_rainbowAttack_of_injective {P Y : Type} [DecidableEq Y] {h : P → Y} {lookup : P → Option P} {g : ℕ → Y → P} {τ col : ℕ} {pw₀ pw : P} {y : Y} (hinj : Function.Injective h) (hcol : col < τ) (hpw : rainbowChainPure h g 0 col pw₀ = pw) (hy : h pw = y) (hlookup : lookup (rainbowChainPure h g 0 τ pw₀) = some pw₀) :

When h is injective, rainbowAttack recovers pw itself on any covered chain point.

theorem Cslib.Crypto.exists_evalPure_rainbowAttack_rainbowPreprocess {P Y : Type} [DecidableEq Y] [DecidableEq P] {h : P → Y} {g : ℕ → Y → P} {τ col : ℕ} {starts : List P} {pw₀ : P} {y : Y} (hmem : pw₀ ∈ starts) (hnodup : (List.map (rainbowChainPure h g 0 τ) starts).Nodup) (hcol : col < τ) (hy : h (rainbowChainPure h g 0 col pw₀) = y) :
∃ (pw' : P), OracleComp.evalPure (OracleComp.sumPure h fun (x : P) => List.lookup x (OracleComp.evalPure h (rainbowPreprocess g τ starts))) (rainbowAttack g τ y) = some pw' ∧ h pw' = y

Two-phase completeness of (rainbowPreprocess, rainbowAttack) ([BS23], Section 18.7, pp. 745–746): against the endpoint table built by rainbowPreprocess g τ starts when the τ-step endpoints of starts are distinct, rainbowAttack inverts y = h(pw) for every point pw = rainbowChainPure h g 0 col pw₀ (pw₀ ∈ starts, col < τ).

theorem Cslib.Crypto.evalPure_rainbowAttack_rainbowPreprocess_of_injective {P Y : Type} [DecidableEq Y] [DecidableEq P] {h : P → Y} {g : ℕ → Y → P} {τ col : ℕ} {starts : List P} {pw₀ pw : P} {y : Y} (hinj : Function.Injective h) (hmem : pw₀ ∈ starts) (hnodup : (List.map (rainbowChainPure h g 0 τ) starts).Nodup) (hcol : col < τ) (hpw : rainbowChainPure h g 0 col pw₀ = pw) (hy : h pw = y) :

When h is injective and starts has distinct τ-step endpoints, two-phase rainbow inversion (rainbowPreprocess, rainbowAttack) recovers pw itself on every covered chain point.

Pathwise lookup budget of rainbow A₁. rainbowAttack makes at most τ table lookups on every path ([BS23], Section 18.7, p. 746).

Pathwise hash budget of rainbow A₁. Across arbitrary answers (even if every lookup hits and fails verification), rainbowAttack makes at most τ * τ hash queries on every path.

Hellman's stateful online attack A₁ (Fig. 18.13a, p. 744) #

def Cslib.Crypto.hellmanLoop {P Y : Type} [DecidableEq Y] (g₀ : Y → P) (y : Y) :
ℕ → P → OracleComp (TableAttackSpec P Y) (Option P)

Remaining rem iterations of Hellman's online search A₁ ([BS23], Section 18.7, p. 744) from current chain element z: at rem = m + 1 (corresponding to τ - i = m), looks z up in the table, reconstructs (g₀ ∘ h)^[m](pw₀) and checks h(pw) = y on a hit, and otherwise advances z ← g₀(h(z)).

def Cslib.Crypto.hellmanAttack {P Y : Type} [DecidableEq Y] (g₀ : Y → P) (τ : ℕ) (y : Y) :

Hellman's online inversion algorithm A₁ ([BS23], Section 18.7, Fig. 18.13a, p. 744): initializes z ← g₀(y) and iterates τ times, looking z up in L, reconstructing pw ← (g₀ ∘ h)^[τ - i](pw₀) and checking h(pw) = y on a hit, and advancing z ← g₀(h(z)) step-by-step between iterations.

theorem Cslib.Crypto.hellmanAttack_sound {P Y : Type} [DecidableEq Y] {h : P → Y} {lookup : P → Option P} {g₀ : Y → P} {τ : ℕ} {y : Y} {pw : P} (heval : OracleComp.evalPure (OracleComp.sumPure h lookup) (hellmanAttack g₀ τ y) = some pw) :
h pw = y

Soundness of Hellman's inversion ([BS23], Section 18.7, line 5 of Algorithm A₁, p. 744): every password output by hellmanAttack satisfies h pw = y.

theorem Cslib.Crypto.exists_evalPure_hellmanAttack_of_lookup_eq_some {P Y : Type} [DecidableEq Y] {h : P → Y} {lookup : P → Option P} {g₀ : Y → P} {τ col : ℕ} {pw₀ : P} {y : Y} (hcol : col < τ) (hy : h ((fun (x : P) => g₀ (h x))^[col] pw₀) = y) (hlookup : lookup ((fun (x : P) => g₀ (h x))^[τ] pw₀) = some pw₀) :
∃ (pw' : P), OracleComp.evalPure (OracleComp.sumPure h lookup) (hellmanAttack g₀ τ y) = some pw' ∧ h pw' = y

Completeness of Hellman's inversion on covered points ([BS23], Section 18.7, pp. 743–744): if y = h(pw) for a point pw = (g₀ ∘ h)^[col](pw₀) at column col < τ whose τ-step endpoint maps to some pw₀ under lookup, then hellmanAttack outputs a valid preimage of y.

theorem Cslib.Crypto.evalPure_hellmanAttack_of_injective {P Y : Type} [DecidableEq Y] {h : P → Y} {lookup : P → Option P} {g₀ : Y → P} {τ col : ℕ} {pw₀ pw : P} {y : Y} (hinj : Function.Injective h) (hcol : col < τ) (hpw : (fun (x : P) => g₀ (h x))^[col] pw₀ = pw) (hy : h pw = y) (hlookup : lookup ((fun (x : P) => g₀ (h x))^[τ] pw₀) = some pw₀) :

When h is injective, hellmanAttack recovers pw itself on any covered chain point.

theorem Cslib.Crypto.exists_evalPure_hellmanAttack_rainbowPreprocess {P Y : Type} [DecidableEq Y] [DecidableEq P] {h : P → Y} {g₀ : Y → P} {τ col : ℕ} {starts : List P} {pw₀ : P} {y : Y} (hmem : pw₀ ∈ starts) (hnodup : (List.map (fun (pw : P) => (fun (x : P) => g₀ (h x))^[τ] pw) starts).Nodup) (hcol : col < τ) (hy : h ((fun (x : P) => g₀ (h x))^[col] pw₀) = y) :
∃ (pw' : P), OracleComp.evalPure (OracleComp.sumPure h fun (x : P) => List.lookup x (OracleComp.evalPure h (rainbowPreprocess (fun (x : ℕ) => g₀) τ starts))) (hellmanAttack g₀ τ y) = some pw' ∧ h pw' = y

Two-phase completeness of (rainbowPreprocess (fun _ => g₀), hellmanAttack) ([BS23], Section 18.7, pp. 743–744): against the endpoint table built by rainbowPreprocess (fun _ => g₀) τ starts when the τ-step endpoints of starts are distinct, hellmanAttack inverts y = h(pw) for every point pw = (g₀ ∘ h)^[col](pw₀) (pw₀ ∈ starts, col < τ).

theorem Cslib.Crypto.evalPure_hellmanAttack_rainbowPreprocess_of_injective {P Y : Type} [DecidableEq Y] [DecidableEq P] {h : P → Y} {g₀ : Y → P} {τ col : ℕ} {starts : List P} {pw₀ pw : P} {y : Y} (hinj : Function.Injective h) (hmem : pw₀ ∈ starts) (hnodup : (List.map (fun (pw : P) => (fun (x : P) => g₀ (h x))^[τ] pw) starts).Nodup) (hcol : col < τ) (hpw : (fun (x : P) => g₀ (h x))^[col] pw₀ = pw) (hy : h pw = y) :
OracleComp.evalPure (OracleComp.sumPure h fun (x : P) => List.lookup x (OracleComp.evalPure h (rainbowPreprocess (fun (x : ℕ) => g₀) τ starts))) (hellmanAttack g₀ τ y) = some pw

When h is injective and starts has distinct τ-step endpoints, two-phase Hellman inversion (rainbowPreprocess (fun _ => g₀), hellmanAttack) recovers pw itself on every covered chain point.

Pathwise lookup budget of Hellman's A₁. hellmanAttack makes at most τ table lookups on every path ([BS23], Section 18.7, p. 744).

theorem Cslib.Crypto.isCostBounded_hashCost_hellmanAttack {P Y : Type} [DecidableEq Y] (g₀ : Y → P) (τ : ℕ) (y : Y) :

Pathwise hash budget of Hellman's A₁. Across arbitrary answers (even if every lookup hits and fails verification, spending m + 2 hash queries at remaining depth m + 1), hellmanAttack makes at most ∑_{m < τ} (m + 2) = τ * (τ + 3) / 2 hash queries on every path.