Quantum Recoverability, Formally Verified in Lean

In collaboration with dmpierre

TLDR: we implement the recovery statement of ZCash’s ZIP 2005 as a RISC-V zkVM guest reusing the Orchard crates and prove it on a laptop. We verify the guest’s RISC-V bytecode directly in Lean, against a formalized version of the ZIP, thereby taking the compiler out of the trusted computing base. This is the long version of the talk we gave at the Cambridge workshop.

Introduction

Quantum resource estimates keep going down. Architectural improvements, smaller quantum circuits for discrete logarithms and, more recently, AI-assisted autoresearch all point the same way.

Quantum recoverability is a more modest goal compared to a full post-quantum (pq) protocol. Instead of fully fledging a fully quantum safe implementation, it is instead to only make sure that assets secured by classical cryptography remain accessible to their rightful owners in the event of a cryptographically relevant quantum computer (CRQC). A quantum recoverability does not protect privacy against harvest-now-decrypt-later attacks. It only guarantees that users can get their funds out, through a recovery protocol proven under a pq proof system.

We wanted to run such a recovery protocol on ZCash Ironwood notes using a RISC-V zkVM rather than a hand-written circuit. Recent progress on binary fields and RISC-V zkVMs (Flock, SP1 Hypercube) has made proving a Rust program competitive with writing a circuit. Also, Ethereum privacy protocols developers write most of their application logic using such zkVMs. In that sense, having a recovery protocol reusing the protocol’s own crates is one that teams can realistically audit and maintain. Finally, standardization around RISC-V zkVMs lets existing and upcoming privacy protocols plan a recovery path without betting on a particular proof system.

This writeup will show:

  1. An implementation of the ZIP 2005 recovery statement as an SP1 guest, along with the precompile work required to make it practical.
  2. A method for proving, in Lean, that the guest’s bytecode enforces the ZIP, without trusting the compiler.

ZIP 2005

Ironwood recently activated and introduces a new shielded pool replacing Orchard. In that context, ZIP 2005 changes how Ironwood notes are constructed so that a future recovery protocol can work. The problem it addresses is that Orchard’s note commitment scheme is not pq binding: even under a pq proof system and a rehashed Merkle tree, a discrete-log-breaking adversary could forge notes that are not in the tree.

The quantum recoverability fix is twofold:

  1. A new note plaintext format binds all of the note’s fields into the derivation of the commitment randomness \mathsf{rcm}, such that an adversary constrained to treat the PRF as a random oracle cannot vary a field without changing \mathsf{rcm}.
  2. A new derivation path for \mathsf{rivkext}, the randomness behind \mathsf{Commit^{ivk}}, covers both existing keys derived from \mathsf{sk} and FROST or hardware wallet keys, for which exporting \mathsf{sk} is undesirable.

The ZIP then describes the recovery statement a pq proof system should enforce. Given an anchor, a randomized spend validating key \mathsf{rk} and a nullifier \mathsf{nf}, the prover knows a note, its keys and a Merkle path such that the key derivations hold, \mathsf{ivk}, \mathsf{pk_d}, \psi, \mathsf{rcm} and the note commitment are derived as specified, the commitment sits in the rehashed tree and \mathsf{nf} is its nullifier.

Implementation

We implemented the statement as an SP1 guest program. The guest follows the statement step by step and does not reimplement anything: it calls into the Orchard crate itself for key derivations, commitments and nullifiers, and into pasta_curves for Pallas arithmetic. Compiled as is, it runs in about 17m cycles, most of them spent on 255-bit modular arithmetic in software.

We brought this intial number down in three steps:

  1. Registering Pallas as a curve in SP1’s already audited Weierstrass add and double precompile
  2. Routing \mathbb{F}_p and \mathbb{F}_q multiplications through SP1’s uint256 precompile
  3. Routing Pallas curve operations through the new precompile.

All in all, this is about 1.2k added lines across forks of SP1, pasta_curves, sinsemilla and Orchard, for a more than 10× improvement down to 1.33m cycles. The two Sinsemilla commitments and the nullifier derivation now account for close to 60% of the cycles, which is the opposite of the hand-written circuit cost model where the BLAKE2b compressions dominate. Today, proving takes around 100s on a 2023 M2 Pro, attesting to the feasibility of the recoverability protocol on day-to-day hardware.

