ret->A = BN_new();
ret->B = BN_new();
ret->p = BN_new();
- ret->h = BN_new();
ret->is_in_mont = 0;
- if (ret->A == NULL || ret->B == NULL || ret->p == NULL || ret->h == NULL)
+ if (ret->A == NULL || ret->B == NULL || ret->p == NULL)
{
if (ret->A != NULL) BN_free(ret->A);
if (ret->B != NULL) BN_free(ret->B);
if (ret->p != NULL) BN_free(ret->p);
- if (ret->h != NULL) BN_free(ret->h);
free(ret);
return(NULL);
}
if (E->A != NULL) BN_clear_free(E->A);
if (E->B != NULL) BN_clear_free(E->B);
if (E->p != NULL) BN_clear_free(E->p);
- if (E->h != NULL) BN_clear_free(E->h);
E->is_in_mont = 0;
free(E);
}
int EC_to_montgomery(EC *E, BN_MONTGOMERY *mont, BN_CTX *ctx)
{
assert(E != NULL);
- assert(E->A != NULL && E->B != NULL && E->p != NULL && E->h != NULL);
+ assert(E->A != NULL && E->B != NULL && E->p != NULL);
assert(mont != NULL);
assert(mont->p != NULL);
if (!BN_lshift(E->B, E->B, mont->R_num_bits)) return 0;
if (!BN_mod(E->B, E->B, mont->p, ctx)) return 0;
- if (!BN_lshift(E->h, E->h, mont->R_num_bits)) return 0;
- if (!BN_mod(E->h, E->h, mont->p, ctx)) return 0;
-
E->is_in_mont = 1;
return 1;
int EC_from_montgomery(EC *E, BN_MONTGOMERY *mont, BN_CTX *ctx)
{
assert(E != NULL);
- assert(E->A != NULL && E->B != NULL && E->p != NULL && E->h != NULL);
+ assert(E->A != NULL && E->B != NULL && E->p != NULL);
assert(mont != NULL);
assert(mont->p != NULL);
if (!BN_mont_red(E->A, mont)) return 0;
if (!BN_mont_red(E->B, mont)) return 0;
- if (!BN_mont_red(E->h, mont)) return 0;
E->is_in_mont = 0;
return 1;
}
#endif /* MONTGOMERY */
-
-int EC_set_half(EC *E)
-/* h <- 1/2 mod p = (p + 1)/2 */
-{
- assert(E != NULL);
- assert(E->p != NULL);
- assert(E->h != NULL);
- assert(!E->is_in_mont);
-
- if (BN_copy(E->h, E->p) == NULL) return 0;
- if (!BN_add_word(E->h, 1)) return 0;
- if (!BN_rshift1(E->h, E->h)) return 0;
- return 1;
-}
int Pnorm, Pinfty, X0, A0;
assert(E != NULL);
- assert(E->A != NULL && E->B != NULL && E->p != NULL && E->h != NULL);
+ assert(E->A != NULL && E->B != NULL && E->p != NULL);
assert(ctx != NULL);
assert(R->X != NULL && R->Y != NULL && R->Z != NULL);
assert(E != NULL);
- assert(E->A != NULL && E->B != NULL && E->p != NULL && E->h != NULL);
+ assert(E->A != NULL && E->B != NULL && E->p != NULL);
assert(ctx != NULL);
assert(R->X != NULL && R->Y != NULL && R->Z != NULL);
assert(E != NULL);
- assert(E->A != NULL && E->B != NULL && E->p != NULL && E->h != NULL);
- assert(!BN_is_zero(E->h));;
+ assert(E->A != NULL && E->B != NULL && E->p != NULL);
assert(ctx != NULL);
if (!BN_mod_mul(n5, n4, n5, p, ctx)) goto err;
if (!BN_mod_mul(n1, n2, n5, p, ctx)) goto err;
if (!BN_mod_sub(n0, n0, n1, p, ctx)) goto err;
- if (!BN_mod_mul(R->Y, n0, E->h, p, ctx)) goto err; /* Y = (L6 * L9 - L8 * L5^3) / 2 */
-
-
+ if (BN_is_odd(n0))
+ if (!BN_add(n0, n0, p)) goto err;
+ /* now 0 <= n0 < 2*p, and n0 is even */
+ if (!BN_rshift1(R->Y, n0)) goto err; /* Y = (L6 * L9 - L8 * L5^3) / 2 */
#ifdef TEST
if (!ECP_is_on_ec(R, E, ctx)) return 0;
assert(R->X != NULL && R->Y != NULL && R->Z != NULL);
assert(E != NULL);
- assert(E->A != NULL && E->B != NULL && E->p != NULL && E->h != NULL);
+ assert(E->A != NULL && E->B != NULL && E->p != NULL);
assert(k != NULL);
assert(!k->neg);
assert(R->X != NULL && R->Y != NULL && R->Z != NULL);
assert(E != NULL);
- assert(E->A != NULL && E->B != NULL && E->p != NULL && E->h != NULL);
+ assert(E->A != NULL && E->B != NULL && E->p != NULL);
assert(ctx != NULL);
assert(R->X != NULL && R->Y != NULL && R->Z != NULL);
assert(E != NULL);
- assert(E->A != NULL && E->B != NULL && E->p != NULL && E->h != NULL);
- assert(!BN_is_zero(E->h));;
+ assert(E->A != NULL && E->B != NULL && E->p != NULL);
assert(ctx != NULL);
if (!BN_mont_mod_mul(n6, n4, n5, mont)) goto err;
if (!BN_mont_mod_mul(n1, n2, n6, mont)) goto err;
if (!BN_mod_sub_quick(n0, n0, n1, p)) goto err;
- if (!BN_mont_mod_mul(R->Y, n0, E->h, mont)) goto err; /* Y = (L6 * L9 - L8 * L5^3) / 2 */
-
+ if (BN_is_odd(n0))
+ if (!BN_add(n0, n0, p)) goto err;
+ /* now 0 <= n0 < 2*p, and n0 is even */
+ if (!BN_rshift1(R->Y, n0)) goto err; /* Y = (L6 * L9 - L8 * L5^3) / 2 */
BN_CTX_end(ctx);
return 1;
assert(R->X != NULL && R->Y != NULL && R->Z != NULL);
assert(E != NULL);
- assert(E->A != NULL && E->B != NULL && E->p != NULL && E->h != NULL);
+ assert(E->A != NULL && E->B != NULL && E->p != NULL);
assert(k != NULL);
assert(!k->neg);
assert(P->X != NULL && P->Y != NULL && P->Z != NULL);
assert(E != NULL);
- assert(E->A != NULL && E->B != NULL && E->p != NULL && E->h != NULL);
+ assert(E->A != NULL && E->B != NULL && E->p != NULL);
assert(k != NULL);
assert(!k->neg);