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