Small Updates on Formally Verifying Go Code at the Assembly Level

This is the era of Lean and formal verification. You can feel it just by looking at the Lean news that pours out every day (https://leanweekly.news/). Writing proofs used to take an astronomical amount of time and money, but now we can hand proof writing over entirely to a sufficiently smart frontier model. And because the Lean kernel checks every proof, it is a job you can trust an agent with.
There is a lot you can do with Lean, from a simple calculator to a proof of the Riemann hypothesis. But when I see opinions that Lean can solve everything, I think there is a fair amount of hype mixed in. To see past the hype, you have to try it yourself and find out what it can and cannot do. So I am running my own experiments, and I am recording the process and what I learned here. My first target is Prysm, the project I work on full time, and its internal libraries.
What formal verification needs
Formally verifying every part of existing software is never easy. Most software is written in languages developers like, such as Python, Go and Rust. To bring that code down to the level of a proof assistant like Lean or Rocq, you need a perfect translator in between. Another problem is that the specification (spec) that existing software implements is often not rigorous.
Formal verification needs a formal specification. The Ethereum consensus layer has a specification written in Python and Markdown (consensus-specs), but it focuses mostly on readability, so it is far from the rigor that Lean has. It seems I am not the only one who feels this way, because the work of rewriting the specification in Lean has moved fast over the last few months. EthLib is a project that rewrites each small piece of Ethereum as a formal specification. Right now the work is mostly on cryptographic schemes, and there is a plan to move the whole consensus layer over. Etheorem is a community project that rewrites the executable consensus specification in Lean, and it already passes the official test suite.
Assuming a complete formal specification exists, the next thing to look at is the existing software. In the Lean ecosystem, Rust is the most mature language. Thanks to the borrow checker in the Rust compiler, important memory properties are guaranteed. The best-known project is Aeneas, which translates Rust code into pure functions. hax is also worth a mention. Lighthouse is using Aeneas to translate its existing Rust code. (Formalhouse)
Go, the language Prysm is written in, still has less tooling than Rust. The tool I am watching right now is Perennial. It builds on Iris, a framework for concurrent programs, and uses a translator called Goose to translate the semantics of Go into Lean. It is still moving fast, so I am keeping a close eye on it. Which led me to this thought:
Don’t depend on Go. Take the machine code that Go compiles to, lift it to assembly, and verify that.
It started with a conversation I had with Nic at the last Devcon. When I talked about my concerns with Prysm and formal verification, the project Nic suggested I look at was Kraken. Kraken, a joint project of the Lean FRO, Google and INRIA, is a Lean framework for verifying assembly programs. For now it supports only a subset of the x86_64 and aarch64 instructions. If I just lift the machine code compiled from Go code into assembly, I no longer have to depend on the Go language itself or on its compiler.
The goal: SSZ and SingleAttestation deserialization
So what should I verify? I start from the simplest foundation, SSZ (Simple SerialiZe). I picked it because 1) the specification is simple (ssz-specs), 2) the formal specification is almost done (EthLib, Serialization/Ssz), and 3) it exists in Prysm as an independent library (methodical-ssz).
The first milestone was to verify the deserialization (decoding) of the SingleAttestation SSZ type, the type sent and received most often on the Ethereum network. When a node receives a byte string from a peer over P2P that claims to be a SingleAttestation, it has to deserialize that byte string. In theory any byte string can come in as input, so if we can pin down the result rigorously, I think that is a pretty big achievement.
class SingleAttestation(Container):
committee_index: CommitteeIndex
attester_index: ValidatorIndex
data: AttestationData
signature: BLSSignature
SingleAttestation is a container with a fixed size (240 bytes). So during deserialization, every byte string that is not 240 bytes must be rejected. And deserializing 240 bytes must give us the SingleAttestation we want. I want to check that when Prysm receives any random byte string, it behaves as the specification above says. And once the serializing encoder is verified too, the implementation naturally gets these properties as well:
roundTrip_of_wellFormed(round trip): serializing an in-range value and then deserializing it gives back the original value.serialize_of_deserialize(canonicality): bytes that deserialization accepts are equal to the result of serializing that value again. In other words, a value has exactly one encoding.fits_of_deserialize(range): the result of deserialization is always within the range of the type (length limits, integer widths).
Method
The big picture
deserialize desc
encode g
UnmarshalSSZ
DecodeSpec.lean: Bind two bridges into one.
The specification is written generically for all of SSZ, while the path I want to verify is the specific machine code that deserializes SingleAttestation. That makes it hard to connect the two in one step. So I put an intermediate type, SszValue, between them and built two bridges. One bridge connects the EthLib specification and SszValue inside pure Lean. The other connects SszValue and the x86-64 machine code on top of Kraken. Once both bridges are in place, all that is left is to bind them into a single theorem.
1. Specification: EthLib Desc
In EthLib, an SSZ type is represented as data called Desc. Integers, byte vectors, containers and so on are all written as Desc values, and a single deserialize function works for every Desc. Thanks to this, writing the specification of SingleAttestation comes down to writing this type as a Desc in one line. (SpecDescription.lean#L26)
def desc : Desc := .container ["committee_index", "attester_index", "data", "signature"]
[.uint 8, .uint 8, Prysm.AttestationData.desc, .byteVector 96]
I wrote this Desc by hand from the electra specification in consensus-specs, and its fields match the Python definition above one to one. I wrote the AttestationData of the data field separately in the same way, too.
2. Bridge 1: SszValue <-> EthLib
EthLib’s deserialize takes a byte string and returns a general value called Value, but what Go code works with is a struct with fields like CommitteeId and AttesterIndex. To close this gap, I defined SszValue, a concrete type that mirrors the Go struct, and encode, which says how this value is laid out as bytes. (Spec.lean#L30-L42)
structure SszValue where
committeeIndex : UInt64
attesterIndex : UInt64
data : Prysm.AttestationData.SszValue
signature : Vector UInt8 96
encode g is 240 bytes: 8 bytes of committee_index, 8 bytes of attester_index, 128 bytes of AttestationData and 96 bytes of signature, joined in that order. For this type, I only need to prove three things.
serialize_eq: serializinggwith EthLib’sserializegivesencode g.toValue_fits: whengis turned into aValue, it is within the range of the type.toValue_surjective: every in-rangeValuecomes from someg.
With just these three, I can reuse the three EthLib properties introduced above (round trip, canonicality, range) as they are (DeserializeOfSerialize.lean), and as a result I get these two theorems.
accepted_is_encoding: an input that the specification accepts is alwaysencode gfor someg.spec_accepts_iff: the specification accepts an input only when the input is exactly 240 bytes.
Thanks to these two theorems, the machine code proof does not need to know anything about the general deserialize. The machine code side has to show just two things: “when encode g comes in, it behaves like this”, and “if it is not 240 bytes, it rejects”.
3. Bridge 2: SszValue <-> machine code
So how do we bring the real code into Lean? From the ELF of Prysm built with Go 1.26.10, I disassemble the (*SingleAttestation).UnmarshalSSZ function into a .S file, and Kraken reads this file and turns it into a Lean program. (Code.lean#L20) Of course, I also have to check separately that the extracted .S really matches the ELF. Reassembling the .S at the original addresses must give the ELF bytes, and three decoders (the Go disassembler, LLVM and the Kraken parser) must read the same bytes as the same instructions. The proof is done on the assembly, but these checks confirm that the assembly is the same as the machine code that actually runs.
Looking at the start of the function, you can see that when the input length in rcx is not 240 (0xf0), it jumps straight to returning ssz.ErrSize.
cmp $0xf0,%rcx ; len(buf) == 240 ?
jne L_2f6 ; → return ssz.ErrSize
I proved two theorems about the machine code. (Statements.lean#L81)
- Reject: if the input length is not 240, it returns
ssz.ErrSize, and the input and the receiver do not change. - Accept: if the 240 bytes that
rbxpoints to have the formencode g, it returns nil, and each field of the receiver gets its matching piece.
The UnmarshalSSZ methods of CommitteeIndex, ValidatorIndex and AttestationData that it calls along the way reuse their own proved theorems as they are. However, Go runtime functions such as runtime.newobject, runtime.makeslice and runtime.memmove are not proved. I put them into the theorem’s assumptions as contracts. The limitations section below covers this in more detail.
4. Binding the bridges
The last step binds the two bridges into one theorem. (DecodeSpec.lean#L21-L36) This theorem starts from the premise that EthLib’s deserialize accepted the input. Crossing bridge 1 tells us that the input is encode g for some g, and crossing bridge 2 tells us that the machine code returns nil on that input and fills in the receiver.
theorem Decode.accept_spec : Decode.accept_spec_statement := by
...
obtain ⟨g, rfl, rfl⟩ := SszValue.accepted_is_encoding hd -- bridge 1
...
exact Decode.accept ... -- bridge 2
I bind the reject side the same way. An input that the specification rejects is not 240 bytes, and the machine code also rejects an input that is not 240 bytes. With this, I could show that, assuming the runtime contracts, Prysm’s decoder makes the same decision as the EthLib specification for any byte string that comes in.
What’s next?
Other SSZ types
Besides SingleAttestation, many other types can receive arbitrary byte strings. Unlike SingleAttestation, which has a fixed size, these need new things like variable-length fields, so they are a bit harder. As groundwork for this, I am finding and fixing bugs in the current code.
Hash Tree Root
The hash tree root is the value compared during consensus, and EthLib already has a hashTreeRoot specification, so the same method can apply. The hash function (SHA-256) itself can either be left as a contract, or I can verify hashtree directly on Kraken. Kraken does not yet support the AVX family of instructions that it mainly uses.
Limitations: what did I assume?
Kraken == the real CPU
Every proof is done on the semantics that Kraken defines. In other words, Kraken is the largest part of the Trusted Computing Base (TCB). If a real CPU runs some instruction differently from Kraken’s model, the proof loses its meaning.
The Go runtime
The Go compiler inserts calls to functions of the runtime package. new(AttestationData) and append in Go code turn into calls to runtime.newobject, runtime.makeslice and runtime.memmove. These functions lead into Go’s internal memory allocation, GC and so on, so I did not prove them. Instead, I made the contract “if you call it like this, it returns like this” an assumption of the theorem.
The theorem has two more assumptions. First, the GC is not in its marking phase during the call (the write barrier is off). Second, the stack is large enough, so the stack does not grow. So the final theorem is a conditional statement: “if the Go runtime behaves as its contracts say and these two conditions hold, the decoder behaves as the specification says”.
Once concurrency comes in, the assumptions about the Go runtime get much larger, so Kraken may not be a good fit for code that mixes goroutines, mutexes, channels and so on. The serialization library in this post, though, is sequential code, so it fits this method well.
Appendix: random ideas
- The places where the specification and the real implementation differ are where legitimate optimizations live. Prysm uses a Field Trie so that it does not hash from scratch on every state change. If I model such an optimization algorithm of the implementation in Lean, and translate the current Prysm code into Lean with a tool like Perennial, I think I could check that the two are equivalent.




