Skip to content

Commit 6498a6c

Browse files
committed
ECDH skews by 0 or 1
1 parent f4da005 commit 6498a6c

File tree

2 files changed

+14
-33
lines changed

2 files changed

+14
-33
lines changed

src/ecmult_const_impl.h

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
secp256k1_fe_cmov(&(r)->y, &neg_y, (n) != abs_n); \
4141
} while(0)
4242

43-
4443
/** Convert a number to WNAF notation.
4544
* The number becomes represented by sum(2^{wi} * wnaf[i], i=0..WNAF_SIZE(w)+1) - return_val.
4645
* It has the following guarantees:
@@ -56,51 +55,35 @@
5655
*/
5756
static int secp256k1_wnaf_const(int *wnaf, const secp256k1_scalar *scalar, int w, int size) {
5857
int global_sign;
59-
int skew = 0;
58+
int skew;
6059
int word = 0;
6160

6261
/* 1 2 3 */
6362
int u_last;
6463
int u;
6564

6665
int flip;
67-
int bit;
68-
secp256k1_scalar s;
69-
int not_neg_one;
66+
secp256k1_scalar s = *scalar;
7067

7168
VERIFY_CHECK(w > 0);
7269
VERIFY_CHECK(size > 0);
7370

7471
/* Note that we cannot handle even numbers by negating them to be odd, as is
7572
* done in other implementations, since if our scalars were specified to have
7673
* width < 256 for performance reasons, their negations would have width 256
77-
* and we'd lose any performance benefit. Instead, we use a technique from
78-
* Section 4.2 of the Okeya/Tagaki paper, which is to add either 1 (for even)
79-
* or 2 (for odd) to the number we are encoding, returning a skew value indicating
74+
* and we'd lose any performance benefit. Instead, we use a variation of a
75+
* technique from Section 4.2 of the Okeya/Tagaki paper, which is to add 1 to the
76+
* number we are encoding when it is even, returning a skew value indicating
8077
* this, and having the caller compensate after doing the multiplication.
8178
*
8279
* In fact, we _do_ want to negate numbers to minimize their bit-lengths (and in
8380
* particular, to ensure that the outputs from the endomorphism-split fit into
84-
* 128 bits). If we negate, the parity of our number flips, inverting which of
85-
* {1, 2} we want to add to the scalar when ensuring that it's odd. Further
86-
* complicating things, -1 interacts badly with `secp256k1_scalar_cadd_bit` and
87-
* we need to special-case it in this logic. */
88-
flip = secp256k1_scalar_is_high(scalar);
89-
/* We add 1 to even numbers, 2 to odd ones, noting that negation flips parity */
90-
bit = flip ^ !secp256k1_scalar_is_even(scalar);
91-
/* We check for negative one, since adding 2 to it will cause an overflow */
92-
secp256k1_scalar_negate(&s, scalar);
93-
not_neg_one = !secp256k1_scalar_is_one(&s);
94-
s = *scalar;
95-
secp256k1_scalar_cadd_bit(&s, bit, not_neg_one);
96-
/* If we had negative one, flip == 1, s.d[0] == 0, bit == 1, so caller expects
97-
* that we added two to it and flipped it. In fact for -1 these operations are
98-
* identical. We only flipped, but since skewing is required (in the sense that
99-
* the skew must be 1 or 2, never zero) and flipping is not, we need to change
100-
* our flags to claim that we only skewed. */
81+
* 128 bits). If we negate, the parity of our number flips, affecting whether
82+
* we want to add to the scalar to ensure that it's odd. */
83+
flip = secp256k1_scalar_is_high(&s);
84+
skew = flip ^ secp256k1_scalar_is_even(&s);
85+
secp256k1_scalar_cadd_bit(&s, 0, skew);
10186
global_sign = secp256k1_scalar_cond_negate(&s, flip);
102-
global_sign *= not_neg_one * 2 - 1;
103-
skew = 1 << bit;
10487

10588
/* 4 */
10689
u_last = secp256k1_scalar_shr_int(&s, w);
@@ -220,19 +203,17 @@ static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, cons
220203
/* Correct for wNAF skew */
221204
secp256k1_gej tmp;
222205
secp256k1_ge a_1;
223-
224206
secp256k1_ge_neg(&a_1, a);
225-
secp256k1_gej_add_ge(r, r, &a_1);
207+
226208
secp256k1_gej_add_ge(&tmp, r, &a_1);
227-
secp256k1_gej_cmov(r, &tmp, skew_1 == 2);
209+
secp256k1_gej_cmov(r, &tmp, skew_1);
228210

229211
if (size > 128) {
230212
secp256k1_ge a_lam;
231213
secp256k1_ge_mul_lambda(&a_lam, &a_1);
232214

233-
secp256k1_gej_add_ge(r, r, &a_lam);
234215
secp256k1_gej_add_ge(&tmp, r, &a_lam);
235-
secp256k1_gej_cmov(r, &tmp, skew_lam == 2);
216+
secp256k1_gej_cmov(r, &tmp, skew_lam);
236217
}
237218
}
238219
}

src/tests.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4347,7 +4347,7 @@ void test_constant_wnaf(const secp256k1_scalar *number, int w) {
43474347
secp256k1_scalar_add(&x, &x, &t);
43484348
}
43494349
/* Skew num because when encoding numbers as odd we use an offset */
4350-
secp256k1_scalar_set_int(&scalar_skew, 1 << (skew == 2));
4350+
secp256k1_scalar_set_int(&scalar_skew, skew);
43514351
secp256k1_scalar_add(&num, &num, &scalar_skew);
43524352
CHECK(secp256k1_scalar_eq(&x, &num));
43534353
}

0 commit comments

Comments
 (0)