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