1 /***********************************************************************
2  * Copyright (c) 2016 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 #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 
29 static int count = 2;
30 
31 /** stolen from tests.c */
ge_equals_ge(const secp256k1_ge * a,const secp256k1_ge * b)32 void ge_equals_ge(const secp256k1_ge *a, const secp256k1_ge *b) {
33     CHECK(a->infinity == b->infinity);
34     if (a->infinity) {
35         return;
36     }
37     CHECK(secp256k1_fe_equal_var(&a->x, &b->x));
38     CHECK(secp256k1_fe_equal_var(&a->y, &b->y));
39 }
40 
ge_equals_gej(const secp256k1_ge * a,const secp256k1_gej * b)41 void ge_equals_gej(const secp256k1_ge *a, const secp256k1_gej *b) {
42     secp256k1_fe z2s;
43     secp256k1_fe u1, u2, s1, s2;
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);
51     u2 = b->x; secp256k1_fe_normalize_weak(&u2);
52     secp256k1_fe_mul(&s1, &a->y, &z2s); secp256k1_fe_mul(&s1, &s1, &b->z);
53     s2 = b->y; secp256k1_fe_normalize_weak(&s2);
54     CHECK(secp256k1_fe_equal_var(&u1, &u2));
55     CHECK(secp256k1_fe_equal_var(&s1, &s2));
56 }
57 
random_fe(secp256k1_fe * x)58 void random_fe(secp256k1_fe *x) {
59     unsigned char bin[32];
60     do {
61         secp256k1_testrand256(bin);
62         if (secp256k1_fe_set_b32(x, bin)) {
63             return;
64         }
65     } while(1);
66 }
67 /** END stolen from tests.c */
68 
69 static uint32_t num_cores = 1;
70 static uint32_t this_core = 0;
71 
skip_section(uint64_t * iter)72 SECP256K1_INLINE static int skip_section(uint64_t* iter) {
73     if (num_cores == 1) return 0;
74     *iter += 0xe7037ed1a0b428dbULL;
75     return ((((uint32_t)*iter ^ (*iter >> 32)) * num_cores) >> 32) != this_core;
76 }
77 
secp256k1_nonce_function_smallint(unsigned char * nonce32,const unsigned char * msg32,const unsigned char * key32,const unsigned char * algo16,void * data,unsigned int attempt)78 int 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) {
81     secp256k1_scalar s;
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) {
91         *idata = (*idata + 1) % EXHAUSTIVE_TEST_ORDER;
92     }
93     secp256k1_scalar_set_int(&s, *idata);
94     secp256k1_scalar_get_b32(nonce32, &s);
95     return 1;
96 }
97 
test_exhaustive_endomorphism(const secp256k1_ge * group)98 void test_exhaustive_endomorphism(const secp256k1_ge *group) {
99     int i;
100     for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
101         secp256k1_ge res;
102         secp256k1_ge_mul_lambda(&res, &group[i]);
103         ge_equals_ge(&group[i * EXHAUSTIVE_TEST_LAMBDA % EXHAUSTIVE_TEST_ORDER], &res);
104     }
105 }
106 
test_exhaustive_addition(const secp256k1_ge * group,const secp256k1_gej * groupj)107 void test_exhaustive_addition(const secp256k1_ge *group, const secp256k1_gej *groupj) {
108     int i, j;
109     uint64_t iter = 0;
110 
111     /* Sanity-check (and check infinity functions) */
112     CHECK(secp256k1_ge_is_infinity(&group[0]));
113     CHECK(secp256k1_gej_is_infinity(&groupj[0]));
114     for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) {
115         CHECK(!secp256k1_ge_is_infinity(&group[i]));
116         CHECK(!secp256k1_gej_is_infinity(&groupj[i]));
117     }
118 
119     /* Check all addition formulae */
120     for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
121         secp256k1_fe fe_inv;
122         if (skip_section(&iter)) continue;
123         secp256k1_fe_inv(&fe_inv, &groupj[j].z);
124         for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
125             secp256k1_ge zless_gej;
126             secp256k1_gej tmp;
127             /* add_var */
128             secp256k1_gej_add_var(&tmp, &groupj[i], &groupj[j], NULL);
129             ge_equals_gej(&group[(i + j) % EXHAUSTIVE_TEST_ORDER], &tmp);
130             /* add_ge */
131             if (j > 0) {
132                 secp256k1_gej_add_ge(&tmp, &groupj[i], &group[j]);
133                 ge_equals_gej(&group[(i + j) % EXHAUSTIVE_TEST_ORDER], &tmp);
134             }
135             /* add_ge_var */
136             secp256k1_gej_add_ge_var(&tmp, &groupj[i], &group[j], NULL);
137             ge_equals_gej(&group[(i + j) % EXHAUSTIVE_TEST_ORDER], &tmp);
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;
142             secp256k1_gej_add_zinv_var(&tmp, &groupj[i], &zless_gej, &fe_inv);
143             ge_equals_gej(&group[(i + j) % EXHAUSTIVE_TEST_ORDER], &tmp);
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);
152         secp256k1_gej_double_var(&tmp, &groupj[i], NULL);
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;
159         secp256k1_gej tmpj;
160         secp256k1_ge_neg(&tmp, &group[i]);
161         ge_equals_ge(&group[EXHAUSTIVE_TEST_ORDER - i], &tmp);
162         secp256k1_gej_neg(&tmpj, &groupj[i]);
163         ge_equals_gej(&group[EXHAUSTIVE_TEST_ORDER - i], &tmpj);
164     }
165 }
166 
test_exhaustive_ecmult(const secp256k1_context * ctx,const secp256k1_ge * group,const secp256k1_gej * groupj)167 void test_exhaustive_ecmult(const secp256k1_context *ctx, const secp256k1_ge *group, const secp256k1_gej *groupj) {
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;
175                 secp256k1_scalar na, ng;
176                 secp256k1_scalar_set_int(&na, i);
177                 secp256k1_scalar_set_int(&ng, j);
178 
179                 secp256k1_ecmult(&ctx->ecmult_ctx, &tmp, &groupj[r_log], &na, &ng);
180                 ge_equals_gej(&group[(i * r_log + j) % EXHAUSTIVE_TEST_ORDER], &tmp);
181 
182                 if (i > 0) {
183                     secp256k1_ecmult_const(&tmp, &group[i], &ng, 256);
184                     ge_equals_gej(&group[(i * j) % EXHAUSTIVE_TEST_ORDER], &tmp);
185                 }
186             }
187         }
188     }
189 }
190 
191 typedef struct {
192     secp256k1_scalar sc[2];
193     secp256k1_ge pt[2];
194 } ecmult_multi_data;
195 
ecmult_multi_callback(secp256k1_scalar * sc,secp256k1_ge * pt,size_t idx,void * cbdata)196 static int ecmult_multi_callback(secp256k1_scalar *sc, secp256k1_ge *pt, size_t idx, void *cbdata) {
197     ecmult_multi_data *data = (ecmult_multi_data*) cbdata;
198     *sc = data->sc[idx];
199     *pt = data->pt[idx];
200     return 1;
201 }
202 
test_exhaustive_ecmult_multi(const secp256k1_context * ctx,const secp256k1_ge * group)203 void test_exhaustive_ecmult_multi(const secp256k1_context *ctx, const secp256k1_ge *group) {
204     int i, j, k, x, y;
205     uint64_t iter = 0;
206     secp256k1_scratch *scratch = secp256k1_scratch_create(&ctx->error_callback, 4096);
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;
214                         secp256k1_scalar g_sc;
215                         ecmult_multi_data data;
216 
217                         secp256k1_scalar_set_int(&data.sc[0], i);
218                         secp256k1_scalar_set_int(&data.sc[1], j);
219                         secp256k1_scalar_set_int(&g_sc, k);
220                         data.pt[0] = group[x];
221                         data.pt[1] = group[y];
222 
223                         secp256k1_ecmult_multi_var(&ctx->error_callback, &ctx->ecmult_ctx, scratch, &tmp, &g_sc, ecmult_multi_callback, &data, 2);
224                         ge_equals_gej(&group[(i * x + j * y + k) % EXHAUSTIVE_TEST_ORDER], &tmp);
225                     }
226                 }
227             }
228         }
229     }
230     secp256k1_scratch_destroy(&ctx->error_callback, scratch);
231 }
232 
r_from_k(secp256k1_scalar * r,const secp256k1_ge * group,int k,int * overflow)233 void r_from_k(secp256k1_scalar *r, const secp256k1_ge *group, int k, int* overflow) {
234     secp256k1_fe x;
235     unsigned char x_bin[32];
236     k %= EXHAUSTIVE_TEST_ORDER;
237     x = group[k].x;
238     secp256k1_fe_normalize(&x);
239     secp256k1_fe_get_b32(x_bin, &x);
240     secp256k1_scalar_set_b32(r, x_bin, overflow);
241 }
242 
test_exhaustive_verify(const secp256k1_context * ctx,const secp256k1_ge * group)243 void test_exhaustive_verify(const secp256k1_context *ctx, const secp256k1_ge *group) {
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++) {
250                     secp256k1_ge nonconst_ge;
251                     secp256k1_ecdsa_signature sig;
252                     secp256k1_pubkey pk;
253                     secp256k1_scalar sk_s, msg_s, r_s, s_s;
254                     secp256k1_scalar s_times_k_s, msg_plus_r_times_sk_s;
255                     int k, should_verify;
256                     unsigned char msg32[32];
257 
258                     if (skip_section(&iter)) continue;
259 
260                     secp256k1_scalar_set_int(&s_s, s);
261                     secp256k1_scalar_set_int(&r_s, r);
262                     secp256k1_scalar_set_int(&msg_s, msg);
263                     secp256k1_scalar_set_int(&sk_s, key);
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++) {
270                         secp256k1_scalar check_x_s;
271                         r_from_k(&check_x_s, group, k, NULL);
272                         if (r_s == check_x_s) {
273                             secp256k1_scalar_set_int(&s_times_k_s, k);
274                             secp256k1_scalar_mul(&s_times_k_s, &s_times_k_s, &s_s);
275                             secp256k1_scalar_mul(&msg_plus_r_times_sk_s, &r_s, &sk_s);
276                             secp256k1_scalar_add(&msg_plus_r_times_sk_s, &msg_plus_r_times_sk_s, &msg_s);
277                             should_verify |= secp256k1_scalar_eq(&s_times_k_s, &msg_plus_r_times_sk_s);
278                         }
279                     }
280                     /* nb we have a "high s" rule */
281                     should_verify &= !secp256k1_scalar_is_high(&s_s);
282 
283                     /* Verify by calling verify */
284                     secp256k1_ecdsa_signature_save(&sig, &r_s, &s_s);
285                     memcpy(&nonconst_ge, &group[sk_s], sizeof(nonconst_ge));
286                     secp256k1_pubkey_save(&pk, &nonconst_ge);
287                     secp256k1_scalar_get_b32(msg32, &msg_s);
288                     CHECK(should_verify ==
289                           secp256k1_ecdsa_verify(ctx, &sig, msg32, &pk));
290                 }
291             }
292         }
293     }
294 }
295 
test_exhaustive_sign(const secp256k1_context * ctx,const secp256k1_ge * group)296 void test_exhaustive_sign(const secp256k1_context *ctx, const secp256k1_ge *group) {
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;
306                 secp256k1_ecdsa_signature sig;
307                 secp256k1_scalar sk, msg, r, s, expected_r;
308                 unsigned char sk32[32], msg32[32];
309                 secp256k1_scalar_set_int(&msg, i);
310                 secp256k1_scalar_set_int(&sk, j);
311                 secp256k1_scalar_get_b32(sk32, &sk);
312                 secp256k1_scalar_get_b32(msg32, &msg);
313 
314                 secp256k1_ecdsa_sign(ctx, &sig, msg32, sk32, secp256k1_nonce_function_smallint, &k);
315 
316                 secp256k1_ecdsa_signature_load(ctx, &r, &s, &sig);
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. */
320                 r_from_k(&expected_r, group, k, NULL);
321                 CHECK(r == expected_r);
322                 CHECK((k * s) % EXHAUSTIVE_TEST_ORDER == (i + r * j) % EXHAUSTIVE_TEST_ORDER ||
323                       (k * (EXHAUSTIVE_TEST_ORDER - 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
344 #include "src/modules/recovery/tests_exhaustive_impl.h"
345 #endif
346 
347 #ifdef ENABLE_MODULE_EXTRAKEYS
348 #include "src/modules/extrakeys/tests_exhaustive_impl.h"
349 #endif
350 
351 #ifdef ENABLE_MODULE_SCHNORRSIG
352 #include "src/modules/schnorrsig/tests_exhaustive_impl.h"
353 #endif
354 
main(int argc,char ** argv)355 int main(int argc, char** argv) {
356     int i;
357     secp256k1_gej groupj[EXHAUSTIVE_TEST_ORDER];
358     secp256k1_ge group[EXHAUSTIVE_TEST_ORDER];
359     unsigned char rand32[32];
360     secp256k1_context *ctx;
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. */
365     setbuf(stdout, NULL);
366     /* Also disable buffering for stderr because it's not guaranteed that it's
367      * unbuffered on all systems. */
368     setbuf(stderr, NULL);
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 */
379     secp256k1_testrand_init(argc > 2 ? argv[2] : NULL);
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);
385         if (num_cores < 1 || this_core >= num_cores) {
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 */
394         ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
395         secp256k1_testrand256(rand32);
396         CHECK(secp256k1_context_randomize(ctx, rand32));
397 
398         /* Generate the entire group */
399         secp256k1_gej_set_infinity(&groupj[0]);
400         secp256k1_ge_set_gej(&group[0], &groupj[0]);
401         for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) {
402             secp256k1_gej_add_ge(&groupj[i], &groupj[i - 1], &secp256k1_ge_const_g);
403             secp256k1_ge_set_gej(&group[i], &groupj[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);
409                 secp256k1_gej_rescale(&groupj[i], &z);
410             }
411 
412             /* Verify against ecmult_gen */
413             {
414                 secp256k1_scalar scalar_i;
415                 secp256k1_gej generatedj;
416                 secp256k1_ge generated;
417 
418                 secp256k1_scalar_set_int(&scalar_i, i);
419                 secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &generatedj, &scalar_i);
420                 secp256k1_ge_set_gej(&generated, &generatedj);
421 
422                 CHECK(group[i].infinity == 0);
423                 CHECK(generated.infinity == 0);
424                 CHECK(secp256k1_fe_equal_var(&generated.x, &group[i].x));
425                 CHECK(secp256k1_fe_equal_var(&generated.y, &group[i].y));
426             }
427         }
428 
429         /* Run the tests */
430         test_exhaustive_endomorphism(group);
431         test_exhaustive_addition(group, groupj);
432         test_exhaustive_ecmult(ctx, group, groupj);
433         test_exhaustive_ecmult_multi(ctx, group);
434         test_exhaustive_sign(ctx, group);
435         test_exhaustive_verify(ctx, group);
436 
437 #ifdef ENABLE_MODULE_RECOVERY
438         test_exhaustive_recovery(ctx, group);
439 #endif
440 #ifdef ENABLE_MODULE_EXTRAKEYS
441         test_exhaustive_extrakeys(ctx, group);
442 #endif
443 #ifdef ENABLE_MODULE_SCHNORRSIG
444         test_exhaustive_schnorrsig(ctx);
445 #endif
446 
447         secp256k1_context_destroy(ctx);
448     }
449 
450     secp256k1_testrand_finish();
451 
452     printf("no problems found\n");
453     return 0;
454 }
455