1 /* crypto/ec/ectest.c */
2 /* ====================================================================
3 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@openssl.org.
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
62 int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); return 0; }
66 #include <openssl/ec.h>
67 #include <openssl/err.h>
71 fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \
72 ERR_print_errors_fp(stderr); \
77 void timings(EC_GROUP *group, int simult, BN_CTX *ctx)
86 if (s == NULL || s0 == NULL) ABORT;
88 if (!EC_GROUP_get_curve_GFp(group, s, NULL, NULL, ctx)) ABORT;
89 fprintf(stdout, "Timings for %d bit prime, ", (int)BN_num_bits(s));
90 if (!EC_GROUP_get_order(group, s, ctx)) ABORT;
91 fprintf(stdout, "%d bit scalars ", (int)BN_num_bits(s));
94 P = EC_POINT_new(group);
96 EC_POINT_copy(P, EC_GROUP_get0_generator(group));
99 for (i = 0; i < 10; i++)
101 if (!BN_pseudo_rand(s, BN_num_bits(s), 0, 0)) ABORT;
104 if (!BN_pseudo_rand(s0, BN_num_bits(s), 0, 0)) ABORT;
106 for (j = 0; j < 10; j++)
108 if (!EC_POINT_mul(group, P, s, simult ? P : NULL, simult ? s0 : NULL, ctx)) ABORT;
110 fprintf(stdout, ".");
113 fprintf(stdout, "\n");
115 clck = clock() - clck;
117 #ifdef CLOCKS_PER_SEC
118 /* "To determine the time in seconds, the value returned
119 * by the clock function should be divided by the value
120 * of the macro CLOCKS_PER_SEC."
124 /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
125 * -- cc on NeXTstep/OpenStep */
126 # define UNIT "units"
127 # define CLOCKS_PER_SEC 1
130 fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
131 simult ? "s*P+t*Q operations" : "point multiplications",
132 (double)clck/CLOCKS_PER_SEC);
133 fprintf(stdout, "average: %.4f " UNIT "\n", (double)clck/(CLOCKS_PER_SEC*i*j));
141 int main(int argc, char *argv[])
146 EC_GROUP *P_192 = NULL, *P_224 = NULL, *P_256 = NULL, *P_384 = NULL, *P_521 = NULL;
149 unsigned char buf[100];
152 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
153 ERR_load_crypto_strings();
163 if (!p || !a || !b) ABORT;
165 if (!BN_hex2bn(&p, "17")) ABORT;
166 if (!BN_hex2bn(&a, "1")) ABORT;
167 if (!BN_hex2bn(&b, "1")) ABORT;
169 group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use EC_GROUP_new_curve_GFp
170 * so that the library gets to choose the EC_METHOD */
173 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
177 tmp = EC_GROUP_new(EC_GROUP_method_of(group));
179 if (!EC_GROUP_copy(tmp, group));
180 EC_GROUP_free(group);
184 if (!EC_GROUP_get_curve_GFp(group, p, a, b, ctx)) ABORT;
186 fprintf(stdout, "Curve defined by Weierstrass equation\n y^2 = x^3 + a*x + b (mod 0x");
187 BN_print_fp(stdout, p);
188 fprintf(stdout, ")\n a = 0x");
189 BN_print_fp(stdout, a);
190 fprintf(stdout, "\n b = 0x");
191 BN_print_fp(stdout, b);
192 fprintf(stdout, "\n");
194 P = EC_POINT_new(group);
195 Q = EC_POINT_new(group);
196 R = EC_POINT_new(group);
197 if (!P || !Q || !R) ABORT;
199 if (!EC_POINT_set_to_infinity(group, P)) ABORT;
200 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
203 if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
205 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
206 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
211 if (!x || !y || !z) ABORT;
213 if (!BN_hex2bn(&x, "D")) ABORT;
214 if (!EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, ctx)) ABORT;
215 if (!EC_POINT_is_on_curve(group, Q, ctx))
217 if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, ctx)) ABORT;
218 fprintf(stderr, "Point is not on curve: x = 0x");
219 BN_print_fp(stderr, x);
220 fprintf(stderr, ", y = 0x");
221 BN_print_fp(stderr, y);
222 fprintf(stderr, "\n");
226 fprintf(stdout, "A cyclic subgroup:\n");
229 if (EC_POINT_is_at_infinity(group, P))
230 fprintf(stdout, " point at infinity\n");
233 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
235 fprintf(stdout, " x = 0x");
236 BN_print_fp(stdout, x);
237 fprintf(stdout, ", y = 0x");
238 BN_print_fp(stdout, y);
239 fprintf(stdout, "\n");
242 if (!EC_POINT_copy(R, P)) ABORT;
243 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
252 if (!EC_POINTs_make_affine(group, 2, points, ctx)) ABORT;
257 while (!EC_POINT_is_at_infinity(group, P));
259 if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
260 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
262 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
264 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
265 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
266 fprintf(stdout, "Generator as octect string, compressed form:\n ");
267 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
269 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
271 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
272 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
273 fprintf(stdout, "\nGenerator as octect string, uncompressed form:\n ");
274 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
276 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
278 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
279 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
280 fprintf(stdout, "\nGenerator as octect string, hybrid form:\n ");
281 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
283 if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx)) ABORT;
284 fprintf(stdout, "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n X = 0x");
285 BN_print_fp(stdout, x);
286 fprintf(stdout, ", Y = 0x");
287 BN_print_fp(stdout, y);
288 fprintf(stdout, ", Z = 0x");
289 BN_print_fp(stdout, z);
290 fprintf(stdout, "\n");
292 if (!EC_POINT_invert(group, P, ctx)) ABORT;
293 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
296 /* Curve P-192 (FIPS PUB 186-2, App. 6) */
298 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF")) ABORT;
299 if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
300 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC")) ABORT;
301 if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1")) ABORT;
302 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
304 if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012")) ABORT;
305 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
306 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
307 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831")) ABORT;
308 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
310 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
311 fprintf(stdout, "\nNIST curve P-192 -- Generator:\n x = 0x");
312 BN_print_fp(stdout, x);
313 fprintf(stdout, "\n y = 0x");
314 BN_print_fp(stdout, y);
315 fprintf(stdout, "\n");
316 /* G_y value taken from the standard: */
317 if (!BN_hex2bn(&z, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811")) ABORT;
318 if (0 != BN_cmp(y, z)) ABORT;
320 fprintf(stdout, "verify group order ...");
322 if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
323 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
324 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
325 fprintf(stdout, ".");
327 if (!EC_GROUP_precompute(group, ctx)) ABORT;
328 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
329 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
330 fprintf(stdout, " ok\n");
332 if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
333 if (!EC_GROUP_copy(P_192, group)) ABORT;
336 /* Curve P-224 (FIPS PUB 186-2, App. 6) */
338 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001")) ABORT;
339 if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
340 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE")) ABORT;
341 if (!BN_hex2bn(&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4")) ABORT;
342 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
344 if (!BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21")) ABORT;
345 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
346 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
347 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D")) ABORT;
348 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
350 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
351 fprintf(stdout, "\nNIST curve P-224 -- Generator:\n x = 0x");
352 BN_print_fp(stdout, x);
353 fprintf(stdout, "\n y = 0x");
354 BN_print_fp(stdout, y);
355 fprintf(stdout, "\n");
356 /* G_y value taken from the standard: */
357 if (!BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34")) ABORT;
358 if (0 != BN_cmp(y, z)) ABORT;
360 fprintf(stdout, "verify group order ...");
362 if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
363 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
364 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
365 fprintf(stdout, ".");
367 if (!EC_GROUP_precompute(group, ctx)) ABORT;
368 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
369 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
370 fprintf(stdout, " ok\n");
372 if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
373 if (!EC_GROUP_copy(P_224, group)) ABORT;
376 /* Curve P-256 (FIPS PUB 186-2, App. 6) */
378 if (!BN_hex2bn(&p, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
379 if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
380 if (!BN_hex2bn(&a, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
381 if (!BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B")) ABORT;
382 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
384 if (!BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296")) ABORT;
385 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
386 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
387 if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E"
388 "84F3B9CAC2FC632551")) ABORT;
389 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
391 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
392 fprintf(stdout, "\nNIST curve P-256 -- Generator:\n x = 0x");
393 BN_print_fp(stdout, x);
394 fprintf(stdout, "\n y = 0x");
395 BN_print_fp(stdout, y);
396 fprintf(stdout, "\n");
397 /* G_y value taken from the standard: */
398 if (!BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5")) ABORT;
399 if (0 != BN_cmp(y, z)) ABORT;
401 fprintf(stdout, "verify group order ...");
403 if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
404 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
405 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
406 fprintf(stdout, ".");
408 if (!EC_GROUP_precompute(group, ctx)) ABORT;
409 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
410 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
411 fprintf(stdout, " ok\n");
413 if (!(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
414 if (!EC_GROUP_copy(P_256, group)) ABORT;
417 /* Curve P-384 (FIPS PUB 186-2, App. 6) */
419 if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
420 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF")) ABORT;
421 if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
422 if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
423 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC")) ABORT;
424 if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141"
425 "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF")) ABORT;
426 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
428 if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B"
429 "9859F741E082542A385502F25DBF55296C3A545E3872760AB7")) ABORT;
430 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
431 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
432 if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
433 "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973")) ABORT;
434 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
436 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
437 fprintf(stdout, "\nNIST curve P-384 -- Generator:\n x = 0x");
438 BN_print_fp(stdout, x);
439 fprintf(stdout, "\n y = 0x");
440 BN_print_fp(stdout, y);
441 fprintf(stdout, "\n");
442 /* G_y value taken from the standard: */
443 if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14"
444 "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F")) ABORT;
445 if (0 != BN_cmp(y, z)) ABORT;
447 fprintf(stdout, "verify group order ...");
449 if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
450 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
451 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
452 fprintf(stdout, ".");
454 if (!EC_GROUP_precompute(group, ctx)) ABORT;
455 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
456 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
457 fprintf(stdout, " ok\n");
459 if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
460 if (!EC_GROUP_copy(P_384, group)) ABORT;
463 /* Curve P-521 (FIPS PUB 186-2, App. 6) */
465 if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
466 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
467 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
468 if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
469 if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
470 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
471 "FFFFFFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
472 if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B"
473 "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573"
474 "DF883D2C34F1EF451FD46B503F00")) ABORT;
475 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
477 if (!BN_hex2bn(&x, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F"
478 "B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B"
479 "3C1856A429BF97E7E31C2E5BD66")) ABORT;
480 if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
481 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
482 if (!BN_hex2bn(&z, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
483 "FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5"
484 "C9B8899C47AEBB6FB71E91386409")) ABORT;
485 if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
487 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
488 fprintf(stdout, "\nNIST curve P-521 -- Generator:\n x = 0x");
489 BN_print_fp(stdout, x);
490 fprintf(stdout, "\n y = 0x");
491 BN_print_fp(stdout, y);
492 fprintf(stdout, "\n");
493 /* G_y value taken from the standard: */
494 if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579"
495 "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C"
496 "7086A272C24088BE94769FD16650")) ABORT;
497 if (0 != BN_cmp(y, z)) ABORT;
499 fprintf(stdout, "verify group order ...");
501 if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
502 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
503 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
504 fprintf(stdout, ".");
506 if (!EC_GROUP_precompute(group, ctx)) ABORT;
507 if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
508 if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
509 fprintf(stdout, " ok\n");
511 if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
512 if (!EC_GROUP_copy(P_521, group)) ABORT;
515 /* more tests using the last curve */
517 if (!EC_POINT_copy(Q, P)) ABORT;
518 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
519 if (!EC_POINT_dbl(group, P, P, ctx)) ABORT;
520 if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
521 if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */
523 if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT;
524 if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT;
525 if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */
528 const EC_POINT *points[3];
529 const BIGNUM *scalars[3];
531 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
536 if (!BN_add(y, z, BN_value_one())) ABORT;
537 if (BN_is_odd(y)) ABORT;
538 if (!BN_rshift1(y, y)) ABORT;
539 scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
542 fprintf(stdout, "simultaneous multiplication ...");
545 /* z is still the group order */
546 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
547 if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT;
548 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
549 if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT;
551 fprintf(stdout, ".");
554 if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
555 if (!BN_copy(z, y)) ABORT;
558 scalars[1] = z; /* z = -y */
560 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
561 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
563 fprintf(stdout, ".");
566 if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
567 if (!BN_add(z, x, y)) ABORT;
571 scalars[2] = z; /* z = -(x+y) */
573 if (!EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx)) ABORT;
574 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
576 fprintf(stdout, " ok\n\n");
581 timings(P_192, 0, ctx);
582 timings(P_192, 1, ctx);
583 timings(P_224, 0, ctx);
584 timings(P_224, 1, ctx);
585 timings(P_256, 0, ctx);
586 timings(P_256, 1, ctx);
587 timings(P_384, 0, ctx);
588 timings(P_384, 1, ctx);
589 timings(P_521, 0, ctx);
590 timings(P_521, 1, ctx);
596 BN_free(p); BN_free(a); BN_free(b);
597 EC_GROUP_free(group);
601 BN_free(x); BN_free(y); BN_free(z);
603 if (P_192) EC_GROUP_free(P_192);
604 if (P_224) EC_GROUP_free(P_224);
605 if (P_256) EC_GROUP_free(P_256);
606 if (P_384) EC_GROUP_free(P_384);
607 if (P_521) EC_GROUP_free(P_521);
611 CRYPTO_mem_leaks_fp(stderr);