Skip to content

Commit b61d92b

Browse files
committed
Argon2 FillBlock SIMD Implementation
- Argon2 FillBlock SIMD Implementation - Some Blake2BP and Blake2SP Perf Improvements
1 parent 2009e78 commit b61d92b

15 files changed

Lines changed: 1205 additions & 271 deletions

HashLib.Benchmark/Delphi/PerformanceBenchmarkConsole.dpr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ uses
106106
HlpBlake3 in '..\..\HashLib\src\Crypto\HlpBlake3.pas',
107107
HlpPBKDF2_HMACNotBuildInAdapter in '..\..\HashLib\src\KDF\HlpPBKDF2_HMACNotBuildInAdapter.pas',
108108
HlpPBKDF_Argon2NotBuildInAdapter in '..\..\HashLib\src\KDF\HlpPBKDF_Argon2NotBuildInAdapter.pas',
109+
HlpArgon2Dispatch in '..\..\HashLib\src\KDF\HlpArgon2Dispatch.pas',
109110
HlpArgon2TypeAndVersion in '..\..\HashLib\src\KDF\HlpArgon2TypeAndVersion.pas',
110111
HlpPBKDF_ScryptNotBuildInAdapter in '..\..\HashLib\src\KDF\HlpPBKDF_ScryptNotBuildInAdapter.pas',
111112
HlpConverters in '..\..\HashLib\src\Utils\HlpConverters.pas',

HashLib.Benchmark/Delphi/PerformanceBenchmarkFMX.dpr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ uses
105105
HlpBlake3 in '..\..\HashLib\src\Crypto\HlpBlake3.pas',
106106
HlpPBKDF2_HMACNotBuildInAdapter in '..\..\HashLib\src\KDF\HlpPBKDF2_HMACNotBuildInAdapter.pas',
107107
HlpPBKDF_Argon2NotBuildInAdapter in '..\..\HashLib\src\KDF\HlpPBKDF_Argon2NotBuildInAdapter.pas',
108+
HlpArgon2Dispatch in '..\..\HashLib\src\KDF\HlpArgon2Dispatch.pas',
108109
HlpArgon2TypeAndVersion in '..\..\HashLib\src\KDF\HlpArgon2TypeAndVersion.pas',
109110
HlpPBKDF_ScryptNotBuildInAdapter in '..\..\HashLib\src\KDF\HlpPBKDF_ScryptNotBuildInAdapter.pas',
110111
HlpConverters in '..\..\HashLib\src\Utils\HlpConverters.pas',

HashLib.Benchmark/src/Core/uPerformanceBenchmark.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ TPerformanceBenchmark = class sealed(TObject)
2020
strict private
2121

2222
class function Calculate(const AHashInstance: IHash;
23-
const ANamePrefix: String = ''; ASize: Int32 = 64 * 1024): String;
23+
const ANamePrefix: String = ''; ASize: Int32 = 1024 * 1024): String;
2424

2525
class constructor PerformanceBenchmark();
2626

HashLib.Tests/Delphi.Tests/HashLib.Tests.dpr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ uses
127127
HlpBlake3 in '..\..\HashLib\src\Crypto\HlpBlake3.pas',
128128
HlpPBKDF2_HMACNotBuildInAdapter in '..\..\HashLib\src\KDF\HlpPBKDF2_HMACNotBuildInAdapter.pas',
129129
HlpPBKDF_Argon2NotBuildInAdapter in '..\..\HashLib\src\KDF\HlpPBKDF_Argon2NotBuildInAdapter.pas',
130+
HlpArgon2Dispatch in '..\..\HashLib\src\KDF\HlpArgon2Dispatch.pas',
130131
HlpArgon2TypeAndVersion in '..\..\HashLib\src\KDF\HlpArgon2TypeAndVersion.pas',
131132
HlpPBKDF_ScryptNotBuildInAdapter in '..\..\HashLib\src\KDF\HlpPBKDF_ScryptNotBuildInAdapter.pas',
132133
HlpConverters in '..\..\HashLib\src\Utils\HlpConverters.pas',

HashLib/src/Crypto/HlpBlake2B.pas

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ TBlake2B = class(THash, ICryptoNotBuildIn, ITransformBlock)
6969
const ATreeConfig: IBlake2BTreeConfig;
7070
ADoTransformKeyBlock: Boolean = True); overload;
7171
procedure Initialize; override;
72+
procedure TransformBlock(ABlock: PByte);
7273
procedure TransformBytes(const AData: THashLibByteArray;
7374
AIndex, ADataLength: Int32); override;
7475
function TransformFinal: IHashResult; override;
@@ -375,6 +376,18 @@ procedure TBlake2B.Initialize;
375376
end;
376377
end;
377378

379+
procedure TBlake2B.TransformBlock(ABlock: PByte);
380+
begin
381+
if FFilledBufferCount = BlockSizeInBytes then
382+
begin
383+
Blake2BIncrementCounter(UInt64(BlockSizeInBytes));
384+
Compress(PByte(FBuffer), 0);
385+
FFilledBufferCount := 0;
386+
end;
387+
Blake2BIncrementCounter(UInt64(BlockSizeInBytes));
388+
Compress(ABlock, 0);
389+
end;
390+
378391
procedure TBlake2B.TransformBytes(const AData: THashLibByteArray;
379392
AIndex, ADataLength: Int32);
380393
var

