Skip to content

Commit a6379f4

Browse files
committed
Add ElligatorSwift benchmarks
1 parent bc604c1 commit a6379f4

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

src/bench.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ static void bench_keygen_run(void* arg, int iters) {
149149
# include "modules/schnorrsig/bench_impl.h"
150150
#endif
151151

152+
#ifdef ENABLE_MODULE_ELLSWIFT
153+
# include "modules/ellswift/bench_impl.h"
154+
#endif
155+
152156
int main(int argc, char** argv) {
153157
int i;
154158
secp256k1_pubkey pubkey;
@@ -162,7 +166,8 @@ int main(int argc, char** argv) {
162166
/* Check for invalid user arguments */
163167
char* valid_args[] = {"ecdsa", "verify", "ecdsa_verify", "sign", "ecdsa_sign", "ecdh", "recover",
164168
"ecdsa_recover", "schnorrsig", "schnorrsig_verify", "schnorrsig_sign", "ec",
165-
"keygen", "ec_keygen"};
169+
"keygen", "ec_keygen", "ellswift", "encode", "ellswift_encode", "decode",
170+
"ellswift_decode", "ellswift_keygen", "ellswift_ecdh"};
166171
size_t valid_args_size = sizeof(valid_args)/sizeof(valid_args[0]);
167172
int invalid_args = have_invalid_args(argc, argv, valid_args, valid_args_size);
168173

@@ -248,5 +253,10 @@ int main(int argc, char** argv) {
248253
run_schnorrsig_bench(iters, argc, argv);
249254
#endif
250255

256+
#ifdef ENABLE_MODULE_ELLSWIFT
257+
/* ElligatorSwift benchmarks */
258+
run_ellswift_bench(iters, argc, argv);
259+
#endif
260+
251261
return 0;
252262
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
include_HEADERS += include/secp256k1_ellswift.h
2+
noinst_HEADERS += src/modules/ellswift/bench_impl.h
23
noinst_HEADERS += src/modules/ellswift/main_impl.h

src/modules/ellswift/bench_impl.h

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/***********************************************************************
2+
* Copyright (c) 2022 Pieter Wuille *
3+
* Distributed under the MIT software license, see the accompanying *
4+
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5+
***********************************************************************/
6+
7+
#ifndef SECP256K1_MODULE_ELLSWIFT_BENCH_H
8+
#define SECP256K1_MODULE_ELLSWIFT_BENCH_H
9+
10+
#include "../include/secp256k1_ellswift.h"
11+
12+
typedef struct {
13+
secp256k1_context *ctx;
14+
secp256k1_pubkey point[256];
15+
unsigned char rnd64[64];
16+
} bench_ellswift_data;
17+
18+
static void bench_ellswift_setup(void* arg) {
19+
int i;
20+
bench_ellswift_data *data = (bench_ellswift_data*)arg;
21+
static const unsigned char init[64] = {
22+
0x78, 0x1f, 0xb7, 0xd4, 0x67, 0x7f, 0x08, 0x68,
23+
0xdb, 0xe3, 0x1d, 0x7f, 0x1b, 0xb0, 0xf6, 0x9e,
24+
0x0a, 0x64, 0xca, 0x32, 0x9e, 0xc6, 0x20, 0x79,
25+
0x03, 0xf3, 0xd0, 0x46, 0x7a, 0x0f, 0xd2, 0x21,
26+
0xb0, 0x2c, 0x46, 0xd8, 0xba, 0xca, 0x26, 0x4f,
27+
0x8f, 0x8c, 0xd4, 0xdd, 0x2d, 0x04, 0xbe, 0x30,
28+
0x48, 0x51, 0x1e, 0xd4, 0x16, 0xfd, 0x42, 0x85,
29+
0x62, 0xc9, 0x02, 0xf9, 0x89, 0x84, 0xff, 0xdc
30+
};
31+
memcpy(data->rnd64, init, 64);
32+
for (i = 0; i < 256; ++i) {
33+
int j;
34+
CHECK(secp256k1_ellswift_decode(data->ctx, &data->point[i], data->rnd64));
35+
for (j = 0; j < 64; ++j) {
36+
data->rnd64[j] += 1;
37+
}
38+
}
39+
CHECK(secp256k1_ellswift_encode(data->ctx, data->rnd64, &data->point[255], init + 16));
40+
}
41+
42+
static void bench_ellswift_encode(void* arg, int iters) {
43+
int i;
44+
bench_ellswift_data *data = (bench_ellswift_data*)arg;
45+
46+
for (i = 0; i < iters; i++) {
47+
CHECK(secp256k1_ellswift_encode(data->ctx, data->rnd64, &data->point[i & 255], data->rnd64 + 16));
48+
}
49+
}
50+
51+
static void bench_ellswift_create(void* arg, int iters) {
52+
int i;
53+
bench_ellswift_data *data = (bench_ellswift_data*)arg;
54+
55+
for (i = 0; i < iters; i++) {
56+
unsigned char buf[64];
57+
CHECK(secp256k1_ellswift_create(data->ctx, buf, data->rnd64, data->rnd64 + 32));
58+
memcpy(data->rnd64, buf, 64);
59+
}
60+
}
61+
62+
static void bench_ellswift_decode(void* arg, int iters) {
63+
int i;
64+
secp256k1_pubkey out;
65+
size_t len;
66+
bench_ellswift_data *data = (bench_ellswift_data*)arg;
67+
68+
for (i = 0; i < iters; i++) {
69+
CHECK(secp256k1_ellswift_decode(data->ctx, &out, data->rnd64) == 1);
70+
len = 33;
71+
CHECK(secp256k1_ec_pubkey_serialize(data->ctx, data->rnd64 + (i % 32), &len, &out, SECP256K1_EC_COMPRESSED));
72+
}
73+
}
74+
75+
static void bench_ellswift_xdh(void* arg, int iters) {
76+
int i;
77+
bench_ellswift_data *data = (bench_ellswift_data*)arg;
78+
79+
for (i = 0; i < iters; i++) {
80+
CHECK(secp256k1_ellswift_xdh(data->ctx, data->rnd64 + (i % 33), data->rnd64, data->rnd64, data->rnd64 + ((i + 16) % 33), NULL, NULL) == 1);
81+
}
82+
}
83+
84+
void run_ellswift_bench(int iters, int argc, char** argv) {
85+
bench_ellswift_data data;
86+
int d = argc == 1;
87+
88+
/* create a context with signing capabilities */
89+
data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
90+
memset(data.rnd64, 11, sizeof(data.rnd64));
91+
92+
if (d || have_flag(argc, argv, "ellswift") || have_flag(argc, argv, "encode") || have_flag(argc, argv, "ellswift_encode")) run_benchmark("ellswift_encode", bench_ellswift_encode, bench_ellswift_setup, NULL, &data, 10, iters);
93+
if (d || have_flag(argc, argv, "ellswift") || have_flag(argc, argv, "decode") || have_flag(argc, argv, "ellswift_decode")) run_benchmark("ellswift_decode", bench_ellswift_decode, bench_ellswift_setup, NULL, &data, 10, iters);
94+
if (d || have_flag(argc, argv, "ellswift") || have_flag(argc, argv, "keygen") || have_flag(argc, argv, "ellswift_keygen")) run_benchmark("ellswift_keygen", bench_ellswift_create, bench_ellswift_setup, NULL, &data, 10, iters);
95+
if (d || have_flag(argc, argv, "ellswift") || have_flag(argc, argv, "ecdh") || have_flag(argc, argv, "ellswift_ecdh")) run_benchmark("ellswift_ecdh", bench_ellswift_xdh, bench_ellswift_setup, NULL, &data, 10, iters);
96+
97+
secp256k1_context_destroy(data.ctx);
98+
}
99+
100+
#endif /* SECP256K1_MODULE_ellswift_BENCH_H */

0 commit comments

Comments
 (0)