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.

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