Skip to content

Commit

Permalink
Improve merkle tree section, clarify serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGackstatter committed Oct 25, 2023
1 parent deb7a35 commit b5115b3
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions tips/TIP-0045/tip-0045.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@ to a Block Issuance Credits account.
## Merkle Tree

A [merkle tree](https://en.wikipedia.org/wiki/Merkle_tree) is a tree data structure that allows for efficient proofs of
inclusion.
inclusion. The serialization schemes defined here are only for a merkle proof of a value in the tree or for the tree's
merkle root. As the tree itself can be represented as the array of its leaves, it does not have its own serialization
scheme.

### Merkle Proof

A merkle tree proof is serialized as one of two possible schemas: A tree node or the hash of the value for which the
proof is computed. A `Leaf Hash` schema is additionally used in tree nodes, but cannot be at the top-level of a proof.

### Node
#### Node

<details>
<summary>Node</summary>
Expand Down Expand Up @@ -107,7 +111,7 @@ proof is computed. A `Leaf Hash` schema is additionally used in tree nodes, but
</tr>
</table>

### Leaf Hash
#### Leaf Hash

<details>
<summary>Leaf Hash</summary>
Expand Down Expand Up @@ -137,7 +141,7 @@ proof is computed. A `Leaf Hash` schema is additionally used in tree nodes, but
</tr>
</table>

### Value Hash
#### Value Hash

<details>
<summary>Value Hash</summary>
Expand Down Expand Up @@ -167,7 +171,7 @@ proof is computed. A `Leaf Hash` schema is additionally used in tree nodes, but
</tr>
</table>

### Proof Computation
#### Proof Computation

A **merkle proof** is computed as follows:

Expand Down Expand Up @@ -213,13 +217,13 @@ A **merkle proof** is computed as follows:
- `Left` set to a `Leaf Hash` with its `Hash` field set to `Hash Subtree(Leaves[0:Split])`.
- `Right` set to the result of `Compute Proof(Leaves[Split:Leaves Len], Index)`.

### Root Computation
### Merkle Root

The **merkle root** is a `ByteArray[32]` and can be computed from two different inputs:

- From a merkle tree with its array of `Leaves` by calling `Hash Subtree(Leaves)`.
- From a merkle proof by calling `Hash Component` on the top-level component of the proof, where `Hash Component` is
defined as:
- From a merkle tree with its array of `Leaves` by executing `Hash Subtree(Leaves)`.
- From a merkle proof by executing `Hash Component` on the top-level component of the proof, where
`Hash Component(Merkle Tree Component)` is a procedure defined as:
- If the component is a `Leaf Hash`, return its field `Hash`.
- If the component is a `Value Hash`, return its field `Hash`.
- If the component is a `Node` return `Hash Node(Hash Component(Left), Hash Component(Right))`.
Expand Down Expand Up @@ -592,8 +596,8 @@ that the output and its ID match which in turn renders the Inputs Commitment unn

More generally, this setup is also useful to prove that a given transaction produced an output with a certain state.

Such an Output ID Proof is a [merkle proof](#merkle-tree), with the array of the serialized `Outputs` as the `Leaves` of
the tree.
Such an Output ID Proof is a [merkle proof](#merkle-proof), with the array of the serialized `Outputs` as the `Leaves`
of the tree.

A proof is serialized as follows:

Expand Down Expand Up @@ -718,9 +722,9 @@ A proof is serialized as follows:
</tr>
</table>

By computing the [merkle root](#root-computation) from the `Output Commitment Proof` the
[Transaction ID](#transaction-id) and Output ID can be reconstructed, using the other fields of the proof. The
`Output ID Proof` can thus be used to prove that a given Output ID was derived from a given Output.
By computing the [merkle root](#merkle-root) from the `Output Commitment Proof` the [Transaction ID](#transaction-id)
and Output ID can be reconstructed, using the other fields of the proof. The `Output ID Proof` can thus be used to prove
that a given Output ID was derived from a given Output.

### Transaction ID

Expand All @@ -729,8 +733,8 @@ A Transaction ID consists of two commitments, the _Transaction Commitment_ and t

- Let `Transaction Bytes` be the concatenated serialization of the fields from `Network ID` to `Payload`.
- Let `Transaction Commitment` be the BLAKE2b-256 hash of `Transaction Bytes`.
- Let `Output Commitment` be the [merkle root](#root-computation) over the merkle tree with the serialized `Outputs` as
its `Leaves`.
- Let `Output Commitment` be the [merkle root](#merkle-root) over the merkle tree with the serialized `Outputs` as its
`Leaves`.
- Let `ID` be the BLAKE2b-256 of the concatenation of `Transaction Commitment` and `Output Commitment`.
- Construct the `Transaction ID` as the concatenation of the `ID` and the little-endian encoded `Creation Slot`.

Expand Down

0 comments on commit b5115b3

Please sign in to comment.