Skip to content

Commit 37dba32

Browse files
committed
Remove unnecessary sign variable from wnaf_const
1 parent 6bb0b77 commit 37dba32

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/ecmult_const_impl.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,22 @@ static int secp256k1_wnaf_const(int *wnaf, const secp256k1_scalar *scalar, int w
101101
/* 4 */
102102
u_last = secp256k1_scalar_shr_int(&s, w);
103103
do {
104-
int sign;
105104
int even;
106105

107106
/* 4.1 4.4 */
108107
u = secp256k1_scalar_shr_int(&s, w);
109108
/* 4.2 */
110109
even = ((u & 1) == 0);
111-
sign = 2 * (u_last > 0) - 1;
112-
u += sign * even;
113-
u_last -= sign * even * (1 << w);
110+
/* In contrast to the original algorithm, u_last is always > 0 and
111+
* therefore we do not need to check its sign. In particular, it's easy
112+
* to see that u_last is never < 0 because u is never < 0. Moreover,
113+
* u_last is never = 0 because u is never even after a loop
114+
* iteration. The same holds analogously for the initial value of
115+
* u_last (in the first loop iteration). */
116+
VERIFY_CHECK(u_last > 0);
117+
VERIFY_CHECK((u_last & 1) == 1);
118+
u += even;
119+
u_last -= even * (1 << w);
114120

115121
/* 4.3, adapted for global sign change */
116122
wnaf[word++] = u_last * global_sign;

0 commit comments

Comments
 (0)