ecmult_multi: reduce strauss memory usage by 30%
What changed, and why it matters
This commit is a straightforward memory optimization for a cryptographic calculation routine. It reduces the amount of temporary memory used during batch elliptic-curve multiplications by storing intermediate values in smaller integer types. There is no indication of a security bug being fixed.
No security action required. Treat as a normal performance/memory optimization patch.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces secp256k1_ecmult_wnaf_small, a wrapper around the existing WNAF (windowed non-adjacent form) generator that writes results into int8_t arrays instead of int arrays. The secp256k1_strauss_point_state structure then changes its wnaf_na_1 and wnaf_na_lam fields from int[129] to int8_t[129], and the Strauss batch multiplication code uses the new wrapper. The WNAF values are bounded by 2^(w-1) where w <= 8 (enforced by VERIFY_CHECK), so they fit safely in a signed 8-bit integer. This is a pure optimization/refactoring change.
Changed components
src/ecmult_impl.hsecp256k1_ecmult_strauss_wnafsecp256k1_strauss_point_stateInspect captured patch +19 / −4
diff --git a/src/ecmult_impl.h b/src/ecmult_impl.h
index 0b53b3f..29f02f5 100644
--- a/src/ecmult_impl.h
+++ b/src/ecmult_impl.h
@@ -220,9 +220,24 @@ static int secp256k1_ecmult_wnaf(int *wnaf, int len, const secp256k1_scalar *a,
return last_set_bit + 1;
}
+/* Same as secp256k1_ecmult_wnaf, but stores to int8_t array. Requires w <= 8. */
+static int secp256k1_ecmult_wnaf_small(int8_t *wnaf, int len, const secp256k1_scalar *a, int w) {
+ int wnaf_tmp[256];
+ int ret, i;
+
+ VERIFY_CHECK(2 <= w && w <= 8);
+ ret = secp256k1_ecmult_wnaf(wnaf_tmp, len, a, w);
+
+ for (i = 0; i < len; i++) {
+ wnaf[i] = (int8_t)wnaf_tmp[i];
+ }
+
+ return ret;
+}
+
struct secp256k1_strauss_point_state {
- int wnaf_na_1[129];
- int wnaf_na_lam[129];
+ int8_t wnaf_na_1[129];
+ int8_t wnaf_na_lam[129];
int bits_na_1;
int bits_na_lam;
};
@@ -259,8 +274,8 @@ static void secp256k1_ecmult_strauss_wnaf(const struct secp256k1_strauss_state *
secp256k1_scalar_split_lambda(&na_1, &na_lam, &na[np]);
/* build wnaf representation for na_1 and na_lam. */
- state->ps[no].bits_na_1 = secp256k1_ecmult_wnaf(state->ps[no].wnaf_na_1, 129, &na_1, WINDOW_A);
- state->ps[no].bits_na_lam = secp256k1_ecmult_wnaf(state->ps[no].wnaf_na_lam, 129, &na_lam, WINDOW_A);
+ state->ps[no].bits_na_1 = secp256k1_ecmult_wnaf_small(state->ps[no].wnaf_na_1, 129, &na_1, WINDOW_A);
+ state->ps[no].bits_na_lam = secp256k1_ecmult_wnaf_small(state->ps[no].wnaf_na_lam, 129, &na_lam, WINDOW_A);
VERIFY_CHECK(state->ps[no].bits_na_1 <= 129);
VERIFY_CHECK(state->ps[no].bits_na_lam <= 129);
if (state->ps[no].bits_na_1 > bits) {
Why this scored 12/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.