summaryrefslogtreecommitdiffstats
path: root/lib/libmp
diff options
context:
space:
mode:
authorsimon <simon@FreeBSD.org>2006-07-30 19:29:26 +0000
committersimon <simon@FreeBSD.org>2006-07-30 19:29:26 +0000
commitcd84d984f541814e3c210dd6761292e09b482524 (patch)
treeceeca20d01eef0cd5e19bc7329a77f8d6d31ba6b /lib/libmp
parent3e01f8e05a4870946d790e1c241e5a6ec827a58c (diff)
downloadFreeBSD-src-cd84d984f541814e3c210dd6761292e09b482524.zip
FreeBSD-src-cd84d984f541814e3c210dd6761292e09b482524.tar.gz
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
Diffstat (limited to 'lib/libmp')
-rw-r--r--lib/libmp/mpasbn.c62
1 files changed, 35 insertions, 27 deletions
diff --git a/lib/libmp/mpasbn.c b/lib/libmp/mpasbn.c
index 0775dba..797960d 100644
--- a/lib/libmp/mpasbn.c
+++ b/lib/libmp/mpasbn.c
@@ -95,15 +95,16 @@ static MINT *_dtom(const char *, const char *);
static MINT *_itom(const char *, short);
static void _madd(const char *, const MINT *, const MINT *, 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 _moveb(const char *, const BIGNUM *, MINT *);
static void _movem(const char *, const MINT *, MINT *);
static void _msub(const char *, const MINT *, const MINT *, MINT *);
static char *_mtod(const char *, const MINT *);
static char *_mtox(const char *, const MINT *);
-static void _mult(const char *, const MINT *, const MINT *, MINT *);
-static void _sdiv(const char *, const MINT *, short, MINT *, short *);
+static void _mult(const char *, const MINT *, const MINT *, MINT *, BN_CTX *);
+static void _sdiv(const char *, const MINT *, short, MINT *, short *, BN_CTX *);
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.
*/
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;
- BN_CTX *c;
- c = BN_CTX_new();
- if (c == NULL)
- _bnerr(msg);
BN_init(&r);
BN_init(&q);
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);
BN_free(&q);
BN_free(&r);
- BN_CTX_free(c);
}
void
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
msqrt(const MINT *nmp, MINT *xmp, MINT *rmp)
{
+ BN_CTX *c;
MINT *tolerance;
MINT *ox, *x;
MINT *z1, *z2, *z3;
short i;
+ c = BN_CTX_new();
+ if (c == NULL)
+ _bnerr("msqrt");
tolerance = _itom("msqrt", 1);
x = _itom("msqrt", 1);
ox = _itom("msqrt", 0);
@@ -370,13 +376,13 @@ msqrt(const MINT *nmp, MINT *xmp, MINT *rmp)
z3 = _itom("msqrt", 0);
do {
_movem("msqrt", x, ox);
- _mdiv("msqrt", nmp, x, z1, z2);
+ _mdiv("msqrt", nmp, x, z1, z2, c);
_madd("msqrt", x, z1, z2);
- _sdiv("msqrt", z2, 2, x, &i);
+ _sdiv("msqrt", z2, 2, x, &i, c);
_msub("msqrt", ox, x, z3);
} while (_mcmpa("msqrt", z3, tolerance) == 1);
_movem("msqrt", x, xmp);
- _mult("msqrt", x, x, z1);
+ _mult("msqrt", x, x, z1, c);
_msub("msqrt", nmp, z1, z2);
_movem("msqrt", z2, rmp);
_mfree("msqrt", tolerance);
@@ -385,6 +391,7 @@ msqrt(const MINT *nmp, MINT *xmp, MINT *rmp)
_mfree("msqrt", z1);
_mfree("msqrt", z2);
_mfree("msqrt", z3);
+ BN_CTX_free(c);
}
/*
@@ -470,26 +477,26 @@ mtox(const MINT *mp)
* Compute rmp=mp1*mp2.
*/
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;
- BN_CTX *c;
- c = BN_CTX_new();
- if (c == NULL)
- _bnerr(msg);
BN_init(&b);
BN_ERRCHECK(msg, BN_mul(&b, mp1->bn, mp2->bn, c));
_moveb(msg, &b, rmp);
BN_free(&b);
- BN_CTX_free(c);
}
void
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.
*/
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;
BIGNUM q, r;
- BN_CTX *c;
char *s;
- c = BN_CTX_new();
- if (c == NULL)
- _bnerr(msg);
BN_init(&q);
BN_init(&r);
dmp = _itom(msg, d);
@@ -565,14 +569,18 @@ _sdiv(const char *msg, const MINT *nmp, short d, MINT *qmp, short *ro)
_mfree(msg, rmp);
BN_free(&r);
BN_free(&q);
- BN_CTX_free(c);
}
void
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);
}
/*
OpenPOWER on IntegriCloud