Skip to content

Commit 1bcea8c

Browse files
committed
Add benchmarks for ellswift module
1 parent 2d1d41a commit 1bcea8c

File tree

3 files changed

+136
-1
lines changed

3 files changed

+136
-1
lines changed

src/bench.c

+29-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ static void help(int default_iters) {
5555
printf(" schnorrsig_verify : Schnorr verification algorithm\n");
5656
#endif
5757

58+
#ifdef ENABLE_MODULE_ELLSWIFT
59+
printf(" ellswift : all ElligatorSwift benchmarks (encode, decode, keygen, ecdh)\n");
60+
printf(" ellswift_encode : ElligatorSwift encoding\n");
61+
printf(" ellswift_decode : ElligatorSwift decoding\n");
62+
printf(" ellswift_keygen : ElligatorSwift key generation\n");
63+
printf(" ellswift_ecdh : ECDH on ElligatorSwift keys\n");
64+
#endif
65+
5866
printf("\n");
5967
}
6068

@@ -153,6 +161,10 @@ static void bench_keygen_run(void *arg, int iters) {
153161
# include "modules/schnorrsig/bench_impl.h"
154162
#endif
155163

164+
#ifdef ENABLE_MODULE_ELLSWIFT
165+
# include "modules/ellswift/bench_impl.h"
166+
#endif
167+
156168
int main(int argc, char** argv) {
157169
int i;
158170
secp256k1_pubkey pubkey;
@@ -166,7 +178,8 @@ int main(int argc, char** argv) {
166178
/* Check for invalid user arguments */
167179
char* valid_args[] = {"ecdsa", "verify", "ecdsa_verify", "sign", "ecdsa_sign", "ecdh", "recover",
168180
"ecdsa_recover", "schnorrsig", "schnorrsig_verify", "schnorrsig_sign", "ec",
169-
"keygen", "ec_keygen"};
181+
"keygen", "ec_keygen", "ellswift", "encode", "ellswift_encode", "decode",
182+
"ellswift_decode", "ellswift_keygen", "ellswift_ecdh"};
170183
size_t valid_args_size = sizeof(valid_args)/sizeof(valid_args[0]);
171184
int invalid_args = have_invalid_args(argc, argv, valid_args, valid_args_size);
172185

@@ -208,6 +221,16 @@ int main(int argc, char** argv) {
208221
}
209222
#endif
210223

224+
#ifndef ENABLE_MODULE_ELLSWIFT
225+
if (have_flag(argc, argv, "ellswift") || have_flag(argc, argv, "ellswift_encode") || have_flag(argc, argv, "ellswift_decode") ||
226+
have_flag(argc, argv, "encode") || have_flag(argc, argv, "decode") || have_flag(argc, argv, "ellswift_keygen") ||
227+
have_flag(argc, argv, "ellswift_ecdh")) {
228+
fprintf(stderr, "./bench: ElligatorSwift module not enabled.\n");
229+
fprintf(stderr, "Use ./configure --enable-module-ellswift.\n\n");
230+
return 1;
231+
}
232+
#endif
233+
211234
/* ECDSA benchmark */
212235
data.ctx = secp256k1_context_create(SECP256K1_CONTEXT_NONE);
213236

@@ -247,5 +270,10 @@ int main(int argc, char** argv) {
247270
run_schnorrsig_bench(iters, argc, argv);
248271
#endif
249272

273+
#ifdef ENABLE_MODULE_ELLSWIFT
274+
/* ElligatorSwift benchmarks */
275+
run_ellswift_bench(iters, argc, argv);
276+
#endif
277+
250278
return 0;
251279
}
+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
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
34
noinst_HEADERS += src/modules/ellswift/tests_impl.h

src/modules/ellswift/bench_impl.h

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

0 commit comments

Comments
 (0)