Unchecked Underflow in TreeTickLib Causes Transaction Revert in Route Creation
An unchecked cast in TreeTickLib converts negative tick values into large positive tree indices, so the computed low index ends up greater than the high index. Users calling the modify function to update pool positions then hit a range validation revert, blocking liquidity operations.
0xapple Follow with Edoscoba and TheSavageTeddy
Originally disclosed on Sherlock
Source: https://github.com/sherlock-audit/2025-09-ammplify-judging/issues/261
Found by
0xapple, Edoscoba, TheSavageTeddy
Summary
Unchecked arithmetic underflow in TreeTickLib.sol causes a transaction revert for users attempting to modify pool positions, as the system generates an invalid tree index that triggers a failure in the RouteImpl.make function's range validation.
Root Cause
In TreeTickLib.sol within the tickToTreeIndex function, the calculation uint24(tick / tickSpacing) + rootWidth / 2 is performed inside an unchecked block. When tick / tickSpacing is sufficiently negative (e.g., less than -rootWidth / 2), the cast uint24(tick / tickSpacing) converts the negative result to a large positive value due to underflow (e.g., -524,289 becomes 16,252,927). Adding rootWidth / 2 to this does not cause another underflow but rather produces an incorrect tree index (e.g., 16,515,071), which propagates through the system, leading to a revert in downstream logic.
function tickToTreeIndex(int24 tick, uint24 rootWidth, int24 tickSpacing) internal pure returns (uint24) {
unchecked {
require(tick % tickSpacing == 0, UnalignedTick(tick, tickSpacing));
return uint24(tick / tickSpacing) + rootWidth / 2; // Underflow occurs here if result is negative
}
}
Internal Pre-conditions
None
External Pre-conditions
None
Attack Path
-
A user initiates a pool position update by calling
WalkerLib.modifywith a valid negativelowTick(e.g., -524,289), a validhighTick(e.g., -200,000),tickSpacing = 1, androotWidth = 524,288(as computed bycalcRootWidth).Solidity function modify(PoolInfo memory pInfo, int24 lowTick, int24 highTick, Data memory data) internal { uint24 low = pInfo.treeTick(lowTick); // Calls treeTick with lowTick uint24 high = pInfo.treeTick(highTick) - 1; // Calls treeTick with highTick Route memory route = RouteImpl.make(pInfo.treeWidth, low, high); route.walk(down, up, phase, toRaw(data)); } -
The
modifyfunction callsPoolInfoImpl.treeTickfor both ticks, which invokesTreeTickLib.tickToTreeIndexto convert each tick to auint24tree index.Solidity function treeTick(PoolInfo memory self, int24 tick) internal pure returns (uint24) { return TreeTickLib.tickToTreeIndex(tick, self.treeWidth, self.tickSpacing); } -
For
lowTick = -524,289, tickToTreeIndex computestick / tickSpacing = -524,289 / 1 = -524,289. The castuint24(-524,289)underflows to2^24 - 524,289 = 16,252,927. AddingrootWidth / 2 = 524,288 / 2 = 262,144yields16,252,927 + 262,144 = 16,515,071. -
For
highTick = -200,000,tickToTreeIndexcomputestick / tickSpacing = -200,000 / 1 = -200,000. The castuint24(-200,000)underflows to2^24 - 200,000 = 16,577,216. AddingrootWidth / 2 = 524,288 / 2 = 262,144yields16,577,216 + 262,144 = 16,839,360. As this value is bigger thanuint24can fit it yields62,144 -
The
modifyfunction passes these indices toRouteImpl.make(pInfo.treeWidth, low, high), wherelow = 16,515,071andhigh = 224,288. Themakefunction checks_right < _left:Solidity function make(uint24 _rootWidth, uint24 _left, uint24 _right) internal pure returns (Route memory) { if (_right < _left) revert InvertedRange(_left, _right); // Reverts here }Since
224,288 < 16,515,071, the transaction reverts with anInvertedRangeerror.
Impact
Users cannot successfully execute the modify function to update pool positions, as the transaction reverts due to the invalid tree index causing a range validation failure in RouteImpl.make. This prevents users from performing intended operations on the pool, such as adding liquidity positions. This effectively disables the core functionality of the protocol.
PoC
No response
Mitigation
No response
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/itos-finance/Ammplify/pull/25
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the tick-to-index conversion casts a signed division result to an unsigned integer inside an unchecked block, so negative ticks wrap to large positive indices instead of being handled or rejected.
Impact
Users cannot execute modify to update pool positions, which prevents adding liquidity and effectively disables core protocol functionality.
What the attacker needed
None
The fix
The protocol team fixed this issue in a pull request, presumably by handling negative tick results correctly instead of letting them wrap.
How it was found
- A user calls WalkerLib.modify with a valid negative low tick, a valid high tick, a tick spacing of one and a root width from calcRootWidth.
- modify calls PoolInfoImpl.treeTick for both ticks, which calls TreeTickLib.tickToTreeIndex.
- For the negative low tick, the signed division result is cast to uint24 and wraps to a large positive value, then the half root width is added.
- For the high tick, the wrapped value plus the half root width overflows past the uint24 maximum and wraps again to a small value.
- modify passes both indices to RouteImpl.make, which sees the right index below the left index and reverts with InvertedRange.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A tick is a price step used by the pool, and the code stores it in a number type that cannot hold negatives. When a negative tick is squeezed into that type it wraps around to a huge positive number. The pool then thinks the lower end of a range sits above the upper end and refuses the whole transaction.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.