Skip to content

Commit 48a1d45

Browse files
authored
Merge pull request #5 from commitground/hotfix/function-to-use-hashed-key
Hotfix/function to use hashed key
2 parents 8eeebd1 + 14ade8d commit 48a1d45

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
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
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "solidity-patricia-tree",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "Patricia Tree solidity implemenation",
55
"directories": {
66
"test": "test"

0 commit comments

Comments
 (0)