From cbca4fa968e59f553495f69f1b1687f10283998f Mon Sep 17 00:00:00 2001 From: cedoor Date: Fri, 9 Sep 2022 12:10:38 +0200 Subject: [PATCH] refactor: set remove method as a zero-leaf update --- .../contracts/IncrementalBinaryTree.sol | 30 +-------------- .../contracts/IncrementalQuinTree.sol | 37 +------------------ yarn.lock | 2 +- 3 files changed, 3 insertions(+), 66 deletions(-) diff --git a/packages/incremental-merkle-tree.sol/contracts/IncrementalBinaryTree.sol b/packages/incremental-merkle-tree.sol/contracts/IncrementalBinaryTree.sol index 3191743..3e0a6b5 100644 --- a/packages/incremental-merkle-tree.sol/contracts/IncrementalBinaryTree.sol +++ b/packages/incremental-merkle-tree.sol/contracts/IncrementalBinaryTree.sol @@ -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. diff --git a/packages/incremental-merkle-tree.sol/contracts/IncrementalQuinTree.sol b/packages/incremental-merkle-tree.sol/contracts/IncrementalQuinTree.sol index 99d365c..d1e696d 100644 --- a/packages/incremental-merkle-tree.sol/contracts/IncrementalQuinTree.sol +++ b/packages/incremental-merkle-tree.sol/contracts/IncrementalQuinTree.sol @@ -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. diff --git a/yarn.lock b/yarn.lock index dc90e67..bc07e68 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8809,7 +8809,7 @@ __metadata: dependencies: bn.js: ^4.11.8 ethereumjs-util: ^6.0.0 - checksum: 03127d09960e5f8a44167463faf25b2894db2f746376dbb8195b789ed11762f93db9c574eaa7c498c400063508e9dfc1c80de2edf5f0e1406b25c87d860ff2f1 + checksum: ae074be0bb012857ab5d3ae644d1163b908a48dd724b7d2567cfde309dc72222d460438f2411936a70dc949dc604ce1ef7118f7273bd525815579143c907e336 languageName: node linkType: hard