]>
Commit | Line | Data |
---|---|---|
44015000 AP |
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 | **********************************************************************/ | |
6 | ||
abe2d3e8 DR |
7 | #ifndef SECP256K1_ECMULT_CONST_IMPL_H |
8 | #define SECP256K1_ECMULT_CONST_IMPL_H | |
44015000 AP |
9 | |
10 | #include "scalar.h" | |
11 | #include "group.h" | |
12 | #include "ecmult_const.h" | |
13 | #include "ecmult_impl.h" | |
14 | ||
44015000 AP |
15 | /* This is like `ECMULT_TABLE_GET_GE` but is constant time */ |
16 | #define ECMULT_CONST_TABLE_GET_GE(r,pre,n,w) do { \ | |
17 | int m; \ | |
18 | int abs_n = (n) * (((n) > 0) * 2 - 1); \ | |
72ae443a | 19 | int idx_n = abs_n / 2; \ |
dd891e0e | 20 | secp256k1_fe neg_y; \ |
44015000 AP |
21 | VERIFY_CHECK(((n) & 1) == 1); \ |
22 | VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \ | |
23 | VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \ | |
72ae443a PD |
24 | VERIFY_SETUP(secp256k1_fe_clear(&(r)->x)); \ |
25 | VERIFY_SETUP(secp256k1_fe_clear(&(r)->y)); \ | |
26 | for (m = 0; m < ECMULT_TABLE_SIZE(w); m++) { \ | |
44015000 AP |
27 | /* This loop is used to avoid secret data in array indices. See |
28 | * the comment in ecmult_gen_impl.h for rationale. */ \ | |
72ae443a PD |
29 | secp256k1_fe_cmov(&(r)->x, &(pre)[m].x, m == idx_n); \ |
30 | secp256k1_fe_cmov(&(r)->y, &(pre)[m].y, m == idx_n); \ | |
44015000 AP |
31 | } \ |
32 | (r)->infinity = 0; \ | |
44015000 AP |
33 | secp256k1_fe_negate(&neg_y, &(r)->y, 1); \ |
34 | secp256k1_fe_cmov(&(r)->y, &neg_y, (n) != abs_n); \ | |
35 | } while(0) | |
36 | ||
37 | ||
768514ba JN |
38 | /** Convert a number to WNAF notation. |
39 | * The number becomes represented by sum(2^{wi} * wnaf[i], i=0..WNAF_SIZE(w)+1) - return_val. | |
40 | * It has the following guarantees: | |
44015000 AP |
41 | * - each wnaf[i] an odd integer between -(1 << w) and (1 << w) |
42 | * - each wnaf[i] is nonzero | |
768514ba | 43 | * - the number of words set is always WNAF_SIZE(w) + 1 |
44015000 AP |
44 | * |
45 | * Adapted from `The Width-w NAF Method Provides Small Memory and Fast Elliptic Scalar | |
46 | * Multiplications Secure against Side Channel Attacks`, Okeya and Tagaki. M. Joye (Ed.) | |
47 | * CT-RSA 2003, LNCS 2612, pp. 328-443, 2003. Springer-Verlagy Berlin Heidelberg 2003 | |
48 | * | |
49 | * Numbers reference steps of `Algorithm SPA-resistant Width-w NAF with Odd Scalar` on pp. 335 | |
50 | */ | |
dd891e0e | 51 | static int secp256k1_wnaf_const(int *wnaf, secp256k1_scalar s, int w) { |
cfe0ed91 | 52 | int global_sign; |
92e53fc4 | 53 | int skew = 0; |
44015000 | 54 | int word = 0; |
c6191fde | 55 | |
44015000 | 56 | /* 1 2 3 */ |
92e53fc4 | 57 | int u_last; |
44015000 | 58 | int u; |
92e53fc4 | 59 | |
cfe0ed91 GM |
60 | int flip; |
61 | int bit; | |
62 | secp256k1_scalar neg_s; | |
63 | int not_neg_one; | |
c6191fde AP |
64 | /* Note that we cannot handle even numbers by negating them to be odd, as is |
65 | * done in other implementations, since if our scalars were specified to have | |
66 | * width < 256 for performance reasons, their negations would have width 256 | |
67 | * and we'd lose any performance benefit. Instead, we use a technique from | |
92e53fc4 | 68 | * Section 4.2 of the Okeya/Tagaki paper, which is to add either 1 (for even) |
c6191fde AP |
69 | * or 2 (for odd) to the number we are encoding, returning a skew value indicating |
70 | * this, and having the caller compensate after doing the multiplication. */ | |
71 | ||
72 | /* Negative numbers will be negated to keep their bit representation below the maximum width */ | |
cfe0ed91 | 73 | flip = secp256k1_scalar_is_high(&s); |
92e53fc4 | 74 | /* We add 1 to even numbers, 2 to odd ones, noting that negation flips parity */ |
83836a95 | 75 | bit = flip ^ !secp256k1_scalar_is_even(&s); |
92e53fc4 | 76 | /* We check for negative one, since adding 2 to it will cause an overflow */ |
92e53fc4 AP |
77 | secp256k1_scalar_negate(&neg_s, &s); |
78 | not_neg_one = !secp256k1_scalar_is_one(&neg_s); | |
79 | secp256k1_scalar_cadd_bit(&s, bit, not_neg_one); | |
80 | /* If we had negative one, flip == 1, s.d[0] == 0, bit == 1, so caller expects | |
81 | * that we added two to it and flipped it. In fact for -1 these operations are | |
82 | * identical. We only flipped, but since skewing is required (in the sense that | |
83 | * the skew must be 1 or 2, never zero) and flipping is not, we need to change | |
84 | * our flags to claim that we only skewed. */ | |
85 | global_sign = secp256k1_scalar_cond_negate(&s, flip); | |
86 | global_sign *= not_neg_one * 2 - 1; | |
87 | skew = 1 << bit; | |
92e53fc4 | 88 | |
44015000 | 89 | /* 4 */ |
92e53fc4 | 90 | u_last = secp256k1_scalar_shr_int(&s, w); |
44015000 AP |
91 | while (word * w < WNAF_BITS) { |
92 | int sign; | |
93 | int even; | |
94 | ||
95 | /* 4.1 4.4 */ | |
96 | u = secp256k1_scalar_shr_int(&s, w); | |
97 | /* 4.2 */ | |
98 | even = ((u & 1) == 0); | |
99 | sign = 2 * (u_last > 0) - 1; | |
100 | u += sign * even; | |
101 | u_last -= sign * even * (1 << w); | |
102 | ||
103 | /* 4.3, adapted for global sign change */ | |
104 | wnaf[word++] = u_last * global_sign; | |
105 | ||
106 | u_last = u; | |
107 | } | |
108 | wnaf[word] = u * global_sign; | |
109 | ||
110 | VERIFY_CHECK(secp256k1_scalar_is_zero(&s)); | |
111 | VERIFY_CHECK(word == WNAF_SIZE(w)); | |
92e53fc4 | 112 | return skew; |
44015000 AP |
113 | } |
114 | ||
115 | ||
dd891e0e PW |
116 | static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, const secp256k1_scalar *scalar) { |
117 | secp256k1_ge pre_a[ECMULT_TABLE_SIZE(WINDOW_A)]; | |
118 | secp256k1_ge tmpa; | |
119 | secp256k1_fe Z; | |
44015000 | 120 | |
c6191fde AP |
121 | int skew_1; |
122 | int wnaf_1[1 + WNAF_SIZE(WINDOW_A - 1)]; | |
92e53fc4 | 123 | #ifdef USE_ENDOMORPHISM |
dd891e0e | 124 | secp256k1_ge pre_a_lam[ECMULT_TABLE_SIZE(WINDOW_A)]; |
92e53fc4 | 125 | int wnaf_lam[1 + WNAF_SIZE(WINDOW_A - 1)]; |
92e53fc4 | 126 | int skew_lam; |
dd891e0e | 127 | secp256k1_scalar q_1, q_lam; |
92e53fc4 | 128 | #endif |
44015000 AP |
129 | |
130 | int i; | |
dd891e0e | 131 | secp256k1_scalar sc = *scalar; |
92e53fc4 AP |
132 | |
133 | /* build wnaf representation for q. */ | |
134 | #ifdef USE_ENDOMORPHISM | |
135 | /* split q into q_1 and q_lam (where q = q_1 + q_lam*lambda, and q_1 and q_lam are ~128 bit) */ | |
136 | secp256k1_scalar_split_lambda(&q_1, &q_lam, &sc); | |
92e53fc4 AP |
137 | skew_1 = secp256k1_wnaf_const(wnaf_1, q_1, WINDOW_A - 1); |
138 | skew_lam = secp256k1_wnaf_const(wnaf_lam, q_lam, WINDOW_A - 1); | |
139 | #else | |
c6191fde | 140 | skew_1 = secp256k1_wnaf_const(wnaf_1, sc, WINDOW_A - 1); |
92e53fc4 | 141 | #endif |
44015000 AP |
142 | |
143 | /* Calculate odd multiples of a. | |
144 | * All multiples are brought to the same Z 'denominator', which is stored | |
145 | * in Z. Due to secp256k1' isomorphism we can do all operations pretending | |
146 | * that the Z coordinate was 1, use affine addition formulae, and correct | |
147 | * the Z coordinate of the result once at the end. | |
148 | */ | |
149 | secp256k1_gej_set_ge(r, a); | |
150 | secp256k1_ecmult_odd_multiples_table_globalz_windowa(pre_a, &Z, r); | |
72ae443a PD |
151 | for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) { |
152 | secp256k1_fe_normalize_weak(&pre_a[i].y); | |
153 | } | |
92e53fc4 AP |
154 | #ifdef USE_ENDOMORPHISM |
155 | for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) { | |
156 | secp256k1_ge_mul_lambda(&pre_a_lam[i], &pre_a[i]); | |
157 | } | |
158 | #endif | |
44015000 AP |
159 | |
160 | /* first loop iteration (separated out so we can directly set r, rather | |
161 | * than having it start at infinity, get doubled several times, then have | |
162 | * its new value added to it) */ | |
92e53fc4 AP |
163 | i = wnaf_1[WNAF_SIZE(WINDOW_A - 1)]; |
164 | VERIFY_CHECK(i != 0); | |
165 | ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, i, WINDOW_A); | |
166 | secp256k1_gej_set_ge(r, &tmpa); | |
c6191fde | 167 | #ifdef USE_ENDOMORPHISM |
92e53fc4 AP |
168 | i = wnaf_lam[WNAF_SIZE(WINDOW_A - 1)]; |
169 | VERIFY_CHECK(i != 0); | |
170 | ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a_lam, i, WINDOW_A); | |
171 | secp256k1_gej_add_ge(r, r, &tmpa); | |
92e53fc4 | 172 | #endif |
44015000 AP |
173 | /* remaining loop iterations */ |
174 | for (i = WNAF_SIZE(WINDOW_A - 1) - 1; i >= 0; i--) { | |
175 | int n; | |
176 | int j; | |
177 | for (j = 0; j < WINDOW_A - 1; ++j) { | |
178 | secp256k1_gej_double_nonzero(r, r, NULL); | |
179 | } | |
c6191fde | 180 | |
92e53fc4 AP |
181 | n = wnaf_1[i]; |
182 | ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A); | |
183 | VERIFY_CHECK(n != 0); | |
184 | secp256k1_gej_add_ge(r, r, &tmpa); | |
c6191fde | 185 | #ifdef USE_ENDOMORPHISM |
92e53fc4 AP |
186 | n = wnaf_lam[i]; |
187 | ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a_lam, n, WINDOW_A); | |
188 | VERIFY_CHECK(n != 0); | |
189 | secp256k1_gej_add_ge(r, r, &tmpa); | |
92e53fc4 | 190 | #endif |
44015000 AP |
191 | } |
192 | ||
193 | secp256k1_fe_mul(&r->z, &r->z, &Z); | |
194 | ||
92e53fc4 AP |
195 | { |
196 | /* Correct for wNAF skew */ | |
dd891e0e PW |
197 | secp256k1_ge correction = *a; |
198 | secp256k1_ge_storage correction_1_stor; | |
c6191fde | 199 | #ifdef USE_ENDOMORPHISM |
dd891e0e | 200 | secp256k1_ge_storage correction_lam_stor; |
c6191fde | 201 | #endif |
dd891e0e PW |
202 | secp256k1_ge_storage a2_stor; |
203 | secp256k1_gej tmpj; | |
92e53fc4 AP |
204 | secp256k1_gej_set_ge(&tmpj, &correction); |
205 | secp256k1_gej_double_var(&tmpj, &tmpj, NULL); | |
206 | secp256k1_ge_set_gej(&correction, &tmpj); | |
207 | secp256k1_ge_to_storage(&correction_1_stor, a); | |
c6191fde | 208 | #ifdef USE_ENDOMORPHISM |
92e53fc4 | 209 | secp256k1_ge_to_storage(&correction_lam_stor, a); |
c6191fde | 210 | #endif |
92e53fc4 AP |
211 | secp256k1_ge_to_storage(&a2_stor, &correction); |
212 | ||
213 | /* For odd numbers this is 2a (so replace it), for even ones a (so no-op) */ | |
214 | secp256k1_ge_storage_cmov(&correction_1_stor, &a2_stor, skew_1 == 2); | |
c6191fde | 215 | #ifdef USE_ENDOMORPHISM |
92e53fc4 | 216 | secp256k1_ge_storage_cmov(&correction_lam_stor, &a2_stor, skew_lam == 2); |
c6191fde | 217 | #endif |
92e53fc4 AP |
218 | |
219 | /* Apply the correction */ | |
220 | secp256k1_ge_from_storage(&correction, &correction_1_stor); | |
221 | secp256k1_ge_neg(&correction, &correction); | |
222 | secp256k1_gej_add_ge(r, r, &correction); | |
223 | ||
c6191fde | 224 | #ifdef USE_ENDOMORPHISM |
92e53fc4 AP |
225 | secp256k1_ge_from_storage(&correction, &correction_lam_stor); |
226 | secp256k1_ge_neg(&correction, &correction); | |
227 | secp256k1_ge_mul_lambda(&correction, &correction); | |
228 | secp256k1_gej_add_ge(r, r, &correction); | |
92e53fc4 | 229 | #endif |
c6191fde | 230 | } |
44015000 AP |
231 | } |
232 | ||
abe2d3e8 | 233 | #endif /* SECP256K1_ECMULT_CONST_IMPL_H */ |