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