# Solidity WAD/RAY Precision, mulDiv & Rounding Exploit Audits

> Eliminate Solidity fixed-point arithmetic bugs: 6-decimal USDC vs 18-decimal WAD scaling, divide-before-multiply truncation, 512-bit mulDiv, and dust loops.

Canonical: https://nftvenueledger.com/protocols/decimal-precision-truncation-wad-ray-rounding-direction
Last updated: 2026-11-14T19:40:25.251Z

**Publication:** NFTVenue Ledger
**Category:** Cross Chain Bridge Validator Exploits
**Published:** 2026-11-13T11:05:25.915Z
**Author:** Dr. Devon Kurosawa, PhD (PhD in Distributed Systems & Cryptographic Protocol Verification)
**Reviewer:** Not recorded

## Answer-first summary

Because the EVM lacks native floating-point opcodes, divide-before-multiply truncation, hardcoded 18-decimal assumptions on 6-decimal USDC, and reversed rounding drain vaults silently.

## Key takeaways

- Never divide before multiplying in Solidity integer math—always combine full products before division via 512-bit Math.mulDiv(x, y, z, Rounding) to prevent precision collapse and 256-bit intermediate reverts.
- Hardcoding 18 decimals fails catastrophically on 6-decimal USDC/USDT, 8-decimal WBTC, and 2-decimal GUSD; normalize all token and oracle values to unified 18-decimal USD WAD via dimensional scaling.
- Per-second compounding interest indices require 27-decimal RAY precision so small per-slot rates do not truncate to zero.
- Solidity 0.8+ does NOT revert on explicit integer downcasts like uint128(x) or int256(x)—always use OpenZeppelin SafeCast when packing storage structs.

## Related financial entities

- **Checks-Effects-Interactions (CEI) Security Pattern** (CEI-SEC) — product: Foundational programming design pattern in smart contract development preventing reentrancy attacks by executing internal state updates before external calls.
- **Concentrated Liquidity AMM Mathematical Specification** (CL-AMM) — product: Algorithmic market-making protocol allowing liquidity providers to allocate capital within discrete price intervals, maximizing capital efficiency.
- **Uniswap v4 Singleton & Hook Architecture** (UNI-V4) — product: Fourth-generation decentralized exchange protocol featuring a unified singleton contract and modular hooks that customize pool execution logic.

## Analysis

Deterministic consensus across thousands of EVM nodes forbids IEEE-754 floating-point instructions (`whose NaN and subnormal rounding nuances vary subtly across CPU architectures`). Every price, interest rate, LP share, and royalty across Ethereum DEXs and NFT venues in our [marketplaces directory](/marketplaces) (from [9mm Pro](/marketplaces/9mm-pro) and [Beethoven X](/marketplaces/beethoven-x) to [Uniswap v3/v4](/tokens/concentrated-liquidity-amm-spec)) executes in **256-bit integer fixed-point arithmetic**: `WAD (10^18)`, `RAY (10^27)`, `RAD (10^45)`, or binary `Q64.96 (2^96)`.

Anchored in our [cross-chain bridge, oracle & exploit RCA master guide](/protocols/cross-chain-bridge-verification-oracle-exploit-rca-guide-2026), **four recurring integer-math anti-patterns** account for hundreds of high-severity audit findings and live exploits (`SushiSwap RouteProcessor, Yield Protocol, Hundred, Balancer V2 linear-pool 1-wei rounding drain`)!

## Four Classic Solidity Fixed-Point Bugs Caught in Every Audit!

1. **Bug 1 — Divide-Before-Multiply Precision Collapse (`(a / b) * c == 0`)**:
 Writing `fee = (amount / 10_000) * feeBps` truncates `amount / 10_000` to **`0`** for every `amount < 10,000 wei` (`on 6-decimal USDC, $0.009999 pays ZERO fee!`)—and on interest rates `(elapsedSeconds / 31_536_000) * aprWad`, `elapsedSeconds / 31_536_000` evaluates to **`0` for every block interval under 1 full year (`0% interest accrued forever!`)**! Always **multiply ALL numerators first** via `Math.mulDiv(a, c, b)`!
