The QuantAMMMathGuard contract's weight normalization process can result in significant precision loss (up to ~1.96e13) when handling high-precision weight values, far exceeding the expected rounding error of 1e-18 mentioned in the comments. This could lead to unexpected weight distributions and potential economic exploits.
Location: pkg/pool-quantamm/contracts/rules/base/QuantAMMMathGuard.sol
The issue occurs in _normalizeWeightUpdates()
where rounding errors compound during weight normalization. The critical vulnerability stems from two key components:
Error Accumulation (Lines 80-105):
Error Concentration (Lines 106-109):
The vulnerability manifests through:
Each addition to newWeightsSum
introduces small rounding errors
With 8 tokens, these errors compound across multiple operations
All accumulated error is concentrated in the first weight
The actual error can be ~1.96e13 times larger than the claimed 1e-18
A simple change of 1 in the last digit can trigger this issue:
This demonstrates that the comment's assumption of "very small (1e-18) rounding error" is off by 13 orders of magnitude in the worst case.
Proof of Concept:
Test Results:
Severity: HIGH
Technical Impact:
Precision loss ~20 trillion times larger than expected
Weight changes deviate significantly from intended values
Breaks assumption of minimal rounding errors
Affects all 8-token pools
Economic Impact:
Unexpected weight distributions
Potential arbitrage opportunities
MEV exploitation possible
Loss of intended pool behavior
Foundry testing framework
Manual code review
Mathematical analysis of precision requirements
Custom test suite for rounding edge cases
Implement Safe Rounding:
Add Precision Safeguards:
Track and limit maximum precision loss
Use higher precision for intermediate calculations
Consider using fixed-point library with more decimal places
Add explicit precision loss checks
Architectural Changes:
Consider using basis points (0.01%) for weight representation
Implement two-phase normalization process
Add precision loss monitoring and alerts
Consider maximum token limits based on precision requirements
This issue is distinct from:
mathguard-weight-overflow.md: Deals with integer overflow (~-1e34), not precision loss
mathguard-negative-weights.md: Focuses on negative weights, not precision degradation
normalization-epsilon-guard-violation.md: Addresses epsilon violations, not precision loss
Please read the CodeHawks documentation to know which submissions are valid. If you disagree, provide a coded PoC and explain the real likelyhood and the detailed impact on the mainnet without any supposition (if, it could, etc) to prove your point.
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.