[Impressum]
[E-Mail]
package generated.javacard.copycardMoneySecure.copycard;
import javacard.framework.ISOException;
import javacard.framework.ISO7816;
public class Math {
public static short plus(short x, short y) {
short res = (short) (x + y);
if (x < 0 && y < 0 && res >= 0)
ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
if (x > 0 && y > 0 && res < 0)
ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
return res;
}
public static short minus(short x, short y) {
short res = (short) (x - y);
if (0 <= x && y < 0 && res < 0) // 0 <= x is important!
ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
if (x < 0 && 0 < y && res > 0)
ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
return res;
}
public static short div(short x, short y) {
if (y == 0)
ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
if (y == -1 && x == -32768)
ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
return (short) (x / y);
}
public static short mod(short x, short y) {
if (y == 0)
ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
return (short) (x % y);
}
public static short mult(short x, short y) {
if (x == 0 || y == 0)
return 0;
if (x == 1)
return y;
if (y == 1)
return x;
if (x == -32768 || y == -32768)
ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
byte sgn = (x < 0 ? (y < 0 ? 1 : (byte) -1) : (y < 0 ? (byte) -1 : 1));
if (x < 0)
x = (short) -x;
if (y < 0)
y = (short) -y;
// check for overflows
short xh = (short) (x >> 8);
short yh = (short) (y >> 8);
if (xh != 0 && yh != 0)
ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
short xl = (short) (x & 0xFF);
short yl = (short) (y & 0xFF);
short r1 = (short) (xl * yl);
short r2 = (short) (xl * yh);
short r3 = (short) (xh * yl);
// x * y = 256 * (xh * yl + yh * xl) + xl * yl
// r4H r4L 0 0
// + 0 r3H r3L 0
// + 0 r2H r2L 0
// + 0 0 r1H r1L
short r = (short) (r2 + r3);
short res = (short) (r * 256 + r1);
if (sgn < 0 && r == 128 && r1 == 0)
return -32768;
if (r > 127 || res < 0)
ISOException.throwIt(ISO7816.SW_CONDITIONS_NOT_SATISFIED);
return (short) (sgn * res);
}
}