Skip to content

Commit cc71265

Browse files
committed
bench_ecmult: add benchmark for ecmult_const_xonly
1 parent 70f149b commit cc71265

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/bench_ecmult.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ typedef struct {
5656

5757
/* Benchmark output. */
5858
secp256k1_gej* output;
59+
secp256k1_fe* output_xonly;
5960
} bench_data;
6061

6162
/* Hashes x into [0, POINTS) twice and store the result in offset1 and offset2. */
@@ -123,6 +124,31 @@ static void bench_ecmult_const_teardown(void* arg, int iters) {
123124
bench_ecmult_teardown_helper(data, &data->offset1, &data->offset2, NULL, iters);
124125
}
125126

127+
static void bench_ecmult_const_xonly(void* arg, int iters) {
128+
bench_data* data = (bench_data*)arg;
129+
int i;
130+
131+
for (i = 0; i < iters; ++i) {
132+
const secp256k1_ge* pubkey = &data->pubkeys[(data->offset1+i) % POINTS];
133+
const secp256k1_scalar* scalar = &data->scalars[(data->offset2+i) % POINTS];
134+
secp256k1_ecmult_const_xonly(&data->output_xonly[i], &pubkey->x, NULL, scalar, 1);
135+
}
136+
}
137+
138+
static void bench_ecmult_const_xonly_teardown(void* arg, int iters) {
139+
bench_data* data = (bench_data*)arg;
140+
int i;
141+
142+
/* verify by comparing with x coordinate of regular ecmult_const result */
143+
for (i = 0; i < iters; ++i) {
144+
const secp256k1_ge* pubkey = &data->pubkeys[(data->offset1+i) % POINTS];
145+
const secp256k1_scalar* scalar = &data->scalars[(data->offset2+i) % POINTS];
146+
secp256k1_gej expected_gej;
147+
secp256k1_ecmult_const(&expected_gej, pubkey, scalar);
148+
CHECK(secp256k1_gej_eq_x_var(&data->output_xonly[i], &expected_gej));
149+
}
150+
}
151+
126152
static void bench_ecmult_1p(void* arg, int iters) {
127153
bench_data* data = (bench_data*)arg;
128154
int i;
@@ -171,6 +197,8 @@ static void run_ecmult_bench(bench_data* data, int iters) {
171197
run_benchmark(str, bench_ecmult_gen, bench_ecmult_setup, bench_ecmult_gen_teardown, data, 10, iters);
172198
sprintf(str, "ecmult_const");
173199
run_benchmark(str, bench_ecmult_const, bench_ecmult_setup, bench_ecmult_const_teardown, data, 10, iters);
200+
sprintf(str, "ecmult_const_xonly");
201+
run_benchmark(str, bench_ecmult_const_xonly, bench_ecmult_setup, bench_ecmult_const_xonly_teardown, data, 10, iters);
174202
/* ecmult with non generator point */
175203
sprintf(str, "ecmult_1p");
176204
run_benchmark(str, bench_ecmult_1p, bench_ecmult_setup, bench_ecmult_1p_teardown, data, 10, iters);
@@ -319,6 +347,7 @@ int main(int argc, char **argv) {
319347
data.pubkeys_gej = malloc(sizeof(secp256k1_gej) * POINTS);
320348
data.expected_output = malloc(sizeof(secp256k1_gej) * (iters + 1));
321349
data.output = malloc(sizeof(secp256k1_gej) * (iters + 1));
350+
data.output_xonly = malloc(sizeof(secp256k1_fe) * (iters + 1));
322351

323352
/* Generate a set of scalars, and private/public keypairs. */
324353
secp256k1_gej_set_ge(&data.pubkeys_gej[0], &secp256k1_ge_const_g);
@@ -361,6 +390,7 @@ int main(int argc, char **argv) {
361390
free(data.pubkeys);
362391
free(data.pubkeys_gej);
363392
free(data.seckeys);
393+
free(data.output_xonly);
364394
free(data.output);
365395
free(data.expected_output);
366396

0 commit comments

Comments
 (0)