From b6cdd4f94e0f26c0d71752e17d114af78133912c Mon Sep 17 00:00:00 2001 From: Chance Date: Thu, 19 Sep 2024 03:15:17 -0700 Subject: [PATCH] refactor(lazy-imt): target depth calculation (#39) ## Description Refactors logarithm calculation to be more efficient. ## Related Issue(s) ## Other information ## Checklist - [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 --- packages/lazy-imt/contracts/InternalLazyIMT.sol | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/lazy-imt/contracts/InternalLazyIMT.sol b/packages/lazy-imt/contracts/InternalLazyIMT.sol index 84e5b34..ffc0b53 100644 --- a/packages/lazy-imt/contracts/InternalLazyIMT.sol +++ b/packages/lazy-imt/contracts/InternalLazyIMT.sol @@ -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