shithub: riscv

Download patch

ref: ffdfc17ceede743b6fa32a6d1659f62f0d0d8650
parent: fbd825890e8376b80a480ef294496d628fb75d3c
author: cinap_lenrek <[email protected]>
date: Tue Dec 1 06:13:52 EST 2015

libmp: with mpmod() m = 2^a - c ensure that digits(c) < digits(m)

--- a/sys/src/libmp/port/mpmod.c
+++ b/sys/src/libmp/port/mpmod.c
@@ -13,7 +13,7 @@
 	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 @@
 			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);