使二进制加法的行为类似于(压缩)十进制加法

时间:2018-01-25 18:32:58

标签: java javacard bcd

我目前正致力于限制性环境,其中唯一允许的类型是:

byte, byte[], short, short[].

我几乎可以肯定我无法导入外部库,因为我正在使用JavaCard,并且已经尝试过这样的事情,但结果并不好。

所以,在这里我必须管理一个大小为6字节的字节数组,它表示卡的余额(以欧元表示),最后一个字节是美分,但现在这并不重要。

鉴于我无法访问整数,我不知道如何以我想要的方式添加两个字节。

让我们举个例子:

用户输入(添加)0x00 0x00 0x00 0x00 0x00 0x57,这对用户来说意味着增加57美分。现在让我们说余额是0x00 ... 0x26

我希望能够创建一个可以修改平衡数组(带有进位)的方法,其方式是在添加之后,分数为83,并表示为0x83。 我也必须处理减法,但我想我之后可以自己解决这个问题。

我的第一个猜测是屏蔽每个字节的每个数字,并且首先单独工作,但这让我无处可去。

我显然不是要求一个完整的解决方案,因为我相信我的问题几乎是不可能的,但如果您对如何处理这个问题有任何想法,我将非常感激。

那么如何在Java Card上相互添加两个包含二进制编码小数的数组?

编辑1:常见数组如下所示:

{ 0x00 , 0x00 , 0x01, 0x52, 0x45, 0x52}

并且代表15 254€和52美分的大端BCD编码整数。

编辑2:嗯,正如我所怀疑的,我的卡不支持包framework.math,所以我不能使用BCDUtil或BigNumbers,这将是有用的。

2 个答案:

答案 0 :(得分:2)

以下实现逐字节逐位地进行BCD。这允许它使用在大多数智能卡处理器上有效的8位寄存器。它明确允许正确处理进位并在溢出的情况下返回进位。

/**
 * Adds two values to each other and stores it in the location of the first value.
 * The values are represented by big endian, packed BCD encoding with a static size.
 * No validation is performed if the arrays do indeed contain packed BCD;
 * the result of the calculation is indeterminate if the arrays contain anything other than packed BCD.
 * This calculation should be constant time;
 * it should only leak information about the values if one of the basic byte calculations leaks timing information.
 *
 * @param x the first buffer containing the packed BCD
 * @param xOff the offset in the first buffer of the packed BCD
 * @param y the second buffer containing the packed BCD
 * @param yOff the offset in the second buffer of the packed BCD
 * @param packedBytes the number of bytes that contain two BCD digits in both buffers
 * @return zero or one depending if the full calculation generates a carry, i.e. overflows
 * @throws ArrayIndexOutOfBoundsException if a packed BCD value is out of bounds
 */
public static byte addPackedBCD(byte[] x, short xOff, byte[] y, short yOff, short packedBytes) {
    // declare temporary variables, we'll handle bytes only
    byte xd, yd, zd, z;
    // set the initial carry to zero, c will only be 0 or 1
    byte c = 0;
    // go through the bytes backwards (least significant bytes first)
    // as we need to take the carry into account
    for (short i = (short) (packedBytes - 1); i >= 0; i--) {
        // retrieve the two least significant digits the current byte in the arrays
        xd = (byte) (x[xOff + i] & 0b00001111);
        yd = (byte) (y[yOff + i] & 0b00001111);
        // zd is the addition of the lower two BCD digits plus the carry
        zd = (byte) (xd + yd + c);
        // c is set to 1 if the final number is larger than 10, otherwise c is set to zero
        // i.e. the value is at least 16 or the value is at least 8 + 4 or 8 + 2
        c = (byte) (((zd & 0b10000) >> 4)
                | (((zd & 0b01000) >> 3)
                        & (((zd & 0b00100) >> 2) | ((zd & 0b00010) >> 1))));
        // subtract 10 if there is a carry and then assign the value to z
        z = (byte) (zd - c * 10);

        // retrieve the two most significant digits the current byte in the arrays
        xd = (byte) ((x[xOff + i] >>> 4) & 0b00001111);
        yd = (byte) ((y[yOff + i] >>> 4) & 0b00001111);
        // zd is the addition of the higher two BCD digits plus the carry
        zd = (byte) (xd + yd + c);
        // c is set to 1 if the final number is larger than 10, otherwise c is set to zero
        // i.e. the value is at least 16 or the value is at least 8 + 4 or 8 + 2
        c = (byte) (((zd & 0b10000) >> 4)
                | (((zd & 0b01000) >> 3)
                        & (((zd & 0b00100) >> 2) | ((zd & 0b00010) >> 1))));
        // subtract 10 if there is a carry and then assign the value to the 4 msb digits of z
        z |= (zd - c * 10) << 4;

        // assign z to the first byte array
        x[xOff + i] = z;
    }

    // finally, return the last carry
    return c;
}

