arnaucube
arnaucube/sonobePublic Self-hosted repository
sonobe/examples/noir_full_flow.rs
examples/noir_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 folding_schemes::{
 18    commitment::{kzg::KZG, pedersen::Pedersen},
 19    folding::{
 20        nova::{
 21            decider_eth::{prepare_calldata, Decider as DeciderEth},
 22            Nova, PreprocessorParam,
 23        },
 24        traits::CommittedInstanceOps,
 25    },
 26    frontend::FCircuit,
 27    transcript::poseidon::poseidon_canonical_config,
 28    Decider, FoldingScheme,
 29};
 30use frontends::noir::{load_noir_circuit, NoirFCircuit};
 31use std::time::Instant;
 32
 33use solidity_verifiers::{
 34    evm::{compile_solidity, Evm},
 35    utils::get_function_selector_for_nova_cyclefold_verifier,
 36    verifiers::nova_cyclefold::get_decider_template_for_cyclefold_decider,
 37    NovaCycleFoldVerifierKey,
 38};
 39
 40fn main() {
 41    // set the initial state
 42    let z_0 = vec![Fr::from(1)];
 43
 44    // initialize the noir fcircuit
 45    let circuit_path = format!("./frontends/src/noir/test_folder/test_mimc/target/test_mimc.json",);
 46
 47    let circuit = load_noir_circuit(circuit_path).unwrap();
 48    let f_circuit = NoirFCircuit {
 49        circuit,
 50        state_len: 1,
 51        external_inputs_len: 0,
 52    };
 53
 54    pub type N = Nova<G1, GVar, G2, GVar2, NoirFCircuit<Fr>, KZG<'static, Bn254>, Pedersen<G2>>;
 55    pub type D = DeciderEth<
 56        G1,
 57        GVar,
 58        G2,
 59        GVar2,
 60        NoirFCircuit<Fr>,
 61        KZG<'static, Bn254>,
 62        Pedersen<G2>,
 63        Groth16<Bn254>,
 64        N,
 65    >;
 66
 67    let poseidon_config = poseidon_canonical_config::<Fr>();
 68    let mut rng = rand::rngs::OsRng;
 69
 70    // prepare the Nova prover & verifier params
 71    let nova_preprocess_params = PreprocessorParam::new(poseidon_config, f_circuit.clone());
 72    let nova_params = N::preprocess(&mut rng, &nova_preprocess_params).unwrap();
 73
 74    // initialize the folding scheme engine, in our case we use Nova
 75    let mut nova = N::init(&nova_params, f_circuit.clone(), z_0).unwrap();
 76
 77    // prepare the Decider prover & verifier params
 78    let (decider_pp, decider_vp) =
 79        D::preprocess(&mut rng, nova_params.clone(), nova.clone()).unwrap();
 80
 81    // run n steps of the folding iteration
 82    for i in 0..5 {
 83        let start = Instant::now();
 84        nova.prove_step(rng, vec![], None).unwrap();
 85        println!("Nova::prove_step {}: {:?}", i, start.elapsed());
 86    }
 87    // verify the last IVC proof
 88    let ivc_proof = nova.ivc_proof();
 89    N::verify(
 90        nova_params.1, // Nova's verifier params
 91        ivc_proof,
 92    )
 93    .unwrap();
 94
 95    let start = Instant::now();
 96    let proof = D::prove(rng, decider_pp, nova.clone()).unwrap();
 97    println!("generated Decider proof: {:?}", start.elapsed());
 98
 99    let verified = D::verify(
100        decider_vp.clone(),
101        nova.i,
102        nova.z_0.clone(),
103        nova.z_i.clone(),
104        &nova.U_i.get_commitments(),
105        &nova.u_i.get_commitments(),
106        &proof,
107    )
108    .unwrap();
109    assert!(verified);
110    println!("Decider proof verification: {}", verified);
111
112    // Now, let's generate the Solidity code that verifies this Decider final proof
113    let function_selector =
114        get_function_selector_for_nova_cyclefold_verifier(nova.z_0.len() * 2 + 1);
115
116    let calldata: Vec<u8> = prepare_calldata(
117        function_selector,
118        nova.i,
119        nova.z_0,
120        nova.z_i,
121        &nova.U_i,
122        &nova.u_i,
123        proof,
124    )
125    .unwrap();
126
127    // prepare the setup params for the solidity verifier
128    let nova_cyclefold_vk = NovaCycleFoldVerifierKey::from((decider_vp, f_circuit.state_len()));
129
130    // generate the solidity code
131    let decider_solidity_code = get_decider_template_for_cyclefold_decider(nova_cyclefold_vk);
132
133    // verify the proof against the solidity code in the EVM
134    let nova_cyclefold_verifier_bytecode = compile_solidity(&decider_solidity_code, "NovaDecider");
135    let mut evm = Evm::default();
136    let verifier_address = evm.create(nova_cyclefold_verifier_bytecode);
137    let (_, output) = evm.call(verifier_address, calldata.clone());
138    assert_eq!(*output.last().unwrap(), 1);
139
140    // save smart contract and the calldata
141    println!("storing nova-verifier.sol and the calldata into files");
142    use std::fs;
143    fs::write(
144        "./examples/nova-verifier.sol",
145        decider_solidity_code.clone(),
146    )
147    .unwrap();
148    fs::write("./examples/solidity-calldata.calldata", calldata.clone()).unwrap();
149    let s = solidity_verifiers::utils::get_formatted_calldata(calldata.clone());
150    fs::write("./examples/solidity-calldata.inputs", s.join(",\n")).expect("");
151}