Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 29 Jul 2006 13:09:24 +0200
From:      "Simon L. Nielsen" <simon@FreeBSD.org>
To:        Stefan Farfeleder <stefanf@FreeBSD.org>
Cc:        cvs-src@FreeBSD.org, src-committers@FreeBSD.org, cvs-all@FreeBSD.org
Subject:   Re: cvs commit: src/lib/libmp mpasbn.c
Message-ID:  <20060729110923.GA1085@zaphod.nitro.dk>
In-Reply-To: <20060729061426.GA79029@wombat.fafoe.narf.at>
References:  <200607282300.k6SN0Gdn005489@repoman.freebsd.org> <20060729061426.GA79029@wombat.fafoe.narf.at>

next in thread | previous in thread | raw e-mail | index | archive | help

--UugvWAfsgieZRqgk
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On 2006.07.29 08:14:27 +0200, Stefan Farfeleder wrote:
> On Fri, Jul 28, 2006 at 11:00:16PM +0000, Simon L. Nielsen wrote:
> > simon       2006-07-28 23:00:16 UTC
> > 
> >   FreeBSD src repository
> > 
> >   Modified files:
> >     lib/libmp            mpasbn.c 
> >   Log:
> >   Do not put BN_CTX structures on the stack, but instead allocate them
> >   runtime using BN_CTX_new().  This is done since in OpenSSL 0.9.7e we
> >   can only allocate BN_CTX on the stack by including an internal OpenSSL
> >   header file, and in OpenSSL 0.9.8 BN_CTX is entirely opaque, so having
> >   it on the stack is not possible at all.
> >   
> >   This is done as preparation for OpenSSL 0.9.8b import.
> 
> It might be preferable to pass the allocated BN_CTXs to _mdiv(),
> _mult() and _sdiv() because then msqrt() would have to call
> BN_CTX_new() only once.

Sounds like a good idea.  How about the attached patch?

-- 
Simon L. Nielsen

--UugvWAfsgieZRqgk
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="libmp-msqrt-one-ctx.patch"

Index: mpasbn.c
===================================================================
RCS file: /home/ncvs/src/lib/libmp/mpasbn.c,v
retrieving revision 1.4
diff -u -d -r1.4 mpasbn.c
--- mpasbn.c	28 Jul 2006 23:00:16 -0000	1.4
+++ mpasbn.c	29 Jul 2006 11:02:31 -0000
@@ -95,15 +95,16 @@
 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 @@
  * 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 @@
 	_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 @@
 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 @@
 	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 @@
 	_mfree("msqrt", z1);
 	_mfree("msqrt", z2);
 	_mfree("msqrt", z3);
+	BN_CTX_free(c);
 }
 
 /*
@@ -470,26 +477,26 @@
  * 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 @@
  * 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 @@
 	_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);
 }
 
 /*

--UugvWAfsgieZRqgk--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20060729110923.GA1085>