Skip to content

Capabilities and the host boundary

How Lema structures external interaction across Layer -1, Layer 0, and Layer 1's six flat-byte OS capability families while keeping TLS, HTTP, S3, and crash recovery inside proved Lema code.

In Anatomy of a Lema program, we saw that an interactive Lema program is a pure decision tree (Prog) of commands and replies. When that program runs on a real server, how does it open a TCP socket, write a 4 KiB disk sector, read the clock, or sign an HTTPS request without trusting a sprawling C or Rust runtime library?

In conventional application runtimes, the foreign-function boundary sits high up the stack: application code calls out to OpenSSL, rustls, libcurl, or OS filesystem helpers. That convenience carries a steep verification cost. Anything inside the foreign runtime sits outside the theorem prover’s reach, and passing complex pointer graphs across an FFI boundary invites memory bugs.

Following the verified-systems approach pioneered by seL4, CompCert, and Project Everest, Lema pushes the unverified host boundary down to flat byte frames over raw sockets, fixed 4096-byte disk blocks, clocks, random bytes, and sealed secret lookups. Everything above that line (binary codecs, copy-on-write block storage, cryptography, X.509 validation, TLS 1.3, HTTP/1.1, and AWS SigV4 signing) is written and proved inside Lema (The proved storage and HTTPS stack).

Trust boundary comparison Where the unverified FFI boundary sits: conventional runtimes vs. Lema
6 flat-byte OS tags
Conventional runtime High FFI boundary
Application business logic Typechecked
⚠ Foreign runtime FFI boundary Pointers & structs
HTTP/1.1 & cloud request signing Unproved C/Rust
TLS 1.3 & X.509 chains (OpenSSL / rustls) Unproved C/Rust
AEAD, Curve25519, SHA-2 primitives Unproved C/asm
OS filesystem rename & crash semantics Outside model
Lema verified-systems stack Proved in Lema
Applications & S3 (Escrow, S3d) checked
Http1 framing & SigV4 signing checked
Tls13, Noise & bounded X509 checked
RFC Crypto & CoW BlockStore checked
Ring.call · Flat LC\x02 byte frames Zero pointers
0x01 tcp 0x02 udp 0x03 block 0x04 random 0x05 clock 0x06 secret

Pushing the host boundary down to flat byte frames brings block storage, cryptography, X.509, TLS 1.3, HTTP/1.1, and S3 inside kernel-checked Lema.

Three layers from pure logic to OS system calls

Rather than hardcoding cloud or POSIX calls into the compiler, Lema organizes external interaction into three layers:

  1. Layer -1 · Hardware-agnostic core

    Prog, Contract, Safe, Handles, Tickets

    Pure mathematical interaction trees, protocol contracts, single-owner resource handles, in-flight ticket locks, and concurrency laws checked in the Lean kernel.

  2. Layer 0 · Systems descriptor model

    Domain, Region, Ring, Clock, Random

    The low-level systems concepts for isolated memory domains, bounded byte regions, request/reply submission rings (Ring.call), timestamps, and OS entropy.

  3. Layer 1 · Six flat-byte OS capabilities

    tcp, udp, block, random, clock, secret (+ exchange & serve)

    Self-contained binary request and reply frames executed by the Rust capability host, plus isolated sub-runs that keep raw secret bytes out of app code.

  4. Above Layer 1 · Proved Lema libraries

    BlockStore, Crypto, TLS 1.3, HTTP/1.1, S3

    Crash-safe storage, RFC cryptography, authenticated transport, and applications written in Lema and checked against Layer 1 contracts.

Layer -1: Hardware-agnostic interaction

At the bottom sits the Prog and Contract core (Anatomy of a Lema program and Evolving programs, handles and concurrency). Because Prog C R A is parameterized over any command vocabulary (C, R), nothing in the language kernel assumes POSIX sockets or Von Neumann memory.

Layer 0: Isolated domains and request/reply rings

