QuantAMM

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

QuantAMMGradientBasedRule - Incorrect Indexing in `intermediateGradientStates[_poolParameters.pool]` Causes Revert

Title

QuantAMMGradientBasedRule - Incorrect Indexing in intermediateGradientStates[_poolParameters.pool] Causes Revert

Summary

The _calculateQuantAMMGradient function in the QuantAMMGradientBasedRule contract incorrectly uses the index i instead of locals.storageArrayIndex when updating intermediateGradientStates[_poolParameters.pool]. This results in an out-of-bounds error when the pool has more than 4 assets and lambda vector length greater than 1, causing the function to revert.

Vulnerability Details

The _calculateQuantAMMGradient function is a key component of the QuantAMMGradientBasedRule, responsible for calculating gradient values for the pool's asset weights. The function relies on intermediateGradientStates, which is a mapping that stores quantum-packed gradient values.

Here's the implementation of the _calculateQuantAMMGradient function:

function _calculateQuantAMMGradient(
int256[] memory _newData,
QuantAMMPoolParameters memory _poolParameters
) internal returns (int256[] memory) {
...
//You cannot have a one token pool so if its one element you know it's scalar
if (_poolParameters.lambda.length == 1) {
...
} else {
// if the parameters are defined as per constituent we do the same as the if loop but
//tracking the appropriate lambda for each asset and the appropriate storage index
if (notDivisibleByTwo) {
--numberOfAssetsMinusOne;
}
for (uint i; i < numberOfAssetsMinusOne; ) {
...
locals.finalValues[locals.secondIndex] = locals.mulFactor.mul(locals.secondIntermediateValue);
>> intermediateGradientStates[_poolParameters.pool][i] = _quantAMMPackTwo128(
locals.intermediateGradientState[i],
locals.secondIntermediateValue
);
unchecked {
i += 2;
++locals.storageArrayIndex;
}
}
...
}
return locals.finalValues;
}

When _poolParameters.lambda.length > 1 and the pool contains more than 4 assets, the function incorrectly uses the index i instead of locals.storageArrayIndex to update intermediateGradientStates. This mismatch causes an out-of-bounds error, as the index i does not correspond to the correct array position for the packed gradient values.

Impact

  1. Reversion of Operations: The function fails with an out-of-bounds revert, blocking all operations requiring gradient calculations.

  2. Loss of Dynamic Updates: Pools cannot dynamically adjust asset weights using gradient-based strategies, degrading performance.

  3. Inefficiency: Without proper calculations, the pool cannot utilize quantum-packed gradient values, leading to suboptimal asset management.

Tools Used

Manual Review

Recommendations

Replace i with locals.storageArrayIndex, so that it points to correct array position.

- intermediateGradientStates[_poolParameters.pool][i] = _quantAMMPackTwo128(
+ intermediateGradientStates[_poolParameters.pool][locals.storageArrayIndex] = _quantAMMPackTwo128(
Updates

Lead Judging Commences

n0kto Lead Judge 7 months ago
Submission Judgement Published
Validated
Assigned finding tags:

finding_gradient_rules_more_than_3_assets_and_1_lambda_will_DoS_the_update

Likelihood: Medium/High, assets>4, lambdas > 1. Impact: Medium/High, DoS update but pool works fine. Pool with 5 assets will use incorrect weights.

Support

FAQs

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