From d783f2756ef5055a3e84b9d5c4d432711d0fa362 Mon Sep 17 00:00:00 2001 From: DrPeterVanNostrand Date: Wed, 29 Aug 2018 20:12:46 +0000 Subject: [PATCH] Added benchmarks for polynomial multiplication and interpolation. --- Cargo.toml | 6 ++++++ README.md | 11 +++++++++++ benches/bench.rs | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 benches/bench.rs diff --git a/Cargo.toml b/Cargo.toml index f57f898..0dff7af 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,4 +28,10 @@ tiny-keccak = "1.4" [dev-dependencies] bincode = "1.0.0" +criterion = "0.2" +rand = "0.4.2" serde_derive = "1.0.55" + +[[bench]] +name = "bench" +harness = false diff --git a/README.md b/README.md index f8199e8..8fe9001 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,17 @@ must tolerate up to `t` adversarial (malicious or faulty) nodes. Because `t + 1` nodes are required to sign or reveal information, messages can be trusted by third-parties as representing the consensus of the network. +## Performance + +Benchmarking functionality is kept in the [`benches` directory](benches). You +can run the benchmarks with the following command: + +``` +$ RUSTFLAGS="-C target_cpu=native" cargo bench +``` + +We use the [`criterion`](https://crates.io/crates/criterion) benchmarking library. + ## License Licensed under either of: diff --git a/benches/bench.rs b/benches/bench.rs new file mode 100644 index 0000000..2c5e68f --- /dev/null +++ b/benches/bench.rs @@ -0,0 +1,36 @@ +#[macro_use] +extern crate criterion; +extern crate rand; +extern crate threshold_crypto; + +use criterion::Criterion; +use threshold_crypto::poly::Poly; + +mod poly_benches { + use super::*; + + // Benchmarks multiplication of two degree 3 polynomials. + fn multiplication(c: &mut Criterion) { + let mut rng = rand::thread_rng(); + let lhs = Poly::random(3, &mut rng).unwrap(); + let rhs = Poly::random(3, &mut rng).unwrap(); + c.bench_function("Polynomial multiplication", move |b| b.iter(|| &lhs * &rhs)); + } + + // Benchmarks Lagrange interpolation for a degree 3 polynomial. + fn interpolate(c: &mut Criterion) { + // Points from the the polynomial: `y(x) = 5x^3 + 0x^2 + x - 2`. + let sample_points = vec![(-1, -8), (2, 40), (3, 136), (5, 628)]; + c.bench_function("Polynomial interpolation", move |b| { + b.iter(|| Poly::interpolate(sample_points.clone()).unwrap()) + }); + } + + criterion_group!{ + name = poly_benches; + config = Criterion::default(); + targets = multiplication, interpolate, + } +} + +criterion_main!(poly_benches::poly_benches);