merkle_root

Function merkle_root 

Source
pub fn merkle_root(leaves: impl AsMut<[[u8; 32]]>) -> Option<[u8; 32]>
Expand description

Calculates the Merkle root for the given leaves.

Equivalent to tree_hash in monero-core: https://github.com/monero-project/monero/blob/893916ad091a92e765ce3241b94e706ad012b62a /src/crypto/tree-hash.c#L62

Monero’s Merkle trees have two notable properties:

  1. Merkle trees are unbalanced, causing each leaf to only be included once with a unique index, without introducing padding.
  2. The leaves of a Merkle tree are not distinguished from its branches. This would allow proving a branch as a leaf, or claiming a leaf is a branch. In order to safely perform proofs with a tree’s root, the amount of leaves within the Merkle tree MUST be known. Thankfully, Monero uses this to commit to the transactions within a block when calculating its hash, and the amount of transactions is additionally committed to. Alternatively, one could assume every valid transaction’s serialization will be longer than 64 bytes.

This function accepts a mutable slice for the leaves, using it as scratch space for the computation of the leaves. The value of this scratch space is undefined after the operation completes.

This function returns None if the tree is empty and Some otherwise.