PRCYCoin  2.0.0.7rc1
P2P Digital Currency
tests_impl.h
Go to the documentation of this file.
1 /**********************************************************************
2  * Copyright (c) 2014-2016 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 #ifndef SECP256K1_MODULE_WHITELIST_TESTS
8 #define SECP256K1_MODULE_WHITELIST_TESTS
9 
11 
12 void test_whitelist_end_to_end(const size_t n_keys) {
13  unsigned char **online_seckey = (unsigned char **) malloc(n_keys * sizeof(*online_seckey));
14  unsigned char **summed_seckey = (unsigned char **) malloc(n_keys * sizeof(*summed_seckey));
15  secp256k1_pubkey2 *online_pubkeys = (secp256k1_pubkey2 *) malloc(n_keys * sizeof(*online_pubkeys));
16  secp256k1_pubkey2 *offline_pubkeys = (secp256k1_pubkey2 *) malloc(n_keys * sizeof(*offline_pubkeys));
17 
18  secp256k1_scalar ssub;
19  unsigned char csub[32];
20  secp256k1_pubkey2 sub_pubkey;
21 
22  /* Generate random keys */
23  size_t i;
24  /* Start with subkey */
26  secp256k1_scalar_get_b32(csub, &ssub);
27  CHECK(secp256k1_ec_seckey_verify2(ctx, csub) == 1);
28  CHECK(secp256k1_ec_pubkey_create2(ctx, &sub_pubkey, csub) == 1);
29  /* Then offline and online whitelist keys */
30  for (i = 0; i < n_keys; i++) {
31  secp256k1_scalar son, soff;
32 
33  online_seckey[i] = (unsigned char *) malloc(32);
34  summed_seckey[i] = (unsigned char *) malloc(32);
35 
36  /* Create two keys */
38  secp256k1_scalar_get_b32(online_seckey[i], &son);
39  CHECK(secp256k1_ec_seckey_verify2(ctx, online_seckey[i]) == 1);
40  CHECK(secp256k1_ec_pubkey_create2(ctx, &online_pubkeys[i], online_seckey[i]) == 1);
41 
43  secp256k1_scalar_get_b32(summed_seckey[i], &soff);
44  CHECK(secp256k1_ec_seckey_verify2(ctx, summed_seckey[i]) == 1);
45  CHECK(secp256k1_ec_pubkey_create2(ctx, &offline_pubkeys[i], summed_seckey[i]) == 1);
46 
47  /* Make summed_seckey correspond to the sum of offline_pubkey and sub_pubkey */
48  secp256k1_scalar_add(&soff, &soff, &ssub);
49  secp256k1_scalar_get_b32(summed_seckey[i], &soff);
50  CHECK(secp256k1_ec_seckey_verify2(ctx, summed_seckey[i]) == 1);
51  }
52 
53  /* Sign/verify with each one */
54  for (i = 0; i < n_keys; i++) {
55  unsigned char serialized[32 + 4 + 32 * SECP256K1_WHITELIST_MAX_N_KEYS] = {0};
56  size_t slen = sizeof(serialized);
59 
60  CHECK(secp256k1_whitelist_sign(ctx, &sig, online_pubkeys, offline_pubkeys, n_keys, &sub_pubkey, online_seckey[i], summed_seckey[i], i, NULL, NULL));
61  CHECK(secp256k1_whitelist_verify(ctx, &sig, online_pubkeys, offline_pubkeys, n_keys, &sub_pubkey) == 1);
62  /* Check that exchanging keys causes a failure */
63  CHECK(secp256k1_whitelist_verify(ctx, &sig, offline_pubkeys, online_pubkeys, n_keys, &sub_pubkey) != 1);
64  /* Serialization round trip */
65  CHECK(secp256k1_whitelist_signature_serialize(ctx, serialized, &slen, &sig) == 1);
66  CHECK(slen == 33 + 32 * n_keys);
67  CHECK(secp256k1_whitelist_signature_parse(ctx, &sig1, serialized, slen) == 1);
68  /* (Check various bad-length conditions) */
69  CHECK(secp256k1_whitelist_signature_parse(ctx, &sig1, serialized, slen + 32) == 0);
70  CHECK(secp256k1_whitelist_signature_parse(ctx, &sig1, serialized, slen + 1) == 0);
71  CHECK(secp256k1_whitelist_signature_parse(ctx, &sig1, serialized, slen - 1) == 0);
72  CHECK(secp256k1_whitelist_signature_parse(ctx, &sig1, serialized, 0) == 0);
73  CHECK(secp256k1_whitelist_verify(ctx, &sig1, online_pubkeys, offline_pubkeys, n_keys, &sub_pubkey) == 1);
74  CHECK(secp256k1_whitelist_verify(ctx, &sig1, offline_pubkeys, online_pubkeys, n_keys, &sub_pubkey) != 1);
75 
76  /* Test n_keys */
79 
80  /* Test bad number of keys in signature */
81  sig.n_keys = n_keys + 1;
82  CHECK(secp256k1_whitelist_verify(ctx, &sig, offline_pubkeys, online_pubkeys, n_keys, &sub_pubkey) != 1);
83  sig.n_keys = n_keys;
84  }
85 
86  for (i = 0; i < n_keys; i++) {
87  free(online_seckey[i]);
88  free(summed_seckey[i]);
89  }
90  free(online_seckey);
91  free(summed_seckey);
92  free(online_pubkeys);
93  free(offline_pubkeys);
94 }
95 
98 
99  const unsigned char serialized0[] = { 1+32*(0+1) };
100  const unsigned char serialized1[] = {
101  0x00,
102  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
103  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
104  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
105  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06
106  };
107  const unsigned char serialized2[] = {
108  0x01,
109  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
110  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
111  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
112  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
113  };
114 
115  /* Empty input */
116  CHECK(secp256k1_whitelist_signature_parse(ctx, &sig, serialized0, 0) == 0);
117  /* Misses one byte of e0 */
118  CHECK(secp256k1_whitelist_signature_parse(ctx, &sig, serialized1, sizeof(serialized1)) == 0);
119  /* Enough bytes for e0, but there is no s value */
120  CHECK(secp256k1_whitelist_signature_parse(ctx, &sig, serialized2, sizeof(serialized2)) == 0);
121 }
122 
124  unsigned char serialized[] = {
125  0x00,
126  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
127  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
128  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
129  0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
130  };
131  size_t serialized_len;
133 
134  CHECK(secp256k1_whitelist_signature_parse(ctx, &sig, serialized, sizeof(serialized)) == 1);
135  serialized_len = sizeof(serialized) - 1;
136  /* Output buffer is one byte too short */
137  CHECK(secp256k1_whitelist_signature_serialize(ctx, serialized, &serialized_len, &sig) == 0);
138 }
139 
141  int i;
144  for (i = 0; i < count; i++) {
148  }
149 }
150 
151 #endif
secp256k1_whitelist_signature_parse
SECP256K1_API int secp256k1_whitelist_signature_parse(const secp256k1_context2 *ctx, secp256k1_whitelist_signature *sig, const unsigned char *input, size_t input_len) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse a whitelist signature.
Definition: main_impl.h:139
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
secp256k1_whitelist_signature::n_keys
size_t n_keys
Definition: secp256k1_whitelist.h:35
secp256k1_pubkey2
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1_2.h:66
random_scalar_order_test
void random_scalar_order_test(secp256k1_scalar_t *num)
Definition: tests.c:68
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
SECP256K1_WHITELIST_MAX_N_KEYS
#define SECP256K1_WHITELIST_MAX_N_KEYS
Definition: secp256k1_whitelist.h:16
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
run_whitelist_tests
void run_whitelist_tests(void)
Definition: tests_impl.h:140
secp256k1_whitelist.h
test_whitelist_bad_parse
void test_whitelist_bad_parse(void)
Definition: tests_impl.h:96
secp256k1_whitelist_signature_n_keys
SECP256K1_API size_t secp256k1_whitelist_signature_n_keys(const secp256k1_whitelist_signature *sig) SECP256K1_ARG_NONNULL(1)
Returns the number of keys a signature expects to have.
Definition: main_impl.h:135
test_whitelist_bad_serialize
void test_whitelist_bad_serialize(void)
Definition: tests_impl.h:123
secp256k1_whitelist_signature_serialize
SECP256K1_API int secp256k1_whitelist_signature_serialize(const secp256k1_context2 *ctx, unsigned char *output, size_t *output_len, const secp256k1_whitelist_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize a whitelist signature.
Definition: main_impl.h:157
test_whitelist_end_to_end
void test_whitelist_end_to_end(const size_t n_keys)
Definition: tests_impl.h:12
CHECK
#define CHECK(cond)
Definition: util.h:43