Language Specification

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 TypeLean 4 PredicateMathematical Bound
u8uint8 n0 ≤ n < 2^8
u16uint16 n0 ≤ n < 2^16
u32uint32 n0 ≤ n < 2^32
u64uint64 n0 ≤ n < 2^64
i32int32 n-2^31 ≤ n < 2^31
i64int64 n-2^63 ≤ n < 2^63
boolbit nn = 0 ∨ n = 1
Flat struct#[derive(Labels)]Per-field store cells (param.field) with per-field labels

Attributes & Spec Grammar

Attribute / MacroMeaning
#[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 *= expr on parameters and flat struct fields.
  • Conditionals: if / else with simple or compound arithmetic guards (e.g. a + b > cap) and early return.
  • Counted Loops: for i in 0..bound (must start at 0, non-mut index, no early return inside the loop).
  • References: A single & or &mut parameter is lowered as a value (no aliasing references).

CLI Commands

leanrust-cliCheck · Extract · Prove
# 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