1 /**********************************************************************
2 * Copyright (c) 2015 Pieter Wuille, Andrew Poelstra *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5 **********************************************************************/
7 #ifndef SECP256K1_ECMULT_CONST_IMPL_H
8 #define SECP256K1_ECMULT_CONST_IMPL_H
12 #include "ecmult_const.h"
13 #include "ecmult_impl.h"
15 /* This is like `ECMULT_TABLE_GET_GE` but is constant time */
16 #define ECMULT_CONST_TABLE_GET_GE(r,pre,n,w) do { \
18 /* Extract the sign-bit for a constant time absolute-value. */ \
19 int mask = (n) >> (sizeof(n) * CHAR_BIT - 1); \
20 int abs_n = ((n) + mask) ^ mask; \
21 int idx_n = abs_n >> 1; \
23 VERIFY_CHECK(((n) & 1) == 1); \
24 VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \
25 VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \
26 VERIFY_SETUP(secp256k1_fe_clear(&(r)->x)); \
27 VERIFY_SETUP(secp256k1_fe_clear(&(r)->y)); \
28 /* Unconditionally set r->x = (pre)[m].x. r->y = (pre)[m].y. because it's either the correct one \
29 * or will get replaced in the later iterations, this is needed to make sure `r` is initialized. */ \
30 (r)->x = (pre)[m].x; \
31 (r)->y = (pre)[m].y; \
32 for (m = 1; m < ECMULT_TABLE_SIZE(w); m++) { \
33 /* This loop is used to avoid secret data in array indices. See
34 * the comment in ecmult_gen_impl.h for rationale. */ \
35 secp256k1_fe_cmov(&(r)->x, &(pre)[m].x, m == idx_n); \
36 secp256k1_fe_cmov(&(r)->y, &(pre)[m].y, m == idx_n); \
39 secp256k1_fe_negate(&neg_y, &(r)->y, 1); \
40 secp256k1_fe_cmov(&(r)->y, &neg_y, (n) != abs_n); \
44 /** Convert a number to WNAF notation.
45 * The number becomes represented by sum(2^{wi} * wnaf[i], i=0..WNAF_SIZE(w)+1) - return_val.
46 * It has the following guarantees:
47 * - each wnaf[i] an odd integer between -(1 << w) and (1 << w)
48 * - each wnaf[i] is nonzero
49 * - the number of words set is always WNAF_SIZE(w) + 1
51 * Adapted from `The Width-w NAF Method Provides Small Memory and Fast Elliptic Scalar
52 * Multiplications Secure against Side Channel Attacks`, Okeya and Tagaki. M. Joye (Ed.)
53 * CT-RSA 2003, LNCS 2612, pp. 328-443, 2003. Springer-Verlag Berlin Heidelberg 2003
55 * Numbers reference steps of `Algorithm SPA-resistant Width-w NAF with Odd Scalar` on pp. 335
57 static int secp256k1_wnaf_const(int *wnaf, const secp256k1_scalar *scalar, int w, int size) {
72 VERIFY_CHECK(size > 0);
74 /* Note that we cannot handle even numbers by negating them to be odd, as is
75 * done in other implementations, since if our scalars were specified to have
76 * 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
80 * this, and having the caller compensate after doing the multiplication.
82 * In fact, we _do_ want to negate numbers to minimize their bit-lengths (and in
83 * 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);
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. */
101 global_sign = secp256k1_scalar_cond_negate(&s, flip);
102 global_sign *= not_neg_one * 2 - 1;
106 u_last = secp256k1_scalar_shr_int(&s, w);
111 u = secp256k1_scalar_shr_int(&s, w);
113 even = ((u & 1) == 0);
114 /* In contrast to the original algorithm, u_last is always > 0 and
115 * therefore we do not need to check its sign. In particular, it's easy
116 * to see that u_last is never < 0 because u is never < 0. Moreover,
117 * u_last is never = 0 because u is never even after a loop
118 * iteration. The same holds analogously for the initial value of
119 * u_last (in the first loop iteration). */
120 VERIFY_CHECK(u_last > 0);
121 VERIFY_CHECK((u_last & 1) == 1);
123 u_last -= even * (1 << w);
125 /* 4.3, adapted for global sign change */
126 wnaf[word++] = u_last * global_sign;
129 } while (word * w < size);
130 wnaf[word] = u * global_sign;
132 VERIFY_CHECK(secp256k1_scalar_is_zero(&s));
133 VERIFY_CHECK(word == WNAF_SIZE_BITS(size, w));
137 static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, const secp256k1_scalar *scalar, int size) {
138 secp256k1_ge pre_a[ECMULT_TABLE_SIZE(WINDOW_A)];
143 #ifdef USE_ENDOMORPHISM
144 secp256k1_ge pre_a_lam[ECMULT_TABLE_SIZE(WINDOW_A)];
145 int wnaf_lam[1 + WNAF_SIZE(WINDOW_A - 1)];
147 secp256k1_scalar q_1, q_lam;
149 int wnaf_1[1 + WNAF_SIZE(WINDOW_A - 1)];
153 /* build wnaf representation for q. */
155 #ifdef USE_ENDOMORPHISM
158 /* split q into q_1 and q_lam (where q = q_1 + q_lam*lambda, and q_1 and q_lam are ~128 bit) */
159 secp256k1_scalar_split_lambda(&q_1, &q_lam, scalar);
160 skew_1 = secp256k1_wnaf_const(wnaf_1, &q_1, WINDOW_A - 1, 128);
161 skew_lam = secp256k1_wnaf_const(wnaf_lam, &q_lam, WINDOW_A - 1, 128);
165 skew_1 = secp256k1_wnaf_const(wnaf_1, scalar, WINDOW_A - 1, size);
166 #ifdef USE_ENDOMORPHISM
171 /* Calculate odd multiples of a.
172 * All multiples are brought to the same Z 'denominator', which is stored
173 * in Z. Due to secp256k1' isomorphism we can do all operations pretending
174 * that the Z coordinate was 1, use affine addition formulae, and correct
175 * the Z coordinate of the result once at the end.
177 secp256k1_gej_set_ge(r, a);
178 secp256k1_ecmult_odd_multiples_table_globalz_windowa(pre_a, &Z, r);
179 for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) {
180 secp256k1_fe_normalize_weak(&pre_a[i].y);
182 #ifdef USE_ENDOMORPHISM
184 for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) {
185 secp256k1_ge_mul_lambda(&pre_a_lam[i], &pre_a[i]);
191 /* first loop iteration (separated out so we can directly set r, rather
192 * than having it start at infinity, get doubled several times, then have
193 * its new value added to it) */
194 i = wnaf_1[WNAF_SIZE_BITS(rsize, WINDOW_A - 1)];
195 VERIFY_CHECK(i != 0);
196 ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, i, WINDOW_A);
197 secp256k1_gej_set_ge(r, &tmpa);
198 #ifdef USE_ENDOMORPHISM
200 i = wnaf_lam[WNAF_SIZE_BITS(rsize, WINDOW_A - 1)];
201 VERIFY_CHECK(i != 0);
202 ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a_lam, i, WINDOW_A);
203 secp256k1_gej_add_ge(r, r, &tmpa);
206 /* remaining loop iterations */
207 for (i = WNAF_SIZE_BITS(rsize, WINDOW_A - 1) - 1; i >= 0; i--) {
210 for (j = 0; j < WINDOW_A - 1; ++j) {
211 secp256k1_gej_double_nonzero(r, r);
215 ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A);
216 VERIFY_CHECK(n != 0);
217 secp256k1_gej_add_ge(r, r, &tmpa);
218 #ifdef USE_ENDOMORPHISM
221 ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a_lam, n, WINDOW_A);
222 VERIFY_CHECK(n != 0);
223 secp256k1_gej_add_ge(r, r, &tmpa);
228 secp256k1_fe_mul(&r->z, &r->z, &Z);
231 /* Correct for wNAF skew */
232 secp256k1_ge correction = *a;
233 secp256k1_ge_storage correction_1_stor;
234 #ifdef USE_ENDOMORPHISM
235 secp256k1_ge_storage correction_lam_stor;
237 secp256k1_ge_storage a2_stor;
239 secp256k1_gej_set_ge(&tmpj, &correction);
240 secp256k1_gej_double_var(&tmpj, &tmpj, NULL);
241 secp256k1_ge_set_gej(&correction, &tmpj);
242 secp256k1_ge_to_storage(&correction_1_stor, a);
243 #ifdef USE_ENDOMORPHISM
245 secp256k1_ge_to_storage(&correction_lam_stor, a);
248 secp256k1_ge_to_storage(&a2_stor, &correction);
250 /* For odd numbers this is 2a (so replace it), for even ones a (so no-op) */
251 secp256k1_ge_storage_cmov(&correction_1_stor, &a2_stor, skew_1 == 2);
252 #ifdef USE_ENDOMORPHISM
254 secp256k1_ge_storage_cmov(&correction_lam_stor, &a2_stor, skew_lam == 2);
258 /* Apply the correction */
259 secp256k1_ge_from_storage(&correction, &correction_1_stor);
260 secp256k1_ge_neg(&correction, &correction);
261 secp256k1_gej_add_ge(r, r, &correction);
263 #ifdef USE_ENDOMORPHISM
265 secp256k1_ge_from_storage(&correction, &correction_lam_stor);
266 secp256k1_ge_neg(&correction, &correction);
267 secp256k1_ge_mul_lambda(&correction, &correction);
268 secp256k1_gej_add_ge(r, r, &correction);
274 #endif /* SECP256K1_ECMULT_CONST_IMPL_H */