recursive snarks - cs251.stanford.edu

36
Recursive SNARKs CS251 Fall 2021 (cs251.stanford.edu) Benedikt Bรผnz

Upload: others

Post on 23-Jul-2022

8 views

Category:

Documents


0 download

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