Two pieces of the statement are not in the guest yet: the FROST and hardware wallet authorization path, and the Merkle path check, which requires the commitment tree to be recomputed with a collapsing hash first.

Formal Verification

A zkVM proof says that some RISC-V program with a given hash executed correctly. It however says nothing about whether that program does what the specification says. The usual answer is to trust the compiler: the Rust source is reviewed, rustc and the zkVM toolchain turn it into an ELF, and the ELF is what gets proven.

For a recovery protocol that users will run once, possibly years from now and under adversarial conditions, we would rather not have rustc in the trusted computing base. The idea, following the evm-asm experiments on the Ethereum side, is to prove the correct at the bytecode level, foregoing the compiler entirely.

Approach

We work on the two ends of the pipeline. At the top, the ZIP is autoformalized in Lean as a GuestStatement: a predicate over field elements and Pallas points listing the checks the guest’s main runs. Since it is the artifact a reader has to trust, it remains short enough to be read against the ZIP side by side. It sits on top of a Lean model of Ironwood’s algebra, i.e. \mathbb{F}_p, \mathbb{F}_q and Pallas points, with their operations defined mathematically.

At the bottom, elf2lean reads the guest ELF produced by rustc and SP1’s toolchain and emits its instructions as decoded RV64IM values in Lean. Their semantics come from riscv-zkvm, a Lean extraction of the official Sail RISC-V model restricted to RV64IM, together with a hand-written executable machine model proven equivalent to the extraction instruction by instruction. It comes with both Zisk and SP1 backends.

In the middle, we prove the bytecode up to the specification in layers:

Layer Object Established by
5 GuestStatement: the checks main runs, over field elements accepting checks
4 Ironwood’s \mathbb{F}_p, \mathbb{F}_q, Pallas points coupling
3 The arithmetic the binary computes: limb-wise Fp.mul, Montgomery reduction postconditions
2 One continuation theorem per guest function, Safe P s symbolic executor
1 RV64IM words: the decoded instructions elf2lean

Layer 2 is where most of the work happens. For each function in the guest, from the Montgomery multiplication up to main, we state a continuation theorem: from any machine state s satisfying the function’s precondition P, executing its instructions is safe and reaches the continuation in a state satisfying its postcondition. These are proven with a symbolic executor stepping through the decoded instructions and discharging the resulting verification conditions. Theorems compose: a caller’s proof uses its callees’ theorems as black boxes, so the ELF is never executed symbolically in one piece.

Layer 3 reads the postconditions as statements in natural number arithmetic, say what the output limbs of Fp.mul hold in terms of its input limbs. Layer 4 couples this limb-level arithmetic to the mathematical field, so that the sequence of calls the binary makes corresponds to a sequence of operations on \mathbb{F}_p, \mathbb{F}_q and Pallas. Layer 5 shows that an accepting run of main implies the GuestStatement on the decoded inputs. Put together: if the ELF runs to completion on an input, the recovery statement holds on it. The zkVM proof certifies the former and Lean certifies the implication.

All in all, what remains in the trusted base consists of the Lean kernel, riscv-zkvm’s axioms, the GuestStatement as a faithful rendering of the ZIP, elf2lean’s decoding of the ELF, the specification of the SP1 precompiles the guest calls, and SP1 Hypercube’s own soundness, which is a separate verification effort. What is no longer in it: rustc, LLVM, the SP1 toolchain and every crate the guest depends on.

Challenges

We lay out two main challenges to this approach:

  1. The ELF is big. The recovery statement itself compiles to around 1.4k instructions, but the guest links the whole of Orchard, pasta_curves and the standard library, for a total of 41,783 RISC-V instructions.
  2. The proofs are very large: more than 333k lines of Lean, with cold builds above two hours. This makes iteration slow and makes it hard to check assumptions for vacuity, which we consider the main risk at this stage, since a precondition that is never satisfiable makes every theorem downstream of it true for the wrong reason.

