sonobe/examples/circom_full_flow.rs
examples/circom_full_flow.rsRaw
1#![allow(non_snake_case)]
2#![allow(non_camel_case_types)]
3#![allow(clippy::upper_case_acronyms)]
4///
5/// This example performs the full flow:
6/// - define the circuit to be folded
7/// - fold the circuit with Nova+CycleFold's IVC
8/// - generate a DeciderEthCircuit final proof
9/// - generate the Solidity contract that verifies the proof
10/// - verify the proof in the EVM
11///
12use ark_bn254::{constraints::GVar, Bn254, Fr, G1Projective as G1};
13
14use ark_groth16::Groth16;
15use ark_grumpkin::{constraints::GVar as GVar2, Projective as G2};
16
17use std::path::PathBuf;
18use std::time::Instant;
19
20use folding_schemes::{
21 commitment::{kzg::KZG, pedersen::Pedersen},
22 folding::nova::{
23 decider_eth::{prepare_calldata, Decider as DeciderEth},
24 Nova, PreprocessorParam,
25 },
26 frontend::FCircuit,
27 transcript::poseidon::poseidon_canonical_config,
28 Decider, FoldingScheme,
29};
30use frontends::circom::CircomFCircuit;
31use solidity_verifiers::{
32 evm::{compile_solidity, Evm},
33 utils::get_function_selector_for_nova_cyclefold_verifier,
34 verifiers::nova_cyclefold::get_decider_template_for_cyclefold_decider,
35 NovaCycleFoldVerifierKey,
36};
37
38fn main() {
39 // set the initial state
40 let z_0 = vec![Fr::from(3_u32)];
41
42 // set the external inputs to be used at each step of the IVC, it has length of 10 since this
43 // is the number of steps that we will do
44 let external_inputs = vec![
45 vec![Fr::from(6u32), Fr::from(7u32)],
46 vec![Fr::from(8u32), Fr::from(9u32)],
47 vec![Fr::from(10u32), Fr::from(11u32)],
48 vec![Fr::from(12u32), Fr::from(13u32)],
49 vec![Fr::from(14u32), Fr::from(15u32)],
50 vec![Fr::from(6u32), Fr::from(7u32)],
51 vec![Fr::from(8u32), Fr::from(9u32)],
52 vec![Fr::from(10u32), Fr::from(11u32)],
53 vec![Fr::from(12u32), Fr::from(13u32)],
54 vec![Fr::from(14u32), Fr::from(15u32)],
55 ];
56
57 // initialize the Circom circuit
58 let r1cs_path = PathBuf::from("./frontends/src/circom/test_folder/with_external_inputs.r1cs");
59 let wasm_path = PathBuf::from(
60 "./frontends/src/circom/test_folder/with_external_inputs_js/with_external_inputs.wasm",
61 );
62
63 let f_circuit_params = (r1cs_path.into(), wasm_path.into(), 1, 2);
64 let f_circuit = CircomFCircuit::<Fr>::new(f_circuit_params).unwrap();
65
66 pub type N =
67 Nova<G1, GVar, G2, GVar2, CircomFCircuit<Fr>, KZG<'static, Bn254>, Pedersen<G2>, false>;
68 pub type D = DeciderEth<
69 G1,
70 GVar,
71 G2,
72 GVar2,
73 CircomFCircuit<Fr>,
74 KZG<'static, Bn254>,
75 Pedersen<G2>,
76 Groth16<Bn254>,
77 N,
78 >;
79
80 let poseidon_config = poseidon_canonical_config::<Fr>();
81 let mut rng = rand::rngs::OsRng;
82
83 // prepare the Nova prover & verifier params
84 let nova_preprocess_params = PreprocessorParam::new(poseidon_config, f_circuit.clone());
85 let nova_params = N::preprocess(&mut rng, &nova_preprocess_params).unwrap();
86
87 // initialize the folding scheme engine, in our case we use Nova
88 let mut nova = N::init(&nova_params, f_circuit.clone(), z_0).unwrap();
89
90 // prepare the Decider prover & verifier params
91 let (decider_pp, decider_vp) =
92 D::preprocess(&mut rng, nova_params.clone(), nova.clone()).unwrap();
93
94 // run n steps of the folding iteration
95 for (i, external_inputs_at_step) in external_inputs.iter().enumerate() {
96 let start = Instant::now();
97 nova.prove_step(rng, external_inputs_at_step.clone(), None)
98 .unwrap();
99 println!("Nova::prove_step {}: {:?}", i, start.elapsed());
100 }
101
102 // verify the last IVC proof
103 let ivc_proof = nova.ivc_proof();
104 N::verify(
105 nova_params.1, // Nova's verifier params
106 ivc_proof,
107 )
108 .unwrap();
109
110 let start = Instant::now();
111 let proof = D::prove(rng, decider_pp, nova.clone()).unwrap();
112 println!("generated Decider proof: {:?}", start.elapsed());
113
114 let verified = D::verify(
115 decider_vp.clone(),
116 nova.i,
117 nova.z_0.clone(),
118 nova.z_i.clone(),
119 &nova.U_i,
120 &nova.u_i,
121 &proof,
122 )
123 .unwrap();
124 assert!(verified);
125 println!("Decider proof verification: {}", verified);
126
127 // Now, let's generate the Solidity code that verifies this Decider final proof
128 let function_selector =
129 get_function_selector_for_nova_cyclefold_verifier(nova.z_0.len() * 2 + 1);
130
131 let calldata: Vec<u8> = prepare_calldata(
132 function_selector,
133 nova.i,
134 nova.z_0,
135 nova.z_i,
136 &nova.U_i,
137 &nova.u_i,
138 proof,
139 )
140 .unwrap();
141
142 // prepare the setup params for the solidity verifier
143 let nova_cyclefold_vk = NovaCycleFoldVerifierKey::from((decider_vp, f_circuit.state_len()));
144
145 // generate the solidity code
146 let decider_solidity_code = get_decider_template_for_cyclefold_decider(nova_cyclefold_vk);
147
148 // verify the proof against the solidity code in the EVM
149 let nova_cyclefold_verifier_bytecode = compile_solidity(&decider_solidity_code, "NovaDecider");
150 let mut evm = Evm::default();
151 let verifier_address = evm.create(nova_cyclefold_verifier_bytecode);
152 let (_, output) = evm.call(verifier_address, calldata.clone());
153 assert_eq!(*output.last().unwrap(), 1);
154
155 // save smart contract and the calldata
156 println!("storing nova-verifier.sol and the calldata into files");
157 use std::fs;
158 fs::write(
159 "./examples/nova-verifier.sol",
160 decider_solidity_code.clone(),
161 )
162 .unwrap();
163 fs::write("./examples/solidity-calldata.calldata", calldata.clone()).unwrap();
164 let s = solidity_verifiers::utils::get_formatted_calldata(calldata.clone());
165 fs::write("./examples/solidity-calldata.inputs", s.join(",\n")).expect("");
166}