refactor: set remove method as a zero-leaf update

This commit is contained in:
cedoor
2022-09-09 12:10:38 +02:00
parent 8ef49c1a03
commit cbca4fa968
3 changed files with 3 additions and 66 deletions

View File

@@ -134,35 +134,7 @@ library IncrementalBinaryTree {
uint256[] calldata proofSiblings,
uint8[] calldata proofPathIndices
) public {
require(
verify(self, leaf, proofSiblings, proofPathIndices),
"IncrementalBinaryTree: leaf is not part of the tree"
);
uint256 depth = self.depth;
uint256 hash = self.zeroes[0];
for (uint8 i = 0; i < depth; ) {
if (proofPathIndices[i] == 0) {
if (proofSiblings[i] == self.lastSubtrees[i][1]) {
self.lastSubtrees[i][0] = hash;
}
hash = PoseidonT3.poseidon([hash, proofSiblings[i]]);
} else {
if (proofSiblings[i] == self.lastSubtrees[i][0]) {
self.lastSubtrees[i][1] = hash;
}
hash = PoseidonT3.poseidon([proofSiblings[i], hash]);
}
unchecked {
++i;
}
}
self.root = hash;
update(self, leaf, 0, proofSiblings, proofPathIndices);
}
/// @dev Verify if the path is correct and the leaf is part of the tree.

View File

@@ -157,42 +157,7 @@ library IncrementalQuinTree {
uint256[4][] calldata proofSiblings,
uint8[] calldata proofPathIndices
) public {
require(
verify(self, leaf, proofSiblings, proofPathIndices),
"IncrementalQuinTree: leaf is not part of the tree"
);
uint256 depth = self.depth;
uint256 hash = self.zeroes[0];
for (uint8 i = 0; i < depth; ) {
uint256[5] memory nodes;
for (uint8 j = 0; j < 5; ) {
if (j < proofPathIndices[i]) {
nodes[j] = proofSiblings[i][j];
} else if (j == proofPathIndices[i]) {
nodes[j] = hash;
} else {
nodes[j] = proofSiblings[i][j - 1];
}
unchecked {
++j;
}
}
if (nodes[0] == self.lastSubtrees[i][0] || nodes[4] == self.lastSubtrees[i][4]) {
self.lastSubtrees[i][proofPathIndices[i]] = hash;
}
hash = PoseidonT6.poseidon(nodes);
unchecked {
++i;
}
}
self.root = hash;
update(self, leaf, 0, proofSiblings, proofPathIndices);
}
/// @dev Verify if the path is correct and the leaf is part of the tree.

View File

@@ -8809,7 +8809,7 @@ __metadata:
dependencies:
bn.js: ^4.11.8
ethereumjs-util: ^6.0.0
checksum: 03127d09960e5f8a44167463faf25b2894db2f746376dbb8195b789ed11762f93db9c574eaa7c498c400063508e9dfc1c80de2edf5f0e1406b25c87d860ff2f1
checksum: ae074be0bb012857ab5d3ae644d1163b908a48dd724b7d2567cfde309dc72222d460438f2411936a70dc949dc604ce1ef7118f7273bd525815579143c907e336
languageName: node
linkType: hard