QuantAMM

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

Division Before Multiplication and Raw Division Operations in Core Calculations

Description

Multiple update rule contracts perform division operations before multiplication in critical mathematical calculations, violating fixed-point arithmetic best practices.

The pattern appears in core components:

// Weight calculations in update rules
ONE.div(locals.denominator).mul(locals.newWeights[locals.i])
// Raw division in normalization factor
locals.normalizationFactor /= int256(_prevWeights.length);
int256(locals.kappa[0]).mul(locals.normalizationFactor - locals.newWeights[locals.i])
// Gradient calculations
locals.mulFactor = oneMinusLambda.pow(THREE).div(convertedLambda);
(_newData[i] - _poolParameters.movingAverage[i]).div(oneMinusLambda);
// Channel following calculations
envelope_exponent = -gradientSquared.div(widthSquared.mul(TWO));
scaledGradient = PI.mul(locals.newWeights[locals.i]).div(locals.width[0].mul(THREE));
// Critical price calculations in MultiHopOracle using raw division
data = 10 ** 36 / data; // For price inversions
data = (data * 10 ** 18) / oracleRes;

Weight Calculation Errors:

  • In pools with large denominators, the initial division could truncate significant digits

  • This affects the accuracy of weight updates, potentially leading to suboptimal asset allocation

  • The error compounds with each weight update operation

Raw Division in Normalization:

  • Uses unsafe raw division (/=) instead of PRBMath's safe division operations

  • Division result directly affects kappa multiplication, a key anti-momentum parameter

  • Dividing by number of weights before multiplication can amplify precision loss

  • More instances detected across multiple contracts:

locals.normalizationFactor /= int256(locals.prevWeightLength); // MomentumUpdateRule
locals.normalizationFactor /= int256(locals.prevWeightsLength); // PowerChannelUpdateRule
locals.normalizationFactor /= int256(locals.prevWeightLength); // ChannelFollowingUpdateRule
locals.normalizationFactor /= int256(locals.prevWeightLength); // DifferenceMomentumUpdateRule

Gradient Calculation Precision Loss:

  • Loss of precision in gradient calculations affects the entire update mechanism

  • Could cause the pool to react incorrectly to price changes

  • Critical for strategies relying on precise price movement tracking

Channel Following Precision:

  • Affects envelope calculations and scaling operations

  • Could lead to incorrect channel boundaries

  • May result in premature or delayed trading signals

Impact

The precision loss from dividing before multiplication affects the protocol's core mathematical operations:

  1. When division occurs first, significant digits can be truncated, especially with large denominators. This truncation then propagates through subsequent multiplications.

  2. These calculations form part of the protocol's trading strategy logic. Precision losses in gradient calculations and channel boundaries can cause:

    • Missed or mistimed trading opportunities

    • Suboptimal asset allocation

    • Degraded price responsiveness

  3. The impact compounds over time as these calculations occur on every weight update, potentially leading to accumulated drift from optimal strategy execution.

Mitigation

Reorder mathematical operations to perform multiplication before division:

// Weight calculations
ONE.mul(locals.newWeights[locals.i]).div(locals.denominator)
// Normalization factor
int256(locals.kappa[0])
.mul(locals.normalizationFactor)
.div(int256(_prevWeights.length)) -
int256(locals.kappa[0]).mul(locals.newWeights[locals.i])
// Gradient calculations
(_newData[i] - _poolParameters.movingAverage[i]).mul(SCALING_FACTOR).div(oneMinusLambda)
// Channel calculations
scaledGradient = PI.mul(locals.newWeights[locals.i]).mul(SCALING_FACTOR).div(locals.width[0].mul(THREE))

This ensures maximum precision is maintained throughout the calculation chain.

Updates

Lead Judging Commences

n0kto Lead Judge 10 months ago
Submission Judgement Published
Invalidated
Reason: Known issue
Assigned finding tags:

invalid_Rules_getWeights_precision_loss_div_mul

## [Low-24] Divide before multiply

Support

FAQs

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

Give us feedback!