verif_arithmetic/lib.rs
1//! # verif-arithmetic — proved integer primitives for safety-critical Rust
2//!
3//! The "library half" of the SPARK story: the arithmetic a flight-control or
4//! motor-control loop reaches for, with the property that makes each one safe
5//! **proved inside the crate**. No external solver, no `smt` dependency, no
6//! `unsafe`, no allocator — `core` and nothing else.
7//!
8//! In C, `a + b` on `unsigned` wraps silently and on `signed` is undefined
9//! behaviour; in Ada with `-gnato` it traps, and SPARK can prove the trap never
10//! fires. Rust's `+` panics in debug and wraps in release, which for flight
11//! software is arguably the worst of both. These primitives take the
12//! total-function route — they return an `Option` — and carry a proof that the
13//! `None` case is exactly the overflow case, so a caller either handles it or
14//! discharges it away.
15//!
16//! ```
17//! use verif_arithmetic::{checked_add, prove};
18//!
19//! // The runtime behaviour...
20//! assert_eq!(checked_add(200, 55), Some(255));
21//! assert_eq!(checked_add(u32::MAX, 1), None);
22//!
23//! // ...and the proof that its overflow flag is exact.
24//! assert!(prove::checked_add_is_total().is_proved());
25//! ```
26//!
27//! # What is in this crate
28//!
29//! | Module | What it gives you |
30//! |---|---|
31//! | [`checked`] | Total arithmetic, split by what a caller does with the overflow. [`checked_add`], [`checked_sub`], [`checked_mul`] and [`mul_div`] return `Option`, where `None` is exactly the case the machine op would have wrapped or panicked. [`saturating_add`] and [`saturating_sub`] return a plain `u32` and **clamp** at the bound instead of reporting it, so there is no `None` to check. |
32//! | [`bounded`] | The `safe_*` functions (the same total route) and [`Bounded<MIN, MAX>`](bounded::Bounded), a refinement-typed integer whose constructors are the only way in. |
33//! | [`fixed`] | [`Fixed`], a Q16.16 fixed-point number: deterministic, no floating point, no soft-float dependency. |
34//! | [`prove`] | The proofs, discharged by exhaustive enumeration. This is the module that makes the rest trustworthy. |
35//!
36//! The crate root re-exports the common names, so `use verif_arithmetic::{checked_add, Bounded, Fixed, Verdict}`
37//! is usually enough without naming a module.
38//!
39//! # The proof is a decision procedure, not a citation
40//!
41//! [`prove`] holds the proofs. Each is a **decision procedure by exhaustive
42//! enumeration**: it walks every input in the domain and returns
43//! [`Verdict::Valid`] only if the property held for all of them, or
44//! [`Verdict::Counterexample`] with a witness if one failed. That is a complete
45//! proof — no size wall, no `Unknown` for a domain it can reach — and it needs
46//! nothing but `core`. It is the same technique used to validate flight-control
47//! tables, and the same one the parent crate's `exhaustive` module uses.
48//!
49//! This is deliberately *not* a symbolic engine. Exhaustive enumeration cannot
50//! reach a 32-bit two-variable domain, and it does not pretend to: a proof whose
51//! domain exceeds [`prove::ENUM_BUDGET`] returns [`Verdict::Unknown`] rather
52//! than a guess. The proofs here are stated at the widths the enumeration can
53//! *finish*, which is the honest boundary of a method with no solver behind it.
54//!
55//! # Feature flags
56//!
57//! There are none. This crate is `no_std`, `#![forbid(unsafe_code)]` and
58//! dependency-free by construction, and there is no configuration in which that
59//! changes. A downstream build links `core` and nothing else.
60//!
61//! # The symbolic oracle lives in the parent crate
62//!
63//! [`verif`](https://docs.rs/verif) — the parent crate —
64//! depends on this one, keeps its own bit-blasting `smt` engine, and states the
65//! **same** obligations symbolically at widths 8, 16 and 32. `tests/oracle.rs`
66//! in the parent then cross-checks the two: the enumeration and the symbolic
67//! engine must agree on every obligation they share, and where they cannot (the
68//! 32-bit two-variable domains) the symbolic engine is the one that answers.
69//!
70//! The split is the same shape as the parent's MIR frontend, which parses MIR
71//! itself and is cross-checked against `rustc_public` in CI: **each crate owns a
72//! self-contained mechanism, and the heavy external one is only ever an oracle.**
73//! A dependency that can lie to you is not a proof; a second opinion is.
74//!
75//! # When to reach for the parent crate instead
76//!
77//! This crate proves things by *enumerating* a domain, so it is the right tool
78//! for a primitive whose domain is small and whose proof you want carried in the
79//! artifact with no solver. When the obligation is symbolic — a bound over all
80//! `u32` inputs, a loop invariant, a contract on a whole `fn` — use
81//! [`verif`](https://docs.rs/verif), which has the BDD engine, `wp`, `mem` and
82//! the MIR frontend. The two are designed to be used together, and the parent
83//! re-exports this crate as `verif::arith`.
84
85#![no_std]
86#![forbid(unsafe_code)]
87#![deny(missing_docs)]
88
89pub mod bounded;
90pub mod checked;
91pub mod fixed;
92pub mod prove;
93
94pub use bounded::{Bounded, safe_add, safe_div, safe_mul, safe_rem, safe_sub};
95pub use checked::{
96 checked_add, checked_mul, checked_sub, mul_div, saturating_add, saturating_sub,
97};
98pub use fixed::Fixed;
99pub use prove::Verdict;