Using AI+Lean to optimize and improve existing code A one-day proof-of-concept by Th0rgal rewrote the sensitive shared logic of Ledger's two Bitcoin apps in Lean 4, producing a formally checked kernel that compiles to 32-bit LLVM IR and runs on both the legacy C app (app-bitcoin) and the Rust V-App (vanadium, RV32IMAC). The Lean spec covers 17 of the kernel's 45 functions across 12 statements with 0 sorryAx, and the integration touched roughly 200 lines of C and 700 lines of Rust, excluding tests. The author estimates about 2 hours 15 minutes of human time total (1 h upfront, 15 min unblocking an inference routing issue, 1 h reviewing), with the rest run autonomously, and cautions the specs are incomplete and the LLVM compilation chain is checked only by differential testing, not proved end-to-end. Lean 4 Ledger Our thesis is that it is already viable today to rewrite existing software in Lean 4 to obtain a formally proved binary that is both more compact and faster. Yesterday, Vincent and Guillaume suggested starting with Ledger's Bitcoin application; here is the result. Important: this is a one-day POC—the specs are not complete, there are other important properties worth formally verifying, and this would require more review on our side as well as validation of our modeling choices on yours. 1. Methodology and Architecture In terms of human time, I had already looked into it a bit when Charles mentioned it to me and had a rough idea of the Ledger stack: it took me about 1 h upfront to frame the problem, lay down the high-level specs, and design the pipeline, 15 min this morning to unblock an inference routing issue, and 1 h at the end to review and polish everything. Everything else ran autonomously. Ledger maintains two Bitcoin apps: the legacy C app app-bitcoin https://github.com/LedgerHQ/app-bitcoin/tree/f010982d3050e02813cd7129bebd68cf2d1f2099 , compiled natively to the microcontroller's instruction set on top of BOLOS, and the new Rust V-App vanadium https://github.com/LedgerHQ/vanadium/tree/ace8e96982408959dea9e7fbc59ee36e8c54e645 , compiled to 32-bit RISC-V RV32IMAC and executed inside a VM running on BOLOS to stream the app's code and data from the host, if I understood correctly . Rather than touching the UI or APDU/ECALL transport rewriting the UI in Lean 4 would have little value , we kept the original UI and I/O in each app and focused the effort on the shared logic where most of the attack surface lives. The result, Th0rgal/lean-ledger-bitcoin https://github.com/Th0rgal/lean-ledger-bitcoin , contains two things of interest for you: - A Lean kernel compiled into both apps FreestandingKernels.lean https://github.com/Th0rgal/lean-ledger-bitcoin/blob/main/BitcoinLedger/FreestandingKernels.lean : this is the Lean code actually executed on the device. turbo-lean https://github.com/Th0rgal/turbo-lean compiles it to 32-bit LLVM IR, replacing the sensitive paths we rewrote in each app. - A short spec on this kernel Spec.lean https://github.com/Th0rgal/lean-ledger-bitcoin/blob/main/Spec.lean , 12 statements : each statement targets a compiled function, and its proof is checked by Lean 0 sorryAx . It currently covers 17 of the kernel's 45 functions. The integrations fit into two patches: app-bitcoin-lean-opt.patch https://github.com/Th0rgal/lean-ledger-bitcoin/blob/752aa9a/ffi/bolos/patches/app-bitcoin-lean-opt.patch on the BOLOS side and vanadium-bitcoin-lean-opt.patch https://github.com/Th0rgal/lean-ledger-bitcoin/blob/752aa9a/ffi/vanadium/patches/vanadium-bitcoin-lean-opt.patch on the Vanadium side. The remaining C/Rust is the original code, lightly patched around 200 lines modified on the C side and 700 on the Rust side, excluding tests . It does three things: calls the Lean kernel in place of the original checks, parses PSBTs and scripts, and talks to the hardware keys, crypto, screen, and transport go through BOLOS syscalls . For that OS-facing part, using Lean wouldn't make sense; for the parsers, however, I think it would be a good idea. It's more work, but it would be worth it since they lend themselves well to formal verification. 2. Execution Plan 1. Specs and cross-testing : formalization in Lean 4, followed by an executable kernel refined in a loop against both repos' test suites. 2. 32-bit LLVM backend in turbo-lean https://github.com/Th0rgal/turbo-lean : allocation-free support for thumbv6m , thumbv8m.main , and riscv32imac . Going through LLVM IR felt safer than emitting assembly directly. This compilation chain is not yet proved end-to-end it is checked via differential testing . Proving it would be relatively expensive in inference, but it would be a good investment since it only needs to be done once. 3. Integration : minimal FFI shim in each app, original UI and I/O preserved, kernel linked with LTO. 4. maxwell optimization loop : iterations on reproducible benchmarks, with proofs and tests green, to get below the baselines. 3. Results Here is the spec. It only covers code actually compiled into the apps and is not complete, but it illustrates what is possible: - Transaction amounts: the kernel returns “valid” if and only if the amount is at most 21 million BTC. For a sum or difference, it returns “valid” if and only if the exact mathematical result stays between 0 and this cap, and in that case returns the exact result. https://github.com/Th0rgal/lean-ledger-bitcoin/blob/85ea35961b919bf1e2ff90644b5d0e6f858e5e74/Spec.lean L32-L51 - Display: splitting an amount into BTC and satoshis without division is exact for every 64-bit amount. https://github.com/Th0rgal/lean-ledger-bitcoin/blob/85ea35961b919bf1e2ff90644b5d0e6f858e5e74/Spec.lean L53-L57 - Derivation paths: the kernel classifies a path element as “hardened” if and only if it is at least 2