Professional Documents
Culture Documents
Lecture 12
Lecture 12
2 ZKP MOOC
What is Scroll?
An EVM-equivalent zk-Rollup
3 ZKP MOOC
Outline
4 ZKP MOOC
The diagram of Layer 1 blockchain
5 ZKP MOOC
The diagram of Layer 1 blockchain
root
6 ZKP MOOC
The diagram of Layer 1 blockchain
Layer 1
𝑇𝑋!
𝑇𝑋"
……
root
𝑇𝑋#
7 ZKP MOOC
The diagram of Layer 1 blockchain
Layer 1
𝑇𝑋!
𝑇𝑋"
……
root
𝑇𝑋#
8 ZKP MOOC
The diagram of Layer 1 blockchain
Layer 1
𝑇𝑋!
𝑇𝑋"
……
root
𝑇𝑋#
9 ZKP MOOC
The diagram of Layer 1 blockchain
Layer 1
𝑇𝑋!
• Secure
𝑇𝑋"
• Decentralized
……
• Expensive L
𝑇𝑋#
• Slow L
10 ZKP MOOC
Zk-Rollup
Layer 1
Layer 2 data 𝝅
𝑇𝑋!
𝑇𝑋"
…… prover
𝑇𝑋#
11 ZKP MOOC
However, …
Layer 1
Layer 2 data 𝝅
𝑇𝑋!
𝑇𝑋"
…… prover
𝑇𝑋#
12 ZKP MOOC
However, …
Layer 1
𝑇𝑋! 𝑇𝑋!
…… ……
𝑇𝑋# 𝑇𝑋#
13 ZKP MOOC
Scroll: a native zkEVM solution
Layer 1
Layer 2 data 𝝅
𝑇𝑋!
• Developer friendly
𝑇𝑋"
• Composability
…… zkEVM
𝑇𝑋#
14 ZKP MOOC
Scroll: a native zkEVM solution
Layer 1
Layer 2 data 𝝅
𝑇𝑋!
• Developer friendly
𝑇𝑋"
• Composability
…… zkEVM • Hard to build L
𝑇𝑋#
• Large proving overhead L
15 ZKP MOOC
Scroll: a native zkEVM solution
Layer 1
Layer 2 data 𝝅
𝑇𝑋!
• Polynomial commitment
𝑇𝑋"
• Lookup + Custom gate
…… zkEVM • Hardware acceleration
𝑇𝑋#
• Recursive proof
16 ZKP MOOC
zkEVM flavors (by Justin Drake)
• Language level
Transpile an EVM-friendly language (Solidity or Yul) to a SNARK-friendly VM which differs from the
EVM. This is the approach of Matter Labs and Starkware.
• Bytecode level
Interpret EVM bytecode directly, though potentially producing different state roots than the EVM, e.g.
if certain implementation-level data structures are replaced with SNARK-friendly alternatives. This is
the approach taken by Scroll, Hermez, and Consensys.
• Consensus level
Target full equivalence with EVM as used by Ethereum L1 consensus. That is, it proves validity of L1
Ethereum state roots. This is part of the "zk-SNARK everything" roadmap for Ethereum.
17 ZKP MOOC
zkEVM flavors (by Justin Drake)
• Language level
Transpile an EVM-friendly language (Solidity or Yul) to a SNARK-friendly VM which differs from the
EVM. This is the approach of Matter Labs and Starkware.
• Bytecode level
Interpret EVM bytecode directly, though potentially producing different state roots than the EVM, e.g.
if certain implementation-level data structures are replaced with SNARK-friendly alternatives. This is
the approach taken by Scroll, Hermez, and Consensys.
• Consensus level
Target full equivalence with EVM as used by Ethereum L1 consensus. That is, it proves validity of L1
Ethereum state roots. This is part of the "zk-SNARK everything" roadmap for Ethereum.
18 ZKP MOOC
Outline
19 ZKP MOOC
The workflow of zero-knowledge proof
𝒘
𝒘11 𝒘
𝒘𝟐𝟐 𝒘
𝒘𝟑𝟑 𝒘
𝒘𝟒𝟒 𝒘
𝒘𝟓𝟓 𝒘𝒏 − ……
𝟏 𝒘𝒏 𝒘𝒏 − 𝟏 𝒘𝒏
Let’s start with R1CS
𝒘
𝒘11 𝒘
𝒘𝟐𝟐 𝒘
𝒘𝟑𝟑 𝒘
𝒘𝟒𝟒 𝒘
𝒘𝟓𝟓 𝒘𝒏 − ……
𝟏 𝒘𝒏 𝒘𝒏 − 𝟏 𝒘𝒏
𝑎# 𝑤# + ⋯ + 𝑎$ 𝑤$ ∗ 𝑏# 𝑤# + ⋯ + 𝑏$ 𝑤$ == 𝑐# 𝑤# + ⋯ + 𝑐$ 𝑤$
Let’s start with R1CS
𝒘
𝒘11 𝒘
𝒘𝟐𝟐 𝒘
𝒘𝟑𝟑 𝒘
𝒘𝟒𝟒 𝒘
𝒘𝟓𝟓 𝒘𝒏 − ……
𝟏 𝒘𝒏 𝒘𝒏 − 𝟏 𝒘𝒏
𝑎# 𝑤# + ⋯ + 𝑎$ 𝑤$ ∗ 𝑏# 𝑤# + ⋯ + 𝑏$ 𝑤$ == 𝑐# 𝑤# + ⋯ + 𝑐$ 𝑤$
𝒘
𝒘11 𝒘
𝒘𝟐𝟐 𝒘
𝒘𝟑𝟑 𝒘
𝒘𝟒𝟒 𝒘
𝒘𝟓𝟓 𝒘𝒏 − ……
𝟏 𝒘𝒏 𝒘𝒏 − 𝟏 𝒘𝒏
𝒊𝒏𝒑𝒖𝒕𝟎 𝒊𝒏𝒑𝒖𝒕𝟏 𝒊𝒏𝒑𝒖𝒕𝟐 𝒗𝒂𝟏 𝒗𝒃𝟏 …… 𝒗𝒄𝟏 𝒗𝒅𝟏
𝑎# 𝑤# + ⋯ + 𝑎$ 𝑤$ ∗ 𝑏# 𝑤# + ⋯ + 𝑏$ 𝑤$ == 𝑐# 𝑤# + ⋯ + 𝑐$ 𝑤$
I know a vector {input, va, vb, vc, …} that satisfies all those constraints
Plonkish Arithmetization
a0 a1 a2 a3 a4 T0 T1 T2 T3 T4
27 ZKP MOOC
Plonkish Arithmetization
a0 a1 a2 a3 a4 T0 T1 T2 T3 T4
28 ZKP MOOC
Plonkish Arithmetization – Custom gate
a0 a1 a2 a3 a4 T0 T1 T2 T3 T4
29 ZKP MOOC
Plonkish Arithmetization – Custom gate
a0 a1 a2 a3 a4 T0 T1 T2 T3 T4
30 ZKP MOOC
Plonkish Arithmetization – Custom gate
a0 a1 a2 a3 a4 T0 T1 T2 T3 T4
31 ZKP MOOC
Plonkish Arithmetization – Custom gate
a0 a1 a2 a3 a4 T0 T1 T2 T3 T4
32 ZKP MOOC
Plonkish Arithmetization – Permutation
a0 a1 a2 a3 a4 T0 T1 T2 T3 T4
33 ZKP MOOC
Plonkish Arithmetization – Lookup argument
a0 a1 a2 a3 a4 T0 T1 T2 T3 T4
34 ZKP MOOC
Plonkish Arithmetization – Lookup argument
a0 a1 a2 a3 a4 T0 T1 T2 T3 T4
35 ZKP MOOC
Plonkish Arithmetization – Lookup argument
a0 a1 a2 a3 a4 T0 T1 T2 T3 T4
36 ZKP MOOC
Plonkish Arithmetization – Lookup argument
a0 a1 a2 a3 a4 T0 T1 T2 T3 T4
37 ZKP MOOC
Plonkish Arithmetization – Constraints
a0 a1 a2 a3 a4 T0 T1 T2 T3 T4
38 ZKP MOOC
How should we choose “front-end”?
Computation
𝑆𝑡𝑎𝑡𝑒 𝑟𝑜𝑜𝑡
Ethereum Virtual Machine (EVM)
Stack
𝑇𝑋!
State Machine
Memory
𝑇𝑋"
……
𝑇𝑋#
Storage bytecode
How should we choose “front-end”?
Computation
• EVM word size is 256bit
𝑆𝑡𝑎𝑡𝑒 𝑟𝑜𝑜𝑡
Ethereum Virtual Machine (EVM)
• Efficient range proof
• EVM has zk-unfriendly opcodes
Stack
State Machine • Efficient way to connect circuits
𝑇𝑋!
Memory
• Read & Write consistency
𝑇𝑋"
……
• Efficient mapping
Computation
• EVM word size is 256bit
𝑆𝑡𝑎𝑡𝑒 𝑟𝑜𝑜𝑡
Ethereum Virtual Machine (EVM)
• Efficient range proof
• EVM has zk-unfriendly opcodes
Stack
State Machine • Efficient way to connect circuits
𝑇𝑋!
Memory
• Read & Write consistency
𝑇𝑋"
……
• Efficient mapping
Computation
• EVM word size is 256bit
𝑆𝑡𝑎𝑡𝑒 𝑟𝑜𝑜𝑡
Ethereum Virtual Machine (EVM)
• Efficient range proof
• EVM has zk-unfriendly opcodes
Stack
State Machine • Efficient way to connect circuits
𝑇𝑋!
Memory
• Read & Write consistency
𝑇𝑋"
……
• Efficient mapping
Transaction:
{
from: "0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8",
to: "0xac03bb73b6a9e108530aff4df5077c2b3d481e5a",
gasLimit: "21000", maxFeePerGas: "300",
maxPriorityFeePerGas: "10", nonce: "0",
value: "10000000000"
}
43 ZKP MOOC
What you need to prove
EVM bytecode
State Stack
Machine Memory Storage
Transaction:
{
from: "0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8",
to: "0xac03bb73b6a9e108530aff4df5077c2b3d481e5a",
gasLimit: "21000", maxFeePerGas: "300",
maxPriorityFeePerGas: "10", nonce: "0",
value: "10000000000"
}
44 ZKP MOOC
What you need to prove
45 ZKP MOOC
What you need to prove
spec
}
zkEVM
46 ZKP MOOC
What you need to prove
spec
}
zkEVM
47 ZKP MOOC
What you need to prove
spec
}
prove
World State (t+1)
input data Root’
zkEVM
48 ZKP MOOC
EVM circuit
Step 1
Execution trace
step opcode
1 PUSH1 80 Step 2
2 PUSH1 40
3 ADD
... ...
Step 3
... ...
n RETURN
...
Step n
49 ZKP MOOC
EVM circuit
Step 1
Execution trace
step opcode
1 PUSH1 80 Step 2
2 PUSH1 40
3 ADD step context
...
Step n
50 ZKP MOOC
EVM circuit
• Step context
Step 1
Execution trace • Codehash
step opcode • Gas left
1 PUSH1 80 Step 2 • Program counter, Stack pointer
2 PUSH1 40
Codehash gas PC SP root
3 ADD
... ... case switch
... ...
n RETURN opcode specific witness
...
Step n
51 ZKP MOOC
EVM circuit
• Step context
Step 1
Execution trace • Codehash
step opcode • Gas left
1 PUSH1 80 Step 2 • Program counter, Stack pointer
2 PUSH1 40
3 ADD Codehash gas PC SP root • Case switch
... ...
sADD sSUB sMUL …… …… ……
• Select opcodes & error cases
... ...
sErr1 sErr2 sErr3 …… …… ……
• Exactly one is switched on
n RETURN opcode specific witness
...
Step n
52 ZKP MOOC
EVM circuit
• Step context
Step 1
Execution trace • Codehash
step opcode • Gas left
1 PUSH1 80 Step 2 • Program counter, Stack pointer
2 PUSH1 40
3 ADD Codehash gas PC SP root • Case switch
... ...
sADD sSUB sMUL …… …… ……
• Select opcodes & error cases
... ...
sErr1 sErr2 sErr3 …… …… ……
• Exactly one is switched on
a_lo a_hi b_lo b_hi c_lo c_hi
n RETURN …… …… …… …… …… ……
• Opcode specific witness
...
• Extra witness used for opcodes
• i.e. operands, carry, limbs, …
Step n
53 ZKP MOOC
EVM circuit - ADD
• Step context
Step 1
sADD * (pc’ – pc – 1) == 0
sADD * (sp’ – sp – 1) == 0
Step 2 sADD * (gas’ – gas – 3) == 0
54 ZKP MOOC
EVM circuit - ADD
• Opcode specific witness
Step 1
idx tag addr R/W value
1 STACK 1023 1 …
Step 2
5 STACK 1022 0 word_a
Codehash gas PC SP root
sErr1 sErr2step
sErr3context
…… …… ……
7 STACK 1023 1 word_c
a_lo a_hi b_lo b_hi c_lo c_hi
…… …… …… …… …… …… … STACK … … …
… MEMORY … … …
Step n … STORAGE … … …
55 ZKP MOOC
EVM circuit - ADD
• Opcode specific witness
Step 1
idx tag addr R/W value
1 STACK 1023 1 …
Step 2
5 STACK 1022 0 word_a
Codehash gas PC SP root
sErr2step
sErr3context
sErr1
a_lo a_hi b_lo
…… ……
b_hi c_lo
……
c_hi
7 STACK 1023 1 word_c RAM circuit
…… …… …… …… …… …… … STACK … … …
… MEMORY … … …
Step n … STORAGE … … …
56 ZKP MOOC
EVM circuit - Hash
• Opcode specific witness
Step 1
Step 2
step context
case switch
Hash
input Hash(input) lookup
... table
Step n
57 ZKP MOOC
EVM circuit - Hash
• Opcode specific witness
Step 1
Step 2
step context
case switch
Hash
input Hash(input) lookup Hash circuit
... table
Step n
58 ZKP MOOC
The architecture of zkEVM circuits
EVM circuit
circuit constrain
lookup lookup
table
59 ZKP MOOC
The architecture of zkEVM circuits
EVM circuit
bitwise opcodes,
range check
stack/memory/etc. pc,opcode transaction & block context
SHA3
circuit constrain
lookup lookup
table
60 ZKP MOOC
The architecture of zkEVM circuits
EVM circuit
bitwise opcodes,
range check
stack/memory/etc. pc,opcode transaction & block context
SHA3
61 ZKP MOOC
The workflow of zero-knowledge proof
Plonk IOP
R1CS
+
Plonkish
KZG
AIR
The proof system for zkEVM
zkEVM
EVM Circuit
RAM Circuit
Storage Circuit
Other Circuits
The proof system for zkEVM
zkEVM
EVM Circuit
RAM Circuit
Aggregation
Circuit
Storage Circuit
Other Circuits
Two-layer architecture
zkEVM
• The first layer needs to handle large computation
• Custom gate, Lookup support (“expressive”, customized)
EVM Circuit
• Hardware friendly prover (parallelizable, low peak memory)
• The verification circuit is small
RAM Circuit
Aggregation • Transparent or Universal trusted setup
Circuit
Storage Circuit
• Some promising candidates
• Plonky2/Starky /eSTARK
• Halo2/Halo2-KZG
Other Circuits
• New IOP without FFTs (i.e. HyperPlonk, Plonk without FFT)
• If Spartan/Virgo/… (sumcheck based) or Nova can support Plonkish
Two-layer architecture
zkEVM
• The second layer needs to be verifier efficient (in EVM)
• Proof is efficiently verifiable on EVM (small proof, low gas cost)
EVM Circuit
• Prove the verification circuit of the former layer efficiently
• Ideally, hardware friendly prover
RAM Circuit
Aggregation • Ideally, transparent or universal trusted setup
Circuit
Storage Circuit
• Some promising candidates
• Groth16
• Plonk with very few columns
Other Circuits
• KZG/Fflonk/Keccak FRI (larger code rate)
Two-layer architecture
zkEVM
• The first layer is Halo2-KZG (Poseidon hash transcript)
• Custom gate, Lookup support
EVM Circuit
• Good enough prover performance (GPU prover)
• The verification circuit is “small”
RAM Circuit
Aggregation • Universal trusted setup
Circuit
Storage Circuit
• The second layer is Halo2-KZG (Keccak hash transcript)
• Custom gate, Lookup support (express non-native efficiently)
• Good enough prover performance (GPU prover)
Other Circuits
• The final verification cost can be configured to be really small
The layout
zkEVM
• The first layer needs to be “expressive”
• EVM circuit has 116 columns, 2496 custom gates, 50 lookups
EVM Circuit
• Highest custom gate degree: 9
• For 1M gas, EVM circuit needs 2^18 rows (more gas, more rows)
RAM Circuit
Aggregation
Circuit
Storage Circuit • The second layer needs to aggregate proofs into one proof
• Aggregation circuit has 23 columns, 1 custom gate, 7 lookups
• Highest custom gate degree: 5
Other Circuits
• For aggregating EVM, RAM, Storage circuits, it needs 2^25 rows
68 ZKP MOOC
The performance
zkEVM
• Our GPU prover optimization
• MSM, NTT and quotient kernel
EVM Circuit
• Pipeline and overlap CPU and GPU computation
• Multi-card implementation, memory optimization
RAM Circuit
Aggregation
Circuit • The Performance
Storage Circuit
• For EVM circuit
• CPU prover takes 270.5s, GPU prover takes 30s (9x speedup!)
Other Circuits
• For Aggregation circuit
• CPU prover takes 2265s, GPU prover takes 149s (15x speedup!)
• For 1M gas, first layer takes 2 minutes, second layer takes 3 minutes
69 ZKP MOOC
Outline
70 ZKP MOOC
Circuit - Randomness
Step 1
Step 2
step context
case switch
...
Step n
71 ZKP MOOC
Circuit - Randomness
Step 2
step context
case switch
...
Step n
72 ZKP MOOC
Circuit - Randomness
case switch
...
Step n
73 ZKP MOOC
Circuit - Randomness
...
Step n
74 ZKP MOOC
Circuit - Randomness
75 ZKP MOOC
Circuit - Randomness
76 ZKP MOOC
Circuit - Layout
Step 1
Execution trace
step opcode
1 PUSH1 80 Step 2
2 PUSH1 40
3 ADD
... ...
Step 3
... ...
n RETURN
...
Step n
77 ZKP MOOC
Circuit - Layout
Execution trace
Step 1 • The execution trace is dynamic
step opcode à enable different constraints
1 PUSH1 80 Step 2 à permutation is not fixed
2 PUSH1 40
3 ADD
à hard to use standard gates
... ...
Step 3
... ...
n RETURN
...
Step n
78 ZKP MOOC
Circuit - Layout
Execution trace
Step 1 • The execution trace is dynamic
step opcode à enable different constraints
1 PUSH1 80 Step 2 à permutation is not fixed
2 PUSH1 40
3 ADD
à hard to use standard gates
... ...
Step 3
... ...
• Better way to layout?
n RETURN
• We have 2000+ custom gates
...
79 ZKP MOOC
Circuit - Dynamic size
Step 1 Precompile
Execution trace
circuit
step opcode
1 PUSH1 80 Step 2 Hash
2 PUSH1 40
circuit
3 ADD
... ...
Step 3
... ...
n RETURN
...
Step n
80 ZKP MOOC
Circuit - Dynamic size
Step 1 Precompile
Execution trace
circuit
step opcode
1 PUSH1 80 Step 2 Hash
2 PUSH1 40
circuit
3 ADD
... ...
Step 3
... ...
n RETURN
...
Step n
81 ZKP MOOC
Circuit - Dynamic size
Step 1
Execution trace • Some bad influences
step opcode
i.e. Maximum number of Keccaks
1 PUSH1 80 Step 2
2 PUSH1 40
• i.e. Mload is more costly (more rows)
3 ADD i.e. Pay larger proving cost for padding
... ...
Step 3
... ...
n RETURN • Can we make zkEVM dynamic?
...
Step n
82 ZKP MOOC
Prover – Hardware & Algorithm
EVM Circuit
RAM Circuit
Aggregation
Circuit
Storage Circuit
Other Circuits
83 ZKP MOOC
Prover – Hardware & Algorithm
• Our prover
EVM Circuit
• GPU can make MSM & NTT really fast
Bottleneck moves to witness generation & data copy
RAM Circuit • Need large CPU memory (1TB -> 300GB+)
Aggregation
Circuit
Storage Circuit • Hardware friendly prover?
• Parallelizable & Low peak memory
84 ZKP MOOC
Prover – Hardware & Algorithm
EVM Circuit
• The first layer needs to be “expressive”
• The second layer needs to be verifier efficient (in EVM)
RAM Circuit • Should we move to smaller field?
Aggregation (Breakdown/FRI with Goldilocks, Mersenne prime)
Circuit
Storage Circuit • Should we stick to EC-based constructions?
(SuperNova, Cyclic elliptic curve with fast MSM)
Other Circuits • More options waiting for you à Reach out to us!
85 ZKP MOOC
Security – Audit & Soundness
86 ZKP MOOC
Security – Audit & Soundness
87 ZKP MOOC
Outline
88 ZKP MOOC
Applications – zkRollup
Layer 1
Layer 2 data 𝝅
𝑇𝑋!
• Prove n Txs on layer 2 are valid
𝑇𝑋"
• Verify proof in smart contract
…… zkEVM
𝑇𝑋#
89 ZKP MOOC
Application - Enshrine blockchain
Layer 1
𝑇𝑋!
𝑇𝑋"
……
𝑇𝑋#
90 ZKP MOOC
Application - Enshrine blockchain
𝝅 𝝅 𝝅 𝝅 𝝅
Layer 1
𝑇𝑋"
……
𝑇𝑋#
91 ZKP MOOC
Application - Enshrine blockchain
𝝅 𝝅 𝝅 𝝅 𝝅
Layer 1
92 ZKP MOOC
Applications – Proof of exploit
• Prove I know a Tx that can change the state root to state root’
(Prove I know a bug that can change your balance, etc)
93 ZKP MOOC
Applications – Attestation (”zk oracle”)
Layer 1
95 ZKP MOOC
Thank you!
@yezhang1998
96 ZKP MOOC
Credit: Faithie/Shutterstock