Skip to content

Commit e6f090d

Browse files
WIP: Eliminate the prej array from ecmult_strauss_wnaf.
1 parent f2d9aea commit e6f090d

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

src/ecmult_impl.h

+22-20
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
* contain prej[0].z / a.z. The other zr[i] values = prej[i].z / prej[i-1].z.
8383
* Prej's Z values are undefined, except for the last value.
8484
*/
85-
static void secp256k1_ecmult_odd_multiples_table(int n, secp256k1_gej *prej, secp256k1_fe *zr, const secp256k1_gej *a) {
85+
static void secp256k1_ecmult_odd_multiples_table(int n, secp256k1_ge *pre_a, secp256k1_fe *zr, secp256k1_gej *a) {
8686
secp256k1_gej d;
8787
secp256k1_ge a_ge, d_ge;
8888
int i;
@@ -100,21 +100,19 @@ static void secp256k1_ecmult_odd_multiples_table(int n, secp256k1_gej *prej, sec
100100
d_ge.infinity = 0;
101101

102102
secp256k1_ge_set_gej_zinv(&a_ge, a, &d.z);
103-
prej[0].x = a_ge.x;
104-
prej[0].y = a_ge.y;
105-
prej[0].z = a->z;
106-
prej[0].infinity = 0;
103+
pre_a[0].x = a->x = a_ge.x;
104+
pre_a[0].y = a->y = a_ge.y;
105+
pre_a[0].infinity = 0;
107106

108107
zr[0] = d.z;
109108
for (i = 1; i < n; i++) {
110-
secp256k1_gej_add_ge_var(&prej[i], &prej[i-1], &d_ge, &zr[i]);
109+
secp256k1_gej_add_ge_var(a, a, &d_ge, &zr[i]);
110+
pre_a[i].x = a->x;
111+
pre_a[i].y = a->y;
112+
pre_a[i].infinity = 0;
111113
}
112114

113-
/*
114-
* Each point in 'prej' has a z coordinate too small by a factor of 'd.z'. Only
115-
* the final point's z coordinate is actually used though, so just update that.
116-
*/
117-
secp256k1_fe_mul(&prej[n-1].z, &prej[n-1].z, &d.z);
115+
secp256k1_fe_mul(&a->z, &a->z, &d.z);
118116
}
119117

120118
/** Fill a table 'pre' with precomputed odd multiples of a.
@@ -133,13 +131,14 @@ static void secp256k1_ecmult_odd_multiples_table(int n, secp256k1_gej *prej, sec
133131
* happen once).
134132
*/
135133
static void secp256k1_ecmult_odd_multiples_table_globalz_windowa(secp256k1_ge *pre, secp256k1_fe *globalz, const secp256k1_gej *a) {
136-
secp256k1_gej prej[ECMULT_TABLE_SIZE(WINDOW_A)];
134+
secp256k1_gej tmp = *a;
137135
secp256k1_fe zr[ECMULT_TABLE_SIZE(WINDOW_A)];
138136

139137
/* Compute the odd multiples in Jacobian form. */
140-
secp256k1_ecmult_odd_multiples_table(ECMULT_TABLE_SIZE(WINDOW_A), prej, zr, a);
138+
secp256k1_ecmult_odd_multiples_table(ECMULT_TABLE_SIZE(WINDOW_A), pre, zr, &tmp);
141139
/* Bring them to the same Z denominator. */
142-
secp256k1_ge_globalz_set_table_gej(ECMULT_TABLE_SIZE(WINDOW_A), pre, globalz, prej, zr);
140+
*globalz = tmp.z;
141+
secp256k1_ge_globalz_set_table_gej(ECMULT_TABLE_SIZE(WINDOW_A), pre, pre, zr);
143142
}
144143

145144
static void secp256k1_ecmult_odd_multiples_table_storage_var(const int n, secp256k1_ge_storage *pre, const secp256k1_gej *a) {
@@ -491,18 +490,21 @@ static void secp256k1_ecmult_strauss_wnaf(const secp256k1_ecmult_context *ctx, c
491490
*/
492491
if (no > 0) {
493492
/* Compute the odd multiples in Jacobian form. */
494-
secp256k1_ecmult_odd_multiples_table(ECMULT_TABLE_SIZE(WINDOW_A), state->prej, state->zr, &a[state->ps[0].input_pos]);
493+
secp256k1_gej tmp = a[state->ps[0].input_pos];
494+
secp256k1_ecmult_odd_multiples_table(ECMULT_TABLE_SIZE(WINDOW_A), state->pre_a, state->zr, &tmp);
495495
for (np = 1; np < no; ++np) {
496-
secp256k1_gej tmp = a[state->ps[np].input_pos];
496+
secp256k1_fe lastz = tmp.z;
497+
tmp = a[state->ps[np].input_pos];
497498
#ifdef VERIFY
498-
secp256k1_fe_normalize_var(&(state->prej[(np - 1) * ECMULT_TABLE_SIZE(WINDOW_A) + ECMULT_TABLE_SIZE(WINDOW_A) - 1].z));
499+
secp256k1_fe_normalize_var(&lastz);
499500
#endif
500-
secp256k1_gej_rescale(&tmp, &(state->prej[(np - 1) * ECMULT_TABLE_SIZE(WINDOW_A) + ECMULT_TABLE_SIZE(WINDOW_A) - 1].z));
501-
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);
501+
secp256k1_gej_rescale(&tmp, &lastz);
502+
secp256k1_ecmult_odd_multiples_table(ECMULT_TABLE_SIZE(WINDOW_A), state->pre_a + np * ECMULT_TABLE_SIZE(WINDOW_A), state->zr + np * ECMULT_TABLE_SIZE(WINDOW_A), &tmp);
502503
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));
503504
}
504505
/* Bring them to the same Z denominator. */
505-
secp256k1_ge_globalz_set_table_gej(ECMULT_TABLE_SIZE(WINDOW_A) * no, state->pre_a, &Z, state->prej, state->zr);
506+
Z = tmp.z;
507+
secp256k1_ge_globalz_set_table_gej(ECMULT_TABLE_SIZE(WINDOW_A) * no, state->pre_a, state->pre_a, state->zr);
506508
} else {
507509
secp256k1_fe_set_int(&Z, 1);
508510
}

src/group.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a
7373
* that mul(a[i].z, zr[i+1]) == a[i+1].z. zr[0] is ignored. The x and y
7474
* coordinates of the result are stored in r, the common z coordinate is
7575
* stored in globalz. */
76-
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);
76+
static void secp256k1_ge_globalz_set_table_gej(size_t len, secp256k1_ge *r, const secp256k1_ge *a, const secp256k1_fe *zr);
7777

