@@ -13,12 +13,12 @@ contract MerkleTest is Test, MurkyBase {
13
13
}
14
14
15
15
/// @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));
18
18
}
19
19
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 );
22
22
bytes32 randomDataHash = keccak256 (abi.encode (leaf));
23
23
bytes32 [] memory proof = new bytes32 [](0 );
24
24
@@ -32,9 +32,11 @@ contract MerkleTest is Test, MurkyBase {
32
32
assertFalse (merkleUser.checkMembership (leaf, index, leaf, proof));
33
33
// check with wrong leaf and wrong index
34
34
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));
35
37
}
36
38
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 )
38
40
external
39
41
{
40
42
vm.assume (leaves.length > 1 && index < leaves.length && wrongIndex != index);
@@ -58,10 +60,41 @@ contract MerkleTest is Test, MurkyBase {
58
60
assertFalse (merkleUser.checkMembership (randomDataHash, wrongIndex, wrongRoot, proof));
59
61
}
60
62
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 )
62
64
external
63
65
{
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
+ }
65
98
bytes32 root = getRoot (leaves);
66
99
vm.assume (wrongRoot != root);
67
100
bytes32 [] memory proof = getProof (leaves, index);
@@ -93,7 +126,6 @@ contract MerkleUser {
93
126
pure
94
127
returns (bool )
95
128
{
96
- bool r = Merkle.verify (proof, rootHash, index, leaf);
97
- return r;
129
+ return Merkle.verify (proof, rootHash, index, leaf);
98
130
}
99
131
}
0 commit comments