mirrored from https://www.bouncycastle.org/repositories/bc-csharp
-
Notifications
You must be signed in to change notification settings - Fork 602
Expand file tree
/
Copy pathBytes.cs
More file actions
184 lines (173 loc) · 5.46 KB
/
Copy pathBytes.cs
File metadata and controls
184 lines (173 loc) · 5.46 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
using System.Numerics;
using System.Runtime.InteropServices;
#endif
using Org.BouncyCastle.Math.Raw;
namespace Org.BouncyCastle.Utilities
{
public static class Bytes
{
public const int NumBits = 8;
public const int NumBytes = 1;
public static void CMov(int len, int cond, byte[] x, byte[] z)
{
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
CMov(len, cond, x.AsSpan(), z.AsSpan());
#else
uint m0 = Nat.CZero((uint)cond), m1 = ~m0;
for (int i = 0; i < len; ++i)
{
uint x_i = x[i], z_i = z[i];
z[i] = (byte)((z_i & m0) | (x_i & m1));
}
#endif
}
public static void CMov(int len, int cond, byte[] x, int xOff, byte[] z, int zOff)
{
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
CMov(len, cond, x.AsSpan(xOff), z.AsSpan(zOff));
#else
uint m0 = Nat.CZero((uint)cond), m1 = ~m0;
for (int i = 0; i < len; ++i)
{
uint x_i = x[xOff + i], z_i = z[zOff + i];
z[zOff + i] = (byte)((z_i & m0) | (x_i & m1));
}
#endif
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
public static void CMov(int len, int cond, ReadOnlySpan<byte> x, Span<byte> z)
{
uint m0 = Nat.CZero((uint)cond), m1 = ~m0;
for (int i = 0; i < len; ++i)
{
uint x_i = x[i], z_i = z[i];
z[i] = (byte)((z_i & m0) | (x_i & m1));
}
}
#endif
public static void Xor(int len, byte[] x, byte[] y, byte[] z)
{
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
Xor(len, x.AsSpan(0, len), y.AsSpan(0, len), z.AsSpan(0, len));
#else
for (int i = 0; i < len; ++i)
{
z[i] = (byte)(x[i] ^ y[i]);
}
#endif
}
public static void Xor(int len, byte[] x, int xOff, byte[] y, int yOff, byte[] z, int zOff)
{
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
Xor(len, x.AsSpan(xOff, len), y.AsSpan(yOff, len), z.AsSpan(zOff, len));
#else
for (int i = 0; i < len; ++i)
{
z[zOff + i] = (byte)(x[xOff + i] ^ y[yOff + i]);
}
#endif
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
public static void Xor(int len, ReadOnlySpan<byte> x, ReadOnlySpan<byte> y, Span<byte> z)
{
int i = 0;
if (Vector.IsHardwareAccelerated)
{
int limit = len - Vector<byte>.Count;
while (i <= limit)
{
var vx = new Vector<byte>(x[i..]);
var vy = new Vector<byte>(y[i..]);
(vx ^ vy).CopyTo(z[i..]);
i += Vector<byte>.Count;
}
}
{
int limit = len - 8;
while (i <= limit)
{
ulong x64 = MemoryMarshal.Read<ulong>(x[i..]);
ulong y64 = MemoryMarshal.Read<ulong>(y[i..]);
ulong z64 = x64 ^ y64;
#if NET8_0_OR_GREATER
MemoryMarshal.Write(z[i..], in z64);
#else
MemoryMarshal.Write(z[i..], ref z64);
#endif
i += 8;
}
}
{
while (i < len)
{
z[i] = (byte)(x[i] ^ y[i]);
++i;
}
}
}
#endif
public static void XorTo(int len, byte[] x, byte[] z)
{
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
XorTo(len, x.AsSpan(0, len), z.AsSpan(0, len));
#else
for (int i = 0; i < len; ++i)
{
z[i] ^= x[i];
}
#endif
}
public static void XorTo(int len, byte[] x, int xOff, byte[] z, int zOff)
{
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
XorTo(len, x.AsSpan(xOff, len), z.AsSpan(zOff, len));
#else
for (int i = 0; i < len; ++i)
{
z[zOff + i] ^= x[xOff + i];
}
#endif
}
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
public static void XorTo(int len, ReadOnlySpan<byte> x, Span<byte> z)
{
int i = 0;
if (Vector.IsHardwareAccelerated)
{
int limit = len - Vector<byte>.Count;
while (i <= limit)
{
var vx = new Vector<byte>(x[i..]);
var vz = new Vector<byte>(z[i..]);
(vx ^ vz).CopyTo(z[i..]);
i += Vector<byte>.Count;
}
}
{
int limit = len - 8;
while (i <= limit)
{
ulong x64 = MemoryMarshal.Read<ulong>(x[i..]);
ulong z64 = MemoryMarshal.Read<ulong>(z[i..]);
z64 ^= x64;
#if NET8_0_OR_GREATER
MemoryMarshal.Write(z[i..], in z64);
#else
MemoryMarshal.Write(z[i..], ref z64);
#endif
i += 8;
}
}
{
while (i < len)
{
z[i] ^= x[i];
++i;
}
}
}
#endif
}
}