Skip to main content

Crate verif

Crate verif 

Source
Expand description

§verif — in-code formal verification for Rust

SPARK/Ada earns its reputation in safety-critical work because the proof tooling is part of the language and part of the artifact: gnatprove sits next to gnatmake, and the contracts (Pre, Post, Type_Invariant, Assert) are first-class syntax the compiler knows about. Rust ships with a spectacular type system and no such prover.

This crate is an experiment in closing that gap from inside Rust itself. It embeds a bit-blasting decision procedure — formulas are lowered to Reduced Ordered Binary Decision Diagrams, which are a canonical form, so equivalence is node identity — and builds the modules below on top of it.

use verif::prelude::*;

// ∀ x:u8. (x & 0x0f) < 16  — decided by the embedded engine
let proved = verif::smt::with_fresh(|| {
    let x = var(8);
    (x & 0x0fu32).lt_val(16).prove().is_proved()
});
assert!(proved);

§The three ways, in code

The same obligation, discharged three ways. The first two return a verdict you can inspect at run time; the third is settled before the program runs, so it has no verdict to return. None of them ever reports Valid for something false.

use verif::prelude::*;

// 1. Symbolic: for every input, via BDDs. Fast, but can say Unknown.
let symbolic = verif::smt::with_fresh(|| {
    let x = var(8);
    smt::prove_implies(x.le_val(200), x.le_val(255)).is_proved()
});
assert!(symbolic);

// 2. Exhaustive: every input of a small domain, no size wall, never Unknown.
assert!(for_all_u8(|a| a <= 255));

// 3. Type-level: the kernel builds a proof term and `rustc` checks it, so a
//    falsehood fails to compile rather than returning a verdict.
use verif::proof::{And, Equals, Imp, Truth};
assert_proof!(And<Truth, Imp<Truth, Truth>>);
assert_proof!(Equals<u8, u8>);

A contract you can state in the source and check at run time, with the same predicate usable symbolically:

use verif::prelude::*;

// Pre/post as a symbolic obligation: `requires x <= 200` and
// `ensures x <= 255`; the engine proves the implication.
let v = verif::smt::with_fresh(|| {
    let x = var(8);
    let contract = ensures(requires(x.le_val(200)), x.le_val(255));
    contract.discharge()
});
assert!(v.is_proved());

§What you get, in one line each

no_std by default, zero external dependencies, a fixed-size heap-free arena, and #![forbid(unsafe_op_in_unsafe_fn)]. The engine links core and nothing else, which is what a bare-metal safety build wants; the host-side tools (the MIR frontend, the file-backed helpers) are behind the std feature.

§The three ways this crate proves something

1. symbolic      smt::prove(p)              -- for all inputs, via BDDs
2. exhaustive    exhaustive::for_all_u16(p) -- every input, no size wall
3. type-level    proof::Theorem             -- the compiler checks the proof term

They are deliberately different tools, and each has a different honesty profile. The symbolic engine is the fast, general one but can return smt::Verdict::Unknown at the BDD size wall. Enumeration is a complete proof that never says “unknown”, but only for small domains. The kernel makes composition of proofs machine-checked by rustc. A serious safety argument uses all three, and says which one it used.

#MechanismEntry pointComplete?Can it lie?
1Symbolic — BDD, then CDCL SAT fallbacksmt::prove, symno (scaling wall)no — it returns Unknown
2Exhaustive — every input of a real domainexhaustive::for_all_u8, exhaustive::for_all_u16yes, small domainsno
3Type-level — Curry–Howard kernelproof::Theorem, assert_proof!yes, for compositionno — it will not compile

The third row is the one to read carefully, because two things in this crate are easy to confuse. assert_proof! is a compile-time obligation: it asks for a proof::Theorem impl and rustc refuses if the proof term cannot be built, which is why a falsehood is a build error and not a bad verdict. proof::Certificate::discharge is the runtime solver route: it takes a predicate to the smt engine and returns Option, so it can answer None at run time. Both are useful; only the first is checked before the program runs.

§Module map

§The engine

ModuleWhat it is
smtThe decision procedure: bit-vector terms, ROBDDs, smt::Verdict, counterexamples, the no_std arena.
bitblastThe bit-level term graph → CNF lowering, used by the SAT fallback.
satThe CDCL SAT solver used as the fallback past the BDD wall.

§Specifications and proofs

ModuleWhat it is
symA symbolic DSL: operator overloading so a specification reads like code, (x + y).eq(y + x).prove().
logicChecked and saturating arithmetic plus a Q16.16 logic::Fixed type, each with proofs attached.
proofThe Curry–Howard kernel: proof::Theorem propositions whose proof terms rustc checks at compile time (so Truth => Falsity does not build), plus runtime proof::Certificates for an engine-produced proof.
contractRuntime requires/ensures/assert over concrete values, with structured errors.
vcvc::requires/vc::ensures/vc::invariant, vc::verify_body, and the vc::Verified witness.
wpWeakest-precondition generation for statements, loops and calls, with a per-rule wp::Report.
memA bounded symbolic array with a proved mem::Mem::read / mem::Mem::store_wp frame condition.
quantBounded quantifiers, expanded into finite conjunctions and disjunctions.

