Less base tokens are transferred when selling quote tokens due to the precision loss that occurred in _GeneralIntegrate()
The _GeneralIntegrate function in the MagicLP maths library divides before multiplying, so integer truncation loses precision when computing the penalty term. As a result, selling quote tokens returns fewer base tokens than the precise formula would give, with the report's example showing a 27.5% shortfall.
Submitted by Matin
https://github.com/code-423n4/2024-03-abracadabra-money/blob/main/src/mimswap/MagicLP.sol#L180-L191
Solidity rounds down the result of an integer division, and because of that, it is always recommended to multiply before dividing to avoid that precision loss. In the case of a prior division over multiplication, the final result may face serious precision loss as the first answer would face truncated precision and then multiplied to another integer.
The problem lies in the magicLP’s quote token querying part. The function querySellQuote() is responsible for querying the price of the quote token. This function calls the sellQuoteToken() function of the contract PMMPricing which uses a curve integral at its heart. To be precise, for the cases where the parameter R is not equal to 1, the function sellQuoteToken() calls the two functions _RAboveSellBaseToken(), _RBelowSellQuoteToken() for the cases R > 1 and R < 1 respectively. These two functions calculate the base token amounts using the _GeneralIntegrate() function.
If we look deeply at the function _GeneralIntegrate() we can see the numerical integration procedure is presented as:
/*
Integrate dodo curve from V1 to V2
require V0>=V1>=V2>0
res = (1-k)i(V1-V2)+ikV0*V0(1/V2-1/V1)
let V1-V2=delta
res = i*delta*(1-k+k(V0^2/V1/V2))
i is the price of V-res trading pair
support k=1 & k=0 case
[round down]
*/
function _GeneralIntegrate(uint256 V0, uint256 V1, uint256 V2, uint256 i, uint256 k) internal pure returns (uint256) {
if (V0 == 0) {
revert ErrIsZero();
}
uint256 fairAmount = i * (V1 - V2); // i*delta
if (k == 0) {
return fairAmount / DecimalMath.ONE;
}
uint256 V0V0V1V2 = DecimalMath.divFloor((V0 * V0) / V1, V2);
uint256 penalty = DecimalMath.mulFloor(k, V0V0V1V2); // k(V0^2/V1/V2)
return (((DecimalMath.ONE - k) + penalty) * fairAmount) / DecimalMath.ONE2;
}
we can see there is a hidden division before a multiplication that makes round down the whole expression. The parameter V0V0V1V2 is calculated in such a way that the V0 * V0 is divided by V1, then the answer is divided by V2. After these divisions, the penalty variable is defined by the multiplication of V0V0V1V2 by the k variable. This writing method is bad as the precision loss can be significant, leading to the magic pool selling fewer base tokens than actual.
At the Proof of Concept part, we can check this behavior precisely.
Proof of Concept
If we rearrange the code in a way that the precision loss is reduced, we can see the difference:
function _GeneralIntegratePrecise(uint256 V0, uint256 V1, uint256 V2, uint256 i, uint256 k) public pure returns (uint256) {
if (V0 == 0) {
revert ErrIsZero();
}
uint256 fairAmount = i * (V1 - V2); // i*delta
if (k == 0) {
return fairAmount / 1e18;
}
uint256 V0V0V1V2 = (V0 * V0);
uint256 penalty = (k * V0V0V1V2) / (V1 * V2); // k(V0^2/V1/V2)
return (((1e18 - k) + penalty) * fairAmount) / 1e36;
}
For the variables:
V0 = 87461235449999999000
V1 = 131454658215568436100
V2 = 45632153140140545000
i = 200
k = 1000000000000000000
We can get these numbers:
Current Implementation 17164
Precise Implementation 21888
The error between these two numbers is % 27.5. Thus, we can see that the actual implementation calculates fewer base token amounts than the precise method.
Recommended Mitigation Steps
Consider modifying the numerical integral calculation to prevent such precision loss and prioritize multiplication over division:
function _GeneralIntegrate(uint256 V0, uint256 V1, uint256 V2, uint256 i, uint256 k) public pure returns (uint256) {
if (V0 == 0) {
revert ErrIsZero();
}
uint256 fairAmount = i * (V1 - V2); // i*delta
if (k == 0) {
return fairAmount / 1e18;
}
uint256 V0V0V1V2 = (V0 * V0);
uint256 penalty = (k * V0V0V1V2) / (V1 * V2); // k(V0^2/V1/V2)
return (((1e18 - k) + penalty) * fairAmount) / 1e36;
}
0xCalibur (Abracadabra) acknowledged
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 penalty term is computed by dividing V0*V0 by V1 and then by V2 before multiplying by k and the fair amount, so truncated precision is amplified by later multiplications.
Impact
A user selling quote tokens receives fewer base tokens than the precise calculation would give, with the report's sample numbers showing a 27.5% difference.
What the attacker needed
K not equal to 0; R not equal to 1; user sells quote tokens
The fix
The numerical integral should be rewritten to multiply before dividing so the precision loss is avoided; the report notes the team acknowledged the issue.
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.
Smart contracts store whole numbers only, so when a division does not come out exactly, the leftover fraction is thrown away. If you divide early and multiply later, that thrown-away amount gets magnified. Here the pool's maths divided before multiplying, so a seller of quote tokens received less base tokens than they should have.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.