7878
/** Set a group element (affine) equal to the point at infinity. */
7979
static void secp256k1_ge_set_infinity(secp256k1_ge *r);

src/group_impl.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ static void secp256k1_ge_set_all_gej_var(secp256k1_ge *r, const secp256k1_gej *a
156156
}
157157
}
158158

159-
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) {
159+
static void secp256k1_ge_globalz_set_table_gej(size_t len, secp256k1_ge *r, const secp256k1_ge *a, const secp256k1_fe *zr) {
160160
size_t i = len - 1;
161161
secp256k1_fe zs;
162162

@@ -166,17 +166,20 @@ static void secp256k1_ge_globalz_set_table_gej(size_t len, secp256k1_ge *r, secp
166166
r[i].y = a[i].y;
167167
/* Ensure all y values are in weak normal form for fast negation of points */
168168
secp256k1_fe_normalize_weak(&r[i].y);
169-
*globalz = a[i].z;
170169
r[i].infinity = 0;
171170
zs = zr[i];
172171

173172
/* Work our way backwards, using the z-ratios to scale the x/y values. */
174173
while (i > 0) {
174+
secp256k1_gej tmpa;
175175
if (i != len - 1) {
176176
secp256k1_fe_mul(&zs, &zs, &zr[i]);
177177
}
178178
i--;
179-
secp256k1_ge_set_gej_zinv(&r[i], &a[i], &zs);
179+
tmpa.x = a[i].x;
180+
tmpa.y = a[i].y;
181+
tmpa.infinity = 0;
182+
secp256k1_ge_set_gej_zinv(&r[i], &tmpa, &zs);
180183
}
181184
}
182185
}

0 commit comments

Comments
 (0)