Skip to content

Commit b62b7b7

Browse files
committed
Update ECDH benchmarks to include pubkey decode and add x-only
1 parent b66e0d1 commit b62b7b7

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

src/bench.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ int main(int argc, char** argv) {
176176
int iters = get_iters(default_iters);
177177

178178
/* Check for invalid user arguments */
179-
char* valid_args[] = {"ecdsa", "verify", "ecdsa_verify", "sign", "ecdsa_sign", "ecdh", "recover",
179+
char* valid_args[] = {"ecdsa", "verify", "ecdsa_verify", "sign", "ecdsa_sign", "ecdh", "ecdh_xonly", "recover",
180180
"ecdsa_recover", "schnorrsig", "schnorrsig_verify", "schnorrsig_sign", "ec",
181181
"keygen", "ec_keygen", "ellswift", "encode", "ellswift_encode", "decode",
182182
"ellswift_decode", "ellswift_keygen", "ellswift_ecdh"};

src/modules/ecdh/bench_impl.h

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,31 @@
1111

1212
typedef struct {
1313
secp256k1_context *ctx;
14-
secp256k1_pubkey point;
14+
unsigned char point[33];
1515
unsigned char scalar[32];
1616
} bench_ecdh_data;
1717

18+
19+
/* Outputs a hash of the coordinates, but also updates data->point with the coordinates. */
20+
static int ecdh_hash_function_bench(unsigned char* output, const unsigned char *x32, const unsigned char *y32, void* arg) {
21+
bench_ecdh_data* data = arg;
22+
int ret = secp256k1_ecdh_hash_function_sha256(output, x32, y32, NULL);
23+
data->point[0] ^= y32[17] & 1;
24+
memcpy(data->point + 1, x32, 32);
25+
return ret;
26+
}
27+
28+
static int ecdh_xonly_hash_function_bench(unsigned char* output, const unsigned char *x32, void* arg) {
29+
bench_ecdh_data* data = arg;
30+
int ret = secp256k1_ecdh_xonly_hash_function_sha256(output, x32, NULL);
31+
memcpy(data->point + 1, x32, 32);
32+
return ret;
33+
}
34+
1835
static void bench_ecdh_setup(void* arg) {
1936
int i;
2037
bench_ecdh_data *data = (bench_ecdh_data*)arg;
21-
const unsigned char point[] = {
38+
static const unsigned char point[33] = {
2239
0x03,
2340
0x54, 0x94, 0xc1, 0x5d, 0x32, 0x09, 0x97, 0x06,
2441
0xc2, 0x39, 0x5f, 0x94, 0x34, 0x87, 0x45, 0xfd,
@@ -29,16 +46,33 @@ static void bench_ecdh_setup(void* arg) {
2946
for (i = 0; i < 32; i++) {
3047
data->scalar[i] = i + 1;
3148
}
32-
CHECK(secp256k1_ec_pubkey_parse(data->ctx, &data->point, point, sizeof(point)) == 1);
49+
memcpy(data->point, point, sizeof(point));
3350
}
3451

3552
static void bench_ecdh(void* arg, int iters) {
3653
int i;
37-
unsigned char res[32];
3854
bench_ecdh_data *data = (bench_ecdh_data*)arg;
3955

4056
for (i = 0; i < iters; i++) {
41-
CHECK(secp256k1_ecdh(data->ctx, res, &data->point, data->scalar, NULL, NULL) == 1);
57+
/* Compute point multiplication of data->point with data->scalar, and then update:
58+
* - data->scalar to be the computed shared secret (hash of point multiplication output)
59+
* - data->point to have X coordinate equal to X coordinate of point multiplication output,
60+
* and optionally flipped Y coordinate. */
61+
secp256k1_pubkey pubkey;
62+
CHECK(secp256k1_ec_pubkey_parse(data->ctx, &pubkey, data->point, sizeof(data->point)) == 1);
63+
CHECK(secp256k1_ecdh(data->ctx, data->scalar, &pubkey, data->scalar, &ecdh_hash_function_bench, arg) == 1);
64+
}
65+
}
66+
67+
static void bench_ecdh_xonly(void* arg, int iters) {
68+
int i;
69+
bench_ecdh_data *data = (bench_ecdh_data*)arg;
70+
71+
for (i = 0; i < iters; i++) {
72+
/* Compute X-only point multiplication of data->point with data->scalar, and then update:
73+
* - data->scalar to be the computed shared secret (hash of point multiplication output X coordinate)
74+
* - data->point to have X coordinate equal to X coordinate of point multiplication output. */
75+
CHECK(secp256k1_ecdh_xonly(data->ctx, data->scalar, data->point + 1, data->scalar, &ecdh_xonly_hash_function_bench, arg) == 1);
4276
}
4377
}
4478

@@ -50,6 +84,7 @@ static void run_ecdh_bench(int iters, int argc, char** argv) {
5084
data.ctx = secp256k1_context_create(SECP256K1_FLAGS_TYPE_CONTEXT);
5185

5286
if (d || have_flag(argc, argv, "ecdh")) run_benchmark("ecdh", bench_ecdh, bench_ecdh_setup, NULL, &data, 10, iters);
87+
if (d || have_flag(argc, argv, "ecdh") || have_flag(argc, argv, "ecdh_xonly")) run_benchmark("ecdh_xonly", bench_ecdh_xonly, bench_ecdh_setup, NULL, &data, 10, iters);
5388

5489
secp256k1_context_destroy(data.ctx);
5590
}

0 commit comments

Comments
 (0)