- pen-and-paper worked examples in all 12 chapters, using the REAL constants throughout: 2^-64 waiting-time arithmetic, headroom budgets, hand type-checking, rfl traces, full goal-state boards, the column-sum audit at 2^54, inverting 19 mod p via Euclid, the x19 fold at real weights, denoting p itself (telescope), the 16p audit (8 fails by 151), the 254+11 inversion-chain bookkeeping, the substitution test, sizing the 28-vs-1000 extraction, cofactor/torsion arithmetic, and the full Bernstein-Lange completeness derivation - CORRECTNESS FIX: ch7 asserted a false factorization of p-1; replaced with the computationally verified p-1 = 2^2 * 3 * 65147 * Q (Q 71-digit prime), witness w=2 verified for all four Pratt conditions - every chapter's exercises now followed immediately by 'Solutions and pathways' (pathway first, then answer), incl. new exercises - NEW Interlude: a complete two-clause verification done entirely by hand, then mapped line-by-line onto the compiled Lean proof - NEW appendices: A pen-and-paper toolkit (8 recipe cards + drills + answers), B guided walkthroughs of every exercise-file hole, C tour of the real repositories; plus glossary, instructor notes, 13-week plan - preamble: worked-example box, solution macros, math-safe inline code Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> |
||
|---|---|---|
| chapters | ||
| exercises | ||
| solutions | ||
| .gitignore | ||
| lakefile.toml | ||
| lean-toolchain | ||
| main.pdf | ||
| main.tex | ||
| preamble.tex | ||
| README.md | ||
Verifying Cryptography with Lean 4
A hands-on curriculum for undergraduates with zero formal-verification
background — from 1 + 1 = 2 to reading (and extending) real,
machine-checked proofs that production elliptic-curve code is correct.
This is the educational companion to a family of verification projects in which the arithmetic core of Ed25519 (from curve25519-dalek and three production forks) and the Pasta curves' field layer were machine-checked in Lean 4 against models extracted from the actual Rust sources:
| Companion project | What is verified there |
|---|---|
| dalek-ed25519-verified | field 𝔽ₚ + Edwards group law + scalar foundations, upstream dalek |
| anza-ed25519-verified | same layers, Solana's fork, its own extraction |
| risc0-ed25519-verified | same layers, RISC Zero's fork |
| betrusted-ed25519-verified | same layers, Betrusted's fork |
| pasta-pallas-verified | Pallas modulus primality (Lucas/Pratt), Montgomery foundations |
| formal-verification-control | the method: invariants, terrain map, failure map, tooling |
The book
main.pdf — twelve chapters + interlude + three
appendices, 106 pages, full color, built with LaTeX/TikZ from the sources
in this repo. No prior Lean or formal methods assumed; high-school algebra
and a little programming suffice.
- Why Verify? — the carry bug testing cannot find
- Meet Lean — programs, types, inductive data
- Propositions as Types — Curry–Howard: proofs are programs
- Tactics — proving as a dialogue with the goal state
- Numbers and Automation —
omega,ring,norm_num,decide, and thesimpdiscipline - Modular Arithmetic — clock worlds, fields, why 2²⁵⁵ − 19
- Primality Certificates — convincing a paranoid kernel a 77-digit number is prime
- From Rust to Lean — the Charon/Aeneas extraction pipeline
- The Denotation Bridge — the commuting square at the heart of it all — Interlude — a complete verification, entirely by hand, then re-enacted in Lean line by line
- Verifying a Field — the full campaign, told honestly (including the crash)
- Honesty and Axioms —
#print axioms, hollow certificates, trusted bases - The Pyramid — group law, scalars, signatures, and where you come in
Appendices: A — the pen-and-paper toolkit (recipe cards with drills); B — guided walkthroughs of every exercise-file hole; C — a tour of the real repositories. Plus a glossary and a thirteen-week course plan.
The didactic machinery, deliberately heavy:
- Pen-and-paper worked examples in every chapter — computations with the real constants (2²⁵⁵−19, radix 2⁵¹, the fold constant 19, the actual 254-squaring inversion chain, the true Pratt tree p−1 = 2²·3·65147·Q), because the real numbers carry the real arguments. Highlights: inverting 19 modulo the 77-digit prime in five lines of Euclid; a fully hand-checked primality certificate for 97; the ×19 fold derived at the real weights; the 16p subtraction constant audited to the bit (8 fails by 151); the complete Bernstein–Lange completeness chain.
- Solutions immediately after every exercise set — each one leads with the pathway (how a person finds the answer) before the answer itself.
- Boxed Big idea / Try it / Pitfall / Aha / Checkpoint elements, TikZ figures throughout.
Everything the book claims about the companion projects reflects their actual, auditable state — including open frontiers.
The exercises (they run!)
exercises/ChNN.lean are working files with sorry holes;
solutions/ChNN.lean are complete. Every solution file compiles with
zero errors against the pinned toolchain (Lean v4.30.0-rc2, Mathlib
5450b53e); solutions to proof exercises contain no sorry.
Setup (one-time, ~5 min + Mathlib cache download):
# 1. install elan (Lean version manager) if you haven't:
curl https://elan.lean-lang.org/elan-init.sh -sSf | sh
# 2. fetch the Mathlib build cache (do NOT build Mathlib yourself):
cd verifying-crypto-with-lean
lake exe cache get
# 3. open the folder in VS Code with the "Lean 4" extension, or:
lake build Solutions # compiles all solution files as a check
Chapters 2–4 need no Mathlib at all — you can start them with any Lean 4 install while the cache downloads.
Building the book
Any TeX Live ≥ 2023 with tikz, tcolorbox, listings, lmodern:
pdflatex main.tex && pdflatex main.tex # twice for the TOC
Honesty ledger
In the spirit of Chapter 11:
- All
solutions/*.leanwere compiled (and their#evaloutputs checked against their comments) at authoring time with the pinned versions above. - Exercise templates compile with
sorrywarnings only. - The book's claims about the companion projects (what is proven, what is frontier) mirror those repos' own READMEs and TRUSTED-BASE ledgers at the time of writing; the repos, not this book, are the source of truth.
- The PDF in the repo is built from the committed sources by the command above; rebuild it yourself if you don't trust binaries (good instinct).