Interleave
namespace Org.BouncyCastle.Math.Raw
{
internal static class Interleave
{
private const ulong M32 = 1431655765;
private const ulong M64 = 6148914691236517205;
private const ulong M64R = 12297829382473034410;
internal static uint Expand8to16(byte x)
{
int num = (x | (x << 4)) & 3855;
int num2 = (num | (num << 2)) & 13107;
return (uint)((num2 | (num2 << 1)) & 21845);
}
internal static uint Expand16to32(ushort x)
{
int num = (x | (x << 8)) & 16711935;
int num2 = (num | (num << 4)) & 252645135;
int num3 = (num2 | (num2 << 2)) & 858993459;
return (uint)((num3 | (num3 << 1)) & 1431655765);
}
internal static ulong Expand32to64(uint x)
{
x = Bits.BitPermuteStep(x, 65280, 8);
x = Bits.BitPermuteStep(x, 15728880, 4);
x = Bits.BitPermuteStep(x, 202116108, 2);
x = Bits.BitPermuteStep(x, 572662306, 1);
return (ulong)((((long)(x >> 1) & 1431655765) << 32) | ((long)x & 1431655765));
}
internal static void Expand64To128(ulong x, ulong[] z, int zOff)
{
x = Bits.BitPermuteStep(x, 4294901760, 16);
x = Bits.BitPermuteStep(x, 280375465148160, 8);
x = Bits.BitPermuteStep(x, 67555025218437360, 4);
x = Bits.BitPermuteStep(x, 868082074056920076, 2);
x = Bits.BitPermuteStep(x, 2459565876494606882, 1);
z[zOff] = (x & 6148914691236517205);
z[zOff + 1] = ((x >> 1) & 6148914691236517205);
}
internal static void Expand64To128(ulong[] xs, int xsOff, int xsLen, ulong[] zs, int zsOff)
{
int num = xsLen;
int num2 = zsOff + (xsLen << 1);
while (--num >= 0) {
num2 -= 2;
Expand64To128(xs[xsOff + num], zs, num2);
}
}
internal static ulong Expand64To128Rev(ulong x, out ulong low)
{
x = Bits.BitPermuteStep(x, 4294901760, 16);
x = Bits.BitPermuteStep(x, 280375465148160, 8);
x = Bits.BitPermuteStep(x, 67555025218437360, 4);
x = Bits.BitPermuteStep(x, 868082074056920076, 2);
x = Bits.BitPermuteStep(x, 2459565876494606882, 1);
low = (ulong)((long)x & -6148914691236517206);
return (ulong)((long)(x << 1) & -6148914691236517206);
}
internal static uint Shuffle(uint x)
{
x = Bits.BitPermuteStep(x, 65280, 8);
x = Bits.BitPermuteStep(x, 15728880, 4);
x = Bits.BitPermuteStep(x, 202116108, 2);
x = Bits.BitPermuteStep(x, 572662306, 1);
return x;
}
internal static ulong Shuffle(ulong x)
{
x = Bits.BitPermuteStep(x, 4294901760, 16);
x = Bits.BitPermuteStep(x, 280375465148160, 8);
x = Bits.BitPermuteStep(x, 67555025218437360, 4);
x = Bits.BitPermuteStep(x, 868082074056920076, 2);
x = Bits.BitPermuteStep(x, 2459565876494606882, 1);
return x;
}
internal static uint Shuffle2(uint x)
{
x = Bits.BitPermuteStep(x, 61680, 12);
x = Bits.BitPermuteStep(x, 13369548, 6);
x = Bits.BitPermuteStep(x, 572662306, 1);
x = Bits.BitPermuteStep(x, 202116108, 2);
return x;
}
internal static ulong Shuffle2(ulong x)
{
x = Bits.BitPermuteStep(x, 4278255360, 24);
x = Bits.BitPermuteStep(x, 264913582878960, 12);
x = Bits.BitPermuteStep(x, 57421771435671756, 6);
x = Bits.BitPermuteStep(x, 723401728380766730, 3);
return x;
}
internal static uint Unshuffle(uint x)
{
x = Bits.BitPermuteStep(x, 572662306, 1);
x = Bits.BitPermuteStep(x, 202116108, 2);
x = Bits.BitPermuteStep(x, 15728880, 4);
x = Bits.BitPermuteStep(x, 65280, 8);
return x;
}
internal static ulong Unshuffle(ulong x)
{
x = Bits.BitPermuteStep(x, 2459565876494606882, 1);
x = Bits.BitPermuteStep(x, 868082074056920076, 2);
x = Bits.BitPermuteStep(x, 67555025218437360, 4);
x = Bits.BitPermuteStep(x, 280375465148160, 8);
x = Bits.BitPermuteStep(x, 4294901760, 16);
return x;
}
internal static ulong Unshuffle(ulong x, out ulong even)
{
ulong num = Unshuffle(x);
even = (num & uint.MaxValue);
return num >> 32;
}
internal static ulong Unshuffle(ulong x0, ulong x1, out ulong even)
{
ulong num = Unshuffle(x0);
ulong num2 = Unshuffle(x1);
even = ((num2 << 32) | (num & uint.MaxValue));
return (ulong)((long)(num >> 32) | ((long)num2 & -4294967296));
}
internal static uint Unshuffle2(uint x)
{
x = Bits.BitPermuteStep(x, 202116108, 2);
x = Bits.BitPermuteStep(x, 572662306, 1);
x = Bits.BitPermuteStep(x, 61680, 12);
x = Bits.BitPermuteStep(x, 13369548, 6);
return x;
}
internal static ulong Unshuffle2(ulong x)
{
x = Bits.BitPermuteStep(x, 57421771435671756, 6);
x = Bits.BitPermuteStep(x, 723401728380766730, 3);
x = Bits.BitPermuteStep(x, 4278255360, 24);
x = Bits.BitPermuteStep(x, 264913582878960, 12);
return x;
}
internal static ulong Transpose(ulong x)
{
x = Bits.BitPermuteStep(x, 4042322160, 28);
x = Bits.BitPermuteStep(x, 225176545447116, 14);
x = Bits.BitPermuteStep(x, 47851476196393130, 7);
return x;
}
}
}