MerkleMath
Functions
proofRoot
Calculates the merkle root for the given leaf and merkle proof.
Will revert if proof length exceeds the tree height.
function proofRoot(uint256 index, bytes32 leaf, bytes32[] memory proof, uint256 height)
internal
pure
returns (bytes32 root_);
Parameters
Name | Type | Description |
---|---|---|
index | uint256 | Index of leaf in tree |
leaf | bytes32 | Leaf of the merkle tree |
proof | bytes32[] | Proof of inclusion of leaf in the tree |
height | uint256 | Height of the merkle tree |
Returns
Name | Type | Description |
---|---|---|
root_ | bytes32 | Calculated Merkle Root |
getParent
Calculates the parent of a node on the path from one of the leafs to root.
Apply unchecked to all ++h operations
function getParent(bytes32 node, bytes32 sibling, uint256 leafIndex, uint256 nodeHeight)
internal
pure
returns (bytes32 parent);
Parameters
Name | Type | Description |
---|---|---|
node | bytes32 | Node on a path from tree leaf to root |
sibling | bytes32 | Sibling for a given node |
leafIndex | uint256 | Index of the tree leaf |
nodeHeight | uint256 | "Level height" for node (ZERO for leafs, ORIGIN_TREE_HEIGHT for root) |
getParent
Calculates the parent of tow nodes in the merkle tree.
We use implementation with H(0,0) = 0 This makes EVERY empty node in the tree equal to ZERO, saving us from storing H(0,0), H(H(0,0), H(0, 0)), and so on
function getParent(bytes32 leftChild, bytes32 rightChild) internal pure returns (bytes32 parent);
Parameters
Name | Type | Description |
---|---|---|
leftChild | bytes32 | Left child of the calculated node |
rightChild | bytes32 | Right child of the calculated node |
Returns
Name | Type | Description |
---|---|---|
parent | bytes32 | Value for the node having above mentioned children |
calculateRoot
Calculates merkle root for a list of given leafs.
Merkle Tree is constructed by padding the list with ZERO values for leafs until list length is 2**height
.
Merkle Root is calculated for the constructed tree, and then saved in leafs[0]
.
Note:
leafs
values are overwritten in the process to avoid excessive memory allocations.- Caller is expected not to reuse
hashes
list after the call, and only useleafs[0]
value, which is guaranteed to contain the calculated merkle root.- root is calculated using the
H(0,0) = 0
Merkle Tree implementation. See MerkleTree.sol for details.
Amount of leaves should be at most 2**height
function calculateRoot(bytes32[] memory hashes, uint256 height) internal pure;
Parameters
Name | Type | Description |
---|---|---|
hashes | bytes32[] | List of leafs for the merkle tree (to be overwritten) |
height | uint256 | Height of the Merkle Tree to construct |
calculateProof
Generates a proof of inclusion of a leaf in the list. If the requested index is outside of the list range, generates a proof of inclusion for an empty leaf (proof of non-inclusion). The Merkle Tree is constructed by padding the list with ZERO values until list length is a power of two AND index is in the extended list range. For example:
hashes.length == 6
and0 <= index <= 7
will "extend" the list to 8 entries.hashes.length == 6
and7 < index <= 15
will "extend" the list to 16 entries.
Note:
leafs
values are overwritten in the process to avoid excessive memory allocations. Caller is expected not to reusehashes
list after the call.
h, leftIndex, rightIndex and levelLength never overflow
function calculateProof(bytes32[] memory hashes, uint256 index) internal pure returns (bytes32[] memory proof);
Parameters
Name | Type | Description |
---|---|---|
hashes | bytes32[] | List of leafs for the merkle tree (to be overwritten) |
index | uint256 | Leaf index to generate the proof for |
Returns
Name | Type | Description |
---|---|---|
proof | bytes32[] | Generated merkle proof |
getHeight
Returns the height of the tree having a given amount of leafs.
h, leftIndex, rightIndex and levelLength never overflow
function getHeight(uint256 leafs) internal pure returns (uint256 height);