Bitcoin Core  22.99.0
P2P Digital Currency
bench_impl.h
Go to the documentation of this file.
1 /***********************************************************************
2  * Copyright (c) 2018-2020 Andrew Poelstra, Jonas Nick *
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 #ifndef SECP256K1_MODULE_SCHNORRSIG_BENCH_H
8 #define SECP256K1_MODULE_SCHNORRSIG_BENCH_H
9 
10 #include "../../../include/secp256k1_schnorrsig.h"
11 
12 #define MSGLEN 32
13 
14 typedef struct {
16  int n;
17 
19  const unsigned char **pk;
20  const unsigned char **sigs;
21  const unsigned char **msgs;
23 
24 void bench_schnorrsig_sign(void* arg, int iters) {
26  int i;
27  unsigned char msg[MSGLEN] = {0};
28  unsigned char sig[64];
29 
30  for (i = 0; i < iters; i++) {
31  msg[0] = i;
32  msg[1] = i >> 8;
33  CHECK(secp256k1_schnorrsig_sign_custom(data->ctx, sig, msg, MSGLEN, data->keypairs[i], NULL));
34  }
35 }
36 
37 void bench_schnorrsig_verify(void* arg, int iters) {
39  int i;
40 
41  for (i = 0; i < iters; i++) {
43  CHECK(secp256k1_xonly_pubkey_parse(data->ctx, &pk, data->pk[i]) == 1);
44  CHECK(secp256k1_schnorrsig_verify(data->ctx, data->sigs[i], data->msgs[i], MSGLEN, &pk));
45  }
46 }
47 
48 void run_schnorrsig_bench(int iters, int argc, char** argv) {
49  int i;
51  int d = argc == 1;
52 
54  data.keypairs = (const secp256k1_keypair **)malloc(iters * sizeof(secp256k1_keypair *));
55  data.pk = (const unsigned char **)malloc(iters * sizeof(unsigned char *));
56  data.msgs = (const unsigned char **)malloc(iters * sizeof(unsigned char *));
57  data.sigs = (const unsigned char **)malloc(iters * sizeof(unsigned char *));
58 
59  CHECK(MSGLEN >= 4);
60  for (i = 0; i < iters; i++) {
61  unsigned char sk[32];
62  unsigned char *msg = (unsigned char *)malloc(MSGLEN);
63  unsigned char *sig = (unsigned char *)malloc(64);
64  secp256k1_keypair *keypair = (secp256k1_keypair *)malloc(sizeof(*keypair));
65  unsigned char *pk_char = (unsigned char *)malloc(32);
67  msg[0] = sk[0] = i;
68  msg[1] = sk[1] = i >> 8;
69  msg[2] = sk[2] = i >> 16;
70  msg[3] = sk[3] = i >> 24;
71  memset(&msg[4], 'm', MSGLEN - 4);
72  memset(&sk[4], 's', 28);
73 
74  data.keypairs[i] = keypair;
75  data.pk[i] = pk_char;
76  data.msgs[i] = msg;
77  data.sigs[i] = sig;
78 
79  CHECK(secp256k1_keypair_create(data.ctx, keypair, sk));
80  CHECK(secp256k1_schnorrsig_sign_custom(data.ctx, sig, msg, MSGLEN, keypair, NULL));
81  CHECK(secp256k1_keypair_xonly_pub(data.ctx, &pk, NULL, keypair));
82  CHECK(secp256k1_xonly_pubkey_serialize(data.ctx, pk_char, &pk) == 1);
83  }
84 
85  if (d || have_flag(argc, argv, "schnorrsig") || have_flag(argc, argv, "sign") || have_flag(argc, argv, "schnorrsig_sign")) run_benchmark("schnorrsig_sign", bench_schnorrsig_sign, NULL, NULL, (void *) &data, 10, iters);
86  if (d || have_flag(argc, argv, "schnorrsig") || have_flag(argc, argv, "verify") || have_flag(argc, argv, "schnorrsig_verify")) run_benchmark("schnorrsig_verify", bench_schnorrsig_verify, NULL, NULL, (void *) &data, 10, iters);
87 
88  for (i = 0; i < iters; i++) {
89  free((void *)data.keypairs[i]);
90  free((void *)data.pk[i]);
91  free((void *)data.msgs[i]);
92  free((void *)data.sigs[i]);
93  }
94  free(data.keypairs);
95  free(data.pk);
96  free(data.msgs);
97  free(data.sigs);
98 
99  secp256k1_context_destroy(data.ctx);
100 }
101 
102 #endif
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
SECP256K1_CONTEXT_SIGN
#define SECP256K1_CONTEXT_SIGN
Definition: secp256k1.h:185
secp256k1_context_struct
Definition: secp256k1.c:47
bench_schnorrsig_data::pk
const unsigned char ** pk
Definition: bench_impl.h:19
bench_schnorrsig_data::keypairs
const secp256k1_keypair ** keypairs
Definition: bench_impl.h:18
secp256k1_schnorrsig_sign_custom
SECP256K1_API int secp256k1_schnorrsig_sign_custom(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msg, size_t msglen, const secp256k1_keypair *keypair, secp256k1_schnorrsig_extraparams *extraparams) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(5)
Create a Schnorr signature with a more flexible API.
Definition: main_impl.h:200
secp256k1_keypair_xonly_pub
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_xonly_pub(const secp256k1_context *ctx, secp256k1_xonly_pubkey *pubkey, int *pk_parity, const secp256k1_keypair *keypair) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4)
Get the x-only public key from a keypair.
Definition: main_impl.h:233
bench_schnorrsig_sign
void bench_schnorrsig_sign(void *arg, int iters)
Definition: bench_impl.h:24
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
secp256k1_schnorrsig_verify
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_schnorrsig_verify(const secp256k1_context *ctx, const unsigned char *sig64, const unsigned char *msg, size_t msglen, const secp256k1_xonly_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(5)
Verify a Schnorr signature.
Definition: main_impl.h:215
bench_schnorrsig_data::sigs
const unsigned char ** sigs
Definition: bench_impl.h:20
secp256k1_keypair
Opaque data structure that holds a keypair consisting of a secret and a public key.
Definition: secp256k1_extrakeys.h:33
bench_schnorrsig_data::ctx
secp256k1_context * ctx
Definition: bench_impl.h:15
secp256k1_keypair_create
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_create(const secp256k1_context *ctx, secp256k1_keypair *keypair, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute the keypair for a secret key.
Definition: main_impl.h:195
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
bench_schnorrsig_verify
void bench_schnorrsig_verify(void *arg, int iters)
Definition: bench_impl.h:37
bench_schnorrsig_data
Definition: bench_impl.h:14
CHECK
#define CHECK(cond)
Unconditional failure on condition failure.
Definition: util.h:35
bench_schnorrsig_data::msgs
const unsigned char ** msgs
Definition: bench_impl.h:21
secp256k1_xonly_pubkey_parse
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_parse(const secp256k1_context *ctx, secp256k1_xonly_pubkey *pubkey, const unsigned char *input32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse a 32-byte sequence into a xonly_pubkey object.
Definition: main_impl.h:21
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
MSGLEN
#define MSGLEN
Definition: bench_impl.h:12
secp256k1_xonly_pubkey_serialize
SECP256K1_API int secp256k1_xonly_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output32, const secp256k1_xonly_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Serialize an xonly_pubkey object into a 32-byte sequence.
Definition: main_impl.h:43
bench_schnorrsig_data::n
int n
Definition: bench_impl.h:16
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
secp256k1_xonly_pubkey
Opaque data structure that holds a parsed and valid "x-only" public key.
Definition: secp256k1_extrakeys.h:22