Skip to content

Commit

Permalink
Update & clean benchmarks (#165)
Browse files Browse the repository at this point in the history
* feat: update & clean benchmarks

* chore: address review suggestions
  • Loading branch information
davidnevadoc authored Jun 21, 2024
1 parent 6abfdbb commit 63d44ee
Show file tree
Hide file tree
Showing 10 changed files with 239 additions and 259 deletions.
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,11 @@ incremental = false
codegen-units = 1

[[bench]]
name = "less_than"
name = "field_arith"
harness = false

[[bench]]
name = "bn256_field"
harness = false

[[bench]]
name = "group"
name = "curve"
harness = false

[[bench]]
Expand All @@ -88,3 +84,7 @@ harness = false
[[bench]]
name = "msm"
harness = false

[[bench]]
name = "pairing"
harness = false
51 changes: 0 additions & 51 deletions benches/bn256_field.rs

This file was deleted.

74 changes: 74 additions & 0 deletions benches/curve.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//! This benchmarks the basic EC operations.
//! It measures `G1` from the BN256 curve.
//!
//! To run this benchmark:
//!
//! cargo bench --bench curve
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
use ff::Field;
use group::prime::PrimeCurveAffine;
use halo2curves::bn256::G1;
use pasta_curves::arithmetic::CurveExt;
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;

fn bench_curve_ops<G: CurveExt>(c: &mut Criterion, name: &'static str) {
{
let mut rng = XorShiftRng::seed_from_u64(3141519u64);

// Generate 2 random points.
let mut p1 = G::random(&mut rng);
let p2 = G::random(&mut rng);
p1 += p2;

let p1_affine = G::AffineExt::from(p1);

let s = G::ScalarExt::random(&mut rng);

const N: usize = 1000;
let v: Vec<G> = (0..N).map(|_| p1 + G::random(&mut rng)).collect();

let mut q = vec![G::AffineExt::identity(); N];

let mut group = c.benchmark_group(format!("{} arithmetic", name));

group.significance_level(0.1).sample_size(1000);
group.throughput(Throughput::Elements(1));

group.bench_function(&format!("{name} check on curve"), move |b| {
b.iter(|| black_box(p1).is_on_curve())
});
group.bench_function(&format!("{name} check equality"), move |b| {
b.iter(|| black_box(p1) == black_box(p1))
});
group.bench_function(&format!("{name} to affine"), move |b| {
b.iter(|| G::AffineExt::from(black_box(p1)))
});
group.bench_function(&format!("{name} doubling"), move |b| {
b.iter(|| black_box(p1).double())
});
group.bench_function(&format!("{name} addition"), move |b| {
b.iter(|| black_box(p1).add(&p2))
});
group.bench_function(&format!("{name} mixed addition"), move |b| {
b.iter(|| black_box(p2).add(&p1_affine))
});
group.bench_function(&format!("{name} scalar multiplication"), move |b| {
b.iter(|| black_box(p1) * black_box(s))
});
group.bench_function(&format!("{name} batch to affine n={N}"), move |b| {
b.iter(|| {
G::batch_normalize(black_box(&v), black_box(&mut q));
})
});
group.finish();
}
}

fn bench_bn256_ops(c: &mut Criterion) {
bench_curve_ops::<G1>(c, "BN256")
}

criterion_group!(benches, bench_bn256_ops);
criterion_main!(benches);
17 changes: 11 additions & 6 deletions benches/fft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//!
//! To run this benchmark:
//!
//! cargo bench -- fft
//! cargo bench --bench fft
//!
//! Caveat: The multicore benchmark assumes:
//! 1. a multi-core system
Expand All @@ -17,17 +17,21 @@ use criterion::{BenchmarkId, Criterion};
use group::ff::Field;
use halo2curves::bn256::Fr as Scalar;
use halo2curves::fft::best_fft;
use rand_core::OsRng;
use rand::{RngCore, SeedableRng};
use rand_xorshift::XorShiftRng;
use std::ops::Range;
use std::time::SystemTime;

const RANGE: Range<u32> = 3..19;
const SEED: [u8; 16] = [
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, 0xe5,
];

fn generate_data(k: u32) -> Vec<Scalar> {
fn generate_data(k: u32, mut rng: impl RngCore) -> Vec<Scalar> {
let n = 1 << k;
let timer = SystemTime::now();
println!("\n\nGenerating 2^{k} = {n} values..",);
let data: Vec<Scalar> = (0..n).map(|_| Scalar::random(OsRng)).collect();
let data: Vec<Scalar> = (0..n).map(|_| Scalar::random(&mut rng)).collect();
let end = timer.elapsed().unwrap();
println!(
"Generating 2^{k} = {n} values took: {} sec.\n\n",
Expand All @@ -38,8 +42,9 @@ fn generate_data(k: u32) -> Vec<Scalar> {

fn fft(c: &mut Criterion) {
let max_k = RANGE.max().unwrap_or(16);
let mut data = generate_data(max_k);
let omega = Scalar::random(OsRng);
let mut rng = XorShiftRng::from_seed(SEED);
let mut data = generate_data(max_k, &mut rng);
let omega = Scalar::random(&mut rng);
let mut group = c.benchmark_group("fft");
for k in RANGE {
group.bench_function(BenchmarkId::new("k", k), |b| {
Expand Down
70 changes: 70 additions & 0 deletions benches/field_arith.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//! This benchmarks the basic FF operations.
//! It measures the base field `Fq` and scalar field `Fr` from the BN256 curve.
//!
//! To run this benchmark:
//!
//! cargo bench --bench field_arith
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
use halo2curves::{
bn256::{Fq, Fr},
ff::Field,
ff_ext::Legendre,
};
use rand::{RngCore, SeedableRng};
use rand_xorshift::XorShiftRng;

const SEED: [u8; 16] = [
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, 0xe5,
];
fn bench_field_arithmetic<F: Field + Legendre>(c: &mut Criterion, name: &'static str) {
let mut rng = XorShiftRng::from_seed(SEED);

let a = <F as Field>::random(&mut rng);
let b = <F as Field>::random(&mut rng);
let exp = rng.next_u64();

let mut group = c.benchmark_group(format!("{} arithmetic", name));

group.significance_level(0.1).sample_size(1000);
group.throughput(Throughput::Elements(1));

group.bench_function(format!("{}_add", name), |bencher| {
bencher.iter(|| black_box(&a).add(black_box(&b)))
});
group.bench_function(format!("{}_double", name), |bencher| {
bencher.iter(|| black_box(&a).double())
});
group.bench_function(format!("{}_sub", name), |bencher| {
bencher.iter(|| black_box(&a).sub(black_box(&b)))
});
group.bench_function(format!("{}_neg", name), |bencher| {
bencher.iter(|| black_box(&a).neg())
});
group.bench_function(format!("{}_mul", name), |bencher| {
bencher.iter(|| black_box(&a).mul(black_box(&b)))
});
group.bench_function(format!("{}_square", name), |bencher| {
bencher.iter(|| black_box(&a).square())
});
group.bench_function(format!("{}_pow_vartime", name), |bencher| {
bencher.iter(|| black_box(&a).pow_vartime(black_box(&[exp])))
});
group.bench_function(format!("{}_invert", name), |bencher| {
bencher.iter(|| black_box(&a).invert())
});
group.bench_function(format!("{}_legendre", name), |bencher| {
bencher.iter(|| black_box(&a).legendre())
});
group.finish()
}

fn bench_bn256_base_field(c: &mut Criterion) {
bench_field_arithmetic::<Fq>(c, "Fq")
}
fn bench_bn256_scalar_field(c: &mut Criterion) {
bench_field_arithmetic::<Fr>(c, "Fr")
}

criterion_group!(benches, bench_bn256_base_field, bench_bn256_scalar_field);
criterion_main!(benches);
52 changes: 0 additions & 52 deletions benches/group.rs

This file was deleted.

Loading

0 comments on commit 63d44ee

Please sign in to comment.