7#ifndef SECP256K1_ECMULT_IMPL_H
8#define SECP256K1_ECMULT_IMPL_H
19#if defined(EXHAUSTIVE_TEST_ORDER)
23# if EXHAUSTIVE_TEST_ORDER > 128
25# elif EXHAUSTIVE_TEST_ORDER > 8
45#define WNAF_SIZE_BITS(bits, w) CEIL_DIV(bits, w)
46#define WNAF_SIZE(w) WNAF_SIZE_BITS(WNAF_BITS, w)
49#define PIPPENGER_SCRATCH_OBJECTS 6
50#define STRAUSS_SCRATCH_OBJECTS 5
52#define PIPPENGER_MAX_BUCKET_WINDOW 12
55#define ECMULT_PIPPENGER_THRESHOLD 88
57#define ECMULT_MAX_POINTS_PER_BATCH 5000000
80 secp256k1_gej_double_var(&d, a, NULL);
95 secp256k1_ge_set_xy(&d_ge, &d.
x, &d.
y);
96 secp256k1_ge_set_gej_zinv(&pre_a[0], a, &d.
z);
97 secp256k1_gej_set_ge(&ai, &pre_a[0]);
105 for (i = 1; i < n; i++) {
106 secp256k1_gej_add_ge_var(&ai, &ai, &d_ge, &zr[i]);
107 secp256k1_ge_set_xy(&pre_a[i], &ai.
x, &ai.
y);
138 secp256k1_ge_set_xy(r, &x[(n-1)/2], &pre[(n-1)/2].y);
140 secp256k1_ge_set_xy(r, &x[(-n-1)/2], &pre[(-n-1)/2].y);
148 secp256k1_ge_from_storage(r, &pre[(n-1)/2]);
150 secp256k1_ge_from_storage(r, &pre[(-n-1)/2]);
164 int last_set_bit = -1;
174 for (bit = 0; bit < len; bit++) {
179 if (secp256k1_scalar_get_bits_limb32(&s, 255, 1)) {
180 secp256k1_scalar_negate(&s, &s);
188 if (secp256k1_scalar_get_bits_limb32(&s, bit, 1) == (
unsigned int)carry) {
194 if (now > len - bit) {
198 word = secp256k1_scalar_get_bits_var(&s, bit, now) + carry;
200 carry = (word >> (w-1)) & 1;
203 wnaf[bit] =
sign * word;
210 int verify_bit = bit;
214 while (verify_bit < 256) {
215 VERIFY_CHECK(secp256k1_scalar_get_bits_limb32(&s, verify_bit, 1) == 0);
220 return last_set_bit + 1;
231 for (i = 0; i < len; i++) {
232 wnaf[i] = (int8_t)wnaf_tmp[i];
259 int wnaf_ng_128[129];
267 for (np = 0; np < num; ++np) {
270 if (secp256k1_scalar_is_zero(&na[np]) || secp256k1_gej_is_infinity(&a[np])) {
274 secp256k1_scalar_split_lambda(&na_1, &na_lam, &na[np]);
300 secp256k1_gej_rescale(&tmp, &Z);
313 for (np = 0; np < no; ++np) {
322 secp256k1_scalar_split_128(&ng_1, &ng_128, ng);
327 if (bits_ng_1 > bits) {
330 if (bits_ng_128 > bits) {
335 secp256k1_gej_set_infinity(r);
337 for (i = bits - 1; i >= 0; i--) {
339 secp256k1_gej_double_var(r, r, NULL);
340 for (np = 0; np < no; ++np) {
343 secp256k1_gej_add_ge_var(r, r, &tmpa, NULL);
347 secp256k1_gej_add_ge_var(r, r, &tmpa, NULL);
350 if (i < bits_ng_1 && (n = wnaf_ng_1[i])) {
352 secp256k1_gej_add_zinv_var(r, r, &tmpa, &Z);
354 if (i < bits_ng_128 && (n = wnaf_ng_128[i])) {
356 secp256k1_gej_add_zinv_var(r, r, &tmpa, &Z);
360 if (!secp256k1_gej_is_infinity(r)) {
379 return n_points*point_size;
387 const size_t scratch_checkpoint = secp256k1_scratch_checkpoint(error_callback, scratch);
389 secp256k1_gej_set_infinity(r);
390 if (inp_g_sc == NULL && n_points == 0) {
403 if (points == NULL || scalars == NULL || state.
aux == NULL || state.
pre_a == NULL || state.
ps == NULL) {
404 secp256k1_scratch_apply_checkpoint(error_callback, scratch, scratch_checkpoint);
408 for (i = 0; i < n_points; i++) {
410 if (!cb(&scalars[i], &point, i+cb_offset, cbdata)) {
411 secp256k1_scratch_apply_checkpoint(error_callback, scratch, scratch_checkpoint);
414 secp256k1_gej_set_ge(&points[i], &point);
417 secp256k1_scratch_apply_checkpoint(error_callback, scratch, scratch_checkpoint);
444 if (secp256k1_scalar_is_zero(s)) {
445 for (pos = 0; pos <
WNAF_SIZE(w); pos++) {
451 if (secp256k1_scalar_is_even(s)) {
455 wnaf[0] = secp256k1_scalar_get_bits_var(work, 0, w) + skew;
462 for (pos =
WNAF_SIZE(w) - 1; pos > 0; pos--) {
463 int val = secp256k1_scalar_get_bits_var(work, pos * w, pos ==
WNAF_SIZE(w)-1 ? last_w : w);
472 while (pos <= max_pos) {
473 int val = secp256k1_scalar_get_bits_var(work, pos * w, pos ==
WNAF_SIZE(w)-1 ? last_w : w);
474 if ((val & 1) == 0) {
475 wnaf[pos - 1] -= (1 << w);
476 wnaf[pos] = (val + 1);
485 if (pos >= 2 && ((wnaf[pos - 1] == 1 && wnaf[pos - 2] < 0) || (wnaf[pos - 1] == -1 && wnaf[pos - 2] > 0))) {
486 if (wnaf[pos - 1] == 1) {
487 wnaf[pos - 2] += 1 << w;
489 wnaf[pos - 2] -= 1 << w;
517 size_t n_wnaf =
WNAF_SIZE(bucket_window+1);
522 for (np = 0; np < num; ++np) {
523 if (secp256k1_scalar_is_zero(&sc[np]) || secp256k1_ge_is_infinity(&pt[np])) {
530 secp256k1_gej_set_infinity(r);
536 for (i = n_wnaf - 1; i >= 0; i--) {
542 secp256k1_gej_set_infinity(&buckets[buc]);
545 for (np = 0; np < no; ++np) {
546 int n = state->
wnaf_na[np*n_wnaf + i];
552 int skew = point_state.
skew_na;
554 secp256k1_ge_neg(&tmp, &pt[point_state.
input_pos]);
555 secp256k1_gej_add_ge_var(&buckets[0], &buckets[0], &tmp, NULL);
560 secp256k1_gej_add_ge_var(&buckets[buc], &buckets[buc], &pt[point_state.
input_pos], NULL);
563 secp256k1_ge_neg(&tmp, &pt[point_state.
input_pos]);
564 secp256k1_gej_add_ge_var(&buckets[buc], &buckets[buc], &tmp, NULL);
568 for (j = 0; j < bucket_window; j++) {
569 secp256k1_gej_double_var(r, r, NULL);
572 secp256k1_gej_set_infinity(&running_sum);
582 secp256k1_gej_add_var(&running_sum, &running_sum, &buckets[buc], NULL);
583 secp256k1_gej_add_var(r, r, &running_sum, NULL);
586 secp256k1_gej_add_var(&running_sum, &running_sum, &buckets[0], NULL);
587 secp256k1_gej_double_var(r, r, NULL);
588 secp256k1_gej_add_var(r, r, &running_sum, NULL);
602 }
else if (n <= 20) {
604 }
else if (n <= 57) {
606 }
else if (n <= 136) {
608 }
else if (n <= 235) {
610 }
else if (n <= 1260) {
612 }
else if (n <= 4420) {
614 }
else if (n <= 7880) {
616 }
else if (n <= 16050) {
627 switch(bucket_window) {
637 case 10:
return 7880;
638 case 11:
return 16050;
647 secp256k1_scalar_split_lambda(s1, s2, &tmp);
648 secp256k1_ge_mul_lambda(p2, p1);
650 if (secp256k1_scalar_is_high(s1)) {
651 secp256k1_scalar_negate(s1, s1);
652 secp256k1_ge_neg(p1, p1);
654 if (secp256k1_scalar_is_high(s2)) {
655 secp256k1_scalar_negate(s2, s2);
656 secp256k1_ge_neg(p2, p2);
665 size_t entries = 2*n_points + 2;
671 const size_t scratch_checkpoint = secp256k1_scratch_checkpoint(error_callback, scratch);
675 size_t entries = 2*n_points + 2;
681 size_t point_idx = 0;
684 secp256k1_gej_set_infinity(r);
685 if (inp_g_sc == NULL && n_points == 0) {
694 points = (
secp256k1_ge *) secp256k1_scratch_alloc(error_callback, scratch, entries *
sizeof(*points));
695 scalars = (
secp256k1_scalar *) secp256k1_scratch_alloc(error_callback, scratch, entries *
sizeof(*scalars));
696 state_space = (
struct secp256k1_pippenger_state *) secp256k1_scratch_alloc(error_callback, scratch,
sizeof(*state_space));
697 if (points == NULL || scalars == NULL || state_space == NULL) {
698 secp256k1_scratch_apply_checkpoint(error_callback, scratch, scratch_checkpoint);
702 state_space->
wnaf_na = (
int *) secp256k1_scratch_alloc(error_callback, scratch, entries*(
WNAF_SIZE(bucket_window+1)) *
sizeof(
int));
703 buckets = (
secp256k1_gej *) secp256k1_scratch_alloc(error_callback, scratch, ((
size_t)1 << bucket_window) *
sizeof(*buckets));
704 if (state_space->
ps == NULL || state_space->
wnaf_na == NULL || buckets == NULL) {
705 secp256k1_scratch_apply_checkpoint(error_callback, scratch, scratch_checkpoint);
709 if (inp_g_sc != NULL) {
710 scalars[0] = *inp_g_sc;
711 points[0] = secp256k1_ge_const_g;
717 while (point_idx < n_points) {
718 if (!cb(&scalars[idx], &points[idx], point_idx + cb_offset, cbdata)) {
719 secp256k1_scratch_apply_checkpoint(error_callback, scratch, scratch_checkpoint);
729 secp256k1_scratch_apply_checkpoint(error_callback, scratch, scratch_checkpoint);
751 size_t space_for_points;
752 size_t space_overhead;
755 entry_size = 2*entry_size;
757 if (space_overhead > max_alloc) {
760 space_for_points = max_alloc - space_overhead;
762 n_points = space_for_points/entry_size;
763 n_points = n_points > max_points ? max_points : n_points;
764 if (n_points > res) {
767 if (n_points < max_points) {
783 secp256k1_gej_set_infinity(r);
784 secp256k1_gej_set_infinity(&tmpj);
787 for (point_idx = 0; point_idx < n_points; point_idx++) {
791 if (!cb(&scalar, &point, point_idx, cbdata)) {
795 secp256k1_gej_set_ge(&pointj, &point);
797 secp256k1_gej_add_var(r, r, &tmpj, NULL);
805 if (max_n_batch_points == 0) {
817 *n_batches =
CEIL_DIV(n, max_n_batch_points);
818 *n_batch_points =
CEIL_DIV(n, *n_batches);
828 size_t n_batch_points;
830 secp256k1_gej_set_infinity(r);
831 if (inp_g_sc == NULL && n == 0) {
837 if (scratch == NULL) {
856 for(i = 0; i < n_batches; i++) {
857 size_t nbp = n < n_batch_points ? n : n_batch_points;
858 size_t offset = n_batch_points*i;
860 if (!f(error_callback, scratch, &tmp, i == 0 ? inp_g_sc : NULL, cb, cbdata, nbp, offset)) {
863 secp256k1_gej_add_var(r, r, &tmp, NULL);
int secp256k1_ecmult_multi_callback(secp256k1_scalar *sc, secp256k1_ge *pt, size_t idx, void *data)
#define ECMULT_TABLE_SIZE(w)
The number of entries a table with precomputed multiples needs to have.
#define STRAUSS_SCRATCH_OBJECTS
static size_t secp256k1_pippenger_bucket_window_inv(int bucket_window)
Returns the maximum optimal number of points for a bucket_window.
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.
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)
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)
static size_t secp256k1_strauss_max_points(const secp256k1_callback *error_callback, secp256k1_scratch *scratch)
static void secp256k1_ecmult_odd_multiples_table(size_t n, secp256k1_ge *pre_a, secp256k1_fe *zr, secp256k1_fe *z, const secp256k1_gej *a)
Fill a table 'pre_a' with precomputed odd multiples of a.
static int secp256k1_wnaf_fixed(int *wnaf, const secp256k1_scalar *s, int w)
Convert a number to WNAF notation.
static SECP256K1_INLINE void secp256k1_ecmult_endo_split(secp256k1_scalar *s1, secp256k1_scalar *s2, secp256k1_ge *p1, secp256k1_ge *p2)
static int secp256k1_ecmult_wnaf(int *wnaf, int len, const secp256k1_scalar *a, int w)
Convert a number to WNAF notation.
static SECP256K1_INLINE void secp256k1_ecmult_table_get_ge_storage(secp256k1_ge *r, const secp256k1_ge_storage *pre, int n, int w)
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)
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)
static SECP256K1_INLINE void secp256k1_ecmult_table_get_ge_lambda(secp256k1_ge *r, const secp256k1_ge *pre, const secp256k1_fe *x, int n, int w)
static size_t secp256k1_strauss_scratch_size(size_t n_points)
#define ECMULT_PIPPENGER_THRESHOLD
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)
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...
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)
#define WNAF_BITS
Larger values for ECMULT_WINDOW_SIZE result in possibly better performance at the cost of an exponent...
static int secp256k1_ecmult_wnaf_small(int8_t *wnaf, int len, const secp256k1_scalar *a, int w)
#define ECMULT_MAX_POINTS_PER_BATCH
#define PIPPENGER_MAX_BUCKET_WINDOW
#define PIPPENGER_SCRATCH_OBJECTS
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)
static void secp256k1_ecmult(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_scalar *na, const secp256k1_scalar *ng)
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)
static SECP256K1_INLINE void secp256k1_ecmult_table_verify(int n, int w)
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)
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...
static SECP256K1_INLINE void secp256k1_ecmult_table_get_ge(secp256k1_ge *r, const secp256k1_ge *pre, int n, int w)
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)
#define secp256k1_fe_negate(r, a, m)
Negate a field element.
#define secp256k1_fe_set_int
static int sign(const secp256k1_context *ctx, struct signer_secrets *signer_secrets, struct signer *signer, const secp256k1_musig_keyagg_cache *cache, const unsigned char *msg32, unsigned char *sig64)
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)]
struct secp256k1_scratch_space_struct secp256k1_scratch
#define VERIFY_CHECK(cond)
This field implementation represents the value as 10 uint32_t limbs in base 2^26.
A group element in affine coordinates on the secp256k1 curve, or occasionally on an isomorphic curve ...
A group element of the secp256k1 curve, in jacobian coordinates.
struct secp256k1_pippenger_point_state * ps
A scalar modulo the group order of the secp256k1 curve.
struct secp256k1_strauss_point_state * ps