Download - Recursive SNARKs - cs251.stanford.edu

Transcript
Page 1: Recursive SNARKs - cs251.stanford.edu

Recursive SNARKs

CS251 Fall 2021(cs251.stanford.edu)

Benedikt BΓΌnz

Page 2: Recursive SNARKs - cs251.stanford.edu

Recap: Non-interactive Proof SystemsA non-interactive proof system is a triple (S, P, V):

β€’ S(𝐢) β‡Ύ public parameters (Sp, Sv) for prover and verifier

(Sp, Sv) is called a reference string

β€’ P(Sp, 𝒙,π’˜) β‡Ύ proof πœ‹

β€’ V(Sv, 𝒙, 𝝅) β‡Ύ accept or reject

Page 3: Recursive SNARKs - cs251.stanford.edu

Recap: zkRollupToday: every miner must verify every posted Tx verify

all Tx

verifyall Tx

verifyall Tx

verify all Tx β‡’ short proof Ο€

summary, Ο€

verifyΟ€

verifying proof is much easier than verifying 10K Tx

verifyΟ€

Rollup Server

Page 4: Recursive SNARKs - cs251.stanford.edu

Recap: zkRollupToday: every miner must verify every posted Tx verify

all Tx

verifyall Tx

verifyall Tx

verify all Tx β‡’ short proof Ο€

summary, Ο€

verifyΟ€

verifying proof is much easier than verifying 10K Tx

verifyΟ€

Rollup Server

Page 5: Recursive SNARKs - cs251.stanford.edu

Rollup with many coordinators

summary2,

Ο€2

verifyΟ€

verifyΟ€

Server 2

Server 1

summary1, Ο€1

summary, Ο€

verifyΟ€

Page 6: Recursive SNARKs - cs251.stanford.edu

Zk-zk-Rollupβ€’ Multiple serversβ€’ Each responsible for subset of users (no overlaps)β€’ Rollup aggregator (can be one of the servers)β€’ Rollup aggregator combines summaries (balance table) and

creates one proof thatβ€’ How can we combine proofs?β€’ Trivial solution: β€’ All servers forward all Txβ€’ Rollup aggregator creates one SNARKβ€’ Does not save work

Page 7: Recursive SNARKs - cs251.stanford.edu

Recap: Non-interactive Proof SystemsA non-interactive proof system is a triple (S, P, V):

β€’ S(𝐢) β‡Ύ public parameters (Sp, Sv) for prover and verifier

(Sp, Sv) is called a reference string

β€’ P(Sp, 𝒙,π’˜) β‡Ύ proof πœ‹

β€’ V(Sv, 𝒙, 𝝅) β‡Ύ accept or reject

Page 8: Recursive SNARKs - cs251.stanford.edu

SNARK of a SNARK ProofA non-interactive proof system is a triple (S, P, V):

β€’ S(𝐢) β‡Ύ public parameters (Sp, Sv) for prover and verifier

(Sp, Sv) is called a reference string

β€’ P(Sp, 𝒙,π’˜) β‡Ύ proof πœ‹

β€’ V(Sv, 𝒙, 𝝅) β‡Ύ accept or reject

Page 9: Recursive SNARKs - cs251.stanford.edu

SNARK of SNARK

𝑆 𝐢 β†’ 𝑆!, 𝑆"πœ‹ ← 𝑃(𝑆!, π‘₯, 𝑀)

