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,
25 },
26 frontend::{circom::CircomFCircuit, FCircuit},
27 Decider, FoldingScheme,
28};
29use solidity_verifiers::{
30 evm::{compile_solidity, Evm},
31 utils::get_function_selector_for_nova_cyclefold_verifier,
32 verifiers::nova_cyclefold::get_decider_template_for_cyclefold_decider,
33 NovaCycleFoldVerifierKey,
34};
35
36mod utils;
37use utils::init_ivc_and_decider_params;
38
39fn main() {
40 // set the initial state
41 let z_0 = vec![Fr::from(3_u32)];
42
43 // set the external inputs to be used at each step of the IVC, it has length of 10 since this
44 // is the number of steps that we will do
45 let external_inputs = vec![
46 vec![Fr::from(6u32), Fr::from(7u32)],
47 vec![Fr::from(8u32), Fr::from(9u32)],
48 vec![Fr::from(10u32), Fr::from(11u32)],
49 vec![Fr::from(12u32), Fr::from(13u32)],
50 vec![Fr::from(14u32), Fr::from(15u32)],
51 vec![Fr::from(6u32), Fr::from(7u32)],
52 vec![Fr::from(8u32), Fr::from(9u32)],
53 vec![Fr::from(10u32), Fr::from(11u32)],
54 vec![Fr::from(12u32), Fr::from(13u32)],
55 vec![Fr::from(14u32), Fr::from(15u32)],
56 ];
57
58 // initialize the Circom circuit
59 let r1cs_path = PathBuf::from(
60 "./folding-schemes/src/frontend/circom/test_folder/with_external_inputs.r1cs",
61 );
62 let wasm_path = PathBuf::from(
63 "./folding-schemes/src/frontend/circom/test_folder/with_external_inputs_js/with_external_inputs.wasm",
64 );
65
66 let f_circuit_params = (r1cs_path, wasm_path, 1, 2);
67 let f_circuit = CircomFCircuit::<Fr>::new(f_circuit_params).unwrap();
68
69 let (fs_prover_params, kzg_vk, g16_pk, g16_vk) =
70 init_ivc_and_decider_params::<CircomFCircuit<Fr>>(f_circuit.clone());
71
72 pub type NOVA =
73 Nova<G1, GVar, G2, GVar2, CircomFCircuit<Fr>, KZG<'static, Bn254>, Pedersen<G2>>;
74 pub type DECIDERETH_FCircuit = DeciderEth<
75 G1,
76 GVar,
77 G2,
78 GVar2,
79 CircomFCircuit<Fr>,
80 KZG<'static, Bn254>,
81 Pedersen<G2>,
82 Groth16<Bn254>,
83 NOVA,
84 >;
85
86 // initialize the folding scheme engine, in our case we use Nova
87 let mut nova = NOVA::init(&fs_prover_params, f_circuit.clone(), z_0).unwrap();
88 // run n steps of the folding iteration
89 for (i, external_inputs_at_step) in external_inputs.iter().enumerate() {
90 let start = Instant::now();
91 nova.prove_step(external_inputs_at_step.clone()).unwrap();
92 println!("Nova::prove_step {}: {:?}", i, start.elapsed());
93 }
94
95 let rng = rand::rngs::OsRng;
96 let start = Instant::now();
97 let proof = DECIDERETH_FCircuit::prove(
98 (g16_pk, fs_prover_params.cs_params.clone()),
99 rng,
100 nova.clone(),
101 )
102 .unwrap();
103 println!("generated Decider proof: {:?}", start.elapsed());
104
105 let verified = DECIDERETH_FCircuit::verify(
106 (g16_vk.clone(), kzg_vk.clone()),
107 nova.i,
108 nova.z_0.clone(),
109 nova.z_i.clone(),
110 &nova.U_i,
111 &nova.u_i,
112 &proof,
113 )
114 .unwrap();
115 assert!(verified);
116 println!("Decider proof verification: {}", verified);
117
118 // Now, let's generate the Solidity code that verifies this Decider final proof
119 let function_selector =
120 get_function_selector_for_nova_cyclefold_verifier(nova.z_0.len() * 2 + 1);
121
122 let calldata: Vec<u8> = prepare_calldata(
123 function_selector,
124 nova.i,
125 nova.z_0,
126 nova.z_i,
127 &nova.U_i,
128 &nova.u_i,
129 proof,
130 )
131 .unwrap();
132
133 // prepare the setup params for the solidity verifier
134 let nova_cyclefold_vk = NovaCycleFoldVerifierKey::from((g16_vk, kzg_vk, f_circuit.state_len()));
135
136 // generate the solidity code
137 let decider_solidity_code = get_decider_template_for_cyclefold_decider(nova_cyclefold_vk);
138
139 // verify the proof against the solidity code in the EVM
140 let nova_cyclefold_verifier_bytecode = compile_solidity(&decider_solidity_code, "NovaDecider");
141 let mut evm = Evm::default();
142 let verifier_address = evm.create(nova_cyclefold_verifier_bytecode);
143 let (_, output) = evm.call(verifier_address, calldata.clone());
144 assert_eq!(*output.last().unwrap(), 1);
145
146 // save smart contract and the calldata
147 println!("storing nova-verifier.sol and the calldata into files");
148 use std::fs;
149 fs::write(
150 "./examples/nova-verifier.sol",
151 decider_solidity_code.clone(),
152 )
153 .unwrap();
154 fs::write("./examples/solidity-calldata.calldata", calldata.clone()).unwrap();
155 let s = solidity_verifiers::utils::get_formatted_calldata(calldata.clone());
156 fs::write("./examples/solidity-calldata.inputs", s.join(",\n")).expect("");
157}