-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
Add a MerkleTree builder #3617
Conversation
Just realized that if someone could be creative and commit merle roots of subtrees that use the same hashings function. That would allow verification of the leafs deep inside the subtrees |
🦋 Changeset detectedLatest commit: eca9085 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
* @dev Keccak256 hash of a sorted pair of bytes32. Frequently used when working with merkle proofs. | ||
*/ | ||
function stdPairHash(bytes32 a, bytes32 b) internal pure returns (bytes32) { | ||
return a < b ? _efficientHash(a, b) : _efficientHash(b, a); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just realized this function is commutative. Should the tree work with non-commutative functions?
I guess it should, so I'd feel better if we add tests with a custom non-commutative keccak256
function. It may require updating the javascript library for testing.
If the tree expects only commutative functions we should be explicit about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should support non commutative hashes, so yes, we should test that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, we should do this after the javascript library is released.
Took the freedom to update the documentation. I left some comments and I think this still needs some work but it's looking really good in my opinion. |
I gave it a thought and I started to see why you wanted to call the function Right now I'm not convinced of For example, for a function sortedKeccak256(bytes32 a, bytes32 b) internal pure returns (bytes32) {
return a < b ? _efficientKeccak256(a, b) : _efficientKeccak256(b, a);
}
function sortedKeccak256(bytes32[] memory ptr) {
...
} I'd say users would need to decide three things: the algorithm, the sorting (for multiproofs), and some particularity of the algorithm. In this case, it's just
So far I'm feeling the best naming convention we can use would be using the name of the algorithm and that's it, but then I'd reconsider calling it I know the naming convention and variants would make it difficult for a user to decide what to use but we can wrap the common use cases: function stdHash(bytes32 a, bytes32 b) {} // Default to sorted for multiproofs
function stdHash(bytes32 a, bytes32 b, Sorting sorting) {}
function poseidon(bytes32 a) {}
function poseidon(bytes32 a, bytes32 b) {} // Default to sorted for multiproofs
functon posiedon(bytes32 a, bytes32 b, Sorting sorting) {} The main issue I see with this is the gas overhead of each hashing operation since it'd be evaluating the Sorry for bringing it up again, just want to make sure we release an API that behaves well over time, he. What do you think @Amxx? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost there!
* @dev Keccak256 hash of a sorted pair of bytes32. Frequently used when working with merkle proofs. | ||
*/ | ||
function stdPairHash(bytes32 a, bytes32 b) internal pure returns (bytes32) { | ||
return a < b ? _efficientHash(a, b) : _efficientHash(b, a); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, we should do this after the javascript library is released.
Co-authored-by: Ernesto García <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, pending to discuss whether to keep the root stored or not
Structure inspired by Tornardo Cash's commitment tree.
Fixes: #4758
PR Checklist