Bitcoin Core  22.99.0
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 #if defined HAVE_CONFIG_H
8 #include "libsecp256k1-config.h"
9 #endif
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <time.h>
14 
15 #ifndef EXHAUSTIVE_TEST_ORDER
16 /* see group_impl.h for allowable values */
17 #define EXHAUSTIVE_TEST_ORDER 13
18 #endif
19 
20 #include "secp256k1.c"
21 #include "../include/secp256k1.h"
22 #include "assumptions.h"
23 #include "group.h"
24 #include "testrand_impl.h"
25 #include "ecmult_gen_prec_impl.h"
26 
27 static int count = 2;
28 
30 void ge_equals_ge(const secp256k1_ge *a, const secp256k1_ge *b) {
31  CHECK(a->infinity == b->infinity);
32  if (a->infinity) {
33  return;
34  }
35  CHECK(secp256k1_fe_equal_var(&a->x, &b->x));
36  CHECK(secp256k1_fe_equal_var(&a->y, &b->y));
37 }
38 
39 void ge_equals_gej(const secp256k1_ge *a, const secp256k1_gej *b) {
40  secp256k1_fe z2s;
41  secp256k1_fe u1, u2, s1, s2;
42  CHECK(a->infinity == b->infinity);
43  if (a->infinity) {
44  return;
45  }
46  /* Check a.x * b.z^2 == b.x && a.y * b.z^3 == b.y, to avoid inverses. */
47  secp256k1_fe_sqr(&z2s, &b->z);
48  secp256k1_fe_mul(&u1, &a->x, &z2s);
49  u2 = b->x; secp256k1_fe_normalize_weak(&u2);
50  secp256k1_fe_mul(&s1, &a->y, &z2s); secp256k1_fe_mul(&s1, &s1, &b->z);
51  s2 = b->y; secp256k1_fe_normalize_weak(&s2);
52  CHECK(secp256k1_fe_equal_var(&u1, &u2));
53  CHECK(secp256k1_fe_equal_var(&s1, &s2));
54 }
55 
57  unsigned char bin[32];
58  do {
60  if (secp256k1_fe_set_b32(x, bin)) {
61  return;
62  }
63  } while(1);
64 }
67 static uint32_t num_cores = 1;
68 static uint32_t this_core = 0;
69 
70 SECP256K1_INLINE static int skip_section(uint64_t* iter) {
71  if (num_cores == 1) return 0;
72  *iter += 0xe7037ed1a0b428dbULL;
73  return ((((uint32_t)*iter ^ (*iter >> 32)) * num_cores) >> 32) != this_core;
74 }
75 
76 int secp256k1_nonce_function_smallint(unsigned char *nonce32, const unsigned char *msg32,
77  const unsigned char *key32, const unsigned char *algo16,
78  void *data, unsigned int attempt) {
80  int *idata = data;
81  (void)msg32;
82  (void)key32;
83  (void)algo16;
84  /* Some nonces cannot be used because they'd cause s and/or r to be zero.
85  * The signing function has retry logic here that just re-calls the nonce
86  * function with an increased `attempt`. So if attempt > 0 this means we
87  * need to change the nonce to avoid an infinite loop. */
88  if (attempt > 0) {
89  *idata = (*idata + 1) % EXHAUSTIVE_TEST_ORDER;
90  }
91  secp256k1_scalar_set_int(&s, *idata);
92  secp256k1_scalar_get_b32(nonce32, &s);
93  return 1;
94 }
95 
97  int i;
98  for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
99  secp256k1_ge res;
100  secp256k1_ge_mul_lambda(&res, &group[i]);
101  ge_equals_ge(&group[i * EXHAUSTIVE_TEST_LAMBDA % EXHAUSTIVE_TEST_ORDER], &res);
102  }
103 }
104 
105 void test_exhaustive_addition(const secp256k1_ge *group, const secp256k1_gej *groupj) {
106  int i, j;
107  uint64_t iter = 0;
108 
109  /* Sanity-check (and check infinity functions) */
110  CHECK(secp256k1_ge_is_infinity(&group[0]));
111  CHECK(secp256k1_gej_is_infinity(&groupj[0]));
112  for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) {
113  CHECK(!secp256k1_ge_is_infinity(&group[i]));
114  CHECK(!secp256k1_gej_is_infinity(&groupj[i]));
115  }
116 
117  /* Check all addition formulae */
118  for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
119  secp256k1_fe fe_inv;
120  if (skip_section(&iter)) continue;
121  secp256k1_fe_inv(&fe_inv, &groupj[j].z);
122  for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
123  secp256k1_ge zless_gej;
124  secp256k1_gej tmp;
125  /* add_var */
126  secp256k1_gej_add_var(&tmp, &groupj[i], &groupj[j], NULL);
127  ge_equals_gej(&group[(i + j) % EXHAUSTIVE_TEST_ORDER], &tmp);
128  /* add_ge */
129  if (j > 0) {
130  secp256k1_gej_add_ge(&tmp, &groupj[i], &group[j]);
131  ge_equals_gej(&group[(i + j) % EXHAUSTIVE_TEST_ORDER], &tmp);
132  }
133  /* add_ge_var */
134  secp256k1_gej_add_ge_var(&tmp, &groupj[i], &group[j], NULL);
135  ge_equals_gej(&group[(i + j) % EXHAUSTIVE_TEST_ORDER], &tmp);
136  /* add_zinv_var */
137  zless_gej.infinity = groupj[j].infinity;
138  zless_gej.x = groupj[j].x;
139  zless_gej.y = groupj[j].y;
140  secp256k1_gej_add_zinv_var(&tmp, &groupj[i], &zless_gej, &fe_inv);
141  ge_equals_gej(&group[(i + j) % EXHAUSTIVE_TEST_ORDER], &tmp);
142  }
143  }
144 
145  /* Check doubling */
146  for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
147  secp256k1_gej tmp;
148  secp256k1_gej_double(&tmp, &groupj[i]);
149  ge_equals_gej(&group[(2 * i) % EXHAUSTIVE_TEST_ORDER], &tmp);
150  secp256k1_gej_double_var(&tmp, &groupj[i], NULL);
151  ge_equals_gej(&group[(2 * i) % EXHAUSTIVE_TEST_ORDER], &tmp);
152  }
153 
154  /* Check negation */
155  for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) {
156  secp256k1_ge tmp;
157  secp256k1_gej tmpj;
158  secp256k1_ge_neg(&tmp, &group[i]);
159  ge_equals_ge(&group[EXHAUSTIVE_TEST_ORDER - i], &tmp);
160  secp256k1_gej_neg(&tmpj, &groupj[i]);
161  ge_equals_gej(&group[EXHAUSTIVE_TEST_ORDER - i], &tmpj);
162  }
163 }
164 
165 void test_exhaustive_ecmult(const secp256k1_ge *group, const secp256k1_gej *groupj) {
166  int i, j, r_log;
167  uint64_t iter = 0;
168  for (r_log = 1; r_log < EXHAUSTIVE_TEST_ORDER; r_log++) {
169  for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
170  if (skip_section(&iter)) continue;
171  for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
172  secp256k1_gej tmp;
173  secp256k1_scalar na, ng;
174  secp256k1_scalar_set_int(&na, i);
175  secp256k1_scalar_set_int(&ng, j);
176 
177  secp256k1_ecmult(&tmp, &groupj[r_log], &na, &ng);
178  ge_equals_gej(&group[(i * r_log + j) % EXHAUSTIVE_TEST_ORDER], &tmp);
179 
180  if (i > 0) {
181  secp256k1_ecmult_const(&tmp, &group[i], &ng, 256);
182  ge_equals_gej(&group[(i * j) % EXHAUSTIVE_TEST_ORDER], &tmp);
183  }
184  }
185  }
186  }
187 }
188 
189 typedef struct {
193 
194 static int ecmult_multi_callback(secp256k1_scalar *sc, secp256k1_ge *pt, size_t idx, void *cbdata) {
195  ecmult_multi_data *data = (ecmult_multi_data*) cbdata;
196  *sc = data->sc[idx];
197  *pt = data->pt[idx];
198  return 1;
199 }
200 
202  int i, j, k, x, y;
203  uint64_t iter = 0;
205  for (i = 0; i < EXHAUSTIVE_TEST_ORDER; i++) {
206  for (j = 0; j < EXHAUSTIVE_TEST_ORDER; j++) {
207  for (k = 0; k < EXHAUSTIVE_TEST_ORDER; k++) {
208  for (x = 0; x < EXHAUSTIVE_TEST_ORDER; x++) {
209  if (skip_section(&iter)) continue;
210  for (y = 0; y < EXHAUSTIVE_TEST_ORDER; y++) {
211  secp256k1_gej tmp;
212  secp256k1_scalar g_sc;
213  ecmult_multi_data data;
214 
215  secp256k1_scalar_set_int(&data.sc[0], i);
216  secp256k1_scalar_set_int(&data.sc[1], j);
217  secp256k1_scalar_set_int(&g_sc, k);
218  data.pt[0] = group[x];
219  data.pt[1] = group[y];
220 
221  secp256k1_ecmult_multi_var(&ctx->error_callback, scratch, &tmp, &g_sc, ecmult_multi_callback, &data, 2);
222  ge_equals_gej(&group[(i * x + j * y + k) % EXHAUSTIVE_TEST_ORDER], &tmp);
223  }
224  }
225  }
226  }
227  }
229 }
230 
231 void r_from_k(secp256k1_scalar *r, const secp256k1_ge *group, int k, int* overflow) {
232  secp256k1_fe x;
233  unsigned char x_bin[32];
235  x = group[k].x;
237  secp256k1_fe_get_b32(x_bin, &x);
238  secp256k1_scalar_set_b32(r, x_bin, overflow);
239 }
240 
242  int s, r, msg, key;
243  uint64_t iter = 0;
244  for (s = 1; s < EXHAUSTIVE_TEST_ORDER; s++) {
245  for (r = 1; r < EXHAUSTIVE_TEST_ORDER; r++) {
246  for (msg = 1; msg < EXHAUSTIVE_TEST_ORDER; msg++) {
247  for (key = 1; key < EXHAUSTIVE_TEST_ORDER; key++) {
248  secp256k1_ge nonconst_ge;
250  secp256k1_pubkey pk;
251  secp256k1_scalar sk_s, msg_s, r_s, s_s;
252  secp256k1_scalar s_times_k_s, msg_plus_r_times_sk_s;
253  int k, should_verify;
254  unsigned char msg32[32];
255 
256  if (skip_section(&iter)) continue;
257 
258  secp256k1_scalar_set_int(&s_s, s);
259  secp256k1_scalar_set_int(&r_s, r);
260  secp256k1_scalar_set_int(&msg_s, msg);
261  secp256k1_scalar_set_int(&sk_s, key);
262 
263  /* Verify by hand */
264  /* Run through every k value that gives us this r and check that *one* works.
265  * Note there could be none, there could be multiple, ECDSA is weird. */
266  should_verify = 0;
267  for (k = 0; k < EXHAUSTIVE_TEST_ORDER; k++) {
268  secp256k1_scalar check_x_s;
269  r_from_k(&check_x_s, group, k, NULL);
270  if (r_s == check_x_s) {
271  secp256k1_scalar_set_int(&s_times_k_s, k);
272  secp256k1_scalar_mul(&s_times_k_s, &s_times_k_s, &s_s);
273  secp256k1_scalar_mul(&msg_plus_r_times_sk_s, &r_s, &sk_s);
274  secp256k1_scalar_add(&msg_plus_r_times_sk_s, &msg_plus_r_times_sk_s, &msg_s);
275  should_verify |= secp256k1_scalar_eq(&s_times_k_s, &msg_plus_r_times_sk_s);
276  }
277  }
278  /* nb we have a "high s" rule */
279  should_verify &= !secp256k1_scalar_is_high(&s_s);
280 
281  /* Verify by calling verify */
282  secp256k1_ecdsa_signature_save(&sig, &r_s, &s_s);
283  memcpy(&nonconst_ge, &group[sk_s], sizeof(nonconst_ge));
284  secp256k1_pubkey_save(&pk, &nonconst_ge);
285  secp256k1_scalar_get_b32(msg32, &msg_s);
286  CHECK(should_verify ==
287  secp256k1_ecdsa_verify(ctx, &sig, msg32, &pk));
288  }
289  }
290  }
291  }
292 }
293 
295  int i, j, k;
296  uint64_t iter = 0;
297 
298  /* Loop */
299  for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) { /* message */
300  for (j = 1; j < EXHAUSTIVE_TEST_ORDER; j++) { /* key */
301  if (skip_section(&iter)) continue;
302  for (k = 1; k < EXHAUSTIVE_TEST_ORDER; k++) { /* nonce */
303  const int starting_k = k;
304  int ret;
306  secp256k1_scalar sk, msg, r, s, expected_r;
307  unsigned char sk32[32], msg32[32];
308  secp256k1_scalar_set_int(&msg, i);
309  secp256k1_scalar_set_int(&sk, j);
310  secp256k1_scalar_get_b32(sk32, &sk);
311  secp256k1_scalar_get_b32(msg32, &msg);
312 
313  ret = secp256k1_ecdsa_sign(ctx, &sig, msg32, sk32, secp256k1_nonce_function_smallint, &k);
314  CHECK(ret == 1);
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 ||
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 
355 int 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. */
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  /* Recreate the ecmult_gen table using the right generator (as selected via EXHAUSTIVE_TEST_ORDER) */
394 
395  while (count--) {
396  /* Build context */
398  secp256k1_testrand256(rand32);
400 
401  /* Generate the entire group */
402  secp256k1_gej_set_infinity(&groupj[0]);
403  secp256k1_ge_set_gej(&group[0], &groupj[0]);
404  for (i = 1; i < EXHAUSTIVE_TEST_ORDER; i++) {
405  secp256k1_gej_add_ge(&groupj[i], &groupj[i - 1], &secp256k1_ge_const_g);
406  secp256k1_ge_set_gej(&group[i], &groupj[i]);
407  if (count != 0) {
408  /* Set a different random z-value for each Jacobian point, except z=1
409  is used in the last iteration. */
410  secp256k1_fe z;
411  random_fe(&z);
412  secp256k1_gej_rescale(&groupj[i], &z);
413  }
414 
415  /* Verify against ecmult_gen */
416  {
417  secp256k1_scalar scalar_i;
418  secp256k1_gej generatedj;
419  secp256k1_ge generated;
420 
421  secp256k1_scalar_set_int(&scalar_i, i);
422  secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &generatedj, &scalar_i);
423  secp256k1_ge_set_gej(&generated, &generatedj);
424 
425  CHECK(group[i].infinity == 0);
426  CHECK(generated.infinity == 0);
427  CHECK(secp256k1_fe_equal_var(&generated.x, &group[i].x));
428  CHECK(secp256k1_fe_equal_var(&generated.y, &group[i].y));
429  }
430  }
431 
432  /* Run the tests */
434  test_exhaustive_addition(group, groupj);
435  test_exhaustive_ecmult(group, groupj);
437  test_exhaustive_sign(ctx, group);
438  test_exhaustive_verify(ctx, group);
439 
440 #ifdef ENABLE_MODULE_RECOVERY
442 #endif
443 #ifdef ENABLE_MODULE_EXTRAKEYS
445 #endif
446 #ifdef ENABLE_MODULE_SCHNORRSIG
448 #endif
449 
451  }
452 
454 
455  printf("no problems found\n");
456  return 0;
457 }
secp256k1_testrand_finish
static void secp256k1_testrand_finish(void)
Print final test information.
test_exhaustive_endomorphism
void test_exhaustive_endomorphism(const secp256k1_ge *group)
Definition: tests_exhaustive.c:96
secp256k1_ecdsa_signature
Opaque data structured that holds a parsed ECDSA signature.
Definition: secp256k1.h:83
secp256k1_gej::infinity
int infinity
Definition: group.h:27
secp256k1_gej_set_infinity
static void secp256k1_gej_set_infinity(secp256k1_gej *r)
Set a group element (jacobian) equal to the point at infinity.
test_exhaustive_extrakeys
static void test_exhaustive_extrakeys(const secp256k1_context *ctx, const secp256k1_ge *group)
Definition: tests_exhaustive_impl.h:13
secp256k1_fe_inv
static void secp256k1_fe_inv(secp256k1_fe *r, const secp256k1_fe *a)
Sets a field element to be the (modular) inverse of another.
SECP256K1_CONTEXT_VERIFY
#define SECP256K1_CONTEXT_VERIFY
Flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size, and secp256k1_context...
Definition: secp256k1.h:184
secp256k1_ge::y
secp256k1_fe y
Definition: group.h:19
SECP256K1_CONTEXT_SIGN
#define SECP256K1_CONTEXT_SIGN
Definition: secp256k1.h:185
secp256k1_scalar_get_b32
static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar *a)
Convert a scalar to a byte array.
EXHAUSTIVE_TEST_ORDER
#define EXHAUSTIVE_TEST_ORDER
Definition: tests_exhaustive.c:17
secp256k1_testrand256
static void secp256k1_testrand256(unsigned char *b32)
Generate a pseudorandom 32-byte array.
ecmult_multi_data::sc
secp256k1_scalar * sc
Definition: tests.c:3797
secp256k1_context_struct
Definition: secp256k1.c:47
secp256k1_ecdsa_signature_load
static void secp256k1_ecdsa_signature_load(const secp256k1_context *ctx, secp256k1_scalar *r, secp256k1_scalar *s, const secp256k1_ecdsa_signature *sig)
Definition: secp256k1.c:295
secp256k1_nonce_function_smallint
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)
Definition: tests_exhaustive.c:76
secp256k1_fe_set_b32
static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a)
Set a field element equal to 32-byte big endian value.
secp256k1_fe_normalize
static void secp256k1_fe_normalize(secp256k1_fe *r)
Field element module.
secp256k1_scalar_is_high
static int secp256k1_scalar_is_high(const secp256k1_scalar *a)
Check whether a scalar is higher than the group order divided by 2.
secp256k1_gej::x
secp256k1_fe x
Definition: group.h:24
group.h
test_exhaustive_ecmult_multi
void test_exhaustive_ecmult_multi(const secp256k1_context *ctx, const secp256k1_ge *group)
Definition: tests_exhaustive.c:201
tinyformat::printf
void printf(const char *fmt, const Args &... args)
Format list of arguments to std::cout, according to the given format string.
Definition: tinyformat.h:1079
ecmult_multi_data
Definition: tests.c:3796
secp256k1_ge_mul_lambda
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.
secp256k1_scratch_space_struct
Definition: scratch.h:12
secp256k1_scratch_destroy
static void secp256k1_scratch_destroy(const secp256k1_callback *error_callback, secp256k1_scratch *scratch)
secp256k1_gej::z
secp256k1_fe z
Definition: group.h:26
secp256k1_ecmult_gen_create_prec_table
static void secp256k1_ecmult_gen_create_prec_table(secp256k1_ge_storage *table, const secp256k1_ge *gen, int bits)
testrand_impl.h
ge_equals_gej
void ge_equals_gej(const secp256k1_ge *a, const secp256k1_gej *b)
Definition: tests_exhaustive.c:39
secp256k1_gej_rescale
static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *b)
Rescale a jacobian point by b which must be non-zero.
tests_exhaustive_impl.h
secp256k1_context_create
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:107
secp256k1_gej_add_ge
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).
secp256k1_ecmult
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.
secp256k1_scalar_add
static int secp256k1_scalar_add(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
Add two scalars together (modulo the group order).
secp256k1_pubkey_save
static void secp256k1_pubkey_save(secp256k1_pubkey *pubkey, secp256k1_ge *ge)
Definition: secp256k1.c:214
tests_exhaustive_impl.h
secp256k1_ecdsa_verify
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:400
ecmult_multi_callback
static int ecmult_multi_callback(secp256k1_scalar *sc, secp256k1_ge *pt, size_t idx, void *cbdata)
Definition: tests_exhaustive.c:194
secp256k1_scalar
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
num_cores
static uint32_t num_cores
END stolen from tests.c.
Definition: tests_exhaustive.c:67
secp256k1_ecmult_gen
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.
secp256k1_ge_const_g
static const secp256k1_ge secp256k1_ge_const_g
Definition: group_impl.h:62
secp256k1_gej
A group element of the secp256k1 curve, in jacobian coordinates.
Definition: group.h:23
test_exhaustive_recovery
static void test_exhaustive_recovery(const secp256k1_context *ctx, const secp256k1_ge *group)
Definition: tests_exhaustive_impl.h:144
assumptions.h
secp256k1_fe_equal_var
static int secp256k1_fe_equal_var(const secp256k1_fe *a, const secp256k1_fe *b)
Same as secp256k1_fe_equal, but may be variable time.
test_exhaustive_addition
void test_exhaustive_addition(const secp256k1_ge *group, const secp256k1_gej *groupj)
Definition: tests_exhaustive.c:105
secp256k1_fe_mul
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.
secp256k1.c
ecmult_multi_data::pt
secp256k1_ge * pt
Definition: tests.c:3798
secp256k1_fe
Definition: field_10x26.h:12
time.h
secp256k1_context_struct::ecmult_gen_ctx
secp256k1_ecmult_gen_context ecmult_gen_ctx
Definition: secp256k1.c:48
secp256k1_gej_neg
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)
secp256k1_gej::y
secp256k1_fe y
Definition: group.h:25
secp256k1_ge_neg
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)
main
int main(int argc, char **argv)
Definition: tests_exhaustive.c:355
secp256k1_ecmult_const
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...
secp256k1_fe_get_b32
static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a)
Convert a field element to a 32-byte big endian value.
skip_section
static SECP256K1_INLINE int skip_section(uint64_t *iter)
Definition: tests_exhaustive.c:70
secp256k1_ecdsa_sign
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:510
CHECK
#define CHECK(cond)
Unconditional failure on condition failure.
Definition: util.h:35
secp256k1_ecmult_multi_var
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.
secp256k1_context_struct::error_callback
secp256k1_callback error_callback
Definition: secp256k1.c:50
secp256k1_gej_add_var
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.
secp256k1_scalar_eq
static int secp256k1_scalar_eq(const secp256k1_scalar *a, const secp256k1_scalar *b)
Compare two scalars.
r_from_k
void r_from_k(secp256k1_scalar *r, const secp256k1_ge *group, int k, int *overflow)
Definition: tests_exhaustive.c:231
secp256k1_fe_sqr
static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a)
Sets a field element to be the square of another.
secp256k1_context_destroy
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:146
ecmult_gen_prec_impl.h
secp256k1_ge::infinity
int infinity
Definition: group.h:20
test_exhaustive_sign
void test_exhaustive_sign(const secp256k1_context *ctx, const secp256k1_ge *group)
Definition: tests_exhaustive.c:294
libsecp256k1-config.h
secp256k1_testrand_init
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.
test_exhaustive_ecmult
void test_exhaustive_ecmult(const secp256k1_ge *group, const secp256k1_gej *groupj)
Definition: tests_exhaustive.c:165
ge_equals_ge
void ge_equals_ge(const secp256k1_ge *a, const secp256k1_ge *b)
stolen from tests.c
Definition: tests_exhaustive.c:30
ECMULT_GEN_PREC_BITS
#define ECMULT_GEN_PREC_BITS
Definition: libsecp256k1-config.h:12
secp256k1_gej_add_ge_var
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).
test_exhaustive_schnorrsig
static void test_exhaustive_schnorrsig(const secp256k1_context *ctx)
Definition: tests_exhaustive_impl.h:186
secp256k1_gej_double_var
static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr)
Set r equal to the double of a.
secp256k1_ecmult_gen_prec_table
static 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)]
Definition: ecmult_gen_static_prec_table.h:10
secp256k1_ecdsa_signature_save
static void secp256k1_ecdsa_signature_save(secp256k1_ecdsa_signature *sig, const secp256k1_scalar *r, const secp256k1_scalar *s)
Definition: secp256k1.c:309
secp256k1_scalar_mul
static void secp256k1_scalar_mul(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
Multiply two scalars (modulo the group order).
secp256k1_gej_double
static void secp256k1_gej_double(secp256k1_gej *r, const secp256k1_gej *a)
Set r equal to the double of a.
SECP256K1_INLINE
#define SECP256K1_INLINE
Definition: secp256k1.h:127
secp256k1_gej_is_infinity
static int secp256k1_gej_is_infinity(const secp256k1_gej *a)
Check whether a group element is the point at infinity.
secp256k1_gej_add_zinv_var
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).
secp256k1_fe_normalize_weak
static void secp256k1_fe_normalize_weak(secp256k1_fe *r)
Weakly normalize a field element: reduce its magnitude to 1, but don't fully normalize.
random_fe
void random_fe(secp256k1_fe *x)
Definition: tests_exhaustive.c:56
secp256k1_ge::x
secp256k1_fe x
Definition: group.h:18
secp256k1_scratch_create
static secp256k1_scratch * secp256k1_scratch_create(const secp256k1_callback *error_callback, size_t max_size)
secp256k1_ge_is_infinity
static int secp256k1_ge_is_infinity(const secp256k1_ge *a)
Check whether a group element is the point at infinity.
secp256k1_scalar_set_b32
static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *bin, int *overflow)
Set a scalar from a big endian byte array.
secp256k1_pubkey
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:70
test_exhaustive_verify
void test_exhaustive_verify(const secp256k1_context *ctx, const secp256k1_ge *group)
Definition: tests_exhaustive.c:241
secp256k1_ge
A group element of the secp256k1 curve, in affine coordinates.
Definition: group.h:13
ByteUnit::k
@ k
ctx
static secp256k1_context * ctx
Definition: tests.c:32
tests_exhaustive_impl.h
this_core
static uint32_t this_core
Definition: tests_exhaustive.c:68
secp256k1_ge_set_gej
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.
secp256k1_scalar_set_int
static void secp256k1_scalar_set_int(secp256k1_scalar *r, unsigned int v)
Set a scalar to an unsigned integer.
secp256k1_context_randomize
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:702
count
static int count
Definition: tests_exhaustive.c:27