Skip to content

Commit

Permalink
refactor(lazy-imt): target depth calculation (#39)
Browse files Browse the repository at this point in the history
<!-- Please refer to our CONTRIBUTING documentation for any questions on
submitting a pull request. -->
<!-- Provide a general summary of your changes in the Title above. -->

## Description

Refactors logarithm calculation to be more efficient.

<!-- Describe your changes in detail. -->
<!-- You may want to answer some of the following questions: -->
<!-- What kind of change does this PR introduce?** (Bug fix, feature,
docs update, ...) -->
<!-- What is the current behavior?** (You can also link to an open issue
here) -->
<!-- What is the new behavior (if this is a feature change)? -->
<!-- Does this PR introduce a breaking change?** (What changes might
users need to make in their application due to this PR?) -->

## Related Issue(s)

<!-- This project accepts pull requests related to open issues. -->
<!-- If suggesting a new feature or change, please discuss it in an
issue first. -->
<!-- If fixing a bug, there should be an issue describing it with steps
to reproduce. -->
<!-- Please link to the issue(s) here -->

<!-- Closes # -->
<!-- Fixes # -->

## Other information

<!-- Any other information that is important to this PR such as
screenshots of how the component looks before and after the change. -->
<!-- Feel free to remove this section if you will not use it. -->

## Checklist

<!-- Please check if the PR fulfills these requirements. -->

-   [x] My code follows the style guidelines of this project
-   [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
-   [x] My changes generate no new warnings
- [x] I have run `yarn format` and `yarn compile` without getting any
errors
- [x] I have added tests that prove my fix is effective or that my
feature works
-   [x] New and existing unit tests pass locally with my changes
  • Loading branch information
chancehudson authored Sep 19, 2024
1 parent a4e7136 commit b6cdd4f
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/lazy-imt/contracts/InternalLazyIMT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,14 @@ library InternalLazyIMT {
uint40 numberOfLeaves = self.numberOfLeaves;
require(index < numberOfLeaves, "LazyIMT: leaf must exist");

// targetDepth = log2_floor(numberOfLeaves)
uint8 targetDepth = 1;
while (uint40(2) ** uint40(targetDepth) < numberOfLeaves) {
targetDepth++;
{
uint40 val = 2;
while (val < numberOfLeaves) {
val <<= 1;
targetDepth++;
}
}
require(depth >= targetDepth, "LazyIMT: proof depth");
// pass depth -1 because we don't need the root value
Expand Down

0 comments on commit b6cdd4f

Please sign in to comment.