Skip to content

Commit

Permalink
Field arithmetic benchmark + reworked assembly (+40% perf) (#49)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
mratsim authored Jun 20, 2023
1 parent 1fc3b83 commit d8e4276
Show file tree
Hide file tree
Showing 3 changed files with 287 additions and 567 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ codegen-units = 1
[[bench]]
name = "less_than"
harness = false

[[bench]]
name = "bn256_field"
harness = false
46 changes: 46 additions & 0 deletions benches/bn256_field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
use halo2curves::bn256::*;
use halo2curves::ff::Field;
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;

pub fn bench_bn256_field(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed([
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc,
0xe5,
]);

let a = Fq::random(&mut rng);
let b = Fq::random(&mut rng);

#[cfg(not(feature = "asm"))]
let mut group = c.benchmark_group("BN256 Field Arithmetic (no assembly)");

#[cfg(feature = "asm")]
let mut group = c.benchmark_group("BN256 Field Arithmetic (with assembly)");

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

group.bench_function("bn256_fq_add", |bencher| {
bencher.iter(|| black_box(&a).add(black_box(&b)))
});
group.bench_function("bn256_fq_double", |bencher| {
bencher.iter(|| black_box(&a).double())
});
group.bench_function("bn256_fq_sub", |bencher| {
bencher.iter(|| black_box(&a).sub(black_box(&b)))
});
group.bench_function("bn256_fq_neg", |bencher| {
bencher.iter(|| black_box(&a).neg())
});
group.bench_function("bn256_fq_mul", |bencher| {
bencher.iter(|| black_box(&a).mul(black_box(&b)))
});
group.bench_function("bn256_fq_square", |bencher| {
bencher.iter(|| black_box(&a).square())
});
}

criterion_group!(benches, bench_bn256_field);
criterion_main!(benches);
Loading

0 comments on commit d8e4276

Please sign in to comment.