7#if defined HAVE_CONFIG_H
15#ifndef EXHAUSTIVE_TEST_ORDER
17#define EXHAUSTIVE_TEST_ORDER 13
36 CHECK(secp256k1_fe_equal_var(&
a->x, &b->
x));
37 CHECK(secp256k1_fe_equal_var(&
a->y, &b->
y));
48 secp256k1_fe_sqr(&z2s, &b->
z);
49 secp256k1_fe_mul(&u1, &
a->x, &z2s);
50 u2 = b->
x; secp256k1_fe_normalize_weak(&u2);
51 secp256k1_fe_mul(&s1, &
a->y, &z2s); secp256k1_fe_mul(&s1, &s1, &b->
z);
52 s2 = b->
y; secp256k1_fe_normalize_weak(&s2);
53 CHECK(secp256k1_fe_equal_var(&u1, &u2));
54 CHECK(secp256k1_fe_equal_var(&s1, &s2));
58 unsigned char bin[32];
60 secp256k1_testrand256(bin);
61 if (secp256k1_fe_set_b32(x, bin)) {
72 if (num_cores == 1)
return 0;
73 *iter += 0xe7037ed1a0b428dbULL;
74 return ((((
uint32_t)*iter ^ (*iter >> 32)) * num_cores) >> 32) != this_core;
78 const unsigned char *key32,
const unsigned char *algo16,
79 void *data,
unsigned int attempt) {
92 secp256k1_scalar_set_int(&
s, *idata);
93 secp256k1_scalar_get_b32(nonce32, &
s);
101 secp256k1_ge_mul_lambda(&res, &group[i]);
111 CHECK(secp256k1_ge_is_infinity(&group[0]));
112 CHECK(secp256k1_gej_is_infinity(&groupj[0]));
114 CHECK(!secp256k1_ge_is_infinity(&group[i]));
115 CHECK(!secp256k1_gej_is_infinity(&groupj[i]));
121 if (skip_section(&iter))
continue;
122 secp256k1_fe_inv(&fe_inv, &groupj[
j].z);
127 secp256k1_gej_add_var(&tmp, &groupj[i], &groupj[
j], NULL);
131 secp256k1_gej_add_ge(&tmp, &groupj[i], &group[
j]);
135 secp256k1_gej_add_ge_var(&tmp, &groupj[i], &group[
j], NULL);
139 zless_gej.
x = groupj[
j].
x;
140 zless_gej.
y = groupj[
j].
y;
141 secp256k1_gej_add_zinv_var(&tmp, &groupj[i], &zless_gej, &fe_inv);
149 secp256k1_gej_double(&tmp, &groupj[i]);
151 secp256k1_gej_double_var(&tmp, &groupj[i], NULL);
159 secp256k1_ge_neg(&tmp, &group[i]);
161 secp256k1_gej_neg(&tmpj, &groupj[i]);
171 if (skip_section(&iter))
continue;
175 secp256k1_scalar_set_int(&na, i);
176 secp256k1_scalar_set_int(&ng,
j);
178 secp256k1_ecmult(&tmp, &groupj[r_log], &na, &ng);
182 secp256k1_ecmult_const(&tmp, &group[i], &ng, 256);
210 if (skip_section(&iter))
continue;
216 secp256k1_scalar_set_int(&data.sc[0], i);
217 secp256k1_scalar_set_int(&data.sc[1],
j);
218 secp256k1_scalar_set_int(&g_sc, k);
219 data.pt[0] = group[x];
220 data.pt[1] = group[y];
222 secp256k1_ecmult_multi_var(&ctx->
error_callback, scratch, &tmp, &g_sc, ecmult_multi_callback, &data, 2);
234 unsigned char x_bin[32];
237 secp256k1_fe_normalize(&x);
238 secp256k1_fe_get_b32(x_bin, &x);
239 secp256k1_scalar_set_b32(
r, x_bin,
overflow);
254 int k, should_verify;
255 unsigned char msg32[32];
257 if (skip_section(&iter))
continue;
259 secp256k1_scalar_set_int(&s_s,
s);
260 secp256k1_scalar_set_int(&r_s,
r);
261 secp256k1_scalar_set_int(&msg_s, msg);
262 secp256k1_scalar_set_int(&sk_s, key);
270 r_from_k(&check_x_s, group, k, NULL);
271 if (r_s == check_x_s) {
272 secp256k1_scalar_set_int(&s_times_k_s, k);
273 secp256k1_scalar_mul(&s_times_k_s, &s_times_k_s, &s_s);
274 secp256k1_scalar_mul(&msg_plus_r_times_sk_s, &r_s, &sk_s);
275 secp256k1_scalar_add(&msg_plus_r_times_sk_s, &msg_plus_r_times_sk_s, &msg_s);
276 should_verify |= secp256k1_scalar_eq(&s_times_k_s, &msg_plus_r_times_sk_s);
280 should_verify &= !secp256k1_scalar_is_high(&s_s);
283 secp256k1_ecdsa_signature_save(&sig, &r_s, &s_s);
284 memcpy(&nonconst_ge, &group[sk_s],
sizeof(nonconst_ge));
285 secp256k1_pubkey_save(&pk, &nonconst_ge);
286 secp256k1_scalar_get_b32(msg32, &msg_s);
287 CHECK(should_verify ==
302 if (skip_section(&iter))
continue;
304 const int starting_k = k;
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);
317 secp256k1_ecdsa_signature_load(ctx, &
r, &
s, &sig);
321 r_from_k(&expected_r, group, k, NULL);
327 if (k < starting_k) {
344#ifdef ENABLE_MODULE_RECOVERY
348#ifdef ENABLE_MODULE_EXTRAKEYS
352#ifdef ENABLE_MODULE_SCHNORRSIG
360 unsigned char rand32[32];
366 setbuf(stdout, NULL);
369 setbuf(stderr, NULL);
377 printf(
"test count = %i\n",
count);
380 secp256k1_testrand_init(argc > 2 ?
argv[2] : NULL);
384 num_cores = strtol(
argv[3], NULL, 0);
385 this_core = strtol(
argv[4], NULL, 0);
386 if (num_cores < 1 || this_core >= num_cores) {
387 fprintf(stderr,
"Usage: %s [count] [seed] [numcores] [thiscore]\n",
argv[0]);
390 printf(
"running tests for core %lu (out of [0..%lu])\n", (
unsigned long)this_core, (
unsigned long)num_cores - 1);
400 secp256k1_testrand256(rand32);
404 secp256k1_gej_set_infinity(&groupj[0]);
405 secp256k1_ge_set_gej(&group[0], &groupj[0]);
407 secp256k1_gej_add_ge(&groupj[i], &groupj[i - 1], &secp256k1_ge_const_g);
408 secp256k1_ge_set_gej(&group[i], &groupj[i]);
414 secp256k1_gej_rescale(&groupj[i], &z);
423 secp256k1_scalar_set_int(&scalar_i, i);
424 secp256k1_ecmult_gen(&ctx->
ecmult_gen_ctx, &generatedj, &scalar_i);
425 secp256k1_ge_set_gej(&generated, &generatedj);
427 CHECK(group[i].infinity == 0);
429 CHECK(secp256k1_fe_equal_var(&generated.
x, &group[i].
x));
430 CHECK(secp256k1_fe_equal_var(&generated.
y, &group[i].
y));
442#ifdef ENABLE_MODULE_RECOVERY
443 test_exhaustive_recovery(ctx, group);
445#ifdef ENABLE_MODULE_EXTRAKEYS
446 test_exhaustive_extrakeys(ctx, group);
448#ifdef ENABLE_MODULE_SCHNORRSIG
449 test_exhaustive_schnorrsig(ctx);
455 secp256k1_testrand_finish();
457 printf(
"no problems found\n");
#define ECMULT_GEN_PREC_BITS
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
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)]
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)]
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1)
#define SECP256K1_CONTEXT_SIGN
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32) SECP256K1_ARG_NONNULL(1)
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
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)
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)
#define SECP256K1_CONTEXT_VERIFY
unsigned __int64 uint64_t
secp256k1_callback error_callback
secp256k1_ecmult_gen_context ecmult_gen_ctx
void test_exhaustive_ecmult_multi(const secp256k1_context *ctx, const secp256k1_ge *group)
void test_exhaustive_ecmult(const secp256k1_ge *group, const secp256k1_gej *groupj)
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)
void ge_equals_ge(const secp256k1_ge *a, const secp256k1_ge *b)
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)
void test_exhaustive_endomorphism(const secp256k1_ge *group)
void random_fe(secp256k1_fe *x)
#define EXHAUSTIVE_TEST_ORDER
memcpy((char *) pInfo->slotDescription, s, l)