🛠️ Circuit Fundamentals: R1CS & QAP

Understand the mathematical foundation of ZK circuits

Circuit Fundamentals

At their core, ZK circuits are **arithmetic circuits** composed of gates that perform addition and multiplication operations over a finite field. Unlike traditional logic circuits with AND/OR/NOT gates, ZK circuits work with finite field elements and algebraic constraints.

Every computation in a ZK-Rollup—from balance updates to signature verification—must be expressed as a circuit of these algebraic operations. The prover demonstrates they know inputs that satisfy all circuit constraints without revealing the inputs themselves.

🔢 Core Circuit Components

+
Addition Gates
Linear operations: out = a + b (cheap, can be combined)
×
Multiplication Gates
Non-linear operations: out = a * b (expensive, creates constraints)
C
Constants
Fixed values known at circuit design time
P
Public Inputs
Values known to both prover and verifier (old state root, new state root)

Interactive: Circuit Builder

Build your own arithmetic circuit by adding gates. Each gate type has different properties and constraint costs.

Select Gate Type

Adds two inputs: out = a + b

Your Circuit

0 gates • ~0 constraints
No gates added yet. Select a gate type and click "Add" to build your circuit.

Finite Field Arithmetic

All operations happen in a **finite field** (typically the BN254 curve's scalar field with prime p ≈ 2^254). This means all arithmetic is modular—results "wrap around" at the field size.

Field Prime (BN254):
21888242871839275222246405745257275088548364400416034343698204186575808495617
Addition (mod p)
(a + b) mod p
Example: (p-1) + 2 = 1
Multiplication (mod p)
(a × b) mod p
Example: 3 × 5 = 15

Circuit Optimization Techniques

🎯 Linear Combination

Multiple additions can be combined into one constraint:

a + b + c = (a + b) + c → single constraint

⚡ Minimize Multiplications

Each multiplication gate creates a constraint. Prefer additions when possible:

x + x + x (cheap) vs 3 * x (creates constraint)

🔄 Reuse Intermediate Values

Store and reuse computed values instead of recomputing:

temp = a * b
out1 = temp + c
out2 = temp + d

💡 Key Insights

  • ZK circuits are arithmetic circuits over finite fields (not boolean logic)
  • Multiplication gates are expensive—they create R1CS/Plonk constraints
  • Addition gates are cheap—multiple can be combined linearly
  • Circuit size determines proving time: fewer gates = faster proofs