Now write a circuit 𝐢′ that verifies πœ‹: β€’ Input π‘₯β€² is xβ€’ Witness 𝑀′ is πœ‹β€’ 𝐢′(π‘₯β€², w’) = 0 iff V(𝑆", πœ‹, π‘₯)=Accept Finally:

𝑆 𝐢′ β†’ 𝑆′!, 𝑆′"πœ‹β€² ← 𝑃(𝑆′!, π‘₯β€², 𝑀′) 9

How can we aggregate proofs?

Page 10: Recursive SNARKs - cs251.stanford.edu

SNARK of SNARKs

𝑆 𝐢 β†’ 𝑆!, 𝑆"πœ‹# ← 𝑃(𝑆!, π‘₯#, 𝑀#) πœ‹$ ← 𝑃(𝑆!, π‘₯$, 𝑀$)

Now write a circuit 𝐢′ that verifies πœ‹: β€’ Input π‘₯β€² is x1||x2

β€’ Witness 𝑀′ is πœ‹#||πœ‹$β€’ 𝐢′(π‘₯β€², w’) = 0 iff V(𝑆" , π‘₯#, πœ‹#)=Accept and V(𝑆" , π‘₯$, πœ‹$)=Accept Finally:

𝑆 𝐢′ β†’ 𝑆′!, 𝑆′"πœ‹β€² ← 𝑃(𝑆′!, π‘₯β€², 𝑀′) 10

How can we aggregate proofs?

Page 11: Recursive SNARKs - cs251.stanford.edu

SNARK of SNARKs

β€’ Note that C’ depends only on V and Sv (not on C1,C2)β€’ We can express V as a circuit:

𝑆! π‘₯β€² πœ‹β€²

+ βˆ’

Γ—

0 = β€œπ΄π‘π‘π‘’π‘π‘‘β€ 𝑀2 = πœ‹β€² is independent of 𝑀3, 𝑀4|C’|=2*|V| < |2* C|

Page 12: Recursive SNARKs - cs251.stanford.edu

Building SNARK of SNARKs

β€’ How big is C’?β€’ Comparison |SHA256 circuit| = 20k gatesβ€’ First SNARK of SNARK ~1 million gates with trusted

setup (BCTV14)β€’ Today less than 50k gates (Halo, BCLMS20, Nova) β€’ no trusted setup

β€’ Independent of inner SNARK circuits!

Page 13: Recursive SNARKs - cs251.stanford.edu

Rollup with many coordinators

summary2,

Ο€1

verifyΟ€

verifyΟ€

Server 2

Server 1

summary1, Ο€2

summary, Ο€

verifyΟ€

Page 14: Recursive SNARKs - cs251.stanford.edu

Zk-zk-Rollupβ€’ Let rooti be the Merkle Tree Root of summary i

β€’ S!, 𝑆" ← 𝑺(𝐢#) // 𝐢# rollup circuit

Merkle treeπ‘Ÿπ‘œπ‘œπ‘‘1 π‘Ÿπ‘œπ‘œπ‘‘2 π‘Ÿπ‘œπ‘œπ‘‘3 π‘Ÿπ‘œπ‘œπ‘‘5

root

Build one root from summaries

𝐢67!(x = S8, 𝐫𝐨𝐨𝐭; w = π‘Ÿπ‘œπ‘œπ‘‘3, π‘Ÿπ‘œπ‘œπ‘‘4… , πœ‹3, πœ‹4, … ):V(S8, π‘₯ = π‘Ÿπ‘œπ‘œπ‘‘9, πœ‹9) for all i and 𝐫𝐨𝐨𝐭=MT(π‘Ÿπ‘œπ‘œπ‘‘9s)

Page 15: Recursive SNARKs - cs251.stanford.edu

Tornado cash

nf1

nullifiers

coinsMerkle

root

Treasury: 300 DAI100 DAI pool:each coin = 100 DAI

nf2(32 bytes)

C1 C2 C3 C4 0 … 0

tree ofheight 20

(220 leaves)

public list of coins

nf, proof 𝝅, A(over Tor)

nf

nf and πœ‹ reveal nothing about which coin was spent.

But, coin #3 cannot be spent again, because nf = H2(k’) is now nullified.

… but observer does not know which are spent

100 DAIto address A

Merkleroot

H1, H2: R β‡Ύ {0,1}256

next = 5

contract state

Withdraw coin #3to addr A:

Page 16: Recursive SNARKs - cs251.stanford.edu

zk3-Rollup (tornado cash rollup)

summary2,

Ο€2

verifyΟ€

verifyΟ€

Rollup Server 2

Rollup Server 1

summary1, Ο€1

summary, Ο€

πœ‹:;

πœ‹:;

πœ‹:;

πœ‹:;

verifyΟ€

Page 17: Recursive SNARKs - cs251.stanford.edu

zk3-Rollupβ€’ Users create SNARK for TC Circuit 𝐢"#

β€’ S$, 𝑆% ← 𝑺(𝐢"#)β€’ πœ‹"# ← 𝑃(𝑆% , 𝑑π‘₯, 𝑀)

β€’ Rollups create SNARKs for 𝐢& = βˆ€' 𝑉 𝑆( , 𝑑π‘₯' , πœ‹' = β€œπ‘Žπ‘π‘π‘’π‘π‘‘β€β€’ 𝑑π‘₯ π‘Ÿπ‘œπ‘œπ‘‘ = 𝑀𝑇 𝑑π‘₯), … , 𝑑π‘₯*β€’ πœ‹+ = πœ‹"#,)|| … ||πœ‹"#,*β€’ S$β€², 𝑆%β€² ← 𝑺(𝐢&)β€’ πœ‹& = 𝑃(𝑆%+ , 𝑑π‘₯ π‘Ÿπ‘œπ‘œπ‘‘, πœ‹+)

β€’ Rollup Aggregator creates SNARK for 𝐢- = βˆ€' 𝑉(𝑆(β€², π‘Ÿπ‘œπ‘œπ‘‘' , πœ‹&,')β€’ S$++, 𝑆%++ ← 𝑺(𝐢-)β€’ π‘Ÿπ‘œπ‘œπ‘‘ = 𝑀𝑇(π‘Ÿπ‘œπ‘œπ‘‘), … , π‘Ÿπ‘œπ‘œπ‘‘.)β€’ πœ‹&β€² = πœ‹&,)|| … ||πœ‹&,.β€’ πœ‹- = 𝑃(𝑆%++, π‘Ÿπ‘œπ‘œπ‘‘, πœ‹&+ )

Page 18: Recursive SNARKs - cs251.stanford.edu

Enhancing transactions with SNARKsβ€’ We’ve seen that private transactions require zero-

knowledge proofsβ€’ Add ZK-SNARKs to every transactionβ€’ Level 1 coordinators verify transaction by verifying

transaction ZK-SNARKsβ€’ Additionally, we can have more complicated transactions

(Smart Contracts) β€’ Transaction verification is constant time regardless of

proof complexityβ€’ Can we also hide the smart contract?

Page 19: Recursive SNARKs - cs251.stanford.edu

ZEXE private execution

β€’ ZEXE is a model of computation (like UTXOs/Scripts or Accounts/EVM)

β€’ The basic unit is a record (similar to a UTXO)β€’ Every transaction consumes records and creates

recordsβ€’ Universal predicate: Prevents double spendsβ€’ Birth predicate: Says how a record can be createdβ€’ Death predicate: Says how a record can be consumed

Page 20: Recursive SNARKs - cs251.stanford.edu

ZEXE private executionRecord 1:Birth predicate 1Death predicate 1Payload 1

Record 2:Birth predicate 1Death predicate 1 Payload 1

Record 3:Birth predicate 3Death predicate 3Payload 3

TX checks that Record 1 and Record 2 have not been spentBirth3(R1, R2,R3) and Death1(R1, R2,R3) and Death2(R1,R2,R3)

Page 21: Recursive SNARKs - cs251.stanford.edu

ZEXE private executionβ€’ Universal predicate (similar to tornado cash)β€’ Uses nullifiersβ€’ Checks that nullifier=H(sk,records) is properly

createdβ€’ Checks that nullifier only appears onceβ€’ Prevents double spends

Merkleroot

tree ofheight 20

(220 leaves)

R1 R2 R3 … 0 0 0

Page 22: Recursive SNARKs - cs251.stanford.edu

Implementing assets with ZEXE

β€’ Record payload has a value v and an asset id β€’ Birth predicateβ€’ Defines the tokenβ€’ New record id needs to match consumed predicate idsβ€’ New record value is sum of inputs

β€’ Death predicateβ€’ Defines the SCRIPTβ€’ E.g. spendable by signatureβ€’ E.g. Spendable by multisigature + preimage of hash

Page 23: Recursive SNARKs - cs251.stanford.edu

Implementing smart contracts with ZEXE

β€’ Record payload is state of smart contract, smart contract instance id

β€’ Birth predicateβ€’ Either creates smart contract orβ€’ One of the inputs needs to be the old smart

contract recordβ€’ Death predicateβ€’ Defines the smart contract logic

Page 24: Recursive SNARKs - cs251.stanford.edu

ZEXE game of Chessβ€’ Record payload is state of smart contract, smart contract

instance idβ€’ Birth predicate

β€’ Starts new game (and assigns pks to black/white) orβ€’ One of the inputs needs to be the old chess game

β€’ Death predicateβ€’ If game finished then pay money to the winnerβ€’ Otherwise input records must be game record + one move

recordβ€’ Move record must be signed by the right player β€’ Move record must contain a valid move

Page 25: Recursive SNARKs - cs251.stanford.edu

Making ZEXE privateβ€’ 𝑆%!, 𝑆(! ← 𝑆(𝐢/) (Universal predicate)β€’ 𝑆%" , 𝑆(" ← 𝑆(𝐢0) (Birth predicate)β€’ 𝑆%# , 𝑆(# ← 𝑆(𝐢1) (Death predicate)β€’ 𝑆%$% , 𝑆($% ← 𝑆(𝐢"2) (TX circuit)β€’ 𝐢"2 = 𝑉 𝑆(!, . , . =0 and 𝑉 𝑆(" , . , . =0 and 𝑉 𝑆(# , . , . =0 And Record=𝐻(π‘π‘Žπ‘¦π‘™π‘œπ‘Žπ‘‘, 𝑆(" , 𝑆(# , π‘Ÿ) // π‘Ÿ random

β€’ TX: Input records || Output records β€’ Compute nullifiers 𝑛𝑓!, … , 𝑛𝑓" from input records β€’ To create a TX, create three ZK-SNARKS (now ZK is important)

β€’ x=TX,𝑀 = π‘π‘Žπ‘¦π‘™π‘œπ‘Žπ‘‘π‘ , 𝑆#!, 𝑆#"β€’ πœ‹$ ← 𝑃 𝑆%#, x 𝑛𝑓!, … , 𝑛𝑓", 𝑀 | | 𝑀𝑇 π‘π‘Ÿπ‘œπ‘œπ‘“π‘ β€’ πœ‹& ← 𝑃 𝑆%!, x, 𝑀‒ πœ‹' ← 𝑃 𝑆%", x, 𝑀

β€’ Create πœ‹() ← 𝑃 𝑆%$%, x, 𝑀||πœ‹$, πœ‹&, πœ‹'

R1 R2 R3 … 0 0 0

Merkleroot

tree ofheight 20

(220 leaves)

MT of all records

Birth and death predicate as well as records are private!

Page 26: Recursive SNARKs - cs251.stanford.edu

Hitchhikers guide to the galaxy

Input Output (42)

Long Computation

What if we want to verify that computation?

Page 27: Recursive SNARKs - cs251.stanford.edu

SNARKs for long computations

Input Output (42)P(Sp, 𝒙,π’˜) β‡Ύ πœ‹V(Sv, 𝒙, πœ‹) β‡Ύ acceptLong Computation, Transcript

C – Circuit for long computationS(𝐢) β‡Ύ (Sp, Sv) 𝒙 = π’Šπ’π’‘π’–π’•, π’π’–π’•π’‘π’–π’•π’˜ = π’•π’“π’‚π’π’”π’„π’“π’Šπ’‘π’•

Issues:-P takes very long-Starts after proving aftercomputation finished-Can’t hand off computation-S also runs at least linear in |C| (ok if many proofs)

Page 28: Recursive SNARKs - cs251.stanford.edu

Handing off computation

Input Output (42), πœ‹S

𝐢T– Circuit for long intermediate computation

S(𝐢T) β‡Ύ (Sp, Sv) π’™πŸ = π’Šπ’π’‘π’–π’•, π’Šπ’π’•πŸ , π’˜πŸ = π’•π’“π’‚π’π’”π’„π’“π’Šπ’‘π’•πŸπ’™πŸ = π’Šπ’π’•πŸ, π’Šπ’π’•πŸ , π’˜πŸ = π’•π’“π’‚π’π’”π’„π’“π’Šπ’‘π’•πŸπ’™πŸ‘ = π’Šπ’π’•πŸ, 𝒐𝒖𝒕𝒑𝒖𝒕 , π’˜πŸ‘ = π’•π’“π’‚π’π’”π’„π’“π’Šπ’‘π’•πŸ‘P(Sp, π’™π’Š, π’˜π’Š) β‡Ύπœ‹9

Int1,πœ‹3 Int2,πœ‹4transcript1 transcript2 transcript3

V(Sv, π’™πŸ, π…πŸ)V(Sv, π’™πŸ, π…πŸ)V(Sv, π’™πŸ‘, π…πŸ‘)|πœ‹|/ V linear in #handoffs

Page 29: Recursive SNARKs - cs251.stanford.edu

Incremental Proofs

β€’ We need updatable/incremental proofs

S(𝐢T) β‡Ύ (Sp, Sv)

𝐢T– Circuit per computation step, 𝑑 number of steps/handoffs

P(Sp, π’™π’Š, π’˜π’Š, πœ‹9Y3) β‡Ύ updated proof πœ‹9 // πœ‹Z =βŠ₯

V(Sv, π’™πŸŽ, 𝒙𝒕, πœ‹:, t) β‡Ύ accept/reject

|πœ‹9| = |πœ‹9Y3| // proofs don’t grow

Page 30: Recursive SNARKs - cs251.stanford.edu

PhotoProof

Allow valid updates of photo and provide proof

Viewer can still verify authenticity

Page 31: Recursive SNARKs - cs251.stanford.edu

PhotoProof

Proof allows valid edits only, Incrementally updated

Page 32: Recursive SNARKs - cs251.stanford.edu

Constant size blockchains

β€’ Rollup reduces the verification costβ€’ Still linear in the number of state updatesβ€’ When a node joins the network they need to verify

one rollup proof per block!β€’ In general starting a full node requires verification of

all blocksβ€’ Can take days!

Page 33: Recursive SNARKs - cs251.stanford.edu

Constant size Blockchain

πœ‹$State-MT1

TX-MT1

Merkle tree

Transactions

πœ‹%State-MT2

TX-MT2

πœ‹&State-MT3

TX-MT3

πœ‹'State-MT4

TX-MT4

πœ‹9 prooves that transactions are valid with respect to the stateANDπœ‹9Y3 was valid for the previous block

Page 34: Recursive SNARKs - cs251.stanford.edu

Constant size Blockchain

πœ‹$State-MT1

TX-MT1

Merkle tree

Transactions

πœ‹%State-MT2

TX-MT2

πœ‹&State-MT3

TX-MT3

πœ‹'State-MT4

TX-MT4

Old miner New minerHead and State 4

Verifies State-MT4 and πœ‹5

Page 35: Recursive SNARKs - cs251.stanford.edu

Constant size Blockchain

β€’ Light clients can verify every block! β€’ Low memory, low computation β€’ Independent of length of chain or #transactions

β€’ Relies on data serving nodes for synching

β€’ Practical today!

Page 36: Recursive SNARKs - cs251.stanford.edu

Next lecture: Crypto tricks and open discussionPlease attend last two lectures if you can

END OF LECTURE