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