Skip to content
Smart contracts Audit finding Intermediate

Unwrapping while equating inside the eq function fails to account for the set L_MATISSA_FLAG

The Float128 library's equality function compares the raw bit patterns of two packed floats, so two values that represent the same number but use different mantissa sizes (38-digit vs 72-digit) are reported as unequal. An attacker could exploit this to cause incorrect equality checks in contracts using the library.

YouCrossTheLineAlfie Follow with 0xbrett8571, agent3bood and 8 others

Originally disclosed on Code4rena

Submitted by YouCrossTheLineAlfie, also found by 0xbrett8571, agent3bood, ChainSentry, gmh5225, harsh123, maxzuvex, patitonar, Rorschach, Uddercover and X-Tray03.

The Float128::eq function is designed to return a boolean if the given two packed floats are equal. However, the issue lies with the way the function equates two packed floats via unwrapping:

    function eq(packedFloat a, packedFloat b) internal pure returns (bool retVal) {
        retVal = packedFloat.unwrap(a) == packedFloat.unwrap(b);
    }

As per the docs, it is clearly mentioned that Mantissa can be of two types M: 38 Digits and L: 72 digits.

/****************************************************************************************************************************
 * The mantissa can be in 2 sizes: M: 38 digits, or L: 72 digits                                                             *
 *      Packed Float Bitmap:                                                                                                 *
 *      255 ... EXPONENT ... 242, L_MATISSA_FLAG (241), MANTISSA_SIGN (240), 239 ... MANTISSA L..., 127 .. MANTISSA M ... 0  *
 *      The exponent is signed using the offset zero to 8191. max values: -8192 and +8191.                                   *
 ****************************************************************************************************************************/

So if there are two packed floats which are equal in nature upon deduction, but one of them has 38 digit mantissa and other has the 72 digit mantissa, the eq function would fail as unwrapping custom float type to underlying type (uint) means that the packed float with 72 digit mantissa will have the L_MANTISSA_FLAG set; which would introduce an incorrect unwrapped version than intended leading to false values.

Impact

The eq function is one of the crucial components of the library, this issue renders it useless for scenarios when one of the packed float has the L_MANTISSA_FLAG on.

Recommended mitigation steps

After running through a few different solutions, the recommendation would be to normalise values before equating, further optimizations are made to reduce gas costs:

    function eq(packedFloat a, packedFloat b) internal pure returns (bool retVal) {
        // If the bit patterns are equal, the values are equal
        if (packedFloat.unwrap(a) == packedFloat.unwrap(b)) {
            return true;
        }

        // If either is zero (no mantissa bits set), special handling
        bool aIsZero = (packedFloat.unwrap(a) & MANTISSA_MASK) == 0;
        bool bIsZero = (packedFloat.unwrap(b) & MANTISSA_MASK) == 0;

        if (aIsZero && bIsZero) return true;
        if (aIsZero || bIsZero) return false;

        // Getting the mantissa and exponent for each value
        (int mantissaA, int exponentA) = decode(a);
        (int mantissaB, int exponentB) = decode(b);

        // Checking if signs are different
        if ((mantissaA < 0) != (mantissaB < 0)) return false;

        // Getting absolute values
        int absA = mantissaA < 0 ? -mantissaA : mantissaA;
        int absB = mantissaB < 0 ? -mantissaB : mantissaB;

        // Applying exponents to normalize values
        // Convert both to a standard form with normalized exponents

        // Removing trailing zeros from mantissas (binary search kind of optimisation can be made, but later realised mantissas can be 10000000001 as well, sticking with this O(num_of_digits - 1) solution)
        while (absA > 0 && absA % 10 == 0) {
            absA /= 10;
            exponentA += 1;
        }

        while (absB > 0 && absB % 10 == 0) {
            absB /= 10;
            exponentB += 1;
        }

        // Checking if the normalized values are equal
        return (absA == absB && exponentA == exponentB);
    }