2. **Bug 2 — Hardcoding `1e18` on `6-Decimal USDC/USDT` or `8-Decimal WBTC` (`10^12x Valuation Error!`)**:
 Multiplying `amount_USDC (1e6)` by `price_USD_WAD (1e18)` and dividing by `1e18` yields `1e6` instead of `1e18`—undervaluing `USDC` debt by **`1,000,000,000,000x`** (allowing `$1` of ETH to borrow the entire USDC pool) or conversely overvaluing `2-decimal GUSD`!
3. **Bug 3 — Intermediate `uint256` Overflow Without 512-Bit `FullMath.mulDiv`**:
 In Solidity `>=0.8.0`, `(x * y) / z` **reverts** whenever `x * y >= 2^256`, even when the final quotient `(x * y) / z` fits comfortably inside `uint128`! OpenZeppelin `Math.mulDiv` (`Remco Bloemen Chinese Remainder Theorem 512-bit trick`) computes exact `floor((x * y) / z)` in ~95 gas without overflowing intermediate `512-bit` products!
4. **Bug 4 — Reversed Rounding in Flash-Swap Loops (`Balancer V2 Boosted Pool $2M+ Exploit`)**:
 In Balancer V2 Linear Pools, swapping `1 wei` down to `0 wei` input cost rounded in the **attacker's favor** once pool balances shrunk via flash swaps—letting an attacker execute **300 micro-swaps inside 1 transaction** to siphon millions!

![Solidity 512-bit Math.mulDiv pipeline normalizing heterogeneous token decimals and enforcing directional rounding](/api/figure?type=flow&title=Overflow-Safe%20Fixed-Point%20Normalization%20%26%20mulDiv%20Pipeline&subtitle=Eliminating%206%2F8%2F18-decimal%20mismatches%2C%20intermediate%20256-bit%20overflows%2C%20and%201-wei%20flash%20siphon%20loops&steps=1.%20Query%20decimals%28%29%3A%3ACache%20IERC20Metadata%28token%29.decimals%28%29%20%3C%3D%2018%20%28reject%20%3E18%20exotic%20bugs%29%7C2.%20Scale%20to%20WAD%2FRAY%3A%3ANormalize%20heterogeneous%20amounts%20%2B%20Chainlink%2FPyth%20expo%20to%20unified%201e18%7C3.%20512-Bit%20mulDiv%3A%3ACompute%20Math.mulDiv%28x%2C%20y%2C%20z%2C%20Rounding%29%20with%20zero%20intermediate%20overflow%7C4.%20Zero-Output%20Guard%3A%3ARevert%20if%20non-zero%20input%20produces%200%20shares%2Fdebt%20or%200%20input%20yields%20%3E0%20out!&metrics=WAD%20%2F%20RAY%20%2F%20Q96%3A%3A10%5E18%20%2F%2010%5E27%20%2F%202%5E96%3A%3AStandard%20DeFi%20decimal%20%26%20binary%20scales%7CMULDIV%20INTERMEDIATE%3A%3A512-Bit%20Exact%3A%3AHandles%20x%20*%20y%20up%20to%202%5E512%20-%201%7CUSDC%20%2F%20WBTC%20DECIMALS%3A%3A6%20%2F%208%20Decimals!%3A%3ANEVER%20assume%2018%20decimals%20on%20ERC-20!%7CZERO-FOR-NONZERO%3A%3AHard%20Revert!%3A%3ABlocks%20free%201-wei%20micro-swap%20loops)

## Heterogeneous Token + Oracle Decimal Normalization Matrix

Whenever converting `amount_A` of Token A (`decimals d_A, oracle price P_A with decimals o_A`) into equivalent units of Token B (`decimals d_B, oracle price P_B with decimals o_B`), **never eyeball powers of 10 in your head**! Verify against the exact dimensional-analysis table:

