sonobe-docs/src/design/nova-decider-onchain.md
src/design/nova-decider-onchain.mdRaw
1# Decider for Onchain Verification
2
3**Overview**: This section describes the approach for the *Decider* (compressed SNARK / final proof) in order to be able to **verify the Nova+CycleFold proofs onchain (in Ethereum's EVM)**.
4
5> **Warning**: This section, as the rest of Sonobe, is experimental. The approach described in this section is highly experimental and has not been audited.
6
7
8## Context
9At the final stage of the Nova+CycleFold folding, after $n$ iterations, we have the committed instances $\{u_n, U_n, U_{EC,n} \}$ and their respective witnessess.
10
11
12<span style="padding:20px;">*Diagram source: CycleFold paper ([https://eprint.iacr.org/2023/1192.pdf](https://eprint.iacr.org/2023/1192.pdf)). In the case of this document $n=i+2$, so $u_{i+2} = u_n$, $U_{i+2}=U_n$, $U_{EC,i+2}=U_{EC,n}$.*</span>
13
14<br>
15
16We work on a cycle of curves composed by $E_1$ and $E_2$, where $E_1.F_r = E_2.F_q$ and $E_1.F_q=E_2.F_r$.
17We will use $F_r$ to refer to $E_1.F_r=E_2.F_q$, and $F_q$ to refer to $E_1.F_q=E_2.F_r$.
18The main circuit constraint field is $F_r$, and $C_{EC}$ circuit constraint field is $F_q$.
19
20Since the objective is to verify the proofs on Ethereum, we set $E_1$=BN254 and $E_2$=Grumpkin. Thus, $F_r$ is the scalar field of the BN254.
21
22The $u_n$ and $U_n$ contain: $\{ \overline{E} \in E_1, \overline{W} \in E_1, u \in F_r, x \in F_r^{|io|} \}$
23
24And $U_{EC,n}$ contains: $\{ \overline{E} \in E_2, \overline{W} \in E_2, u \in F_q, x \in F_q^{|io|} \}$
25
26
27## The Decider approach
28The decider proof is computed once, and after all the folding has taken place. Our aim is to be able to verify this proof in the Ethereum's EVM.
29
30
31
32The prover computes $(U_{n+1}, W_{n+1}, \overline{T}) = NIFS.P((U_n, W_n), (u_n, w_n))$
33
34The *Decider Circuit* verifies in its R1CS relation over $F_r$ the following checks:
35
361. correct RelaxedR1CS relation of $U_{n+1}, W_{n+1}$ of the AugmentedFCircuit
372. check that $u_n.\overline{E}=0$ and $u_n.u=1$
383. check that $u_n.x_0 = H(n, z_0, z_n, U_n)$ and $u_n.x_1 = H(U_{EC,n})$
394. Pedersen commitments verification of $U_{EC,n}.\{ \overline{E}, \overline{W} \}$ with respect $W_{EC,n}$ (the witness of the committed instance)
40(where $\overline{E},\overline{W} \in E_2$, this check is native in $F_r$)
41<br>*The following check is done non-natively (in $F_r$):*
425. check the correct RelaxedR1CS relation of $U_{EC,n}, W_{EC,n}$ of the CycleFoldCircuit (this is non-native operations and with naive sparse matrix-vector product blows up the number of constraints
436. Check correct computation of the KZG challenges
44$$c_E = H(\overline{E}.\{x,y\}),~~c_W = H(\overline{W}.\{x,y\})$$
45which we do through in-circuit Transcript.
467. check that the KZG evaluations are correct
47 - $eval_W == p_W(c_W)$
48 - $eval_E == p_E(c_E)$
49where $p_W, p_E \in \mathbb{F}[X]$ are the interpolated polynomials from $W_{i+1}.W,~ W_{i+1}.E$ respectively.
508. check that the given NIFS challenge $r$ is indeed well computed. This challenge is then used outside the circuit by the Verifier to compute NIFS.V obtaining $U_{i+1}$
51
52Additionally we would have to check (outside of the circuit):
53
549. Commitments verification of $U_{n+1}.\{ \overline{E}, \overline{W} \}$ with respect $W_{n+1}$ (where $\overline{E}, \overline{W} \in E_1$)
5510. check $NIFS.V(r, U_n, u_n, \overline{T}) \stackrel{?}{=} U_{n+1}$
56
57The check 7 would be too expensive if using Pedersen commitments verification in-circuit (non-natively), so we changed these commitments from Pedersen to KZG, and then verify the KZG commitments outside of the circuit and directly onchain.
58
59The prover would generate a *Groth16* proof over BN254 for this *Decider Circuit*, which can later be verified onchain in the EVM together with the KGZ commitments of check 7 and check 8.
60
61In this way, the final proof to be verified onchain would be:
62
63- a Groth16 proof of the checks 1-8
64- the KZG proofs of the check 9
65- the NIFS.V (check 10), which relates the inputs of checks 1-8 and check 9
66
67<br>
68
69<u>The RelaxedR1CS checker circuit</u>
70
71The *"correct RelaxedR1CS relation"* (used in check 1) is checked by the gadget proposed by [Nalin Bhardwaj](https://twitter.com/nibnalin/), which is a R1CS circuit that checks the RelaxedR1CS relation, [more details here](https://github.com/privacy-scaling-explorations/sonobe/issues/19).
72
73The idea is that we check in a R1CS circiut the RelaxedR1CS relation ($Az \circ Bz - uCz -E=0$). Note that this approach has a blowup of `3x` with respect to the original circuit number of constraints `x` (when using sparse representation of the matrices).
74
75
76### Circuit costs
77> Note: the number of constraints written in this section is the current values that we have, which we aim to reduce in future iterations.
78
79<u>Estimated costs of the full decider-onchain circuit:</u>
80
81*(`x` is the number of constraints of the circuit that we're folding, and the AugmentedFCircuit takes ~80k constraints)*
82
831. $U_{n+1}$ relation: `3(x+80k)`
842. $u_n$ check: `<1000`
853. $u_n.x$ hash check: `2634`
864. Pedersen check of $U_{EC,n}$ commitments (native): `<5M` both commitments (including the inputs allocations). This is a couple of native MSMs of <1500 elements each one)
875. $U_{EC,n}$ relation (non-native): `5.1M`
886. Check correct computation of the KZG challenges: `7708`
897. check that the KZG evaluations are correct
908. check that the given NIFS challenge $r$ is indeed well computed
91
92Total: 3 * (x + 80) + 1000 + 2634 + 4_967_155 + 5_146_236 + 7708
93
94eg: for a circuit of `500k` constraints the decider circuit would take approximately `11.6M` constraints.
95
96As can be seen, most of the costs come from the Pedersen commitments verification and the $U_{EC,n}$ relation