Skip to content

Commit 5020b54

Browse files
committed
Field arithmetic benchmark + reworked assembly (+40% perf) (privacy-ethereum#49)
* add benchmarks for BN256 * small assembly changes: adcx -> adc * rework mul assembly to use 2 carry chains * remove need for nightly for asm by using register instead of constant * remove unused regs * run cargo fmt
1 parent 1fd2e54 commit 5020b54

File tree

3 files changed

+287
-567
lines changed

3 files changed

+287
-567
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@ overflow-checks = false
5050
lto = true
5151
incremental = false
5252
codegen-units = 1
53+
54+
[[bench]]
55+
name = "bn256_field"
56+
harness = false

benches/bn256_field.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
2+
use halo2curves::bn256::*;
3+
use halo2curves::ff::Field;
4+
use rand::SeedableRng;
5+
use rand_xorshift::XorShiftRng;
6+
7+
pub fn bench_bn256_field(c: &mut Criterion) {
8+
let mut rng = XorShiftRng::from_seed([
9+
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc,
10+
0xe5,
11+
]);
12+
13+
let a = Fq::random(&mut rng);
14+
let b = Fq::random(&mut rng);
15+
16+
#[cfg(not(feature = "asm"))]
17+
let mut group = c.benchmark_group("BN256 Field Arithmetic (no assembly)");
18+
19+
#[cfg(feature = "asm")]
20+
let mut group = c.benchmark_group("BN256 Field Arithmetic (with assembly)");
21+
22+
group.significance_level(0.1).sample_size(10000);
23+
group.throughput(Throughput::Elements(1));
24+
25+
group.bench_function("bn256_fq_add", |bencher| {
26+
bencher.iter(|| black_box(&a).add(black_box(&b)))
27+
});
28+
group.bench_function("bn256_fq_double", |bencher| {
29+
bencher.iter(|| black_box(&a).double())
30+
});
31+
group.bench_function("bn256_fq_sub", |bencher| {
32+
bencher.iter(|| black_box(&a).sub(black_box(&b)))
33+
});
34+
group.bench_function("bn256_fq_neg", |bencher| {
35+
bencher.iter(|| black_box(&a).neg())
36+
});
37+
group.bench_function("bn256_fq_mul", |bencher| {
38+
bencher.iter(|| black_box(&a).mul(black_box(&b)))
39+
});
40+
group.bench_function("bn256_fq_square", |bencher| {
41+
bencher.iter(|| black_box(&a).square())
42+
});
43+
}
44+
45+
criterion_group!(benches, bench_bn256_field);
46+
criterion_main!(benches);

0 commit comments

Comments
 (0)