@@ -560,9 +560,9 @@ public boolean same_value(Bignat other) {
560560 * @param y array with second bignat
561561 * @param yOffset start offset in array of {@code y}
562562 * @param yLength length of {@code y}
563- * @return true if carry of most significant byte occurs, false otherwise
563+ * @return 0x01 if carry of most significant byte occurs, 0x00 otherwise
564564 */
565- public static boolean add (byte [] x , short xOffset , short xLength , byte [] y ,
565+ public static byte add (byte [] x , short xOffset , short xLength , byte [] y ,
566566 short yOffset , short yLength ) {
567567 short result = 0 ;
568568 short i = (short ) (xLength + xOffset - 1 );
@@ -581,7 +581,12 @@ public static boolean add(byte[] x, short xOffset, short xLength, byte[] y,
581581 i --;
582582 }
583583
584- return result != 0 ;
584+ // 1. result != 0 => result | -result will have the sign bit set
585+ // 2. casting magic to overcome the absence of int
586+ // 3. move the sign bit to the rightmost position
587+ // 4. discard the sign bit which is present due to the unavoidable casts
588+ // and return the value of the rightmost bit
589+ return (byte ) ((byte ) (((short )(result | -result ) & (short )0xFFFF ) >>> 15 ) & 0x01 );
585590 }
586591
587592 /**
@@ -1417,18 +1422,20 @@ public void mult_rsa_trick(Bignat x, Bignat y, byte[] x_pow_2, byte[] y_pow_2) {
14171422 // Copy x to the end of mult_resultArray
14181423 xOffset = (short ) (bnh .fnc_mult_resultArray1 .length - x .length ());
14191424 Util .arrayCopyNonAtomic (x .value , (short ) 0 , bnh .fnc_mult_resultArray1 , xOffset , x .length ());
1420- if (add (bnh .fnc_mult_resultArray1 , xOffset , x .size , y .value , (short ) 0 , y .size )) {
1421- xOffset --;
1422- bnh .fnc_mult_resultArray1 [xOffset ] = 0x01 ;
1423- }
1425+
1426+ // modified for CT
1427+ byte carry = add (bnh .fnc_mult_resultArray1 , xOffset , x .size , y .value , (short ) 0 , y .size );
1428+ xOffset --;
1429+ bnh .fnc_mult_resultArray1 [xOffset ] = carry ; // add carry if occured
14241430 } else {
14251431 // Copy x to the end of mult_resultArray
14261432 yOffset = (short ) (bnh .fnc_mult_resultArray1 .length - y .length ());
14271433 Util .arrayCopyNonAtomic (y .value , (short ) 0 , bnh .fnc_mult_resultArray1 , yOffset , y .length ());
1428- if (add (bnh .fnc_mult_resultArray1 , yOffset , y .size , x .value , (short ) 0 , x .size )) {
1429- yOffset --;
1430- bnh .fnc_mult_resultArray1 [yOffset ] = 0x01 ; // add carry if occured
1431- }
1434+
1435+ // modified for CT
1436+ byte carry = add (bnh .fnc_mult_resultArray1 , yOffset , y .size , x .value , (short ) 0 , x .size );
1437+ yOffset --;
1438+ bnh .fnc_mult_resultArray1 [yOffset ] = carry ; // add carry if occured
14321439 }
14331440
14341441 // ((x+y)^2)
0 commit comments