summary refs log tree commit diff
path: root/crypto/src/math
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/math')
-rw-r--r--crypto/src/math/ec/custom/sec/SecT131Field.cs2
-rw-r--r--crypto/src/math/raw/Interleave.cs40
2 files changed, 28 insertions, 14 deletions
diff --git a/crypto/src/math/ec/custom/sec/SecT131Field.cs b/crypto/src/math/ec/custom/sec/SecT131Field.cs
index 6088b264c..f2c878d6a 100644
--- a/crypto/src/math/ec/custom/sec/SecT131Field.cs
+++ b/crypto/src/math/ec/custom/sec/SecT131Field.cs
@@ -370,7 +370,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec
         protected static void ImplSquare(ulong[] x, ulong[] zz)
         {
             Interleave.Expand64To128(x, 0, 2, zz, 0);
-            zz[4] = Interleave.Expand8to16((uint)x[2]);
+            zz[4] = Interleave.Expand8to16((byte)x[2]);
         }
     }
 }
diff --git a/crypto/src/math/raw/Interleave.cs b/crypto/src/math/raw/Interleave.cs
index 02aa79551..3e994a43c 100644
--- a/crypto/src/math/raw/Interleave.cs
+++ b/crypto/src/math/raw/Interleave.cs
@@ -12,23 +12,37 @@ namespace Org.BouncyCastle.Math.Raw
         private const ulong M64 = 0x5555555555555555UL;
         private const ulong M64R = 0xAAAAAAAAAAAAAAAAUL;
 
-        internal static uint Expand8to16(uint x)
+        internal static uint Expand8to16(byte x)
         {
-            x &= 0xFFU;
-            x = (x | (x << 4)) & 0x0F0FU;
-            x = (x | (x << 2)) & 0x3333U;
-            x = (x | (x << 1)) & 0x5555U;
-            return x;
+            uint t = x;
+
+#if NETCOREAPP3_0_OR_GREATER
+            if (Bmi2.IsSupported)
+            {
+                return Bmi2.ParallelBitDeposit(t, 0x55555555U);
+            }
+#endif
+            t = (t | (t << 4)) & 0x0F0FU;
+            t = (t | (t << 2)) & 0x3333U;
+            t = (t | (t << 1)) & 0x5555U;
+            return t;
         }
 
-        internal static uint Expand16to32(uint x)
+        internal static uint Expand16to32(ushort x)
         {
-            x &= 0xFFFFU;
-            x = (x | (x << 8)) & 0x00FF00FFU;
-            x = (x | (x << 4)) & 0x0F0F0F0FU;
-            x = (x | (x << 2)) & 0x33333333U;
-            x = (x | (x << 1)) & 0x55555555U;
-            return x;
+            uint t = x;
+
+#if NETCOREAPP3_0_OR_GREATER
+            if (Bmi2.IsSupported)
+            {
+                return Bmi2.ParallelBitDeposit(t, 0x55555555U);
+            }
+#endif
+            t = (t | (t << 8)) & 0x00FF00FFU;
+            t = (t | (t << 4)) & 0x0F0F0F0FU;
+            t = (t | (t << 2)) & 0x33333333U;
+            t = (t | (t << 1)) & 0x55555555U;
+            return t;
         }
 
         internal static ulong Expand32to64(uint x)