shithub: riscv

Download patch

ref: bdc87e17bd2522552f3c97c741bda0ae29df3528
parent: b65f409d542e63302a68325f292a364a0b3bae2e
author: cinap_lenrek <[email protected]>
date: Sun Aug 28 12:33:52 EDT 2016

libmp: timingsafe sign flip for small power-of-two negative divisor for mpdiv()

--- a/sys/src/libmp/port/mpdiv.c
+++ b/sys/src/libmp/port/mpdiv.c
@@ -24,12 +24,11 @@
 	if(divisor->top == 1 && (divisor->p[0] & divisor->p[0]-1) == 0){
 		vlong r = (vlong)dividend->sign * (dividend->p[0] & divisor->p[0]-1);
 		if(quotient != nil){
-			sign = divisor->sign;
 			for(s = 0; ((divisor->p[0] >> s) & 1) == 0; s++)
 				;
 			mpright(dividend, s, quotient);
-			if(sign < 0 && quotient->top != 0)
-				quotient->sign ^= -2;
+			if(divisor->sign < 0)
+				quotient->sign ^= (-mpmagcmp(quotient, mpzero) >> 31) << 1;
 		}
 		if(remainder != nil){
 			remainder->flags |= dividend->flags & MPtimesafe;