bn_fast_mp_invmod fix related to #118
diff --git a/bn_fast_mp_invmod.c b/bn_fast_mp_invmod.c
index 91b5bf6..be1a810 100644
--- a/bn_fast_mp_invmod.c
+++ b/bn_fast_mp_invmod.c
@@ -138,6 +138,14 @@ top:
goto LBL_ERR;
}
}
+
+ /* too big */
+ while (mp_cmp_mag(&D, b) != MP_LT) {
+ if ((res = mp_sub(&D, b, &D)) != MP_OKAY) {
+ goto LBL_ERR;
+ }
+ }
+
mp_exch(&D, c);
c->sign = neg;
res = MP_OKAY;