Bitcoin ABC 0.30.7
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#define MSGLEN 32
17
18typedef struct {
20 int n;
21
23 const unsigned char **pk;
24 const unsigned char **sigs;
25 const unsigned char **msgs;
27
28void bench_schnorrsig_sign(void* arg, int iters) {
30 int i;
31 unsigned char msg[MSGLEN] = {0};
32 unsigned char sig[64];
33
34 for (i = 0; i < iters; i++) {
35 msg[0] = i;
36 msg[1] = i >> 8;
37 CHECK(secp256k1_schnorrsig_sign_custom(data->ctx, sig, msg, MSGLEN, data->keypairs[i], NULL));
38 }
39}
40
41void bench_schnorrsig_verify(void* arg, int iters) {
43 int i;
44
45 for (i = 0; i < iters; i++) {
47 CHECK(secp256k1_xonly_pubkey_parse(data->ctx, &pk, data->pk[i]) == 1);
48 CHECK(secp256k1_schnorrsig_verify(data->ctx, data->sigs[i], data->msgs[i], MSGLEN, &pk));
49 }
50}
51
52int main(void) {
53 int i;
55 int iters = get_iters(10000);
56
58 data.keypairs = (const secp256k1_keypair **)malloc(iters * sizeof(secp256k1_keypair *));
59 data.pk = (const unsigned char **)malloc(iters * sizeof(unsigned char *));
60 data.msgs = (const unsigned char **)malloc(iters * sizeof(unsigned char *));
61 data.sigs = (const unsigned char **)malloc(iters * sizeof(unsigned char *));
62
63 CHECK(MSGLEN >= 4);
64 for (i = 0; i < iters; i++) {
65 unsigned char sk[32];
66 unsigned char *msg = (unsigned char *)malloc(MSGLEN);
67 unsigned char *sig = (unsigned char *)malloc(64);
68 secp256k1_keypair *keypair = (secp256k1_keypair *)malloc(sizeof(*keypair));
69 unsigned char *pk_char = (unsigned char *)malloc(32);
71 msg[0] = sk[0] = i;
72 msg[1] = sk[1] = i >> 8;
73 msg[2] = sk[2] = i >> 16;
74 msg[3] = sk[3] = i >> 24;
75 memset(&msg[4], 'm', MSGLEN - 4);
76 memset(&sk[4], 's', 28);
77
78 data.keypairs[i] = keypair;
79 data.pk[i] = pk_char;
80 data.msgs[i] = msg;
81 data.sigs[i] = sig;
82
83 CHECK(secp256k1_keypair_create(data.ctx, keypair, sk));
84 CHECK(secp256k1_schnorrsig_sign_custom(data.ctx, sig, msg, MSGLEN, keypair, NULL));
85 CHECK(secp256k1_keypair_xonly_pub(data.ctx, &pk, NULL, keypair));
86 CHECK(secp256k1_xonly_pubkey_serialize(data.ctx, pk_char, &pk) == 1);
87 }
88
89 run_benchmark("schnorrsig_sign", bench_schnorrsig_sign, NULL, NULL, (void *) &data, 10, iters);
90 run_benchmark("schnorrsig_verify", bench_schnorrsig_verify, NULL, NULL, (void *) &data, 10, iters);
91
92 for (i = 0; i < iters; i++) {
93 free((void *)data.keypairs[i]);
94 free((void *)data.pk[i]);
95 free((void *)data.msgs[i]);
96 free((void *)data.sigs[i]);
97 }
98 free(data.keypairs);
99 free(data.pk);
100 free(data.msgs);
101 free(data.sigs);
102
104 return 0;
105}
#define MSGLEN
void bench_schnorrsig_sign(void *arg, int iters)
int main(void)
void bench_schnorrsig_verify(void *arg, int iters)
SchnorrSig sig
Definition: processor.cpp:498
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:174
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:173
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:197
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:235
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_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:192
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:207
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.