Skip to content

Commit 593e6ba

Browse files
committed
Clean up ecmult_bench to make space for more benchmarks
1 parent 50f3367 commit 593e6ba

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

src/bench_ecmult.c

+22-17
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,26 @@ typedef struct {
2828
secp256k1_gej* expected_output;
2929
secp256k1_ecmult_multi_func ecmult_multi;
3030

31-
/* Changes per test */
31+
/* Changes per benchmark */
3232
size_t count;
3333
int includes_g;
3434

35-
/* Changes per test iteration */
35+
/* Changes per benchmark iteration, used to pick different scalars and pubkeys
36+
* in each run. */
3637
size_t offset1;
3738
size_t offset2;
3839

39-
/* Test output. */
40+
/* Benchmark output. */
4041
secp256k1_gej* output;
4142
} bench_data;
4243

43-
static int bench_callback(secp256k1_scalar* sc, secp256k1_ge* ge, size_t idx, void* arg) {
44+
/* Hashes x into [0, POINTS) twice and store the result in offset1 and offset2. */
45+
static void hash_into_offset(bench_data* data, size_t x) {
46+
data->offset1 = (x * 0x537b7f6f + 0x8f66a481) % POINTS;
47+
data->offset2 = (x * 0x7f6f537b + 0x6a1a8f49) % POINTS;
48+
}
49+
50+
static int bench_ecmult_multi_callback(secp256k1_scalar* sc, secp256k1_ge* ge, size_t idx, void* arg) {
4451
bench_data* data = (bench_data*)arg;
4552
if (data->includes_g) ++idx;
4653
if (idx == 0) {
@@ -53,7 +60,7 @@ static int bench_callback(secp256k1_scalar* sc, secp256k1_ge* ge, size_t idx, vo
5360
return 1;
5461
}
5562

56-
static void bench_ecmult(void* arg, int iters) {
63+
static void bench_ecmult_multi(void* arg, int iters) {
5764
bench_data* data = (bench_data*)arg;
5865

5966
int includes_g = data->includes_g;
@@ -62,19 +69,18 @@ static void bench_ecmult(void* arg, int iters) {
6269
iters = iters / data->count;
6370

6471
for (iter = 0; iter < iters; ++iter) {
65-
data->ecmult_multi(&data->ctx->error_callback, &data->ctx->ecmult_ctx, data->scratch, &data->output[iter], data->includes_g ? &data->scalars[data->offset1] : NULL, bench_callback, arg, count - includes_g);
72+
data->ecmult_multi(&data->ctx->error_callback, &data->ctx->ecmult_ctx, data->scratch, &data->output[iter], data->includes_g ? &data->scalars[data->offset1] : NULL, bench_ecmult_multi_callback, arg, count - includes_g);
6673
data->offset1 = (data->offset1 + count) % POINTS;
6774
data->offset2 = (data->offset2 + count - 1) % POINTS;
6875
}
6976
}
7077

71-
static void bench_ecmult_setup(void* arg) {
78+
static void bench_ecmult_multi_setup(void* arg) {
7279
bench_data* data = (bench_data*)arg;
73-
data->offset1 = (data->count * 0x537b7f6f + 0x8f66a481) % POINTS;
74-
data->offset2 = (data->count * 0x7f6f537b + 0x6a1a8f49) % POINTS;
80+
hash_into_offset(data, data->count);
7581
}
7682

77-
static void bench_ecmult_teardown(void* arg, int iters) {
83+
static void bench_ecmult_multi_teardown(void* arg, int iters) {
7884
bench_data* data = (bench_data*)arg;
7985
int iter;
8086
iters = iters / data->count;
@@ -102,7 +108,7 @@ static void generate_scalar(uint32_t num, secp256k1_scalar* scalar) {
102108
CHECK(!overflow);
103109
}
104110

105-
static void run_test(bench_data* data, size_t count, int includes_g, int num_iters) {
111+
static void run_ecmult_multi_bench(bench_data* data, size_t count, int includes_g, int num_iters) {
106112
char str[32];
107113
static const secp256k1_scalar zero = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
108114
size_t iters = 1 + num_iters / count;
@@ -112,8 +118,7 @@ static void run_test(bench_data* data, size_t count, int includes_g, int num_ite
112118
data->includes_g = includes_g;
113119

114120
/* Compute (the negation of) the expected results directly. */
115-
data->offset1 = (data->count * 0x537b7f6f + 0x8f66a481) % POINTS;
116-
data->offset2 = (data->count * 0x7f6f537b + 0x6a1a8f49) % POINTS;
121+
hash_into_offset(data, data->count);
117122
for (iter = 0; iter < iters; ++iter) {
118123
secp256k1_scalar tmp;
119124
secp256k1_scalar total = data->scalars[(data->offset1++) % POINTS];
@@ -127,8 +132,8 @@ static void run_test(bench_data* data, size_t count, int includes_g, int num_ite
127132
}
128133

129134
/* Run the benchmark. */
130-
sprintf(str, includes_g ? "ecmult_%ig" : "ecmult_%i", (int)count);
131-
run_benchmark(str, bench_ecmult, bench_ecmult_setup, bench_ecmult_teardown, data, 10, count * iters);
135+
sprintf(str, includes_g ? "ecmult_multi %ig" : "ecmult_multi %i", (int)count);
136+
run_benchmark(str, bench_ecmult_multi, bench_ecmult_multi_setup, bench_ecmult_multi_teardown, data, 10, count * iters);
132137
}
133138

134139
int main(int argc, char **argv) {
@@ -185,7 +190,7 @@ int main(int argc, char **argv) {
185190
free(pubkeys_gej);
186191

187192
for (i = 1; i <= 8; ++i) {
188-
run_test(&data, i, 1, iters);
193+
run_ecmult_multi_bench(&data, i, 1, iters);
189194
}
190195

191196
/* This is disabled with low count of iterations because the loop runs 77 times even with iters=1
@@ -194,7 +199,7 @@ int main(int argc, char **argv) {
194199
if (iters > 2) {
195200
for (p = 0; p <= 11; ++p) {
196201
for (i = 9; i <= 16; ++i) {
197-
run_test(&data, i << p, 1, iters);
202+
run_ecmult_multi_bench(&data, i << p, 1, iters);
198203
}
199204
}
200205
}

0 commit comments

Comments
 (0)