From 0223beea17c6a45ac6978e4e7cbb133a7cba60d6 Mon Sep 17 00:00:00 2001 From: Vlad Krasnov Date: Fri, 27 Oct 2023 10:01:35 -0400 Subject: [PATCH] Benchmarks: add ECDSA benchmarks Fixes: #1772 --- bench/Cargo.toml | 5 ++++ bench/ecdsa.rs | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 bench/ecdsa.rs diff --git a/bench/Cargo.toml b/bench/Cargo.toml index e50f85f7db..98d002e442 100644 --- a/bench/Cargo.toml +++ b/bench/Cargo.toml @@ -19,3 +19,8 @@ path = "aead.rs" name = "x25519" harness = false path = "x25519.rs" + +[[bench]] +name = "ecdsa" +harness = false +path = "ecdsa.rs" diff --git a/bench/ecdsa.rs b/bench/ecdsa.rs new file mode 100644 index 0000000000..eb68d896cf --- /dev/null +++ b/bench/ecdsa.rs @@ -0,0 +1,73 @@ +// Copyright 2015-2023 Brian Smith. +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES +// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY +// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION +// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN +// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +#![allow(missing_docs)] + +use criterion::{black_box, criterion_group, criterion_main, BatchSize, Criterion}; +use ring::{ + rand, + signature::{self, EcdsaKeyPair, EcdsaSigningAlgorithm, EcdsaVerificationAlgorithm, KeyPair}, +}; + +static ALGORITHMS: &[(&str, &EcdsaSigningAlgorithm, &EcdsaVerificationAlgorithm)] = &[ + ( + "p256", + &signature::ECDSA_P256_SHA256_ASN1_SIGNING, + &signature::ECDSA_P256_SHA256_ASN1, + ), + ( + "p384", + &signature::ECDSA_P384_SHA384_ASN1_SIGNING, + &signature::ECDSA_P384_SHA384_ASN1, + ), +]; + +fn sign(c: &mut Criterion) { + for (alg_name, alg, _) in ALGORITHMS { + c.bench_function(&format!("ecdsa_{alg_name}_sign"), |b| { + let rng = rand::SystemRandom::new(); + let pkcs8_bytes = EcdsaKeyPair::generate_pkcs8(alg, &rng).unwrap(); + let key_pair = EcdsaKeyPair::from_pkcs8(alg, pkcs8_bytes.as_ref(), &rng).unwrap(); + + b.iter(|| { + key_pair.sign(&rng, black_box(&[])).unwrap(); + }) + }); + } +} + +fn verify(c: &mut Criterion) { + for (alg_name, sign_alg, verify_alg) in ALGORITHMS { + c.bench_function(&format!("ecdsa_{alg_name}_verify"), |b| { + let rng = rand::SystemRandom::new(); + let pkcs8_bytes = EcdsaKeyPair::generate_pkcs8(sign_alg, &rng).unwrap(); + let key_pair = EcdsaKeyPair::from_pkcs8(sign_alg, pkcs8_bytes.as_ref(), &rng).unwrap(); + + let public_key = + signature::UnparsedPublicKey::new(*verify_alg, key_pair.public_key().as_ref()); + + b.iter_batched( + || key_pair.sign(&rng, &[]).unwrap(), + |signature| { + public_key + .verify(black_box(&[]), black_box(signature.as_ref())) + .unwrap(); + }, + BatchSize::LargeInput, + ) + }); + } +} + +criterion_group!(ecdsa, sign, verify); +criterion_main!(ecdsa);