Skip to content

Commit 813c66f

Browse files
⚡️ Optimize Pow2 Padding (#1467)
Co-authored-by: clandestine.eth <96172957+0xClandestine@users.noreply.github.com>
1 parent 6837801 commit 813c66f

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/utils/MerkleTreeLib.sol

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,16 @@ library MerkleTreeLib {
258258
assembly {
259259
result := mload(0x40)
260260
let l := mload(leaves)
261-
if iszero(l) {
261+
let p := sub(l, 1)
262+
if iszero(lt(p, 0xffffffff)) {
262263
mstore(0x00, 0xe7171dc4) // `MerkleTreeLeavesEmpty()`.
263-
revert(0x1c, 0x04)
264+
revert(0x1c, mul(iszero(l), 0x04)) // If `p > 2**32 - 1`, revert with empty.
264265
}
265-
let p := sub(mload(leaves), 1)
266-
for { let i := 1 } lt(i, 0x80) { i := add(i, i) } { p := or(p, shr(i, p)) }
267-
p := add(p, 1)
266+
p := or(shr(1, p), p)
267+
p := or(shr(2, p), p)
268+
p := or(shr(4, p), p)
269+
p := or(shr(8, p), p)
270+
p := add(1, or(shr(16, p), p)) // Supports up to `2**32 - 1`.
268271
mstore(result, p) // Store length.
269272
mstore(0x40, add(result, add(0x20, shl(5, p)))) // Allocate memory.
270273
let d := sub(result, leaves)

0 commit comments

Comments
 (0)