From df9ae252465fa846b0930f4c5445490d88423046 Mon Sep 17 00:00:00 2001 From: thedavidmeister Date: Mon, 19 Jan 2026 13:42:14 +0400 Subject: [PATCH 1/2] snapshot --- .gas-snapshot | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 326abb8..57828d5 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,3 +1,3 @@ -LibCtPopTest:testCTPOPReference(uint256) (runs: 2048, μ: 3604, ~: 3605) -LibCtPopTest:testCTPOPShuffled(uint8,bytes32) (runs: 2048, μ: 149131, ~: 149131) -LibCtPopTest:testCTPOPUnshuffled(uint8) (runs: 2048, μ: 4146, ~: 4146) \ No newline at end of file +LibCtPopTest:testCTPOPReference(uint256) (runs: 2048, μ: 756, ~: 757) +LibCtPopTest:testCTPOPShuffled(uint8,bytes32) (runs: 2048, μ: 144923, ~: 144923) +LibCtPopTest:testCTPOPUnshuffled(uint8) (runs: 2048, μ: 966, ~: 966) \ No newline at end of file From 6f09bf4396ec0d4d7234d103612b6d833d24fb79 Mon Sep 17 00:00:00 2001 From: thedavidmeister Date: Mon, 19 Jan 2026 13:46:25 +0400 Subject: [PATCH 2/2] tests --- .gas-snapshot | 9 ++++++--- test/src/lib/LibCtPop.ctpop.t.sol | 31 +++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 57828d5..ecd2ecc 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,3 +1,6 @@ -LibCtPopTest:testCTPOPReference(uint256) (runs: 2048, μ: 756, ~: 757) -LibCtPopTest:testCTPOPShuffled(uint8,bytes32) (runs: 2048, μ: 144923, ~: 144923) -LibCtPopTest:testCTPOPUnshuffled(uint8) (runs: 2048, μ: 966, ~: 966) \ No newline at end of file +LibCtPopTest:testCtPop0() (gas: 741) +LibCtPopTest:testCtPop1() (gas: 764) +LibCtPopTest:testCtPop256EdgeCase() (gas: 677) +LibCtPopTest:testCtPopReference(uint256) (runs: 2048, μ: 789, ~: 790) +LibCtPopTest:testCtPopShuffled(uint8,bytes32) (runs: 2048, μ: 144902, ~: 144902) +LibCtPopTest:testCtPopUnshuffled(uint8) (runs: 2048, μ: 989, ~: 989) \ No newline at end of file diff --git a/test/src/lib/LibCtPop.ctpop.t.sol b/test/src/lib/LibCtPop.ctpop.t.sol index 5dcfef9..f541498 100644 --- a/test/src/lib/LibCtPop.ctpop.t.sol +++ b/test/src/lib/LibCtPop.ctpop.t.sol @@ -3,7 +3,6 @@ pragma solidity =0.8.25; import {Test} from "forge-std/Test.sol"; - import {LibCtPop} from "src/lib/LibCtPop.sol"; /// @title LibCtPopTest @@ -13,7 +12,7 @@ import {LibCtPop} from "src/lib/LibCtPop.sol"; contract LibCtPopTest is Test { /// We should be able to count the number of bits set when we simply set a /// sequence of bits from the low bit to some mid bit. - function testCTPOPUnshuffled(uint8 n) external pure { + function testCtPopUnshuffled(uint8 n) external pure { uint256 x = (1 << n) - 1; uint256 ct = LibCtPop.ctpop(x); assertEq(n, ct); @@ -22,7 +21,7 @@ contract LibCtPopTest is Test { } /// The distribution of bits in the underlying `uint256` should not matter. - function testCTPOPShuffled(uint8 n, bytes32 rand) external pure { + function testCtPopShuffled(uint8 n, bytes32 rand) external pure { uint256 x = (1 << n) - 1; uint256 y = 0; @@ -44,8 +43,32 @@ contract LibCtPopTest is Test { assertEq(ct, ctSlow); } - function testCTPOPReference(uint256 x) external pure { + function testCtPopReference(uint256 x) external pure { + uint256 ct = LibCtPop.ctpop(x); + uint256 ctSlow = LibCtPop.ctpopSlow(x); + assertEq(ct, ctSlow); + } + + function testCtPop256EdgeCase() external pure { + uint256 x = type(uint256).max; + uint256 ct = LibCtPop.ctpop(x); + assertEq(256, ct); + uint256 ctSlow = LibCtPop.ctpopSlow(x); + assertEq(ct, ctSlow); + } + + function testCtPop0() external pure { + uint256 x = 0; + uint256 ct = LibCtPop.ctpop(x); + assertEq(0, ct); + uint256 ctSlow = LibCtPop.ctpopSlow(x); + assertEq(ct, ctSlow); + } + + function testCtPop1() external pure { + uint256 x = 1; uint256 ct = LibCtPop.ctpop(x); + assertEq(1, ct); uint256 ctSlow = LibCtPop.ctpopSlow(x); assertEq(ct, ctSlow); }