Documentation

CslibFrontier.Crypto.Computational.MerkleTree.Prog

Merkle trees as oracle programs #

merkleRootProg, merklePathProg and merkleVerifyProg lift the binary Merkle tree operations merkleRoot, merklePath and merkleVerify (CslibFrontier.Crypto.Computational.MerkleTree, CslibFrontier.Crypto.Computational.MerkleTree.Binding) to oracle programs over the unkeyed hash interface MerkleHashSpec (List Bool → List Bool). Random-oracle constructions such as Micali's computationally sound proofs (CslibFrontier.Crypto.Computational.Kilian.Micali) and the BCS transformation (CslibFrontier.Crypto.Protocols.BCS.ROM) build their provers and verifiers from these programs.

Evaluating the programs against a deterministic hash h with OracleComp.evalPure h reproduces the pure operations at fun _ => h with the empty key (evalPure_merkleRootProg, evalPure_merklePathProg, evalPure_merkleVerifyProg). Like merkleVerify, merkleVerifyProg accepts only paths whose labels have the length of the root. Under a shared lazy random oracle whose answers all have one length, every honest opening verifies with probability one (merkleVerifyProg_merklePathProg). Under the memoized random oracle memoize dist, a verification makes at most path.length + 1 queries and records every hash step of the checked path in the final trace, as straight-line extraction (CslibFrontier.Crypto.Computational.MerkleTree.ROMExtract) requires.

Main definitions #

Main results #

Oracle-program Merkle tree #

@[reducible, inline]

Oracle specification for an unkeyed Merkle hash function List Bool → List Bool.

Oracle-program computation of leaf-level labels in a Merkle tree, hashing each leaf and padding to 2 ^ Nat.clog 2 leaves.length leaves with the empty word [].

Oracle-program computation of one parent layer in a Merkle tree.

Oracle-program computation of level j of the Merkle tree over leaves.

Oracle-program computation of the Merkle root of leaves.

One step of merklePathProg: record the sibling of the current node, then compute the parent level and move to the parent position.

Oracle-program computation of the sibling authentication path for position i.

One step of merkleInputsProg: hash the current head input and pair it with sibling.

Oracle-program computation of the hash inputs met while recomputing a root from leaf at position i along path.

Oracle-program reconstruction of the candidate root from leaf, i and path.

Oracle-program Merkle path verification at depth d: checks path.length == d, i < 2 ^ d, that every label has the length of root, and that the recomputed root equals root, as merkleVerify does.

Simulation by a pure hash #

@[simp]
theorem Cslib.Crypto.evalPure_merkleLevelProg (h : List Bool → List Bool) (leaves : List (List Bool)) (j : ℕ) :
OracleComp.evalPure h (merkleLevelProg leaves j) = merkleLevel (fun (x : List Bool) => h) [] leaves j
@[simp]
theorem Cslib.Crypto.evalPure_merkleRootProg (h : List Bool → List Bool) (leaves : List (List Bool)) :
OracleComp.evalPure h (merkleRootProg leaves) = merkleRoot (fun (x : List Bool) => h) [] leaves
@[simp]
theorem Cslib.Crypto.evalPure_merklePathProg (h : List Bool → List Bool) (leaves : List (List Bool)) (i : ℕ) :
OracleComp.evalPure h (merklePathProg leaves i) = merklePath (fun (x : List Bool) => h) [] leaves i
@[simp]
theorem Cslib.Crypto.evalPure_merkleInputsStepProg (h : List Bool → List Bool) (state : List (List Bool) × ℕ) (sibling : List Bool) :
OracleComp.evalPure h (merkleInputsStepProg state sibling) = merkleInputsStep (fun (x : List Bool) => h) [] state sibling
@[simp]
theorem Cslib.Crypto.evalPure_merkleInputsProg (h : List Bool → List Bool) (leaf : List Bool) (i : ℕ) (path : List (List Bool)) :
OracleComp.evalPure h (merkleInputsProg leaf i path) = merkleInputs (fun (x : List Bool) => h) [] leaf i path
@[simp]
theorem Cslib.Crypto.evalPure_merkleRootOfPathProg (h : List Bool → List Bool) (leaf : List Bool) (i : ℕ) (path : List (List Bool)) :
OracleComp.evalPure h (merkleRootOfPathProg leaf i path) = merkleRootOfPath (fun (x : List Bool) => h) [] leaf i path
@[simp]
theorem Cslib.Crypto.evalPure_merkleVerifyProg (h : List Bool → List Bool) (d : ℕ) (root : List Bool) (i : ℕ) (leaf : List Bool) (path : List (List Bool)) :
OracleComp.evalPure h (merkleVerifyProg d root i leaf path) = merkleVerify (fun (x : List Bool) => h) [] d root i leaf path
theorem Cslib.Crypto.evalPure_merkleVerifyProg_merklePathProg (h : List Bool → List Bool) {ℓ : ℕ} (hlen : ∀ (x : List Bool), (h x).length = ℓ) (leaves : List (List Bool)) {i : ℕ} (hi : i < 2 ^ Nat.clog 2 leaves.length) :
(OracleComp.evalPure h do let root ← merkleRootProg leaves let path ← merklePathProg leaves i merkleVerifyProg (Nat.clog 2 leaves.length) root i (leaves.getD i []) path) = true

