5 * Elliptic Curve Arithmetic Functions
7 * Copyright (C) Lenka Fibikova 2000
17 #include <openssl/bn.h>
19 #include "../bn/bn_mont2.h" /* XXX */
27 ret=(EC_POINT *)malloc(sizeof(EC_POINT));
28 if (ret == NULL) return NULL;
34 if (ret->X == NULL || ret->Y == NULL || ret->Z == NULL)
36 if (ret->X != NULL) BN_free(ret->X);
37 if (ret->Y != NULL) BN_free(ret->Y);
38 if (ret->Z != NULL) BN_free(ret->Z);
46 void ECP_clear_free(EC_POINT *P)
48 if (P == NULL) return;
51 if (P->X != NULL) BN_clear_free(P->X);
52 if (P->Y != NULL) BN_clear_free(P->Y);
53 if (P->Z != NULL) BN_clear_free(P->Z);
58 void ECP_clear_free_precompute(ECP_PRECOMPUTE *prec)
63 if (prec == NULL) return;
67 max <<= (prec->r - 1);
69 for (i = 0; i < max; i++)
71 if (prec->Pi[i] != NULL) ECP_clear_free(prec->Pi[i]);
78 int ECP_is_on_ec(EC_POINT *P, EC *E, BN_CTX *ctx)
80 BIGNUM *n0, *n1, *n2, *p;
85 assert(P->X != NULL && P->Y != NULL && P->Z != NULL);
88 assert(E->A != NULL && E->B != NULL && E->p != NULL);
92 assert(!P->is_in_mont);
94 if (ECP_is_infty(P)) return 1;
105 Pnorm = (ECP_is_norm(P));
109 if (!BN_mod_mul(n0, P->Z, P->Z, p, ctx)) goto err;
110 if (!BN_mod_mul(n1, n0, n0, p, ctx)) goto err;
111 if (!BN_mod_mul(n2, n0, n1, p, ctx)) goto err;
114 if (!BN_mod_mul(n0, P->X, P->X, p, ctx)) goto err;
115 if (!BN_mod_mul(n0, n0, P->X, p, ctx)) goto err;
119 if (!BN_mod_mul(n1, P->X, E->A, p, ctx)) goto err;
123 if (!BN_mod_mul(n1, n1, P->X, p, ctx)) goto err;
124 if (!BN_mod_mul(n1, n1, E->A, p, ctx)) goto err;
126 if (!BN_mod_add(n0, n0, n1, p, ctx)) goto err;
130 if (!BN_mod_add(n0, n0, E->B, p, ctx)) goto err;
134 if (!BN_mod_mul(n2, n2, E->B, p, ctx)) goto err;
135 if (!BN_mod_add(n0, n0, n2, p, ctx)) goto err;
138 if (!BN_mod_mul(n1, P->Y, P->Y, p, ctx)) goto err;
151 EC_POINT *ECP_generate(BIGNUM *x, BIGNUM *z,EC *E, BN_CTX *ctx)
152 /* x == NULL || z = 0 -> point of infinity */
153 /* z == NULL || z = 1 -> normalized */
156 EC_POINT *ret = NULL;
157 int Pnorm, Pinfty, X0, A0;
160 assert(E->A != NULL && E->B != NULL && E->p != NULL);
164 Pinfty = (x == NULL);
168 Pnorm = BN_is_one(z);
169 Pinfty = (Pinfty || BN_is_zero(z));
174 if ((ret = ECP_new()) == NULL) return NULL;
175 if (!BN_zero(ret->Z))
184 A0 = BN_is_zero(E->A);
186 if ((ret = ECP_new()) == NULL) return NULL;
191 n0 = BN_CTX_get(ctx);
192 n1 = BN_CTX_get(ctx);
193 if (n1 == NULL) goto err;
195 if (!BN_zero(n0)) goto err;
196 if (!BN_zero(n1)) goto err;
200 if (!BN_mod_sqr(n0, x, E->p, ctx)) goto err;
201 if (!BN_mod_mul(n0, n0, x, E->p, ctx)) goto err; /* x^3 */
206 if (!BN_mod_mul(n1, E->A, x, E->p, ctx)) goto err; /* Ax */
207 if (!BN_mod_add(n0, n0, n1, E->p, ctx)) goto err; /* x^3 + Ax */
210 if (!BN_is_zero(E->B))
211 if (!BN_mod_add(n0, n0, E->B, E->p, ctx)) goto err; /* x^3 + Ax +B */
213 if (!BN_mod_sqrt(ret->Y, n0, E->p, ctx)) goto err;
214 if (BN_copy(ret->X, x) == NULL) goto err;
218 if (!BN_one(ret->Z)) goto err;
222 if (BN_copy(ret->Z, z) == NULL) goto err;
223 if (!BN_mod_sqr(n0, z, E->p, ctx)) goto err;
224 if (!BN_mod_mul(ret->X, ret->X, n0, E->p, ctx)) goto err;
225 if (!BN_mod_mul(n0, n0, z, E->p, ctx)) goto err;
226 if (!BN_mod_mul(ret->Y, ret->Y, n0, E->p, ctx)) goto err;
230 if (!ECP_is_on_ec(ret, E, ctx)) goto err;
237 if (ret != NULL) ECP_clear_free(ret);
243 int ECP_ecp2bin(EC_POINT *P, unsigned char *to, int form)
244 /* form = 1 ... compressed
251 assert (P->X != NULL && P->Y != NULL && P->Z != NULL);
252 assert (!P->is_in_mont);
253 assert (ECP_is_norm(P) || ECP_is_infty(P));
255 assert (form > 0 && form < 4);
257 if (BN_is_zero(P->Z))
263 bx = BN_num_bytes(P->X);
264 if (form == 1 ) bytes = bx + 1;
267 by = BN_num_bytes(P->Y);
268 bytes = (bx > by ? bx : by);
269 bytes = bytes * 2 + 1;
271 memset(to, 0, bytes);
275 case 1: to[0] = 2; break;
276 case 2: to[0] = 4; break;
277 case 3: to[0] = 6; break;
279 if (form != 2) to[0] += BN_is_bit_set(P->Y, 0);
282 if ((BN_bn2bin(P->X, to + 1)) != bx) return 0;
285 if ((BN_bn2bin(P->Y, to + bx + 1)) != by) return 0;
292 int ECP_bin2ecp(unsigned char *from, int len, EC_POINT *P, EC *E, BN_CTX *ctx)
299 assert (E->A != NULL && E->B != NULL && E->p != NULL);
300 assert (!E->is_in_mont);
302 assert (ctx != NULL);
303 assert (from != NULL);
305 assert (P->X != NULL && P->Y != NULL && P->Z != NULL);
307 if (len == 1 && from[0] != 0) return 0;
309 if (len == 0 || len == 1)
311 if (!BN_zero(P->Z)) return 0;
320 if ((x = BN_new()) == NULL) return 0;
321 if (BN_bin2bn(from + 1, len - 1, x) == NULL) return 0;
323 pp = ECP_generate(x, NULL, E, ctx);
325 if (pp == NULL) return 0;
330 if (BN_is_bit_set(P->Y, 0) != y)
331 if (!BN_sub(P->Y, E->p, P->Y)) return 0;
338 if (BN_bin2bn(from + 1, y, P->X) == NULL) return 0;
339 if (BN_bin2bn(from + y + 1, y, P->Y) == NULL) return 0;
340 if (!BN_set_word(P->Z, 1)) return 0;
348 if (!ECP_is_on_ec(P, E, ctx)) return 0;
353 int ECP_normalize(EC_POINT *P, EC *E, BN_CTX *ctx)
358 assert (P->X != NULL && P->Y != NULL && P->Z != NULL);
361 assert (E->A != NULL && E->B != NULL && E->p != NULL);
363 assert (ctx != NULL);
365 if (ECP_is_norm(P)) return 1;
366 if (ECP_is_infty(P)) return 0;
368 if ((zm = BN_mod_inverse(P->Z, P->Z, E->p, ctx)) == NULL) return 0;
370 assert(!P->is_in_mont);
375 if (z == NULL) goto err;
377 if (!BN_mod_mul(z, zm, zm, E->p, ctx)) goto err;
378 if (!BN_mod_mul(P->X, P->X, z, E->p, ctx)) goto err;
380 if (!BN_mod_mul(z, z, zm, E->p, ctx)) goto err;
381 if (!BN_mod_mul(P->Y, P->Y, z, E->p, ctx)) goto err;
383 if (!BN_one(P->Z)) goto err;
385 if (zm != NULL) BN_clear_free(zm);
391 if (zm != NULL) BN_clear_free(zm);
397 int ECP_copy(EC_POINT *R, EC_POINT *P)
400 assert(P->X != NULL && P->Y != NULL && P->Z != NULL);
403 assert(R->X != NULL && R->Y != NULL && R->Z != NULL);
405 if (BN_copy(R->X, P->X) == NULL) return 0;
406 if (BN_copy(R->Y, P->Y) == NULL) return 0;
407 if (BN_copy(R->Z, P->Z) == NULL) return 0;
408 R->is_in_mont = P->is_in_mont;
414 EC_POINT *ECP_dup(EC_POINT *P)
419 if (ret == NULL) return NULL;
421 if (!ECP_copy(ret, P))
431 EC_POINT *ECP_minus(EC_POINT *P, BIGNUM *p) /* mont || non-mont */
436 assert(P->X != NULL && P->Y != NULL && P->Z != NULL);
440 assert(BN_cmp(P->Y, p) < 0);
443 if (ret == NULL) return NULL;
445 if (BN_is_zero(ret->Y)) return ret;
447 if (!BN_sub(ret->Y, p, ret->Y))
458 int ECP_cmp(EC_POINT *P, EC_POINT *Q, BIGNUM *p, BN_CTX *ctx)
466 BIGNUM *n0, *n1, *n2, *n3, *n4;
470 assert(P->X != NULL && P->Y != NULL && P->Z != NULL);
473 assert(Q->X != NULL && Q->Y != NULL && Q->Z != NULL);
478 assert(!P->is_in_mont);
479 assert(!Q->is_in_mont);
481 if (ECP_is_infty(P) && ECP_is_infty(Q)) return 0;
482 if (ECP_is_infty(P) || ECP_is_infty(Q)) return 1;
485 Pnorm = (ECP_is_norm(P));
486 Qnorm = (ECP_is_norm(Q));
489 n0 = BN_CTX_get(ctx);
490 n1 = BN_CTX_get(ctx);
491 n2 = BN_CTX_get(ctx);
492 n3 = BN_CTX_get(ctx);
493 n4 = BN_CTX_get(ctx);
494 if (n4 == NULL) goto err;
498 if (BN_copy(n1, P->X) == NULL) goto err; /* L1 = x_p */
499 if (BN_copy(n2, P->Y) == NULL) goto err; /* L2 = y_p */
503 if (!BN_sqr(n0, Q->Z, ctx)) goto err;
504 if (!BN_mod_mul(n1, P->X, n0, p, ctx)) goto err; /* L1 = x_p * z_q^2 */
506 if (!BN_mod_mul(n0, n0, Q->Z, p, ctx)) goto err;
507 if (!BN_mod_mul(n2, P->Y, n0, p, ctx)) goto err; /* L2 = y_p * z_q^3 */
512 if (BN_copy(n3, Q->X) == NULL) goto err; /* L3 = x_q */
513 if (BN_copy(n4, Q->Y) == NULL) goto err; /* L4 = y_q */
517 if (!BN_sqr(n0, P->Z, ctx)) goto err;
518 if (!BN_mod_mul(n3, Q->X, n0, p, ctx)) goto err; /* L3 = x_q * z_p^2 */
520 if (!BN_mod_mul(n0, n0, P->Z, p, ctx)) goto err;
521 if (!BN_mod_mul(n4, Q->Y, n0, p, ctx)) goto err; /* L4 = y_q * z_p^3 */
524 if (!BN_mod_sub(n0, n1, n3, p, ctx)) goto err; /* L5 = L1 - L3 */
532 if (!BN_mod_sub(n0, n2, n4, p, ctx)) goto err; /* L6 = L2 - L4 */
549 int ECP_double(EC_POINT *R, EC_POINT *P, EC *E, BN_CTX *ctx)
552 BIGNUM *n0, *n1, *n2, *n3, *p;
556 assert(P->X != NULL && P->Y != NULL && P->Z != NULL);
559 assert(R->X != NULL && R->Y != NULL && R->Z != NULL);
562 assert(E->A != NULL && E->B != NULL && E->p != NULL);
566 assert(!P->is_in_mont);
570 if (!BN_zero(R->Z)) return 0;
574 Pnorm = (ECP_is_norm(P));
575 A0 = (BN_is_zero(E->A));
578 n0 = BN_CTX_get(ctx);
579 n1 = BN_CTX_get(ctx);
580 n2 = BN_CTX_get(ctx);
581 n3 = BN_CTX_get(ctx);
582 if (n3 == NULL) goto err;
589 if (!BN_mod_sqr(n1, P->X, p, ctx)) goto err;
590 if (!BN_mul_word(n1, 3)) goto err;
591 if (!A0) /* if A = 0: L1 = 3 * x^2 + a * z^4 = 3 * x ^2 */
592 if (!BN_mod_add(n1, n1, E->A, p, ctx)) goto err; /* L1 = 3 * x^2 + a * z^4 = 3 * x^2 + a */
596 if (!BN_mod_sqr(n0, P->Z, p, ctx)) goto err;
597 if (!BN_mod_mul(n0, n0, n0, p, ctx)) goto err;
598 if (!BN_mod_mul(n0, n0, E->A, p, ctx)) goto err;
599 if (!BN_mod_sqr(n1, P->X, p, ctx)) goto err;
600 if (!BN_mul_word(n1, 3)) goto err;
601 if (!BN_mod_add(n1, n1, n0, p, ctx)) goto err; /* L1 = 3 * x^2 + a * z^4 */
607 if (BN_copy(n0, P->Y) == NULL) goto err;
611 if (!BN_mod_mul(n0, P->Y, P->Z, p, ctx)) goto err;
613 if (!BN_lshift1(n0, n0)) goto err;
614 if (!BN_smod(R->Z, n0, p, ctx)) goto err; /* Z = 2 * y * z */
617 if (!BN_mod_sqr(n3, P->Y, p, ctx)) goto err;
618 if (!BN_mod_mul(n2, P->X, n3, p, ctx)) goto err;
619 if (!BN_lshift(n2, n2, 2)) goto err;
620 if (!BN_smod(n2, n2, p, ctx)) goto err; /* L2 = 4 * x * y^2 */
623 if (!BN_lshift1(n0, n2)) goto err;
624 if (!BN_mod_sqr(R->X, n1, p, ctx)) goto err;
625 if (!BN_mod_sub(R->X, R->X, n0, p, ctx)) goto err; /* X = L1^2 - 2 * L2 */
628 if (!BN_mod_sqr(n0, n3, p, ctx)) goto err;
629 if (!BN_lshift(n3, n0, 3)) goto err;
630 if (!BN_smod(n3, n3, p, ctx)) goto err; /* L3 = 8 * y^4 */
633 if (!BN_mod_sub(n0, n2, R->X, p, ctx)) goto err;
634 if (!BN_mod_mul(n0, n1, n0, p, ctx)) goto err;
635 if (!BN_mod_sub(R->Y, n0, n3, p, ctx)) goto err; /* Y = L1 * (L2 - X) - L3 */
639 if (!ECP_is_on_ec(R, E, ctx)) return 0;
651 int ECP_add(EC_POINT *R, EC_POINT *P, EC_POINT *Q, EC *E, BN_CTX *ctx)
652 /* R <- P + Q (on E) */
654 BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6, *p;
658 assert(P->X != NULL && P->Y != NULL && P->Z != NULL);
661 assert(Q->X != NULL && Q->Y != NULL && Q->Z != NULL);
664 assert(R->X != NULL && R->Y != NULL && R->Z != NULL);
667 assert(E->A != NULL && E->B != NULL && E->p != NULL);
671 assert(!P->is_in_mont);
672 assert(!Q->is_in_mont);
674 if (P == Q) return ECP_double(R, P, E, ctx);
676 if (ECP_is_infty(P)) return ECP_copy(R, Q);
677 if (ECP_is_infty(Q)) return ECP_copy(R, P);
679 Pnorm = (ECP_is_norm(P));
680 Qnorm = (ECP_is_norm(Q));
683 n0 = BN_CTX_get(ctx);
684 n1 = BN_CTX_get(ctx);
685 n2 = BN_CTX_get(ctx);
686 n3 = BN_CTX_get(ctx);
687 n4 = BN_CTX_get(ctx);
688 n5 = BN_CTX_get(ctx);
689 n6 = BN_CTX_get(ctx);
690 if (n6 == NULL) goto err;
697 if (BN_copy(n1, P->X) == NULL) goto err; /* L1 = x_p */
698 if (BN_copy(n2, P->Y) == NULL) goto err; /* L2 = y_p */
702 if (!BN_sqr(n0, Q->Z, ctx)) goto err;
703 if (!BN_mod_mul(n1, P->X, n0, p, ctx)) goto err; /* L1 = x_p * z_q^2 */
705 if (!BN_mod_mul(n0, n0, Q->Z, p, ctx)) goto err;
706 if (!BN_mod_mul(n2, P->Y, n0, p, ctx)) goto err; /* L2 = y_p * z_q^3 */
712 if (BN_copy(n3, Q->X) == NULL) goto err; /* L3 = x_q */
713 if (BN_copy(n4, Q->Y) == NULL) goto err; /* L4 = y_q */
717 if (!BN_sqr(n0, P->Z, ctx)) goto err;
718 if (!BN_mod_mul(n3, Q->X, n0, p, ctx)) goto err; /* L3 = x_q * z_p^2 */
720 if (!BN_mod_mul(n0, n0, P->Z, p, ctx)) goto err;
721 if (!BN_mod_mul(n4, Q->Y, n0, p, ctx)) goto err; /* L4 = y_q * z_p^3 */
725 if (!BN_mod_sub(n5, n1, n3, p, ctx)) goto err; /* L5 = L1 - L3 */
726 if (!BN_mod_sub(n6, n2, n4, p, ctx)) goto err; /* L6 = L2 - L4 */
731 if (BN_is_zero(n6)) /* P = Q => P + Q = 2P */
734 return ECP_double(R, P, E, ctx);
736 else /* P = -Q => P + Q = \infty */
739 if (!BN_zero(R->Z)) return 0;
745 if (!BN_mod_add(n1, n1, n3, p, ctx)) goto err; /* L7 = L1 + L3 */
746 if (!BN_mod_add(n2, n2, n4, p, ctx)) goto err; /* L8 = L2 + L4 */
751 if (BN_copy(n0, Q->Z) == NULL) goto err;
755 if (!BN_mod_mul(n0, P->Z, Q->Z, p, ctx)) goto err;
757 if (!BN_mod_mul(R->Z, n0, n5, p, ctx)) goto err; /* Z = z_p * z_q * L_5 */
760 if (!BN_mod_sqr(n0, n6, p, ctx)) goto err;
761 if (!BN_mod_sqr(n4, n5, p, ctx)) goto err;
762 if (!BN_mod_mul(n3, n1, n4, p, ctx)) goto err;
763 if (!BN_mod_sub(R->X, n0, n3, p, ctx)) goto err; /* X = L6^2 - L5^2 * L7 */
766 if (!BN_lshift1(n0, R->X)) goto err;
767 if (!BN_mod_sub(n0, n3, n0, p, ctx)) goto err; /* L9 = L5^2 * L7 - 2X */
770 if (!BN_mod_mul(n0, n0, n6, p, ctx)) goto err;
771 if (!BN_mod_mul(n5, n4, n5, p, ctx)) goto err;
772 if (!BN_mod_mul(n1, n2, n5, p, ctx)) goto err;
773 if (!BN_mod_sub(n0, n0, n1, p, ctx)) goto err;
775 if (!BN_add(n0, n0, p)) goto err;
776 /* now 0 <= n0 < 2*p, and n0 is even */
777 if (!BN_rshift1(R->Y, n0)) goto err; /* Y = (L6 * L9 - L8 * L5^3) / 2 */
780 if (!ECP_is_on_ec(R, E, ctx)) return 0;
792 ECP_PRECOMPUTE *ECP_precompute(int r, EC_POINT *P, EC *E, BN_CTX *ctx)
799 assert(!P->is_in_mont);
800 assert(!E->is_in_mont);
802 ret=(ECP_PRECOMPUTE *)malloc(sizeof(ECP_PRECOMPUTE));
803 if (ret == NULL) return NULL;
810 ret->Pi=(EC_POINT **)malloc(sizeof(EC_POINT *) * max);
811 if (ret->Pi == NULL) goto err;
815 if ((P2 = ECP_new()) == NULL) goto err;
816 if (!ECP_double(P2, P, E, ctx)) goto err;
819 if((ret->Pi[0] = ECP_dup(P)) == NULL) goto err;
822 /* P_i = P_(i-1) + P2 */
823 for (i = 1; i < max; i++)
825 if ((ret->Pi[i] = ECP_new()) == NULL) goto err;
827 if (!ECP_add(ret->Pi[i], P2, ret->Pi[i - 1], E, ctx)) goto err;
837 ECP_clear_free_precompute(ret);
842 int ECP_multiply(EC_POINT *R, BIGNUM *k, ECP_PRECOMPUTE *prec, EC *E, BN_CTX *ctx)
849 assert(R->X != NULL && R->Y != NULL && R->Z != NULL);
852 assert(E->A != NULL && E->B != NULL && E->p != NULL);
858 assert(prec != NULL);
860 assert(!E->is_in_mont);
864 if (!BN_zero(R->Z)) return 0;
875 if (!BN_zero(R->Z)) return 0;
880 if (!BN_is_bit_set(k, j))
882 if (!ECP_double(R, R, E, ctx)) return 0;
888 if (nextw < -1) nextw = -1;
890 while(!BN_is_bit_set(k, t))
893 if (!ECP_double(R, R, E, ctx)) return 0;
903 if (BN_is_bit_set(k, j)) h++;
904 if (!ECP_double(R, R, E, ctx)) return 0;
906 if (!ECP_double(R, R, E, ctx)) return 0;
910 if (!ECP_add(R, R, prec->Pi[h], E, ctx)) return 0;
912 for (; j > nextw; j--)
914 if (!ECP_double(R, R, E, ctx)) return 0;
928 int ECP_to_montgomery(EC_POINT *P, BN_MONTGOMERY *mont, BN_CTX *ctx)
931 assert(P->X != NULL && P->Y != NULL && P->Z != NULL);
933 assert(mont != NULL);
934 assert(mont->p != NULL);
938 if (P->is_in_mont) return 1;
940 if (!BN_lshift(P->X, P->X, mont->R_num_bits)) return 0;
941 if (!BN_mod(P->X, P->X, mont->p, ctx)) return 0;
943 if (!BN_lshift(P->Y, P->Y, mont->R_num_bits)) return 0;
944 if (!BN_mod(P->Y, P->Y, mont->p, ctx)) return 0;
946 if (!BN_lshift(P->Z, P->Z, mont->R_num_bits)) return 0;
947 if (!BN_mod(P->Z, P->Z, mont->p, ctx)) return 0;
954 int ECP_from_montgomery(EC_POINT *P, BN_MONTGOMERY *mont, BN_CTX *ctx)
958 assert(P->X != NULL && P->Y != NULL && P->Z != NULL);
960 assert(mont != NULL);
961 assert(mont->p != NULL);
965 if (!P->is_in_mont) return 1;
967 if (!BN_mont_red(P->X, mont)) return 0;
968 if (!BN_mont_red(P->Y, mont)) return 0;
969 if (!BN_mont_red(P->Z, mont)) return 0;
976 int ECP_mont_cmp(EC_POINT *P, EC_POINT *Q, BN_MONTGOMERY *mont, BN_CTX *ctx)
984 BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *p;
987 assert(P->X != NULL && P->Y != NULL && P->Z != NULL);
990 assert(Q->X != NULL && Q->Y != NULL && Q->Z != NULL);
992 assert(mont != NULL);
993 assert(mont->p != NULL);
998 if (!ECP_to_montgomery(P, mont, ctx)) return 0;
1001 if (!ECP_to_montgomery(Q, mont, ctx)) return 0;
1004 if (ECP_is_infty(P) && ECP_is_infty(Q)) return 0;
1005 if (ECP_is_infty(P) || ECP_is_infty(Q)) return 1;
1009 n0 = BN_CTX_get(ctx);
1010 n1 = BN_CTX_get(ctx);
1011 n2 = BN_CTX_get(ctx);
1012 n3 = BN_CTX_get(ctx);
1013 n4 = BN_CTX_get(ctx);
1014 n5 = BN_CTX_get(ctx);
1015 if (n5 == 0) goto err;
1021 if (!BN_mont_mod_mul(n5, Q->Z, Q->Z, mont)) goto err;
1022 if (!BN_mont_mod_mul(n1, P->X, n5, mont)) goto err; /* L1 = x_p * z_q^2 */
1024 if (!BN_mont_mod_mul(n0, n5, Q->Z, mont)) goto err;
1025 if (!BN_mont_mod_mul(n2, P->Y, n0, mont)) goto err; /* L2 = y_p * z_q^3 */
1027 if (!BN_mont_mod_mul(n5, P->Z, P->Z, mont)) goto err;
1028 if (!BN_mont_mod_mul(n3, Q->X, n5, mont)) goto err; /* L3 = x_q * z_p^2 */
1030 if (!BN_mont_mod_mul(n0, n5, P->Z, mont)) goto err;
1031 if (!BN_mont_mod_mul(n4, Q->Y, n0, mont)) goto err; /* L4 = y_q * z_p^3 */
1034 if (!BN_mod_sub_quick(n0, n1, n3, p)) goto err; /* L5 = L1 - L3 */
1036 if (!BN_is_zero(n0))
1042 if (!BN_mod_sub_quick(n0, n2, n4, p)) goto err; /* L6 = L2 - L4 */
1044 if (!BN_is_zero(n0))
1059 int ECP_mont_double(EC_POINT *R, EC_POINT *P, EC *E, BN_MONTGOMERY *mont, BN_CTX *ctx)
1060 /* R <- 2P (on E) */
1062 BIGNUM *n0, *n1, *n2, *n3, *p;
1065 assert(P->X != NULL && P->Y != NULL && P->Z != NULL);
1068 assert(R->X != NULL && R->Y != NULL && R->Z != NULL);
1071 assert(E->A != NULL && E->B != NULL && E->p != NULL);
1073 assert(ctx != NULL);
1076 if (!ECP_to_montgomery(P, mont, ctx)) return 0;
1079 if (!EC_to_montgomery(E, mont, ctx)) return 0;
1083 if (ECP_is_infty(P))
1085 if (!BN_zero(R->Z)) return 0;
1091 n0 = BN_CTX_get(ctx);
1092 n1 = BN_CTX_get(ctx);
1093 n2 = BN_CTX_get(ctx);
1094 n3 = BN_CTX_get(ctx);
1095 if (n3 == 0) goto err;
1100 if (!BN_mont_mod_mul(n0, P->Z, P->Z, mont)) goto err;
1101 if (!BN_mont_mod_mul(n2, n0, n0, mont)) goto err;
1102 if (!BN_mont_mod_mul(n0, n2, E->A, mont)) goto err;
1103 if (!BN_mont_mod_mul(n1, P->X, P->X, mont)) goto err;
1104 if (!BN_mod_lshift1_quick(n2, n1, p)) goto err;
1105 if (!BN_mod_add_quick(n1, n1, n2, p)) goto err;
1106 if (!BN_mod_add_quick(n1, n1, n0, p)) goto err; /* L1 = 3 * x^2 + a * z^4 */
1109 if (!BN_mont_mod_mul(n0, P->Y, P->Z, mont)) goto err;
1110 if (!BN_mod_lshift1_quick(R->Z, n0, p)) goto err; /* Z = 2 * y * z */
1113 if (!BN_mont_mod_mul(n3, P->Y, P->Y, mont)) goto err;
1114 if (!BN_mont_mod_mul(n2, P->X, n3, mont)) goto err;
1115 if (!BN_mod_lshift_quick(n2, n2, 2, p)) goto err; /* L2 = 4 * x * y^2 */
1118 if (!BN_mod_lshift1_quick(n0, n2, p)) goto err;
1119 if (!BN_mont_mod_mul(R->X, n1, n1, mont)) goto err;
1120 if (!BN_mod_sub_quick(R->X, R->X, n0, p)) goto err; /* X = L1^2 - 2 * L2 */
1123 if (!BN_mont_mod_mul(n0, n3, n3, mont)) goto err;
1124 if (!BN_mod_lshift_quick(n3, n0, 3, p)) goto err; /* L3 = 8 * y^4 */
1128 if (!BN_mod_sub_quick(n2, n2, R->X, p)) goto err;
1129 if (!BN_mont_mod_mul(n0, n1, n2, mont)) goto err;
1130 if (!BN_mod_sub_quick(R->Y, n0, n3, p)) goto err; /* Y = L1 * (L2 - X) - L3 */
1141 int ECP_mont_add(EC_POINT *R, EC_POINT *P, EC_POINT *Q, EC *E, BN_MONTGOMERY *mont, BN_CTX *ctx)
1142 /* R <- P + Q (on E) */
1144 BIGNUM *n0, *n1, *n2, *n3, *n4, *n5, *n6, *p;
1147 assert(P->X != NULL && P->Y != NULL && P->Z != NULL);
1150 assert(Q->X != NULL && Q->Y != NULL && Q->Z != NULL);
1153 assert(R->X != NULL && R->Y != NULL && R->Z != NULL);
1156 assert(E->A != NULL && E->B != NULL && E->p != NULL);
1158 assert(ctx != NULL);
1161 if (!ECP_to_montgomery(Q, mont, ctx)) return 0;
1164 if (!ECP_to_montgomery(P, mont, ctx)) return 0;
1167 if (!EC_to_montgomery(E, mont, ctx)) return 0;
1169 if (P == Q) return ECP_mont_double(R, P, E, mont, ctx);
1171 if (ECP_is_infty(P)) return ECP_copy(R, Q);
1172 if (ECP_is_infty(Q)) return ECP_copy(R, P);
1176 n0 = BN_CTX_get(ctx);
1177 n1 = BN_CTX_get(ctx);
1178 n2 = BN_CTX_get(ctx);
1179 n3 = BN_CTX_get(ctx);
1180 n4 = BN_CTX_get(ctx);
1181 n5 = BN_CTX_get(ctx);
1182 n6 = BN_CTX_get(ctx);
1183 if (n6 == NULL) goto err;
1191 if (!BN_mont_mod_mul(n6, Q->Z, Q->Z, mont)) goto err;
1192 if (!BN_mont_mod_mul(n1, P->X, n6, mont)) goto err; /* L1 = x_p * z_q^2 */
1194 if (!BN_mont_mod_mul(n0, n6, Q->Z, mont)) goto err;
1195 if (!BN_mont_mod_mul(n2, P->Y, n0, mont)) goto err; /* L2 = y_p * z_q^3 */
1199 if (!BN_mont_mod_mul(n6, P->Z, P->Z, mont)) goto err;
1200 if (!BN_mont_mod_mul(n3, Q->X, n6, mont)) goto err; /* L3 = x_q * z_p^2 */
1202 if (!BN_mont_mod_mul(n0, n6, P->Z, mont)) goto err;
1203 if (!BN_mont_mod_mul(n4, Q->Y, n0, mont)) goto err; /* L4 = y_q * z_p^3 */
1207 if (!BN_mod_sub_quick(n5, n1, n3, p)) goto err; /* L5 = L1 - L3 */
1208 if (!BN_mod_sub_quick(n6, n2, n4, p)) goto err; /*L6 = L2 - L4 */
1214 if (BN_is_zero(n6)) /* P = Q => P + Q = 2P */
1217 return ECP_mont_double(R, P, E, mont, ctx);
1219 else /* P = -Q => P + Q = \infty */
1222 if (!BN_zero(R->Z)) return 0;
1228 if (!BN_mod_add_quick(n1, n1, n3, p)) goto err; /* L7 = L1 + L3 */
1229 if (!BN_mod_add_quick(n2, n2, n4, p)) goto err; /* L8 = L2 + L4 */
1233 if (!BN_mont_mod_mul(n0, P->Z, Q->Z, mont)) goto err;
1234 if (!BN_mont_mod_mul(R->Z, n0, n5, mont)) goto err; /* Z = z_p * z_q * L_5 */
1238 if (!BN_mont_mod_mul(n0, n6, n6, mont)) goto err;
1239 if (!BN_mont_mod_mul(n4, n5, n5, mont)) goto err;
1240 if (!BN_mont_mod_mul(n3, n1, n4, mont)) goto err;
1241 if (!BN_mod_sub_quick(R->X, n0, n3, p)) goto err; /* X = L6^2 - L5^2 * L7 */
1245 if (!BN_mod_lshift1_quick(n0, R->X, p)) goto err;
1246 if (!BN_mod_sub_quick(n3, n3, n0, p)) goto err; /* L9 = L5^2 * L7 - 2X */
1250 if (!BN_mont_mod_mul(n0, n3, n6, mont)) goto err;
1251 if (!BN_mont_mod_mul(n6, n4, n5, mont)) goto err;
1252 if (!BN_mont_mod_mul(n1, n2, n6, mont)) goto err;
1253 if (!BN_mod_sub_quick(n0, n0, n1, p)) goto err;
1255 if (!BN_add(n0, n0, p)) goto err;
1256 /* now 0 <= n0 < 2*p, and n0 is even */
1257 if (!BN_rshift1(R->Y, n0)) goto err; /* Y = (L6 * L9 - L8 * L5^3) / 2 */
1268 ECP_PRECOMPUTE *ECP_mont_precompute(int r, EC_POINT *P, EC *E, BN_MONTGOMERY *mont, BN_CTX *ctx)
1270 ECP_PRECOMPUTE *ret;
1275 assert(r < sizeof(unsigned int) * 8 - 1);
1277 assert(mont != NULL);
1278 assert(mont->p != NULL);
1281 if (!ECP_to_montgomery(P, mont, ctx)) return 0;
1284 if (!EC_to_montgomery(E, mont, ctx)) return 0;
1286 ret=(ECP_PRECOMPUTE *)malloc(sizeof(ECP_PRECOMPUTE));
1287 if (ret == NULL) return NULL;
1294 ret->Pi=(EC_POINT **)malloc(sizeof(EC_POINT *) * max);
1295 if (ret->Pi == NULL) goto err;
1299 if ((P2 = ECP_new()) == NULL) goto err;
1300 if (!ECP_mont_double(P2, P, E, mont, ctx)) goto err;
1303 if((ret->Pi[0] = ECP_dup(P)) == NULL) goto err;
1306 /* P_i = P_(i-1) + P2 */
1307 for (i = 1; i < max; i++)
1309 if ((ret->Pi[i] = ECP_new()) == NULL) goto err;
1310 if (!ECP_mont_add(ret->Pi[i], P2, ret->Pi[i - 1], E, mont, ctx)) goto err;
1320 ECP_clear_free_precompute(ret);
1325 int ECP_mont_multiply(EC_POINT *R, BIGNUM *k, ECP_PRECOMPUTE *prec, EC *E, BN_MONTGOMERY *mont, BN_CTX *ctx)
1326 /* R = [k]P P = prec->Pi[0]*/
1332 assert(R->X != NULL && R->Y != NULL && R->Z != NULL);
1335 assert(E->A != NULL && E->B != NULL && E->p != NULL);
1340 assert(ctx != NULL);
1341 assert(prec != NULL);
1343 assert(mont != NULL);
1344 assert(mont->p != NULL);
1347 if (!EC_to_montgomery(E, mont, ctx)) return 0;
1352 if (!BN_zero(R->Z)) return 0;
1362 if (!BN_zero(R->Z)) return 0;
1367 if (!BN_is_bit_set(k, j))
1369 if (!ECP_mont_double(R, R, E, mont, ctx)) return 0;
1375 if (nextw < -1) nextw = -1;
1377 while(!BN_is_bit_set(k, t))
1380 if (!ECP_mont_double(R, R, E, mont, ctx)) return 0;
1390 if (BN_is_bit_set(k, j)) h++;
1391 if (!ECP_mont_double(R, R, E, mont, ctx)) return 0;
1393 if (!ECP_mont_double(R, R, E, mont, ctx)) return 0;
1397 if (!ECP_mont_add(R, R, prec->Pi[h], E, mont, ctx)) return 0;
1399 for (; j > nextw; j--)
1401 if (!ECP_mont_double(R, R, E, mont, ctx)) return 0;
1411 int ECP_mont_multiply2(EC_POINT *R, BIGNUM *k, EC_POINT *P, EC *E, BN_MONTGOMERY *mont, BN_CTX *ctx)
1419 assert(R->X != NULL && R->Y != NULL && R->Z != NULL);
1422 assert(P->X != NULL && P->Y != NULL && P->Z != NULL);
1425 assert(E->A != NULL && E->B != NULL && E->p != NULL);
1430 assert(ctx != NULL);
1432 assert(mont != NULL);
1433 assert(mont->p != NULL);
1436 if (!EC_to_montgomery(E, mont, ctx)) return 0;
1439 if (!ECP_to_montgomery(P, mont, ctx)) return 0;
1444 if (!BN_zero(R->Z)) return 0;
1449 if ((h = BN_dup(k)) == NULL) return 0;
1451 if (!BN_lshift1(h, h)) goto err;
1452 if (!BN_add(h, h, k)) goto err;
1454 if (!ECP_copy(R, P)) goto err;
1455 if ((mP = ECP_mont_minus(P, mont)) == NULL) goto err;
1457 for(j = BN_num_bits(h) - 2; j > 0; j--)
1459 if (!ECP_mont_double(R, R, E, mont, ctx)) goto err;
1460 kj = BN_is_bit_set(k, j);
1461 hj = BN_is_bit_set(h, j);
1462 if (hj == 1 && kj == 0)
1463 if (!ECP_mont_add(R, R, P, E, mont, ctx)) goto err;
1464 if (hj == 0 && kj == 1)
1465 if (!ECP_mont_add(R, R, mP, E, mont, ctx)) goto err;
1468 if (h != NULL) BN_free(h);
1469 if (mP != NULL) ECP_clear_free(mP);
1473 if (h != NULL) BN_free(h);
1474 if (mP != NULL) ECP_clear_free(mP);
1478 #endif /* MONTGOMERY */