You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.4 KiB

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