summary refs log tree commit diff
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
parentLatest XDH, EdDSA updates from bc-java (diff)
downloadBouncyCastle.NET-ed25519-08b8d058c924763cb68f835655d4b39ac300f379.tar.xz
Add Bits and Longs classes from bc-java
-rw-r--r--crypto/BouncyCastle.Android.csproj2
-rw-r--r--crypto/BouncyCastle.csproj4
-rw-r--r--crypto/BouncyCastle.iOS.csproj2
-rw-r--r--crypto/crypto.csproj10
-rw-r--r--crypto/src/math/raw/Bits.cs29
-rw-r--r--crypto/src/math/raw/Interleave.cs101
-rw-r--r--crypto/src/util/Integers.cs16
-rw-r--r--crypto/src/util/Longs.cs65
8 files changed, 169 insertions, 60 deletions
diff --git a/crypto/BouncyCastle.Android.csproj b/crypto/BouncyCastle.Android.csproj
index a27c5e8ae..fe25eec6b 100644
--- a/crypto/BouncyCastle.Android.csproj
+++ b/crypto/BouncyCastle.Android.csproj
@@ -1395,6 +1395,7 @@
     <Compile Include="src\math\field\IPolynomial.cs" />
     <Compile Include="src\math\field\IPolynomialExtensionField.cs" />
     <Compile Include="src\math\field\PrimeField.cs" />
+    <Compile Include="src\math\raw\Bits.cs" />
     <Compile Include="src\math\raw\Interleave.cs" />
     <Compile Include="src\math\raw\Mod.cs" />
     <Compile Include="src\math\raw\Nat.cs" />
@@ -1557,6 +1558,7 @@
     <Compile Include="src\util\Enums.cs" />
     <Compile Include="src\util\IMemoable.cs" />
     <Compile Include="src\util\Integers.cs" />
+    <Compile Include="src\util\Longs.cs" />
     <Compile Include="src\util\MemoableResetException.cs" />
     <Compile Include="src\util\Platform.cs" />
     <Compile Include="src\util\Strings.cs" />
diff --git a/crypto/BouncyCastle.csproj b/crypto/BouncyCastle.csproj
index 3285296bc..315f6cd46 100644
--- a/crypto/BouncyCastle.csproj
+++ b/crypto/BouncyCastle.csproj
@@ -1389,6 +1389,7 @@
     <Compile Include="src\math\field\IPolynomial.cs" />
     <Compile Include="src\math\field\IPolynomialExtensionField.cs" />
     <Compile Include="src\math\field\PrimeField.cs" />
+    <Compile Include="src\math\raw\Bits.cs" />
     <Compile Include="src\math\raw\Interleave.cs" />
     <Compile Include="src\math\raw\Mod.cs" />
     <Compile Include="src\math\raw\Nat.cs" />
@@ -1551,6 +1552,7 @@
     <Compile Include="src\util\Enums.cs" />
     <Compile Include="src\util\IMemoable.cs" />
     <Compile Include="src\util\Integers.cs" />
+    <Compile Include="src\util\Longs.cs" />
     <Compile Include="src\util\MemoableResetException.cs" />
     <Compile Include="src\util\Platform.cs" />
     <Compile Include="src\util\Strings.cs" />
@@ -1662,4 +1664,4 @@
     <None Include="checklist.txt" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
-</Project>
+</Project>
\ No newline at end of file
diff --git a/crypto/BouncyCastle.iOS.csproj b/crypto/BouncyCastle.iOS.csproj
index 4be7052f9..b8d43cb27 100644
--- a/crypto/BouncyCastle.iOS.csproj
+++ b/crypto/BouncyCastle.iOS.csproj
@@ -1390,6 +1390,7 @@
     <Compile Include="src\math\field\IPolynomial.cs" />
     <Compile Include="src\math\field\IPolynomialExtensionField.cs" />
     <Compile Include="src\math\field\PrimeField.cs" />
+    <Compile Include="src\math\raw\Bits.cs" />
     <Compile Include="src\math\raw\Interleave.cs" />
     <Compile Include="src\math\raw\Mod.cs" />
     <Compile Include="src\math\raw\Nat.cs" />
@@ -1552,6 +1553,7 @@
     <Compile Include="src\util\Enums.cs" />
     <Compile Include="src\util\IMemoable.cs" />
     <Compile Include="src\util\Integers.cs" />
+    <Compile Include="src\util\Longs.cs" />
     <Compile Include="src\util\MemoableResetException.cs" />
     <Compile Include="src\util\Platform.cs" />
     <Compile Include="src\util\Strings.cs" />
diff --git a/crypto/crypto.csproj b/crypto/crypto.csproj
index a940fbd0b..797c6ae9e 100644
--- a/crypto/crypto.csproj
+++ b/crypto/crypto.csproj
@@ -6834,6 +6834,11 @@
                     BuildAction = "Compile"
                 />
                 <File
