-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathProgram.cs
375 lines (333 loc) · 11.1 KB
/
Program.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
// Copyright © Clinton Ingram and Contributors. Licensed under the MIT License.
using System;
using System.Linq;
using System.Collections.Generic;
using System.Security.Cryptography;
using BenchmarkDotNet.Filters;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Attributes;
namespace BlakeBench;
static class Paths
{
public const string DotNetCLIx64 = @"c:\program files\dotnet\dotnet.exe";
public const string DotNetCLIx86 = @"c:\program files (x86)\dotnet\dotnet.exe";
}
static class BenchConfig
{
public const int HashBytes = 5;
public static readonly byte[] Key = [ ];
//public static readonly byte[] Key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"u8.ToArray();
public static readonly List<byte[]> Data = [
"abc"u8.ToArray(),
new byte[3268].RandomFill(), // size of Windows 10 srgb.icm
new byte[1024 * 1024 * 3].RandomFill()
];
public static string ToHexString(this byte[] a) => string.Concat(a.Select(x => x.ToString("X2")));
public static byte[] RandomFill(this byte[] a)
{
new Random(42).NextBytes(a);
return a;
}
}
class Program
{
public static void Main()
{
if (!Blake2Rfc.SelftTest.blake2b_selftest() || !Blake2Rfc.SelftTest.blake2s_selftest())
{
Console.WriteLine("Error: BLAKE2 RFC self-test failed.");
return;
}
if (!Blake3Ref.SelfTest())
{
Console.WriteLine("Error: BLAKE3 ref self-test failed.");
return;
}
Console.WriteLine("""
Choose a benchmark:
0. Just test each BLAKE2 library once, don't benchmark
1. Blake2Fast vs .NET in-box algorithms (MD5 and SHA2)
2. Blake2Fast BLAKE2b vs 3rd party libraries
3. Blake2Fast BLAKE2s vs 3rd party libraries
4. Blake2Fast BLAKE3 vs 3rd party libraries
5. Blake2Fast 2.0 vs Current
6. Blake2Fast performance on multiple runtimes (check SDK paths in Program.cs)
"""
);
switch (Console.ReadKey().Key)
{
case ConsoleKey.D0:
singleRun();
break;
case ConsoleKey.D1:
BenchmarkRunner.Run<BlakeBench>(new DefaultCustomConfig().AddFilter(new AllCategoriesFilter([ "OtherHash" ])));
break;
case ConsoleKey.D2:
BenchmarkRunner.Run<BlakeBench>(new DefaultCustomConfig().AddFilter(new AllCategoriesFilter([ "Blake2b" ])));
break;
case ConsoleKey.D3:
BenchmarkRunner.Run<BlakeBench>(new DefaultCustomConfig().AddFilter(new AllCategoriesFilter([ "Blake2s" ])));
break;
case ConsoleKey.D4:
BenchmarkRunner.Run<BlakeBench>(new DefaultCustomConfig().AddFilter(new AllCategoriesFilter(["Blake3"])));
break;
case ConsoleKey.D5:
BenchmarkRunner.Run<BlakeBench>(new MultipleVersionConfig().AddFilter(new AllCategoriesFilter(["JitTest"])));
break;
case ConsoleKey.D6:
BenchmarkRunner.Run<BlakeBench>(new MultipleJitConfig().AddFilter(new AllCategoriesFilter([ "JitTest" ])));
break;
default:
Console.WriteLine("Unrecognized command.");
break;
}
}
private static void singleRun()
{
var bench = new BlakeBench();
// BLAKE2B
Console.WriteLine();
Console.WriteLine(bench.GetHashBlake2bRfc(BenchConfig.Data.Last()).ToHexString());
Console.WriteLine(bench.GetHashBlake2bFast(BenchConfig.Data.Last()).ToHexString());
Console.WriteLine(bench.GetHashBlake2Sharp(BenchConfig.Data.Last()).ToHexString());
Console.WriteLine(bench.GetHashByteTerrace2b(BenchConfig.Data.Last()).ToHexString());
Console.WriteLine(bench.GetHashDHB2(BenchConfig.Data.Last()).ToHexString());
Console.WriteLine(bench.GetHashICB2(BenchConfig.Data.Last()).ToHexString());
Console.WriteLine(bench.GetHashKSB2(BenchConfig.Data.Last()).ToHexString());
Console.WriteLine(bench.GetHashBlake2Core(BenchConfig.Data.Last()).ToHexString());
#if NET6_0_OR_GREATER
Console.WriteLine(bench.GetHashNSec(BenchConfig.Data.Last()).ToHexString()); // not RFC-compliant -- result will be all 0s when digest size < default
#endif
// BLAKE2S
Console.WriteLine();
Console.WriteLine(bench.GetHashBlake2sRfc(BenchConfig.Data.Last()).ToHexString());
Console.WriteLine(bench.GetHashBlake2sFast(BenchConfig.Data.Last()).ToHexString());
Console.WriteLine(bench.GetHash2snet(BenchConfig.Data.Last()).ToHexString());
Console.WriteLine(bench.GetHashByteTerrace2s(BenchConfig.Data.Last()).ToHexString());
#if !NUGETBENCH
// BLAKE3
Console.WriteLine();
Console.WriteLine(bench.GetHashBlake3Ref(BenchConfig.Data.Last()).ToHexString());
Console.WriteLine(bench.GetHashBlake3Fast(BenchConfig.Data.Last()).ToHexString());
#if NET7_0_OR_GREATER
Console.WriteLine(bench.GetHashBlake3Net(BenchConfig.Data.Last()).ToHexString());
#endif
#endif
}
}
public class BlakeBench
{
public static IEnumerable<byte[]> Data() => BenchConfig.Data;
/*
*
* BLAKE2B
*
*/
//[Benchmark(Description = "Blake2bRFC"), BenchmarkCategory("Blake2b")]
[ArgumentsSource(nameof(Data))]
public byte[] GetHashBlake2bRfc(byte[] data)
{
return Blake2Rfc.Blake2b.ComputeHash(BenchConfig.HashBytes, BenchConfig.Key, data);
}
[Benchmark(Description = "*Blake2Fast.Blake2b"), BenchmarkCategory("Blake2b", "JitTest")]
[ArgumentsSource(nameof(Data))]
public byte[] GetHashBlake2bFast(byte[] data)
{
#if NUGETBENCH
return Blake2Fast.Blake2b.ComputeHash(BenchConfig.HashBytes, BenchConfig.Key, data);
#else
return Blake2Fast.Blake2b.HashData(BenchConfig.HashBytes, BenchConfig.Key, data);
#endif
}
[Benchmark(Description = "Blake2Sharp"), BenchmarkCategory("Blake2b")]
[ArgumentsSource(nameof(Data))]
public byte[] GetHashBlake2Sharp(byte[] data)
{
var cfg = new Blake2Sharp.Blake2BConfig() {
OutputSizeInBytes = BenchConfig.HashBytes,
Key = BenchConfig.Key
};
return Blake2Sharp.Blake2B.ComputeHash(data, cfg);
}
[Benchmark(Description = "ByteTerrace"), BenchmarkCategory("Blake2b")]
[ArgumentsSource(nameof(Data))]
public byte[] GetHashByteTerrace2b(byte[] data)
{
using var b2b = ByteTerrace.Maths.Cryptography.Blake2b.New(BenchConfig.Key, BenchConfig.HashBytes);
return b2b.ComputeHash(data);
}
[Benchmark(Description = "S.D.HashFunction"), BenchmarkCategory("Blake2b")]
[ArgumentsSource(nameof(Data))]
public byte[] GetHashDHB2(byte[] data)
{
var cfg = new System.Data.HashFunction.Blake2.Blake2BConfig() {
HashSizeInBits = BenchConfig.HashBytes * 8,
Key = BenchConfig.Key
};
var b2 = System.Data.HashFunction.Blake2.Blake2BFactory.Instance.Create(cfg);
return b2.ComputeHash(data).Hash;
}
[Benchmark(Description = "Konscious"), BenchmarkCategory("Blake2b")]
[ArgumentsSource(nameof(Data))]
public byte[] GetHashKSB2(byte[] data)
{
using var b2 = new Konscious.Security.Cryptography.HMACBlake2B(BenchConfig.Key, BenchConfig.HashBytes * 8);
return b2.ComputeHash(data);
}
[Benchmark(Description = "Isopoh"), BenchmarkCategory("Blake2b")]
[ArgumentsSource(nameof(Data))]
public byte[] GetHashICB2(byte[] data)
{
var cfg = new Isopoh.Cryptography.Blake2b.Blake2BConfig() {
OutputSizeInBytes = BenchConfig.HashBytes,
Key = BenchConfig.Key
};
return Isopoh.Cryptography.Blake2b.Blake2B.ComputeHash(data, cfg, null);
}
[Benchmark(Description = "Blake2Core"), BenchmarkCategory("Blake2b")]
[ArgumentsSource(nameof(Data))]
public byte[] GetHashBlake2Core(byte[] data)
{
var cfg = new Blake2Core.Blake2BConfig() {
OutputSizeInBytes = BenchConfig.HashBytes,
Key = BenchConfig.Key
};
return Blake2Core.Blake2B.ComputeHash(data, cfg);
}
#if NET6_0_OR_GREATER
[Benchmark(Description = "NSec"), BenchmarkCategory("Blake2b")]
[ArgumentsSource(nameof(Data))]
public byte[] GetHashNSec(byte[] data)
{
// this implementation won't do hashes less than 256 bits, so just return 0s for shorter hashes
var b2 = new NSec.Cryptography.Blake2b(Math.Max(BenchConfig.HashBytes, 32));
var h = b2.Hash(data);
return BenchConfig.HashBytes >= 32 ? h : new byte[BenchConfig.HashBytes];
}
#endif
/*
*
* BLAKE2S
*
*/
//[Benchmark(Description = "Blake2sRFC"), BenchmarkCategory("Blake2s")]
[ArgumentsSource(nameof(Data))]
public byte[] GetHashBlake2sRfc(byte[] data)
{
return Blake2Rfc.Blake2s.ComputeHash(BenchConfig.HashBytes, BenchConfig.Key, data);
}
[Benchmark(Description = "*Blake2Fast.Blake2s"), BenchmarkCategory("Blake2s", "JitTest")]
[ArgumentsSource(nameof(Data))]
public byte[] GetHashBlake2sFast(byte[] data)
{
#if NUGETBENCH
return Blake2Fast.Blake2s.ComputeHash(BenchConfig.HashBytes, BenchConfig.Key, data);
#else
return Blake2Fast.Blake2s.HashData(BenchConfig.HashBytes, BenchConfig.Key, data);
#endif
}
[Benchmark(Description = "Blake2s-net"), BenchmarkCategory("Blake2s")]
[ArgumentsSource(nameof(Data))]
public byte[] GetHash2snet(byte[] data)
{
var cfg = new Blake2s.Blake2sConfig() {
OutputSizeInBytes = BenchConfig.HashBytes,
Key = BenchConfig.Key
};
return Blake2s.Blake2S.ComputeHash(data, cfg);
}
[Benchmark(Description = "ByteTerrace"), BenchmarkCategory("Blake2s")]
[ArgumentsSource(nameof(Data))]
public byte[] GetHashByteTerrace2s(byte[] data)
{
using var b2s = ByteTerrace.Maths.Cryptography.Blake2s.New(BenchConfig.Key, BenchConfig.HashBytes);
return b2s.ComputeHash(data);
}
#if !NUGETBENCH
/*
*
* BLAKE3
*
*/
//[Benchmark(Description = "Blake3Ref"), BenchmarkCategory("Blake3")]
[ArgumentsSource(nameof(Data))]
public byte[] GetHashBlake3Ref(byte[] data)
{
return Blake3Ref.Hash(BenchConfig.HashBytes, data, BenchConfig.Key, default);
}
[Benchmark(Description = "Blake2Fast"), BenchmarkCategory("Blake3")]
[ArgumentsSource(nameof(Data))]
public byte[] GetHashBlake3Fast(byte[] data)
{
return Blake2Fast.Blake3.HashData(BenchConfig.HashBytes, BenchConfig.Key, new ReadOnlySpan<byte>(data));
}
#if NET7_0_OR_GREATER
[Benchmark(Description = "Blake3Net"), BenchmarkCategory("Blake3")]
[ArgumentsSource(nameof(Data))]
public byte[] GetHashBlake3Net(byte[] data)
{
using var hasher = Blake3.Hasher.New();
hasher.Update(data);
return hasher.Finalize().AsSpan().Slice(0, BenchConfig.HashBytes).ToArray();
}
#endif
#endif
/*
*
* BLAKE2 vs OTHERS
*
*/
[Benchmark(Description = "BLAKE2-256"), BenchmarkCategory("OtherHash")]
[ArgumentsSource(nameof(Data))]
public byte[] GetHashBlake2s256(byte[] data)
{
#if NUGETBENCH
return Blake2Fast.Blake2s.ComputeHash(data);
#else
return Blake2Fast.Blake2s.HashData(data);
#endif
}
[Benchmark(Description = "BLAKE2-512"), BenchmarkCategory("OtherHash")]
[ArgumentsSource(nameof(Data))]
public byte[] GetHashBlake2b256(byte[] data)
{
#if NUGETBENCH
return Blake2Fast.Blake2b.ComputeHash(data);
#else
return Blake2Fast.Blake2b.HashData(data);
#endif
}
[Benchmark(Description = "MD5"), BenchmarkCategory("OtherHash")]
[ArgumentsSource(nameof(Data))]
public byte[] GetHashMD5(byte[] data)
{
#if NET5_0_OR_GREATER
return MD5.HashData(data);
#else
using var md5 = MD5.Create();
return md5.ComputeHash(data);
#endif
}
[Benchmark(Description = "SHA-256"), BenchmarkCategory("OtherHash")]
[ArgumentsSource(nameof(Data))]
public byte[] GetHashSha256(byte[] data)
{
#if NET5_0_OR_GREATER
return SHA256.HashData(data);
#else
using var sha = SHA256.Create();
return sha.ComputeHash(data);
#endif
}
[Benchmark(Description = "SHA-512"), BenchmarkCategory("OtherHash")]
[ArgumentsSource(nameof(Data))]
public byte[] GetHashSha512(byte[] data)
{
#if NET5_0_OR_GREATER
return SHA512.HashData(data);
#else
using var sha = SHA512.Create();
return sha.ComputeHash(data);
#endif
}
}