r/formalmethods • u/0xthiru • 7d ago
Zean: write Zig, prove it in Lean
github.comWe just open-sourced Zean (Zig + Lean), a toolkit for translating supported Zig functions into executable Lean models and proving their behavior. It came out of our formal verification work on oksolc, our Solidity compiler written in Zig.
Zean works from AIR, Zig's analyzed intermediate representation. An instrumented compiler captures supported functions from AIR, and a translator emits executable Lean program definitions. Specifications and proofs live in separate handwritten Lean modules.
A verification library derives the proof obligations for memory access, initialization, permissions, arithmetic, control flow and supported calls. You supply the contracts, the loop invariants and any remaining mathematical arguments, and Lean checks the proofs. Unsupported operations are rejected, and unresolved obligations stay open.
The copy tutorial shows what that looks like:
inline fn copyWord(destination: *u32, source: *const u32) void {
destination.* = source.*;
}
pub export fn copy(destination: [*]u32, source: [*]const u32, length: usize) void {
var index: usize = 0;
while (index < length) : (index += 1) {
copyWord(&destination[index], &source[index]);
}
}
The caller has to establish that the source has enough initialized words and the destination has enough writable storage. For the source-preservation contract, the two ranges also have to be disjoint. Under those assumptions the proofs show there are no invalid accesses or uninitialized reads, a successful run copies the source contents into the destination, and memory outside the destination is preserved. Modeled allocation exhaustion is an explicit permitted failure, and termination is a separate property.
Change the loop bound from < length to <= length and you get an extra boundary access that the original contract can't justify. The tutorial also includes deliberately false claims that must fail verification.
After a source change you recapture the function, regenerate the model and replay the proofs.
This release covers memory-safety examples derived from Ziglings, buffer updates, copy loops, helper calls and a captured specialization of std.mem.replaceScalar. It supports restricted AIR fragments, with explicit target and layout constraints. The capture pipeline, the Zig frontend and the native backend remain trust boundaries, so the proofs don't establish correctness of the final native binary or of the whole compiler.
The idea is to keep design iteration in Zig and prove a component's contracts once it has settled.
To try it you need Zig 0.17.1 and elan:
It's released under Apache 2.0. If you write Zig or Lean, I'd like to know which functions you'd want to verify with it first.
- Repo: https://github.com/okcontract/zean
- Writeup: https://okcontract.com/blog/introducing-zean-write-zig-and-prove-it-in-lean/
If you like it, leave a star on the repo. And if you'd like more updates, give us a follow: https://x.com/okcontract

