Skip to content

⚡️ Optimize Pow2 Padding - #1464

Merged
Vectorized merged 2 commits into
Vectorized:pow2-paddingfrom
0xClandestine:0xclandestine/fpml
Aug 18, 2025
Merged

⚡️ Optimize Pow2 Padding#1464
Vectorized merged 2 commits into
Vectorized:pow2-paddingfrom
0xClandestine:0xclandestine/fpml

Conversation

@0xClandestine

Copy link
Copy Markdown
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
Copy Markdown
Owner

Does the bytecode size go up or down?

@0xClandestine

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Owner

I can see why this might be needed onchain.

@0xClandestine

Copy link
Copy Markdown
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