← Lab /Security & privacy

Dimensional analysis can eliminate entire class of DeFi formula bugs

Applying dimensional analysis can categorically rule out logic and arithmetic bugs in DeFi smart contracts without code changes.

Security & privacy Trail of Bits 24 Mar 2026 Method Prototype evidence

Published as “Spotting issues in DeFi with dimensional analysis”

Why it matters

Adopting dimensional annotations could reduce arithmetic bugs and improve auditability of DeFi contracts.

Summary

The post introduces dimensional analysis, a technique from physics, as a way to check that DeFi formulas have consistent units such as tokens, prices, and liquidity. It demonstrates several concrete examples where formulas are dimensionally invalid, such as a price calculation that yields the square root of a price and a K-value formula that fails dimensional checks. The authors argue that documenting units in code, as done in Reserve Protocol, and using a Claude plugin prototype can help catch these bugs automatically, though no large-scale evaluation is provided.

What they found 3

  1. A price formula that divides token amount by liquidity is dimensionally incorrect.

    “Consequently, we have different dimensions on the right and left sides of the formula. This means the formula \(\textit{Price} = \frac{\text{number of token A}}{\textit{liquidity}}\) is incorrect.”

  2. Among two candidate K formulas, only the one with division is dimensionally valid.

    “So we can see that the first formula can’t be valid, and the second one is dimensionally valid!”

  3. Reserve Protocol’s unit comments serve as a best-practice example of dimensional safety.

    “Reserve Protocol’s unit comments are a textbook best practice.”

Caveats 3

  • In a stable pool, tokens are designed to maintain near-equal value. Under that assumption, token A and token B are treated as if they were the same “dimension.” This trick makes the formula workable in this special case.
  • These conventions require discipline, but they improve dimensional safety in a language that does not natively support it.
  • We’ve taken a first step toward automating this kind of analysis with a Claude plugin for dimensional checking, which we’ll introduce in a follow-up post.

What’s new here 4

  • Applies dimensional analysis to DeFi smart contract formulas for the first time.
  • Shows concrete examples where incorrect formulas are caught purely by unit mismatch.
  • Highlights Reserve Protocol’s unit-comment practice as a real-world illustration.
  • Introduces a Claude plugin prototype for automated dimensional checking.