FORMAL SPECIFICATION

ENC Protocol

Encode, Encrypt, Enclave — a formally verified protocol for log-based, verifiable, sovereign data structures.

7Layers
66Files
14,869Lines
162Theorems
What

A Formally Verified Database Protocol

ENC (Encode, Encrypt, Enclave) is a protocol for building log-based, verifiable, sovereign data structures with role-based access control. Every component — from cryptographic primitives to React frontends — is specified in Lean 4 and proven correct.

Core idea: A single Enclave structure (7 fields) determines the complete server, SDK, and frontend for any application. Three layers, one source of truth, zero redundancy.
Architecture

Seven Layers

Each layer composes from the ones below. No cycles. Every layer has a formal spec and a corresponding Lean implementation.

1

FTA — Theory

Abstract math. Three primitives (Values, Keys, Time), three functions (reduce, render, effect), 48 theorem obligations.

2

Core — Protocol

Crypto, SMT, CT, Events, RBAC, Validation, Node, SDK. 22 files, 3,361 lines.

3

Middleware

ECDH (1-to-1 encryption) and MLS (group ratchet). 2 files, 361 lines.

4

Applications

Registry, Timeline, DM, Chat, Messenger. Schema, SDK, DataView, State, Components per app.

5

Typeclasses

6 classes × 4 instances. 43 composition theorems proving all apps satisfy the theory.

6–7

Bridge + Codegen

Theory↔Protocol mapping, React DSL, and the three codegen targets (Server, Client, Frontend).

Data Flow

One Enclave, Three Targets

Information is defined once and flows downward. The client does not define its own type — ClientView = Enclave.

Enclave (7 fields) Server: allRoutes, ddl, handlers
Enclave (same) Client: reads, writes, protocol
Enclave + AppDef + CSS Frontend: components, hooks, styles

37 axiomatized IO methods across three runtimes. Everything else — 14,869 lines — is derived, proven, or structurally enforced.

RuntimeMethodsImplementation~JS
Server12Cloudflare DO API50 lines
Client10fetch + WebSocket30 lines
Frontend8React hooks + DOM20 lines
Crypto7noble-curves10 lines
Total37~110
Applications

Four Apps, One Pattern

ServiceSchemaDataViewEncryptedReadsWritesComponents
Registrystatic538
Timelinestatic7712
ChatstaticMLS059
DMstaticECDH029

Messenger (20 components) composes DM + Chat + Timeline at the frontend layer. 58 total components, all specified in Lean.

Verification

162 Theorems

LayerTheoremsKey results
FTA10Meta-theorems on the abstract framework
Core20SMT soundness, CT inclusion, expiry rejection
Apps65Schema well-formedness, all 17 writes reduce to createCommit
Composition43C1–C6 conditions satisfied for all 4 applications
Codegen24Route counts, capability matrix, read derivation
Guide

Reading Order

Start here. Then read the theory, then the protocol, then the architecture.

For Lean beginners: Lean Intro first, then Lean Proofs.

Codebase

66 Lean Files

LayerFilesLinesWhat
1 FTA12946Abstract theory — pure math, no ENC knowledge
2 Core223,361Crypto, SMT, CT, Events, RBAC, Validation, Node, SDK
3 Middleware2361ECDH (1-to-1) + MLS (group) encryption
4 Apps197,182Registry, Timeline, DM, Chat, Messenger
5 Typeclasses31,2156 classes × 4 instances, 43 composition theorems
6 Bridge + UI2967Theory↔Protocol mapping, React component DSL
7 Codegen3595Server + Client + Frontend (three duals)
6614,869

ENC PROTOCOL · 66 FILES · 14,869 LINES · 162 THEOREMS