5 * Elliptic Curve Arithmetic Functions
7 * Copyright (C) Lenka Fibikova 2000
25 ret=(EC *)malloc(sizeof(EC));
26 if (ret == NULL) return NULL;
33 if (ret->A == NULL || ret->B == NULL || ret->p == NULL || ret->h == NULL)
35 if (ret->A != NULL) BN_free(ret->A);
36 if (ret->B != NULL) BN_free(ret->B);
37 if (ret->p != NULL) BN_free(ret->p);
38 if (ret->h != NULL) BN_free(ret->h);
46 void EC_clear_free(EC *E)
48 if (E == NULL) return;
50 if (E->A != NULL) BN_clear_free(E->A);
51 if (E->B != NULL) BN_clear_free(E->B);
52 if (E->p != NULL) BN_clear_free(E->p);
53 if (E->h != NULL) BN_clear_free(E->h);
60 int EC_to_montgomery(EC *E, BN_MONTGOMERY *mont, BN_CTX *ctx)
63 assert(E->A != NULL && E->B != NULL && E->p != NULL && E->h != NULL);
66 assert(mont->p != NULL);
70 if (E->is_in_mont) return 1;
72 if (!BN_lshift(E->A, E->A, mont->R_num_bits)) return 0;
73 if (!BN_mod(E->A, E->A, mont->p, ctx)) return 0;
75 if (!BN_lshift(E->B, E->B, mont->R_num_bits)) return 0;
76 if (!BN_mod(E->B, E->B, mont->p, ctx)) return 0;
78 if (!BN_lshift(E->h, E->h, mont->R_num_bits)) return 0;
79 if (!BN_mod(E->h, E->h, mont->p, ctx)) return 0;
87 int EC_from_montgomery(EC *E, BN_MONTGOMERY *mont, BN_CTX *ctx)
90 assert(E->A != NULL && E->B != NULL && E->p != NULL && E->h != NULL);
93 assert(mont->p != NULL);
97 if (!E->is_in_mont) return 1;
99 if (!BN_mont_red(E->A, mont)) return 0;
100 if (!BN_mont_red(E->B, mont)) return 0;
101 if (!BN_mont_red(E->h, mont)) return 0;
106 #endif /* MONTGOMERY */
108 int EC_set_half(EC *E)
109 /* h <- 1/2 mod p = (p + 1)/2 */
112 assert(E->p != NULL);
113 assert(E->h != NULL);
114 assert(!E->is_in_mont);
116 if (BN_copy(E->h, E->p) == NULL) return 0;
117 if (!BN_add_word(E->h, 1)) return 0;
118 if (!BN_rshift1(E->h, E->h)) return 0;