Inconsistent mantissa size auto-scaling between packedFloat encoding and calculations will lead to unacceptable rounding errors
The library's packedFloat encoding and its arithmetic operations apply different rules for choosing the mantissa size, so values with very small exponents keep 72-digit mantissas when encoded but lose those extra digits during addition and subtraction. This inconsistency produces rounding errors of 9 ULPs, far above the library's stated tolerance of 1 ULP for add and sub.
Originally disclosed on Code4rena
Submitted by montecristo
Library has “mantissa size auto-scaling” rule to achieve the following:
In other words, the library down scales the size of the mantissa to make sure it is as gas efficient as possible, but it up scales to make sure it keeps a minimum level of precision. The library prioritizes precision over gas efficiency.
However, there is a discrepancy between auto-scaling rule of encoding (i.e. toPackedFloat) and other calculations (add, sub, mul, div).
packedFloat encoding has the following rule:
-
If mantissa has 38 digits and exponent > -18, encoded
packedFloatwill be forced to be in L-mantissa -
If mantissa has 72 digits, encoded
packedFloatwill use L-mantissa -
- If
-18 < exp + digitsMantissa - 38, output will use L-mantissa - Otherwise, output will use M-mantissa
- If
However, all arithmetic operations enforce the following auto-scaling rule:
- For calculation result
r, if-18 < exp + digitsMantissa - 38, output will use L-mantissa - Otherwise, output will use M-mantissa
For example, check how auto-scaling rule is implemented in mul function:
File: 2025-04-forte/src/Float128.sol
493: if (Loperation) {
494: // MIN_L_DIGIT_NUMBER is equal to BASE ** (MAX_L_DIGITS - 1).
495: // We avoid losing the lsd this way, but we could get 1 extra digit
496: rMan = Uint512.div512x256(r0, r1, MIN_L_DIGIT_NUMBER);
497: assembly {
498: rExp := add(rExp, MAX_DIGITS_L_MINUS_1)
499: let hasExtraDigit := gt(rMan, MAX_L_DIGIT_NUMBER)
500:@> let maxExp := sub(sub(add(ZERO_OFFSET, MAXIMUM_EXPONENT), DIGIT_DIFF_L_M), hasExtraDigit)
501:@> Loperation := gt(rExp, maxExp)
...
518: } else {
519: assembly {
520: // multiplication between 2 numbers with k digits can result in a number between 2*k - 1 and 2*k digits
521: // we check first if rMan is a 2k-digit number
522: let is76digit := gt(rMan, MAX_75_DIGIT_NUMBER)
523:@> let maxExp := add(
524:@> sub(sub(add(ZERO_OFFSET, MAXIMUM_EXPONENT), DIGIT_DIFF_L_M), DIGIT_DIFF_76_L),
525:@> iszero(is76digit)
526:@> )
527:@> Loperation := gt(rExp, maxExp)
...
553: if Loperation {
554: r := or(r, MANTISSA_L_FLAG_MASK)
555: }
The implementation just checks rExp is greater than maxExp and decide mantissa L flag on rExp > maxExp condition. (This condition is equal to -18 < exp + digitsMantissa - 38 in rule #3).
So clearly, arithmetic operations use only #3 of auto-scaling rule for encoding, while encoding also has additional #1 and #2 rules.
This means the following:
- Encoded
packedFloats can contain more information (72-digit) even when their exponent is less than -52 - This additional digits contributes to arithmetic operations
- However, the operation discards the additional digits after calculation if the exponent is less than -52
This can lead to the following problems:
- Unreliable behavior of the library (calculation will output 38 digits even though arguments are all L-mantissa, or vice versa)
- Unacceptable rounding error as we will observe in POC section.
Recommended mitigation steps
Enforce the same auto-scaling rule between encoding and calculation operations. i.e.:
- Drop rule #1 and #2 from auto-scaling rule of encoding
- Or, add rule #1 and #2 to auto-scaling rule of calculations
NOTE: Basically rule #1 and rule #3.1 are equivalent. So the fix just needs to handle the case of rule #2.
Proof of Concept
Scenario:
- We have two
packedFloataandb - Both of them have
exp < -52and have 72-digit mantissas aandbwill be L-mantissa due to encoding rule #2- However, both
a + banda - bwill be in M-mantissa becauseexp < -52 - We evaluate
a + b - banda - b + band compare the results - They will differ by 9 ULPs
- However, according to README, maximum acceptable ULP of
addandsubare 1 resp. So expected result should not be off by more than 2 or 3 ULPs
Put the following content in test/poc.t.sol and run forge test --match-test testM03POC -vvv:
pragma solidity ^0.8.24;
import "forge-std/Test.sol";
import "src/Float128.sol";
import {Ln} from "src/Ln.sol";
import {Math} from "src/Math.sol";
import {packedFloat} from "src/Types.sol";
contract ForteTest is Test {
using Float128 for packedFloat;
function testM03POC() external {
int256 aMan = 100000000000000000000000000000000000000000000000000000000000000000000000;
int256 aExp = -53;
int256 bMan = 999999999999999999999999999999999999999999999999999999999999999999999999;
int256 bExp = -56;
packedFloat a = Float128.toPackedFloat(aMan, aExp);
packedFloat b = Float128.toPackedFloat(bMan, bExp);
packedFloat left = a.add(b).sub(b);
packedFloat right = a.sub(b).add(b);
_debug("a", a);
_debug("b", b);
_debug("left", left);
_debug("right", right);
(int256 lMan, int256 lExp) = left.decode();
(int256 rMan, int256 rExp) = right.decode();
assertEq(lExp, rExp, "exp mismatch");
assertEq(rMan - lMan, 9, "ULP != 9");
}
function _debug(string memory message, packedFloat float) internal {
console.log(message);
_debug(float);
}
function _debug(packedFloat float) internal {
(int256 mantissa, int256 exponent) = float.decode();
emit log_named_uint("\tunwrapped", packedFloat.unwrap(float));
emit log_named_int("\tmantissa", mantissa);
emit log_named_uint(
"\tmantissa digits", Float128.findNumberOfDigits(packedFloat.unwrap(float) & Float128.MANTISSA_MASK)
);
emit log_named_int("\texponent", exponent);
}
}
Console output:
Logs:
a
unwrapped: 57525106735054637002573000029187941038311220714476402038523254701685114667008
mantissa: 100000000000000000000000000000000000000000000000000000000000000000000000
mantissa digits: 72
exponent: -53
b
unwrapped: 57504804570277296390618000459179026016121290907713894611025795427269603229695
mantissa: 999999999999999999999999999999999999999999999999999999999999999999999999
mantissa digits: 72
exponent: -56
left
unwrapped: 57754696853475826965418828704284520445468793621070232503079063507853155237878
mantissa: 99999999999999999999999999999999999990
mantissa digits: 38
exponent: -20
right
unwrapped: 57754696853475826965418828704284520445468793621070232503079063507853155237887
mantissa: 99999999999999999999999999999999999999
mantissa digits: 38
exponent: -20
oscarserna (Forte) disputed
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 encoding routine applies extra mantissa-size rules (forcing a long mantissa for 38-digit values with exponent above -18 and for 72-digit values) that the arithmetic routines do not implement, so the two disagree on when to keep the long mantissa.
Impact
Calculations can be off by 9 ULPs, well beyond the library's documented maximum of 1 ULP for add and sub, so results are unreliable and precision is lost.
What the attacker needed
Use of the Float128 library; values with exponent below -52
The fix
The same auto-scaling rule should be enforced for both encoding and calculation, either by dropping the extra encoding rules or by adding them to the arithmetic operations.
How it was found
- Create two packedFloat values whose exponents are below -52 and whose mantissas have 72 digits.
- Add the first to the second and then subtract the second from the result.
- Subtract the second from the first and then add the second back.
- Decode both results and compare the mantissas, observing a difference of 9 ULPs.
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.
This library stores decimal numbers in a compact form, splitting each number into a mantissa (the significant digits) and an exponent (how far the decimal point moves). It uses two sizes of mantissa, a short one and a long one, and picks between them to save space while keeping enough precision. The bug is that the code that stores a number and the code that does maths on numbers choose the size by different rules, so digits get silently dropped and results come out slightly wrong.