-
Notifications
You must be signed in to change notification settings - Fork 272
/
Perf.FixedTimeEquals.cs
96 lines (76 loc) · 4.1 KB
/
Perf.FixedTimeEquals.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using BenchmarkDotNet.Attributes;
using MicroBenchmarks;
using Test.Cryptography;
namespace System.Security.Cryptography.Primitives.Tests.Performance
{
[BenchmarkCategory(Categories.Libraries)]
public class Perf_FixedTimeEquals
{
byte[] baseValue, errorVector;
[GlobalSetup(Target = nameof(FixedTimeEquals_256Bit_Equal))]
public void Setup_Equal()
=> Setup(
"741202531e19d673ad7fff334594549e7c81a285dd02865ddd12530612a96336",
"0000000000000000000000000000000000000000000000000000000000000000");
[Benchmark]
public bool FixedTimeEquals_256Bit_Equal() => CryptographicOperations.FixedTimeEquals(baseValue, errorVector);
[GlobalSetup(Target = nameof(FixedTimeEquals_256Bit_LastBitDifferent))]
public void Setup_LastBitDifferent()
=> Setup(
"741202531e19d673ad7fff334594549e7c81a285dd02865ddd12530612a96336",
"0000000000000000000000000000000000000000000000000000000000000001");
[Benchmark]
public bool FixedTimeEquals_256Bit_LastBitDifferent() => CryptographicOperations.FixedTimeEquals(baseValue, errorVector);
[GlobalSetup(Target = nameof(FixedTimeEquals_256Bit_FirstBitDifferent))]
public void Setup_FirstBitDifferent()
=> Setup(
"741202531e19d673ad7fff334594549e7c81a285dd02865ddd12530612a96336",
"8000000000000000000000000000000000000000000000000000000000000000");
[Benchmark]
public bool FixedTimeEquals_256Bit_FirstBitDifferent() => CryptographicOperations.FixedTimeEquals(baseValue, errorVector);
[GlobalSetup(Target = nameof(FixedTimeEquals_256Bit_CascadingErrors))]
public void Setup_CascadingErrors()
=> Setup(
"741202531e19d673ad7fff334594549e7c81a285dd02865ddd12530612a96336",
"0102040810204080112244880000000000000000000000000000000000000000");
[Benchmark]
public bool FixedTimeEquals_256Bit_CascadingErrors() => CryptographicOperations.FixedTimeEquals(baseValue, errorVector);
[GlobalSetup(Target = nameof(FixedTimeEquals_256Bit_AllBitsDifferent))]
public void Setup_AllBitsDifferent()
=> Setup(
"741202531e19d673ad7fff334594549e7c81a285dd02865ddd12530612a96336",
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
[Benchmark]
public bool FixedTimeEquals_256Bit_AllBitsDifferent() => CryptographicOperations.FixedTimeEquals(baseValue, errorVector);
[GlobalSetup(Target = nameof(FixedTimeEquals_256Bit_VersusZero))]
public void Setup_VersusZero()
=> Setup(
"741202531e19d673ad7fff334594549e7c81a285dd02865ddd12530612a96336",
"741202531e19d673ad7fff334594549e7c81a285dd02865ddd12530612a96336");
[Benchmark]
public bool FixedTimeEquals_256Bit_VersusZero() => CryptographicOperations.FixedTimeEquals(baseValue, errorVector);
[GlobalSetup(Target = nameof(FixedTimeEquals_256Bit_SameReference))]
public void Setup_SameReference()
=> baseValue = "741202531e19d673ad7fff334594549e7c81a285dd02865ddd12530612a96336".HexToByteArray();
[Benchmark]
public bool FixedTimeEquals_256Bit_SameReference() => CryptographicOperations.FixedTimeEquals(baseValue, baseValue);
private void Setup(string baseValueHex, string errorVectorHex)
{
if (errorVectorHex.Length != baseValueHex.Length)
{
throw new InvalidOperationException();
}
byte[] a = baseValueHex.HexToByteArray();
byte[] b = errorVectorHex.HexToByteArray();
for (int i = 0; i < a.Length; i++)
{
b[i] ^= a[i];
}
baseValue = a;
errorVector = b;
}
}
}