Below is the test that proves to the issue to be fixed when the mitigation above is replaced with the existing eq function:

    function testFixedEq() public {
        // Contains 72 digits (71 zeros) and -71 exponent (L Mantissa used here)
        packedFloat packed1 = Float128.toPackedFloat(100000000000000000000000000000000000000000000000000000000000000000000000, -71);

        // This is exactly the same value which would've resulted if packed1 was human readable (a * 10^b)
        packedFloat packed2 = Float128.toPackedFloat(1, 0);

        (int256 mantissa2, int256 exponent2) = packed2.decode();
        (int256 mantissa1, int256 exponent1) = packed1.decode();

        console2.log("Mantissa1: ", mantissa1); // Mantissa1:  100000000000000000000000000000000000000000000000000000000000000000000000
        console2.log("Exponent1: ", exponent1); // Exponent1:  -71
        console2.log("Mantissa2: ", mantissa2); // Mantissa2:  10000000000000000000000000000000000000
        console2.log("Exponent2: ", exponent2); // Exponent2:  -37

        // Eq Passes now
        bool isEqual = Float128.eq(packed1, packed2);
        assertEq(isEqual, true);
    }

Proof of Concept

The below test case can ran inside the /test folder by creating a file called test.t.sol:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "forge-std/Test.sol";
import "forge-std/console2.sol";
import "src/Float128.sol";

contract TestingContract is Test {
    using Float128 for packedFloat;
    using Float128 for int256;

function testBrokenEq() public {
        // Contains 72 digits (71 zeros) and -71 exponent (L Mantissa used here)
        packedFloat packed1 = Float128.toPackedFloat(100000000000000000000000000000000000000000000000000000000000000000000000, -71);

        // This is exactly the same value which would've resulted if packed1 was human readable (a * 10^b)
        packedFloat packed2 = Float128.toPackedFloat(1, 0);

        (int256 mantissa2, int256 exponent2) = packed2.decode();
        (int256 mantissa1, int256 exponent1) = packed1.decode();

        console2.log("Mantissa1: ", mantissa1); // Mantissa1:  100000000000000000000000000000000000000000000000000000000000000000000000
        console2.log("Exponent1: ", exponent1); // Exponent1:  -71
        console2.log("Mantissa2: ", mantissa2); // Mantissa2:  10000000000000000000000000000000000000
        console2.log("Exponent2: ", exponent2); // Exponent2:  -37

        // Eq fails
        bool isEqual = Float128.eq(packed1, packed2);
        assertEq(isEqual, false);
    }

}

oscarserna (Forte) confirmed


The original report as published on Code4rena, by YouCrossTheLineAlfie. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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 eq function compares the raw packed representations without normalising the mantissa size or exponent, so the L_MANTISSA_FLAG bit causes a mismatch.

Impact

The eq function returns incorrect results when one of the packed floats has the L_MANTISSA_FLAG set, making it unreliable for equality checks.

What the attacker needed

Contract uses Float128.eq; one value has L_MANTISSA_FLAG set

The fix

The eq function should normalise the values before comparing, for example by decoding both, removing trailing zeros from the mantissas, and comparing the normalised mantissa and exponent.

How it was found

  1. Create a packed float with a 72-digit mantissa and a negative exponent that equals 1.
  2. Create a packed float for the value 1 with a 38-digit mantissa.
  3. Call Float128.eq on the two packed floats.
  4. Observe that the function returns false even though the values are equal.

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 format. The same number can be stored in two ways: with a short mantissa (38 digits) or a long one (72 digits). The equality check just compares the raw storage bits, so it says they are different even though they mean the same value.

What is Smart contracts?

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.

More by YouCrossTheLineAlfie

View all

10 more disclosed reports, best first.

More from Forte: Float128 Solidity Library

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Smart contract vulnerabilities

The lesson behind this report.

Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.