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.

168 lines
5.7 KiB

Implemented Mova folding scheme (#161) * Adding Mova Co-Authored-By: Togzhan Barakbayeva <45527668+btogzhan2000@users.noreply.github.com> Co-Authored-By: Ilia Vlasov <5365540+elijahvlasov@users.noreply.github.com> Co-Authored-By: matthew-a-klein <96837318+matthew-a-klein@users.noreply.github.com> * Fix CLI * Updated from main * Solution to stop the CLI from complaining about deadcode PR comment Co-authored-by: arnaucube <root@arnaucube.com> * Requested changes and update from main * Refactor NIFSTrait & port Mova impl to it * refactor NIFSTrait interface to fit Nova variants (Nova,Mova,Ova) Refactor NIFSTrait interface to fit Nova variants (Nova,Mova,Ova). The relevant change is instead of passing the challenge as input, now it passes the transcript and computes the challenges internally (Nova & Ova still compute a single challenge, but Mova computes multiple while absorbing at different steps). * port Mova impl to the NIFSTrait * remove unnecessary wrappers in the nova/zk.rs * remove Nova NIFS methods that are no longer needed after the refactor * put together the different NIFS implementations (Nova, Mova, Ova) so that they can interchanged at usage. The idea is that Nova and its variants (Ova & Mova) share most of the logic for the circuits & IVC & Deciders, so with the abstracted NIFS interface we will be able to reuse most of the already existing Nova code for having the Mova & Ova circuits, IVC, and Decider. * adapt Nova's DeciderEth prepare_calldata & update examples to it * small update to fix solidity tests --------- Co-authored-by: Togzhan Barakbayeva <45527668+btogzhan2000@users.noreply.github.com> Co-authored-by: Ilia Vlasov <5365540+elijahvlasov@users.noreply.github.com> Co-authored-by: matthew-a-klein <96837318+matthew-a-klein@users.noreply.github.com> Co-authored-by: arnaucube <root@arnaucube.com> Co-authored-by: arnaucube <git@arnaucube.com>
2 months 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
2 months 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
2 months ago
Implemented Mova folding scheme (#161) * Adding Mova Co-Authored-By: Togzhan Barakbayeva <45527668+btogzhan2000@users.noreply.github.com> Co-Authored-By: Ilia Vlasov <5365540+elijahvlasov@users.noreply.github.com> Co-Authored-By: matthew-a-klein <96837318+matthew-a-klein@users.noreply.github.com> * Fix CLI * Updated from main * Solution to stop the CLI from complaining about deadcode PR comment Co-authored-by: arnaucube <root@arnaucube.com> * Requested changes and update from main * Refactor NIFSTrait & port Mova impl to it * refactor NIFSTrait interface to fit Nova variants (Nova,Mova,Ova) Refactor NIFSTrait interface to fit Nova variants (Nova,Mova,Ova). The relevant change is instead of passing the challenge as input, now it passes the transcript and computes the challenges internally (Nova & Ova still compute a single challenge, but Mova computes multiple while absorbing at different steps). * port Mova impl to the NIFSTrait * remove unnecessary wrappers in the nova/zk.rs * remove Nova NIFS methods that are no longer needed after the refactor * put together the different NIFS implementations (Nova, Mova, Ova) so that they can interchanged at usage. The idea is that Nova and its variants (Ova & Mova) share most of the logic for the circuits & IVC & Deciders, so with the abstracted NIFS interface we will be able to reuse most of the already existing Nova code for having the Mova & Ova circuits, IVC, and Decider. * adapt Nova's DeciderEth prepare_calldata & update examples to it * small update to fix solidity tests --------- Co-authored-by: Togzhan Barakbayeva <45527668+btogzhan2000@users.noreply.github.com> Co-authored-by: Ilia Vlasov <5365540+elijahvlasov@users.noreply.github.com> Co-authored-by: matthew-a-klein <96837318+matthew-a-klein@users.noreply.github.com> Co-authored-by: arnaucube <root@arnaucube.com> Co-authored-by: arnaucube <git@arnaucube.com>
2 months 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::FCircuit,
  24. transcript::poseidon::poseidon_canonical_config,
  25. Decider, FoldingScheme,
  26. };
  27. use frontends::circom::CircomFCircuit;
  28. use solidity_verifiers::{
  29. evm::{compile_solidity, Evm},
  30. utils::get_function_selector_for_nova_cyclefold_verifier,
  31. verifiers::nova_cyclefold::get_decider_template_for_cyclefold_decider,
  32. NovaCycleFoldVerifierKey,
  33. };
  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("./frontends/src/circom/test_folder/with_external_inputs.r1cs");
  53. let wasm_path = PathBuf::from(
  54. "./frontends/src/circom/test_folder/with_external_inputs_js/with_external_inputs.wasm",
  55. );
  56. let f_circuit_params = (r1cs_path.into(), wasm_path.into(), 1, 2);
  57. let f_circuit = CircomFCircuit::<Fr>::new(f_circuit_params).unwrap();
  58. pub type N =
  59. Nova<G1, GVar, G2, GVar2, CircomFCircuit<Fr>, KZG<'static, Bn254>, Pedersen<G2>, false>;
  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. let pp_hash = nova_params.1.pp_hash().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. pp_hash,
  117. nova.i,
  118. nova.z_0,
  119. nova.z_i,
  120. &nova.U_i,
  121. &nova.u_i,
  122. proof,
  123. )
  124. .unwrap();
  125. // prepare the setup params for the solidity verifier
  126. let nova_cyclefold_vk = NovaCycleFoldVerifierKey::from((decider_vp, f_circuit.state_len()));
  127. // generate the solidity code
  128. let decider_solidity_code = get_decider_template_for_cyclefold_decider(nova_cyclefold_vk);
  129. // verify the proof against the solidity code in the EVM
  130. let nova_cyclefold_verifier_bytecode = compile_solidity(&decider_solidity_code, "NovaDecider");
  131. let mut evm = Evm::default();
  132. let verifier_address = evm.create(nova_cyclefold_verifier_bytecode);
  133. let (_, output) = evm.call(verifier_address, calldata.clone());
  134. assert_eq!(*output.last().unwrap(), 1);
  135. // save smart contract and the calldata
  136. println!("storing nova-verifier.sol and the calldata into files");
  137. use std::fs;
  138. fs::write(
  139. "./examples/nova-verifier.sol",
  140. decider_solidity_code.clone(),
  141. )
  142. .unwrap();
  143. fs::write("./examples/solidity-calldata.calldata", calldata.clone()).unwrap();
  144. let s = solidity_verifiers::utils::get_formatted_calldata(calldata.clone());
  145. fs::write("./examples/solidity-calldata.inputs", s.join(",\n")).expect("");
  146. }