Bytes
using System;
using System.Numerics;
using System.Runtime.InteropServices;
namespace Org.BouncyCastle.Utilities
{
public static class Bytes
{
public const int NumBits = 8;
public const int NumBytes = 1;
public static void Xor(int len, byte[] x, byte[] y, byte[] z)
{
Xor(len, x.AsSpan(0, len), y.AsSpan(0, len), z.AsSpan(0, len));
}
public static void Xor(int len, byte[] x, int xOff, byte[] y, int yOff, byte[] z, int zOff)
{
Xor(len, x.AsSpan(xOff, len), y.AsSpan(yOff, len), z.AsSpan(zOff, len));
}
public static void Xor(int len, ReadOnlySpan<byte> x, ReadOnlySpan<byte> y, Span<byte> z)
{
int i = 0;
if (Vector.IsHardwareAccelerated) {
for (int num = len - Vector<byte>.Count; i <= num; i += Vector<byte>.Count) {
int num2 = i;
Vector<byte> left = new Vector<byte>(x.Slice(num2, x.Length - num2));
num2 = i;
Vector<byte> right = new Vector<byte>(y.Slice(num2, y.Length - num2));
Vector<byte> vector = left ^ right;
num2 = i;
vector.CopyTo(z.Slice(num2, z.Length - num2));
}
}
for (int num3 = len - 8; i <= num3; i += 8) {
int num2 = i;
ulong num4 = MemoryMarshal.Read<ulong>(x.Slice(num2, x.Length - num2));
num2 = i;
ulong num5 = MemoryMarshal.Read<ulong>(y.Slice(num2, y.Length - num2));
ulong value = num4 ^ num5;
num2 = i;
MemoryMarshal.Write(z.Slice(num2, z.Length - num2), ref value);
}
for (; i < len; i++) {
z[i] = (byte)(x[i] ^ y[i]);
}
}
public static void XorTo(int len, byte[] x, byte[] z)
{
XorTo(len, x.AsSpan(0, len), z.AsSpan(0, len));
}
public static void XorTo(int len, byte[] x, int xOff, byte[] z, int zOff)
{
XorTo(len, x.AsSpan(xOff, len), z.AsSpan(zOff, len));
}
public static void XorTo(int len, ReadOnlySpan<byte> x, Span<byte> z)
{
int i = 0;
if (Vector.IsHardwareAccelerated) {
for (int num = len - Vector<byte>.Count; i <= num; i += Vector<byte>.Count) {
int num2 = i;
Vector<byte> left = new Vector<byte>(x.Slice(num2, x.Length - num2));
num2 = i;
Vector<byte> right = new Vector<byte>(z.Slice(num2, z.Length - num2));
Vector<byte> vector = left ^ right;
num2 = i;
vector.CopyTo(z.Slice(num2, z.Length - num2));
}
}
for (int num3 = len - 8; i <= num3; i += 8) {
int num2 = i;
ulong num4 = MemoryMarshal.Read<ulong>(x.Slice(num2, x.Length - num2));
num2 = i;
ulong value = MemoryMarshal.Read<ulong>(z.Slice(num2, z.Length - num2));
value ^= num4;
num2 = i;
MemoryMarshal.Write(z.Slice(num2, z.Length - num2), ref value);
}
for (; i < len; i++) {
z[i] ^= x[i];
}
}
}
}