X96 sqrt ratio calculated is wrong whenever (abs_tick & 1) != 0
& (abs_tick & 0x2) != 0
#259
Labels
2 (Med Risk)
Assets not at direct risk, but function/availability of the protocol could be impacted or leak value
bug
Something isn't working
🤖_primary
AI based primary recommendation
🤖_27_group
AI based duplicate group recommendation
sufficient quality report
This report is of sufficient quality
Lines of code
https://github.com/code-423n4/2024-08-superposition/blob/4528c9d2dbe1550d2660dac903a8246076044905/pkg/seawater/src/maths/tick_math.rs#L20-L59
https://github.com/code-423n4/2024-08-superposition/blob/4528c9d2dbe1550d2660dac903a8246076044905/README.md#L70-L92
Vulnerability details
Proof of Concept
Take a look at https://github.com/code-423n4/2024-08-superposition/blob/4528c9d2dbe1550d2660dac903a8246076044905/pkg/seawater/src/maths/tick_math.rs#L20-L59
This function calculates the
X96
encoded square root price for a given tick index.Per the documentation this should be the same as is done in the Uniswap instance, see https://github.com/Uniswap/v3-core/blob/d8b1c635c275d2a9450bd6a78f3fa2484fef73eb/contracts/libraries/TickMath.sol#L23-L54:
Issue however as hinted by the
@audit
tag is that this deviates from the linked implementation, cause in Superposition's caseabs_tick
is being AND'd with1
instead of0x2
. And then the value is multiplied against the ratio value of0xfffcb933bd6fad37aa2d162d1a594001
.Impact
The
X96
encoded square root price for a given tick index would be wrong in this case, considering protocol assumes the Uniswap calculation to be intended/correct.This then means that all the instances where this price is used to be flawed/ broken too, considering even in swaps or integrations with the AMM the wrong price would be returned, when the real calculation of
(abs_tick & 0x2) != 0
since the check done in scope before the multiplication of the ratio is(abs_tick & 1) != 0
This then means that the calculation is broken in two instances:
(abs_tick & 1) != 0
it deviates heavily from the Uniswap standard as it makes and extra calculation, and(abs_tick & 0x2) != 0
it also deviates from the Uniswap standard as it doesn't make the makes expected calculation, see Uniswap's TickMath.sol#L23-L54 for more info.Recommended Mitigation Steps
Change
(abs_tick & 1) != 0
for(abs_tick & 0x2) != 0
.Assessed type
Uniswap
The text was updated successfully, but these errors were encountered: