Skip to content

Commit 4be0575

Browse files
committed
Merge branch 'snarkpack-integration' of https://github.com/cryptonetlab/testudo into snarkpack-integration
2 parents af03d8f + 425c274 commit 4be0575

File tree

8 files changed

+130
-10
lines changed

8 files changed

+130
-10
lines changed

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ csv = "1.1.5"
5252
criterion = "0.3.6"
5353

5454
[lib]
55-
name = "libspartan"
55+
name = "libtestudo"
5656
path = "src/lib.rs"
5757

5858
[[bin]]
@@ -63,6 +63,10 @@ path = "profiler/testudo.rs"
6363
name = "testudo"
6464
harness = false
6565

66+
[[bench]]
67+
name = "pst"
68+
harness = false
69+
6670
[features]
6771
multicore = ["rayon"]
6872
profile = []

benches/pst.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
use std::time::Instant;
2+
3+
use ark_poly_commit::multilinear_pc::MultilinearPC;
4+
use ark_serialize::CanonicalSerialize;
5+
use libtestudo::{
6+
parameters::PoseidonConfiguration, poseidon_transcript::PoseidonTranscript, sqrt_pst::Polynomial,
7+
};
8+
use serde::Serialize;
9+
type F = ark_bls12_377::Fr;
10+
type E = ark_bls12_377::Bls12_377;
11+
use ark_std::UniformRand;
12+
13+
#[derive(Default, Clone, Serialize)]
14+
struct BenchmarkResults {
15+
power: usize,
16+
commit_time: u128,
17+
opening_time: u128,
18+
verification_time: u128,
19+
proof_size: usize,
20+
commiter_key_size: usize,
21+
}
22+
fn main() {
23+
let params = ark_bls12_377::Fr::poseidon_params();
24+
25+
let mut writer = csv::Writer::from_path("sqrt_pst.csv").expect("unable to open csv writer");
26+
for &s in [4, 5, 20, 27].iter() {
27+
println!("Running for {} inputs", s);
28+
let mut rng = ark_std::test_rng();
29+
let mut br = BenchmarkResults::default();
30+
br.power = s;
31+
let num_vars = s;
32+
let len = 2_usize.pow(num_vars as u32);
33+
let z: Vec<F> = (0..len).into_iter().map(|_| F::rand(&mut rng)).collect();
34+
let r: Vec<F> = (0..num_vars)
35+
.into_iter()
36+
.map(|_| F::rand(&mut rng))
37+
.collect();
38+
39+
let setup_vars = (num_vars as f32 / 2.0).ceil() as usize;
40+
let gens = MultilinearPC::<E>::setup((num_vars as f32 / 2.0).ceil() as usize, &mut rng);
41+
let (ck, vk) = MultilinearPC::<E>::trim(&gens, setup_vars);
42+
43+
let mut cks = Vec::<u8>::new();
44+
ck.serialize_with_mode(&mut cks, ark_serialize::Compress::Yes)
45+
.unwrap();
46+
br.commiter_key_size = cks.len();
47+
48+
let mut pl = Polynomial::from_evaluations(&z.clone());
49+
50+
let v = pl.eval(&r);
51+
52+
let start = Instant::now();
53+
let (comm_list, t) = pl.commit(&ck);
54+
let duration = start.elapsed().as_millis();
55+
br.commit_time = duration;
56+
57+
let mut prover_transcript = PoseidonTranscript::new(&params);
58+
59+
let start = Instant::now();
60+
let (u, pst_proof, mipp_proof) = pl.open(&mut prover_transcript, comm_list, &ck, &r, &t);
61+
let duration = start.elapsed().as_millis();
62+
br.opening_time = duration;
63+
64+
let mut p1 = Vec::<u8>::new();
65+
let mut p2 = Vec::<u8>::new();
66+
pst_proof
67+
.serialize_with_mode(&mut p1, ark_serialize::Compress::Yes)
68+
.unwrap();
69+
70+
mipp_proof
71+
.serialize_with_mode(&mut p2, ark_serialize::Compress::Yes)
72+
.unwrap();
73+
74+
br.proof_size = p1.len() + p2.len();
75+
76+
let mut verifier_transcript = PoseidonTranscript::new(&params);
77+
78+
let start = Instant::now();
79+
let res = Polynomial::verify(
80+
&mut verifier_transcript,
81+
&vk,
82+
&u,
83+
&r,
84+
v,
85+
&pst_proof,
86+
&mipp_proof,
87+
&t,
88+
);
89+
let duration = start.elapsed().as_millis();
90+
br.verification_time = duration;
91+
assert!(res == true);
92+
93+
writer
94+
.serialize(br)
95+
.expect("unable to write results to csv");
96+
writer.flush().expect("wasn't able to flush");
97+
}
98+
}