Note that none of these are theoretical blockers. They are proof engineering problems, and the kind that proof automation and agentic proving are getting pretty good at.

Future Directions

Scaling should improve on its own, as both agentic proof generation and the underlying tactics are moving fast. More interestingly, the ELF infrastructure is independent of ZIP 2005: any small guest with a short specification should be provable the same way, with only the top two layers rewritten.

A recovery protocol for Ethereum privacy pools, along the lines of the one we described in our previous post, is an obvious candidate. On the tooling side, a decompiler from the decoded instructions back to structured control flow, better tactics for limb arithmetic and more proof automation would each cut iteration time substantially.

2 Likes

Really strong result, especially taking rustc and LLVM out of the TCB for a one-shot emergency recovery guest where users will run the binary years later under adversarial conditions.

Two concrete questions on applying this pattern to Ethereum recovery guests (privacy pools, and also sweeping unspent classical ERC-5564 stealth addresses where you want a PQ proof of seed/key derivation rather than exposing an ECDSA spend in the mempool):

  1. Precompile TCB vs ELF size: moving Pallas and uint256 arithmetic into SP1 precompiles gave the 10x cycle drop (17M to 1.33M), but also shifts that math from verified RV64IM bytecode into trusted precompile specs. For an Ethereum recovery guest (secp256k1, Keccak/Poseidon, Merkle path, simple key derivation), would you expect a minimal no_std guest to land well below Orchard’s 41.7k instructions, or does auditing the precompile specs become the real bottleneck?

  2. Vacuity checks on the 333k lines of Lean: since you flagged unsatisfiable preconditions as the main risk at this stage, did you try instantiating the top-level continuation theorems on one concrete test vector (a real note and valid witness) to mechanically rule out vacuity?

Also curious how you are thinking about the missing hardware wallet / FROST authorization path in the guest, since key-export constraints are the exact same pain point for Ethereum hardware signers during a PQ migration.

1 Like

Hey @siriusthemaster , thanks for the questions! :slight_smile:

Precompiles vs ELF size. The precompile specs turned out to be the small part. The model of UINT256_MUL and the Pallas add/double is a few hundred lines of Lean, and we prove it against the mathematics (naturals mod m, Mathlib’s Weierstrass group law). What stays trusted is that the model matches SP1’s executor and AIR. We audited that by hand. In each of these our model traps and SP1 does not, so a proof has to show the guest never reaches them. For secp256k1 and Keccak you would be using SP1’s existing audited precompiles, and I would expect the same shape: short specs, a handful of edge cases that need care.

The 41.7k instructions are mostly not arithmetic. The statement itself is about 1.4k; the rest is what basically the guest linker: the allocator, the panic and formatting machinery, witness deserialization. That is also where our remaining assumptions sit. So for an Ethereum guest I would expect the big win from no_std, no allocator, fixed-size inputs and panic = abort, more than from the choice of precompiles. I’d expect such a guest to land well below Orchard’s size, but we have not built one.

Vacuity. A valid witness is unfortunately the wrong test here. The theorems have the form “every accepting halt reachable from this state implies the statement holds of the input”. If the input is valid, the conclusion is already true and every hypothesis holds for free, so instantiating on a real note checks nothing. All the content is on inputs the statement rejects, so that is where we test: take the real machine state on a rejecting input, vary only what a hypothesis leaves unconstrained, and run the interpreter looking for an accepting halt.

That check found real problems. The top-level theorem is still conditional on contracts for the Rust runtime under main (allocator, panic path, formatting), and some of those were stated too strongly: they quantify over arbitrary memory, and a state with a crafted panic hook does reach an accepting halt. So as currently stated those hypotheses cannot all hold for a rejecting input, which is exactly the “true for the wrong reason” risk the post mentions. The proofs of the guest’s own code are unaffected. The fix is to restate the runtime contracts over an invariant on the statics and the heap, and then prove them by stepping the allocator and panic path like everything else. We haven’t quite done that yet, but we have also added formal verification at the Rust level using Aeneas, so we still have high confidence in the results.

This method of verification turns out to be really well-suited for small, cryptographic code and we’re using it in some work on LeanVM right now, and did similar work a month or two ago on an xmss verifier for ots.golf