Bitcoin ABC  0.26.3
P2P Digital Currency
bench_schnorrsig.c
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 #include <string.h>
8 #include <stdlib.h>
9 
10 
11 #include "include/secp256k1.h"
13 #include "util.h"
14 #include "bench.h"
15 
16 typedef struct {
18  int n;
19 
21  const unsigned char **pk;
22  const unsigned char **sigs;
23  const unsigned char **msgs;
25 
26 void bench_schnorrsig_sign(void* arg, int iters) {
28  int i;
29  unsigned char msg[32] = "benchmarkexamplemessagetemplate";
30  unsigned char sig[64];
31 
32  for (i = 0; i < iters; i++) {
33  msg[0] = i;
34  msg[1] = i >> 8;
35  CHECK(secp256k1_schnorrsig_sign(data->ctx, sig, msg, data->keypairs[i], NULL, NULL));
36  }
37 }
38 
39 void bench_schnorrsig_verify(void* arg, int iters) {
41  int i;
42 
43  for (i = 0; i < iters; i++) {
45  CHECK(secp256k1_xonly_pubkey_parse(data->ctx, &pk, data->pk[i]) == 1);
46  CHECK(secp256k1_schnorrsig_verify(data->ctx, data->sigs[i], data->msgs[i], &pk));
47  }
48 }
49 
50 int main(void) {
51  int i;
53  int iters = get_iters(10000);
54 
56  data.keypairs = (const secp256k1_keypair **)malloc(iters * sizeof(secp256k1_keypair *));
57  data.pk = (const unsigned char **)malloc(iters * sizeof(unsigned char *));
58  data.msgs = (const unsigned char **)malloc(iters * sizeof(unsigned char *));
59  data.sigs = (const unsigned char **)malloc(iters * sizeof(unsigned char *));
60 
61  for (i = 0; i < iters; i++) {
62  unsigned char sk[32];
63  unsigned char *msg = (unsigned char *)malloc(32);
64  unsigned char *sig = (unsigned char *)malloc(64);
65  secp256k1_keypair *keypair = (secp256k1_keypair *)malloc(sizeof(*keypair));
66  unsigned char *pk_char = (unsigned char *)malloc(32);
68  msg[0] = sk[0] = i;
69  msg[1] = sk[1] = i >> 8;
70  msg[2] = sk[2] = i >> 16;
71  msg[3] = sk[3] = i >> 24;
72  memset(&msg[4], 'm', 28);
73  memset(&sk[4], 's', 28);
74 
75  data.keypairs[i] = keypair;
76  data.pk[i] = pk_char;
77  data.msgs[i] = msg;
78  data.sigs[i] = sig;
79 
80  CHECK(secp256k1_keypair_create(data.ctx, keypair, sk));
81  CHECK(secp256k1_schnorrsig_sign(data.ctx, sig, msg, keypair, NULL, NULL));
82  CHECK(secp256k1_keypair_xonly_pub(data.ctx, &pk, NULL, keypair));
83  CHECK(secp256k1_xonly_pubkey_serialize(data.ctx, pk_char, &pk) == 1);
84  }
85 
86  run_benchmark("schnorrsig_sign", bench_schnorrsig_sign, NULL, NULL, (void *) &data, 10, iters);
87  run_benchmark("schnorrsig_verify", bench_schnorrsig_verify, NULL, NULL, (void *) &data, 10, iters);
88 
89  for (i = 0; i < iters; i++) {
90  free((void *)data.keypairs[i]);
91  free((void *)data.pk[i]);
92  free((void *)data.msgs[i]);
93  free((void *)data.sigs[i]);
94  }
95  free(data.keypairs);
96  free(data.pk);
97  free(data.msgs);
98  free(data.sigs);
99 
101  return 0;
102 }
void bench_schnorrsig_sign(void *arg, int iters)
int main(void)
void bench_schnorrsig_verify(void *arg, int iters)
SchnorrSig sig
Definition: processor.cpp:491
int get_iters(int default_iters)
Definition: bench.h:124
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:76
#define CHECK(cond)
Definition: util.h:53
#define SECP256K1_CONTEXT_SIGN
Definition: secp256k1.h:171
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:152
#define SECP256K1_CONTEXT_VERIFY
Flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size, and secp256k1_context...
Definition: secp256k1.h:170
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition: secp256k1.c:196
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
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:171
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:209
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_API int secp256k1_schnorrsig_sign(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msg32, const secp256k1_keypair *keypair, secp256k1_nonce_function_hardened noncefp, void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create a Schnorr signature.
Definition: main_impl.h:127
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_schnorrsig_verify(const secp256k1_context *ctx, const unsigned char *sig64, const unsigned char *msg32, const secp256k1_xonly_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Verify a Schnorr signature.
Definition: main_impl.h:190
const unsigned char ** sigs
const unsigned char ** pk
const unsigned char ** msgs
const secp256k1_keypair ** keypairs
secp256k1_context * ctx
Opaque data structure that holds a keypair consisting of a secret and a public key.
Opaque data structure that holds a parsed and valid "x-only" public key.