summary refs log tree commit diff
path: root/crypto/src/math/raw/Interleave.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2020-10-18 23:42:54 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2020-10-18 23:42:54 +0700
commit08b8d058c924763cb68f835655d4b39ac300f379 (patch)
treea4925fd9367f42a0c65f5b14c619f7b179cd53de /crypto/src/math/raw/Interleave.cs
parentLatest XDH, EdDSA updates from bc-java (diff)
downloadBouncyCastle.NET-ed25519-08b8d058c924763cb68f835655d4b39ac300f379.tar.xz
Add Bits and Longs classes from bc-java
Diffstat (limited to 'crypto/src/math/raw/Interleave.cs')
-rw-r--r--crypto/src/math/raw/Interleave.cs101
1 files changed, 42 insertions, 59 deletions
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;
         }
     }