Pass BN_CTX to internal functions instead of allocating it internally.

This allows msqrt() to only call BN_CTX_new() once intead of many times.

Suggested and reviewed by:	stefanf
This commit is contained in:
Simon L. B. Nielsen 2006-07-30 19:29:26 +00:00
parent 902ce6f035
commit 78078a569f
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=160840

View file

@ -95,15 +95,16 @@ static MINT *_dtom(const char *, const char *);
static MINT *_itom(const char *, short); static MINT *_itom(const char *, short);
static void _madd(const char *, const MINT *, const MINT *, MINT *); static void _madd(const char *, const MINT *, const MINT *, MINT *);
static int _mcmpa(const char *, const MINT *, const MINT *); static int _mcmpa(const char *, const MINT *, const MINT *);
static void _mdiv(const char *, const MINT *, const MINT *, MINT *, MINT *); static void _mdiv(const char *, const MINT *, const MINT *, MINT *, MINT *,
BN_CTX *);
static void _mfree(const char *, MINT *); static void _mfree(const char *, MINT *);
static void _moveb(const char *, const BIGNUM *, MINT *); static void _moveb(const char *, const BIGNUM *, MINT *);
static void _movem(const char *, const MINT *, MINT *); static void _movem(const char *, const MINT *, MINT *);
static void _msub(const char *, const MINT *, const MINT *, MINT *); static void _msub(const char *, const MINT *, const MINT *, MINT *);
static char *_mtod(const char *, const MINT *); static char *_mtod(const char *, const MINT *);
static char *_mtox(const char *, const MINT *); static char *_mtox(const char *, const MINT *);
static void _mult(const char *, const MINT *, const MINT *, MINT *); static void _mult(const char *, const MINT *, const MINT *, MINT *, BN_CTX *);
static void _sdiv(const char *, const MINT *, short, MINT *, short *); static void _sdiv(const char *, const MINT *, short, MINT *, short *, BN_CTX *);
static MINT *_xtom(const char *, const char *); static MINT *_xtom(const char *, const char *);
/* /*
@ -223,14 +224,11 @@ _mcmpa(const char *msg __unused, const MINT *mp1, const MINT *mp2)
* Compute qmp=nmp/dmp and rmp=nmp%dmp. * Compute qmp=nmp/dmp and rmp=nmp%dmp.
*/ */
static void 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)
{ {
BIGNUM q, r; BIGNUM q, r;
BN_CTX *c;
c = BN_CTX_new();
if (c == NULL)
_bnerr(msg);
BN_init(&r); BN_init(&r);
BN_init(&q); BN_init(&q);
BN_ERRCHECK(msg, BN_div(&q, &r, nmp->bn, dmp->bn, c)); BN_ERRCHECK(msg, BN_div(&q, &r, nmp->bn, dmp->bn, c));
@ -238,14 +236,18 @@ _mdiv(const char *msg, const MINT *nmp, const MINT *dmp, MINT *qmp, MINT *rmp)
_moveb(msg, &r, rmp); _moveb(msg, &r, rmp);
BN_free(&q); BN_free(&q);
BN_free(&r); BN_free(&r);
BN_CTX_free(c);
} }
void void
mdiv(const MINT *nmp, const MINT *dmp, MINT *qmp, MINT *rmp) mdiv(const MINT *nmp, const MINT *dmp, MINT *qmp, MINT *rmp)
{ {
BN_CTX *c;
_mdiv("mdiv", nmp, dmp, qmp, rmp); c = BN_CTX_new();
if (c == NULL)
_bnerr("mdiv");
_mdiv("mdiv", nmp, dmp, qmp, rmp, c);
BN_CTX_free(c);
} }
/* /*
@ -357,11 +359,15 @@ _movem(const char *msg, const MINT *smp, MINT *tmp)
void void
msqrt(const MINT *nmp, MINT *xmp, MINT *rmp) msqrt(const MINT *nmp, MINT *xmp, MINT *rmp)
{ {
BN_CTX *c;
MINT *tolerance; MINT *tolerance;
MINT *ox, *x; MINT *ox, *x;
MINT *z1, *z2, *z3; MINT *z1, *z2, *z3;
short i; short i;
c = BN_CTX_new();
if (c == NULL)
_bnerr("msqrt");
tolerance = _itom("msqrt", 1); tolerance = _itom("msqrt", 1);
x = _itom("msqrt", 1); x = _itom("msqrt", 1);
ox = _itom("msqrt", 0); ox = _itom("msqrt", 0);
@ -370,13 +376,13 @@ msqrt(const MINT *nmp, MINT *xmp, MINT *rmp)
z3 = _itom("msqrt", 0); z3 = _itom("msqrt", 0);
do { do {
_movem("msqrt", x, ox); _movem("msqrt", x, ox);
_mdiv("msqrt", nmp, x, z1, z2); _mdiv("msqrt", nmp, x, z1, z2, c);
_madd("msqrt", x, z1, z2); _madd("msqrt", x, z1, z2);
_sdiv("msqrt", z2, 2, x, &i); _sdiv("msqrt", z2, 2, x, &i, c);
_msub("msqrt", ox, x, z3); _msub("msqrt", ox, x, z3);
} while (_mcmpa("msqrt", z3, tolerance) == 1); } while (_mcmpa("msqrt", z3, tolerance) == 1);
_movem("msqrt", x, xmp); _movem("msqrt", x, xmp);
_mult("msqrt", x, x, z1); _mult("msqrt", x, x, z1, c);
_msub("msqrt", nmp, z1, z2); _msub("msqrt", nmp, z1, z2);
_movem("msqrt", z2, rmp); _movem("msqrt", z2, rmp);
_mfree("msqrt", tolerance); _mfree("msqrt", tolerance);
@ -385,6 +391,7 @@ msqrt(const MINT *nmp, MINT *xmp, MINT *rmp)
_mfree("msqrt", z1); _mfree("msqrt", z1);
_mfree("msqrt", z2); _mfree("msqrt", z2);
_mfree("msqrt", z3); _mfree("msqrt", z3);
BN_CTX_free(c);
} }
/* /*
@ -470,26 +477,26 @@ mtox(const MINT *mp)
* Compute rmp=mp1*mp2. * Compute rmp=mp1*mp2.
*/ */
static void static void
_mult(const char *msg, const MINT *mp1, const MINT *mp2, MINT *rmp) _mult(const char *msg, const MINT *mp1, const MINT *mp2, MINT *rmp, BN_CTX *c)
{ {
BIGNUM b; BIGNUM b;
BN_CTX *c;
c = BN_CTX_new();
if (c == NULL)
_bnerr(msg);
BN_init(&b); BN_init(&b);
BN_ERRCHECK(msg, BN_mul(&b, mp1->bn, mp2->bn, c)); BN_ERRCHECK(msg, BN_mul(&b, mp1->bn, mp2->bn, c));
_moveb(msg, &b, rmp); _moveb(msg, &b, rmp);
BN_free(&b); BN_free(&b);
BN_CTX_free(c);
} }
void void
mult(const MINT *mp1, const MINT *mp2, MINT *rmp) mult(const MINT *mp1, const MINT *mp2, MINT *rmp)
{ {
BN_CTX *c;
_mult("mult", mp1, mp2, rmp); c = BN_CTX_new();
if (c == NULL)
_bnerr("mult");
_mult("mult", mp1, mp2, rmp, c);
BN_CTX_free(c);
} }
/* /*
@ -538,16 +545,13 @@ rpow(const MINT *bmp, short e, MINT *rmp)
* Compute qmp=nmp/d and ro=nmp%d. * Compute qmp=nmp/d and ro=nmp%d.
*/ */
static void static void
_sdiv(const char *msg, const MINT *nmp, short d, MINT *qmp, short *ro) _sdiv(const char *msg, const MINT *nmp, short d, MINT *qmp, short *ro,
BN_CTX *c)
{ {
MINT *dmp, *rmp; MINT *dmp, *rmp;
BIGNUM q, r; BIGNUM q, r;
BN_CTX *c;
char *s; char *s;
c = BN_CTX_new();
if (c == NULL)
_bnerr(msg);
BN_init(&q); BN_init(&q);
BN_init(&r); BN_init(&r);
dmp = _itom(msg, d); dmp = _itom(msg, d);
@ -565,14 +569,18 @@ _sdiv(const char *msg, const MINT *nmp, short d, MINT *qmp, short *ro)
_mfree(msg, rmp); _mfree(msg, rmp);
BN_free(&r); BN_free(&r);
BN_free(&q); BN_free(&q);
BN_CTX_free(c);
} }
void void
sdiv(const MINT *nmp, short d, MINT *qmp, short *ro) sdiv(const MINT *nmp, short d, MINT *qmp, short *ro)
{ {
BN_CTX *c;
_sdiv("sdiv", nmp, d, qmp, ro); c = BN_CTX_new();
if (c == NULL)
_bnerr("sdiv");
_sdiv("sdiv", nmp, d, qmp, ro, c);
BN_CTX_free(c);
} }
/* /*