Skip to content

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.

  1. 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.

  2. 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.

  3. 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.

  4. 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):

Crash-safe storage · BlockStore Alternating superblocks and two-barrier copy-on-write commit
4096-byte sectors
Sector 0 · Even Epoch 42

Active superblock

Valid SHA-256 over header and live tree.

Sector 1 · Odd Epoch 43

Staging superblock

Updated only after Barrier 1 completes.

Sectors 2..8 Preserved

Epoch 42 live blocks

Never overwritten during Epoch 43 commit.

Sectors 9..11 Shadow CoW

Epoch 43 new blocks

Allocated free in both Epoch 42 and 43.

Phase 1 · Shadow write writeBlock 9..11

Stages data and bitmaps into free sectors.

Phase 2 · Barrier 1 flush

Ensures shadow blocks persist before pointer flip.

Phase 3 · Pointer flip writeBlock 1 (ep 43)

Writes new superblock + SHA-256 into Sector 1.

Phase 4 · Barrier 2 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 (0 and 1): Sectors 0 and 1 hold 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, executes flush (barrier 2), and reads back the committed tree to confirm.
  • All-or-nothing crash recovery: Under the explicit CrashIntegrity premise (that an untouched 4 KiB sector retains its bytes and a completed flush persists earlier writes), BlockCrash proves 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.

Pythonic contract view (Std.ChaCha20Poly1305)
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))) == none

Lean

Lean view (Std.ChaCha20Poly1305)
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⟩⟩ = none

Explanation

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.

PrimitiveStandardWhat is checked in the Lean kernel
Sha256Unboxed & Sha512FIPS 180-4Unboxed 32-bit and 64-bit round functions proved bit-for-bit equal to the FIPS mathematical digest specifications.
Hmac & SigV4RFC 2104, AWS SigV4Keyed MAC equivalence and canonical AWS Signature Version 4 request signing and constant-time server verification.
ChaCha20Poly1305RFC 8439Stream cipher and Poly1305 authenticator proved equal to RFC 8439, plus AEAD round-trip decryption and tag-mismatch rejection.
Field25519 & X25519RFC 7748Finite-field arithmetic mod 2^255 - 19 and branchless 255-step Montgomery ladder proved equal to RFC 7748 (rejecting all-zero shared secrets).
Ed25519RFC 8032Constant-time key generation and signing plus signature verification proved equal to the RFC 8032 specification.
P256Verify & RsaVerifyFIPS 186-5, RFC 8017Public-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:

  1. 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_sound proves 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.
  2. TLS 1.3 handshakes and record protection (Tls13 and Tls13KeySchedule): Implements HKDF-SHA256 key derivation (proving distinct labels and transcripts never collide) and client/server TLS 1.3 (TLS_CHACHA20_POLY1305_SHA256 with X25519). An AuthenticatedSession carries proof fields confirming certificate validation, transcript signature verification, and peer Finished MAC 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 report notSent when zero application-data records crossed the socket.
  3. Noise XX and live rustls interop: Alongside Noise_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 against rustls, 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 over Tls13. Checked theorems prove serialized headers never contain CR/LF injection characters, Content-Length and chunked bodies round-trip cleanly, and notSent holds exactly when failure occurred before the encrypted HTTP request frame was sent.
  • Opaque SecretExchange and ServeVerified drivers: 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, and Escrow: S3 proves that wire bytes carry the exact canonical method, path, headers, and payload hash signed by SigV4. Built on BlockStore and those transport layers, S3d serves SigV4-authenticated objects without overwriting existing putIfAbsent keys, Collector reconciles crash-safe observation archives, and Escrow proves double-entry balance conservation and at-most-once settlement.

Lema v0.3.2 · Updated 29 October 2026 · Version and compatibility