Skip to content

Commit 0544537

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

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/bench_ecmult.c

Lines changed: 31 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,32 @@ 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+
int known_on_curve = 1;
135+
secp256k1_ecmult_const_xonly(&data->output_xonly[i], &pubkey->x, NULL, scalar, known_on_curve);
136+
}
137+
}
138+
139+
static void bench_ecmult_const_xonly_teardown(void* arg, int iters) {
140+
bench_data* data = (bench_data*)arg;
141+
int i;
142+
143+
/* verify by comparing with x coordinate of regular ecmult result */
144+
for (i = 0; i < iters; ++i) {
145+
const secp256k1_gej* pubkey_gej = &data->pubkeys_gej[(data->offset1+i) % POINTS];
146+
const secp256k1_scalar* scalar = &data->scalars[(data->offset2+i) % POINTS];
147+
secp256k1_gej expected_gej;
148+
secp256k1_ecmult(&expected_gej, pubkey_gej, scalar, NULL);
149+
CHECK(secp256k1_gej_eq_x_var(&data->output_xonly[i], &expected_gej));
150+
}
151+
}
152+
126153
static void bench_ecmult_1p(void* arg, int iters) {
127154
bench_data* data = (bench_data*)arg;
128155
int i;
@@ -171,6 +198,8 @@ static void run_ecmult_bench(bench_data* data, int iters) {
171198
run_benchmark(str, bench_ecmult_gen, bench_ecmult_setup, bench_ecmult_gen_teardown, data, 10, iters);
172199
sprintf(str, "ecmult_const");
173200
run_benchmark(str, bench_ecmult_const, bench_ecmult_setup, bench_ecmult_const_teardown, data, 10, iters);
201+
sprintf(str, "ecmult_const_xonly");
202+
run_benchmark(str, bench_ecmult_const_xonly, bench_ecmult_setup, bench_ecmult_const_xonly_teardown, data, 10, iters);
174203
/* ecmult with non generator point */
175204
sprintf(str, "ecmult_1p");
176205
run_benchmark(str, bench_ecmult_1p, bench_ecmult_setup, bench_ecmult_1p_teardown, data, 10, iters);
@@ -319,6 +348,7 @@ int main(int argc, char **argv) {
319348
data.pubkeys_gej = malloc(sizeof(secp256k1_gej) * POINTS);
320349
data.expected_output = malloc(sizeof(secp256k1_gej) * (iters + 1));
321350
data.output = malloc(sizeof(secp256k1_gej) * (iters + 1));
351+
data.output_xonly = malloc(sizeof(secp256k1_fe) * (iters + 1));
322352

323353
/* Generate a set of scalars, and private/public keypairs. */
324354
secp256k1_gej_set_ge(&data.pubkeys_gej[0], &secp256k1_ge_const_g);
@@ -361,6 +391,7 @@ int main(int argc, char **argv) {
361391
free(data.pubkeys);
362392
free(data.pubkeys_gej);
363393
free(data.seckeys);
394+
free(data.output_xonly);
364395
free(data.output);
365396
free(data.expected_output);
366397

0 commit comments

Comments
 (0)