| Token Pair & Oracle Setup | `d_A, o_A` | `d_B, o_B` | Exact Unified Conversion Formula (`Amt_B` in native `B` units) |
| :--- | :--- | :--- | :--- |
| `WETH -> USDC` (Chainlink 8-dec USD) | `18, 8` | `6, 8` | `mulDiv(amt_A, P_A, P_B * 10^(18 - 6))` = `mulDiv(amt_A, P_A, P_B * 1e12)` |
| `USDC -> WETH` (Chainlink 8-dec USD) | `6, 8` | `18, 8` | `mulDiv(amt_A * 1e12, P_A, P_B)` |
| `WBTC -> USDC` (WBTC 8d, ETH-quoted!) | `8, 18` | `6, 8` | Normalize **both** prices to `18-dec USD WAD` first, then scale `10^(d_B - d_A)`! |
| General Universal Formula (Any `d, o <= 18`) | `d_A, o_A` | `d_B, o_B` | `Value_USD_WAD = mulDiv(amt_A, P_A * 10^(18 - o_A), 10^d_A)` |

![Comparison of precision loss and overflow behavior across naive Solidity math, WAD math, RAY math, and Q64.96](/api/figure?type=bars&title=Fixed-Point%20Representation%20Trade-Offs%20in%20EVM%20Protocols&subtitle=Precision%20granularity%2C%20gas%20cost%2C%20and%20typical%20protocol%20domain%20across%20standard%20EVM%20numeric%20formats&steps=Naive%20%28a%20*%20b%29%20%2F%20c%3A%3AREVERTS%20whenever%20a%20*%20b%20%3E%3D%202%5E256%20%28~1.15e77%20-%3E%20overflows%20at%20%24100B%20WAD*RAY!%29%7CWAD%20%2810%5E18%20Decimal%29%3A%3A18%20decimals%3B%20standard%20for%20ERC-20%20balances%2C%20LTVs%2C%20and%20USD%20valuations%7CRAY%20%2810%5E27%20Decimal%29%3A%3A27%20decimals%20%28MakerDAO%20%2F%20Aave%29%3B%20eliminates%20per-second%20compound%20interest%20drift!%7CQ64.96%20%2F%20Q128.128%20Binary%3A%3ABit-shift%20%3E%3E%2096%20%2F%20%3E%3E%20128%20%28Uniswap%20v3%2Fv4%29%3B%20fastest%20binary%20price%20%26%20fee%20math!&metrics=PER-SECOND%20INTEREST%3A%3AUse%20RAY%20%2810%5E27%29%3A%3A18-dec%20WAD%20loses%20precision%20at%20low%20APRs!%7CWEIRD%20ERC-20%20LIST%3A%3AGUSD%3D2%2C%20YAM%3D24!%3A%3ATest%20weird-erc20%20suite%20in%20Foundry!%7CROUNDING%20AUDIT%3A%3AHalmos%20Check%3A%3AProve%20no%202-tx%20cycle%20increases%20balance%7CCASTING%20SAFETY%3A%3ASafeCast%20Lib%3A%3ASolidity%200.8%20does%20NOT%20revert%20uint128%28x%29!)

## Silent Downcast Truncation Trap (`uint128(x)` Does NOT Revert in Solidity 0.8!`)

Read that metric card twice—it surprises 80% of developers! While Solidity `0.8+` checks `+`, `-`, and `*` for overflow, **explicit type downcasting `uint96(bigUint256)` or `int256(hugeUint256)` silently truncates upper bits WITHOUT reverting**! Always use OpenZeppelin **`SafeCast.toUint128(x)`** alongside virtual-share guards from [ERC-4626 first-depositor share inflation](/protocols/erc4626-vault-first-depositor-share-inflation-rounding-attack)!

## Universal Dimensional-Safe Asset Conversion & Anti-Siphon Formula

