Skip to content

feat: kernel VM for certificate checking (experimental) - #16493

Draft
nomeata wants to merge 9 commits into
masterfrom
joachim/kernel-vm
Draft

nomeata wants to merge 9 commits into
masterfrom
joachim/kernel-vm

Conversation

@nomeata

@nomeata nomeata commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

This PR adds an experimental small register machine to the kernel, Lean.KernelVM.run, which the kernel evaluates natively, and uses it to check bv_decide certificates (bitblasting, CNF conversion and LRAT checking) inside the kernel. This is a parking spot for the prototype; it is not meant to be merged as is.

The VM. Lean.KernelVM.run (p : Nat) (inputs : List Input) (fuel : Nat) : Bool is an ordinary Lean definition giving the reference semantics. The kernel evaluates it natively (src/kernel/kernel_vm.cpp) when the program and fuel are numerals and the inputs are numeral or string literals; a checker without the native implementation can still unfold it, so there is no new axiom.

  • The program is a flat bytecode stored in a Nat literal: 64-bit words, 65536 registers, a word-addressed memory that grows on demand. Instructions are constants, binary operations with register or immediate operands (wrapping arithmetic, the high word of an unsigned product, bitwise operations, comparisons, and signed arithmetic that fails on overflow), loads and stores, input byte/size reads, jumps and assert. Fuel counts executed instructions.
  • A run only succeeds or fails; tactics compute all inputs, including expected outputs, ahead of time.
  • Inputs are read-only byte arrays: numerals (little-endian) or strings (UTF-8).
  • A small structured language (Exp/Stmt in Init/KernelVM/Asm.lean) compiles to bytecode. The compiler is untrusted: library programs are stored as bytecode literals, and tests/elab/kernelVMPrograms.lean checks that they match their compiled source.
  • Lean.KernelVM.runMem runs the same C++ interpreter at elaboration time and returns the final memory.
  • The kernel also reduces Nat.getByte and Nat.getUInt64 natively, and ByteArray.toNatLE (backed by mpz_import) makes encoding inputs cheap.
  • The interpreter is differentially tested against the compiled reference semantics on random structured and raw bytecode programs (tests/elab/kernelVM.lean).

Programs in this PR.

  • Std.Tactic.BVDecide.LRAT.KernelVM.lratVMProg: a flat-array LRAT checker (RUP and deletion steps).
  • Std.Tactic.BVDecide.KernelVM.bvVMProg: bitblasts a BVLogicalExpr, converts it to CNF and checks an LRAT proof. It builds exactly the AIG of BVLogicalExpr.bitblast and the CNF of Std.Sat.AIG.toCNF; tests/elab/kernelVMBitblast.lean checks this on random expressions, along with evaluation against BVLogicalExpr.eval.
  • Int.Internal.Linear.linCheckProg and the option grind.lia.vm: an experiment that checks the linear steps of lia refutations in one VM run. It was slower than the current kernel-reducible certificates, because those steps are cheap and linking the inputs to the goal dominates. Its soundness theorem is an axiom.

Measurements on the bv_decide benchmarks (medians of 7 runs; SAT solving excluded and identical, since the CNF is identical):

goal LRAT cert (text) today: compiled check kernel VM
bv_decide_mul 27 MB 723 ms 624 ms
bv_decide_realworld #2 25 MB 533 ms 470 ms
bv_decide_inequality #7 15 MB 359 ms 277 ms
bv_decide_mod 157 MB 3926 ms 3261 ms

