Invertible PRFs and Plinko hint tables
DefinitionPlinkoTree sampler. A node is a tuple : the balls and the bins , with . A key is a split rule ; at an internal node () the first balls go to the left child and the rest to the right child . From the root , is the bin reached by following ball , and is the set of balls of the leaf reached by walking towards bin .
Ideal randomness. For a finite set and a derandomized binomial sampler , the ideal key for splits node by . The sampler is exact if, for uniform , whenever and .
iPRF. For a permutation of , and . The loads of are ; for uniform they are distributed as . All probabilities are over uniform elements of finite sets.
Plinko. The database has blocks of bits; entry is offset of block , and bits live in . Hint slot uses offset in block , and the client finds slots by an inversion algorithm , correct if it equals . A slot is empty, a regular hint , a backup hint , or a promoted hint that also covers entry . HintInit streams the database in a given order and XORs each entry into the slots found by inversion. A query for (with coins: a replacement index, a hint slot, a bit and padding offsets) queries a fresh index instead if is cached, checks that the chosen slot was found by inversion and contains the index, sends the hint's other blocks and offsets (or their complement, by , padded with random offsets), recovers the bit as the stored parity XOR the server's answer, empties the used slot, promotes the first backup hint with the answered entry, and caches the answer. An update of entry XORs the change into every slot found by inversion at , into the cached answer for and into the promoted hint holding . The correctness game runs HintInit and then a list of queries and updates, and reports failure if some query outputs a bit different from the current entry.
import Mathlib
/-!
# Invertible PRFs and Plinko hint tables
Model for the information-theoretic core of "Plinko: Single-Server PIR with Efficient
Updates via Invertible PRFs" (Hoover, Patel, Persiano, Yeo).
* **Tree sampler.** A key is a split rule `κ : Node → ℕ`. Starting from the root node
holding the balls `0, …, n-1` and the bins `0, …, m-1`, every internal node sends its first
`min (κ v) count` balls to the left child (the lower half of its bins) and the rest to the
right child. `sample κ x` is the bin reached by ball `x`; `sampleInv κ y` is the set of balls
reaching bin `y`.
* **Ideal randomness.** `idealSplits binom F` uses an ideal random function `F` from nodes to a
finite set `R` and a derandomized binomial sampler `binom`; `ExactBinomial` says that `binom`
samples exactly `Binomial(c, a/b)` from a uniform element of `R`.
* **iPRF.** `iF σ κ x = sample κ (σ x)` with inverse `iFinv σ κ y = σ⁻¹ (sampleInv κ y)`.
* **Plinko.** Hint tables with regular, backup and promoted hints, built and maintained
through the inverse of per-block offset functions, the online query, and the correctness game.
Bits are elements of `ZMod 2`, so XOR is `+`.
All randomness is uniform over finite types; `prob P` is the probability of `P` under the
uniform distribution.
-/
namespace Plinko
open Finset
/-- Probability of an event when the outcome is uniform on a finite type. -/
noncomputable def prob {Ω : Type*} [Fintype Ω] (P : Ω → Prop) : ℝ := by
classical
exact ((Finset.univ.filter P).card : ℝ) / (Fintype.card Ω : ℝ)
/-- The load vector of `f : [n] → [m]`: the number of preimages of each bin.
For a uniform `f` this is the multinomial distribution `MN(n, m)`. -/
def loads {n m : ℕ} (f : Fin n → Fin m) : Fin m → ℕ :=
fun y => (Finset.univ.filter (fun x => f x = y)).card
/-! ## The tree sampler -/
/-- A node of the sampling tree: the balls `start, …, start + count - 1` and the bins
`low, …, high`. -/
structure Node where
start : ℕ
count : ℕ
low : ℕ
high : ℕ
deriving DecidableEq
/-- The last bin of the left child. -/
def Node.mid (v : Node) : ℕ := (v.low + v.high) / 2
/-- Left child when `s` balls go left. -/
def Node.left (v : Node) (s : ℕ) : Node := ⟨v.start, s, v.low, v.mid⟩
/-- Right child when `s` balls go left. -/
def Node.right (v : Node) (s : ℕ) : Node := ⟨v.start + s, v.count - s, v.mid + 1, v.high⟩
/-- The root for `n` balls and `m` bins. -/
def root (n m : ℕ) : Node := ⟨0, n, 0, m - 1⟩
/-- A key of the tree sampler: the requested number of balls sent left at each node
(capped at the node's ball count). -/
abbrev Splits := Node → ℕ
/-- Walk down from `v` to a leaf (`low = high`), turning left at an internal node `u` exactly
when `goLeft u s` holds, where `s = min (κ u) u.count` is the number of balls sent left. -/
def descend (κ : Splits) (goLeft : Node → ℕ → Bool) (v : Node) : Node :=
if v.low < v.high then
if goLeft v (min (κ v) v.count) then descend κ goLeft (v.left (min (κ v) v.count))
else descend κ goLeft (v.right (min (κ v) v.count))
else v
termination_by v.high - v.low
decreasing_by
all_goals
simp only [Node.left, Node.right, Node.mid]
omega
/-- Forward evaluation `S(κ, x)`: the bin reached by ball `x`. -/
def sample {n m : ℕ} [NeZero m] (κ : Splits) (x : Fin n) : Fin m :=
⟨min (descend κ (fun v s => decide (x.val < v.start + s)) (root n m)).low (m - 1), by
have := Nat.pos_of_neZero m
omega⟩
/-- Backward evaluation `S⁻¹(κ, y)`: the balls of the leaf reached by walking towards bin `y`. -/
def sampleInv {n m : ℕ} (κ : Splits) (y : Fin m) : Finset (Fin n) :=
let v := descend κ (fun u _ => decide (y.val ≤ u.mid)) (root n m)
Finset.univ.filter (fun x : Fin n => v.start ≤ x.val ∧ x.val < v.start + v.count)
/-- Finite index set for the nodes of the tree for `n` balls and `m` bins. -/
abbrev NodeIdx (n m : ℕ) := Fin (n + 1) × Fin (n + 1) × Fin m × Fin m
/-- The index of a node (coordinates are clamped; nodes of the tree are never clamped). -/
def Node.idx {n m : ℕ} [NeZero m] (v : Node) : NodeIdx n m :=
(⟨min v.start n, by omega⟩, ⟨min v.count n, by omega⟩,
⟨min v.low (m - 1), by have := Nat.pos_of_neZero m; omega⟩,
⟨min v.high (m - 1), by have := Nat.pos_of_neZero m; omega⟩)
/-- The splits produced by an ideal random function `F` from nodes to `R` and a derandomized
binomial sampler `binom c a b r` (meant to sample `Binomial(c, a/b)` from `r`): at node `v` the
left child gets each ball with probability `(mid - low + 1) / (high - low + 1)`. -/
def idealSplits {n m : ℕ} [NeZero m] {R : Type*} (binom : ℕ → ℕ → ℕ → R → ℕ)
(F : NodeIdx n m → R) : Splits :=
fun v => binom v.count (v.mid - v.low + 1) (v.high - v.low + 1) (F v.idx)
/-- `binom c a b` applied to a uniform `r ∈ R` is exactly `Binomial(c, a/b)`, for all `c ≤ n`
and `0 < a < b ≤ m`. -/
def ExactBinomial (R : Type*) [Fintype R] (binom : ℕ → ℕ → ℕ → R → ℕ) (n m : ℕ) : Prop :=
∀ c ≤ n, ∀ a b : ℕ, 0 < a → a < b → b ≤ m → ∀ k : ℕ,
prob (fun r : R => binom c a b r = k) =
(c.choose k : ℝ) * ((a : ℝ) / b) ^ k * (1 - (a : ℝ) / b) ^ (c - k)
/-! ## The invertible PRF -/
/-- `iF.F((σ, κ), x) = S(κ, σ(x))`. -/
def iF {n m : ℕ} [NeZero m] (σ : Equiv.Perm (Fin n)) (κ : Splits) (x : Fin n) : Fin m :=
sample κ (σ x)
/-- `iF.F⁻¹((σ, κ), y) = {σ⁻¹(x) : x ∈ S⁻¹(κ, y)}`. -/
def iFinv {n m : ℕ} (σ : Equiv.Perm (Fin n)) (κ : Splits) (y : Fin m) : Finset (Fin n) :=
(sampleInv κ y).map σ.symm.toEmbedding
/-! ## Plinko hint tables -/
/-- Bits; XOR is addition. -/
abbrev Bit := ZMod 2
/-- Database index `(α, β)`: entry `β` of block `α` (that is, index `α·w + β`). -/
abbrev Idx (c w : ℕ) := Fin c × Fin w
/-- A database of `c` blocks of `w` bits. -/
abbrev DB (c w : ℕ) := Idx c w → Bit
/-- Per-block offsets of the `h` hint slots: `off α j = iF.F(K[α], j)`, with the client's
inversion algorithm `inv α β`, which should equal `iF.F⁻¹(K[α], β)`. -/
structure Offsets (c w h : ℕ) where
off : Fin c → Fin h → Fin w
inv : Fin c → Fin w → Finset (Fin h)
/-- The inversion algorithm is correct. -/
def Offsets.Correct {c w h : ℕ} (O : Offsets c w h) : Prop :=
∀ α β, O.inv α β = Finset.univ.filter (fun j => O.off α j = β)
/-- Contents of a hint slot. A regular hint `(P, p)` covers the entries `(a, off a j)` for
`a ∈ P`; a backup hint `(B, p, p')` stores the parities inside and outside `B`; a promoted
hint `(P, x, p)` covers `(a, off a j)` for `a ∈ P` together with the entry `x`. -/
inductive Slot (c w : ℕ) where
| empty
| regular (P : Finset (Fin c)) (p : Bit)
| backup (B : Finset (Fin c)) (pIn pOut : Bit)
| promoted (P : Finset (Fin c)) (x : Idx c w) (p : Bit)
/-- Client state: the hint slots and the cache of answered indices, each with the slot of the
backup hint promoted when it was answered. -/
structure State (c w h : ℕ) where
slot : Fin h → Slot c w
cache : Idx c w → Option (Bit × Fin h)
/-- Adjust a slot found by inverting at entry `x` for a change `u` of `D[x]`. -/
def Slot.flip {c w : ℕ} (x : Idx c w) (u : Bit) : Slot c w → Slot c w
| .empty => .empty
| .regular P p => if x.1 ∈ P then .regular P (p + u) else .regular P p
| .backup B p q => if x.1 ∈ B then .backup B (p + u) q else .backup B p (q + u)
| .promoted P y p => if x.1 ∈ P then .promoted P y (p + u) else .promoted P y p
/-- The iPRF loop of `HintInit` and `UpdateHint`: flip every slot `j ∈ inv α β`. -/
def State.touch {c w h : ℕ} (O : Offsets c w h) (st : State c w h) (x : Idx c w) (u : Bit) :
State c w h :=
{ st with slot := fun j => if j ∈ O.inv x.1 x.2 then (st.slot j).flip x u else st.slot j }
/-- Initial slots: `nr` regular hints, then backup hints, with block sets `P j` and zero
parities. -/
def initSlots {c w h : ℕ} (nr : ℕ) (P : Fin h → Finset (Fin c)) : Fin h → Slot c w :=
fun j => if j.val < nr then .regular (P j) 0 else .backup (P j) 0 0
/-- `HintInit`: stream the database in the order `order`, updating the hints found by
inversion. -/
def hintInit {c w h : ℕ} (O : Offsets c w h) (nr : ℕ) (P : Fin h → Finset (Fin c))
(D : DB c w) (order : List (Idx c w)) : State c w h :=
order.foldl (fun st x => st.touch O x (D x)) ⟨initSlots nr P, fun _ => none⟩
/-- `UpdateHint` for a change `u` of entry `x`: the iPRF loop, then the cached answer for `x`
and the promoted hint holding `x`. -/
def updateHint {c w h : ℕ} (O : Offsets c w h) (st : State c w h) (x : Idx c w) (u : Bit) :
State c w h :=
let st' := st.touch O x u
match st.cache x with
| none => st'
| some (a, j) =>
{ slot := Function.update st'.slot j
(match st'.slot j with
| .promoted P y p => if y = x then .promoted P y (p + u) else .promoted P y p
| s => s)
cache := Function.update st'.cache x (some (a + u, j)) }
/-- `GetHint` acceptance test for slot `j` found by inverting at `x`: a regular hint must
contain block `x.1`; a promoted hint must contain `x.1` among its own blocks or hold `x`. -/
def Slot.accepts {c w : ℕ} (x : Idx c w) : Slot c w → Bool
| .regular P _ => decide (x.1 ∈ P)
| .promoted P y _ => decide (x.1 ∈ P ∨ x = y)
| _ => false
/-- A usable hint as (blocks, offsets, parity). -/
def Slot.view {c w h : ℕ} (O : Offsets c w h) (j : Fin h) :
Slot c w → Option (Finset (Fin c) × (Fin c → Fin w) × Bit)
| .regular P p => some (P, fun a => O.off a j, p)
| .promoted P y p => some (insert y.1 P, Function.update (fun a => O.off a j) y.1 y.2, p)
| _ => none
/-- Whether a slot holds an unpromoted backup hint. -/
def Slot.isBackup {c w : ℕ} : Slot c w → Bool
| .backup _ _ _ => true
| _ => false
/-- The first remaining backup slot. -/
def firstBackup {c w h : ℕ} (st : State c w h) : Option (Fin h) :=
let s := Finset.univ.filter (fun j => (st.slot j).isBackup = true)
if hs : s.Nonempty then some (s.min' hs) else none
/-- Promote backup hint `(B, p, p')` with the answered entry `x` and its value `a`. -/
def promote {c w : ℕ} (x : Idx c w) (a : Bit) : Slot c w → Slot c w
| .backup B p q => if x.1 ∈ B then .promoted Bᶜ x (q + a) else .promoted B x (p + a)
| s => s
/-- XOR of `D[a, o a]` over the blocks `a ∈ s`. -/
def parity {c w : ℕ} (D : DB c w) (s : Finset (Fin c)) (o : Fin c → Fin w) : Bit :=
∑ a ∈ s, D (a, o a)
/-- Client coins of one query: the uncached index queried instead of a cached one, the chosen
hint slot, the bit `b`, and the random offsets for the blocks outside the hint. -/
structure Coins (c w h : ℕ) where
fresh : Idx c w
hint : Fin h
b : Bool
pad : Fin c → Fin w
/-- One query for `x` (`Query`, `Answer`, `Recon`), returning the client's output (`none` on
failure) and the new state. -/
def queryStep {c w h : ℕ} (O : Offsets c w h) (st : State c w h) (D : DB c w) (x : Idx c w)
(κ : Coins c w h) : Option Bit × State c w h :=
let i := if (st.cache x).isSome then κ.fresh else x
if (st.cache i).isSome then (none, st) else
if ¬ (κ.hint ∈ O.inv i.1 i.2 ∧ (st.slot κ.hint).accepts i = true) then (none, st) else
match (st.slot κ.hint).view O κ.hint, firstBackup st with
| some (E, o, p), some j0 =>
let S := E.erase i.1
let sent : Finset (Fin c) := if κ.b then Sᶜ else S
let o' : Fin c → Fin w := fun a => if a ∈ S then o a else κ.pad a
let r0 := parity D sent o'
let r1 := parity D sentᶜ o'
let ans := p + (if κ.b then r1 else r0)
let st' : State c w h :=
{ slot := Function.update (Function.update st.slot κ.hint .empty) j0
(promote i ans (st.slot j0))
cache := Function.update st.cache i (some (ans, j0)) }
(some (if i = x then ans else ((st.cache x).map Prod.fst).getD ans), st')
| _, _ => (none, st)
/-- Operations of the correctness game: a query with the client's coins, or an update. -/
inductive Op (c w h : ℕ) where
| query (x : Idx c w) (κ : Coins c w h)
| update (x : Idx c w) (d : Bit)
/-- The correctness game `G^cor`: returns `true` (bad) if some query outputs a bit different
from the current database entry. Updates run `UpdateDB` and `UpdateHint`. -/
def corGame {c w h : ℕ} (O : Offsets c w h) : DB c w → State c w h → List (Op c w h) → Bool
| _, _, [] => false
| D, st, .query x κ :: ops =>
match queryStep O st D x κ with
| (some a, st') => decide (a ≠ D x) || corGame O D st' ops
| (none, st') => corGame O D st' ops
| D, st, .update x d :: ops =>
corGame O (Function.update D x d) (updateHint O st x (d - D x)) ops
/-- Plinko's offsets from per-block iPRF keys `K α = (σ, κ)`, with iPRF inversion. -/
def iprfOffsets {c w h : ℕ} [NeZero w] (K : Fin c → Equiv.Perm (Fin h) × Splits) :
Offsets c w h where
off α j := iF (K α).1 (K α).2 j
inv α β := iFinv (K α).1 (K α).2 β
end Plinko