Proving RISC-V ⇔ Clean: the SLL example

The Lambda VM series on formal verification continues, with LambdaClass and 3MI Labs: how we prove that a RISC-V instruction matches its Clean model, using SLL as the example. Three small equalities in Lean, from the official Sail spec to the message on the ALU bus.

Proving RISC-V ⇔ Clean: the SLL example

Contents

  1. Introduction
  2. What is SLL
  3. Background: what the Clean model does
  4. Our goal
  5. The plan: three equalities, one file each
  6. Step 1: Sail.lean
  7. Step 2: Bridge.lean
  8. Step 3: Alu.lean
  9. What is next
  10. Summary

Introduction

In a previous post we explained how to formally verify the circuits of a zkVM with Clean, using a toy VM (an adder, a multiplier and a CPU) as the example. In Lambda VM we have started applying the same method to our own circuits: we are translating them to Clean, and we call the result the Clean model of Lambda VM (formal/ in our repo). In this post we show, using one instruction (SLL) as an example, how we prove that a RISC-V instruction matches its Clean model. In other words, we prove that what the Clean model computes for that instruction is exactly what the official RISC-V spec says it should compute. The other instructions we have proven so far follow the same pattern.

As you read, you can follow our SLL implementation here.

What is SLL

SLL (Shift Left Logical) is the RISC-V instruction sll rd, rs1, rs2, which:

  • shifts the bits of rs1 to the left by rs2 mod 64 positions (the 6 lowest bits of rs2),
  • fills the right side with zeros,
  • applies mod 2^64, so anything past bit 63 is lost,
  • stores the result in rd.

In RV64, registers are 64 bits, so the useful shifts go from 0 to 63, and 6 bits are enough for that (2^6 = 64). The final mod 2^64 is there so the result fits in the register.

Example:

If rs1 = 0b1100 … 0000 0001 = 2^63 + 2^62 + 1 and rs2 = 0b0000 … 0100 0010 = 66, then the 6 lowest bits of rs2 are 0b000010 = 2 = 66 mod 64.

Shifting rs1 by 2 pushes bits 63 and 62 out, so rd = 0b0000 … 0000 0100 = 4.

Background: what the Clean model does

In the previous post we saw, with the toy VM, what a table's soundness proves: its Spec on every row, and the Guarantees of every message it pushes. Let's see what that means for SHIFT in the Clean model of Lambda VM. In the file formal/LambdaVM/Tables/Shift.lean we model the SHIFT table as a Clean circuit. Its soundness says: if a row satisfies the constraints and the lookups it pulls satisfy their Guarantees, then:

  1. RowSpec holds. When the row is active (mu = 1), the columns out0 and out1 hold the correct shift result: out0 + 2^32·out1 = shiftResult.
  2. The Guarantees hold for the message the row pushes to the ALU bus. The bus, its message (Msg) and the Guarantees are defined in formal/LambdaVM/Buses/Alu.lean. This is all the CPU sees: it never reads the SHIFT rows, only the message.

Our goal

The full goal is to prove that, in the Clean model, the CPU's SLL matches RISC-V's SLL, that is, that the CPU writes the right value to rd and moves the PC forward.

The Clean model of Lambda VM does not have the CPU yet, so in this post we focus on proving only one part:

What we prove in this post
If a Msg on the ALU bus has the SLL flags and satisfies the Guarantees, then its out is the result of RISC-V's SLL.

Once we have the CPU, we will only need to prove that:

  • the CPU pulls the Msg with the SLL flags and the values of rs1 and rs2 as operands;
  • the CPU writes the out of the Msg to rd and moves the PC forward.

The plan: three equalities, one file each

To prove "RISC-V's SLL" = "the out of the bus Msg", we prove three equalities, each in its own file:

  1. "RISC-V SLL from Sail* (execute_SLL)" = "shift computation in Lean (sllPure)"
    File: Sail.lean
  2. "shift computation in Lean (sllPure)" = "shiftResult from the Clean model"
    File: Bridge.lean
  3. "shift computation in Lean (sllPure)" = "the out of the Msg"
    File: Alu.lean

The proof of Step 3 uses Step 2. Every instruction has these same three files, in formal-riscv/RiscvEquiv/<Instruction>/.

* In the next section you can see what Sail is.
The chain of equalities: execute_SLL = sllPure (Step 1, Sail.lean), sllPure = shiftResult 0 0 0 (Step 2, Bridge.lean), sllPure = out of the Msg (Step 3, Alu.lean, uses Step 2)
The chain of equalities · three steps, one file each

Step 1: Sail.lean

File: formal-riscv/RiscvEquiv/Sll/Sail.lean

Equality: "RISC-V SLL from Sail (execute_SLL)" = "shift computation in Lean (sllPure)"

What is Sail

Sail is a language for describing ISAs (instruction set architectures). sail-riscv is the official RISC-V spec written in Sail: it describes what each instruction does, the decoding and the state (PC, registers, memory, etc.).

The Sail compiler can translate this spec to Lean, as functions that describe each instruction. We generate that Lean model ourselves, from pinned commits of the spec and of the compiler (sail-riscv-lean/ in our repo). That the Sail code matches the RISC-V manual is not proven anywhere. It is a trust assumption, backed by it being the official model and by the architecture tests it passes.

In this file we use execute_RTYPE, the Sail function that runs the base R-type instructions: they work on two registers and store the result in a third one (ADD, SUB, SLL, SLT, SLTU, XOR, SRL, SRA, OR, AND).

What this file does