HashLib/src/Crypto/HlpBlake2BP.pas

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -231,28 +231,17 @@ procedure TBlake2BP.Initialize;
231231
procedure TBlake2BP.ProcessLeafLane(AIdx: Int32; APtrData: PByte;
232232
ADataLength: UInt64);
233233
var
234-
LLeafHashes: THashLibGenericArray<TBlake2B>;
235-
LTemp: THashLibByteArray;
236-
LCounter: UInt64;
237234
LPtrData: PByte;
235+
LCounter: UInt64;
238236
begin
239-
System.SetLength(LTemp, BlockSizeInBytes);
237+
LPtrData := APtrData + AIdx * BlockSizeInBytes;
238+
LCounter := ADataLength;
240239

241-
LPtrData := APtrData;
242-
LCounter := ADataLength;
243-
LLeafHashes := FLeafHashes;
244-
245-
// Start at lane offset
246-
System.Inc(LPtrData, AIdx * BlockSizeInBytes);
247-
248-
// Process all full "stripes" of ParallelismDegree * BlockSizeInBytes
249240
while (LCounter >= StripeSize) do
250241
begin
251-
System.Move(LPtrData^, LTemp[0], BlockSizeInBytes);
252-
LLeafHashes[AIdx].TransformBytes(LTemp, 0, BlockSizeInBytes);
253-
254-
System.Inc(LPtrData, UInt64(StripeSize));
255-
LCounter := LCounter - UInt64(StripeSize);
242+
FLeafHashes[AIdx].TransformBlock(LPtrData);
243+
System.Inc(LPtrData, StripeSize);
244+
LCounter := LCounter - StripeSize;
256245
end;
257246
end;
258247

HashLib/src/Crypto/HlpBlake2S.pas

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ TBlake2S = class(THash, ICryptoNotBuildIn, ITransformBlock)
6969
const ATreeConfig: IBlake2STreeConfig;
7070
ADoTransformKeyBlock: Boolean = True); overload;
7171
procedure Initialize; override;
72+
procedure TransformBlock(ABlock: PByte);
7273
procedure TransformBytes(const AData: THashLibByteArray;
7374
AIndex, ADataLength: Int32); override;
7475
function TransformFinal: IHashResult; override;
@@ -373,6 +374,18 @@ procedure TBlake2S.Initialize;
373374
end;
374375
end;
375376

377+
procedure TBlake2S.TransformBlock(ABlock: PByte);
378+
begin
379+
if FFilledBufferCount = BlockSizeInBytes then
380+
begin
381+
Blake2SIncrementCounter(UInt32(BlockSizeInBytes));
382+
Compress(PByte(FBuffer), 0);
383+
FFilledBufferCount := 0;
384+
end;
385+
Blake2SIncrementCounter(UInt32(BlockSizeInBytes));
386+
Compress(ABlock, 0);
387+
end;
388+
376389
procedure TBlake2S.TransformBytes(const AData: THashLibByteArray;
377390
AIndex, ADataLength: Int32);
378391
var

HashLib/src/Crypto/HlpBlake2SP.pas

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -231,28 +231,17 @@ procedure TBlake2SP.Initialize;
231231
procedure TBlake2SP.ProcessLeafLane(AIdx: Int32; APtrData: PByte;
232232
ADataLength: UInt64);
233233
var
234-
LLeafHashes: THashLibGenericArray<TBlake2S>;
235-
LTemp: THashLibByteArray;
236-
LCounter: UInt64;
237234
LPtrData: PByte;
235+
LCounter: UInt64;
238236
begin
239-
System.SetLength(LTemp, BlockSizeInBytes);
237+
LPtrData := APtrData + AIdx * BlockSizeInBytes;
238+
LCounter := ADataLength;
240239

241-
LPtrData := APtrData;
242-
LCounter := ADataLength;
243-
LLeafHashes := FLeafHashes;
244-
245-
// Start at lane offset
246-
System.Inc(LPtrData, AIdx * BlockSizeInBytes);
247-
248-
// Process all full "stripes" of ParallelismDegree * BlockSizeInBytes
249240
while (LCounter >= StripeSize) do
250241
begin
251-
System.Move(LPtrData^, LTemp[0], BlockSizeInBytes);
252-
LLeafHashes[AIdx].TransformBytes(LTemp, 0, BlockSizeInBytes);
253-
254-
System.Inc(LPtrData, UInt64(StripeSize));
255-
LCounter := LCounter - UInt64(StripeSize);
242+
FLeafHashes[AIdx].TransformBlock(LPtrData);
243+
System.Inc(LPtrData, StripeSize);
244+
LCounter := LCounter - StripeSize;
256245
end;
257246
end;
258247

0 commit comments

Comments
 (0)