The difference comes from not parsing the text certificate (about 25% of today's time). On the LRAT check alone, the interpreted flat-array checker and the compiled verified checker are at parity.

Measurements on zeta. zeta verifies zeros of the Riemann zeta function in two layers. Its "Kernel" layer writes explicit interval-arithmetic proof terms, which grow like √t per zero; at height 10⁸ these took 3,545 CPU-ms per zero, against 58 CPU-ms for the "Native" layer, a reflective checker run via native_decide. The proofs are too large to keep, so independent re-checking is impractical. A kernel VM could run a native-style checker inside the kernel instead, with proofs about the size of the input data. To estimate the cost, the dominant part, the Riemann–Siegel main-sum loop, was written as VM bytecode with 128-bit fixed-point arithmetic (zeta uses the grid Q = 2⁶⁶). The run asserts that it reproduces the result of zeta's compiled Lean code (Fx) exactly, on identical inputs:

ν (height t) compiled Lean (zeta's Fx code) kernel VM, with umulh kernel VM, without umulh
40 (10⁴) 42 µs 53 µs 91 µs
400 (10⁶) 418 µs 457 µs 836 µs
4000 (10⁸) 4.45 ms 5.02 ms (1.13×) 8.50 ms (1.9×)

These are best-of-5 for the VM and best-of-20 for compiled Lean. The benchmark covers the composite-n phases and the sums; the prime phases (Taylor series) and per-endpoint constant work are left out on both sides. A full port would plausibly cost 65–110 CPU-ms per zero at 10⁸ in the kernel, compared with 3,545 CPU-ms for zeta's kernel layer today. Without umulh, the high word of a 64×64 product has to be emulated with 32-bit pieces. The benchmark code is not part of this PR.

Not done. bv_decide does not yet produce proofs through the VM: that needs a kernel-reduced check that the encoded expression matches the reflected term, and a soundness statement for bvVMProg. The bitblaster bytecode is large (about 200 KB) because gate operations are expanded inline; a call instruction would shrink it.

🤖 Generated with Claude Code

nomeata and others added 8 commits November 2, 2026 17:08
Adds `Lean.KernelVM.run`, a small imperative machine whose reference semantics is defined in Lean and which the kernel evaluates natively, plus native `Nat.getByte`/`Nat.getUInt64`.

🤖 Built with Claude Code
…experiment

Adds partial-correctness rules for `Lean.KernelVM` programs with a verified trial-division example, and an experimental `grind.lia.vm` option that checks the linear steps of a `lia` refutation in one run of a cutting-planes checker. Its soundness theorem `unsat_of_linCheck` is still an axiom.

🤖 Built with Claude Code

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Adds `Std.Tactic.BVDecide.LRAT.KernelVM.lratVMProg`, a flat-array LRAT checker (RUP and deletion steps) for `Lean.KernelVM`, with encoders for CNFs and proofs, and moves the notation for writing VM programs to `Lean.KernelVM.DSL`. The kernel VM now flattens chains of `seq`, evaluates register and constant operands inline, and copies inputs into byte buffers.

🤖 Built with Claude Code

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`Lean.KernelVM.run` now takes the program as a numeral holding a flat register-machine bytecode, with fuel counting instructions, and its inputs are numerals or strings. The structured language becomes an untrusted source language with a compiler (`Lean.KernelVM.Stmt.compile`), and the library programs are stored as compiled literals. Adds `ByteArray.toNatLE`, implemented natively, for encoding inputs. Removes the Hoare rules for the previous semantics.

🤖 Built with Claude Code

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Adds `Std.Tactic.BVDecide.KernelVM.bvVMProg`, which bitblasts a `BVLogicalExpr` into an AIG with structural hashing, emits the Tseitin encoding of the cone of influence of its output, and checks an LRAT proof against it. In mode 1 it stops after building the CNF, which the new `Lean.KernelVM.runMem` can read at elaboration time. The LRAT checker now skips satisfied hints.

🤖 Built with Claude Code

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The kernel VM bitblaster now builds the same AIG as `BVLogicalExpr.bitblast` and the same CNF as `Std.Sat.AIG.toCNF`, so the SAT solver sees exactly the problem it sees with `bv_decide` today. The circuits follow the `blast*` functions, and the CNF is produced by the same depth-first traversal with if-then-else detection. The test compares both on random expressions.

🤖 Built with Claude Code

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Adds `BinOp.umulh`, the high 64 bits of the 128-bit product of two words, so that bytecode can implement multi-word arithmetic with four multiplications per limb product instead of sixteen.

🤖 Built with Claude Code

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	src/runtime/mpz.cpp
#	src/runtime/mpz.h
@nomeata

nomeata commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator Author

(This was an experiment to see what’s possible, but will likely not go forward in this form, so just storing it here.)

`List.foldr` is only computable after `Init.Data.List.Impl`, which this module precedes.

🤖 Built with Claude Code

Co-Authored-By: Claude Opus 5.5 <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant