forked from cryptape/libecc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
curve_basic_examples.c
346 lines (335 loc) · 10.5 KB
/
curve_basic_examples.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
/*
* Copyright (C) 2017 - This file is part of libecc project
*
* Authors:
* Ryad BENADJILA <[email protected]>
* Arnaud EBALARD <[email protected]>
* Jean-Pierre FLORI <[email protected]>
*
* Contributors:
* Nicolas VIVET <[email protected]>
* Karim KHALFALLAH <[email protected]>
*
* This software is licensed under a dual BSD and GPL v2 license.
* See LICENSE file at the root folder of the project.
*/
#include "libec.h"
/* We include the printf external dependency for printf output */
#include "print.h"
/* We include the time external dependency for performance measurement */
#include "time.h"
/* Declare our Tonelli-Shanks algorithm to find square roots
* in Fp, implemented in another module.
*/
int fp_square_root(fp_t sqrt1, fp_t sqrt2, fp_src_t n);
/* The followin function picks a random Fp element x, where Fp is the
* curve underlying prime field, and computes y in Fp such that:
* y^2 = x^3 + ax + b, where a and b are the input elliptic
* curve parameters.
*
* This means that (x, y) are the affine coordinates of a "random"
* point on our curve. The function then outputs the projective
* coordinates of (x, y), i.e. the triplet (x, y, 1).
* PS: all our operations on points are done with projective coordinates.
*
* Computing y means computing a quadratic residue in Fp, for which we
* use the Tonelli-Shanks algorithm implemented in the Fp source example
* (fp_square_residue.c).
*/
void get_random_point_on_curve(ec_params *curve_params, prj_pt *out_point);
void get_random_point_on_curve(ec_params *curve_params, prj_pt *out_point)
{
nn nn_tmp;
/* Inside our internal representation, curve_params->ec_curve
* contains the curve coefficients a and b.
* curve_params->ec_fp is the Fp context of the curve.
*/
fp x, y, fp_tmp1, fp_tmp2;
fp_ctx_src_t ctx;
/* Initialize our x value with the curve Fp context */
ctx = &(curve_params->ec_fp);
fp_init(&x, ctx);
fp_init(&y, ctx);
fp_init(&fp_tmp1, ctx);
fp_init(&fp_tmp2, ctx);
nn_init(&nn_tmp, 0);
nn_set_word_value(&nn_tmp, WORD(3));
while (1) {
/* Get a random Fp */
fp_get_random(&x, ctx);
fp_copy(&fp_tmp1, &x);
fp_copy(&fp_tmp2, &x);
/* Compute x^3 + ax + b */
fp_pow(&fp_tmp1, &fp_tmp1, &nn_tmp);
fp_mul(&fp_tmp2, &fp_tmp2, &(curve_params->ec_curve.a));
fp_add(&fp_tmp1, &fp_tmp1, &fp_tmp2);
fp_add(&fp_tmp1, &fp_tmp1, &(curve_params->ec_curve.b));
/*
* Get any of the two square roots, corresponding to (x, y)
* and (x, -y) both on the curve. If no square root exist,
* go to next random Fp.
*/
if (fp_square_root(&y, &fp_tmp2, &fp_tmp1) == 0) {
/* Check that we indeed satisfy the curve equation */
if (!is_on_curve(&x, &y, &(curve_params->ec_curve))) {
/* This should not happen ... */
ext_printf("Error: Tonelli-Shanks found a bad "
"solution to curve equation ...\n");
continue;
}
break;
}
}
/* Now initialize our point with the coordinates (x, y, 1) */
fp_one(&fp_tmp1);
prj_pt_init_from_coords(out_point, &(curve_params->ec_curve), &x, &y,
&fp_tmp1);
fp_uninit(&x);
fp_uninit(&y);
fp_uninit(&fp_tmp1);
fp_uninit(&fp_tmp2);
nn_uninit(&nn_tmp);
}
#define PERF_SCALAR_MUL 40
int check_curve(const u8 *curve_name);
int check_curve(const u8 *curve_name)
{
unsigned int i;
u64 t1, t2;
int ret = 0;
nn nn_k;
/* libecc internal structure holding the curve parameters */
ec_params curve_params;
/* libecc internal structure holding projective points on curves */
prj_pt A, B, C, D;
prj_pt TMP;
aff_pt T;
/* Importing a specific curve parameters from the constant static
* buffers describing it:
* It is possible to import a curves parameters by its name.
*/
const ec_str_params *the_curve_const_parameters =
ec_get_curve_params_by_name(curve_name,
(u8)local_strnlen((const char *)
curve_name,
MAX_CURVE_NAME_LEN)
+ 1);
/* Get out if getting the parameters went wrong */
if (the_curve_const_parameters == NULL) {
ext_printf("Error: error when importing curve %s "
"parameters ...\n", curve_name);
ret = -1;
goto out;
}
/* Now map the curve parameters to our libecc internal representation */
import_params(&curve_params, the_curve_const_parameters);
/* Get two random points on the curve */
get_random_point_on_curve(&curve_params, &A);
get_random_point_on_curve(&curve_params, &B);
/*
* Let's add the two points with our Montgomery and non Montgomery
* (regular) variants to check that both results represent the same
* point.
* C = A + B with regular point addition
* D = A + B with Montgomery point addition
*/
prj_pt_add(&C, &A, &B);
prj_pt_add_monty(&D, &A, &B);
if (prj_pt_cmp(&C, &D) != 0) {
ext_printf("Error: A+B differs with Montgomery and "
"non Montgomery add methods ...\n");
ret = -1;
goto out;
}
/*
* Check that the resulting additive point C = A+B is indeed on the
* curve. In order to check this, we have to go back to affine
* representation
*/
prj_pt_to_aff(&T, &C);
if (!is_on_curve(&(T.x), &(T.y), &(curve_params.ec_curve))) {
ext_printf("Error: C = A+B is not on the %s curve!\n",
curve_params.curve_name);
ret = -1;
goto out;
}
/* Same check with doubling
* C = 2A = A+A with regular point doubling
* D = 2A = A+A with Montgomery point doubling
*/
prj_pt_dbl(&C, &A);
prj_pt_dbl_monty(&D, &A);
if (prj_pt_cmp(&C, &D) != 0) {
ext_printf("Error: 2A differs with Montgomery and "
"non Montgomery add methods ...\n");
ret = -1;
goto out;
}
/* Check that the resulting point C = 2A is indeed on the curve.
* In order to check this, we have to go back to affine representation
*
*/
prj_pt_to_aff(&T, &C);
if (!is_on_curve(&(T.x), &(T.y), &(curve_params.ec_curve))) {
ext_printf("Error: C = A+B is not on the %s curve!\n",
curve_params.curve_name);
ret = -1;
goto out;
}
/*
* If the cofactor of the curve is 1, this means that the order of the
* generator is the cardinal of the curve (and hence the order of the
* curve points group). This means that for any point P on the curve,
* we should have qP = 0 (the inifinity point, i.e. the zero neutral
* element of the curve additive group). We test both Montgomery and
* non Montgomery methods to check this on our point A, B, C = A + B
* and D = 2A.
*/
prj_pt_add_monty(&C, &A, &B);
prj_pt_dbl_monty(&D, &A);
if (nn_isone(&(curve_params.ec_gen_cofactor))) {
prj_pt_mul(&TMP, &(curve_params.ec_gen_order), &A);
if (!prj_pt_iszero(&TMP)) {
ext_printf("Error: qA is not 0! (regular mul)\n");
ret = -1;
goto out;
}
prj_pt_mul_monty(&TMP, &(curve_params.ec_gen_order), &A);
if (!prj_pt_iszero(&TMP)) {
ext_printf("Error: qA is not 0! (Montgomery mul)\n");
ret = -1;
goto out;
}
prj_pt_mul(&TMP, &(curve_params.ec_gen_order), &B);
if (!prj_pt_iszero(&TMP)) {
ext_printf("Error: qB is not 0! (regular mul)\n");
ret = -1;
goto out;
}
prj_pt_mul_monty(&TMP, &(curve_params.ec_gen_order), &B);
if (!prj_pt_iszero(&TMP)) {
ext_printf("Error: qB is not 0! (Montgomery mul)\n");
ret = -1;
goto out;
}
prj_pt_mul(&TMP, &(curve_params.ec_gen_order), &C);
if (!prj_pt_iszero(&TMP)) {
ext_printf("Error: qC is not 0! (regular mul)\n");
ret = -1;
goto out;
}
prj_pt_mul_monty(&TMP, &(curve_params.ec_gen_order), &C);
if (!prj_pt_iszero(&TMP)) {
ext_printf("Error: qC is not 0! (Montgomery mul)\n");
ret = -1;
goto out;
}
prj_pt_mul(&TMP, &(curve_params.ec_gen_order), &D);
if (!prj_pt_iszero(&TMP)) {
ext_printf("Error: qD is not 0! (regular mul)\n");
ret = -1;
goto out;
}
prj_pt_mul_monty(&TMP, &(curve_params.ec_gen_order), &D);
if (!prj_pt_iszero(&TMP)) {
ext_printf("Error: qD is not 0! (Montgomery mul)\n");
ret = -1;
goto out;
}
}
/* Now let's show that even though they give the same results, our
* Montgomery variant for point addition and doubling are faster!
* We compute kA many times to have a decent performance measurement,
* where k is chose random at each iteration. We also check that kA
* is indeed on the curve.
*/
nn_init(&nn_k, 0);
if (get_ms_time(&t1)) {
ext_printf("Error: cannot get time with get_ms_time\n");
ret = -1;
goto out;
}
for (i = 0; i < PERF_SCALAR_MUL; i++) {
/* k = random mod (q) */
nn_get_random_mod(&nn_k, &(curve_params.ec_gen_order));
/* Compute kA with regular add/double formulas */
prj_pt_mul(&TMP, &nn_k, &A);
prj_pt_to_aff(&T, &TMP);
if (!is_on_curve(&(T.x), &(T.y), &(curve_params.ec_curve))) {
ext_printf("Error: kA is not on the %s curve!\n",
curve_params.curve_name);
nn_print("k=", &nn_k);
ret = -1;
goto out;
}
}
if (get_ms_time(&t2)) {
ext_printf("Error: cannot get time with get_ms_time\n");
ret = -1;
goto out;
}
ext_printf(" [*] Regular EC scalar multiplication took %f seconds "
"on average\n",
(double)(t2 - t1) / (double)(PERF_SCALAR_MUL * 1000ULL));
if (get_ms_time(&t1)) {
ext_printf("Error: cannot get time with get_ms_time\n");
ret = -1;
goto out;
}
for (i = 0; i < PERF_SCALAR_MUL; i++) {
/* k = random mod (q) */
nn_get_random_mod(&nn_k, &(curve_params.ec_gen_order));
/* Compute kA with Montgomery add/double formulas */
prj_pt_mul_monty(&TMP, &nn_k, &A);
prj_pt_to_aff(&T, &TMP);
if (!is_on_curve(&(T.x), &(T.y), &(curve_params.ec_curve))) {
ext_printf("Error: kA is not on the %s curve!\n",
curve_params.curve_name);
nn_print("k=", &nn_k);
ret = -1;
goto out;
}
}
if (get_ms_time(&t2)) {
ext_printf("Error: cannot get time with get_ms_time\n");
ret = -1;
goto out;
}
ext_printf(" [*] Montgomery EC scalar multiplication took %f seconds "
"on average\n",
(double)(t2 - t1) / (double)(PERF_SCALAR_MUL * 1000ULL));
prj_pt_uninit(&A);
prj_pt_uninit(&B);
prj_pt_uninit(&C);
prj_pt_uninit(&D);
prj_pt_uninit(&TMP);
aff_pt_uninit(&T);
nn_uninit(&nn_k);
out:
return ret;
}
#ifdef CURVE_BASIC_EXAMPLES
int main()
{
unsigned int i;
u8 curve_name[MAX_CURVE_NAME_LEN] = { 0 };
/* Traverse all the possible curves we have at our disposal (known curves and
* user defined curves).
*/
for (i = 0; i < EC_CURVES_NUM; i++) {
/* All our possible curves are in ../curves/curves_list.h
* We can get the curve name from its internal type.
*/
ec_get_curve_name_by_type(ec_maps[i].type, curve_name,
sizeof(curve_name));
/* Check our curve! */
ext_printf("[+] Checking curve %s\n", curve_name);
if (check_curve(curve_name)) {
ext_printf("Error: error performing check on "
"curve %s\n", curve_name);
return -1;
}
}
return 0;
}
#endif