From 4ccc092c18e245f69b25703cd73e776f5d1efc40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Love=20H=C3=B6rnquist=20=C3=85strand?= Date: Thu, 11 Jan 2007 20:23:15 +0000 Subject: [PATCH] Update to imath-1.8 from Michael Fromberger MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed a bug in s_udiv() affecting the computation of quotient digits. Thanks to Love Åstrand for isolating this bug. Also in this release, defining USELLONG=Y or USELLONG=N on the command line for make will switch support for the "long long" data type on or off without having to edit the Makefile. The default is still to permit use of "long long", even though the type is not standard ANSI C90. git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@19854 ec53bebd-3082-4978-b11e-865c3cabbd6b --- lib/des/imath/imath.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/des/imath/imath.c b/lib/des/imath/imath.c index 46b3bf635..8b33c922a 100755 --- a/lib/des/imath/imath.c +++ b/lib/des/imath/imath.c @@ -34,7 +34,6 @@ #endif #include -#include #include #include @@ -2994,7 +2993,7 @@ static mp_result s_udiv(mp_int a, mp_int b) k = s_norm(a, b); ua = MP_USED(a); ub = MP_USED(b); btop = b->digits[ub - 1]; - if((res = mp_int_init_size(&q, ua + 1)) != MP_OK) return res; + if((res = mp_int_init_size(&q, ua)) != MP_OK) return res; if((res = mp_int_init_size(&t, ua + 1)) != MP_OK) goto CLEANUP; da = MP_DIGITS(a); @@ -3012,7 +3011,7 @@ static mp_result s_udiv(mp_int a, mp_int b) r.digits -= 1; r.used += 1; - if(++skip > 1) + if(++skip > 1 && qpos > 0) q.digits[qpos++] = 0; CLAMP(&r); @@ -3021,15 +3020,19 @@ static mp_result s_udiv(mp_int a, mp_int b) mp_word pfx = r.digits[r.used - 1]; mp_word qdigit; - if(r.used > 1 && (pfx < btop || r.digits[r.used - 2] == 0)) { + if(r.used > 1 && pfx <= btop) { pfx <<= MP_DIGIT_BIT / 2; pfx <<= MP_DIGIT_BIT / 2; pfx |= r.digits[r.used - 2]; } qdigit = pfx / btop; - if(qdigit > MP_DIGIT_MAX) - qdigit = 1; + if(qdigit > MP_DIGIT_MAX) { + if(qdigit & MP_DIGIT_MAX) + qdigit = MP_DIGIT_MAX; + else + qdigit = 1; + } s_dbmul(MP_DIGITS(b), (mp_digit) qdigit, t.digits, ub); t.used = ub + 1; CLAMP(&t); @@ -3046,7 +3049,7 @@ static mp_result s_udiv(mp_int a, mp_int b) skip = 0; } } - + /* Put quotient digits in the correct order, and discard extra zeroes */ q.used = qpos; REV(mp_digit, q.digits, qpos);