Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

MerkleTree

Git Source

Provides functions for calculating Merkle roots

Functions

calculateMerkleRoot

Calculates the Merkle root of an array of SSZ-encoded data using sha256 for hashing

function calculateMerkleRoot(bytes32[] memory data)
    internal
    pure
    returns (bytes32 root);

Parameters

NameTypeDescription
databytes32[]The array of SSZ-encoded data (as bytes32 values)

Returns

NameTypeDescription
rootbytes32The Merkle root of the data

verifyMerkleLeaf

Verifies the inclusion of a leaf node in a Merkle tree using a given proof.

Follows a bottom-up approach by iteratively hashing the leaf with proof elements based on the index and comparing the computed root with the provided Merkle root.

function verifyMerkleLeaf(
    bytes32[] memory proof,
    bytes32 root,
    bytes32 leaf,
    uint256 index
) public pure returns (bool);

Parameters

NameTypeDescription
proofbytes32[]Array of bytes32 values representing the Merkle proof.
rootbytes32The Merkle root to verify the leaf node against.
leafbytes32The leaf node whose inclusion in the Merkle tree is being verified.
indexuint256The index of the leaf node in the tree, used to determine proof element order.

Returns

NameTypeDescription
<none>boolTrue if the computed Merkle root matches the provided root, indicating valid inclusion; otherwise, false.

calculateMerkleRootFromProof

Calculates the Merkle root from a leaf, proof, and index.

function calculateMerkleRootFromProof(
    bytes32[] memory proof,
    bytes32 leaf,
    uint256 index
) public pure returns (bytes32 root);

Parameters

NameTypeDescription
proofbytes32[]Array of bytes32 values representing the Merkle proof.
leafbytes32The leaf node whose inclusion in the Merkle tree is being verified.
indexuint256The index of the leaf node in the tree, used to determine proof element order.

Returns

NameTypeDescription
rootbytes32The computed Merkle root.