Bitcoin Core  0.20.99
P2P Digital Currency
bench_ecdh.c
Go to the documentation of this file.
1 /**********************************************************************
2  * Copyright (c) 2015 Pieter Wuille, Andrew Poelstra *
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 <string.h>
8 
9 #include "include/secp256k1.h"
10 #include "include/secp256k1_ecdh.h"
11 #include "util.h"
12 #include "bench.h"
13 
14 typedef struct {
17  unsigned char scalar[32];
19 
20 static void bench_ecdh_setup(void* arg) {
21  int i;
22  bench_ecdh_data *data = (bench_ecdh_data*)arg;
23  const unsigned char point[] = {
24  0x03,
25  0x54, 0x94, 0xc1, 0x5d, 0x32, 0x09, 0x97, 0x06,
26  0xc2, 0x39, 0x5f, 0x94, 0x34, 0x87, 0x45, 0xfd,
27  0x75, 0x7c, 0xe3, 0x0e, 0x4e, 0x8c, 0x90, 0xfb,
28  0xa2, 0xba, 0xd1, 0x84, 0xf8, 0x83, 0xc6, 0x9f
29  };
30 
31  for (i = 0; i < 32; i++) {
32  data->scalar[i] = i + 1;
33  }
34  CHECK(secp256k1_ec_pubkey_parse(data->ctx, &data->point, point, sizeof(point)) == 1);
35 }
36 
37 static void bench_ecdh(void* arg, int iters) {
38  int i;
39  unsigned char res[32];
40  bench_ecdh_data *data = (bench_ecdh_data*)arg;
41 
42  for (i = 0; i < iters; i++) {
43  CHECK(secp256k1_ecdh(data->ctx, res, &data->point, data->scalar, NULL, NULL) == 1);
44  }
45 }
46 
47 int main(void) {
48  bench_ecdh_data data;
49 
50  int iters = get_iters(20000);
51 
52  /* create a context with no capabilities */
54 
55  run_benchmark("ecdh", bench_ecdh, bench_ecdh_setup, NULL, &data, 10, iters);
56 
58  return 0;
59 }
static void bench_ecdh(void *arg, int iters)
Definition: bench_ecdh.c:37
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
static void bench_ecdh_setup(void *arg)
Definition: bench_ecdh.c:20
#define SECP256K1_FLAGS_TYPE_CONTEXT
Definition: secp256k1.h:160
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition: secp256k1.c:195
unsigned char scalar[32]
Definition: bench_ecdh.c:17
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:277
#define CHECK(cond)
Definition: util.h:53
secp256k1_context * ctx
Definition: bench_ecdh.c:15
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdh(const secp256k1_context *ctx, unsigned char *output, const secp256k1_pubkey *pubkey, const unsigned char *seckey, secp256k1_ecdh_hash_function hashfp, void *data) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Compute an EC Diffie-Hellman secret in constant time.
Definition: main_impl.h:29
int get_iters(int default_iters)
Definition: bench.h:124
int main(void)
Definition: bench_ecdh.c:47
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:151
secp256k1_pubkey point
Definition: bench_ecdh.c:16
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:67