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 /* ====================================================================
5 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * openssl-core@openssl.org.
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com).
58 #include <openssl/err.h>
63 const EC_METHOD
*EC_GFp_simple_method(void)
65 static const EC_METHOD ret
= {
66 ec_GFp_simple_group_init
,
67 ec_GFp_simple_group_finish
,
68 ec_GFp_simple_group_clear_finish
,
69 ec_GFp_simple_group_copy
,
70 ec_GFp_simple_group_set_curve_GFp
,
71 ec_GFp_simple_group_get_curve_GFp
,
72 ec_GFp_simple_group_check_discriminant
,
73 ec_GFp_simple_point_init
,
74 ec_GFp_simple_point_finish
,
75 ec_GFp_simple_point_clear_finish
,
76 ec_GFp_simple_point_copy
,
77 ec_GFp_simple_point_set_to_infinity
,
78 ec_GFp_simple_set_Jprojective_coordinates_GFp
,
79 ec_GFp_simple_get_Jprojective_coordinates_GFp
,
80 ec_GFp_simple_point_set_affine_coordinates_GFp
,
81 ec_GFp_simple_point_get_affine_coordinates_GFp
,
82 ec_GFp_simple_set_compressed_coordinates_GFp
,
83 ec_GFp_simple_point2oct
,
84 ec_GFp_simple_oct2point
,
88 ec_GFp_simple_is_at_infinity
,
89 ec_GFp_simple_is_on_curve
,
91 ec_GFp_simple_make_affine
,
92 ec_GFp_simple_points_make_affine
,
93 ec_GFp_simple_field_mul
,
94 ec_GFp_simple_field_sqr
,
97 0 /* field_set_to_one */ };
103 int ec_GFp_simple_group_init(EC_GROUP
*group
)
105 BN_init(&group
->field
);
108 group
->a_is_minus3
= 0;
113 void ec_GFp_simple_group_finish(EC_GROUP
*group
)
115 BN_free(&group
->field
);
121 void ec_GFp_simple_group_clear_finish(EC_GROUP
*group
)
123 BN_clear_free(&group
->field
);
124 BN_clear_free(&group
->a
);
125 BN_clear_free(&group
->b
);
129 int ec_GFp_simple_group_copy(EC_GROUP
*dest
, const EC_GROUP
*src
)
131 if (!BN_copy(&dest
->field
, &src
->field
)) return 0;
132 if (!BN_copy(&dest
->a
, &src
->a
)) return 0;
133 if (!BN_copy(&dest
->b
, &src
->b
)) return 0;
135 dest
->a_is_minus3
= src
->a_is_minus3
;
141 int ec_GFp_simple_group_set_curve_GFp(EC_GROUP
*group
,
142 const BIGNUM
*p
, const BIGNUM
*a
, const BIGNUM
*b
, BN_CTX
*ctx
)
145 BN_CTX
*new_ctx
= NULL
;
148 /* p must be a prime > 3 */
149 if (BN_num_bits(p
) <= 2 || !BN_is_odd(p
))
151 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_SET_CURVE_GFP
, EC_R_INVALID_FIELD
);
157 ctx
= new_ctx
= BN_CTX_new();
163 tmp_a
= BN_CTX_get(ctx
);
164 if (tmp_a
== NULL
) goto err
;
167 if (!BN_copy(&group
->field
, p
)) goto err
;
168 group
->field
.neg
= 0;
171 if (!BN_nnmod(tmp_a
, a
, p
, ctx
)) goto err
;
172 if (group
->meth
->field_encode
)
173 { if (!group
->meth
->field_encode(group
, &group
->a
, tmp_a
, ctx
)) goto err
; }
175 if (!BN_copy(&group
->a
, tmp_a
)) goto err
;
178 if (!BN_nnmod(&group
->b
, b
, p
, ctx
)) goto err
;
179 if (group
->meth
->field_encode
)
180 if (!group
->meth
->field_encode(group
, &group
->b
, &group
->b
, ctx
)) goto err
;
182 /* group->a_is_minus3 */
183 if (!BN_add_word(tmp_a
, 3)) goto err
;
184 group
->a_is_minus3
= (0 == BN_cmp(tmp_a
, &group
->field
));
191 BN_CTX_free(new_ctx
);
196 int ec_GFp_simple_group_get_curve_GFp(const EC_GROUP
*group
, BIGNUM
*p
, BIGNUM
*a
, BIGNUM
*b
, BN_CTX
*ctx
)
199 BN_CTX
*new_ctx
= NULL
;
203 if (!BN_copy(p
, &group
->field
)) return 0;
206 if (a
!= NULL
|| b
!= NULL
)
208 if (group
->meth
->field_decode
)
212 ctx
= new_ctx
= BN_CTX_new();
218 if (!group
->meth
->field_decode(group
, a
, &group
->a
, ctx
)) goto err
;
222 if (!group
->meth
->field_decode(group
, b
, &group
->b
, ctx
)) goto err
;
229 if (!BN_copy(a
, &group
->a
)) goto err
;
233 if (!BN_copy(b
, &group
->b
)) goto err
;
242 BN_CTX_free(new_ctx
);
247 int ec_GFp_simple_group_check_discriminant(const EC_GROUP
*group
, BN_CTX
*ctx
)
250 BIGNUM
*a
,*b
,*order
,*tmp_1
,*tmp_2
;
251 const BIGNUM
*p
= &group
->field
;
252 BN_CTX
*new_ctx
= NULL
;
256 ctx
= new_ctx
= BN_CTX_new();
259 ECerr(EC_F_EC_GFP_SIMPLE_GROUP_CHECK_DISCRIMINANT
, ERR_R_MALLOC_FAILURE
);
266 tmp_1
= BN_CTX_get(ctx
);
267 tmp_2
= BN_CTX_get(ctx
);
268 order
= BN_CTX_get(ctx
);
269 if (order
== NULL
) goto err
;
271 if (group
->meth
->field_decode
)
273 if (!group
->meth
->field_decode(group
, a
, &group
->a
, ctx
)) goto err
;
274 if (!group
->meth
->field_decode(group
, b
, &group
->b
, ctx
)) goto err
;
278 if (!BN_copy(a
, &group
->a
)) goto err
;
279 if (!BN_copy(b
, &group
->b
)) goto err
;
282 /* check the discriminant:
283 * y^2 = x^3 + a*x + b is an elliptic curve <=> 4*a^3 + 27*b^2 != 0 (mod p)
287 if (BN_is_zero(b
)) goto err
;
289 else if (!BN_is_zero(b
))
291 if (!BN_mod_sqr(tmp_1
, a
, p
, ctx
)) goto err
;
292 if (!BN_mod_mul(tmp_2
, tmp_1
, a
, p
, ctx
)) goto err
;
293 if (!BN_lshift(tmp_1
, tmp_2
, 2)) goto err
;
296 if (!BN_mod_sqr(tmp_2
, b
, p
, ctx
)) goto err
;
297 if (!BN_mul_word(tmp_2
, 27)) goto err
;
300 if (!BN_mod_add(a
, tmp_1
, tmp_2
, p
, ctx
)) goto err
;
301 if (BN_is_zero(a
)) goto err
;
308 BN_CTX_free(new_ctx
);
313 int ec_GFp_simple_point_init(EC_POINT
*point
)
324 void ec_GFp_simple_point_finish(EC_POINT
*point
)
332 void ec_GFp_simple_point_clear_finish(EC_POINT
*point
)
334 BN_clear_free(&point
->X
);
335 BN_clear_free(&point
->Y
);
336 BN_clear_free(&point
->Z
);
341 int ec_GFp_simple_point_copy(EC_POINT
*dest
, const EC_POINT
*src
)
343 if (!BN_copy(&dest
->X
, &src
->X
)) return 0;
344 if (!BN_copy(&dest
->Y
, &src
->Y
)) return 0;
345 if (!BN_copy(&dest
->Z
, &src
->Z
)) return 0;
346 dest
->Z_is_one
= src
->Z_is_one
;
352 int ec_GFp_simple_point_set_to_infinity(const EC_GROUP
*group
, EC_POINT
*point
)
355 return (BN_zero(&point
->Z
));
359 int ec_GFp_simple_set_Jprojective_coordinates_GFp(const EC_GROUP
*group
, EC_POINT
*point
,
360 const BIGNUM
*x
, const BIGNUM
*y
, const BIGNUM
*z
, BN_CTX
*ctx
)
362 BN_CTX
*new_ctx
= NULL
;
367 ctx
= new_ctx
= BN_CTX_new();
374 if (!BN_nnmod(&point
->X
, x
, &group
->field
, ctx
)) goto err
;
375 if (group
->meth
->field_encode
)
377 if (!group
->meth
->field_encode(group
, &point
->X
, &point
->X
, ctx
)) goto err
;
383 if (!BN_nnmod(&point
->Y
, y
, &group
->field
, ctx
)) goto err
;
384 if (group
->meth
->field_encode
)
386 if (!group
->meth
->field_encode(group
, &point
->Y
, &point
->Y
, ctx
)) goto err
;
394 if (!BN_nnmod(&point
->Z
, z
, &group
->field
, ctx
)) goto err
;
395 Z_is_one
= BN_is_one(&point
->Z
);
396 if (group
->meth
->field_encode
)
398 if (Z_is_one
&& (group
->meth
->field_set_to_one
!= 0))
400 if (!group
->meth
->field_set_to_one(group
, &point
->Z
, ctx
)) goto err
;
404 if (!group
->meth
->field_encode(group
, &point
->Z
, &point
->Z
, ctx
)) goto err
;
407 point
->Z_is_one
= Z_is_one
;
414 BN_CTX_free(new_ctx
);
419 int ec_GFp_simple_get_Jprojective_coordinates_GFp(const EC_GROUP
*group
, const EC_POINT
*point
,
420 BIGNUM
*x
, BIGNUM
*y
, BIGNUM
*z
, BN_CTX
*ctx
)
422 BN_CTX
*new_ctx
= NULL
;
425 if (group
->meth
->field_decode
!= 0)
429 ctx
= new_ctx
= BN_CTX_new();
436 if (!group
->meth
->field_decode(group
, x
, &point
->X
, ctx
)) goto err
;
440 if (!group
->meth
->field_decode(group
, y
, &point
->Y
, ctx
)) goto err
;
444 if (!group
->meth
->field_decode(group
, z
, &point
->Z
, ctx
)) goto err
;
451 if (!BN_copy(x
, &point
->X
)) goto err
;
455 if (!BN_copy(y
, &point
->Y
)) goto err
;
459 if (!BN_copy(z
, &point
->Z
)) goto err
;
467 BN_CTX_free(new_ctx
);
472 int ec_GFp_simple_point_set_affine_coordinates_GFp(const EC_GROUP
*group
, EC_POINT
*point
,
473 const BIGNUM
*x
, const BIGNUM
*y
, BN_CTX
*ctx
)
475 if (x
== NULL
|| y
== NULL
)
477 /* unlike for projective coordinates, we do not tolerate this */
478 ECerr(EC_F_EC_GFP_SIMPLE_POINT_SET_AFFINE_COORDINATES_GFP
, ERR_R_PASSED_NULL_PARAMETER
);
482 return EC_POINT_set_Jprojective_coordinates_GFp(group
, point
, x
, y
, BN_value_one(), ctx
);
486 int ec_GFp_simple_point_get_affine_coordinates_GFp(const EC_GROUP
*group
, const EC_POINT
*point
,
487 BIGNUM
*x
, BIGNUM
*y
, BN_CTX
*ctx
)
489 BN_CTX
*new_ctx
= NULL
;
490 BIGNUM
*X
, *Y
, *Z
, *Z_1
, *Z_2
, *Z_3
;
491 const BIGNUM
*X_
, *Y_
, *Z_
;
494 if (EC_POINT_is_at_infinity(group
, point
))
496 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES_GFP
, EC_R_POINT_AT_INFINITY
);
502 ctx
= new_ctx
= BN_CTX_new();
511 Z_1
= BN_CTX_get(ctx
);
512 Z_2
= BN_CTX_get(ctx
);
513 Z_3
= BN_CTX_get(ctx
);
514 if (Z_3
== NULL
) goto err
;
516 /* transform (X, Y, Z) into (x, y) := (X/Z^2, Y/Z^3) */
518 if (group
->meth
->field_decode
)
520 if (!group
->meth
->field_decode(group
, X
, &point
->X
, ctx
)) goto err
;
521 if (!group
->meth
->field_decode(group
, Y
, &point
->Y
, ctx
)) goto err
;
522 if (!group
->meth
->field_decode(group
, Z
, &point
->Z
, ctx
)) goto err
;
523 X_
= X
; Y_
= Y
; Z_
= Z
;
536 if (!BN_copy(x
, X_
)) goto err
;
540 if (!BN_copy(y
, Y_
)) goto err
;
545 if (!BN_mod_inverse(Z_1
, Z_
, &group
->field
, ctx
))
547 ECerr(EC_F_EC_GFP_SIMPLE_POINT_GET_AFFINE_COORDINATES_GFP
, ERR_R_BN_LIB
);
551 if (group
->meth
->field_encode
== 0)
553 /* field_sqr works on standard representation */
554 if (!group
->meth
->field_sqr(group
, Z_2
, Z_1
, ctx
)) goto err
;
558 if (!BN_mod_sqr(Z_2
, Z_1
, &group
->field
, ctx
)) goto err
;
563 if (group
->meth
->field_encode
== 0)
565 /* field_mul works on standard representation */
566 if (!group
->meth
->field_mul(group
, x
, X_
, Z_2
, ctx
)) goto err
;
570 if (!BN_mod_mul(x
, X_
, Z_2
, &group
->field
, ctx
)) goto err
;
576 if (group
->meth
->field_encode
== 0)
578 /* field_mul works on standard representation */
579 if (!group
->meth
->field_mul(group
, Z_3
, Z_2
, Z_1
, ctx
)) goto err
;
580 if (!group
->meth
->field_mul(group
, y
, Y_
, Z_3
, ctx
)) goto err
;
585 if (!BN_mod_mul(Z_3
, Z_2
, Z_1
, &group
->field
, ctx
)) goto err
;
586 if (!BN_mod_mul(y
, Y_
, Z_3
, &group
->field
, ctx
)) goto err
;
596 BN_CTX_free(new_ctx
);
601 int ec_GFp_simple_set_compressed_coordinates_GFp(const EC_GROUP
*group
, EC_POINT
*point
,
602 const BIGNUM
*x_
, int y_bit
, BN_CTX
*ctx
)
604 BN_CTX
*new_ctx
= NULL
;
605 BIGNUM
*tmp1
, *tmp2
, *x
, *y
;
610 ctx
= new_ctx
= BN_CTX_new();
615 y_bit
= (y_bit
!= 0);
618 tmp1
= BN_CTX_get(ctx
);
619 tmp2
= BN_CTX_get(ctx
);
622 if (y
== NULL
) goto err
;
624 /* Recover y. We have a Weierstrass equation
625 * y^2 = x^3 + a*x + b,
626 * so y is one of the square roots of x^3 + a*x + b.
630 if (!BN_nnmod(x
, x_
, &group
->field
,ctx
)) goto err
;
631 if (group
->meth
->field_decode
== 0)
633 /* field_{sqr,mul} work on standard representation */
634 if (!group
->meth
->field_sqr(group
, tmp2
, x_
, ctx
)) goto err
;
635 if (!group
->meth
->field_mul(group
, tmp1
, tmp2
, x_
, ctx
)) goto err
;
639 if (!BN_mod_sqr(tmp2
, x_
, &group
->field
, ctx
)) goto err
;
640 if (!BN_mod_mul(tmp1
, tmp2
, x_
, &group
->field
, ctx
)) goto err
;
643 /* tmp1 := tmp1 + a*x */
644 if (group
->a_is_minus3
)
646 if (!BN_mod_lshift1_quick(tmp2
, x
, &group
->field
)) goto err
;
647 if (!BN_mod_add_quick(tmp2
, tmp2
, x
, &group
->field
)) goto err
;
648 if (!BN_mod_sub_quick(tmp1
, tmp1
, tmp2
, &group
->field
)) goto err
;
652 if (group
->meth
->field_decode
)
654 if (!group
->meth
->field_decode(group
, tmp2
, &group
->a
, ctx
)) goto err
;
655 if (!BN_mod_mul(tmp2
, tmp2
, x
, &group
->field
, ctx
)) goto err
;
659 /* field_mul works on standard representation */
660 if (!group
->meth
->field_mul(group
, tmp2
, &group
->a
, x
, ctx
)) goto err
;
663 if (!BN_mod_add_quick(tmp1
, tmp1
, tmp2
, &group
->field
)) goto err
;
666 /* tmp1 := tmp1 + b */
667 if (group
->meth
->field_decode
)
669 if (!group
->meth
->field_decode(group
, tmp2
, &group
->b
, ctx
)) goto err
;
670 if (!BN_mod_add_quick(tmp1
, tmp1
, tmp2
, &group
->field
)) goto err
;
674 if (!BN_mod_add_quick(tmp1
, tmp1
, &group
->b
, &group
->field
)) goto err
;
677 if (!BN_mod_sqrt(y
, tmp1
, &group
->field
, ctx
))
679 unsigned long err
= ERR_peek_error();
681 if (ERR_GET_LIB(err
) == ERR_LIB_BN
&& ERR_GET_REASON(err
) == BN_R_NOT_A_SQUARE
)
683 (void)ERR_get_error();
684 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP
, EC_R_INVALID_COMPRESSED_POINT
);
687 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP
, ERR_R_BN_LIB
);
690 /* If tmp1 is not a square (i.e. there is no point on the curve with
691 * our x), then y now is a nonsense value too */
693 if (y_bit
!= BN_is_odd(y
))
699 kron
= BN_kronecker(x
, &group
->field
, ctx
);
700 if (kron
== -2) goto err
;
703 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP
, EC_R_INVALID_COMPRESSION_BIT
);
705 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP
, EC_R_INVALID_COMPRESSED_POINT
);
708 if (!BN_usub(y
, &group
->field
, y
)) goto err
;
710 if (y_bit
!= BN_is_odd(y
))
712 ECerr(EC_F_EC_GFP_SIMPLE_SET_COMPRESSED_COORDINATES_GFP
, ERR_R_INTERNAL_ERROR
);
716 if (!EC_POINT_set_affine_coordinates_GFp(group
, point
, x
, y
, ctx
)) goto err
;
723 BN_CTX_free(new_ctx
);
728 size_t ec_GFp_simple_point2oct(const EC_GROUP
*group
, const EC_POINT
*point
, point_conversion_form_t form
,
729 unsigned char *buf
, size_t len
, BN_CTX
*ctx
)
732 BN_CTX
*new_ctx
= NULL
;
735 size_t field_len
, i
, skip
;
737 if ((form
!= POINT_CONVERSION_COMPRESSED
)
738 && (form
!= POINT_CONVERSION_UNCOMPRESSED
)
739 && (form
!= POINT_CONVERSION_HYBRID
))
741 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT
, EC_R_INVALID_FORM
);
745 if (EC_POINT_is_at_infinity(group
, point
))
747 /* encodes to a single 0 octet */
752 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT
, EC_R_BUFFER_TOO_SMALL
);
761 /* ret := required output buffer length */
762 field_len
= BN_num_bytes(&group
->field
);
763 ret
= (form
== POINT_CONVERSION_COMPRESSED
) ? 1 + field_len
: 1 + 2*field_len
;
765 /* if 'buf' is NULL, just return required length */
770 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT
, EC_R_BUFFER_TOO_SMALL
);
776 ctx
= new_ctx
= BN_CTX_new();
785 if (y
== NULL
) goto err
;
787 if (!EC_POINT_get_affine_coordinates_GFp(group
, point
, x
, y
, ctx
)) goto err
;
789 if ((form
== POINT_CONVERSION_COMPRESSED
|| form
== POINT_CONVERSION_HYBRID
) && BN_is_odd(y
))
796 skip
= field_len
- BN_num_bytes(x
);
797 if (skip
> field_len
)
799 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT
, ERR_R_INTERNAL_ERROR
);
807 skip
= BN_bn2bin(x
, buf
+ i
);
809 if (i
!= 1 + field_len
)
811 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT
, ERR_R_INTERNAL_ERROR
);
815 if (form
== POINT_CONVERSION_UNCOMPRESSED
|| form
== POINT_CONVERSION_HYBRID
)
817 skip
= field_len
- BN_num_bytes(y
);
818 if (skip
> field_len
)
820 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT
, ERR_R_INTERNAL_ERROR
);
828 skip
= BN_bn2bin(y
, buf
+ i
);
834 ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT
, ERR_R_INTERNAL_ERROR
);
842 BN_CTX_free(new_ctx
);
849 BN_CTX_free(new_ctx
);
854 int ec_GFp_simple_oct2point(const EC_GROUP
*group
, EC_POINT
*point
,
855 const unsigned char *buf
, size_t len
, BN_CTX
*ctx
)
857 point_conversion_form_t form
;
859 BN_CTX
*new_ctx
= NULL
;
861 size_t field_len
, enc_len
;
866 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT
, EC_R_BUFFER_TOO_SMALL
);
872 if ((form
!= 0) && (form
!= POINT_CONVERSION_COMPRESSED
)
873 && (form
!= POINT_CONVERSION_UNCOMPRESSED
)
874 && (form
!= POINT_CONVERSION_HYBRID
))
876 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT
, EC_R_INVALID_ENCODING
);
879 if ((form
== 0 || form
== POINT_CONVERSION_UNCOMPRESSED
) && y_bit
)
881 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT
, EC_R_INVALID_ENCODING
);
889 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT
, EC_R_INVALID_ENCODING
);
893 return EC_POINT_set_to_infinity(group
, point
);
896 field_len
= BN_num_bytes(&group
->field
);
897 enc_len
= (form
== POINT_CONVERSION_COMPRESSED
) ? 1 + field_len
: 1 + 2*field_len
;
901 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT
, EC_R_INVALID_ENCODING
);
907 ctx
= new_ctx
= BN_CTX_new();
915 if (y
== NULL
) goto err
;
917 if (!BN_bin2bn(buf
+ 1, field_len
, x
)) goto err
;
918 if (BN_ucmp(x
, &group
->field
) >= 0)
920 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT
, EC_R_INVALID_ENCODING
);
924 if (form
== POINT_CONVERSION_COMPRESSED
)
926 if (!EC_POINT_set_compressed_coordinates_GFp(group
, point
, x
, y_bit
, ctx
)) goto err
;
930 if (!BN_bin2bn(buf
+ 1 + field_len
, field_len
, y
)) goto err
;
931 if (BN_ucmp(y
, &group
->field
) >= 0)
933 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT
, EC_R_INVALID_ENCODING
);
936 if (form
== POINT_CONVERSION_HYBRID
)
938 if (y_bit
!= BN_is_odd(y
))
940 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT
, EC_R_INVALID_ENCODING
);
945 if (!EC_POINT_set_affine_coordinates_GFp(group
, point
, x
, y
, ctx
)) goto err
;
948 if (!EC_POINT_is_on_curve(group
, point
, ctx
)) /* test required by X9.62 */
950 ECerr(EC_F_EC_GFP_SIMPLE_OCT2POINT
, EC_R_POINT_IS_NOT_ON_CURVE
);
959 BN_CTX_free(new_ctx
);
964 int ec_GFp_simple_add(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
, const EC_POINT
*b
, BN_CTX
*ctx
)
966 int (*field_mul
)(const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, const BIGNUM
*, BN_CTX
*);
967 int (*field_sqr
)(const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
969 BN_CTX
*new_ctx
= NULL
;
970 BIGNUM
*n0
, *n1
, *n2
, *n3
, *n4
, *n5
, *n6
;
974 return EC_POINT_dbl(group
, r
, a
, ctx
);
975 if (EC_POINT_is_at_infinity(group
, a
))
976 return EC_POINT_copy(r
, b
);
977 if (EC_POINT_is_at_infinity(group
, b
))
978 return EC_POINT_copy(r
, a
);
980 field_mul
= group
->meth
->field_mul
;
981 field_sqr
= group
->meth
->field_sqr
;
986 ctx
= new_ctx
= BN_CTX_new();
992 n0
= BN_CTX_get(ctx
);
993 n1
= BN_CTX_get(ctx
);
994 n2
= BN_CTX_get(ctx
);
995 n3
= BN_CTX_get(ctx
);
996 n4
= BN_CTX_get(ctx
);
997 n5
= BN_CTX_get(ctx
);
998 n6
= BN_CTX_get(ctx
);
999 if (n6
== NULL
) goto end
;
1001 /* Note that in this function we must not read components of 'a' or 'b'
1002 * once we have written the corresponding components of 'r'.
1003 * ('r' might be one of 'a' or 'b'.)
1009 if (!BN_copy(n1
, &a
->X
)) goto end
;
1010 if (!BN_copy(n2
, &a
->Y
)) goto end
;
1016 if (!field_sqr(group
, n0
, &b
->Z
, ctx
)) goto end
;
1017 if (!field_mul(group
, n1
, &a
->X
, n0
, ctx
)) goto end
;
1018 /* n1 = X_a * Z_b^2 */
1020 if (!field_mul(group
, n0
, n0
, &b
->Z
, ctx
)) goto end
;
1021 if (!field_mul(group
, n2
, &a
->Y
, n0
, ctx
)) goto end
;
1022 /* n2 = Y_a * Z_b^3 */
1028 if (!BN_copy(n3
, &b
->X
)) goto end
;
1029 if (!BN_copy(n4
, &b
->Y
)) goto end
;
1035 if (!field_sqr(group
, n0
, &a
->Z
, ctx
)) goto end
;
1036 if (!field_mul(group
, n3
, &b
->X
, n0
, ctx
)) goto end
;
1037 /* n3 = X_b * Z_a^2 */
1039 if (!field_mul(group
, n0
, n0
, &a
->Z
, ctx
)) goto end
;
1040 if (!field_mul(group
, n4
, &b
->Y
, n0
, ctx
)) goto end
;
1041 /* n4 = Y_b * Z_a^3 */
1045 if (!BN_mod_sub_quick(n5
, n1
, n3
, p
)) goto end
;
1046 if (!BN_mod_sub_quick(n6
, n2
, n4
, p
)) goto end
;
1054 /* a is the same point as b */
1056 ret
= EC_POINT_dbl(group
, r
, a
, ctx
);
1062 /* a is the inverse of b */
1063 if (!BN_zero(&r
->Z
)) goto end
;
1071 if (!BN_mod_add_quick(n1
, n1
, n3
, p
)) goto end
;
1072 if (!BN_mod_add_quick(n2
, n2
, n4
, p
)) goto end
;
1073 /* 'n7' = n1 + n3 */
1074 /* 'n8' = n2 + n4 */
1077 if (a
->Z_is_one
&& b
->Z_is_one
)
1079 if (!BN_copy(&r
->Z
, n5
)) goto end
;
1084 { if (!BN_copy(n0
, &b
->Z
)) goto end
; }
1085 else if (b
->Z_is_one
)
1086 { if (!BN_copy(n0
, &a
->Z
)) goto end
; }
1088 { if (!field_mul(group
, n0
, &a
->Z
, &b
->Z
, ctx
)) goto end
; }
1089 if (!field_mul(group
, &r
->Z
, n0
, n5
, ctx
)) goto end
;
1092 /* Z_r = Z_a * Z_b * n5 */
1095 if (!field_sqr(group
, n0
, n6
, ctx
)) goto end
;
1096 if (!field_sqr(group
, n4
, n5
, ctx
)) goto end
;
1097 if (!field_mul(group
, n3
, n1
, n4
, ctx
)) goto end
;
1098 if (!BN_mod_sub_quick(&r
->X
, n0
, n3
, p
)) goto end
;
1099 /* X_r = n6^2 - n5^2 * 'n7' */
1102 if (!BN_mod_lshift1_quick(n0
, &r
->X
, p
)) goto end
;
1103 if (!BN_mod_sub_quick(n0
, n3
, n0
, p
)) goto end
;
1104 /* n9 = n5^2 * 'n7' - 2 * X_r */
1107 if (!field_mul(group
, n0
, n0
, n6
, ctx
)) goto end
;
1108 if (!field_mul(group
, n5
, n4
, n5
, ctx
)) goto end
; /* now n5 is n5^3 */
1109 if (!field_mul(group
, n1
, n2
, n5
, ctx
)) goto end
;
1110 if (!BN_mod_sub_quick(n0
, n0
, n1
, p
)) goto end
;
1112 if (!BN_add(n0
, n0
, p
)) goto end
;
1113 /* now 0 <= n0 < 2*p, and n0 is even */
1114 if (!BN_rshift1(&r
->Y
, n0
)) goto end
;
1115 /* Y_r = (n6 * 'n9' - 'n8' * 'n5^3') / 2 */
1120 if (ctx
) /* otherwise we already called BN_CTX_end */
1122 if (new_ctx
!= NULL
)
1123 BN_CTX_free(new_ctx
);
1128 int ec_GFp_simple_dbl(const EC_GROUP
*group
, EC_POINT
*r
, const EC_POINT
*a
, BN_CTX
*ctx
)
1130 int (*field_mul
)(const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, const BIGNUM
*, BN_CTX
*);
1131 int (*field_sqr
)(const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
1133 BN_CTX
*new_ctx
= NULL
;
1134 BIGNUM
*n0
, *n1
, *n2
, *n3
;
1137 if (EC_POINT_is_at_infinity(group
, a
))
1139 if (!BN_zero(&r
->Z
)) return 0;
1144 field_mul
= group
->meth
->field_mul
;
1145 field_sqr
= group
->meth
->field_sqr
;
1150 ctx
= new_ctx
= BN_CTX_new();
1156 n0
= BN_CTX_get(ctx
);
1157 n1
= BN_CTX_get(ctx
);
1158 n2
= BN_CTX_get(ctx
);
1159 n3
= BN_CTX_get(ctx
);
1160 if (n3
== NULL
) goto err
;
1162 /* Note that in this function we must not read components of 'a'
1163 * once we have written the corresponding components of 'r'.
1164 * ('r' might the same as 'a'.)
1170 if (!field_sqr(group
, n0
, &a
->X
, ctx
)) goto err
;
1171 if (!BN_mod_lshift1_quick(n1
, n0
, p
)) goto err
;
1172 if (!BN_mod_add_quick(n0
, n0
, n1
, p
)) goto err
;
1173 if (!BN_mod_add_quick(n1
, n0
, &group
->a
, p
)) goto err
;
1174 /* n1 = 3 * X_a^2 + a_curve */
1176 else if (group
->a_is_minus3
)
1178 if (!field_sqr(group
, n1
, &a
->Z
, ctx
)) goto err
;
1179 if (!BN_mod_add_quick(n0
, &a
->X
, n1
, p
)) goto err
;
1180 if (!BN_mod_sub_quick(n2
, &a
->X
, n1
, p
)) goto err
;
1181 if (!field_mul(group
, n1
, n0
, n2
, ctx
)) goto err
;
1182 if (!BN_mod_lshift1_quick(n0
, n1
, p
)) goto err
;
1183 if (!BN_mod_add_quick(n1
, n0
, n1
, p
)) goto err
;
1184 /* n1 = 3 * (X_a + Z_a^2) * (X_a - Z_a^2)
1185 * = 3 * X_a^2 - 3 * Z_a^4 */
1189 if (!field_sqr(group
, n0
, &a
->X
, ctx
)) goto err
;
1190 if (!BN_mod_lshift1_quick(n1
, n0
, p
)) goto err
;
1191 if (!BN_mod_add_quick(n0
, n0
, n1
, p
)) goto err
;
1192 if (!field_sqr(group
, n1
, &a
->Z
, ctx
)) goto err
;
1193 if (!field_sqr(group
, n1
, n1
, ctx
)) goto err
;
1194 if (!field_mul(group
, n1
, n1
, &group
->a
, ctx
)) goto err
;
1195 if (!BN_mod_add_quick(n1
, n1
, n0
, p
)) goto err
;
1196 /* n1 = 3 * X_a^2 + a_curve * Z_a^4 */
1202 if (!BN_copy(n0
, &a
->Y
)) goto err
;
1206 if (!field_mul(group
, n0
, &a
->Y
, &a
->Z
, ctx
)) goto err
;
1208 if (!BN_mod_lshift1_quick(&r
->Z
, n0
, p
)) goto err
;
1210 /* Z_r = 2 * Y_a * Z_a */
1213 if (!field_sqr(group
, n3
, &a
->Y
, ctx
)) goto err
;
1214 if (!field_mul(group
, n2
, &a
->X
, n3
, ctx
)) goto err
;
1215 if (!BN_mod_lshift_quick(n2
, n2
, 2, p
)) goto err
;
1216 /* n2 = 4 * X_a * Y_a^2 */
1219 if (!BN_mod_lshift1_quick(n0
, n2
, p
)) goto err
;
1220 if (!field_sqr(group
, &r
->X
, n1
, ctx
)) goto err
;
1221 if (!BN_mod_sub_quick(&r
->X
, &r
->X
, n0
, p
)) goto err
;
1222 /* X_r = n1^2 - 2 * n2 */
1225 if (!field_sqr(group
, n0
, n3
, ctx
)) goto err
;
1226 if (!BN_mod_lshift_quick(n3
, n0
, 3, p
)) goto err
;
1227 /* n3 = 8 * Y_a^4 */
1230 if (!BN_mod_sub_quick(n0
, n2
, &r
->X
, p
)) goto err
;
1231 if (!field_mul(group
, n0
, n1
, n0
, ctx
)) goto err
;
1232 if (!BN_mod_sub_quick(&r
->Y
, n0
, n3
, p
)) goto err
;
1233 /* Y_r = n1 * (n2 - X_r) - n3 */
1239 if (new_ctx
!= NULL
)
1240 BN_CTX_free(new_ctx
);
1245 int ec_GFp_simple_invert(const EC_GROUP
*group
, EC_POINT
*point
, BN_CTX
*ctx
)
1247 if (EC_POINT_is_at_infinity(group
, point
) || BN_is_zero(&point
->Y
))
1248 /* point is its own inverse */
1251 return BN_usub(&point
->Y
, &group
->field
, &point
->Y
);
1255 int ec_GFp_simple_is_at_infinity(const EC_GROUP
*group
, const EC_POINT
*point
)
1257 return BN_is_zero(&point
->Z
);
1261 int ec_GFp_simple_is_on_curve(const EC_GROUP
*group
, const EC_POINT
*point
, BN_CTX
*ctx
)
1263 int (*field_mul
)(const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, const BIGNUM
*, BN_CTX
*);
1264 int (*field_sqr
)(const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
1266 BN_CTX
*new_ctx
= NULL
;
1267 BIGNUM
*rh
, *tmp1
, *tmp2
, *Z4
, *Z6
;
1270 if (EC_POINT_is_at_infinity(group
, point
))
1273 field_mul
= group
->meth
->field_mul
;
1274 field_sqr
= group
->meth
->field_sqr
;
1279 ctx
= new_ctx
= BN_CTX_new();
1285 rh
= BN_CTX_get(ctx
);
1286 tmp1
= BN_CTX_get(ctx
);
1287 tmp2
= BN_CTX_get(ctx
);
1288 Z4
= BN_CTX_get(ctx
);
1289 Z6
= BN_CTX_get(ctx
);
1290 if (Z6
== NULL
) goto err
;
1292 /* We have a curve defined by a Weierstrass equation
1293 * y^2 = x^3 + a*x + b.
1294 * The point to consider is given in Jacobian projective coordinates
1295 * where (X, Y, Z) represents (x, y) = (X/Z^2, Y/Z^3).
1296 * Substituting this and multiplying by Z^6 transforms the above equation into
1297 * Y^2 = X^3 + a*X*Z^4 + b*Z^6.
1298 * To test this, we add up the right-hand side in 'rh'.
1302 if (!field_sqr(group
, rh
, &point
->X
, ctx
)) goto err
;
1303 if (!field_mul(group
, rh
, rh
, &point
->X
, ctx
)) goto err
;
1305 if (!point
->Z_is_one
)
1307 if (!field_sqr(group
, tmp1
, &point
->Z
, ctx
)) goto err
;
1308 if (!field_sqr(group
, Z4
, tmp1
, ctx
)) goto err
;
1309 if (!field_mul(group
, Z6
, Z4
, tmp1
, ctx
)) goto err
;
1311 /* rh := rh + a*X*Z^4 */
1312 if (!field_mul(group
, tmp1
, &point
->X
, Z4
, ctx
)) goto err
;
1313 if (group
->a_is_minus3
)
1315 if (!BN_mod_lshift1_quick(tmp2
, tmp1
, p
)) goto err
;
1316 if (!BN_mod_add_quick(tmp2
, tmp2
, tmp1
, p
)) goto err
;
1317 if (!BN_mod_sub_quick(rh
, rh
, tmp2
, p
)) goto err
;
1321 if (!field_mul(group
, tmp2
, tmp1
, &group
->a
, ctx
)) goto err
;
1322 if (!BN_mod_add_quick(rh
, rh
, tmp2
, p
)) goto err
;
1325 /* rh := rh + b*Z^6 */
1326 if (!field_mul(group
, tmp1
, &group
->b
, Z6
, ctx
)) goto err
;
1327 if (!BN_mod_add_quick(rh
, rh
, tmp1
, p
)) goto err
;
1331 /* point->Z_is_one */
1333 /* rh := rh + a*X */
1334 if (group
->a_is_minus3
)
1336 if (!BN_mod_lshift1_quick(tmp2
, &point
->X
, p
)) goto err
;
1337 if (!BN_mod_add_quick(tmp2
, tmp2
, &point
->X
, p
)) goto err
;
1338 if (!BN_mod_sub_quick(rh
, rh
, tmp2
, p
)) goto err
;
1342 if (!field_mul(group
, tmp2
, &point
->X
, &group
->a
, ctx
)) goto err
;
1343 if (!BN_mod_add_quick(rh
, rh
, tmp2
, p
)) goto err
;
1347 if (!BN_mod_add_quick(rh
, rh
, &group
->b
, p
)) goto err
;
1351 if (!field_sqr(group
, tmp1
, &point
->Y
, ctx
)) goto err
;
1353 ret
= (0 == BN_cmp(tmp1
, rh
));
1357 if (new_ctx
!= NULL
)
1358 BN_CTX_free(new_ctx
);
1363 int ec_GFp_simple_cmp(const EC_GROUP
*group
, const EC_POINT
*a
, const EC_POINT
*b
, BN_CTX
*ctx
)
1367 * 0 equal (in affine coordinates)
1371 int (*field_mul
)(const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, const BIGNUM
*, BN_CTX
*);
1372 int (*field_sqr
)(const EC_GROUP
*, BIGNUM
*, const BIGNUM
*, BN_CTX
*);
1373 BN_CTX
*new_ctx
= NULL
;
1374 BIGNUM
*tmp1
, *tmp2
, *Za23
, *Zb23
;
1375 const BIGNUM
*tmp1_
, *tmp2_
;
1378 if (EC_POINT_is_at_infinity(group
, a
))
1380 return EC_POINT_is_at_infinity(group
, b
) ? 0 : 1;
1383 if (a
->Z_is_one
&& b
->Z_is_one
)
1385 return ((BN_cmp(&a
->X
, &b
->X
) == 0) && BN_cmp(&a
->Y
, &b
->Y
) == 0) ? 0 : 1;
1388 field_mul
= group
->meth
->field_mul
;
1389 field_sqr
= group
->meth
->field_sqr
;
1393 ctx
= new_ctx
= BN_CTX_new();
1399 tmp1
= BN_CTX_get(ctx
);
1400 tmp2
= BN_CTX_get(ctx
);
1401 Za23
= BN_CTX_get(ctx
);
1402 Zb23
= BN_CTX_get(ctx
);
1403 if (Zb23
== NULL
) goto end
;
1405 /* We have to decide whether
1406 * (X_a/Z_a^2, Y_a/Z_a^3) = (X_b/Z_b^2, Y_b/Z_b^3),
1407 * or equivalently, whether
1408 * (X_a*Z_b^2, Y_a*Z_b^3) = (X_b*Z_a^2, Y_b*Z_a^3).
1413 if (!field_sqr(group
, Zb23
, &b
->Z
, ctx
)) goto end
;
1414 if (!field_mul(group
, tmp1
, &a
->X
, Zb23
, ctx
)) goto end
;
1421 if (!field_sqr(group
, Za23
, &a
->Z
, ctx
)) goto end
;
1422 if (!field_mul(group
, tmp2
, &b
->X
, Za23
, ctx
)) goto end
;
1428 /* compare X_a*Z_b^2 with X_b*Z_a^2 */
1429 if (BN_cmp(tmp1_
, tmp2_
) != 0)
1431 ret
= 1; /* points differ */
1438 if (!field_mul(group
, Zb23
, Zb23
, &b
->Z
, ctx
)) goto end
;
1439 if (!field_mul(group
, tmp1
, &a
->Y
, Zb23
, ctx
)) goto end
;
1446 if (!field_mul(group
, Za23
, Za23
, &a
->Z
, ctx
)) goto end
;
1447 if (!field_mul(group
, tmp2
, &b
->Y
, Za23
, ctx
)) goto end
;
1453 /* compare Y_a*Z_b^3 with Y_b*Z_a^3 */
1454 if (BN_cmp(tmp1_
, tmp2_
) != 0)
1456 ret
= 1; /* points differ */
1460 /* points are equal */
1465 if (new_ctx
!= NULL
)
1466 BN_CTX_free(new_ctx
);
1471 int ec_GFp_simple_make_affine(const EC_GROUP
*group
, EC_POINT
*point
, BN_CTX
*ctx
)
1473 BN_CTX
*new_ctx
= NULL
;
1477 if (point
->Z_is_one
|| EC_POINT_is_at_infinity(group
, point
))
1482 ctx
= new_ctx
= BN_CTX_new();
1488 x
= BN_CTX_get(ctx
);
1489 y
= BN_CTX_get(ctx
);
1490 if (y
== NULL
) goto err
;
1492 if (!EC_POINT_get_affine_coordinates_GFp(group
, point
, x
, y
, ctx
)) goto err
;
1493 if (!EC_POINT_set_affine_coordinates_GFp(group
, point
, x
, y
, ctx
)) goto err
;
1494 if (!point
->Z_is_one
)
1496 ECerr(EC_F_EC_GFP_SIMPLE_MAKE_AFFINE
, ERR_R_INTERNAL_ERROR
);
1504 if (new_ctx
!= NULL
)
1505 BN_CTX_free(new_ctx
);
1510 int ec_GFp_simple_points_make_affine(const EC_GROUP
*group
, size_t num
, EC_POINT
*points
[], BN_CTX
*ctx
)
1512 BN_CTX
*new_ctx
= NULL
;
1513 BIGNUM
*tmp0
, *tmp1
;
1515 BIGNUM
**heap
= NULL
;
1524 ctx
= new_ctx
= BN_CTX_new();
1530 tmp0
= BN_CTX_get(ctx
);
1531 tmp1
= BN_CTX_get(ctx
);
1532 if (tmp0
== NULL
|| tmp1
== NULL
) goto err
;
1534 /* Before converting the individual points, compute inverses of all Z values.
1535 * Modular inversion is rather slow, but luckily we can do with a single
1536 * explicit inversion, plus about 3 multiplications per input value.
1542 /* Now pow2 is the smallest power of 2 satifsying pow2 >= num.
1543 * We need twice that. */
1546 heap
= OPENSSL_malloc(pow2
* sizeof heap
[0]);
1547 if (heap
== NULL
) goto err
;
1549 /* The array is used as a binary tree, exactly as in heapsort:
1553 * heap[4] heap[5] heap[6] heap[7]
1554 * heap[8]heap[9] heap[10]heap[11] heap[12]heap[13] heap[14] heap[15]
1556 * We put the Z's in the last line;
1557 * then we set each other node to the product of its two child-nodes (where
1558 * empty or 0 entries are treated as ones);
1559 * then we invert heap[1];
1560 * then we invert each other node by replacing it by the product of its
1561 * parent (after inversion) and its sibling (before inversion).
1564 for (i
= pow2
/2 - 1; i
> 0; i
--)
1566 for (i
= 0; i
< num
; i
++)
1567 heap
[pow2
/2 + i
] = &points
[i
]->Z
;
1568 for (i
= pow2
/2 + num
; i
< pow2
; i
++)
1571 /* set each node to the product of its children */
1572 for (i
= pow2
/2 - 1; i
> 0; i
--)
1575 if (heap
[i
] == NULL
) goto err
;
1577 if (heap
[2*i
] != NULL
)
1579 if ((heap
[2*i
+ 1] == NULL
) || BN_is_zero(heap
[2*i
+ 1]))
1581 if (!BN_copy(heap
[i
], heap
[2*i
])) goto err
;
1585 if (BN_is_zero(heap
[2*i
]))
1587 if (!BN_copy(heap
[i
], heap
[2*i
+ 1])) goto err
;
1591 if (!group
->meth
->field_mul(group
, heap
[i
],
1592 heap
[2*i
], heap
[2*i
+ 1], ctx
)) goto err
;
1598 /* invert heap[1] */
1599 if (!BN_is_zero(heap
[1]))
1601 if (!BN_mod_inverse(heap
[1], heap
[1], &group
->field
, ctx
))
1603 ECerr(EC_F_EC_GFP_SIMPLE_POINTS_MAKE_AFFINE
, ERR_R_BN_LIB
);
1607 if (group
->meth
->field_encode
!= 0)
1609 /* in the Montgomery case, we just turned R*H (representing H)
1610 * into 1/(R*H), but we need R*(1/H) (representing 1/H);
1611 * i.e. we have need to multiply by the Montgomery factor twice */
1612 if (!group
->meth
->field_encode(group
, heap
[1], heap
[1], ctx
)) goto err
;
1613 if (!group
->meth
->field_encode(group
, heap
[1], heap
[1], ctx
)) goto err
;
1616 /* set other heap[i]'s to their inverses */
1617 for (i
= 2; i
< pow2
/2 + num
; i
+= 2)
1620 if ((heap
[i
+ 1] != NULL
) && !BN_is_zero(heap
[i
+ 1]))
1622 if (!group
->meth
->field_mul(group
, tmp0
, heap
[i
/2], heap
[i
+ 1], ctx
)) goto err
;
1623 if (!group
->meth
->field_mul(group
, tmp1
, heap
[i
/2], heap
[i
], ctx
)) goto err
;
1624 if (!BN_copy(heap
[i
], tmp0
)) goto err
;
1625 if (!BN_copy(heap
[i
+ 1], tmp1
)) goto err
;
1629 if (!BN_copy(heap
[i
], heap
[i
/2])) goto err
;
1633 /* we have replaced all non-zero Z's by their inverses, now fix up all the points */
1634 for (i
= 0; i
< num
; i
++)
1636 EC_POINT
*p
= points
[i
];
1638 if (!BN_is_zero(&p
->Z
))
1640 /* turn (X, Y, 1/Z) into (X/Z^2, Y/Z^3, 1) */
1642 if (!group
->meth
->field_sqr(group
, tmp1
, &p
->Z
, ctx
)) goto err
;
1643 if (!group
->meth
->field_mul(group
, &p
->X
, &p
->X
, tmp1
, ctx
)) goto err
;
1645 if (!group
->meth
->field_mul(group
, tmp1
, tmp1
, &p
->Z
, ctx
)) goto err
;
1646 if (!group
->meth
->field_mul(group
, &p
->Y
, &p
->Y
, tmp1
, ctx
)) goto err
;
1648 if (group
->meth
->field_set_to_one
!= 0)
1650 if (!group
->meth
->field_set_to_one(group
, &p
->Z
, ctx
)) goto err
;
1654 if (!BN_one(&p
->Z
)) goto err
;
1664 if (new_ctx
!= NULL
)
1665 BN_CTX_free(new_ctx
);
1668 /* heap[pow2/2] .. heap[pow2-1] have not been allocated locally! */
1669 for (i
= pow2
/2 - 1; i
> 0; i
--)
1671 if (heap
[i
] != NULL
)
1672 BN_clear_free(heap
[i
]);
1680 int ec_GFp_simple_field_mul(const EC_GROUP
*group
, BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*b
, BN_CTX
*ctx
)
1682 return BN_mod_mul(r
, a
, b
, &group
->field
, ctx
);
1686 int ec_GFp_simple_field_sqr(const EC_GROUP
*group
, BIGNUM
*r
, const BIGNUM
*a
, BN_CTX
*ctx
)
1688 return BN_mod_sqr(r
, a
, &group
->field
, ctx
);