One-round and -round ORAM in the balls-in-bins model
DefinitionOneRoundORAMThis file defines the model used in all statements of the mission.
Schemes. A -round ORAM scheme Scheme k B₁ B₂ St R N₁ N₂ simulates a virtual array of cells over using a physical array of cells over . Physical cells hold Option B₂, and every cell starts as (none). The scheme has an initial state in St. To process an operation (Op.rd a or Op.wr a d), it performs rounds of reads. In round it reads the addresses access i dr op st ω, where dr is the list of blocks returned by earlier rounds. The function out then receives all blocks read and returns three things: the answer (none = ), a list of physical writes, applied in order, and the new state. A one-round scheme is the case .
Games. The randomness is drawn uniformly from the finite nonempty type R, once per game. Running a list of operations from the initial configuration produces the server's transcript, which lists, for each operation, the physical addresses read and the addresses written.
- The correctness advantage of a list of operations is the probability that some read returns a value other than the last value written to that virtual address ( if there is none).
- An obliviousness adversary is a list of left/right query pairs (two reads or two writes) together with a predicate on transcripts. Its advantage is .
Resources.
- Worst-case overhead : in every situation, at most reads and at most writes per operation.
- Amortized overhead : every list of operations, under every , produces at most physical reads and writes in total.
Balls-in-bins. Physical blocks are B₁ ⊕ (Fin w₂ → Bool), so a cell holds a ball or a -bit non-ball string. A state consists of bits and registers, each holding a ball or . The state size is of the number of states, which equals . A scheme is balls-in-bins when three conditions hold:
- Its registers start empty.
- From every configuration, one step never increases the number of copies of a ball in memory and registers, except that the ball being written may gain one copy. Balls are therefore moved, never duplicated or created.
- Any ball it returns was read in that step or sits in a register.
Restrictions.
- -partition-restricted: all rounds except the last read only inside a fixed set of at most physical cells.
- Static: a read operation leaves physical memory unchanged.
import Mathlib
/-!
# One-round and `k`-round ORAM in the balls-in-bins model
Syntax, games and resource measures for ORAM schemes interacting with an
array-only server, following Cash–Drucker–Hoover, *A Lower Bound for One-Round
Oblivious RAM* (TCC 2020), Definitions 1–6, 8–10.
-/
namespace OneRoundORAM
open Classical
/-- A memory operation on an array of `N` cells holding blocks from `B`:
a read of address `a`, or a write of block `d` to address `a`. -/
inductive Op (N : ℕ) (B : Type) where
| rd (a : Fin N)
| wr (a : Fin N) (d : B)
/-- The block written by an operation (`none` for a read). -/
def Op.ball {N : ℕ} {B : Type} : Op N B → Option B
| .rd _ => none
| .wr _ d => some d
/-- A `k`-round ORAM scheme (Definitions 1 and 8) simulating a virtual array of
`N₁` cells over `B₁` using a physical array of `N₂` cells over `B₂`, with state
space `St` and randomness space `R`. Physical cells hold `Option B₂`, where
`none` is the initial empty value `⊥`.
* `init` is the initial state.
* `access i dr op st ω` is the list of physical addresses read in round `i`,
given the blocks `dr` returned by the reads of earlier rounds.
* `out dr op st ω = (dout, w, st')` returns the answer `dout` (`none` = `⊥`),
the list `w` of physical writes (address, block), applied in order, and the
new state. -/
structure Scheme (k : ℕ) (B₁ B₂ St R : Type) (N₁ N₂ : ℕ) where
init : St
access : Fin k → List (Option B₂) → Op N₁ B₁ → St → R → List (Fin N₂)
out : List (Option B₂) → Op N₁ B₁ → St → R → Option B₁ × List (Fin N₂ × B₂) × St
/-- A one-round ORAM scheme (Definition 1). -/
abbrev OneRound (B₁ B₂ St R : Type) (N₁ N₂ : ℕ) := Scheme 1 B₁ B₂ St R N₁ N₂
/-- Configuration of the game: client state and physical memory. -/
structure Config (St B₂ : Type) (N₂ : ℕ) where
st : St
mem : Fin N₂ → Option B₂
/-- Apply a list of physical writes, in order. -/
def applyWrites {B₂ : Type} {N₂ : ℕ} (M : Fin N₂ → Option B₂) (w : List (Fin N₂ × B₂)) :
Fin N₂ → Option B₂ :=
w.foldl (fun M x => Function.update M x.1 (some x.2)) M
variable {k : ℕ} {B₁ B₂ St R : Type} {N₁ N₂ : ℕ}
/-- The accumulated physical reads of all `k` rounds (the `Access` algorithm of
Section 6; for `k = 1` this is just `Access`). -/
def Scheme.reads (O : Scheme k B₁ B₂ St R N₁ N₂) (M : Fin N₂ → Option B₂) (op : Op N₁ B₁)
(st : St) (ω : R) : List (Fin N₂) :=
(List.finRange k).foldl (fun rds i => rds ++ O.access i (rds.map M) op st ω) []
/-- One oracle call: returns `(dout, rds, wrts)` and the new configuration. -/
def Scheme.step (O : Scheme k B₁ B₂ St R N₁ N₂) (ω : R) (c : Config St B₂ N₂) (op : Op N₁ B₁) :
(Option B₁ × List (Fin N₂) × List (Fin N₂)) × Config St B₂ N₂ :=
let rds := O.reads c.mem op c.st ω
let o := O.out (rds.map c.mem) op c.st ω
((o.1, rds, o.2.1.map Prod.fst), ⟨o.2.2, applyWrites c.mem o.2.1⟩)
/-- Initial configuration: initial state, all physical cells `⊥`. -/
def Scheme.initConfig (O : Scheme k B₁ B₂ St R N₁ N₂) : Config St B₂ N₂ :=
⟨O.init, fun _ => none⟩
/-- Run a sequence of operations; returns per-operation `(dout, rds, wrts)`. -/
def Scheme.runFrom (O : Scheme k B₁ B₂ St R N₁ N₂) (ω : R) :
Config St B₂ N₂ → List (Op N₁ B₁) → List (Option B₁ × List (Fin N₂) × List (Fin N₂))
| _, [] => []
| c, op :: ops => (O.step ω c op).1 :: O.runFrom ω (O.step ω c op).2 ops
/-- Final configuration after running a sequence of operations. -/
def Scheme.configAfter (O : Scheme k B₁ B₂ St R N₁ N₂) (ω : R) :
Config St B₂ N₂ → List (Op N₁ B₁) → Config St B₂ N₂
| c, [] => c
| c, op :: ops => O.configAfter ω (O.step ω c op).2 ops
/-- The server's view: physical addresses read and written by each operation. -/
def Scheme.transcript (O : Scheme k B₁ B₂ St R N₁ N₂) (ω : R) (ops : List (Op N₁ B₁)) :
List (List (Fin N₂) × List (Fin N₂)) :=
(O.runFrom ω O.initConfig ops).map (·.2)
/-- Correctness failure (game `G^cor`, Figure 1): some read returns a value
different from the last value written to that virtual address (`⊥` if none). -/
def Scheme.failsFrom (O : Scheme k B₁ B₂ St R N₁ N₂) (ω : R) :
Config St B₂ N₂ → (Fin N₁ → Option B₁) → List (Op N₁ B₁) → Prop
| _, _, [] => False
| c, M₁, op :: ops =>
(match op with
| .rd a => (O.step ω c op).1.1 ≠ M₁ a
| .wr _ _ => False) ∨
O.failsFrom ω (O.step ω c op).2
(match op with
| .rd _ => M₁
| .wr a d => Function.update M₁ a (some d)) ops
/-- Probability of an event under the uniform distribution on a finite type. -/
noncomputable def prob {Ω : Type} [Fintype Ω] (P : Ω → Prop) : ℝ :=
((Finset.univ.filter P).card : ℝ) / Fintype.card Ω
/-- Correctness advantage (Definition 2) of the adversary issuing `ops`. -/
noncomputable def Scheme.corAdv [Fintype R] (O : Scheme k B₁ B₂ St R N₁ N₂)
(ops : List (Op N₁ B₁)) : ℝ :=
prob (fun ω : R => O.failsFrom ω O.initConfig (fun _ => none) ops)
/-- A left/right query of the obliviousness game: two reads or two writes. -/
inductive QPair (N₁ : ℕ) (B₁ : Type) where
| rd (a₀ a₁ : Fin N₁)
| wr (a₀ : Fin N₁) (d₀ : B₁) (a₁ : Fin N₁) (d₁ : B₁)
/-- The operation processed in world `b` (`false` = left, `true` = right). -/
def QPair.pick {N₁ : ℕ} {B₁ : Type} : QPair N₁ B₁ → Bool → Op N₁ B₁
| .rd a₀ a₁, b => .rd (if b then a₁ else a₀)
| .wr a₀ d₀ a₁ d₁, b => if b then .wr a₁ d₁ else .wr a₀ d₀
/-- A (non-adaptive) adversary for the obliviousness game (Figure 2): a list of
left/right queries and a guess computed from the server's view. -/
structure OblAdversary (N₁ N₂ : ℕ) (B₁ : Type) where
queries : List (QPair N₁ B₁)
guess : List (List (Fin N₂) × List (Fin N₂)) → Prop
/-- `Pr[G^{obl-b}_O(A) = 1]`. -/
noncomputable def Scheme.oblProb [Fintype R] (O : Scheme k B₁ B₂ St R N₁ N₂)
(A : OblAdversary N₁ N₂ B₁) (b : Bool) : ℝ :=
prob (fun ω : R => A.guess (O.transcript ω (A.queries.map (·.pick b))))
/-- Obliviousness advantage (Definition 3). -/
noncomputable def Scheme.oblAdv [Fintype R] (O : Scheme k B₁ B₂ St R N₁ N₂)
(A : OblAdversary N₁ N₂ B₁) : ℝ :=
O.oblProb A true - O.oblProb A false
/-- Worst-case overhead `p` (Definition 4): every operation reads at most `p`
physical cells and writes at most `p` physical cells. -/
def Scheme.WorstCaseOverhead (O : Scheme k B₁ B₂ St R N₁ N₂) (p : ℕ) : Prop :=
(∀ M op st ω, (O.reads M op st ω).length ≤ p) ∧
(∀ dr op st ω, (O.out dr op st ω).2.1.length ≤ p)
/-- Amortized overhead `p` (Definition 4): for every sequence of `Q` operations
and every choice of randomness, at most `p * Q` physical operations in total. -/
def Scheme.AmortizedOverhead (O : Scheme k B₁ B₂ St R N₁ N₂) (p : ℝ) : Prop :=
∀ (ops : List (Op N₁ B₁)) (ω : R),
((((O.transcript ω ops).map (fun t => t.1.length + t.2.length)).sum : ℕ) : ℝ) ≤ p * ops.length
/-- Balls-in-bins state space: `m` bits and `r` registers holding a ball or `⊥`. -/
abbrev BState (m r : ℕ) (B₁ : Type) := (Fin m → Bool) × (Fin r → Option B₁)
/-- Balls-in-bins physical blocks: balls, or `w₂`-bit non-ball strings. -/
abbrev BBlock (B₁ : Type) (w₂ : ℕ) := B₁ ⊕ (Fin w₂ → Bool)
/-- State size `log₂ |StSp|` (Definition 4). -/
noncomputable def stateSize (m r : ℕ) (B₁ : Type) [Fintype B₁] : ℝ :=
Real.logb 2 (Fintype.card (BState m r B₁))
/-- Number of copies of ball `b` in physical memory and registers. -/
noncomputable def ballCount {m r w₂ N₂ : ℕ} (c : Config (BState m r B₁) (BBlock B₁ w₂) N₂)
(b : B₁) : ℕ :=
(Finset.univ.filter (fun i => c.mem i = some (Sum.inl b))).card +
(Finset.univ.filter (fun j => c.st.2 j = some b)).card
/-- Balls-in-bins without duplication (Definition 5): registers start empty; in
every step, from every configuration, no ball gains copies except that the
written ball may gain one copy (so balls are moved, not duplicated); and
a returned ball is among the blocks read or in a register. -/
def Scheme.IsBallsInBins {m r w₂ : ℕ}
(O : Scheme k B₁ (BBlock B₁ w₂) (BState m r B₁) R N₁ N₂) : Prop :=
O.init.2 = (fun _ => none) ∧
∀ (ω : R) (c : Config (BState m r B₁) (BBlock B₁ w₂) N₂) (op : Op N₁ B₁),
(∀ b : B₁, ballCount (O.step ω c op).2 b ≤
ballCount c b + (if op.ball = some b then 1 else 0)) ∧
(∀ b : B₁, (O.step ω c op).1.1 = some b →
some (Sum.inl b) ∈ (O.reads c.mem op c.st ω).map c.mem ∨ ∃ j, c.st.2 j = some b)
/-- `ℓ`-partition-restricted (Definition 9): all rounds but the last read only
inside a fixed set of at most `ℓ` physical cells. -/
def Scheme.PartitionRestricted (O : Scheme k B₁ B₂ St R N₁ N₂) (ℓ : ℕ) : Prop :=
∃ P : Finset (Fin N₂), P.card ≤ ℓ ∧
∀ (i : Fin k), i.val + 1 < k → ∀ dr op st ω, ∀ a ∈ O.access i dr op st ω, a ∈ P
/-- Static (Definition 10): a read operation leaves physical memory unchanged. -/
def Scheme.IsStatic (O : Scheme k B₁ B₂ St R N₁ N₂) : Prop :=
∀ (M : Fin N₂ → Option B₂) (a : Fin N₁) (st : St) (ω : R),
applyWrites M (O.out ((O.reads M (.rd a) st ω).map M) (.rd a) st ω).2.1 = M
end OneRoundORAM
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Definition bundle (namespace OneRoundORAM). All notions below are parameterised by a number of rounds , a virtual block type , a physical block type , a client-state type , a randomness type , a virtual array size and a physical array size . Write .
Operations. An operation is either a read with , or a write with , . The ball of an operation is for a read and for .
Schemes. A -round scheme consists of
- an initial state ;
- for each round a map giving a finite list (repetitions allowed) of physical addresses in , where is a list of values in ;
- a map with , a finite list of pairs (address in , block in ), and .
A one-round scheme is the case . Physical memory is a function ; a configuration is a pair . Applying to sets , then , etc. (later writes to the same cell win). Note a write always stores a genuine block, never , so a cell can never be reset to .
Reads and one step. For memory , operation op, state and randomness , the accumulated read list is built round by round: start with the empty list ; for set , where is the list of current memory values at the addresses of . The read list is (empty if ). One step from on op computes , records the triple , and moves to . The initial configuration is . The same is used for every step of a run (one random draw per whole game, not per operation). Running a list of operations threads configurations through steps; the transcript is the list, one entry per operation, of (read-address list, write-address list) — values written and round boundaries are not included.
Correctness. For a fixed list of operations, a run fails if at some read in the list, the returned differs from the value most recently written to virtual address earlier in the list, or from if none was. With finite, is the uniform probability over , defined as (this is if is empty, by the division-by-zero convention). The correctness advantage of a fixed (non-adaptive) operation list is .
Obliviousness. A query pair is either two reads or two writes (a read can never be paired with a write). An adversary is a fixed finite list of query pairs together with an arbitrary predicate on transcripts. For , , and
a signed difference (no absolute value; since may be complemented, a bound for all is equivalent to for all ). Adversaries are non-adaptive and deterministic.
Overhead. Worst-case overhead : for every memory , operation, state and (reachable or not), the read list has length ; and for every list of values (of any length), operation, state and , the write list of has length . Amortized overhead : for every finite operation list of length (including ) and every , the total, over the transcript, of (number of reads + number of writes) is .
Balls-in-bins types. ( bits and registers). (a ball, or a -bit non-ball string; for there is exactly one non-ball string). With finite, the state size is
For a configuration, is the number of memory cells holding the ball plus the number of registers holding .
Balls-in-bins without duplication. A scheme with state and blocks is balls-in-bins if (i) all registers of are (the bits are unconstrained), and (ii) for every , every configuration (reachable or not) and every operation: for every ball , ; and if then occurs among the memory values at the read addresses or in some register of the state before the step. Nothing restricts the bits, the non-ball strings, or where a ball is placed: since only per-ball counts are compared, a step may write ball into a cell it did not read, provided it simultaneously destroys another copy of (e.g. by overwriting an unread cell known to hold ). Repeated writes of the same ball value by the user may legitimately create several copies.
Partition-restricted. is -partition-restricted if there is a set with such that every round with (all rounds but the last), for every input, reads only addresses in . For this is vacuous ( works for any ).
Static. is static if for every memory , address , state and , applying the writes produced on to yields exactly again (writes on reads are allowed, and still appear in the transcript and in overhead counts, as long as each written cell ends with its prior value; the state may change).
Docstring remarks. The docstrings agree with the code. The phrase "balls are only moved, never copied or created" in the balls-in-bins docstring is a gloss of the count condition; the code enforces only the counting inequality and the output-provenance condition, not that a placed ball came from a read cell.