Skip to content

Commit

Permalink
Merge pull request #219 from privacy-scaling-explorations/refactor/in…
Browse files Browse the repository at this point in the history
…sert

Use `if` instead of `while` to increment depth in the `LeanIMT.insert` function
  • Loading branch information
cedoor committed Mar 20, 2024
2 parents 60d1bac + 21df170 commit b4c91e3
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/imt.sol/contracts/internal/InternalLeanIMT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ library InternalLeanIMT {
revert LeafAlreadyExists();
}

while (2 ** self.depth < self.size + 1) {
// A new insertion can increase a tree's depth by at most 1,
// and only if the number of leaves supported by the current
// depth is less than the number of leaves to be supported after insertion.
if (2 ** self.depth < self.size + 1) {
self.depth += 1;
}

Expand Down Expand Up @@ -103,6 +106,8 @@ library InternalLeanIMT {
currentLevel = leaves;

// Calculate the depth of the tree after adding the new values.
// Unlike the 'insert' function, we need a while here as
// N insertions can increase the tree's depth more than once.
while (2 ** self.depth < self.size + leaves.length) {
self.depth += 1;
}
Expand Down

0 comments on commit b4c91e3

Please sign in to comment.