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 for (m = 0; m < ECMULT_TABLE_SIZE(w); m++) { \
29 /* This loop is used to avoid secret data in array indices. See
30 * the comment in ecmult_gen_impl.h for rationale. */ \
31 secp256k1_fe_cmov(&(r)->x, &(pre)[m].x, m == idx_n); \
32 secp256k1_fe_cmov(&(r)->y, &(pre)[m].y, m == idx_n); \
35 secp256k1_fe_negate(&neg_y, &(r)->y, 1); \
36 secp256k1_fe_cmov(&(r)->y, &neg_y, (n) != abs_n); \
40 /** Convert a number to WNAF notation.
41 * The number becomes represented by sum(2^{wi} * wnaf[i], i=0..WNAF_SIZE(w)+1) - return_val.
42 * It has the following guarantees:
43 * - each wnaf[i] an odd integer between -(1 << w) and (1 << w)
44 * - each wnaf[i] is nonzero
45 * - the number of words set is always WNAF_SIZE(w) + 1
47 * Adapted from `The Width-w NAF Method Provides Small Memory and Fast Elliptic Scalar
48 * Multiplications Secure against Side Channel Attacks`, Okeya and Tagaki. M. Joye (Ed.)
49 * CT-RSA 2003, LNCS 2612, pp. 328-443, 2003. Springer-Verlagy Berlin Heidelberg 2003
51 * Numbers reference steps of `Algorithm SPA-resistant Width-w NAF with Odd Scalar` on pp. 335
53 static int secp256k1_wnaf_const(int *wnaf, const secp256k1_scalar *scalar, int w, int size) {
68 VERIFY_CHECK(size > 0);
70 /* Note that we cannot handle even numbers by negating them to be odd, as is
71 * done in other implementations, since if our scalars were specified to have
72 * width < 256 for performance reasons, their negations would have width 256
73 * and we'd lose any performance benefit. Instead, we use a technique from
74 * Section 4.2 of the Okeya/Tagaki paper, which is to add either 1 (for even)
75 * or 2 (for odd) to the number we are encoding, returning a skew value indicating
76 * this, and having the caller compensate after doing the multiplication.
78 * In fact, we _do_ want to negate numbers to minimize their bit-lengths (and in
79 * particular, to ensure that the outputs from the endomorphism-split fit into
80 * 128 bits). If we negate, the parity of our number flips, inverting which of
81 * {1, 2} we want to add to the scalar when ensuring that it's odd. Further
82 * complicating things, -1 interacts badly with `secp256k1_scalar_cadd_bit` and
83 * we need to special-case it in this logic. */
84 flip = secp256k1_scalar_is_high(scalar);
85 /* We add 1 to even numbers, 2 to odd ones, noting that negation flips parity */
86 bit = flip ^ !secp256k1_scalar_is_even(scalar);
87 /* We check for negative one, since adding 2 to it will cause an overflow */
88 secp256k1_scalar_negate(&s, scalar);
89 not_neg_one = !secp256k1_scalar_is_one(&s);
91 secp256k1_scalar_cadd_bit(&s, bit, not_neg_one);
92 /* If we had negative one, flip == 1, s.d[0] == 0, bit == 1, so caller expects
93 * that we added two to it and flipped it. In fact for -1 these operations are
94 * identical. We only flipped, but since skewing is required (in the sense that
95 * the skew must be 1 or 2, never zero) and flipping is not, we need to change
96 * our flags to claim that we only skewed. */
97 global_sign = secp256k1_scalar_cond_negate(&s, flip);
98 global_sign *= not_neg_one * 2 - 1;
102 u_last = secp256k1_scalar_shr_int(&s, w);
108 u = secp256k1_scalar_shr_int(&s, w);
110 even = ((u & 1) == 0);
111 sign = 2 * (u_last > 0) - 1;
113 u_last -= sign * even * (1 << w);
115 /* 4.3, adapted for global sign change */
116 wnaf[word++] = u_last * global_sign;
119 } while (word * w < size);
120 wnaf[word] = u * global_sign;
122 VERIFY_CHECK(secp256k1_scalar_is_zero(&s));
123 VERIFY_CHECK(word == WNAF_SIZE_BITS(size, w));
127 static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, const secp256k1_scalar *scalar, int size) {
128 secp256k1_ge pre_a[ECMULT_TABLE_SIZE(WINDOW_A)];
133 #ifdef USE_ENDOMORPHISM
134 secp256k1_ge pre_a_lam[ECMULT_TABLE_SIZE(WINDOW_A)];
135 int wnaf_lam[1 + WNAF_SIZE(WINDOW_A - 1)];
137 secp256k1_scalar q_1, q_lam;
139 int wnaf_1[1 + WNAF_SIZE(WINDOW_A - 1)];
143 /* build wnaf representation for q. */
145 #ifdef USE_ENDOMORPHISM
148 /* split q into q_1 and q_lam (where q = q_1 + q_lam*lambda, and q_1 and q_lam are ~128 bit) */
149 secp256k1_scalar_split_lambda(&q_1, &q_lam, scalar);
150 skew_1 = secp256k1_wnaf_const(wnaf_1, &q_1, WINDOW_A - 1, 128);
151 skew_lam = secp256k1_wnaf_const(wnaf_lam, &q_lam, WINDOW_A - 1, 128);
155 skew_1 = secp256k1_wnaf_const(wnaf_1, scalar, WINDOW_A - 1, size);
156 #ifdef USE_ENDOMORPHISM
161 /* Calculate odd multiples of a.
162 * All multiples are brought to the same Z 'denominator', which is stored
163 * in Z. Due to secp256k1' isomorphism we can do all operations pretending
164 * that the Z coordinate was 1, use affine addition formulae, and correct
165 * the Z coordinate of the result once at the end.
167 secp256k1_gej_set_ge(r, a);
168 secp256k1_ecmult_odd_multiples_table_globalz_windowa(pre_a, &Z, r);
169 for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) {
170 secp256k1_fe_normalize_weak(&pre_a[i].y);
172 #ifdef USE_ENDOMORPHISM
174 for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) {
175 secp256k1_ge_mul_lambda(&pre_a_lam[i], &pre_a[i]);
181 /* first loop iteration (separated out so we can directly set r, rather
182 * than having it start at infinity, get doubled several times, then have
183 * its new value added to it) */
184 i = wnaf_1[WNAF_SIZE_BITS(rsize, WINDOW_A - 1)];
185 VERIFY_CHECK(i != 0);
186 ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, i, WINDOW_A);
187 secp256k1_gej_set_ge(r, &tmpa);
188 #ifdef USE_ENDOMORPHISM
190 i = wnaf_lam[WNAF_SIZE_BITS(rsize, WINDOW_A - 1)];
191 VERIFY_CHECK(i != 0);
192 ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a_lam, i, WINDOW_A);
193 secp256k1_gej_add_ge(r, r, &tmpa);
196 /* remaining loop iterations */
197 for (i = WNAF_SIZE_BITS(rsize, WINDOW_A - 1) - 1; i >= 0; i--) {
200 for (j = 0; j < WINDOW_A - 1; ++j) {
201 secp256k1_gej_double_nonzero(r, r);
205 ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A);
206 VERIFY_CHECK(n != 0);
207 secp256k1_gej_add_ge(r, r, &tmpa);
208 #ifdef USE_ENDOMORPHISM
211 ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a_lam, n, WINDOW_A);
212 VERIFY_CHECK(n != 0);
213 secp256k1_gej_add_ge(r, r, &tmpa);
218 secp256k1_fe_mul(&r->z, &r->z, &Z);
221 /* Correct for wNAF skew */
222 secp256k1_ge correction = *a;
223 secp256k1_ge_storage correction_1_stor;
224 #ifdef USE_ENDOMORPHISM
225 secp256k1_ge_storage correction_lam_stor;
227 secp256k1_ge_storage a2_stor;
229 secp256k1_gej_set_ge(&tmpj, &correction);
230 secp256k1_gej_double_var(&tmpj, &tmpj, NULL);
231 secp256k1_ge_set_gej(&correction, &tmpj);
232 secp256k1_ge_to_storage(&correction_1_stor, a);
233 #ifdef USE_ENDOMORPHISM
235 secp256k1_ge_to_storage(&correction_lam_stor, a);
238 secp256k1_ge_to_storage(&a2_stor, &correction);
240 /* For odd numbers this is 2a (so replace it), for even ones a (so no-op) */
241 secp256k1_ge_storage_cmov(&correction_1_stor, &a2_stor, skew_1 == 2);
242 #ifdef USE_ENDOMORPHISM
244 secp256k1_ge_storage_cmov(&correction_lam_stor, &a2_stor, skew_lam == 2);
248 /* Apply the correction */
249 secp256k1_ge_from_storage(&correction, &correction_1_stor);
250 secp256k1_ge_neg(&correction, &correction);
251 secp256k1_gej_add_ge(r, r, &correction);
253 #ifdef USE_ENDOMORPHISM
255 secp256k1_ge_from_storage(&correction, &correction_lam_stor);
256 secp256k1_ge_neg(&correction, &correction);
257 secp256k1_ge_mul_lambda(&correction, &correction);
258 secp256k1_gej_add_ge(r, r, &correction);
264 #endif /* SECP256K1_ECMULT_CONST_IMPL_H */