arnaucube
arnaucube/testudoPublic Self-hosted repository
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 libspartan::{Instance, NIZKGens, NIZK};
 8use merlin::Transcript;
 9use ark_serialize::*;
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 proof_encoded = Vec::new();
37    proof.serialize(&mut proof_encoded).unwrap();
38    let msg_proof_len = format!("NIZK::proof_compressed_len {:?}", proof_encoded.len());
39    print(&msg_proof_len);
40
41    // verify the proof of satisfiability
42    let mut verifier_transcript = Transcript::new(b"nizk_example");
43    assert!(proof
44      .verify(&inst, &inputs, &mut verifier_transcript, &gens)
45      .is_ok());
46
47    println!();
48  }
49}