Path: blob/main/frontier/10-snarks-starks/sage/10a-arithmetic-circuits.ipynb
483 views
Notebook 10a: Arithmetic Circuits
Module 10. SNARKs and STARKs
Motivating Question. You want to prove that you know a secret input such that , without revealing . Before we can build such a proof system, we need a way to represent any computation as a structured mathematical object. Arithmetic circuits are that object, they turn programs into addition and multiplication gates over a finite field. How does become a circuit?
Prerequisites. You should be comfortable with:
Finite fields and arithmetic in them (Module 01–02)
Polynomials over finite fields (Module 02)
Learning objectives. By the end of this notebook you will be able to:
Represent a computation as a directed acyclic graph (DAG) of addition and multiplication gates.
Flatten an algebraic expression into a sequence of elementary gates.
Evaluate a circuit by propagating wire values.
Understand why arithmetic circuits are the foundation of SNARKs and STARKs.
1. What Is an Arithmetic Circuit?
Bridge from Module 02. In Module 02, we did arithmetic in polynomial rings over finite fields. An arithmetic circuit formalizes exactly this: each gate performs one field operation (add or multiply), and wires carry field elements. And in Module 09 (Fiat-Shamir), we saw how interactive proofs become non-interactive, SNARKs apply the same transform to circuit-based proofs, turning them into a single non-interactive argument.
An arithmetic circuit over a field is a directed acyclic graph (DAG) where:
Input wires carry values from (public inputs, private inputs, constants).
Gates perform either addition () or multiplication () on two input wires.
Output wires carry the gate's result to subsequent gates or the final output.
Every polynomial computation, and indeed every computation that can be expressed over a finite field, can be written as an arithmetic circuit.
2. A Concrete Circuit:
Let's trace how the computation becomes a circuit.
Step 1: Identify the operations.
(multiplication gate)
(multiplication gate)
(addition gate)
(addition gate)
Step 2: Draw the circuit.
Four gates, two multiplications, two additions. This is the flattened form of .
3. Evaluating the Circuit
Given an input , we propagate values through the circuit by evaluating each gate in order.
Checkpoint 1. Every wire in the circuit carries a field element. The complete set of wire values, input wires, intermediate wires, and output wires, is called the witness (or trace). This witness is what a prover will later claim to know, and what a verifier will check.
4. Flattening: From Expressions to Circuits
Any algebraic expression can be flattened into a circuit where each gate has exactly one operation. The key rule: each gate output gets its own wire name.
Let's flatten a more complex expression: .
5. Circuit Size and Depth
Two key metrics for an arithmetic circuit:
| Metric | Definition | Why It Matters |
|---|---|---|
| Size | Total number of gates | Determines proof generation time |
| Depth | Longest path from input to output | Affects parallelizability |
| Multiplicative size | Number of multiplication gates only | Key for R1CS/QAP (next notebooks) |
Addition gates are "free" in many proof systems (they can be absorbed into the constraint structure). Multiplication gates are the expensive ones.
Checkpoint 2. The number of multiplication gates in a circuit directly determines the number of constraints in the R1CS representation, and hence the size of the SNARK proof's trusted setup and proving time. Minimizing multiplication gates is a key optimization in zero-knowledge circuit design.
6. Constants and Public vs. Private Inputs
In a zero-knowledge setting, wires are classified as:
| Type | Who knows it | Example |
|---|---|---|
| Public input (instance) | Everyone | The claimed output |
| Private input (witness) | Prover only | The secret such that |
| Constant | Hardcoded | The 5 in |
| Intermediate | Prover computes | Wire values |
The prover's goal: convince the verifier that there exists a private input making the circuit output equal the public input, without revealing the private input.
7. More Complex Circuits
Let's build a circuit for a more interesting computation: checking that a Sudoku cell constraint holds.
Example: Prove that over . One way: prove that there exists such that . (If then and no such exists.)
8. Boolean Circuits vs. Arithmetic Circuits
You might be familiar with Boolean circuits (AND, OR, NOT gates). Arithmetic circuits are their field-element generalization.
| Boolean Circuits | Arithmetic Circuits |
|---|---|
| Wires carry bits (0 or 1) | Wires carry field elements |
| AND, OR, NOT gates | ADD (), MUL () gates |
| Used in traditional CS complexity | Used in SNARKs/STARKs |
We can enforce Boolean constraints in an arithmetic circuit: to ensure wire is Boolean, add the constraint . This forces .
Misconception alert. "Arithmetic circuits can only handle numbers." Wrong! By enforcing Boolean constraints on wires, you can embed any Boolean computation inside an arithmetic circuit. SHA-256, AES, comparisons, if-else, all can be expressed as arithmetic circuits, though the circuit size may be large.
9. Circuit for a Hash Preimage Proof
A classic ZK application: "I know a preimage such that ."
Real hash functions have massive circuits (SHA-256 ≈ 25,000 gates). Let's use a toy "hash" to illustrate the concept: .
Crypto foreshadowing. In real ZK systems:
Zcash uses a circuit for the Pedersen hash and EdDSA signature verification (~100K gates)
zkEVM projects encode the entire Ethereum Virtual Machine as an arithmetic circuit (~millions of gates)
The circuit is compiled once; then any execution can be proven
10. Exercises
Exercise 1 (Worked): Flatten and Evaluate
Problem. Flatten into an arithmetic circuit. Evaluate it at over .
Solution:
Exercise 2 (Guided): Multi-Variable Circuit
Problem. Flatten into a circuit. Evaluate at over .
Fill in the TODOs:
Exercise 3 (Independent): Inequality Circuit
Problem.
Build a circuit that proves (i.e., is a square root of ).
Identify which wires are public (verifier sees ) and private (prover knows ).
Test with . Find both square roots and show the circuit accepts both.
Try , does a square root exist? (Hint: check if is a quadratic residue mod .)
Summary
| Concept | Key Fact |
|---|---|
| Arithmetic circuit | DAG of and gates over a field |
| Flattening | Any algebraic expression → sequence of elementary gates |
| Wire values | Complete evaluation trace = the witness |
| Circuit size | Number of gates; multiplicative gates matter most |
| Public vs. private | Verifier sees public inputs/outputs; prover knows all wire values |
| Boolean in arithmetic | Enforce to constrain a wire to |
Arithmetic circuits are the universal language of zero-knowledge proof systems. In the next notebook, we'll transform these circuits into a mathematical constraint system called R1CS, the first step toward building a SNARK.
Next: 10b: R1CS Constraints