How to add formal verification to your project: properties, tools, specs
How to add formal verification to a Rust, Python or TypeScript codebase now that coding agents write proofs: write down the properties the code must keep, pick the strongest check the tools support for each, keep the specs honest and run it all in CI. The tools per language, and which of our articles has the details.
Formal verification checks that code keeps a written property for every input, and a checker confirms each step, so you don’t have to trust whoever (or whatever) wrote the code. Coding agents now write much of the proof work, which makes verification practical in ordinary codebases, not just in cryptography labs. This guide covers how to add it: find the properties, choose a check for each, pick the tools, keep the specs honest and run it in CI. The evidence and worked examples are in our verification articles. For the basics, see what formal proofs are.
1. Write down the properties
Start from what the code must always do, not from the tools. Every codebase has properties like these:
- It never crashes, hangs or throws on any input it accepts, especially input it doesn’t control (files, specs, requests, user data).
- Outputs stay in bounds: no negative sizes, indexes inside the array, values within their declared range.
- Things are conserved: parts sum to the total, nothing is lost or duplicated, and data survives a round trip (
parse(print(x)) == x). - Order and structure hold: a sort is sorted, a scale is monotonic, a transform is idempotent, a tree stays balanced.
- It matches a simpler reference: the fast version returns what the obvious version would.
- Whatever isn’t allowed never happens: an unauthorised read, a state the design forbids, an interleaving that corrupts data.
An agent can draft these from the code. Each one should be a sentence a person can read and agree with, because the property is what you’ll end up trusting.
2. Pick the strongest check the tools support
For each property, use the strongest check that reaches the code:
| Check | What it gives you | Use it for |
|---|---|---|
| Property-based tests | Searches for counterexamples across many generated inputs; no guarantee | Every property, as the baseline; whatever stronger tools can’t reach |
| Bounded model checking | Every input up to a size you choose | Crash-freedom, overflow and bounds in Rust or C; parsers |
| Proofs of the code in place | The property for every input, on the code you ship | Rust with Verus; annotated TypeScript or Python where the tools cover the code |
| A verified core | A proof in a verification language, compiled into your app | Small, stable logic in a language with weak verifiers (Python, TypeScript, Go, Java) |
| Design models | Every interleaving of a model of the design | Distributed protocols and concurrent algorithms |
The tools’ reach is the real limit. Floating-point maths, for example, is still hard to prove in most of them, so use property tests against a reference implementation there and prove the integer and structural parts. The strategies guide takes one function through every check. Validating AI-written code covers the testing side, including mutation testing to show the property tests actually catch bugs.
3. The tools, by language
| Language | Tests and bounded checks | Proofs | Guide |
|---|---|---|---|
| Rust | proptest, Kani | Verus; Aeneas or hax for deep proofs in Lean or F* | Rust |
| Python | Hypothesis, CrossHair | Nagini (research), or a Dafny core compiled to Python | Python |
| TypeScript | fast-check | LemmaScript (preview) for annotated TypeScript, or a Dafny core compiled to JavaScript | TypeScript |
| Go, Java, C# | Your language’s property-testing library | A Dafny core | Proof languages |
| C | CBMC | Verified libraries such as HACL* | Proof languages |
| Designs | — | TLA+, P | Strategies |
For compilers, protocol theory or mathematics, the deep-proof languages are Lean, Rocq and Isabelle. Before proving something yourself, look for a verified component that already does the job, such as HACL* for cryptography or Cedar for authorization. A proof stops at the boundary of the code it covers, so check inputs where they enter: our proved Dafny code, compiled to JavaScript, still accepted bad input.
4. Keep the specs honest
Agents are good at proofs and weak at specs. In one test they wrote correct specs 48% of the time, against 92% for the code itself. Under pressure they also weaken a spec until the proof passes: in one pipeline, 99.1% of programs verified but only 1.9% proved anything tied to a spec (strategies). So:
- A person reviews the spec. Keep a plain-words version beside the formal one, plus a few wrong implementations it must reject.
- Freeze specs once approved. Keep them in their own files, and make CI fail when an agent’s change touches them.
- Check proofs mechanically. Fail the build on escape hatches such as
sorry,assume,admitand new axioms.
This is a specific case of keeping agents from claiming “done” when it isn’t.
5. Run it in CI and keep it current
Property tests and bounded checks run on every pull request. Proofs and long model-checking runs can go on a schedule, like slow test suites. Proofs need upkeep: they can break after small valid changes, and published projects carry 2 to 27 lines of proof per line of code. Agents take on most of that repair work now, and the specs stay with you (proof languages compared).
FAQ
How do I add formal verification to a TypeScript or Python project?
Write down the properties the code must keep, test them all with fast-check or Hypothesis, then go further where the tools reach. For TypeScript that means LemmaScript on annotated code or a Dafny core compiled to JavaScript; for Python, Nagini or a Dafny core compiled to Python. Keep the specs reviewed and frozen.
Can AI agents write formal proofs?
Yes, increasingly well when a verifier is in the loop and the spec is given. They’re much weaker at writing specs, so a person should review each spec and CI should stop it changing.
Is property-based testing formal verification?
No. It searches for counterexamples, and passing runs don’t rule bugs out. A proof covers every input the spec describes. Property tests are still the baseline for every property, and the check of last resort where proof tools can’t reach.
