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.

167 lines
5.7 KiB

Add IVCProof to the existing folding schemes (Nova,HyperNova,ProtoGalaxy) (#167) * Add IVCProof to the existing folding schemes (Nova,HyperNova,ProtoGalaxy) * Implement `from_ivc_proof` for the FoldingSchemes trait (and Nova, HyperNova, ProtoGalaxy), so that the FoldingScheme IVC's instance can be constructed from the given parameters and the last IVCProof, which allows to sent the IVCProof between different parties, so that they can continue iterating the IVC from the received IVCProof. Also the serializers allow for the IVCProof to be sent to a verifier that can deserialize it and verify it. This allows to remove the logic from the file [folding/nova/serialize.rs](https://github.com/privacy-scaling-explorations/sonobe/blob/f1d82418ba047cf90805f2d0505370246df24d68/folding-schemes/src/folding/nova/serialize.rs) and [folding/hypernova/serialize.rs](https://github.com/privacy-scaling-explorations/sonobe/blob/f1d82418ba047cf90805f2d0505370246df24d68/folding-schemes/src/folding/hypernova/serialize.rs) (removing the whole files), which is now covered by the `IVCProof` generated serializers (generated by macro instead of handwritten), and the test that the file contained is now abstracted and applied to all the 3 existing folding schemes (Nova, HyperNova, ProtoGalaxy) at the folding/mod.rs file. * update Nova VerifierParams serializers to avoid serializing the R1CS to save big part of the old serialized size * rm .instances() since it's not needed * add nova params serialization to nova's ivc test to ensure that IVC verification works with deserialized data * Add unified FS::ProverParam & VerifierParam serialization & deserialization (for all Nova, HyperNova and ProtoGalaxy), without serializing the R1CS/CCS and thus saving substantial serialized bytes space. * rm CanonicalDeserialize warnings msgs for VerifierParams
1 month ago
Add IVCProof to the existing folding schemes (Nova,HyperNova,ProtoGalaxy) (#167) * Add IVCProof to the existing folding schemes (Nova,HyperNova,ProtoGalaxy) * Implement `from_ivc_proof` for the FoldingSchemes trait (and Nova, HyperNova, ProtoGalaxy), so that the FoldingScheme IVC's instance can be constructed from the given parameters and the last IVCProof, which allows to sent the IVCProof between different parties, so that they can continue iterating the IVC from the received IVCProof. Also the serializers allow for the IVCProof to be sent to a verifier that can deserialize it and verify it. This allows to remove the logic from the file [folding/nova/serialize.rs](https://github.com/privacy-scaling-explorations/sonobe/blob/f1d82418ba047cf90805f2d0505370246df24d68/folding-schemes/src/folding/nova/serialize.rs) and [folding/hypernova/serialize.rs](https://github.com/privacy-scaling-explorations/sonobe/blob/f1d82418ba047cf90805f2d0505370246df24d68/folding-schemes/src/folding/hypernova/serialize.rs) (removing the whole files), which is now covered by the `IVCProof` generated serializers (generated by macro instead of handwritten), and the test that the file contained is now abstracted and applied to all the 3 existing folding schemes (Nova, HyperNova, ProtoGalaxy) at the folding/mod.rs file. * update Nova VerifierParams serializers to avoid serializing the R1CS to save big part of the old serialized size * rm .instances() since it's not needed * add nova params serialization to nova's ivc test to ensure that IVC verification works with deserialized data * Add unified FS::ProverParam & VerifierParam serialization & deserialization (for all Nova, HyperNova and ProtoGalaxy), without serializing the R1CS/CCS and thus saving substantial serialized bytes space. * rm CanonicalDeserialize warnings msgs for VerifierParams
1 month ago
  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.into(), wasm_path.into(), 1, 2);
  58. let f_circuit = CircomFCircuit::<Fr>::new(f_circuit_params).unwrap();
  59. pub type N =
  60. Nova<G1, GVar, G2, GVar2, CircomFCircuit<Fr>, KZG<'static, Bn254>, Pedersen<G2>, false>;
  61. pub type D = DeciderEth<
  62. G1,
  63. GVar,
  64. G2,
  65. GVar2,
  66. CircomFCircuit<Fr>,
  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, f_circuit.clone(), z_0).unwrap();
  79. // prepare the Decider prover & verifier params
  80. let (decider_pp, decider_vp) =
  81. D::preprocess(&mut rng, nova_params.clone(), nova.clone()).unwrap();
  82. // run n steps of the folding iteration
  83. for (i, external_inputs_at_step) in external_inputs.iter().enumerate() {
  84. let start = Instant::now();
  85. nova.prove_step(rng, external_inputs_at_step.clone(), None)
  86. .unwrap();
  87. println!("Nova::prove_step {}: {:?}", i, start.elapsed());
  88. }
  89. // verify the last IVC proof
  90. let ivc_proof = nova.ivc_proof();
  91. N::verify(
  92. nova_params.1, // Nova's verifier params
  93. ivc_proof,
  94. )
  95. .unwrap();
  96. let start = Instant::now();
  97. let proof = D::prove(rng, decider_pp, nova.clone()).unwrap();
  98. println!("generated Decider proof: {:?}", start.elapsed());
  99. let verified = D::verify(
  100. decider_vp.clone(),
  101. nova.i,
  102. nova.z_0.clone(),
  103. nova.z_i.clone(),
  104. &nova.U_i,
  105. &nova.u_i,
  106. &proof,
  107. )
  108. .unwrap();
  109. assert!(verified);
  110. println!("Decider proof verification: {}", verified);
  111. // Now, let's generate the Solidity code that verifies this Decider final proof
  112. let function_selector =
  113. get_function_selector_for_nova_cyclefold_verifier(nova.z_0.len() * 2 + 1);
  114. let calldata: Vec<u8> = prepare_calldata(
  115. function_selector,
  116. nova.i,
  117. nova.z_0,
  118. nova.z_i,
  119. &nova.U_i,
  120. &nova.u_i,
  121. proof,
  122. )
  123. .unwrap();
  124. // prepare the setup params for the solidity verifier
  125. let nova_cyclefold_vk = NovaCycleFoldVerifierKey::from((decider_vp, f_circuit.state_len()));
  126. // generate the solidity code
  127. let decider_solidity_code = get_decider_template_for_cyclefold_decider(nova_cyclefold_vk);
  128. // verify the proof against the solidity code in the EVM
  129. let nova_cyclefold_verifier_bytecode = compile_solidity(&decider_solidity_code, "NovaDecider");
  130. let mut evm = Evm::default();
  131. let verifier_address = evm.create(nova_cyclefold_verifier_bytecode);
  132. let (_, output) = evm.call(verifier_address, calldata.clone());
  133. assert_eq!(*output.last().unwrap(), 1);
  134. // save smart contract and the calldata
  135. println!("storing nova-verifier.sol and the calldata into files");
  136. use std::fs;
  137. fs::write(
  138. "./examples/nova-verifier.sol",
  139. decider_solidity_code.clone(),
  140. )
  141. .unwrap();
  142. fs::write("./examples/solidity-calldata.calldata", calldata.clone()).unwrap();
  143. let s = solidity_verifiers::utils::get_formatted_calldata(calldata.clone());
  144. fs::write("./examples/solidity-calldata.inputs", s.join(",\n")).expect("");
  145. }