Rounding and precision errors
Solidity has no decimals, so every division rounds. If a calculation rounds in the caller's favour, or divides before it multiplies, an attacker can repeat it until tiny errors add up to real money. Rounding flaws show up in share accounting, interest and reward maths, fees and exchange rates, and often pass tests because each individual error is small.
Common patterns
- Wrong rounding direction. Shares, fees or debts that round in the user’s favour.
- Divide before multiply. Precision lost early in a calculation and then multiplied up.
- Mismatched decimals. Tokens with 6, 8 and 18 decimals combined without scaling.
- Zero results. Small deposits or rewards that round to zero, either losing user funds or letting an attacker skip a fee.
- Repeated rounding. A function that leaks one unit per call, called thousands of times in one transaction.
Prevention
- Decide and document the rounding direction for every calculation.
- Multiply before dividing, and use full-precision maths libraries for complex expressions.
- Normalise decimals explicitly.
- Fuzz and invariant test with extreme values. For vaults, see ERC-4626 inflation attacks.
Questions
Frequently asked questions
Which way should a contract round?
In favour of the protocol. When users deposit or mint, round shares down. When they withdraw or burn, round the shares they must give up up. That way rounding can never be farmed.
How do auditors find these bugs?
By checking the direction of every division in value calculations, and by fuzzing the maths with extreme values, tiny amounts and long loops.
Published Updated
