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 *****************************************************************************/
7 #ifndef SECP256K1_ECMULT_IMPL_H
8 #define SECP256K1_ECMULT_IMPL_H
18 #if defined(EXHAUSTIVE_TEST_ORDER)
19 /* We need to lower these values for exhaustive tests because
20 * the tables cannot have infinities in them (this breaks the
21 * affine-isomorphism stuff which tracks z-ratios) */
22 # if EXHAUSTIVE_TEST_ORDER > 128
25 # elif EXHAUSTIVE_TEST_ORDER > 8
33 /* optimal for 128-bit and 256-bit exponents. */
35 /** Larger values for ECMULT_WINDOW_SIZE result in possibly better
36 * performance at the cost of an exponentially larger precomputed
37 * table. The exact table size is
38 * (1 << (WINDOW_G - 2)) * sizeof(secp256k1_ge_storage) bytes,
39 * where sizeof(secp256k1_ge_storage) is typically 64 bytes but can
40 * be larger due to platform-specific padding and alignment.
41 * If the endomorphism optimization is enabled (USE_ENDOMORMPHSIM)
42 * two tables of this size are used instead of only one.
44 # define WINDOW_G ECMULT_WINDOW_SIZE
47 /* Noone will ever need more than a window size of 24. The code might
48 * be correct for larger values of ECMULT_WINDOW_SIZE but this is not
51 * The following limitations are known, and there are probably more:
52 * If WINDOW_G > 27 and size_t has 32 bits, then the code is incorrect
53 * because the size of the memory object that we allocate (in bytes)
54 * will not fit in a size_t.
55 * If WINDOW_G > 31 and int has 32 bits, then the code is incorrect
56 * because certain expressions will overflow.
58 #if ECMULT_WINDOW_SIZE < 2 || ECMULT_WINDOW_SIZE > 24
59 # error Set ECMULT_WINDOW_SIZE to an integer in range [2..24].
62 #ifdef USE_ENDOMORPHISM
67 #define WNAF_SIZE_BITS(bits, w) (((bits) + (w) - 1) / (w))
68 #define WNAF_SIZE(w) WNAF_SIZE_BITS(WNAF_BITS, w)
70 /** The number of entries a table with precomputed multiples needs to have. */
71 #define ECMULT_TABLE_SIZE(w) (1 << ((w)-2))
73 /* The number of objects allocated on the scratch space for ecmult_multi algorithms */
74 #define PIPPENGER_SCRATCH_OBJECTS 6
75 #define STRAUSS_SCRATCH_OBJECTS 6
77 #define PIPPENGER_MAX_BUCKET_WINDOW 12
79 /* Minimum number of points for which pippenger_wnaf is faster than strauss wnaf */
80 #ifdef USE_ENDOMORPHISM
81 #define ECMULT_PIPPENGER_THRESHOLD 88
83 #define ECMULT_PIPPENGER_THRESHOLD 160
86 #ifdef USE_ENDOMORPHISM
87 #define ECMULT_MAX_POINTS_PER_BATCH 5000000
89 #define ECMULT_MAX_POINTS_PER_BATCH 10000000
92 /** Fill a table 'prej' with precomputed odd multiples of a. Prej will contain
93 * the values [1*a,3*a,...,(2*n-1)*a], so it space for n values. zr[0] will
94 * contain prej[0].z / a.z. The other zr[i] values = prej[i].z / prej[i-1].z.
95 * Prej's Z values are undefined, except for the last value.
97 static void secp256k1_ecmult_odd_multiples_table(int n, secp256k1_gej *prej, secp256k1_fe *zr, const secp256k1_gej *a) {
99 secp256k1_ge a_ge, d_ge;
102 VERIFY_CHECK(!a->infinity);
104 secp256k1_gej_double_var(&d, a, NULL);
107 * Perform the additions on an isomorphism where 'd' is affine: drop the z coordinate
108 * of 'd', and scale the 1P starting value's x/y coordinates without changing its z.
114 secp256k1_ge_set_gej_zinv(&a_ge, a, &d.z);
118 prej[0].infinity = 0;
121 for (i = 1; i < n; i++) {
122 secp256k1_gej_add_ge_var(&prej[i], &prej[i-1], &d_ge, &zr[i]);
126 * Each point in 'prej' has a z coordinate too small by a factor of 'd.z'. Only
127 * the final point's z coordinate is actually used though, so just update that.
129 secp256k1_fe_mul(&prej[n-1].z, &prej[n-1].z, &d.z);
132 /** Fill a table 'pre' with precomputed odd multiples of a.
134 * There are two versions of this function:
135 * - secp256k1_ecmult_odd_multiples_table_globalz_windowa which brings its
136 * resulting point set to a single constant Z denominator, stores the X and Y
137 * coordinates as ge_storage points in pre, and stores the global Z in rz.
138 * It only operates on tables sized for WINDOW_A wnaf multiples.
139 * - secp256k1_ecmult_odd_multiples_table_storage_var, which converts its
140 * resulting point set to actually affine points, and stores those in pre.
141 * It operates on tables of any size, but uses heap-allocated temporaries.
143 * To compute a*P + b*G, we compute a table for P using the first function,
144 * and for G using the second (which requires an inverse, but it only needs to
147 static void secp256k1_ecmult_odd_multiples_table_globalz_windowa(secp256k1_ge *pre, secp256k1_fe *globalz, const secp256k1_gej *a) {
148 secp256k1_gej prej[ECMULT_TABLE_SIZE(WINDOW_A)];
149 secp256k1_fe zr[ECMULT_TABLE_SIZE(WINDOW_A)];
151 /* Compute the odd multiples in Jacobian form. */
152 secp256k1_ecmult_odd_multiples_table(ECMULT_TABLE_SIZE(WINDOW_A), prej, zr, a);
153 /* Bring them to the same Z denominator. */
154 secp256k1_ge_globalz_set_table_gej(ECMULT_TABLE_SIZE(WINDOW_A), pre, globalz, prej, zr);
157 static void secp256k1_ecmult_odd_multiples_table_storage_var(const int n, secp256k1_ge_storage *pre, const secp256k1_gej *a) {
159 secp256k1_ge d_ge, p_ge;
163 secp256k1_fe dx_over_dz_squared;
166 VERIFY_CHECK(!a->infinity);
168 secp256k1_gej_double_var(&d, a, NULL);
170 /* First, we perform all the additions in an isomorphic curve obtained by multiplying
171 * all `z` coordinates by 1/`d.z`. In these coordinates `d` is affine so we can use
172 * `secp256k1_gej_add_ge_var` to perform the additions. For each addition, we store
173 * the resulting y-coordinate and the z-ratio, since we only have enough memory to
174 * store two field elements. These are sufficient to efficiently undo the isomorphism
175 * and recompute all the `x`s.
181 secp256k1_ge_set_gej_zinv(&p_ge, a, &d.z);
187 for (i = 0; i < (n - 1); i++) {
188 secp256k1_fe_normalize_var(&pj.y);
189 secp256k1_fe_to_storage(&pre[i].y, &pj.y);
190 secp256k1_gej_add_ge_var(&pj, &pj, &d_ge, &zr);
191 secp256k1_fe_normalize_var(&zr);
192 secp256k1_fe_to_storage(&pre[i].x, &zr);
195 /* Invert d.z in the same batch, preserving pj.z so we can extract 1/d.z */
196 secp256k1_fe_mul(&zi, &pj.z, &d.z);
197 secp256k1_fe_inv_var(&zi, &zi);
199 /* Directly set `pre[n - 1]` to `pj`, saving the inverted z-coordinate so
200 * that we can combine it with the saved z-ratios to compute the other zs
201 * without any more inversions. */
202 secp256k1_ge_set_gej_zinv(&p_ge, &pj, &zi);
203 secp256k1_ge_to_storage(&pre[n - 1], &p_ge);
205 /* Compute the actual x-coordinate of D, which will be needed below. */
206 secp256k1_fe_mul(&d.z, &zi, &pj.z); /* d.z = 1/d.z */
207 secp256k1_fe_sqr(&dx_over_dz_squared, &d.z);
208 secp256k1_fe_mul(&dx_over_dz_squared, &dx_over_dz_squared, &d.x);
210 /* Going into the second loop, we have set `pre[n-1]` to its final affine
211 * form, but still need to set `pre[i]` for `i` in 0 through `n-2`. We
212 * have `zi = (p.z * d.z)^-1`, where
214 * `p.z` is the z-coordinate of the point on the isomorphic curve
215 * which was ultimately assigned to `pre[n-1]`.
216 * `d.z` is the multiplier that must be applied to all z-coordinates
217 * to move from our isomorphic curve back to secp256k1; so the
218 * product `p.z * d.z` is the z-coordinate of the secp256k1
219 * point assigned to `pre[n-1]`.
221 * All subsequent inverse-z-coordinates can be obtained by multiplying this
222 * factor by successive z-ratios, which is much more efficient than directly
223 * computing each one.
225 * Importantly, these inverse-zs will be coordinates of points on secp256k1,
226 * while our other stored values come from computations on the isomorphic
227 * curve. So in the below loop, we will take care not to actually use `zi`
228 * or any derived values until we're back on secp256k1.
232 secp256k1_fe zi2, zi3;
233 const secp256k1_fe *rzr;
236 secp256k1_ge_from_storage(&p_ge, &pre[i]);
238 /* For each remaining point, we extract the z-ratio from the stored
239 * x-coordinate, compute its z^-1 from that, and compute the full
240 * point from that. */
242 secp256k1_fe_mul(&zi, &zi, rzr);
243 secp256k1_fe_sqr(&zi2, &zi);
244 secp256k1_fe_mul(&zi3, &zi2, &zi);
245 /* To compute the actual x-coordinate, we use the stored z ratio and
246 * y-coordinate, which we obtained from `secp256k1_gej_add_ge_var`
247 * in the loop above, as well as the inverse of the square of its
248 * z-coordinate. We store the latter in the `zi2` variable, which is
249 * computed iteratively starting from the overall Z inverse then
250 * multiplying by each z-ratio in turn.
252 * Denoting the z-ratio as `rzr`, we observe that it is equal to `h`
253 * from the inside of the above `gej_add_ge_var` call. This satisfies
255 * rzr = d_x * z^2 - x * d_z^2
257 * where (`d_x`, `d_z`) are Jacobian coordinates of `D` and `(x, z)`
258 * are Jacobian coordinates of our desired point -- except both are on
259 * the isomorphic curve that we were using when we called `gej_add_ge_var`.
260 * To get back to secp256k1, we must multiply both `z`s by `d_z`, or
261 * equivalently divide both `x`s by `d_z^2`. Our equation then becomes
263 * rzr = d_x * z^2 / d_z^2 - x
265 * (The left-hand-side, being a ratio of z-coordinates, is unaffected
266 * by the isomorphism.)
268 * Rearranging to solve for `x`, we have
270 * x = d_x * z^2 / d_z^2 - rzr
272 * But what we actually want is the affine coordinate `X = x/z^2`,
275 * X = d_x / d_z^2 - rzr / z^2
276 * = dx_over_dz_squared - rzr * zi2
278 secp256k1_fe_mul(&p_ge.x, rzr, &zi2);
279 secp256k1_fe_negate(&p_ge.x, &p_ge.x, 1);
280 secp256k1_fe_add(&p_ge.x, &dx_over_dz_squared);
281 /* y is stored_y/z^3, as we expect */
282 secp256k1_fe_mul(&p_ge.y, &p_ge.y, &zi3);
284 secp256k1_ge_to_storage(&pre[i], &p_ge);
288 /** The following two macro retrieves a particular odd multiple from a table
289 * of precomputed multiples. */
290 #define ECMULT_TABLE_GET_GE(r,pre,n,w) do { \
291 VERIFY_CHECK(((n) & 1) == 1); \
292 VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \
293 VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \
295 *(r) = (pre)[((n)-1)/2]; \
297 *(r) = (pre)[(-(n)-1)/2]; \
298 secp256k1_fe_negate(&((r)->y), &((r)->y), 1); \
302 #define ECMULT_TABLE_GET_GE_STORAGE(r,pre,n,w) do { \
303 VERIFY_CHECK(((n) & 1) == 1); \
304 VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \
305 VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \
307 secp256k1_ge_from_storage((r), &(pre)[((n)-1)/2]); \
309 secp256k1_ge_from_storage((r), &(pre)[(-(n)-1)/2]); \
310 secp256k1_fe_negate(&((r)->y), &((r)->y), 1); \
314 static const size_t SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE =
315 ROUND_TO_ALIGN(sizeof((*((secp256k1_ecmult_context*) NULL)->pre_g)[0]) * ECMULT_TABLE_SIZE(WINDOW_G))
316 #ifdef USE_ENDOMORPHISM
317 + ROUND_TO_ALIGN(sizeof((*((secp256k1_ecmult_context*) NULL)->pre_g_128)[0]) * ECMULT_TABLE_SIZE(WINDOW_G))
321 static void secp256k1_ecmult_context_init(secp256k1_ecmult_context *ctx) {
323 #ifdef USE_ENDOMORPHISM
324 ctx->pre_g_128 = NULL;
328 static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, void **prealloc) {
330 void* const base = *prealloc;
331 size_t const prealloc_size = SECP256K1_ECMULT_CONTEXT_PREALLOCATED_SIZE;
333 if (ctx->pre_g != NULL) {
337 /* get the generator */
338 secp256k1_gej_set_ge(&gj, &secp256k1_ge_const_g);
341 size_t size = sizeof((*ctx->pre_g)[0]) * ((size_t)ECMULT_TABLE_SIZE(WINDOW_G));
342 /* check for overflow */
343 VERIFY_CHECK(size / sizeof((*ctx->pre_g)[0]) == ((size_t)ECMULT_TABLE_SIZE(WINDOW_G)));
344 ctx->pre_g = (secp256k1_ge_storage (*)[])manual_alloc(prealloc, sizeof((*ctx->pre_g)[0]) * ECMULT_TABLE_SIZE(WINDOW_G), base, prealloc_size);
347 /* precompute the tables with odd multiples */
348 secp256k1_ecmult_odd_multiples_table_storage_var(ECMULT_TABLE_SIZE(WINDOW_G), *ctx->pre_g, &gj);
350 #ifdef USE_ENDOMORPHISM
352 secp256k1_gej g_128j;
355 size_t size = sizeof((*ctx->pre_g_128)[0]) * ((size_t) ECMULT_TABLE_SIZE(WINDOW_G));
356 /* check for overflow */
357 VERIFY_CHECK(size / sizeof((*ctx->pre_g_128)[0]) == ((size_t)ECMULT_TABLE_SIZE(WINDOW_G)));
358 ctx->pre_g_128 = (secp256k1_ge_storage (*)[])manual_alloc(prealloc, sizeof((*ctx->pre_g_128)[0]) * ECMULT_TABLE_SIZE(WINDOW_G), base, prealloc_size);
360 /* calculate 2^128*generator */
362 for (i = 0; i < 128; i++) {
363 secp256k1_gej_double_var(&g_128j, &g_128j, NULL);
365 secp256k1_ecmult_odd_multiples_table_storage_var(ECMULT_TABLE_SIZE(WINDOW_G), *ctx->pre_g_128, &g_128j);
370 static void secp256k1_ecmult_context_finalize_memcpy(secp256k1_ecmult_context *dst, const secp256k1_ecmult_context *src) {
371 if (src->pre_g != NULL) {
372 /* We cast to void* first to suppress a -Wcast-align warning. */
373 dst->pre_g = (secp256k1_ge_storage (*)[])(void*)((unsigned char*)dst + ((unsigned char*)(src->pre_g) - (unsigned char*)src));
375 #ifdef USE_ENDOMORPHISM
376 if (src->pre_g_128 != NULL) {
377 dst->pre_g_128 = (secp256k1_ge_storage (*)[])(void*)((unsigned char*)dst + ((unsigned char*)(src->pre_g_128) - (unsigned char*)src));
382 static int secp256k1_ecmult_context_is_built(const secp256k1_ecmult_context *ctx) {
383 return ctx->pre_g != NULL;
386 static void secp256k1_ecmult_context_clear(secp256k1_ecmult_context *ctx) {
387 secp256k1_ecmult_context_init(ctx);
390 /** Convert a number to WNAF notation. The number becomes represented by sum(2^i * wnaf[i], i=0..bits),
391 * with the following guarantees:
392 * - each wnaf[i] is either 0, or an odd integer between -(1<<(w-1) - 1) and (1<<(w-1) - 1)
393 * - two non-zero entries in wnaf are separated by at least w-1 zeroes.
394 * - the number of set values in wnaf is returned. This number is at most 256, and at most one more
395 * than the number of bits in the (absolute value) of the input.
397 static int secp256k1_ecmult_wnaf(int *wnaf, int len, const secp256k1_scalar *a, int w) {
398 secp256k1_scalar s = *a;
399 int last_set_bit = -1;
404 VERIFY_CHECK(wnaf != NULL);
405 VERIFY_CHECK(0 <= len && len <= 256);
406 VERIFY_CHECK(a != NULL);
407 VERIFY_CHECK(2 <= w && w <= 31);
409 memset(wnaf, 0, len * sizeof(wnaf[0]));
411 if (secp256k1_scalar_get_bits(&s, 255, 1)) {
412 secp256k1_scalar_negate(&s, &s);
419 if (secp256k1_scalar_get_bits(&s, bit, 1) == (unsigned int)carry) {
425 if (now > len - bit) {
429 word = secp256k1_scalar_get_bits_var(&s, bit, now) + carry;
431 carry = (word >> (w-1)) & 1;
434 wnaf[bit] = sign * word;
442 CHECK(secp256k1_scalar_get_bits(&s, bit++, 1) == 0);
445 return last_set_bit + 1;
448 struct secp256k1_strauss_point_state {
449 #ifdef USE_ENDOMORPHISM
450 secp256k1_scalar na_1, na_lam;
452 int wnaf_na_lam[130];
462 struct secp256k1_strauss_state {
466 #ifdef USE_ENDOMORPHISM
467 secp256k1_ge* pre_a_lam;
469 struct secp256k1_strauss_point_state* ps;
472 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) {
475 #ifdef USE_ENDOMORPHISM
476 /* Splitted G factors. */
477 secp256k1_scalar ng_1, ng_128;
480 int wnaf_ng_128[129];
491 for (np = 0; np < num; ++np) {
492 if (secp256k1_scalar_is_zero(&na[np]) || secp256k1_gej_is_infinity(&a[np])) {
495 state->ps[no].input_pos = np;
496 #ifdef USE_ENDOMORPHISM
497 /* split na into na_1 and na_lam (where na = na_1 + na_lam*lambda, and na_1 and na_lam are ~128 bit) */
498 secp256k1_scalar_split_lambda(&state->ps[no].na_1, &state->ps[no].na_lam, &na[np]);
500 /* build wnaf representation for na_1 and na_lam. */
501 state->ps[no].bits_na_1 = secp256k1_ecmult_wnaf(state->ps[no].wnaf_na_1, 130, &state->ps[no].na_1, WINDOW_A);
502 state->ps[no].bits_na_lam = secp256k1_ecmult_wnaf(state->ps[no].wnaf_na_lam, 130, &state->ps[no].na_lam, WINDOW_A);
503 VERIFY_CHECK(state->ps[no].bits_na_1 <= 130);
504 VERIFY_CHECK(state->ps[no].bits_na_lam <= 130);
505 if (state->ps[no].bits_na_1 > bits) {
506 bits = state->ps[no].bits_na_1;
508 if (state->ps[no].bits_na_lam > bits) {
509 bits = state->ps[no].bits_na_lam;
512 /* build wnaf representation for na. */
513 state->ps[no].bits_na = secp256k1_ecmult_wnaf(state->ps[no].wnaf_na, 256, &na[np], WINDOW_A);
514 if (state->ps[no].bits_na > bits) {
515 bits = state->ps[no].bits_na;
521 /* Calculate odd multiples of a.
522 * All multiples are brought to the same Z 'denominator', which is stored
523 * in Z. Due to secp256k1' isomorphism we can do all operations pretending
524 * that the Z coordinate was 1, use affine addition formulae, and correct
525 * the Z coordinate of the result once at the end.
526 * The exception is the precomputed G table points, which are actually
527 * affine. Compared to the base used for other points, they have a Z ratio
528 * of 1/Z, so we can use secp256k1_gej_add_zinv_var, which uses the same
529 * isomorphism to efficiently add with a known Z inverse.
532 /* Compute the odd multiples in Jacobian form. */
533 secp256k1_ecmult_odd_multiples_table(ECMULT_TABLE_SIZE(WINDOW_A), state->prej, state->zr, &a[state->ps[0].input_pos]);
534 for (np = 1; np < no; ++np) {
535 secp256k1_gej tmp = a[state->ps[np].input_pos];
537 secp256k1_fe_normalize_var(&(state->prej[(np - 1) * ECMULT_TABLE_SIZE(WINDOW_A) + ECMULT_TABLE_SIZE(WINDOW_A) - 1].z));
539 secp256k1_gej_rescale(&tmp, &(state->prej[(np - 1) * ECMULT_TABLE_SIZE(WINDOW_A) + ECMULT_TABLE_SIZE(WINDOW_A) - 1].z));
540 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);
541 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));
543 /* Bring them to the same Z denominator. */
544 secp256k1_ge_globalz_set_table_gej(ECMULT_TABLE_SIZE(WINDOW_A) * no, state->pre_a, &Z, state->prej, state->zr);
546 secp256k1_fe_set_int(&Z, 1);
549 #ifdef USE_ENDOMORPHISM
550 for (np = 0; np < no; ++np) {
551 for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) {
552 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]);
557 /* 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) */
558 secp256k1_scalar_split_128(&ng_1, &ng_128, ng);
560 /* Build wnaf representation for ng_1 and ng_128 */
561 bits_ng_1 = secp256k1_ecmult_wnaf(wnaf_ng_1, 129, &ng_1, WINDOW_G);
562 bits_ng_128 = secp256k1_ecmult_wnaf(wnaf_ng_128, 129, &ng_128, WINDOW_G);
563 if (bits_ng_1 > bits) {
566 if (bits_ng_128 > bits) {
572 bits_ng = secp256k1_ecmult_wnaf(wnaf_ng, 256, ng, WINDOW_G);
573 if (bits_ng > bits) {
579 secp256k1_gej_set_infinity(r);
581 for (i = bits - 1; i >= 0; i--) {
583 secp256k1_gej_double_var(r, r, NULL);
584 #ifdef USE_ENDOMORPHISM
585 for (np = 0; np < no; ++np) {
586 if (i < state->ps[np].bits_na_1 && (n = state->ps[np].wnaf_na_1[i])) {
587 ECMULT_TABLE_GET_GE(&tmpa, state->pre_a + np * ECMULT_TABLE_SIZE(WINDOW_A), n, WINDOW_A);
588 secp256k1_gej_add_ge_var(r, r, &tmpa, NULL);
590 if (i < state->ps[np].bits_na_lam && (n = state->ps[np].wnaf_na_lam[i])) {
591 ECMULT_TABLE_GET_GE(&tmpa, state->pre_a_lam + np * ECMULT_TABLE_SIZE(WINDOW_A), n, WINDOW_A);
592 secp256k1_gej_add_ge_var(r, r, &tmpa, NULL);
595 if (i < bits_ng_1 && (n = wnaf_ng_1[i])) {
596 ECMULT_TABLE_GET_GE_STORAGE(&tmpa, *ctx->pre_g, n, WINDOW_G);
597 secp256k1_gej_add_zinv_var(r, r, &tmpa, &Z);
599 if (i < bits_ng_128 && (n = wnaf_ng_128[i])) {
600 ECMULT_TABLE_GET_GE_STORAGE(&tmpa, *ctx->pre_g_128, n, WINDOW_G);
601 secp256k1_gej_add_zinv_var(r, r, &tmpa, &Z);
604 for (np = 0; np < no; ++np) {
605 if (i < state->ps[np].bits_na && (n = state->ps[np].wnaf_na[i])) {
606 ECMULT_TABLE_GET_GE(&tmpa, state->pre_a + np * ECMULT_TABLE_SIZE(WINDOW_A), n, WINDOW_A);
607 secp256k1_gej_add_ge_var(r, r, &tmpa, NULL);
610 if (i < bits_ng && (n = wnaf_ng[i])) {
611 ECMULT_TABLE_GET_GE_STORAGE(&tmpa, *ctx->pre_g, n, WINDOW_G);
612 secp256k1_gej_add_zinv_var(r, r, &tmpa, &Z);
618 secp256k1_fe_mul(&r->z, &r->z, &Z);
622 static void secp256k1_ecmult(const secp256k1_ecmult_context *ctx, secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_scalar *na, const secp256k1_scalar *ng) {
623 secp256k1_gej prej[ECMULT_TABLE_SIZE(WINDOW_A)];
624 secp256k1_fe zr[ECMULT_TABLE_SIZE(WINDOW_A)];
625 secp256k1_ge pre_a[ECMULT_TABLE_SIZE(WINDOW_A)];
626 struct secp256k1_strauss_point_state ps[1];
627 #ifdef USE_ENDOMORPHISM
628 secp256k1_ge pre_a_lam[ECMULT_TABLE_SIZE(WINDOW_A)];
630 struct secp256k1_strauss_state state;
635 #ifdef USE_ENDOMORPHISM
636 state.pre_a_lam = pre_a_lam;
639 secp256k1_ecmult_strauss_wnaf(ctx, &state, r, 1, a, na, ng);
642 static size_t secp256k1_strauss_scratch_size(size_t n_points) {
643 #ifdef USE_ENDOMORPHISM
644 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);
646 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);
648 return n_points*point_size;
651 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) {
652 secp256k1_gej* points;
653 secp256k1_scalar* scalars;
654 struct secp256k1_strauss_state state;
657 secp256k1_gej_set_infinity(r);
658 if (inp_g_sc == NULL && n_points == 0) {
662 if (!secp256k1_scratch_allocate_frame(scratch, secp256k1_strauss_scratch_size(n_points), STRAUSS_SCRATCH_OBJECTS)) {
665 points = (secp256k1_gej*)secp256k1_scratch_alloc(scratch, n_points * sizeof(secp256k1_gej));
666 scalars = (secp256k1_scalar*)secp256k1_scratch_alloc(scratch, n_points * sizeof(secp256k1_scalar));
667 state.prej = (secp256k1_gej*)secp256k1_scratch_alloc(scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_gej));
668 state.zr = (secp256k1_fe*)secp256k1_scratch_alloc(scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_fe));
669 #ifdef USE_ENDOMORPHISM
670 state.pre_a = (secp256k1_ge*)secp256k1_scratch_alloc(scratch, n_points * 2 * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_ge));
671 state.pre_a_lam = state.pre_a + n_points * ECMULT_TABLE_SIZE(WINDOW_A);
673 state.pre_a = (secp256k1_ge*)secp256k1_scratch_alloc(scratch, n_points * ECMULT_TABLE_SIZE(WINDOW_A) * sizeof(secp256k1_ge));
675 state.ps = (struct secp256k1_strauss_point_state*)secp256k1_scratch_alloc(scratch, n_points * sizeof(struct secp256k1_strauss_point_state));
677 for (i = 0; i < n_points; i++) {
679 if (!cb(&scalars[i], &point, i+cb_offset, cbdata)) {
680 secp256k1_scratch_deallocate_frame(scratch);
683 secp256k1_gej_set_ge(&points[i], &point);
685 secp256k1_ecmult_strauss_wnaf(ctx, &state, r, n_points, points, scalars, inp_g_sc);
686 secp256k1_scratch_deallocate_frame(scratch);
690 /* Wrapper for secp256k1_ecmult_multi_func interface */
691 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) {
692 return secp256k1_ecmult_strauss_batch(actx, scratch, r, inp_g_sc, cb, cbdata, n, 0);
695 static size_t secp256k1_strauss_max_points(secp256k1_scratch *scratch) {
696 return secp256k1_scratch_max_allocation(scratch, STRAUSS_SCRATCH_OBJECTS) / secp256k1_strauss_scratch_size(1);
699 /** Convert a number to WNAF notation.
700 * The number becomes represented by sum(2^{wi} * wnaf[i], i=0..WNAF_SIZE(w)+1) - return_val.
701 * It has the following guarantees:
702 * - each wnaf[i] is either 0 or an odd integer between -(1 << w) and (1 << w)
703 * - the number of words set is always WNAF_SIZE(w)
704 * - the returned skew is 0 or 1
706 static int secp256k1_wnaf_fixed(int *wnaf, const secp256k1_scalar *s, int w) {
711 const secp256k1_scalar *work = s;
713 if (secp256k1_scalar_is_zero(s)) {
714 for (pos = 0; pos < WNAF_SIZE(w); pos++) {
720 if (secp256k1_scalar_is_even(s)) {
724 wnaf[0] = secp256k1_scalar_get_bits_var(work, 0, w) + skew;
725 /* Compute last window size. Relevant when window size doesn't divide the
726 * number of bits in the scalar */
727 last_w = WNAF_BITS - (WNAF_SIZE(w) - 1) * w;
729 /* Store the position of the first nonzero word in max_pos to allow
730 * skipping leading zeros when calculating the wnaf. */
731 for (pos = WNAF_SIZE(w) - 1; pos > 0; pos--) {
732 int val = secp256k1_scalar_get_bits_var(work, pos * w, pos == WNAF_SIZE(w)-1 ? last_w : w);
741 while (pos <= max_pos) {
742 int val = secp256k1_scalar_get_bits_var(work, pos * w, pos == WNAF_SIZE(w)-1 ? last_w : w);
743 if ((val & 1) == 0) {
744 wnaf[pos - 1] -= (1 << w);
745 wnaf[pos] = (val + 1);
749 /* Set a coefficient to zero if it is 1 or -1 and the proceeding digit
750 * is strictly negative or strictly positive respectively. Only change
751 * coefficients at previous positions because above code assumes that
752 * wnaf[pos - 1] is odd.
754 if (pos >= 2 && ((wnaf[pos - 1] == 1 && wnaf[pos - 2] < 0) || (wnaf[pos - 1] == -1 && wnaf[pos - 2] > 0))) {
755 if (wnaf[pos - 1] == 1) {
756 wnaf[pos - 2] += 1 << w;
758 wnaf[pos - 2] -= 1 << w;
768 struct secp256k1_pippenger_point_state {
773 struct secp256k1_pippenger_state {
775 struct secp256k1_pippenger_point_state* ps;
779 * pippenger_wnaf computes the result of a multi-point multiplication as
780 * follows: The scalars are brought into wnaf with n_wnaf elements each. Then
781 * for every i < n_wnaf, first each point is added to a "bucket" corresponding
782 * to the point's wnaf[i]. Second, the buckets are added together such that
783 * r += 1*bucket[0] + 3*bucket[1] + 5*bucket[2] + ...
785 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) {
786 size_t n_wnaf = WNAF_SIZE(bucket_window+1);
792 for (np = 0; np < num; ++np) {
793 if (secp256k1_scalar_is_zero(&sc[np]) || secp256k1_ge_is_infinity(&pt[np])) {
796 state->ps[no].input_pos = np;
797 state->ps[no].skew_na = secp256k1_wnaf_fixed(&state->wnaf_na[no*n_wnaf], &sc[np], bucket_window+1);
800 secp256k1_gej_set_infinity(r);
806 for (i = n_wnaf - 1; i >= 0; i--) {
807 secp256k1_gej running_sum;
809 for(j = 0; j < ECMULT_TABLE_SIZE(bucket_window+2); j++) {
810 secp256k1_gej_set_infinity(&buckets[j]);
813 for (np = 0; np < no; ++np) {
814 int n = state->wnaf_na[np*n_wnaf + i];
815 struct secp256k1_pippenger_point_state point_state = state->ps[np];
820 /* correct for wnaf skew */
821 int skew = point_state.skew_na;
823 secp256k1_ge_neg(&tmp, &pt[point_state.input_pos]);
824 secp256k1_gej_add_ge_var(&buckets[0], &buckets[0], &tmp, NULL);
829 secp256k1_gej_add_ge_var(&buckets[idx], &buckets[idx], &pt[point_state.input_pos], NULL);
832 secp256k1_ge_neg(&tmp, &pt[point_state.input_pos]);
833 secp256k1_gej_add_ge_var(&buckets[idx], &buckets[idx], &tmp, NULL);
837 for(j = 0; j < bucket_window; j++) {
838 secp256k1_gej_double_var(r, r, NULL);
841 secp256k1_gej_set_infinity(&running_sum);
842 /* Accumulate the sum: bucket[0] + 3*bucket[1] + 5*bucket[2] + 7*bucket[3] + ...
843 * = bucket[0] + bucket[1] + bucket[2] + bucket[3] + ...
844 * + 2 * (bucket[1] + 2*bucket[2] + 3*bucket[3] + ...)
845 * using an intermediate running sum:
846 * running_sum = bucket[0] + bucket[1] + bucket[2] + ...
848 * The doubling is done implicitly by deferring the final window doubling (of 'r').
850 for(j = ECMULT_TABLE_SIZE(bucket_window+2) - 1; j > 0; j--) {
851 secp256k1_gej_add_var(&running_sum, &running_sum, &buckets[j], NULL);
852 secp256k1_gej_add_var(r, r, &running_sum, NULL);
855 secp256k1_gej_add_var(&running_sum, &running_sum, &buckets[0], NULL);
856 secp256k1_gej_double_var(r, r, NULL);
857 secp256k1_gej_add_var(r, r, &running_sum, NULL);
863 * Returns optimal bucket_window (number of bits of a scalar represented by a
864 * set of buckets) for a given number of points.
866 static int secp256k1_pippenger_bucket_window(size_t n) {
867 #ifdef USE_ENDOMORPHISM
872 } else if (n <= 20) {
874 } else if (n <= 57) {
876 } else if (n <= 136) {
878 } else if (n <= 235) {
880 } else if (n <= 1260) {
882 } else if (n <= 4420) {
884 } else if (n <= 7880) {
886 } else if (n <= 16050) {
889 return PIPPENGER_MAX_BUCKET_WINDOW;
894 } else if (n <= 11) {
896 } else if (n <= 45) {
898 } else if (n <= 100) {
900 } else if (n <= 275) {
902 } else if (n <= 625) {
904 } else if (n <= 1850) {
906 } else if (n <= 3400) {
908 } else if (n <= 9630) {
910 } else if (n <= 17900) {
912 } else if (n <= 32800) {
915 return PIPPENGER_MAX_BUCKET_WINDOW;
921 * Returns the maximum optimal number of points for a bucket_window.
923 static size_t secp256k1_pippenger_bucket_window_inv(int bucket_window) {
924 switch(bucket_window) {
925 #ifdef USE_ENDOMORPHISM
935 case 10: return 7880;
936 case 11: return 16050;
937 case PIPPENGER_MAX_BUCKET_WINDOW: return SIZE_MAX;
948 case 10: return 17900;
949 case 11: return 32800;
950 case PIPPENGER_MAX_BUCKET_WINDOW: return SIZE_MAX;
957 #ifdef USE_ENDOMORPHISM
958 SECP256K1_INLINE static void secp256k1_ecmult_endo_split(secp256k1_scalar *s1, secp256k1_scalar *s2, secp256k1_ge *p1, secp256k1_ge *p2) {
959 secp256k1_scalar tmp = *s1;
960 secp256k1_scalar_split_lambda(s1, s2, &tmp);
961 secp256k1_ge_mul_lambda(p2, p1);
963 if (secp256k1_scalar_is_high(s1)) {
964 secp256k1_scalar_negate(s1, s1);
965 secp256k1_ge_neg(p1, p1);
967 if (secp256k1_scalar_is_high(s2)) {
968 secp256k1_scalar_negate(s2, s2);
969 secp256k1_ge_neg(p2, p2);
975 * Returns the scratch size required for a given number of points (excluding
976 * base point G) without considering alignment.
978 static size_t secp256k1_pippenger_scratch_size(size_t n_points, int bucket_window) {
979 #ifdef USE_ENDOMORPHISM
980 size_t entries = 2*n_points + 2;
982 size_t entries = n_points + 1;
984 size_t entry_size = sizeof(secp256k1_ge) + sizeof(secp256k1_scalar) + sizeof(struct secp256k1_pippenger_point_state) + (WNAF_SIZE(bucket_window+1)+1)*sizeof(int);
985 return (sizeof(secp256k1_gej) << bucket_window) + sizeof(struct secp256k1_pippenger_state) + entries * entry_size;
988 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) {
989 /* Use 2(n+1) with the endomorphism, n+1 without, when calculating batch
990 * sizes. The reason for +1 is that we add the G scalar to the list of
992 #ifdef USE_ENDOMORPHISM
993 size_t entries = 2*n_points + 2;
995 size_t entries = n_points + 1;
997 secp256k1_ge *points;
998 secp256k1_scalar *scalars;
999 secp256k1_gej *buckets;
1000 struct secp256k1_pippenger_state *state_space;
1002 size_t point_idx = 0;
1007 secp256k1_gej_set_infinity(r);
1008 if (inp_g_sc == NULL && n_points == 0) {
1012 bucket_window = secp256k1_pippenger_bucket_window(n_points);
1013 if (!secp256k1_scratch_allocate_frame(scratch, secp256k1_pippenger_scratch_size(n_points, bucket_window), PIPPENGER_SCRATCH_OBJECTS)) {
1016 points = (secp256k1_ge *) secp256k1_scratch_alloc(scratch, entries * sizeof(*points));
1017 scalars = (secp256k1_scalar *) secp256k1_scratch_alloc(scratch, entries * sizeof(*scalars));
1018 state_space = (struct secp256k1_pippenger_state *) secp256k1_scratch_alloc(scratch, sizeof(*state_space));
1019 state_space->ps = (struct secp256k1_pippenger_point_state *) secp256k1_scratch_alloc(scratch, entries * sizeof(*state_space->ps));
1020 state_space->wnaf_na = (int *) secp256k1_scratch_alloc(scratch, entries*(WNAF_SIZE(bucket_window+1)) * sizeof(int));
1021 buckets = (secp256k1_gej *) secp256k1_scratch_alloc(scratch, sizeof(*buckets) << bucket_window);
1023 if (inp_g_sc != NULL) {
1024 scalars[0] = *inp_g_sc;
1025 points[0] = secp256k1_ge_const_g;
1027 #ifdef USE_ENDOMORPHISM
1028 secp256k1_ecmult_endo_split(&scalars[0], &scalars[1], &points[0], &points[1]);
1033 while (point_idx < n_points) {
1034 if (!cb(&scalars[idx], &points[idx], point_idx + cb_offset, cbdata)) {
1035 secp256k1_scratch_deallocate_frame(scratch);
1039 #ifdef USE_ENDOMORPHISM
1040 secp256k1_ecmult_endo_split(&scalars[idx - 1], &scalars[idx], &points[idx - 1], &points[idx]);
1046 secp256k1_ecmult_pippenger_wnaf(buckets, bucket_window, state_space, r, scalars, points, idx);
1049 for(i = 0; (size_t)i < idx; i++) {
1050 secp256k1_scalar_clear(&scalars[i]);
1051 state_space->ps[i].skew_na = 0;
1052 for(j = 0; j < WNAF_SIZE(bucket_window+1); j++) {
1053 state_space->wnaf_na[i * WNAF_SIZE(bucket_window+1) + j] = 0;
1056 for(i = 0; i < 1<<bucket_window; i++) {
1057 secp256k1_gej_clear(&buckets[i]);
1059 secp256k1_scratch_deallocate_frame(scratch);
1063 /* Wrapper for secp256k1_ecmult_multi_func interface */
1064 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) {
1065 return secp256k1_ecmult_pippenger_batch(actx, scratch, r, inp_g_sc, cb, cbdata, n, 0);
1069 * Returns the maximum number of points in addition to G that can be used with
1070 * a given scratch space. The function ensures that fewer points may also be
1073 static size_t secp256k1_pippenger_max_points(secp256k1_scratch *scratch) {
1074 size_t max_alloc = secp256k1_scratch_max_allocation(scratch, PIPPENGER_SCRATCH_OBJECTS);
1078 for (bucket_window = 1; bucket_window <= PIPPENGER_MAX_BUCKET_WINDOW; bucket_window++) {
1080 size_t max_points = secp256k1_pippenger_bucket_window_inv(bucket_window);
1081 size_t space_for_points;
1082 size_t space_overhead;
1083 size_t entry_size = sizeof(secp256k1_ge) + sizeof(secp256k1_scalar) + sizeof(struct secp256k1_pippenger_point_state) + (WNAF_SIZE(bucket_window+1)+1)*sizeof(int);
1085 #ifdef USE_ENDOMORPHISM
1086 entry_size = 2*entry_size;
1088 space_overhead = (sizeof(secp256k1_gej) << bucket_window) + entry_size + sizeof(struct secp256k1_pippenger_state);
1089 if (space_overhead > max_alloc) {
1092 space_for_points = max_alloc - space_overhead;
1094 n_points = space_for_points/entry_size;
1095 n_points = n_points > max_points ? max_points : n_points;
1096 if (n_points > res) {
1099 if (n_points < max_points) {
1100 /* A larger bucket_window may support even more points. But if we
1101 * would choose that then the caller couldn't safely use any number
1102 * smaller than what this function returns */
1109 /* Computes ecmult_multi by simply multiplying and adding each point. Does not
1110 * require a scratch space */
1111 static int secp256k1_ecmult_multi_simple_var(const secp256k1_ecmult_context *ctx, secp256k1_gej *r, const secp256k1_scalar *inp_g_sc, secp256k1_ecmult_multi_callback cb, void *cbdata, size_t n_points) {
1113 secp256k1_scalar szero;
1116 secp256k1_scalar_set_int(&szero, 0);
1117 secp256k1_gej_set_infinity(r);
1118 secp256k1_gej_set_infinity(&tmpj);
1119 /* r = inp_g_sc*G */
1120 secp256k1_ecmult(ctx, r, &tmpj, &szero, inp_g_sc);
1121 for (point_idx = 0; point_idx < n_points; point_idx++) {
1123 secp256k1_gej pointj;
1124 secp256k1_scalar scalar;
1125 if (!cb(&scalar, &point, point_idx, cbdata)) {
1128 /* r += scalar*point */
1129 secp256k1_gej_set_ge(&pointj, &point);
1130 secp256k1_ecmult(ctx, &tmpj, &pointj, &scalar, NULL);
1131 secp256k1_gej_add_var(r, r, &tmpj, NULL);
1136 /* Compute the number of batches and the batch size given the maximum batch size and the
1137 * total number of points */
1138 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) {
1139 if (max_n_batch_points == 0) {
1142 if (max_n_batch_points > ECMULT_MAX_POINTS_PER_BATCH) {
1143 max_n_batch_points = ECMULT_MAX_POINTS_PER_BATCH;
1147 *n_batch_points = 0;
1150 /* Compute ceil(n/max_n_batch_points) and ceil(n/n_batches) */
1151 *n_batches = 1 + (n - 1) / max_n_batch_points;
1152 *n_batch_points = 1 + (n - 1) / *n_batches;
1156 typedef int (*secp256k1_ecmult_multi_func)(const secp256k1_ecmult_context*, secp256k1_scratch*, secp256k1_gej*, const secp256k1_scalar*, secp256k1_ecmult_multi_callback cb, void*, size_t);
1157 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) {
1160 int (*f)(const secp256k1_ecmult_context*, secp256k1_scratch*, secp256k1_gej*, const secp256k1_scalar*, secp256k1_ecmult_multi_callback cb, void*, size_t, size_t);
1162 size_t n_batch_points;
1164 secp256k1_gej_set_infinity(r);
1165 if (inp_g_sc == NULL && n == 0) {
1167 } else if (n == 0) {
1168 secp256k1_scalar szero;
1169 secp256k1_scalar_set_int(&szero, 0);
1170 secp256k1_ecmult(ctx, r, r, &szero, inp_g_sc);
1173 if (scratch == NULL) {
1174 return secp256k1_ecmult_multi_simple_var(ctx, r, inp_g_sc, cb, cbdata, n);
1177 /* Compute the batch sizes for Pippenger's algorithm given a scratch space. If it's greater than
1178 * a threshold use Pippenger's algorithm. Otherwise use Strauss' algorithm.
1179 * As a first step check if there's enough space for Pippenger's algo (which requires less space
1180 * than Strauss' algo) and if not, use the simple algorithm. */
1181 if (!secp256k1_ecmult_multi_batch_size_helper(&n_batches, &n_batch_points, secp256k1_pippenger_max_points(scratch), n)) {
1182 return secp256k1_ecmult_multi_simple_var(ctx, r, inp_g_sc, cb, cbdata, n);
1184 if (n_batch_points >= ECMULT_PIPPENGER_THRESHOLD) {
1185 f = secp256k1_ecmult_pippenger_batch;
1187 if (!secp256k1_ecmult_multi_batch_size_helper(&n_batches, &n_batch_points, secp256k1_strauss_max_points(scratch), n)) {
1188 return secp256k1_ecmult_multi_simple_var(ctx, r, inp_g_sc, cb, cbdata, n);
1190 f = secp256k1_ecmult_strauss_batch;
1192 for(i = 0; i < n_batches; i++) {
1193 size_t nbp = n < n_batch_points ? n : n_batch_points;
1194 size_t offset = n_batch_points*i;
1196 if (!f(ctx, scratch, &tmp, i == 0 ? inp_g_sc : NULL, cb, cbdata, nbp, offset)) {
1199 secp256k1_gej_add_var(r, r, &tmp, NULL);
1205 #endif /* SECP256K1_ECMULT_IMPL_H */