Completeness of merkleVerifyProg against merklePathProg (pure simulation form): for any deterministic hash h whose digests all have ℓ bits and i < 2 ^ Nat.clog 2 leaves.length, merkleVerifyProg accepts merkleRootProg leaves and merklePathProg leaves i.

theorem Cslib.Crypto.merkleVerifyProg_merklePathProg {ℓ : ℕ} (dist : (q : List Bool) → PMF (MerkleHashSpec q)) (hdist : ∀ (q : List Bool), ∀ a ∈ (dist q).support, List.length a = ℓ) (leaves : List (List Bool)) {i : ℕ} (hi : i < 2 ^ Nat.clog 2 leaves.length) :
PMF.map Prod.fst (OracleComp.runWith (RandomOracle.answer dist) (do let root ← merkleRootProg leaves let path ← merklePathProg leaves i merkleVerifyProg (Nat.clog 2 leaves.length) root i (leaves.getD i []) path) fun (x : List Bool) => none) = PMF.pure true

Completeness of merkleVerifyProg against merklePathProg (lazy random-oracle form): under a shared lazy random oracle RandomOracle.answer dist whose answers all have ℓ bits, computing the root with merkleRootProg, opening position i < 2 ^ Nat.clog 2 leaves.length with merklePathProg, and verifying with merkleVerifyProg returns true with probability 1.

Query bounds and memoized traces #

theorem Cslib.Crypto.isQueryBounded_merkleVerifyProg (d : ℕ) (root : List Bool) (i : ℕ) (leaf : List Bool) (path : List (List Bool)) :
(merkleVerifyProg d root i leaf path).IsQueryBounded (path.length + 1)

merkleVerifyProg d root i leaf path makes at most path.length + 1 Merkle hash queries (one leaf query plus one query per sibling along path).

theorem Cslib.Crypto.mem_support_run_memoize_invariant {Query : Type} [BEq Query] [LawfulBEq Query] {Value β : Type} (dist : Query → PMF Value) (prog : OracleComp (fun (x : Query) => Value) β) {tr tr' : List (Query × Value)} {res : β} (hnodup : (List.map Prod.fst tr).Nodup) (hsupp : ∀ e ∈ tr, e.2 ∈ (dist e.1).support) (hmem : (res, tr') ∈ ((OracleComp.interpret (memoize dist) prog).run tr).support) :
(∀ e ∈ tr, e ∈ tr') ∧ (List.map Prod.fst tr').Nodup ∧ (∀ e ∈ tr', e.2 ∈ (dist e.1).support) ∧ ∀ (h : Query → Value), TraceConsistentWith h tr' → res = OracleComp.evalPure h prog

Multi-step invariant of memoize: running prog from tr produces tr' extending tr with Nodup keys and supported values, and res = evalPure h prog for every h consistent with tr'.

theorem Cslib.Crypto.pathQueriedInTrace_of_mem_support_merkleVerifyProg (dist : List Bool → PMF (List Bool)) (d : ℕ) (root : List Bool) (i : ℕ) (leaf : List Bool) (path : List (List Bool)) {tr tr' : List (List Bool × List Bool)} {ok : Bool} (hnodup : (List.map Prod.fst tr).Nodup) (hsupp : ∀ e ∈ tr, e.2 ∈ (dist e.1).support) (hmem : (ok, tr') ∈ ((OracleComp.interpret (memoize dist) (merkleVerifyProg d root i leaf path)).run tr).support) {h : List Bool → List Bool} (hcons : TraceConsistentWith h tr') :
PathQueriedInTrace (fun (x : List Bool) => h) [] tr' i leaf path

Running merkleVerifyProg d root i leaf path under memoize dist records every hash step of merkleInputs in the final trace tr'.

theorem Cslib.Crypto.pathQueriedInTrace_of_mem_support_mapM_merkleVerifyProg {β : Type} {dist : List Bool → PMF (List Bool)} {check : β → OracleComp MerkleHashSpec Bool} {items : List β} {tr tr' : List (List Bool × List Bool)} {checks : List Bool} (hnodup : (List.map Prod.fst tr).Nodup) (hsupp : ∀ e ∈ tr, e.2 ∈ (dist e.1).support) (hmem : (checks, tr') ∈ ((OracleComp.interpret (memoize dist) (List.mapM check items)).run tr).support) (d : ℕ) (root : β → List Bool) (pos : β → ℕ) (leaf : β → List Bool) (path : β → List (List Bool)) (hcheck : ∀ (b : β), check b = merkleVerifyProg d (root b) (pos b) (leaf b) (path b)) {h : List Bool → List Bool} (hcons : TraceConsistentWith h tr') (b : β) :
b ∈ items → PathQueriedInTrace (fun (x : List Bool) => h) [] tr' (pos b) (leaf b) (path b)

Running a list of Merkle verifications under memoize dist records every hash step of each checked opening in the final trace tr'. Item b is checked by check b, which verifies the opening (leaf b, path b) at position pos b against root b (hcheck).