summary refs log tree commit diff
path: root/crypto/src/util
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/util
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/util')
-rw-r--r--crypto/src/util/Integers.cs16
-rw-r--r--crypto/src/util/Longs.cs65
2 files changed, 81 insertions, 0 deletions
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);
+        }
+    }
+}