请注意,我只测试了两个包含单字节/两个BCD数字的数组。但是,进位工作和所有65536种组合都已经过测试,方法必须有效。

最重要的是,您可能希望在执行任何操作之前测试打包BCD编码的正确性。可以将相同的方法集成到添加的for循环中,以获得更高的效率。对所有单字节值进行测试,如上一代码块所示。

/**
 * Checks if the buffer contains a valid packed BCD representation.
 * The values are represented by packed BCD encoding with a static size.
 * This calculation should be constant time;
 * it should only leak information about the values if one of the basic byte calculations leaks timing information.
 *
 * @param x the buffer containing the packed BCD
 * @param xOff the offset in the buffer of the packed BCD
 * @param packedBytes the number of bytes that packed BCD in the buffer
 * @return true if and only if the value is valid, packed BCD
 * @throws ArrayIndexOutOfBoundsException if the packed BCD value is out of bounds
 */
public static boolean validPackedBCD(byte[] x, short xOff, short packedBytes) {
    // declare temporary variable, we'll handle bytes only
    byte xdd;
    // c is the correctness of the digits; it will be off-zero if invalid encoding is encountered
    byte c = 0;

    short end = (short) (xOff + packedBytes);
    // go through the bytes, reusing xOff for efficiency
    for (; xOff < end; xOff++) {
        xdd = x[xOff];
        // c will be set to non-zero if the high bit of each encoded decimal is set ...
        // and either one of the two decimals is set as that would indicate a value of 10 or higher
        // i.e. only values 8 + 4 or 8 + 2 are 10 or higher if you look at the bits in the digits
        c |= ((xdd & 0b1000_1000) >> 2) & (((xdd & 0b0100_0100) >> 1) | (xdd & 0b0010_0010));
    }

    // finally, return the result - c is zero in case all bytes encode two packed BCD values
    return c == 0;
}

请注意,这个也在Java Card的BCDUtil中实现。然而,我确实不喜欢课堂设计,我不认为这是有充分记录的,所以我决定采用不同的方法。它也在javacardx中,这意味着如果没有实现它理论上会抛出异常。

answer of EJP isn't applicable,除了表示使用的编码是打包BCD的编码。琼斯提出的补充很快,但它并没有显示如何处理32位字之间的进位:

  

请注意,如果应该执行该位置,则总和的最高位数将超过9。此外,没有简单的方法来检测这个携带!

这当然是Java Card所必需的,因为它只有16位有符号短路基本类型整数。因此,琼斯提出的方法不能直接适用;任何利用Jones 方法的答案都应该表明如何处理Java Card中使用的字节或短路之间的进位。

答案 1 :(得分:-2)

这不是十六进制,它是压缩十进制,是BCD的一种形式。

您可以使用内部进位一次一个字节进行压缩十进制加法和减法运算。如果需要的话,有一个增加6来强制进位进入MS数字的技巧,然后如果它携带则再次屏蔽并将其移出,以校正LS数字。这里的解释太广泛了。

参见Jones on BCD arithmetic,其中显示了如何在32位字上有效地使用位操作数来实现压缩十进制算法。