PIVX Core  5.6.99
P2P Digital Currency
ecmult_impl.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (c) 2013, 2014, 2017 Pieter Wuille, Andrew Poelstra, Jonas Nick *
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 #ifndef SECP256K1_ECMULT_IMPL_H
8 #define SECP256K1_ECMULT_IMPL_H
9 
10 #include <string.h>
11 #include <stdint.h>
12 
13 #include "group.h"
14 #include "scalar.h"
15 #include "ecmult.h"
16 
17 #if defined(EXHAUSTIVE_TEST_ORDER)
18 /* We need to lower these values for exhaustive tests because
19  * the tables cannot have infinities in them (this breaks the
20  * affine-isomorphism stuff which tracks z-ratios) */
21 # if EXHAUSTIVE_TEST_ORDER > 128
22 # define WINDOW_A 5
23 # define WINDOW_G 8
24 # elif EXHAUSTIVE_TEST_ORDER > 8
25 # define WINDOW_A 4
26 # define WINDOW_G 4
27 # else
28 # define WINDOW_A 2
29 # define WINDOW_G 2
30 # endif
31 #else
32 /* optimal for 128-bit and 256-bit exponents. */
33 #define WINDOW_A 5
36 #ifdef USE_ENDOMORPHISM
38 #define WINDOW_G 15
39 #else
41 #define WINDOW_G 16
42 #endif
43 #endif
44 
45 #ifdef USE_ENDOMORPHISM
46  #define WNAF_BITS 128
47 #else
48  #define WNAF_BITS 256
49 #endif
50 #define WNAF_SIZE_BITS(bits, w) (((bits) + (w) - 1) / (w))
51 #define WNAF_SIZE(w) WNAF_SIZE_BITS(WNAF_BITS, w)
52 
54 #define ECMULT_TABLE_SIZE(w) (1 << ((w)-2))
55 
56 /* The number of objects allocated on the scratch space for ecmult_multi algorithms */
57 #define PIPPENGER_SCRATCH_OBJECTS 6
58 #define STRAUSS_SCRATCH_OBJECTS 6
59 
60 #define PIPPENGER_MAX_BUCKET_WINDOW 12
61 
62 /* Minimum number of points for which pippenger_wnaf is faster than strauss wnaf */
63 #ifdef USE_ENDOMORPHISM
64  #define ECMULT_PIPPENGER_THRESHOLD 88
65 #else
66  #define ECMULT_PIPPENGER_THRESHOLD 160
67 #endif
68 
69 #ifdef USE_ENDOMORPHISM
70  #define ECMULT_MAX_POINTS_PER_BATCH 5000000
71 #else
72  #define ECMULT_MAX_POINTS_PER_BATCH 10000000
73 #endif
74 
80 static void secp256k1_ecmult_odd_multiples_table(int n, secp256k1_gej *prej, secp256k1_fe *zr, const secp256k1_gej *a) {
81  secp256k1_gej d;
82  secp256k1_ge a_ge, d_ge;
83  int i;
84 
86 
87  secp256k1_gej_double_var(&d, a, NULL);
88 
89  /*
90  * Perform the additions on an isomorphism where 'd' is affine: drop the z coordinate
91  * of 'd', and scale the 1P starting value's x/y coordinates without changing its z.
92  */
93  d_ge.x = d.x;
94  d_ge.y = d.y;
95  d_ge.infinity = 0;
96 
97  secp256k1_ge_set_gej_zinv(&a_ge, a, &d.z);
98  prej[0].x = a_ge.x;
99  prej[0].y = a_ge.y;
100  prej[0].z = a->z;
101  prej[0].infinity = 0;
102 
103  zr[0] = d.z;
104  for (i = 1; i < n; i++) {
105  secp256k1_gej_add_ge_var(&prej[i], &prej[i-1], &d_ge, &zr[i]);
106  }
107 
108  /*
109  * Each point in 'prej' has a z coordinate too small by a factor of 'd.z'. Only
110  * the final point's z coordinate is actually used though, so just update that.
111  */
112  secp256k1_fe_mul(&prej[n-1].z, &prej[n-1].z, &d.z);
113 }
114 
130 static void secp256k1_ecmult_odd_multiples_table_globalz_windowa(secp256k1_ge *pre, secp256k1_fe *globalz, const secp256k1_gej *a) {
133 
134  /* Compute the odd multiples in Jacobian form. */
135  secp256k1_ecmult_odd_multiples_table(ECMULT_TABLE_SIZE(WINDOW_A), prej, zr, a);
136  /* Bring them to the same Z denominator. */
137  secp256k1_ge_globalz_set_table_gej(ECMULT_TABLE_SIZE(WINDOW_A), pre, globalz, prej, zr);
138 }
139 
140 static void secp256k1_ecmult_odd_multiples_table_storage_var(int n, secp256k1_ge_storage *pre, const secp256k1_gej *a, const secp256k1_callback *cb) {
141  secp256k1_gej *prej = (secp256k1_gej*)checked_malloc(cb, sizeof(secp256k1_gej) * n);
142  secp256k1_ge *prea = (secp256k1_ge*)checked_malloc(cb, sizeof(secp256k1_ge) * n);
143  secp256k1_fe *zr = (secp256k1_fe*)checked_malloc(cb, sizeof(secp256k1_fe) * n);
144  int i;
145 
146  /* Compute the odd multiples in Jacobian form. */
147  secp256k1_ecmult_odd_multiples_table(n, prej, zr, a);
148  /* Convert them in batch to affine coordinates. */
149  secp256k1_ge_set_table_gej_var(prea, prej, zr, n);
150  /* Convert them to compact storage form. */
151  for (i = 0; i < n; i++) {
152  secp256k1_ge_to_storage(&pre[i], &prea[i]);
153  }
154 
155  free(prea);
156  free(prej);
157  free(zr);
158 }
159 
162 #define ECMULT_TABLE_GET_GE(r,pre,n,w) do { \
163  VERIFY_CHECK(((n) & 1) == 1); \
164  VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \
165  VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \
166  if ((n) > 0) { \
167  *(r) = (pre)[((n)-1)/2]; \
168  } else { \
169  secp256k1_ge_neg((r), &(pre)[(-(n)-1)/2]); \
170  } \
171 } while(0)
172 
173 #define ECMULT_TABLE_GET_GE_STORAGE(r,pre,n,w) do { \
174  VERIFY_CHECK(((n) & 1) == 1); \
175  VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \
176  VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \
177  if ((n) > 0) { \
178  secp256k1_ge_from_storage((r), &(pre)[((n)-1)/2]); \
179  } else { \
180  secp256k1_ge_from_storage((r), &(pre)[(-(n)-1)/2]); \
181  secp256k1_ge_neg((r), (r)); \
182  } \
183 } while(0)
184 
185 static void secp256k1_ecmult_context_init(secp256k1_ecmult_context *ctx) {
186  ctx->pre_g = NULL;
187 #ifdef USE_ENDOMORPHISM
188  ctx->pre_g_128 = NULL;
189 #endif
190 }
191 
192 static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, const secp256k1_callback *cb) {
193  secp256k1_gej gj;
194 
195  if (ctx->pre_g != NULL) {
196  return;
197  }
198 
199  /* get the generator */
200  secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g);
201 
202  ctx->pre_g = (secp256k1_ge_storage (*)[])checked_malloc(cb, sizeof((*ctx->pre_g)[0]) * ECMULT_TABLE_SIZE(WINDOW_G));
203 
204  /* precompute the tables with odd multiples */
205  secp256k1_ecmult_odd_multiples_table_storage_var(ECMULT_TABLE_SIZE(WINDOW_G), *ctx->pre_g, &gj, cb);
206 
207 #ifdef USE_ENDOMORPHISM
208  {
209  secp256k1_gej g_128j;
210  int i;
211 
212  ctx->pre_g_128 = (secp256k1_ge_storage (*)[])checked_malloc(cb, sizeof((*ctx->pre_g_128)[0]) * ECMULT_TABLE_SIZE(WINDOW_G));
213 
214  /* calculate 2^128*generator */
215  g_128j = gj;
216  for (i = 0; i < 128; i++) {
217  secp256k1_gej_double_var(&g_128j, &g_128j, NULL);
218  }
219  secp256k1_ecmult_odd_multiples_table_storage_var(ECMULT_TABLE_SIZE(WINDOW_G), *ctx->pre_g_128, &g_128j, cb);
220  }
221 #endif
222 }
223 
224 static void secp256k1_ecmult_context_clone(secp256k1_ecmult_context *dst,
225  const secp256k1_ecmult_context *src, const secp256k1_callback *cb) {
226  if (src->pre_g == NULL) {
227  dst->pre_g = NULL;
228  } else {
229  size_t size = sizeof((*dst->pre_g)[0]) * ECMULT_TABLE_SIZE(WINDOW_G);
230  dst->pre_g = (secp256k1_ge_storage (*)[])checked_malloc(cb, size);
231  memcpy(dst->pre_g, src->pre_g, size);
232  }
233 #ifdef USE_ENDOMORPHISM
234  if (src->pre_g_128 == NULL) {
235  dst->pre_g_128 = NULL;
236  } else {
237  size_t size = sizeof((*dst->pre_g_128)[0]) * ECMULT_TABLE_SIZE(WINDOW_G);
238  dst->pre_g_128 = (secp256k1_ge_storage (*)[])checked_malloc(cb, size);
239  memcpy(dst->pre_g_128, src->pre_g_128, size);
240  }
241 #endif
242 }
243 
244 static int secp256k1_ecmult_context_is_built(const secp256k1_ecmult_context *ctx) {
245  return ctx->pre_g != NULL;
246 }
247 
248 static void secp256k1_ecmult_context_clear(secp256k1_ecmult_context *ctx) {
249  free(ctx->pre_g);
250 #ifdef USE_ENDOMORPHISM
251  free(ctx->pre_g_128);
252 #endif
253  secp256k1_ecmult_context_init(ctx);
254 }
255 
263 static int secp256k1_ecmult_wnaf(int *wnaf, int len, const secp256k1_scalar *a, int w) {
264  secp256k1_scalar s = *a;
265  int last_set_bit = -1;
266  int bit = 0;
267  int sign = 1;
268  int carry = 0;
269 
270  VERIFY_CHECK(wnaf != NULL);
271  VERIFY_CHECK(0 <= len && len <= 256);
272  VERIFY_CHECK(a != NULL);
273  VERIFY_CHECK(2 <= w && w <= 31);
274 
275  memset(wnaf, 0, len * sizeof(wnaf[0]));
276 
277  if (secp256k1_scalar_get_bits(&s, 255, 1)) {
278  secp256k1_scalar_negate(&s, &s);
279  sign = -1;
280  }
281 
282  while (bit < len) {
283  int now;
284  int word;
285  if (secp256k1_scalar_get_bits(&s, bit, 1) == (unsigned int)carry) {
286  bit++;
287  continue;
288  }
289 
290  now = w;
291  if (now > len - bit) {
292  now = len - bit;
293  }
294 
295  word = secp256k1_scalar_get_bits_var(&s, bit, now) + carry;
296 
297  carry = (word >> (w-1)) & 1;
298  word -= carry << w;
299 
300  wnaf[bit] = sign * word;
301  last_set_bit = bit;
302 
303  bit += now;
304  }
305 #ifdef VERIFY
306  CHECK(carry == 0);
307  while (bit < 256) {
308  CHECK(secp256k1_scalar_get_bits(&s, bit++, 1) == 0);
309  }
310 #endif
311  return last_set_bit + 1;
312 }
313 
315 #ifdef USE_ENDOMORPHISM
316  secp256k1_scalar na_1, na_lam;
317  int wnaf_na_1[130];
318  int wnaf_na_lam[130];
319  int bits_na_1;
320  int bits_na_lam;
321 #else
322  int wnaf_na[256];
323  int bits_na;
324 #endif
325  size_t input_pos;
326 };
327 
332 #ifdef USE_ENDOMORPHISM
333  secp256k1_ge* pre_a_lam;
334 #endif
336 };
337 
338 static void secp256k1_ecmult_strauss_wnaf(const secp256k1_ecmult_context *ctx, const struct secp256k1_strauss_state *state, secp256k1_gej *r, int num, const secp256k1_gej *a, const secp256k1_scalar *na, const secp256k1_scalar *ng) {
339  secp256k1_ge tmpa;
340  secp256k1_fe Z;
341 #ifdef USE_ENDOMORPHISM
342  /* Splitted G factors. */
343  secp256k1_scalar ng_1, ng_128;
344  int wnaf_ng_1[129];
345  int bits_ng_1 = 0;
346  int wnaf_ng_128[129];
347  int bits_ng_128 = 0;
348 #else
349  int wnaf_ng[256];
350  int bits_ng = 0;
351 #endif
352  int i;
353  int bits = 0;
354  int np;
355  int no = 0;
356 
357  for (np = 0; np < num; ++np) {
358  if (secp256k1_scalar_is_zero(&na[np]) || secp256k1_gej_is_infinity(&a[np])) {
359  continue;
360  }
361  state->ps[no].input_pos = np;
362 #ifdef USE_ENDOMORPHISM
363  /* split na into na_1 and na_lam (where na = na_1 + na_lam*lambda, and na_1 and na_lam are ~128 bit) */
364  secp256k1_scalar_split_lambda(&state->ps[no].na_1, &state->ps[no].na_lam, &na[np]);
365 
366  /* build wnaf representation for na_1 and na_lam. */
367  state->ps[no].bits_na_1 = secp256k1_ecmult_wnaf(state->ps[no].wnaf_na_1, 130, &state->ps[no].na_1, WINDOW_A);
368  state->ps[no].bits_na_lam = secp256k1_ecmult_wnaf(state->ps[no].wnaf_na_lam, 130, &state->ps[no].na_lam, WINDOW_A);
369  VERIFY_CHECK(state->ps[no].bits_na_1 <= 130);
370  VERIFY_CHECK(state->ps[no].bits_na_lam <= 130);
371  if (state->ps[no].bits_na_1 > bits) {
372  bits = state->ps[no].bits_na_1;
373  }
374  if (state->ps[no].bits_na_lam > bits) {
375  bits = state->ps[no].bits_na_lam;
376  }
377 #else
378  /* build wnaf representation for na. */
379  state->ps[no].bits_na = secp256k1_ecmult_wnaf(state->ps[no].wnaf_na, 256, &na[np], WINDOW_A);
380  if (state->ps[no].bits_na > bits) {
381  bits = state->ps[no].bits_na;
382  }
383 #endif
384  ++no;
385  }
386 
387  /* Calculate odd multiples of a.
388  * All multiples are brought to the same Z 'denominator', which is stored
389  * in Z. Due to secp256k1' isomorphism we can do all operations pretending
390  * that the Z coordinate was 1, use affine addition formulae, and correct
391  * the Z coordinate of the result once at the end.
392  * The exception is the precomputed G table points, which are actually
393  * affine. Compared to the base used for other points, they have a Z ratio
394  * of 1/Z, so we can use secp256k1_gej_add_zinv_var, which uses the same
395  * isomorphism to efficiently add with a known Z inverse.
396  */
397  if (no > 0) {
398  /* Compute the odd multiples in Jacobian form. */
399  secp256k1_ecmult_odd_multiples_table(ECMULT_TABLE_SIZE(WINDOW_A), state->prej, state->zr, &a[state->ps[0].input_pos]);
400  for (np = 1; np < no; ++np) {
401  secp256k1_gej tmp = a[state->ps[np].input_pos];
402 #ifdef VERIFY
403  secp256k1_fe_normalize_var(&(state->prej[(np - 1) * ECMULT_TABLE_SIZE(WINDOW_A) + ECMULT_TABLE_SIZE(WINDOW_A) - 1].z));
404 #endif
405  secp256k1_gej_rescale(&tmp, &(state->prej[(np - 1) * ECMULT_TABLE_SIZE(WINDOW_A) + ECMULT_TABLE_SIZE(WINDOW_A) - 1].z));
406  secp256k1_ecmult_odd_multiples_table(ECMULT_TABLE_SIZE(WINDOW_A), state->prej + np * ECMULT_TABLE_SIZE(WINDOW_A), state->zr + np * ECMULT_TABLE_SIZE(WINDOW_A), &tmp);
407  secp256k1_fe_mul(state->zr + np * ECMULT_TABLE_SIZE(WINDOW_A), state->zr + np * ECMULT_TABLE_SIZE(WINDOW_A), &(a[state->ps[np].input_pos].z));
408  }
409  /* Bring them to the same Z denominator. */
410  secp256k1_ge_globalz_set_table_gej(ECMULT_TABLE_SIZE(WINDOW_A) * no, state->pre_a, &Z, state->prej, state->zr);
411  } else {
412  secp256k1_fe_set_int(&Z, 1);
413  }
414 
415 #ifdef USE_ENDOMORPHISM
416  for (np = 0; np < no; ++np) {
417  for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) {
418  secp256k1_ge_mul_lambda(&state->pre_a_lam[np * ECMULT_TABLE_SIZE(WINDOW_A) + i], &state->pre_a[np * ECMULT_TABLE_SIZE(WINDOW_A) + i]);
419  }
420  }
421 
422  if (ng) {
423  /* split ng into ng_1 and ng_128 (where gn = gn_1 + gn_128*2^128, and gn_1 and gn_128 are ~128 bit) */
424  secp256k1_scalar_split_128(&ng_1, &ng_128, ng);
425 
426  /* Build wnaf representation for ng_1 and ng_128 */
427  bits_ng_1 = secp256k1_ecmult_wnaf(wnaf_ng_1, 129, &ng_1, WINDOW_G);
428  bits_ng_128 = secp256k1_ecmult_wnaf(wnaf_ng_128, 129, &ng_128, WINDOW_G);
429  if (bits_ng_1 > bits) {
430  bits = bits_ng_1;
431  }
432  if (bits_ng_128 > bits) {
433  bits = bits_ng_128;
434  }
435  }
436 #else
437  if (ng) {
438  bits_ng = secp256k1_ecmult_wnaf(wnaf_ng, 256, ng, WINDOW_G);
439  if (bits_ng > bits) {
440  bits = bits_ng;
441  }
442  }
443 #endif
444 
445  secp256k1_gej_set_infinity(r);
446 
447  for (i = bits - 1; i >= 0; i--) {
448  int n;
449  secp256k1_gej_double_var(r, r, NULL);
450 #ifdef USE_ENDOMORPHISM
451  for (np = 0; np < no; ++np) {
452  if (i < state->ps[np].bits_na_1 && (n = state->ps[np].wnaf_na_1[i])) {
453  ECMULT_TABLE_GET_GE(&tmpa, state->pre_a + np * ECMULT_TABLE_SIZE(WINDOW_A), n, WINDOW_A);
454  secp256k1_gej_add_ge_var(r, r, &tmpa, NULL);
455  }
456  if (i < state->ps[np].bits_na_lam && (n = state->ps[np].wnaf_na_lam[i])) {
457  ECMULT_TABLE_GET_GE(&tmpa, state->pre_a_lam + np * ECMULT_TABLE_SIZE(WINDOW_A), n, WINDOW_A);
458  secp256k1_gej_add_ge_var(r, r, &tmpa, NULL);
459  }
460  }
461  if (i < bits_ng_1 && (n = wnaf_ng_1[i])) {
462  ECMULT_TABLE_GET_GE_STORAGE(&tmpa, *ctx->pre_g, n, WINDOW_G);
463  secp256k1_gej_add_zinv_var(r, r, &tmpa, &Z);
464  }
465  if (i < bits_ng_128 && (n = wnaf_ng_128[i])) {
466  ECMULT_TABLE_GET_GE_STORAGE(&tmpa, *ctx->pre_g_128, n, WINDOW_G);
467  secp256k1_gej_add_zinv_var(r, r, &tmpa, &Z);
468  }
469 #else
470  for (np = 0; np < no; ++np) {
471  if (i < state->ps[np].bits_na && (n = state->ps[np].wnaf_na[i])) {
472  ECMULT_TABLE_GET_GE(&tmpa, state->pre_a + np * ECMULT_TABLE_SIZE(WINDOW_A), n, WINDOW_A);
473  secp256k1_gej_add_ge_var(r, r, &tmpa, NULL);
474  }
475  }
476  if (i < bits_ng && (n = wnaf_ng[i])) {
477  ECMULT_TABLE_GET_GE_STORAGE(&tmpa, *ctx->pre_g, n, WINDOW_G);
478  secp256k1_gej_add_zinv_var(r, r, &tmpa, &Z);
479  }
480 #endif
481  }
482 
483  if (!r->infinity) {
484  secp256k1_fe_mul(&r->z, &r->z, &Z);
485  }
486 }
487 
488 static void secp256k1_ecmult(const secp256k1_ecmult_context *ctx, secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_scalar *na, const secp256k1_scalar *ng) {
492  struct secp256k1_strauss_point_state ps[1];
493 #ifdef USE_ENDOMORPHISM
495 #endif
496  struct secp256k1_strauss_state state;
497 
498  state.prej = prej;
499  state.zr = zr;
500  state.pre_a = pre_a;
501 #ifdef USE_ENDOMORPHISM
502  state.pre_a_lam = pre_a_lam;
503 #endif
504  state.ps = ps;
505  secp256k1_ecmult_strauss_wnaf(ctx, &state, r, 1, a, na, ng);
506 }
507 
508 static size_t secp256k1_strauss_scratch_size(size_t n_points) {
509 #ifdef USE_ENDOMORPHISM
510  static const size_t point_size = (2 * sizeof(secp256k1_ge) + sizeof(secp256k1_gej) + sizeof(secp256k1_fe)) * ECMULT_TABLE_SIZE(WINDOW_A) + sizeof(struct secp256k1_strauss_point_state) + sizeof(secp256k1_gej) + sizeof(secp256k1_scalar);
511 #else
512  static const size_t point_size = (sizeof(secp256k1_ge) + sizeof(secp256k1_gej) + sizeof(secp256k1_fe)) * ECMULT_TABLE_SIZE(WINDOW_A) + sizeof(struct secp256k1_strauss_point_state) + sizeof(secp256k1_gej) + sizeof(secp256k1_scalar);
513 #endif
514  return n_points*point_size;
515 }
516 
517 static int secp256k1_ecmult_strauss_batch(const secp256k1_ecmult_context *ctx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n_points, size_t cb_offset) {
518  secp256k1_gej* points;
519  secp256k1_scalar* scalars;
520  struct secp256k1_strauss_state state;
521  size_t i;
522 
523  secp256k1_gej_set_infinity(r);
524  if (inp_g_sc == NULL && n_points == 0) {
525  return 1;
526  }
527 
528  if (!secp256k1_scratch_allocate_frame(scratch, secp256k1_strauss_scratch_size(n_points), STRAUSS_SCRATCH_OBJECTS)) {
529  return 0;
530  }
531  points = (secp256k1_gej*)secp256k1_scratch_alloc(scratch, n_points * sizeof(secp256k1_gej));
532  scalars = (secp256k1_scalar*)secp256k1_scratch_alloc(scratch, n_points * sizeof(secp256k1_scalar));
533  state.prej = (secp256k1_gej*)secp256k1_scratch_alloc(scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_gej));
534  state.zr = (secp256k1_fe*)secp256k1_scratch_alloc(scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_fe));
535 #ifdef USE_ENDOMORPHISM
536  state.pre_a = (secp256k1_ge*)secp256k1_scratch_alloc(scratch, n_points * 2 * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_ge));
537  state.pre_a_lam = state.pre_a + n_points * ECMULT_TABLE_SIZE(WINDOW_A);
538 #else
539  state.pre_a = (secp256k1_ge*)secp256k1_scratch_alloc(scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_ge));
540 #endif
541  state.ps = (struct secp256k1_strauss_point_state*)secp256k1_scratch_alloc(scratch, n_points * sizeof(struct secp256k1_strauss_point_state));
542 
543  for (i = 0; i < n_points; i++) {
544  secp256k1_ge point;
545  if (!cb(&scalars[i], &point, i+cb_offset, cbdata)) {
546  secp256k1_scratch_deallocate_frame(scratch);
547  return 0;
548  }
549  secp256k1_gej_set_ge(&points[i], &point);
550  }
551  secp256k1_ecmult_strauss_wnaf(ctx, &state, r, n_points, points, scalars, inp_g_sc);
552  secp256k1_scratch_deallocate_frame(scratch);
553  return 1;
554 }
555 
556 /* Wrapper for secp256k1_ecmult_multi_func interface */
557 static int secp256k1_ecmult_strauss_batch_single(const secp256k1_ecmult_context *actx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n) {
558  return secp256k1_ecmult_strauss_batch(actx, scratch, r, inp_g_sc, cb, cbdata, n, 0);
559 }
560 
561 static size_t secp256k1_strauss_max_points(secp256k1_scratch *scratch) {
562  return secp256k1_scratch_max_allocation(scratch, STRAUSS_SCRATCH_OBJECTS) / secp256k1_strauss_scratch_size(1);
563 }
564 
572 static int secp256k1_wnaf_fixed(int *wnaf, const secp256k1_scalar *s, int w) {
573  int skew = 0;
574  int pos;
575  int max_pos;
576  int last_w;
577  const secp256k1_scalar *work = s;
578 
579  if (secp256k1_scalar_is_zero(s)) {
580  for (pos = 0; pos < WNAF_SIZE(w); pos++) {
581  wnaf[pos] = 0;
582  }
583  return 0;
584  }
585 
586  if (secp256k1_scalar_is_even(s)) {
587  skew = 1;
588  }
589 
590  wnaf[0] = secp256k1_scalar_get_bits_var(work, 0, w) + skew;
591  /* Compute last window size. Relevant when window size doesn't divide the
592  * number of bits in the scalar */
593  last_w = WNAF_BITS - (WNAF_SIZE(w) - 1) * w;
594 
595  /* Store the position of the first nonzero word in max_pos to allow
596  * skipping leading zeros when calculating the wnaf. */
597  for (pos = WNAF_SIZE(w) - 1; pos > 0; pos--) {
598  int val = secp256k1_scalar_get_bits_var(work, pos * w, pos == WNAF_SIZE(w)-1 ? last_w : w);
599  if(val != 0) {
600  break;
601  }
602  wnaf[pos] = 0;
603  }
604  max_pos = pos;
605  pos = 1;
606 
607  while (pos <= max_pos) {
608  int val = secp256k1_scalar_get_bits_var(work, pos * w, pos == WNAF_SIZE(w)-1 ? last_w : w);
609  if ((val & 1) == 0) {
610  wnaf[pos - 1] -= (1 << w);
611  wnaf[pos] = (val + 1);
612  } else {
613  wnaf[pos] = val;
614  }
615  /* Set a coefficient to zero if it is 1 or -1 and the proceeding digit
616  * is strictly negative or strictly positive respectively. Only change
617  * coefficients at previous positions because above code assumes that
618  * wnaf[pos - 1] is odd.
619  */
620  if (pos >= 2 && ((wnaf[pos - 1] == 1 && wnaf[pos - 2] < 0) || (wnaf[pos - 1] == -1 && wnaf[pos - 2] > 0))) {
621  if (wnaf[pos - 1] == 1) {
622  wnaf[pos - 2] += 1 << w;
623  } else {
624  wnaf[pos - 2] -= 1 << w;
625  }
626  wnaf[pos - 1] = 0;
627  }
628  ++pos;
629  }
630 
631  return skew;
632 }
633 
635  int skew_na;
636  size_t input_pos;
637 };
638 
640  int *wnaf_na;
642 };
643 
644 /*
645  * pippenger_wnaf computes the result of a multi-point multiplication as
646  * follows: The scalars are brought into wnaf with n_wnaf elements each. Then
647  * for every i < n_wnaf, first each point is added to a "bucket" corresponding
648  * to the point's wnaf[i]. Second, the buckets are added together such that
649  * r += 1*bucket[0] + 3*bucket[1] + 5*bucket[2] + ...
650  */
651 static int secp256k1_ecmult_pippenger_wnaf(secp256k1_gej *buckets, int bucket_window, struct secp256k1_pippenger_state *state, secp256k1_gej *r, const secp256k1_scalar *sc, const secp256k1_ge *pt, size_t num) {
652  size_t n_wnaf = WNAF_SIZE(bucket_window+1);
653  size_t np;
654  size_t no = 0;
655  int i;
656  int j;
657 
658  for (np = 0; np < num; ++np) {
659  if (secp256k1_scalar_is_zero(&sc[np]) || secp256k1_ge_is_infinity(&pt[np])) {
660  continue;
661  }
662  state->ps[no].input_pos = np;
663  state->ps[no].skew_na = secp256k1_wnaf_fixed(&state->wnaf_na[no*n_wnaf], &sc[np], bucket_window+1);
664  no++;
665  }
666  secp256k1_gej_set_infinity(r);
667 
668  if (no == 0) {
669  return 1;
670  }
671 
672  for (i = n_wnaf - 1; i >= 0; i--) {
673  secp256k1_gej running_sum;
674 
675  for(j = 0; j < ECMULT_TABLE_SIZE(bucket_window+2); j++) {
676  secp256k1_gej_set_infinity(&buckets[j]);
677  }
678 
679  for (np = 0; np < no; ++np) {
680  int n = state->wnaf_na[np*n_wnaf + i];
681  struct secp256k1_pippenger_point_state point_state = state->ps[np];
682  secp256k1_ge tmp;
683  int idx;
684 
685  if (i == 0) {
686  /* correct for wnaf skew */
687  int skew = point_state.skew_na;
688  if (skew) {
689  secp256k1_ge_neg(&tmp, &pt[point_state.input_pos]);
690  secp256k1_gej_add_ge_var(&buckets[0], &buckets[0], &tmp, NULL);
691  }
692  }
693  if (n > 0) {
694  idx = (n - 1)/2;
695  secp256k1_gej_add_ge_var(&buckets[idx], &buckets[idx], &pt[point_state.input_pos], NULL);
696  } else if (n < 0) {
697  idx = -(n + 1)/2;
698  secp256k1_ge_neg(&tmp, &pt[point_state.input_pos]);
699  secp256k1_gej_add_ge_var(&buckets[idx], &buckets[idx], &tmp, NULL);
700  }
701  }
702 
703  for(j = 0; j < bucket_window; j++) {
704  secp256k1_gej_double_var(r, r, NULL);
705  }
706 
707  secp256k1_gej_set_infinity(&running_sum);
708  /* Accumulate the sum: bucket[0] + 3*bucket[1] + 5*bucket[2] + 7*bucket[3] + ...
709  * = bucket[0] + bucket[1] + bucket[2] + bucket[3] + ...
710  * + 2 * (bucket[1] + 2*bucket[2] + 3*bucket[3] + ...)
711  * using an intermediate running sum:
712  * running_sum = bucket[0] + bucket[1] + bucket[2] + ...
713  *
714  * The doubling is done implicitly by deferring the final window doubling (of 'r').
715  */
716  for(j = ECMULT_TABLE_SIZE(bucket_window+2) - 1; j > 0; j--) {
717  secp256k1_gej_add_var(&running_sum, &running_sum, &buckets[j], NULL);
718  secp256k1_gej_add_var(r, r, &running_sum, NULL);
719  }
720 
721  secp256k1_gej_add_var(&running_sum, &running_sum, &buckets[0], NULL);
722  secp256k1_gej_double_var(r, r, NULL);
723  secp256k1_gej_add_var(r, r, &running_sum, NULL);
724  }
725  return 1;
726 }
727 
732 static int secp256k1_pippenger_bucket_window(size_t n) {
733 #ifdef USE_ENDOMORPHISM
734  if (n <= 1) {
735  return 1;
736  } else if (n <= 4) {
737  return 2;
738  } else if (n <= 20) {
739  return 3;
740  } else if (n <= 57) {
741  return 4;
742  } else if (n <= 136) {
743  return 5;
744  } else if (n <= 235) {
745  return 6;
746  } else if (n <= 1260) {
747  return 7;
748  } else if (n <= 4420) {
749  return 9;
750  } else if (n <= 7880) {
751  return 10;
752  } else if (n <= 16050) {
753  return 11;
754  } else {
756  }
757 #else
758  if (n <= 1) {
759  return 1;
760  } else if (n <= 11) {
761  return 2;
762  } else if (n <= 45) {
763  return 3;
764  } else if (n <= 100) {
765  return 4;
766  } else if (n <= 275) {
767  return 5;
768  } else if (n <= 625) {
769  return 6;
770  } else if (n <= 1850) {
771  return 7;
772  } else if (n <= 3400) {
773  return 8;
774  } else if (n <= 9630) {
775  return 9;
776  } else if (n <= 17900) {
777  return 10;
778  } else if (n <= 32800) {
779  return 11;
780  } else {
782  }
783 #endif
784 }
785 
789 static size_t secp256k1_pippenger_bucket_window_inv(int bucket_window) {
790  switch(bucket_window) {
791 #ifdef USE_ENDOMORPHISM
792  case 1: return 1;
793  case 2: return 4;
794  case 3: return 20;
795  case 4: return 57;
796  case 5: return 136;
797  case 6: return 235;
798  case 7: return 1260;
799  case 8: return 1260;
800  case 9: return 4420;
801  case 10: return 7880;
802  case 11: return 16050;
803  case PIPPENGER_MAX_BUCKET_WINDOW: return SIZE_MAX;
804 #else
805  case 1: return 1;
806  case 2: return 11;
807  case 3: return 45;
808  case 4: return 100;
809  case 5: return 275;
810  case 6: return 625;
811  case 7: return 1850;
812  case 8: return 3400;
813  case 9: return 9630;
814  case 10: return 17900;
815  case 11: return 32800;
816  case PIPPENGER_MAX_BUCKET_WINDOW: return SIZE_MAX;
817 #endif
818  }
819  return 0;
820 }
821 
822 
823 #ifdef USE_ENDOMORPHISM
824 SECP256K1_INLINE static void secp256k1_ecmult_endo_split(secp256k1_scalar *s1, secp256k1_scalar *s2, secp256k1_ge *p1, secp256k1_ge *p2) {
825  secp256k1_scalar tmp = *s1;
826  secp256k1_scalar_split_lambda(s1, s2, &tmp);
827  secp256k1_ge_mul_lambda(p2, p1);
828 
829  if (secp256k1_scalar_is_high(s1)) {
830  secp256k1_scalar_negate(s1, s1);
831  secp256k1_ge_neg(p1, p1);
832  }
833  if (secp256k1_scalar_is_high(s2)) {
834  secp256k1_scalar_negate(s2, s2);
835  secp256k1_ge_neg(p2, p2);
836  }
837 }
838 #endif
839 
844 static size_t secp256k1_pippenger_scratch_size(size_t n_points, int bucket_window) {
845 #ifdef USE_ENDOMORPHISM
846  size_t entries = 2*n_points + 2;
847 #else
848  size_t entries = n_points + 1;
849 #endif
850  size_t entry_size = sizeof(secp256k1_ge) + sizeof(secp256k1_scalar) + sizeof(struct secp256k1_pippenger_point_state) + (WNAF_SIZE(bucket_window+1)+1)*sizeof(int);
851  return ((1<<bucket_window) * sizeof(secp256k1_gej) + sizeof(struct secp256k1_pippenger_state) + entries * entry_size);
852 }
853 
854 static int secp256k1_ecmult_pippenger_batch(const secp256k1_ecmult_context *ctx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n_points, size_t cb_offset) {
855  /* Use 2(n+1) with the endomorphism, n+1 without, when calculating batch
856  * sizes. The reason for +1 is that we add the G scalar to the list of
857  * other scalars. */
858 #ifdef USE_ENDOMORPHISM
859  size_t entries = 2*n_points + 2;
860 #else
861  size_t entries = n_points + 1;
862 #endif
863  secp256k1_ge *points;
864  secp256k1_scalar *scalars;
865  secp256k1_gej *buckets;
866  struct secp256k1_pippenger_state *state_space;
867  size_t idx = 0;
868  size_t point_idx = 0;
869  int i, j;
870  int bucket_window;
871 
872  (void)ctx;
873  secp256k1_gej_set_infinity(r);
874  if (inp_g_sc == NULL && n_points == 0) {
875  return 1;
876  }
877 
878  bucket_window = secp256k1_pippenger_bucket_window(n_points);
879  if (!secp256k1_scratch_allocate_frame(scratch, secp256k1_pippenger_scratch_size(n_points, bucket_window), PIPPENGER_SCRATCH_OBJECTS)) {
880  return 0;
881  }
882  points = (secp256k1_ge *) secp256k1_scratch_alloc(scratch, entries * sizeof(*points));
883  scalars = (secp256k1_scalar *) secp256k1_scratch_alloc(scratch, entries * sizeof(*scalars));
884  state_space = (struct secp256k1_pippenger_state *) secp256k1_scratch_alloc(scratch, sizeof(*state_space));
885  state_space->ps = (struct secp256k1_pippenger_point_state *) secp256k1_scratch_alloc(scratch, entries * sizeof(*state_space->ps));
886  state_space->wnaf_na = (int *) secp256k1_scratch_alloc(scratch, entries*(WNAF_SIZE(bucket_window+1)) * sizeof(int));
887  buckets = (secp256k1_gej *) secp256k1_scratch_alloc(scratch, (1<<bucket_window) * sizeof(*buckets));
888 
889  if (inp_g_sc != NULL) {
890  scalars[0] = *inp_g_sc;
891  points[0] = secp256k1_ge_const_g;
892  idx++;
893 #ifdef USE_ENDOMORPHISM
894  secp256k1_ecmult_endo_split(&scalars[0], &scalars[1], &points[0], &points[1]);
895  idx++;
896 #endif
897  }
898 
899  while (point_idx < n_points) {
900  if (!cb(&scalars[idx], &points[idx], point_idx + cb_offset, cbdata)) {
901  secp256k1_scratch_deallocate_frame(scratch);
902  return 0;
903  }
904  idx++;
905 #ifdef USE_ENDOMORPHISM
906  secp256k1_ecmult_endo_split(&scalars[idx - 1], &scalars[idx], &points[idx - 1], &points[idx]);
907  idx++;
908 #endif
909  point_idx++;
910  }
911 
912  secp256k1_ecmult_pippenger_wnaf(buckets, bucket_window, state_space, r, scalars, points, idx);
913 
914  /* Clear data */
915  for(i = 0; (size_t)i < idx; i++) {
916  secp256k1_scalar_clear(&scalars[i]);
917  state_space->ps[i].skew_na = 0;
918  for(j = 0; j < WNAF_SIZE(bucket_window+1); j++) {
919  state_space->wnaf_na[i * WNAF_SIZE(bucket_window+1) + j] = 0;
920  }
921  }
922  for(i = 0; i < 1<<bucket_window; i++) {
923  secp256k1_gej_clear(&buckets[i]);
924  }
925  secp256k1_scratch_deallocate_frame(scratch);
926  return 1;
927 }
928 
929 /* Wrapper for secp256k1_ecmult_multi_func interface */
930 static int secp256k1_ecmult_pippenger_batch_single(const secp256k1_ecmult_context *actx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n) {
931  return secp256k1_ecmult_pippenger_batch(actx, scratch, r, inp_g_sc, cb, cbdata, n, 0);
932 }
933 
939 static size_t secp256k1_pippenger_max_points(secp256k1_scratch *scratch) {
940  size_t max_alloc = secp256k1_scratch_max_allocation(scratch, PIPPENGER_SCRATCH_OBJECTS);
941  int bucket_window;
942  size_t res = 0;
943 
944  for (bucket_window = 1; bucket_window <= PIPPENGER_MAX_BUCKET_WINDOW; bucket_window++) {
945  size_t n_points;
946  size_t max_points = secp256k1_pippenger_bucket_window_inv(bucket_window);
947  size_t space_for_points;
948  size_t space_overhead;
949  size_t entry_size = sizeof(secp256k1_ge) + sizeof(secp256k1_scalar) + sizeof(struct secp256k1_pippenger_point_state) + (WNAF_SIZE(bucket_window+1)+1)*sizeof(int);
950 
951 #ifdef USE_ENDOMORPHISM
952  entry_size = 2*entry_size;
953 #endif
954  space_overhead = ((1<<bucket_window) * sizeof(secp256k1_gej) + entry_size + sizeof(struct secp256k1_pippenger_state));
955  if (space_overhead > max_alloc) {
956  break;
957  }
958  space_for_points = max_alloc - space_overhead;
959 
960  n_points = space_for_points/entry_size;
961  n_points = n_points > max_points ? max_points : n_points;
962  if (n_points > res) {
963  res = n_points;
964  }
965  if (n_points < max_points) {
966  /* A larger bucket_window may support even more points. But if we
967  * would choose that then the caller couldn't safely use any number
968  * smaller than what this function returns */
969  break;
970  }
971  }
972  return res;
973 }
974 
976 static int secp256k1_ecmult_multi_var(const secp256k1_ecmult_context *ctx, secp256k1_scratch *scratch, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n) {
977  size_t i;
978 
979  int (*f)(const secp256k1_ecmult_context*, secp256k1_scratch*, secp256k1_gej*, const secp256k1_scalar*, secp256k1_ecmult_multi_callback cb, void*, size_t, size_t);
980  size_t max_points;
981  size_t n_batches;
982  size_t n_batch_points;
983 
984  secp256k1_gej_set_infinity(r);
985  if (inp_g_sc == NULL && n == 0) {
986  return 1;
987  } else if (n == 0) {
988  secp256k1_scalar szero;
989  secp256k1_scalar_set_int(&szero, 0);
990  secp256k1_ecmult(ctx, r, r, &szero, inp_g_sc);
991  return 1;
992  }
993 
994  max_points = secp256k1_pippenger_max_points(scratch);
995  if (max_points == 0) {
996  return 0;
997  } else if (max_points > ECMULT_MAX_POINTS_PER_BATCH) {
998  max_points = ECMULT_MAX_POINTS_PER_BATCH;
999  }
1000  n_batches = (n+max_points-1)/max_points;
1001  n_batch_points = (n+n_batches-1)/n_batches;
1002 
1003  if (n_batch_points >= ECMULT_PIPPENGER_THRESHOLD) {
1004  f = secp256k1_ecmult_pippenger_batch;
1005  } else {
1006  max_points = secp256k1_strauss_max_points(scratch);
1007  if (max_points == 0) {
1008  return 0;
1009  }
1010  n_batches = (n+max_points-1)/max_points;
1011  n_batch_points = (n+n_batches-1)/n_batches;
1012  f = secp256k1_ecmult_strauss_batch;
1013  }
1014  for(i = 0; i < n_batches; i++) {
1015  size_t nbp = n < n_batch_points ? n : n_batch_points;
1016  size_t offset = n_batch_points*i;
1017  secp256k1_gej tmp;
1018  if (!f(ctx, scratch, &tmp, i == 0 ? inp_g_sc : NULL, cb, cbdata, nbp, offset)) {
1019  return 0;
1020  }
1021  secp256k1_gej_add_var(r, r, &tmp, NULL);
1022  n -= nbp;
1023  }
1024  return 1;
1025 }
1026 
1027 #endif /* SECP256K1_ECMULT_IMPL_H */
int() secp256k1_ecmult_multi_callback(secp256k1_scalar *sc, secp256k1_ge *pt, size_t idx, void *data)
Definition: ecmult.h:33
#define STRAUSS_SCRATCH_OBJECTS
Definition: ecmult_impl.h:58
#define WNAF_SIZE(w)
Definition: ecmult_impl.h:51
#define ECMULT_TABLE_GET_GE_STORAGE(r, pre, n, w)
Definition: ecmult_impl.h:173
#define WINDOW_A
Definition: ecmult_impl.h:33
#define ECMULT_PIPPENGER_THRESHOLD
Definition: ecmult_impl.h:66
#define WNAF_BITS
Definition: ecmult_impl.h:48
#define ECMULT_MAX_POINTS_PER_BATCH
Definition: ecmult_impl.h:72
#define PIPPENGER_MAX_BUCKET_WINDOW
Definition: ecmult_impl.h:60
#define ECMULT_TABLE_SIZE(w)
The number of entries a table with precomputed multiples needs to have.
Definition: ecmult_impl.h:54
#define PIPPENGER_SCRATCH_OBJECTS
Definition: ecmult_impl.h:57
int(* secp256k1_ecmult_multi_func)(const secp256k1_ecmult_context *, secp256k1_scratch *, secp256k1_gej *, const secp256k1_scalar *, secp256k1_ecmult_multi_callback cb, void *, size_t)
Definition: ecmult_impl.h:975
#define ECMULT_TABLE_GET_GE(r, pre, n, w)
The following two macro retrieves a particular odd multiple from a table of precomputed multiples.
Definition: ecmult_impl.h:162
#define WINDOW_G
larger numbers may result in slightly better performance, at the cost of exponentially larger precomp...
Definition: ecmult_impl.h:41
void * memcpy(void *a, const void *b, size_t c)
#define SECP256K1_INLINE
Definition: secp256k1.h:123
secp256k1_ge_storage(* pre_g)[]
Definition: ecmult.h:17
A group element of the secp256k1 curve, in affine coordinates.
Definition: group.h:14
int infinity
Definition: group.h:17
secp256k1_fe x
Definition: group.h:15
secp256k1_fe y
Definition: group.h:16
A group element of the secp256k1 curve, in jacobian coordinates.
Definition: group.h:24
secp256k1_fe y
Definition: group.h:26
secp256k1_fe x
Definition: group.h:25
int infinity
Definition: group.h:28
secp256k1_fe z
Definition: group.h:27
struct secp256k1_pippenger_point_state * ps
Definition: ecmult_impl.h:641
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
struct secp256k1_strauss_point_state * ps
Definition: ecmult_impl.h:335
secp256k1_gej * prej
Definition: ecmult_impl.h:329
secp256k1_ge * pre_a
Definition: ecmult_impl.h:331
#define CHECK(cond)
Definition: util.h:52
#define VERIFY_CHECK(cond)
Definition: util.h:67