-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: update & clean benchmarks * chore: address review suggestions
- Loading branch information
1 parent
6abfdbb
commit 63d44ee
Showing
10 changed files
with
239 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.