git: 9front

Download patch

ref: d5b32ac6ce6a549ec9304d27233b4a94b26b093c
parent: 20bac609b54d90d249232651e7ae99d08e8f524b
author: cinap_lenrek <cinap_lenrek@felloff.net>
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;
--