Bitcoin Core  22.99.0
P2P Digital Currency
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 
10 #include "../include/secp256k1.h"
11 #include "util.h"
12 #include "bench.h"
13 
14 void 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 
59 typedef 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 
69 static void bench_verify(void* arg, int iters) {
70  int i;
72 
73  for (i = 0; i < iters; i++) {
74  secp256k1_pubkey pubkey;
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 
88 typedef struct {
90  unsigned char msg[32];
91  unsigned char key[32];
93 
94 static 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 
106 static 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;
114  secp256k1_ecdsa_signature signature;
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
125 # include "modules/ecdh/bench_impl.h"
126 #endif
127 
128 #ifdef ENABLE_MODULE_RECOVERY
130 #endif
131 
132 #ifdef ENABLE_MODULE_SCHNORRSIG
134 #endif
135 
136 int main(int argc, char** argv) {
137  int i;
138  secp256k1_pubkey pubkey;
140  bench_verify_data data;
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;
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 }
bench_verify_data
Definition: bench.c:59
secp256k1_ecdsa_signature
Opaque data structured that holds a parsed ECDSA signature.
Definition: secp256k1.h:83
secp256k1_ecdsa_signature_parse_der
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)
Parse a DER ECDSA signature.
Definition: secp256k1.c:319
bench_verify_data::msg
unsigned char msg[32]
Definition: bench.c:61
SECP256K1_CONTEXT_VERIFY
#define SECP256K1_CONTEXT_VERIFY
Flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size, and secp256k1_context...
Definition: secp256k1.h:184
bench_verify_data::pubkey
unsigned char pubkey[33]
Definition: bench.c:65
SECP256K1_CONTEXT_SIGN
#define SECP256K1_CONTEXT_SIGN
Definition: secp256k1.h:185
secp256k1_ecdsa_signature_serialize_der
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)
Serialize an ECDSA signature in DER format.
Definition: secp256k1.c:356
bench_sign_data::ctx
secp256k1_context * ctx
Definition: bench.c:89
secp256k1_context_struct
Definition: secp256k1.c:47
string.h
bench_sign_data::key
unsigned char key[32]
Definition: bench.c:91
tinyformat::printf
void printf(const char *fmt, const Args &... args)
Format list of arguments to std::cout, according to the given format string.
Definition: tinyformat.h:1079
bench_verify_data::siglen
size_t siglen
Definition: bench.c:64
bench_sign_setup
static void bench_sign_setup(void *arg)
Definition: bench.c:94
bench_impl.h
util.h
bench.h
secp256k1_context_create
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object (in dynamically allocated memory).
Definition: secp256k1.c:107
bench_sign_run
static void bench_sign_run(void *arg, int iters)
Definition: bench.c:106
secp256k1_ecdsa_verify
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)
Verify an ECDSA signature.
Definition: secp256k1.c:400
bench_impl.h
secp256k1_ec_pubkey_serialize
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)
Serialize a pubkey object into a serialized byte sequence.
Definition: secp256k1.c:246
SECP256K1_EC_COMPRESSED
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
Definition: secp256k1.h:190
bench_verify_data::pubkeylen
size_t pubkeylen
Definition: bench.c:66
secp256k1_ec_pubkey_parse
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)
Parse a variable-length public key into the pubkey object.
Definition: secp256k1.c:228
run_benchmark
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
have_invalid_args
int have_invalid_args(int argc, char **argv, char **valid_args, size_t n)
Definition: bench.h:132
main
int main(void)
Definition: bench.c:156
print_output_table_header_row
void print_output_table_header_row(void)
Definition: bench.h:163
bench_verify
static void bench_verify(void *arg, int iters)
Definition: bench.c:69
bench_sign_data::msg
unsigned char msg[32]
Definition: bench.c:90
bench_verify_data::key
unsigned char key[32]
Definition: bench.c:62
secp256k1_ecdsa_sign
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)
Create an ECDSA signature.
Definition: secp256k1.c:510
CHECK
#define CHECK(cond)
Unconditional failure on condition failure.
Definition: util.h:35
bench_verify_data::sig
unsigned char sig[72]
Definition: bench.c:63
get_iters
int get_iters(int default_iters)
Definition: bench.h:154
secp256k1_context_destroy
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition: secp256k1.c:146
secp256k1_ec_pubkey_create
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)
Compute the public key for a secret key.
Definition: secp256k1.c:547
bench_verify_data::ctx
secp256k1_context * ctx
Definition: bench.c:60
run_recovery_bench
void run_recovery_bench(int iters, int argc, char **argv)
Definition: bench_impl.h:51
bench_sign_data
Definition: bench.c:88
bench_impl.h
secp256k1_pubkey
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:70
run_ecdh_bench
void run_ecdh_bench(int iters, int argc, char **argv)
Definition: bench_impl.h:45
run_schnorrsig_bench
void run_schnorrsig_bench(int iters, int argc, char **argv)
Definition: bench_impl.h:48
have_flag
int have_flag(int argc, char **argv, char *flag)
Definition: bench.h:116
help
void help(int default_iters)
Definition: bench.c:14