Merkle

Git Source

Author: Matter Labs

Note: security-contact: security@matterlabs.dev

Functions

calculateRoot

Calculate Merkle root by the provided Merkle proof. NOTE: When using this function, check that the _path length is equal to the tree height to prevent shorter/longer paths attack however, for chains settling on GW the proof includes the GW proof, so the path increases. See Mailbox for more details.

function calculateRoot(
  bytes32[] calldata _path,
  uint256 _index,
  bytes32 _itemHash
) internal pure returns (bytes32);

Parameters

NameTypeDescription
_pathbytes32[]Merkle path from the leaf to the root
_indexuint256Leaf index in the tree
_itemHashbytes32Hash of leaf content

Returns

NameTypeDescription
<none>bytes32The Merkle root

calculateRootMemory

Calculate Merkle root by the provided Merkle proof.

NOTE: When using this function, check that the _path length is appropriate to prevent shorter/longer paths attack

NOTE the tree can be joined. In this case the second tree's leaves indexes increase by the number of leaves in the first tree.

function calculateRootMemory(
  bytes32[] memory _path,
  uint256 _index,
  bytes32 _itemHash
) internal pure returns (bytes32);

Parameters

NameTypeDescription
_pathbytes32[]Merkle path from the leaf to the root
_indexuint256Leaf index in the tree.
_itemHashbytes32Hash of leaf content

Returns

NameTypeDescription
<none>bytes32The Merkle root

calculateRootPaths

Calculate Merkle root by the provided Merkle proof for a range of elements NOTE: When using this function, check that the _startPath and _endPath lengths are equal to the tree height to prevent shorter/longer paths attack

function calculateRootPaths(
  bytes32[] memory _startPath,
  bytes32[] memory _endPath,
  uint256 _startIndex,
  bytes32[] memory _itemHashes
) internal pure returns (bytes32);

Parameters

NameTypeDescription
_startPathbytes32[]Merkle path from the first element of the range to the root
_endPathbytes32[]Merkle path from the last element of the range to the root
_startIndexuint256Index of the first element of the range in the tree
_itemHashesbytes32[]Hashes of the elements in the range

Returns

NameTypeDescription
<none>bytes32The Merkle root

efficientHash

Keccak hash of the concatenation of two 32-byte words

function efficientHash(bytes32 _lhs, bytes32 _rhs)
  internal
  pure
  returns (bytes32 result);

_validatePathLengthForSingleProof

function _validatePathLengthForSingleProof(uint256 _index, uint256 _pathLength)
  private
  pure;