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