Crypto winter
English

Solidity rounding and precision loss

Integer division discards remainders. Rounding the wrong direction or dividing too early can undercharge or over-credit users.

Risk assessment

Integer division discards remainders. Rounding the wrong direction or dividing too early can undercharge or over-credit users.

Mitigation

Specify a rounding direction for every operation. Apply full-precision mulDiv before rounding at the accounting boundary.

Solidity mitigation excerpt

uint256 input = Math.mulDiv(quantity, numerator, denominator, Math.Rounding.Ceil);

Foundry · Regression check

Fuzz small and boundary values. Assert the required input rounds up, output rounds down and a round trip cannot increase user assets.

Mitigation

Related topics

What should I verify first?

Fuzz small and boundary values. Assert the required input rounds up, output rounds down and a round trip cannot increase user assets.

Primary reference