10#include "../include/secp256k1.h"
14static void help(
int default_iters) {
15 printf(
"Benchmarks the following algorithms:\n");
16 printf(
" - ECDSA signing/verification\n");
18#ifdef ENABLE_MODULE_ECDH
19 printf(
" - ECDH key exchange (optional module)\n");
22#ifdef ENABLE_MODULE_RECOVERY
23 printf(
" - Public key recovery (optional module)\n");
26#ifdef ENABLE_MODULE_SCHNORRSIG
27 printf(
" - Schnorr signatures (optional module)\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");
34 printf(
"Usage: ./bench [args]\n");
35 printf(
"By default, all benchmarks will be run.\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 printf(
" ec : all EC public key algorithms (keygen)\n");
42 printf(
" ec_keygen : EC public key generation\n");
44#ifdef ENABLE_MODULE_RECOVERY
45 printf(
" ecdsa_recover : ECDSA public key recovery algorithm\n");
48#ifdef ENABLE_MODULE_ECDH
49 printf(
" ecdh : ECDH key exchange algorithm\n");
52#ifdef ENABLE_MODULE_SCHNORRSIG
53 printf(
" schnorrsig : all Schnorr signature algorithms (sign, verify)\n");
54 printf(
" schnorrsig_sign : Schnorr sigining algorithm\n");
55 printf(
" schnorrsig_verify : Schnorr verification algorithm\n");
58#ifdef ENABLE_MODULE_ELLSWIFT
59 printf(
" ellswift : all ElligatorSwift benchmarks (encode, decode, keygen, ecdh)\n");
60 printf(
" ellswift_encode : ElligatorSwift encoding\n");
61 printf(
" ellswift_decode : ElligatorSwift decoding\n");
62 printf(
" ellswift_keygen : ElligatorSwift key generation\n");
63 printf(
" ellswift_ecdh : ECDH on ElligatorSwift keys\n");
71 unsigned char msg[32];
72 unsigned char key[32];
73 unsigned char sig[72];
75 unsigned char pubkey[33];
83 for (i = 0; i < iters; i++) {
86 data->sig[
data->siglen - 1] ^= (i & 0xFF);
87 data->sig[
data->siglen - 2] ^= ((i >> 8) & 0xFF);
88 data->sig[
data->siglen - 3] ^= ((i >> 16) & 0xFF);
92 data->sig[
data->siglen - 1] ^= (i & 0xFF);
93 data->sig[
data->siglen - 2] ^= ((i >> 8) & 0xFF);
94 data->sig[
data->siglen - 3] ^= ((i >> 16) & 0xFF);
102 for (i = 0; i < 32; i++) {
103 data->msg[i] = i + 1;
105 for (i = 0; i < 32; i++) {
106 data->key[i] = i + 65;
114 unsigned char sig[74];
115 for (i = 0; i < iters; i++) {
121 for (j = 0; j < 32; j++) {
122 data->msg[j] = sig[j];
123 data->key[j] = sig[j + 32];
132 for (i = 0; i < 32; i++) {
133 data->key[i] = i + 65;
141 for (i = 0; i < iters; i++) {
142 unsigned char pub33[33];
147 memcpy(
data->key, pub33 + 1, 32);
152#ifdef ENABLE_MODULE_ECDH
156#ifdef ENABLE_MODULE_RECOVERY
160#ifdef ENABLE_MODULE_SCHNORRSIG
164#ifdef ENABLE_MODULE_ELLSWIFT
168int main(
int argc,
char** argv) {
175 int default_iters = 20000;
179 char* valid_args[] = {
"ecdsa",
"verify",
"ecdsa_verify",
"sign",
"ecdsa_sign",
"ecdh",
"recover",
180 "ecdsa_recover",
"schnorrsig",
"schnorrsig_verify",
"schnorrsig_sign",
"ec",
181 "keygen",
"ec_keygen",
"ellswift",
"encode",
"ellswift_encode",
"decode",
182 "ellswift_decode",
"ellswift_keygen",
"ellswift_ecdh"};
183 size_t valid_args_size =
sizeof(valid_args)/
sizeof(valid_args[0]);
192 }
else if (invalid_args) {
193 fprintf(stderr,
"./bench: unrecognized argument.\n\n");
200#ifndef ENABLE_MODULE_ECDH
202 fprintf(stderr,
"./bench: ECDH module not enabled.\n");
203 fprintf(stderr,
"Use ./configure --enable-module-ecdh.\n\n");
208#ifndef ENABLE_MODULE_RECOVERY
210 fprintf(stderr,
"./bench: Public key recovery module not enabled.\n");
211 fprintf(stderr,
"Use ./configure --enable-module-recovery.\n\n");
216#ifndef ENABLE_MODULE_SCHNORRSIG
217 if (
have_flag(argc, argv,
"schnorrsig") ||
have_flag(argc, argv,
"schnorrsig_sign") ||
have_flag(argc, argv,
"schnorrsig_verify")) {
218 fprintf(stderr,
"./bench: Schnorr signatures module not enabled.\n");
219 fprintf(stderr,
"Use ./configure --enable-module-schnorrsig.\n\n");
224#ifndef ENABLE_MODULE_ELLSWIFT
227 have_flag(argc, argv,
"ellswift_ecdh")) {
228 fprintf(stderr,
"./bench: ElligatorSwift module not enabled.\n");
229 fprintf(stderr,
"Use ./configure --enable-module-ellswift.\n\n");
237 for (i = 0; i < 32; i++) {
240 for (i = 0; i < 32; i++) {
241 data.key[i] = 33 + i;
251 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);
253 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);
254 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);
258#ifdef ENABLE_MODULE_ECDH
263#ifdef ENABLE_MODULE_RECOVERY
268#ifdef ENABLE_MODULE_SCHNORRSIG
273#ifdef ENABLE_MODULE_ELLSWIFT
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 help(int default_iters)
static void bench_keygen_run(void *arg, int 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.
Opaque data structure that holds a parsed ECDSA signature.
Opaque data structure that holds a parsed and valid public key.