Ziggifying Lodestar - Part 1
Lodestar v1.47 replaced its @chainsafe/blst Rust bindings with
lodestar-z/bls Zig bindings that use supranational's blst for the
underlying cryptography.
This is an exciting change for us in the team since its our first Zig integration since our
official blog post signalling our intention to use Zig.
BLS - A Primer
For a complete picture on BLS and its role in Ethereum, see the eth2book.
In a distributed system like Ethereum, authenticity of messages is a core property that the system must verify.
BLS (Boneh-Lynn-Shacham) is a cryptographic signature scheme to do that. Consensus clients verify BLS signatures for attestations, blocks, sync committees, deposits, execution payload bids, and other consensus messages.
Let's zoom in on just attestations and do a bit of napkin math on them. At the time of writing this, the network has about 900,000 validators, which means we have about 900,000 / 32 = ~28000 attestations per slot. An optimization the network does is to aggregate multiple attestations into a single object. This ends up being one of the costlier workloads of a consensus client.
BLS Aggregation
By far the most expensive cryptographic operation is the pairing check. This post will not go into detail of how pairing checks work, but simply put, BLS12-381 uses finite fields (approximately 381-bits wide as indicated in its name) and many multiplications, additions (with carry) and modular reductions are done on these fields. It would therefore be ideal to perform as few pairing checks as possible.
This is why BLS is such a great scheme for Ethereum's use case. BLS is aggregation-friendly due to its properties, which means we can combine N signatures from many attestations into a single one, and verifying that single signature is equivalent to verifying those same N signatures.
In practice, most attestations have the same signing root because validators (usually) vote for the same block, source and target checkpoint in the same signing domain.
We can optimize attestation verification by noticing the above. Then, instead of verifying each signature one by one, costing us a total of N pairing checks, we aggregate keys and signatures with random weights before verifying a single aggregated signature, we only have to do a single pairing check.
Random weights are necessary here since an attacker can create invalid signatures based on valid ones that cancel each other out, since signatures and public keys are essentially just points on a curve.
Flow
For the rest of this post, let's focus on the happy path of same-message attestations.
Original
The rewrite had more to do with a re-structuring of our workloads rather than just a simple one-to-one port from the Rust bindings to Zig.
Here's what's wrong with our previous implementation: we were doing too much work across Node environments rather than delegating the heavy work to the more performant native layer. Here are two images roughly going through the flow of how a same-message verification job is completed through our main and worker threads, respectively:

Our main thread is in charge of orchestration. It first resolves the public keys before sending a work request to our worker threads. Our worker threads spawn native threads to handle compute-heavy operations.

Our worker thread is in charge of the actual verification work.
We can quickly see some bad things from this flow:
- We cross NAPI boundaries a total of SIX times - every incoming arrow into the bindings across the dotted line is one NAPI call
- We were also doing a bunch of wasteful work (round-trip serialization/deserializations)
The root of the problem was our pubkey cache, which lived on our main environment only.
Node cannot send PublicKey wrappers to a worker, since they contained native
Rust objects owned by the main environment. We had to convert between representations
(adding to our napi call count) to be portable between main and worker threads.
The rewrite
Our rewrite changed this into a native, process-wide, concurrency-safe cache. In simple words, it is now in Zig and shared globally (behind a read/write lock) among all Node environments to allow reads/writes from all threads.
Here's the revised flow:

All the main thread has to do is to send a verification request to the worker.

The worker now does all the work, but even then, most of it is dispatched to the native layer.
Improvements:
- Main thread BLS workload is very lean now! Most work is delegated to the worker thread.
- Worker thread only crosses the napi boundary one time. All heavy work is done natively before returning the final result.
Arguably the biggest benefit is moving the cache to the native layer. The cache being native now also means we no longer have to do an awkward serialization/deserialization dance in the Node runtime, and can instead send verification requests to workers using validator indices instead of serializing the cached public keys, since the cache has a mapping of indices to public keys! This saves a whole lot of busy, unnecessary work that we were doing previously.
Results
We saw some big wins in our BLS verification metrics. The following screenshots show our metrics from a week before v1.47 went live on our nodes, and a week after. The node has a solo staker setup.
Throughput drastically increased:

It is much faster now to verify a signature set:

The latency from worker drastically reduced because of the new streamlined flow:

What's next
We're in the process of moving the following modules into Zig as well:
Stay tuned for more!