Skip to content

Commit 6dbb007

Browse files
committed
Increase sparsity of pippenger fixed window naf representation
1 parent cd329db commit 6dbb007

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/ecmult_impl.h

+13
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,19 @@ static int secp256k1_wnaf_fixed(int *wnaf, const secp256k1_scalar *s, int w) {
607607
} else {
608608
wnaf[pos] = (val + sign) ^ sign;
609609
}
610+
/* Set a coefficient to zero if it is 1 or -1 and the proceeding digit
611+
* is strictly negative or strictly positive respectively. Only change
612+
* coefficients at previous positions because above code assumes that
613+
* wnaf[pos - 1] is odd.
614+
*/
615+
if (pos >= 2 && ((wnaf[pos - 1] == 1 && wnaf[pos - 2] < 0) || (wnaf[pos - 1] == -1 && wnaf[pos - 2] > 0))) {
616+
if (wnaf[pos - 1] == 1) {
617+
wnaf[pos - 2] += 1 << w;
618+
} else {
619+
wnaf[pos - 2] -= 1 << w;
620+
}
621+
wnaf[pos - 1] = 0;
622+
}
610623
++pos;
611624
}
612625
VERIFY_CHECK(pos == WNAF_SIZE(w));

src/tests.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -3022,8 +3022,7 @@ void test_fixed_wnaf(const secp256k1_scalar *number, int w) {
30223022
for (i = WNAF_SIZE(w)-1; i >= 0; --i) {
30233023
secp256k1_scalar t;
30243024
int v = wnaf[i];
3025-
CHECK(v != 0); /* check nonzero */
3026-
CHECK(v & 1); /* check parity */
3025+
CHECK(v == 0 || v & 1); /* check parity */
30273026
CHECK(v > -(1 << w)); /* check range above */
30283027
CHECK(v < (1 << w)); /* check range below */
30293028

0 commit comments

Comments
 (0)