Bitcoin Core  27.99.0
P2P Digital Currency
bench_impl.h
Go to the documentation of this file.
1 /***********************************************************************
2  * Distributed under the MIT software license, see the accompanying *
3  * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
4  ***********************************************************************/
5 
6 #ifndef SECP256K1_MODULE_ELLSWIFT_BENCH_H
7 #define SECP256K1_MODULE_ELLSWIFT_BENCH_H
8 
9 #include "../../../include/secp256k1_ellswift.h"
10 
11 typedef struct {
13  secp256k1_pubkey point[256];
14  unsigned char rnd64[64];
16 
17 static void bench_ellswift_setup(void *arg) {
18  int i;
20  static const unsigned char init[64] = {
21  0x78, 0x1f, 0xb7, 0xd4, 0x67, 0x7f, 0x08, 0x68,
22  0xdb, 0xe3, 0x1d, 0x7f, 0x1b, 0xb0, 0xf6, 0x9e,
23  0x0a, 0x64, 0xca, 0x32, 0x9e, 0xc6, 0x20, 0x79,
24  0x03, 0xf3, 0xd0, 0x46, 0x7a, 0x0f, 0xd2, 0x21,
25  0xb0, 0x2c, 0x46, 0xd8, 0xba, 0xca, 0x26, 0x4f,
26  0x8f, 0x8c, 0xd4, 0xdd, 0x2d, 0x04, 0xbe, 0x30,
27  0x48, 0x51, 0x1e, 0xd4, 0x16, 0xfd, 0x42, 0x85,
28  0x62, 0xc9, 0x02, 0xf9, 0x89, 0x84, 0xff, 0xdc
29  };
30  memcpy(data->rnd64, init, 64);
31  for (i = 0; i < 256; ++i) {
32  int j;
33  CHECK(secp256k1_ellswift_decode(data->ctx, &data->point[i], data->rnd64));
34  for (j = 0; j < 64; ++j) {
35  data->rnd64[j] += 1;
36  }
37  }
38  CHECK(secp256k1_ellswift_encode(data->ctx, data->rnd64, &data->point[255], init + 16));
39 }
40 
41 static void bench_ellswift_encode(void *arg, int iters) {
42  int i;
44 
45  for (i = 0; i < iters; i++) {
46  CHECK(secp256k1_ellswift_encode(data->ctx, data->rnd64, &data->point[i & 255], data->rnd64 + 16));
47  }
48 }
49 
50 static void bench_ellswift_create(void *arg, int iters) {
51  int i;
53 
54  for (i = 0; i < iters; i++) {
55  unsigned char buf[64];
56  CHECK(secp256k1_ellswift_create(data->ctx, buf, data->rnd64, data->rnd64 + 32));
57  memcpy(data->rnd64, buf, 64);
58  }
59 }
60 
61 static void bench_ellswift_decode(void *arg, int iters) {
62  int i;
64  size_t len;
66 
67  for (i = 0; i < iters; i++) {
68  CHECK(secp256k1_ellswift_decode(data->ctx, &out, data->rnd64) == 1);
69  len = 33;
70  CHECK(secp256k1_ec_pubkey_serialize(data->ctx, data->rnd64 + (i % 32), &len, &out, SECP256K1_EC_COMPRESSED));
71  }
72 }
73 
74 static void bench_ellswift_xdh(void *arg, int iters) {
75  int i;
77 
78  for (i = 0; i < iters; i++) {
79  int party = i & 1;
81  data->rnd64 + (i % 33),
82  data->rnd64,
83  data->rnd64,
84  data->rnd64 + ((i + 16) % 33),
85  party,
87  NULL) == 1);
88  }
89 }
90 
91 void run_ellswift_bench(int iters, int argc, char **argv) {
93  int d = argc == 1;
94 
95  /* create a context with signing capabilities */
97 
98  if (d || have_flag(argc, argv, "ellswift") || have_flag(argc, argv, "encode") || have_flag(argc, argv, "ellswift_encode")) run_benchmark("ellswift_encode", bench_ellswift_encode, bench_ellswift_setup, NULL, &data, 10, iters);
99  if (d || have_flag(argc, argv, "ellswift") || have_flag(argc, argv, "decode") || have_flag(argc, argv, "ellswift_decode")) run_benchmark("ellswift_decode", bench_ellswift_decode, bench_ellswift_setup, NULL, &data, 10, iters);
100  if (d || have_flag(argc, argv, "ellswift") || have_flag(argc, argv, "keygen") || have_flag(argc, argv, "ellswift_keygen")) run_benchmark("ellswift_keygen", bench_ellswift_create, bench_ellswift_setup, NULL, &data, 10, iters);
101  if (d || have_flag(argc, argv, "ellswift") || have_flag(argc, argv, "ecdh") || have_flag(argc, argv, "ellswift_ecdh")) run_benchmark("ellswift_ecdh", bench_ellswift_xdh, bench_ellswift_setup, NULL, &data, 10, iters);
102 
104 }
105 
106 #endif
static void run_benchmark(char *name, void(*benchmark)(void *), void(*setup)(void *), void(*teardown)(void *), void *data, int count, int iter)
Definition: bench.c:26
static void bench_ellswift_setup(void *arg)
Definition: bench_impl.h:17
static void bench_ellswift_decode(void *arg, int iters)
Definition: bench_impl.h:61
static void bench_ellswift_create(void *arg, int iters)
Definition: bench_impl.h:50
void run_ellswift_bench(int iters, int argc, char **argv)
Definition: bench_impl.h:91
static void bench_ellswift_encode(void *arg, int iters)
Definition: bench_impl.h:41
static void bench_ellswift_xdh(void *arg, int iters)
Definition: bench_impl.h:74
#define CHECK(cond)
Unconditional failure on condition failure.
Definition: util.h:35
static int have_flag(int argc, char **argv, char *flag)
Definition: bench.h:132
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:186
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:279
#define SECP256K1_CONTEXT_NONE
Context flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size,...
Definition: secp256k1.h:205
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:140
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
Definition: secp256k1.h:215
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ellswift_create(const secp256k1_context *ctx, unsigned char *ell64, const unsigned char *seckey32, const unsigned char *auxrnd32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute an ElligatorSwift public key for a secret key.
Definition: main_impl.h:450
SECP256K1_API int secp256k1_ellswift_decode(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *ell64) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Decode a 64-bytes ElligatorSwift encoded public key.
Definition: main_impl.h:489
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ellswift_xdh(const secp256k1_context *ctx, unsigned char *output, const unsigned char *ell_a64, const unsigned char *ell_b64, const unsigned char *seckey32, int party, secp256k1_ellswift_xdh_hash_function hashfp, void *data) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(7)
Given a private key, and ElligatorSwift public keys sent in both directions, compute a shared secret ...
Definition: main_impl.h:549
SECP256K1_API const secp256k1_ellswift_xdh_hash_function secp256k1_ellswift_xdh_hash_function_bip324
An implementation of an secp256k1_ellswift_xdh_hash_function compatible with BIP324.
SECP256K1_API int secp256k1_ellswift_encode(const secp256k1_context *ctx, unsigned char *ell64, const secp256k1_pubkey *pubkey, const unsigned char *rnd32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Construct a 64-byte ElligatorSwift encoding of a given pubkey.
Definition: main_impl.h:399
secp256k1_context * ctx
Definition: bench_impl.h:12
unsigned char rnd64[64]
Definition: bench_impl.h:14
secp256k1_pubkey point[256]
Definition: bench_impl.h:13
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:74