Skip to content

Commit

Permalink
feat: [internal-r6] clear unused bits (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
chefburger authored Sep 2, 2024
1 parent 9166870 commit cf59155
Show file tree
Hide file tree
Showing 49 changed files with 77 additions and 55 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24261
24266
2 changes: 1 addition & 1 deletion .forge-snapshots/BinPoolManagerTest#testGasDonate.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
119419
119431
Original file line number Diff line number Diff line change
@@ -1 +1 @@
971597
971609
Original file line number Diff line number Diff line change
@@ -1 +1 @@
330908
330920
Original file line number Diff line number Diff line change
@@ -1 +1 @@
338509
338521
Original file line number Diff line number Diff line change
@@ -1 +1 @@
141059
141071
Original file line number Diff line number Diff line change
@@ -1 +1 @@
173967
173973
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179996
180002
Original file line number Diff line number Diff line change
@@ -1 +1 @@
133998
134004
Original file line number Diff line number Diff line change
@@ -1 +1 @@
305471
305477
Original file line number Diff line number Diff line change
@@ -1 +1 @@
34501
34507
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21295
21262
Original file line number Diff line number Diff line change
@@ -1 +1 @@
348065
348304
Original file line number Diff line number Diff line change
@@ -1 +1 @@
163580
163775
Original file line number Diff line number Diff line change
@@ -1 +1 @@
238829
239056
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#donateBothTokens.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
164012
164024
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#gasDonateOneToken.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
108961
108967
Original file line number Diff line number Diff line change
@@ -1 +1 @@
150637
150645
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115371
115519
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131655
131787
Original file line number Diff line number Diff line change
@@ -1 +1 @@
164122
164357
Original file line number Diff line number Diff line change
@@ -1 +1 @@
149672
149804
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_simple.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
72285
72396
Original file line number Diff line number Diff line change
@@ -1 +1 @@
143936
144042
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withHooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
88571
88682
2 changes: 1 addition & 1 deletion .forge-snapshots/CLPoolManagerTest#swap_withNative.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
72288
72399
Original file line number Diff line number Diff line change
@@ -1 +1 @@
198
233
Original file line number Diff line number Diff line change
@@ -1 +1 @@
198
268
Original file line number Diff line number Diff line change
@@ -1 +1 @@
542
548
Original file line number Diff line number Diff line change
@@ -1 +1 @@
209
218
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1202
1256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1553
1679
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1041
1098
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1553
1679
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1213
1233
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1968
2000
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1044
1062
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1968
2000
2 changes: 1 addition & 1 deletion .forge-snapshots/VaultBytecodeSize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7766
7769
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5174
5182
2 changes: 1 addition & 1 deletion src/libraries/ProtocolFeeLibrary.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ library ProtocolFeeLibrary {
// Equivalent to: getZeroForOneFee(self) <= MAX_PROTOCOL_FEE && getOneForZeroFee(self) <= MAX_PROTOCOL_FEE
assembly ("memory-safe") {
let isZeroForOneFeeOk := lt(and(self, 0xfff), FEE_0_THRESHOLD)
let isOneForZeroFeeOk := lt(self, FEE_1_THRESHOLD)
let isOneForZeroFeeOk := lt(and(self, 0xfff000), FEE_1_THRESHOLD)
valid := and(isZeroForOneFeeOk, isOneForZeroFeeOk)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/SettlementGuard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ library SettlementGuard {
if (currentLocker != address(0) && newLocker != address(0)) revert IVault.LockerAlreadySet(currentLocker);

assembly ("memory-safe") {
tstore(LOCKER_SLOT, newLocker)
tstore(LOCKER_SLOT, and(newLocker, 0xffffffffffffffffffffffffffffffffffffffff))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/libraries/VaultReserve.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ library VaultReserve {
function setVaultReserve(Currency currency, uint256 amount) internal {
assembly ("memory-safe") {
// record <currency, amount> in transient storage
tstore(RESERVE_TYPE_SLOT, currency)
tstore(RESERVE_TYPE_SLOT, and(currency, 0xffffffffffffffffffffffffffffffffffffffff))
tstore(RESERVE_AMOUNT_SLOT, amount)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/pool-cl/libraries/LiquidityMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ library LiquidityMath {
/// @return z The liquidity delta
function addDelta(uint128 x, int128 y) internal pure returns (uint128 z) {
assembly ("memory-safe") {
z := add(x, y)
z := add(and(x, 0xffffffffffffffffffffffffffffffff), signextend(15, y))

if shr(128, z) {
// store 0x93dafdf1, error SafeCastOverflow at memory 0 address and revert from pointer 28, to byte 32
Expand Down
28 changes: 20 additions & 8 deletions src/pool-cl/libraries/SqrtPriceMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ library SqrtPriceMath {
// in addition, we must check that the denominator does not underflow
// equivalent: if (product / amount != sqrtPX96 || numerator1 <= product) revert PriceOverflow();
assembly ("memory-safe") {
if iszero(and(eq(div(product, amount), sqrtPX96), gt(numerator1, product))) {
if iszero(
and(
eq(div(product, amount), and(sqrtPX96, 0xffffffffffffffffffffffffffffffffffffffff)),
gt(numerator1, product)
)
) {
mstore(0, 0xf5c787f1) // selector for PriceOverflow()
revert(0x1c, 0x04)
}
Expand Down Expand Up @@ -103,7 +108,7 @@ library SqrtPriceMath {

// equivalent: if (sqrtPX96 <= quotient) revert NotEnoughLiquidity();
assembly ("memory-safe") {
if iszero(gt(sqrtPX96, quotient)) {
if iszero(gt(and(sqrtPX96, 0xffffffffffffffffffffffffffffffffffffffff), quotient)) {
mstore(0, 0x4323a555) // selector for NotEnoughLiquidity()
revert(0x1c, 0x04)
}
Expand All @@ -129,7 +134,10 @@ library SqrtPriceMath {
{
// equivalent: if (sqrtPX96 == 0 || liquidity == 0) revert InvalidPriceOrLiquidity();
assembly ("memory-safe") {
if or(iszero(sqrtPX96), iszero(liquidity)) {
if or(
iszero(and(sqrtPX96, 0xffffffffffffffffffffffffffffffffffffffff)),
iszero(and(liquidity, 0xffffffffffffffffffffffffffffffff))
) {
mstore(0, 0x4f2461b8) // selector for InvalidPriceOrLiquidity()
revert(0x1c, 0x04)
}
Expand All @@ -155,7 +163,10 @@ library SqrtPriceMath {
{
// equivalent: if (sqrtPX96 == 0 || liquidity == 0) revert InvalidPriceOrLiquidity();
assembly ("memory-safe") {
if or(iszero(sqrtPX96), iszero(liquidity)) {
if or(
iszero(and(sqrtPX96, 0xffffffffffffffffffffffffffffffffffffffff)),
iszero(and(liquidity, 0xffffffffffffffffffffffffffffffff))
) {
mstore(0, 0x4f2461b8) // selector for InvalidPriceOrLiquidity()
revert(0x1c, 0x04)
}
Expand Down Expand Up @@ -185,7 +196,7 @@ library SqrtPriceMath {

// equivalent: if (sqrtRatioAX96 == 0) revert InvalidPrice();
assembly ("memory-safe") {
if iszero(sqrtRatioAX96) {
if iszero(and(sqrtRatioAX96, 0xffffffffffffffffffffffffffffffffffffffff)) {
mstore(0, 0x00bfc921) // selector for InvalidPrice()
revert(0x1c, 0x04)
}
Expand All @@ -203,7 +214,8 @@ library SqrtPriceMath {
/// @notice Equivalent to: `a >= b ? a - b : b - a`
function absDiff(uint160 a, uint160 b) internal pure returns (uint256 res) {
assembly ("memory-safe") {
let diff := sub(a, b)
let diff :=
sub(and(a, 0xffffffffffffffffffffffffffffffffffffffff), and(b, 0xffffffffffffffffffffffffffffffffffffffff))
// mask = 0 if a >= b else -1 (all 1s)
let mask := sar(255, diff)
// if a >= b, res = a - b = 0 ^ (a - b)
Expand All @@ -230,7 +242,7 @@ library SqrtPriceMath {
uint256 _liquidity;
assembly ("memory-safe") {
// avoid implicit upcasting
_liquidity := liquidity
_liquidity := and(liquidity, 0xffffffffffffffffffffffffffffffff)
}
/**
* Equivalent to:
Expand All @@ -241,7 +253,7 @@ library SqrtPriceMath {
*/
amount1 = FullMath.mulDiv(_liquidity, numerator, denominator);
assembly ("memory-safe") {
amount1 := add(amount1, and(gt(mulmod(_liquidity, numerator, denominator), 0), roundUp))
amount1 := add(amount1, and(gt(mulmod(_liquidity, numerator, denominator), 0), and(roundUp, 0x1)))
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/pool-cl/libraries/SwapMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ library SwapMath {
// sqrtPriceTargetX96 = max(sqrtPriceNextX96, sqrtPriceLimitX96)
// when zeroForOne == false, nextOrLimit reduces to sqrtPriceNextX96 < sqrtPriceLimitX96
// sqrtPriceTargetX96 = min(sqrtPriceNextX96, sqrtPriceLimitX96)
let nextOrLimit := xor(lt(sqrtPriceNextX96, sqrtPriceLimitX96), zeroForOne)
sqrtPriceNextX96 := and(sqrtPriceNextX96, 0xffffffffffffffffffffffffffffffffffffffff)
sqrtPriceLimitX96 := and(sqrtPriceLimitX96, 0xffffffffffffffffffffffffffffffffffffffff)
let nextOrLimit := xor(lt(sqrtPriceNextX96, sqrtPriceLimitX96), and(zeroForOne, 0x1))
let symDiff := xor(sqrtPriceNextX96, sqrtPriceLimitX96)
sqrtPriceTargetX96 := xor(sqrtPriceLimitX96, mul(symDiff, nextOrLimit))
}
Expand Down
5 changes: 4 additions & 1 deletion src/pool-cl/libraries/Tick.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ library Tick {
int24 MIN_TICK = TickMath.MIN_TICK;
// tick spacing will never be 0 since TickMath.MIN_TICK_SPACING is 1
assembly ("memory-safe") {
tickSpacing := signextend(2, tickSpacing)
let minTick := sdiv(MIN_TICK, tickSpacing)
let maxTick := sdiv(MAX_TICK, tickSpacing)
let numTicks := add(sub(maxTick, minTick), 1)
Expand Down Expand Up @@ -166,7 +167,9 @@ library Tick {

// update two members in one go
assembly ("memory-safe") {
sstore(info.slot, or(liquidityGrossAfter, shl(128, liquidityNetAfter)))
sstore(
info.slot, or(and(liquidityGrossAfter, 0xffffffffffffffffffffffffffffffff), shl(128, liquidityNetAfter))
)
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/pool-cl/libraries/TickBitmap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ library TickBitmap {
// compressed = tick / tickSpacing;
// if (tick < 0 && tick % tickSpacing != 0) compressed--;
assembly ("memory-safe") {
tick := signextend(2, tick)
tickSpacing := signextend(2, tickSpacing)
compressed :=
sub(
sdiv(tick, tickSpacing),
Expand All @@ -35,7 +37,7 @@ library TickBitmap {
function position(int24 tick) internal pure returns (int16 wordPos, uint8 bitPos) {
assembly ("memory-safe") {
// signed arithmetic shift right
wordPos := sar(8, tick) //
wordPos := sar(8, signextend(2, tick)) //
bitPos := and(tick, 0xff)
}
}
Expand All @@ -51,6 +53,8 @@ library TickBitmap {
// uint256 mask = 1 << bitPos;
// self[wordPos] ^= mask;
assembly ("memory-safe") {
tick := signextend(2, tick)
tickSpacing := signextend(2, tickSpacing)
// ensure that the tick is spaced
if smod(tick, tickSpacing) {
let fmp := mload(0x40)
Expand Down
1 change: 1 addition & 0 deletions src/pool-cl/libraries/TickMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ library TickMath {
// Equivalent: uint256 absTick = tick < 0 ? uint256(-int256(tick)) : uint256(int256(tick));
uint256 absTick;
assembly ("memory-safe") {
tick := signextend(2, tick)
// mask = 0 if tick >= 0 else -1 (all 1s)
let mask := sar(255, tick)
absTick := xor(mask, add(mask, tick))
Expand Down

0 comments on commit cf59155

Please sign in to comment.