Dash Core  0.12.2.1
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 typedef struct {
16  unsigned char msg[32];
17  unsigned char key[32];
18  unsigned char sig[72];
19  size_t siglen;
20  unsigned char pubkey[33];
21  size_t pubkeylen;
23 
24 static void benchmark_verify(void* arg) {
25  int i;
27 
28  for (i = 0; i < 20000; i++) {
29  secp256k1_pubkey pubkey;
31  data->sig[data->siglen - 1] ^= (i & 0xFF);
32  data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
33  data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
34  CHECK(secp256k1_ec_pubkey_parse(data->ctx, &pubkey, data->pubkey, data->pubkeylen) == 1);
35  CHECK(secp256k1_ecdsa_signature_parse_der(data->ctx, &sig, data->sig, data->siglen) == 1);
36  CHECK(secp256k1_ecdsa_verify(data->ctx, &sig, data->msg, &pubkey) == (i == 0));
37  data->sig[data->siglen - 1] ^= (i & 0xFF);
38  data->sig[data->siglen - 2] ^= ((i >> 8) & 0xFF);
39  data->sig[data->siglen - 3] ^= ((i >> 16) & 0xFF);
40  }
41 }
42 
43 int main(void) {
44  int i;
45  secp256k1_pubkey pubkey;
48 
50 
51  for (i = 0; i < 32; i++) {
52  data.msg[i] = 1 + i;
53  }
54  for (i = 0; i < 32; i++) {
55  data.key[i] = 33 + i;
56  }
57  data.siglen = 72;
58  CHECK(secp256k1_ecdsa_sign(data.ctx, &sig, data.msg, data.key, NULL, NULL));
60  CHECK(secp256k1_ec_pubkey_create(data.ctx, &pubkey, data.key));
61  data.pubkeylen = 33;
62  CHECK(secp256k1_ec_pubkey_serialize(data.ctx, data.pubkey, &data.pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
63 
64  run_benchmark("ecdsa_verify", benchmark_verify, NULL, NULL, &data, 10, 20000);
65 
67  return 0;
68 }
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)
Definition: secp256k1.c:168
#define SECP256K1_CONTEXT_SIGN
Definition: secp256k1.h:161
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx)
Definition: secp256k1.c:94
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)
Definition: secp256k1.c:409
#define SECP256K1_EC_COMPRESSED
Definition: secp256k1.h:165
void run_benchmark(char *name, void(*benchmark)(void *), void(*setup)(void *), void(*teardown)(void *), void *data, int count, int iter)
Definition: bench.h:33
int main(void)
Definition: bench_verify.c:43
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)
Definition: secp256k1.c:349
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)
Definition: secp256k1.c:152
#define CHECK(cond)
Definition: util.h:52
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)
Definition: secp256k1.c:216
#define SECP256K1_CONTEXT_VERIFY
Definition: secp256k1.h:160
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)
Definition: secp256k1.c:253
static void benchmark_verify(void *arg)
Definition: bench_verify.c:24
secp256k1_context * ctx
Definition: bench_verify.c:15
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Definition: secp256k1.c:60
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)
Definition: secp256k1.c:297