Bitcoin Core  22.99.0
P2P Digital Currency
bench_impl.h
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 https://www.opensource.org/licenses/mit-license.php.*
5  ***********************************************************************/
6 
7 #ifndef SECP256K1_MODULE_ECDH_BENCH_H
8 #define SECP256K1_MODULE_ECDH_BENCH_H
9 
10 #include "../include/secp256k1_ecdh.h"
11 
12 typedef struct {
14  secp256k1_pubkey point;
15  unsigned char scalar[32];
17 
18 static void bench_ecdh_setup(void* arg) {
19  int i;
20  bench_ecdh_data *data = (bench_ecdh_data*)arg;
21  const unsigned char point[] = {
22  0x03,
23  0x54, 0x94, 0xc1, 0x5d, 0x32, 0x09, 0x97, 0x06,
24  0xc2, 0x39, 0x5f, 0x94, 0x34, 0x87, 0x45, 0xfd,
25  0x75, 0x7c, 0xe3, 0x0e, 0x4e, 0x8c, 0x90, 0xfb,
26  0xa2, 0xba, 0xd1, 0x84, 0xf8, 0x83, 0xc6, 0x9f
27  };
28 
29  for (i = 0; i < 32; i++) {
30  data->scalar[i] = i + 1;
31  }
32  CHECK(secp256k1_ec_pubkey_parse(data->ctx, &data->point, point, sizeof(point)) == 1);
33 }
34 
35 static void bench_ecdh(void* arg, int iters) {
36  int i;
37  unsigned char res[32];
38  bench_ecdh_data *data = (bench_ecdh_data*)arg;
39 
40  for (i = 0; i < iters; i++) {
41  CHECK(secp256k1_ecdh(data->ctx, res, &data->point, data->scalar, NULL, NULL) == 1);
42  }
43 }
44 
45 void run_ecdh_bench(int iters, int argc, char** argv) {
46  bench_ecdh_data data;
47  int d = argc == 1;
48 
49  /* create a context with no capabilities */
51 
52  if (d || have_flag(argc, argv, "ecdh")) run_benchmark("ecdh", bench_ecdh, bench_ecdh_setup, NULL, &data, 10, iters);
53 
55 }
56 
57 #endif /* SECP256K1_MODULE_ECDH_BENCH_H */
secp256k1_context_struct
Definition: secp256k1.c:47
bench_ecdh_data
Definition: bench_impl.h:12
secp256k1_context_create
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:107
secp256k1_ec_pubkey_parse
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:228
run_benchmark
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:82
bench_ecdh_data::scalar
unsigned char scalar[32]
Definition: bench_impl.h:19
CHECK
#define CHECK(cond)
Unconditional failure on condition failure.
Definition: util.h:35
bench_ecdh_data::point
secp256k1_pubkey point
Definition: bench_impl.h:18
secp256k1_context_destroy
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition: secp256k1.c:146
SECP256K1_FLAGS_TYPE_CONTEXT
#define SECP256K1_FLAGS_TYPE_CONTEXT
Definition: secp256k1.h:174
secp256k1_ecdh
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
secp256k1_pubkey
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:70
bench_ecdh_data::ctx
secp256k1_context * ctx
Definition: bench_impl.h:17
bench_ecdh
static void bench_ecdh(void *arg, int iters)
Definition: bench_impl.h:35
run_ecdh_bench
void run_ecdh_bench(int iters, int argc, char **argv)
Definition: bench_impl.h:45
bench_ecdh_setup
static void bench_ecdh_setup(void *arg)
Definition: bench_impl.h:18
have_flag
int have_flag(int argc, char **argv, char *flag)
Definition: bench.h:116
ctx
static secp256k1_context * ctx
Definition: tests.c:32