The leanrust-core-1 Fragment
LeanRust intentionally targets a small, decidable, total subset of Rust — big enough to express security-critical state machines, parsers, authorization policies, and bounded loops, while keeping the Lean 4 model readable in one sitting.
Supported Types & Bounds
| Rust Type | Lean 4 Predicate | Mathematical Bound |
|---|---|---|
u8 | uint8 n | 0 ≤ n < 2^8 |
u16 | uint16 n | 0 ≤ n < 2^16 |
u32 | uint32 n | 0 ≤ n < 2^32 |
u64 | uint64 n | 0 ≤ n < 2^64 |
i32 | int32 n | -2^31 ≤ n < 2^31 |
i64 | int64 n | -2^63 ≤ n < 2^63 |
bool | bit n | n = 0 ∨ n = 1 |
Flat struct | #[derive(Labels)] | Per-field store cells (param.field) with per-field labels |
Attributes & Spec Grammar
| Attribute / Macro | Meaning |
|---|---|
#[unhackable] | Marks a function for Lean 4 certification. Requires a security boundary plus either #[ensures] or a valid noninterference proof. |
#[noninterference] | Requires a kernel-checked TypedCmd + ni_soundness proof for both confidentiality and integrity. |
#[ensures("...")] | Postcondition proposition over old(path), result / result.field, arithmetic + - *, comparisons, &&, ||, !, and ->. |
#[input(name, trusted | secret)] | Overrides default (public, tainted) label on a function parameter. |
#[label(secret, trusted)] | Sets per-field security label inside a #[derive(Labels)] struct. |
#[trusted] / #[secret] | Sets the return value's label (default: public and tainted). |
invariant!("...") | Loop induction hypothesis at the start of a for i in 0..bound body. Bare parameter = current cell; old(x) = pre-loop value; i = trip counter. |
Allowed Expressions & Control Flow
- Assignments:
x = expr,x += expr,x -= expr,x *= expron parameters and flat struct fields. - Conditionals:
if/elsewith simple or compound arithmetic guards (e.g.a + b > cap) and earlyreturn. - Counted Loops:
for i in 0..bound(must start at 0, non-mutindex, no earlyreturninside the loop). - References: A single
&or&mutparameter is lowered as a value (no aliasing references).
CLI Commands
# Fast fragment & noninterference check (does not invoke Lean)
cargo run -p leanrust-cli -- check examples/vault/src/lib.rs
# Emit generated Lean 4 model without running the kernel
cargo run -p leanrust-cli -- extract examples/vault/src/lib.rs -o Prove.lean
# Full certification: emit Prove.lean, run `lake build` + `lake env lean`, mint CERTIFICATE.txt
cargo run -p leanrust-cli -- prove examples/vault/src/lib.rs