+                    RelPath = "src\math\raw\Bits.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
+                <File
                     RelPath = "src\math\raw\Interleave.cs"
                     SubType = "Code"
                     BuildAction = "Compile"
@@ -7644,6 +7649,11 @@
                     BuildAction = "Compile"
                 />
                 <File
+                    RelPath = "src\util\Longs.cs"
+                    SubType = "Code"
+                    BuildAction = "Compile"
+                />
+                <File
                     RelPath = "src\util\MemoableResetException.cs"
                     SubType = "Code"
                     BuildAction = "Compile"
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;
         }
     }
diff --git a/crypto/src/util/Integers.cs b/crypto/src/util/Integers.cs
index 7d98de586..b243b88b2 100644
--- a/crypto/src/util/Integers.cs
+++ b/crypto/src/util/Integers.cs
@@ -1,5 +1,7 @@
 using System;
 
+using Org.BouncyCastle.Math.Raw;
+
 namespace Org.BouncyCastle.Utilities
 {
     public abstract class Integers
@@ -29,6 +31,20 @@ namespace Org.BouncyCastle.Utilities
             return DeBruijnTZ[(uint)((i & -i) * 0x04D7651F) >> 27];
         }
 
+        public static int Reverse(int i)
+        {
+            i = (int)Bits.BitPermuteStepSimple((uint)i, 0x55555555U, 1);
+            i = (int)Bits.BitPermuteStepSimple((uint)i, 0x33333333U, 2);
+            i = (int)Bits.BitPermuteStepSimple((uint)i, 0x0F0F0F0FU, 4);
+            return ReverseBytes(i);
+        }
+
+        public static int ReverseBytes(int i)
+        {
+            return RotateLeft((int)((uint)i & 0xFF00FF00U),  8) |
+                   RotateLeft((int)((uint)i & 0x00FF00FFU), 24);
+        }
+
         public static int RotateLeft(int i, int distance)
         {
             return (i << distance) ^ (int)((uint)i >> -distance);
diff --git a/crypto/src/util/Longs.cs b/crypto/src/util/Longs.cs
new file mode 100644
index 000000000..d206c1f81
--- /dev/null
+++ b/crypto/src/util/Longs.cs
@@ -0,0 +1,65 @@
+using System;
+
+using Org.BouncyCastle.Math.Raw;
+
+namespace Org.BouncyCastle.Utilities
+{
+    public abstract class Longs
+    {
+        public static long Reverse(long i)
+        {
+            i = (long)Bits.BitPermuteStepSimple((ulong)i, 0x5555555555555555UL, 1);
+            i = (long)Bits.BitPermuteStepSimple((ulong)i, 0x3333333333333333UL, 2);
+            i = (long)Bits.BitPermuteStepSimple((ulong)i, 0x0F0F0F0F0F0F0F0FUL, 4);
+            return ReverseBytes(i);
+        }
+
+        [CLSCompliantAttribute(false)]
+        public static ulong Reverse(ulong i)
+        {
+            i = Bits.BitPermuteStepSimple(i, 0x5555555555555555UL, 1);
+            i = Bits.BitPermuteStepSimple(i, 0x3333333333333333UL, 2);
+            i = Bits.BitPermuteStepSimple(i, 0x0F0F0F0F0F0F0F0FUL, 4);
+            return ReverseBytes(i);
+        }
+
+        public static long ReverseBytes(long i)
+        {
+            return RotateLeft((long)((ulong)i & 0xFF000000FF000000UL),  8) |
+                   RotateLeft((long)((ulong)i & 0x00FF000000FF0000UL), 24) |
+                   RotateLeft((long)((ulong)i & 0x0000FF000000FF00UL), 40) |
+                   RotateLeft((long)((ulong)i & 0x000000FF000000FFUL), 56);
+        }
+
+        [CLSCompliantAttribute(false)]
+        public static ulong ReverseBytes(ulong i)
+        {
+            return RotateLeft(i & 0xFF000000FF000000UL,  8) |
+                   RotateLeft(i & 0x00FF000000FF0000UL, 24) |
+                   RotateLeft(i & 0x0000FF000000FF00UL, 40) |
+                   RotateLeft(i & 0x000000FF000000FFUL, 56);
+        }
+
+        public static long RotateLeft(long i, int distance)
+        {
+            return (i << distance) ^ (long)((ulong)i >> -distance);
+        }
+
+        [CLSCompliantAttribute(false)]
+        public static ulong RotateLeft(ulong i, int distance)
+        {
+            return (i << distance) ^ (i >> -distance);
+        }
+
+        public static long RotateRight(long i, int distance)
+        {
+            return (long)((ulong)i >> distance) ^ (i << -distance);
+        }
+
+        [CLSCompliantAttribute(false)]
+        public static ulong RotateRight(ulong i, int distance)
+        {
+            return (i >> distance) ^ (i << -distance);
+        }
+    }
+}