Skip to content

Commit 00b5cfe

Browse files
committed
test efficiency of span changes in pr; heap seems to gain little extra savings and the speeds probably the main target next anyway
1 parent fb0154a commit 00b5cfe

5 files changed

Lines changed: 75 additions & 6 deletions

File tree

src/BCrypt.Net/BCrypt.Net.csproj

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<AssemblyTitle>BCrypt.Net-Next</AssemblyTitle>
55
<AssemblyName>BCrypt.Net-Next</AssemblyName>
66

7-
<TargetFrameworks>netstandard2.0;netstandard2.1;net20;net35;net40;net452;net462;net472</TargetFrameworks>
7+
<TargetFrameworks>netcoreapp3.1;netstandard2.0;netstandard2.1;net20;net35;net40;net452;net462;net472</TargetFrameworks>
88
<OutputTypeEx>Library</OutputTypeEx>
99
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1010
<IncludeSource>true</IncludeSource>
@@ -28,6 +28,7 @@
2828
<Deterministic>false</Deterministic>
2929
</PropertyGroup>
3030

31+
3132
<PropertyGroup Condition="'$(TargetFramework)' == 'net20' ">
3233
<FrameworkPathOverride Condition="'$(TargetFramework)' == 'net20'">C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client</FrameworkPathOverride>
3334
</PropertyGroup>
@@ -48,10 +49,22 @@
4849
<Reference Include="System" />
4950
</ItemGroup>
5051

52+
53+
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0|net452|net462|net472'">
54+
<PackageReference Include="System.Memory" Version="4.5.3" />
55+
</ItemGroup>
56+
57+
<PropertyGroup Condition="'$(TargetFramework)' == 'netstandard2.1|netstandard2.0|net452|net462|net472'">
58+
<DefineConstants>$(DefineConstants);HAS_SPAN</DefineConstants>
59+
</PropertyGroup>
60+
61+
5162
<PropertyGroup Condition="'$(Configuration)'=='Release'">
5263
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\BCrypt.Net-Next.xml</DocumentationFile>
5364
</PropertyGroup>
5465

66+
67+
5568
<ItemGroup>
5669
<PackageReference Update="SourceLink.Create.GitHub" Version="2.8.3" />
5770
</ItemGroup>

