Precision loss in setMaxSpotOffsetBps function leads to Incorrect Numa Prices
The setMaxSpotOffsetBps function in NumaOracle suffers from precision loss when computing square root values, causing the spot price to be adjusted by incorrect percentages. This leads to inaccurate conversions between Numa and nuAssets.
Source: https://github.com/sherlock-audit/2024-12-numa-audit-judging/issues/175
Found by
jokr
Summary
Due to precision loss in NumaOracle.setMaxSpotOffsetBps(), the spot price is modified (increased or decreased) by incorrect percentages, resulting in incorrect prices.
Root Cause
Due to precision loss during the calculation of maxSpotOffsetPlus1SqrtBps and maxSpotOffsetMinus1SqrtBps, the spot price from the LP increases or decreases by more than the desired percentage.
function setMaxSpotOffsetBps(uint _maxSpotOffsetBps) external onlyOwner {
require(_maxSpotOffsetBps < 10000, "percentage must be less than 100");
// @audit-issue precision loss here
maxSpotOffsetPlus1SqrtBps =
100 *
uint160(Math.sqrt(10000 + _maxSpotOffsetBps));
// @audit-issue precision loss here
maxSpotOffsetMinus1SqrtBps =
100 *
uint160(Math.sqrt(10000 - _maxSpotOffsetBps));
emit MaxSpotOffsetBps(_maxSpotOffsetBps);
}
For example, let’s say _maxSpotOffsetBps = 145 (1.45% as mentioned in the documentation).
In this case, maxSpotOffsetPlus1SqrtBps should ideally be 10072. However, since Math.sqrt(10000 + 145) = 100.72, it will be rounded down to 100 in Solidity. As a result, maxSpotOffsetPlus1SqrtBps will be set to 10000 instead of 10072. This means that, instead of increasing the spot price by 1.45%, the spot price will remain unchanged.
Similarly, for maxSpotOffsetMinus1SqrtBps, it will be set to 9900 instead of 9927. This results in the spot price decreasing by 1.99% instead of the intended 1.45%.
Internal pre-conditions
No response
External pre-conditions
No response
Attack Path
No response
Impact
Incorrect prices result in conversions between Numa and nuAssets occurring at inaccurate rates.
PoC
Initial values:
sqrtPriceX96: 51371404683662199233634777298 (From Numa/DAI pool)
Converted to price: 0.42042033575707566
sqrtPriceX96_2: 1364847094945173773261425958388466 (From USDC/ETH pool)
Converted to price: 3369.6994581667514
Testing with 145 bps (1.45%):
maxSpotOffsetPlus1SqrtBps: 10000
maxSpotOffsetMinus1SqrtBps: 9900
run() results (direct prices):
Original price: 0.42042033575707566
Decreased price: 0.4120539710755098
Increased price: 0.42042033575707566
Percentage change (decrease): -1.99%
Percentage change (increase): 0.00%
run2() results (inverse prices):
Original price: 3369.6994581667514 (ETH price in USDC)
Decreased price: 3302.642438949233
Increased price: 3369.6994581667514
Percentage change (decrease): -1.99%
Percentage change (increase): 0.00%
Mitigation
Increase the precision of _maxSpotOffsetBps to avoid precision losses.
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 integer square root function truncates the result, and multiplying by 100 after truncation amplifies the error, leading to incorrect offset values.
Impact
Incorrect prices result in conversions between Numa and nuAssets occurring at inaccurate rates.
What the attacker needed
Owner calls setMaxSpotOffsetBps
The fix
Increase the precision of `_maxSpotOffsetBps` to avoid precision losses.
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.
When a smart contract calculates numbers, it sometimes rounds down to whole numbers, losing precision. Here, that rounding caused the allowed price change to be wrong: instead of changing by 1.45%, the price either didn't change or changed by 1.99%. This means trades between two tokens could happen at unfair rates.