Bitcoin Core  31.0.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 <stdlib.h>
9 #include <string.h>
10 
11 #include "../include/secp256k1.h"
12 #include "util.h"
13 #include "bench.h"
14 
15 static void help(int default_iters) {
16  printf("Benchmarks the following algorithms:\n");
17  printf(" - ECDSA signing/verification\n");
18 
19 #ifdef ENABLE_MODULE_RECOVERY
20  printf(" - Public key recovery (optional module)\n");
21 #endif
22 
23 #ifdef ENABLE_MODULE_ECDH
24  printf(" - ECDH key exchange (optional module)\n");
25 #endif
26 
27 #ifdef ENABLE_MODULE_SCHNORRSIG
28  printf(" - Schnorr signatures (optional module)\n");
29 #endif
30 
31 #ifdef ENABLE_MODULE_ELLSWIFT
32  printf(" - ElligatorSwift (optional module)\n");
33 #endif
34 
35  printf("\n");
36  printf("The default number of iterations for each benchmark is %d. This can be\n", default_iters);
37  printf("customized using the SECP256K1_BENCH_ITERS environment variable.\n");
38  printf("\n");
39  printf("Usage: ./bench [args]\n");
40  printf("By default, all benchmarks will be run.\n");
41  printf("args:\n");
42  printf(" help : display this help and exit\n");
43  printf(" ecdsa : all ECDSA algorithms--sign, verify, recovery (if enabled)\n");
44  printf(" ecdsa_sign : ECDSA siging algorithm\n");
45  printf(" ecdsa_verify : ECDSA verification algorithm\n");
46  printf(" ec : all EC public key algorithms (keygen)\n");
47  printf(" ec_keygen : EC public key generation\n");
48 
49 #ifdef ENABLE_MODULE_RECOVERY
50  printf(" ecdsa_recover : ECDSA public key recovery algorithm\n");
51 #endif
52 
53 #ifdef ENABLE_MODULE_ECDH
54  printf(" ecdh : ECDH key exchange algorithm\n");
55 #endif
56 
57 #ifdef ENABLE_MODULE_SCHNORRSIG
58  printf(" schnorrsig : all Schnorr signature algorithms (sign, verify)\n");
59  printf(" schnorrsig_sign : Schnorr sigining algorithm\n");
60  printf(" schnorrsig_verify : Schnorr verification algorithm\n");
61 #endif
62 
63 #ifdef ENABLE_MODULE_ELLSWIFT
64  printf(" ellswift : all ElligatorSwift benchmarks (encode, decode, keygen, ecdh)\n");
65  printf(" ellswift_encode : ElligatorSwift encoding\n");
66  printf(" ellswift_decode : ElligatorSwift decoding\n");
67  printf(" ellswift_keygen : ElligatorSwift key generation\n");
68  printf(" ellswift_ecdh : ECDH on ElligatorSwift keys\n");
69 #endif
70 
71  printf("\n");
72 }
73 
74 typedef struct {
76  unsigned char msg[32];
77  unsigned char key[32];
78  unsigned char sig[72];
79  size_t siglen;
80  unsigned char pubkey[33];
81  size_t pubkeylen;
82 } bench_data;
83 
84 static void bench_verify(void* arg, int iters) {
85  int i;
86  bench_data* data = (bench_data*)arg;
87 
88  for (i = 0; i < iters; i++) {
89  secp256k1_pubkey pubkey;
91  data->sig[data->siglen - 1] ^= (i & 0xFF);
92  data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
93  data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
94  CHECK(secp256k1_ec_pubkey_parse(data->ctx, &pubkey, data->pubkey, data->pubkeylen) == 1);
95  CHECK(secp256k1_ecdsa_signature_parse_der(data->ctx, &sig, data->sig, data->siglen) == 1);
96  CHECK(secp256k1_ecdsa_verify(data->ctx, &sig, data->msg, &pubkey) == (i == 0));
97  data->sig[data->siglen - 1] ^= (i & 0xFF);
98  data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
99  data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
100  }
101 }
102 
103 static void bench_sign_setup(void* arg) {
104  int i;
105  bench_data *data = (bench_data*)arg;
106 
107  for (i = 0; i < 32; i++) {
108  data->msg[i] = i + 1;
109  }
110  for (i = 0; i < 32; i++) {
111  data->key[i] = i + 65;
112  }
113 }
114 
115 static void bench_sign_run(void* arg, int iters) {
116  int i;
117  bench_data *data = (bench_data*)arg;
118 
119  unsigned char sig[74];
120  for (i = 0; i < iters; i++) {
121  size_t siglen = 74;
122  int j;
123  secp256k1_ecdsa_signature signature;
124  CHECK(secp256k1_ecdsa_sign(data->ctx, &signature, data->msg, data->key, NULL, NULL));
125  CHECK(secp256k1_ecdsa_signature_serialize_der(data->ctx, sig, &siglen, &signature));
126  for (j = 0; j < 32; j++) {
127  data->msg[j] = sig[j];
128  data->key[j] = sig[j + 32];
129  }
130  }
131 }
132 
133 static void bench_keygen_setup(void* arg) {
134  int i;
135  bench_data *data = (bench_data*)arg;
136 
137  for (i = 0; i < 32; i++) {
138  data->key[i] = i + 65;
139  }
140 }
141 
142 static void bench_keygen_run(void *arg, int iters) {
143  int i;
144  bench_data *data = (bench_data*)arg;
145 
146  for (i = 0; i < iters; i++) {
147  unsigned char pub33[33];
148  size_t len = 33;
149  secp256k1_pubkey pubkey;
150  CHECK(secp256k1_ec_pubkey_create(data->ctx, &pubkey, data->key));
151  CHECK(secp256k1_ec_pubkey_serialize(data->ctx, pub33, &len, &pubkey, SECP256K1_EC_COMPRESSED));
152  memcpy(data->key, pub33 + 1, 32);
153  }
154 }
155 
156 
157 #ifdef ENABLE_MODULE_ECDH
158 # include "modules/ecdh/bench_impl.h"
159 #endif
160 
161 #ifdef ENABLE_MODULE_RECOVERY
163 #endif
164 
165 #ifdef ENABLE_MODULE_SCHNORRSIG
167 #endif
168 
169 #ifdef ENABLE_MODULE_ELLSWIFT
171 #endif
172 
173 int main(int argc, char** argv) {
174  int i;
175  secp256k1_pubkey pubkey;
178 
179  int d = argc == 1;
180 
181  /* Check for invalid user arguments */
182  char* valid_args[] = {"ecdsa", "verify", "ecdsa_verify", "sign", "ecdsa_sign", "ecdh", "recover",
183  "ecdsa_recover", "schnorrsig", "schnorrsig_verify", "schnorrsig_sign", "ec",
184  "keygen", "ec_keygen", "ellswift", "encode", "ellswift_encode", "decode",
185  "ellswift_decode", "ellswift_keygen", "ellswift_ecdh"};
186  size_t valid_args_size = sizeof(valid_args)/sizeof(valid_args[0]);
187  int invalid_args = have_invalid_args(argc, argv, valid_args, valid_args_size);
188 
189  int default_iters = 20000;
190  int iters = get_iters(default_iters);
191  if (iters == 0) {
192  help(default_iters);
193  return EXIT_FAILURE;
194  }
195 
196  if (argc > 1) {
197  if (have_flag(argc, argv, "-h")
198  || have_flag(argc, argv, "--help")
199  || have_flag(argc, argv, "help")) {
200  help(default_iters);
201  return EXIT_SUCCESS;
202  } else if (invalid_args) {
203  fprintf(stderr, "./bench: unrecognized argument.\n\n");
204  help(default_iters);
205  return EXIT_FAILURE;
206  }
207  }
208 
209 /* Check if the user tries to benchmark optional module without building it */
210 #ifndef ENABLE_MODULE_ECDH
211  if (have_flag(argc, argv, "ecdh")) {
212  fprintf(stderr, "./bench: ECDH module not enabled.\n");
213  fprintf(stderr, "See README.md for configuration instructions.\n\n");
214  return EXIT_FAILURE;
215  }
216 #endif
217 
218 #ifndef ENABLE_MODULE_RECOVERY
219  if (have_flag(argc, argv, "recover") || have_flag(argc, argv, "ecdsa_recover")) {
220  fprintf(stderr, "./bench: Public key recovery module not enabled.\n");
221  fprintf(stderr, "See README.md for configuration instructions.\n\n");
222  return EXIT_FAILURE;
223  }
224 #endif
225 
226 #ifndef ENABLE_MODULE_SCHNORRSIG
227  if (have_flag(argc, argv, "schnorrsig") || have_flag(argc, argv, "schnorrsig_sign") || have_flag(argc, argv, "schnorrsig_verify")) {
228  fprintf(stderr, "./bench: Schnorr signatures module not enabled.\n");
229  fprintf(stderr, "See README.md for configuration instructions.\n\n");
230  return EXIT_FAILURE;
231  }
232 #endif
233 
234 #ifndef ENABLE_MODULE_ELLSWIFT
235  if (have_flag(argc, argv, "ellswift") || have_flag(argc, argv, "ellswift_encode") || have_flag(argc, argv, "ellswift_decode") ||
236  have_flag(argc, argv, "encode") || have_flag(argc, argv, "decode") || have_flag(argc, argv, "ellswift_keygen") ||
237  have_flag(argc, argv, "ellswift_ecdh")) {
238  fprintf(stderr, "./bench: ElligatorSwift module not enabled.\n");
239  fprintf(stderr, "See README.md for configuration instructions.\n\n");
240  return EXIT_FAILURE;
241  }
242 #endif
243 
244  /* ECDSA benchmark */
246 
247  for (i = 0; i < 32; i++) {
248  data.msg[i] = 1 + i;
249  }
250  for (i = 0; i < 32; i++) {
251  data.key[i] = 33 + i;
252  }
253  data.siglen = 72;
254  CHECK(secp256k1_ecdsa_sign(data.ctx, &sig, data.msg, data.key, NULL, NULL));
255  CHECK(secp256k1_ecdsa_signature_serialize_der(data.ctx, data.sig, &data.siglen, &sig));
256  CHECK(secp256k1_ec_pubkey_create(data.ctx, &pubkey, data.key));
257  data.pubkeylen = 33;
258  CHECK(secp256k1_ec_pubkey_serialize(data.ctx, data.pubkey, &data.pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
259 
261  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);
262 
263  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);
264  if (d || have_flag(argc, argv, "ec") || have_flag(argc, argv, "keygen") || have_flag(argc, argv, "ec_keygen")) run_benchmark("ec_keygen", bench_keygen_run, bench_keygen_setup, NULL, &data, 10, iters);
265 
267 
268 #ifdef ENABLE_MODULE_ECDH
269  /* ECDH benchmarks */
270  run_ecdh_bench(iters, argc, argv);
271 #endif
272 
273 #ifdef ENABLE_MODULE_RECOVERY
274  /* ECDSA recovery benchmarks */
275  run_recovery_bench(iters, argc, argv);
276 #endif
277 
278 #ifdef ENABLE_MODULE_SCHNORRSIG
279  /* Schnorr signature benchmarks */
280  run_schnorrsig_bench(iters, argc, argv);
281 #endif
282 
283 #ifdef ENABLE_MODULE_ELLSWIFT
284  /* ElligatorSwift benchmarks */
285  run_ellswift_bench(iters, argc, argv);
286 #endif
287 
288  return EXIT_SUCCESS;
289 }
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:574
return EXIT_SUCCESS
#define SECP256K1_CONTEXT_NONE
Context flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size, and secp256k1_context_preallocated_create.
Definition: secp256k1.h:214
static void run_benchmark(char *name, void(*benchmark)(void *), void(*setup)(void *), void(*teardown)(void *), void *data, int count, int iter)
Definition: bench.c:26
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:187
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:268
memcpy(result.begin(), stream.data(), stream.size())
static int have_invalid_args(int argc, char **argv, char **valid_args, size_t n)
Definition: bench.h:128
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:612
static void bench_keygen_setup(void *arg)
Definition: bench.c:133
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
Definition: secp256k1.h:224
static void bench_verify(void *arg, int iters)
Definition: bench.c:84
size_t pubkeylen
Definition: bench.c:81
void run_ellswift_bench(int iters, int argc, char **argv)
Definition: bench_impl.h:91
int main(void)
Definition: bench.c:156
static int get_iters(int default_iters)
Definition: bench.h:150
static int have_flag(int argc, char **argv, char *flag)
Definition: bench.h:112
static void run_schnorrsig_bench(int iters, int argc, char **argv)
Definition: bench_impl.h:48
#define CHECK(cond)
Unconditional failure on condition failure.
Definition: util.h:35
static void bench_sign_setup(void *arg)
Definition: bench.c:103
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:250
Opaque data structure that holds a parsed ECDSA signature.
Definition: secp256k1.h:74
secp256k1_context * ctx
Definition: bench.c:75
static void run_recovery_bench(int iters, int argc, char **argv)
Definition: bench_impl.h:51
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:377
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:414
static void help(int default_iters)
Definition: bench.c:15
static void bench_keygen_run(void *arg, int iters)
Definition: bench.c:142
static void run_ecdh_bench(int iters, int argc, char **argv)
Definition: bench_impl.h:45
static void print_output_table_header_row(void)
Definition: bench.h:165
size_t siglen
Definition: bench.c:79
void printf(FormatStringCheck< sizeof...(Args)> fmt, const Args &... args)
Format list of arguments to std::cout, according to the given format string.
Definition: tinyformat.h:1096
static void bench_sign_run(void *arg, int iters)
Definition: bench.c:115
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:141
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:458
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:61