A zero-knowledge circuit compiler that transforms high-level programs into arithmetic circuits and R1CS constraint systems.
- High-level language with public/private inputs and assertions
- SSA intermediate representation
- Circuit optimization (constant folding, dead code elimination)
- R1CS constraint system generation
- Witness calculation and verification
program = statement*
statement = "public" IDENT
| "private" IDENT
| "const" IDENT "=" NUMBER
| "let" IDENT "=" expr
| "return" expr
| "assert" expr "==" expr
expr = term ("+" term | "*" term)*
term = IDENT | NUMBER | "(" expr ")"
# Compile only
cargo run examples/simple.zk
# Compile and execute
cargo run examples/simple.zk inputs/inputs.toml
Generates:
circuit/simple.json
- Circuit gatescircuit/simple.r1cs
- R1CS constraintscircuit/simple.witness
- Execution trace
The compiler pipeline:
- Lexer → tokens
- Parser → AST
- SSA conversion → intermediate form
- Optimization → constant folding, dead code elimination
- Circuit generation → arithmetic gates
- R1CS generation → constraint matrix
- Witness calculation → execution with inputs
Uses i32
arithmetic for simplicity. Production ZK requires finite field arithmetic but my aim with this project was to explore the different compiler techniques.
Check examples/
for test programs showing language features and optimizations. The current example files end with .zk
, the files ending in .mc
were used for early testing.