7 #ifndef SECP256K1_ECMULT_IMPL_H 8 #define SECP256K1_ECMULT_IMPL_H 17 #if defined(EXHAUSTIVE_TEST_ORDER) 21 # if EXHAUSTIVE_TEST_ORDER > 128 24 # elif EXHAUSTIVE_TEST_ORDER > 8 36 #ifdef USE_ENDOMORPHISM 45 #ifdef USE_ENDOMORPHISM 50 #define WNAF_SIZE_BITS(bits, w) (((bits) + (w) - 1) / (w)) 51 #define WNAF_SIZE(w) WNAF_SIZE_BITS(WNAF_BITS, w) 54 #define ECMULT_TABLE_SIZE(w) (1 << ((w)-2)) 57 #define PIPPENGER_SCRATCH_OBJECTS 6 58 #define STRAUSS_SCRATCH_OBJECTS 6 60 #define PIPPENGER_MAX_BUCKET_WINDOW 12 63 #ifdef USE_ENDOMORPHISM 64 #define ECMULT_PIPPENGER_THRESHOLD 88 66 #define ECMULT_PIPPENGER_THRESHOLD 160 69 #ifdef USE_ENDOMORPHISM 70 #define ECMULT_MAX_POINTS_PER_BATCH 5000000 72 #define ECMULT_MAX_POINTS_PER_BATCH 10000000 104 for (i = 1; i < n; i++) {
170 for (i = 0; i < (n - 1); i++) {
273 #define ECMULT_TABLE_GET_GE(r,pre,n,w) do { \ 274 VERIFY_CHECK(((n) & 1) == 1); \ 275 VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \ 276 VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \ 278 *(r) = (pre)[((n)-1)/2]; \ 280 *(r) = (pre)[(-(n)-1)/2]; \ 281 secp256k1_fe_negate(&((r)->y), &((r)->y), 1); \ 285 #define ECMULT_TABLE_GET_GE_STORAGE(r,pre,n,w) do { \ 286 VERIFY_CHECK(((n) & 1) == 1); \ 287 VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \ 288 VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \ 290 secp256k1_ge_from_storage((r), &(pre)[((n)-1)/2]); \ 292 secp256k1_ge_from_storage((r), &(pre)[(-(n)-1)/2]); \ 293 secp256k1_fe_negate(&((r)->y), &((r)->y), 1); \ 299 #ifdef USE_ENDOMORPHISM 300 ctx->pre_g_128 = NULL;
307 if (
ctx->pre_g != NULL) {
319 #ifdef USE_ENDOMORPHISM 328 for (i = 0; i < 128; i++) {
338 if (src->
pre_g == NULL) {
345 #ifdef USE_ENDOMORPHISM 346 if (src->pre_g_128 == NULL) {
347 dst->pre_g_128 = NULL;
351 memcpy(dst->pre_g_128, src->pre_g_128, size);
357 return ctx->pre_g != NULL;
362 #ifdef USE_ENDOMORPHISM 363 free(
ctx->pre_g_128);
377 int last_set_bit = -1;
387 memset(wnaf, 0, len *
sizeof(wnaf[0]));
403 if (now > len - bit) {
409 carry = (word >> (w-1)) & 1;
412 wnaf[bit] =
sign * word;
423 return last_set_bit + 1;
427 #ifdef USE_ENDOMORPHISM 430 int wnaf_na_lam[130];
444 #ifdef USE_ENDOMORPHISM 453 #ifdef USE_ENDOMORPHISM 458 int wnaf_ng_128[129];
469 for (np = 0; np < num; ++np) {
474 #ifdef USE_ENDOMORPHISM 476 secp256k1_scalar_split_lambda(&state->
ps[no].na_1, &state->
ps[no].na_lam, &na[np]);
483 if (state->
ps[no].bits_na_1 > bits) {
484 bits = state->
ps[no].bits_na_1;
486 if (state->
ps[no].bits_na_lam > bits) {
487 bits = state->
ps[no].bits_na_lam;
512 for (np = 1; np < no; ++np) {
527 #ifdef USE_ENDOMORPHISM 528 for (np = 0; np < no; ++np) {
541 if (bits_ng_1 > bits) {
544 if (bits_ng_128 > bits) {
551 if (bits_ng > bits) {
559 for (i = bits - 1; i >= 0; i--) {
562 #ifdef USE_ENDOMORPHISM 563 for (np = 0; np < no; ++np) {
564 if (i < state->ps[np].bits_na_1 && (n = state->
ps[np].wnaf_na_1[i])) {
568 if (i < state->ps[np].bits_na_lam && (n = state->
ps[np].wnaf_na_lam[i])) {
573 if (i < bits_ng_1 && (n = wnaf_ng_1[i])) {
577 if (i < bits_ng_128 && (n = wnaf_ng_128[i])) {
582 for (np = 0; np < no; ++np) {
588 if (i < bits_ng && (n = wnaf_ng[i])) {
605 #ifdef USE_ENDOMORPHISM 613 #ifdef USE_ENDOMORPHISM 614 state.pre_a_lam = pre_a_lam;
621 #ifdef USE_ENDOMORPHISM 626 return n_points*point_size;
636 if (inp_g_sc == NULL && n_points == 0) {
647 #ifdef USE_ENDOMORPHISM 655 for (i = 0; i < n_points; i++) {
657 if (!cb(&scalars[i], &point, i+cb_offset, cbdata)) {
692 for (pos = 0; pos <
WNAF_SIZE(w); pos++) {
709 for (pos =
WNAF_SIZE(w) - 1; pos > 0; pos--) {
719 while (pos <= max_pos) {
721 if ((val & 1) == 0) {
722 wnaf[pos - 1] -= (1 << w);
723 wnaf[pos] = (val + 1);
732 if (pos >= 2 && ((wnaf[pos - 1] == 1 && wnaf[pos - 2] < 0) || (wnaf[pos - 1] == -1 && wnaf[pos - 2] > 0))) {
733 if (wnaf[pos - 1] == 1) {
734 wnaf[pos - 2] += 1 << w;
736 wnaf[pos - 2] -= 1 << w;
764 size_t n_wnaf =
WNAF_SIZE(bucket_window+1);
770 for (np = 0; np < num; ++np) {
784 for (i = n_wnaf - 1; i >= 0; i--) {
791 for (np = 0; np < no; ++np) {
792 int n = state->
wnaf_na[np*n_wnaf + i];
799 int skew = point_state.
skew_na;
815 for(j = 0; j < bucket_window; j++) {
845 #ifdef USE_ENDOMORPHISM 850 }
else if (n <= 20) {
852 }
else if (n <= 57) {
854 }
else if (n <= 136) {
856 }
else if (n <= 235) {
858 }
else if (n <= 1260) {
860 }
else if (n <= 4420) {
862 }
else if (n <= 7880) {
864 }
else if (n <= 16050) {
872 }
else if (n <= 11) {
874 }
else if (n <= 45) {
876 }
else if (n <= 100) {
878 }
else if (n <= 275) {
880 }
else if (n <= 625) {
882 }
else if (n <= 1850) {
884 }
else if (n <= 3400) {
886 }
else if (n <= 9630) {
888 }
else if (n <= 17900) {
890 }
else if (n <= 32800) {
902 switch(bucket_window) {
903 #ifdef USE_ENDOMORPHISM 913 case 10:
return 7880;
914 case 11:
return 16050;
926 case 10:
return 17900;
927 case 11:
return 32800;
935 #ifdef USE_ENDOMORPHISM 938 secp256k1_scalar_split_lambda(s1, s2, &tmp);
939 secp256k1_ge_mul_lambda(p2, p1);
957 #ifdef USE_ENDOMORPHISM 958 size_t entries = 2*n_points + 2;
960 size_t entries = n_points + 1;
970 #ifdef USE_ENDOMORPHISM 971 size_t entries = 2*n_points + 2;
973 size_t entries = n_points + 1;
980 size_t point_idx = 0;
986 if (inp_g_sc == NULL && n_points == 0) {
1001 if (inp_g_sc != NULL) {
1002 scalars[0] = *inp_g_sc;
1005 #ifdef USE_ENDOMORPHISM 1006 secp256k1_ecmult_endo_split(&scalars[0], &scalars[1], &points[0], &points[1]);
1011 while (point_idx < n_points) {
1012 if (!cb(&scalars[idx], &points[idx], point_idx + cb_offset, cbdata)) {
1017 #ifdef USE_ENDOMORPHISM 1018 secp256k1_ecmult_endo_split(&scalars[idx - 1], &scalars[idx], &points[idx - 1], &points[idx]);
1027 for(i = 0; (size_t)i < idx; i++) {
1030 for(j = 0; j <
WNAF_SIZE(bucket_window+1); j++) {
1034 for(i = 0; i < 1<<bucket_window; i++) {
1059 size_t space_for_points;
1060 size_t space_overhead;
1063 #ifdef USE_ENDOMORPHISM 1064 entry_size = 2*entry_size;
1067 if (space_overhead > max_alloc) {
1070 space_for_points = max_alloc - space_overhead;
1072 n_points = space_for_points/entry_size;
1073 n_points = n_points > max_points ? max_points : n_points;
1074 if (n_points > res) {
1077 if (n_points < max_points) {
1099 for (point_idx = 0; point_idx < n_points; point_idx++) {
1103 if (!cb(&scalar, &point, point_idx, cbdata)) {
1117 if (max_n_batch_points == 0) {
1125 *n_batch_points = 0;
1129 *n_batches = 1 + (n - 1) / max_n_batch_points;
1130 *n_batch_points = 1 + (n - 1) / *n_batches;
1140 size_t n_batch_points;
1143 if (inp_g_sc == NULL && n == 0) {
1145 }
else if (n == 0) {
1151 if (scratch == NULL) {
1168 for(i = 0; i < n_batches; i++) {
1169 size_t nbp = n < n_batch_points ? n : n_batch_points;
1170 size_t offset = n_batch_points*i;
1172 if (!
f(
ctx, scratch, &tmp, i == 0 ? inp_g_sc : NULL, cb, cbdata, nbp, offset)) {
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)
static int secp256k1_ge_is_infinity(const secp256k1_ge *a)
#define VERIFY_CHECK(cond)
Definition: util.h:67
static int secp256k1_ecmult_context_is_built(const secp256k1_ecmult_context *ctx)
Definition: ecmult_impl.h:356
static void secp256k1_ecmult_context_clone(secp256k1_ecmult_context *dst, const secp256k1_ecmult_context *src, const secp256k1_callback *cb)
Definition: ecmult_impl.h:336
static int secp256k1_gej_is_infinity(const secp256k1_gej *a)
struct secp256k1_strauss_point_state * ps
Definition: ecmult_impl.h:447
Definition: field_10x26.h:12
static void secp256k1_gej_add_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_gej *b, secp256k1_fe *rzr)
static int secp256k1_scalar_is_even(const secp256k1_scalar *a)
int bits_na
Definition: ecmult_impl.h:435
static void secp256k1_ecmult_context_clear(secp256k1_ecmult_context *ctx)
Definition: ecmult_impl.h:360
static size_t secp256k1_pippenger_scratch_size(size_t n_points, int bucket_window)
Definition: ecmult_impl.h:956
static void secp256k1_ecmult_context_init(secp256k1_ecmult_context *ctx)
Definition: ecmult_impl.h:297
static size_t secp256k1_pippenger_max_points(secp256k1_scratch *scratch)
Definition: ecmult_impl.h:1051
#define WNAF_BITS
Definition: ecmult_impl.h:48
static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a)
static void secp256k1_fe_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe *SECP256K1_RESTRICT b)
static void secp256k1_fe_normalize_var(secp256k1_fe *r)
secp256k1_fe x
Definition: group.h:25
int wnaf_na[256]
Definition: ecmult_impl.h:434
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:763
Definition: ecmult_impl.h:751
static void secp256k1_fe_negate(secp256k1_fe *r, const secp256k1_fe *a, int m)
secp256k1_fe * zr
Definition: ecmult_impl.h:442
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)
Definition: ecmult_impl.h:1042
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)
Definition: ecmult_impl.h:629
static unsigned int secp256k1_scalar_get_bits(const secp256k1_scalar *a, unsigned int offset, unsigned int count)
static void secp256k1_scalar_negate(secp256k1_scalar *r, const secp256k1_scalar *a)
#define PIPPENGER_MAX_BUCKET_WINDOW
Definition: ecmult_impl.h:60
static int secp256k1_scalar_is_zero(const secp256k1_scalar *a)
#define ECMULT_TABLE_GET_GE_STORAGE(r, pre, n, w)
Definition: ecmult_impl.h:285
#define ECMULT_TABLE_SIZE(w)
Definition: ecmult_impl.h:54
static void secp256k1_gej_add_zinv_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, const secp256k1_fe *bzinv)
static void secp256k1_fe_set_int(secp256k1_fe *r, int a)
static void secp256k1_fe_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a)
static void secp256k1_ecmult_odd_multiples_table_storage_var(const int n, secp256k1_ge_storage *pre, const secp256k1_gej *a)
Definition: ecmult_impl.h:140
static void secp256k1_gej_set_infinity(secp256k1_gej *r)
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)
Definition: ecmult_impl.h:1089
static void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a)
static void secp256k1_gej_add_ge_var(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b, secp256k1_fe *rzr)
Definition: ecmult_impl.h:746
size_t input_pos
Definition: ecmult_impl.h:437
static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, secp256k1_fe *rzr)
f
Definition: linearize-data.py:263
static const secp256k1_ge secp256k1_ge_const_g
Definition: group_impl.h:64
#define WINDOW_G
Definition: ecmult_impl.h:41
static void secp256k1_scratch_deallocate_frame(secp256k1_scratch *scratch)
#define SECP256K1_INLINE
Definition: secp256k1.h:123
static int secp256k1_scratch_allocate_frame(secp256k1_scratch *scratch, size_t n, size_t objects)
static void secp256k1_ecmult_odd_multiples_table_globalz_windowa(secp256k1_ge *pre, secp256k1_fe *globalz, const secp256k1_gej *a)
Definition: ecmult_impl.h:130
Definition: ecmult_impl.h:426
#define PIPPENGER_SCRATCH_OBJECTS
Definition: ecmult_impl.h:57
static size_t secp256k1_pippenger_bucket_window_inv(int bucket_window)
Definition: ecmult_impl.h:901
#define ECMULT_PIPPENGER_THRESHOLD
Definition: ecmult_impl.h:66
static secp256k1_context * ctx
Definition: tests.c:46
int infinity
Definition: group.h:28
static void secp256k1_gej_clear(secp256k1_gej *r)
static int secp256k1_scalar_is_high(const secp256k1_scalar *a)
int * wnaf_na
Definition: ecmult_impl.h:752
static int secp256k1_pippenger_bucket_window(size_t n)
Definition: ecmult_impl.h:844
static size_t secp256k1_scratch_max_allocation(const secp256k1_scratch *scratch, size_t n_objects)
static int secp256k1_wnaf_fixed(int *wnaf, const secp256k1_scalar *s, int w)
Definition: ecmult_impl.h:684
#define ECMULT_MAX_POINTS_PER_BATCH
Definition: ecmult_impl.h:72
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)
Definition: ecmult_impl.h:1135
static void secp256k1_scalar_clear(secp256k1_scalar *r)
secp256k1_fe x
Definition: group.h:15
static size_t secp256k1_strauss_scratch_size(size_t n_points)
Definition: ecmult_impl.h:620
#define CHECK(cond)
Definition: util.h:52
static void * secp256k1_scratch_alloc(secp256k1_scratch *scratch, size_t n)
Definition: scalar_4x64.h:13
int infinity
Definition: group.h:17
#define WINDOW_A
Definition: ecmult_impl.h:33
#define STRAUSS_SCRATCH_OBJECTS
Definition: ecmult_impl.h:58
static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a)
static void secp256k1_scalar_split_128(secp256k1_scalar *r1, secp256k1_scalar *r2, const secp256k1_scalar *a)
Definition: scalar_low_impl.h:105
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)
Definition: ecmult_impl.h:450
size_t input_pos
Definition: ecmult_impl.h:748
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)
Definition: ecmult_impl.h:669
secp256k1_ge * pre_a
Definition: ecmult_impl.h:443
static void secp256k1_gej_rescale(secp256k1_gej *r, const secp256k1_fe *b)
#define ECMULT_TABLE_GET_GE(r, pre, n, w)
Definition: ecmult_impl.h:273
static void secp256k1_scalar_set_int(secp256k1_scalar *r, unsigned int v)
static void secp256k1_ge_set_gej_zinv(secp256k1_ge *r, const secp256k1_gej *a, const secp256k1_fe *zi)
Definition: group_impl.h:74
secp256k1_fe z
Definition: group.h:27
static void secp256k1_ecmult_odd_multiples_table(int n, secp256k1_gej *prej, secp256k1_fe *zr, const secp256k1_gej *a)
Definition: ecmult_impl.h:80
void * memcpy(void *a, const void *b, size_t c)
Definition: glibc_compat.cpp:18
#define WNAF_SIZE(w)
Definition: ecmult_impl.h:51
struct secp256k1_pippenger_point_state * ps
Definition: ecmult_impl.h:753
static unsigned int secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count)
secp256k1_gej * prej
Definition: ecmult_impl.h:441
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)
Definition: ecmult_impl.h:966
int() secp256k1_ecmult_multi_callback(secp256k1_scalar *sc, secp256k1_ge *pt, size_t idx, void *data)
Definition: ecmult.h:33
static void secp256k1_ge_from_storage(secp256k1_ge *r, const secp256k1_ge_storage *a)
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a)
Definition: ecmult_impl.h:440
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:1116
secp256k1_fe y
Definition: group.h:26
static int secp256k1_ecmult_wnaf(int *wnaf, int len, const secp256k1_scalar *a, int w)
Definition: ecmult_impl.h:375
static void secp256k1_ecmult(const secp256k1_ecmult_context *ctx, secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_scalar *na, const secp256k1_scalar *ng)
Definition: ecmult_impl.h:600
static size_t secp256k1_strauss_max_points(secp256k1_scratch *scratch)
Definition: ecmult_impl.h:673
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:1134
secp256k1_fe y
Definition: group.h:16
int skew_na
Definition: ecmult_impl.h:747
static void secp256k1_fe_inv_var(secp256k1_fe *r, const secp256k1_fe *a)
static void secp256k1_ge_to_storage(secp256k1_ge_storage *r, const secp256k1_ge *a)
static SECP256K1_INLINE void * checked_malloc(const secp256k1_callback *cb, size_t size)
Definition: util.h:71
static void secp256k1_ecmult_context_build(secp256k1_ecmult_context *ctx, const secp256k1_callback *cb)
Definition: ecmult_impl.h:304
secp256k1_ge_storage(* pre_g)[]
Definition: ecmult.h:17
def sign()
Definition: gitian-build.py:91