From ffdfc17ceede743b6fa32a6d1659f62f0d0d8650 Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Tue, 1 Dec 2015 11:13:52 +0100 Subject: [PATCH] libmp: with mpmod() m = 2^a - c ensure that digits(c) < digits(m) --- sys/src/libmp/port/mpmod.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/sys/src/libmp/port/mpmod.c b/sys/src/libmp/port/mpmod.c index c053f5b7f..b5dddf105 100644 --- a/sys/src/libmp/port/mpmod.c +++ b/sys/src/libmp/port/mpmod.c @@ -13,7 +13,7 @@ mpmod(mpint *x, mpint *n, mpint *r) sign = x->sign; assert(n->flags & MPnorm); - if(n->top < 2 || n->top > nelem(q) || (x->top-n->top) > nelem(q)) + if(n->top <= 2 || n->top > nelem(q) || (x->top-n->top) > nelem(q)) goto hard; /* @@ -42,11 +42,15 @@ mpmod(mpint *x, mpint *n, mpint *r) c = mpnew(0); p = mpnew(0); } - mpassign(n, p); - - mpleft(n, s, m); + mpleft(p, s, m); mpleft(mpone, k*Dbits, c); mpsub(c, m, c); + if(c->top >= k){ + mpassign(mpzero, p); + busy = 0; + goto hard; + } + mpassign(n, p); } mpleft(x, s, r);