exp() function is not accurate when x/g is not small
The exp() function in AdaptiveFee.sol uses a Taylor series that is inaccurate when x/g is large, causing errors up to 15% in exp() and 18% in sigmoid(), which affects fee calculation. The calculation also consumes significant gas due to computing up to the 8th term.
Originally disclosed on Code4rena
Submitted by 141345
The evaluation of exp function will be inaccurate, and further affect the accuracy of sigmoid() function, eventually affecting fee calculation.
Also the calculation takes quite a lot of gas to calculate to 8th term.
Proof of Concept
x/g takes value between 0 to 6, but taylor expansion maintains good approximation only near 0. When x/g is close to 5, the error is around 7% for exp() and 7% forsigmoid() respectively. When x/g is close to 6, the error could go up to 15% for exp() and 18% for sigmoid().
// src/core/contracts/libraries/AdaptiveFee.sol
function exp(
uint256 x,
uint16 g,
uint256 gHighestDegree
) internal pure returns (uint256 res) {
// calculating:
// g**8 + x * g**7 + (x**2 * g**6) / 2 + (x**3 * g**5) / 6 + (x**4 * g**4) / 24 + (x**5 * g**3) / 120 + (x**6 * g^2) / 720 + x**7 * g / 5040 + x**8 / 40320
// x**8 < 152 bits (19*8) and g**8 < 128 bits (8*16)
// so each summand < 152 bits and res < 155 bits
uint256 xLowestDegree = x;
res = gHighestDegree; // g**8
gHighestDegree /= g; // g**7
res += xLowestDegree * gHighestDegree;
gHighestDegree /= g; // g**6
xLowestDegree *= x; // x**2
res += (xLowestDegree * gHighestDegree) / 2;
gHighestDegree /= g; // g**5
xLowestDegree *= x; // x**3
res += (xLowestDegree * gHighestDegree) / 6;
gHighestDegree /= g; // g**4
xLowestDegree *= x; // x**4
res += (xLowestDegree * gHighestDegree) / 24;
gHighestDegree /= g; // g**3
xLowestDegree *= x; // x**5
res += (xLowestDegree * gHighestDegree) / 120;
gHighestDegree /= g; // g**2
xLowestDegree *= x; // x**6
res += (xLowestDegree * gHighestDegree) / 720;
xLowestDegree *= x; // x**7
res += (xLowestDegree * g) / 5040 + (xLowestDegree * x) / (40320);
}
Recommended Mitigation Steps
The value of exp(1), exp(2), exp(3), exp(4), exp(5), exp(6) can be pre-calculated and saved to constants, then be used as denominator or multiplier.
For example, to calculate exp(3.48), what we can do is calculate exp(0.48), and then multiply by exp(3).
When the power is less than 0.5, taylor expansion up to x^3 can give good accuracy. exp(0.5) and corresponding sigmoid() has maximum error in the order of 2e-4 to 1e-4.
vladyan18 (QuickSwap & StellaSwap) acknowledged and commented:
Thank you!
Indeed, the Taylor series converges fast enough only close to zero. Tests and practice show that the current degree of accuracy is satisfactory and retains the main property - monotonous growth from volatility. However, the recommendations for gas optimization and accuracy improvement are really good.
I am going to award this one based on the quality of the report and level of detail. While the error tolerance may be acceptable to the sponsor, I think the warden does a great job of demonstrating their point.
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
The Taylor series approximation for exp() is only accurate near zero, but the code uses it for x/g up to 6 without range reduction.
What the attacker needed
None
The fix
The recommendation is to pre-calculate exp(1) through exp(6) as constants and use range reduction, then apply Taylor expansion only for small powers.
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 smart contract calculates fees using a mathematical function called exp(). The code approximates this function with a polynomial that is only accurate for small inputs. For larger inputs, the approximation is off by up to 18%, so fees may be miscalculated.