Make libmp(3) buildable.

This commit is contained in:
jkim 2018-09-19 07:05:31 +00:00
parent 9dfee4aa81
commit 76850e3a16

View File

@ -144,16 +144,18 @@ _dtom(const char *msg, const char *s)
void void
mp_gcd(const MINT *mp1, const MINT *mp2, MINT *rmp) mp_gcd(const MINT *mp1, const MINT *mp2, MINT *rmp)
{ {
BIGNUM b; BIGNUM *b;
BN_CTX *c; BN_CTX *c;
b = NULL;
c = BN_CTX_new(); c = BN_CTX_new();
if (c == NULL) if (c != NULL)
b = BN_new();
if (c == NULL || b == NULL)
_bnerr("gcd"); _bnerr("gcd");
BN_init(&b); BN_ERRCHECK("gcd", BN_gcd(b, mp1->bn, mp2->bn, c));
BN_ERRCHECK("gcd", BN_gcd(&b, mp1->bn, mp2->bn, c)); _moveb("gcd", b, rmp);
_moveb("gcd", &b, rmp); BN_free(b);
BN_free(&b);
BN_CTX_free(c); BN_CTX_free(c);
} }
@ -187,12 +189,14 @@ mp_itom(short n)
static void static void
_madd(const char *msg, const MINT *mp1, const MINT *mp2, MINT *rmp) _madd(const char *msg, const MINT *mp1, const MINT *mp2, MINT *rmp)
{ {
BIGNUM b; BIGNUM *b;
BN_init(&b); b = BN_new();
BN_ERRCHECK(msg, BN_add(&b, mp1->bn, mp2->bn)); if (b == NULL)
_moveb(msg, &b, rmp); _bnerr(msg);
BN_free(&b); BN_ERRCHECK(msg, BN_add(b, mp1->bn, mp2->bn));
_moveb(msg, b, rmp);
BN_free(b);
} }
void void
@ -229,15 +233,19 @@ static void
_mdiv(const char *msg, const MINT *nmp, const MINT *dmp, MINT *qmp, MINT *rmp, _mdiv(const char *msg, const MINT *nmp, const MINT *dmp, MINT *qmp, MINT *rmp,
BN_CTX *c) BN_CTX *c)
{ {
BIGNUM q, r; BIGNUM *q, *r;
BN_init(&r); q = NULL;
BN_init(&q); r = BN_new();
BN_ERRCHECK(msg, BN_div(&q, &r, nmp->bn, dmp->bn, c)); if (r != NULL)
_moveb(msg, &q, qmp); q = BN_new();
_moveb(msg, &r, rmp); if (r == NULL || q == NULL)
BN_free(&q); _bnerr(msg);
BN_free(&r); BN_ERRCHECK(msg, BN_div(q, r, nmp->bn, dmp->bn, c));
_moveb(msg, q, qmp);
_moveb(msg, r, rmp);
BN_free(q);
BN_free(r);
} }
void void
@ -402,12 +410,14 @@ mp_msqrt(const MINT *nmp, MINT *xmp, MINT *rmp)
static void static void
_msub(const char *msg, const MINT *mp1, const MINT *mp2, MINT *rmp) _msub(const char *msg, const MINT *mp1, const MINT *mp2, MINT *rmp)
{ {
BIGNUM b; BIGNUM *b;
BN_init(&b); b = BN_new();
BN_ERRCHECK(msg, BN_sub(&b, mp1->bn, mp2->bn)); if (b == NULL)
_moveb(msg, &b, rmp); _bnerr(msg);
BN_free(&b); BN_ERRCHECK(msg, BN_sub(b, mp1->bn, mp2->bn));
_moveb(msg, b, rmp);
BN_free(b);
} }
void void
@ -481,12 +491,14 @@ mp_mtox(const MINT *mp)
static void static void
_mult(const char *msg, const MINT *mp1, const MINT *mp2, MINT *rmp, BN_CTX *c) _mult(const char *msg, const MINT *mp1, const MINT *mp2, MINT *rmp, BN_CTX *c)
{ {
BIGNUM b; BIGNUM *b;
BN_init(&b); b = BN_new();
BN_ERRCHECK(msg, BN_mul(&b, mp1->bn, mp2->bn, c)); if (b == NULL)
_moveb(msg, &b, rmp); _bnerr(msg);
BN_free(&b); BN_ERRCHECK(msg, BN_mul(b, mp1->bn, mp2->bn, c));
_moveb(msg, b, rmp);
BN_free(b);
} }
void void
@ -508,16 +520,18 @@ mp_mult(const MINT *mp1, const MINT *mp2, MINT *rmp)
void void
mp_pow(const MINT *bmp, const MINT *emp, const MINT *mmp, MINT *rmp) mp_pow(const MINT *bmp, const MINT *emp, const MINT *mmp, MINT *rmp)
{ {
BIGNUM b; BIGNUM *b;
BN_CTX *c; BN_CTX *c;
b = NULL;
c = BN_CTX_new(); c = BN_CTX_new();
if (c == NULL) if (c != NULL)
b = BN_new();
if (c == NULL || b == NULL)
_bnerr("pow"); _bnerr("pow");
BN_init(&b); BN_ERRCHECK("pow", BN_mod_exp(b, bmp->bn, emp->bn, mmp->bn, c));
BN_ERRCHECK("pow", BN_mod_exp(&b, bmp->bn, emp->bn, mmp->bn, c)); _moveb("pow", b, rmp);
_moveb("pow", &b, rmp); BN_free(b);
BN_free(&b);
BN_CTX_free(c); BN_CTX_free(c);
} }
@ -528,18 +542,20 @@ void
mp_rpow(const MINT *bmp, short e, MINT *rmp) mp_rpow(const MINT *bmp, short e, MINT *rmp)
{ {
MINT *emp; MINT *emp;
BIGNUM b; BIGNUM *b;
BN_CTX *c; BN_CTX *c;
b = NULL;
c = BN_CTX_new(); c = BN_CTX_new();
if (c == NULL) if (c != NULL)
b = BN_new();
if (c == NULL || b == NULL)
_bnerr("rpow"); _bnerr("rpow");
BN_init(&b);
emp = _itom("rpow", e); emp = _itom("rpow", e);
BN_ERRCHECK("rpow", BN_exp(&b, bmp->bn, emp->bn, c)); BN_ERRCHECK("rpow", BN_exp(b, bmp->bn, emp->bn, c));
_moveb("rpow", &b, rmp); _moveb("rpow", b, rmp);
_mfree("rpow", emp); _mfree("rpow", emp);
BN_free(&b); BN_free(b);
BN_CTX_free(c); BN_CTX_free(c);
} }
@ -551,16 +567,20 @@ _sdiv(const char *msg, const MINT *nmp, short d, MINT *qmp, short *ro,
BN_CTX *c) BN_CTX *c)
{ {
MINT *dmp, *rmp; MINT *dmp, *rmp;
BIGNUM q, r; BIGNUM *q, *r;
char *s; char *s;
BN_init(&q); r = NULL;
BN_init(&r); q = BN_new();
if (q != NULL)
r = BN_new();
if (q == NULL || r == NULL)
_bnerr(msg);
dmp = _itom(msg, d); dmp = _itom(msg, d);
rmp = _itom(msg, 0); rmp = _itom(msg, 0);
BN_ERRCHECK(msg, BN_div(&q, &r, nmp->bn, dmp->bn, c)); BN_ERRCHECK(msg, BN_div(q, r, nmp->bn, dmp->bn, c));
_moveb(msg, &q, qmp); _moveb(msg, q, qmp);
_moveb(msg, &r, rmp); _moveb(msg, r, rmp);
s = _mtox(msg, rmp); s = _mtox(msg, rmp);
errno = 0; errno = 0;
*ro = strtol(s, NULL, 16); *ro = strtol(s, NULL, 16);
@ -569,8 +589,8 @@ _sdiv(const char *msg, const MINT *nmp, short d, MINT *qmp, short *ro,
free(s); free(s);
_mfree(msg, dmp); _mfree(msg, dmp);
_mfree(msg, rmp); _mfree(msg, rmp);
BN_free(&r); BN_free(r);
BN_free(&q); BN_free(q);
} }
void void