-
Notifications
You must be signed in to change notification settings - Fork 11.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Ernesto García <ernestognw@gmail.com>
- Loading branch information
1 parent
e831429
commit 92ff025
Showing
13 changed files
with
444 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'openzeppelin-solidity': minor | ||
--- | ||
|
||
`Hashes`: A library with commonly used hash functions. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'openzeppelin-solidity': minor | ||
--- | ||
|
||
`MerkleTree`: A data structure that allows inserting elements into a merkle tree and updating its root hash. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import {MerkleTree} from "../utils/structs/MerkleTree.sol"; | ||
|
||
contract MerkleTreeMock { | ||
using MerkleTree for MerkleTree.Bytes32PushTree; | ||
|
||
MerkleTree.Bytes32PushTree private _tree; | ||
|
||
event LeafInserted(bytes32 leaf, uint256 index, bytes32 root); | ||
|
||
function setup(uint8 _depth, bytes32 _zero) public { | ||
_tree.setup(_depth, _zero); | ||
} | ||
|
||
function push(bytes32 leaf) public { | ||
(uint256 leafIndex, bytes32 currentRoot) = _tree.push(leaf); | ||
emit LeafInserted(leaf, leafIndex, currentRoot); | ||
} | ||
|
||
function root() public view returns (bytes32) { | ||
return _tree.root(); | ||
} | ||
|
||
function depth() public view returns (uint256) { | ||
return _tree.depth(); | ||
} | ||
|
||
// internal state | ||
function nextLeafIndex() public view returns (uint256) { | ||
return _tree._nextLeafIndex; | ||
} | ||
|
||
function sides(uint256 i) public view returns (bytes32) { | ||
return _tree._sides[i]; | ||
} | ||
|
||
function zeros(uint256 i) public view returns (bytes32) { | ||
return _tree._zeros[i]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
/** | ||
* @dev Library of standard hash functions. | ||
*/ | ||
library Hashes { | ||
/** | ||
* @dev Commutative Keccak256 hash of a sorted pair of bytes32. Frequently used when working with merkle proofs. | ||
* | ||
* NOTE: Equivalent to the `standardNodeHash` in our https://github.com/OpenZeppelin/merkle-tree[JavaScript library]. | ||
*/ | ||
function commutativeKeccak256(bytes32 a, bytes32 b) internal pure returns (bytes32) { | ||
return a < b ? _efficientKeccak256(a, b) : _efficientKeccak256(b, a); | ||
} | ||
|
||
/** | ||
* @dev Implementation of keccak256(abi.encode(a, b)) that doesn't allocate or expand memory. | ||
*/ | ||
function _efficientKeccak256(bytes32 a, bytes32 b) private pure returns (bytes32 value) { | ||
/// @solidity memory-safe-assembly | ||
assembly { | ||
mstore(0x00, a) | ||
mstore(0x20, b) | ||
value := keccak256(0x00, 0x40) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.