src/BCrypt.Net/BCrypt.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,11 @@ private static int Char64(char character)
950950
/// <summary>Blowfish encipher a single 64-bit block encoded as two 32-bit halves.</summary>
951951
/// <param name="blockArray">An array containing the two 32-bit half blocks.</param>
952952
/// <param name="offset"> The position in the array of the blocks.</param>
953+
#if HAS_SPAN
954+
private void Encipher(Span<uint> blockArray, int offset)
955+
#else
953956
private void Encipher(uint[] blockArray, int offset)
957+
#endif
954958
{
955959
uint block = blockArray[offset];
956960
uint r = blockArray[offset + 1];
@@ -985,7 +989,11 @@ private void Encipher(uint[] blockArray, int offset)
985989
/// <param name="data">The string to extract the data from.</param>
986990
/// <param name="offset"> [in,out] The current offset.</param>
987991
/// <returns>The next word of material from data.</returns>
992+
#if HAS_SPAN
993+
private static uint StreamToWord(ReadOnlySpan<byte> data, ref int offset)
994+
#else
988995
private static uint StreamToWord(byte[] data, ref int offset)
996+
#endif
989997
{
990998
int i;
991999
uint word = 0;
@@ -1009,11 +1017,19 @@ private void InitializeKey()
10091017

10101018
/// <summary>Key the Blowfish cipher.</summary>
10111019
/// <param name="keyBytes">The key byte array.</param>
1020+
#if HAS_SPAN
1021+
private void Key(ReadOnlySpan<byte> keyBytes)
1022+
#else
10121023
private void Key(byte[] keyBytes)
1024+
#endif
10131025
{
10141026
int i;
10151027
int koffp = 0;
1028+
#if HAS_SPAN
1029+
Span<uint> lr = stackalloc uint[2] { 0, 0 };
1030+
#else
10161031
uint[] lr = { 0, 0 };
1032+
#endif
10171033
int plen = _p.Length, slen = _s.Length;
10181034

10191035
for (i = 0; i < plen; i++)
@@ -1043,12 +1059,20 @@ private void Key(byte[] keyBytes)
10431059
/// <param name="saltBytes"> Salt byte array.</param>
10441060
/// <param name="inputBytes">Input byte array.</param>
10451061
// ReSharper disable once InconsistentNaming
1062+
#if HAS_SPAN
1063+
private void EKSKey(ReadOnlySpan<byte> saltBytes, ReadOnlySpan<byte> inputBytes)
1064+
#else
10461065
private void EKSKey(byte[] saltBytes, byte[] inputBytes)
1066+
#endif
10471067
{
10481068
int i;
10491069
int passwordOffset = 0;
10501070
int saltOffset = 0;
1071+
#if HAS_SPAN
1072+
Span<uint> lr = stackalloc uint[2] { 0, 0 };
1073+
#else
10511074
uint[] lr = { 0, 0 };
1075+
#endif
10521076
int plen = _p.Length, slen = _s.Length;
10531077

10541078
for (i = 0; i < plen; i++)
@@ -1082,13 +1106,23 @@ private void EKSKey(byte[] saltBytes, byte[] inputBytes)
10821106
/// <param name="saltBytes"> The salt byte array to hash with.</param>
10831107
/// <param name="workFactor"> The binary logarithm of the number of rounds of hashing to apply.</param>
10841108
/// <returns>A byte array containing the hashed result.</returns>
1085-
internal byte[] CryptRaw(byte[] inputBytes, byte[] saltBytes, int workFactor)
1109+
#if HAS_SPAN
1110+
internal byte[] CryptRaw(ReadOnlySpan<byte> inputBytes, ReadOnlySpan<byte> saltBytes, int workFactor)
1111+
#else
1112+
internal byte[] CryptRaw(byte[] inputBytes, byte[] saltBytes, int workFactor)
1113+
#endif
10861114
{
10871115
int i;
10881116
int j;
10891117

1118+
#if HAS_SPAN
1119+
Span<uint> cdata = stackalloc uint[BfCryptCiphertext.Length];
1120+
BfCryptCiphertext.CopyTo(cdata);
1121+
#else
10901122
uint[] cdata = new uint[BfCryptCiphertext.Length];
10911123
Array.Copy(BfCryptCiphertext, cdata, BfCryptCiphertext.Length);
1124+
#endif
1125+
10921126
int clen = cdata.Length;
10931127

10941128
if (workFactor < MinRounds || workFactor > MaxRounds)

src/Benchmark/TestBcrypt_Hashing.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class TestBcrypt_Hashing
1313
{
1414
public IEnumerable<object[]> Data()
1515
{
16-
yield return new object[] { "", "$2a$06$DCq7YPn5Rq63x1Lad4cll.", "$2a$06$DCq7YPn5Rq63x1Lad4cll.TV4S6ytwfsfvkgY8jIucDrjc8deX1s."};
16+
yield return new object[] { "", "$2a$06$DCq7YPn5Rq63x1Lad4cll.", "$2a$06$DCq7YPn5Rq63x1Lad4cll.TV4S6ytwfsfvkgY8jIucDrjc8deX1s." };
1717
yield return new object[] { "", "$2a$08$HqWuK6/Ng6sg9gQzbLrgb.", "$2a$08$HqWuK6/Ng6sg9gQzbLrgb.Tl.ZHfXLhvt/SgVyWhQqgqcZ7ZuUtye" };
1818
yield return new object[] { "", "$2a$10$k1wbIrmNyFAPwPVPSVa/ze", "$2a$10$k1wbIrmNyFAPwPVPSVa/zecw2BCEnBwVS2GbrmgzxFUOqW9dk4TCW" };
1919
yield return new object[] { "", "$2a$12$k42ZFHFWqBp3vWli.nIn8u", "$2a$12$k42ZFHFWqBp3vWli.nIn8uYyIkbvYRvodzbfbK18SSsY.CsIQPlxO" };
@@ -37,7 +37,7 @@ public IEnumerable<object[]> Data()
3737

3838
[Benchmark(Baseline = true)]
3939
[ArgumentsSource(nameof(Data))]
40-
public string TestHashValidateEnhanced(string key, string salt, string hash)
40+
public string TestHashValidate(string key, string salt, string hash)
4141
{
4242
string hashed = BaseLine.BCrypt.HashPassword(key, salt, enhancedEntropy: false);
4343
var validateHashCheck = BaseLine.BCrypt.Verify(key, hashed);
@@ -47,12 +47,20 @@ public string TestHashValidateEnhanced(string key, string salt, string hash)
4747

4848
[Benchmark]
4949
[ArgumentsSource(nameof(Data))]
50-
public string TestHashValidateEnhancedPerf1(string key, string salt, string hash)
50+
public string TestHashValidatePerf1(string key, string salt, string hash)
5151
{
5252
string hashed = PerfMerge1.BCrypt.HashPassword(key, salt, enhancedEntropy: false);
5353
var validateHashCheck = PerfMerge1.BCrypt.Verify(key, hashed);
5454
return hashed + validateHashCheck.ToString();
55+
}
5556

57+
[Benchmark]
58+
[ArgumentsSource(nameof(Data))]
59+
public string TestHashValidateCurrent(string key, string salt, string hash)
60+
{
61+
string hashed = BCrypt.HashPassword(key, salt, enhancedEntropy: false);
62+
var validateHashCheck = BCrypt.Verify(key, hashed);
63+
return hashed + validateHashCheck.ToString();
5664
}
5765
}
5866
}

src/Benchmark/TestBcrypt_Hashing_Enhanced.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,20 @@ public string TestHashValidateEnhanced(string key, string salt, string hash)
4343
string hashed = BaseLine.BCrypt.HashPassword(key, salt, enhancedEntropy: true);
4444
var validateHashCheck = BaseLine.BCrypt.EnhancedVerify(key, hashed);
4545
return hashed + validateHashCheck.ToString();
46-
4746
}
4847

4948
[Benchmark]
5049
[ArgumentsSource(nameof(Data))]
5150
public string TestHashValidateEnhancedPerf1(string key, string salt, string hash)
51+
{
52+
string hashed = PerfMerge1.BCrypt.HashPassword(key, salt, enhancedEntropy: true);
53+
var validateHashCheck = PerfMerge1.BCrypt.EnhancedVerify(key, hashed);
54+
return hashed + validateHashCheck.ToString();
55+
}
56+
57+
[Benchmark]
58+
[ArgumentsSource(nameof(Data))]
59+
public string TestHashValidateEnhancedCurrent(string key, string salt, string hash)
5260
{
5361
string hashed = PerfMerge1.BCrypt.HashPassword(key, salt, enhancedEntropy: true);
5462
var validateHashCheck = PerfMerge1.BCrypt.EnhancedVerify(key, hashed);

src/Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS test
2+
WORKDIR /src
3+
COPY ./ ./
4+
#RUN dotnet test BCrypt.Net.UnitTests
5+
RUN dotnet build BCrypt.Net.sln -c Benchmark --framework netcoreapp3.1
6+
RUN dotnet run --project Benchmark/Benchmark.csproj -c Benchmark --framework netcoreapp3.1

0 commit comments

Comments
 (0)