Bitcoin ABC 0.32.7
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 https://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 "util.h"
14#include "group.h"
15#include "scalar.h"
16#include "ecmult.h"
17#include "precomputed_ecmult.h"
18
19#if defined(EXHAUSTIVE_TEST_ORDER)
20/* We need to lower these values for exhaustive tests because
21 * the tables cannot have infinities in them (this breaks the
22 * affine-isomorphism stuff which tracks z-ratios) */
23# if EXHAUSTIVE_TEST_ORDER > 128
24# define WINDOW_A 5
25# elif EXHAUSTIVE_TEST_ORDER > 8
26# define WINDOW_A 4
27# else
28# define WINDOW_A 2
29# endif
30#else
31/* optimal for 128-bit and 256-bit exponents. */
32# define WINDOW_A 5
42#endif
43
44#define WNAF_BITS 128
45#define WNAF_SIZE_BITS(bits, w) (((bits) + (w) - 1) / (w))
46#define WNAF_SIZE(w) WNAF_SIZE_BITS(WNAF_BITS, w)
47
48/* The number of objects allocated on the scratch space for ecmult_multi algorithms */
49#define PIPPENGER_SCRATCH_OBJECTS 6
50#define STRAUSS_SCRATCH_OBJECTS 7
51
52#define PIPPENGER_MAX_BUCKET_WINDOW 12
53
54/* Minimum number of points for which pippenger_wnaf is faster than strauss wnaf */
55#define ECMULT_PIPPENGER_THRESHOLD 88
56
57#define ECMULT_MAX_POINTS_PER_BATCH 5000000
58
66 secp256k1_ge a_ge, d_ge;
67 int i;
68
70
71 secp256k1_gej_double_var(&d, a, NULL);
72
73 /*
74 * Perform the additions on an isomorphism where 'd' is affine: drop the z coordinate
75 * of 'd', and scale the 1P starting value's x/y coordinates without changing its z.
76 */
77 d_ge.x = d.x;
78 d_ge.y = d.y;
79 d_ge.infinity = 0;
80
81 secp256k1_ge_set_gej_zinv(&a_ge, a, &d.z);
82 prej[0].x = a_ge.x;
83 prej[0].y = a_ge.y;
84 prej[0].z = a->z;
85 prej[0].infinity = 0;
86
87 zr[0] = d.z;
88 for (i = 1; i < n; i++) {
89 secp256k1_gej_add_ge_var(&prej[i], &prej[i-1], &d_ge, &zr[i]);
90 }
91
92 /*
93 * Each point in 'prej' has a z coordinate too small by a factor of 'd.z'. Only
94 * the final point's z coordinate is actually used though, so just update that.
95 */
96 secp256k1_fe_mul(&prej[n-1].z, &prej[n-1].z, &d.z);
97}
98
101#define ECMULT_TABLE_GET_GE(r,pre,n,w) do { \
102 VERIFY_CHECK(((n) & 1) == 1); \
103 VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \
104 VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \
105 if ((n) > 0) { \
106 *(r) = (pre)[((n)-1)/2]; \
107 } else { \
108 *(r) = (pre)[(-(n)-1)/2]; \
109 secp256k1_fe_negate(&((r)->y), &((r)->y), 1); \
110 } \
111} while(0)
112
113#define ECMULT_TABLE_GET_GE_STORAGE(r,pre,n,w) do { \
114 VERIFY_CHECK(((n) & 1) == 1); \
115 VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \
116 VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \
117 if ((n) > 0) { \
118 secp256k1_ge_from_storage((r), &(pre)[((n)-1)/2]); \
119 } else { \
120 secp256k1_ge_from_storage((r), &(pre)[(-(n)-1)/2]); \
121 secp256k1_fe_negate(&((r)->y), &((r)->y), 1); \
122 } \
123} while(0)
124
132static int secp256k1_ecmult_wnaf(int *wnaf, int len, const secp256k1_scalar *a, int w) {
134 int last_set_bit = -1;
135 int bit = 0;
136 int sign = 1;
137 int carry = 0;
138
139 VERIFY_CHECK(wnaf != NULL);
140 VERIFY_CHECK(0 <= len && len <= 256);
141 VERIFY_CHECK(a != NULL);
142 VERIFY_CHECK(2 <= w && w <= 31);
143
144 memset(wnaf, 0, len * sizeof(wnaf[0]));
145
146 s = *a;
147 if (secp256k1_scalar_get_bits(&s, 255, 1)) {
149 sign = -1;
150 }
151
152 while (bit < len) {
153 int now;
154 int word;
155 if (secp256k1_scalar_get_bits(&s, bit, 1) == (unsigned int)carry) {
156 bit++;
157 continue;
158 }
159
160 now = w;
161 if (now > len - bit) {
162 now = len - bit;
163 }
164
165 word = secp256k1_scalar_get_bits_var(&s, bit, now) + carry;
166
167 carry = (word >> (w-1)) & 1;
168 word -= carry << w;
169
170 wnaf[bit] = sign * word;
171 last_set_bit = bit;
172
173 bit += now;
174 }
175#ifdef VERIFY
176 CHECK(carry == 0);
177 while (bit < 256) {
178 CHECK(secp256k1_scalar_get_bits(&s, bit++, 1) == 0);
179 }
180#endif
181 return last_set_bit + 1;
182}
183
186 int wnaf_na_1[129];
187 int wnaf_na_lam[129];
190 size_t input_pos;
191};
192
199};
200
201static void secp256k1_ecmult_strauss_wnaf(const struct secp256k1_strauss_state *state, secp256k1_gej *r, size_t num, const secp256k1_gej *a, const secp256k1_scalar *na, const secp256k1_scalar *ng) {
202 secp256k1_ge tmpa;
203 secp256k1_fe Z;
204 /* Split G factors. */
205 secp256k1_scalar ng_1, ng_128;
206 int wnaf_ng_1[129];
207 int bits_ng_1 = 0;
208 int wnaf_ng_128[129];
209 int bits_ng_128 = 0;
210 int i;
211 int bits = 0;
212 size_t np;
213 size_t no = 0;
214
215 for (np = 0; np < num; ++np) {
216 if (secp256k1_scalar_is_zero(&na[np]) || secp256k1_gej_is_infinity(&a[np])) {
217 continue;
218 }
219 state->ps[no].input_pos = np;
220 /* split na into na_1 and na_lam (where na = na_1 + na_lam*lambda, and na_1 and na_lam are ~128 bit) */
221 secp256k1_scalar_split_lambda(&state->ps[no].na_1, &state->ps[no].na_lam, &na[np]);
222
223 /* build wnaf representation for na_1 and na_lam. */
224 state->ps[no].bits_na_1 = secp256k1_ecmult_wnaf(state->ps[no].wnaf_na_1, 129, &state->ps[no].na_1, WINDOW_A);
225 state->ps[no].bits_na_lam = secp256k1_ecmult_wnaf(state->ps[no].wnaf_na_lam, 129, &state->ps[no].na_lam, WINDOW_A);
226 VERIFY_CHECK(state->ps[no].bits_na_1 <= 129);
227 VERIFY_CHECK(state->ps[no].bits_na_lam <= 129);
228 if (state->ps[no].bits_na_1 > bits) {
229 bits = state->ps[no].bits_na_1;
230 }
231 if (state->ps[no].bits_na_lam > bits) {
232 bits = state->ps[no].bits_na_lam;
233 }
234 ++no;
235 }
236
237 /* Calculate odd multiples of a.
238 * All multiples are brought to the same Z 'denominator', which is stored
239 * in Z. Due to secp256k1' isomorphism we can do all operations pretending
240 * that the Z coordinate was 1, use affine addition formulae, and correct
241 * the Z coordinate of the result once at the end.
242 * The exception is the precomputed G table points, which are actually
243 * affine. Compared to the base used for other points, they have a Z ratio
244 * of 1/Z, so we can use secp256k1_gej_add_zinv_var, which uses the same
245 * isomorphism to efficiently add with a known Z inverse.
246 */
247 if (no > 0) {
248 /* Compute the odd multiples in Jacobian form. */
250 for (np = 1; np < no; ++np) {
251 secp256k1_gej tmp = a[state->ps[np].input_pos];
252#ifdef VERIFY
254#endif
255 secp256k1_gej_rescale(&tmp, &(state->prej[(np - 1) * ECMULT_TABLE_SIZE(WINDOW_A) + ECMULT_TABLE_SIZE(WINDOW_A) - 1].z));
257 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));
258 }
259 /* Bring them to the same Z denominator. */
261 } else {
263 }
264
265 for (np = 0; np < no; ++np) {
266 for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) {
268 }
269 }
270
271 if (ng) {
272 /* 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) */
273 secp256k1_scalar_split_128(&ng_1, &ng_128, ng);
274
275 /* Build wnaf representation for ng_1 and ng_128 */
276 bits_ng_1 = secp256k1_ecmult_wnaf(wnaf_ng_1, 129, &ng_1, WINDOW_G);
277 bits_ng_128 = secp256k1_ecmult_wnaf(wnaf_ng_128, 129, &ng_128, WINDOW_G);
278 if (bits_ng_1 > bits) {
279 bits = bits_ng_1;
280 }
281 if (bits_ng_128 > bits) {
282 bits = bits_ng_128;
283 }
284 }
285
287
288 for (i = bits - 1; i >= 0; i--) {
289 int n;
290 secp256k1_gej_double_var(r, r, NULL);
291 for (np = 0; np < no; ++np) {
292 if (i < state->ps[np].bits_na_1 && (n = state->ps[np].wnaf_na_1[i])) {
294 secp256k1_gej_add_ge_var(r, r, &tmpa, NULL);
295 }
296 if (i < state->ps[np].bits_na_lam && (n = state->ps[np].wnaf_na_lam[i])) {
298 secp256k1_gej_add_ge_var(r, r, &tmpa, NULL);
299 }
300 }
301 if (i < bits_ng_1 && (n = wnaf_ng_1[i])) {
303 secp256k1_gej_add_zinv_var(r, r, &tmpa, &Z);
304 }
305 if (i < bits_ng_128 && (n = wnaf_ng_128[i])) {
307 secp256k1_gej_add_zinv_var(r, r, &tmpa, &Z);
308 }
309 }
310
311 if (!r->infinity) {
312 secp256k1_fe_mul(&r->z, &r->z, &Z);
313 }
314}
315
316static void secp256k1_ecmult(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_scalar *na, const secp256k1_scalar *ng) {
322 struct secp256k1_strauss_state state;
323
324 state.prej = prej;
325 state.zr = zr;
326 state.pre_a = pre_a;
327 state.pre_a_lam = pre_a_lam;
328 state.ps = ps;
329 secp256k1_ecmult_strauss_wnaf(&state, r, 1, a, na, ng);
330}
331
332static size_t secp256k1_strauss_scratch_size(size_t n_points) {
333 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);
334 return n_points*point_size;
335}
336
337static int secp256k1_ecmult_strauss_batch(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_points, size_t cb_offset) {
338 secp256k1_gej* points;
339 secp256k1_scalar* scalars;
340 struct secp256k1_strauss_state state;
341 size_t i;
342 const size_t scratch_checkpoint = secp256k1_scratch_checkpoint(error_callback, scratch);
343
345 if (inp_g_sc == NULL && n_points == 0) {
346 return 1;
347 }
348
349 /* We allocate STRAUSS_SCRATCH_OBJECTS objects on the scratch space. If these
350 * allocations change, make sure to update the STRAUSS_SCRATCH_OBJECTS
351 * constant and strauss_scratch_size accordingly. */
352 points = (secp256k1_gej*)secp256k1_scratch_alloc(error_callback, scratch, n_points * sizeof(secp256k1_gej));
353 scalars = (secp256k1_scalar*)secp256k1_scratch_alloc(error_callback, scratch, n_points * sizeof(secp256k1_scalar));
354 state.prej = (secp256k1_gej*)secp256k1_scratch_alloc(error_callback, scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_gej));
355 state.zr = (secp256k1_fe*)secp256k1_scratch_alloc(error_callback, scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_fe));
356 state.pre_a = (secp256k1_ge*)secp256k1_scratch_alloc(error_callback, scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_ge));
357 state.pre_a_lam = (secp256k1_ge*)secp256k1_scratch_alloc(error_callback, scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_ge));
358 state.ps = (struct secp256k1_strauss_point_state*)secp256k1_scratch_alloc(error_callback, scratch, n_points * sizeof(struct secp256k1_strauss_point_state));
359
360 if (points == NULL || scalars == NULL || state.prej == NULL || state.zr == NULL || state.pre_a == NULL || state.pre_a_lam == NULL || state.ps == NULL) {
361 secp256k1_scratch_apply_checkpoint(error_callback, scratch, scratch_checkpoint);
362 return 0;
363 }
364
365 for (i = 0; i < n_points; i++) {
366 secp256k1_ge point;
367 if (!cb(&scalars[i], &point, i+cb_offset, cbdata)) {
368 secp256k1_scratch_apply_checkpoint(error_callback, scratch, scratch_checkpoint);
369 return 0;
370 }
371 secp256k1_gej_set_ge(&points[i], &point);
372 }
373 secp256k1_ecmult_strauss_wnaf(&state, r, n_points, points, scalars, inp_g_sc);
374 secp256k1_scratch_apply_checkpoint(error_callback, scratch, scratch_checkpoint);
375 return 1;
376}
377
378/* Wrapper for secp256k1_ecmult_multi_func interface */
379static int secp256k1_ecmult_strauss_batch_single(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) {
380 return secp256k1_ecmult_strauss_batch(error_callback, scratch, r, inp_g_sc, cb, cbdata, n, 0);
381}
382
383static size_t secp256k1_strauss_max_points(const secp256k1_callback* error_callback, secp256k1_scratch *scratch) {
385}
386
394static int secp256k1_wnaf_fixed(int *wnaf, const secp256k1_scalar *s, int w) {
395 int skew = 0;
396 int pos;
397 int max_pos;
398 int last_w;
399 const secp256k1_scalar *work = s;
400
402 for (pos = 0; pos < WNAF_SIZE(w); pos++) {
403 wnaf[pos] = 0;
404 }
405 return 0;
406 }
407
409 skew = 1;
410 }
411
412 wnaf[0] = secp256k1_scalar_get_bits_var(work, 0, w) + skew;
413 /* Compute last window size. Relevant when window size doesn't divide the
414 * number of bits in the scalar */
415 last_w = WNAF_BITS - (WNAF_SIZE(w) - 1) * w;
416
417 /* Store the position of the first nonzero word in max_pos to allow
418 * skipping leading zeros when calculating the wnaf. */
419 for (pos = WNAF_SIZE(w) - 1; pos > 0; pos--) {
420 int val = secp256k1_scalar_get_bits_var(work, pos * w, pos == WNAF_SIZE(w)-1 ? last_w : w);
421 if(val != 0) {
422 break;
423 }
424 wnaf[pos] = 0;
425 }
426 max_pos = pos;
427 pos = 1;
428
429 while (pos <= max_pos) {
430 int val = secp256k1_scalar_get_bits_var(work, pos * w, pos == WNAF_SIZE(w)-1 ? last_w : w);
431 if ((val & 1) == 0) {
432 wnaf[pos - 1] -= (1 << w);
433 wnaf[pos] = (val + 1);
434 } else {
435 wnaf[pos] = val;
436 }
437 /* Set a coefficient to zero if it is 1 or -1 and the proceeding digit
438 * is strictly negative or strictly positive respectively. Only change
439 * coefficients at previous positions because above code assumes that
440 * wnaf[pos - 1] is odd.
441 */
442 if (pos >= 2 && ((wnaf[pos - 1] == 1 && wnaf[pos - 2] < 0) || (wnaf[pos - 1] == -1 && wnaf[pos - 2] > 0))) {
443 if (wnaf[pos - 1] == 1) {
444 wnaf[pos - 2] += 1 << w;
445 } else {
446 wnaf[pos - 2] -= 1 << w;
447 }
448 wnaf[pos - 1] = 0;
449 }
450 ++pos;
451 }
452
453 return skew;
454}
455
458 size_t input_pos;
459};
460
464};
465
466/*
467 * pippenger_wnaf computes the result of a multi-point multiplication as
468 * follows: The scalars are brought into wnaf with n_wnaf elements each. Then
469 * for every i < n_wnaf, first each point is added to a "bucket" corresponding
470 * to the point's wnaf[i]. Second, the buckets are added together such that
471 * r += 1*bucket[0] + 3*bucket[1] + 5*bucket[2] + ...
472 */
473static 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) {
474 size_t n_wnaf = WNAF_SIZE(bucket_window+1);
475 size_t np;
476 size_t no = 0;
477 int i;
478 int j;
479
480 for (np = 0; np < num; ++np) {
481 if (secp256k1_scalar_is_zero(&sc[np]) || secp256k1_ge_is_infinity(&pt[np])) {
482 continue;
483 }
484 state->ps[no].input_pos = np;
485 state->ps[no].skew_na = secp256k1_wnaf_fixed(&state->wnaf_na[no*n_wnaf], &sc[np], bucket_window+1);
486 no++;
487 }
489
490 if (no == 0) {
491 return 1;
492 }
493
494 for (i = n_wnaf - 1; i >= 0; i--) {
495 secp256k1_gej running_sum;
496
497 for(j = 0; j < ECMULT_TABLE_SIZE(bucket_window+2); j++) {
498 secp256k1_gej_set_infinity(&buckets[j]);
499 }
500
501 for (np = 0; np < no; ++np) {
502 int n = state->wnaf_na[np*n_wnaf + i];
503 struct secp256k1_pippenger_point_state point_state = state->ps[np];
504 secp256k1_ge tmp;
505 int idx;
506
507 if (i == 0) {
508 /* correct for wnaf skew */
509 int skew = point_state.skew_na;
510 if (skew) {
511 secp256k1_ge_neg(&tmp, &pt[point_state.input_pos]);
512 secp256k1_gej_add_ge_var(&buckets[0], &buckets[0], &tmp, NULL);
513 }
514 }
515 if (n > 0) {
516 idx = (n - 1)/2;
517 secp256k1_gej_add_ge_var(&buckets[idx], &buckets[idx], &pt[point_state.input_pos], NULL);
518 } else if (n < 0) {
519 idx = -(n + 1)/2;
520 secp256k1_ge_neg(&tmp, &pt[point_state.input_pos]);
521 secp256k1_gej_add_ge_var(&buckets[idx], &buckets[idx], &tmp, NULL);
522 }
523 }
524
525 for(j = 0; j < bucket_window; j++) {
526 secp256k1_gej_double_var(r, r, NULL);
527 }
528
529 secp256k1_gej_set_infinity(&running_sum);
530 /* Accumulate the sum: bucket[0] + 3*bucket[1] + 5*bucket[2] + 7*bucket[3] + ...
531 * = bucket[0] + bucket[1] + bucket[2] + bucket[3] + ...
532 * + 2 * (bucket[1] + 2*bucket[2] + 3*bucket[3] + ...)
533 * using an intermediate running sum:
534 * running_sum = bucket[0] + bucket[1] + bucket[2] + ...
535 *
536 * The doubling is done implicitly by deferring the final window doubling (of 'r').
537 */
538 for(j = ECMULT_TABLE_SIZE(bucket_window+2) - 1; j > 0; j--) {
539 secp256k1_gej_add_var(&running_sum, &running_sum, &buckets[j], NULL);
540 secp256k1_gej_add_var(r, r, &running_sum, NULL);
541 }
542
543 secp256k1_gej_add_var(&running_sum, &running_sum, &buckets[0], NULL);
544 secp256k1_gej_double_var(r, r, NULL);
545 secp256k1_gej_add_var(r, r, &running_sum, NULL);
546 }
547 return 1;
548}
549
555 if (n <= 1) {
556 return 1;
557 } else if (n <= 4) {
558 return 2;
559 } else if (n <= 20) {
560 return 3;
561 } else if (n <= 57) {
562 return 4;
563 } else if (n <= 136) {
564 return 5;
565 } else if (n <= 235) {
566 return 6;
567 } else if (n <= 1260) {
568 return 7;
569 } else if (n <= 4420) {
570 return 9;
571 } else if (n <= 7880) {
572 return 10;
573 } else if (n <= 16050) {
574 return 11;
575 } else {
577 }
578}
579
583static size_t secp256k1_pippenger_bucket_window_inv(int bucket_window) {
584 switch(bucket_window) {
585 case 1: return 1;
586 case 2: return 4;
587 case 3: return 20;
588 case 4: return 57;
589 case 5: return 136;
590 case 6: return 235;
591 case 7: return 1260;
592 case 8: return 1260;
593 case 9: return 4420;
594 case 10: return 7880;
595 case 11: return 16050;
596 case PIPPENGER_MAX_BUCKET_WINDOW: return SIZE_MAX;
597 }
598 return 0;
599}
600
601
603 secp256k1_scalar tmp = *s1;
604 secp256k1_scalar_split_lambda(s1, s2, &tmp);
606
607 if (secp256k1_scalar_is_high(s1)) {
609 secp256k1_ge_neg(p1, p1);
610 }
611 if (secp256k1_scalar_is_high(s2)) {
613 secp256k1_ge_neg(p2, p2);
614 }
615}
616
621static size_t secp256k1_pippenger_scratch_size(size_t n_points, int bucket_window) {
622 size_t entries = 2*n_points + 2;
623 size_t entry_size = sizeof(secp256k1_ge) + sizeof(secp256k1_scalar) + sizeof(struct secp256k1_pippenger_point_state) + (WNAF_SIZE(bucket_window+1)+1)*sizeof(int);
624 return (sizeof(secp256k1_gej) << bucket_window) + sizeof(struct secp256k1_pippenger_state) + entries * entry_size;
625}
626
627static int secp256k1_ecmult_pippenger_batch(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_points, size_t cb_offset) {
628 const size_t scratch_checkpoint = secp256k1_scratch_checkpoint(error_callback, scratch);
629 /* Use 2(n+1) with the endomorphism, when calculating batch
630 * sizes. The reason for +1 is that we add the G scalar to the list of
631 * other scalars. */
632 size_t entries = 2*n_points + 2;
633 secp256k1_ge *points;
634 secp256k1_scalar *scalars;
635 secp256k1_gej *buckets;
636 struct secp256k1_pippenger_state *state_space;
637 size_t idx = 0;
638 size_t point_idx = 0;
639 int i, j;
640 int bucket_window;
641
643 if (inp_g_sc == NULL && n_points == 0) {
644 return 1;
645 }
646 bucket_window = secp256k1_pippenger_bucket_window(n_points);
647
648 /* We allocate PIPPENGER_SCRATCH_OBJECTS objects on the scratch space. If
649 * these allocations change, make sure to update the
650 * PIPPENGER_SCRATCH_OBJECTS constant and pippenger_scratch_size
651 * accordingly. */
652 points = (secp256k1_ge *) secp256k1_scratch_alloc(error_callback, scratch, entries * sizeof(*points));
653 scalars = (secp256k1_scalar *) secp256k1_scratch_alloc(error_callback, scratch, entries * sizeof(*scalars));
654 state_space = (struct secp256k1_pippenger_state *) secp256k1_scratch_alloc(error_callback, scratch, sizeof(*state_space));
655 if (points == NULL || scalars == NULL || state_space == NULL) {
656 secp256k1_scratch_apply_checkpoint(error_callback, scratch, scratch_checkpoint);
657 return 0;
658 }
659 state_space->ps = (struct secp256k1_pippenger_point_state *) secp256k1_scratch_alloc(error_callback, scratch, entries * sizeof(*state_space->ps));
660 state_space->wnaf_na = (int *) secp256k1_scratch_alloc(error_callback, scratch, entries*(WNAF_SIZE(bucket_window+1)) * sizeof(int));
661 buckets = (secp256k1_gej *) secp256k1_scratch_alloc(error_callback, scratch, (1<<bucket_window) * sizeof(*buckets));
662 if (state_space->ps == NULL || state_space->wnaf_na == NULL || buckets == NULL) {
663 secp256k1_scratch_apply_checkpoint(error_callback, scratch, scratch_checkpoint);
664 return 0;
665 }
666
667 if (inp_g_sc != NULL) {
668 scalars[0] = *inp_g_sc;
669 points[0] = secp256k1_ge_const_g;
670 idx++;
671 secp256k1_ecmult_endo_split(&scalars[0], &scalars[1], &points[0], &points[1]);
672 idx++;
673 }
674
675 while (point_idx < n_points) {
676 if (!cb(&scalars[idx], &points[idx], point_idx + cb_offset, cbdata)) {
677 secp256k1_scratch_apply_checkpoint(error_callback, scratch, scratch_checkpoint);
678 return 0;
679 }
680 idx++;
681 secp256k1_ecmult_endo_split(&scalars[idx - 1], &scalars[idx], &points[idx - 1], &points[idx]);
682 idx++;
683 point_idx++;
684 }
685
686 secp256k1_ecmult_pippenger_wnaf(buckets, bucket_window, state_space, r, scalars, points, idx);
687
688 /* Clear data */
689 for(i = 0; (size_t)i < idx; i++) {
690 secp256k1_scalar_clear(&scalars[i]);
691 state_space->ps[i].skew_na = 0;
692 for(j = 0; j < WNAF_SIZE(bucket_window+1); j++) {
693 state_space->wnaf_na[i * WNAF_SIZE(bucket_window+1) + j] = 0;
694 }
695 }
696 for(i = 0; i < 1<<bucket_window; i++) {
697 secp256k1_gej_clear(&buckets[i]);
698 }
699 secp256k1_scratch_apply_checkpoint(error_callback, scratch, scratch_checkpoint);
700 return 1;
701}
702
703/* Wrapper for secp256k1_ecmult_multi_func interface */
704static int secp256k1_ecmult_pippenger_batch_single(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) {
705 return secp256k1_ecmult_pippenger_batch(error_callback, scratch, r, inp_g_sc, cb, cbdata, n, 0);
706}
707
713static size_t secp256k1_pippenger_max_points(const secp256k1_callback* error_callback, secp256k1_scratch *scratch) {
714 size_t max_alloc = secp256k1_scratch_max_allocation(error_callback, scratch, PIPPENGER_SCRATCH_OBJECTS);
715 int bucket_window;
716 size_t res = 0;
717
718 for (bucket_window = 1; bucket_window <= PIPPENGER_MAX_BUCKET_WINDOW; bucket_window++) {
719 size_t n_points;
720 size_t max_points = secp256k1_pippenger_bucket_window_inv(bucket_window);
721 size_t space_for_points;
722 size_t space_overhead;
723 size_t entry_size = sizeof(secp256k1_ge) + sizeof(secp256k1_scalar) + sizeof(struct secp256k1_pippenger_point_state) + (WNAF_SIZE(bucket_window+1)+1)*sizeof(int);
724
725 entry_size = 2*entry_size;
726 space_overhead = (sizeof(secp256k1_gej) << bucket_window) + entry_size + sizeof(struct secp256k1_pippenger_state);
727 if (space_overhead > max_alloc) {
728 break;
729 }
730 space_for_points = max_alloc - space_overhead;
731
732 n_points = space_for_points/entry_size;
733 n_points = n_points > max_points ? max_points : n_points;
734 if (n_points > res) {
735 res = n_points;
736 }
737 if (n_points < max_points) {
738 /* A larger bucket_window may support even more points. But if we
739 * would choose that then the caller couldn't safely use any number
740 * smaller than what this function returns */
741 break;
742 }
743 }
744 return res;
745}
746
747/* Computes ecmult_multi by simply multiplying and adding each point. Does not
748 * require a scratch space */
749static int secp256k1_ecmult_multi_simple_var(secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n_points) {
750 size_t point_idx;
751 secp256k1_scalar szero;
752 secp256k1_gej tmpj;
753
754 secp256k1_scalar_set_int(&szero, 0);
757 /* r = inp_g_sc*G */
758 secp256k1_ecmult(r, &tmpj, &szero, inp_g_sc);
759 for (point_idx = 0; point_idx < n_points; point_idx++) {
760 secp256k1_ge point;
761 secp256k1_gej pointj;
762 secp256k1_scalar scalar;
763 if (!cb(&scalar, &point, point_idx, cbdata)) {
764 return 0;
765 }
766 /* r += scalar*point */
767 secp256k1_gej_set_ge(&pointj, &point);
768 secp256k1_ecmult(&tmpj, &pointj, &scalar, NULL);
769 secp256k1_gej_add_var(r, r, &tmpj, NULL);
770 }
771 return 1;
772}
773
774/* Compute the number of batches and the batch size given the maximum batch size and the
775 * total number of points */
776static int secp256k1_ecmult_multi_batch_size_helper(size_t *n_batches, size_t *n_batch_points, size_t max_n_batch_points, size_t n) {
777 if (max_n_batch_points == 0) {
778 return 0;
779 }
780 if (max_n_batch_points > ECMULT_MAX_POINTS_PER_BATCH) {
781 max_n_batch_points = ECMULT_MAX_POINTS_PER_BATCH;
782 }
783 if (n == 0) {
784 *n_batches = 0;
785 *n_batch_points = 0;
786 return 1;
787 }
788 /* Compute ceil(n/max_n_batch_points) and ceil(n/n_batches) */
789 *n_batches = 1 + (n - 1) / max_n_batch_points;
790 *n_batch_points = 1 + (n - 1) / *n_batches;
791 return 1;
792}
793
795static 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) {
796 size_t i;
797
798 int (*f)(const secp256k1_callback* error_callback, secp256k1_scratch*, secp256k1_gej*, const secp256k1_scalar*, secp256k1_ecmult_multi_callback cb, void*, size_t, size_t);
799 size_t n_batches;
800 size_t n_batch_points;
801
803 if (inp_g_sc == NULL && n == 0) {
804 return 1;
805 } else if (n == 0) {
806 secp256k1_scalar szero;
807 secp256k1_scalar_set_int(&szero, 0);
808 secp256k1_ecmult(r, r, &szero, inp_g_sc);
809 return 1;
810 }
811 if (scratch == NULL) {
812 return secp256k1_ecmult_multi_simple_var(r, inp_g_sc, cb, cbdata, n);
813 }
814
815 /* Compute the batch sizes for Pippenger's algorithm given a scratch space. If it's greater than
816 * a threshold use Pippenger's algorithm. Otherwise use Strauss' algorithm.
817 * As a first step check if there's enough space for Pippenger's algo (which requires less space
818 * than Strauss' algo) and if not, use the simple algorithm. */
819 if (!secp256k1_ecmult_multi_batch_size_helper(&n_batches, &n_batch_points, secp256k1_pippenger_max_points(error_callback, scratch), n)) {
820 return secp256k1_ecmult_multi_simple_var(r, inp_g_sc, cb, cbdata, n);
821 }
822 if (n_batch_points >= ECMULT_PIPPENGER_THRESHOLD) {
824 } else {
825 if (!secp256k1_ecmult_multi_batch_size_helper(&n_batches, &n_batch_points, secp256k1_strauss_max_points(error_callback, scratch), n)) {
826 return secp256k1_ecmult_multi_simple_var(r, inp_g_sc, cb, cbdata, n);
827 }
829 }
830 for(i = 0; i < n_batches; i++) {
831 size_t nbp = n < n_batch_points ? n : n_batch_points;
832 size_t offset = n_batch_points*i;
833 secp256k1_gej tmp;
834 if (!f(error_callback, scratch, &tmp, i == 0 ? inp_g_sc : NULL, cb, cbdata, nbp, offset)) {
835 return 0;
836 }
837 secp256k1_gej_add_var(r, r, &tmp, NULL);
838 n -= nbp;
839 }
840 return 1;
841}
842
843#endif /* SECP256K1_ECMULT_IMPL_H */
#define ECMULT_TABLE_SIZE(w)
The number of entries a table with precomputed multiples needs to have.
Definition: ecmult.h:33
int() secp256k1_ecmult_multi_callback(secp256k1_scalar *sc, secp256k1_ge *pt, size_t idx, void *data)
Definition: ecmult.h:38
#define STRAUSS_SCRATCH_OBJECTS
Definition: ecmult_impl.h:50
static size_t secp256k1_pippenger_bucket_window_inv(int bucket_window)
Returns the maximum optimal number of points for a bucket_window.
Definition: ecmult_impl.h:583
static size_t secp256k1_pippenger_max_points(const secp256k1_callback *error_callback, secp256k1_scratch *scratch)
Returns the maximum number of points in addition to G that can be used with a given scratch space.
Definition: ecmult_impl.h:713
static int secp256k1_ecmult_pippenger_batch(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_points, size_t cb_offset)
Definition: ecmult_impl.h:627
#define WNAF_SIZE(w)
Definition: ecmult_impl.h:46
static int secp256k1_ecmult_strauss_batch_single(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)
Definition: ecmult_impl.h:379
static size_t secp256k1_strauss_max_points(const secp256k1_callback *error_callback, secp256k1_scratch *scratch)
Definition: ecmult_impl.h:383
static int secp256k1_wnaf_fixed(int *wnaf, const secp256k1_scalar *s, int w)
Convert a number to WNAF notation.
Definition: ecmult_impl.h:394
static SECP256K1_INLINE void secp256k1_ecmult_endo_split(secp256k1_scalar *s1, secp256k1_scalar *s2, secp256k1_ge *p1, secp256k1_ge *p2)
Definition: ecmult_impl.h:602
#define ECMULT_TABLE_GET_GE_STORAGE(r, pre, n, w)
Definition: ecmult_impl.h:113
static int secp256k1_ecmult_wnaf(int *wnaf, int len, const secp256k1_scalar *a, int w)
Convert a number to WNAF notation.
Definition: ecmult_impl.h:132
static void secp256k1_ecmult_odd_multiples_table(int n, secp256k1_gej *prej, secp256k1_fe *zr, const secp256k1_gej *a)
Fill a table 'prej' with precomputed odd multiples of a.
Definition: ecmult_impl.h:64
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)
Definition: ecmult_impl.h:795
#define WINDOW_A
Definition: ecmult_impl.h:32
static size_t secp256k1_strauss_scratch_size(size_t n_points)
Definition: ecmult_impl.h:332
#define ECMULT_PIPPENGER_THRESHOLD
Definition: ecmult_impl.h:55
static int secp256k1_ecmult_multi_simple_var(secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n_points)
Definition: ecmult_impl.h:749
static int secp256k1_pippenger_bucket_window(size_t n)
Returns optimal bucket_window (number of bits of a scalar represented by a set of buckets) for a give...
Definition: ecmult_impl.h:554
static int secp256k1_ecmult_pippenger_batch_single(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)
Definition: ecmult_impl.h:704
#define WNAF_BITS
Larger values for ECMULT_WINDOW_SIZE result in possibly better performance at the cost of an exponent...
Definition: ecmult_impl.h:44
#define ECMULT_MAX_POINTS_PER_BATCH
Definition: ecmult_impl.h:57
#define PIPPENGER_MAX_BUCKET_WINDOW
Definition: ecmult_impl.h:52
#define PIPPENGER_SCRATCH_OBJECTS
Definition: ecmult_impl.h:49
static int secp256k1_ecmult_strauss_batch(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_points, size_t cb_offset)
Definition: ecmult_impl.h:337
static void secp256k1_ecmult(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_scalar *na, const secp256k1_scalar *ng)
Definition: ecmult_impl.h:316
static int secp256k1_ecmult_multi_batch_size_helper(size_t *n_batches, size_t *n_batch_points, size_t max_n_batch_points, size_t n)
Definition: ecmult_impl.h:776
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)
Definition: ecmult_impl.h:473
static size_t secp256k1_pippenger_scratch_size(size_t n_points, int bucket_window)
Returns the scratch size required for a given number of points (excluding base point G) without consi...
Definition: ecmult_impl.h:621
static void secp256k1_ecmult_strauss_wnaf(const struct secp256k1_strauss_state *state, secp256k1_gej *r, size_t num, const secp256k1_gej *a, const secp256k1_scalar *na, const secp256k1_scalar *ng)
Definition: ecmult_impl.h:201
#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:101
int(* secp256k1_ecmult_multi_func)(const secp256k1_callback *error_callback, secp256k1_scratch *, secp256k1_gej *, const secp256k1_scalar *, secp256k1_ecmult_multi_callback cb, void *, size_t)
Definition: ecmult_impl.h:794
static void secp256k1_fe_normalize_var(secp256k1_fe *r)
Normalize a field element, without constant-time guarantee.
static void secp256k1_fe_set_int(secp256k1_fe *r, int a)
Set a field element equal to a small (not greater than 0x7FFF), non-negative integer.
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.
static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr)
Set r equal to the double of a.
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).
static void secp256k1_gej_clear(secp256k1_gej *r)
Clear a secp256k1_gej to prevent leaking sensitive information.
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.
static void secp256k1_gej_set_infinity(secp256k1_gej *r)
Set a group element (jacobian) equal to the point at infinity.
static int secp256k1_gej_is_infinity(const secp256k1_gej *a)
Check whether a group element is the point at infinity.
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).
static void secp256k1_ge_globalz_set_table_gej(size_t len, secp256k1_ge *r, secp256k1_fe *globalz, const secp256k1_gej *a, const secp256k1_fe *zr)
Bring a batch inputs given in jacobian coordinates (with known z-ratios) to the same global z "denomi...
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.
static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *b)
Rescale a jacobian point by b which must be non-zero.
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)
static int secp256k1_ge_is_infinity(const secp256k1_ge *a)
Check whether a group element is the point at infinity.
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a)
Set a group element (jacobian) equal to another which is given in affine coordinates.
static void secp256k1_ge_set_gej_zinv(secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zi)
Definition: group_impl.h:67
static const secp256k1_ge secp256k1_ge_const_g
Definition: group_impl.h:62
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)]
#define WINDOW_G
static void secp256k1_scalar_split_128(secp256k1_scalar *r1, secp256k1_scalar *r2, const secp256k1_scalar *k)
Find r1 and r2 such that r1+r2*2^128 = k.
static int secp256k1_scalar_is_even(const secp256k1_scalar *a)
Check whether a scalar, considered as an nonnegative integer, is even.
static int secp256k1_scalar_is_zero(const secp256k1_scalar *a)
Check whether a scalar equals zero.
static void secp256k1_scalar_set_int(secp256k1_scalar *r, unsigned int v)
Set a scalar to an unsigned integer.
static unsigned int secp256k1_scalar_get_bits(const secp256k1_scalar *a, unsigned int offset, unsigned int count)
Access bits from a scalar.
static void secp256k1_scalar_negate(secp256k1_scalar *r, const secp256k1_scalar *a)
Compute the complement of a scalar (modulo the group order).
static int secp256k1_scalar_is_high(const secp256k1_scalar *a)
Check whether a scalar is higher than the group order divided by 2.
static unsigned int secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count)
Access bits from a scalar.
static void secp256k1_scalar_clear(secp256k1_scalar *r)
Clear a scalar to prevent the leak of sensitive data.
static void secp256k1_scalar_split_lambda(secp256k1_scalar *r1, secp256k1_scalar *r2, const secp256k1_scalar *k)
Find r1 and r2 such that r1+r2*lambda = k, where r1 and r2 or their negations are maximum 128 bits lo...
static void secp256k1_scratch_apply_checkpoint(const secp256k1_callback *error_callback, secp256k1_scratch *scratch, size_t checkpoint)
Applies a check point received from secp256k1_scratch_checkpoint, undoing all allocations since that ...
static size_t secp256k1_scratch_max_allocation(const secp256k1_callback *error_callback, const secp256k1_scratch *scratch, size_t n_objects)
Returns the maximum allocation the scratch space will allow.
static void * secp256k1_scratch_alloc(const secp256k1_callback *error_callback, secp256k1_scratch *scratch, size_t n)
Returns a pointer into the most recently allocated frame, or NULL if there is insufficient available ...
static size_t secp256k1_scratch_checkpoint(const secp256k1_callback *error_callback, const secp256k1_scratch *scratch)
Returns an opaque object used to "checkpoint" a scratch space.
#define CHECK(cond)
Definition: util.h:80
#define VERIFY_CHECK(cond)
Definition: util.h:95
#define SECP256K1_INLINE
Definition: secp256k1.h:127
A group element of the secp256k1 curve, in affine coordinates.
Definition: group.h:13
int infinity
Definition: group.h:16
secp256k1_fe x
Definition: group.h:14
secp256k1_fe y
Definition: group.h:15
A group element of the secp256k1 curve, in jacobian coordinates.
Definition: group.h:23
secp256k1_fe y
Definition: group.h:25
secp256k1_fe x
Definition: group.h:24
int infinity
Definition: group.h:27
secp256k1_fe z
Definition: group.h:26
struct secp256k1_pippenger_point_state * ps
Definition: ecmult_impl.h:463
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
secp256k1_ge * pre_a_lam
Definition: ecmult_impl.h:197
struct secp256k1_strauss_point_state * ps
Definition: ecmult_impl.h:198
secp256k1_gej * prej
Definition: ecmult_impl.h:194
secp256k1_ge * pre_a
Definition: ecmult_impl.h:196