Layer 0 defines the five systems concepts needed to run isolated workloads on a physical host:

  • Domain: Isolated execution contexts with capped memory budgets so separate workers do not share mutable pointers.
  • Region: Bounded byte buffers with generation-checked handles and busy locks while an asynchronous I/O operation is in flight.
  • Ring: Descriptor queues pairing request buffers with reply buffers, including the synchronous one-request/one-reply helper Ring.call (ByteArray request frame in, ByteArray reply frame out).
  • Clock and Random: Monotonic and UTC timestamps plus cryptographic randomness from the host OS.

Layer 1: The six flat-byte host capability families

Over Ring.call, Lema defines six primitive host capability families that cover standard networking, storage, time, randomness, and credentials:

TagFamilyOperationsWhat crosses the boundary
0x01tcpconnect, accept, send, recv, closeBounded byte slices against allowlisted endpoint and listener IDs.
0x02udpsendTo, recvFromBounded UDP datagrams (up to 65,507 bytes) over configured endpoints.
0x03blockinfo, readBlock, writeBlock, flushFixed 4096-byte sector reads, writes, and fsync barriers on named volumes.
0x04randombytes1 to 4096 bytes per call from the host cryptographic RNG.
0x05clockutcMs, monoNsUTC epoch milliseconds (for X.509/SigV4 windows) and monotonic nanoseconds.
0x06secretgetRole-gated lookup of versioned secret bundles inside verified sub-runs.

Each family is opted into independently in an application’s execution manifest. A pure calculation tool names zero capability families; a local storage tool names only block and clock and has no link-time or runtime path to tcp, udp, or secret.

Flat binary frames and honest failure states

Pointers and heap graphs never cross the host boundary. Every Layer 1 call encodes a self-contained binary request frame (LC\x02) and receives a self-contained binary reply frame whose codecs are checked in the Lean kernel to round-trip without ambiguity and reject trailing or malformed bytes.

Just as importantly, the host never throws an untyped exception across the boundary. Every reply frame starts with an explicit status (ok, refused, failed, or malformed), and failed replies carry physical certainty flags right in the contract state:

  • Uncertain disk writes (DirtyUncertain): If a writeBlock or flush fails due to a timeout or device error, you cannot assume the sector stayed untouched. The block contract marks that volume dirty and refuses any further writes until a flush succeeds or BlockStore runs crash recovery from its alternating superblocks.
  • Uncertain network sends (NotSent vs. Sent): A tcp send failure reports whether zero bytes left the host (NotSent, safe for automatic retry) or bytes may have reached the peer (Sent), preserving retry safety all the way up through TLS 1.3 and HTTP/1.1.

Keeping raw secrets out of application code (exchange and serve)

How does an application sign an AWS S3 request or terminate TLS 1.3 with a private key without letting general application code read raw secret bytes (and accidentally log or leak them)?

Lema pairs the secret tag with two opaque driver sub-runs and mandatory static bytecode checks:

  1. Outbound requests (exchange): Your application asks for a signed (SigV4) or bearer HTTPS exchange. Instead of handing secret bytes to your application, the VM runner executes the proved SecretExchange driver in an isolated sub-run. The driver completes TLS 1.3, checks the server’s X.509 certificate against the secret bundle’s allowed origin, signs and sends the request, and returns only the HTTP response to your code.
  2. Inbound servers (serve): An HTTPS server issues serve and respond, while the isolated ServeVerified driver holds the server’s signing key, terminates TLS 1.3, verifies client signatures, and hands the application only authenticated request data.
  3. Mandatory release gates: Any closure touching secret must pass static ConstantTime (no secret-dependent branches or memory indices) and SecretFlow checks on its compiled VM IR before lema run --release will start it (Checked, tested and assumed).

Next, read The proved storage and HTTPS stack (#8) to see how BlockStore, cryptography, TLS 1.3, HTTP/1.1, and S3 are built and proved on top of these six capabilities.

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