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.

52 lines
1.7 KiB

  1. #![allow(non_snake_case)]
  2. extern crate flate2;
  3. extern crate libspartan;
  4. extern crate merlin;
  5. use flate2::{write::ZlibEncoder, Compression};
  6. use libspartan::{Instance, SNARKGens, SNARK};
  7. use merlin::Transcript;
  8. fn print(msg: &str) {
  9. let star = "* ";
  10. println!("{:indent$}{}{}", "", star, msg.to_string(), 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 as 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(&inst, &decomm, vars, &inputs, &gens, &mut prover_transcript);
  29. let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
  30. bincode::serialize_into(&mut encoder, &proof).unwrap();
  31. let proof_encoded = encoder.finish().unwrap();
  32. let msg_proof_len = format!("SNARK::proof_compressed_len {:?}", proof_encoded.len());
  33. print(&msg_proof_len);
  34. // verify the proof of satisfiability
  35. let mut verifier_transcript = Transcript::new(b"snark_example");
  36. assert!(proof
  37. .verify(&comm, &inputs, &mut verifier_transcript, &gens)
  38. .is_ok());
  39. println!();
  40. }
  41. }