Proving & Trust Reports

Clever's verification philosophy is: a human reads the specification; the machine checks the rest — and every build reports exactly what was proven versus assumed.

Laws Next to Code (law)

A law is a universally quantified boolean claim written in the same file as the functions it specifies. Preconditions use short-circuiting || (!pre || claim), so neither side is required to avoid traps outside the precondition.

Clever has two proof engines:

  1. Clever's own kernel (Clever/Kernel/):
    • proof by bv: bit-blasts word/boolean claims, invokes a SAT solver, and verifies the LRAT certificate inside Clever's kernel. Fast for bitwise identities, carry/borrow arithmetic, masking, and 64-bit reductions.
    • Clever proof scripts (simp, decide, cases, induction, rewrite, use, exact): checked by Clever's data kernel over enums, recursive functions, &mut state passing, loops, arrays, and Bytes.
  2. Lean 4 escape hatch (proof by lean:):
    • Embeds verbatim Lean 4 tactic text checked against the generated Lean model (clever lean). Ideal for wide nonlinear arithmetic (e.g., 128-bit reductions mod p), omega, or multi-pass loop simulations.

Example: Bit-Vector Kernel (proof by bv)

Proven
fun ch(x: U32, y: U32, z: U32): U32 = z ^ (x & (y ^ z))
fun specCh(x: U32, y: U32, z: U32): U32 = (x & y) ^ (~x & z)

fun maj(x: U32, y: U32, z: U32): U32 = (x & y) | (z & (x | y))
fun specMaj(x: U32, y: U32, z: U32): U32 = (x & y) ^ (x & z) ^ (y & z)

law ch_equiv(x: U32, y: U32, z: U32): ch(x, y, z) == specCh(x, y, z)
  proof by bv

law maj_equiv(x: U32, y: U32, z: U32): maj(x, y, z) == specMaj(x, y, z)
  proof by bv

Example: Data Kernel (Structural Induction & decide)

Proven
enum List8 {
  Nil,
  Cons(val head: U8, val tail: List8)
}

fun len(xs: &List8): U64 = when (xs) {
  List8.Nil -> 0u64
  is List8.Cons -> 1u64 +% len(xs.tail)
}

fun appendList(a: &List8, b: &List8): List8 = when (a) {
  List8.Nil -> b.share()
  is List8.Cons -> List8.Cons(a.head, appendList(a.tail, b))
}

law len_example(): len(List8.Cons(1u8, List8.Cons(2u8, List8.Nil))) == 2u64
  proof by decide

law append_len(a: List8, b: List8): len(appendList(a, b)) == len(a) +% len(b)
  proof by
    induction a
    simp

Example: Lean 4 Proof (proof by lean:)

When a claim goes beyond bit-blasting or built-in kernel tactics, drop into Lean 4 tactics right inside the .clv file. clever prove checks the proof with Lean's kernel and audits its axioms (only propext, Classical.choice, Quot.sound are permitted — no sorryAx or native bv_decide axioms):

Proven
fun double(x: U64): U64 = x +% x

law double_is_mul2(x: U64): double(x) == x *% 2u64
  proof by lean:
    intro x
    unfold double
    have hx := x.toNat_lt
    simp only [beq_iff_eq, UInt64.eq_iff_toBitVec_eq, ← BitVec.toNat_inj,
      UInt64.toNat_toBitVec, UInt64.toNat_add, UInt64.toNat_mul, UInt64.reduceToNat]
    omega

(Inspect the generated Lean goal for any file with clever lean F.clv -o Model.lean.)

Contracts (requires / ensures), Loop invariants & @noabort

Proofs in Clever are not just documentation — proofs pay rent in speed.

Proven
const LIMIT: U64 = 1_000_000u64

fun boundedScale(x: U64): U64
  requires x < LIMIT
  ensures result < LIMIT * 10u64
    proof by bv
{
  x * 10u64
}

@noabort
export fun safeScale(x: U64): U64 {
  if (x < LIMIT) { boundedScale(x) } else { LIMIT * 10u64 }
}

export fun searchBound(b: &Buf<U64>, x: U64): U64 {
  var base = 0u64
  var size = b.len
  while (size > 1u64)
    invariant (base <= b.len && size <= b.len - base)
    decreases (size)
  {
    val half = size / 2u64
    val mid = base +% half
    if (b[mid] < x) { base = mid }
    size = size - half
  }
  if (size == 1u64 && b[base] < x) { base + 1u64 } else { base }
}
safeScale()

Use clever checks F.clv to see every trap site that survived optimization and why it was kept:

clever checks F.clv --target c
clever checks F.clv --input         # only trap sites reachable from untrusted export inputs

Testing & Auditing Law Quality

A proof of a vacuous law (false || ...) proves nothing. Clever ships built-in law-quality tools:

# Property-test every law (especially laws without a `proof by` yet)
clever test F.clv --n 1000

# Plain-English explanation of what each law states
clever laws F.clv --explain

# Flag laws whose precondition is rarely or never satisfied
clever laws F.clv --vacuity

# Search for counterexamples to broken laws
clever laws F.clv --cex

# Mutation-test specifications: plant bugs in code and verify laws catch them
clever spec-mutate F.clv

# Differential equivalence check between two functions (e.g. fast vs spec)
clever equiv F.clv fastFn specFn

Cross-check Clever kernel laws against Lean's own kernel as a second opinion:

clever prove F.clv --via lean

Trust Reports (clever trust) & Semantic Lockfiles (clever.lock)

Per-Build Trust Reports

clever prove F.clv -o F.proofs.json
clever trust F.clv --target c --use-proofs F.proofs.json
clever c F.clv -o out.c --use-proofs F.proofs.json --trust-report out.trust.json

Every report itemizes:

Dependency Trust-Surface Pinning (clever.lock)

In a multi-package build (clever.toml), clever lock records the reachable trust surface of every dependency: definition code/spec Merkle hashes, signatures, contracts, laws and their proven/tested status, externs, capabilities, intrinsics, unsafe uses, and toolchain hashes.

clever fetch                # fetches dependencies into content-addressed store (only network step)
clever lock                 # prints diff against clever.lock
clever lock --accept        # writes/updates clever.lock after review
clever build                # FAILS if any dependency's trust surface differs from clever.lock
clever update dep --to ../dep-v2 --review --accept   # diffs surface & re-proves dep's laws locally