# How Rings Guarantees Network Correctness with Formal Verification

- Published: 2026-12-02
- Author: [Ryan Kung](https://medium.com/@ryankung)
- Canonical URL: https://rings.rs/blog/how-rings-guarantees-network-correctness-with-formal-verification/

![How Rings Guarantees Network Correctness with Formal Verification](/assets/blog/how-rings-guarantees-network-correctness-with-formal-verification/cover.png)

Rings Network has been in development for four years. Because of its architecture—combining multi-step WebRTC handshakes, Chord DHT overlay topology state, and Sphinx/AEZ onion circuit state—making sure the network behaves reliably under every condition is hard. What happens on a live mesh when browser tabs join and leave every few seconds? In a semi-connected `(ABC)(CDE)` topology where NATs block a direct link between `A` and `E`, how do we guarantee `A` and `E` can still talk reliably through `C`? These interactions are subtle, and for a long time they were a recurring source of bugs.

![Fig. 1. Three coupled asynchronous state machines under browser churn and semi-connected WebRTC topologies.](/assets/blog/how-rings-guarantees-network-correctness-with-formal-verification/figure-1.png)

## 1. Avoiding Specification Drift with Embedded Model Checking

Formal verification is the obvious direction to look. The catch with traditional standalone tools—like TLA+ or Coq/Lean 4—is that even if you prove theorems over an abstract spec, hundreds of thousands of lines of production Rust keep moving. Sooner or later you get *specification drift*: the spec you proved and the code you ship stop saying the same thing, and the proof gives you no guarantee about the binary actually running in production.

We needed a verification approach tied directly to the codebase, where specification drift cannot happen. So we started with Stateright.

Stateright is an embedded finite-state model checker written in Rust (similar in spirit to TLA+'s TLC checker, but living inside the host language). Instead of rewriting a protocol in a separate specification language, you implement Stateright's `Model` trait directly over your real Rust state structs. It simulates packet loss, reordering, duplicate delivery, timeouts, and node crashes in memory, exploring reachable state interleavings via breadth-first search. When production code changes by a single line, the model checker runs that exact new line.

## 2. Algebraic Carriers and Monadic Effect Isolation

Good formal verification has one prerequisite, though: the system itself has to be formally expressible. Fortunately, that work started four years ago when I wrote the first lines of Rings. When we implemented Chord, we modeled its identifier space explicitly as the finite ring `Z/2^160Z`. When we built end-to-end encryption such as ElGamal, we wrote protocols against abstract algebraic traits—abelian groups, finite fields, and module actions—rather than scattering curve-specific calls across the codebase.

Over the past six months, we pushed that structure across the rest of the runtime. Replicated DHT storage was refactored onto CRDT join-semilattices (`EntryCrdt`), local crash recovery onto committed-memory product lattices, and multi-hop onion circuits and remote calls onto typed Kleisli category pipelines. Just as importantly, we used functors and writer-monad style transitions to separate pure state functions from side effects: core reducers take `(State, Event)` and return `(State', Effects)` as plain data, leaving an outer interpreter to perform socket I/O, timers, and disk writes. That gave the core runtime clean falsifiability.

![Fig. 2. Algebraic carriers across layers and functor/writer-monad separation of pure transitions from I/O effects.](/assets/blog/how-rings-guarantees-network-correctness-with-formal-verification/figure-2.png)

## 3. From Stateright to an In-House Wasm Model Checker

With those boundaries in place, we modeled our subsystems in Stateright: generational transport flow-control windows (5.91 million states on its own), ICE candidate settling, CorrectChord stabilization, CRDT replica handoffs under crashes, Sphinx/AEZ onion admission accounting, anti-replay filters, and Onion-TURN NAT bridges.

Then we hit two Stateright limitations on our hardest problem—frequent browser disconnect/rejoin churn across connection generations:

1. Stateright relies on a multithreaded native runtime and does not compile to `wasm32-unknown-unknown`, whereas a large share of Rings nodes run inside browser tabs.
2. A ring cannot converge while links are actively flapping; what we needed to verify is *conditional liveness*—that once churn stops, the protocol is guaranteed to converge back to the Chord fixpoint under weak fairness (`WF`) and strong fairness (`SF`). Deciding that cleanly requires strongly connected component (SCC / Streett trap) analysis over the reachable state graph.

So alongside Stateright, we wrote our own model-checking engine, `CheckedModel`, in ~920 lines of single-threaded, allocation-only Rust (`crates/core/src/swarm/transport/test_model_check.rs`). It steps the shipped `TopologyState` and `ConnectionLifecycleRegistry` directly, deduplicates states with a 128-bit salted SipHash fingerprint, and decides both safety (`Always`) and fair-suffix liveness in one breadth-first pass. Because it has zero thread dependencies, our headless Chrome CI compiles the exact same checker to WebAssembly and runs the same exhaustive search on `wasm32` as on 64-bit native—and because `usize` hashes at 32 bits on Wasm versus 64 bits on native, matching state counts on both targets also rule out fingerprint collisions.

![Fig. 3. Dual model-checking architecture combining embedded Stateright suites with the in-house Wasm-compatible CheckedModel engine.](/assets/blog/how-rings-guarantees-network-correctness-with-formal-verification/figure-3.png)

## 4. Mutant Refutation Across 9.50 Million Reachable States

One last problem remained: how do we know an invariant we wrote isn't accidentally vacuous? In model checking, a slightly misstated assertion that evaluates to true everywhere will happily pass ten million states while checking nothing.

To keep every property falsifiable, we added *mutant refutation*: 54 single-line protocol fault switches wired into the models—such as allowing replayed onion cells, skipping per-slot sender caps, deleting local replicas before successor acknowledgment, or letting callbacks from a retired connection mutate the routing table. In CI, production code must clear all 16 model suites—**9,496,521 reachable states**—with zero violations; then CI flips on each of the **54 mutants** and requires **54 / 54 to fail** with a minimal counterexample trace (the four rejoin mutants, for instance, are all refuted within 10–788 states in 9.4 ms total).

![Fig. 4. Quantitative summary of 9.50M verified states and 54 refuted safety mutants across Rings 1.0.](/assets/blog/how-rings-guarantees-network-correctness-with-formal-verification/figure-4.png)

Paired with strict-provenance Miri on core pointer invariants and ASan/LSan across `fsync` durability and C FFI bindings, the whole suite finishes in roughly 100 seconds in CI. Finite-state model checking still has clear limits—open-network Sybil resistance and economic incentives belong to epoch-randomized placement and DRanking in Rings 2.0—but for the WebRTC handshakes, non-transitive bridges, and browser churn bugs that haunted us for years, we no longer rely on adding `sleep`s and hoping tests stay green.

## Further reading

- [Rings 1.0 Whitepaper](https://rings.rs/papers/rings/) — Section 10 formal verification state relations, Table VI metrics, and Rejoin BFS breakdown
- [One Lookup, Many Fingers](https://rings.rs/blog/one-lookup-many-fingers/) — range-proved finger convergence for browser meshes
- [Correctness: How We Build Distributed Systems Correct](https://rings.rs/blog/correctness-how-we-build-distributed-system-correct/) — algebraic curve laws, CRDT semilattices, and pure state-effect transitions
- [Chord DHT & CRDT Storage](https://rings.rs/docs/advanced-topic/chord.html) — overlay stabilization, gap bridging, and storage replication

github: [https://github.com/RingsNetwork/rings](https://github.com/RingsNetwork/rings)
