QuantAMM

QuantAMM
49,600 OP
View results
Submission Details
Severity: high
Invalid

Precision Loss Due to Division by `1e9` in QuantAMM's Packing and Unpacking Operations

Title

Precision Loss Due to Division by 1e9 in QuantAMM's Packing and Unpacking Operations

Summary

The QuantAMM protocol exhibits systematic precision loss in its packing and unpacking mechanisms, particularly in QuantAMMStorage.sol, including the quantAMMPack32Array function. The issue arises due to truncation and integer division, causing significant loss of precision.

Vulnerability Details

Here's the implementation of quantAMMPack32Array function in QuantAMMStorage contract:

function quantAMMPack32Array(int256[] memory _sourceArray) internal pure returns (int256[] memory targetArray) {
...
targetArray = new int256[](targetArrayLength);
for (uint i; i < nonStickySourceLength; ) {
unchecked {
targetArray[storageIndex] = quantAMMPackEight32(
int256(_sourceArray[i] / 1e9),
int256(_sourceArray[i + 1] / 1e9),
int256(_sourceArray[i + 2] / 1e9),
int256(_sourceArray[i + 3] / 1e9),
int256(_sourceArray[i + 4] / 1e9),
int256(_sourceArray[i + 5] / 1e9),
int256(_sourceArray[i + 6] / 1e9),
int256(_sourceArray[i + 7] / 1e9)
);
i += 8;
++storageIndex;
}
}
}
...

Root Issue

  1. General Precision Loss:
    As seen above, division by 1e9 was carried out which will truncate values below certain precision thresholds, leading to loss of least significant digits and asymmetric treatment of negative values.

  2. Precision Loss in quantAMMPack32Array:
    The function performs integer division by 1e9 before packing values, causing significant truncation.

  3. Cumulative Effects:
    Losses compound over multiple operations, particularly in token balances, pool share calculations, and asset allocations, leading to misrepresentation and imbalance in pools.

Impact

  1. Value Misrepresentation:
    Original values are not accurately preserved after pack/unpack cycles, leading to incorrect calculations and allocations.

  2. Disrupted Financial Calculations:
    Loss of precision impacts token amounts, price ratios, and asset allocations, potentially causing imbalanced pools or unfair swaps.

  3. Substantial Errors:
    Smaller values are disproportionately affected, and sub-unit values are completely lost below certain precision thresholds.

  4. Cumulative Loss:
    Over multiple operations, these errors compound, affecting user balances and protocol trustworthiness.

Tools Used

Manual Review

Recommendations

Here are some possible recommendations to fix the precision issue:

  1. Use Accurate Math: Use libraries that handle decimals better to avoid losing precision.

  2. Round Values: Add proper rounding instead of cutting off decimals.

  3. Increase Precision: Work with higher precision values (e.g., 1e18 instead of 1e9).

  4. Scale Safely: Scale values up before storing and scale them back down after retrieval.

  5. Add Extra Bits: Use available storage space to keep more precise numbers.

Updates

Lead Judging Commences

n0kto Lead Judge 7 months ago
Submission Judgement Published
Invalidated
Reason: Design choice

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.