Fuzzing vs formal verification: how much assurance does your protocol need?
Manual review, invariant fuzzing and formal verification find different bugs. What each technique really proves, where it falls short and how to combine them.
Published: 3 min read
A smart contract is the worst kind of software to get wrong. The code is public, it holds real money, anyone can call it in any order, and once it is deployed a fix is slow at best and impossible at worst.
A careful manual review is essential, but it has a blind spot. Humans are very good at spotting flawed logic and bad design. They are not good at exploring every possible sequence of transactions. The bugs that drain protocols usually live in that space: a rounding error that only matters after a donation, or a state that is only reachable after five specific calls.
That is where fuzzing and formal verification come in. They are often mentioned together, but they answer different questions.
Start with invariants
Both techniques need the same thing from you: a precise statement of what must always be true. These are your protocol’s invariants, and writing them down is half the value of the exercise. Typical examples:
- Solvency: the contract always holds at least as many tokens as it owes its users.
- Conservation: the shares held by all users add up to the total supply.
- Access control: only the governance timelock can change fee parameters.
- No free value: depositing and immediately withdrawing never returns more than was deposited.
If your team cannot write these down, that is already a finding.
Fuzzing: try to break it, millions of times
An invariant fuzzer (Foundry, Echidna or Medusa, for example) calls your contracts with random inputs in random order, and after every call checks that your invariants still hold. Handlers keep the calls realistic: they bound the inputs and track what should have happened in “ghost” variables.
contract VaultInvariants is Test {
MockERC20 asset;
Vault vault;
VaultHandler handler;
function setUp() public {
asset = new MockERC20();
vault = new Vault(asset);
handler = new VaultHandler(vault, asset);
// The fuzzer calls random sequences of the handler's functions.
targetContract(address(handler));
}
/// Solvency: the vault always holds at least what it owes.
function invariant_solvent() public view {
assertGe(asset.balanceOf(address(vault)), vault.totalDeposits());
}
/// Accounting matches every deposit and withdrawal the fuzzer made.
function invariant_accounting() public view {
assertEq(vault.totalDeposits(), handler.ghostDeposited() - handler.ghostWithdrawn());
}
}
When an invariant breaks, the fuzzer reports the exact sequence of calls that broke it and shrinks it to the shortest version. You get a reproducible exploit, not a theory.
Strengths: it is fast to set up, cheap to run on every commit, and every failure is concrete.
Limits: it only explores the inputs it happens to try. A condition that needs one specific 256-bit value, or a state that sits twenty calls deep, can survive millions of runs. A long green fuzzing campaign is strong evidence, but it is not proof.
Formal verification: prove it for every input
Formal verification does not sample inputs. It reasons about all of them at once. Symbolic execution tools such as Halmos and hevm treat inputs as mathematical variables and ask a solver whether any value can make an assertion fail. Halmos reuses Foundry’s test syntax, so a property looks familiar:
/// Halmos treats `user` and `amount` as symbolic: every possible value at once.
function check_withdrawNeverExceedsBalance(address user, uint256 amount) public {
uint256 balanceBefore = vault.balanceOf(user);
vm.prank(user);
vault.withdraw(amount); // if this call does not revert…
assert(amount <= balanceBefore); // …this must hold for every input
}
Provers such as the Certora Prover go further. Rules are written in a dedicated specification language, checked against the compiled bytecode, and can quantify over every function of a contract.
Strengths: when a property is proven, it holds for every input and every state the model covers. When it is not, you get a concrete counterexample.
Limits: a proof is only as good as its specification and its assumptions. Loops are usually checked only up to a fixed bound. External contracts have to be modelled. Large or math-heavy code can time out. Formal verification proves what you wrote down, not what you meant.
Side by side
| Aspect | Manual review | Invariant fuzzing | Formal verification |
|---|---|---|---|
| Question it answers | Is the design sound? | Can I break this property? | Can this property ever break? |
| Coverage | What the auditors reason about | Sampled inputs and call sequences | All inputs, within the stated bounds |
| Output | Findings and recommendations | Minimised counterexamples | Proofs or counterexamples |
| Main limitation | Human attention | Cannot prove that bugs are absent | Only as good as the specification |
| Best for | Logic, economics, integrations | Whole-protocol invariants | Critical math and accounting |
How we combine them
In our audits, the three layers build on each other:
- Manual review first. We learn how value moves through the protocol and write the invariants down with your developers.
- Fuzz the whole system against those invariants: Foundry for fast iteration, Echidna and Medusa for long campaigns.
- Formally verify the core, meaning the code where a failure would be catastrophic: share accounting, math libraries and the access control around upgrades.
- Leave everything in your repository, running in CI, so that the next change is checked too.
None of these replaces the others. All three also stop at the edge of the contract. A perfectly verified vault does not help if a compromised front end asks your users to sign a malicious transaction. That is why we also test the Web 2.5 layer around the contracts.
If you are preparing a launch or an upgrade, tell us about your protocol.