Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
tests_exhaustive.c
Go to the documentation of this file.
1/***********************************************************************
2 * Copyright (c) 2016 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#if defined HAVE_CONFIG_H
8#include "libsecp256k1-config.h"
9#endif
10
11#include <stdio.h>
12#include <stdlib.h>
13
14#include <time.h>
15
16#undef USE_ECMULT_STATIC_PRECOMPUTATION
17
18#ifndef EXHAUSTIVE_TEST_ORDER
19/* see group_impl.h for allowable values */
20#define EXHAUSTIVE_TEST_ORDER 13
21#endif
22
23#include "include/secp256k1.h"
24#include "assumptions.h"
25#include "group.h"
26#include "secp256k1.c"
27#include "testrand_impl.h"
28
29static int count = 2;
30
33 CHECK(a->infinity == b->infinity);
34 if (a->infinity) {
35 return;
36 }
39}
40
44 CHECK(a->infinity == b->infinity);
45 if (a->infinity) {
46 return;
47 }
48 /* Check a.x * b.z^2 == b.x && a.y * b.z^3 == b.y, to avoid inverses. */
49 secp256k1_fe_sqr(&z2s, &b->z);
50 secp256k1_fe_mul(&u1, &a->x, &z2s);
52 secp256k1_fe_mul(&s1, &a->y, &z2s); secp256k1_fe_mul(&s1, &s1, &b->z);
56}
57
59 unsigned char bin[32];
60 do {
62 if (secp256k1_fe_set_b32(x, bin)) {
63 return;
64 }
65 } while(1);
66}
71
73 if (num_cores == 1) return 0;
74 *iter += 0xe7037ed1a0b428dbULL;
75 return ((((uint32_t)*iter ^ (*iter >> 32)) * num_cores) >> 32) != this_core;
76}
77
78int secp256k1_nonce_function_smallint(unsigned char *nonce32, const unsigned char *msg32,
79 const unsigned char *key32, const unsigned char *algo16,
80 void *data, unsigned int attempt) {
82 int *idata = data;
83 (void)msg32;
84 (void)key32;
85 (void)algo16;
86 /* Some nonces cannot be used because they'd cause s and/or r to be zero.
87 * The signing function has retry logic here that just re-calls the nonce
88 * function with an increased `attempt`. So if attempt > 0 this means we
89 * need to change the nonce to avoid an infinite loop. */
90 if (attempt > 0) {
92 }
95 return 1;
96}
97
106
108 int i, j;
109 uint64_t iter = 0;
110
111 /* Sanity-check (and check infinity functions) */
114 for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) {
117 }
118
119 /* Check all addition formulae */
120 for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
122 if (skip_section(&iter)) continue;
124 for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
126 secp256k1_gej tmp;
127 /* add_var */
130 /* add_ge */
131 if (j > 0) {
132 secp256k1_gej_add_ge(&tmp, &groupj[i], &group[j]);
134 }
135 /* add_ge_var */
138 /* add_zinv_var */
139 zless_gej.infinity = groupj[j].infinity;
140 zless_gej.x = groupj[j].x;
141 zless_gej.y = groupj[j].y;
144 }
145 }
146
147 /* Check doubling */
148 for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
149 secp256k1_gej tmp;
150 secp256k1_gej_double(&tmp, &groupj[i]);
151 ge_equals_gej(&group[(2 * i) % EXHAUSTIVE_TEST_ORDER], &tmp);
153 ge_equals_gej(&group[(2 * i) % EXHAUSTIVE_TEST_ORDER], &tmp);
154 }
155
156 /* Check negation */
157 for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) {
158 secp256k1_ge tmp;
160 secp256k1_ge_neg(&tmp, &group[i]);
164 }
165}
166
168 int i, j, r_log;
169 uint64_t iter = 0;
170 for (r_log = 1; r_log < EXHAUSTIVE_TEST_ORDER; r_log++) {
171 for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
172 if (skip_section(&iter)) continue;
173 for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
174 secp256k1_gej tmp;
178
181
182 if (i > 0) {
183 secp256k1_ecmult_const(&tmp, &group[i], &ng, 256);
185 }
186 }
187 }
188 }
189}
190
191typedef struct {
195
196static int ecmult_multi_callback(secp256k1_scalar *sc, secp256k1_ge *pt, size_t idx, void *cbdata) {
198 *sc = data->sc[idx];
199 *pt = data->pt[idx];
200 return 1;
201}
202
204 int i, j, k, x, y;
205 uint64_t iter = 0;
207 for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
208 for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
209 for (k = 0; k < EXHAUSTIVE_TEST_ORDER; k++) {
210 for (x = 0; x < EXHAUSTIVE_TEST_ORDER; x++) {
211 if (skip_section(&iter)) continue;
212 for (y = 0; y < EXHAUSTIVE_TEST_ORDER; y++) {
213 secp256k1_gej tmp;
216
217 secp256k1_scalar_set_int(&data.sc[0], i);
218 secp256k1_scalar_set_int(&data.sc[1], j);
220 data.pt[0] = group[x];
221 data.pt[1] = group[y];
222
224 ge_equals_gej(&group[(i * x + j * y + k) % EXHAUSTIVE_TEST_ORDER], &tmp);
225 }
226 }
227 }
228 }
229 }
231}
232
233void r_from_k(secp256k1_scalar *r, const secp256k1_ge *group, int k, int* overflow) {
234 secp256k1_fe x;
235 unsigned char x_bin[32];
237 x = group[k].x;
241}
242
244 int s, r, msg, key;
245 uint64_t iter = 0;
246 for (s = 1; s < EXHAUSTIVE_TEST_ORDER; s++) {
247 for (r = 1; r < EXHAUSTIVE_TEST_ORDER; r++) {
248 for (msg = 1; msg < EXHAUSTIVE_TEST_ORDER; msg++) {
249 for (key = 1; key < EXHAUSTIVE_TEST_ORDER; key++) {
255 int k, should_verify;
256 unsigned char msg32[32];
257
258 if (skip_section(&iter)) continue;
259
264
265 /* Verify by hand */
266 /* Run through every k value that gives us this r and check that *one* works.
267 * Note there could be none, there could be multiple, ECDSA is weird. */
268 should_verify = 0;
269 for (k = 0; k < EXHAUSTIVE_TEST_ORDER; k++) {
272 if (r_s == check_x_s) {
278 }
279 }
280 /* nb we have a "high s" rule */
282
283 /* Verify by calling verify */
290 }
291 }
292 }
293 }
294}
295
297 int i, j, k;
298 uint64_t iter = 0;
299
300 /* Loop */
301 for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) { /* message */
302 for (j = 1; j < EXHAUSTIVE_TEST_ORDER; j++) { /* key */
303 if (skip_section(&iter)) continue;
304 for (k = 1; k < EXHAUSTIVE_TEST_ORDER; k++) { /* nonce */
305 const int starting_k = k;
307 secp256k1_scalar sk, msg, r, s, expected_r;
308 unsigned char sk32[32], msg32[32];
313
315
317 /* Note that we compute expected_r *after* signing -- this is important
318 * because our nonce-computing function function might change k during
319 * signing. */
321 CHECK(r == expected_r);
322 CHECK((k * s) % EXHAUSTIVE_TEST_ORDER == (i + r * j) % EXHAUSTIVE_TEST_ORDER ||
324
325 /* Overflow means we've tried every possible nonce */
326 if (k < starting_k) {
327 break;
328 }
329 }
330 }
331 }
332
333 /* We would like to verify zero-knowledge here by counting how often every
334 * possible (s, r) tuple appears, but because the group order is larger
335 * than the field order, when coercing the x-values to scalar values, some
336 * appear more often than others, so we are actually not zero-knowledge.
337 * (This effect also appears in the real code, but the difference is on the
338 * order of 1/2^128th the field order, so the deviation is not useful to a
339 * computationally bounded attacker.)
340 */
341}
342
343#ifdef ENABLE_MODULE_RECOVERY
345#endif
346
347#ifdef ENABLE_MODULE_EXTRAKEYS
349#endif
350
351#ifdef ENABLE_MODULE_SCHNORRSIG
353#endif
354
355int main(int argc, char** argv) {
356 int i;
359 unsigned char rand32[32];
361
362 /* Disable buffering for stdout to improve reliability of getting
363 * diagnostic information. Happens right at the start of main because
364 * setbuf must be used before any other operation on the stream. */
366 /* Also disable buffering for stderr because it's not guaranteed that it's
367 * unbuffered on all systems. */
369
370 printf("Exhaustive tests for order %lu\n", (unsigned long)EXHAUSTIVE_TEST_ORDER);
371
372 /* find iteration count */
373 if (argc > 1) {
374 count = strtol(argv[1], NULL, 0);
375 }
376 printf("test count = %i\n", count);
377
378 /* find random seed */
380
381 /* set up split processing */
382 if (argc > 4) {
383 num_cores = strtol(argv[3], NULL, 0);
384 this_core = strtol(argv[4], NULL, 0);
386 fprintf(stderr, "Usage: %s [count] [seed] [numcores] [thiscore]\n", argv[0]);
387 return 1;
388 }
389 printf("running tests for core %lu (out of [0..%lu])\n", (unsigned long)this_core, (unsigned long)num_cores - 1);
390 }
391
392 while (count--) {
393 /* Build context */
395 secp256k1_testrand256(rand32);
397
398 /* Generate the entire group */
401 for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) {
404 if (count != 0) {
405 /* Set a different random z-value for each Jacobian point, except z=1
406 is used in the last iteration. */
407 secp256k1_fe z;
408 random_fe(&z);
410 }
411
412 /* Verify against ecmult_gen */
413 {
417
421
422 CHECK(group[i].infinity == 0);
423 CHECK(generated.infinity == 0);
426 }
427 }
428
429 /* Run the tests */
436
437#ifdef ENABLE_MODULE_RECOVERY
439#endif
440#ifdef ENABLE_MODULE_EXTRAKEYS
442#endif
443#ifdef ENABLE_MODULE_SCHNORRSIG
445#endif
446
448 }
449
451
452 printf("no problems found\n");
453 return 0;
454}
int main(void)
Definition bench.c:157
secp256k1_context * ctx
static void secp256k1_ecmult(const secp256k1_ecmult_context *ctx, secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_scalar *na, const secp256k1_scalar *ng)
Double multiply: R = na*A + ng*G.
static int secp256k1_ecmult_multi_var(const secp256k1_callback *error_callback, const secp256k1_ecmult_context *ctx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n)
Multi-multiply: R = inp_g_sc * G + sum_i ni * Ai.
static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, const secp256k1_scalar *q, int bits)
Multiply: R = q*A (in constant-time) Here bits should be set to the maximum bitlength of the absolute...
static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp256k1_gej *r, const secp256k1_scalar *a)
Multiply with the generator: R = a*G.
static void test_exhaustive_extrakeys(const secp256k1_context *ctx, const secp256k1_ge *group)
static void secp256k1_fe_normalize_weak(secp256k1_fe *r)
Weakly normalize a field element: reduce its magnitude to 1, but don't fully normalize.
static int secp256k1_fe_equal_var(const secp256k1_fe *a, const secp256k1_fe *b)
Same as secp256k1_fe_equal, but may be variable time.
static void secp256k1_fe_inv(secp256k1_fe *r, const secp256k1_fe *a)
Sets a field element to be the (modular) inverse of another.
static void secp256k1_fe_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe *SECP256K1_RESTRICT b)
Sets a field element to be the product of two others.
static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a)
Set a field element equal to 32-byte big endian value.
static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a)
Sets a field element to be the square of another.
static void secp256k1_fe_normalize(secp256k1_fe *r)
Field element module.
static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a)
Convert a field element to a 32-byte big endian value.
static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr)
Set r equal to the double of a.
static void secp256k1_gej_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, const secp256k1_fe *bzinv)
Set r equal to the sum of a and b (with the inverse of b's Z coordinate passed as bzinv).
static void secp256k1_ge_mul_lambda(secp256k1_ge *r, const secp256k1_ge *a)
Set r to be equal to lambda times a, where lambda is chosen in a way such that this is very fast.
static void secp256k1_gej_set_infinity(secp256k1_gej *r)
Set a group element (jacobian) equal to the point at infinity.
static int secp256k1_gej_is_infinity(const secp256k1_gej *a)
Check whether a group element is the point at infinity.
static void secp256k1_gej_add_ge_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, secp256k1_fe *rzr)
Set r equal to the sum of a and b (with b given in affine coordinates).
static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b)
Set r equal to the sum of a and b (with b given in affine coordinates, and not infinity).
static void secp256k1_gej_add_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_gej *b, secp256k1_fe *rzr)
Set r equal to the sum of a and b.
static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *b)
Rescale a jacobian point by b which must be non-zero.
static void secp256k1_ge_set_gej(secp256k1_ge *r, secp256k1_gej *a)
Set a group element equal to another which is given in jacobian coordinates.
static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a)
Set r equal to the inverse of a (i.e., mirrored around the X axis)
static int secp256k1_ge_is_infinity(const secp256k1_ge *a)
Check whether a group element is the point at infinity.
static void secp256k1_gej_double(secp256k1_gej *r, const secp256k1_gej *a)
Set r equal to the double of a.
static void secp256k1_gej_neg(secp256k1_gej *r, const secp256k1_gej *a)
Set r equal to the inverse of a (i.e., mirrored around the X axis)
static const secp256k1_ge secp256k1_ge_const_g
Generator for secp256k1, value 'g' defined in "Standards for Efficient Cryptography" (SEC2) 2....
Definition group_impl.h:52
SchnorrSig sig
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 test_exhaustive_recovery(const secp256k1_context *ctx, const secp256k1_ge *group)
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_set_int(secp256k1_scalar *r, unsigned int v)
Set a scalar to an unsigned integer.
static int secp256k1_scalar_eq(const secp256k1_scalar *a, const secp256k1_scalar *b)
Compare two scalars.
static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar *a)
Convert a scalar to a byte array.
static int secp256k1_scalar_add(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
Add two scalars together (modulo the group order).
static void secp256k1_scalar_mul(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
Multiply two scalars (modulo the group order).
static int secp256k1_scalar_is_high(const secp256k1_scalar *a)
Check whether a scalar is higher than the group order divided by 2.
static void test_exhaustive_schnorrsig(const secp256k1_context *ctx)
static void secp256k1_scratch_destroy(const secp256k1_callback *error_callback, secp256k1_scratch *scratch)
static secp256k1_scratch * secp256k1_scratch_create(const secp256k1_callback *error_callback, size_t max_size)
#define CHECK(cond)
Definition util.h:53
static void secp256k1_ecdsa_signature_save(secp256k1_ecdsa_signature *sig, const secp256k1_scalar *r, const secp256k1_scalar *s)
Definition secp256k1.c:333
static void secp256k1_pubkey_save(secp256k1_pubkey *pubkey, secp256k1_ge *ge)
Definition secp256k1.c:264
static void secp256k1_ecdsa_signature_load(const secp256k1_context *ctx, secp256k1_scalar *r, secp256k1_scalar *s, const secp256k1_ecdsa_signature *sig)
Definition secp256k1.c:319
#define SECP256K1_CONTEXT_SIGN
Definition secp256k1.h:171
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32) SECP256K1_ARG_NONNULL(1)
Updates the context randomization to protect against side-channel leakage.
Definition secp256k1.c:730
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 int secp256k1_ecdsa_sign(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create an ECDSA signature.
Definition secp256k1.c:535
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(const secp256k1_context *ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const secp256k1_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Verify an ECDSA signature.
Definition secp256k1.c:424
#define SECP256K1_INLINE
Definition secp256k1.h:124
#define SECP256K1_CONTEXT_VERIFY
Flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size, and secp256k1_context...
Definition secp256k1.h:170
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition secp256k1.c:196
secp256k1_scalar * sc
Definition tests.c:3007
secp256k1_ge * pt
Definition tests.c:3008
secp256k1_callback error_callback
Definition secp256k1.c:73
secp256k1_ecmult_gen_context ecmult_gen_ctx
Definition secp256k1.c:71
secp256k1_ecmult_context ecmult_ctx
Definition secp256k1.c:70
Opaque data structured that holds a parsed ECDSA signature.
Definition secp256k1.h:80
A group element of the secp256k1 curve, in affine coordinates.
Definition group.h:13
A group element of the secp256k1 curve, in jacobian coordinates.
Definition group.h:23
Opaque data structure that holds a parsed and valid public key.
Definition secp256k1.h:67
A scalar modulo the group order of the secp256k1 curve.
Definition scalar_4x64.h:13
static void secp256k1_testrand256(unsigned char *b32)
Generate a pseudorandom 32-byte array.
static void secp256k1_testrand_init(const char *hexseed)
Initialize the test RNG using (hex encoded) array up to 16 bytes, or randomly if hexseed is NULL.
static void secp256k1_testrand_finish(void)
Print final test information.
void test_exhaustive_ecmult_multi(const secp256k1_context *ctx, const secp256k1_ge *group)
void test_exhaustive_sign(const secp256k1_context *ctx, const secp256k1_ge *group)
int secp256k1_nonce_function_smallint(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int attempt)
static uint32_t this_core
static SECP256K1_INLINE int skip_section(uint64_t *iter)
void ge_equals_ge(const secp256k1_ge *a, const secp256k1_ge *b)
stolen from tests.c
void ge_equals_gej(const secp256k1_ge *a, const secp256k1_gej *b)
void r_from_k(secp256k1_scalar *r, const secp256k1_ge *group, int k, int *overflow)
void test_exhaustive_addition(const secp256k1_ge *group, const secp256k1_gej *groupj)
void test_exhaustive_verify(const secp256k1_context *ctx, const secp256k1_ge *group)
static uint32_t num_cores
END stolen from tests.c.
static int ecmult_multi_callback(secp256k1_scalar *sc, secp256k1_ge *pt, size_t idx, void *cbdata)
void test_exhaustive_endomorphism(const secp256k1_ge *group)
static int count
void random_fe(secp256k1_fe *x)
void test_exhaustive_ecmult(const secp256k1_context *ctx, const secp256k1_ge *group, const secp256k1_gej *groupj)
#define EXHAUSTIVE_TEST_ORDER