Documentation

CslibFrontier.Crypto.Computational.MerkleTree.Binding

Merkle trees as authenticated data structures: verification and position binding #

Cslib.Crypto.merkleRoot hashes a list of leaves and Cslib.Crypto.merklePath opens one position. This module adds the verifier and the security notion of an authenticated data structure scheme ([BS23], Section 8.9.1): a verifier merkleVerify with completeness, binding and a polynomial-time certificate, the game of Attack Game 8.2, and Theorem 8.8: under collision resistance of the hash on its domain, Merkle trees are position binding, with no loss.

Differences from [BS23]. Attack Game 8.2 is stated for a scheme over (X^n, Y) with a fixed number n of items; here:

Kilian's construction. Kilian's succinct arguments commit to a PCP proof with a hash tree and open the queried positions (Barak and Goldreich, Construction 3.4, steps P1.3 and V3). That tree differs from this one, which is [BS23]'s Figure 8.12: its leaf labels are the proof bits themselves, not their hashes, and an internal label hashes the plain concatenation ℓ_γ0 ℓ_γ1 of its children's labels, not their encPair pairing. Its prover sends both child labels at every level of a path rather than only the siblings, and the verifier checks the same hash equations along the path. Barak and Goldreich's hashing family resists non-uniform circuits.

Main definitions #

Main results #

References #

Verifying openings #

def Cslib.Crypto.merkleVerify (H : List Bool → List Bool → List Bool) (k : List Bool) (d : ℕ) (root : List Bool) (i : ℕ) (leaf : List Bool) (path : List (List Bool)) :

Accept path as an opening of position i of a depth-d tree with root root to leaf: the path has d labels, i < 2 ^ d, every label has the length of root, and the recomputed root is root. This is the verifier V of the Merkle tree as an authenticated data structure ([BS23], Definition 8.3), with the depth as an explicit argument. The length check makes the path a list of digests, as a Merkle proof is in the source (Section 8.9, p. 321): an accepted root is a digest, so the labels are too.

An opening of one position of a Merkle tree: the claimed leaf and its authentication path, one label per level from the leaf up.

  • leaf : Leaf

    The claimed leaf.

  • path : List (List Bool)

    The authentication path of the leaf.

