From b709d39aa5989305f448f72ed25dc1fbce9a365a Mon Sep 17 00:00:00 2001 From: Moritz Lenz Date: Mon, 7 Oct 2013 17:35:26 -0500 Subject: [PATCH] libtommath: Fix mp_mod(a, b, c) if b < 0 and a = n * b, n integer it used to return b, now it return 0. From https://github.com/libtom/libtommath Change-Id: I99f5d42c09a6fbddfb32b7dd0ed10ef04f1c08a5 --- lib/hcrypto/libtommath/bn_mp_mod.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/hcrypto/libtommath/bn_mp_mod.c b/lib/hcrypto/libtommath/bn_mp_mod.c index f5cf8d09f..274e7dcd1 100644 --- a/lib/hcrypto/libtommath/bn_mp_mod.c +++ b/lib/hcrypto/libtommath/bn_mp_mod.c @@ -15,7 +15,7 @@ * Tom St Denis, tomstdenis@gmail.com, http://libtom.org */ -/* c = a mod b, 0 <= c < b */ +/* c = a mod b, 0 <= c < b if b > 0, b < c <= 0 if b < 0 */ int mp_mod (mp_int * a, mp_int * b, mp_int * c) { @@ -31,11 +31,11 @@ mp_mod (mp_int * a, mp_int * b, mp_int * c) return res; } - if (t.sign != b->sign) { - res = mp_add (b, &t, c); - } else { + if (mp_iszero(&t) || t.sign == b.sign) { res = MP_OKAY; mp_exch (&t, c); + } else { + res = mp_add (b, &t, c); } mp_clear (&t);