From 6b5d68e8a8d1991e15873ff1e0615d87f3eb7eec Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Sat, 12 Nov 2022 22:41:39 +0700 Subject: Refactoring in Pqc.Crypto.Cmce --- crypto/src/math/ec/custom/sec/SecT131Field.cs | 2 +- crypto/src/math/raw/Interleave.cs | 40 ++++++++++++++++++--------- 2 files changed, 28 insertions(+), 14 deletions(-) (limited to 'crypto/src/math') 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) -- cgit 1.4.1