FVO — Formal Verification Online
📘 Formal verification guide

Formal verification for smart contracts

9 min read · Updated

This article is part of our formal verification guide, which covers the foundations, model checking, solvers, theorem proving, equivalence checking and industry use cases.

A smart contract is a small program that holds and moves real money, cannot easily be patched after deployment and is attacked by anyone on the internet. That combination makes it one of the strongest cases for formal verification outside hardware — and the techniques will feel familiar to any hardware verification engineer.

Why testing is not enough

In 2016 a flaw in "The DAO" contract allowed an attacker to drain around 60 million dollars of Ether by re-entering a withdrawal function before its balance was updated. Since then, logic errors, missing access checks and arithmetic edge cases in DeFi protocols have cost far more. These bugs typically need a specific sequence of calls with specific values — precisely the kind of corner case that formal methods search exhaustively.

Contracts are state machines

A contract has state (its storage: balances, owners, flags), and every transaction is a transition. That is the same model as a hardware block with registers, and the same kinds of properties apply:

Property typeSmart-contract exampleHardware analogue
InvariantSum of all balances equals total supplyCredit conservation (Chapter 14)
Access controlOnly the owner can pause the contractPrivileged registers not writable from user mode
Transition ruleA transfer changes exactly two balances, by +x and −xCounter increments by exactly one per event
No reentrancyState is updated before any external callNo re-entry into a busy state machine
Liveness / no lock-upUsers can always withdraw their own fundsNo deadlock; every request is eventually served

How the tools work

SMT-based verification

The contract (or its EVM bytecode) is translated into logical formulas. EVM words are 256-bit, which maps directly onto the bit-vector theory of SMT solvers; storage maps onto arrays. The tool asks the solver: "is there a state satisfying the invariant and a call that breaks it?" If not, the invariant is inductive and proven for every reachable state — the same argument as k-induction in hardware (BMC vs k-induction, SAT vs SMT solvers).

Symbolic execution and symbolic testing

Instead of concrete test inputs, a test function takes symbolic arguments; the tool explores every path through the code and asks the solver whether any input makes an assertion fail. This lets developers write properties in the same language as their tests.

Tools you will meet

  • The Solidity compiler has a built-in SMTChecker that checks assertions, overflow and other conditions.
  • Dedicated provers such as the Certora Prover verify rules and invariants written in a specification language.
  • Semantics-based frameworks such as KEVM (the K framework's formal EVM semantics) support deep proofs.
  • Symbolic testing tools such as Halmos and hevm run Foundry-style tests with symbolic inputs.
  • Auditors also use proof assistants for core protocols and cryptographic components.

An example invariant

specification sketch
// Solidity-style pseudo-specification
// invariant: the contract's recorded total equals the sum of user balances
invariant totalMatchesSum:  totalSupply == sum(balances[u] for all u)

// rule: transfer moves value and nothing else
rule transferMoves(from, to, x):
    require from != to
    before = (balances[from], balances[to], totalSupply)
    transfer(from, to, x)
    assert balances[from] == before.0 - x
    assert balances[to]   == before.1 + x
    assert totalSupply    == before.2

"Sum over all users" is handled the way hardware engineers handle "for all IDs": a ghost variable (auxiliary state) that tracks the sum, or a symbolic user — the same idea as auxiliary code and symbolic variables.

Pitfalls shared with hardware formal

  • Over-constraining assumptions ("assume the caller is not the contract itself") can hide the exact attack you care about — see when proofs lie.
  • Vacuous rules pass because their preconditions can never be met. Always check that each rule can be reached.
  • Incomplete specifications: a proof only covers the properties you wrote. Review them like code.
  • Environment modelling: external contracts and oracles must be modelled, often non-deterministically — like the neighbour stubs in Chapter 16.

Formal verification of smart contracts is the same discipline as hardware formal, applied to a different state machine. Start with the shared foundations in our complete formal verification guide.

Learn it hands-on

More from the formal verification guide

← Back to formal verification foundations: the complete guide

Discussion

No comments yet.

Was this page helpful?

Tip: select any text on the page before clicking Report and it will be quoted in your report.