Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
tests_impl.h
Go to the documentation of this file.
1/***********************************************************************
2 * Copyright (c) 2020 Jonas Nick *
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_EXTRAKEYS_TESTS_H
8#define SECP256K1_MODULE_EXTRAKEYS_TESTS_H
9
10#include "secp256k1_extrakeys.h"
11
18
25 unsigned char sk[32];
26 unsigned char xy_sk[32];
27 unsigned char buf32[32];
28 unsigned char ones32[32];
29 unsigned char zeros64[64] = { 0 };
30 int pk_parity;
31 int i;
32
33 int ecount;
37
39 memset(ones32, 0xFF, 32);
41 CHECK(secp256k1_ec_pubkey_create(sign, &pk, sk) == 1);
43
44 /* Test xonly_pubkey_from_pubkey */
45 ecount = 0;
50 CHECK(ecount == 1);
53 CHECK(ecount == 2);
54 memset(&pk, 0, sizeof(pk));
56 CHECK(ecount == 3);
57
58 /* Choose a secret key such that the resulting pubkey and xonly_pubkey match. */
59 memset(sk, 0, sizeof(sk));
60 sk[0] = 1;
63 CHECK(secp256k1_memcmp_var(&pk, &xonly_pk, sizeof(pk)) == 0);
64 CHECK(pk_parity == 0);
65
66 /* Choose a secret key such that pubkey and xonly_pubkey are each others
67 * negation. */
68 sk[0] = 2;
71 CHECK(secp256k1_memcmp_var(&xonly_pk, &pk, sizeof(xonly_pk)) != 0);
72 CHECK(pk_parity == 1);
75 CHECK(secp256k1_fe_equal(&pk1.x, &pk2.x) == 1);
76 secp256k1_fe_negate(&y, &pk2.y, 1);
77 CHECK(secp256k1_fe_equal(&pk1.y, &y) == 1);
78
79 /* Test xonly_pubkey_serialize and xonly_pubkey_parse */
80 ecount = 0;
82 CHECK(ecount == 1);
85 CHECK(ecount == 2);
86 {
87 /* A pubkey filled with 0s will fail to serialize due to pubkey_load
88 * special casing. */
90 memset(&pk_tmp, 0, sizeof(pk_tmp));
92 }
93 /* pubkey_load called illegal callback */
94 CHECK(ecount == 3);
95
97 ecount = 0;
99 CHECK(ecount == 1);
101 CHECK(ecount == 2);
102
103 /* Serialization and parse roundtrip */
108
109 /* Test parsing invalid field elements */
110 memset(&xonly_pk, 1, sizeof(xonly_pk));
111 /* Overflowing field element */
114 memset(&xonly_pk, 1, sizeof(xonly_pk));
115 /* There's no point with x-coordinate 0 on secp256k1 */
118 /* If a random 32-byte string can not be parsed with ec_pubkey_parse
119 * (because interpreted as X coordinate it does not correspond to a point on
120 * the curve) then xonly_pubkey_parse should fail as well. */
121 for (i = 0; i < count; i++) {
122 unsigned char rand33[33];
125 if (!secp256k1_ec_pubkey_parse(ctx, &pk, rand33, 33)) {
126 memset(&xonly_pk, 1, sizeof(xonly_pk));
129 } else {
131 }
132 }
133 CHECK(ecount == 2);
134
138}
139
141 unsigned char pk1_ser[32] = {
142 0x58, 0x84, 0xb3, 0xa2, 0x4b, 0x97, 0x37, 0x88, 0x92, 0x38, 0xa6, 0x26, 0x62, 0x52, 0x35, 0x11,
143 0xd0, 0x9a, 0xa1, 0x1b, 0x80, 0x0b, 0x5e, 0x93, 0x80, 0x26, 0x11, 0xef, 0x67, 0x4b, 0xd9, 0x23
144 };
145 const unsigned char pk2_ser[32] = {
146 0xde, 0x36, 0x0e, 0x87, 0x59, 0x8f, 0x3c, 0x01, 0x36, 0x2a, 0x2a, 0xb8, 0xc6, 0xf4, 0x5e, 0x4d,
147 0xb2, 0xc2, 0xd5, 0x03, 0xa7, 0xf9, 0xf1, 0x4f, 0xa8, 0xfa, 0x95, 0xa8, 0xe9, 0x69, 0x76, 0x1c
148 };
151 int ecount = 0;
153
156
158 CHECK(ecount == 1);
160 CHECK(ecount == 2);
165 CHECK(ecount == 2);
166 memset(&pk1, 0, sizeof(pk1)); /* illegal pubkey */
168 CHECK(ecount == 3);
170 CHECK(ecount == 5);
172 CHECK(ecount == 6);
173
175}
176
178 unsigned char zeros64[64] = { 0 };
179 unsigned char overflows[32];
180 unsigned char sk[32];
184 int pk_parity;
185 unsigned char tweak[32];
186 int i;
187
188 int ecount;
192
193 memset(overflows, 0xff, sizeof(overflows));
198
199 ecount = 0;
201 CHECK(ecount == 1);
203 CHECK(ecount == 2);
206 CHECK(ecount == 3);
208 CHECK(ecount == 4);
209 /* NULL internal_xonly_pk zeroes the output_pk */
212 CHECK(ecount == 5);
213 /* NULL tweak zeroes the output_pk */
215
216 /* Invalid tweak zeroes the output_pk */
219
220 /* A zero tweak is fine */
222
223 /* Fails if the resulting key was infinity */
224 for (i = 0; i < count; i++) {
226 /* Because sk may be negated before adding, we need to try with tweak =
227 * sk as well as tweak = -sk. */
234 }
235
236 /* Invalid pk with a valid tweak */
239 ecount = 0;
241 CHECK(ecount == 1);
243
247}
248
250 unsigned char zeros64[64] = { 0 };
251 unsigned char overflows[32];
252 unsigned char sk[32];
257 unsigned char output_pk32[32];
258 unsigned char buf32[32];
259 int pk_parity;
260 unsigned char tweak[32];
261
262 int ecount;
266
267 memset(overflows, 0xff, sizeof(overflows));
272
273 ecount = 0;
278 CHECK(ecount == 1);
280 CHECK(ecount == 2);
283 CHECK(ecount == 3);
284 /* invalid pk_parity value */
286 CHECK(ecount == 3);
288 CHECK(ecount == 4);
290 CHECK(ecount == 5);
291
292 memset(tweak, 1, sizeof(tweak));
298
299 /* Wrong pk_parity */
301 /* Wrong public key */
304
305 /* Overflowing tweak not allowed */
309 CHECK(ecount == 5);
310
314}
315
316/* Starts with an initial pubkey and recursively creates N_PUBKEYS - 1
317 * additional pubkeys by calling tweak_add. Then verifies every tweak starting
318 * from the last pubkey. */
319#define N_PUBKEYS 32
321 unsigned char sk[32];
323 unsigned char pk_serialized[32];
324 unsigned char tweak[N_PUBKEYS - 1][32];
325 int i;
326
328 CHECK(secp256k1_ec_pubkey_create(ctx, &pk[0], sk) == 1);
329 /* Add tweaks */
330 for (i = 0; i < N_PUBKEYS - 1; i++) {
332 memset(tweak[i], i + 1, sizeof(tweak[i]));
335 }
336
337 /* Verify tweaks */
338 for (i = N_PUBKEYS - 1; i > 0; i--) {
340 int pk_parity;
345 }
346}
347#undef N_PUBKEYS
348
349void test_keypair(void) {
350 unsigned char sk[32];
351 unsigned char sk_tmp[32];
352 unsigned char zeros96[96] = { 0 };
353 unsigned char overflows[32];
358 int ecount;
362
363 CHECK(sizeof(zeros96) == sizeof(keypair));
364 memset(overflows, 0xFF, sizeof(overflows));
365
366 /* Test keypair_create */
367 ecount = 0;
371 CHECK(ecount == 1);
372 CHECK(secp256k1_keypair_create(verify, &keypair, sk) == 0);
374 CHECK(ecount == 2);
377 CHECK(ecount == 3);
380 CHECK(ecount == 4);
381
382 /* Invalid secret key */
387
388 /* Test keypair_pub */
389 ecount = 0;
394 CHECK(ecount == 1);
396 CHECK(ecount == 2);
397 CHECK(secp256k1_memcmp_var(zeros96, &pk, sizeof(pk)) == 0);
398
399 /* Using an invalid keypair is fine for keypair_pub */
400 memset(&keypair, 0, sizeof(keypair));
402 CHECK(secp256k1_memcmp_var(zeros96, &pk, sizeof(pk)) == 0);
403
404 /* keypair holds the same pubkey as pubkey_create */
405 CHECK(secp256k1_ec_pubkey_create(sign, &pk, sk) == 1);
408 CHECK(secp256k1_memcmp_var(&pk, &pk_tmp, sizeof(pk)) == 0);
409
411 ecount = 0;
416 CHECK(ecount == 1);
419 CHECK(ecount == 2);
421 /* Using an invalid keypair will set the xonly_pk to 0 (first reset
422 * xonly_pk). */
424 memset(&keypair, 0, sizeof(keypair));
427 CHECK(ecount == 3);
428
430 CHECK(secp256k1_ec_pubkey_create(sign, &pk, sk) == 1);
434 CHECK(secp256k1_memcmp_var(&xonly_pk, &xonly_pk_tmp, sizeof(pk)) == 0);
436
437 /* Test keypair_seckey */
438 ecount = 0;
443 CHECK(ecount == 1);
445 CHECK(ecount == 2);
447
448 /* keypair returns the same seckey it got */
451 CHECK(secp256k1_memcmp_var(sk, sk_tmp, sizeof(sk_tmp)) == 0);
452
453
454 /* Using an invalid keypair is fine for keypair_seckey */
455 memset(&keypair, 0, sizeof(keypair));
458
462}
463
465 unsigned char sk[32];
467 unsigned char overflows[32];
468 unsigned char zeros96[96] = { 0 };
469 unsigned char tweak[32];
470 int i;
471 int ecount = 0;
475
476 CHECK(sizeof(zeros96) == sizeof(keypair));
479 memset(overflows, 0xFF, 32);
481
483 CHECK(ecount == 1);
485 CHECK(ecount == 2);
488 CHECK(ecount == 3);
490 CHECK(ecount == 4);
491 /* This does not set the keypair to zeroes */
493
494 /* Invalid tweak zeroes the keypair */
498
499 /* A zero tweak is fine */
502
503 /* Fails if the resulting keypair was (sk=0, pk=infinity) */
504 for (i = 0; i < count; i++) {
509 memcpy(&keypair_tmp, &keypair, sizeof(keypair));
510 /* Because sk may be negated before adding, we need to try with tweak =
511 * sk as well as tweak = -sk. */
519 }
520
521 /* Invalid keypair with a valid tweak */
522 memset(&keypair, 0, sizeof(keypair));
524 ecount = 0;
526 CHECK(ecount == 1);
528 /* Only seckey part of keypair invalid */
530 memset(&keypair, 0, 32);
532 CHECK(ecount == 2);
533 /* Only pubkey part of keypair invalid */
535 memset(&keypair.data[32], 0, 64);
537 CHECK(ecount == 3);
538
539 /* Check that the keypair_tweak_add implementation is correct */
541 for (i = 0; i < count; i++) {
546 unsigned char pk32[32];
547 unsigned char sk32[32];
548 int pk_parity;
549
554
555 /* Check that it passes xonly_pubkey_tweak_add_check */
558
559 /* Check that the resulting pubkey matches xonly_pubkey_tweak_add */
563
564 /* Check that the secret key in the keypair is tweaked correctly */
568 }
572}
573
575 /* xonly key test cases */
581
582 /* keypair tests */
583 test_keypair();
585}
586
587#endif
secp256k1_context * ctx
int flags
void test_xonly_pubkey(void)
Definition tests_impl.h:19
void test_keypair_add(void)
Definition tests_impl.h:464
void test_xonly_pubkey_tweak_check(void)
Definition tests_impl.h:249
void run_extrakeys_tests(void)
Definition tests_impl.h:574
void test_xonly_pubkey_tweak(void)
Definition tests_impl.h:177
static secp256k1_context * api_test_context(int flags, int *ecount)
Definition tests_impl.h:12
void test_xonly_pubkey_tweak_recursive(void)
Definition tests_impl.h:320
#define N_PUBKEYS
Definition tests_impl.h:319
void test_xonly_pubkey_comparison(void)
Definition tests_impl.h:140
void test_keypair(void)
Definition tests_impl.h:349
static void secp256k1_fe_negate(secp256k1_fe *r, const secp256k1_fe *a, int m)
Set a field element equal to the additive inverse of another.
static int secp256k1_fe_equal(const secp256k1_fe *a, const secp256k1_fe *b)
Compare two field elements.
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *bin, int *overflow)
Set a scalar from a big endian byte array.
static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar *a)
Convert a scalar to a byte array.
static void secp256k1_scalar_negate(secp256k1_scalar *r, const secp256k1_scalar *a)
Compute the complement of a scalar (modulo the group order).
static SECP256K1_INLINE int secp256k1_memcmp_var(const void *s1, const void *s2, size_t n)
Semantics like memcmp.
Definition util.h:224
#define CHECK(cond)
Definition util.h:53
static int secp256k1_pubkey_load(const secp256k1_context *ctx, secp256k1_ge *ge, const secp256k1_pubkey *pubkey)
Definition secp256k1.c:245
#define SECP256K1_CONTEXT_SIGN
Definition secp256k1.h:174
#define SECP256K1_TAG_PUBKEY_EVEN
Prefix byte used to tag various encoded curvepoints for specific purposes.
Definition secp256k1.h:183
SECP256K1_API void secp256k1_context_set_error_callback(secp256k1_context *ctx, void(*fun)(const char *message, void *data), const void *data) SECP256K1_ARG_NONNULL(1)
Set a callback function to be called when an internal consistency check fails.
Definition secp256k1.c:212
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:152
SECP256K1_API void secp256k1_context_set_illegal_callback(secp256k1_context *ctx, void(*fun)(const char *message, void *data), const void *data) SECP256K1_ARG_NONNULL(1)
Set a callback function to be called when an illegal argument is passed to an API call.
Definition secp256k1.c:203
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:278
#define SECP256K1_CONTEXT_NONE
Definition secp256k1.h:176
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(const secp256k1_context *ctx, secp256k1_pubkey *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.c:599
#define SECP256K1_CONTEXT_VERIFY
Flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size, and secp256k1_context...
Definition secp256k1.h:173
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition secp256k1.c:196
SECP256K1_API int secp256k1_xonly_pubkey_cmp(const secp256k1_context *ctx, const secp256k1_xonly_pubkey *pk1, const secp256k1_xonly_pubkey *pk2) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compare two x-only public keys using lexicographic order.
Definition main_impl.h:58
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_pub(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const secp256k1_keypair *keypair) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Get the public key from a keypair.
Definition main_impl.h:225
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_sec(const secp256k1_context *ctx, unsigned char *seckey, const secp256k1_keypair *keypair) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Get the secret key from a keypair.
Definition main_impl.h:215
SECP256K1_API int secp256k1_xonly_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output32, const secp256k1_xonly_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Serialize an xonly_pubkey object into a 32-byte sequence.
Definition main_impl.h:43
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_tweak_add_check(const secp256k1_context *ctx, const unsigned char *tweaked_pubkey32, int tweaked_pk_parity, const secp256k1_xonly_pubkey *internal_pubkey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5)
Checks that a tweaked pubkey is the result of calling secp256k1_xonly_pubkey_tweak_add with internal_...
Definition main_impl.h:135
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_create(const secp256k1_context *ctx, secp256k1_keypair *keypair, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute the keypair for a secret key.
Definition main_impl.h:197
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_from_pubkey(const secp256k1_context *ctx, secp256k1_xonly_pubkey *xonly_pubkey, int *pk_parity, const secp256k1_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4)
Converts a secp256k1_pubkey into a secp256k1_xonly_pubkey.
Definition main_impl.h:98
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_xonly_tweak_add(const secp256k1_context *ctx, secp256k1_keypair *keypair, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a keypair by adding tweak32 to the secret key and updating the public key accordingly.
Definition main_impl.h:256
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_tweak_add(const secp256k1_context *ctx, secp256k1_pubkey *output_pubkey, const secp256k1_xonly_pubkey *internal_pubkey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Tweak an x-only public key by adding the generator multiplied with tweak32 to it.
Definition main_impl.h:117
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_xonly_pub(const secp256k1_context *ctx, secp256k1_xonly_pubkey *pubkey, int *pk_parity, const secp256k1_keypair *keypair) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4)
Get the x-only public key from a keypair.
Definition main_impl.h:235
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_xonly_pubkey_parse(const secp256k1_context *ctx, secp256k1_xonly_pubkey *pubkey, const unsigned char *input32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse a 32-byte sequence into a xonly_pubkey object.
Definition main_impl.h:21
A group element of the secp256k1 curve, in affine coordinates.
Definition group.h:13
Opaque data structure that holds a keypair consisting of a secret and a public key.
Opaque data structure that holds a parsed and valid public key.
Definition secp256k1.h:70
A scalar modulo the group order of the secp256k1 curve.
Definition scalar_4x64.h:13
Opaque data structure that holds a parsed and valid "x-only" public key.
static void secp256k1_testrand256(unsigned char *b32)
Generate a pseudorandom 32-byte array.
static void counting_illegal_callback_fn(const char *str, void *data)
Definition tests.c:34
static int count
Definition tests.c:31