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.

50 lines
1.6 KiB

  1. #![allow(non_snake_case)]
  2. extern crate flate2;
  3. extern crate libspartan;
  4. extern crate merlin;
  5. extern crate rand;
  6. use flate2::{write::ZlibEncoder, Compression};
  7. use libspartan::{Instance, NIZKGens, NIZK};
  8. use merlin::Transcript;
  9. fn print(msg: &str) {
  10. let star = "* ";
  11. println!("{:indent$}{}{}", "", star, msg.to_string(), indent = 2);
  12. }
  13. pub fn main() {
  14. // the list of number of variables (and constraints) in an R1CS instance
  15. let inst_sizes = vec![10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
  16. println!("Profiler:: NIZK");
  17. for &s in inst_sizes.iter() {
  18. let num_vars = (2 as usize).pow(s as u32);
  19. let num_cons = num_vars;
  20. let num_inputs = 10;
  21. // produce a synthetic R1CSInstance
  22. let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  23. // produce public generators
  24. let gens = NIZKGens::new(num_cons, num_vars);
  25. // produce a proof of satisfiability
  26. let mut prover_transcript = Transcript::new(b"nizk_example");
  27. let proof = NIZK::prove(&inst, vars, &inputs, &gens, &mut prover_transcript);
  28. let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
  29. bincode::serialize_into(&mut encoder, &proof).unwrap();
  30. let proof_encoded = encoder.finish().unwrap();
  31. let msg_proof_len = format!("NIZK::proof_compressed_len {:?}", proof_encoded.len());
  32. print(&msg_proof_len);
  33. // verify the proof of satisfiability
  34. let mut verifier_transcript = Transcript::new(b"nizk_example");
  35. assert!(proof
  36. .verify(&inst, &inputs, &mut verifier_transcript, &gens)
  37. .is_ok());
  38. println!();
  39. }
  40. }