It turns the execution of SLL, which changes the machine state, into the value it computes.

sllPure is that value: rs1 shifted left by the 6 lowest bits of rs2, mod 2^64.

def sllPure (rs1 rs2 : BitVec 64) : BitVec 64 := rs1 <<< (rs2.toNat % 64)

The theorem execute_SLL proves that Sail's SLL is exactly "read rs1 and rs2, write sllPure to rd". From here on we can work with sllPure, a plain computation, instead of the machine state.

theorem execute_SLL (rs2 rs1 rd : regidx) :
    execute_RTYPE rs2 rs1 rd rop.SLL = (do
      let a ← rX_bits rs1
      let b ← rX_bits rs2
      wX_bits rd (sllPure a b)
      pure RETIRE_SUCCESS) := by
  [...]

Step 2: Bridge.lean

File: formal-riscv/RiscvEquiv/Sll/Bridge.lean

Equality: "shift computation in Lean (sllPure)" = "shiftResult from the Clean model"

What this file does

shiftResult is the Clean function that gives the result of SHIFT (formal/LambdaVM/Buses/Alu.lean). It covers all shifts with three flags: word (32 or 64 bits), signed (arithmetic or logical) and direction (left or right). For SLL, all three are 0.

def shiftResult (word signed direction x s : ℕ) : ℕ :=
  let k := s % (if word = 1 then 32 else 64)
  if direction = 0 then (x <<< k) % 2^64
  else if signed = 0 then x >>> k
  else sra64 x k

Step 3 needs sllPure = shiftResult, but the two functions have different types:

  • sllPure stands for Sail's SLL and works with BitVec 64, so the cut to 64 bits is built in.
  • shiftResult comes from the Clean model and works with natural numbers (ℕ), so the mod 2^64 is written out.

This file turns sllPure into a natural number and proves it equals shiftResult, with two results.

shiftResult_sll: with the SLL flags (0, 0, 0), shiftResult gives what we expect: x shifted left by s mod 64, mod 2^64.

lemma shiftResult_sll (x s : ℕ) :
    Alu.shiftResult 0 0 0 x s = (x <<< (s % 64)) % 2^64 := by
  simp [Alu.shiftResult]

sllPure_toNat: sllPure turned into a natural number (.toNat) is shiftResult with the SLL flags.

theorem sllPure_toNat (a b : BitVec 64) :
    (sllPure a b).toNat = Alu.shiftResult 0 0 0 a.toNat b.toNat := by
  rw [shiftResult_sll, sllPure, BitVec.toNat_shiftLeft]

Step 3: Alu.lean

File: formal-riscv/RiscvEquiv/Sll/Alu.lean

Equality: "shift computation in Lean (sllPure)" = "the out of the Msg"

What this file does

The theorem sll_alu_matches_sail says: if a Msg on the ALU bus with operands a and b¹ has the SLL flags² and satisfies the Guarantees³, then its result (outLo + 2^32·outHi) is sllPure of a and b.

theorem sll_alu_matches_sail (m : Alu.Msg (F p)) (hg : Alu.Guarantees m)
    (hflags : m.flags.val = opselSHIFT)
    (a b : BitVec 64) (ha : dword m.in1Lo m.in1Hi = a.toNat)
    (hb : dword m.in2Lo m.in2Hi % 64 = b.toNat % 64) :
    dword m.outLo m.outHi = (sllPure a b).toNat := by
  rw [sllPure_toNat]
  exact shift_alu m hg 0 0 (by norm_num) (by norm_num) (by simpa using hflags)
    _ b ha hb

You can see in the proof that we use Step 2: sllPure_toNat swaps sllPure for shiftResult 0 0 0.

Notes:

¹ Operands.
ha says that the first operand (in1Lo + 2^32·in1Hi) is a. hb says that the second one agrees with b mod 64, because SLL only reads the 6 lowest bits of the shift amount. There is no range hypothesis: SHIFT range-checks its operands itself.

² Flags. flags is a number in the message that tells the bus which operation to run: flags = opsel + variants, where opsel is the operation number. opselSHIFT = 5, so for shifts flags = 5 + word + 32·signed + 64·direction. For SLL, flags = 5.

³ Guarantees as a hypothesis. Clean proves the Guarantees only for messages that a table pushed (here, SHIFT). The table that pushes is the one that proves them. This theorem talks about any Msg. That is why the Guarantees are a hypothesis here (hg).Each clause has its own hypotheses. LT assumes the low halves are less than 2^32, EQ assumes all four are, and SHIFT assumes nothing: it range-checks its operands itself.It is a definition, not a theorem. A made-up Msg may not satisfy it. SHIFT proves that its pushes do.

What is next

To prove the full equivalence between RISC-V instructions and Lambda VM circuits, we need:

  1. CPU in Clean. As we said in "Our goal", each instruction needs one more step: the CPU pulls the Msg with the right flags and operands, writes the out to rd and moves the PC forward. That is where the hypotheses of sll_alu_matches_sail (hg, hflags, ha and hb) get proven.
  2. Clean ⇔ Rust. Finally, we need to formally verify that the Clean model matches the Rust circuits. Today, a randomized differential test checks this (prover/src/tests/lean_diff.rs).

Summary

In this post we proved that the out of an ALU bus Msg is exactly what the official RISC-V spec says SLL computes, as long as the Msg satisfies the Guarantees and has the SLL flags. We did it with three small equalities, each in its own file, all meeting at sllPure. The same three files exist for every instruction we have proven so far, and the CPU Clean model will use them as they are.