From 7c7d014750693c0ad81b5747baf1929b4cc6f021 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Sun, 23 Oct 2022 20:21:30 +0700 Subject: Xor methods --- crypto/src/math/raw/Nat256.cs | 71 +++++++++++++++++++++++++++++++++++++++++++ crypto/src/math/raw/Nat512.cs | 37 ++++++++++++++++------ 2 files changed, 98 insertions(+), 10 deletions(-) (limited to 'crypto/src/math/raw') 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 x, ReadOnlySpan y, Span z) + { +#if NETCOREAPP3_0_OR_GREATER + if (Avx2.IsSupported && Unsafe.SizeOf>() == 32) + { + var X = MemoryMarshal.Cast(x[..8]); + var Y = MemoryMarshal.Cast(y[..8]); + var Z = MemoryMarshal.Cast(z[..8]); + + var X0 = MemoryMarshal.Read>(X[0x00..0x20]); + var Y0 = MemoryMarshal.Read>(Y[0x00..0x20]); + + var Z0 = Avx2.Xor(X0, Y0); + + MemoryMarshal.Write(Z[0x00..0x20], ref Z0); + return; + } + + if (Sse2.IsSupported && Unsafe.SizeOf>() == 16) + { + var X = MemoryMarshal.Cast(x[..8]); + var Y = MemoryMarshal.Cast(y[..8]); + var Z = MemoryMarshal.Cast(z[..8]); + + var X0 = MemoryMarshal.Read>(X[0x00..0x10]); + var X1 = MemoryMarshal.Read>(X[0x10..0x20]); + + var Y0 = MemoryMarshal.Read>(Y[0x00..0x10]); + var Y1 = MemoryMarshal.Read>(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 x, ReadOnlySpan y, Span z) + { #if NETCOREAPP3_0_OR_GREATER if (Avx2.IsSupported && Unsafe.SizeOf>() == 32) { - var X = MemoryMarshal.Cast(x.AsSpan(xOff, 16)); - var Y = MemoryMarshal.Cast(y.AsSpan(yOff, 16)); - var Z = MemoryMarshal.Cast(z.AsSpan(zOff, 16)); + var X = MemoryMarshal.Cast(x[..16]); + var Y = MemoryMarshal.Cast(y[..16]); + var Z = MemoryMarshal.Cast(z[..16]); var X0 = MemoryMarshal.Read>(X[0x00..0x20]); var X1 = MemoryMarshal.Read>(X[0x20..0x40]); @@ -73,9 +89,9 @@ namespace Org.BouncyCastle.Math.Raw if (Sse2.IsSupported && Unsafe.SizeOf>() == 16) { - var X = MemoryMarshal.Cast(x.AsSpan(xOff, 16)); - var Y = MemoryMarshal.Cast(y.AsSpan(yOff, 16)); - var Z = MemoryMarshal.Cast(z.AsSpan(zOff, 16)); + var X = MemoryMarshal.Cast(x[..16]); + var Y = MemoryMarshal.Cast(y[..16]); + var Z = MemoryMarshal.Cast(z[..16]); var X0 = MemoryMarshal.Read>(X[0x00..0x10]); var X1 = MemoryMarshal.Read>(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 } } -- cgit 1.4.1