Try โ€‚โ€‰HackMD

Folding Circom circuits: a ZKML case study

Previous work from other groups

Folding model architecture vs inference

  • Folding model architecture
    • Pros: get much more compression ratio as we wish per inference
    • Cons: require models to be specifically designed for folding, which might not be practical for the current web2 industry
  • Folding model inference
    • Pros: do not need to modify current models
    • Cons: performance still limited by the model depth and complexity

Let's try to fold model inference!

In order to use Nova-Scotia to fold Circom circuits:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More โ†’

We write a circuit that takes a list of public inputs (these must be named step_in for the Nova-Scotia interface) and outputs the same number of public outputs (named step_out). These public outputs will then be routed to the next step of recursion as step_in, and this will continue until we reach the end of the recursion iterations. Within a step circuit, besides the public inputs, Circom circuits can input additional private inputs (with any name/JSON structure Circom will accept).

Let's try to write out the step_in, step_out, and private inputs at each step!

Scenario 1: Public Data, Private Model

Attempt 1: naive approach

signals Step 1 Step 2 โ€ฆ Step N-1 Step N
stepin
[modelHash,data1,data2,...,dataN,0,0,...,0,0]
stepout,1
โ€ฆ
stepout,Nโˆ’2
stepout,Nโˆ’1
stepout
[modelHash,data1,data2,...,dataN,out1,0,...,0,0]
[modelHash,data1,data2,...,dataN,out1,out2,...,0,0]
โ€ฆ
[modelHash,data1,data2,...,dataN,out1,out2,...,outNโˆ’1,0]
[modelHash,data1,data2,...,dataN,out1,out2,...,outNโˆ’1,outN]
private inputs model weights model weights โ€ฆ model weights model weights

Issues:

  • the size of the public outputs (hence the size of the proof) grows linearly
  • a lot of switchers need to be used to insert at the correct positions

Attempt 2: Merkle root?

signals Step 1 Step 2 โ€ฆ Step N-1 Step N
stepin
[modelHash,zeroRoot,zeroRoot]
โ€ฆ
stepout,Nโˆ’2
stepout,Nโˆ’1
stepout
[modelHash,dataMerkleRoot1,outputMerkleRoot1]
[modelHash,dataMerkleRoot2,outputMerkleRoot2]
โ€ฆ
[modelHash,dataMerkleRootNโˆ’1,outputMerkleRootNโˆ’1]
[modelHash,dataMerkleRootN,outputMerkleRootN]
private inputs
[modelWeights,dataLeaf0,merklePaths0]
[modelWeights,dataLeaf1,merklePaths1]
โ€ฆ
[modelWeights,dataLeafNโˆ’2,merklePathsNโˆ’2]
[modelWeights,dataLeafNโˆ’1,merklePathsNโˆ’1]

Improvements:

  • less public inputs/outputs

Issues:

  • order of data matters!
  • relatively cheaper (than Attempt 3) if try to verify the entire tree on chain
  • 2logโกN
    number of hashes per step
  • need to publish Merkle leaves elsewhere

Attempt 3: Recursive hashing?

signals Step 1 Step 2 โ€ฆ Step N-1 Step N
stepin
[modelHash,0,0]
โ€ฆ
stepout,Nโˆ’2
stepout,Nโˆ’1
stepout
[modelHash,H(0,data1),H(0,out1)]
[modelHash,H(H(0,data1),data2),H(H(0,out1),out2)]
โ€ฆ
[modelHash,H(...,dataNโˆ’1),H(...,outNโˆ’1)]
[modelHash,H(...,data1),H(...,outNโˆ’1)]
private inputs
[modelWeights,data1]
[modelWeights,data2]
โ€ฆ
[modelWeights,dataNโˆ’1]
[modelWeights,dataN]

Improvements:

  • only 2 hashes per step
  • less signals to pass around

Issues:

  • order of data still matters
  • very expensive to verify on chain, but luckily we can precompute and commit the final hash
  • need to publish raw inputs elsewhere

Scenario 2: Private Data, Public Model

Attempt 1: naive approach

signals Step 1 Step 2 โ€ฆ Step N-1 Step N
stepin
[modelWeights,0,0,...,0,0,0,0,...,0,0]
stepout,1
โ€ฆ
stepout,Nโˆ’2
stepout,Nโˆ’1
stepout
[modelWeights,dataHash1,0,...,0,0,out1,0,...,0,0]
[modelWeights,dataHash1,dataHash2,...,0,0,out1,out2,...,0,0]
โ€ฆ
[modelWeights,dataHash1,dataHash2,...,dataHashNโˆ’1,0,out1,out2,...,outNโˆ’1,0]
[modelWeights,dataHash1,dataHash2,...,dataHashNโˆ’1,dataHashN,out1,out2,...,outNโˆ’1,outN]
private inputs
data1
data2
โ€ฆ
dataNโˆ’1
dataN

Issues: same as Scenario 1 Attempt 1

Solution: see Scenario 1 Attempt 3

Issues:

  • we will need to publish the model weights elsewhere