# Rounding and precision errors in Solidity: why one wei matters

URL: https://shield.quillaudits.com/learn/vulnerabilities/rounding-precision-errors/

A rounding error in Solidity is integer math that drops a remainder in the user's favor, which an attacker repeats or amplifies until it becomes real money. Every division in Solidity rounds down unless you round up yourself. Round against the user, multiply before you divide, and test the values near zero, where one wei is a large share.

Last updated 24 November 2026. 5 minute read. By the QuillShield team.

## How does a rounding error become an exploit?

Solidity has no fractions. `7 / 2` is 3, and the half is gone. That is fine when the protocol keeps the remainder, and a bug when the user keeps it. An attacker turns it into money in three ways:

- **Repetition.** A withdrawal that burns one share too few can be repeated in a loop or a batch until the remainders add up.
- **Tiny balances.** Near zero, one wei is a large share of a pool, so a one-wei error moves prices.
- **Rates that round to 0.** A price or exchange rate that rounds to 0 can switch off any check that multiplies by it.

Dividing before multiplying loses precision the same way, and so does mixing tokens with 6 and 18 decimals without scaling them.

## What does vulnerable code look like?

This pool works out how many shares to burn for a withdrawal, and rounds that number down.

```
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract SharePool {
    uint256 public totalShares;
    uint256 public totalAssets;
    mapping(address => uint256) public shares;

    function deposit(uint256 assets) external {
        // Rounds down: the user gets slightly fewer shares, which is the safe way.
        uint256 minted = totalShares == 0 ? assets : (assets * totalShares) / totalAssets;
        shares[msg.sender] += minted;
        totalShares += minted;
        totalAssets += assets;
        // ... pull `assets` from msg.sender
    }

    function withdraw(uint256 assets) external {
        // Rounds down too: the user burns too few shares and keeps the remainder.
        uint256 toBurn = (assets * totalShares) / totalAssets;
        shares[msg.sender] -= toBurn;
        totalShares -= toBurn;
        totalAssets -= assets;
        // ... send `assets` to msg.sender
    }
}
```

With 1,000 shares backed by 3,000 assets, each share is worth 3 assets. `withdraw(2)` should burn two thirds of a share, and 2 × 1,000 / 3,000 rounds down to 0. The caller gets 2 assets for nothing and can repeat it until the pool is empty.

## How do you fix it?

```
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract SafeSharePool {
    uint256 public totalShares;
    uint256 public totalAssets;
    mapping(address => uint256) public shares;

    function mulDivUp(uint256 x, uint256 y, uint256 d) internal pure returns (uint256) {
        return (x * y + d - 1) / d;
    }

    function deposit(uint256 assets) external {
        uint256 minted = totalShares == 0 ? assets : (assets * totalShares) / totalAssets;
        require(minted > 0, "zero shares");
        shares[msg.sender] += minted;
        totalShares += minted;
        totalAssets += assets;
        // ... pull `assets` from msg.sender
    }

    function withdraw(uint256 assets) external {
        require(assets > 0, "zero assets");
        // Rounds up: the pool, not the user, keeps the remainder.
        uint256 toBurn = mulDivUp(assets, totalShares, totalAssets);
        shares[msg.sender] -= toBurn;
        totalShares -= toBurn;
        totalAssets -= assets;
        // ... send `assets` to msg.sender
    }
}
```

- **Round against the user.** Round down what you pay out or mint for users, and round up what they pay or burn. The fixed pool burns 1 share for `withdraw(2)`.
- **Use a full-precision mulDiv.** `(x * y + d - 1) / d` can overflow on large values. OpenZeppelin's `Math.mulDiv` takes a rounding direction and keeps the full 512-bit product in its middle step.
- **Multiply before you divide,** and scale tokens to a common number of decimals before you compare or combine them.
- **Handle 0 on purpose.** Revert when a computed amount, rate or price rounds to 0, unless 0 is a valid answer.

## How has it been exploited?

At 7:46 AM UTC on 3 November 2025 an attacker hit Balancer V2's Composable Stable Pools. Balancer's post-mortem estimates $94.8M of user funds stolen from the pools it deployed, and names the core problem as incorrect rounding in the "exact out" swap path for Stable Pools. Counting forks of the same code on other chains, BlockSec puts total losses above $125M.

BlockSec's analysis shows how. The pool scales token amounts with a step that only rounds down. In a single batch swap, the attacker set one token's balance on a rounding boundary, then made a swap whose rounded-down input understated what it should have cost. That shrank the pool's invariant, and because the pool token's price is the invariant divided by its supply, it lowered that price. The attacker took the profit by withdrawing in a separate transaction.

The same class broke Resupply in June 2025, when an exchange rate rounded down to 0. The page on the [ERC-4626 inflation attack](https://shield.quillaudits.com/learn/vulnerabilities/erc4626-inflation-attack/) walks through it.

## What should a reviewer check?

- For every division, which way does it round, and does the protocol keep the remainder?
- Are multiplications done before divisions, with a mulDiv that can't overflow?
- What happens at 1 wei of assets, 1 share and an empty pool?
- Can a user repeat a small operation, in a loop, a batch or many transactions, and stack the remainders?
- Can a rate or price round to 0, and what does the code do then?
- Are token decimals handled, 6 against 18, and is scaling applied in the right direction?
- Do fuzz or invariant tests check that the contract's assets always cover what its shares or balances are worth?

## How does QuillShield check for it?

Rounding bugs hide between functions, because the rounding in one conversion is often only wrong next to the rounding in another. A QuillShield full audit reviews the arithmetic across the whole codebase in scope, and on a full audit a finding comes with its proof of concept result, so you can see whether the remainder can actually be taken.

Arithmetic also changes in small commits, like a new fee or a refactored conversion. QuillShield checks each pull request against the audit, so new arithmetic is reviewed next to the code it has to agree with.

Full audits start at $49 and are priced from the lines of Solidity in scope, as the [pricing page](https://shield.quillaudits.com/pricing/) shows. The guide to [running your first audit](https://shield.quillaudits.com/guides/run-your-first-audit/) walks through getting a quote for your own code. [Sign up free](https://shield.quillaudits.com/register) and you see the exact price before anything is charged.

## Common questions

### Should I round up or down?

Round in the protocol's favor: down when you work out what to pay out or mint for a user, up when you work out what a user must pay or burn. The ERC-4626 standard asks vaults to favor the vault in every calculation.

### Can one wei of rounding really matter?

Yes, when an attacker can repeat it or when balances are tiny. Balancer V2 lost an estimated $94.8M in November 2025 to incorrect rounding in one swap path.

### Does Solidity 0.8 fix precision problems?

No. Checked arithmetic stops overflow and underflow, but division still drops the remainder. Precision depends on the order of operations and the rounding you choose.

### How do I test rounding?

Fuzz small and large values, include 1 wei and empty states, and write an invariant test that the contract's assets always cover what its shares are worth.

## Sources

- [Nov 3 Exploit Post-Mortem, Balancer](https://medium.com/balancer-protocol/nov-3-exploit-post-mortem-51dcbeb6b020)
- [In-Depth Analysis: The Balancer V2 Exploit, BlockSec](https://blocksec.com/blog/in-depth-analysis-the-balancer-v2-exploit)
- [ERC-4626: Tokenized Vaults](https://eips.ethereum.org/EIPS/eip-4626)
- [Resupply crvUSD-wstUSR Market Post Mortem, Resupply](https://mirror.xyz/0x521CB9b35514E9c8a8a929C890bf1489F63B2C84/ygJ1kh6satW9l_NDBM47V87CfaQbn2q0tWy_rtp76OI)
