#![allow(non_snake_case)] #![allow(non_camel_case_types)] #![allow(clippy::upper_case_acronyms)] /// /// This example performs the full flow: /// - define the circuit to be folded /// - fold the circuit with Nova+CycleFold's IVC /// - generate a DeciderEthCircuit final proof /// - generate the Solidity contract that verifies the proof /// - verify the proof in the EVM /// #[cfg(test)] mod tests { use ark_bn254::{constraints::GVar, Bn254, Fr, G1Projective as G1}; use ark_grumpkin::{constraints::GVar as GVar2, Projective as G2}; use ark_crypto_primitives::snark::SNARK; use ark_groth16::{Groth16, ProvingKey, VerifyingKey as G16VerifierKey}; use ark_poly_commit::kzg10::VerifierKey as KZGVerifierKey; use ark_ff::{BigInteger, BigInteger256, PrimeField}; use ark_std::Zero; use std::path::PathBuf; use std::rc::Rc; use std::time::Instant; use folding_schemes::{ commitment::{ kzg::{ProverKey as KZGProverKey, KZG}, pedersen::Pedersen, CommitmentScheme, }, folding::nova::{ decider_eth::{prepare_calldata, Decider as DeciderEth}, decider_eth_circuit::DeciderEthCircuit, get_r1cs, Nova, ProverParams, VerifierParams, }, frontend::{circom::CircomFCircuit, FCircuit}, transcript::poseidon::poseidon_canonical_config, Decider, Error, FoldingScheme, }; use solidity_verifiers::{ utils::get_function_selector_for_nova_cyclefold_verifier, verifiers::nova_cyclefold::get_decider_template_for_cyclefold_decider, NovaCycleFoldVerifierKey, }; // This method computes the Nova's Prover & Verifier parameters for the example. // Warning: this method is only for testing purposes. For a real world use case those parameters // should be generated carefully (both the PoseidonConfig and the PedersenParams). #[allow(clippy::type_complexity)] fn init_nova_ivc_params>( F_circuit: FC, ) -> ( ProverParams, Pedersen>, VerifierParams, KZGVerifierKey, ) { let mut rng = ark_std::test_rng(); let poseidon_config = poseidon_canonical_config::(); // get the CM & CF_CM len let (r1cs, cf_r1cs) = get_r1cs::(&poseidon_config, F_circuit).unwrap(); let cs_len = r1cs.A.n_rows; let cf_cs_len = cf_r1cs.A.n_rows; // let (pedersen_params, _) = Pedersen::::setup(&mut rng, cf_len).unwrap(); let (kzg_pk, kzg_vk): (KZGProverKey, KZGVerifierKey) = KZG::::setup(&mut rng, cs_len).unwrap(); let (cf_pedersen_params, _) = Pedersen::::setup(&mut rng, cf_cs_len).unwrap(); let fs_prover_params = ProverParams::, Pedersen> { poseidon_config: poseidon_config.clone(), cs_params: kzg_pk.clone(), cf_cs_params: cf_pedersen_params, }; let fs_verifier_params = VerifierParams:: { poseidon_config: poseidon_config.clone(), r1cs, cf_r1cs, }; (fs_prover_params, fs_verifier_params, kzg_vk) } /// Initializes Nova parameters and DeciderEth parameters. Only for test purposes. #[allow(clippy::type_complexity)] fn init_ivc_and_decider_params>( f_circuit: FC, ) -> ( ProverParams, Pedersen>, KZGVerifierKey, ProvingKey, G16VerifierKey, ) { let mut rng = rand::rngs::OsRng; let start = Instant::now(); let (fs_prover_params, _, kzg_vk) = init_nova_ivc_params::(f_circuit.clone()); println!("generated Nova folding params: {:?}", start.elapsed()); pub type NOVA = Nova, Pedersen>; let z_0 = vec![Fr::zero(); f_circuit.state_len()]; let nova = NOVA::init(&fs_prover_params, f_circuit, z_0.clone()).unwrap(); let decider_circuit = DeciderEthCircuit::, Pedersen>::from_nova::( nova.clone(), ) .unwrap(); let start = Instant::now(); let (g16_pk, g16_vk) = Groth16::::circuit_specific_setup(decider_circuit.clone(), &mut rng).unwrap(); println!( "generated G16 (Decider circuit) params: {:?}", start.elapsed() ); (fs_prover_params, kzg_vk, g16_pk, g16_vk) } fn f_vec_to_bits(v: Vec) -> Vec { v.iter() .map(|v_i| { if v_i.is_one() { return true; } return false; }) .collect() } // returns the bytes representation of the given vector of finite field elements that represent // bits fn f_vec_to_bytes(v: Vec) -> Vec { let b = f_vec_to_bits(v); BigInteger256::from_bits_le(&b).to_bytes_le() } fn bytes_to_f_vec(b: Vec) -> Result, Error> { use num_bigint::BigUint; let bi = BigUint::from_bytes_le(&b); let bi = BigInteger256::try_from(bi).unwrap(); let bits = bi.to_bits_le(); Ok(bits .iter() .map(|&e| if e == true { F::one() } else { F::zero() }) .collect()) } // function to compute the next state of the folding via rust-native code (not Circom). Used to // check the Circom values. use tiny_keccak::{Hasher, Keccak}; fn rust_native_step( _i: usize, z_i: Vec, _external_inputs: Vec, ) -> Result, Error> { let b = f_vec_to_bytes(z_i.to_vec()); let mut h = Keccak::v256(); h.update(&b); let mut z_i1 = [0u8; 32]; h.finalize(&mut z_i1); bytes_to_f_vec(z_i1.to_vec()) } #[test] fn full_flow() { // set how many steps of folding we want to compute let n_steps = 10; // set the initial state let z_0_aux: Vec = vec![0_u32; 32 * 8]; let z_0: Vec = z_0_aux.iter().map(|v| Fr::from(*v)).collect::>(); // initialize the Circom circuit let r1cs_path = PathBuf::from("./circuit/keccak-chain.r1cs"); let wasm_path = PathBuf::from("./circuit/keccak-chain_js/keccak-chain.wasm"); let f_circuit_params = (r1cs_path, wasm_path, 32 * 8, 0); let mut f_circuit = CircomFCircuit::::new(f_circuit_params).unwrap(); // Note (optional): for more speed, we can set a custom rust-native logic, which will be // used for the `step_native` method instead of extracting the values from the circom // witness: f_circuit.set_custom_step_native(Rc::new(rust_native_step)); // ---------------- // Sanity check // check that the f_circuit produces valid R1CS constraints use ark_r1cs_std::alloc::AllocVar; use ark_r1cs_std::fields::fp::FpVar; use ark_r1cs_std::R1CSVar; use ark_relations::r1cs::ConstraintSystem; let cs = ConstraintSystem::::new_ref(); let z_0_var = Vec::>::new_witness(cs.clone(), || Ok(z_0.clone())).unwrap(); let z_1_var = f_circuit .generate_step_constraints(cs.clone(), 1, z_0_var, vec![]) .unwrap(); // check z_1_var against the native z_1 let z_1_native = f_circuit.step_native(1, z_0.clone(), vec![]).unwrap(); assert_eq!(z_1_var.value().unwrap(), z_1_native); // check that the constraint system is satisfied assert!(cs.is_satisfied().unwrap()); // ---------------- let (fs_prover_params, kzg_vk, g16_pk, g16_vk) = init_ivc_and_decider_params::>(f_circuit.clone()); pub type NOVA = Nova, KZG<'static, Bn254>, Pedersen>; pub type DECIDERETH_FCircuit = DeciderEth< G1, GVar, G2, GVar2, CircomFCircuit, KZG<'static, Bn254>, Pedersen, Groth16, NOVA, >; // initialize the folding scheme engine, in our case we use Nova let mut nova = NOVA::init(&fs_prover_params, f_circuit.clone(), z_0.clone()).unwrap(); // run n steps of the folding iteration for _ in 0..n_steps { let start = Instant::now(); nova.prove_step(vec![]).unwrap(); println!("Nova::prove_step {}: {:?}", nova.i, start.elapsed()); } // perform the hash chain natively in rust (which uses a rust Keccak256 library) let mut z_i_native = z_0.clone(); for i in 0..n_steps { z_i_native = rust_native_step(i, z_i_native.clone(), vec![]).unwrap(); } // check that the value of the last folding state (nova.z_i) computed through folding, is // equal to the natively computed hash using the rust_native_step method assert_eq!(nova.z_i, z_i_native); // ---------------- // Sanity check // The following lines contain a sanity check that checks the IVC proof (before going into // the zkSNARK proof) let verifier_params = VerifierParams:: { poseidon_config: nova.poseidon_config.clone(), r1cs: nova.clone().r1cs, cf_r1cs: nova.clone().cf_r1cs, }; let (running_instance, incoming_instance, cyclefold_instance) = nova.instances(); NOVA::verify( verifier_params, z_0, nova.z_i.clone(), nova.i, running_instance, incoming_instance, cyclefold_instance, ) .unwrap(); // ---------------- let rng = rand::rngs::OsRng; let start = Instant::now(); let proof = DECIDERETH_FCircuit::prove( (g16_pk, fs_prover_params.cs_params.clone()), rng, nova.clone(), ) .unwrap(); println!("generated Decider proof: {:?}", start.elapsed()); let verified = DECIDERETH_FCircuit::verify( (g16_vk.clone(), kzg_vk.clone()), nova.i, nova.z_0.clone(), nova.z_i.clone(), &nova.U_i, &nova.u_i, &proof, ) .unwrap(); assert!(verified); println!("Decider proof verification: {}", verified); // generate the Solidity code that verifies this Decider final proof let function_selector = get_function_selector_for_nova_cyclefold_verifier(nova.z_0.len() * 2 + 1); let calldata: Vec = prepare_calldata( function_selector, nova.i, nova.z_0, nova.z_i, &nova.U_i, &nova.u_i, proof, ) .unwrap(); // prepare the setup params for the solidity verifier let nova_cyclefold_vk = NovaCycleFoldVerifierKey::from((g16_vk, kzg_vk, f_circuit.state_len())); // generate the solidity code let decider_solidity_code = get_decider_template_for_cyclefold_decider(nova_cyclefold_vk); /* * Note: since we're proving the Keccak256 (ie. 32 byte size, 256 bits), the number of * inputs is too big for the contract. In a real world use case we would convert the binary * representation into a couple of field elements which would be inputs of the Decider * circuit, and in-circuit we would obtain the binary representation to be used for the * final proof check. * * The following code is commented out for that reason. // verify the proof against the solidity code in the EVM use solidity_verifiers::evm::{compile_solidity, Evm}; let nova_cyclefold_verifier_bytecode = compile_solidity(&decider_solidity_code, "NovaDecider"); let mut evm = Evm::default(); let verifier_address = evm.create(nova_cyclefold_verifier_bytecode); let (_, output) = evm.call(verifier_address, calldata.clone()); assert_eq!(*output.last().unwrap(), 1); */ // save smart contract and the calldata println!("storing nova-verifier.sol and the calldata into files"); use std::fs; fs::create_dir_all("./solidity").unwrap(); fs::write( "./solidity/nova-verifier.sol", decider_solidity_code.clone(), ) .unwrap(); fs::write("./solidity/solidity-calldata.calldata", calldata.clone()).unwrap(); let s = solidity_verifiers::utils::get_formatted_calldata(calldata.clone()); fs::write("./solidity/solidity-calldata.inputs", s.join(",\n")).expect(""); } }