Skip to content

Conversation

@0xClandestine
Copy link
Contributor

Description

⚡️ Replace iterative roundUpPow2 calculation with a constant-time bit-twiddling version inspired by “Bit Hacks” – Round up to next power of 2.

O(log(n)) -> O(1)

Checklist

Ensure you completed all of the steps below before submitting your pull request:

  • Ran forge fmt?
  • Ran forge test?

Pull requests with an incomplete checklist will be thrown out.

@Vectorized
Copy link
Owner

Does the bytecode size go up or down?

@0xClandestine
Copy link
Contributor Author

Does the bytecode size go up or down?

@Vectorized MerkleTreeLibTest size increased by 12 bytes after my last commit. Also thinking about it further, there's no need to fully support uint256 values with this approximation, 32 bits would sufficient to support all possible leaves.length values since memory has practical size limits.

@atarpara
Copy link
Collaborator

@Vectorized why we don't use ceil(log2 n) = floor(log2(n-1)) + 1 ?

we have optimized formula for a floor(log2 n). Are you worried about bytecode size (is this lib use prod in ethereum)?

@Vectorized
Copy link
Owner

I can see why this might be needed onchain.

@0xClandestine
Copy link
Contributor Author

I can see why this might be needed onchain.

I know merkleizeSha256 is needed for beaconchain related proofs and is actively used by EigenLayer.

@Vectorized Vectorized changed the base branch from main to pow2-padding August 18, 2025 06:11
@Vectorized Vectorized merged commit 6578bc9 into Vectorized:pow2-padding Aug 18, 2025
12 of 13 checks passed
@Vectorized Vectorized mentioned this pull request Aug 18, 2025
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants