@@ -28,16 +28,9 @@ static void secp256k1_ecmult_gen_context_init(secp256k1_ecmult_gen_context *ctx)
28
28
29
29
static void secp256k1_ecmult_gen_context_build (secp256k1_ecmult_gen_context * ctx , void * * prealloc ) {
30
30
#ifndef USE_ECMULT_STATIC_PRECOMPUTATION
31
- #if USE_COMB
32
31
secp256k1_ge prec [COMB_POINTS_TOTAL + COMB_OFFSET ];
33
32
secp256k1_gej u , sum ;
34
33
int block , index , spacing , stride , tooth ;
35
- #else
36
- secp256k1_ge prec [ECMULT_GEN_PREC_N * ECMULT_GEN_PREC_G ];
37
- secp256k1_gej gj ;
38
- secp256k1_gej nums_gej ;
39
- int i , j ;
40
- #endif
41
34
size_t const prealloc_size = SECP256K1_ECMULT_GEN_CONTEXT_PREALLOCATED_SIZE ;
42
35
void * const base = * prealloc ;
43
36
#endif
@@ -46,7 +39,6 @@ static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context *ctx
46
39
return ;
47
40
}
48
41
#ifndef USE_ECMULT_STATIC_PRECOMPUTATION
49
- #if USE_COMB
50
42
ctx -> prec = (secp256k1_ge_storage (* )[COMB_BLOCKS ][COMB_POINTS ])manual_alloc (prealloc , prealloc_size , base , prealloc_size );
51
43
52
44
/* get the generator */
@@ -95,72 +87,12 @@ static void secp256k1_ecmult_gen_context_build(secp256k1_ecmult_gen_context *ctx
95
87
ctx -> offset = prec [COMB_POINTS_TOTAL ];
96
88
#endif
97
89
98
- #else
99
- ctx -> prec = (secp256k1_ge_storage (* )[ECMULT_GEN_PREC_N ][ECMULT_GEN_PREC_G ])manual_alloc (prealloc , prealloc_size , base , prealloc_size );
100
-
101
- /* get the generator */
102
- secp256k1_gej_set_ge (& gj , & secp256k1_ge_const_g );
103
-
104
- /* Construct a group element with no known corresponding scalar (nothing up my sleeve). */
105
- {
106
- static const unsigned char nums_b32 [33 ] = "The scalar for this x is unknown" ;
107
- secp256k1_fe nums_x ;
108
- secp256k1_ge nums_ge ;
109
- int r ;
110
- r = secp256k1_fe_set_b32 (& nums_x , nums_b32 );
111
- (void )r ;
112
- VERIFY_CHECK (r );
113
- r = secp256k1_ge_set_xo_var (& nums_ge , & nums_x , 0 );
114
- (void )r ;
115
- VERIFY_CHECK (r );
116
- secp256k1_gej_set_ge (& nums_gej , & nums_ge );
117
- /* Add G to make the bits in x uniformly distributed. */
118
- secp256k1_gej_add_ge_var (& nums_gej , & nums_gej , & secp256k1_ge_const_g , NULL );
119
- }
120
-
121
- /* compute prec. */
122
- {
123
- secp256k1_gej precj [ECMULT_GEN_PREC_N * ECMULT_GEN_PREC_G ]; /* Jacobian versions of prec. */
124
- secp256k1_gej gbase ;
125
- secp256k1_gej numsbase ;
126
- gbase = gj ; /* PREC_G^j * G */
127
- numsbase = nums_gej ; /* 2^j * nums. */
128
- for (j = 0 ; j < ECMULT_GEN_PREC_N ; j ++ ) {
129
- /* Set precj[j*PREC_G .. j*PREC_G+(PREC_G-1)] to (numsbase, numsbase + gbase, ..., numsbase + (PREC_G-1)*gbase). */
130
- precj [j * ECMULT_GEN_PREC_G ] = numsbase ;
131
- for (i = 1 ; i < ECMULT_GEN_PREC_G ; i ++ ) {
132
- secp256k1_gej_add_var (& precj [j * ECMULT_GEN_PREC_G + i ], & precj [j * ECMULT_GEN_PREC_G + i - 1 ], & gbase , NULL );
133
- }
134
- /* Multiply gbase by PREC_G. */
135
- for (i = 0 ; i < ECMULT_GEN_PREC_B ; i ++ ) {
136
- secp256k1_gej_double_var (& gbase , & gbase , NULL );
137
- }
138
- /* Multiply numbase by 2. */
139
- secp256k1_gej_double_var (& numsbase , & numsbase , NULL );
140
- if (j == ECMULT_GEN_PREC_N - 2 ) {
141
- /* In the last iteration, numsbase is (1 - 2^j) * nums instead. */
142
- secp256k1_gej_neg (& numsbase , & numsbase );
143
- secp256k1_gej_add_var (& numsbase , & numsbase , & nums_gej , NULL );
144
- }
145
- }
146
- secp256k1_ge_set_all_gej_var (prec , precj , ECMULT_GEN_PREC_N * ECMULT_GEN_PREC_G );
147
- }
148
- for (j = 0 ; j < ECMULT_GEN_PREC_N ; j ++ ) {
149
- for (i = 0 ; i < ECMULT_GEN_PREC_G ; i ++ ) {
150
- secp256k1_ge_to_storage (& (* ctx -> prec )[j ][i ], & prec [j * ECMULT_GEN_PREC_G + i ]);
151
- }
152
- }
153
- #endif
154
90
#else
155
91
(void )prealloc ;
156
- #if USE_COMB
157
92
ctx -> prec = (secp256k1_ge_storage (* )[COMB_BLOCKS ][COMB_POINTS ])secp256k1_ecmult_gen_ctx_prec ;
158
93
#if COMB_OFFSET
159
94
secp256k1_ge_from_storage (& ctx -> offset , & secp256k1_ecmult_gen_ctx_offset );
160
95
#endif
161
- #else
162
- ctx -> prec = (secp256k1_ge_storage (* )[ECMULT_GEN_PREC_N ][ECMULT_GEN_PREC_G ])secp256k1_ecmult_static_context ;
163
- #endif
164
96
#endif
165
97
secp256k1_ecmult_gen_blind (ctx , NULL );
166
98
}
@@ -171,28 +103,20 @@ static int secp256k1_ecmult_gen_context_is_built(const secp256k1_ecmult_gen_cont
171
103
172
104
static void secp256k1_ecmult_gen_context_finalize_memcpy (secp256k1_ecmult_gen_context * dst , const secp256k1_ecmult_gen_context * src ) {
173
105
#ifndef USE_ECMULT_STATIC_PRECOMPUTATION
174
- #if USE_COMB
175
106
if (src -> prec != NULL ) {
176
107
/* We cast to void* first to suppress a -Wcast-align warning. */
177
108
dst -> prec = (secp256k1_ge_storage (* )[COMB_BLOCKS ][COMB_POINTS ])(void * )((unsigned char * )dst + ((unsigned char * )src -> prec - (unsigned char * )src ));
178
109
}
179
110
#if COMB_OFFSET
180
111
dst -> offset = src -> offset ;
181
112
#endif
182
- #else
183
- if (src -> prec != NULL ) {
184
- dst -> prec = (secp256k1_ge_storage (* )[ECMULT_GEN_PREC_N ][ECMULT_GEN_PREC_G ])(void * )((unsigned char * )dst + ((unsigned char * )src -> prec - (unsigned char * )src ));
185
- }
186
- #endif
187
113
#endif
188
114
(void )dst , (void )src ;
189
115
}
190
116
191
117
static void secp256k1_ecmult_gen_context_clear (secp256k1_ecmult_gen_context * ctx ) {
192
- #if USE_COMB
193
118
#if COMB_OFFSET
194
119
secp256k1_ge_clear (& ctx -> offset );
195
- #endif
196
120
#endif
197
121
secp256k1_scalar_clear (& ctx -> blind );
198
122
secp256k1_gej_clear (& ctx -> initial );
@@ -205,8 +129,6 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
205
129
secp256k1_scalar gnb ;
206
130
int bits ;
207
131
208
- #if USE_COMB
209
-
210
132
#if COMB_NEGATION
211
133
secp256k1_fe neg ;
212
134
int sign ;
@@ -252,6 +174,16 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
252
174
VERIFY_CHECK (0 <= abs && abs < COMB_POINTS );
253
175
254
176
for (index = 0 ; index < COMB_POINTS ; ++ index ) {
177
+ /** This uses a conditional move to avoid any secret data in array indexes.
178
+ * _Any_ use of secret indexes has been demonstrated to result in timing
179
+ * sidechannels, even when the cache-line access patterns are uniform.
180
+ * See also:
181
+ * "A word of warning", CHES 2013 Rump Session, by Daniel J. Bernstein and Peter Schwabe
182
+ * (https://cryptojedi.org/peter/data/chesrump-20130822.pdf) and
183
+ * "Cache Attacks and Countermeasures: the Case of AES", RSA 2006,
184
+ * by Dag Arne Osvik, Adi Shamir, and Eran Tromer
185
+ * (http://www.tau.ac.il/~tromer/papers/cache.pdf)
186
+ */
255
187
secp256k1_ge_storage_cmov (& adds , & (* ctx -> prec )[block ][index ], index == abs );
256
188
}
257
189
@@ -278,32 +210,6 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
278
210
memset (recoded , 0 , sizeof (recoded ));
279
211
abs = 0 ;
280
212
281
- #else
282
- int i , j ;
283
- memset (& adds , 0 , sizeof (adds ));
284
- * r = ctx -> initial ;
285
- /* Blind scalar/point multiplication by computing (n-b)G + bG instead of nG. */
286
- secp256k1_scalar_add (& gnb , gn , & ctx -> blind );
287
- add .infinity = 0 ;
288
- for (j = 0 ; j < ECMULT_GEN_PREC_N ; j ++ ) {
289
- bits = secp256k1_scalar_get_bits (& gnb , j * ECMULT_GEN_PREC_B , ECMULT_GEN_PREC_B );
290
- for (i = 0 ; i < ECMULT_GEN_PREC_G ; i ++ ) {
291
- /** This uses a conditional move to avoid any secret data in array indexes.
292
- * _Any_ use of secret indexes has been demonstrated to result in timing
293
- * sidechannels, even when the cache-line access patterns are uniform.
294
- * See also:
295
- * "A word of warning", CHES 2013 Rump Session, by Daniel J. Bernstein and Peter Schwabe
296
- * (https://cryptojedi.org/peter/data/chesrump-20130822.pdf) and
297
- * "Cache Attacks and Countermeasures: the Case of AES", RSA 2006,
298
- * by Dag Arne Osvik, Adi Shamir, and Eran Tromer
299
- * (http://www.tau.ac.il/~tromer/papers/cache.pdf)
300
- */
301
- secp256k1_ge_storage_cmov (& adds , & (* ctx -> prec )[j ][i ], i == bits );
302
- }
303
- secp256k1_ge_from_storage (& add , & adds );
304
- secp256k1_gej_add_ge (r , r , & add );
305
- }
306
- #endif
307
213
bits = 0 ;
308
214
secp256k1_ge_clear (& add );
309
215
memset (& adds , 0 , sizeof (adds ));
@@ -312,9 +218,7 @@ static void secp256k1_ecmult_gen(const secp256k1_ecmult_gen_context *ctx, secp25
312
218
313
219
/* Setup blinding values for secp256k1_ecmult_gen. */
314
220
static void secp256k1_ecmult_gen_blind (secp256k1_ecmult_gen_context * ctx , const unsigned char * seed32 ) {
315
- #if USE_COMB
316
221
int spacing ;
317
- #endif
318
222
secp256k1_scalar b ;
319
223
secp256k1_gej gb ;
320
224
secp256k1_fe s ;
@@ -327,13 +231,11 @@ static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const
327
231
secp256k1_gej_set_ge (& ctx -> initial , & secp256k1_ge_const_g );
328
232
secp256k1_gej_neg (& ctx -> initial , & ctx -> initial );
329
233
secp256k1_scalar_set_int (& ctx -> blind , 1 );
330
- #if USE_COMB
331
234
for (spacing = 1 ; spacing < COMB_SPACING ; ++ spacing ) {
332
235
secp256k1_scalar_add (& ctx -> blind , & ctx -> blind , & ctx -> blind );
333
236
}
334
237
#if COMB_OFFSET
335
238
secp256k1_gej_add_ge (& ctx -> initial , & ctx -> initial , & ctx -> offset );
336
- #endif
337
239
#endif
338
240
}
339
241
/* The prior blinding value (if not reset) is chained forward by including it in the hash. */
@@ -369,13 +271,11 @@ static void secp256k1_ecmult_gen_blind(secp256k1_ecmult_gen_context *ctx, const
369
271
secp256k1_scalar_negate (& b , & b );
370
272
ctx -> blind = b ;
371
273
ctx -> initial = gb ;
372
- #if USE_COMB
373
274
for (spacing = 1 ; spacing < COMB_SPACING ; ++ spacing ) {
374
275
secp256k1_scalar_add (& ctx -> blind , & ctx -> blind , & ctx -> blind );
375
276
}
376
277
#if COMB_OFFSET
377
278
secp256k1_gej_add_ge (& ctx -> initial , & ctx -> initial , & ctx -> offset );
378
- #endif
379
279
#endif
380
280
secp256k1_scalar_clear (& b );
381
281
secp256k1_gej_clear (& gb );
0 commit comments