Remove unnecessary sign variable from wnaf_const

This commit is contained in:
Jonas Nick 2020-04-17 18:06:47 +00:00
parent 6bb0b77e15
commit 37dba329c6

View File

@ -101,16 +101,22 @@ static int secp256k1_wnaf_const(int *wnaf, const secp256k1_scalar *scalar, int w
/* 4 */ /* 4 */
u_last = secp256k1_scalar_shr_int(&s, w); u_last = secp256k1_scalar_shr_int(&s, w);
do { do {
int sign;
int even; int even;
/* 4.1 4.4 */ /* 4.1 4.4 */
u = secp256k1_scalar_shr_int(&s, w); u = secp256k1_scalar_shr_int(&s, w);
/* 4.2 */ /* 4.2 */
even = ((u & 1) == 0); even = ((u & 1) == 0);
sign = 2 * (u_last > 0) - 1; /* In contrast to the original algorithm, u_last is always > 0 and
u += sign * even; * therefore we do not need to check its sign. In particular, it's easy
u_last -= sign * even * (1 << w); * to see that u_last is never < 0 because u is never < 0. Moreover,
* u_last is never = 0 because u is never even after a loop
* iteration. The same holds analogously for the initial value of
* u_last (in the first loop iteration). */
VERIFY_CHECK(u_last > 0);
VERIFY_CHECK((u_last & 1) == 1);
u += even;
u_last -= even * (1 << w);
/* 4.3, adapted for global sign change */ /* 4.3, adapted for global sign change */
wnaf[word++] = u_last * global_sign; wnaf[word++] = u_last * global_sign;