diff --git a/crypto/src/math/raw/Bits.cs b/crypto/src/math/raw/Bits.cs
new file mode 100644
index 000000000..d344e1672
--- /dev/null
+++ b/crypto/src/math/raw/Bits.cs
@@ -0,0 +1,29 @@
+using System;
+
+namespace Org.BouncyCastle.Math.Raw
+{
+ internal abstract class Bits
+ {
+ internal static uint BitPermuteStep(uint x, uint m, int s)
+ {
+ uint t = (x ^ (x >> s)) & m;
+ return (t ^ (t << s)) ^ x;
+ }
+
+ internal static ulong BitPermuteStep(ulong x, ulong m, int s)
+ {
+ ulong t = (x ^ (x >> s)) & m;
+ return (t ^ (t << s)) ^ x;
+ }
+
+ internal static uint BitPermuteStepSimple(uint x, uint m, int s)
+ {
+ return ((x & m) << s) | ((x >> s) & m);
+ }
+
+ internal static ulong BitPermuteStepSimple(ulong x, ulong m, int s)
+ {
+ return ((x & m) << s) | ((x >> s) & m);
+ }
+ }
+}
diff --git a/crypto/src/math/raw/Interleave.cs b/crypto/src/math/raw/Interleave.cs
index 49d3768d7..8e98eac32 100644
--- a/crypto/src/math/raw/Interleave.cs
+++ b/crypto/src/math/raw/Interleave.cs
@@ -70,11 +70,10 @@ namespace Org.BouncyCastle.Math.Raw
internal static ulong Expand32to64(uint x)
{
// "shuffle" low half to even bits and high half to odd bits
- uint t;
- t = (x ^ (x >> 8)) & 0x0000FF00U; x ^= (t ^ (t << 8));
- t = (x ^ (x >> 4)) & 0x00F000F0U; x ^= (t ^ (t << 4));
- t = (x ^ (x >> 2)) & 0x0C0C0C0CU; x ^= (t ^ (t << 2));
- t = (x ^ (x >> 1)) & 0x22222222U; x ^= (t ^ (t << 1));
+ x = Bits.BitPermuteStep(x, 0x0000FF00U, 8);
+ x = Bits.BitPermuteStep(x, 0x00F000F0U, 4);
+ x = Bits.BitPermuteStep(x, 0x0C0C0C0CU, 2);
+ x = Bits.BitPermuteStep(x, 0x22222222U, 1);
return ((x >> 1) & M32) << 32 | (x & M32);
}
@@ -82,12 +81,11 @@ namespace Org.BouncyCastle.Math.Raw
internal static void Expand64To128(ulong x, ulong[] z, int zOff)
{
// "shuffle" low half to even bits and high half to odd bits
- ulong t;
- t = (x ^ (x >> 16)) & 0x00000000FFFF0000UL; x ^= (t ^ (t << 16));
- t = (x ^ (x >> 8)) & 0x0000FF000000FF00UL; x ^= (t ^ (t << 8));
- t = (x ^ (x >> 4)) & 0x00F000F000F000F0UL; x ^= (t ^ (t << 4));
- t = (x ^ (x >> 2)) & 0x0C0C0C0C0C0C0C0CUL; x ^= (t ^ (t << 2));
- t = (x ^ (x >> 1)) & 0x2222222222222222UL; x ^= (t ^ (t << 1));
+ x = Bits.BitPermuteStep(x, 0x00000000FFFF0000UL, 16);
+ x = Bits.BitPermuteStep(x, 0x0000FF000000FF00UL, 8);
+ x = Bits.BitPermuteStep(x, 0x00F000F000F000F0UL, 4);
+ x = Bits.BitPermuteStep(x, 0x0C0C0C0C0C0C0C0CUL, 2);
+ x = Bits.BitPermuteStep(x, 0x2222222222222222UL, 1);
z[zOff ] = (x ) & M64;
z[zOff + 1] = (x >> 1) & M64;
@@ -97,28 +95,19 @@ namespace Org.BouncyCastle.Math.Raw
{
for (int i = 0; i < xsLen; ++i)
{
- // "shuffle" low half to even bits and high half to odd bits
- ulong x = xs[xsOff + i], t;
- t = (x ^ (x >> 16)) & 0x00000000FFFF0000UL; x ^= (t ^ (t << 16));
- t = (x ^ (x >> 8)) & 0x0000FF000000FF00UL; x ^= (t ^ (t << 8));
- t = (x ^ (x >> 4)) & 0x00F000F000F000F0UL; x ^= (t ^ (t << 4));
- t = (x ^ (x >> 2)) & 0x0C0C0C0C0C0C0C0CUL; x ^= (t ^ (t << 2));
- t = (x ^ (x >> 1)) & 0x2222222222222222UL; x ^= (t ^ (t << 1));
-
- zs[zsOff++] = (x ) & M64;
- zs[zsOff++] = (x >> 1) & M64;
+ Expand64To128(xs[xsOff + i], zs, zsOff);
+ zsOff += 2;
}
}
internal static void Expand64To128Rev(ulong x, ulong[] z, int zOff)
{
// "shuffle" low half to even bits and high half to odd bits
- ulong t;
- t = (x ^ (x >> 16)) & 0x00000000FFFF0000UL; x ^= (t ^ (t << 16));
- t = (x ^ (x >> 8)) & 0x0000FF000000FF00UL; x ^= (t ^ (t << 8));
- t = (x ^ (x >> 4)) & 0x00F000F000F000F0UL; x ^= (t ^ (t << 4));
- t = (x ^ (x >> 2)) & 0x0C0C0C0C0C0C0C0CUL; x ^= (t ^ (t << 2));
- t = (x ^ (x >> 1)) & 0x2222222222222222UL; x ^= (t ^ (t << 1));
+ x = Bits.BitPermuteStep(x, 0x00000000FFFF0000UL, 16);
+ x = Bits.BitPermuteStep(x, 0x0000FF000000FF00UL, 8);
+ x = Bits.BitPermuteStep(x, 0x00F000F000F000F0UL, 4);
+ x = Bits.BitPermuteStep(x, 0x0C0C0C0C0C0C0C0CUL, 2);
+ x = Bits.BitPermuteStep(x, 0x2222222222222222UL, 1);
z[zOff] = (x ) & M64R;
z[zOff + 1] = (x << 1) & M64R;
@@ -127,68 +116,62 @@ namespace Org.BouncyCastle.Math.Raw
internal static uint Shuffle(uint x)
{
// "shuffle" low half to even bits and high half to odd bits
- uint t;
- t = (x ^ (x >> 8)) & 0x0000FF00U; x ^= (t ^ (t << 8));
- t = (x ^ (x >> 4)) & 0x00F000F0U; x ^= (t ^ (t << 4));
- t = (x ^ (x >> 2)) & 0x0C0C0C0CU; x ^= (t ^ (t << 2));
- t = (x ^ (x >> 1)) & 0x22222222U; x ^= (t ^ (t << 1));
+ x = Bits.BitPermuteStep(x, 0x0000FF00U, 8);
+ x = Bits.BitPermuteStep(x, 0x00F000F0U, 4);
+ x = Bits.BitPermuteStep(x, 0x0C0C0C0CU, 2);
+ x = Bits.BitPermuteStep(x, 0x22222222U, 1);
return x;
}
internal static ulong Shuffle(ulong x)
{
// "shuffle" low half to even bits and high half to odd bits
- ulong t;
- t = (x ^ (x >> 16)) & 0x00000000FFFF0000UL; x ^= (t ^ (t << 16));
- t = (x ^ (x >> 8)) & 0x0000FF000000FF00UL; x ^= (t ^ (t << 8));
- t = (x ^ (x >> 4)) & 0x00F000F000F000F0UL; x ^= (t ^ (t << 4));
- t = (x ^ (x >> 2)) & 0x0C0C0C0C0C0C0C0CUL; x ^= (t ^ (t << 2));
- t = (x ^ (x >> 1)) & 0x2222222222222222UL; x ^= (t ^ (t << 1));
+ x = Bits.BitPermuteStep(x, 0x00000000FFFF0000UL, 16);
+ x = Bits.BitPermuteStep(x, 0x0000FF000000FF00UL, 8);
+ x = Bits.BitPermuteStep(x, 0x00F000F000F000F0UL, 4);
+ x = Bits.BitPermuteStep(x, 0x0C0C0C0C0C0C0C0CUL, 2);
+ x = Bits.BitPermuteStep(x, 0x2222222222222222UL, 1);
return x;
}
internal static uint Shuffle2(uint x)
{
// "shuffle" (twice) low half to even bits and high half to odd bits
- uint t;
- t = (x ^ (x >> 7)) & 0x00AA00AAU; x ^= (t ^ (t << 7));
- t = (x ^ (x >> 14)) & 0x0000CCCCU; x ^= (t ^ (t << 14));
- t = (x ^ (x >> 4)) & 0x00F000F0U; x ^= (t ^ (t << 4));
- t = (x ^ (x >> 8)) & 0x0000FF00U; x ^= (t ^ (t << 8));
+ x = Bits.BitPermuteStep(x, 0x00AA00AAU, 7);
+ x = Bits.BitPermuteStep(x, 0x0000CCCCU, 14);
+ x = Bits.BitPermuteStep(x, 0x00F000F0U, 4);
+ x = Bits.BitPermuteStep(x, 0x0000FF00U, 8);
return x;
}
internal static uint Unshuffle(uint x)
{
// "unshuffle" even bits to low half and odd bits to high half
- uint t;
- t = (x ^ (x >> 1)) & 0x22222222U; x ^= (t ^ (t << 1));
- t = (x ^ (x >> 2)) & 0x0C0C0C0CU; x ^= (t ^ (t << 2));
- t = (x ^ (x >> 4)) & 0x00F000F0U; x ^= (t ^ (t << 4));
- t = (x ^ (x >> 8)) & 0x0000FF00U; x ^= (t ^ (t << 8));
+ x = Bits.BitPermuteStep(x, 0x22222222U, 1);
+ x = Bits.BitPermuteStep(x, 0x0C0C0C0CU, 2);
+ x = Bits.BitPermuteStep(x, 0x00F000F0U, 4);
+ x = Bits.BitPermuteStep(x, 0x0000FF00U, 8);
return x;
}
internal static ulong Unshuffle(ulong x)
{
// "unshuffle" even bits to low half and odd bits to high half
- ulong t;
- t = (x ^ (x >> 1)) & 0x2222222222222222UL; x ^= (t ^ (t << 1));
- t = (x ^ (x >> 2)) & 0x0C0C0C0C0C0C0C0CUL; x ^= (t ^ (t << 2));
- t = (x ^ (x >> 4)) & 0x00F000F000F000F0UL; x ^= (t ^ (t << 4));
- t = (x ^ (x >> 8)) & 0x0000FF000000FF00UL; x ^= (t ^ (t << 8));
- t = (x ^ (x >> 16)) & 0x00000000FFFF0000UL; x ^= (t ^ (t << 16));
+ x = Bits.BitPermuteStep(x, 0x2222222222222222UL, 1);
+ x = Bits.BitPermuteStep(x, 0x0C0C0C0C0C0C0C0CUL, 2);
+ x = Bits.BitPermuteStep(x, 0x00F000F000F000F0UL, 4);
+ x = Bits.BitPermuteStep(x, 0x0000FF000000FF00UL, 8);
+ x = Bits.BitPermuteStep(x, 0x00000000FFFF0000UL, 16);
return x;
}
internal static uint Unshuffle2(uint x)
{
// "unshuffle" (twice) even bits to low half and odd bits to high half
- uint t;
- t = (x ^ (x >> 8)) & 0x0000FF00U; x ^= (t ^ (t << 8));
- t = (x ^ (x >> 4)) & 0x00F000F0U; x ^= (t ^ (t << 4));
- t = (x ^ (x >> 14)) & 0x0000CCCCU; x ^= (t ^ (t << 14));
- t = (x ^ (x >> 7)) & 0x00AA00AAU; x ^= (t ^ (t << 7));
+ x = Bits.BitPermuteStep(x, 0x0000FF00U, 8);
+ x = Bits.BitPermuteStep(x, 0x00F000F0U, 4);
+ x = Bits.BitPermuteStep(x, 0x0000CCCCU, 14);
+ x = Bits.BitPermuteStep(x, 0x00AA00AAU, 7);
return x;
}
}
|