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.

59 lines
1.6 KiB

  1. #![allow(non_snake_case)]
  2. extern crate flate2;
  3. extern crate libspartan;
  4. extern crate merlin;
  5. use libspartan::{Instance, SNARKGens, SNARK};
  6. use merlin::Transcript;
  7. use ark_serialize::*;
  8. fn print(msg: &str) {
  9. let star = "* ";
  10. println!("{:indent$}{}{}", "", star, msg, indent = 2);
  11. }
  12. pub fn main() {
  13. // the list of number of variables (and constraints) in an R1CS instance
  14. let inst_sizes = vec![10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
  15. println!("Profiler:: SNARK");
  16. for &s in inst_sizes.iter() {
  17. let num_vars = (2_usize).pow(s as u32);
  18. let num_cons = num_vars;
  19. let num_inputs = 10;
  20. // produce a synthetic R1CSInstance
  21. let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  22. // produce public generators
  23. let gens = SNARKGens::new(num_cons, num_vars, num_inputs, num_cons);
  24. // create a commitment to R1CSInstance
  25. let (comm, decomm) = SNARK::encode(&inst, &gens);
  26. // produce a proof of satisfiability
  27. let mut prover_transcript = Transcript::new(b"snark_example");
  28. let proof = SNARK::prove(
  29. &inst,
  30. &comm,
  31. &decomm,
  32. vars,
  33. &inputs,
  34. &gens,
  35. &mut prover_transcript,
  36. );
  37. let mut proof_encoded = Vec::new();
  38. proof.serialize(&mut proof_encoded).unwrap();
  39. let msg_proof_len = format!("SNARK::proof_compressed_len {:?}", proof_encoded.len());
  40. print(&msg_proof_len);
  41. // verify the proof of satisfiability
  42. let mut verifier_transcript = Transcript::new(b"snark_example");
  43. assert!(proof
  44. .verify(&comm, &inputs, &mut verifier_transcript, &gens)
  45. .is_ok());
  46. println!();
  47. }
  48. }