Ruma Lean
Formal verification of Kahn's sort and State Res v2 using Lean 4.
Reference standard implementation in rust for other homeservers.
What's Inside?
The project is structured into three main parts in RumaLean/:
-
DirectedAcyclicGraph.lean -
Kahn.lean -
StateRes.lean
Equivalence Proof: Lean vs. Rust
Tie-Breaking Rule
The Matrix spec mandates tie-breaking by Power Level, Timestamp, and Event ID.
def eventToLex (e : Event) : ℕᵒᵈ ×ₗ ℕ ×ₗ String :=
toLex (OrderDual.toDual e.power_level,
toLex (e.origin_server_ts, e.event_id))
Topological Sort (Kahn's)
The sorting algorithm must be deterministic to ensure state consistency across the Matrix.
/-- Kahn's sort implementation -/
def kahnSort (g : Graph) : List Event :=
-- Logic proven deterministic
-- in Lean's total order
Development
You can run development tasks directly:
Why "Lean"?
- Dependency Minimization: The Rust implementation carries zero external dependencies, avoiding the 400-600 crate bloat of the full Ruma stack.
- Formal Correctness: Every line of the Rust implementation is mirrored by a mathematical proof in the Lean model.
- ZK Efficiency: Fewer instructions and smaller memory footprints result in significantly lower AIR trace rows in zkVMs.
Written securely with zero sorry proofs left behind.