@ -1,47 +0,0 @@ |
|||
extern crate byteorder;
|
|||
extern crate core;
|
|||
extern crate criterion;
|
|||
extern crate digest;
|
|||
extern crate libspartan;
|
|||
extern crate merlin;
|
|||
extern crate rand;
|
|||
extern crate sha3;
|
|||
|
|||
use libspartan::commitments::{Commitments, MultiCommitGens};
|
|||
use libspartan::math::Math;
|
|||
use libspartan::scalar::Scalar;
|
|||
use rand::rngs::OsRng;
|
|||
|
|||
use criterion::*;
|
|||
|
|||
fn commitment_benchmark(c: &mut Criterion) {
|
|||
let mut rng = OsRng;
|
|||
for &s in [20].iter() {
|
|||
let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
|
|||
let mut group = c.benchmark_group("commitment_bools");
|
|||
group.plot_config(plot_config);
|
|||
|
|||
let n = (s as usize).pow2();
|
|||
let gens = MultiCommitGens::new(n, b"test-m");
|
|||
let blind = Scalar::random(&mut rng);
|
|||
let vec: Vec<bool> = vec![true; n];
|
|||
let name = format!("commitment_bools_{}", n);
|
|||
group.bench_function(&name, move |b| {
|
|||
b.iter(|| vec.commit(black_box(&blind), black_box(&gens)));
|
|||
});
|
|||
group.finish();
|
|||
}
|
|||
}
|
|||
|
|||
fn set_duration() -> Criterion {
|
|||
Criterion::default().sample_size(10)
|
|||
// .measurement_time(Duration::new(0, 50000000))
|
|||
}
|
|||
|
|||
criterion_group! {
|
|||
name = benches_commitment;
|
|||
config = set_duration();
|
|||
targets = commitment_benchmark
|
|||
}
|
|||
|
|||
criterion_main!(benches_commitment);
|
@ -1,85 +0,0 @@ |
|||
extern crate byteorder;
|
|||
extern crate core;
|
|||
extern crate criterion;
|
|||
extern crate digest;
|
|||
extern crate libspartan;
|
|||
extern crate merlin;
|
|||
extern crate rand;
|
|||
extern crate sha3;
|
|||
|
|||
use libspartan::math::Math;
|
|||
use libspartan::nizk::DotProductProof;
|
|||
use libspartan::scalar::Scalar;
|
|||
use libspartan::scalar::ScalarBytes;
|
|||
use rand::rngs::OsRng;
|
|||
|
|||
use criterion::*;
|
|||
|
|||
fn dotproduct_benchmark_dalek(c: &mut Criterion) {
|
|||
let mut csprng: OsRng = OsRng;
|
|||
|
|||
for &s in [20].iter() {
|
|||
let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
|
|||
let mut group = c.benchmark_group("dotproduct_benchmark_dalek");
|
|||
group.plot_config(plot_config);
|
|||
|
|||
let n = (s as usize).pow2();
|
|||
let vec_a = (0..n)
|
|||
.map(|_i| ScalarBytes::random(&mut csprng))
|
|||
.collect::<Vec<ScalarBytes>>();
|
|||
let vec_b = (0..n)
|
|||
.map(|_i| ScalarBytes::random(&mut csprng))
|
|||
.collect::<Vec<ScalarBytes>>();
|
|||
|
|||
let name = format!("dotproduct_dalek_{}", n);
|
|||
group.bench_function(&name, move |b| {
|
|||
b.iter(|| compute_dotproduct(black_box(&vec_a), black_box(&vec_b)));
|
|||
});
|
|||
group.finish();
|
|||
}
|
|||
}
|
|||
|
|||
fn compute_dotproduct(a: &Vec<ScalarBytes>, b: &Vec<ScalarBytes>) -> ScalarBytes {
|
|||
let mut res = ScalarBytes::zero();
|
|||
for i in 0..a.len() {
|
|||
res = &res + &a[i] * &b[i];
|
|||
}
|
|||
res
|
|||
}
|
|||
|
|||
fn dotproduct_benchmark_opt(c: &mut Criterion) {
|
|||
let mut csprng: OsRng = OsRng;
|
|||
|
|||
for &s in [20].iter() {
|
|||
let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
|
|||
let mut group = c.benchmark_group("dotproduct_benchmark_opt");
|
|||
group.plot_config(plot_config);
|
|||
|
|||
let n = (s as usize).pow2();
|
|||
let vec_a = (0..n)
|
|||
.map(|_i| Scalar::random(&mut csprng))
|
|||
.collect::<Vec<Scalar>>();
|
|||
let vec_b = (0..n)
|
|||
.map(|_i| Scalar::random(&mut csprng))
|
|||
.collect::<Vec<Scalar>>();
|
|||
|
|||
let name = format!("dotproduct_opt_{}", n);
|
|||
group.bench_function(&name, move |b| {
|
|||
b.iter(|| DotProductProof::compute_dotproduct(black_box(&vec_a), black_box(&vec_b)));
|
|||
});
|
|||
group.finish();
|
|||
}
|
|||
}
|
|||
|
|||
fn set_duration() -> Criterion {
|
|||
Criterion::default().sample_size(10)
|
|||
// .measurement_time(Duration::new(0, 50000000))
|
|||
}
|
|||
|
|||
criterion_group! {
|
|||
name = benches_dotproduct;
|
|||
config = set_duration();
|
|||
targets = dotproduct_benchmark_dalek, dotproduct_benchmark_opt
|
|||
}
|
|||
|
|||
criterion_main!(benches_dotproduct);
|
@ -1,191 +0,0 @@ |
|||
extern crate byteorder;
|
|||
extern crate core;
|
|||
extern crate criterion;
|
|||
extern crate digest;
|
|||
extern crate libspartan;
|
|||
extern crate merlin;
|
|||
extern crate rand;
|
|||
extern crate sha3;
|
|||
|
|||
use criterion::*;
|
|||
use libspartan::dense_mlpoly::{DensePolynomial, PolyCommitmentGens, PolyEvalProof};
|
|||
use libspartan::math::Math;
|
|||
use libspartan::random::RandomTape;
|
|||
use libspartan::scalar::Scalar;
|
|||
use libspartan::transcript::ProofTranscript;
|
|||
use merlin::Transcript;
|
|||
use rand::rngs::OsRng;
|
|||
|
|||
fn commit_benchmark(c: &mut Criterion) {
|
|||
let mut csprng: OsRng = OsRng;
|
|||
|
|||
for &s in [4, 8, 12, 14, 16, 20].iter() {
|
|||
let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
|
|||
let mut group = c.benchmark_group("commit_benchmark");
|
|||
group.plot_config(plot_config);
|
|||
|
|||
let n = (s as usize).pow2();
|
|||
let m = n.square_root();
|
|||
let z = (0..n)
|
|||
.map(|_i| Scalar::random(&mut csprng))
|
|||
.collect::<Vec<Scalar>>();
|
|||
assert_eq!(m * m, z.len()); // check if Z's size if a perfect square
|
|||
|
|||
let poly = DensePolynomial::new(z);
|
|||
let gens = PolyCommitmentGens::new(s, b"test-m");
|
|||
let name = format!("polycommit_commit_{}", n);
|
|||
group.bench_function(&name, move |b| {
|
|||
b.iter(|| poly.commit(black_box(false), black_box(&gens), black_box(None)));
|
|||
});
|
|||
group.finish();
|
|||
}
|
|||
}
|
|||
|
|||
fn eval_benchmark(c: &mut Criterion) {
|
|||
let mut csprng: OsRng = OsRng;
|
|||
|
|||
for &s in [4, 8, 12, 14, 16, 20].iter() {
|
|||
let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
|
|||
let mut group = c.benchmark_group("eval_benchmark");
|
|||
group.plot_config(plot_config);
|
|||
|
|||
let n = (s as usize).pow2();
|
|||
let m = n.square_root();
|
|||
let mut z: Vec<Scalar> = Vec::new();
|
|||
for _ in 0..n {
|
|||
z.push(Scalar::random(&mut csprng));
|
|||
}
|
|||
assert_eq!(m * m, z.len()); // check if Z's size if a perfect square
|
|||
|
|||
let poly = DensePolynomial::new(z);
|
|||
|
|||
let mut r: Vec<Scalar> = Vec::new();
|
|||
for _ in 0..s {
|
|||
r.push(Scalar::random(&mut csprng));
|
|||
}
|
|||
|
|||
let name = format!("polycommit_eval_{}", n);
|
|||
group.bench_function(&name, move |b| {
|
|||
b.iter(|| poly.evaluate(black_box(&r)));
|
|||
});
|
|||
group.finish();
|
|||
}
|
|||
}
|
|||
|
|||
fn evalproof_benchmark(c: &mut Criterion) {
|
|||
let mut csprng: OsRng = OsRng;
|
|||
|
|||
for &s in [4, 8, 12, 14, 16, 20].iter() {
|
|||
let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
|
|||
let mut group = c.benchmark_group("evalproof_benchmark");
|
|||
group.plot_config(plot_config);
|
|||
|
|||
let n = (s as usize).pow2();
|
|||
let m = n.square_root();
|
|||
let mut z: Vec<Scalar> = Vec::new();
|
|||
for _ in 0..n {
|
|||
z.push(Scalar::random(&mut csprng));
|
|||
}
|
|||
assert_eq!(m * m, z.len()); // check if Z's size if a perfect square
|
|||
|
|||
let poly = DensePolynomial::new(z);
|
|||
|
|||
let gens = PolyCommitmentGens::new(s, b"test-m");
|
|||
|
|||
let mut r: Vec<Scalar> = Vec::new();
|
|||
for _ in 0..s {
|
|||
r.push(Scalar::random(&mut csprng));
|
|||
}
|
|||
|
|||
let eval = poly.evaluate(&r);
|
|||
|
|||
let name = format!("polycommit_evalproof_{}", n);
|
|||
group.bench_function(&name, move |b| {
|
|||
b.iter(|| {
|
|||
let mut random_tape = RandomTape::new(b"proof");
|
|||
let mut prover_transcript = Transcript::new(b"example");
|
|||
PolyEvalProof::prove(
|
|||
black_box(&poly),
|
|||
black_box(None),
|
|||
black_box(&r),
|
|||
black_box(&eval),
|
|||
black_box(None),
|
|||
black_box(&gens),
|
|||
black_box(&mut prover_transcript),
|
|||
black_box(&mut random_tape),
|
|||
)
|
|||
});
|
|||
});
|
|||
group.finish();
|
|||
}
|
|||
}
|
|||
|
|||
fn evalproofverify_benchmark(c: &mut Criterion) {
|
|||
let mut csprng: OsRng = OsRng;
|
|||
|
|||
for &s in [4, 8, 12, 14, 16, 20].iter() {
|
|||
let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
|
|||
let mut group = c.benchmark_group("evalproofverify_benchmark");
|
|||
group.plot_config(plot_config);
|
|||
|
|||
let n = s.pow2();
|
|||
let m = n.square_root();
|
|||
let mut z: Vec<Scalar> = Vec::new();
|
|||
for _ in 0..n {
|
|||
z.push(Scalar::random(&mut csprng));
|
|||
}
|
|||
assert_eq!(m * m, z.len()); // check if Z's size if a perfect square
|
|||
|
|||
let poly = DensePolynomial::new(z);
|
|||
let gens = PolyCommitmentGens::new(s, b"test-m");
|
|||
|
|||
let mut r: Vec<Scalar> = Vec::new();
|
|||
for _ in 0..s {
|
|||
r.push(Scalar::random(&mut csprng));
|
|||
}
|
|||
|
|||
let (poly_commitment, blinds) = poly.commit(false, &gens, None);
|
|||
let eval = poly.evaluate(&r);
|
|||
|
|||
let mut random_tape = RandomTape::new(b"proof");
|
|||
let mut prover_transcript = Transcript::new(b"example");
|
|||
let (proof, c_zr) = PolyEvalProof::prove(
|
|||
black_box(&poly),
|
|||
black_box(Some(&blinds)),
|
|||
black_box(&r),
|
|||
black_box(&eval),
|
|||
black_box(None),
|
|||
black_box(&gens),
|
|||
black_box(&mut prover_transcript),
|
|||
black_box(&mut random_tape),
|
|||
);
|
|||
let name = format!("polycommit_evalproofverify_{}", n);
|
|||
group.bench_function(&name, move |b| {
|
|||
b.iter(|| {
|
|||
let mut verifier_transcript = Transcript::new(b"example");
|
|||
|
|||
proof.verify(
|
|||
black_box(&gens),
|
|||
black_box(&mut verifier_transcript),
|
|||
black_box(&r),
|
|||
black_box(&c_zr),
|
|||
black_box(&poly_commitment),
|
|||
)
|
|||
});
|
|||
});
|
|||
group.finish();
|
|||
}
|
|||
}
|
|||
|
|||
fn set_duration() -> Criterion {
|
|||
Criterion::default().sample_size(10)
|
|||
// .measurement_time(Duration::new(0, 50000000))
|
|||
}
|
|||
|
|||
criterion_group! {
|
|||
name = benches_polycommit;
|
|||
config = set_duration();
|
|||
targets = commit_benchmark, eval_benchmark, evalproof_benchmark, evalproofverify_benchmark
|
|||
}
|
|||
|
|||
criterion_main!(benches_polycommit);
|
@ -0,0 +1,130 @@ |
|||
extern crate byteorder;
|
|||
extern crate core;
|
|||
extern crate criterion;
|
|||
extern crate digest;
|
|||
extern crate libspartan;
|
|||
extern crate merlin;
|
|||
extern crate rand;
|
|||
extern crate sha3;
|
|||
|
|||
use libspartan::r1csinstance::R1CSInstance;
|
|||
use libspartan::spartan::{SNARKGens, SNARK};
|
|||
use merlin::Transcript;
|
|||
|
|||
use criterion::*;
|
|||
|
|||
fn snark_encode_benchmark(c: &mut Criterion) {
|
|||
for &s in [10, 12, 16].iter() {
|
|||
let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
|
|||
let mut group = c.benchmark_group("SNARK_encode_benchmark");
|
|||
group.plot_config(plot_config);
|
|||
|
|||
let num_vars = (2 as usize).pow(s as u32);
|
|||
let num_cons = num_vars;
|
|||
let num_inputs = 10;
|
|||
let (inst, _vars, _input) =
|
|||
R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
|
|||
let n = inst.get_num_vars();
|
|||
|
|||
// produce public parameters
|
|||
let gens = SNARKGens::new(&inst.size());
|
|||
|
|||
let name = format!("SNARK_encode_{}", n);
|
|||
group.bench_function(&name, move |b| {
|
|||
b.iter(|| {
|
|||
SNARK::encode(black_box(&inst), black_box(&gens));
|
|||
});
|
|||
});
|
|||
group.finish();
|
|||
}
|
|||
}
|
|||
|
|||
fn snark_prove_benchmark(c: &mut Criterion) {
|
|||
for &s in [10, 12, 16].iter() {
|
|||
let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
|
|||
let mut group = c.benchmark_group("SNARK_prove_benchmark");
|
|||
group.plot_config(plot_config);
|
|||
|
|||
let num_vars = (2 as usize).pow(s as u32);
|
|||
let num_cons = num_vars;
|
|||
let num_inputs = 10;
|
|||
|
|||
let (inst, vars, input) = R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
|
|||
let n = inst.get_num_vars();
|
|||
|
|||
// produce public parameters
|
|||
let gens = SNARKGens::new(&inst.size());
|
|||
|
|||
// encode the R1CS instance
|
|||
let (_comm, decomm) = SNARK::encode(&inst, &gens);
|
|||
|
|||
// produce a proof
|
|||
let name = format!("SNARK_prove_{}", n);
|
|||
group.bench_function(&name, move |b| {
|
|||
b.iter(|| {
|
|||
let mut prover_transcript = Transcript::new(b"example");
|
|||
SNARK::prove(
|
|||
black_box(&inst),
|
|||
black_box(&decomm),
|
|||
black_box(vars.clone()),
|
|||
black_box(&input),
|
|||
black_box(&gens),
|
|||
black_box(&mut prover_transcript),
|
|||
);
|
|||
});
|
|||
});
|
|||
group.finish();
|
|||
}
|
|||
}
|
|||
|
|||
fn snark_verify_benchmark(c: &mut Criterion) {
|
|||
for &s in [10, 12, 16].iter() {
|
|||
let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
|
|||
let mut group = c.benchmark_group("SNARK_verify_benchmark");
|
|||
group.plot_config(plot_config);
|
|||
|
|||
let num_vars = (2 as usize).pow(s as u32);
|
|||
let num_cons = num_vars;
|
|||
let num_inputs = 10;
|
|||
let (inst, vars, input) = R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
|
|||
let n = inst.get_num_vars();
|
|||
|
|||
// produce public parameters
|
|||
let gens = SNARKGens::new(&inst.size());
|
|||
|
|||
// encode the R1CS instance
|
|||
let (comm, decomm) = SNARK::encode(&inst, &gens);
|
|||
|
|||
// produce a proof of satisfiability
|
|||
let mut prover_transcript = Transcript::new(b"example");
|
|||
let proof = SNARK::prove(&inst, &decomm, vars, &input, &gens, &mut prover_transcript);
|
|||
|
|||
let name = format!("SNARK_verify_{}", n);
|
|||
group.bench_function(&name, move |b| {
|
|||
b.iter(|| {
|
|||
let mut verifier_transcript = Transcript::new(b"example");
|
|||
assert!(proof
|
|||
.verify(
|
|||
black_box(&comm),
|
|||
black_box(&input),
|
|||
black_box(&mut verifier_transcript),
|
|||
black_box(&gens)
|
|||
)
|
|||
.is_ok());
|
|||
});
|
|||
});
|
|||
group.finish();
|
|||
}
|
|||
}
|
|||
|
|||
fn set_duration() -> Criterion {
|
|||
Criterion::default().sample_size(10)
|
|||
}
|
|||
|
|||
criterion_group! {
|
|||
name = benches_snark;
|
|||
config = set_duration();
|
|||
targets = snark_encode_benchmark, snark_prove_benchmark, snark_verify_benchmark
|
|||
}
|
|||
|
|||
criterion_main!(benches_snark);
|
@ -1,206 +0,0 @@ |
|||
extern crate byteorder;
|
|||
extern crate core;
|
|||
extern crate criterion;
|
|||
extern crate digest;
|
|||
extern crate libspartan;
|
|||
extern crate merlin;
|
|||
extern crate rand;
|
|||
extern crate sha3;
|
|||
|
|||
use libspartan::math::Math;
|
|||
use libspartan::r1csinstance::{R1CSCommitmentGens, R1CSInstance};
|
|||
use libspartan::r1csproof::R1CSGens;
|
|||
use libspartan::spartan::{NIZKGens, SNARKGens, NIZK, SNARK};
|
|||
use merlin::Transcript;
|
|||
|
|||
use criterion::*;
|
|||
|
|||
fn snark_encode_benchmark(c: &mut Criterion) {
|
|||
for &s in [10, 12, 16].iter() {
|
|||
let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
|
|||
let mut group = c.benchmark_group("SNARK_encode_benchmark");
|
|||
group.plot_config(plot_config);
|
|||
|
|||
let num_vars = s.pow2();
|
|||
let num_cons = num_vars;
|
|||
let num_inputs = 10;
|
|||
let (inst, _vars, _input) =
|
|||
R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
|
|||
let n = inst.get_num_vars();
|
|||
let r1cs_size = inst.size();
|
|||
let gens_r1cs = R1CSCommitmentGens::new(&r1cs_size, b"gens_r1cs");
|
|||
|
|||
let name = format!("SNARK_encode_{}", n);
|
|||
group.bench_function(&name, move |b| {
|
|||
b.iter(|| {
|
|||
SNARK::encode(black_box(&inst), black_box(&gens_r1cs));
|
|||
});
|
|||
});
|
|||
group.finish();
|
|||
}
|
|||
}
|
|||
|
|||
fn snark_prove_benchmark(c: &mut Criterion) {
|
|||
for &s in [10, 12, 16].iter() {
|
|||
let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
|
|||
let mut group = c.benchmark_group("SNARK_prove_benchmark");
|
|||
group.plot_config(plot_config);
|
|||
|
|||
let num_vars = s.pow2();
|
|||
let num_cons = num_vars;
|
|||
let num_inputs = 10;
|
|||
|
|||
let (inst, vars, input) = R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
|
|||
let n = inst.get_num_vars();
|
|||
|
|||
let r1cs_size = inst.size();
|
|||
let gens_r1cs_eval = R1CSCommitmentGens::new(&r1cs_size, b"gens_r1cs_eval");
|
|||
let gens_r1cs_sat = R1CSGens::new(num_cons, num_vars, b"gens_r1cs_sat");
|
|||
|
|||
// produce a proof of satisfiability
|
|||
let (_comm, decomm) = SNARK::encode(&inst, &gens_r1cs_eval);
|
|||
let gens = SNARKGens::new(gens_r1cs_sat, gens_r1cs_eval);
|
|||
|
|||
let name = format!("SNARK_prove_{}", n);
|
|||
group.bench_function(&name, move |b| {
|
|||
b.iter(|| {
|
|||
let mut prover_transcript = Transcript::new(b"example");
|
|||
SNARK::prove(
|
|||
black_box(&inst),
|
|||
black_box(&decomm),
|
|||
black_box(vars.clone()),
|
|||
black_box(&input),
|
|||
black_box(&gens),
|
|||
black_box(&mut prover_transcript),
|
|||
);
|
|||
});
|
|||
});
|
|||
group.finish();
|
|||
}
|
|||
}
|
|||
|
|||
fn snark_verify_benchmark(c: &mut Criterion) {
|
|||
for &s in [10, 12, 16].iter() {
|
|||
let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
|
|||
let mut group = c.benchmark_group("SNARK_verify_benchmark");
|
|||
group.plot_config(plot_config);
|
|||
|
|||
let num_vars = s.pow2();
|
|||
let num_cons = num_vars;
|
|||
let num_inputs = 10;
|
|||
let (inst, vars, input) = R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
|
|||
let n = inst.get_num_vars();
|
|||
|
|||
let r1cs_size = inst.size();
|
|||
let gens_r1cs_eval = R1CSCommitmentGens::new(&r1cs_size, b"gens_r1cs_eval");
|
|||
|
|||
// create a commitment to R1CSInstance
|
|||
let (comm, decomm) = SNARK::encode(&inst, &gens_r1cs_eval);
|
|||
|
|||
let gens_r1cs_sat = R1CSGens::new(num_cons, num_vars, b"gens_r1cs_sat");
|
|||
let gens = SNARKGens::new(gens_r1cs_sat, gens_r1cs_eval);
|
|||
|
|||
// produce a proof of satisfiability
|
|||
let mut prover_transcript = Transcript::new(b"example");
|
|||
let proof = SNARK::prove(&inst, &decomm, vars, &input, &gens, &mut prover_transcript);
|
|||
|
|||
let name = format!("SNARK_verify_{}", n);
|
|||
group.bench_function(&name, move |b| {
|
|||
b.iter(|| {
|
|||
let mut verifier_transcript = Transcript::new(b"example");
|
|||
assert!(proof
|
|||
.verify(
|
|||
black_box(&comm),
|
|||
black_box(&input),
|
|||
black_box(&mut verifier_transcript),
|
|||
black_box(&gens)
|
|||
)
|
|||
.is_ok());
|
|||
});
|
|||
});
|
|||
group.finish();
|
|||
}
|
|||
}
|
|||
|
|||
fn nizk_prove_benchmark(c: &mut Criterion) {
|
|||
for &s in [10, 12, 16].iter() {
|
|||
let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
|
|||
let mut group = c.benchmark_group("NIZK_prove_benchmark");
|
|||
group.plot_config(plot_config);
|
|||
|
|||
let num_vars = s.pow2();
|
|||
let num_cons = num_vars;
|
|||
let num_inputs = 10;
|
|||
|
|||
let (inst, vars, input) = R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
|
|||
let n = inst.get_num_vars();
|
|||
|
|||
let gens_r1cs_sat = R1CSGens::new(num_cons, num_vars, b"gens_r1cs_sat");
|
|||
let gens = NIZKGens::new(gens_r1cs_sat);
|
|||
|
|||
let name = format!("NIZK_prove_{}", n);
|
|||
group.bench_function(&name, move |b| {
|
|||
b.iter(|| {
|
|||
let mut prover_transcript = Transcript::new(b"example");
|
|||
NIZK::prove(
|
|||
black_box(&inst),
|
|||
black_box(vars.clone()),
|
|||
black_box(&input),
|
|||
black_box(&gens),
|
|||
black_box(&mut prover_transcript),
|
|||
);
|
|||
});
|
|||
});
|
|||
group.finish();
|
|||
}
|
|||
}
|
|||
|
|||
fn nizk_verify_benchmark(c: &mut Criterion) {
|
|||
for &s in [10, 12, 16].iter() {
|
|||
let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
|
|||
let mut group = c.benchmark_group("NIZK_verify_benchmark");
|
|||
group.plot_config(plot_config);
|
|||
|
|||
let num_vars = s.pow2();
|
|||
let num_cons = num_vars;
|
|||
let num_inputs = 10;
|
|||
let (inst, vars, input) = R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
|
|||
let n = inst.get_num_vars();
|
|||
|
|||
let gens_r1cs_sat = R1CSGens::new(num_cons, num_vars, b"gens_r1cs_sat");
|
|||
let gens = NIZKGens::new(gens_r1cs_sat);
|
|||
|
|||
// produce a proof of satisfiability
|
|||
let mut prover_transcript = Transcript::new(b"example");
|
|||
let proof = NIZK::prove(&inst, vars, &input, &gens, &mut prover_transcript);
|
|||
|
|||
let name = format!("NIZK_verify_{}", n);
|
|||
group.bench_function(&name, move |b| {
|
|||
b.iter(|| {
|
|||
let mut verifier_transcript = Transcript::new(b"example");
|
|||
assert!(proof
|
|||
.verify(
|
|||
black_box(&inst),
|
|||
black_box(&input),
|
|||
black_box(&mut verifier_transcript),
|
|||
black_box(&gens)
|
|||
)
|
|||
.is_ok());
|
|||
});
|
|||
});
|
|||
group.finish();
|
|||
}
|
|||
}
|
|||
|
|||
fn set_duration() -> Criterion {
|
|||
Criterion::default().sample_size(10)
|
|||
// .measurement_time(Duration::new(0, 50000000))
|
|||
}
|
|||
|
|||
criterion_group! {
|
|||
name = benches_spartan;
|
|||
config = set_duration();
|
|||
targets = snark_encode_benchmark, snark_prove_benchmark, snark_verify_benchmark, nizk_prove_benchmark, nizk_verify_benchmark
|
|||
}
|
|||
|
|||
criterion_main!(benches_spartan);
|
@ -1,152 +0,0 @@ |
|||
#![allow(non_snake_case)]
|
|||
|
|||
extern crate byteorder;
|
|||
extern crate core;
|
|||
extern crate criterion;
|
|||
extern crate digest;
|
|||
extern crate libspartan;
|
|||
extern crate merlin;
|
|||
extern crate rand;
|
|||
extern crate sha3;
|
|||
|
|||
use libspartan::commitments::Commitments;
|
|||
use libspartan::commitments::MultiCommitGens;
|
|||
use libspartan::dense_mlpoly::DensePolynomial;
|
|||
use libspartan::math::Math;
|
|||
use libspartan::nizk::DotProductProof;
|
|||
use libspartan::random::RandomTape;
|
|||
use libspartan::scalar::Scalar;
|
|||
use libspartan::sumcheck::ZKSumcheckInstanceProof;
|
|||
use libspartan::transcript::ProofTranscript;
|
|||
use merlin::Transcript;
|
|||
use rand::rngs::OsRng;
|
|||
|
|||
use criterion::*;
|
|||
|
|||
fn prove_benchmark(c: &mut Criterion) {
|
|||
for &s in [10, 12, 16, 20].iter() {
|
|||
let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
|
|||
let mut group = c.benchmark_group("zksumcheck_prove_benchmark");
|
|||
group.plot_config(plot_config);
|
|||
|
|||
// produce tables
|
|||
let gens_n = MultiCommitGens::new(3, b"test-m");
|
|||
let gens_1 = MultiCommitGens::new(1, b"test-1");
|
|||
let num_rounds = s;
|
|||
let n = s.pow2();
|
|||
|
|||
let mut csprng: OsRng = OsRng;
|
|||
|
|||
let vec_A = (0..n)
|
|||
.map(|_i| Scalar::random(&mut csprng))
|
|||
.collect::<Vec<Scalar>>();
|
|||
let vec_B = (0..n)
|
|||
.map(|_i| Scalar::random(&mut csprng))
|
|||
.collect::<Vec<Scalar>>();
|
|||
let claim = DotProductProof::compute_dotproduct(&vec_A, &vec_B);
|
|||
let mut poly_A = DensePolynomial::new(vec_A);
|
|||
let mut poly_B = DensePolynomial::new(vec_B);
|
|||
|
|||
let blind_claim = Scalar::random(&mut csprng);
|
|||
let comb_func =
|
|||
|poly_A_comp: &Scalar, poly_B_comp: &Scalar| -> Scalar { poly_A_comp * poly_B_comp };
|
|||
|
|||
let name = format!("zksumcheck_prove_{}", n);
|
|||
group.bench_function(&name, move |b| {
|
|||
b.iter(|| {
|
|||
let mut random_tape = RandomTape::new(b"proof");
|
|||
let mut prover_transcript = Transcript::new(b"example");
|
|||
ZKSumcheckInstanceProof::prove_quad(
|
|||
black_box(&claim),
|
|||
black_box(&blind_claim),
|
|||
black_box(num_rounds),
|
|||
black_box(&mut poly_A),
|
|||
black_box(&mut poly_B),
|
|||
black_box(comb_func),
|
|||
black_box(&gens_1),
|
|||
black_box(&gens_n),
|
|||
black_box(&mut prover_transcript),
|
|||
black_box(&mut random_tape),
|
|||
)
|
|||
});
|
|||
});
|
|||
group.finish();
|
|||
}
|
|||
}
|
|||
|
|||
fn verify_benchmark(c: &mut Criterion) {
|
|||
for &s in [10, 12, 16, 20].iter() {
|
|||
let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
|
|||
let mut group = c.benchmark_group("zksumcheck_verify_benchmark");
|
|||
group.plot_config(plot_config);
|
|||
|
|||
// produce tables
|
|||
let gens_n = MultiCommitGens::new(3, b"test-m");
|
|||
let gens_1 = MultiCommitGens::new(1, b"test-1");
|
|||
let num_rounds = s;
|
|||
let n = s.pow2();
|
|||
|
|||
let mut csprng: OsRng = OsRng;
|
|||
|
|||
let vec_A = (0..n)
|
|||
.map(|_i| Scalar::random(&mut csprng))
|
|||
.collect::<Vec<Scalar>>();
|
|||
let vec_B = (0..n)
|
|||
.map(|_i| Scalar::random(&mut csprng))
|
|||
.collect::<Vec<Scalar>>();
|
|||
let claim = DotProductProof::compute_dotproduct(&vec_A, &vec_B);
|
|||
let mut poly_A = DensePolynomial::new(vec_A);
|
|||
let mut poly_B = DensePolynomial::new(vec_B);
|
|||
let blind_claim = Scalar::random(&mut csprng);
|
|||
let comb_func =
|
|||
|poly_A_comp: &Scalar, poly_B_comp: &Scalar| -> Scalar { poly_A_comp * poly_B_comp };
|
|||
|
|||
let mut random_tape = RandomTape::new(b"proof");
|
|||
let mut prover_transcript = Transcript::new(b"example");
|
|||
let (proof, _r, _v, _blind_post_claim) = ZKSumcheckInstanceProof::prove_quad(
|
|||
&claim,
|
|||
&blind_claim,
|
|||
num_rounds,
|
|||
&mut poly_A,
|
|||
&mut poly_B,
|
|||
comb_func,
|
|||
&gens_1,
|
|||
&gens_n,
|
|||
&mut prover_transcript,
|
|||
&mut random_tape,
|
|||
);
|
|||
|
|||
let name = format!("zksumcheck_verify_{}", n);
|
|||
let degree_bound = 2;
|
|||
let comm_claim = claim.commit(&blind_claim, &gens_1).compress();
|
|||
group.bench_function(&name, move |b| {
|
|||
b.iter(|| {
|
|||
let mut verifier_transcript = Transcript::new(b"example");
|
|||
assert!(proof
|
|||
.verify(
|
|||
black_box(&comm_claim),
|
|||
black_box(num_rounds),
|
|||
black_box(degree_bound),
|
|||
black_box(&gens_1),
|
|||
black_box(&gens_n),
|
|||
black_box(&mut verifier_transcript)
|
|||
)
|
|||
.is_ok())
|
|||
});
|
|||
});
|
|||
group.finish();
|
|||
}
|
|||
}
|
|||
|
|||
fn set_duration() -> Criterion {
|
|||
Criterion::default().sample_size(10)
|
|||
// .measurement_time(Duration::new(0, 50000000))
|
|||
}
|
|||
|
|||
criterion_group! {
|
|||
name = benches_r1cs;
|
|||
config = set_duration();
|
|||
targets = verify_benchmark, prove_benchmark
|
|||
}
|
|||
|
|||
criterion_main!(benches_r1cs);
|
@ -0,0 +1,51 @@ |
|||
#![allow(non_snake_case)]
|
|||
extern crate flate2;
|
|||
extern crate libspartan;
|
|||
extern crate merlin;
|
|||
extern crate rand;
|
|||
|
|||
use flate2::{write::ZlibEncoder, Compression};
|
|||
use libspartan::r1csinstance::R1CSInstance;
|
|||
use libspartan::spartan::{NIZKGens, NIZK};
|
|||
use libspartan::timer::Timer;
|
|||
use merlin::Transcript;
|
|||
|
|||
pub fn main() {
|
|||
// the list of number of variables (and constraints) in an R1CS instance
|
|||
let inst_sizes = vec![12, 16, 20];
|
|||
|
|||
println!("Profiler:: NIZK");
|
|||
for &s in inst_sizes.iter() {
|
|||
let num_vars = (2 as usize).pow(s as u32);
|
|||
let num_cons = num_vars;
|
|||
let num_inputs = 10;
|
|||
let (inst, vars, input) = R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
|
|||
|
|||
Timer::print(&format!("number_of_constraints {}", num_cons));
|
|||
|
|||
// produce public generators
|
|||
let gens = NIZKGens::new(num_cons, num_vars);
|
|||
|
|||
// produce a proof of satisfiability
|
|||
let timer_prove = Timer::new("NIZK::prove");
|
|||
let mut prover_transcript = Transcript::new(b"example");
|
|||
let proof = NIZK::prove(&inst, vars, &input, &gens, &mut prover_transcript);
|
|||
timer_prove.stop();
|
|||
|
|||
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
|
|||
bincode::serialize_into(&mut encoder, &proof).unwrap();
|
|||
let proof_encoded = encoder.finish().unwrap();
|
|||
let msg_proof_len = format!("NIZK::proof_compressed_len {:?}", proof_encoded.len());
|
|||
Timer::print(&msg_proof_len);
|
|||
|
|||
// verify the proof of satisfiability
|
|||
let timer_verify = Timer::new("NIZK::verify");
|
|||
let mut verifier_transcript = Transcript::new(b"example");
|
|||
assert!(proof
|
|||
.verify(&inst, &input, &mut verifier_transcript, &gens)
|
|||
.is_ok());
|
|||
timer_verify.stop();
|
|||
|
|||
println!();
|
|||
}
|
|||
}
|
@ -0,0 +1,56 @@ |
|||
#![allow(non_snake_case)]
|
|||
extern crate flate2;
|
|||
extern crate libspartan;
|
|||
extern crate merlin;
|
|||
extern crate rand;
|
|||
|
|||
use flate2::{write::ZlibEncoder, Compression};
|
|||
use libspartan::r1csinstance::R1CSInstance;
|
|||
use libspartan::spartan::{SNARKGens, SNARK};
|
|||
use libspartan::timer::Timer;
|
|||
use merlin::Transcript;
|
|||
|
|||
pub fn main() {
|
|||
// the list of number of variables (and constraints) in an R1CS instance
|
|||
let inst_sizes = vec![12, 16, 20];
|
|||
|
|||
println!("Profiler:: SNARK");
|
|||
for &s in inst_sizes.iter() {
|
|||
let num_vars = (2 as usize).pow(s as u32);
|
|||
let num_cons = num_vars;
|
|||
let num_inputs = 10;
|
|||
let (inst, vars, input) = R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
|
|||
|
|||
Timer::print(&format!("number_of_constraints {}", num_cons));
|
|||
|
|||
// produce public generators
|
|||
let gens = SNARKGens::new(&inst.size());
|
|||
|
|||
// create a commitment to R1CSInstance
|
|||
let timer_encode = Timer::new("SNARK::encode");
|
|||
let (comm, decomm) = SNARK::encode(&inst, &gens);
|
|||
timer_encode.stop();
|
|||
|
|||
// produce a proof of satisfiability
|
|||
let timer_prove = Timer::new("SNARK::prove");
|
|||
let mut prover_transcript = Transcript::new(b"example");
|
|||
let proof = SNARK::prove(&inst, &decomm, vars, &input, &gens, &mut prover_transcript);
|
|||
timer_prove.stop();
|
|||
|
|||
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
|
|||
bincode::serialize_into(&mut encoder, &proof).unwrap();
|
|||
let proof_encoded = encoder.finish().unwrap();
|
|||
let msg_proof_len = format!("SNARK::proof_compressed_len {:?}", proof_encoded.len());
|
|||
Timer::print(&msg_proof_len);
|
|||
|
|||
// verify the proof of satisfiability
|
|||
let timer_verify = Timer::new("SNARK::verify");
|
|||
let mut verifier_transcript = Transcript::new(b"example");
|
|||
assert!(proof
|
|||
.verify(&comm, &input, &mut verifier_transcript, &gens)
|
|||
.is_ok());
|
|||
timer_verify.stop();
|
|||
|
|||
println!();
|
|||
}
|
|||
}
|
@ -1,96 +0,0 @@ |
|||
#![allow(non_snake_case)]
|
|||
extern crate flate2;
|
|||
extern crate libspartan;
|
|||
extern crate merlin;
|
|||
extern crate rand;
|
|||
|
|||
use flate2::{write::ZlibEncoder, Compression};
|
|||
use libspartan::math::Math;
|
|||
use libspartan::r1csinstance::{R1CSCommitmentGens, R1CSInstance};
|
|||
use libspartan::r1csproof::R1CSGens;
|
|||
use libspartan::spartan::{NIZKGens, SNARKGens, NIZK, SNARK};
|
|||
use libspartan::timer::Timer;
|
|||
use merlin::Transcript;
|
|||
|
|||
pub fn main() {
|
|||
// the list of number of variables (and constraints) in an R1CS instance
|
|||
let inst_sizes = vec![12, 16, 20];
|
|||
|
|||
println!("Profiler:: SNARK");
|
|||
for &s in inst_sizes.iter() {
|
|||
let num_vars = (s as usize).pow2();
|
|||
let num_cons = num_vars;
|
|||
let num_inputs = 10;
|
|||
let (inst, vars, input) = R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
|
|||
|
|||
Timer::print(&format!("number_of_constraints {}", num_cons));
|
|||
|
|||
let r1cs_size = inst.size();
|
|||
let gens_r1cs_eval = R1CSCommitmentGens::new(&r1cs_size, b"gens_r1cs_eval");
|
|||
|
|||
// create a commitment to R1CSInstance
|
|||
let timer_encode = Timer::new("SNARK::encode");
|
|||
let (comm, decomm) = SNARK::encode(&inst, &gens_r1cs_eval);
|
|||
timer_encode.stop();
|
|||
|
|||
let gens_r1cs_sat = R1CSGens::new(num_cons, num_vars, b"gens_r1cs_sat");
|
|||
let gens = SNARKGens::new(gens_r1cs_sat, gens_r1cs_eval);
|
|||
|
|||
// produce a proof of satisfiability
|
|||
let timer_prove = Timer::new("SNARK::prove");
|
|||
let mut prover_transcript = Transcript::new(b"example");
|
|||
let proof = SNARK::prove(&inst, &decomm, vars, &input, &gens, &mut prover_transcript);
|
|||
timer_prove.stop();
|
|||
|
|||
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
|
|||
bincode::serialize_into(&mut encoder, &proof).unwrap();
|
|||
let proof_encoded = encoder.finish().unwrap();
|
|||
let msg_proof_len = format!("SNARK::proof_compressed_len {:?}", proof_encoded.len());
|
|||
Timer::print(&msg_proof_len);
|
|||
|
|||
// verify the proof of satisfiability
|
|||
let timer_verify = Timer::new("SNARK::verify");
|
|||
let mut verifier_transcript = Transcript::new(b"example");
|
|||
assert!(proof
|
|||
.verify(&comm, &input, &mut verifier_transcript, &gens)
|
|||
.is_ok());
|
|||
timer_verify.stop();
|
|||
|
|||
println!();
|
|||
}
|
|||
|
|||
println!("Profiler:: NIZK");
|
|||
for &s in inst_sizes.iter() {
|
|||
let num_vars = (s as usize).pow2();
|
|||
let num_cons = num_vars;
|
|||
let num_inputs = 10;
|
|||
let (inst, vars, input) = R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
|
|||
|
|||
Timer::print(&format!("number_of_constraints {}", num_cons));
|
|||
|
|||
let gens_r1cs_sat = R1CSGens::new(num_cons, num_vars, b"gens_r1cs_sat");
|
|||
let gens = NIZKGens::new(gens_r1cs_sat);
|
|||
|
|||
// produce a proof of satisfiability
|
|||
let timer_prove = Timer::new("NIZK::prove");
|
|||
let mut prover_transcript = Transcript::new(b"example");
|
|||
let proof = NIZK::prove(&inst, vars, &input, &gens, &mut prover_transcript);
|
|||
timer_prove.stop();
|
|||
|
|||
let mut encoder = ZlibEncoder::new(Vec::new(), Compression::default());
|
|||
bincode::serialize_into(&mut encoder, &proof).unwrap();
|
|||
let proof_encoded = encoder.finish().unwrap();
|
|||
let msg_proof_len = format!("NIZK::proof_compressed_len {:?}", proof_encoded.len());
|
|||
Timer::print(&msg_proof_len);
|
|||
|
|||
// verify the proof of satisfiability
|
|||
let timer_verify = Timer::new("NIZK::verify");
|
|||
let mut verifier_transcript = Transcript::new(b"example");
|
|||
assert!(proof
|
|||
.verify(&inst, &input, &mut verifier_transcript, &gens)
|
|||
.is_ok());
|
|||
timer_verify.stop();
|
|||
|
|||
println!();
|
|||
}
|
|||
}
|