Skip to content
Snippets Groups Projects
Commit a4ef3e2a authored by STEVAN Antoine's avatar STEVAN Antoine :crab:
Browse files

use PLNK instead of duplicating "bench" framework (dragoon/komodo!84)

i've basically refactored the whole "bench" framework that was inlined in  `examples/benches/operations/field.rs` and `examples/benches/operations/curve_group.rs` into a new repo called [PLNK](https://gitlab.isae-supaero.fr/a.stevan/plnk).

nothing effectively changes on the side of Komodo but now the code is much simpler here :)
parent 10acafb1
No related branches found
No related tags found
No related merge requests found
...@@ -14,6 +14,7 @@ ark-poly = "0.4.2" ...@@ -14,6 +14,7 @@ ark-poly = "0.4.2"
ark-serialize = "0.4.2" ark-serialize = "0.4.2"
ark-std = "0.4.0" ark-std = "0.4.0"
clap = { version = "4.5.4", features = ["derive"] } clap = { version = "4.5.4", features = ["derive"] }
plnk = { git = "https://gitlab.isae-supaero.fr/a.stevan/plnk", tag = "0.3.0", version = "0.3.0" }
rand = "0.8.5" rand = "0.8.5"
rs_merkle = "1.4.1" rs_merkle = "1.4.1"
thiserror = "1.0.50" thiserror = "1.0.50"
......
...@@ -18,12 +18,12 @@ python scripts/plot/benches.py results.ndjson --bench setup ...@@ -18,12 +18,12 @@ python scripts/plot/benches.py results.ndjson --bench setup
```nushell ```nushell
cargo run --example bench_field_operations -- --nb-measurements 1000 cargo run --example bench_field_operations -- --nb-measurements 1000
| lines | lines
| each { from nuon } | each { from json }
| to ndjson | to ndjson
| save --force field.ndjson | save --force field.ndjson
cargo run --example bench_curve_group_operations -- --nb-measurements 1000 cargo run --example bench_curve_group_operations -- --nb-measurements 1000
| lines | lines
| each { from nuon } | each { from json }
| to ndjson | to ndjson
| save --force curve_group.ndjson | save --force curve_group.ndjson
``` ```
...@@ -34,7 +34,7 @@ def read-atomic-ops [ ...@@ -34,7 +34,7 @@ def read-atomic-ops [
let raw = $in let raw = $in
| insert t {|it| $it.times |math avg} | insert t {|it| $it.times |math avg}
| reject times | reject times
| rename --column { op: "group", curve: "species", t: "measurement" } | rename --column { label: "group", name: "species", t: "measurement" }
let included = if $include != [] { let included = if $include != [] {
$raw | where group in $include $raw | where group in $include
......
// see `benches/README.md` // see `benches/README.md`
use std::time::{Duration, Instant}; use std::time::Instant;
use ark_ec::CurveGroup; use ark_ec::CurveGroup;
use ark_ff::PrimeField; use ark_ff::PrimeField;
use clap::{command, Parser}; use clap::{command, Parser};
use rand::RngCore;
fn bench(b: &Bencher, op: &str, thing: fn(&mut dyn RngCore) -> Duration) {
let mut rng = ark_std::test_rng();
let mut times = vec![];
for i in 0..b.nb_measurements {
eprint!(
"{} on {} [{:>5}/{}]\r",
op,
b.name,
i + 1,
b.nb_measurements
);
times.push(thing(&mut rng).as_nanos());
}
eprintln!();
println!(
r#"{{op: "{}", curve: "{}", times: {:?}}}"#,
op, b.name, times
);
}
macro_rules! timeit {
($f:tt) => {{
let start_time = Instant::now();
#[allow(clippy::redundant_closure_call)]
let _ = $f();
Instant::now().duration_since(start_time)
}};
}
#[derive(Clone)]
struct Bencher {
nb_measurements: usize,
name: String,
}
impl Bencher {
fn new(nb_measurements: usize) -> Self {
Self {
nb_measurements,
name: "".to_string(),
}
}
fn with_name(&self, name: impl ToString) -> Self {
let mut new = self.clone();
new.name = name.to_string();
new
}
}
fn bench_template<F: PrimeField, G: CurveGroup<ScalarField = F>>(b: &Bencher) { fn bench_template<F: PrimeField, G: CurveGroup<ScalarField = F>>(b: &mut plnk::Bencher) {
bench(b, "random sampling", |rng| timeit!((|| G::rand(rng)))); plnk::bench(b, "random sampling", |rng| plnk::timeit!((|| G::rand(rng))));
bench(b, "addition", |rng| { plnk::bench(b, "addition", |rng| {
let g1 = G::rand(rng); let g1 = G::rand(rng);
let g2 = G::rand(rng); let g2 = G::rand(rng);
timeit!((|| g1 + g2)) plnk::timeit!((|| g1 + g2))
}); });
bench(b, "substraction", |rng| { plnk::bench(b, "substraction", |rng| {
let g1 = G::rand(rng); let g1 = G::rand(rng);
let g2 = G::rand(rng); let g2 = G::rand(rng);
timeit!((|| g1 - g2)) plnk::timeit!((|| g1 - g2))
}); });
bench(b, "double", |rng| { plnk::bench(b, "double", |rng| {
let g1 = G::rand(rng); let g1 = G::rand(rng);
timeit!((|| g1.double())) plnk::timeit!((|| g1.double()))
}); });
bench(b, "scalar multiplication", |rng| { plnk::bench(b, "scalar multiplication", |rng| {
let g1 = G::rand(rng); let g1 = G::rand(rng);
let f1 = F::rand(rng); let f1 = F::rand(rng);
timeit!((|| g1.mul(f1))) plnk::timeit!((|| g1.mul(f1)))
}); });
bench(b, "into affine", |rng| { plnk::bench(b, "into affine", |rng| {
let g1 = G::rand(rng); let g1 = G::rand(rng);
timeit!((|| g1.into_affine())) plnk::timeit!((|| g1.into_affine()))
}); });
bench(b, "from affine", |rng| { plnk::bench(b, "from affine", |rng| {
let g1_affine = G::rand(rng).into_affine(); let g1_affine = G::rand(rng).into_affine();
timeit!((|| Into::<G>::into(g1_affine))) plnk::timeit!((|| Into::<G>::into(g1_affine)))
}); });
bench(b, "affine addition", |rng| { plnk::bench(b, "affine addition", |rng| {
let g1_affine = G::rand(rng).into_affine(); let g1_affine = G::rand(rng).into_affine();
let g2_affine = G::rand(rng).into_affine(); let g2_affine = G::rand(rng).into_affine();
timeit!((|| g1_affine + g2_affine)) plnk::timeit!((|| g1_affine + g2_affine))
}); });
bench(b, "affine scalar multiplication", |rng| { plnk::bench(b, "affine scalar multiplication", |rng| {
let g1_affine = G::rand(rng).into_affine(); let g1_affine = G::rand(rng).into_affine();
let f1 = F::rand(rng); let f1 = F::rand(rng);
timeit!((|| g1_affine * f1)) plnk::timeit!((|| g1_affine * f1))
}); });
} }
...@@ -128,11 +74,11 @@ struct Cli { ...@@ -128,11 +74,11 @@ struct Cli {
fn main() { fn main() {
let cli = Cli::parse(); let cli = Cli::parse();
let bencher = Bencher::new(cli.nb_measurements); let bencher = plnk::Bencher::new(cli.nb_measurements, ark_std::rand::thread_rng());
bench_template::<ark_bls12_381::Fr, ark_bls12_381::G1Projective>( bench_template::<ark_bls12_381::Fr, ark_bls12_381::G1Projective>(
&bencher.with_name("BLS12-381"), &mut bencher.with_name("BLS12-381"),
); );
bench_template::<ark_bn254::Fr, ark_bn254::G1Projective>(&bencher.with_name("BN-254")); bench_template::<ark_bn254::Fr, ark_bn254::G1Projective>(&mut bencher.with_name("BN-254"));
bench_template::<ark_pallas::Fr, ark_pallas::Projective>(&bencher.with_name("PALLAS")); bench_template::<ark_pallas::Fr, ark_pallas::Projective>(&mut bencher.with_name("PALLAS"));
} }
...@@ -3,128 +3,74 @@ use std::time::{Duration, Instant}; ...@@ -3,128 +3,74 @@ use std::time::{Duration, Instant};
use ark_ff::PrimeField; use ark_ff::PrimeField;
use clap::{arg, command, Parser}; use clap::{arg, command, Parser};
use rand::RngCore;
fn bench(b: &Bencher, op: &str, thing: fn(&mut dyn RngCore) -> Duration) {
let mut rng = ark_std::test_rng();
let mut times = vec![];
for i in 0..b.nb_measurements {
eprint!(
"{} on {} [{:>5}/{}]\r",
op,
b.name,
i + 1,
b.nb_measurements
);
times.push(thing(&mut rng).as_nanos());
}
eprintln!();
println!(
r#"{{op: "{}", curve: "{}", times: {:?}}}"#,
op, b.name, times
);
}
macro_rules! timeit {
($f:tt) => {{
let start_time = Instant::now();
#[allow(clippy::redundant_closure_call)]
let _ = $f();
Instant::now().duration_since(start_time)
}};
}
#[derive(Clone)]
struct Bencher {
nb_measurements: usize,
name: String,
}
impl Bencher {
fn new(nb_measurements: usize) -> Self {
Self {
nb_measurements,
name: "".to_string(),
}
}
fn with_name(&self, name: impl ToString) -> Self {
let mut new = self.clone();
new.name = name.to_string();
new
}
}
fn bench_template<F: PrimeField>(b: &Bencher) { fn bench_template<F: PrimeField>(b: &mut plnk::Bencher) {
bench(b, "random sampling", |rng| timeit!((|| F::rand(rng)))); plnk::bench(b, "random sampling", |rng| plnk::timeit!((|| F::rand(rng))));
bench(b, "addition", |rng| { plnk::bench(b, "addition", |rng| {
let f1 = F::rand(rng); let f1 = F::rand(rng);
let f2 = F::rand(rng); let f2 = F::rand(rng);
timeit!((|| f1 + f2)) plnk::timeit!((|| f1 + f2))
}); });
bench(b, "substraction", |rng| { plnk::bench(b, "substraction", |rng| {
let f1 = F::rand(rng); let f1 = F::rand(rng);
let f2 = F::rand(rng); let f2 = F::rand(rng);
timeit!((|| f1 - f2)) plnk::timeit!((|| f1 - f2))
}); });
bench(b, "double", |rng| { plnk::bench(b, "double", |rng| {
let f1 = F::rand(rng); let f1 = F::rand(rng);
timeit!((|| f1.double())) plnk::timeit!((|| f1.double()))
}); });
bench(b, "multiplication", |rng| { plnk::bench(b, "multiplication", |rng| {
let f1 = F::rand(rng); let f1 = F::rand(rng);
let f2 = F::rand(rng); let f2 = F::rand(rng);
timeit!((|| f1 * f2)) plnk::timeit!((|| f1 * f2))
}); });
bench(b, "square", |rng| { plnk::bench(b, "square", |rng| {
let f1 = F::rand(rng); let f1 = F::rand(rng);
timeit!((|| f1.square())) plnk::timeit!((|| f1.square()))
}); });
bench(b, "inverse", |rng| { plnk::bench(b, "inverse", |rng| {
let f1 = F::rand(rng); let f1 = F::rand(rng);
timeit!((|| f1.inverse())) plnk::timeit!((|| f1.inverse()))
}); });
bench(b, "legendre", |rng| { plnk::bench(b, "legendre", |rng| {
let f1 = F::rand(rng); let f1 = F::rand(rng);
timeit!((|| f1.legendre())) plnk::timeit!((|| f1.legendre()))
}); });
bench(b, "sqrt", |rng| { plnk::bench(b, "sqrt", |rng| {
let f1 = F::rand(rng); let f1 = F::rand(rng);
if f1.legendre().is_qr() { if f1.legendre().is_qr() {
timeit!((|| f1.sqrt())) plnk::timeit!((|| f1.sqrt()))
} else { } else {
Duration::default() Duration::default()
} }
}); });
bench(b, "exponentiation", |rng| { plnk::bench(b, "exponentiation", |rng| {
let f1 = F::rand(rng); let f1 = F::rand(rng);
timeit!((|| f1.pow(F::MODULUS))) plnk::timeit!((|| f1.pow(F::MODULUS)))
}); });
bench(b, "into bigint", |rng| { plnk::bench(b, "into bigint", |rng| {
let f1 = F::rand(rng); let f1 = F::rand(rng);
timeit!((|| f1.into_bigint())) plnk::timeit!((|| f1.into_bigint()))
}); });
} }
...@@ -140,9 +86,9 @@ struct Cli { ...@@ -140,9 +86,9 @@ struct Cli {
fn main() { fn main() {
let cli = Cli::parse(); let cli = Cli::parse();
let bencher = Bencher::new(cli.nb_measurements); let bencher = plnk::Bencher::new(cli.nb_measurements, ark_std::rand::thread_rng());
bench_template::<ark_bls12_381::Fr>(&bencher.with_name("BLS12-381")); bench_template::<ark_bls12_381::Fr>(&mut bencher.with_name("BLS12-381"));
bench_template::<ark_bn254::Fr>(&bencher.with_name("BN-254")); bench_template::<ark_bn254::Fr>(&mut bencher.with_name("BN-254"));
bench_template::<ark_pallas::Fr>(&bencher.with_name("PALLAS")); bench_template::<ark_pallas::Fr>(&mut bencher.with_name("PALLAS"));
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment