xiaohongshu
xiaohongshu
creusot
Formal verification of Rust code using Creusot and the Pearlite specification language. Use when the user wants to (1) add contracts/specifications to Rust functions, (2) prove correctness of Rust code, (3) write loop invariants, (4) use Creusot's Why3-based verification, (5) work with Pearlite s...
Full skill instructions
Creusot is a deductive verification tool for safe Rust. It translates Rust + Pearlite specifications to Why3/Coma and uses SMT solvers to prove correctness.
use creusot_contracts::prelude::*; // Current (creusot_std in newer versions)
| Attribute | Purpose |
|---|---|
#[requires(P)] | Precondition - must hold when function called |
#[ensures(P)] | Postcondition - guaranteed when function returns |
#[invariant(P)] | Loop invariant - true at every iteration |
#[variant(E)] | Termination measure - must decrease each iteration |
#[logic] | Pure logical function (not callable from Rust) |
#[predicate] | Logical function returning bool |
#[trusted] | Skip verification (assume contract holds) |
| Operator | Meaning |
|---|---|
x@ | View/model operator - converts Rust value to logical type (e.g., i64 → Int) |
^x | Final value of mutable borrow (prophecy) |
*x | Current value of borrow |
==> | Logical implication (in pearlite!{}) |
forall<x: T> | Universal quantifier (in pearlite!{}) |
exists<x: T> | Existential quantifier (in pearlite!{}) |
Int - Unbounded mathematical integers (no overflow)Seq<T> - Mathematical sequencesSet<T>, FSet<T> - Sets (infinite/finite)Map<K, V> - Mathematical functionsGhost<T> - Ghost values (exist only in proofs)Snapshot<T> - Immutable snapshot of a valuecargo creusot new project-name
cd project-name
cargo creusot # Compile to Coma only
cargo creusot prove # Compile and prove
cargo creusot prove -i # Open Why3 IDE on failure
cargo creusot prove --ide-always # Always open IDE
#[requires(x@ < i64::MAX@)] // Precondition
#[ensures(result@ == x@ + 1)] // Postcondition
pub fn add_one(x: i64) -> i64 {
x + 1
}
Loops MUST have invariants to be verified. The invariant must:
#[requires(n@ * (n@ + 1) / 2 <= u64::MAX@)]
#[ensures(result@ == n@ * (n@ + 1) / 2)]
pub fn sum_up_to(n: u64) -> u64 {
let mut sum = 0;
let mut i = 0;
#[invariant(i@ <= n@)]
#[invariant(sum@ == i@ * (i@ + 1) / 2)]
while i < n {
i += 1;
sum += i;
}
sum
}
For loops use produced variable (sequence of yielded elements):
#[invariant(sum@ * 2 == produced.len() * (produced.len() + 1))]
for i in 1..=n {
sum += i;
}
#[predicate]
fn sorted<T: Ord>(s: Seq<T>) -> bool {
pearlite! {
forall<i: Int, j: Int> 0 <= i && i < j && j < s.len()
==> s[i] <= s[j]
}
}
#[logic]
fn sum_seq(s: Seq<Int>) -> Int {
if s.len() == 0 { 0 }
else { s[0] + sum_seq(s.tail()) }
}
Use ^ (final) to specify the value at end of borrow lifetime:
#[ensures(^x == *x + 1)] // Final value equals current + 1
pub fn increment(x: &mut i32) {
*x += 1;
}
Ghost code exists only during verification:
let old_v = ghost!(v); // Snapshot for invariant
#[invariant([email protected]_of(old_v@))]
pub struct OPair(pub u64, pub u64);
impl Invariant for OPair {
#[logic]
fn invariant(self) -> bool {
pearlite! { self.0 <= self.1 }
}
}
Always specify bounds to prevent overflow verification failures:
#[requires(x@ + y@ <= i64::MAX@)]
#[requires(x@ + y@ >= i64::MIN@)]
#[requires(i@ < [email protected]())] // Bounds check
#[ensures(result@ == v@[i@])] // Element access
#[ensures((^v)@.len() == [email protected]())] // Length preserved
#[logic]
#[variant(x)] // x must decrease (implement WellFounded)
#[requires(x >= 0)]
fn factorial(x: Int) -> Int {
if x == 0 { 1 } else { x * factorial(x - 1) }
}
cargo creusot prove -i verif/[FILE].comaverif/[crate]_rlib/[module]/[function].comawhy3find.json, Cargo.tomlFor detailed Pearlite syntax, common specification patterns, and advanced features, see references/pearlite-syntax.md.
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience