在 JavaCard 中的 64 位字字节数组上向左旋转 [英] Rotate left on a 64-bit word byte array in JavaCard

查看:25
本文介绍了在 JavaCard 中的 64 位字字节数组上向左旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对当前在 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.

丑陋的方法是在表示为 8 字节数组的 64 位字上硬​​编码 ROTL 的所有 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.

如何使它更精简,以便我可以使用 byte 对 64 位字(在字节数组中)按需即时执行任意数量的 ROTL 操作和 short 类型(因为 JavaCard 不能识别更复杂的东西,比如 intlong 等等),而不需要对所有 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.

推荐答案

一个非常简单的实现,在单独的参数中接收四个 shorts

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