Skip to content

Commit dee92f8

Browse files
authored
Additional benchmark: sha256 folding (#379)
* reorganize * add another benchmark: naive sha256 * remove * Rename msm_generic, msm_integer to msm and msm_small Introduce commit_small Update test/bench to use these new methods Drop the case for h to be optional in Pedersen commitment * make bench generic; fix pp_digest tests * benchmark commit rather than msm, allowing msm module to be private to the crate
1 parent a736dca commit dee92f8

File tree

14 files changed

+390
-116
lines changed

14 files changed

+390
-116
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ name = "ppsnark"
6363
harness = false
6464

6565
[[bench]]
66-
name = "msm"
66+
name = "commit"
6767
harness = false
6868

6969
[features]

benches/msm.rs benches/commit.rs

+93-34
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
//! Benchmarking the commit times for hyperkzg over BN254 field using
2-
//! halo2curves library and the Nova-provided MSM routine, on a range of scalar bit-widths
3-
use core::{ops::Mul, time::Duration};
2+
//! halo2curves library and the Nova-provided commitment engine, on a range of scalar bit-widths
3+
use core::time::Duration;
44
use criterion::{black_box, criterion_group, criterion_main, Criterion};
5-
use halo2curves::{
6-
bn256::{Fr as Scalar, G1Affine},
7-
ff::Field,
8-
group::Curve,
9-
msm::msm_best,
5+
use halo2curves::{bn256::Fr as Scalar, ff::Field, msm::msm_best};
6+
use nova_snark::{
7+
provider::Bn256EngineKZG,
8+
traits::{commitment::CommitmentEngineTrait, Engine},
109
};
11-
use nova_snark::provider::msm::{msm_generic, msm_integer};
1210
use rand::Rng;
1311
use rayon::prelude::*;
1412

@@ -21,20 +19,15 @@ targets = bench_commit
2119
criterion_main!(commit);
2220

2321
fn bench_commit(c: &mut Criterion) {
22+
type E = Bn256EngineKZG;
23+
2424
let min = 1 << 20;
2525
let max = 1 << 24;
2626

2727
// sample bases for the purpose of testing
28-
let bases: Vec<_> = (0..max)
29-
.into_par_iter()
30-
.map(|_| {
31-
let mut rng = rand::thread_rng();
32-
let scalar = Scalar::random(&mut rng);
33-
G1Affine::generator().mul(scalar).to_affine()
34-
})
35-
.collect();
28+
let ck = <E as Engine>::CE::setup(b"test_from_label", max);
3629

37-
assert_eq!(bases.len(), max);
30+
let zero = <E as Engine>::Scalar::zero();
3831

3932
// random scalars that are in the set {0, 1}
4033
let scalars_u1 = (0..max)
@@ -119,71 +112,137 @@ fn bench_commit(c: &mut Criterion) {
119112
let mut size = min;
120113
while size <= max {
121114
c.bench_function(&format!("halo2curves_commit_u1_{size}"), |b| {
122-
b.iter(|| black_box(msm_best(&scalars_u1_field[..size], &bases[..size])))
115+
b.iter(|| black_box(msm_best(&scalars_u1_field[..size], &ck.ck()[..size])))
123116
});
124117

125118
c.bench_function(&format!("nova_generic_commit_u1_{size}"), |b| {
126-
b.iter(|| black_box(msm_generic(&scalars_u1_field[..size], &bases[..size])))
119+
b.iter(|| {
120+
black_box(<E as Engine>::CE::commit(
121+
&ck,
122+
&scalars_u1_field[..size],
123+
&zero,
124+
))
125+
})
127126
});
128127

129128
c.bench_function(&format!("nova_specialized_commit_u1_{size}"), |b| {
130-
b.iter(|| black_box(msm_integer(&scalars_u1[..size], &bases[..size])))
129+
b.iter(|| {
130+
black_box(<E as Engine>::CE::commit_small(
131+
&ck,
132+
&scalars_u1[..size],
133+
&zero,
134+
))
135+
})
131136
});
132137

133138
c.bench_function(&format!("halo2curves_commit_u10_{size}"), |b| {
134-
b.iter(|| black_box(msm_best(&scalars_u10_field[..size], &bases[..size])))
139+
b.iter(|| black_box(msm_best(&scalars_u10_field[..size], &ck.ck()[..size])))
135140
});
136141

137142
c.bench_function(&format!("nova_generic_commit_u10_{size}"), |b| {
138-
b.iter(|| black_box(msm_generic(&scalars_u10_field[..size], &bases[..size])))
143+
b.iter(|| {
144+
black_box(<E as Engine>::CE::commit(
145+
&ck,
146+
&scalars_u10_field[..size],
147+
&zero,
148+
))
149+
})
139150
});
140151

141152
c.bench_function(&format!("nova_specialized_commit_u10_{size}"), |b| {
142-
b.iter(|| black_box(msm_integer(&scalars_u10[..size], &bases[..size])))
153+
b.iter(|| {
154+
black_box(<E as Engine>::CE::commit_small(
155+
&ck,
156+
&scalars_u10[..size],
157+
&zero,
158+
))
159+
})
143160
});
144161

145162
c.bench_function(&format!("halo2curves_commit_u16_{size}"), |b| {
146-
b.iter(|| black_box(msm_best(&scalars_u16_field[..size], &bases[..size])))
163+
b.iter(|| black_box(msm_best(&scalars_u16_field[..size], &ck.ck()[..size])))
147164
});
148165

149166
c.bench_function(&format!("nova_generic_commit_u16_{size}"), |b| {
150-
b.iter(|| black_box(msm_generic(&scalars_u16_field[..size], &bases[..size])))
167+
b.iter(|| {
168+
black_box(<E as Engine>::CE::commit(
169+
&ck,
170+
&scalars_u16_field[..size],
171+
&zero,
172+
))
173+
})
151174
});
152175

153176
c.bench_function(&format!("nova_specialized_commit_u16_{size}"), |b| {
154-
b.iter(|| black_box(msm_integer(&scalars_u16[..size], &bases[..size])))
177+
b.iter(|| {
178+
black_box(<E as Engine>::CE::commit_small(
179+
&ck,
180+
&scalars_u16[..size],
181+
&zero,
182+
))
183+
})
155184
});
156185

157186
c.bench_function(&format!("halo2curves_commit_u32_{size}"), |b| {
158-
b.iter(|| black_box(msm_best(&scalars_u32_field[..size], &bases[..size])))
187+
b.iter(|| black_box(msm_best(&scalars_u32_field[..size], &ck.ck()[..size])))
159188
});
160189

161190
c.bench_function(&format!("nova_generic_commit_u32_{size}"), |b| {
162-
b.iter(|| black_box(msm_generic(&scalars_u32_field[..size], &bases[..size])))
191+
b.iter(|| {
192+
black_box(<E as Engine>::CE::commit(
193+
&ck,
194+
&scalars_u32_field[..size],
195+
&zero,
196+
))
197+
})
163198
});
164199

165200
c.bench_function(&format!("nova_specialized_commit_u32_{size}"), |b| {
166-
b.iter(|| black_box(msm_integer(&scalars_u32[..size], &bases[..size])))
201+
b.iter(|| {
202+
black_box(<E as Engine>::CE::commit_small(
203+
&ck,
204+
&scalars_u32[..size],
205+
&zero,
206+
))
207+
})
167208
});
168209

169210
c.bench_function(&format!("halo2curves_commit_u64_{size}"), |b| {
170-
b.iter(|| black_box(msm_best(&scalars_u64_field[..size], &bases[..size])))
211+
b.iter(|| black_box(msm_best(&scalars_u64_field[..size], &ck.ck()[..size])))
171212
});
172213

173214
c.bench_function(&format!("nova_generic_commit_u64_{size}"), |b| {
174-
b.iter(|| black_box(msm_generic(&scalars_u64_field[..size], &bases[..size])))
215+
b.iter(|| {
216+
black_box(<E as Engine>::CE::commit(
217+
&ck,
218+
&scalars_u64_field[..size],
219+
&zero,
220+
))
221+
})
175222
});
176223

177224
c.bench_function(&format!("nova_specialized_commit_u64_{size}"), |b| {
178-
b.iter(|| black_box(msm_integer(&scalars_u64[..size], &bases[..size])))
225+
b.iter(|| {
226+
black_box(<E as Engine>::CE::commit_small(
227+
&ck,
228+
&scalars_u64[..size],
229+
&zero,
230+
))
231+
})
179232
});
180233

181234
c.bench_function(&format!("halo2curves_commit_random_{size}"), |b| {
182-
b.iter(|| black_box(msm_best(&scalars_random_field[..size], &bases[..size])))
235+
b.iter(|| black_box(msm_best(&scalars_random_field[..size], &ck.ck()[..size])))
183236
});
184237

185238
c.bench_function(&format!("nova_generic_commit_random_{size}"), |b| {
186-
b.iter(|| black_box(msm_best(&scalars_random_field[..size], &bases[..size])))
239+
b.iter(|| {
240+
black_box(<E as Engine>::CE::commit(
241+
&ck,
242+
&scalars_random_field[..size],
243+
&zero,
244+
))
245+
})
187246
});
188247

189248
size *= 4;

src/neutron/circuit/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ impl<'a, E: Engine, SC: StepCircuit<E::Scalar>> NeutronAugmentedCircuit<'a, E, S
272272

273273
impl<E: Engine, SC: StepCircuit<E::Scalar>> NeutronAugmentedCircuit<'_, E, SC> {
274274
/// synthesize circuit giving constraint system
275-
pub fn synthesize<CS: ConstraintSystem<<E as Engine>::Scalar>>(
275+
pub fn synthesize<CS: ConstraintSystem<E::Scalar>>(
276276
self,
277277
cs: &mut CS,
278278
) -> Result<Vec<AllocatedNum<E::Scalar>>, SynthesisError> {

0 commit comments

Comments
 (0)