mark all block comments that need format preserving so that
[openssl.git] / crypto / ec / ecp_smpl.c
1 /* crypto/ec/ecp_smpl.c */
2 /* Includes code written by Lenka Fibikova <fibikova@exp-math.uni-essen.de>
3  * for the OpenSSL project. 
4  * Includes code written by Bodo Moeller for the OpenSSL project.
5 */
6 /* ====================================================================
7  * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer. 
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. All advertising materials mentioning features or use of this
22  *    software must display the following acknowledgment:
23  *    "This product includes software developed by the OpenSSL Project
24  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
25  *
26  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27  *    endorse or promote products derived from this software without
28  *    prior written permission. For written permission, please contact
29  *    openssl-core@openssl.org.
30  *
31  * 5. Products derived from this software may not be called "OpenSSL"
32  *    nor may "OpenSSL" appear in their names without prior written
33  *    permission of the OpenSSL Project.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51  * OF THE POSSIBILITY OF SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This product includes cryptographic software written by Eric Young
55  * (eay@cryptsoft.com).  This product includes software written by Tim
56  * Hudson (tjh@cryptsoft.com).
57  *
58  */
59 /* ====================================================================
60  * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
61  * Portions of this software developed by SUN MICROSYSTEMS, INC.,
62  * and contributed to the OpenSSL project.
63  */
64
65 #include <openssl/err.h>
66 #include <openssl/symhacks.h>
67
68 #ifdef OPENSSL_FIPS
69 #include <openssl/fips.h>
70 #endif
71
72 #include "ec_lcl.h"
73
74 const EC_METHOD *EC_GFp_simple_method(void)
75         {
76         static const EC_METHOD ret = {
77                 EC_FLAGS_DEFAULT_OCT,
78                 NID_X9_62_prime_field,
79                 ec_GFp_simple_group_init,
80                 ec_GFp_simple_group_finish,
81                 ec_GFp_simple_group_clear_finish,
82                 ec_GFp_simple_group_copy,
83                 ec_GFp_simple_group_set_curve,
84                 ec_GFp_simple_group_get_curve,
85                 ec_GFp_simple_group_get_degree,
86                 ec_GFp_simple_group_check_discriminant,
87                 ec_GFp_simple_point_init,
88                 ec_GFp_simple_point_finish,
89                 ec_GFp_simple_point_clear_finish,
90                 ec_GFp_simple_point_copy,
91                 ec_GFp_simple_point_set_to_infinity,
92                 ec_GFp_simple_set_Jprojective_coordinates_GFp,
93                 ec_GFp_simple_get_Jprojective_coordinates_GFp,
94                 ec_GFp_simple_point_set_affine_coordinates,
95                 ec_GFp_simple_point_get_affine_coordinates,
96                 0,0,0,
97                 ec_GFp_simple_add,
98                 ec_GFp_simple_dbl,
99                 ec_GFp_simple_invert,
100                 ec_GFp_simple_is_at_infinity,
101                 ec_GFp_simple_is_on_curve,
102                 ec_GFp_simple_cmp,
103                 ec_GFp_simple_make_affine,
104                 ec_GFp_simple_points_make_affine,
105                 0 /* mul */,
106                 0 /* precompute_mult */,
107                 0 /* have_precompute_mult */,   
108                 ec_GFp_simple_field_mul,
109                 ec_GFp_simple_field_sqr,
110                 0 /* field_div */,
111                 0 /* field_encode */,
112                 0 /* field_decode */,
113                 0 /* field_set_to_one */ };
114
115 #ifdef OPENSSL_FIPS
116         if (FIPS_mode())
117                 return fips_ec_gfp_simple_method();
118 #endif
119
120         return &ret;
121         }
122
123
124 /* Most method functions in this file are designed to work with
125  * non-trivial representations of field elements if necessary
126  * (see ecp_mont.c): while standard modular addition and subtraction
127  * are used, the field_mul and field_sqr methods will be used for
128  * multiplication, and field_encode and field_decode (if defined)
129  * will be used for converting between representations.
130
131  * Functions ec_GFp_simple_points_make_affine() and
132  * ec_GFp_simple_point_get_affine_coordinates() specifically assume
133  * that if a non-trivial representation is used, it is a Montgomery
134  * representation (i.e. 'encoding' means multiplying by some factor R).
135  */
136
137
138 int ec_GFp_simple_group_init(EC_GROUP *group)
139         {
140         BN_init(&group->field);
141         BN_init(&group->a);
142         BN_init(&group->b);
143         group->a_is_minus3 = 0;
144         return 1;
145         }
146
147
148 void ec_GFp_simple_group_finish(EC_GROUP *group)
149         {
150         BN_free(&group->field);
151         BN_free(&group->a);
152         BN_free(&group->b);
153         }
154
155
156 void ec_GFp_simple_group_clear_finish(EC_GROUP *group)
157         {
158         BN_clear_free(&group->field);
159         BN_clear_free(&group->a);
160         BN_clear_free(&group->b);
161         }
162
163
164 int ec_GFp_simple_group_copy(EC_GROUP *dest, const EC_GROUP *src)
165         {
166         if (!BN_copy(&dest->field, &src->field)) return 0;
167         if (!BN_copy(&dest->a, &src->a)) return 0;
168         if (!BN_copy(&dest->b, &src->b)) return 0;
169
170         dest->a_is_minus3 = src->a_is_minus3;
171
172         return 1;
173         }
174
175
176 int ec_GFp_simple_group_set_curve(EC_GROUP *group,
177         const BIGNUM *p, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
178         {
179         int ret = 0;
180         BN_CTX *new_ctx = NULL;
181         BIGNUM *tmp_a;
182         
183         /* p must be a prime > 3 */
184         if (BN_num_bits(p) <= 2 || !BN_is_odd(p))
185                 {
186                 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE, EC_R_INVALID_FIELD);
187                 return 0;
188                 }
189
190         if (ctx == NULL)
191                 {
192                 ctx = new_ctx = BN_CTX_new();
193                 if (ctx == NULL)
194                         return 0;
195                 }
196
197         BN_CTX_start(ctx);
198         tmp_a = BN_CTX_get(ctx);
199         if (tmp_a == NULL) goto err;
200
201         /* group->field */
202         if (!BN_copy(&group->field, p)) goto err;
203         BN_set_negative(&group->field, 0);
204
205         /* group->a */
206         if (!BN_nnmod(tmp_a, a, p, ctx)) goto err;
207         if (group->meth->field_encode)
208                 { if (!group->meth->field_encode(group, &group->a, tmp_a, ctx)) goto err; }     
209         else
210                 if (!BN_copy(&group->a, tmp_a)) goto err;
211         
212         /* group->b */
213         if (!BN_nnmod(&group->b, b, p, ctx)) goto err;
214         if (group->meth->field_encode)
215                 if (!group->meth->field_encode(group, &group->b, &group->b, ctx)) goto err;
216         
217         /* group->a_is_minus3 */
218         if (!BN_add_word(tmp_a, 3)) goto err;
219         group->a_is_minus3 = (0 == BN_cmp(tmp_a, &group->field));
220
221         ret = 1;
222
223  err:
224         BN_CTX_end(ctx);
225         if (new_ctx != NULL)
226                 BN_CTX_free(new_ctx);
227         return ret;
228         }
229
230
231 int ec_GFp_simple_group_get_curve(const EC_GROUP *group, BIGNUM *p, BIGNUM *a, BIGNUM *b, BN_CTX *ctx)
232         {
233         int ret = 0;
234         BN_CTX *new_ctx = NULL;
235         
236         if (p != NULL)
237                 {
238                 if (!BN_copy(p, &group->field)) return 0;
239                 }
240
241         if (a != NULL || b != NULL)
242                 {
243                 if (group->meth->field_decode)
244                         {
245                         if (ctx == NULL)
246                                 {
247                                 ctx = new_ctx = BN_CTX_new();
248                                 if (ctx == NULL)
249                                         return 0;
250                                 }
251                         if (a != NULL)
252                                 {
253                                 if (!group->meth->field_decode(group, a, &group->a, ctx)) goto err;
254                                 }
255                         if (b != NULL)
256                                 {
257                                 if (!group->meth->field_decode(group, b, &group->b, ctx)) goto err;
258                                 }
259                         }
260                 else
261                         {
262                         if (a != NULL)
263                                 {
264                                 if (!BN_copy(a, &group->a)) goto err;
265                                 }
266                         if (b != NULL)
267                                 {
268                                 if (!BN_copy(b, &group->b)) goto err;
269                                 }
270                         }
271                 }
272         
273         ret = 1;
274         
275  err:
276         if (new_ctx)
277                 BN_CTX_free(new_ctx);
278         return ret;
279         }
280
281
282 int ec_GFp_simple_group_get_degree(const EC_GROUP *group)
283         {
284         return BN_num_bits(&group->field);
285         }
286
287
288 int ec_GFp_simple_group_check_discriminant(const EC_GROUP *group, BN_CTX *ctx)
289         {
290         int ret = 0;
291         BIGNUM *a,*b,*order,*tmp_1,*tmp_2;
292         const BIGNUM *p = &group->field;
293         BN_CTX *new_ctx = NULL;
294
295         if (ctx == NULL)
296                 {
297                 ctx = new_ctx = BN_CTX_new();
298                 if (ctx == NULL)
299                         {
300                         ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT, ERR_R_MALLOC_FAILURE);
301                         goto err;
302                         }
303                 }
304         BN_CTX_start(ctx);
305         a = BN_CTX_get(ctx);
306         b = BN_CTX_get(ctx);
307         tmp_1 = BN_CTX_get(ctx);
308         tmp_2 = BN_CTX_get(ctx);
309         order = BN_CTX_get(ctx);
310         if (order == NULL) goto err;
311
312         if (group->meth->field_decode)
313                 {
314                 if (!group->meth->field_decode(group, a, &group->a, ctx)) goto err;
315                 if (!group->meth->field_decode(group, b, &group->b, ctx)) goto err;
316                 }
317         else
318                 {
319                 if (!BN_copy(a, &group->a)) goto err;
320                 if (!BN_copy(b, &group->b)) goto err;
321                 }
322         
323         /*-
324          * check the discriminant:
325          * y^2 = x^3 + a*x + b is an elliptic curve <=> 4*a^3 + 27*b^2 != 0 (mod p) 
326          * 0 =< a, b < p 
327          */
328         if (BN_is_zero(a))
329                 {
330                 if (BN_is_zero(b)) goto err;
331                 }
332         else if (!BN_is_zero(b))
333                 {
334                 if (!BN_mod_sqr(tmp_1, a, p, ctx)) goto err;
335                 if (!BN_mod_mul(tmp_2, tmp_1, a, p, ctx)) goto err;
336                 if (!BN_lshift(tmp_1, tmp_2, 2)) goto err;
337                 /* tmp_1 = 4*a^3 */
338
339                 if (!BN_mod_sqr(tmp_2, b, p, ctx)) goto err;
340                 if (!BN_mul_word(tmp_2, 27)) goto err;
341                 /* tmp_2 = 27*b^2 */
342
343                 if (!BN_mod_add(a, tmp_1, tmp_2, p, ctx)) goto err;
344                 if (BN_is_zero(a)) goto err;
345                 }
346         ret = 1;
347
348 err:
349         if (ctx != NULL)
350                 BN_CTX_end(ctx);
351         if (new_ctx != NULL)
352                 BN_CTX_free(new_ctx);
353         return ret;
354         }
355
356
357 int ec_GFp_simple_point_init(EC_POINT *point)
358         {
359         BN_init(&point->X);
360         BN_init(&point->Y);
361         BN_init(&point->Z);
362         point->Z_is_one = 0;
363
364         return 1;
365         }
366
367
368 void ec_GFp_simple_point_finish(EC_POINT *point)
369         {
370         BN_free(&point->X);
371         BN_free(&point->Y);
372         BN_free(&point->Z);
373         }
374
375
376 void ec_GFp_simple_point_clear_finish(EC_POINT *point)
377         {
378         BN_clear_free(&point->X);
379         BN_clear_free(&point->Y);
380         BN_clear_free(&point->Z);
381         point->Z_is_one = 0;
382         }
383
384
385 int ec_GFp_simple_point_copy(EC_POINT *dest, const EC_POINT *src)
386         {
387         if (!BN_copy(&dest->X, &src->X)) return 0;
388         if (!BN_copy(&dest->Y, &src->Y)) return 0;
389         if (!BN_copy(&dest->Z, &src->Z)) return 0;
390         dest->Z_is_one = src->Z_is_one;
391
392         return 1;
393         }
394
395
396 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP *group, EC_POINT *point)
397         {
398         point->Z_is_one = 0;
399         BN_zero(&point->Z);
400         return 1;
401         }
402
403
404 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP *group, EC_POINT *point,
405         const BIGNUM *x, const BIGNUM *y, const BIGNUM *z, BN_CTX *ctx)
406         {
407         BN_CTX *new_ctx = NULL;
408         int ret = 0;
409         
410         if (ctx == NULL)
411                 {
412                 ctx = new_ctx = BN_CTX_new();
413                 if (ctx == NULL)
414                         return 0;
415                 }
416
417         if (x != NULL)
418                 {
419                 if (!BN_nnmod(&point->X, x, &group->field, ctx)) goto err;
420                 if (group->meth->field_encode)
421                         {
422                         if (!group->meth->field_encode(group, &point->X, &point->X, ctx)) goto err;
423                         }
424                 }
425         
426         if (y != NULL)
427                 {
428                 if (!BN_nnmod(&point->Y, y, &group->field, ctx)) goto err;
429                 if (group->meth->field_encode)
430                         {
431                         if (!group->meth->field_encode(group, &point->Y, &point->Y, ctx)) goto err;
432                         }
433                 }
434         
435         if (z != NULL)
436                 {
437                 int Z_is_one;
438
439                 if (!BN_nnmod(&point->Z, z, &group->field, ctx)) goto err;
440                 Z_is_one = BN_is_one(&point->Z);
441                 if (group->meth->field_encode)
442                         {
443                         if (Z_is_one && (group->meth->field_set_to_one != 0))
444                                 {
445                                 if (!group->meth->field_set_to_one(group, &point->Z, ctx)) goto err;
446                                 }
447                         else
448                                 {
449                                 if (!group->meth->field_encode(group, &point->Z, &point->Z, ctx)) goto err;
450                                 }
451                         }
452                 point->Z_is_one = Z_is_one;
453                 }
454         
455         ret = 1;
456         
457  err:
458         if (new_ctx != NULL)
459                 BN_CTX_free(new_ctx);
460         return ret;
461         }
462
463
464 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP *group, const EC_POINT *point,
465         BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *ctx)
466         {
467         BN_CTX *new_ctx = NULL;
468         int ret = 0;
469         
470         if (group->meth->field_decode != 0)
471                 {
472                 if (ctx == NULL)
473                         {
474                         ctx = new_ctx = BN_CTX_new();
475                         if (ctx == NULL)
476                                 return 0;
477                         }
478
479                 if (x != NULL)
480                         {
481                         if (!group->meth->field_decode(group, x, &point->X, ctx)) goto err;
482                         }
483                 if (y != NULL)
484                         {
485                         if (!group->meth->field_decode(group, y, &point->Y, ctx)) goto err;
486                         }
487                 if (z != NULL)
488                         {
489                         if (!group->meth->field_decode(group, z, &point->Z, ctx)) goto err;
490                         }
491                 }
492         else    
493                 {
494                 if (x != NULL)
495                         {
496                         if (!BN_copy(x, &point->X)) goto err;
497                         }
498                 if (y != NULL)
499                         {
500                         if (!BN_copy(y, &point->Y)) goto err;
501                         }
502                 if (z != NULL)
503                         {
504                         if (!BN_copy(z, &point->Z)) goto err;
505                         }
506                 }
507         
508         ret = 1;
509
510  err:
511         if (new_ctx != NULL)
512                 BN_CTX_free(new_ctx);
513         return ret;
514         }
515
516
517 int ec_GFp_simple_point_set_affine_coordinates(const EC_GROUP *group, EC_POINT *point,
518         const BIGNUM *x, const BIGNUM *y, BN_CTX *ctx)
519         {
520         if (x == NULL || y == NULL)
521                 {
522                 /* unlike for projective coordinates, we do not tolerate this */
523                 ECerr(EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES, ERR_R_PASSED_NULL_PARAMETER);
524                 return 0;
525                 }
526
527         return EC_POINT_set_Jprojective_coordinates_GFp(group, point, x, y, BN_value_one(), ctx);
528         }
529
530
531 int ec_GFp_simple_point_get_affine_coordinates(const EC_GROUP *group, const EC_POINT *point,
532         BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
533         {
534         BN_CTX *new_ctx = NULL;
535         BIGNUM *Z, *Z_1, *Z_2, *Z_3;
536         const BIGNUM *Z_;
537         int ret = 0;
538
539         if (EC_POINT_is_at_infinity(group, point))
540                 {
541                 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES, EC_R_POINT_AT_INFINITY);
542                 return 0;
543                 }
544
545         if (ctx == NULL)
546                 {
547                 ctx = new_ctx = BN_CTX_new();
548                 if (ctx == NULL)
549                         return 0;
550                 }
551
552         BN_CTX_start(ctx);
553         Z = BN_CTX_get(ctx);
554         Z_1 = BN_CTX_get(ctx);
555         Z_2 = BN_CTX_get(ctx);
556         Z_3 = BN_CTX_get(ctx);
557         if (Z_3 == NULL) goto err;
558
559         /* transform  (X, Y, Z)  into  (x, y) := (X/Z^2, Y/Z^3) */
560         
561         if (group->meth->field_decode)
562                 {
563                 if (!group->meth->field_decode(group, Z, &point->Z, ctx)) goto err;
564                 Z_ = Z;
565                 }
566         else
567                 {
568                 Z_ = &point->Z;
569                 }
570         
571         if (BN_is_one(Z_))
572                 {
573                 if (group->meth->field_decode)
574                         {
575                         if (x != NULL)
576                                 {
577                                 if (!group->meth->field_decode(group, x, &point->X, ctx)) goto err;
578                                 }
579                         if (y != NULL)
580                                 {
581                                 if (!group->meth->field_decode(group, y, &point->Y, ctx)) goto err;
582                                 }
583                         }
584                 else
585                         {
586                         if (x != NULL)
587                                 {
588                                 if (!BN_copy(x, &point->X)) goto err;
589                                 }
590                         if (y != NULL)
591                                 {
592                                 if (!BN_copy(y, &point->Y)) goto err;
593                                 }
594                         }
595                 }
596         else
597                 {
598                 if (!BN_mod_inverse(Z_1, Z_, &group->field, ctx))
599                         {
600                         ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES, ERR_R_BN_LIB);
601                         goto err;
602                         }
603                 
604                 if (group->meth->field_encode == 0)
605                         {
606                         /* field_sqr works on standard representation */
607                         if (!group->meth->field_sqr(group, Z_2, Z_1, ctx)) goto err;
608                         }
609                 else
610                         {
611                         if (!BN_mod_sqr(Z_2, Z_1, &group->field, ctx)) goto err;
612                         }
613         
614                 if (x != NULL)
615                         {
616                         /* in the Montgomery case, field_mul will cancel out Montgomery factor in X: */
617                         if (!group->meth->field_mul(group, x, &point->X, Z_2, ctx)) goto err;
618                         }
619
620                 if (y != NULL)
621                         {
622                         if (group->meth->field_encode == 0)
623                                 {
624                                 /* field_mul works on standard representation */
625                                 if (!group->meth->field_mul(group, Z_3, Z_2, Z_1, ctx)) goto err;
626                                 }
627                         else
628                                 {
629                                 if (!BN_mod_mul(Z_3, Z_2, Z_1, &group->field, ctx)) goto err;
630                                 }
631
632                         /* in the Montgomery case, field_mul will cancel out Montgomery factor in Y: */
633                         if (!group->meth->field_mul(group, y, &point->Y, Z_3, ctx)) goto err;
634                         }
635                 }
636
637         ret = 1;
638
639  err:
640         BN_CTX_end(ctx);
641         if (new_ctx != NULL)
642                 BN_CTX_free(new_ctx);
643         return ret;
644         }
645
646 int ec_GFp_simple_add(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
647         {
648         int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
649         int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
650         const BIGNUM *p;
651         BN_CTX *new_ctx = NULL;
652         BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6;
653         int ret = 0;
654         
655         if (a == b)
656                 return EC_POINT_dbl(group, r, a, ctx);
657         if (EC_POINT_is_at_infinity(group, a))
658                 return EC_POINT_copy(r, b);
659         if (EC_POINT_is_at_infinity(group, b))
660                 return EC_POINT_copy(r, a);
661         
662         field_mul = group->meth->field_mul;
663         field_sqr = group->meth->field_sqr;
664         p = &group->field;
665
666         if (ctx == NULL)
667                 {
668                 ctx = new_ctx = BN_CTX_new();
669                 if (ctx == NULL)
670                         return 0;
671                 }
672
673         BN_CTX_start(ctx);
674         n0 = BN_CTX_get(ctx);
675         n1 = BN_CTX_get(ctx);
676         n2 = BN_CTX_get(ctx);
677         n3 = BN_CTX_get(ctx);
678         n4 = BN_CTX_get(ctx);
679         n5 = BN_CTX_get(ctx);
680         n6 = BN_CTX_get(ctx);
681         if (n6 == NULL) goto end;
682
683         /* Note that in this function we must not read components of 'a' or 'b'
684          * once we have written the corresponding components of 'r'.
685          * ('r' might be one of 'a' or 'b'.)
686          */
687
688         /* n1, n2 */
689         if (b->Z_is_one)
690                 {
691                 if (!BN_copy(n1, &a->X)) goto end;
692                 if (!BN_copy(n2, &a->Y)) goto end;
693                 /* n1 = X_a */
694                 /* n2 = Y_a */
695                 }
696         else
697                 {
698                 if (!field_sqr(group, n0, &b->Z, ctx)) goto end;
699                 if (!field_mul(group, n1, &a->X, n0, ctx)) goto end;
700                 /* n1 = X_a * Z_b^2 */
701
702                 if (!field_mul(group, n0, n0, &b->Z, ctx)) goto end;
703                 if (!field_mul(group, n2, &a->Y, n0, ctx)) goto end;
704                 /* n2 = Y_a * Z_b^3 */
705                 }
706
707         /* n3, n4 */
708         if (a->Z_is_one)
709                 {
710                 if (!BN_copy(n3, &b->X)) goto end;
711                 if (!BN_copy(n4, &b->Y)) goto end;
712                 /* n3 = X_b */
713                 /* n4 = Y_b */
714                 }
715         else
716                 {
717                 if (!field_sqr(group, n0, &a->Z, ctx)) goto end;
718                 if (!field_mul(group, n3, &b->X, n0, ctx)) goto end;
719                 /* n3 = X_b * Z_a^2 */
720
721                 if (!field_mul(group, n0, n0, &a->Z, ctx)) goto end;
722                 if (!field_mul(group, n4, &b->Y, n0, ctx)) goto end;
723                 /* n4 = Y_b * Z_a^3 */
724                 }
725
726         /* n5, n6 */
727         if (!BN_mod_sub_quick(n5, n1, n3, p)) goto end;
728         if (!BN_mod_sub_quick(n6, n2, n4, p)) goto end;
729         /* n5 = n1 - n3 */
730         /* n6 = n2 - n4 */
731
732         if (BN_is_zero(n5))
733                 {
734                 if (BN_is_zero(n6))
735                         {
736                         /* a is the same point as b */
737                         BN_CTX_end(ctx);
738                         ret = EC_POINT_dbl(group, r, a, ctx);
739                         ctx = NULL;
740                         goto end;
741                         }
742                 else
743                         {
744                         /* a is the inverse of b */
745                         BN_zero(&r->Z);
746                         r->Z_is_one = 0;
747                         ret = 1;
748                         goto end;
749                         }
750                 }
751
752         /* 'n7', 'n8' */
753         if (!BN_mod_add_quick(n1, n1, n3, p)) goto end;
754         if (!BN_mod_add_quick(n2, n2, n4, p)) goto end;
755         /* 'n7' = n1 + n3 */
756         /* 'n8' = n2 + n4 */
757
758         /* Z_r */
759         if (a->Z_is_one && b->Z_is_one)
760                 {
761                 if (!BN_copy(&r->Z, n5)) goto end;
762                 }
763         else
764                 {
765                 if (a->Z_is_one)
766                         { if (!BN_copy(n0, &b->Z)) goto end; }
767                 else if (b->Z_is_one)
768                         { if (!BN_copy(n0, &a->Z)) goto end; }
769                 else
770                         { if (!field_mul(group, n0, &a->Z, &b->Z, ctx)) goto end; }
771                 if (!field_mul(group, &r->Z, n0, n5, ctx)) goto end;
772                 }
773         r->Z_is_one = 0;
774         /* Z_r = Z_a * Z_b * n5 */
775
776         /* X_r */
777         if (!field_sqr(group, n0, n6, ctx)) goto end;
778         if (!field_sqr(group, n4, n5, ctx)) goto end;
779         if (!field_mul(group, n3, n1, n4, ctx)) goto end;
780         if (!BN_mod_sub_quick(&r->X, n0, n3, p)) goto end;
781         /* X_r = n6^2 - n5^2 * 'n7' */
782         
783         /* 'n9' */
784         if (!BN_mod_lshift1_quick(n0, &r->X, p)) goto end;
785         if (!BN_mod_sub_quick(n0, n3, n0, p)) goto end;
786         /* n9 = n5^2 * 'n7' - 2 * X_r */
787
788         /* Y_r */
789         if (!field_mul(group, n0, n0, n6, ctx)) goto end;
790         if (!field_mul(group, n5, n4, n5, ctx)) goto end; /* now n5 is n5^3 */
791         if (!field_mul(group, n1, n2, n5, ctx)) goto end;
792         if (!BN_mod_sub_quick(n0, n0, n1, p)) goto end;
793         if (BN_is_odd(n0))
794                 if (!BN_add(n0, n0, p)) goto end;
795         /* now  0 <= n0 < 2*p,  and n0 is even */
796         if (!BN_rshift1(&r->Y, n0)) goto end;
797         /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
798
799         ret = 1;
800
801  end:
802         if (ctx) /* otherwise we already called BN_CTX_end */
803                 BN_CTX_end(ctx);
804         if (new_ctx != NULL)
805                 BN_CTX_free(new_ctx);
806         return ret;
807         }
808
809
810 int ec_GFp_simple_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *ctx)
811         {
812         int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
813         int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
814         const BIGNUM *p;
815         BN_CTX *new_ctx = NULL;
816         BIGNUM *n0, *n1, *n2, *n3;
817         int ret = 0;
818         
819         if (EC_POINT_is_at_infinity(group, a))
820                 {
821                 BN_zero(&r->Z);
822                 r->Z_is_one = 0;
823                 return 1;
824                 }
825
826         field_mul = group->meth->field_mul;
827         field_sqr = group->meth->field_sqr;
828         p = &group->field;
829
830         if (ctx == NULL)
831                 {
832                 ctx = new_ctx = BN_CTX_new();
833                 if (ctx == NULL)
834                         return 0;
835                 }
836
837         BN_CTX_start(ctx);
838         n0 = BN_CTX_get(ctx);
839         n1 = BN_CTX_get(ctx);
840         n2 = BN_CTX_get(ctx);
841         n3 = BN_CTX_get(ctx);
842         if (n3 == NULL) goto err;
843
844         /* Note that in this function we must not read components of 'a'
845          * once we have written the corresponding components of 'r'.
846          * ('r' might the same as 'a'.)
847          */
848
849         /* n1 */
850         if (a->Z_is_one)
851                 {
852                 if (!field_sqr(group, n0, &a->X, ctx)) goto err;
853                 if (!BN_mod_lshift1_quick(n1, n0, p)) goto err;
854                 if (!BN_mod_add_quick(n0, n0, n1, p)) goto err;
855                 if (!BN_mod_add_quick(n1, n0, &group->a, p)) goto err;
856                 /* n1 = 3 * X_a^2 + a_curve */
857                 }
858         else if (group->a_is_minus3)
859                 {
860                 if (!field_sqr(group, n1, &a->Z, ctx)) goto err;
861                 if (!BN_mod_add_quick(n0, &a->X, n1, p)) goto err;
862                 if (!BN_mod_sub_quick(n2, &a->X, n1, p)) goto err;
863                 if (!field_mul(group, n1, n0, n2, ctx)) goto err;
864                 if (!BN_mod_lshift1_quick(n0, n1, p)) goto err;
865                 if (!BN_mod_add_quick(n1, n0, n1, p)) goto err;
866                 /* n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
867                  *    = 3 * X_a^2 - 3 * Z_a^4 */
868                 }
869         else
870                 {
871                 if (!field_sqr(group, n0, &a->X, ctx)) goto err;
872                 if (!BN_mod_lshift1_quick(n1, n0, p)) goto err;
873                 if (!BN_mod_add_quick(n0, n0, n1, p)) goto err;
874                 if (!field_sqr(group, n1, &a->Z, ctx)) goto err;
875                 if (!field_sqr(group, n1, n1, ctx)) goto err;
876                 if (!field_mul(group, n1, n1, &group->a, ctx)) goto err;
877                 if (!BN_mod_add_quick(n1, n1, n0, p)) goto err;
878                 /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */
879                 }
880
881         /* Z_r */
882         if (a->Z_is_one)
883                 {
884                 if (!BN_copy(n0, &a->Y)) goto err;
885                 }
886         else
887                 {
888                 if (!field_mul(group, n0, &a->Y, &a->Z, ctx)) goto err;
889                 }
890         if (!BN_mod_lshift1_quick(&r->Z, n0, p)) goto err;
891         r->Z_is_one = 0;
892         /* Z_r = 2 * Y_a * Z_a */
893
894         /* n2 */
895         if (!field_sqr(group, n3, &a->Y, ctx)) goto err;
896         if (!field_mul(group, n2, &a->X, n3, ctx)) goto err;
897         if (!BN_mod_lshift_quick(n2, n2, 2, p)) goto err;
898         /* n2 = 4 * X_a * Y_a^2 */
899
900         /* X_r */
901         if (!BN_mod_lshift1_quick(n0, n2, p)) goto err;
902         if (!field_sqr(group, &r->X, n1, ctx)) goto err;
903         if (!BN_mod_sub_quick(&r->X, &r->X, n0, p)) goto err;
904         /* X_r = n1^2 - 2 * n2 */
905         
906         /* n3 */
907         if (!field_sqr(group, n0, n3, ctx)) goto err;
908         if (!BN_mod_lshift_quick(n3, n0, 3, p)) goto err;
909         /* n3 = 8 * Y_a^4 */
910         
911         /* Y_r */
912         if (!BN_mod_sub_quick(n0, n2, &r->X, p)) goto err;
913         if (!field_mul(group, n0, n1, n0, ctx)) goto err;
914         if (!BN_mod_sub_quick(&r->Y, n0, n3, p)) goto err;
915         /* Y_r = n1 * (n2 - X_r) - n3 */
916
917         ret = 1;
918
919  err:
920         BN_CTX_end(ctx);
921         if (new_ctx != NULL)
922                 BN_CTX_free(new_ctx);
923         return ret;
924         }
925
926
927 int ec_GFp_simple_invert(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
928         {
929         if (EC_POINT_is_at_infinity(group, point) || BN_is_zero(&point->Y))
930                 /* point is its own inverse */
931                 return 1;
932         
933         return BN_usub(&point->Y, &group->field, &point->Y);
934         }
935
936
937 int ec_GFp_simple_is_at_infinity(const EC_GROUP *group, const EC_POINT *point)
938         {
939         return BN_is_zero(&point->Z);
940         }
941
942
943 int ec_GFp_simple_is_on_curve(const EC_GROUP *group, const EC_POINT *point, BN_CTX *ctx)
944         {
945         int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
946         int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
947         const BIGNUM *p;
948         BN_CTX *new_ctx = NULL;
949         BIGNUM *rh, *tmp, *Z4, *Z6;
950         int ret = -1;
951
952         if (EC_POINT_is_at_infinity(group, point))
953                 return 1;
954         
955         field_mul = group->meth->field_mul;
956         field_sqr = group->meth->field_sqr;
957         p = &group->field;
958
959         if (ctx == NULL)
960                 {
961                 ctx = new_ctx = BN_CTX_new();
962                 if (ctx == NULL)
963                         return -1;
964                 }
965
966         BN_CTX_start(ctx);
967         rh = BN_CTX_get(ctx);
968         tmp = BN_CTX_get(ctx);
969         Z4 = BN_CTX_get(ctx);
970         Z6 = BN_CTX_get(ctx);
971         if (Z6 == NULL) goto err;
972
973         /*-
974          * We have a curve defined by a Weierstrass equation
975          *      y^2 = x^3 + a*x + b.
976          * The point to consider is given in Jacobian projective coordinates
977          * where  (X, Y, Z)  represents  (x, y) = (X/Z^2, Y/Z^3).
978          * Substituting this and multiplying by  Z^6  transforms the above equation into
979          *      Y^2 = X^3 + a*X*Z^4 + b*Z^6.
980          * To test this, we add up the right-hand side in 'rh'.
981          */
982
983         /* rh := X^2 */
984         if (!field_sqr(group, rh, &point->X, ctx)) goto err;
985
986         if (!point->Z_is_one)
987                 {
988                 if (!field_sqr(group, tmp, &point->Z, ctx)) goto err;
989                 if (!field_sqr(group, Z4, tmp, ctx)) goto err;
990                 if (!field_mul(group, Z6, Z4, tmp, ctx)) goto err;
991
992                 /* rh := (rh + a*Z^4)*X */
993                 if (group->a_is_minus3)
994                         {
995                         if (!BN_mod_lshift1_quick(tmp, Z4, p)) goto err;
996                         if (!BN_mod_add_quick(tmp, tmp, Z4, p)) goto err;
997                         if (!BN_mod_sub_quick(rh, rh, tmp, p)) goto err;
998                         if (!field_mul(group, rh, rh, &point->X, ctx)) goto err;
999                         }
1000                 else
1001                         {
1002                         if (!field_mul(group, tmp, Z4, &group->a, ctx)) goto err;
1003                         if (!BN_mod_add_quick(rh, rh, tmp, p)) goto err;
1004                         if (!field_mul(group, rh, rh, &point->X, ctx)) goto err;
1005                         }
1006
1007                 /* rh := rh + b*Z^6 */
1008                 if (!field_mul(group, tmp, &group->b, Z6, ctx)) goto err;
1009                 if (!BN_mod_add_quick(rh, rh, tmp, p)) goto err;
1010                 }
1011         else
1012                 {
1013                 /* point->Z_is_one */
1014
1015                 /* rh := (rh + a)*X */
1016                 if (!BN_mod_add_quick(rh, rh, &group->a, p)) goto err;
1017                 if (!field_mul(group, rh, rh, &point->X, ctx)) goto err;
1018                 /* rh := rh + b */
1019                 if (!BN_mod_add_quick(rh, rh, &group->b, p)) goto err;
1020                 }
1021
1022         /* 'lh' := Y^2 */
1023         if (!field_sqr(group, tmp, &point->Y, ctx)) goto err;
1024
1025         ret = (0 == BN_ucmp(tmp, rh));
1026
1027  err:
1028         BN_CTX_end(ctx);
1029         if (new_ctx != NULL)
1030                 BN_CTX_free(new_ctx);
1031         return ret;
1032         }
1033
1034
1035 int ec_GFp_simple_cmp(const EC_GROUP *group, const EC_POINT *a, const EC_POINT *b, BN_CTX *ctx)
1036         {
1037         /* return values:
1038          *  -1   error
1039          *   0   equal (in affine coordinates)
1040          *   1   not equal
1041          */
1042
1043         int (*field_mul)(const EC_GROUP *, BIGNUM *, const BIGNUM *, const BIGNUM *, BN_CTX *);
1044         int (*field_sqr)(const EC_GROUP *, BIGNUM *, const BIGNUM *, BN_CTX *);
1045         BN_CTX *new_ctx = NULL;
1046         BIGNUM *tmp1, *tmp2, *Za23, *Zb23;
1047         const BIGNUM *tmp1_, *tmp2_;
1048         int ret = -1;
1049         
1050         if (EC_POINT_is_at_infinity(group, a))
1051                 {
1052                 return EC_POINT_is_at_infinity(group, b) ? 0 : 1;
1053                 }
1054
1055         if (EC_POINT_is_at_infinity(group, b))
1056                 return 1;
1057         
1058         if (a->Z_is_one && b->Z_is_one)
1059                 {
1060                 return ((BN_cmp(&a->X, &b->X) == 0) && BN_cmp(&a->Y, &b->Y) == 0) ? 0 : 1;
1061                 }
1062
1063         field_mul = group->meth->field_mul;
1064         field_sqr = group->meth->field_sqr;
1065
1066         if (ctx == NULL)
1067                 {
1068                 ctx = new_ctx = BN_CTX_new();
1069                 if (ctx == NULL)
1070                         return -1;
1071                 }
1072
1073         BN_CTX_start(ctx);
1074         tmp1 = BN_CTX_get(ctx);
1075         tmp2 = BN_CTX_get(ctx);
1076         Za23 = BN_CTX_get(ctx);
1077         Zb23 = BN_CTX_get(ctx);
1078         if (Zb23 == NULL) goto end;
1079
1080         /*-
1081          * We have to decide whether
1082          *     (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
1083          * or equivalently, whether
1084          *     (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
1085          */
1086
1087         if (!b->Z_is_one)
1088                 {
1089                 if (!field_sqr(group, Zb23, &b->Z, ctx)) goto end;
1090                 if (!field_mul(group, tmp1, &a->X, Zb23, ctx)) goto end;
1091                 tmp1_ = tmp1;
1092                 }
1093         else
1094                 tmp1_ = &a->X;
1095         if (!a->Z_is_one)
1096                 {
1097                 if (!field_sqr(group, Za23, &a->Z, ctx)) goto end;
1098                 if (!field_mul(group, tmp2, &b->X, Za23, ctx)) goto end;
1099                 tmp2_ = tmp2;
1100                 }
1101         else
1102                 tmp2_ = &b->X;
1103         
1104         /* compare  X_a*Z_b^2  with  X_b*Z_a^2 */
1105         if (BN_cmp(tmp1_, tmp2_) != 0)
1106                 {
1107                 ret = 1; /* points differ */
1108                 goto end;
1109                 }
1110
1111
1112         if (!b->Z_is_one)
1113                 {
1114                 if (!field_mul(group, Zb23, Zb23, &b->Z, ctx)) goto end;
1115                 if (!field_mul(group, tmp1, &a->Y, Zb23, ctx)) goto end;
1116                 /* tmp1_ = tmp1 */
1117                 }
1118         else
1119                 tmp1_ = &a->Y;
1120         if (!a->Z_is_one)
1121                 {
1122                 if (!field_mul(group, Za23, Za23, &a->Z, ctx)) goto end;
1123                 if (!field_mul(group, tmp2, &b->Y, Za23, ctx)) goto end;
1124                 /* tmp2_ = tmp2 */
1125                 }
1126         else
1127                 tmp2_ = &b->Y;
1128
1129         /* compare  Y_a*Z_b^3  with  Y_b*Z_a^3 */
1130         if (BN_cmp(tmp1_, tmp2_) != 0)
1131                 {
1132                 ret = 1; /* points differ */
1133                 goto end;
1134                 }
1135
1136         /* points are equal */
1137         ret = 0;
1138
1139  end:
1140         BN_CTX_end(ctx);
1141         if (new_ctx != NULL)
1142                 BN_CTX_free(new_ctx);
1143         return ret;
1144         }
1145
1146
1147 int ec_GFp_simple_make_affine(const EC_GROUP *group, EC_POINT *point, BN_CTX *ctx)
1148         {
1149         BN_CTX *new_ctx = NULL;
1150         BIGNUM *x, *y;
1151         int ret = 0;
1152
1153         if (point->Z_is_one || EC_POINT_is_at_infinity(group, point))
1154                 return 1;
1155
1156         if (ctx == NULL)
1157                 {
1158                 ctx = new_ctx = BN_CTX_new();
1159                 if (ctx == NULL)
1160                         return 0;
1161                 }
1162
1163         BN_CTX_start(ctx);
1164         x = BN_CTX_get(ctx);
1165         y = BN_CTX_get(ctx);
1166         if (y == NULL) goto err;
1167
1168         if (!EC_POINT_get_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
1169         if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) goto err;
1170         if (!point->Z_is_one)
1171                 {
1172                 ECerr(EC_F_EC_GFP_SIMPLE_MAKE_AFFINE, ERR_R_INTERNAL_ERROR);
1173                 goto err;
1174                 }
1175         
1176         ret = 1;
1177
1178  err:
1179         BN_CTX_end(ctx);
1180         if (new_ctx != NULL)
1181                 BN_CTX_free(new_ctx);
1182         return ret;
1183         }
1184
1185
1186 int ec_GFp_simple_points_make_affine(const EC_GROUP *group, size_t num, EC_POINT *points[], BN_CTX *ctx)
1187         {
1188         BN_CTX *new_ctx = NULL;
1189         BIGNUM *tmp, *tmp_Z;
1190         BIGNUM **prod_Z = NULL;
1191         size_t i;
1192         int ret = 0;
1193
1194         if (num == 0)
1195                 return 1;
1196
1197         if (ctx == NULL)
1198                 {
1199                 ctx = new_ctx = BN_CTX_new();
1200                 if (ctx == NULL)
1201                         return 0;
1202                 }
1203
1204         BN_CTX_start(ctx);
1205         tmp = BN_CTX_get(ctx);
1206         tmp_Z = BN_CTX_get(ctx);
1207         if (tmp == NULL || tmp_Z == NULL) goto err;
1208
1209         prod_Z = OPENSSL_malloc(num * sizeof prod_Z[0]);
1210         if (prod_Z == NULL) goto err;
1211         for (i = 0; i < num; i++)
1212                 {
1213                 prod_Z[i] = BN_new();
1214                 if (prod_Z[i] == NULL) goto err;
1215                 }
1216
1217         /* Set each prod_Z[i] to the product of points[0]->Z .. points[i]->Z,
1218          * skipping any zero-valued inputs (pretend that they're 1). */
1219
1220         if (!BN_is_zero(&points[0]->Z))
1221                 {
1222                 if (!BN_copy(prod_Z[0], &points[0]->Z)) goto err;
1223                 }
1224         else
1225                 {
1226                 if (group->meth->field_set_to_one != 0)
1227                         {
1228                         if (!group->meth->field_set_to_one(group, prod_Z[0], ctx)) goto err;
1229                         }
1230                 else
1231                         {
1232                         if (!BN_one(prod_Z[0])) goto err;
1233                         }
1234                 }
1235
1236         for (i = 1; i < num; i++)
1237                 {
1238                 if (!BN_is_zero(&points[i]->Z))
1239                         {
1240                         if (!group->meth->field_mul(group, prod_Z[i], prod_Z[i - 1], &points[i]->Z, ctx)) goto err;
1241                         }
1242                 else
1243                         {
1244                         if (!BN_copy(prod_Z[i], prod_Z[i - 1])) goto err;
1245                         }
1246                 }
1247
1248         /* Now use a single explicit inversion to replace every
1249          * non-zero points[i]->Z by its inverse. */
1250
1251         if (!BN_mod_inverse(tmp, prod_Z[num - 1], &group->field, ctx))
1252                 {
1253                 ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE, ERR_R_BN_LIB);
1254                 goto err;
1255                 }
1256         if (group->meth->field_encode != 0)
1257                 {
1258                 /* In the Montgomery case, we just turned  R*H  (representing H)
1259                  * into  1/(R*H),  but we need  R*(1/H)  (representing 1/H);
1260                  * i.e. we need to multiply by the Montgomery factor twice. */
1261                 if (!group->meth->field_encode(group, tmp, tmp, ctx)) goto err;
1262                 if (!group->meth->field_encode(group, tmp, tmp, ctx)) goto err;
1263                 }
1264
1265         for (i = num - 1; i > 0; --i)
1266                 {
1267                 /* Loop invariant: tmp is the product of the inverses of
1268                  * points[0]->Z .. points[i]->Z (zero-valued inputs skipped). */
1269                 if (!BN_is_zero(&points[i]->Z))
1270                         {
1271                         /* Set tmp_Z to the inverse of points[i]->Z (as product
1272                          * of Z inverses 0 .. i, Z values 0 .. i - 1). */
1273                         if (!group->meth->field_mul(group, tmp_Z, prod_Z[i - 1], tmp, ctx)) goto err;
1274                         /* Update tmp to satisfy the loop invariant for i - 1. */
1275                         if (!group->meth->field_mul(group, tmp, tmp, &points[i]->Z, ctx)) goto err;
1276                         /* Replace points[i]->Z by its inverse. */
1277                         if (!BN_copy(&points[i]->Z, tmp_Z)) goto err;
1278                         }
1279                 }
1280
1281         if (!BN_is_zero(&points[0]->Z))
1282                 {
1283                 /* Replace points[0]->Z by its inverse. */
1284                 if (!BN_copy(&points[0]->Z, tmp)) goto err;
1285                 }
1286
1287         /* Finally, fix up the X and Y coordinates for all points. */
1288
1289         for (i = 0; i < num; i++)
1290                 {
1291                 EC_POINT *p = points[i];
1292
1293                 if (!BN_is_zero(&p->Z))
1294                         {
1295                         /* turn  (X, Y, 1/Z)  into  (X/Z^2, Y/Z^3, 1) */
1296
1297                         if (!group->meth->field_sqr(group, tmp, &p->Z, ctx)) goto err;
1298                         if (!group->meth->field_mul(group, &p->X, &p->X, tmp, ctx)) goto err;
1299
1300                         if (!group->meth->field_mul(group, tmp, tmp, &p->Z, ctx)) goto err;
1301                         if (!group->meth->field_mul(group, &p->Y, &p->Y, tmp, ctx)) goto err;
1302
1303                         if (group->meth->field_set_to_one != 0)
1304                                 {
1305                                 if (!group->meth->field_set_to_one(group, &p->Z, ctx)) goto err;
1306                                 }
1307                         else
1308                                 {
1309                                 if (!BN_one(&p->Z)) goto err;
1310                                 }
1311                         p->Z_is_one = 1;
1312                         }
1313                 }
1314
1315         ret = 1;
1316
1317  err:
1318         BN_CTX_end(ctx);
1319         if (new_ctx != NULL)
1320                 BN_CTX_free(new_ctx);
1321         if (prod_Z != NULL)
1322                 {
1323                 for (i = 0; i < num; i++)
1324                         {
1325                         if (prod_Z[i] == NULL) break;
1326                         BN_clear_free(prod_Z[i]);
1327                         }
1328                 OPENSSL_free(prod_Z);
1329                 }
1330         return ret;
1331         }
1332
1333
1334 int ec_GFp_simple_field_mul(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
1335         {
1336         return BN_mod_mul(r, a, b, &group->field, ctx);
1337         }
1338
1339
1340 int ec_GFp_simple_field_sqr(const EC_GROUP *group, BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
1341         {
1342         return BN_mod_sqr(r, a, &group->field, ctx);
1343         }