Bitcoin ABC  0.22.13
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 http://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 #ifdef ENABLE_OPENSSL_TESTS
19 #include <openssl/bn.h>
20 #include <openssl/ecdsa.h>
21 #include <openssl/obj_mac.h>
22 #endif
23 
24 
25 typedef struct {
27  unsigned char msg[32];
28  unsigned char key[32];
29  unsigned char sig[72];
30  size_t siglen;
31  unsigned char pubkey[33];
32  size_t pubkeylen;
33 #ifdef ENABLE_OPENSSL_TESTS
34  EC_GROUP* ec_group;
35 #endif
37 
38 static void benchmark_verify(void* arg, int iters) {
39  int i;
41 
42  for (i = 0; i < iters; i++) {
43  secp256k1_pubkey pubkey;
45  data->sig[data->siglen - 1] ^= (i & 0xFF);
46  data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
47  data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
48  CHECK(secp256k1_ec_pubkey_parse(data->ctx, &pubkey, data->pubkey, data->pubkeylen) == 1);
49  CHECK(secp256k1_ecdsa_signature_parse_der(data->ctx, &sig, data->sig, data->siglen) == 1);
50  CHECK(secp256k1_ecdsa_verify(data->ctx, &sig, data->msg, &pubkey) == (i == 0));
51  data->sig[data->siglen - 1] ^= (i & 0xFF);
52  data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
53  data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
54  }
55 }
56 
57 #ifdef ENABLE_OPENSSL_TESTS
58 static void benchmark_verify_openssl(void* arg, int iters) {
59  int i;
61 
62  for (i = 0; i < iters; i++) {
63  data->sig[data->siglen - 1] ^= (i & 0xFF);
64  data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
65  data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
66  {
67  EC_KEY *pkey = EC_KEY_new();
68  const unsigned char *pubkey = &data->pubkey[0];
69  int result;
70 
71  CHECK(pkey != NULL);
72  result = EC_KEY_set_group(pkey, data->ec_group);
73  CHECK(result);
74  result = (o2i_ECPublicKey(&pkey, &pubkey, data->pubkeylen)) != NULL;
75  CHECK(result);
76  result = ECDSA_verify(0, &data->msg[0], sizeof(data->msg), &data->sig[0], data->siglen, pkey) == (i == 0);
77  CHECK(result);
78  EC_KEY_free(pkey);
79  }
80  data->sig[data->siglen - 1] ^= (i & 0xFF);
81  data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
82  data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
83  }
84 }
85 #endif
86 
87 #ifdef ENABLE_MODULE_SCHNORR
88 static void benchmark_schnorr_verify(void* arg, int iters) {
89  int i;
91 
92  for (i = 0; i < iters; i++) {
93  secp256k1_pubkey pubkey;
94  data->sig[data->siglen - 1] ^= (i & 0xFF);
95  data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
96  data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
97  CHECK(secp256k1_ec_pubkey_parse(data->ctx, &pubkey, data->pubkey, data->pubkeylen) == 1);
98  CHECK(secp256k1_schnorr_verify(data->ctx, data->sig, data->msg, &pubkey) == (i == 0));
99  data->sig[data->siglen - 1] ^= (i & 0xFF);
100  data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
101  data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
102  }
103 }
104 #endif
105 
106 int main(void) {
107  int i;
108  secp256k1_pubkey pubkey;
110  benchmark_verify_t data;
111 
112  int iters = get_iters(20000);
113 
115 
116  for (i = 0; i < 32; i++) {
117  data.msg[i] = 1 + i;
118  }
119  for (i = 0; i < 32; i++) {
120  data.key[i] = 33 + i;
121  }
122  data.siglen = 72;
123  CHECK(secp256k1_ecdsa_sign(data.ctx, &sig, data.msg, data.key, NULL, NULL));
124  CHECK(secp256k1_ecdsa_signature_serialize_der(data.ctx, data.sig, &data.siglen, &sig));
125  CHECK(secp256k1_ec_pubkey_create(data.ctx, &pubkey, data.key));
126  data.pubkeylen = 33;
128 
129  run_benchmark("ecdsa_verify", benchmark_verify, NULL, NULL, &data, 10, iters);
130 #ifdef ENABLE_OPENSSL_TESTS
131  data.ec_group = EC_GROUP_new_by_curve_name(NID_secp256k1);
132  run_benchmark("ecdsa_verify_openssl", benchmark_verify_openssl, NULL, NULL, &data, 10, iters);
133  EC_GROUP_free(data.ec_group);
134 #endif
135 #ifdef ENABLE_MODULE_SCHNORR
136  CHECK(secp256k1_schnorr_sign(data.ctx, data.sig, data.msg, data.key, NULL, NULL));
137  data.siglen = 64;
138  run_benchmark("schnorr_verify", benchmark_schnorr_verify, NULL, NULL, &data, 10, iters);
139 #endif
140 
142  return 0;
143 }
unsigned char pubkey[33]
Definition: bench_verify.c:31
std::array< uint8_t, 64 > sig
Definition: processor.cpp:248
static void benchmark_verify(void *arg, int iters)
Definition: bench_verify.c:38
SECP256K1_API int secp256k1_schnorr_sign(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msg32, 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
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
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:297
#define SECP256K1_CONTEXT_SIGN
Definition: secp256k1.h:171
unsigned char msg[32]
Definition: bench_verify.c:27
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition: secp256k1.c:197
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:574
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
Definition: secp256k1.h:176
int main(void)
Definition: bench_verify.c:106
SECP256K1_API int secp256k1_ecdsa_sign(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *msg32, 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:536
unsigned char sig[72]
Definition: bench_verify.c:29
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:279
Opaque data structured that holds a parsed ECDSA signature.
Definition: secp256k1.h:80
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_schnorr_verify(const secp256k1_context *ctx, const unsigned char *sig64, const unsigned char *msg32, 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
#define CHECK(cond)
Definition: util.h:53
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:344
#define SECP256K1_CONTEXT_VERIFY
Flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size, and secp256k1_context...
Definition: secp256k1.h:170
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:381
int get_iters(int default_iters)
Definition: bench.h:124
unsigned char key[32]
Definition: bench_verify.c:28
secp256k1_context * ctx
Definition: bench_verify.c:26
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:153
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:67
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(const secp256k1_context *ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msg32, 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:425