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