§Complete by enumeration

ModuleWhat it is
exhaustiveComplete proofs by enumeration over u8/u16 domains: no size wall, no Unknown.

§The proved arithmetic, and its symbolic oracle

ModuleWhat it is
arithThe sibling crate verif-arithmetic, re-exported: total integer operations and a refinement-typed arith::Bounded, each with an enumeration proof carried in the artifact. no_std, #![forbid(unsafe_code)] and dependency-free on its own.
primitivesThe symbolic oracle for those same operations: the obligations restated at widths enumeration cannot reach, cross-checked against it by tests/oracle.rs.

§Interop and ergonomics

ModuleWhat it is
smtscriptSMT-LIB2 export, so an independent solver can decide the same condition.
macrosspec! (in-code contracts), verify!, verify_implies!, contract!, proof!, assert_proof!, check!, verify_exhaustive!.

§Verifying real Rust (std only)

ModuleWhat it is
mirVerify real Rust: read rustc’s own MIR, symbolically execute it, and discharge rustc’s overflow, underflow and division-by-zero asserts plus contracts written as an in-code spec! { … } block.

The mir frontend is the only module that needs a compiler at run time, so it is behind the std feature. Its page appears in this documentation only when that feature is enabled (the published docs on docs.rs are built with --all-features, so it is there).

§Feature flags

FeatureDefaultEffect
(none)yesno_std, zero dependencies, core only. The engine, the DSL, the kernel, the contracts, wp, mem, quant, exhaustive.
stdnoAdds the mir frontend, the thread-local arena used by the host-side test suite, and the file-backed helpers.

There is no alloc feature and no allocator anywhere: every structure is fixed-size, and the arena has a documented capacity rather than growing.

§What this is not

It is not a replacement for SPARK’s proof tooling, and it does not make arbitrary Rust correct. It reasons about bit-vectors and boolean predicates, plus one bounded array (mem, addressed by an integer). It does not model pointers, address arithmetic across objects, aliasing or concurrency: you cannot state “this pointer is valid” or “these two threads do not race”. The BDD engine is not a CDCL SAT solver: equalities between two wide multipliers can hit the size wall, in which case the honest answer is smt::Verdict::Unknown.

Read each module’s page — and the crate README, which records the claim each feature makes and the check that backs it — before trusting a green test.

§Where to read more

  • The README is the long-form document: one section per claim, with the gate that holds it.
  • CONTRIBUTING.md covers the ground rules and the gate script.
  • SECURITY.md covers reporting a soundness bug — a false Valid — which is the vulnerability class that matters in a verifier.
  • AGENTS.md is the engineering log: why each decision was made, including the ones that were reversed.

Re-exports§

pub use verif_arithmetic as arith;

Modules§

bitblast
Bit-slicing a term into gates for the SAT backend.
contract
Runtime contract checking with the same predicate vocabulary as the prover.
exhaustive
Complete verification by exhaustive enumeration.
logic
The symbolic oracle for the proved arithmetic primitives.
macros
Declarative macros that make verification read like syntax, the way Pre/Post/Assert read in SPARK.
mem
A bounded symbolic memory, and the frame condition that comes with it.
mir
A MIR frontend: verify the Rust you actually wrote.
prelude
The names you almost always want: the prover entry points, the symbolic constructors, and the verification macros.
primitives
The symbolic oracle for the proved safe_* operations and Bounded.
proof
Machine-checked proofs, and proof composition.
quant
Bounded quantification, expanded rather than decided.
sat
A CDCL SAT solver, so the crate has a decision procedure that scales.
smt
An SMT decision procedure that lives inside the compiled artifact.
smtscript
SMT-LIB2 export: hand a verification condition to an independent solver.
sym
A symbolic DSL for writing specifications the way you write expressions.
vc
Verification conditions: requires / ensures / invariant, and the Verified<T> wrapper that only exists once a condition is discharged.
wp
Hoare logic: weakest preconditions, and automatic verification-condition generation from a small imperative language.

Macros§

assert_proof
A compile-time proof obligation: assert that a type-level proposition is provable. If the kernel cannot build the proof term, this does not compile.
check
Runtime check on a concrete value, mirroring verify!. Use this in the deployed build for obligations that are checked, not proved.
contract
State a contract with SPARK-like syntax and discharge it.
proof
Produce a Certificate for a predicate, or panic if the prover could not establish it. The certificate is a value you can pass around and compose, and its existence is what lets you build a Verified.
spec
State a contract inside the code, as a block the build-time verifier reads from the source text.
verify
Discharge a predicate with the embedded decision procedure, panicking with the full verdict (including any counterexample) if it is not proved.
verify_exhaustive
Complete proof by enumeration over a small domain, panicking with the smallest counterexample if the property does not hold.
verify_implies
Prove pre => post and panic with the verdict if it does not hold.