summary refs log tree commit diff
path: root/crypto
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2022-10-09 19:07:44 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2022-10-09 19:07:44 +0700
commitc9a0b9b02d8980adc3f9a547bd14898c6b101285 (patch)
treef3c3640e506208afd980234c54dba05a852a55c9 /crypto
parentImprovements to PRNG classes (diff)
downloadBouncyCastle.NET-ed25519-c9a0b9b02d8980adc3f9a547bd14898c6b101285.tar.xz
Round out Bits methods
- AggressiveInlining
- Debug guards
Diffstat (limited to 'crypto')
-rw-r--r--crypto/src/math/raw/Bits.cs65
1 files changed, 61 insertions, 4 deletions
diff --git a/crypto/src/math/raw/Bits.cs b/crypto/src/math/raw/Bits.cs
index d344e1672..423151651 100644
--- a/crypto/src/math/raw/Bits.cs
+++ b/crypto/src/math/raw/Bits.cs
@@ -1,28 +1,85 @@
-using System;
+using System.Diagnostics;
+#if NETSTANDARD1_0_OR_GREATER || NETCOREAPP1_0_OR_GREATER
+using System.Runtime.CompilerServices;
+#endif
 
 namespace Org.BouncyCastle.Math.Raw
 {
-    internal abstract class Bits
+    internal static class Bits
     {
+#if NETSTANDARD1_0_OR_GREATER || NETCOREAPP1_0_OR_GREATER
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+#endif
         internal static uint BitPermuteStep(uint x, uint m, int s)
         {
+            Debug.Assert((m & (m << s)) == 0U);
+            Debug.Assert((m << s) >> s == m);
+
             uint t = (x ^ (x >> s)) & m;
-            return (t ^ (t << s)) ^ x;
+            return t ^ (t << s) ^ x;
         }
 
+#if NETSTANDARD1_0_OR_GREATER || NETCOREAPP1_0_OR_GREATER
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+#endif
         internal static ulong BitPermuteStep(ulong x, ulong m, int s)
         {
+            Debug.Assert((m & (m << s)) == 0UL);
+            Debug.Assert((m << s) >> s == m);
+
             ulong t = (x ^ (x >> s)) & m;
-            return (t ^ (t << s)) ^ x;
+            return t ^ (t << s) ^ x;
+        }
+
+#if NETSTANDARD1_0_OR_GREATER || NETCOREAPP1_0_OR_GREATER
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+#endif
+        internal static void BitPermuteStep2(ref uint hi, ref uint lo, uint m, int s)
+        {
+#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP1_1_OR_GREATER
+            Debug.Assert(!Unsafe.AreSame(ref hi, ref lo) || (m & (m << s)) == 0U);
+#endif
+            Debug.Assert((m << s) >> s == m);
+
+            uint t = ((lo >> s) ^ hi) & m;
+            lo ^= t << s;
+            hi ^= t;
         }
 
+#if NETSTANDARD1_0_OR_GREATER || NETCOREAPP1_0_OR_GREATER
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+#endif
+        internal static void BitPermuteStep2(ref ulong hi, ref ulong lo, ulong m, int s)
+        {
+#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP1_1_OR_GREATER
+            Debug.Assert(!Unsafe.AreSame(ref hi, ref lo) || (m & (m << s)) == 0UL);
+#endif
+            Debug.Assert((m << s) >> s == m);
+
+            ulong t = ((lo >> s) ^ hi) & m;
+            lo ^= t << s;
+            hi ^= t;
+        }
+
+#if NETSTANDARD1_0_OR_GREATER || NETCOREAPP1_0_OR_GREATER
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+#endif
         internal static uint BitPermuteStepSimple(uint x, uint m, int s)
         {
+            Debug.Assert((m & (m << s)) == 0U);
+            Debug.Assert((m << s) >> s == m);
+
             return ((x & m) << s) | ((x >> s) & m);
         }
 
+#if NETSTANDARD1_0_OR_GREATER || NETCOREAPP1_0_OR_GREATER
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
+#endif
         internal static ulong BitPermuteStepSimple(ulong x, ulong m, int s)
         {
+            Debug.Assert((m & (m << s)) == 0UL);
+            Debug.Assert((m << s) >> s == m);
+
             return ((x & m) << s) | ((x >> s) & m);
         }
     }