1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//! # 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
//!
//! ```text
//! 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.
//!
//! | # | Mechanism | Entry point | Complete? | Can it lie? |
//! |---|---|---|---|---|
//! | 1 | Symbolic — BDD, then CDCL SAT fallback | [`smt::prove`], [`sym`] | no (scaling wall) | no — it returns `Unknown` |
//! | 2 | Exhaustive — every input of a real domain | [`exhaustive::for_all_u8`], [`exhaustive::for_all_u16`] | yes, small domains | no |
//! | 3 | Type-level — Curry–Howard kernel | [`proof::Theorem`], `assert_proof!` | yes, for composition | no — 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
//!
//! | Module | What it is |
//! |---|---|
//! | [`smt`] | The decision procedure: bit-vector terms, ROBDDs, [`smt::Verdict`], counterexamples, the `no_std` arena. |
//! | [`bitblast`] | The bit-level term graph → CNF lowering, used by the SAT fallback. |
//! | [`sat`] | The CDCL SAT solver used as the fallback past the BDD wall. |
//!
//! ## Specifications and proofs
//!
//! | Module | What it is |
//! |---|---|
//! | [`sym`] | A symbolic DSL: operator overloading so a specification reads like code, `(x + y).eq(y + x).prove()`. |
//! | [`logic`] | Checked and saturating arithmetic plus a Q16.16 [`logic::Fixed`] type, each with proofs attached. |
//! | [`mod@proof`] | The Curry–Howard kernel: [`proof::Theorem`] propositions whose proof terms `rustc` checks at compile time (so `Truth => Falsity` does not build), plus runtime [`proof::Certificate`]s for an engine-produced proof. |
//! | [`mod@contract`] | Runtime `requires`/`ensures`/`assert` over concrete values, with structured errors. |
//! | [`vc`] | [`vc::requires`]/[`vc::ensures`]/[`vc::invariant`], [`vc::verify_body`], and the [`vc::Verified`] witness. |
//! | [`wp`] | Weakest-precondition generation for statements, loops and calls, with a per-rule [`wp::Report`]. |
//! | [`mem`] | A bounded symbolic array with a proved [`mem::Mem::read`] / [`mem::Mem::store_wp`] frame condition. |
//! | [`quant`] | Bounded quantifiers, expanded into finite conjunctions and disjunctions. |
//!
//! ## Complete by enumeration
//!
//! | Module | What it is |
//! |---|---|
//! | [`exhaustive`] | Complete proofs by enumeration over `u8`/`u16` domains: no size wall, no `Unknown`. |
//!
//! ## The proved arithmetic, and its symbolic oracle
//!
//! | Module | What it is |
//! |---|---|
//! | [`arith`] | The 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. |
//! | [`primitives`] | The 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
//!
//! | Module | What it is |
//! |---|---|
//! | [`smtscript`] | SMT-LIB2 export, so an independent solver can decide the same condition. |
//! | [`macros`] | `verify!`, `verify_implies!`, `contract!`, `proof!`, `assert_proof!`, `check!`, `verify_exhaustive!`. |
//!
//! ## Verifying real Rust (`std` only)
//!
//! | Module | What it is |
//! |---|---|
//! | `mir` | Verify **real Rust**: read `rustc`'s own MIR, symbolically execute it, and discharge rustc's overflow, underflow and division-by-zero asserts plus `/// requires:` / `/// ensures:` doc-comment contracts. |
//!
//! 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
//!
//! | Feature | Default | Effect |
//! |---|---|---|
//! | *(none)* | yes | `no_std`, zero dependencies, `core` only. The engine, the DSL, the kernel, the contracts, `wp`, `mem`, `quant`, `exhaustive`. |
//! | `std` | no | Adds 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.
// The proved runtime primitives live in a sibling crate so that the arithmetic a
// safety build ships is `no_std`, `#![forbid(unsafe_code)]` and dependency-free
// on its own, with its proofs carried inside it. Re-exported as `arith` so the
// module reads `verif::arith` rather than the `verif::verif_arithmetic` stutter,
// and so the caller never has to name the workspace layout.
pub use verif_arithmetic as arith;
/// The names you almost always want: the prover entry points, the symbolic
/// constructors, and the verification macros.