Precision Loss Due to Division by 1e9
in QuantAMM's Packing and Unpacking Operations
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.
Here's the implementation of quantAMMPack32Array
function in QuantAMMStorage contract:
Root Issue
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.
Precision Loss in quantAMMPack32Array:
The function performs integer division by 1e9 before packing values, causing significant truncation.
Cumulative Effects:
Losses compound over multiple operations, particularly in token balances, pool share calculations, and asset allocations, leading to misrepresentation and imbalance in pools.
Value Misrepresentation:
Original values are not accurately preserved after pack/unpack cycles, leading to incorrect calculations and allocations.
Disrupted Financial Calculations:
Loss of precision impacts token amounts, price ratios, and asset allocations, potentially causing imbalanced pools or unfair swaps.
Substantial Errors:
Smaller values are disproportionately affected, and sub-unit values are completely lost below certain precision thresholds.
Cumulative Loss:
Over multiple operations, these errors compound, affecting user balances and protocol trustworthiness.
Manual Review
Here are some possible recommendations to fix the precision issue:
Use Accurate Math: Use libraries that handle decimals better to avoid losing precision.
Round Values: Add proper rounding instead of cutting off decimals.
Increase Precision: Work with higher precision values (e.g., 1e18 instead of 1e9).
Scale Safely: Scale values up before storing and scale them back down after retrieval.
Add Extra Bits: Use available storage space to keep more precise numbers.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.