-
Notifications
You must be signed in to change notification settings - Fork 3
/
BinaryXor.cs
138 lines (127 loc) · 3.88 KB
/
BinaryXor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
namespace Science.Cryptography.Ciphers.Specialized;
public static class BinaryXor
{
public static void Xor(ReadOnlySpan<byte> input, Span<byte> output, ReadOnlySpan<byte> key)
{
if (output.Length < input.Length)
{
throw new ArgumentException("Size of output buffer is insufficient.", nameof(output));
}
if (Avx2.IsSupported)
{
if (input.Length < 256 / 8)
{
BinaryXor.SlowXor(input, output, key);
}
else
{
BinaryXor.Avx2Xor256(input, output, key);
}
}
else
{
BinaryXor.SlowXor(input, output, key);
}
}
public static unsafe void Avx2Xor256(ReadOnlySpan<byte> input, Span<byte> output, ReadOnlySpan<byte> key)
{
// initialize
var length = input.Length;
int keyLength = key.Length;
int vectorLength = Vector256<byte>.Count, maxVectorLength = length / vectorLength * vectorLength;
// prefill buffer with running instances of key,
// so the right slice can be instantly selected
//
// same length:
// vector (4): 01 02 03 04
// key (4): 01 02 03 04
//
// key is longer:
// vector (4): 01 02 03 04
// key (6): 01 02 03 04 05 06 01 02 03
//
// key is shorter:
// vector (4): 01 02 03 04
// key (3): 01 02 03 01 02 03
bool isVectorLengthMultipleOfKeyLength = (vectorLength % keyLength) == 0;
bool isKeyLongerThanVector = vectorLength < keyLength;
Span<byte> keyVectorBytes = stackalloc byte[isVectorLengthMultipleOfKeyLength switch
{
true => vectorLength,
false => keyLength + vectorLength - 1,
}];
if (isVectorLengthMultipleOfKeyLength)
{
for (int keyCopyIndex = 0; keyCopyIndex < keyVectorBytes.Length; keyCopyIndex += keyLength)
{
key.CopyTo(keyVectorBytes[keyCopyIndex..]);
}
}
else if (isKeyLongerThanVector)
{
key.CopyTo(keyVectorBytes);
key[..(vectorLength - 1)].CopyTo(keyVectorBytes[keyLength..]);
}
else
{
var remaining = keyVectorBytes.Length % keyLength;
var lastPosition = keyVectorBytes.Length - remaining;
int keyCopyIndex;
for (keyCopyIndex = 0; keyCopyIndex < lastPosition; keyCopyIndex += keyLength)
{
key.CopyTo(keyVectorBytes[keyCopyIndex..]);
}
key[..remaining].CopyTo(keyVectorBytes[keyCopyIndex..]);
}
// chunks of vector size
Vector256<byte> keyVector;
Unsafe.SkipInit(out keyVector);
for (int chunkStartIndex = 0; chunkStartIndex < maxVectorLength; chunkStartIndex += vectorLength)
{
// input span as vector
ref byte pointer = ref MemoryMarshal.GetReference(input[chunkStartIndex..(chunkStartIndex + vectorLength)]);
var vector = Unsafe.As<byte, Vector256<byte>>(ref pointer);
// select the matching slice of the key bytes buffer
if (isVectorLengthMultipleOfKeyLength)
{
if (chunkStartIndex == 0)
{
ref byte keyPointer = ref MemoryMarshal.GetReference(keyVectorBytes);
keyVector = Unsafe.As<byte, Vector256<byte>>(ref keyPointer);
}
}
else
{
ref byte keyPointer = ref MemoryMarshal.GetReference(keyVectorBytes[(chunkStartIndex % keyLength)..]);
keyVector = Unsafe.As<byte, Vector256<byte>>(ref keyPointer);
}
// execute xor
var result = Avx2.Xor(vector, keyVector);
// read from vector
fixed (byte* outputPointer = &MemoryMarshal.GetReference(output[chunkStartIndex..]))
{
Avx.Store(outputPointer, result);
}
}
// process final block
if (length != maxVectorLength)
{
var remaining = length - maxVectorLength;
SlowXor(input[^remaining..], output[^remaining..], key, keyStart: maxVectorLength);
}
}
public static void SlowXor(ReadOnlySpan<byte> input, Span<byte> output, ReadOnlySpan<byte> key, int keyStart = 0)
{
int inputLength = input.Length;
int keyLength = key.Length;
for (int i = 0; i < inputLength; i++)
{
output[i] = (byte)(input[i] ^ key[(keyStart + i) % keyLength]);
}
}
}