From owner-cvs-src@FreeBSD.ORG Sat Jul 29 11:09:27 2006 Return-Path: X-Original-To: cvs-src@FreeBSD.org Delivered-To: cvs-src@FreeBSD.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 1D68116A4DD; Sat, 29 Jul 2006 11:09:27 +0000 (UTC) (envelope-from simon@zaphod.nitro.dk) Received: from mx.nitro.dk (zarniwoop.nitro.dk [83.92.207.38]) by mx1.FreeBSD.org (Postfix) with ESMTP id 5656843D45; Sat, 29 Jul 2006 11:09:25 +0000 (GMT) (envelope-from simon@zaphod.nitro.dk) Received: from zaphod.nitro.dk (unknown [192.168.3.39]) by mx.nitro.dk (Postfix) with ESMTP id 896E22D65C4; Sat, 29 Jul 2006 11:09:24 +0000 (UTC) Received: by zaphod.nitro.dk (Postfix, from userid 3000) id 568E011444; Sat, 29 Jul 2006 13:09:24 +0200 (CEST) Date: Sat, 29 Jul 2006 13:09:24 +0200 From: "Simon L. Nielsen" To: Stefan Farfeleder Message-ID: <20060729110923.GA1085@zaphod.nitro.dk> References: <200607282300.k6SN0Gdn005489@repoman.freebsd.org> <20060729061426.GA79029@wombat.fafoe.narf.at> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="UugvWAfsgieZRqgk" Content-Disposition: inline In-Reply-To: <20060729061426.GA79029@wombat.fafoe.narf.at> User-Agent: Mutt/1.5.11 Cc: cvs-src@FreeBSD.org, src-committers@FreeBSD.org, cvs-all@FreeBSD.org Subject: Re: cvs commit: src/lib/libmp mpasbn.c X-BeenThere: cvs-src@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: CVS commit messages for the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 29 Jul 2006 11:09:27 -0000 --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--