From df651f2abe8711de81ffb4d7e0fddee36a298e70 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Mon, 6 Nov 2023 17:25:12 +0700 Subject: Create indirection layer for intrinsics support --- crypto/src/crypto/digests/Haraka256_X86.cs | 6 ++--- crypto/src/crypto/digests/Haraka512_X86.cs | 8 +++--- crypto/src/crypto/engines/AesEngine_X86.cs | 8 +++--- crypto/src/crypto/engines/ChaCha7539Engine.cs | 20 +++++++------- crypto/src/crypto/engines/ChaChaEngine.cs | 10 +++---- crypto/src/crypto/engines/Salsa20Engine.cs | 3 ++- crypto/src/crypto/engines/SparkleEngine.cs | 6 ++--- crypto/src/crypto/modes/GCMBlockCipher.cs | 22 +++++++++++----- crypto/src/crypto/modes/gcm/BasicGcmMultiplier.cs | 2 +- crypto/src/crypto/modes/gcm/GcmUtilities.cs | 6 ++--- crypto/src/math/ec/custom/sec/SecT113Field.cs | 5 ++-- crypto/src/math/ec/custom/sec/SecT131Field.cs | 5 ++-- crypto/src/math/ec/custom/sec/SecT163Field.cs | 5 ++-- crypto/src/math/ec/custom/sec/SecT193Field.cs | 4 +-- crypto/src/math/ec/custom/sec/SecT233Field.cs | 5 ++-- crypto/src/math/ec/custom/sec/SecT239Field.cs | 5 ++-- crypto/src/math/ec/custom/sec/SecT283Field.cs | 5 ++-- crypto/src/math/ec/custom/sec/SecT409Field.cs | 4 +-- crypto/src/math/ec/custom/sec/SecT571Field.cs | 4 +-- crypto/src/math/ec/rfc7748/X25519Field.cs | 18 ++++++++----- crypto/src/math/ec/rfc7748/X448Field.cs | 3 ++- crypto/src/math/raw/Interleave.cs | 32 +++++++++++------------ crypto/src/math/raw/Nat256.cs | 6 +++-- crypto/src/math/raw/Nat512.cs | 24 +++++++++++------ crypto/src/pqc/crypto/bike/BikeRing.cs | 2 +- crypto/src/pqc/crypto/cmce/CmceEngine.cs | 2 +- crypto/src/pqc/crypto/sphincsplus/HarakaS_X86.cs | 2 +- crypto/src/runtime/intrinsics/Vector.cs | 24 +++++++++++++++++ crypto/src/runtime/intrinsics/x86/Aes.cs | 20 ++++++++++++++ crypto/src/runtime/intrinsics/x86/Avx2.cs | 20 ++++++++++++++ crypto/src/runtime/intrinsics/x86/Bmi1.cs | 20 ++++++++++++++ crypto/src/runtime/intrinsics/x86/Bmi2.cs | 20 ++++++++++++++ crypto/src/runtime/intrinsics/x86/Pclmulqdq.cs | 20 ++++++++++++++ crypto/src/runtime/intrinsics/x86/Sse2.cs | 20 ++++++++++++++ crypto/src/runtime/intrinsics/x86/Sse41.cs | 20 ++++++++++++++ crypto/src/runtime/intrinsics/x86/Ssse3.cs | 20 ++++++++++++++ 36 files changed, 311 insertions(+), 95 deletions(-) create mode 100644 crypto/src/runtime/intrinsics/Vector.cs create mode 100644 crypto/src/runtime/intrinsics/x86/Aes.cs create mode 100644 crypto/src/runtime/intrinsics/x86/Avx2.cs create mode 100644 crypto/src/runtime/intrinsics/x86/Bmi1.cs create mode 100644 crypto/src/runtime/intrinsics/x86/Bmi2.cs create mode 100644 crypto/src/runtime/intrinsics/x86/Pclmulqdq.cs create mode 100644 crypto/src/runtime/intrinsics/x86/Sse2.cs create mode 100644 crypto/src/runtime/intrinsics/x86/Sse41.cs create mode 100644 crypto/src/runtime/intrinsics/x86/Ssse3.cs (limited to 'crypto') diff --git a/crypto/src/crypto/digests/Haraka256_X86.cs b/crypto/src/crypto/digests/Haraka256_X86.cs index 51642a65b..f2c377f46 100644 --- a/crypto/src/crypto/digests/Haraka256_X86.cs +++ b/crypto/src/crypto/digests/Haraka256_X86.cs @@ -12,7 +12,7 @@ namespace Org.BouncyCastle.Crypto.Digests public static class Haraka256_X86 { - public static bool IsSupported => Aes.IsSupported; + public static bool IsSupported => Org.BouncyCastle.Runtime.Intrinsics.X86.Aes.IsEnabled; public static void Hash(ReadOnlySpan input, Span output) { @@ -117,7 +117,7 @@ namespace Org.BouncyCastle.Crypto.Digests [MethodImpl(MethodImplOptions.AggressiveInlining)] private static Vector128 Load128(ReadOnlySpan t) { - if (BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) return MemoryMarshal.Read>(t); return Vector128.Create( @@ -129,7 +129,7 @@ namespace Org.BouncyCastle.Crypto.Digests [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void Store128(Vector128 s, Span t) { - if (BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) { MemoryMarshal.Write(t, ref s); return; diff --git a/crypto/src/crypto/digests/Haraka512_X86.cs b/crypto/src/crypto/digests/Haraka512_X86.cs index fb979ab48..e05d878b7 100644 --- a/crypto/src/crypto/digests/Haraka512_X86.cs +++ b/crypto/src/crypto/digests/Haraka512_X86.cs @@ -12,7 +12,7 @@ namespace Org.BouncyCastle.Crypto.Digests public static class Haraka512_X86 { - public static bool IsSupported => Aes.IsSupported; + public static bool IsSupported => Org.BouncyCastle.Runtime.Intrinsics.X86.Aes.IsEnabled; // Haraka round constants internal static readonly Vector128[] DefaultRoundConstants = new Vector128[] @@ -200,7 +200,7 @@ namespace Org.BouncyCastle.Crypto.Digests [MethodImpl(MethodImplOptions.AggressiveInlining)] private static Vector128 Load128(ReadOnlySpan t) { - if (BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) return MemoryMarshal.Read>(t); return Vector128.Create( @@ -212,7 +212,7 @@ namespace Org.BouncyCastle.Crypto.Digests [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void Store128(Vector128 s, Span t) { - if (BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) { MemoryMarshal.Write(t, ref s); return; @@ -226,7 +226,7 @@ namespace Org.BouncyCastle.Crypto.Digests [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void Store64(Vector64 s, Span t) { - if (BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 8) + if (Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) { MemoryMarshal.Write(t, ref s); return; diff --git a/crypto/src/crypto/engines/AesEngine_X86.cs b/crypto/src/crypto/engines/AesEngine_X86.cs index e9523bad5..274fe327d 100644 --- a/crypto/src/crypto/engines/AesEngine_X86.cs +++ b/crypto/src/crypto/engines/AesEngine_X86.cs @@ -16,7 +16,7 @@ namespace Org.BouncyCastle.Crypto.Engines public struct AesEngine_X86 : IBlockCipher { - public static bool IsSupported => Aes.IsSupported; + public static bool IsSupported => Org.BouncyCastle.Runtime.Intrinsics.X86.Aes.IsEnabled; private static Vector128[] CreateRoundKeys(ReadOnlySpan key, bool forEncryption) { @@ -798,7 +798,7 @@ namespace Org.BouncyCastle.Crypto.Engines [MethodImpl(MethodImplOptions.AggressiveInlining)] private static Vector128 Load128(ReadOnlySpan t) { - if (BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) return MemoryMarshal.Read>(t); return Vector128.Create( @@ -810,7 +810,7 @@ namespace Org.BouncyCastle.Crypto.Engines [MethodImpl(MethodImplOptions.AggressiveInlining)] private static Vector64 Load64(ReadOnlySpan t) { - if (BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 8) + if (Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) return MemoryMarshal.Read>(t); return Vector64.Create( @@ -821,7 +821,7 @@ namespace Org.BouncyCastle.Crypto.Engines [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void Store128(Vector128 s, Span t) { - if (BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) { MemoryMarshal.Write(t, ref s); return; diff --git a/crypto/src/crypto/engines/ChaCha7539Engine.cs b/crypto/src/crypto/engines/ChaCha7539Engine.cs index d9cdac541..e87c535ef 100644 --- a/crypto/src/crypto/engines/ChaCha7539Engine.cs +++ b/crypto/src/crypto/engines/ChaCha7539Engine.cs @@ -144,13 +144,13 @@ namespace Org.BouncyCastle.Crypto.Engines Debug.Assert(index == 0); #if NETCOREAPP3_0_OR_GREATER - if (Avx2.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Avx2.IsEnabled) { ImplProcessBlocks2_X86_Avx2(rounds, engineState, input, output); return; } - if (Sse2.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Sse2.IsEnabled) { ImplProcessBlocks2_X86_Sse2(rounds, engineState, input, output); return; @@ -222,7 +222,7 @@ namespace Org.BouncyCastle.Crypto.Engines internal static void ImplProcessBlocks2_X86_Avx2(int rounds, uint[] state, ReadOnlySpan input, Span output) { - if (!Avx2.IsSupported) + if (!Org.BouncyCastle.Runtime.Intrinsics.X86.Avx2.IsEnabled) throw new PlatformNotSupportedException(); Debug.Assert(rounds % 2 == 0); @@ -310,7 +310,7 @@ namespace Org.BouncyCastle.Crypto.Engines internal static void ImplProcessBlocks2_X86_Sse2(int rounds, uint[] state, ReadOnlySpan input, Span output) { - if (!Sse2.IsSupported) + if (!Org.BouncyCastle.Runtime.Intrinsics.X86.Sse2.IsEnabled) throw new PlatformNotSupportedException(); Debug.Assert(rounds % 2 == 0); @@ -455,7 +455,7 @@ namespace Org.BouncyCastle.Crypto.Engines [MethodImpl(MethodImplOptions.AggressiveInlining)] private static Vector128 Load128_Byte(ReadOnlySpan t) { - if (BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) return MemoryMarshal.Read>(t); return Vector128.Create( @@ -467,7 +467,7 @@ namespace Org.BouncyCastle.Crypto.Engines [MethodImpl(MethodImplOptions.AggressiveInlining)] private static Vector128 Load128_UInt32(ReadOnlySpan t) { - if (BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) return MemoryMarshal.Read>(MemoryMarshal.AsBytes(t)); return Vector128.Create(t[0], t[1], t[2], t[3]); @@ -476,7 +476,7 @@ namespace Org.BouncyCastle.Crypto.Engines [MethodImpl(MethodImplOptions.AggressiveInlining)] private static Vector256 Load256_Byte(ReadOnlySpan t) { - if (BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 32) + if (Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) return MemoryMarshal.Read>(t); return Vector256.Create( @@ -490,7 +490,7 @@ namespace Org.BouncyCastle.Crypto.Engines [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void Store128_Byte(Vector128 s, Span t) { - if (BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) { MemoryMarshal.Write(t, ref s); return; @@ -504,8 +504,8 @@ namespace Org.BouncyCastle.Crypto.Engines [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void Store256_Byte(Vector256 s, Span t) { - if (BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 32) - { + if (Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) + { MemoryMarshal.Write(t, ref s); return; } diff --git a/crypto/src/crypto/engines/ChaChaEngine.cs b/crypto/src/crypto/engines/ChaChaEngine.cs index 093e1a250..8e727ae61 100644 --- a/crypto/src/crypto/engines/ChaChaEngine.cs +++ b/crypto/src/crypto/engines/ChaChaEngine.cs @@ -83,7 +83,7 @@ namespace Org.BouncyCastle.Crypto.Engines Debug.Assert(output.Length >= 64); #if NETCOREAPP3_0_OR_GREATER - if (Sse2.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Sse2.IsEnabled) { var x0 = Load128_UInt32(input.AsSpan()); var x1 = Load128_UInt32(input.AsSpan(4)); @@ -217,7 +217,7 @@ namespace Org.BouncyCastle.Crypto.Engines [MethodImpl(MethodImplOptions.AggressiveInlining)] private static Vector128 Load128_UInt32(ReadOnlySpan t) { - if (BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) return MemoryMarshal.Read>(MemoryMarshal.AsBytes(t)); return Vector128.Create(t[0], t[1], t[2], t[3]); @@ -226,9 +226,9 @@ namespace Org.BouncyCastle.Crypto.Engines [MethodImpl(MethodImplOptions.AggressiveInlining)] private static void Store128_UInt32(Vector128 s, Span t) { - if (BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) - { - MemoryMarshal.Write(t, ref s); + if (Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) + { + MemoryMarshal.Write(t, ref s); return; } diff --git a/crypto/src/crypto/engines/Salsa20Engine.cs b/crypto/src/crypto/engines/Salsa20Engine.cs index 2e8f8e50a..3d33a2186 100644 --- a/crypto/src/crypto/engines/Salsa20Engine.cs +++ b/crypto/src/crypto/engines/Salsa20Engine.cs @@ -263,7 +263,8 @@ namespace Org.BouncyCastle.Crypto.Engines throw new ArgumentException("Number of rounds must be even"); #if NETCOREAPP3_0_OR_GREATER - if (Sse41.IsSupported && BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Sse41.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) { Vector128 b0, b1, b2, b3; { diff --git a/crypto/src/crypto/engines/SparkleEngine.cs b/crypto/src/crypto/engines/SparkleEngine.cs index 13aaf1edf..2873a8a56 100644 --- a/crypto/src/crypto/engines/SparkleEngine.cs +++ b/crypto/src/crypto/engines/SparkleEngine.cs @@ -1140,7 +1140,7 @@ namespace Org.BouncyCastle.Crypto.Engines Debug.Assert((steps & 1) == 0); #if NETCOREAPP3_0_OR_GREATER - if (Sse2.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Sse2.IsEnabled) { var s0246 = Vector128.Create(state[0], state[2], state[4], state[6]); var s1357 = Vector128.Create(state[1], state[3], state[5], state[7]); @@ -1348,7 +1348,7 @@ namespace Org.BouncyCastle.Crypto.Engines [MethodImpl(MethodImplOptions.AggressiveInlining)] private static Vector128 Load128(ReadOnlySpan t) { - if (BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) return MemoryMarshal.Read>(MemoryMarshal.AsBytes(t)); return Vector128.Create(t[0], t[1], t[2], t[3]); @@ -1358,7 +1358,7 @@ namespace Org.BouncyCastle.Crypto.Engines private static void Store128(Vector128 s, Span t) { var b = MemoryMarshal.AsBytes(t); - if (BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) { MemoryMarshal.Write(b, ref s); return; diff --git a/crypto/src/crypto/modes/GCMBlockCipher.cs b/crypto/src/crypto/modes/GCMBlockCipher.cs index ad4c044d4..b413088ab 100644 --- a/crypto/src/crypto/modes/GCMBlockCipher.cs +++ b/crypto/src/crypto/modes/GCMBlockCipher.cs @@ -30,7 +30,9 @@ namespace Org.BouncyCastle.Crypto.Modes Vector128.Create((byte)15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0); private static bool IsFourWaySupported => - Pclmulqdq.IsSupported && Ssse3.IsSupported && Unsafe.SizeOf>() == BlockSize; + Org.BouncyCastle.Runtime.Intrinsics.X86.Pclmulqdq.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.X86.Ssse3.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPacked; #endif internal static IGcmMultiplier CreateGcmMultiplier() @@ -1093,7 +1095,8 @@ namespace Org.BouncyCastle.Crypto.Modes GetNextCtrBlock(ctrBlock); #if NETCOREAPP3_0_OR_GREATER - if (Sse2.IsSupported && Unsafe.SizeOf>() == BlockSize) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Sse2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPacked) { var t0 = MemoryMarshal.Read>(input); var t1 = MemoryMarshal.Read>(ctrBlock); @@ -1135,7 +1138,8 @@ namespace Org.BouncyCastle.Crypto.Modes GetNextCtrBlock(ctrBlock); #if NETCOREAPP3_0_OR_GREATER - if (Sse2.IsSupported && Unsafe.SizeOf>() == BlockSize) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Sse2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPacked) { var t0 = MemoryMarshal.Read>(input); var t1 = MemoryMarshal.Read>(ctrBlock); @@ -1175,7 +1179,8 @@ namespace Org.BouncyCastle.Crypto.Modes GetNextCtrBlock(ctrBlock); #if NETCOREAPP3_0_OR_GREATER - if (Sse2.IsSupported && Unsafe.SizeOf>() == BlockSize) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Sse2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPacked) { var t0 = MemoryMarshal.Read>(input); var t1 = MemoryMarshal.Read>(ctrBlock); @@ -1290,7 +1295,8 @@ namespace Org.BouncyCastle.Crypto.Modes GetNextCtrBlock(ctrBlock); #if NETCOREAPP3_0_OR_GREATER - if (Sse2.IsSupported && Unsafe.SizeOf>() == BlockSize) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Sse2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPacked) { var t0 = MemoryMarshal.Read>(input); var t1 = MemoryMarshal.Read>(ctrBlock); @@ -1332,7 +1338,8 @@ namespace Org.BouncyCastle.Crypto.Modes GetNextCtrBlocks2(ctrBlocks); #if NETCOREAPP3_0_OR_GREATER - if (Sse2.IsSupported && Unsafe.SizeOf>() == BlockSize) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Sse2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPacked) { var t0 = MemoryMarshal.Read>(input); var t1 = MemoryMarshal.Read>(ctrBlocks); @@ -1372,7 +1379,8 @@ namespace Org.BouncyCastle.Crypto.Modes ctrBlocks = ctrBlocks[BlockSize..]; #if NETCOREAPP3_0_OR_GREATER - if (Sse2.IsSupported && Unsafe.SizeOf>() == BlockSize) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Sse2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPacked) { var t0 = MemoryMarshal.Read>(input); var t1 = MemoryMarshal.Read>(ctrBlocks); diff --git a/crypto/src/crypto/modes/gcm/BasicGcmMultiplier.cs b/crypto/src/crypto/modes/gcm/BasicGcmMultiplier.cs index 7fa275449..d902e08cf 100644 --- a/crypto/src/crypto/modes/gcm/BasicGcmMultiplier.cs +++ b/crypto/src/crypto/modes/gcm/BasicGcmMultiplier.cs @@ -10,7 +10,7 @@ namespace Org.BouncyCastle.Crypto.Modes.Gcm : IGcmMultiplier { #if NETCOREAPP3_0_OR_GREATER - internal static bool IsHardwareAccelerated => Pclmulqdq.IsSupported; + internal static bool IsHardwareAccelerated => Org.BouncyCastle.Runtime.Intrinsics.X86.Pclmulqdq.IsEnabled; #else internal static bool IsHardwareAccelerated => false; #endif diff --git a/crypto/src/crypto/modes/gcm/GcmUtilities.cs b/crypto/src/crypto/modes/gcm/GcmUtilities.cs index 1cc4d262d..ef6ae62c3 100644 --- a/crypto/src/crypto/modes/gcm/GcmUtilities.cs +++ b/crypto/src/crypto/modes/gcm/GcmUtilities.cs @@ -78,7 +78,7 @@ namespace Org.BouncyCastle.Crypto.Modes.Gcm ulong z0, z1, z2; #if NETCOREAPP3_0_OR_GREATER - if (Pclmulqdq.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Pclmulqdq.IsEnabled) { var X = Vector128.Create(x.n1, x.n0); var Y = Vector128.Create(y.n1, y.n0); @@ -168,7 +168,7 @@ namespace Org.BouncyCastle.Crypto.Modes.Gcm internal static void MultiplyExt(Vector128 X, Vector128 Y, out Vector128 Z0, out Vector128 Z1, out Vector128 Z2) { - if (!Pclmulqdq.IsSupported) + if (!Org.BouncyCastle.Runtime.Intrinsics.X86.Pclmulqdq.IsEnabled) throw new PlatformNotSupportedException(nameof(GcmUtilities.MultiplyExt)); Z0 = Pclmulqdq.CarrylessMultiply(X, Y, 0x00); @@ -241,7 +241,7 @@ namespace Org.BouncyCastle.Crypto.Modes.Gcm [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void SquareExt(Vector128 X, out Vector128 Z0, out Vector128 Z2) { - if (!Pclmulqdq.IsSupported) + if (!Org.BouncyCastle.Runtime.Intrinsics.X86.Pclmulqdq.IsEnabled) throw new PlatformNotSupportedException(nameof(GcmUtilities.SquareExt)); Z0 = Pclmulqdq.CarrylessMultiply(X, X, 0x00); diff --git a/crypto/src/math/ec/custom/sec/SecT113Field.cs b/crypto/src/math/ec/custom/sec/SecT113Field.cs index 596d8070b..2477b9c78 100644 --- a/crypto/src/math/ec/custom/sec/SecT113Field.cs +++ b/crypto/src/math/ec/custom/sec/SecT113Field.cs @@ -287,7 +287,8 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec private static void ImplMultiply(ReadOnlySpan x, ReadOnlySpan y, Span zz) { #if NETCOREAPP3_0_OR_GREATER - if (Pclmulqdq.IsSupported && BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Pclmulqdq.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) { var X01 = Vector128.Create(x[0], x[1]); var Y01 = Vector128.Create(y[0], y[1]); @@ -424,7 +425,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec #endif { #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.X64.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.X64.IsEnabled) { zz[3] = Bmi2.X64.ParallelBitDeposit(x[1] >> 32, 0x5555555555555555UL); zz[2] = Bmi2.X64.ParallelBitDeposit(x[1] , 0x5555555555555555UL); diff --git a/crypto/src/math/ec/custom/sec/SecT131Field.cs b/crypto/src/math/ec/custom/sec/SecT131Field.cs index 743fa6a5d..49d504afb 100644 --- a/crypto/src/math/ec/custom/sec/SecT131Field.cs +++ b/crypto/src/math/ec/custom/sec/SecT131Field.cs @@ -324,7 +324,8 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec private static void ImplMultiply(ReadOnlySpan x, ReadOnlySpan y, Span zz) { #if NETCOREAPP3_0_OR_GREATER - if (Pclmulqdq.IsSupported && BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Pclmulqdq.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) { var X01 = Vector128.Create(x[0], x[1]); var X2_ = Vector128.CreateScalar(x[2]); @@ -620,7 +621,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec zz[4] = Interleave.Expand8to16((byte)x[2]); #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.X64.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.X64.IsEnabled) { zz[3] = Bmi2.X64.ParallelBitDeposit(x[1] >> 32, 0x5555555555555555UL); zz[2] = Bmi2.X64.ParallelBitDeposit(x[1] , 0x5555555555555555UL); diff --git a/crypto/src/math/ec/custom/sec/SecT163Field.cs b/crypto/src/math/ec/custom/sec/SecT163Field.cs index d0f09cd8b..e4fda48ac 100644 --- a/crypto/src/math/ec/custom/sec/SecT163Field.cs +++ b/crypto/src/math/ec/custom/sec/SecT163Field.cs @@ -335,7 +335,8 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec private static void ImplMultiply(ReadOnlySpan x, ReadOnlySpan y, Span zz) { #if NETCOREAPP3_0_OR_GREATER - if (Pclmulqdq.IsSupported && BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Pclmulqdq.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) { var X01 = Vector128.Create(x[0], x[1]); var X2_ = Vector128.CreateScalar(x[2]); @@ -623,7 +624,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec #endif { #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.X64.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.X64.IsEnabled) { zz[5] = Bmi2.X64.ParallelBitDeposit(x[2] >> 32, 0x5555555555555555UL); zz[4] = Bmi2.X64.ParallelBitDeposit(x[2] , 0x5555555555555555UL); diff --git a/crypto/src/math/ec/custom/sec/SecT193Field.cs b/crypto/src/math/ec/custom/sec/SecT193Field.cs index b610bf554..7eebc29c6 100644 --- a/crypto/src/math/ec/custom/sec/SecT193Field.cs +++ b/crypto/src/math/ec/custom/sec/SecT193Field.cs @@ -360,7 +360,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec private static void ImplMultiply(ReadOnlySpan x, ReadOnlySpan y, Span zz) { #if NETCOREAPP3_0_OR_GREATER - if (Pclmulqdq.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Pclmulqdq.IsEnabled) { var X01 = Vector128.Create(x[0], x[1]); var X2_ = Vector128.CreateScalar(x[2]); @@ -545,7 +545,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec zz[6] = x[3] & M01; #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.X64.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.X64.IsEnabled) { zz[5] = Bmi2.X64.ParallelBitDeposit(x[2] >> 32, 0x5555555555555555UL); zz[4] = Bmi2.X64.ParallelBitDeposit(x[2] , 0x5555555555555555UL); diff --git a/crypto/src/math/ec/custom/sec/SecT233Field.cs b/crypto/src/math/ec/custom/sec/SecT233Field.cs index 00bbc0635..bf7b33139 100644 --- a/crypto/src/math/ec/custom/sec/SecT233Field.cs +++ b/crypto/src/math/ec/custom/sec/SecT233Field.cs @@ -378,7 +378,8 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec private static void ImplMultiply(ReadOnlySpan x, ReadOnlySpan y, Span zz) { #if NETCOREAPP3_0_OR_GREATER - if (Pclmulqdq.IsSupported && BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Pclmulqdq.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) { var X01 = Vector128.Create(x[0], x[1]); var X23 = Vector128.Create(x[2], x[3]); @@ -576,7 +577,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec #endif { #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.X64.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.X64.IsEnabled) { ulong x0 = x[0], x1 = x[1], x2 = x[2], x3 = x[3]; zz[7] = Bmi2.X64.ParallelBitDeposit(x3 >> 32, 0x5555555555555555UL); diff --git a/crypto/src/math/ec/custom/sec/SecT239Field.cs b/crypto/src/math/ec/custom/sec/SecT239Field.cs index b90867b76..a6f3c9e4d 100644 --- a/crypto/src/math/ec/custom/sec/SecT239Field.cs +++ b/crypto/src/math/ec/custom/sec/SecT239Field.cs @@ -387,7 +387,8 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec private static void ImplMultiply(ReadOnlySpan x, ReadOnlySpan y, Span zz) { #if NETCOREAPP3_0_OR_GREATER - if (Pclmulqdq.IsSupported && BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Pclmulqdq.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) { var X01 = Vector128.Create(x[0], x[1]); var X23 = Vector128.Create(x[2], x[3]); @@ -587,7 +588,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec #endif { #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.X64.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.X64.IsEnabled) { ulong x0 = x[0], x1 = x[1], x2 = x[2], x3 = x[3]; zz[7] = Bmi2.X64.ParallelBitDeposit(x3 >> 32, 0x5555555555555555UL); diff --git a/crypto/src/math/ec/custom/sec/SecT283Field.cs b/crypto/src/math/ec/custom/sec/SecT283Field.cs index 498a72c81..92f8ea385 100644 --- a/crypto/src/math/ec/custom/sec/SecT283Field.cs +++ b/crypto/src/math/ec/custom/sec/SecT283Field.cs @@ -386,7 +386,8 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec private static void ImplMultiply(ReadOnlySpan x, ReadOnlySpan y, Span zz) { #if NETCOREAPP3_0_OR_GREATER - if (Pclmulqdq.IsSupported && BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Pclmulqdq.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) { var X01 = Vector128.Create(x[0], x[1]); var X23 = Vector128.Create(x[2], x[3]); @@ -746,7 +747,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec zz[8] = Interleave.Expand32to64((uint)x[4]); #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.X64.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.X64.IsEnabled) { zz[7] = Bmi2.X64.ParallelBitDeposit(x[3] >> 32, 0x5555555555555555UL); zz[6] = Bmi2.X64.ParallelBitDeposit(x[3] , 0x5555555555555555UL); diff --git a/crypto/src/math/ec/custom/sec/SecT409Field.cs b/crypto/src/math/ec/custom/sec/SecT409Field.cs index 6a5afb0dc..a8a39a575 100644 --- a/crypto/src/math/ec/custom/sec/SecT409Field.cs +++ b/crypto/src/math/ec/custom/sec/SecT409Field.cs @@ -551,7 +551,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec Debug.Assert(y >> 59 == 0); #if NETCOREAPP3_0_OR_GREATER - if (Pclmulqdq.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Pclmulqdq.IsEnabled) { var X = Vector128.CreateScalar(x); var Y = Vector128.CreateScalar(y); @@ -607,7 +607,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec zz[12] = Interleave.Expand32to64((uint)x[6]); #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.X64.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.X64.IsEnabled) { zz[11] = Bmi2.X64.ParallelBitDeposit(x[5] >> 32, 0x5555555555555555UL); zz[10] = Bmi2.X64.ParallelBitDeposit(x[5] , 0x5555555555555555UL); diff --git a/crypto/src/math/ec/custom/sec/SecT571Field.cs b/crypto/src/math/ec/custom/sec/SecT571Field.cs index e970027a5..47f157dc3 100644 --- a/crypto/src/math/ec/custom/sec/SecT571Field.cs +++ b/crypto/src/math/ec/custom/sec/SecT571Field.cs @@ -654,7 +654,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec #endif { #if NETCOREAPP3_0_OR_GREATER - if (Pclmulqdq.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Pclmulqdq.IsEnabled) { var X = Vector128.CreateScalar(x); var Y = Vector128.CreateScalar(y); @@ -711,7 +711,7 @@ namespace Org.BouncyCastle.Math.EC.Custom.Sec #endif { #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.X64.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.X64.IsEnabled) { zz[17] = Bmi2.X64.ParallelBitDeposit(x[8] >> 32, 0x5555555555555555UL); zz[16] = Bmi2.X64.ParallelBitDeposit(x[8] , 0x5555555555555555UL); diff --git a/crypto/src/math/ec/rfc7748/X25519Field.cs b/crypto/src/math/ec/rfc7748/X25519Field.cs index 47bca9935..ffe468f73 100644 --- a/crypto/src/math/ec/rfc7748/X25519Field.cs +++ b/crypto/src/math/ec/rfc7748/X25519Field.cs @@ -35,7 +35,8 @@ namespace Org.BouncyCastle.Math.EC.Rfc7748 public static void Add(int[] x, int[] y, int[] z) { #if NETCOREAPP3_0_OR_GREATER - if (Avx2.IsSupported && BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 32) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Avx2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) { var X = MemoryMarshal.AsBytes(x.AsSpan(0, 8)); var Y = MemoryMarshal.AsBytes(y.AsSpan(0, 8)); @@ -54,7 +55,8 @@ namespace Org.BouncyCastle.Math.EC.Rfc7748 return; } - if (Sse2.IsSupported && BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Sse2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) { var X = MemoryMarshal.AsBytes(x.AsSpan(0, 8)); var Y = MemoryMarshal.AsBytes(y.AsSpan(0, 8)); @@ -105,7 +107,8 @@ namespace Org.BouncyCastle.Math.EC.Rfc7748 public static void Apm(int[] x, int[] y, int[] zp, int[] zm) { #if NETCOREAPP3_0_OR_GREATER - if (Avx2.IsSupported && BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 32) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Avx2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) { var X = MemoryMarshal.AsBytes(x.AsSpan(0, 8)); var Y = MemoryMarshal.AsBytes(y.AsSpan(0, 8)); @@ -132,7 +135,8 @@ namespace Org.BouncyCastle.Math.EC.Rfc7748 return; } - if (Sse2.IsSupported && BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Sse2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) { var X = MemoryMarshal.AsBytes(x.AsSpan(0, 8)); var Y = MemoryMarshal.AsBytes(y.AsSpan(0, 8)); @@ -1101,7 +1105,8 @@ namespace Org.BouncyCastle.Math.EC.Rfc7748 public static void Sub(int[] x, int[] y, int[] z) { #if NETCOREAPP3_0_OR_GREATER - if (Avx2.IsSupported && BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 32) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Avx2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) { var X = MemoryMarshal.AsBytes(x.AsSpan(0, 8)); var Y = MemoryMarshal.AsBytes(y.AsSpan(0, 8)); @@ -1120,7 +1125,8 @@ namespace Org.BouncyCastle.Math.EC.Rfc7748 return; } - if (Sse2.IsSupported && BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Sse2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) { var X = MemoryMarshal.AsBytes(x.AsSpan(0, 8)); var Y = MemoryMarshal.AsBytes(y.AsSpan(0, 8)); diff --git a/crypto/src/math/ec/rfc7748/X448Field.cs b/crypto/src/math/ec/rfc7748/X448Field.cs index d6210ddeb..1b9fbb839 100644 --- a/crypto/src/math/ec/rfc7748/X448Field.cs +++ b/crypto/src/math/ec/rfc7748/X448Field.cs @@ -1458,7 +1458,8 @@ namespace Org.BouncyCastle.Math.EC.Rfc7748 public static void Sub(ReadOnlySpan x, ReadOnlySpan y, Span z) { #if NETCOREAPP3_0_OR_GREATER - if (Avx2.IsSupported && BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 32) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Avx2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) { var ControlCarry = Vector256.Create(7U, 0U, 1U, 2U, 3U, 4U, 5U, 6U); var Mask28 = Vector256.Create(M28); diff --git a/crypto/src/math/raw/Interleave.cs b/crypto/src/math/raw/Interleave.cs index 8082ce57c..e71f8e394 100644 --- a/crypto/src/math/raw/Interleave.cs +++ b/crypto/src/math/raw/Interleave.cs @@ -17,7 +17,7 @@ namespace Org.BouncyCastle.Math.Raw uint t = x; #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.IsEnabled) { return Bmi2.ParallelBitDeposit(t, 0x55555555U); } @@ -33,7 +33,7 @@ namespace Org.BouncyCastle.Math.Raw uint t = x; #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.IsEnabled) { return Bmi2.ParallelBitDeposit(t, 0x55555555U); } @@ -48,7 +48,7 @@ namespace Org.BouncyCastle.Math.Raw internal static ulong Expand32to64(uint x) { #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.IsEnabled) { return (ulong)Bmi2.ParallelBitDeposit(x >> 16, 0x55555555U) << 32 | Bmi2.ParallelBitDeposit(x , 0x55555555U); @@ -67,7 +67,7 @@ namespace Org.BouncyCastle.Math.Raw internal static void Expand64To128(ulong x, ulong[] z, int zOff) { #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.X64.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.X64.IsEnabled) { z[zOff ] = Bmi2.X64.ParallelBitDeposit(x , 0x5555555555555555UL); z[zOff + 1] = Bmi2.X64.ParallelBitDeposit(x >> 32, 0x5555555555555555UL); @@ -90,7 +90,7 @@ namespace Org.BouncyCastle.Math.Raw internal static void Expand64To128(ulong x, Span z) { #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.X64.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.X64.IsEnabled) { z[0] = Bmi2.X64.ParallelBitDeposit(x , 0x5555555555555555UL); z[1] = Bmi2.X64.ParallelBitDeposit(x >> 32, 0x5555555555555555UL); @@ -136,7 +136,7 @@ namespace Org.BouncyCastle.Math.Raw internal static ulong Expand64To128Rev(ulong x, out ulong low) { #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.X64.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.X64.IsEnabled) { low = Bmi2.X64.ParallelBitDeposit(x >> 32, 0xAAAAAAAAAAAAAAAAUL); return Bmi2.X64.ParallelBitDeposit(x , 0xAAAAAAAAAAAAAAAAUL); @@ -157,7 +157,7 @@ namespace Org.BouncyCastle.Math.Raw internal static uint Shuffle(uint x) { #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.IsEnabled) { return Bmi2.ParallelBitDeposit(x >> 16, 0xAAAAAAAAU) | Bmi2.ParallelBitDeposit(x , 0x55555555U); @@ -175,7 +175,7 @@ namespace Org.BouncyCastle.Math.Raw internal static ulong Shuffle(ulong x) { #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.X64.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.X64.IsEnabled) { return Bmi2.X64.ParallelBitDeposit(x >> 32, 0xAAAAAAAAAAAAAAAAUL) | Bmi2.X64.ParallelBitDeposit(x , 0x5555555555555555UL); @@ -194,7 +194,7 @@ namespace Org.BouncyCastle.Math.Raw internal static uint Shuffle2(uint x) { #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.IsEnabled) { return Bmi2.ParallelBitDeposit(x >> 24, 0x88888888U) | Bmi2.ParallelBitDeposit(x >> 16, 0x44444444U) @@ -219,7 +219,7 @@ namespace Org.BouncyCastle.Math.Raw internal static ulong Shuffle2(ulong x) { #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.X64.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.X64.IsEnabled) { return Bmi2.X64.ParallelBitDeposit(x >> 48, 0x8888888888888888UL) | Bmi2.X64.ParallelBitDeposit(x >> 32, 0x4444444444444444UL) @@ -242,7 +242,7 @@ namespace Org.BouncyCastle.Math.Raw internal static uint Unshuffle(uint x) { #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.IsEnabled) { return Bmi2.ParallelBitExtract(x, 0xAAAAAAAAU) << 16 | Bmi2.ParallelBitExtract(x, 0x55555555U); @@ -260,7 +260,7 @@ namespace Org.BouncyCastle.Math.Raw internal static ulong Unshuffle(ulong x) { #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.X64.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.X64.IsEnabled) { return Bmi2.X64.ParallelBitExtract(x, 0xAAAAAAAAAAAAAAAAUL) << 32 | Bmi2.X64.ParallelBitExtract(x, 0x5555555555555555UL); @@ -279,7 +279,7 @@ namespace Org.BouncyCastle.Math.Raw internal static ulong Unshuffle(ulong x, out ulong even) { #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.X64.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.X64.IsEnabled) { even = Bmi2.X64.ParallelBitExtract(x, 0x5555555555555555UL); return Bmi2.X64.ParallelBitExtract(x, 0xAAAAAAAAAAAAAAAAUL); @@ -294,7 +294,7 @@ namespace Org.BouncyCastle.Math.Raw internal static ulong Unshuffle(ulong x0, ulong x1, out ulong even) { #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.X64.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.X64.IsEnabled) { even = Bmi2.X64.ParallelBitExtract(x0, 0x5555555555555555UL) | Bmi2.X64.ParallelBitExtract(x1, 0x5555555555555555UL) << 32; @@ -312,7 +312,7 @@ namespace Org.BouncyCastle.Math.Raw internal static uint Unshuffle2(uint x) { #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.IsEnabled) { return Bmi2.ParallelBitExtract(x, 0x88888888U) << 24 | Bmi2.ParallelBitExtract(x, 0x44444444U) << 16 @@ -337,7 +337,7 @@ namespace Org.BouncyCastle.Math.Raw internal static ulong Unshuffle2(ulong x) { #if NETCOREAPP3_0_OR_GREATER - if (Bmi2.X64.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi2.X64.IsEnabled) { return Bmi2.X64.ParallelBitExtract(x, 0x8888888888888888UL) << 48 | Bmi2.X64.ParallelBitExtract(x, 0x4444444444444444UL) << 32 diff --git a/crypto/src/math/raw/Nat256.cs b/crypto/src/math/raw/Nat256.cs index 59039d3fa..49adf04af 100644 --- a/crypto/src/math/raw/Nat256.cs +++ b/crypto/src/math/raw/Nat256.cs @@ -1865,7 +1865,8 @@ namespace Org.BouncyCastle.Math.Raw public static void Xor(ReadOnlySpan x, ReadOnlySpan y, Span z) { #if NETCOREAPP3_0_OR_GREATER - if (Avx2.IsSupported && Unsafe.SizeOf>() == 32) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Avx2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPacked) { var X = MemoryMarshal.AsBytes(x[..8]); var Y = MemoryMarshal.AsBytes(y[..8]); @@ -1880,7 +1881,8 @@ namespace Org.BouncyCastle.Math.Raw return; } - if (Sse2.IsSupported && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Sse2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPacked) { var X = MemoryMarshal.AsBytes(x[..8]); var Y = MemoryMarshal.AsBytes(y[..8]); diff --git a/crypto/src/math/raw/Nat512.cs b/crypto/src/math/raw/Nat512.cs index 56fa9a2c9..71b53214c 100644 --- a/crypto/src/math/raw/Nat512.cs +++ b/crypto/src/math/raw/Nat512.cs @@ -67,7 +67,8 @@ namespace Org.BouncyCastle.Math.Raw public static void Xor(ReadOnlySpan x, ReadOnlySpan y, Span z) { #if NETCOREAPP3_0_OR_GREATER - if (Avx2.IsSupported && Unsafe.SizeOf>() == 32) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Avx2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPacked) { var X = MemoryMarshal.AsBytes(x[..16]); var Y = MemoryMarshal.AsBytes(y[..16]); @@ -87,7 +88,8 @@ namespace Org.BouncyCastle.Math.Raw return; } - if (Sse2.IsSupported && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Sse2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPacked) { var X = MemoryMarshal.AsBytes(x[..16]); var Y = MemoryMarshal.AsBytes(y[..16]); @@ -145,7 +147,8 @@ namespace Org.BouncyCastle.Math.Raw public static void XorTo(ReadOnlySpan x, Span z) { #if NETCOREAPP3_0_OR_GREATER - if (Avx2.IsSupported && Unsafe.SizeOf>() == 32) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Avx2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPacked) { var X = MemoryMarshal.AsBytes(x[..16]); var Z = MemoryMarshal.AsBytes(z[..16]); @@ -164,7 +167,8 @@ namespace Org.BouncyCastle.Math.Raw return; } - if (Sse2.IsSupported && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Sse2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPacked) { var X = MemoryMarshal.AsBytes(x[..16]); var Z = MemoryMarshal.AsBytes(z[..16]); @@ -221,7 +225,8 @@ namespace Org.BouncyCastle.Math.Raw public static void Xor64(ReadOnlySpan x, ReadOnlySpan y, Span z) { #if NETCOREAPP3_0_OR_GREATER - if (Avx2.IsSupported && Unsafe.SizeOf>() == 32) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Avx2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPacked) { var X = MemoryMarshal.AsBytes(x[..8]); var Y = MemoryMarshal.AsBytes(y[..8]); @@ -241,7 +246,8 @@ namespace Org.BouncyCastle.Math.Raw return; } - if (Sse2.IsSupported && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Sse2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPacked) { var X = MemoryMarshal.AsBytes(x[..8]); var Y = MemoryMarshal.AsBytes(y[..8]); @@ -299,7 +305,8 @@ namespace Org.BouncyCastle.Math.Raw public static void XorTo64(ReadOnlySpan x, Span z) { #if NETCOREAPP3_0_OR_GREATER - if (Avx2.IsSupported && Unsafe.SizeOf>() == 32) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Avx2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPacked) { var X = MemoryMarshal.AsBytes(x[..8]); var Z = MemoryMarshal.AsBytes(z[..8]); @@ -318,7 +325,8 @@ namespace Org.BouncyCastle.Math.Raw return; } - if (Sse2.IsSupported && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Sse2.IsEnabled && + Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPacked) { var X = MemoryMarshal.AsBytes(x[..8]); var Z = MemoryMarshal.AsBytes(z[..8]); diff --git a/crypto/src/pqc/crypto/bike/BikeRing.cs b/crypto/src/pqc/crypto/bike/BikeRing.cs index 1fec7b89d..7f23e5273 100644 --- a/crypto/src/pqc/crypto/bike/BikeRing.cs +++ b/crypto/src/pqc/crypto/bike/BikeRing.cs @@ -213,7 +213,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Bike var zzBounds = zz[SizeExt - 1]; #if NETCOREAPP3_0_OR_GREATER - if (Pclmulqdq.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Pclmulqdq.IsEnabled) { int i = 0; diff --git a/crypto/src/pqc/crypto/cmce/CmceEngine.cs b/crypto/src/pqc/crypto/cmce/CmceEngine.cs index 297efe6f8..b2a3c7cf2 100644 --- a/crypto/src/pqc/crypto/cmce/CmceEngine.cs +++ b/crypto/src/pqc/crypto/cmce/CmceEngine.cs @@ -1016,7 +1016,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.Cmce private static int Ctz(ulong input) { #if NETCOREAPP3_0_OR_GREATER - if (Bmi1.X64.IsSupported) + if (Org.BouncyCastle.Runtime.Intrinsics.X86.Bmi1.X64.IsEnabled) { return (int)Bmi1.X64.TrailingZeroCount(input); } diff --git a/crypto/src/pqc/crypto/sphincsplus/HarakaS_X86.cs b/crypto/src/pqc/crypto/sphincsplus/HarakaS_X86.cs index 756f71a71..7a19d69fa 100644 --- a/crypto/src/pqc/crypto/sphincsplus/HarakaS_X86.cs +++ b/crypto/src/pqc/crypto/sphincsplus/HarakaS_X86.cs @@ -187,7 +187,7 @@ namespace Org.BouncyCastle.Pqc.Crypto.SphincsPlus [MethodImpl(MethodImplOptions.AggressiveInlining)] private static Vector128 Load128(ReadOnlySpan t) { - if (BitConverter.IsLittleEndian && Unsafe.SizeOf>() == 16) + if (Org.BouncyCastle.Runtime.Intrinsics.Vector.IsPackedLittleEndian) return MemoryMarshal.Read>(t); return Vector128.Create( diff --git a/crypto/src/runtime/intrinsics/Vector.cs b/crypto/src/runtime/intrinsics/Vector.cs new file mode 100644 index 000000000..eff52ef44 --- /dev/null +++ b/crypto/src/runtime/intrinsics/Vector.cs @@ -0,0 +1,24 @@ +#if NETCOREAPP3_0_OR_GREATER +using System; +using System.Runtime.CompilerServices; +using System.Runtime.Intrinsics; +#endif + +namespace Org.BouncyCastle.Runtime.Intrinsics +{ + internal static class Vector + { +#if NETCOREAPP3_0_OR_GREATER + internal static bool IsPacked => + Unsafe.SizeOf>() == 8 && + Unsafe.SizeOf>() == 16 && + Unsafe.SizeOf>() == 32; + + internal static bool IsPackedLittleEndian => IsPacked && BitConverter.IsLittleEndian; +#else + internal static bool IsPacked => false; + + internal static bool IsPackedLittleEndian => false; +#endif + } +} diff --git a/crypto/src/runtime/intrinsics/x86/Aes.cs b/crypto/src/runtime/intrinsics/x86/Aes.cs new file mode 100644 index 000000000..9aa57783e --- /dev/null +++ b/crypto/src/runtime/intrinsics/x86/Aes.cs @@ -0,0 +1,20 @@ +namespace Org.BouncyCastle.Runtime.Intrinsics.X86 +{ + internal static class Aes + { +#if NETCOREAPP3_0_OR_GREATER + internal static bool IsEnabled => System.Runtime.Intrinsics.X86.Aes.IsSupported; +#else + internal static bool IsEnabled => false; +#endif + +// internal static class X64 +// { +//#if NETCOREAPP3_0_OR_GREATER +// internal static bool IsEnabled => System.Runtime.Intrinsics.X86.Aes.X64.IsSupported; +//#else +// internal static bool IsEnabled => false; +//#endif +// } + } +} diff --git a/crypto/src/runtime/intrinsics/x86/Avx2.cs b/crypto/src/runtime/intrinsics/x86/Avx2.cs new file mode 100644 index 000000000..180b4f35c --- /dev/null +++ b/crypto/src/runtime/intrinsics/x86/Avx2.cs @@ -0,0 +1,20 @@ +namespace Org.BouncyCastle.Runtime.Intrinsics.X86 +{ + internal static class Avx2 + { +#if NETCOREAPP3_0_OR_GREATER + internal static bool IsEnabled => System.Runtime.Intrinsics.X86.Avx2.IsSupported; +#else + internal static bool IsEnabled => false; +#endif + +// internal static class X64 +// { +//#if NETCOREAPP3_0_OR_GREATER +// internal static bool IsEnabled => System.Runtime.Intrinsics.X86.Avx2.X64.IsSupported; +//#else +// internal static bool IsEnabled => false; +//#endif +// } + } +} diff --git a/crypto/src/runtime/intrinsics/x86/Bmi1.cs b/crypto/src/runtime/intrinsics/x86/Bmi1.cs new file mode 100644 index 000000000..a2eabec12 --- /dev/null +++ b/crypto/src/runtime/intrinsics/x86/Bmi1.cs @@ -0,0 +1,20 @@ +namespace Org.BouncyCastle.Runtime.Intrinsics.X86 +{ + internal static class Bmi1 + { +//#if NETCOREAPP3_0_OR_GREATER +// internal static bool IsEnabled => System.Runtime.Intrinsics.X86.Bmi1.IsSupported; +//#else +// internal static bool IsEnabled => false; +//#endif + + internal static class X64 + { +#if NETCOREAPP3_0_OR_GREATER + internal static bool IsEnabled => System.Runtime.Intrinsics.X86.Bmi1.X64.IsSupported; +#else + internal static bool IsEnabled => false; +#endif + } + } +} diff --git a/crypto/src/runtime/intrinsics/x86/Bmi2.cs b/crypto/src/runtime/intrinsics/x86/Bmi2.cs new file mode 100644 index 000000000..5ce1c03c4 --- /dev/null +++ b/crypto/src/runtime/intrinsics/x86/Bmi2.cs @@ -0,0 +1,20 @@ +namespace Org.BouncyCastle.Runtime.Intrinsics.X86 +{ + internal static class Bmi2 + { +#if NETCOREAPP3_0_OR_GREATER + internal static bool IsEnabled => System.Runtime.Intrinsics.X86.Bmi2.IsSupported; +#else + internal static bool IsEnabled => false; +#endif + + internal static class X64 + { +#if NETCOREAPP3_0_OR_GREATER + internal static bool IsEnabled => System.Runtime.Intrinsics.X86.Bmi2.X64.IsSupported; +#else + internal static bool IsEnabled => false; +#endif + } + } +} diff --git a/crypto/src/runtime/intrinsics/x86/Pclmulqdq.cs b/crypto/src/runtime/intrinsics/x86/Pclmulqdq.cs new file mode 100644 index 000000000..a28ea7230 --- /dev/null +++ b/crypto/src/runtime/intrinsics/x86/Pclmulqdq.cs @@ -0,0 +1,20 @@ +namespace Org.BouncyCastle.Runtime.Intrinsics.X86 +{ + internal static class Pclmulqdq + { +#if NETCOREAPP3_0_OR_GREATER + internal static bool IsEnabled => System.Runtime.Intrinsics.X86.Pclmulqdq.IsSupported; +#else + internal static bool IsEnabled => false; +#endif + +// internal static class X64 +// { +//#if NETCOREAPP3_0_OR_GREATER +// internal static bool IsEnabled => System.Runtime.Intrinsics.X86.Pclmulqdq.X64.IsSupported; +//#else +// internal static bool IsEnabled => false; +//#endif +// } + } +} diff --git a/crypto/src/runtime/intrinsics/x86/Sse2.cs b/crypto/src/runtime/intrinsics/x86/Sse2.cs new file mode 100644 index 000000000..62fecaf3d --- /dev/null +++ b/crypto/src/runtime/intrinsics/x86/Sse2.cs @@ -0,0 +1,20 @@ +namespace Org.BouncyCastle.Runtime.Intrinsics.X86 +{ + internal static class Sse2 + { +#if NETCOREAPP3_0_OR_GREATER + internal static bool IsEnabled => System.Runtime.Intrinsics.X86.Sse2.IsSupported; +#else + internal static bool IsEnabled => false; +#endif + +// internal static class X64 +// { +//#if NETCOREAPP3_0_OR_GREATER +// internal static bool IsEnabled => System.Runtime.Intrinsics.X86.Sse2.X64.IsSupported; +//#else +// internal static bool IsEnabled => false; +//#endif +// } + } +} diff --git a/crypto/src/runtime/intrinsics/x86/Sse41.cs b/crypto/src/runtime/intrinsics/x86/Sse41.cs new file mode 100644 index 000000000..7d2ccc44e --- /dev/null +++ b/crypto/src/runtime/intrinsics/x86/Sse41.cs @@ -0,0 +1,20 @@ +namespace Org.BouncyCastle.Runtime.Intrinsics.X86 +{ + internal static class Sse41 + { +#if NETCOREAPP3_0_OR_GREATER + internal static bool IsEnabled => System.Runtime.Intrinsics.X86.Sse41.IsSupported; +#else + internal static bool IsEnabled => false; +#endif + +// internal static class X64 +// { +//#if NETCOREAPP3_0_OR_GREATER +// internal static bool IsEnabled => System.Runtime.Intrinsics.X86.Sse41.X64.IsSupported; +//#else +// internal static bool IsEnabled => false; +//#endif +// } + } +} diff --git a/crypto/src/runtime/intrinsics/x86/Ssse3.cs b/crypto/src/runtime/intrinsics/x86/Ssse3.cs new file mode 100644 index 000000000..b1a849914 --- /dev/null +++ b/crypto/src/runtime/intrinsics/x86/Ssse3.cs @@ -0,0 +1,20 @@ +namespace Org.BouncyCastle.Runtime.Intrinsics.X86 +{ + internal static class Ssse3 + { +#if NETCOREAPP3_0_OR_GREATER + internal static bool IsEnabled => System.Runtime.Intrinsics.X86.Ssse3.IsSupported; +#else + internal static bool IsEnabled => false; +#endif + +// internal static class X64 +// { +//#if NETCOREAPP3_0_OR_GREATER +// internal static bool IsEnabled => System.Runtime.Intrinsics.X86.Ssse3.X64.IsSupported; +//#else +// internal static bool IsEnabled => false; +//#endif +// } + } +} -- cgit 1.4.1