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 ***********************************************************************/
7 #if defined HAVE_CONFIG_H
8 #include "libsecp256k1-config.h"
16 #undef USE_ECMULT_STATIC_PRECOMPUTATION
18 #ifndef EXHAUSTIVE_TEST_ORDER
19 /* see group_impl.h for allowable values */
20 #define EXHAUSTIVE_TEST_ORDER 13
23 #include "include/secp256k1.h"
24 #include "assumptions.h"
26 #include "secp256k1.c"
27 #include "testrand_impl.h"
31 /** stolen from tests.c */
32 void ge_equals_ge(const secp256k1_ge *a, const secp256k1_ge *b) {
33 CHECK(a->infinity == b->infinity);
37 CHECK(secp256k1_fe_equal_var(&a->x, &b->x));
38 CHECK(secp256k1_fe_equal_var(&a->y, &b->y));
41 void ge_equals_gej(const secp256k1_ge *a, const secp256k1_gej *b) {
43 secp256k1_fe u1, u2, s1, s2;
44 CHECK(a->infinity == b->infinity);
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));
58 void random_fe(secp256k1_fe *x) {
59 unsigned char bin[32];
61 secp256k1_testrand256(bin);
62 if (secp256k1_fe_set_b32(x, bin)) {
67 /** END stolen from tests.c */
69 static uint32_t num_cores = 1;
70 static uint32_t this_core = 0;
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;
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) {
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. */
91 *idata = (*idata + 1) % EXHAUSTIVE_TEST_ORDER;
93 secp256k1_scalar_set_int(&s, *idata);
94 secp256k1_scalar_get_b32(nonce32, &s);
98 void test_exhaustive_endomorphism(const secp256k1_ge *group) {
100 for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
102 secp256k1_ge_mul_lambda(&res, &group[i]);
103 ge_equals_ge(&group[i * EXHAUSTIVE_TEST_LAMBDA % EXHAUSTIVE_TEST_ORDER], &res);
107 void test_exhaustive_addition(const secp256k1_ge *group, const secp256k1_gej *groupj) {
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]));
119 /* Check all addition formulae */
120 for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
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;
128 secp256k1_gej_add_var(&tmp, &groupj[i], &groupj[j], NULL);
129 ge_equals_gej(&group[(i + j) % EXHAUSTIVE_TEST_ORDER], &tmp);
132 secp256k1_gej_add_ge(&tmp, &groupj[i], &group[j]);
133 ge_equals_gej(&group[(i + j) % EXHAUSTIVE_TEST_ORDER], &tmp);
136 secp256k1_gej_add_ge_var(&tmp, &groupj[i], &group[j], NULL);
137 ge_equals_gej(&group[(i + j) % EXHAUSTIVE_TEST_ORDER], &tmp);
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);
148 for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
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);
157 for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) {
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);
167 void test_exhaustive_ecmult(const secp256k1_context *ctx, const secp256k1_ge *group, const secp256k1_gej *groupj) {
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++) {
175 secp256k1_scalar na, ng;
176 secp256k1_scalar_set_int(&na, i);
177 secp256k1_scalar_set_int(&ng, j);
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);
183 secp256k1_ecmult_const(&tmp, &group[i], &ng, 256);
184 ge_equals_gej(&group[(i * j) % EXHAUSTIVE_TEST_ORDER], &tmp);
192 secp256k1_scalar sc[2];
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;
203 void test_exhaustive_ecmult_multi(const secp256k1_context *ctx, const secp256k1_ge *group) {
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++) {
214 secp256k1_scalar g_sc;
215 ecmult_multi_data data;
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];
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);
230 secp256k1_scratch_destroy(&ctx->error_callback, scratch);
233 void r_from_k(secp256k1_scalar *r, const secp256k1_ge *group, int k, int* overflow) {
235 unsigned char x_bin[32];
236 k %= EXHAUSTIVE_TEST_ORDER;
238 secp256k1_fe_normalize(&x);
239 secp256k1_fe_get_b32(x_bin, &x);
240 secp256k1_scalar_set_b32(r, x_bin, overflow);
243 void test_exhaustive_verify(const secp256k1_context *ctx, const secp256k1_ge *group) {
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;
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];
258 if (skip_section(&iter)) continue;
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);
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. */
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);
280 /* nb we have a "high s" rule */
281 should_verify &= !secp256k1_scalar_is_high(&s_s);
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));
296 void test_exhaustive_sign(const secp256k1_context *ctx, const secp256k1_ge *group) {
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);
314 secp256k1_ecdsa_sign(ctx, &sig, msg32, sk32, secp256k1_nonce_function_smallint, &k);
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
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);
325 /* Overflow means we've tried every possible nonce */
326 if (k < starting_k) {
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.)
343 #ifdef ENABLE_MODULE_RECOVERY
344 #include "src/modules/recovery/tests_exhaustive_impl.h"
347 #ifdef ENABLE_MODULE_EXTRAKEYS
348 #include "src/modules/extrakeys/tests_exhaustive_impl.h"
351 #ifdef ENABLE_MODULE_SCHNORRSIG
352 #include "src/modules/schnorrsig/tests_exhaustive_impl.h"
355 int main(int argc, char** argv) {
357 secp256k1_gej groupj[EXHAUSTIVE_TEST_ORDER];
358 secp256k1_ge group[EXHAUSTIVE_TEST_ORDER];
359 unsigned char rand32[32];
360 secp256k1_context *ctx;
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);
370 printf("Exhaustive tests for order %lu\n", (unsigned long)EXHAUSTIVE_TEST_ORDER);
372 /* find iteration count */
374 count = strtol(argv[1], NULL, 0);
376 printf("test count = %i\n", count);
378 /* find random seed */
379 secp256k1_testrand_init(argc > 2 ? argv[2] : NULL);
381 /* set up split processing */
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]);
389 printf("running tests for core %lu (out of [0..%lu])\n", (unsigned long)this_core, (unsigned long)num_cores - 1);
394 ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
395 secp256k1_testrand256(rand32);
396 CHECK(secp256k1_context_randomize(ctx, rand32));
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]);
405 /* Set a different random z-value for each Jacobian point, except z=1
406 is used in the last iteration. */
409 secp256k1_gej_rescale(&groupj[i], &z);
412 /* Verify against ecmult_gen */
414 secp256k1_scalar scalar_i;
415 secp256k1_gej generatedj;
416 secp256k1_ge generated;
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);
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));
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);
437 #ifdef ENABLE_MODULE_RECOVERY
438 test_exhaustive_recovery(ctx, group);
440 #ifdef ENABLE_MODULE_EXTRAKEYS
441 test_exhaustive_extrakeys(ctx, group);
443 #ifdef ENABLE_MODULE_SCHNORRSIG
444 test_exhaustive_schnorrsig(ctx);
447 secp256k1_context_destroy(ctx);
450 secp256k1_testrand_finish();
452 printf("no problems found\n");