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.

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