Skip to content

Commit ec0a7b3

Browse files
committed
Don't touch leading zeros in wnaf_fixed.
1 parent 9e36d1b commit ec0a7b3

File tree

2 files changed

+73
-12
lines changed

2 files changed

+73
-12
lines changed

src/ecmult_impl.h

+20-9
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,9 @@ static size_t secp256k1_strauss_max_points(secp256k1_scratch *scratch) {
567567
*/
568568
static int secp256k1_wnaf_fixed(int *wnaf, const secp256k1_scalar *s, int w) {
569569
int skew = 0;
570-
int pos = 1;
570+
int pos;
571+
int max_pos;
572+
int last_w;
571573
const secp256k1_scalar *work = s;
572574

573575
if (secp256k1_scalar_is_zero(s)) {
@@ -582,14 +584,24 @@ static int secp256k1_wnaf_fixed(int *wnaf, const secp256k1_scalar *s, int w) {
582584
}
583585

584586
wnaf[0] = secp256k1_scalar_get_bits_var(work, 0, w) + skew;
585-
586-
while (pos * w < WNAF_BITS) {
587-
int now = w;
588-
int val;
589-
if (now + pos * w > WNAF_BITS) {
590-
now = WNAF_BITS - pos * w;
587+
/* Compute last window size. Relevant when window size doesn't divide the
588+
* number of bits in the scalar */
589+
last_w = WNAF_BITS - (WNAF_SIZE(w) - 1) * w;
590+
591+
/* Store the position of the first nonzero word in max_pos to allow
592+
* skipping leading zeros when calculating the wnaf. */
593+
for (pos = WNAF_SIZE(w) - 1; pos > 0; pos--) {
594+
int val = secp256k1_scalar_get_bits_var(work, pos * w, pos == WNAF_SIZE(w)-1 ? last_w : w);
595+
if(val != 0) {
596+
break;
591597
}
592-
val = secp256k1_scalar_get_bits_var(work, pos * w, now);
598+
wnaf[pos] = 0;
599+
}
600+
max_pos = pos;
601+
pos = 1;
602+
603+
while (pos <= max_pos) {
604+
int val = secp256k1_scalar_get_bits_var(work, pos * w, pos == WNAF_SIZE(w)-1 ? last_w : w);
593605
if ((val & 1) == 0) {
594606
wnaf[pos - 1] -= (1 << w);
595607
wnaf[pos] = (val + 1);
@@ -611,7 +623,6 @@ static int secp256k1_wnaf_fixed(int *wnaf, const secp256k1_scalar *s, int w) {
611623
}
612624
++pos;
613625
}
614-
VERIFY_CHECK(pos == WNAF_SIZE(w));
615626

616627
return skew;
617628
}

src/tests.c

+53-3
Original file line numberDiff line numberDiff line change
@@ -3040,20 +3040,70 @@ void test_fixed_wnaf(const secp256k1_scalar *number, int w) {
30403040
CHECK(secp256k1_scalar_eq(&x, &num));
30413041
}
30423042

3043-
void test_fixed_wnaf_zero(int w) {
3043+
/* Checks that the first 8 elements of wnaf are equal to wnaf_expected and the
3044+
* rest is 0.*/
3045+
void test_fixed_wnaf_small_helper(int *wnaf, int *wnaf_expected, int w) {
3046+
int i;
3047+
for (i = WNAF_SIZE(w)-1; i >= 8; --i) {
3048+
CHECK(wnaf[i] == 0);
3049+
}
3050+
for (i = 7; i >= 0; --i) {
3051+
CHECK(wnaf[i] == wnaf_expected[i]);
3052+
}
3053+
}
3054+
3055+
void test_fixed_wnaf_small(void) {
3056+
int w = 4;
30443057
int wnaf[256] = {0};
30453058
int i;
30463059
int skew;
30473060
secp256k1_scalar num;
30483061

30493062
secp256k1_scalar_set_int(&num, 0);
30503063
skew = secp256k1_wnaf_fixed(wnaf, &num, w);
3051-
30523064
for (i = WNAF_SIZE(w)-1; i >= 0; --i) {
30533065
int v = wnaf[i];
30543066
CHECK(v == 0);
30553067
}
30563068
CHECK(skew == 0);
3069+
3070+
secp256k1_scalar_set_int(&num, 1);
3071+
skew = secp256k1_wnaf_fixed(wnaf, &num, w);
3072+
for (i = WNAF_SIZE(w)-1; i >= 1; --i) {
3073+
int v = wnaf[i];
3074+
CHECK(v == 0);
3075+
}
3076+
CHECK(wnaf[0] == 1);
3077+
CHECK(skew == 0);
3078+
3079+
{
3080+
int wnaf_expected[8] = { 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf, 0xf };
3081+
secp256k1_scalar_set_int(&num, 0xffffffff);
3082+
skew = secp256k1_wnaf_fixed(wnaf, &num, w);
3083+
test_fixed_wnaf_small_helper(wnaf, wnaf_expected, w);
3084+
CHECK(skew == 0);
3085+
}
3086+
{
3087+
int wnaf_expected[8] = { -1, -1, -1, -1, -1, -1, -1, 0xf };
3088+
secp256k1_scalar_set_int(&num, 0xeeeeeeee);
3089+
skew = secp256k1_wnaf_fixed(wnaf, &num, w);
3090+
test_fixed_wnaf_small_helper(wnaf, wnaf_expected, w);
3091+
CHECK(skew == 1);
3092+
}
3093+
{
3094+
int wnaf_expected[8] = { 1, 0, 1, 0, 1, 0, 1, 0 };
3095+
secp256k1_scalar_set_int(&num, 0x01010101);
3096+
skew = secp256k1_wnaf_fixed(wnaf, &num, w);
3097+
test_fixed_wnaf_small_helper(wnaf, wnaf_expected, w);
3098+
CHECK(skew == 0);
3099+
}
3100+
{
3101+
int wnaf_expected[8] = { -0xf, 0, 0xf, -0xf, 0, 0xf, 1, 0 };
3102+
secp256k1_scalar_set_int(&num, 0x01ef1ef1);
3103+
skew = secp256k1_wnaf_fixed(wnaf, &num, w);
3104+
test_fixed_wnaf_small_helper(wnaf, wnaf_expected, w);
3105+
CHECK(skew == 0);
3106+
}
30573107
}
30583108

30593109
void run_wnaf(void) {
@@ -3067,7 +3117,7 @@ void run_wnaf(void) {
30673117
n.d[0] = 2;
30683118
test_constant_wnaf(&n, 4);
30693119
/* Test 0 */
3070-
test_fixed_wnaf_zero(4);
3120+
test_fixed_wnaf_small();
30713121
/* Random tests */
30723122
for (i = 0; i < count; i++) {
30733123
random_scalar_order(&n);

0 commit comments

Comments
 (0)