Lean 4 Rewrite Of Ledger Bitcoin App Logic Tested

A Lean 4 rewrite of the shared logic in Ledger’s Bitcoin apps produced a smaller binary, a partly proved specification and a list of 11 suspected bugs. The team behind it calls the work a one-day proof of concept, and says the specs are incomplete and the modeling choices still need outside review.

The team wanted to check one claim: that existing software can be redone in Lean 4 to give a formally proved binary that is smaller and quicker.

Two apps, one kernel

Ledger ships two Bitcoin apps. The older one, app-bitcoin, is written in C and built natively for the device’s microcontroller under BOLOS. The newer one, Vanadium, is Rust, built for 32-bit RISC-V (RV32IMAC) and executed in a virtual machine on BOLOS.

The screen and transport code stayed as they were, because redoing a UI in Lean 4 would be of little use. The effort went into the logic both apps share, where most of the attack surface lives. The output is a Lean kernel built into both apps and a short specification. The kernel is compiled to 32-bit LLVM IR by a tool called turbo-lean, covering thumbv6m, thumbv8m.main and riscv32imac.

Changes to the existing code are small: around 200 lines on the C side and 700 on the Rust side, not counting tests. What remains handles PSBT and script parsing and hardware access through BOLOS syscalls, and hands validation checks to the Lean kernel.

Taproot signing on Vanadium: baseline and Lean variant side by side, with cumulative executed instructions
Image: share.thomas.md

What was proved

Twelve statements make up the specification. Each one targets a compiled function, and Lean checks every proof with no unfinished steps. Together they reach 17 of the kernel’s 45 functions. Examples:

  • Amounts above 21 million BTC are rejected, and any sum or difference must land between 0 and that limit and come out exact.
  • Showing an amount as BTC and satoshis without using division gives the exact answer for any 64-bit value.
  • A path element counts as hardened exactly when it is 2^31 or more, and paths over 10 elements on BOLOS or 16 on Vanadium fail.
  • SegWit programs pass at 20 or 32 bytes for version 0 and at 2 to 40 bytes for versions 1 to 16, and nowhere else.
  • Only the ALL, NONE and SINGLE signature hash types are accepted, each with or without ANYONECANPAY, and DEFAULT for Taproot.
  • A CompactSize length prefix must use its shortest canonical encoding.

Every statement is paired with an example that passes and one that fails. The BIP-370 nLockTime computation was also proved to match the rule for any list of inputs.

Size, speed and the bugs

The patched binary is smaller on both targets and faster on Vanadium. On BOLOS, latency is unchanged on the Nano S+ and higher on the Nano X. Signing on Vanadium runs 14.6% fewer RV32 instructions and loads 5% fewer pages under an identical compiler profile. One kernel serves three instruction sets and both apps, with a maximum stack frame of 96 bytes.

Formalizing both apps against each other surfaced 11 bugs or spec divergences, all on the Vanadium side. The team hasn’t manually confirmed each one, though they look solid, and the nLockTime behavior in particular needs confirmation from Ledger.

The caveats are significant. Measurements came from the Speculos emulator, not real hardware. Much of the RAM and file-size reduction on Vanadium comes from build settings, so only the 14.6% drop in executed instructions is attributable to the code. BOLOS sizes and latencies came from two different builds, and a full benchmark is being re-run. The LLVM compilation chain is checked by differential testing, not proved end to end.

What the run cost

Human time was modest: about an hour to frame the problem and design the pipeline, 15 minutes to fix an inference routing issue, and an hour to review. The rest was autonomous. It ran 19.9 hours of wall-clock time across 30 agents (one coordinator and 29 subagents) on 25 microVMs, including a two-hour pause for an inference provisioning fault.

  • 379.0 CPU hours of compute in total.
  • 696.6 million tokens across the agents, about 23.2 million each on average.
  • 2,494 theorems proved.
  • Roughly $537 at public API and cloud rates, excluding the team’s own server.

What comes next

The team suggests the parsers as the next Lean target and says proving the compiler chain is a one-off expense worth making. First, though, Ledger would need to review the specs and the suspected Vanadium bugs, and the hardware benchmarks would need to be redone.

Source: share.thomas.md

Written by

Saksham Bhargava

Saksham Bhargava runs Alteroid and has been writing about consumer technology for over five years, covering iPhone, Android, Windows, AI tools and gaming. With an MCA and six years of hands-on web development experience, he tests the features he writes about on his own devices and focuses on clear, practical guides that solve real problems. Away from the screen, he enjoys travelling to places most tourists skip.