11#include "../include/secp256k1.h"
15static void help(
const char *executable_path,
int default_iters) {
16 printf(
"Benchmarks the following algorithms:\n");
17 printf(
" - ECDSA signing/verification\n");
19#ifdef ENABLE_MODULE_RECOVERY
20 printf(
" - Public key recovery (optional module)\n");
23#ifdef ENABLE_MODULE_ECDH
24 printf(
" - ECDH key exchange (optional module)\n");
27#ifdef ENABLE_MODULE_SCHNORRSIG
28 printf(
" - Schnorr signatures (optional module)\n");
31#ifdef ENABLE_MODULE_ELLSWIFT
32 printf(
" - ElligatorSwift (optional module)\n");
35#ifdef ENABLE_MODULE_SILENTPAYMENTS
36 printf(
" - Silent payments (optional module)\n");
40 printf(
"The default number of iterations for each benchmark is %d. This can be\n", default_iters);
41 printf(
"customized using the SECP256K1_BENCH_ITERS environment variable.\n");
43 printf(
"Usage: %s [args]\n", executable_path);
44 printf(
"By default, all benchmarks will be run.\n");
46 printf(
" help : display this help and exit\n");
47 printf(
" ecdsa : all ECDSA algorithms--sign, verify, recovery (if enabled)\n");
48 printf(
" ecdsa_sign : ECDSA siging algorithm\n");
49 printf(
" ecdsa_verify : ECDSA verification algorithm\n");
50 printf(
" ec : all EC public key algorithms (keygen)\n");
51 printf(
" ec_keygen : EC public key generation\n");
53#ifdef ENABLE_MODULE_RECOVERY
54 printf(
" ecdsa_recover : ECDSA public key recovery algorithm\n");
57#ifdef ENABLE_MODULE_ECDH
58 printf(
" ecdh : ECDH key exchange algorithm\n");
61#ifdef ENABLE_MODULE_SCHNORRSIG
62 printf(
" schnorrsig : all Schnorr signature algorithms (sign, verify)\n");
63 printf(
" schnorrsig_sign : Schnorr sigining algorithm\n");
64 printf(
" schnorrsig_verify : Schnorr verification algorithm\n");
67#ifdef ENABLE_MODULE_ELLSWIFT
68 printf(
" ellswift : all ElligatorSwift benchmarks (encode, decode, keygen, ecdh)\n");
69 printf(
" ellswift_encode : ElligatorSwift encoding\n");
70 printf(
" ellswift_decode : ElligatorSwift decoding\n");
71 printf(
" ellswift_keygen : ElligatorSwift key generation\n");
72 printf(
" ellswift_ecdh : ECDH on ElligatorSwift keys\n");
75#ifdef ENABLE_MODULE_SILENTPAYMENTS
76 printf(
" silentpayments : all Silent payments benchmarks (scan_nomatch, scan_worstcase)\n");
77 printf(
" silentpayments_scan_nomatch : Silent payments scanning common case (no match)\n");
78 printf(
" silentpayments_scan_worstcase : Silent payments scanning worst case (block-sized tx, all match)\n");
86 unsigned char msg[32];
87 unsigned char key[32];
88 unsigned char sig[72];
90 unsigned char pubkey[33];
98 for (i = 0; i < iters; i++) {
101 data->sig[
data->siglen - 1] ^= (i & 0xFF);
102 data->sig[
data->siglen - 2] ^= ((i >> 8) & 0xFF);
103 data->sig[
data->siglen - 3] ^= ((i >> 16) & 0xFF);
107 data->sig[
data->siglen - 1] ^= (i & 0xFF);
108 data->sig[
data->siglen - 2] ^= ((i >> 8) & 0xFF);
109 data->sig[
data->siglen - 3] ^= ((i >> 16) & 0xFF);
117 for (i = 0; i < 32; i++) {
118 data->msg[i] = i + 1;
120 for (i = 0; i < 32; i++) {
121 data->key[i] = i + 65;
129 unsigned char sig[74];
130 for (i = 0; i < iters; i++) {
136 for (j = 0; j < 32; j++) {
137 data->msg[j] = sig[j];
138 data->key[j] = sig[j + 32];
147 for (i = 0; i < 32; i++) {
148 data->key[i] = i + 65;
156 for (i = 0; i < iters; i++) {
157 unsigned char pub33[33];
162 memcpy(
data->key, pub33 + 1, 32);
167#ifdef ENABLE_MODULE_ECDH
171#ifdef ENABLE_MODULE_RECOVERY
175#ifdef ENABLE_MODULE_SCHNORRSIG
179#ifdef ENABLE_MODULE_ELLSWIFT
183#ifdef ENABLE_MODULE_SILENTPAYMENTS
187int main(
int argc,
char** argv) {
196 char* valid_args[] = {
"ecdsa",
"verify",
"ecdsa_verify",
"sign",
"ecdsa_sign",
"ecdh",
"recover",
197 "ecdsa_recover",
"schnorrsig",
"schnorrsig_verify",
"schnorrsig_sign",
"ec",
198 "keygen",
"ec_keygen",
"ellswift",
"encode",
"ellswift_encode",
"decode",
199 "ellswift_decode",
"ellswift_keygen",
"ellswift_ecdh",
"silentpayments",
200 "silentpayments_scan_nomatch",
"silentpayments_scan_worstcase"};
203 int default_iters = 20000;
206 help(argv[0], default_iters);
214 help(argv[0], default_iters);
216 }
else if (invalid_args) {
217 fprintf(stderr,
"./bench: unrecognized argument.\n\n");
218 help(argv[0], default_iters);
224#ifndef ENABLE_MODULE_ECDH
226 fprintf(stderr,
"./bench: ECDH module not enabled.\n");
227 fprintf(stderr,
"See README.md for configuration instructions.\n\n");
232#ifndef ENABLE_MODULE_RECOVERY
234 fprintf(stderr,
"./bench: Public key recovery module not enabled.\n");
235 fprintf(stderr,
"See README.md for configuration instructions.\n\n");
240#ifndef ENABLE_MODULE_SCHNORRSIG
241 if (
have_flag(argc, argv,
"schnorrsig") ||
have_flag(argc, argv,
"schnorrsig_sign") ||
have_flag(argc, argv,
"schnorrsig_verify")) {
242 fprintf(stderr,
"./bench: Schnorr signatures module not enabled.\n");
243 fprintf(stderr,
"See README.md for configuration instructions.\n\n");
248#ifndef ENABLE_MODULE_ELLSWIFT
251 have_flag(argc, argv,
"ellswift_ecdh")) {
252 fprintf(stderr,
"./bench: ElligatorSwift module not enabled.\n");
253 fprintf(stderr,
"See README.md for configuration instructions.\n\n");
258#ifndef ENABLE_MODULE_SILENTPAYMENTS
259 if (
have_flag(argc, argv,
"silentpayments") ||
have_flag(argc, argv,
"silentpayments_scan_nomatch") ||
260 have_flag(argc, argv,
"silentpayments_scan_worstcase")) {
261 fprintf(stderr,
"./bench: silentpayments module not enabled.\n");
262 fprintf(stderr,
"See README.md for configuration instructions.\n\n");
270 for (i = 0; i < 32; i++) {
273 for (i = 0; i < 32; i++) {
274 data.key[i] = 33 + i;
284 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);
286 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);
287 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);
291#ifdef ENABLE_MODULE_ECDH
296#ifdef ENABLE_MODULE_RECOVERY
301#ifdef ENABLE_MODULE_SCHNORRSIG
306#ifdef ENABLE_MODULE_ELLSWIFT
311#ifdef ENABLE_MODULE_SILENTPAYMENTS
static const PrecomputedData data
Precomputed COutPoint and CCoins values.
static void run_benchmark(char *name, void(*benchmark)(void *), void(*setup)(void *), void(*teardown)(void *), void *data, int count, int iter)
static void run_ecdh_bench(int iters, int argc, char **argv)
void run_ellswift_bench(int iters, int argc, char **argv)
#define CHECK(cond)
Unconditional failure on condition failure.
static void run_recovery_bench(int iters, int argc, char **argv)
static void run_schnorrsig_bench(int iters, int argc, char **argv)
static void bench_keygen_run(void *arg, int iters)
static void help(const char *executable_path, int default_iters)
static void bench_sign_setup(void *arg)
static void bench_sign_run(void *arg, int iters)
static void bench_keygen_setup(void *arg)
static void bench_verify(void *arg, int iters)
static int get_iters(int default_iters)
static void print_output_table_header_row(void)
static int have_invalid_args(int argc, char **argv, char **valid_args, size_t n)
static int have_flag(int argc, char **argv, char *flag)
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1)
Destroy a secp256k1 context object (created in dynamically allocated memory).
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.
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object (in dynamically allocated memory).
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.
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.
#define SECP256K1_CONTEXT_NONE
Context flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size,...
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.
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.
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
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.
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.
static void run_silentpayments_bench(int iters, int argc, char **argv)
Opaque data structure that holds a parsed ECDSA signature.
Opaque data structure that holds a parsed and valid public key.