The proved storage and HTTPS stack
How Lema builds crash-safe block storage, RFC cryptography, X.509 validation, TLS 1.3, HTTP/1.1, and S3 inside the language above the six host capabilities, and the exact scope of each proof.
In most everyday application languages, a service that speaks HTTPS, signs cloud storage requests, or persists state across power loss delegates the hardest parts of its behavior to native foreign libraries: a C or Rust TLS stack (OpenSSL or rustls), an external crypto library, an HTTP parser, and operating-system file rename conventions. Because those libraries sit outside the language, you cannot prove a theorem about what happens to your application state when a socket drops mid-request or a disk loses power between writes.
Building on the tradition of verified cryptographic and protocol stacks such as Project Everest (miTLS and EverCrypt in F*) and Fiat-Crypto (in Rocq), Lema implements that stack inside the language above the six flat-byte host capabilities (Capabilities and the host boundary). Copy-on-write block storage, crash recovery, cryptographic primitives, X.509 certificate validation, TLS 1.3, Noise XX, HTTP/1.1 framing, AWS SigV4 signing, and S3 clients and servers are ordinary Lema modules in the store carrying kernel-checked theorems.
-
Layer 1 foundation
Raw blocks, sockets, clocks, RNG, secrets
Six flat-byte host capability tags (tcp, udp, block, random, clock, secret) with explicit clean vs. uncertain failure flags.
-
Storage & journals
BlockStore, BlockCrash, Record, ActorJournal
Copy-on-write 4 KiB sector store with alternating superblocks, two-barrier atomic commits, and write-ahead actor journals.
-
Cryptography & transport
ChaCha20-Poly1305, Curve25519, X.509, TLS 1.3
Fixed-width implementations proved equivalent to RFC/FIPS specifications, powering authenticated TLS 1.3 and Noise sessions.
-
Protocols & services
HTTP/1.1, S3, S3d, Collector, Escrow
Header-safe HTTP framing, SigV4-verified S3 storage, and durable applications with end-to-end conservation laws.
1. Crash-safe storage on raw 4 KiB sectors (BlockStore and journals)
Instead of trusting operating-system filesystem rename semantics, the host exposes raw 4096-byte disk sectors (block: info, readBlock, writeBlock, flush), and Lema implements its own proved copy-on-write store (BlockStore and BlockCrash):
Active superblock
Valid SHA-256 over header and live tree.
Staging superblock
Updated only after Barrier 1 completes.
Epoch 42 live blocks
Never overwritten during Epoch 43 commit.
Epoch 43 new blocks
Allocated free in both Epoch 42 and 43.
writeBlock 9..11 Stages data and bitmaps into free sectors.
flush Ensures shadow blocks persist before pointer flip.
writeBlock 1 (ep 43) Writes new superblock + SHA-256 into Sector 1.
flush + read-back Confirms Epoch 43; earlier crash recovers Epoch 42.
Under the stated CrashIntegrity premise, power loss before Barrier 2 recovers the intact prior epoch because live sectors are never overwritten in place.
- Alternating superblocks (
0and1): Sectors0and1hold even and odd epoch superblocks protected by SHA-256 digests over both the superblock header and the file tree. New commits allocate only sectors free in both the current and previous epochs, so live data from the prior committed epoch is never overwritten during an update. - Two-barrier atomic commits: Updating a file writes new data and bitmap blocks first, executes
flush(barrier 1), writes the new superblock into the alternating slot, executesflush(barrier 2), and reads back the committed tree to confirm. - All-or-nothing crash recovery: Under the explicit
CrashIntegritypremise (that an untouched 4 KiB sector retains its bytes and a completedflushpersists earlier writes),BlockCrashproves in the Lean kernel that any crash or uncertain write fault at any point during a commit recovers cleanly to either the previous confirmed state or the complete new state, leaving unrelated files untouched.
On top of BlockStore, Record tracks durable operation intent markers and ActorJournal implements bounded ring journals and snapshot rotation for crash-safe stateful actors.
2. Cryptography proved against RFC standards
Every cryptographic module in Lema pairs a readable mathematical Spec (transcribing the RFC or NIST FIPS standard over mathematical integers and byte lists) with a fast fixed-width implementation, linked by kernel-checked equivalence theorems:
Pythonic
From the cryptography library (audit/inputs/ChaCha20Poly1305.json), used by TLS 1.3 and Noise record protection.
law aead_roundtrip_checked(key: ByteArray, nonce: ByteArray, aad: ByteArray, pt: ByteArray, ct: ByteArray):
requires sealChecked(key, nonce, aad, pt) == some(ct)
ensures openChecked(key, nonce, aad, ct) == some(pt)
law open_rejects_tag_mismatch(key: List(UInt8), nonce: List(UInt8), aad: List(UInt8), body: List(UInt8), tag: List(UInt8)):
requires length(tag) == 16
requires tag != Spec.sealTag(key, nonce, aad, body)
ensures open(ByteArray.mk(Array.mk(key)), ByteArray.mk(Array.mk(nonce)), ByteArray.mk(Array.mk(aad)), ByteArray.mk(Array.mk(body ++ tag))) == noneLean
theorem aead_roundtrip_checked (key nonce aad pt ct : ByteArray)
(h : sealChecked key nonce aad pt = some ct) :
openChecked key nonce aad ct = some pt
theorem open_rejects_tag_mismatch (key nonce aad body tag : List UInt8)
(ht : tag.length = 16)
(hne : tag ≠ Spec.sealTag key nonce aad body) :
open ⟨⟨key⟩⟩ ⟨⟨nonce⟩⟩ ⟨⟨aad⟩⟩ ⟨⟨body ++ tag⟩⟩ = noneExplanation
The first theorem proves that whenever sealChecked accepts a 32-byte key, 12-byte nonce, additional authenticated data, and plaintext within RFC 8439 bounds and produces ciphertext ct, openChecked recovers the exact original plaintext pt.
The second theorem proves that if the 16-byte authentication tag at the end of a message differs in any bit from the RFC 8439 Poly1305 tag, open returns none and releases zero plaintext bytes.
The implementation computes ChaCha20 in 16 unboxed UInt32 registers and Poly1305 in five 26-bit UInt64 limbs, proved equal to the RFC 8439 mathematical specification and checked by the VM’s ConstantTime static analyzer.
| Primitive | Standard | What is checked in the Lean kernel |
|---|---|---|
Sha256Unboxed & Sha512 | FIPS 180-4 | Unboxed 32-bit and 64-bit round functions proved bit-for-bit equal to the FIPS mathematical digest specifications. |
Hmac & SigV4 | RFC 2104, AWS SigV4 | Keyed MAC equivalence and canonical AWS Signature Version 4 request signing and constant-time server verification. |
ChaCha20Poly1305 | RFC 8439 | Stream cipher and Poly1305 authenticator proved equal to RFC 8439, plus AEAD round-trip decryption and tag-mismatch rejection. |
Field25519 & X25519 | RFC 7748 | Finite-field arithmetic mod 2^255 - 19 and branchless 255-step Montgomery ladder proved equal to RFC 7748 (rejecting all-zero shared secrets). |
Ed25519 | RFC 8032 | Constant-time key generation and signing plus signature verification proved equal to the RFC 8032 specification. |
P256Verify & RsaVerify | FIPS 186-5, RFC 8017 | Public-input X.509 and TLS signature verification for ECDSA P-256 (under the stated Fermat primality premise) and RSA PSS / PKCS#1 v1.5. |
Secret-bearing routines (ChaCha20Poly1305, X25519, Ed25519.sign) are also tested against RFC and Wycheproof vectors on the VM and gated by static ConstantTime and NoBigIntPrimitives bytecode analyses (The compiler and VM).
3. X.509 certificates, TLS 1.3, and Noise
Above the cryptographic primitives sit bounded certificate validation and authenticated transport:
- Bounded X.509 v3 chain validation (
X509): Parses DER certificates and chains up to Depth 4 by structural recursion that is proved never to read past slice bounds.validate_soundproves that every accepted chain sits within its UTC validity window, verifies signatures up to a pinned trust anchor, enforces CA and path-length constraints, matches the target hostname under RFC 6125 wildcard rules, and rejects legacy RSA PKCS#1 v1.5 signatures on TLS 1.3 handshakes. - TLS 1.3 handshakes and record protection (
Tls13andTls13KeySchedule): Implements HKDF-SHA256 key derivation (proving distinct labels and transcripts never collide) and client/server TLS 1.3 (TLS_CHACHA20_POLY1305_SHA256withX25519). AnAuthenticatedSessioncarries proof fields confirming certificate validation, transcript signature verification, and peerFinishedMAC agreement. Checked record-layer laws guarantee that every handshake failure closes the TCP socket, every encrypted record uses a fresh 64-bit nonce, and failed requests accurately reportnotSentwhen zero application-data records crossed the socket. - Noise XX and live
rustlsinterop: AlongsideNoise_XX_25519_ChaChaPoly_SHA256(proved for nonce freshness and symmetric key agreement), Lema’s TLS 1.3 client and server are tested over live loopback TCP sockets againstrustls, including negative fixtures for expired certificates and wrong hostnames.
4. HTTP/1.1, S3, and end-to-end applications
At the top of the stack sit application protocols and services:
Http1: Runs bounded HTTP/1.1 client and server exchanges overTls13. Checked theorems prove serialized headers never contain CR/LF injection characters,Content-Lengthand chunked bodies round-trip cleanly, andnotSentholds exactly when failure occurred before the encrypted HTTP request frame was sent.- Opaque
SecretExchangeandServeVerifieddrivers: Outbound signed/bearer HTTPS calls (exchange) and inbound authenticated HTTPS servers (serve) run inside isolated driver sub-runs that bind credentials to verified X.509 peers so raw secrets never enter application code (Capabilities and the host boundary). S3,S3d,Collector, andEscrow:S3proves that wire bytes carry the exact canonical method, path, headers, and payload hash signed by SigV4. Built onBlockStoreand those transport layers,S3dserves SigV4-authenticated objects without overwriting existingputIfAbsentkeys,Collectorreconciles crash-safe observation archives, andEscrowproves double-entry balance conservation and at-most-once settlement.
Lema v0.3.2 · Updated 29 October 2026 · Version and compatibility