-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Add a MerkleTree builder #3617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a MerkleTree builder #3617
Changes from 44 commits
6b079ee
42c695b
ca83cde
f4f46ca
af7cb9c
45eae37
ac648c6
1b184c9
4863418
3c19dcf
9fc7f31
652c8a1
c74ab55
a9932c9
d8bdfd0
4d0ed52
b131354
6422af6
3ab0d21
5639d7c
24c829a
f954a98
acdc6a9
cebdc2a
d4ced94
a3a813c
ec05d19
8ecc790
ec3d96b
bcc0667
b50ebee
5b15205
b390790
e331674
91f7057
088fa8c
2d869b7
567cd3e
a13237a
03bea3e
c475bad
6a9e873
1e59539
051107b
a1dd158
7a21c4e
01c2879
2494680
0a2bfce
08c9a3c
31712fb
55853be
eca27fc
eca9085
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. |
| 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| pragma solidity ^0.8.0; | ||
|
|
||
| import {MerkleTree} from "../utils/structs/MerkleTree.sol"; | ||
|
|
||
| contract MerkleTreeMock { | ||
| using MerkleTree for MerkleTree.Bytes32MerkleTree; | ||
|
|
||
| MerkleTree.Bytes32MerkleTree private _tree; | ||
|
|
||
| constructor(uint8 _depth, bytes32 _zero) { | ||
| _tree.setup(_depth, _zero); | ||
| } | ||
|
|
||
| function insert(bytes32 leaf) public returns (uint256) { | ||
| return _tree.insert(leaf); | ||
| } | ||
|
|
||
| function getDepth() public view returns (uint256) { | ||
| return _tree.getDepth(); | ||
| } | ||
|
|
||
| function getRoot() public view returns (bytes32) { | ||
| return _tree.root; | ||
| } | ||
|
|
||
| // 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]; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,27 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // SPDX-License-Identifier: MIT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pragma solidity ^0.8.0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @dev Library of standard hash functions. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| library Hashes { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @dev Keccak256 hash of a sorted pair of bytes32. Frequently used when working with merkle proofs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Amxx marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function stdPairHash(bytes32 a, bytes32 b) internal pure returns (bytes32) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ernestognw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return a < b ? _efficientHash(a, b) : _efficientHash(b, a); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @dev Implementation of keccak256(abi.encode(a, b)) that doesn't allocate or expand memory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function _efficientHash(bytes32 a, bytes32 b) private pure returns (bytes32 value) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ernestognw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// @solidity memory-safe-assembly | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assembly { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mstore(0x00, a) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mstore(0x20, b) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| value := keccak256(0x00, 0x40) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think of?
Suggested change
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Going further, what about
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I shared some extra thoughts here. I wasn't convinced of either so I'm open to chat about this again if we can agree on a better API for future additions. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.