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.

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