Bitcoin ABC 0.33.6
P2P Digital Currency
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#include <stdio.h>
8#include <stdlib.h>
9#include <time.h>
10
11#ifndef EXHAUSTIVE_TEST_ORDER
12/* see group_impl.h for allowable values */
13#define EXHAUSTIVE_TEST_ORDER 13
14#endif
15
16/* These values of B are all values in [1, 8] that result in a curve with even order. */
17#define EXHAUSTIVE_TEST_CURVE_HAS_EVEN_ORDER (SECP256K1_B == 1 || SECP256K1_B == 6 || SECP256K1_B == 8)
18
19#ifdef USE_EXTERNAL_DEFAULT_CALLBACKS
20 #pragma message("Ignoring USE_EXTERNAL_CALLBACKS in exhaustive_tests.")
21 #undef USE_EXTERNAL_DEFAULT_CALLBACKS
22#endif
23#include "secp256k1.c"
24
25#include "../include/secp256k1.h"
26#include "assumptions.h"
27#include "group.h"
28#include "testrand_impl.h"
31#include "util.h"
32
33static int count = 2;
34
36static void ge_equals_ge(const secp256k1_ge *a, const secp256k1_ge *b) {
37 CHECK(a->infinity == b->infinity);
38 if (a->infinity) {
39 return;
40 }
43}
44
45static void ge_equals_gej(const secp256k1_ge *a, const secp256k1_gej *b) {
46 secp256k1_fe z2s;
47 secp256k1_fe u1, u2, s1, s2;
48 CHECK(a->infinity == b->infinity);
49 if (a->infinity) {
50 return;
51 }
52 /* Check a.x * b.z^2 == b.x && a.y * b.z^3 == b.y, to avoid inverses. */
53 secp256k1_fe_sqr(&z2s, &b->z);
54 secp256k1_fe_mul(&u1, &a->x, &z2s);
55 u2 = b->x; secp256k1_fe_normalize_weak(&u2);
56 secp256k1_fe_mul(&s1, &a->y, &z2s); secp256k1_fe_mul(&s1, &s1, &b->z);
57 s2 = b->y; secp256k1_fe_normalize_weak(&s2);
60}
61
62static void random_fe(secp256k1_fe *x) {
63 unsigned char bin[32];
64 do {
66 if (secp256k1_fe_set_b32_limit(x, bin)) {
67 return;
68 }
69 } while(1);
70}
71
73 int tries = 10;
74 while (--tries >= 0) {
75 random_fe(nz);
77 if (!secp256k1_fe_is_zero(nz)) {
78 break;
79 }
80 }
81 /* Infinitesimal probability of spurious failure here */
82 CHECK(tries >= 0);
83}
86static uint32_t num_cores = 1;
87static uint32_t this_core = 0;
88
89SECP256K1_INLINE static int skip_section(uint64_t* iter) {
90 if (num_cores == 1) return 0;
91 *iter += 0xe7037ed1a0b428dbULL;
92 return ((((uint32_t)*iter ^ (*iter >> 32)) * num_cores) >> 32) != this_core;
93}
94
95static int secp256k1_nonce_function_smallint(unsigned char *nonce32, const unsigned char *msg32,
96 const unsigned char *key32, const unsigned char *algo16,
97 void *data, unsigned int attempt) {
99 int *idata = data;
100 (void)msg32;
101 (void)key32;
102 (void)algo16;
103 /* Some nonces cannot be used because they'd cause s and/or r to be zero.
104 * The signing function has retry logic here that just re-calls the nonce
105 * function with an increased `attempt`. So if attempt > 0 this means we
106 * need to change the nonce to avoid an infinite loop. */
107 if (attempt > 0) {
108 *idata = (*idata + 1) % EXHAUSTIVE_TEST_ORDER;
109 }
110 secp256k1_scalar_set_int(&s, *idata);
111 secp256k1_scalar_get_b32(nonce32, &s);
112 return 1;
113}
114
116 int i;
117 for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
118 secp256k1_ge res;
120 ge_equals_ge(&group[i * EXHAUSTIVE_TEST_LAMBDA % EXHAUSTIVE_TEST_ORDER], &res);
121 }
122}
123
124static void test_exhaustive_addition(const secp256k1_ge *group, const secp256k1_gej *groupj) {
125 int i, j;
126 uint64_t iter = 0;
127
128 /* Sanity-check (and check infinity functions) */
130 CHECK(secp256k1_gej_is_infinity(&groupj[0]));
131 for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) {
133 CHECK(!secp256k1_gej_is_infinity(&groupj[i]));
134 }
135
136 /* Check all addition formulae */
137 for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
138 secp256k1_fe fe_inv;
139 if (skip_section(&iter)) continue;
140 secp256k1_fe_inv(&fe_inv, &groupj[j].z);
141 for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
142 secp256k1_ge zless_gej;
143 secp256k1_gej tmp;
144 /* add_var */
145 secp256k1_gej_add_var(&tmp, &groupj[i], &groupj[j], NULL);
146 ge_equals_gej(&group[(i + j) % EXHAUSTIVE_TEST_ORDER], &tmp);
147 /* add_ge */
148 if (j > 0) {
149 secp256k1_gej_add_ge(&tmp, &groupj[i], &group[j]);
150 ge_equals_gej(&group[(i + j) % EXHAUSTIVE_TEST_ORDER], &tmp);
151 }
152 /* add_ge_var */
153 secp256k1_gej_add_ge_var(&tmp, &groupj[i], &group[j], NULL);
154 ge_equals_gej(&group[(i + j) % EXHAUSTIVE_TEST_ORDER], &tmp);
155 /* add_zinv_var */
156 zless_gej.infinity = groupj[j].infinity;
157 zless_gej.x = groupj[j].x;
158 zless_gej.y = groupj[j].y;
159 secp256k1_gej_add_zinv_var(&tmp, &groupj[i], &zless_gej, &fe_inv);
160 ge_equals_gej(&group[(i + j) % EXHAUSTIVE_TEST_ORDER], &tmp);
161 }
162 }
163
164 /* Check doubling */
165 for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
166 secp256k1_gej tmp;
167 secp256k1_gej_double(&tmp, &groupj[i]);
168 ge_equals_gej(&group[(2 * i) % EXHAUSTIVE_TEST_ORDER], &tmp);
169 secp256k1_gej_double_var(&tmp, &groupj[i], NULL);
170 ge_equals_gej(&group[(2 * i) % EXHAUSTIVE_TEST_ORDER], &tmp);
171 }
172
173 /* Check negation */
174 for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) {
175 secp256k1_ge tmp;
176 secp256k1_gej tmpj;
177 secp256k1_ge_neg(&tmp, &group[i]);
179 secp256k1_gej_neg(&tmpj, &groupj[i]);
181 }
182}
183
184static void test_exhaustive_ecmult(const secp256k1_ge *group, const secp256k1_gej *groupj) {
185 int i, j, r_log;
186 uint64_t iter = 0;
187 for (r_log = 1; r_log < EXHAUSTIVE_TEST_ORDER; r_log++) {
188 for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
189 if (skip_section(&iter)) continue;
190 for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
191 secp256k1_gej tmp;
192 secp256k1_scalar na, ng;
195
196 secp256k1_ecmult(&tmp, &groupj[r_log], &na, &ng);
197 ge_equals_gej(&group[(i * r_log + j) % EXHAUSTIVE_TEST_ORDER], &tmp);
198
199 }
200 }
201 }
202
203 for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
204 for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
205 int ret;
206 secp256k1_gej tmp;
207 secp256k1_fe xn, xd, tmpf;
209
210 if (skip_section(&iter)) continue;
211
213
214 /* Test secp256k1_ecmult_const. */
215 secp256k1_ecmult_const(&tmp, &group[i], &ng);
216 ge_equals_gej(&group[(i * j) % EXHAUSTIVE_TEST_ORDER], &tmp);
217
218 if (i != 0 && j != 0) {
219 /* Test secp256k1_ecmult_const_xonly with all curve X coordinates, and xd=NULL. */
220 ret = secp256k1_ecmult_const_xonly(&tmpf, &group[i].x, NULL, &ng, 0);
221 CHECK(ret);
223
224 /* Test secp256k1_ecmult_const_xonly with all curve X coordinates, with random xd. */
226 secp256k1_fe_mul(&xn, &xd, &group[i].x);
227 ret = secp256k1_ecmult_const_xonly(&tmpf, &xn, &xd, &ng, 0);
228 CHECK(ret);
230 }
231 }
232 }
233}
234
235typedef struct {
239
240static int ecmult_multi_callback(secp256k1_scalar *sc, secp256k1_ge *pt, size_t idx, void *cbdata) {
241 ecmult_multi_data *data = (ecmult_multi_data*) cbdata;
242 *sc = data->sc[idx];
243 *pt = data->pt[idx];
244 return 1;
245}
246
248 int i, j, k, x, y;
249 uint64_t iter = 0;
251 for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
252 for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
253 for (k = 0; k < EXHAUSTIVE_TEST_ORDER; k++) {
254 for (x = 0; x < EXHAUSTIVE_TEST_ORDER; x++) {
255 if (skip_section(&iter)) continue;
256 for (y = 0; y < EXHAUSTIVE_TEST_ORDER; y++) {
257 secp256k1_gej tmp;
258 secp256k1_scalar g_sc;
260
261 secp256k1_scalar_set_int(&data.sc[0], i);
262 secp256k1_scalar_set_int(&data.sc[1], j);
263 secp256k1_scalar_set_int(&g_sc, k);
264 data.pt[0] = group[x];
265 data.pt[1] = group[y];
266
267 secp256k1_ecmult_multi_var(&ctx->error_callback, scratch, &tmp, &g_sc, ecmult_multi_callback, &data, 2);
268 ge_equals_gej(&group[(i * x + j * y + k) % EXHAUSTIVE_TEST_ORDER], &tmp);
269 }
270 }
271 }
272 }
273 }
275}
276
277static void r_from_k(secp256k1_scalar *r, const secp256k1_ge *group, int k, int* overflow) {
278 secp256k1_fe x;
279 unsigned char x_bin[32];
281 x = group[k].x;
283 secp256k1_fe_get_b32(x_bin, &x);
284 secp256k1_scalar_set_b32(r, x_bin, overflow);
285}
286
288 int s, r, msg, key;
289 uint64_t iter = 0;
290 for (s = 1; s < EXHAUSTIVE_TEST_ORDER; s++) {
291 for (r = 1; r < EXHAUSTIVE_TEST_ORDER; r++) {
292 for (msg = 1; msg < EXHAUSTIVE_TEST_ORDER; msg++) {
293 for (key = 1; key < EXHAUSTIVE_TEST_ORDER; key++) {
294 secp256k1_ge nonconst_ge;
297 secp256k1_scalar sk_s, msg_s, r_s, s_s;
298 secp256k1_scalar s_times_k_s, msg_plus_r_times_sk_s;
299 int k, should_verify;
300 unsigned char msg32[32];
301
302 if (skip_section(&iter)) continue;
303
307 secp256k1_scalar_set_int(&sk_s, key);
308
309 /* Verify by hand */
310 /* Run through every k value that gives us this r and check that *one* works.
311 * Note there could be none, there could be multiple, ECDSA is weird. */
312 should_verify = 0;
313 for (k = 0; k < EXHAUSTIVE_TEST_ORDER; k++) {
314 secp256k1_scalar check_x_s;
315 r_from_k(&check_x_s, group, k, NULL);
316 if (r_s == check_x_s) {
317 secp256k1_scalar_set_int(&s_times_k_s, k);
318 secp256k1_scalar_mul(&s_times_k_s, &s_times_k_s, &s_s);
319 secp256k1_scalar_mul(&msg_plus_r_times_sk_s, &r_s, &sk_s);
320 secp256k1_scalar_add(&msg_plus_r_times_sk_s, &msg_plus_r_times_sk_s, &msg_s);
321 should_verify |= secp256k1_scalar_eq(&s_times_k_s, &msg_plus_r_times_sk_s);
322 }
323 }
324 /* nb we have a "high s" rule */
325 should_verify &= !secp256k1_scalar_is_high(&s_s);
326
327 /* Verify by calling verify */
329 memcpy(&nonconst_ge, &group[sk_s], sizeof(nonconst_ge));
330 secp256k1_pubkey_save(&pk, &nonconst_ge);
331 secp256k1_scalar_get_b32(msg32, &msg_s);
332 CHECK(should_verify ==
333 secp256k1_ecdsa_verify(ctx, &sig, msg32, &pk));
334 }
335 }
336 }
337 }
338}
339
341 int i, j, k;
342 uint64_t iter = 0;
343
344 /* Loop */
345 for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) { /* message */
346 for (j = 1; j < EXHAUSTIVE_TEST_ORDER; j++) { /* key */
347 if (skip_section(&iter)) continue;
348 for (k = 1; k < EXHAUSTIVE_TEST_ORDER; k++) { /* nonce */
349 const int starting_k = k;
350 int ret;
352 secp256k1_scalar sk, msg, r, s, expected_r;
353 unsigned char sk32[32], msg32[32];
356 secp256k1_scalar_get_b32(sk32, &sk);
358
360 CHECK(ret == 1);
361
363 /* Note that we compute expected_r *after* signing -- this is important
364 * because our nonce-computing function function might change k during
365 * signing. */
366 r_from_k(&expected_r, group, k, NULL);
367 CHECK(r == expected_r);
368 CHECK((k * s) % EXHAUSTIVE_TEST_ORDER == (i + r * j) % EXHAUSTIVE_TEST_ORDER ||
370
371 /* Overflow means we've tried every possible nonce */
372 if (k < starting_k) {
373 break;
374 }
375 }
376 }
377 }
378
379 /* We would like to verify zero-knowledge here by counting how often every
380 * possible (s, r) tuple appears, but because the group order is larger
381 * than the field order, when coercing the x-values to scalar values, some
382 * appear more often than others, so we are actually not zero-knowledge.
383 * (This effect also appears in the real code, but the difference is on the
384 * order of 1/2^128th the field order, so the deviation is not useful to a
385 * computationally bounded attacker.)
386 */
387}
388
389#ifdef ENABLE_MODULE_RECOVERY
391#endif
392
393#ifdef ENABLE_MODULE_EXTRAKEYS
395#endif
396
397#ifdef ENABLE_MODULE_SCHNORRSIG
399#endif
400
401#ifdef ENABLE_MODULE_ELLSWIFT
403#endif
404
405int main(int argc, char** argv) {
406 int i;
409 unsigned char rand32[32];
411
412 /* Disable buffering for stdout to improve reliability of getting
413 * diagnostic information. Happens right at the start of main because
414 * setbuf must be used before any other operation on the stream. */
415 setbuf(stdout, NULL);
416 /* Also disable buffering for stderr because it's not guaranteed that it's
417 * unbuffered on all systems. */
418 setbuf(stderr, NULL);
419
420 printf("Exhaustive tests for order %lu\n", (unsigned long)EXHAUSTIVE_TEST_ORDER);
421
422 /* find iteration count */
423 if (argc > 1) {
424 count = strtol(argv[1], NULL, 0);
425 }
426 printf("test count = %i\n", count);
427
428 /* find random seed */
429 secp256k1_testrand_init(argc > 2 ? argv[2] : NULL);
430
431 /* set up split processing */
432 if (argc > 4) {
433 num_cores = strtol(argv[3], NULL, 0);
434 this_core = strtol(argv[4], NULL, 0);
435 if (num_cores < 1 || this_core >= num_cores) {
436 fprintf(stderr, "Usage: %s [count] [seed] [numcores] [thiscore]\n", argv[0]);
437 return 1;
438 }
439 printf("running tests for core %lu (out of [0..%lu])\n", (unsigned long)this_core, (unsigned long)num_cores - 1);
440 }
441
442 /* Recreate the ecmult{,_gen} tables using the right generator (as selected via EXHAUSTIVE_TEST_ORDER) */
445
446 while (count--) {
447 /* Build context */
449 secp256k1_testrand256(rand32);
451
452 /* Generate the entire group */
453 secp256k1_gej_set_infinity(&groupj[0]);
454 secp256k1_ge_set_gej(&group[0], &groupj[0]);
455 for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) {
456 secp256k1_gej_add_ge(&groupj[i], &groupj[i - 1], &secp256k1_ge_const_g);
457 secp256k1_ge_set_gej(&group[i], &groupj[i]);
458 if (count != 0) {
459 /* Set a different random z-value for each Jacobian point, except z=1
460 is used in the last iteration. */
461 secp256k1_fe z;
462 random_fe(&z);
463 secp256k1_gej_rescale(&groupj[i], &z);
464 }
465
466 /* Verify against ecmult_gen */
467 {
468 secp256k1_scalar scalar_i;
469 secp256k1_gej generatedj;
470 secp256k1_ge generated;
471
472 secp256k1_scalar_set_int(&scalar_i, i);
473 secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &generatedj, &scalar_i);
474 secp256k1_ge_set_gej(&generated, &generatedj);
475
476 CHECK(group[i].infinity == 0);
477 CHECK(generated.infinity == 0);
478 CHECK(secp256k1_fe_equal_var(&generated.x, &group[i].x));
479 CHECK(secp256k1_fe_equal_var(&generated.y, &group[i].y));
480 }
481 }
482
483 /* Run the tests */
490
491#ifdef ENABLE_MODULE_RECOVERY
493#endif
494#ifdef ENABLE_MODULE_EXTRAKEYS
496#endif
497#ifdef ENABLE_MODULE_SCHNORRSIG
499#endif
500#ifdef ENABLE_MODULE_ELLSWIFT
501 /* The ellswift algorithm does have additional edge cases when operating on
502 * curves of even order, which are not included in the code as secp256k1 is
503 * of odd order. Skip the ellswift tests if the used exhaustive tests curve
504 * is even-ordered accordingly. */
505 #if !EXHAUSTIVE_TEST_CURVE_HAS_EVEN_ORDER
507 #endif
508#endif
509
511 }
512
514
515 printf("no problems found\n");
516 return 0;
517}
static int secp256k1_ecmult_multi_var(const secp256k1_callback *error_callback, 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(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_scalar *na, const secp256k1_scalar *ng)
Double multiply: R = na*A + ng*G.
static void secp256k1_ecmult_compute_two_tables(secp256k1_ge_storage *table, secp256k1_ge_storage *table_128, int window_g, const secp256k1_ge *gen)
static int secp256k1_ecmult_const_xonly(secp256k1_fe *r, const secp256k1_fe *n, const secp256k1_fe *d, const secp256k1_scalar *q, int known_on_curve)
Same as secp256k1_ecmult_const, but takes in an x coordinate of the base point only,...
static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, const secp256k1_scalar *q)
Multiply: R = q*A (in constant-time for q)
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.
#define ECMULT_GEN_PREC_BITS
Definition: ecmult_gen.h:14
static void secp256k1_ecmult_gen_compute_table(secp256k1_ge_storage *table, const secp256k1_ge *gen, int bits)
static void test_exhaustive_ellswift(const secp256k1_context *ctx, const secp256k1_ge *group)
static void test_exhaustive_extrakeys(const secp256k1_context *ctx, const secp256k1_ge *group)
static int secp256k1_fe_equal_var(const secp256k1_fe *a, const secp256k1_fe *b)
Determine whether two field elements are equal, without constant-time guarantee.
#define secp256k1_fe_normalize_weak
Definition: field.h:79
#define secp256k1_fe_mul
Definition: field.h:94
#define secp256k1_fe_is_zero
Definition: field.h:85
#define secp256k1_fe_set_b32_limit
Definition: field.h:89
#define secp256k1_fe_get_b32
Definition: field.h:90
#define secp256k1_fe_inv
Definition: field.h:99
#define secp256k1_fe_sqr
Definition: field.h:95
#define secp256k1_fe_normalize
Definition: field.h:78
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
Definition: group_impl.h:70
secp256k1_context * ctx
Definition: bench_impl.h:13
void printf(const char *fmt, const Args &...args)
Format list of arguments to std::cout, according to the given format string.
Definition: tinyformat.h:1126
const secp256k1_ge_storage secp256k1_pre_g_128[ECMULT_TABLE_SIZE(WINDOW_G)]
const secp256k1_ge_storage secp256k1_pre_g[ECMULT_TABLE_SIZE(WINDOW_G)]
#define WINDOW_G
const secp256k1_ge_storage secp256k1_ecmult_gen_prec_table[ECMULT_GEN_PREC_N(ECMULT_GEN_PREC_BITS)][ECMULT_GEN_PREC_G(ECMULT_GEN_PREC_BITS)]
SchnorrSig sig
Definition: processor.cpp:537
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 SECP256K1_INLINE
Definition: util.h:48
#define CHECK(cond)
Definition: util.h:128
static void secp256k1_ecdsa_signature_save(secp256k1_ecdsa_signature *sig, const secp256k1_scalar *r, const secp256k1_scalar *s)
Definition: secp256k1.c:353
static void secp256k1_pubkey_save(secp256k1_pubkey *pubkey, secp256k1_ge *ge)
Definition: secp256k1.c:258
static void secp256k1_ecdsa_signature_load(const secp256k1_context *ctx, secp256k1_scalar *r, secp256k1_scalar *s, const secp256k1_ecdsa_signature *sig)
Definition: secp256k1.c:339
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 SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32) SECP256K1_ARG_NONNULL(1)
Randomizes the context to provide enhanced protection against side-channel leakage.
Definition: secp256k1.c:751
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
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:558
#define SECP256K1_CONTEXT_NONE
Context flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size,...
Definition: secp256k1.h:192
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:444
secp256k1_scalar * sc
Definition: tests.c:4625
secp256k1_ge * pt
Definition: tests.c:4626
secp256k1_callback error_callback
Definition: secp256k1.c:63
secp256k1_ecmult_gen_context ecmult_gen_ctx
Definition: secp256k1.c:61
Opaque data structured that holds a parsed ECDSA signature.
Definition: secp256k1.h:74
This field implementation represents the value as 10 uint32_t limbs in base 2^26.
Definition: field_10x26.h:14
A group element in affine coordinates on the secp256k1 curve, or occasionally on an isomorphic curve ...
Definition: group.h:16
int infinity
Definition: group.h:19
secp256k1_fe x
Definition: group.h:17
secp256k1_fe y
Definition: group.h:18
A group element of the secp256k1 curve, in jacobian coordinates.
Definition: group.h:28
secp256k1_fe y
Definition: group.h:30
secp256k1_fe x
Definition: group.h:29
int infinity
Definition: group.h:32
secp256k1_fe z
Definition: group.h:31
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:61
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.
static void ge_equals_ge(const secp256k1_ge *a, const secp256k1_ge *b)
stolen from tests.c
static void test_exhaustive_verify(const secp256k1_context *ctx, const secp256k1_ge *group)
static void test_exhaustive_endomorphism(const secp256k1_ge *group)
static void test_exhaustive_ecmult(const secp256k1_ge *group, const secp256k1_gej *groupj)
static uint32_t this_core
static void ge_equals_gej(const secp256k1_ge *a, const secp256k1_gej *b)
static SECP256K1_INLINE int skip_section(uint64_t *iter)
int main(int argc, char **argv)
static void r_from_k(secp256k1_scalar *r, const secp256k1_ge *group, int k, int *overflow)
static void test_exhaustive_ecmult_multi(const secp256k1_context *ctx, const secp256k1_ge *group)
static void random_fe(secp256k1_fe *x)
static void random_fe_non_zero(secp256k1_fe *nz)
static void test_exhaustive_sign(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)
static int count
static void test_exhaustive_addition(const secp256k1_ge *group, const secp256k1_gej *groupj)
#define EXHAUSTIVE_TEST_ORDER
static 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)