Impact
Incorrect rounding direction for signed mul and div operations
The mulDiv(x, y, z) function incorrectly handled cases where both the intermediate product $x * y$ and the divisor $z$ were negative. The logic assumed that if the intermediate product was negative, the final result must also be negative, neglecting the sign of $z$.
This resulted in rounding being applied in the wrong direction for cases where both $x * y$ and $z$ were negative. The functions most at risk are fixed_div_floor and fixed_div_ceil, as they often use non-constant numbers as the divisor $z$ in mulDiv.
This error is present in all signed FixedPoint and SorobanFixedPoint implementations, including i64, i128, and I256.
Negative Overflow in i64
The mulDiv(x, y, z) function for i64 used the i128 type to handle "phantom overflows". These are overflows that occur intermediately during a calculation, like when computing the intermediate product $x * y$. When the final result of mulDiv was computed in i128, it was scaled back down to i64 before returning. While the code verified that the result did not exceed i64::MAX, it did not check against i64::MIN.
This caused negative results smaller than i64:MIN to wrap around to a large positive number instead of being caught as an overflow.
This error only exists for the FixedPoint implementation of i64.
Patches
- v1.3.0 users should upgrade to patch v1.3.1
- v1.4.0 users should upgrade to patch v1.4.1
All versions >=v1.4.1 contain the patch.
Workarounds
There are no known workarounds. Upgrade to the patched version.
Credits
soroban-fixed-point-math would like to thank the team at Certora for discovering and reporting the issue.
References
Impact
Incorrect rounding direction for signed mul and div operations
The$x * y$ and the divisor $z$ were negative. The logic assumed that if the intermediate product was negative, the final result must also be negative, neglecting the sign of $z$ .
mulDiv(x, y, z)function incorrectly handled cases where both the intermediate productThis resulted in rounding being applied in the wrong direction for cases where both$x * y$ and $z$ were negative. The functions most at risk are $z$ in
fixed_div_floorandfixed_div_ceil, as they often use non-constant numbers as the divisormulDiv.This error is present in all signed
FixedPointandSorobanFixedPointimplementations, includingi64,i128, andI256.Negative Overflow in
i64The$x * y$ . When the final result of
mulDiv(x, y, z)function fori64used thei128type to handle "phantom overflows". These are overflows that occur intermediately during a calculation, like when computing the intermediate productmulDivwas computed ini128, it was scaled back down toi64before returning. While the code verified that the result did not exceedi64::MAX, it did not check againsti64::MIN.This caused negative results smaller than
i64:MINto wrap around to a large positive number instead of being caught as an overflow.This error only exists for the
FixedPointimplementation ofi64.Patches
All versions
>=v1.4.1contain the patch.Workarounds
There are no known workarounds. Upgrade to the patched version.
Credits
soroban-fixed-point-math would like to thank the team at Certora for discovering and reporting the issue.
References