Formal verification for smart contracts
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 type | Smart-contract example | Hardware analogue |
|---|---|---|
| Invariant | Sum of all balances equals total supply | Credit conservation (Chapter 14) |
| Access control | Only the owner can pause the contract | Privileged registers not writable from user mode |
| Transition rule | A transfer changes exactly two balances, by +x and −x | Counter increments by exactly one per event |
| No reentrancy | State is updated before any external call | No re-entry into a busy state machine |
| Liveness / no lock-up | Users can always withdraw their own funds | No 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
// 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.