10 #include "../include/secp256k1.h" 14 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");
42 #ifdef ENABLE_MODULE_RECOVERY 43 printf(
" ecdsa_recover : ECDSA public key recovery algorithm\n");
46 #ifdef ENABLE_MODULE_ECDH 47 printf(
" ecdh : ECDH key exchange algorithm\n");
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");
61 unsigned char msg[32];
62 unsigned char key[32];
63 unsigned char sig[72];
65 unsigned char pubkey[33];
73 for (i = 0; i < iters; i++) {
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);
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);
90 unsigned char msg[32];
91 unsigned char key[32];
98 for (i = 0; i < 32; i++) {
101 for (i = 0; i < 32; i++) {
102 data->
key[i] = i + 65;
110 unsigned char sig[74];
111 for (i = 0; i < iters; i++) {
117 for (j = 0; j < 32; j++) {
118 data->
msg[j] = sig[j];
119 data->
key[j] = sig[j + 32];
124 #ifdef ENABLE_MODULE_ECDH 128 #ifdef ENABLE_MODULE_RECOVERY 132 #ifdef ENABLE_MODULE_SCHNORRSIG 136 int main(
int argc,
char** argv) {
143 int default_iters = 20000;
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]);
158 }
else if (invalid_args) {
159 fprintf(stderr,
"./bench: unrecognized argument.\n\n");
166 #ifndef ENABLE_MODULE_ECDH 168 fprintf(stderr,
"./bench: ECDH module not enabled.\n");
169 fprintf(stderr,
"Use ./configure --enable-module-ecdh.\n\n");
174 #ifndef ENABLE_MODULE_RECOVERY 176 fprintf(stderr,
"./bench: Public key recovery module not enabled.\n");
177 fprintf(stderr,
"Use ./configure --enable-module-recovery.\n\n");
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");
193 for (i = 0; i < 32; i++) {
196 for (i = 0; i < 32; i++) {
197 data.
key[i] = 33 + i;
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);
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);
218 #ifdef ENABLE_MODULE_ECDH 223 #ifdef ENABLE_MODULE_RECOVERY 228 #ifdef ENABLE_MODULE_SCHNORRSIG 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.
void run_recovery_bench(int iters, int argc, char **argv)
void run_benchmark(char *name, void(*benchmark)(void *, int), void(*setup)(void *), void(*teardown)(void *, int), void *data, int count, int iter)
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.
void print_output_table_header_row(void)
#define SECP256K1_CONTEXT_SIGN
void run_ecdh_bench(int iters, int argc, char **argv)
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.
static void bench_verify(void *arg, int iters)
#define CHECK(cond)
Unconditional failure on condition failure.
static void bench_sign_setup(void *arg)
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.
Opaque data structured that holds a parsed ECDSA signature.
int have_invalid_args(int argc, char **argv, char **valid_args, size_t n)
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.
void help(int default_iters)
#define SECP256K1_CONTEXT_VERIFY
Flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size, and secp256k1_context...
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.
int get_iters(int default_iters)
static void bench_sign_run(void *arg, int iters)
int have_flag(int argc, char **argv, char *flag)
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object (in dynamically allocated memory).
void run_schnorrsig_bench(int iters, int argc, char **argv)
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.
Opaque data structure that holds a parsed and valid public key.