PRCYCoin  2.0.0.7rc1
P2P Digital Currency
bench_whitelist.c
Go to the documentation of this file.
1 /**********************************************************************
2  * Copyright (c) 2017 Jonas Nick *
3  * Distributed under the MIT software license, see the accompanying *
4  * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5  **********************************************************************/
6 #include <stdio.h>
7 
8 #include "include/secp256k1_2.h"
9 
11 #include "bench.h"
12 #include "util.h"
13 #include "hash_impl.h"
14 #include "num_impl.h"
15 #include "scalar_impl.h"
16 #include "testrand_impl.h"
17 
18 #define MAX_N_KEYS 30
19 
20 typedef struct {
21  secp256k1_context2* ctx;
22  unsigned char online_seckey[MAX_N_KEYS][32];
23  unsigned char summed_seckey[MAX_N_KEYS][32];
24  secp256k1_pubkey2 online_pubkeys[MAX_N_KEYS];
25  secp256k1_pubkey2 offline_pubkeys[MAX_N_KEYS];
26  unsigned char csub[32];
29  size_t n_keys;
30 } bench_data;
31 
32 static void bench_whitelist(void* arg) {
33  bench_data* data = (bench_data*)arg;
34  CHECK(secp256k1_whitelist_verify(data->ctx, &data->sig, data->online_pubkeys, data->offline_pubkeys, data->n_keys, &data->sub_pubkey) == 1);
35 }
36 
37 static void bench_whitelist_setup(void* arg) {
38  bench_data* data = (bench_data*)arg;
39  int i = 0;
40  CHECK(secp256k1_whitelist_sign(data->ctx, &data->sig, data->online_pubkeys, data->offline_pubkeys, data->n_keys, &data->sub_pubkey, data->online_seckey[i], data->summed_seckey[i], i, NULL, NULL));
41 }
42 
43 static void run_test(bench_data* data) {
44  char str[32];
45  sprintf(str, "whitelist_%i", (int)data->n_keys);
46  run_benchmark(str, bench_whitelist, bench_whitelist_setup, NULL, data, 100, 1);
47 }
48 
50  do {
51  unsigned char b32[32];
52  int overflow = 0;
53  secp256k1_rand256(b32);
54  secp256k1_scalar_set_b32(num, b32, &overflow);
55  if (overflow || secp256k1_scalar_is_zero(num)) {
56  continue;
57  }
58  break;
59  } while(1);
60 }
61 
62 int main(void) {
63  bench_data data;
64  size_t i;
65  size_t n_keys = 30;
66  secp256k1_scalar ssub;
67 
69 
70  /* Start with subkey */
71  random_scalar_order(&ssub);
72  secp256k1_scalar_get_b32(data.csub, &ssub);
73  CHECK(secp256k1_ec_seckey_verify2(data.ctx, data.csub) == 1);
74  CHECK(secp256k1_ec_pubkey_create2(data.ctx, &data.sub_pubkey, data.csub) == 1);
75  /* Then offline and online whitelist keys */
76  for (i = 0; i < n_keys; i++) {
77  secp256k1_scalar son, soff;
78 
79  /* Create two keys */
80  random_scalar_order(&son);
81  secp256k1_scalar_get_b32(data.online_seckey[i], &son);
83  CHECK(secp256k1_ec_pubkey_create2(data.ctx, &data.online_pubkeys[i], data.online_seckey[i]) == 1);
84 
85  random_scalar_order(&soff);
86  secp256k1_scalar_get_b32(data.summed_seckey[i], &soff);
88  CHECK(secp256k1_ec_pubkey_create2(data.ctx, &data.offline_pubkeys[i], data.summed_seckey[i]) == 1);
89 
90  /* Make summed_seckey correspond to the sum of offline_pubkey and sub_pubkey */
91  secp256k1_scalar_add(&soff, &soff, &ssub);
92  secp256k1_scalar_get_b32(data.summed_seckey[i], &soff);
94  }
95 
96  /* Run test */
97  for (i = 1; i <= n_keys; ++i) {
98  data.n_keys = i;
99  run_test(&data);
100  }
101 
103  return(0);
104 }
bench.h
secp256k1_ec_pubkey_create2
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create2(const secp256k1_context2 *ctx, secp256k1_pubkey2 *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_2.c:434
util.h
testrand_impl.h
bench_data::online_pubkeys
secp256k1_pubkey2 online_pubkeys[MAX_N_KEYS]
Definition: bench_whitelist.c:24
bench_data::sub_pubkey
secp256k1_pubkey2 sub_pubkey
Definition: bench_whitelist.c:27
secp256k1_pubkey2
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1_2.h:66
secp256k1_context_destroy
SECP256K1_API void secp256k1_context_destroy(secp256k1_context2 *ctx)
Destroy a secp256k1 context object.
Definition: secp256k1_2.c:110
run_benchmark
void run_benchmark(char *name, void(*benchmark)(void *), void(*setup)(void *), void(*teardown)(void *), void *data, int count, int iter)
Definition: bench.h:34
secp256k1_scalar
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
secp256k1_whitelist_sign
SECP256K1_API int secp256k1_whitelist_sign(const secp256k1_context2 *ctx, secp256k1_whitelist_signature *sig, const secp256k1_pubkey2 *online_pubkeys, const secp256k1_pubkey2 *offline_pubkeys, const size_t n_keys, const secp256k1_pubkey2 *sub_pubkey, const unsigned char *online_seckey, const unsigned char *summed_seckey, const size_t index, secp256k1_nonce_function2 noncefp, const void *noncedata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(6) SECP256K1_ARG_NONNULL(7) SECP256K1_ARG_NONNULL(8)
Compute a whitelist signature Returns 1: signature was successfully created 0: signature was not succ...
Definition: main_impl.h:15
secp256k1_ec_seckey_verify2
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify2(const secp256k1_context2 *ctx, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Verify an ECDSA secret key.
Definition: secp256k1_2.c:421
MAX_N_KEYS
#define MAX_N_KEYS
Definition: bench_whitelist.c:18
secp256k1_whitelist_verify
SECP256K1_API int secp256k1_whitelist_verify(const secp256k1_context2 *ctx, const secp256k1_whitelist_signature *sig, const secp256k1_pubkey2 *online_pubkeys, const secp256k1_pubkey2 *offline_pubkeys, const size_t n_keys, const secp256k1_pubkey2 *sub_pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(6)
Verify a whitelist signature Returns 1: signature is valid 0: signature is not valid In: ctx: pointer...
Definition: main_impl.h:103
secp256k1_whitelist_signature
Opaque data structure that holds a parsed whitelist proof.
Definition: secp256k1_whitelist.h:34
bench_data::sig
secp256k1_whitelist_signature sig
Definition: bench_whitelist.c:28
secp256k1_whitelist.h
hash_impl.h
bench_data::online_seckey
unsigned char online_seckey[MAX_N_KEYS][32]
Definition: bench_whitelist.c:22
scalar_impl.h
num_impl.h
secp256k1_context_struct2
Definition: secp256k1_types.h:15
bench_data
Definition: bench_ecmult.c:23
bench_data::csub
unsigned char csub[32]
Definition: bench_whitelist.c:26
secp256k1_2.h
random_scalar_order
void random_scalar_order(secp256k1_scalar *num)
Definition: bench_whitelist.c:49
main
int main(void)
Definition: bench_whitelist.c:62
secp256k1_context_create2
SECP256K1_API secp256k1_context2 * secp256k1_context_create2(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object.
Definition: secp256k1_2.c:75
bench_data::offline_pubkeys
secp256k1_pubkey2 offline_pubkeys[MAX_N_KEYS]
Definition: bench_whitelist.c:25
SECP256K1_CONTEXT_VERIFY
#define SECP256K1_CONTEXT_VERIFY
Flags to pass to secp256k1_context_create2.
Definition: secp256k1_2.h:167
bench_data::n_keys
size_t n_keys
Definition: bench_whitelist.c:29
CHECK
#define CHECK(cond)
Definition: util.h:43
bench_data::ctx
secp256k1_context2 * ctx
Definition: bench_ecmult.c:25
SECP256K1_CONTEXT_SIGN
#define SECP256K1_CONTEXT_SIGN
Definition: secp256k1_2.h:168
bench_data::summed_seckey
unsigned char summed_seckey[MAX_N_KEYS][32]
Definition: bench_whitelist.c:23