Skip to content

Commit 6493b1d

Browse files
authored
fix: Correct benchmark function references, entropy seeds (#72)
- Fix BENCHMARK() macros to reference correct functions (BM_HMAC_SHA256, BM_AES_GCM_128, BM_AES_GCM_256) instead of wrong ones - Fix typo: BM_AEC_GCM -> BM_AES_GCM - Add benchmark::DoNotOptimize() to prevent compiler from optimizing out hash/encryption results - Use proper DRBG seeds with sufficient entropy (16 bytes) instead of short "test" string that doesn't meet SEC_P_COM requirement - Adjust benchmark input ranges for more meaningful measurements
1 parent a87e673 commit 6493b1d

2 files changed

Lines changed: 28 additions & 16 deletions

File tree

tools/benchmark/bm_drbg.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,38 @@ using namespace coinbase::crypto;
1010
static void DRBG_String(benchmark::State& state)
1111
{
1212
int u = state.range(0);
13+
// DRBG requires at least SEC_P_COM bits (128 bits) of entropy in the seed.
14+
// Use a fixed-size random seed generated once per benchmark run.
15+
buf_t seed = gen_random(SEC_P_COM / 8); // 16 bytes
1316
buf_t res;
1417
for (auto _ : state)
1518
{
16-
res = ro::drbg_sample_string(mem_t("test"), u);
19+
res = ro::drbg_sample_string(seed, u);
1720
}
1821
}
1922
BENCHMARK(DRBG_String)->Name("Crypto/DRBG/String")->RangeMultiplier(2)->Range(1 << 10, 1 << 18);
2023

2124
static void DRBG_Number(benchmark::State& state)
2225
{
2326
int u = state.range(0);
27+
buf_t seed = gen_random(SEC_P_COM / 8); // 16-byte DRBG seed
2428
mod_t m(bn_t::generate_prime(u, false), /* multiplicative_dense */ true);
2529
bn_t res;
2630
for (auto _ : state)
2731
{
28-
res = ro::drbg_sample_number(mem_t("test"), m);
32+
res = ro::drbg_sample_number(seed, m);
2933
}
3034
}
3135
BENCHMARK(DRBG_Number)->Name("Crypto/DRBG/Number")->RangeMultiplier(2)->Range(1 << 8, 1 << 12);
3236

3337
static void DRBG_Curve(benchmark::State& state)
3438
{
3539
ecurve_t curve = get_curve(state.range(0));
40+
buf_t seed = gen_random(SEC_P_COM / 8); // 16-byte DRBG seed
3641
ecc_point_t res;
3742
for (auto _ : state)
3843
{
39-
res = ro::drbg_sample_curve(mem_t("test"), curve);
44+
res = ro::drbg_sample_curve(seed, curve);
4045
}
4146
}
4247
BENCHMARK(DRBG_Curve)->Name("Crypto/DRBG/Curve")->Arg(3)->Arg(4);

tools/benchmark/bm_hash.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,53 @@
33
#include <cbmpc/crypto/base.h>
44
#include <cbmpc/crypto/ro.h>
55

6-
#define bit_len_lb 1 << 8
7-
#define bit_len_ub 1 << 12
8-
96
using namespace coinbase::crypto;
107

118
static void BM_SHA256(benchmark::State& state) {
129
buf_t input = gen_random(state.range(0));
13-
for (auto _ : state) auto _dummy = sha256_t::hash(input);
10+
for (auto _ : state) {
11+
auto hash = sha256_t::hash(input);
12+
benchmark::DoNotOptimize(hash);
13+
}
1414
}
15-
BENCHMARK(BM_SHA256)->Name("Core/Hash/SHA256")->RangeMultiplier(4)->Range(1, 4096);
15+
BENCHMARK(BM_SHA256)->Name("Core/Hash/SHA256")->RangeMultiplier(4)->Range(1 << 4, 1 << 12);
1616

1717
static void BM_HMAC_SHA256(benchmark::State& state) {
1818
buf_t input = gen_random(state.range(0));
1919
buf_t key = gen_random(16);
2020

2121
for (auto _ : state) {
2222
hmac_sha256_t hmac(key);
23-
hmac.calculate(input);
23+
auto mac = hmac.calculate(input);
24+
benchmark::DoNotOptimize(mac);
2425
}
2526
}
26-
BENCHMARK(BM_SHA256)->Name("Core/Hash/HMAC-SHA256")->RangeMultiplier(4)->Range(1, 4096);
27+
BENCHMARK(BM_HMAC_SHA256)->Name("Core/Hash/HMAC-SHA256")->RangeMultiplier(4)->Range(1 << 4, 1 << 12);
2728

28-
static void BM_AEC_GCM_128(benchmark::State& state) {
29+
static void BM_AES_GCM_128(benchmark::State& state) {
2930
buf_t input = gen_random(state.range(0));
3031

3132
buf_t key = gen_random(16);
3233
buf_t iv = gen_random(12);
3334

3435
buf_t output;
35-
for (auto _ : state) aes_gcm_t::encrypt(key, iv, mem_t(), 12, input, output);
36+
for (auto _ : state) {
37+
aes_gcm_t::encrypt(key, iv, mem_t(), 12, input, output);
38+
benchmark::DoNotOptimize(output);
39+
}
3640
}
37-
BENCHMARK(BM_AEC_GCM_128)->Name("Core/Hash/AES-GCM-128")->RangeMultiplier(4)->Range(1, 4096);
41+
BENCHMARK(BM_AES_GCM_128)->Name("Core/Hash/AES-GCM-128")->RangeMultiplier(4)->Range(1 << 10, 1 << 22);
3842

39-
static void BM_AEC_GCM_256(benchmark::State& state) {
43+
static void BM_AES_GCM_256(benchmark::State& state) {
4044
buf_t input = gen_random(state.range(0));
4145

4246
buf_t key = gen_random(32);
4347
buf_t iv = gen_random(12);
4448

4549
buf_t output;
46-
for (auto _ : state) aes_gcm_t::encrypt(key, iv, mem_t(), 12, input, output);
50+
for (auto _ : state) {
51+
aes_gcm_t::encrypt(key, iv, mem_t(), 12, input, output);
52+
benchmark::DoNotOptimize(output);
53+
}
4754
}
48-
BENCHMARK(BM_AEC_GCM_128)->Name("Core/Hash/AES-GCM-256")->RangeMultiplier(4)->Range(1, 4096);
55+
BENCHMARK(BM_AES_GCM_256)->Name("Core/Hash/AES-GCM-256")->RangeMultiplier(4)->Range(1 << 10, 1 << 22);

0 commit comments

Comments
 (0)