Skip to content

Commit b6c2a3c

Browse files
Merge #1761: ecmult_multi: reduce strauss memory usage by 30%
26166c4 ecmult_multi: reduce strauss memory usage by 30% (Jonas Nick) Pull request description: This is a draft because I'm not sure about the cleanest way to implement it. ACKs for top commit: real-or-random: ACK 26166c4 benchmarks show no significant difference (only tried low point counts) siv2r: tACK 26166c4 hebasto: ACK 26166c4, I have reviewed the code and it looks OK. Tree-SHA512: f289daee0b0b51451331eefdd99200a78bd83539365d38465c038dc0e6ad940daf821119f7161b08a2390cf046e3859a8f950f2fe881a427aba16353031def7d
2 parents c8206b1 + 26166c4 commit b6c2a3c

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/ecmult_impl.h

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,24 @@ static int secp256k1_ecmult_wnaf(int *wnaf, int len, const secp256k1_scalar *a,
220220
return last_set_bit + 1;
221221
}
222222

223+
/* Same as secp256k1_ecmult_wnaf, but stores to int8_t array. Requires w <= 8. */
224+
static int secp256k1_ecmult_wnaf_small(int8_t *wnaf, int len, const secp256k1_scalar *a, int w) {
225+
int wnaf_tmp[256];
226+
int ret, i;
227+
228+
VERIFY_CHECK(2 <= w && w <= 8);
229+
ret = secp256k1_ecmult_wnaf(wnaf_tmp, len, a, w);
230+
231+
for (i = 0; i < len; i++) {
232+
wnaf[i] = (int8_t)wnaf_tmp[i];
233+
}
234+
235+
return ret;
236+
}
237+
223238
struct secp256k1_strauss_point_state {
224-
int wnaf_na_1[129];
225-
int wnaf_na_lam[129];
239+
int8_t wnaf_na_1[129];
240+
int8_t wnaf_na_lam[129];
226241
int bits_na_1;
227242
int bits_na_lam;
228243
};
@@ -259,8 +274,8 @@ static void secp256k1_ecmult_strauss_wnaf(const struct secp256k1_strauss_state *
259274
secp256k1_scalar_split_lambda(&na_1, &na_lam, &na[np]);
260275

261276
/* build wnaf representation for na_1 and na_lam. */
262-
state->ps[no].bits_na_1 = secp256k1_ecmult_wnaf(state->ps[no].wnaf_na_1, 129, &na_1, WINDOW_A);
263-
state->ps[no].bits_na_lam = secp256k1_ecmult_wnaf(state->ps[no].wnaf_na_lam, 129, &na_lam, WINDOW_A);
277+
state->ps[no].bits_na_1 = secp256k1_ecmult_wnaf_small(state->ps[no].wnaf_na_1, 129, &na_1, WINDOW_A);
278+
state->ps[no].bits_na_lam = secp256k1_ecmult_wnaf_small(state->ps[no].wnaf_na_lam, 129, &na_lam, WINDOW_A);
264279
VERIFY_CHECK(state->ps[no].bits_na_1 <= 129);
265280
VERIFY_CHECK(state->ps[no].bits_na_lam <= 129);
266281
if (state->ps[no].bits_na_1 > bits) {

0 commit comments

Comments
 (0)