Instances For
    theorem Cslib.Crypto.MerkleOpening.ext {Leaf : Type} {x y : MerkleOpening Leaf} (leaf : x.leaf = y.leaf) (path : x.path = y.path) :
    x = y
    def Cslib.Crypto.instDecidableEqMerkleOpening.decEq {Leaf✝ : Type} [DecidableEq Leaf✝] (x✝ x✝¹ : MerkleOpening Leaf✝) :
    Decidable (x✝ = x✝¹)
    @[instance_reducible]
    @[instance_reducible]
    instance Cslib.Crypto.instReprMerkleOpening {Leaf✝ : Type} [Repr Leaf✝] :
    @[instance_reducible]
    def Cslib.Crypto.MerkleOpening.verify (H : List Bool → List Bool → List Bool) (k : List Bool) {Leaf : Type} (encLeaf : Leaf → List Bool) (d : ℕ) (root : List Bool) (i : ℕ) (o : MerkleOpening Leaf) :

    Accept the opening o of position i of a depth-d tree with root root, whose leaves are the encodings encLeaf of the claimed values (merkleVerify on encLeaf o.leaf).

    @[simp]
    theorem Cslib.Crypto.MerkleOpening.verify_mk (H : List Bool → List Bool → List Bool) (k : List Bool) {Leaf : Type} (encLeaf : Leaf → List Bool) (d : ℕ) (root : List Bool) (i : ℕ) (leaf : Leaf) (path : List (List Bool)) :
    verify H k encLeaf d root i { leaf := leaf, path := path } = merkleVerify H k d root i (encLeaf leaf) path
    @[simp]
    theorem Cslib.Crypto.MerkleOpening.verify_id (H : List Bool → List Bool → List Bool) (k : List Bool) (d : ℕ) (root : List Bool) (i : ℕ) (o : MerkleOpening (List Bool)) :
    verify H k id d root i o = merkleVerify H k d root i o.leaf o.path
    theorem Cslib.Crypto.encMerkleOpening_path_budget {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {S✝ : γ✝ → ℕ} {g✝ : γ✝ → MerkleOpening (List Bool)} :

    The encoding of an opening with bit-string leaves: the leaf, then the path.

    theorem Cslib.Crypto.encMerkleOpening_mk_inO {γ : Type} {encIn : γ ↪ List Bool} {x0 : γ → List Bool} {T0 S0 : γ → ℕ} {x1 : γ → List (List Bool)} {T1 S1 : γ → ℕ} (h0 : Turing.MultiTapeTM.ComputableInO x0 encIn Turing.MultiTapeTM.encBits T0 S0) (h1 : Turing.MultiTapeTM.ComputableInO x1 encIn (Turing.MultiTapeTM.encList Turing.MultiTapeTM.encBits) T1 S1) :
    Turing.MultiTapeTM.ComputableInO (fun (x : γ) => { leaf := x0 x, path := x1 x }) encIn encMerkleOpening (fun (x : γ) => T0 x + T1 x) fun (x : γ) => S0 x + S1 x
    @[reducible, inline]

    The tuple of the fields of encMerkleOpening.

    theorem Cslib.Crypto.encMerkleOpening_leaf_budget {γ✝ : Type} {encIn✝ : γ✝ ↪ List Bool} {S✝ : γ✝ → ℕ} {g✝ : γ✝ → MerkleOpening (List Bool)} :
    theorem Cslib.Crypto.encMerkleOpening_mk_budget {γ : Type} {encIn : γ ↪ List Bool} {budget : γ → ℕ} {x0 : γ → List Bool} {x1 : γ → List (List Bool)} (h0 : Turing.MultiTapeTM.ComputableInSpace x0 encIn Turing.MultiTapeTM.encBits budget) (h1 : Turing.MultiTapeTM.ComputableInSpace x1 encIn (Turing.MultiTapeTM.encList Turing.MultiTapeTM.encBits) budget) :
    Turing.MultiTapeTM.ComputableInSpace (fun (x : γ) => { leaf := x0 x, path := x1 x }) encIn encMerkleOpening budget
    theorem Cslib.Crypto.merkleVerify_eq_true_iff {H : List Bool → List Bool → List Bool} {k : List Bool} {d : ℕ} {root : List Bool} {i : ℕ} {leaf : List Bool} {path : List (List Bool)} :
    merkleVerify H k d root i leaf path = true ↔ path.length = d ∧ i < 2 ^ d ∧ (∀ s ∈ path, s.length = root.length) ∧ merkleRootOfPath H k leaf i path = root

    The checks of merkleVerify, as propositions.

    theorem Cslib.Crypto.length_of_mem_merkleLevel {H : List Bool → List Bool → List Bool} {k : List Bool} {ℓ : ℕ} (hlen : ∀ (x : List Bool), (H k x).length = ℓ) {leaves : List (List Bool)} {j : ℕ} {y : List Bool} (hy : y ∈ merkleLevel H k leaves j) :
    y.length = ℓ

    Every node of the tree has ℓ bits when every digest of H k does.

    theorem Cslib.Crypto.length_merkleRoot {H : List Bool → List Bool → List Bool} {k : List Bool} {ℓ : ℕ} (hlen : ∀ (x : List Bool), (H k x).length = ℓ) (leaves : List (List Bool)) :
    (merkleRoot H k leaves).length = ℓ

    The root has ℓ bits when every digest of H k does.

    theorem Cslib.Crypto.length_of_mem_merklePath {H : List Bool → List Bool → List Bool} {k : List Bool} {ℓ : ℕ} (hlen : ∀ (x : List Bool), (H k x).length = ℓ) {leaves : List (List Bool)} {i : ℕ} (hi : i < 2 ^ Nat.clog 2 leaves.length) {s : List Bool} (hs : s ∈ merklePath H k leaves i) :
    s.length = ℓ

    The labels of an honest authentication path have ℓ bits when every digest of H k does: below the root, every sibling of a position inside the tree is a node of the tree.

    theorem Cslib.Crypto.merkleVerify_merklePath {H : List Bool → List Bool → List Bool} {k : List Bool} {ℓ : ℕ} (hlen : ∀ (x : List Bool), (H k x).length = ℓ) (leaves : List (List Bool)) {i : ℕ} (hi : i < 2 ^ Nat.clog 2 leaves.length) :
    merkleVerify H k (Nat.clog 2 leaves.length) (merkleRoot H k leaves) i (leaves.getD i []) (merklePath H k leaves i) = true

    Honest openings are accepted: when every digest of H k has the same length, every position i < 2 ^ Nat.clog 2 leaves.length of the tree over leaves, opened by its authentication path, is accepted against the root (the correctness requirement of [BS23], Definition 8.3).

    theorem Cslib.Crypto.merkleVerify_merklePath_of_clog_eq {H : List Bool → List Bool → List Bool} {k : List Bool} {ℓ : ℕ} (hlen : ∀ (x : List Bool), (H k x).length = ℓ) {leaves : List (List Bool)} {d i : ℕ} (hd : Nat.clog 2 leaves.length = d) (hi : i < 2 ^ d) :
    merkleVerify H k d (merkleRoot H k leaves) i (leaves.getD i []) (merklePath H k leaves i) = true

    Honest openings are accepted at any depth d equal to the tree's depth, for instance d = Nat.clog 2 N for N leaves, when every digest of H k has the same length.

    theorem Cslib.Crypto.length_le_of_mem_merklePath {H : List Bool → List Bool → List Bool} {k : List Bool} (hlen : ∀ (k x : List Bool), (H k x).length = k.length) {leaves : List (List Bool)} {i : ℕ} {s : List Bool} (hs : s ∈ merklePath H k leaves i) :

    Openings are short: the labels of an authentication path have at most k.length bits (a sibling outside the tree reads as []), so an opening of the tree over leaves has at most Nat.clog 2 leaves.length * k.length bits of labels.

    theorem Cslib.Crypto.isCollision_of_merkleVerify {H : List Bool → List Bool → List Bool} {k : List Bool} {d : ℕ} {root : List Bool} {i : ℕ} {leaf₁ leaf₂ : List Bool} {path₁ path₂ : List (List Bool)} (h₁ : merkleVerify H k d root i leaf₁ path₁ = true) (h₂ : merkleVerify H k d root i leaf₂ path₂ = true) (hne : leaf₁ ≠ leaf₂) (n : ℕ) :
    isCollision H (fun (x : ℕ) (x_1 : List Bool) => true) n k (extractMerkleCollision H k leaf₁ leaf₂ i path₁ path₂).1 (extractMerkleCollision H k leaf₁ leaf₂ i path₁ path₂).2 = true

    Binding of the verifier: two openings of position i to different leaves, both accepted against root at depth d, contain a collision of H k, which extractMerkleCollision finds. The length check makes the two paths equally long, as isCollision_extractMerkleCollision requires.

    theorem Cslib.Crypto.isCollision_of_merkleVerify_merklePath {H : List Bool → List Bool → List Bool} {k : List Bool} {leaves : List (List Bool)} {d i : ℕ} {leaf : List Bool} {path : List (List Bool)} (hd : Nat.clog 2 leaves.length = d) (h : merkleVerify H k d (merkleRoot H k leaves) i leaf path = true) (hne : leaf ≠ leaves.getD i []) (n : ℕ) :
    isCollision H (fun (x : ℕ) (x_1 : List Bool) => true) n k (extractMerkleCollision H k leaf (leaves.getD i []) i path (merklePath H k leaves i)).1 (extractMerkleCollision H k leaf (leaves.getD i []) i path (merklePath H k leaves i)).2 = true

    Binding against an honest tree: an opening of position i of the tree over leaves, accepted against its root at its depth, to a leaf other than leaf i, together with the honest path of i, contains a collision of H k. Unlike isCollision_of_merkleVerify, this needs no hypothesis on the lengths of the digests of H k.

    def Cslib.Crypto.merkleHashDomain (leafSpace : ℕ → List Bool → Bool) (n : ℕ) (x : List Bool) :

    The domain of the hash of a Merkle tree with leaf space leafSpace, at parameter n: the leaves x with leafSpace n x, and the pairs of n-bit digests (isDigestPair n). This is X ∪ Y² for Y = {0, 1}ⁿ in [BS23], Section 8.9 (p. 320): "The input to h is either a single element in X, or a pair of elements in Y."

    theorem Cslib.Crypto.isCollision_merkleHashDomain_of_merkleVerify {H : List Bool → List Bool → List Bool} {k : List Bool} {leafSpace : ℕ → List Bool → Bool} {n : ℕ} (hlen : ∀ (x : List Bool), (H k x).length = n) {d : ℕ} {root : List Bool} {i : ℕ} {leaf₁ leaf₂ : List Bool} {path₁ path₂ : List (List Bool)} (h₁ : merkleVerify H k d root i leaf₁ path₁ = true) (h₂ : merkleVerify H k d root i leaf₂ path₂ = true) (hleaf₁ : leafSpace n leaf₁ = true) (hleaf₂ : leafSpace n leaf₂ = true) (hne : leaf₁ ≠ leaf₂) :
    isCollision H (merkleHashDomain leafSpace) n k (extractMerkleCollision H k leaf₁ leaf₂ i path₁ path₂).1 (extractMerkleCollision H k leaf₁ leaf₂ i path₁ path₂).2 = true

    Binding of the verifier on the hash's domain: two openings of position i to different leaves of leafSpace n, both accepted against root at depth d, contain a collision of H k on merkleHashDomain leafSpace at n, when every digest of H k has n bits. The verifier's length check makes every label a digest, so the collision lies in the leaf space or among the pairs of digests.

    Position binding #

    The output of a position-binding adversary: a root and a position, then two openings of that position. The depth is the length of the first path.

    Instances For

      The encoding of MerkleBindingOutput: the root and the position as a pair, then the two openings, which is the grouping of the tuple this record replaced. Bit strings are encoded as themselves and the position in unary, as in computableInPolyTime_extractMerkleCollision, so an efficient adversary outputs only polynomially bounded positions.

      A binding output assembled from polynomial-time fields is polynomial-time.

      @[reducible, inline]

      A position-binding adversary receives the unary security parameter n and the hash key, and outputs a root, a position and two openings.

      def Cslib.Crypto.merkleBreaksBinding (H : List Bool → List Bool → List Bool) (leafSpace : ℕ → List Bool → Bool) (n : ℕ) (k : List Bool) (out : MerkleBindingOutput) :

      out breaks position binding under the key k at parameter n: both leaves lie in the leaf space leafSpace n, both openings are accepted at the same position of the same root, at the depth of the first path, and the leaves differ.

      noncomputable def Cslib.Crypto.merkleBindingGame (H : List Bool → List Bool → List Bool) (leafSpace : ℕ → List Bool → Bool) (adversary : MerkleBindingAdversary) (n : ℕ) :

      The position-binding experiment ([BS23], Attack Game 8.2, p. 324, for a keyed hash and leaf space leafSpace): sample a uniform n-bit key k, run the adversary on (n, k), and accept when its output breaks binding under k at n.

      def Cslib.Crypto.MerklePositionBinding (H : List Bool → List Bool → List Bool) (leafSpace : ℕ → List Bool → Bool) :

      Position binding of Merkle trees over H with leaf space leafSpace, a notion novel to this library: every uniform strict PPT adversary breaks binding with negligible probability. As in [BS23], Definition 8.4 and Attack Game 8.2 (p. 324), the two leaves lie in the leaf space and merkleVerify accepts only paths of digests. It is stronger than Definition 8.4 in one respect: the adversary chooses the depth of the tree, where the source's scheme has a fixed number of leaves. It follows from collision resistance of H on merkleHashDomain leafSpace, the assumption of Theorem 8.8 (CollisionResistantOn.merklePositionBinding). Adversaries write positions in unary, so this covers polynomially bounded positions.

      The collision finder built from a position-binding adversary: run it, and extract a collision from its two openings with extractMerkleCollision.

      theorem Cslib.Crypto.isCollision_of_merkleBreaksBinding {H : List Bool → List Bool → List Bool} {leafSpace : ℕ → List Bool → Bool} (hlen : ∀ (k x : List Bool), (H k x).length = k.length) {n : ℕ} {k : List Bool} (hk : k.length = n) {out : MerkleBindingOutput} (h : merkleBreaksBinding H leafSpace n k out = true) :
      have col := extractMerkleCollision H k out.first.leaf out.second.leaf out.position out.first.path out.second.path; isCollision H (merkleHashDomain leafSpace) n k col.1 col.2 = true

      A binding break yields a collision on the hash's domain: if out breaks position binding under an n-bit key k, and H k returns n-bit digests, then extractMerkleCollision finds two different words of merkleHashDomain leafSpace n with the same hash under k.

      theorem Cslib.Crypto.succProbability_merkleBindingGame_le {H : List Bool → List Bool → List Bool} {leafSpace : ℕ → List Bool → Bool} (hlen : ∀ (k x : List Bool), (H k x).length = k.length) (adversary : MerkleBindingAdversary) (n : ℕ) :

      The reduction loses nothing: at every security parameter, a position-binding adversary succeeds at most as often as its collision finder finds a collision on the hash's domain, when H k returns k.length-bit digests.

      The reduction is efficient: the collision finder of a strict PPT binding adversary is strict PPT, for a polynomial-time H that returns k.length bits.

      theorem Cslib.Crypto.MerklePositionBinding.negligible {H : List Bool → List Bool → List Bool} {leafSpace : ℕ → List Bool → Bool} (h : MerklePositionBinding H leafSpace) (adversary : MerkleBindingAdversary) (hadversary : Probability.IsPPT encMerkleBindingOutput adversary) :
      Negligible fun (n : ℕ) => Game.succProbability (merkleBindingGame H leafSpace adversary n).eval

      Position binding gives negligible success probability to every strict PPT adversary.

      theorem Cslib.Crypto.MerklePositionBinding.of_negligible {H : List Bool → List Bool → List Bool} {leafSpace : ℕ → List Bool → Bool} (h : ∀ (adversary : ℕ → List Bool → ProbComp MerkleBindingOutput), Probability.IsPPT encMerkleBindingOutput adversary → Negligible fun (n : ℕ) => Game.succProbability (merkleBindingGame H leafSpace adversary n).eval) :

      Establish position binding by bounding every strict PPT adversary's success probability.

      Merkle trees are position binding under collision resistance of the hash on its domain ([BS23], Theorem 8.8, Section 8.9.1, p. 324): if H is collision resistant on the leaf space and the pairs of digests (merkleHashDomain leafSpace, the source's X ∪ Y², Section 8.9, p. 320), then no uniform strict PPT adversary opens a position of a Merkle tree over H to two different leaves of leafSpace except with negligible probability. H is a polynomial-time keyed hash with digests of the key's length.

      This is Theorem 8.8 for keyed hashes and uniform adversaries, with these differences:

      • the hash takes a uniform n-bit key, given to the adversary; footnote 1 on p. 290 says that this keyed form is the same as the source's system parameter;
      • digests have exactly n bits, so Y = {0, 1}ⁿ;
      • the leaf space is any predicate leafSpace, of which the source's finite, efficiently recognizable and polynomially bounded leaf spaces (Definition 2.9, p. 30) are instances;
      • the adversary chooses the depth of the tree, which makes the game stronger than the source's, whose scheme has a fixed number of leaves; positions are 0 ≤ i < 2 ^ d, the padding positions included, and written in unary, which covers polynomially bounded positions.

      The reduction is merkleCollisionFinder, with no loss (succProbability_merkleBindingGame_le), and it is strict PPT (isPPT_merkleCollisionFinder).

      A collision-resistant HashFamily, collision resistant on all words, gives position binding for every leaf space. This is CollisionResistantOn.merklePositionBinding through CollisionResistantOn.mono: collision resistance on all words implies it on merkleHashDomain leafSpace.