diff options
author | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-10-23 20:21:30 +0700 |
---|---|---|
committer | Peter Dettman <peter.dettman@bouncycastle.org> | 2022-10-23 20:21:30 +0700 |
commit | 7c7d014750693c0ad81b5747baf1929b4cc6f021 (patch) | |
tree | 5ab42cb372a2299114a15c536b93cd3df6729aaf | |
parent | Various cleanup (diff) | |
download | BouncyCastle.NET-ed25519-7c7d014750693c0ad81b5747baf1929b4cc6f021.tar.xz |
Xor methods
-rw-r--r-- | crypto/src/math/raw/Nat256.cs | 71 | ||||
-rw-r--r-- | crypto/src/math/raw/Nat512.cs | 37 |
2 files changed, 98 insertions, 10 deletions
diff --git a/crypto/src/math/raw/Nat256.cs b/crypto/src/math/raw/Nat256.cs index 710060bee..086ba53b1 100644 --- a/crypto/src/math/raw/Nat256.cs +++ b/crypto/src/math/raw/Nat256.cs @@ -1,5 +1,11 @@ using System; using System.Diagnostics; +#if NETCOREAPP3_0_OR_GREATER +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using System.Runtime.Intrinsics; +using System.Runtime.Intrinsics.X86; +#endif using Org.BouncyCastle.Crypto.Utilities; @@ -1364,6 +1370,71 @@ namespace Org.BouncyCastle.Math.Raw return new BigInteger(1, bs); } + public static void Xor(uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff) + { +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + Xor(x.AsSpan(xOff), y.AsSpan(yOff), z.AsSpan(zOff)); +#else + for (int i = 0; i < 8; i += 4) + { + z[zOff + i + 0] = x[xOff + i + 0] ^ y[yOff + i + 0]; + z[zOff + i + 1] = x[xOff + i + 1] ^ y[yOff + i + 1]; + z[zOff + i + 2] = x[xOff + i + 2] ^ y[yOff + i + 2]; + z[zOff + i + 3] = x[xOff + i + 3] ^ y[yOff + i + 3]; + } +#endif + } + +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + public static void Xor(ReadOnlySpan<uint> x, ReadOnlySpan<uint> y, Span<uint> z) + { +#if NETCOREAPP3_0_OR_GREATER + if (Avx2.IsSupported && Unsafe.SizeOf<Vector256<byte>>() == 32) + { + var X = MemoryMarshal.Cast<uint, byte>(x[..8]); + var Y = MemoryMarshal.Cast<uint, byte>(y[..8]); + var Z = MemoryMarshal.Cast<uint, byte>(z[..8]); + + var X0 = MemoryMarshal.Read<Vector256<byte>>(X[0x00..0x20]); + var Y0 = MemoryMarshal.Read<Vector256<byte>>(Y[0x00..0x20]); + + var Z0 = Avx2.Xor(X0, Y0); + + MemoryMarshal.Write(Z[0x00..0x20], ref Z0); + return; + } + + if (Sse2.IsSupported && Unsafe.SizeOf<Vector128<byte>>() == 16) + { + var X = MemoryMarshal.Cast<uint, byte>(x[..8]); + var Y = MemoryMarshal.Cast<uint, byte>(y[..8]); + var Z = MemoryMarshal.Cast<uint, byte>(z[..8]); + + var X0 = MemoryMarshal.Read<Vector128<byte>>(X[0x00..0x10]); + var X1 = MemoryMarshal.Read<Vector128<byte>>(X[0x10..0x20]); + + var Y0 = MemoryMarshal.Read<Vector128<byte>>(Y[0x00..0x10]); + var Y1 = MemoryMarshal.Read<Vector128<byte>>(Y[0x10..0x20]); + + var Z0 = Sse2.Xor(X0, Y0); + var Z1 = Sse2.Xor(X1, Y1); + + MemoryMarshal.Write(Z[0x00..0x10], ref Z0); + MemoryMarshal.Write(Z[0x10..0x20], ref Z1); + return; + } +#endif + + for (int i = 0; i < 8; i += 4) + { + z[i + 0] = x[i + 0] ^ y[i + 0]; + z[i + 1] = x[i + 1] ^ y[i + 1]; + z[i + 2] = x[i + 2] ^ y[i + 2]; + z[i + 3] = x[i + 3] ^ y[i + 3]; + } + } +#endif + public static void Zero(uint[] z) { z[0] = 0; diff --git a/crypto/src/math/raw/Nat512.cs b/crypto/src/math/raw/Nat512.cs index 6e8c593fd..37cf0b6ff 100644 --- a/crypto/src/math/raw/Nat512.cs +++ b/crypto/src/math/raw/Nat512.cs @@ -50,12 +50,28 @@ namespace Org.BouncyCastle.Math.Raw public static void Xor(uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff) { +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + Xor(x.AsSpan(xOff), y.AsSpan(yOff), z.AsSpan(zOff)); +#else + for (int i = 0; i < 16; i += 4) + { + z[zOff + i + 0] = x[xOff + i + 0] ^ y[yOff + i + 0]; + z[zOff + i + 1] = x[xOff + i + 1] ^ y[yOff + i + 1]; + z[zOff + i + 2] = x[xOff + i + 2] ^ y[yOff + i + 2]; + z[zOff + i + 3] = x[xOff + i + 3] ^ y[yOff + i + 3]; + } +#endif + } + +#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER + public static void Xor(ReadOnlySpan<uint> x, ReadOnlySpan<uint> y, Span<uint> z) + { #if NETCOREAPP3_0_OR_GREATER if (Avx2.IsSupported && Unsafe.SizeOf<Vector256<byte>>() == 32) { - var X = MemoryMarshal.Cast<uint, byte>(x.AsSpan(xOff, 16)); - var Y = MemoryMarshal.Cast<uint, byte>(y.AsSpan(yOff, 16)); - var Z = MemoryMarshal.Cast<uint, byte>(z.AsSpan(zOff, 16)); + var X = MemoryMarshal.Cast<uint, byte>(x[..16]); + var Y = MemoryMarshal.Cast<uint, byte>(y[..16]); + var Z = MemoryMarshal.Cast<uint, byte>(z[..16]); var X0 = MemoryMarshal.Read<Vector256<byte>>(X[0x00..0x20]); var X1 = MemoryMarshal.Read<Vector256<byte>>(X[0x20..0x40]); @@ -73,9 +89,9 @@ namespace Org.BouncyCastle.Math.Raw if (Sse2.IsSupported && Unsafe.SizeOf<Vector128<byte>>() == 16) { - var X = MemoryMarshal.Cast<uint, byte>(x.AsSpan(xOff, 16)); - var Y = MemoryMarshal.Cast<uint, byte>(y.AsSpan(yOff, 16)); - var Z = MemoryMarshal.Cast<uint, byte>(z.AsSpan(zOff, 16)); + var X = MemoryMarshal.Cast<uint, byte>(x[..16]); + var Y = MemoryMarshal.Cast<uint, byte>(y[..16]); + var Z = MemoryMarshal.Cast<uint, byte>(z[..16]); var X0 = MemoryMarshal.Read<Vector128<byte>>(X[0x00..0x10]); var X1 = MemoryMarshal.Read<Vector128<byte>>(X[0x10..0x20]); @@ -102,11 +118,12 @@ namespace Org.BouncyCastle.Math.Raw for (int i = 0; i < 16; i += 4) { - z[zOff + i + 0] = x[xOff + i + 0] ^ y[yOff + i + 0]; - z[zOff + i + 1] = x[xOff + i + 1] ^ y[yOff + i + 1]; - z[zOff + i + 2] = x[xOff + i + 2] ^ y[yOff + i + 2]; - z[zOff + i + 3] = x[xOff + i + 3] ^ y[yOff + i + 3]; + z[i + 0] = x[i + 0] ^ y[i + 0]; + z[i + 1] = x[i + 1] ^ y[i + 1]; + z[i + 2] = x[i + 2] ^ y[i + 2]; + z[i + 3] = x[i + 3] ^ y[i + 3]; } } +#endif } } |