Skip to content

Instantly share code, notes, and snippets.

@adria0
Created October 2, 2024 06:33
Show Gist options
  • Save adria0/c440185d765a368aaf21ca5741a63ab7 to your computer and use it in GitHub Desktop.
Save adria0/c440185d765a368aaf21ca5741a63ab7 to your computer and use it in GitHub Desktop.
use criterion::{black_box, criterion_group, criterion_main, AxisScale, Criterion, PlotConfiguration};
use group::{Curve, Group, GroupEncoding, UncompressedEncoding};
use halo2curves::{
bn256::{self, G1},
serde::SerdeObject,
CurveAffine,
};
fn bench_curve_serial<G>(c: &mut Criterion, name: &'static str)
where
G: CurveAffine + UncompressedEncoding + SerdeObject,
G::Curve: GroupEncoding + SerdeObject,
{
let mut rng = rand_core::OsRng;
let curve = G::Curve::random(&mut rng);
let affine = G::Curve::to_affine(&curve);
let curve_raw = curve.to_raw_bytes();
let affine_raw = affine.to_raw_bytes();
let curve_compressed = curve.to_bytes();
let affine_uncompressed = affine.to_uncompressed();
let curve_raw_ref = &curve_raw;
let affine_raw_ref = &affine_raw;
let curve_compressed_ref = &curve_compressed;
let affine_uncompressed_ref = &affine_uncompressed;
let mut group = c.benchmark_group(format!("{} serial", name));
let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
group.plot_config(plot_config);
group.bench_function(&format!("{name} aff raw"), move |b| {
b.iter(|| G::from_raw_bytes(black_box(affine_raw_ref)))
});
group.bench_function(
&format!("{name} aff raw un unchk"),
move |b| b.iter(|| G::from_raw_bytes_unchecked(black_box(affine_raw_ref))),
);
group.bench_function(&format!("{name} cur raw"), move |b| {
b.iter(|| G::Curve::from_raw_bytes(black_box(curve_raw_ref)))
});
group.bench_function(
&format!("{name} cur raw unchk"),
move |b| b.iter(|| G::Curve::from_raw_bytes_unchecked(black_box(curve_raw_ref))),
);
group.bench_function(
&format!("{name} cur comp chk"),
move |b| b.iter(|| <G::Curve as GroupEncoding>::from_bytes(black_box(curve_compressed_ref))),
);
group.bench_function(
&format!("{name} cur comp unchk"),
move |b| b.iter(|| <G::Curve as GroupEncoding>::from_bytes_unchecked(black_box(curve_compressed_ref))),
);
group.bench_function(
&format!("{name} aff uncomp chk"),
move |b| b.iter(|| <G as UncompressedEncoding>::from_uncompressed(black_box(affine_uncompressed_ref))),
);
group.bench_function(
&format!("{name} aff uncomp unchk"),
move |b| b.iter(|| <G as UncompressedEncoding>::from_uncompressed_unchecked(black_box(affine_uncompressed_ref))),
);
group.finish();
}
fn bench_bn256_serial(c: &mut Criterion) {
bench_curve_serial::<bn256::G1Affine>(c, "BN256")
}
criterion_group!(benches, bench_bn256_serial);
criterion_main!(benches);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment