OpenSSL: Speed up crypto_ec_point_compute_y_sqr()

Optimize the calculation by computing (x^2 + a) first to get rid of one
separate multiplication by x.

Signed-off-by: Jouni Malinen <j@w1.fi>
This commit is contained in:
Jouni Malinen 2022-01-10 16:30:52 +02:00
parent b26f5c0fe3
commit 384aa245ef

View file

@ -1942,29 +1942,23 @@ struct crypto_bignum *
crypto_ec_point_compute_y_sqr(struct crypto_ec *e, crypto_ec_point_compute_y_sqr(struct crypto_ec *e,
const struct crypto_bignum *x) const struct crypto_bignum *x)
{ {
BIGNUM *tmp, *tmp2, *y_sqr = NULL; BIGNUM *tmp;
if (TEST_FAIL()) if (TEST_FAIL())
return NULL; return NULL;
tmp = BN_new(); tmp = BN_new();
tmp2 = BN_new();
/* y^2 = x^3 + ax + b */ /* y^2 = x^3 + ax + b = (x^2 + a)x + b */
if (tmp && tmp2 && if (tmp &&
BN_mod_sqr(tmp, (const BIGNUM *) x, e->prime, e->bnctx) && BN_mod_sqr(tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
BN_mod_add_quick(tmp, e->a, tmp, e->prime) &&
BN_mod_mul(tmp, tmp, (const BIGNUM *) x, e->prime, e->bnctx) && BN_mod_mul(tmp, tmp, (const BIGNUM *) x, e->prime, e->bnctx) &&
BN_mod_mul(tmp2, e->a, (const BIGNUM *) x, e->prime, e->bnctx) && BN_mod_add_quick(tmp, tmp, e->b, e->prime))
BN_mod_add_quick(tmp2, tmp2, tmp, e->prime) && return (struct crypto_bignum *) tmp;
BN_mod_add_quick(tmp2, tmp2, e->b, e->prime)) {
y_sqr = tmp2;
tmp2 = NULL;
}
BN_clear_free(tmp); BN_clear_free(tmp);
BN_clear_free(tmp2); return NULL;
return (struct crypto_bignum *) y_sqr;
} }