More EC stuff, including EC_POINTs_mul() for simultaneous scalar
[openssl.git] / crypto / ec / ectest.c
1 /* crypto/ec/ectest.c */
2 /* ====================================================================
3  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer. 
11  *
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
15  *    distribution.
16  *
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/)"
21  *
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.
26  *
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.
30  *
31  * 6. Redistributions of any form whatsoever must retain the following
32  *    acknowledgment:
33  *    "This product includes software developed by the OpenSSL Project
34  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35  *
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  * ====================================================================
49  *
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).
53  *
54  */
55
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <time.h>
59
60
61 #ifdef OPENSSL_NO_EC
62 int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); return 0; }
63 #else
64
65
66 #include <openssl/ec.h>
67 #include <openssl/err.h>
68
69 #define ABORT do { \
70         fflush(stdout); \
71         fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \
72         ERR_print_errors_fp(stderr); \
73         exit(1); \
74 } while (0)
75
76
77 void timings(EC_GROUP *group, int simult, BN_CTX *ctx)
78         {
79         clock_t clck;
80         int i, j;
81         BIGNUM *s, *s0;
82         EC_POINT *P;
83         EC_POINT *points[1];
84         BIGNUM *scalars[1];
85                 
86         s = BN_new();
87         s0 = BN_new();
88         if (s == NULL || s0 == NULL) ABORT;
89
90         if (!EC_GROUP_get_curve_GFp(group, s, NULL, NULL, ctx)) ABORT;
91         fprintf(stdout, "Timings for %d bit prime, ", (int)BN_num_bits(s));
92         if (!EC_GROUP_get_order(group, s, ctx)) ABORT;
93         fprintf(stdout, "%d bit exponents ", (int)BN_num_bits(s));
94         fflush(stdout);
95
96         P = EC_POINT_new(group);
97         if (P == NULL) ABORT;
98         EC_POINT_copy(P, EC_GROUP_get0_generator(group));
99
100         points[0] = P;
101         scalars[0] = s0;
102
103         clck = clock();
104         for (i = 0; i < 10; i++)
105                 {
106                 if (!BN_pseudo_rand(s, BN_num_bits(s), 0, 0)) ABORT;
107                 if (simult)
108                         {
109                         if (!BN_pseudo_rand(s0, BN_num_bits(s), 0, 0)) ABORT;
110                         }
111                 for (j = 0; j < 10; j++)
112                         {
113                         if (!EC_POINTs_mul(group, P, s, simult != 0, points, scalars, ctx)) ABORT;
114                         }
115                 fprintf(stdout, ".");
116                 fflush(stdout);
117                 }
118         fprintf(stdout, "\n");
119         
120         clck = clock() - clck;
121
122 #ifdef CLOCKS_PER_SEC
123         /* "To determine the time in seconds, the value returned
124          * by the clock function should be divided by the value
125          * of the macro CLOCKS_PER_SEC."
126          *                                       -- ISO/IEC 9899 */
127 #       define UNIT "s"
128 #else
129         /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
130          *                            -- cc on NeXTstep/OpenStep */
131 #       define UNIT "units"
132 #       define CLOCKS_PER_SEC 1
133 #endif
134
135         fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
136                 simult ? "s*P+t*Q operations" : "point multiplications",
137                 (double)clck/CLOCKS_PER_SEC);
138         fprintf(stdout, "average: %.4f " UNIT "\n", (double)clck/(CLOCKS_PER_SEC*i*j));
139
140         EC_POINT_free(P);
141         BN_free(s);
142         BN_free(s0);
143         }
144
145
146 int main(int argc, char *argv[])
147         {       
148         BN_CTX *ctx = NULL;
149         BIGNUM *p, *a, *b;
150         EC_GROUP *group;
151         EC_GROUP *P_192 = NULL, *P_224 = NULL, *P_256 = NULL, *P_384 = NULL, *P_521 = NULL;
152         EC_POINT *P, *Q, *R;
153         BIGNUM *x, *y, *z;
154         unsigned char buf[100];
155         size_t i, len;
156         
157         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
158         ERR_load_crypto_strings();
159
160 #if 1 /* optional */
161         ctx = BN_CTX_new();
162         if (!ctx) ABORT;
163 #endif
164
165         p = BN_new();
166         a = BN_new();
167         b = BN_new();
168         if (!p || !a || !b) ABORT;
169
170         if (!BN_hex2bn(&p, "17")) ABORT;
171         if (!BN_hex2bn(&a, "1")) ABORT;
172         if (!BN_hex2bn(&b, "1")) ABORT;
173         
174         group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use EC_GROUP_new_curve_GFp
175                                                      * so that the library gets to choose the EC_METHOD */
176         if (!group) ABORT;
177
178         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
179
180         {
181                 EC_GROUP *tmp;
182                 tmp = EC_GROUP_new(EC_GROUP_method_of(group));
183                 if (!tmp) ABORT;
184                 if (!EC_GROUP_copy(tmp, group));
185                 EC_GROUP_free(group);
186                 group = tmp;
187         }
188         
189         if (!EC_GROUP_get_curve_GFp(group, p, a, b, ctx)) ABORT;
190
191         fprintf(stdout, "Curve defined by Weierstrass equation\n     y^2 = x^3 + a*x + b  (mod 0x");
192         BN_print_fp(stdout, p);
193         fprintf(stdout, ")\n     a = 0x");
194         BN_print_fp(stdout, a);
195         fprintf(stdout, "\n     b = 0x");
196         BN_print_fp(stdout, b);
197         fprintf(stdout, "\n");
198
199         P = EC_POINT_new(group);
200         Q = EC_POINT_new(group);
201         R = EC_POINT_new(group);
202         if (!P || !Q || !R) ABORT;
203         
204         if (!EC_POINT_set_to_infinity(group, P)) ABORT;
205         if (!EC_POINT_is_at_infinity(group, P)) ABORT;
206
207         buf[0] = 0;
208         if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
209
210         if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
211         if (!EC_POINT_is_at_infinity(group, P)) ABORT;
212
213         x = BN_new();
214         y = BN_new();
215         z = BN_new();
216         if (!x || !y || !z) ABORT;
217
218         if (!BN_hex2bn(&x, "D")) ABORT;
219         if (!EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, ctx)) ABORT;
220         if (!EC_POINT_is_on_curve(group, Q, ctx))
221                 {
222                 if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, ctx)) ABORT;
223                 fprintf(stderr, "Point is not on curve: x = 0x");
224                 BN_print_fp(stderr, x);
225                 fprintf(stderr, ", y = 0x");
226                 BN_print_fp(stderr, y);
227                 fprintf(stderr, "\n");
228                 ABORT;
229                 }
230
231         fprintf(stdout, "A cyclic subgroup:\n");
232         do
233                 {
234                 if (EC_POINT_is_at_infinity(group, P))
235                         fprintf(stdout, "     point at infinity\n");
236                 else
237                         {
238                         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
239
240                         fprintf(stdout, "     x = 0x");
241                         BN_print_fp(stdout, x);
242                         fprintf(stdout, ", y = 0x");
243                         BN_print_fp(stdout, y);
244                         fprintf(stdout, "\n");
245                         }
246                 
247                 if (!EC_POINT_copy(R, P)) ABORT;
248                 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
249
250 #if 0 /* optional */
251                 {
252                         EC_POINT *points[3];
253                 
254                         points[0] = R;
255                         points[1] = Q;
256                         points[2] = P;
257                         if (!EC_POINTs_make_affine(group, 2, points, ctx)) ABORT;
258                 }
259 #endif
260
261                 }
262         while (!EC_POINT_is_at_infinity(group, P));
263
264         if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
265         if (!EC_POINT_is_at_infinity(group, P)) ABORT;
266
267         len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
268         if (len == 0) ABORT;
269         if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
270         if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
271         fprintf(stdout, "Generator as octect string, compressed form:\n     ");
272         for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
273         
274         len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
275         if (len == 0) ABORT;
276         if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
277         if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
278         fprintf(stdout, "\nGenerator as octect string, uncompressed form:\n     ");
279         for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
280         
281         len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
282         if (len == 0) ABORT;
283         if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
284         if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
285         fprintf(stdout, "\nGenerator as octect string, hybrid form:\n     ");
286         for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
287         
288         if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx)) ABORT;
289         fprintf(stdout, "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n     X = 0x");
290         BN_print_fp(stdout, x);
291         fprintf(stdout, ", Y = 0x");
292         BN_print_fp(stdout, y);
293         fprintf(stdout, ", Z = 0x");
294         BN_print_fp(stdout, z);
295         fprintf(stdout, "\n");
296
297         if (!EC_POINT_invert(group, P, ctx)) ABORT;
298         if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
299
300
301         /* Curve P-192 (FIPS PUB 186-2, App. 6) */
302         
303         if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF")) ABORT;
304         if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
305         if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC")) ABORT;
306         if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1")) ABORT;
307         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
308
309         if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012")) ABORT;
310         if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
311         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
312         if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831")) ABORT;
313         if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
314
315         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
316         fprintf(stdout, "\nNIST curve P-192 -- Generator:\n     x = 0x");
317         BN_print_fp(stdout, x);
318         fprintf(stdout, "\n     y = 0x");
319         BN_print_fp(stdout, y);
320         fprintf(stdout, "\n");
321         /* G_y value taken from the standard: */
322         if (!BN_hex2bn(&z, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811")) ABORT;
323         if (0 != BN_cmp(y, z)) ABORT;
324         
325         fprintf(stdout, "verify group order ... ");
326         if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
327         if (!EC_POINTs_mul(group, Q, z, 0, NULL, NULL, ctx)) ABORT;
328         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
329         fprintf(stdout, "ok\n");
330
331         if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
332         if (!EC_GROUP_copy(P_192, group)) ABORT;
333
334
335         /* Curve P-224 (FIPS PUB 186-2, App. 6) */
336         
337         if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001")) ABORT;
338         if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
339         if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE")) ABORT;
340         if (!BN_hex2bn(&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4")) ABORT;
341         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
342
343         if (!BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21")) ABORT;
344         if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
345         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
346         if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D")) ABORT;
347         if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
348
349         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
350         fprintf(stdout, "\nNIST curve P-224 -- Generator:\n     x = 0x");
351         BN_print_fp(stdout, x);
352         fprintf(stdout, "\n     y = 0x");
353         BN_print_fp(stdout, y);
354         fprintf(stdout, "\n");
355         /* G_y value taken from the standard: */
356         if (!BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34")) ABORT;
357         if (0 != BN_cmp(y, z)) ABORT;
358         
359         fprintf(stdout, "verify group order ... ");
360         if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
361         if (!EC_POINTs_mul(group, Q, z, 0, NULL, NULL, ctx)) ABORT;
362         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
363         fprintf(stdout, "ok\n");
364         
365         if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
366         if (!EC_GROUP_copy(P_224, group)) ABORT;
367
368
369         /* Curve P-256 (FIPS PUB 186-2, App. 6) */
370         
371         if (!BN_hex2bn(&p, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
372         if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
373         if (!BN_hex2bn(&a, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
374         if (!BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B")) ABORT;
375         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
376
377         if (!BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296")) ABORT;
378         if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
379         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
380         if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E"
381                 "84F3B9CAC2FC632551")) ABORT;
382         if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
383
384         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
385         fprintf(stdout, "\nNIST curve P-256 -- Generator:\n     x = 0x");
386         BN_print_fp(stdout, x);
387         fprintf(stdout, "\n     y = 0x");
388         BN_print_fp(stdout, y);
389         fprintf(stdout, "\n");
390         /* G_y value taken from the standard: */
391         if (!BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5")) ABORT;
392         if (0 != BN_cmp(y, z)) ABORT;
393         
394         fprintf(stdout, "verify group order ... ");
395         if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
396         if (!EC_POINTs_mul(group, Q, z, 0, NULL, NULL, ctx)) ABORT;
397         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
398         fprintf(stdout, "ok\n");
399         
400         if (!(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
401         if (!EC_GROUP_copy(P_256, group)) ABORT;
402
403
404         /* Curve P-384 (FIPS PUB 186-2, App. 6) */
405         
406         if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
407                 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF")) ABORT;
408         if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
409         if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
410                 "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC")) ABORT;
411         if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141"
412                 "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF")) ABORT;
413         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
414
415         if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B"
416                 "9859F741E082542A385502F25DBF55296C3A545E3872760AB7")) ABORT;
417         if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
418         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
419         if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
420                 "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973")) ABORT;
421         if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
422
423         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
424         fprintf(stdout, "\nNIST curve P-384 -- Generator:\n     x = 0x");
425         BN_print_fp(stdout, x);
426         fprintf(stdout, "\n     y = 0x");
427         BN_print_fp(stdout, y);
428         fprintf(stdout, "\n");
429         /* G_y value taken from the standard: */
430         if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14"
431                 "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F")) ABORT;
432         if (0 != BN_cmp(y, z)) ABORT;
433         
434         fprintf(stdout, "verify group order ... ");
435         if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
436         if (!EC_POINTs_mul(group, Q, z, 0, NULL, NULL, ctx)) ABORT;
437         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
438         fprintf(stdout, "ok\n");
439         
440         if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
441         if (!EC_GROUP_copy(P_384, group)) ABORT;
442
443
444         /* Curve P-521 (FIPS PUB 186-2, App. 6) */
445         
446         if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
447                 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
448                 "FFFFFFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
449         if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT;
450         if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
451                 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
452                 "FFFFFFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
453         if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B"
454                 "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573"
455                 "DF883D2C34F1EF451FD46B503F00")) ABORT;
456         if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
457
458         if (!BN_hex2bn(&x, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F"
459                 "B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B"
460                 "3C1856A429BF97E7E31C2E5BD66")) ABORT;
461         if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
462         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
463         if (!BN_hex2bn(&z, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
464                 "FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5"
465                 "C9B8899C47AEBB6FB71E91386409")) ABORT;
466         if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
467
468         if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
469         fprintf(stdout, "\nNIST curve P-521 -- Generator:\n     x = 0x");
470         BN_print_fp(stdout, x);
471         fprintf(stdout, "\n     y = 0x");
472         BN_print_fp(stdout, y);
473         fprintf(stdout, "\n");
474         /* G_y value taken from the standard: */
475         if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579"
476                 "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C"
477                 "7086A272C24088BE94769FD16650")) ABORT;
478         if (0 != BN_cmp(y, z)) ABORT;
479         
480         fprintf(stdout, "verify group order ... ");
481         if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
482         if (!EC_POINTs_mul(group, Q, z, 0, NULL, NULL, ctx)) ABORT;
483         if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
484         fprintf(stdout, "ok\n");
485
486         if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
487         if (!EC_GROUP_copy(P_521, group)) ABORT;
488
489
490         /* more tests using the last curve */
491
492         if (!EC_POINT_copy(Q, P)) ABORT;
493         if (EC_POINT_is_at_infinity(group, Q)) ABORT;
494         if (!EC_POINT_dbl(group, P, P, ctx)) ABORT;
495         if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
496         if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */
497
498         if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT;
499         if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT;
500         if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */
501
502         {
503                 EC_POINT *points[2];
504                 BIGNUM *scalars[2];
505         
506                 if (EC_POINT_is_at_infinity(group, Q)) ABORT;
507
508                 if (!BN_add(y, z, BN_value_one())) ABORT;
509                 if (BN_is_odd(y)) ABORT;
510                 if (!BN_rshift1(y, y)) ABORT;
511
512                 points[0] = Q;
513                 points[1] = Q;
514                 scalars[0] = y; /* (group order + 1)/2,  so  y*Q + y*Q = Q */
515                 scalars[1] = y;
516
517                 fprintf(stdout, "simultaneous multiplication ... ");
518
519                 /* z is still the group order */
520                 if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
521                 if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT;
522                 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
523                 if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT;
524
525                 fprintf(stdout, "ok\n\n");
526         }
527
528
529 #if 0
530         timings(P_192, 0, ctx);
531         timings(P_192, 1, ctx);
532 #endif
533
534
535         if (ctx)
536                 BN_CTX_free(ctx);
537         BN_free(p); BN_free(a); BN_free(b);
538         EC_GROUP_free(group);
539         EC_POINT_free(P);
540         EC_POINT_free(Q);
541         EC_POINT_free(R);
542         BN_free(x); BN_free(y); BN_free(z);
543
544         if (P_192) EC_GROUP_free(P_192);
545         if (P_224) EC_GROUP_free(P_224);
546         if (P_256) EC_GROUP_free(P_256);
547         if (P_384) EC_GROUP_free(P_384);
548         if (P_521) EC_GROUP_free(P_521);
549
550         ERR_free_strings();
551         ERR_remove_state(0);
552         CRYPTO_mem_leaks_fp(stderr);
553         
554         return 0;
555         }
556 #endif