-
Notifications
You must be signed in to change notification settings - Fork 40
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
feat: merkle tree verifier implementation to support all numbers of leaves #253
Changes from 7 commits
fbb2432
de3150c
5c100fb
a413aeb
946d5ee
01e669f
b880871
d854b08
61baa70
fe5bf8e
bb5462e
a8855d0
de1f723
dc33cea
a8cf071
2e6acb3
f7f7d5b
85bc59d
5215a18
5651fa5
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 |
---|---|---|
|
@@ -101,6 +101,36 @@ contract BinaryMerkleProofTest is DSTest { | |
assertTrue(isValid); | ||
} | ||
|
||
function testVerifyLeafTwoOfEight() external { | ||
bytes32 root = 0xc1ad6548cb4c7663110df219ec8b36ca63b01158956f4be31a38a88d0c7f7071; | ||
bytes32[] memory sideNodes = new bytes32[](3); | ||
sideNodes[0] = 0xb413f47d13ee2fe6c845b2ee141af81de858df4ec549a58b7970bb96645bc8d2; | ||
sideNodes[1] = 0x78850a5ab36238b076dd99fd258c70d523168704247988a94caa8c9ccd056b8d; | ||
sideNodes[2] = 0x4301a067262bbb18b4919742326f6f6d706099f9c0e8b0f2db7b88f204b2cf09; | ||
|
||
uint256 key = 1; | ||
uint256 numLeaves = 8; | ||
BinaryMerkleProof memory proof = BinaryMerkleProof(sideNodes, key, numLeaves); | ||
bytes memory data = hex"02"; | ||
bool isValid = BinaryMerkleTree.verify(root, proof, data); | ||
assertTrue(isValid); | ||
} | ||
|
||
function testVerifyLeafThreeOfEight() external { | ||
bytes32 root = 0xc1ad6548cb4c7663110df219ec8b36ca63b01158956f4be31a38a88d0c7f7071; | ||
bytes32[] memory sideNodes = new bytes32[](3); | ||
sideNodes[0] = 0x4f35212d12f9ad2036492c95f1fe79baf4ec7bd9bef3dffa7579f2293ff546a4; | ||
sideNodes[1] = 0x6bcf0e2e93e0a18e22789aee965e6553f4fbe93f0acfc4a705d691c8311c4965; | ||
sideNodes[2] = 0x4301a067262bbb18b4919742326f6f6d706099f9c0e8b0f2db7b88f204b2cf09; | ||
|
||
uint256 key = 2; | ||
uint256 numLeaves = 8; | ||
BinaryMerkleProof memory proof = BinaryMerkleProof(sideNodes, key, numLeaves); | ||
bytes memory data = hex"03"; | ||
bool isValid = BinaryMerkleTree.verify(root, proof, data); | ||
assertTrue(isValid); | ||
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. can we also include a test where we expect the verification to fail? 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. 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. It was a good idea to add invalid proofs 👍 Found a bug :D 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. actually not a bug, but an edge case: Assume you have a merkle tree of 5 leaves. When creating the proof, if you set the Verified with the implementation of tendermint and it also has the same behaviour. 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. Could you expand on this? What does 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. My bad, i meant any number from that range will result in a valid proof. The verifier won't complain about it. So if you have a tree of 5 leaves, and in the proof you set the total number to 7, the proof is still considered valid 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. |
||
} | ||
|
||
function testVerifyLeafSevenOfEight() external { | ||
bytes32 root = 0xc1ad6548cb4c7663110df219ec8b36ca63b01158956f4be31a38a88d0c7f7071; | ||
bytes32[] memory sideNodes = new bytes32[](3); | ||
|
@@ -130,4 +160,25 @@ contract BinaryMerkleProofTest is DSTest { | |
bool isValid = BinaryMerkleTree.verify(root, proof, data); | ||
assertTrue(isValid); | ||
} | ||
|
||
// Test vectors: | ||
// 0x00 | ||
// 0x01 | ||
// 0x02 | ||
// 0x03 | ||
// 0x04 | ||
function testVerifyProofOfFiveLeaves() external { | ||
bytes32 root = 0xb855b42d6c30f5b087e05266783fbd6e394f7b926013ccaa67700a8b0c5a596f; | ||
bytes32[] memory sideNodes = new bytes32[](3); | ||
sideNodes[0] = 0x96a296d224f285c67bee93c30f8a309157f0daa35dc5b87e410b78630a09cfc7; | ||
sideNodes[1] = 0x52c56b473e5246933e7852989cd9feba3b38f078742b93afff1e65ed46797825; | ||
sideNodes[2] = 0x4f35212d12f9ad2036492c95f1fe79baf4ec7bd9bef3dffa7579f2293ff546a4; | ||
|
||
uint256 key = 1; | ||
uint256 numLeaves = 5; | ||
BinaryMerkleProof memory proof = BinaryMerkleProof(sideNodes, key, numLeaves); | ||
bytes memory data = bytes(hex"01"); | ||
bool isValid = BinaryMerkleTree.verify(root, proof, data); | ||
assertTrue(isValid); | ||
} | ||
} |
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.
Do we want to revert in this library?
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.
I'll create an issue to return error codes instead, thanks 👍