向左旋转JavaCard中的64位字字节数组 [英] Rotate left on a 64-bit word byte array in JavaCard
问题描述
我正在尝试对当前表示为JavaCard智能卡中8个字节的字节数组的64位字进行任意旋转次数(ROTL)。
I am trying to perform a rotate left (ROTL) operation on an arbitrary amount of rotations on a 64-bit word currently represented as a byte array of 8 bytes in a JavaCard smart card.
丑陋的方法是将ROTL的所有64种可能排列硬编码为一个8字节数组表示的64位字,但这只会使整个代码库膨胀。
The ugly way around is to hard-code all 64 possible permutations of ROTL on a 64-bit word represented as an 8 byte array but that will simply bloat the entire codebase up.
如何使其更精简,以便可以使用<$即时对64位字(字节数组)按需进行任意数量的ROTL操作仅c $ c> byte 和 short
类型(由于JavaCard无法识别 int $ c之类的更复杂的东西$ c>或
long
等),而无需对所有ROTL64排列进行硬编码。
How do I make it leaner so that I can on-the-fly do any arbitrary amount of ROTL operations on demand on a 64-bit word (in byte array) with the use of byte
and short
types only (due to JavaCard not recognizing more complex things like int
or long
and so on) without need for hard-coding all ROTL64 permutations.
推荐答案
一个非常简单的实现,它在单独的参数中接收四个短裤
A very simple implementation which receives the four shorts in separate parameters
public static void rotateRight64(short x3, short x2, short x1, short x0,
short rotAmount, short[] out)
{
assert out.length() == 4;
rotAmount &= (1 << 6) - 1; // limit the range to 0..63
if (rotAmount >= 16)
rotateRight64(x0, x3, x2, x1, rotAmount - 16, out);
else
{
out[0] = (short)((x0 >>> rotAmount) | (x1 << (16-rotAmount)));
out[1] = (short)((x1 >>> rotAmount) | (x2 << (16-rotAmount)));
out[2] = (short)((x2 >>> rotAmount) | (x3 << (16-rotAmount)));
out[3] = (short)((x3 >>> rotAmount) | (x0 << (16-rotAmount)));
}
}
这是右旋转,但旋转很容易向左旋转 64-rotAmount
It's a rotate right but it's easy to do a rotate left by rotating right 64 - rotAmount
或者,可以像这样完成,而无需进行粗调>
Alternatively it can be done like this without the coarse shifting step
public static void rotateRight(short[] out, short[] in, short rotAmount) // in ror rotAmount
{
assert out.length() == 4 && in.length() == 4 && rotAmount >= 0 && rotAmount < 64;
const short shift = (short)(rotAmount % 16);
const short limbshift = (short)(rotAmount / 16);
short tmp = in[0];
for (short i = 0; i < 4; ++i)
{
short index = (short)((i + limbshift) % 4);
out[i] = (short)((in[index] >>> shift) | (in[index + 1] << (16 - shift)));
}
}
这种方式可以轻松地将其更改为任意-精确移位/旋转
This way it can be easily changed to an arbitrary-precision shift/rotate
如果输入数组为 byte
,则可以更改 short [ 4]
到 byte [8]
并将所有常量从16→8和4→8更改。实际上,它们可以被普遍化而没有问题。 ,我只是很难进行编码以使其简单易懂
If the input array is byte
then you can change short[4]
to byte[8]
and change all the constants from 16 → 8 and from 4 → 8. In fact they can be generalized without problem, I'm just hard coding to keep it simple and easier to understand
这篇关于向左旋转JavaCard中的64位字字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!