Skip to content

Commit 81ebe48

Browse files
committed
Saffron/benches: add bench for read proof
1 parent d6c3a90 commit 81ebe48

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

saffron/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ ark-std.workspace = true
4949
ctor = "0.2"
5050
proptest.workspace = true
5151
once_cell.workspace = true
52+
criterion = { workspace = true, features = ["html_reports"] }
5253

5354
[[bin]]
5455
name = "saffron-og-flow"
5556
path = "og-flow/main.rs"
57+
58+
[[bench]]
59+
name = "read_proof_bench"
60+
harness = false

saffron/benches/read_proof_bench.rs

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
//! Run this bench using `cargo criterion -p saffron --bench read_proof_bench`
2+
3+
use ark_ff::{One, UniformRand, Zero};
4+
use ark_poly::{univariate::DensePolynomial, Evaluations};
5+
use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion};
6+
use kimchi::{circuits::domains::EvaluationDomains, groupmap::GroupMap};
7+
use mina_curves::pasta::{Fp, Vesta};
8+
use once_cell::sync::Lazy;
9+
use poly_commitment::{commitment::CommitmentCurve, ipa::SRS, SRS as _};
10+
use rand::rngs::OsRng;
11+
use saffron::{
12+
env,
13+
read_proof::{prove, verify},
14+
ScalarField, SRS_SIZE,
15+
};
16+
17+
// Set up static resources to avoid re-computation during benchmarks
18+
static SRS: Lazy<SRS<Vesta>> = Lazy::new(|| {
19+
if let Ok(srs) = std::env::var("SRS_FILEPATH") {
20+
env::get_srs_from_cache(srs)
21+
} else {
22+
SRS::create(SRS_SIZE)
23+
}
24+
});
25+
26+
static DOMAIN: Lazy<EvaluationDomains<ScalarField>> =
27+
Lazy::new(|| EvaluationDomains::<ScalarField>::create(SRS_SIZE).unwrap());
28+
29+
static GROUP_MAP: Lazy<<Vesta as CommitmentCurve>::Map> =
30+
Lazy::new(<Vesta as CommitmentCurve>::Map::setup);
31+
32+
fn generate_test_data(
33+
size: usize,
34+
) -> (Vec<ScalarField>, Vec<ScalarField>, Vec<ScalarField>, Vesta) {
35+
let mut rng = o1_utils::tests::make_test_rng(None);
36+
37+
// Generate data with specified size
38+
let data: Vec<ScalarField> = (0..size).map(|_| Fp::rand(&mut rng)).collect();
39+
40+
// Create data commitment
41+
let data_poly: DensePolynomial<ScalarField> =
42+
Evaluations::from_vec_and_domain(data.clone(), DOMAIN.d1).interpolate();
43+
let data_comm: Vesta = SRS.commit_non_hiding(&data_poly, 1).chunks[0];
44+
45+
// Generate query (about 10% of positions will be queried)
46+
let query: Vec<ScalarField> = (0..size)
47+
.map(|_| {
48+
if rand::random::<f32>() < 0.1 {
49+
Fp::one()
50+
} else {
51+
Fp::zero()
52+
}
53+
})
54+
.collect();
55+
56+
// Compute answer as data * query
57+
let answer: Vec<ScalarField> = data.iter().zip(query.iter()).map(|(d, q)| *d * q).collect();
58+
59+
(data, query, answer, data_comm)
60+
}
61+
62+
fn bench_read_proof_prove(c: &mut Criterion) {
63+
let (data, query, answer, data_comm) = generate_test_data(SRS_SIZE);
64+
65+
let description = format!("prove size {}", SRS_SIZE);
66+
c.bench_function(description.as_str(), |b| {
67+
b.iter_batched(
68+
|| OsRng,
69+
|mut rng| {
70+
black_box(prove(
71+
*DOMAIN,
72+
&SRS,
73+
&GROUP_MAP,
74+
&mut rng,
75+
data.as_slice(),
76+
query.as_slice(),
77+
answer.as_slice(),
78+
&data_comm,
79+
))
80+
},
81+
BatchSize::NumIterations(10),
82+
)
83+
});
84+
}
85+
86+
fn bench_read_proof_verify(c: &mut Criterion) {
87+
let (data, query, answer, data_comm) = generate_test_data(SRS_SIZE);
88+
89+
// Create proof first
90+
let mut rng = OsRng;
91+
let proof = prove(
92+
*DOMAIN,
93+
&SRS,
94+
&GROUP_MAP,
95+
&mut rng,
96+
data.as_slice(),
97+
query.as_slice(),
98+
answer.as_slice(),
99+
&data_comm,
100+
);
101+
102+
let description = format!("verify size {}", SRS_SIZE);
103+
c.bench_function(&description.as_str(), |b| {
104+
b.iter_batched(
105+
|| OsRng,
106+
|mut rng| {
107+
black_box(verify(
108+
*DOMAIN, &SRS, &GROUP_MAP, &mut rng, &data_comm, &proof,
109+
))
110+
},
111+
BatchSize::SmallInput,
112+
)
113+
});
114+
}
115+
116+
criterion_group!(benches, bench_read_proof_prove, bench_read_proof_verify);
117+
criterion_main!(benches);

0 commit comments

Comments
 (0)