Skip to content

Commit 2464d32

Browse files
committed
Add an option to get non inclusion proof with hashed key
1 parent 42d6f5a commit 2464d32

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

contracts/tree.sol

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ library PatriciaTree {
3333
}
3434

3535
function doesInclude(Tree storage tree, bytes key) internal view returns (bool) {
36-
bytes32 valueHash = _findNode(tree, key);
36+
return doesIncludeHashedKey(tree, keccak256(key));
37+
}
38+
39+
function doesIncludeHashedKey(Tree storage tree, bytes32 hashedKey) internal view returns (bool) {
40+
bytes32 valueHash = _findNodeWithHashedKey(tree, hashedKey);
3741
return (valueHash != bytes32(0));
3842
}
3943

@@ -114,12 +118,21 @@ library PatriciaTree {
114118
bytes32 potentialSiblingValue,
115119
uint branchMask,
116120
bytes32[] _siblings
121+
) {
122+
return getNonInclusionProofWithHashedKey(tree, keccak256(key));
123+
}
124+
125+
function getNonInclusionProofWithHashedKey(Tree storage tree, bytes32 hashedKey) internal view returns (
126+
bytes32 potentialSiblingLabel,
127+
bytes32 potentialSiblingValue,
128+
uint branchMask,
129+
bytes32[] _siblings
117130
){
118131
uint length;
119132
uint numSiblings;
120133

121134
// Start from root edge
122-
D.Label memory label = D.Label(keccak256(key), 256);
135+
D.Label memory label = D.Label(hashedKey, 256);
123136
D.Edge memory e = tree.rootEdge;
124137
bytes32[256] memory siblings;
125138

@@ -268,10 +281,14 @@ library PatriciaTree {
268281
}
269282

270283
function _findNode(Tree storage tree, bytes key) private view returns (bytes32) {
284+
return _findNodeWithHashedKey(tree, keccak256(key));
285+
}
286+
287+
function _findNodeWithHashedKey(Tree storage tree, bytes32 hashedKey) private view returns (bytes32) {
271288
if (tree.rootEdge.node == 0 && tree.rootEdge.label.length == 0) {
272289
return 0;
273290
} else {
274-
D.Label memory k = D.Label(keccak256(key), 256);
291+
D.Label memory k = D.Label(hashedKey, 256);
275292
return _findAtEdge(tree, tree.rootEdge, k);
276293
}
277294
}

0 commit comments

Comments
 (0)