下面的实现逐字节和逐位地遍历 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 的编码。 Jones 提出的加法速度很快,但没有说明如何处理 32 位字之间的进位:
请注意,如果该位置应该有进位,则总和的最高有效位将超过 9。此外,没有简单的方法来检测这种携带!
这对于 Java Card 来说当然是必需的,因为它只有 16 位有符号短整数基类型。因此,琼斯提出的方法不能直接适用;任何利用 Jones 方法的答案都应该指出如何处理 Java Card 中使用的字节或短字节之间的进位。