Bitcoin ABC  0.26.3
P2P Digital Currency
bench_verify.c
Go to the documentation of this file.
1 /***********************************************************************
2  * Copyright (c) 2014 Pieter Wuille *
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 <stdio.h>
8 #include <string.h>
9 
10 #include "include/secp256k1.h"
11 #include "util.h"
12 #include "bench.h"
13 
14 #ifdef ENABLE_MODULE_SCHNORR
16 #endif
17 
18 typedef struct {
20  unsigned char msg[32];
21  unsigned char key[32];
22  unsigned char sig[72];
23  size_t siglen;
24  unsigned char pubkey[33];
25  size_t pubkeylen;
27 
28 static void bench_verify(void* arg, int iters) {
29  int i;
31 
32  for (i = 0; i < iters; i++) {
33  secp256k1_pubkey pubkey;
35  data->sig[data->siglen - 1] ^= (i & 0xFF);
36  data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
37  data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
38  CHECK(secp256k1_ec_pubkey_parse(data->ctx, &pubkey, data->pubkey, data->pubkeylen) == 1);
39  CHECK(secp256k1_ecdsa_signature_parse_der(data->ctx, &sig, data->sig, data->siglen) == 1);
40  CHECK(secp256k1_ecdsa_verify(data->ctx, &sig, data->msg, &pubkey) == (i == 0));
41  data->sig[data->siglen - 1] ^= (i & 0xFF);
42  data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
43  data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
44  }
45 }
46 
47 #ifdef ENABLE_MODULE_SCHNORR
48 static void bench_schnorr_verify(void* arg, int iters) {
49  int i;
51 
52  for (i = 0; i < iters; i++) {
53  secp256k1_pubkey pubkey;
54  data->sig[data->siglen - 1] ^= (i & 0xFF);
55  data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
56  data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
57  CHECK(secp256k1_ec_pubkey_parse(data->ctx, &pubkey, data->pubkey, data->pubkeylen) == 1);
58  CHECK(secp256k1_schnorr_verify(data->ctx, data->sig, data->msg, &pubkey) == (i == 0));
59  data->sig[data->siglen - 1] ^= (i & 0xFF);
60  data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
61  data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
62  }
63 }
64 #endif
65 
66 int main(void) {
67  int i;
68  secp256k1_pubkey pubkey;
70  bench_verify_data data;
71 
72  int iters = get_iters(20000);
73 
75 
76  for (i = 0; i < 32; i++) {
77  data.msg[i] = 1 + i;
78  }
79  for (i = 0; i < 32; i++) {
80  data.key[i] = 33 + i;
81  }
82  data.siglen = 72;
83  CHECK(secp256k1_ecdsa_sign(data.ctx, &sig, data.msg, data.key, NULL, NULL));
85  CHECK(secp256k1_ec_pubkey_create(data.ctx, &pubkey, data.key));
86  data.pubkeylen = 33;
88 
89  run_benchmark("ecdsa_verify", bench_verify, NULL, NULL, &data, 10, iters);
90 
91 #ifdef ENABLE_MODULE_SCHNORR
92  CHECK(secp256k1_schnorr_sign(data.ctx, data.sig, data.msg, data.key, NULL, NULL));
93  data.siglen = 64;
94  run_benchmark("schnorr_verify", bench_schnorr_verify, NULL, NULL, &data, 10, iters);
95 #endif
96 
98  return 0;
99 }
int main(void)
Definition: bench_verify.c:66
static void bench_verify(void *arg, int iters)
Definition: bench_verify.c:28
SchnorrSig sig
Definition: processor.cpp:423
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 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.
Definition: secp256k1.c:296
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.
Definition: secp256k1.c:535
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.
Definition: secp256k1.c:278
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.
Definition: secp256k1.c:343
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.
Definition: secp256k1.c:573
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_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
Definition: secp256k1.h:176
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.
Definition: secp256k1.c:424
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.
Definition: secp256k1.c:380
#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_schnorr_sign(const secp256k1_context *ctx, unsigned char *sig64, 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 a signature using a custom EC-Schnorr-SHA256 construction.
Definition: main_impl.h:33
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_schnorr_verify(const secp256k1_context *ctx, const unsigned char *sig64, 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 a signature created by secp256k1_schnorr_sign.
Definition: main_impl.h:13
unsigned char pubkey[33]
Definition: bench_verify.c:24
secp256k1_context * ctx
Definition: bench_verify.c:19
unsigned char msg[32]
Definition: bench_verify.c:20
unsigned char sig[72]
Definition: bench_verify.c:22
unsigned char key[32]
Definition: bench_verify.c:21
Opaque data structured that holds a parsed ECDSA signature.
Definition: secp256k1.h:80
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:67