MLKEM benchmark: also add a "private key from seed" benchmark. Change-Id: Ie78a79edb29e1ef0ed31ec621290262e6a6a6964 Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/98067 Reviewed-by: Xiangfei Ding <xfding@google.com> Auto-Submit: Rudolf Polzer <rpolzer@google.com> Commit-Queue: Xiangfei Ding <xfding@google.com>
diff --git a/bench/mlkem.cc b/bench/mlkem.cc index 84bbb66..69cdb98 100644 --- a/bench/mlkem.cc +++ b/bench/mlkem.cc
@@ -21,6 +21,7 @@ #include <openssl/err.h> #include <openssl/evp.h> #include <openssl/mlkem.h> +#include <openssl/rand.h> #include "./internal.h" @@ -136,6 +137,23 @@ } } +void BM_SpeedMLKEM768PrivateKeyFromSeedOnly(benchmark::State &state) { + for (auto _ : state) { + state.PauseTiming(); + uint8_t seed[MLKEM_SEED_BYTES]; + RAND_bytes(seed, sizeof(seed)); + benchmark::DoNotOptimize(seed); + state.ResumeTiming(); + + MLKEM768_private_key priv; + if (!MLKEM768_private_key_from_seed(&priv, seed, sizeof(seed))) { + state.SkipWithError("MLKEM768_private_key_from_seed failed"); + return; + } + benchmark::DoNotOptimize(priv); + } +} + void BM_SpeedMLKEM768DecapOnly(benchmark::State &state) { uint8_t ciphertext[MLKEM768_CIPHERTEXT_BYTES]; // This ciphertext is nonsense, but decap is constant-time so, for the @@ -215,6 +233,23 @@ } } +void BM_SpeedMLKEM1024PrivateKeyFromSeedOnly(benchmark::State &state) { + for (auto _ : state) { + state.PauseTiming(); + uint8_t seed[MLKEM_SEED_BYTES]; + RAND_bytes(seed, sizeof(seed)); + benchmark::DoNotOptimize(seed); + state.ResumeTiming(); + + MLKEM1024_private_key priv; + if (!MLKEM1024_private_key_from_seed(&priv, seed, sizeof(seed))) { + state.SkipWithError("MLKEM1024_private_key_from_seed failed"); + return; + } + benchmark::DoNotOptimize(priv); + } +} + void BM_SpeedMLKEM1024DecapOnly(benchmark::State &state) { uint8_t ciphertext[MLKEM1024_CIPHERTEXT_BYTES]; // This ciphertext is nonsense, but decap is constant-time so, for the @@ -294,10 +329,14 @@ BENCHMARK(BM_SpeedMLKEM1024ParseEncap)->Apply(bssl::bench::SetThreads); BENCHMARK(BM_SpeedMLKEM768KeyGenOnly)->Apply(bssl::bench::SetThreads); + BENCHMARK(BM_SpeedMLKEM768PrivateKeyFromSeedOnly) + ->Apply(bssl::bench::SetThreads); BENCHMARK(BM_SpeedMLKEM768DecapOnly)->Apply(bssl::bench::SetThreads); BENCHMARK(BM_SpeedMLKEM768ParseOnly)->Apply(bssl::bench::SetThreads); BENCHMARK(BM_SpeedMLKEM768EncapOnly)->Apply(bssl::bench::SetThreads); BENCHMARK(BM_SpeedMLKEM1024KeyGenOnly)->Apply(bssl::bench::SetThreads); + BENCHMARK(BM_SpeedMLKEM1024PrivateKeyFromSeedOnly) + ->Apply(bssl::bench::SetThreads); BENCHMARK(BM_SpeedMLKEM1024DecapOnly)->Apply(bssl::bench::SetThreads); BENCHMARK(BM_SpeedMLKEM1024ParseOnly)->Apply(bssl::bench::SetThreads); BENCHMARK(BM_SpeedMLKEM1024EncapOnly)->Apply(bssl::bench::SetThreads);