Formal Verification
Proving a contract's behaviour mathematically against a spec, rather than testing examples and hoping.
Basically
Formal verification uses maths to prove that software follows its rules for every possible input. Testing checks selected cases, while formal verification checks all cases covered by the specification.
Key Takeaways
- Formal verification uses mathematical proofs to guarantee that software behaves correctly for all possible inputs, not just the ones you test. Testing can show the presence of bugs, but formal verification can prove their absence within the specification.
- In Bitcoin, formal verification powers Miniscript's provable spending policies and has verified the correctness of libsecp256k1, the cryptographic library underlying every digital signature on the network.
- Formal verification complements smart contract audits and testing. Verification proves properties about all states, audits catch business logic flaws, and tests validate behaviour in real environments.
What Is Formal Verification?
Formal verification uses mathematical methods to prove or disprove that a system, such as a program, protocol or circuit, satisfies a formal specification of its intended behaviour. It reasons about the system abstractly instead of running software with sample inputs and checking outputs. This covers every possible execution path and every conceivable input at the same time.
Edsger Dijkstra described the limits of testing in the late 1960s: "Program testing can be used to show the presence of bugs, but never to show their absence." A test suite may check 10,000 inputs, yet the 10,001st might still trigger a bug. Formal verification addresses this limitation by proving that no possible input can violate a specified property.
Smart contracts in blockchains and cryptocurrency are immutable once deployed, handle significant financial value and operate in adversarial environments where any exploitable flaw will eventually be found. A single bug in a smart contract can lead to losses in the hundreds of millions of dollars. Formal verification therefore provides valuable mathematical certainty.
How It Works
Formal verification requires a formal model of the code or protocol and a formal specification of the properties the system should satisfy. A verification engine checks whether the model satisfies the specification under all conditions. Three primary approaches exist, each with different tradeoffs.
Model Checking
Model checking automatically explores every reachable state of a system to determine whether a property holds. It represents the system as a finite-state machine, and the checker exhaustively traverses all possible states and transitions. If a property is violated, the checker produces a counterexample: a concrete sequence of steps that triggers the failure.
Clarke, Emerson, and Sifakis received the 2007 ACM Turing Award for developing model checking. Tools such as TLA+, used by Amazon Web Services since 2011 to verify distributed systems, and SPIN demonstrate its practical impact. The primary limitation is the state explosion problem: as system complexity grows, the number of states increases exponentially, making exhaustive exploration infeasible for large systems.
Theorem Proving
Theorem proving constructs machine-checkable mathematical proofs that a system meets its specification. Unlike model checking, it can handle systems with infinite state spaces. A human expert typically guides a proof assistant such as Coq, Isabelle or Lean, specifying proof strategies while the tool mechanically verifies each logical step.
Notable successes include the seL4 microkernel, the first general-purpose OS kernel with machine-checked functional correctness, proven in 2009, and CompCert, a formally verified C compiler. The tradeoff is effort. Constructing proofs requires specialised expertise and significantly more time than automated approaches.
Symbolic Execution
Symbolic execution runs a program with symbolic variables instead of concrete values, tracking how abstract inputs flow through decision points. The engine splits into separate paths at each branch and builds logical constraints. An SMT solver such as Z3 then determines whether any combination of inputs can violate an assertion.
Symbolic execution bridges the gap between testing and full formal verification. It reasons about classes of inputs rather than individual values, catching bugs that random testing might miss. When an assertion fails, it produces a concrete counterexample showing exactly which inputs trigger the violation.
Verification in Practice
A simplified example illustrates the difference between testing and formal verification. Consider a token transfer function:
The test confirms behaviour for a single transfer of 100 tokens between two specific accounts. The formal verification proves that the invariant holds for every possible transfer, every possible amount and every possible starting state.
Formal Verification in Bitcoin
Bitcoin's approach to formal verification reflects its conservative design philosophy. Bitcoin constrains its scripting language to make correctness provable by construction rather than verifying arbitrary programs after the fact.
Miniscript
Miniscript, developed by Pieter Wuille, Andrew Poelstra and Sanket Kanjalkar at Blockstream Research starting in 2018, is a structured subset of Bitcoin Script where every valid expression has formally provable properties. Its type system classifies expressions into four base types: Base, Verify, Key and Wrapped. Five modifier flags accompany these types, and the compiler verifies safety at compile time.
Miniscript can prove consensus and standardness completeness, meaning valid witnesses can always be constructed, and consensus soundness, meaning invalid witnesses are always rejected, for any spending policy it expresses. It can also prove non-malleability guarantees, exact bytecode output and worst-case witness sizes for every spending branch, making entire classes of Bitcoin Script bugs structurally impossible. Miniscript was integrated into Bitcoin Core in versions 25.0 and 26.0 in 2023 and formalised as BIP 379 in 2024.
libsecp256k1 Verification
Blockstream researchers Russell O'Connor and Andrew Poelstra used the Coq proof assistant to formally verify the safegcd modular inverse algorithm in libsecp256k1. The cryptographic library underpins every ECDSA and Schnorr signature on Bitcoin. Completed in December 2024, this work provides mathematical certainty that the most security-critical arithmetic in Bitcoin's signature verification is functionally correct.
Simplicity
Simplicity, designed by Russell O'Connor at Blockstream, is a programming language built from the ground up for formal verification. It is deliberately Turing-incomplete, with no recursion, no loops and no global state, and its formal denotational semantics are defined in Coq to enable machine-checked proofs of contract correctness. Simplicity was deployed on the Liquid Network sidechain in July 2025, bringing formally verifiable smart contracts to Bitcoin's ecosystem.
Lightning Network Verification
Researchers at the University of Warsaw and Imperial College London published the first machine-checkable proof in 2025 that the Lightning Network always safeguards the funds of honest users. Separate work by Grundmann and Hartenstein used TLA+ to model-check Lightning security. Their work verified that honest parties are guaranteed to recover their correct balances for payments over multiple hops.
Smart Contract Verification Tools
A growing ecosystem of formal verification tools beyond Bitcoin targets smart contract platforms, particularly Ethereum. The research article on Miniscript and Bitcoin spending policies explains how these tools fit into broader security work.
Why It Matters
The DAO hack in June 2016 exploited a reentrancy vulnerability in Ethereum, draining approximately 3.6 million ETH, roughly $50 million at the time, and forcing a controversial hard fork. The Wormhole bridge hack in February 2022 exploited an account validation flaw, stealing 120,000 wETH worth $326 million. The vulnerable code in both cases had been tested and audited, but the specific exploit conditions were never exercised, showing why testing alone is insufficient.
Formal verification could have caught both vulnerabilities. The DAO's reentrancy bug violates the straightforward invariant "external calls must not occur before state updates", while Wormhole's flaw violates "only messages signed by the actual guardian set may authorise mints". Formal methods tools can express and verify both properties automatically.
Bitcoin's UTXO model and Script language are simpler than Ethereum's account model, making them more amenable to formal reasoning and particularly relevant to protocols building on Bitcoin. Projects like Spark that extend Bitcoin's capabilities benefit from this foundation. The underlying cryptographic primitives, including Schnorr signatures and FROST threshold signing, and spending policies, including Miniscript and Tapscript, all have well-defined formal semantics that support verification.
The Specification Problem
Formal verification proves that code matches its specification, but it cannot verify properties that are not specified. An incomplete or incorrect specification, or one that fails to capture a relevant attack vector, can leave a system verified yet still vulnerable. A buggy specification can create a false sense of security that is worse than no verification at all.
State Explosion
Model checking faces exponential growth in the number of states as system complexity increases. Concurrent systems with many interacting components are especially challenging because all possible interleavings must be considered. Techniques like symbolic model checking using Binary Decision Diagrams mitigate this, but the fundamental scaling challenge remains.
Cost and Expertise
Formal specifications require specialised mathematical and logical expertise that most development teams lack. Formal verification for a complex smart contract system can cost substantially and take weeks or months. Teams therefore typically reserve it for the most critical components: core token logic, bridge validation, signature verification and consensus rules.
Scope Boundaries
Formal verification proves properties about an abstract model of the system. External dependencies such as oracles, network conditions and hardware behaviour, deployment configuration and interactions with unverified contracts fall outside the verification boundary. A formally verified contract can still fail if it relies on an unverified oracle that provides incorrect data or if the deployment environment differs from the verified model.
This glossary entry is for informational purposes only and does not constitute financial or investment advice. Always do your own research before using any protocol or technology.