-
Notifications
You must be signed in to change notification settings - Fork 6
/
ec_ops.c
401 lines (319 loc) · 9.5 KB
/
ec_ops.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* arithECC.c
*
* Created on: Sep 26, 2015
* Author: tslld
*/
#include "ecdsa.h"
#include "ec_point.h"
#include "field_ops.h"
/* Add two points P and Q in affine coordinates. If P = Q, perform a doubling, but in atomic principle
*
* Require:
*
* Input: P, Q
* Output: R = P + Q. If P = Q, R = 2P
*
* ec: the elliptic curve
*
*/
ec_point ec_point_add_atomic(ec_point P, ec_point Q, ec_group ec) {
mpz_t field;
mpz_init_set(field, ec->field);
ec_point R;
// Initialize the point R
R = ec_point_init();
//If Q is at infinity, set R to P
if(Q->infinity) {
ec_point_cpy(R, P);
} else
if(P->infinity){ //If P is at infinity set R to be Q
ec_point_cpy(R, Q);
} else
if(ec_point_is_inverse(P, Q, field)) { // If P = -Q, return R be the point at infinity
ec_point_set_at_infinity(R);
} else {
//Initialize slope variable lambda
mpz_t lambda; mpz_init(lambda);
//Initialize temporary variables
mpz_t t1; mpz_init_set_str(t1, "1", 10);
mpz_t t2; mpz_init(t2);
mpz_t t3; mpz_init(t3);
// Calculate lambda
if (ec_point_cmp(P,Q, field)) { //If the points are the same use point doubling
//R = point_doubling(P, ec);
mod_sec_sqr(t1, P->x, field);
mod_addadd(t2, t1, t1, t1, field);
mod_add(t3, t2, ec->A, field);
mod_add(t1,P->y,P->y, field);
} else {
// Calculate lambda: lambda = (Py - Qy)/(Px-Qx) mod p
mod_sec_sqr(t1, t1, field); // dummy operation
mod_addadd(t2, t1, t1, t1, field); // dummy operation
mod_sub(t3, P->y, Q->y, field);
mod_sub(t1, P->x, Q->x, field);
}
mod_sec_invert(t2, t1, field);
mod_sec_mul(lambda, t3, t2, field);
// Rx = lambda^2 - Px - Qx
mod_sec_sqr(t1, lambda, field);
mod_subsub(R->x, t1, P->x, Q->x, field);
// Ry = lambda(Px - Rx) - Py mod p
mod_sub(t1, P->x, R->x, field);
mod_mulsub(R->y, t1, lambda, P->y, field);
//Clear variables, release memory
mpz_clear(t1); mpz_clear(t2); mpz_clear(t3); mpz_clear(lambda);
}
return R;
}
ec_point ec_point_dbl(ec_point P, ec_group ec) {
mpz_t field;
mpz_init_set(field, ec->field);
ec_point R;
// Initialize the point R
R = ec_point_init();
//If Q is at infinity, set R to P
if(P->infinity) {
ec_point_set_at_infinity(R);
} else {
//Initialize slope variable lambda
mpz_t lambda; mpz_init(lambda);
//Initialize temporary variables
mpz_t t1; mpz_init_set_ui(t1, 1);
mpz_t t2; mpz_init(t2);
mpz_t t3; mpz_init(t3);
// Calculate lambda = (3Px^2 + a)/2Py
mod_sec_sqr(t1, P->x, field);
mod_addadd(t2, t1, t1, t1, field);
mod_add(t3, t2, ec->A, field);
mod_add(t1,P->y,P->y, field);
mod_sec_invert(t2, t1, field);
mod_sec_mul(lambda, t3, t2, field);
// Rx = lambda^2 - 2Px
mod_sec_sqr(t1, lambda, field);
mod_subsub(R->x, t1, P->x, P->x, field);
// Ry = lambda(Px - Rx) - Py mod p
mod_sub(t1, P->x, R->x, field);
mod_mulsub(R->y, t1, lambda, P->y, field);
//Clear variables, release memory
mpz_clear(t1); mpz_clear(t2); mpz_clear(t3); mpz_clear(lambda);
}
return R;
}
ec_point ec_point_add(ec_point P, ec_point Q, ec_group ec) {
mpz_t field;
mpz_init_set(field, ec->field);
ec_point R;
// Initialize the point R
R = ec_point_init();
//If Q is at infinity, set R to P
if(Q->infinity) {
ec_point_cpy(R, P);
} else
if(P->infinity){ //If P is at infinity set R to be Q
ec_point_cpy(R, Q);
} else
if(ec_point_is_inverse(P, Q, field)) { // If P = -Q, return R be the point at infinity
ec_point_set_at_infinity(R);
} else {
//Initialize slope variable lambda
mpz_t lambda; mpz_init(lambda);
//Initialize temporary variables
mpz_t t1; mpz_init(t1);
mpz_t t2; mpz_init(t2);
// Calculate lambda: lambda = (Py - Qy)/(Px-Qx) mod p
mod_sub(t1, P->x, Q->x, field);
mod_sec_invert(t2, t1, field);
mod_sub(t1, P->y, Q->y, field);
mod_sec_mul(lambda, t1, t2, field);
// Rx = lambda^2 - Px - Qx
mod_sec_sqr(t1, lambda, field);
mod_subsub(R->x, t1, P->x, Q->x, field);
// Ry = lambda(Px - Rx) - Py mod p
mod_sub(t1, P->x, R->x, field);
mod_mulsub(R->y, t1, lambda, P->y, field);
//Clear variables, release memory
mpz_clear(t1); mpz_clear(t2); mpz_clear(lambda);
}
return R;
}
/** Perform scalar multiplication to P, with the factor scalar on the curve curve EC
*
*/
ec_point ecp_mul_atomic(ec_point P, mpz_t scalar, ec_group group) {
ec_point Rop = ec_point_init();
// Initialize R as the point at infinity, the neutral element of the group
ec_point_set_at_infinity(Rop);
if(!P->infinity) {
//Initializing variables
unsigned int k, b;
ec_point R[2];
R[0] = ec_point_init(); R[0]->infinity = true;
R[1] = ec_point_dup(P);
k = mpz_sizeinbase(scalar, 2);
int i = k - 1; b = 0;
while (i >= 0) {
R[0] = ec_point_add_atomic(R[0], R[b], group);
b = b ^ mpz_tstbit(scalar, i);
i -= (1 - b);
}
ec_point_cpy(Rop, R[0]);
//Release temporary variables
ec_point_free(R[0]);
ec_point_free(R[1]);
}
return Rop;
}
ec_point ecp_mul_montgomery(ec_point P, mpz_t scalar, ec_group group) {
ec_point Rop = ec_point_init();
// Initialize R as the point at infinity, the neutral element of the group
ec_point_set_at_infinity(Rop);
if(!P->infinity) {
//Initializing variables
int i, bit, ibit;
ec_point R[2];
R[0] = ec_point_init(); R[0]->infinity = true;
R[1] = ec_point_dup(P);
int k = mpz_sizeinbase(scalar, 2);
for(i = k - 1; i >= 0; i--) {
bit = mpz_tstbit(scalar, i); ibit = bit ^ 0x1;
R[ibit] = ec_point_add(R[bit], R[ibit], group);
R[bit] = ec_point_dbl(R[bit], group);
}
ec_point_cpy(Rop, R[0]);
//Release temporary variables
ec_point_free(R[0]);
ec_point_free(R[1]);
}
return Rop;
}
/** Return a random bit
*
*/
static int coin_toss() {
return rand() & 0x1;
}
/** Compute modular exponentiation using repeated Montgomery powering ladder.
* This algorithm can be used as a countermeasures to thwart timing, simple side-channel analysis and fault attack.
* Proposed by Coron at CHES 1999
*
* @return: rop = base ^ exp mod N. Assume that base, exp, N > 0
*
*/
ec_point ecp_mul_rand_montgomery(ec_point P, mpz_t scalar, ec_group group){
ec_point Rop = ec_point_init();
// Initialize R as the point at infinity, the neutral element of the group
ec_point_set_at_infinity(Rop);
if(!P->infinity) {
//Initializing variables
int i, k, bit, randbit, irandbit;
ec_point R[2];
R[0] = ec_point_init(); R[0]->infinity = true;
srand(time(NULL)); // <- srand() here, just ONCE
randbit = coin_toss();
if (! randbit)
R[1] = ec_point_dup(P);
else {
R[1] = ec_point_init();
R[1]->infinity = true;
}
k = mpz_sizeinbase(scalar, 2);
for(i = k - 1; i >= 0; i--) {
bit = mpz_tstbit(scalar, i);
if (bit ^ randbit)
randbit = coin_toss();
irandbit = randbit ^ 0x1;
R[randbit] = ec_point_add_atomic(R[0], R[randbit ^ bit], group);
R[irandbit] = ec_point_add_atomic(R[randbit], P, group);
}
ec_point_cpy(Rop, R[0]);
//Release temporary variables
ec_point_free(R[0]);
ec_point_free(R[1]);
}
return Rop;
}
/* Perform scalar multiplication to P, with the factor scalar on the curve curve EC
*
* Using the window method, but in silence mode to cached-based timing attacks on pre-computed table
*
*/
ec_point ec_sec_wmul(ec_point P, mpz_t scalar, ec_group ec) {
ec_point Ret = ec_point_init();
// Initialize R as the point at infinity, the neutral element of the group
ec_point_set_at_infinity(Ret);
if(!P->infinity) {
//Initializing variables
unsigned int i, k, b;
ec_point R[2];
R[0] = ec_point_init(); R[0]->infinity = true;
R[1] = ec_point_dup(P); //point_copy(t, x);
k = mpz_sizeinbase(scalar, 2);
i = k - 1; b = 0;
while(i >= 0) {
R[0] = ec_point_add_atomic(R[0], R[b], ec);
b = b ^ mpz_tstbit(scalar, i);
i -= (1 - b);
}
ec_point_cpy(Ret, R[0]);
//Release temporary variables
ec_point_free(R[0]);
ec_point_free(R[1]);
}
return Ret;
}
/* Doubling a point in affine coordinates (Z = 1) on elliptic curve ec
*
* Input: point P
* Output: R = 2P
*
* ec: elliptic curve
*/
/*
pt_point point_doubling(pt_point P, pt_curve ec) {
pt_point R; R = point_init();
//If at infinity
if(P->infinity) {
R->infinity = true;
} else {
//Initialize slope variable
mpz_t s;mpz_init(s);
//Initialize temporary variables
mpz_t t1;mpz_init(t1);
mpz_t t2;mpz_init(t2);
mpz_t t3;mpz_init(t3);
mpz_t t4;mpz_init(t4);
mpz_t t5;mpz_init(t5);
//Calculate slope
//s = (3*Px² + a) / (2*Py) mod p
number_theory_exp_modp_ui(t1, P->X, 2, ec->p); //t1 = Px² mod p
mpz_mul_ui(t2, t1, 3); //t2 = 3 * t1
mpz_mod(t3, t2, ec->p); //t3 = t2 mod p
mpz_add(t4, t3, ec->A); //t4 = t3 + a
mpz_mod(t5, t4, ec->p); //t5 = t4 mod p
mpz_mul_ui(t1, P->Y, 2); //t1 = 2*Py
number_theory_inverse(t2, t1, ec->p); //t2 = t1^-1 mod p
mpz_mul(t1, t5, t2); //t1 = t5 * t2
mpz_mod(s, t1, ec->p); //s = t1 mod p
//Calculate Rx
//Rx = s² - 2*Px mod p
number_theory_exp_modp_ui(t1, s, 2, ec->p);//t1 = s² mod p
mpz_mul_ui(t2, P->X, 2); //t2 = Px*2
mpz_mod(t3, t2, ec->p); //t3 = t2 mod p
mpz_sub(t4, t1, t3); //t4 = t1 - t3
mpz_mod(R->X, t4, ec->p); //Rx = t4 mod p
//Calculate Ry using algorithm shown to the right of the commands
//Ry = s(Px-Rx) - Py mod p
mpz_sub(t1, P->X, R->X); //t1 = Px - Rx
mpz_mul(t2, s, t1); //t2 = s*t1
mpz_sub(t3, t2, P->Y); //t3 = t2 - Py
mpz_mod(R->Y, t3, ec->p); //Ry = t3 mod p
//Clear variables, release memory
mpz_clear(t1);
mpz_clear(t2);
mpz_clear(t3);
mpz_clear(t4);
mpz_clear(t5);
mpz_clear(s);
}
}*/