benches/testudo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use ark_crypto_primitives::sponge::Absorb;
55
use ark_ec::pairing::Pairing;
66
use ark_ff::PrimeField;
77
use ark_serialize::*;
8-
use libspartan::parameters::PoseidonConfiguration;
9-
use libspartan::{
8+
use libtestudo::parameters::PoseidonConfiguration;
9+
use libtestudo::{
1010
poseidon_transcript::PoseidonTranscript,
1111
testudo_snark::{TestudoSnark, TestudoSnarkGens},
1212
Instance,

examples/cubic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
use ark_ec::pairing::Pairing;
1212
use ark_ff::{BigInteger, PrimeField};
1313
use ark_std::{One, UniformRand, Zero};
14-
use libspartan::testudo_snark::{TestudoSnark, TestudoSnarkGens};
15-
use libspartan::{
14+
use libtestudo::testudo_snark::{TestudoSnark, TestudoSnarkGens};
15+
use libtestudo::{
1616
parameters::poseidon_params, poseidon_transcript::PoseidonTranscript, InputsAssignment, Instance,
1717
VarsAssignment,
1818
};

profiler/testudo.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#![allow(non_snake_case)]
22
#![allow(clippy::assertions_on_result_states)]
33

4-
extern crate libspartan;
4+
extern crate libtestudo;
55
extern crate merlin;
66
use ark_crypto_primitives::sponge::poseidon::PoseidonConfig;
77
use ark_crypto_primitives::sponge::Absorb;
88
use ark_ec::pairing::Pairing;
99
use ark_ff::PrimeField;
1010
use ark_serialize::*;
11-
use libspartan::parameters::PoseidonConfiguration;
12-
use libspartan::poseidon_transcript::PoseidonTranscript;
13-
use libspartan::{
11+
use libtestudo::parameters::PoseidonConfiguration;
12+
use libtestudo::poseidon_transcript::PoseidonTranscript;
13+
use libtestudo::{
1414
testudo_snark::{TestudoSnark, TestudoSnarkGens},
1515
Instance,
1616
};

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ mod product_tree;
2828
mod r1csinstance;
2929
mod r1csproof;
3030
mod sparse_mlpoly;
31-
mod sqrt_pst;
31+
pub mod sqrt_pst;
3232
mod sumcheck;
3333
pub mod testudo_nizk;
3434
pub mod testudo_snark;

src/r1csinstance.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,9 @@ impl<F: PrimeField> R1CSInstance<F> {
314314
&self,
315315
gens: &R1CSCommitmentGens<E>,
316316
) -> (R1CSCommitment<E::G1>, R1CSDecommitment<F>) {
317+
// Noting that matrices A, B and C are sparse, produces a combined dense
318+
// dense polynomial from the non-zero entry that we commit to. This
319+
// represents the computational commitment.
317320
let (comm, dense) = SparseMatPolynomial::multi_commit(&[&self.A, &self.B, &self.C], &gens.gens);
318321
let r1cs_comm = R1CSCommitment {
319322
num_cons: self.num_cons,
@@ -322,6 +325,8 @@ impl<F: PrimeField> R1CSInstance<F> {
322325
comm,
323326
};
324327

328+
// The decommitment is used by the prover to convince the verifier
329+
// the received openings of A, B and C are correct.
325330
let r1cs_decomm = R1CSDecommitment { dense };
326331

327332
(r1cs_comm, r1cs_decomm)

src/sparse_mlpoly.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use ark_serialize::*;
2020
use core::cmp::Ordering;
2121

2222
#[derive(Debug, CanonicalSerialize, CanonicalDeserialize, Clone)]
23+
// Each SparseMatEntry is a tuple (row, col, val) representing a non-zero value
24+
// in an R1CS matrix.
2325
pub struct SparseMatEntry<F: PrimeField> {
2426
row: usize,
2527
col: usize,
@@ -33,9 +35,11 @@ impl<F: PrimeField> SparseMatEntry<F> {
3335
}
3436

3537
#[derive(Debug, CanonicalSerialize, CanonicalDeserialize, Clone)]
38+
// The sparse multilinearrepresentation of an R1CS matrix of size x*y
3639
pub struct SparseMatPolynomial<F: PrimeField> {
3740
num_vars_x: usize,
3841
num_vars_y: usize,
42+
// The non-zero entries in the matrix, represented by the tuple (row, col,val)
3943
M: Vec<SparseMatEntry<F>>,
4044
}
4145

@@ -346,6 +350,7 @@ impl<F: PrimeField> SparseMatPolynomial<F> {
346350
}
347351
}
348352

353+
// get the number of non_zero entries in a sparse R1CS matrix
349354
pub fn get_num_nz_entries(&self) -> usize {
350355
self.M.len().next_power_of_two()
351356
}
@@ -364,6 +369,7 @@ impl<F: PrimeField> SparseMatPolynomial<F> {
364369
(ops_row, ops_col, val)
365370
}
366371

372+
// Produce the dense representation of sparse matrices A, B and C.
367373
fn multi_sparse_to_dense_rep(
368374
sparse_polys: &[&SparseMatPolynomial<F>],
369375
) -> MultiSparseMatPolynomialAsDense<F> {
@@ -384,11 +390,17 @@ impl<F: PrimeField> SparseMatPolynomial<F> {
384390
let mut val_vec: Vec<DensePolynomial<F>> = Vec::new();
385391
for poly in sparse_polys {
386392
let (ops_row, ops_col, val) = poly.sparse_to_dense_vecs(N);
393+
// aggregate all the row and columns that contain non-zero values in the
394+
// three matrices
387395
ops_row_vec.push(ops_row);
388396
ops_col_vec.push(ops_col);
397+
// create dense polynomials, in Lagrange representation, for the non-zero
398+
// values of each matrix
389399
val_vec.push(DensePolynomial::new(val));
390400
}
391401

402+
// Note: everything else from
403+
392404
let any_poly = &sparse_polys[0];
393405

394406
let num_mem_cells = if any_poly.num_vars_x > any_poly.num_vars_y {
@@ -401,6 +413,7 @@ impl<F: PrimeField> SparseMatPolynomial<F> {
401413
let col = AddrTimestamps::new(num_mem_cells, N, ops_col_vec);
402414

403415
// combine polynomials into a single polynomial for commitment purposes
416+
// this is done because the commitment used has a public setup
404417
let comb_ops = DensePolynomial::merge(
405418
row
406419
.ops_addr

0 commit comments

Comments
 (0)