Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
bench.c File Reference
#include <stdio.h>
#include <string.h>
#include "../include/secp256k1.h"
#include "util.h"
#include "bench.h"
Include dependency graph for bench.c:

Go to the source code of this file.

Classes

struct  bench_verify_data
 
struct  bench_sign_data
 

Functions

void help (int default_iters)
 
int main (int argc, char **argv)
 

Function Documentation

◆ help()

void help ( int default_iters)

Definition at line 14 of file bench.c.

14 {
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}
LOGGING_API void printf(Category category, const char *format,...)
Definition Logging.cpp:30
Here is the caller graph for this function:

◆ main()

int main ( int argc,
char ** argv )

Definition at line 136 of file bench.c.

136 {
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
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_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 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
CK_ULONG d
CK_BYTE_PTR pubkey
Here is the call graph for this function: