Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Compact Book

The resource smart developers recommend when someone asks how to learn Compact properly.

A structured, example-driven guide for understanding Compact, the domain-specific language that compiles to zero-knowledge circuits on the Midnight blockchain. Built for developers who learn through code, mental models, and working examples, not documentation dumps.

Inspired by the Rust-Notes repo by Tushar Pamnani.


What is Midnight?

Midnight is a data-protection blockchain with two parallel worlds:

WorldWhereWho sees itContains
PublicOn-chainEveryoneProofs, contract code, public data
PrivateLocal storageOnly the ownerSensitive data, secrets

The bridge between them is zero-knowledge cryptography. Computations happen locally on private data; only a proof of correct execution goes on-chain. Validators verify the proof without ever seeing the inputs.

This is the key insight: Midnight doesn’t hide computation. It proves computation happened correctly without revealing what you computed on.


What is Compact?

Compact is Midnight’s smart contract language. It looks like TypeScript, but every program compiles to a finite zero-knowledge circuit.

You write Compact (TypeScript-like)
        ↓
Compiler produces ZK circuits + TypeScript bindings
        ↓
Your DApp calls circuits → generates proof → submits to chain

The compiler handles all the cryptographic machinery. You write business logic.


Who is this for?

BackgroundWhy this works for you
TypeScript developerSyntax is familiar. Mental models are different, but you already understand types and functions.
Solidity developerYou understand state and contracts. The privacy model is the new layer.
Rust developerYou’re comfortable with strong types, ownership, and bounded computation. Compact will feel natural.
ZK newcomerYou don’t need to understand circuits. You write code that compiles to them.

You should be comfortable with at least one typed language before starting. Compact builds on that.


Learning Path

Start at the top. Each note assumes you understood the previous one.

#TopicWhat you learnLevel
00What is CompactWhat Compact is, what it compiles to, why boundedness matters🟢 Beginner
01Modules and ImportsOrganizing code across files🟢 Beginner
02Setting Up the CompilerInstall the toolchain, compile your first contract🟢 Beginner
03VS Code ExtensionSyntax highlighting, snippets, integrated building🟢 Beginner
04Neovim SetupNeovim plugin for Compact🟢 Beginner
05Formatter UsageCode formatting, style enforcement🟢 Beginner
06Fixup UsageAutomatic code migration🟡 Intermediate
07Data TypesThe complete type system🟡 Intermediate
08CircuitsHow circuits work, why they’re not functions🟡 Intermediate
09Ledger StatePublic vs private state, the disclose() boundary🟡 Intermediate
10WitnessesHow private inputs enter circuits without touching the chain🟡 Intermediate
11Explicit DisclosureThe witness protection program, common mistakes🔴 Advanced
12Ledger ADTsMap, Set, MerkleTree, choosing the right state structure🔴 Advanced
13Standard LibraryHashing, tokens, merkle trees🔴 Advanced
14Writing A ContractThe four pieces of every contract🟡 Intermediate
15Why CompactWhy this design exists, what problems it solves🟢 Beginner
16Security and Best PracticesPrivacy patterns, commitment design, common leaks🔴 Advanced
17Testing and DebuggingError reading, version management, troubleshooting🟡 Intermediate
18Example ProjectsFull working contracts with walkthroughs🟢 Beginner
19Compact GrammarSyntax rules, precedence, what the grammar enforces🟡 Intermediate
20Keywords ReferenceEvery keyword, organized by purpose🟢 Beginner

Reading order:

  • 00 → 01 → 02 → 03/04, Get started. Install tools, choose your editor.
  • 05 → 06, Set up tooling. Formatter and fixup keep code consistent.
  • 07 → 08 → 09 → 10 → 11, Core concepts. Types, circuits, privacy model.
  • 12 → 13 → 14, Advanced patterns. State structures, stdlib, writing contracts.
  • 15 → 16, Design & security. Philosophy and best practices.
  • 17, Testing. Debugging and version management.
  • 18, Examples. Put it all together with working code.
  • 19 → 20, Reference. Grammar and keywords when you need them.

What Makes These Notes Different

Most documentation explains what. These notes explain why and what it actually means.

Every note follows this structure:

  1. Intuition First, The 30-second version before the details
  2. Mental Model, How to think about it (precise, not vague)
  3. Why It Exists, The problem it solves
  4. Core Example, One clean example, then extended
  5. Under the Hood, What actually happens during compilation
  6. Common Mistakes, What you WILL get wrong (and why)
  7. Comparison Layer, How it maps to Rust, Solidity, TypeScript
  8. Practical Usage, Where it appears in real contracts
  9. Quick Recap, Memory anchors

Cross-links connect concepts.


Three Core Mental Models

Before you write any code, understand these:

1. A circuit is a constraint system, not a function. It doesn’t “run” in the normal sense. It declares relationships between inputs and outputs that must hold. The proof proves those relationships held.

2. The ledger is public. Private data stays local. export ledger fields are on-chain and readable by everyone. Private data lives in witnesses and never touches the chain, only a proof that you operated on it correctly.

3. disclose() is a compile-time annotation, not encryption. It tells the compiler “I am intentionally making this public.” The compiler prevents accidental disclosure. There’s no runtime cost and no magic.


The Five Program Elements

Every Compact contract is built from these:

ElementKeywordPurpose
Public stateexport ledgerOn-chain, readable by all
Private inputswitnessCallbacks the DApp provides
LogiccircuitCompiles to ZK circuits
InitializationconstructorRuns once on deploy
Organizationmodule / importNamespace and file management

Resources


Contributing

Found a misconception that should be addressed? A better analogy? A mistake?

Open an issue or PR. This repo improves through developer feedback.