@@ -13,12 +13,12 @@ contract MerkleTest is Test, MurkyBase {
1313 }
1414
1515 /// @notice Hashing function for Murky
16- function hashLeafPairs (bytes32 left , bytes32 right ) public pure override returns (bytes32 _hash ) {
17- _hash = keccak256 (abi.encode (left, right));
16+ function hashLeafPairs (bytes32 left , bytes32 right ) public pure override returns (bytes32 ) {
17+ return keccak256 (abi.encode (left, right));
1818 }
1919
20- function testCheckMembershipSingleLeaf (bytes32 leaf , uint256 index ) external {
21- vm.assume (index != 0 );
20+ function test_checkMembershipSingleLeaf (bytes32 leaf , bytes32 wrongRoot , uint256 index ) external {
21+ vm.assume (index != 0 && wrongRoot != leaf );
2222 bytes32 randomDataHash = keccak256 (abi.encode (leaf));
2323 bytes32 [] memory proof = new bytes32 [](0 );
2424
@@ -32,9 +32,11 @@ contract MerkleTest is Test, MurkyBase {
3232 assertFalse (merkleUser.checkMembership (leaf, index, leaf, proof));
3333 // check with wrong leaf and wrong index
3434 assertFalse (merkleUser.checkMembership (randomDataHash, index, leaf, proof));
35+ // check with wrong index, wrong leaf and wrong root
36+ assertFalse (merkleUser.checkMembership (randomDataHash, index, wrongRoot, proof));
3537 }
3638
37- function testCheckMembership (bytes32 [] memory leaves , uint256 index , uint256 wrongIndex , bytes32 wrongRoot )
39+ function test_checkMembership (bytes32 [] memory leaves , uint256 index , uint256 wrongIndex , bytes32 wrongRoot )
3840 external
3941 {
4042 vm.assume (leaves.length > 1 && index < leaves.length && wrongIndex != index);
@@ -58,10 +60,41 @@ contract MerkleTest is Test, MurkyBase {
5860 assertFalse (merkleUser.checkMembership (randomDataHash, wrongIndex, wrongRoot, proof));
5961 }
6062
61- function testCheckMembershipLargeTree (bytes32 [] memory leaves , uint256 index , uint256 wrongIndex , bytes32 wrongRoot )
63+ function test_checkMembershipLargeTree (bytes32 [] memory leaves , uint256 index , uint256 wrongIndex , bytes32 wrongRoot )
6264 external
6365 {
64- vm.assume (leaves.length > 1 && index < leaves.length && wrongIndex != index);
66+ vm.assume (leaves.length >= 128 && index < leaves.length && wrongIndex != index);
67+ bytes32 root = getRoot (leaves);
68+ vm.assume (wrongRoot != root);
69+ bytes32 [] memory proof = getProof (leaves, index);
70+ bytes32 leaf = leaves[index];
71+ bytes32 randomDataHash = keccak256 (abi.encode (leaf));
72+
73+ // should return true for leaf and false for random hash
74+ assertTrue (merkleUser.checkMembership (leaf, index, root, proof));
75+ // check with wrong leaf
76+ assertFalse (merkleUser.checkMembership (randomDataHash, index, root, proof));
77+ // check with fixed wrong index
78+ assertFalse (merkleUser.checkMembership (leaf, leaves.length , root, proof));
79+ // check with wrong index
80+ assertFalse (merkleUser.checkMembership (leaf, wrongIndex, root, proof));
81+ // check with wrong index and wrong leaf
82+ assertFalse (merkleUser.checkMembership (randomDataHash, wrongIndex, root, proof));
83+ // check with wrong index, wrong leaf and wrong root
84+ assertFalse (merkleUser.checkMembership (randomDataHash, wrongIndex, wrongRoot, proof));
85+ }
86+
87+ function test_checkMembershipLargeTree2 (bytes32 [256 ] memory c_leaves , uint256 index , uint256 wrongIndex , bytes32 wrongRoot )
88+ external
89+ {
90+ vm.assume (index < c_leaves.length && wrongIndex != index);
91+ bytes32 [] memory leaves = new bytes32 [](c_leaves.length );
92+ for (uint256 i = 0 ; i < c_leaves.length ; ) {
93+ leaves[i] = c_leaves[i];
94+ unchecked {
95+ ++ i;
96+ }
97+ }
6598 bytes32 root = getRoot (leaves);
6699 vm.assume (wrongRoot != root);
67100 bytes32 [] memory proof = getProof (leaves, index);
@@ -93,7 +126,6 @@ contract MerkleUser {
93126 pure
94127 returns (bool )
95128 {
96- bool r = Merkle.verify (proof, rootHash, index, leaf);
97- return r;
129+ return Merkle.verify (proof, rootHash, index, leaf);
98130 }
99131}
0 commit comments