Skip to content

Commit 665775b

Browse files
committed
Don't split the g factor when not using endomorphism
1 parent 9431d6b commit 665775b

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/ecmult_impl.h

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
/** larger numbers may result in slightly better performance, at the cost of
1818
exponentially larger precomputed tables. WINDOW_G == 14 results in 640 KiB. */
19+
#ifdef USE_ENDOMORPHISM
1920
#define WINDOW_G 14
21+
#else
22+
#define WINDOW_G 15
23+
#endif
2024

2125
/** Fill a table 'pre' with precomputed odd multiples of a. W determines the size of the table.
2226
* pre will contains the values [1*a,3*a,5*a,...,(2^(w-1)-1)*a], so it needs place for
@@ -69,7 +73,9 @@ static void secp256k1_ecmult_table_precomp_ge_var(secp256k1_ge_t *pre, const sec
6973
typedef struct {
7074
/* For accelerating the computation of a*P + b*G: */
7175
secp256k1_ge_t pre_g[ECMULT_TABLE_SIZE(WINDOW_G)]; /* odd multiples of the generator */
76+
#ifdef USE_ENDOMORPHISM
7277
secp256k1_ge_t pre_g_128[ECMULT_TABLE_SIZE(WINDOW_G)]; /* odd multiples of 2^128*generator */
78+
#endif
7379
} secp256k1_ecmult_consts_t;
7480

7581
static const secp256k1_ecmult_consts_t *secp256k1_ecmult_consts = NULL;
@@ -85,14 +91,18 @@ static void secp256k1_ecmult_start(void) {
8591
const secp256k1_ge_t *g = &secp256k1_ge_consts->g;
8692
secp256k1_gej_t gj; secp256k1_gej_set_ge(&gj, g);
8793

94+
#ifdef USE_ENDOMORPHISM
8895
/* calculate 2^128*generator */
8996
secp256k1_gej_t g_128j = gj;
9097
for (int i=0; i<128; i++)
9198
secp256k1_gej_double_var(&g_128j, &g_128j);
99+
#endif
92100

93101
/* precompute the tables with odd multiples */
94102
secp256k1_ecmult_table_precomp_ge_var(ret->pre_g, &gj, WINDOW_G);
103+
#ifdef USE_ENDOMORPHISM
95104
secp256k1_ecmult_table_precomp_ge_var(ret->pre_g_128, &g_128j, WINDOW_G);
105+
#endif
96106

97107
/* Set the global pointer to the precomputation table. */
98108
secp256k1_ecmult_consts = ret;
@@ -172,7 +182,6 @@ static void secp256k1_ecmult(secp256k1_gej_t *r, const secp256k1_gej_t *a, const
172182
secp256k1_gej_t pre_a_lam[ECMULT_TABLE_SIZE(WINDOW_A)];
173183
for (int i=0; i<ECMULT_TABLE_SIZE(WINDOW_A); i++)
174184
secp256k1_gej_mul_lambda(&pre_a_lam[i], &pre_a[i]);
175-
#endif
176185

177186
/* Splitted G factors. */
178187
secp256k1_num_t ng_1, ng_128;
@@ -185,6 +194,10 @@ static void secp256k1_ecmult(secp256k1_gej_t *r, const secp256k1_gej_t *a, const
185194
int wnaf_ng_128[129]; int bits_ng_128 = secp256k1_ecmult_wnaf(wnaf_ng_128, &ng_128, WINDOW_G);
186195
if (bits_ng_1 > bits) bits = bits_ng_1;
187196
if (bits_ng_128 > bits) bits = bits_ng_128;
197+
#else
198+
int wnaf_ng[257]; int bits_ng = secp256k1_ecmult_wnaf(wnaf_ng, ng, WINDOW_G);
199+
if (bits_ng > bits) bits = bits_ng;
200+
#endif
188201

189202
secp256k1_gej_set_infinity(r);
190203
secp256k1_gej_t tmpj;
@@ -202,12 +215,6 @@ static void secp256k1_ecmult(secp256k1_gej_t *r, const secp256k1_gej_t *a, const
202215
ECMULT_TABLE_GET_GEJ(&tmpj, pre_a_lam, n, WINDOW_A);
203216
secp256k1_gej_add_var(r, r, &tmpj);
204217
}
205-
#else
206-
if (i < bits_na && (n = wnaf_na[i])) {
207-
ECMULT_TABLE_GET_GEJ(&tmpj, pre_a, n, WINDOW_A);
208-
secp256k1_gej_add_var(r, r, &tmpj);
209-
}
210-
#endif
211218
if (i < bits_ng_1 && (n = wnaf_ng_1[i])) {
212219
ECMULT_TABLE_GET_GE(&tmpa, c->pre_g, n, WINDOW_G);
213220
secp256k1_gej_add_ge_var(r, r, &tmpa);
@@ -216,6 +223,16 @@ static void secp256k1_ecmult(secp256k1_gej_t *r, const secp256k1_gej_t *a, const
216223
ECMULT_TABLE_GET_GE(&tmpa, c->pre_g_128, n, WINDOW_G);
217224
secp256k1_gej_add_ge_var(r, r, &tmpa);
218225
}
226+
#else
227+
if (i < bits_na && (n = wnaf_na[i])) {
228+
ECMULT_TABLE_GET_GEJ(&tmpj, pre_a, n, WINDOW_A);
229+
secp256k1_gej_add_var(r, r, &tmpj);
230+
}
231+
if (i < bits_ng && (n = wnaf_ng[i])) {
232+
ECMULT_TABLE_GET_GE(&tmpa, c->pre_g, n, WINDOW_G);
233+
secp256k1_gej_add_ge_var(r, r, &tmpa);
234+
}
235+
#endif
219236
}
220237
}
221238

0 commit comments

Comments
 (0)