```formula
1) Universal Dimensional-Safe Asset Conversion (Token A [d_A] -> Token B [d_B] via normalized 18-dec USD WAD prices):
 P_A_wad = P_A_raw * 10^(18 - oracleDecimals_A)
 P_B_wad = P_B_raw * 10^(18 - oracleDecimals_B)
 USD_wad = Math.mulDiv( amount_A, P_A_wad, 10^d_A, Rounding_Mode ) [Exact 18-decimal USD value]
 amount_B = Math.mulDiv( USD_wad, 10^d_B, P_B_wad, Rounding_Mode ) [Exact native Token B units]

2) Anti-Micro-Siphon Guard (Blocks Balancer Linear-Pool Style 1-Wei Loops):
 If Rounding_Mode == Floor (User Output): require(amountIn > 0 && amountOut > 0, "ZeroOutputDustSwap!");
 If Rounding_Mode == Ceil (User Input): require(amountOut > 0 && amountIn > 0, "FreeOutputExploit!");
 // Plus always cast packed storage fields via SafeCast.toUint128(amount) — NEVER raw uint128(amount)!
```

## Preventing Cross-Chain Relayer Replay & Reorg Double-Spends Next

From bit-level EVM math, we return to cross-chain consensus edge cases: what happens when a fast bridge relayer pays out `$2M` on Arbitrum after `3 blocks` on Polygon or an L1, and the source chain experiences a **12-block consensus reorganization (`reorg`)** erasing the deposit? Audit finality depths next in [bridge relayer message replay & chain-reorg finality depth](/protocols/bridge-relayer-message-replay-chain-reorg-finality-depth) inside our [Cross-Chain Bridge & Oracle Security hub](/topics/cross-chain-bridge-validator-exploits).

## Frequently asked questions

### Does Solidity 0.8.x automatically revert if uint256 overflows when cast to uint128 or uint96?

NO! Solidity 0.8 checked arithmetic only covers +, -, *, /, and ** operators; writing uint96(amount) silently masks off the upper 160 bits without reverting—requiring OpenZeppelin SafeCast.toUint96(amount).

### How does OpenZeppelin Math.mulDiv avoid reverting when x * y exceeds 2^256 - 1?

Math.mulDiv computes the full 512-bit product (prod1, prod0) using EVM MUL and MULMOD (Chinese Remainder Theorem) and divides in assembly, reverting ONLY if the final quotient exceeds 2^256 - 1.

### Why do lending protocols like Aave and MakerDAO use 27-decimal RAY for interest indices instead of 18-decimal WAD?

At a 1% annual rate compounded every 2-second L2 block, rate-per-block is ~6.3e-10; in 18-decimal WAD, compounding small principal slices loses significant significant digits, whereas 27-decimal RAY preserves 9 extra digits of precision.

### How did 1-wei rounding drain over $2M from Balancer V2 Boosted Linear Pools?

By flash-borrowing almost all pool liquidity to shrink virtual balances, the attacker created an exchange rate where swapping tiny dust rounded input cost down to 0 while output rounded to >=1, repeating hundreds of times in 1 tx.


## Source URLs

- https://docs.openzeppelin.com/contracts/5.x/api/utils#Math
- https://docs.openzeppelin.com/contracts/5.x/api/utils#SafeCast
- https://github.com/d-xo/weird-erc20

## Citation

- APA: Dr. Devon Kurosawa, PhD. (2026). Solidity Fixed-Point Precision Audits: WAD/RAY Scaling, mulDiv Overflow & Rounding Exploits. NFTVenue Ledger. https://nftvenueledger.com/protocols/decimal-precision-truncation-wad-ray-rounding-direction
- Canonical article: [Solidity Fixed-Point Precision Audits: WAD/RAY Scaling, mulDiv Overflow & Rounding Exploits](https://nftvenueledger.com/protocols/decimal-precision-truncation-wad-ray-rounding-direction)
- Site research index: [NFTVenue Ledger](https://nftvenueledger.com/llms-full.txt)
