Skip to content

Commit 96f68a0

Browse files
committed
Don't invert scalar in wnaf_fixed when it is even because a caller might
intentionally give a scalar with many leading zeros.
1 parent 6dbb007 commit 96f68a0

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

src/ecmult_impl.h

+5-17
Original file line numberDiff line numberDiff line change
@@ -563,15 +563,11 @@ static size_t secp256k1_strauss_max_points(secp256k1_scratch *scratch) {
563563
* It has the following guarantees:
564564
* - each wnaf[i] is either 0 or an odd integer between -(1 << w) and (1 << w)
565565
* - the number of words set is always WNAF_SIZE(w)
566-
* - the returned skew is 0 without endomorphism, or 0 or 1 with endomorphism
566+
* - the returned skew is 0 or 1
567567
*/
568568
static int secp256k1_wnaf_fixed(int *wnaf, const secp256k1_scalar *s, int w) {
569-
int sign = 0;
570569
int skew = 0;
571570
int pos = 1;
572-
#ifndef USE_ENDOMORPHISM
573-
secp256k1_scalar neg_s;
574-
#endif
575571
const secp256k1_scalar *work = s;
576572

577573
if (secp256k1_scalar_is_zero(s)) {
@@ -583,16 +579,10 @@ static int secp256k1_wnaf_fixed(int *wnaf, const secp256k1_scalar *s, int w) {
583579
}
584580

585581
if (secp256k1_scalar_is_even(s)) {
586-
#ifdef USE_ENDOMORPHISM
587582
skew = 1;
588-
#else
589-
secp256k1_scalar_negate(&neg_s, s);
590-
work = &neg_s;
591-
sign = -1;
592-
#endif
593583
}
594584

595-
wnaf[0] = (secp256k1_scalar_get_bits_var(work, 0, w) + skew + sign) ^ sign;
585+
wnaf[0] = secp256k1_scalar_get_bits_var(work, 0, w) + skew;
596586

597587
while (pos * w < WNAF_BITS) {
598588
int now = w;
@@ -602,10 +592,10 @@ static int secp256k1_wnaf_fixed(int *wnaf, const secp256k1_scalar *s, int w) {
602592
}
603593
val = secp256k1_scalar_get_bits_var(work, pos * w, now);
604594
if ((val & 1) == 0) {
605-
wnaf[pos - 1] -= ((1 << w) + sign) ^ sign;
606-
wnaf[pos] = (val + 1 + sign) ^ sign;
595+
wnaf[pos - 1] -= (1 << w);
596+
wnaf[pos] = (val + 1);
607597
} else {
608-
wnaf[pos] = (val + sign) ^ sign;
598+
wnaf[pos] = val;
609599
}
610600
/* Set a coefficient to zero if it is 1 or -1 and the proceeding digit
611601
* is strictly negative or strictly positive respectively. Only change
@@ -678,7 +668,6 @@ static int secp256k1_ecmult_pippenger_wnaf(secp256k1_gej *buckets, int bucket_wi
678668
secp256k1_ge tmp;
679669
int idx;
680670

681-
#ifdef USE_ENDOMORPHISM
682671
if (i == 0) {
683672
/* correct for wnaf skew */
684673
int skew = point_state.skew_na;
@@ -687,7 +676,6 @@ static int secp256k1_ecmult_pippenger_wnaf(secp256k1_gej *buckets, int bucket_wi
687676
secp256k1_gej_add_ge_var(&buckets[0], &buckets[0], &tmp, NULL);
688677
}
689678
}
690-
#endif
691679
if (n > 0) {
692680
idx = (n - 1)/2;
693681
secp256k1_gej_add_ge_var(&buckets[idx], &buckets[idx], &pt[point_state.input_pos], NULL);

0 commit comments

Comments
 (0)