TRZK compiles Lean specifications of zero-knowledge primitives to optimized low-level implementations via equality saturation with hardware-aware cost models.
This cut is a deliberately minimal end-to-end pipeline. Future growth happens on a working foundation.
- Expression language:
ArithExpr = Const BabyBear | Var Nat | Add | Sub | Neg | Mul - Value type: BabyBear field (
p = 2³¹ − 2²⁷ + 1) in canonical residue form - Rewrite rules:
e + 0 → e,x + (−x) → 0,e * 1 → e,e * 0 → 0,x − x → 0,−(−x) → x - Rust emitter targeting
u32BabyBear arithmetic withu64intermediates - CLI
trzkthat turns a.leanspec file into a Rust function - Integration test with crafted and fuzz vectors
Multiplicative inverse, multiple representations (Montgomery), multiple fields (Goldilocks), exponentiation, NTT, ZK primitives. All deferred to future iterations.
- Field / representation generalization
- Matrix operations
- NTT support
- optisat / LambdaSat — verified e-graph + saturation.
- Lean 4.26.0 (see
lean-toolchain). - Mathlib
v4.26.0.
# Build
lake build
# Compile a spec to Rust
cat > /tmp/spec.lean <<'EOF'
open TRZK (ArithExpr)
def spec : ArithExpr := .add (.var 0) (.const 0)
EOF
./.lake/build/bin/trzk /tmp/spec.lean --output /tmp/out.rs
cat /tmp/out.rs
# → … helper preamble …
# pub fn arith_spec(x0: u32) -> u32 { x0 }
# Run the integration test
./integration_tests/run.sh --op add0
./integration_tests/run.sh --op add0 --fuzz -n 1000Four stages, each with one owner:
- Parse — Lean itself. The user writes
def spec : ArithExpr := ...in a.leanfile; the CLI runs it vialake env lean --run. - Saturate — optisat. Our
ArithOptype plugs in viaNodeOpsandExtractabletypeclass instances. - Emit — hand-rolled Rust emitter (
TRZK/Emit.lean). - Execute — scripted harness (
integration_tests/).
See docs/pipeline.md for details.
| Path | Purpose |
|---|---|
TRZK/ArithExpr.lean |
AST users write specs in |
TRZK/ArithOp.lean |
e-graph node + optisat typeclass instances |
TRZK/Field/BabyBear.lean |
BabyBear field type, instances, primality |
TRZK/Rule.lean |
rewrite rule registry |
TRZK/Pipeline.lean |
embed + optimize |
TRZK/Emit.lean |
Rust emitter |
Compile.lean |
trzk CLI |
Tests/*.lean |
#guard tests |
integration_tests/ |
end-to-end harness |
docs/ |
pipeline, saturation, onboarding, glossary |
See LICENSE.