Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
bench.c
Go to the documentation of this file.
1/***********************************************************************
2 * Copyright (c) 2014 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#include <stdio.h>
8#include <string.h>
9
11#include "util.h"
12#include "bench.h"
13
14void help(int default_iters) {
15 printf("Benchmarks the following algorithms:\n");
16 printf(" - ECDSA signing/verification\n");
17
18#ifdef ENABLE_MODULE_ECDH
19 printf(" - ECDH key exchange (optional module)\n");
20#endif
21
22#ifdef ENABLE_MODULE_RECOVERY
23 printf(" - Public key recovery (optional module)\n");
24#endif
25
26#ifdef ENABLE_MODULE_SCHNORRSIG
27 printf(" - Schnorr signatures (optional module)\n");
28#endif
29
30 printf("\n");
31 printf("The default number of iterations for each benchmark is %d. This can be\n", default_iters);
32 printf("customized using the SECP256K1_BENCH_ITERS environment variable.\n");
33 printf("\n");
34 printf("Usage: ./bench [args]\n");
35 printf("By default, all benchmarks will be run.\n");
36 printf("args:\n");
37 printf(" help : display this help and exit\n");
38 printf(" ecdsa : all ECDSA algorithms--sign, verify, recovery (if enabled)\n");
39 printf(" ecdsa_sign : ECDSA siging algorithm\n");
40 printf(" ecdsa_verify : ECDSA verification algorithm\n");
41
42#ifdef ENABLE_MODULE_RECOVERY
43 printf(" ecdsa_recover : ECDSA public key recovery algorithm\n");
44#endif
45
46#ifdef ENABLE_MODULE_ECDH
47 printf(" ecdh : ECDH key exchange algorithm\n");
48#endif
49
50#ifdef ENABLE_MODULE_SCHNORRSIG
51 printf(" schnorrsig : all Schnorr signature algorithms (sign, verify)\n");
52 printf(" schnorrsig_sign : Schnorr sigining algorithm\n");
53 printf(" schnorrsig_verify : Schnorr verification algorithm\n");
54#endif
55
56 printf("\n");
57}
58
59typedef struct {
61 unsigned char msg[32];
62 unsigned char key[32];
63 unsigned char sig[72];
64 size_t siglen;
65 unsigned char pubkey[33];
66 size_t pubkeylen;
68
69static void bench_verify(void* arg, int iters) {
70 int i;
72
73 for (i = 0; i < iters; i++) {
76 data->sig[data->siglen - 1] ^= (i & 0xFF);
77 data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
78 data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
79 CHECK(secp256k1_ec_pubkey_parse(data->ctx, &pubkey, data->pubkey, data->pubkeylen) == 1);
80 CHECK(secp256k1_ecdsa_signature_parse_der(data->ctx, &sig, data->sig, data->siglen) == 1);
81 CHECK(secp256k1_ecdsa_verify(data->ctx, &sig, data->msg, &pubkey) == (i == 0));
82 data->sig[data->siglen - 1] ^= (i & 0xFF);
83 data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
84 data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
85 }
86}
87
88typedef struct {
90 unsigned char msg[32];
91 unsigned char key[32];
93
94static void bench_sign_setup(void* arg) {
95 int i;
96 bench_sign_data *data = (bench_sign_data*)arg;
97
98 for (i = 0; i < 32; i++) {
99 data->msg[i] = i + 1;
100 }
101 for (i = 0; i < 32; i++) {
102 data->key[i] = i + 65;
103 }
104}
105
106static void bench_sign_run(void* arg, int iters) {
107 int i;
108 bench_sign_data *data = (bench_sign_data*)arg;
109
110 unsigned char sig[74];
111 for (i = 0; i < iters; i++) {
112 size_t siglen = 74;
113 int j;
115 CHECK(secp256k1_ecdsa_sign(data->ctx, &signature, data->msg, data->key, NULL, NULL));
116 CHECK(secp256k1_ecdsa_signature_serialize_der(data->ctx, sig, &siglen, &signature));
117 for (j = 0; j < 32; j++) {
118 data->msg[j] = sig[j];
119 data->key[j] = sig[j + 32];
120 }
121 }
122}
123
124#ifdef ENABLE_MODULE_ECDH
126#endif
127
128#ifdef ENABLE_MODULE_RECOVERY
130#endif
131
132#ifdef ENABLE_MODULE_SCHNORRSIG
134#endif
135
136int main(int argc, char** argv) {
137 int i;
141
142 int d = argc == 1;
143 int default_iters = 20000;
144 int iters = get_iters(default_iters);
145
146 /* Check for invalid user arguments */
147 char* valid_args[] = {"ecdsa", "verify", "ecdsa_verify", "sign", "ecdsa_sign", "ecdh", "recover",
148 "ecdsa_recover", "schnorrsig", "schnorrsig_verify", "schnorrsig_sign"};
149 size_t valid_args_size = sizeof(valid_args)/sizeof(valid_args[0]);
150 int invalid_args = have_invalid_args(argc, argv, valid_args, valid_args_size);
151
152 if (argc > 1) {
153 if (have_flag(argc, argv, "-h")
154 || have_flag(argc, argv, "--help")
155 || have_flag(argc, argv, "help")) {
156 help(default_iters);
157 return 0;
158 } else if (invalid_args) {
159 fprintf(stderr, "./bench: unrecognized argument.\n\n");
160 help(default_iters);
161 return 1;
162 }
163 }
164
165/* Check if the user tries to benchmark optional module without building it */
166#ifndef ENABLE_MODULE_ECDH
167 if (have_flag(argc, argv, "ecdh")) {
168 fprintf(stderr, "./bench: ECDH module not enabled.\n");
169 fprintf(stderr, "Use ./configure --enable-module-ecdh.\n\n");
170 return 1;
171 }
172#endif
173
174#ifndef ENABLE_MODULE_RECOVERY
175 if (have_flag(argc, argv, "recover") || have_flag(argc, argv, "ecdsa_recover")) {
176 fprintf(stderr, "./bench: Public key recovery module not enabled.\n");
177 fprintf(stderr, "Use ./configure --enable-module-recovery.\n\n");
178 return 1;
179 }
180#endif
181
182#ifndef ENABLE_MODULE_SCHNORRSIG
183 if (have_flag(argc, argv, "schnorrsig") || have_flag(argc, argv, "schnorrsig_sign") || have_flag(argc, argv, "schnorrsig_verify")) {
184 fprintf(stderr, "./bench: Schnorr signatures module not enabled.\n");
185 fprintf(stderr, "Use ./configure --enable-module-schnorrsig.\n\n");
186 return 1;
187 }
188#endif
189
190 /* ECDSA verification benchmark */
192
193 for (i = 0; i < 32; i++) {
194 data.msg[i] = 1 + i;
195 }
196 for (i = 0; i < 32; i++) {
197 data.key[i] = 33 + i;
198 }
199 data.siglen = 72;
200 CHECK(secp256k1_ecdsa_sign(data.ctx, &sig, data.msg, data.key, NULL, NULL));
201 CHECK(secp256k1_ecdsa_signature_serialize_der(data.ctx, data.sig, &data.siglen, &sig));
202 CHECK(secp256k1_ec_pubkey_create(data.ctx, &pubkey, data.key));
203 data.pubkeylen = 33;
204 CHECK(secp256k1_ec_pubkey_serialize(data.ctx, data.pubkey, &data.pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
205
207 if (d || have_flag(argc, argv, "ecdsa") || have_flag(argc, argv, "verify") || have_flag(argc, argv, "ecdsa_verify")) run_benchmark("ecdsa_verify", bench_verify, NULL, NULL, &data, 10, iters);
208
210
211 /* ECDSA signing benchmark */
213
214 if (d || have_flag(argc, argv, "ecdsa") || have_flag(argc, argv, "sign") || have_flag(argc, argv, "ecdsa_sign")) run_benchmark("ecdsa_sign", bench_sign_run, bench_sign_setup, NULL, &data, 10, iters);
215
217
218#ifdef ENABLE_MODULE_ECDH
219 /* ECDH benchmarks */
220 run_ecdh_bench(iters, argc, argv);
221#endif
222
223#ifdef ENABLE_MODULE_RECOVERY
224 /* ECDSA recovery benchmarks */
225 run_recovery_bench(iters, argc, argv);
226#endif
227
228#ifdef ENABLE_MODULE_SCHNORRSIG
229 /* Schnorr signature benchmarks */
230 run_schnorrsig_bench(iters, argc, argv);
231#endif
232
233 return 0;
234}
void help(int default_iters)
Definition bench.c:14
void print_output_table_header_row(void)
Definition bench.h:163
int have_flag(int argc, char **argv, char *flag)
Definition bench.h:116
int get_iters(int default_iters)
Definition bench.h:154
int have_invalid_args(int argc, char **argv, char **valid_args, size_t n)
Definition bench.h:132
void run_benchmark(char *name, void(*benchmark)(void *, int), void(*setup)(void *), void(*teardown)(void *, int), void *data, int count, int iter)
Definition bench.h:82
void run_ecdh_bench(int iters, int argc, char **argv)
Definition bench_impl.h:45
#define CHECK(cond)
Definition util.h:80
char ** argv
fc::array< char, 72 > signature
Definition elliptic.hpp:25
void run_recovery_bench(int iters, int argc, char **argv)
Definition bench_impl.h:51
void run_schnorrsig_bench(int iters, int argc, char **argv)
Definition bench_impl.h:48
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1)
Definition secp256k1.c:146
#define SECP256K1_CONTEXT_SIGN
Definition secp256k1.h:196
SECP256K1_API int secp256k1_ec_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey *pubkey, unsigned int flags) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Definition secp256k1.c:246
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Definition secp256k1.c:107
SECP256K1_API int secp256k1_ecdsa_sign(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Definition secp256k1.c:514
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *input, size_t inputlen) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Definition secp256k1.c:228
SECP256K1_API int secp256k1_ecdsa_signature_parse_der(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *input, size_t inputlen) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Definition secp256k1.c:319
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Definition secp256k1.c:551
#define SECP256K1_EC_COMPRESSED
Definition secp256k1.h:201
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(const secp256k1_context *ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const secp256k1_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Definition secp256k1.c:400
SECP256K1_API int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Definition secp256k1.c:356
#define SECP256K1_CONTEXT_VERIFY
Definition secp256k1.h:195
secp256k1_context * ctx
Definition bench.c:89
size_t pubkeylen
Definition bench.c:66
secp256k1_context * ctx
Definition bench.c:60
size_t siglen
Definition bench.c:64
CK_ULONG d
uint16_t j
CK_BYTE_PTR pubkey
CK_ULONG siglen