Selaa lähdekoodia

Merge branch 'master' of https://github.com/PascalCoin/PascalCoin

PascalCoin 7 vuotta sitten
vanhempi
commit
e2f341350e

+ 4 - 8
src/core/URandomHash.pas

@@ -165,8 +165,8 @@ type
       function Checksum(const AInput: TArray<TBytes>): UInt32; overload; inline;
       function Hash(const ABlockHeader: TBytes; ARound: Int32) : TArray<TBytes>; overload;
     public
-      property CachedHeader : TBytes read GetCachedHeader;
-      property CachedNonce : UInt32 read FCachedNonce;
+      property NextHeader : TBytes read GetCachedHeader;
+      property NextNonce : UInt32 read FCachedNonce;
       constructor Create;
       destructor Destroy; override;
       function Hash(const ABlockHeader: TBytes): TBytes; overload; inline;
@@ -694,12 +694,8 @@ end;
 
 function TRandomHashFast.Checksum(const AInput: TBytes; AOffset, ALength: Integer): UInt32;
 begin
-   if AOffset = 0 then
-     FMurmurHash3_x86_32.TransformBytes(AInput, AOffset, ALength)
-   else
-     //TODO: Fix MurMur3 implementation in HashLib4Pascal to support non-zero offset
-     FMurmurHash3_x86_32.TransformBytes(TArrayTool<Byte>.Copy(AInput, AOffset, ALength), 0, ALength);
-   Result := FMurmurHash3_x86_32.TransformFinal.GetUInt32();
+  FMurmurHash3_x86_32.TransformBytes(AInput, AOffset, ALength);
+  Result := FMurmurHash3_x86_32.TransformFinal.GetUInt32();
 end;
 
 function TRandomHashFast.Checksum(const AInput : TArray<TBytes>): UInt32;

+ 8 - 8
src/libraries/hashlib4pascal/HlpConverters.pas

@@ -72,16 +72,16 @@ type
     class function ReadUInt64AsBytesLE(a_in: UInt64): THashLibByteArray;
       overload; static; inline;
 
-    class procedure ReadUInt32AsBytesLE(a_in: UInt32; a_out: THashLibByteArray;
+    class procedure ReadUInt32AsBytesLE(a_in: UInt32; const a_out: THashLibByteArray;
       a_index: Int32); overload; static; inline;
 
-    class procedure ReadUInt32AsBytesBE(a_in: UInt32; a_out: THashLibByteArray;
+    class procedure ReadUInt32AsBytesBE(a_in: UInt32; const a_out: THashLibByteArray;
       a_index: Int32); overload; static; inline;
 
-    class procedure ReadUInt64AsBytesLE(a_in: UInt64; a_out: THashLibByteArray;
+    class procedure ReadUInt64AsBytesLE(a_in: UInt64; const a_out: THashLibByteArray;
       a_index: Int32); overload; static; inline;
 
-    class procedure ReadUInt64AsBytesBE(a_in: UInt64; a_out: THashLibByteArray;
+    class procedure ReadUInt64AsBytesBE(a_in: UInt64; const a_out: THashLibByteArray;
       a_index: Int32); overload; static; inline;
 
     class function ConvertStringToBytes(const a_in: String;
@@ -298,7 +298,7 @@ begin
 end;
 
 class procedure TConverters.ReadUInt32AsBytesLE(a_in: UInt32;
-  a_out: THashLibByteArray; a_index: Int32);
+  const a_out: THashLibByteArray; a_index: Int32);
 begin
   a_out[a_index] := Byte(a_in);
   a_out[a_index + 1] := Byte(a_in shr 8);
@@ -307,7 +307,7 @@ begin
 end;
 
 class procedure TConverters.ReadUInt32AsBytesBE(a_in: UInt32;
-  a_out: THashLibByteArray; a_index: Int32);
+  const a_out: THashLibByteArray; a_index: Int32);
 begin
   a_out[a_index] := Byte(a_in shr 24);
   a_out[a_index + 1] := Byte(a_in shr 16);
@@ -316,7 +316,7 @@ begin
 end;
 
 class procedure TConverters.ReadUInt64AsBytesLE(a_in: UInt64;
-  a_out: THashLibByteArray; a_index: Int32);
+  const a_out: THashLibByteArray; a_index: Int32);
 begin
   a_out[a_index] := Byte(a_in);
   a_out[a_index + 1] := Byte(a_in shr 8);
@@ -329,7 +329,7 @@ begin
 end;
 
 class procedure TConverters.ReadUInt64AsBytesBE(a_in: UInt64;
-  a_out: THashLibByteArray; a_index: Int32);
+  const a_out: THashLibByteArray; a_index: Int32);
 begin
   a_out[a_index] := Byte(a_in shr 56);
   a_out[a_index + 1] := Byte(a_in shr 48);

+ 4 - 0
src/libraries/hashlib4pascal/HlpMurmurHash3_x64_128.pas

@@ -449,6 +449,10 @@ begin
     begin
       ProcessPendings;
     end;
+  end
+  else
+  begin
+    i := 0;
   end;
 
   nBlocks := len shr 4;

+ 4 - 0
src/libraries/hashlib4pascal/HlpMurmurHash3_x86_128.pas

@@ -493,6 +493,10 @@ begin
     begin
       ProcessPendings;
     end;
+  end
+  else
+  begin
+    i := 0;
   end;
 
   nBlocks := len shr 4;

+ 4 - 3
src/libraries/hashlib4pascal/HlpMurmurHash3_x86_32.pas

@@ -232,7 +232,6 @@ begin
       ...
       idx = 2, len = 6 -> [0, 2[ + [0, 2[ => Block = [2,6[, buf []
       * }
-
 {$IFDEF DEBUG}
     System.Assert(a_index = 0); // nothing would work anyways if a_index is !=0
 {$ENDIF DEBUG}
@@ -250,6 +249,10 @@ begin
       TransformUInt32Fast(k);
       Fm_idx := 0;
     end;
+  end
+  else
+  begin
+    i := 0;
   end;
 
   nBlocks := len shr 2;
@@ -260,7 +263,6 @@ begin
   while i < nBlocks do
   begin
     k := TConverters.ReadBytesAsUInt32LE(ptr_a_data, a_index + (i * 4));
-
     TransformUInt32Fast(k);
 
     System.Inc(i);
@@ -270,7 +272,6 @@ begin
   offset := a_index + (i * 4);
   while offset < (len + a_index) do
   begin
-
     ByteUpdate(a_data[offset]);
     System.Inc(offset);
 

+ 4 - 0
src/libraries/hashlib4pascal/HlpSipHash.pas

@@ -323,6 +323,10 @@ begin
       ProcessBlock(m);
       Fm_idx := 0;
     end;
+  end
+  else
+  begin
+    i := 0;
   end;
 
   iter := Length shr 3;

+ 7 - 0
src/tests/PascalCoinUnitTests.lpi

@@ -105,6 +105,13 @@
         <OptimizationLevel Value="4"/>
       </Optimizations>
     </CodeGeneration>
+    <Linking>
+      <Options>
+        <Win32>
+          <GraphicApplication Value="True"/>
+        </Win32>
+      </Options>
+    </Linking>
     <Other>
       <CustomOptions Value="-dUNITTESTS"/>
     </Other>

+ 37 - 6
src/tests/URandomHashTests.pas

@@ -71,6 +71,7 @@ type
     procedure TestRandomHash;
     procedure TestRandomHash_CachedHeaderConsistency;
     procedure TestRandomHash_NonceOptimization;
+    procedure TestRandomHash_OptimalNonceSet;
     procedure TestExpand;
     procedure TestCompress;
     procedure TestChecksum_1;
@@ -143,6 +144,19 @@ const
     (Input: 200; Expected: '0x488bdfcf90f4b5c0803691562bb30604e9c3b39ed37e8eea9957ed8ae12dec26')
   );
 
+  DATA_RANDOMHASH_OPTIMAL_NONCESET : array[1..100] of UInt32 = (
+    { These is optimal nonceset for CPU optimized mining starting with DATA_BYTES as initial block header }
+    3069945400, 2894709675, 1561269480, 3709359162, 2319787398, 3469273288, 2165748579, 410191319, 3608549958, 1974915349, 2363626475,
+    2548419799, 1823243182, 2294194608, 2811822650, 1012595382, 1092960857, 2782130538, 1982611689, 3100121871, 2270918976, 558577795,
+    2602811692, 4159717317, 785699107, 2186194385, 1034122923, 3839875076, 1682247977, 3676810924, 1273798997, 1148982166, 2734953558,
+    1415853762, 4205729760, 765141560, 20322278, 31405433, 1372749894, 2429062937, 1922093937, 3803474393, 2563777622, 3993339204,
+    1157535065, 446159258, 3194232545, 4126577194, 1870725135, 1160276278, 1428921085, 2867908339, 1946859222, 528575112, 379687846,
+    3828220365, 1373182914, 3161692348, 2095512829, 1380741873, 3907258631, 3555518702, 526588200, 4063802913, 1155384674, 3931593099,
+    401714470, 3665001943, 3278099097, 354142354, 3141798166, 1380691927, 2643014168, 204089028, 472568518, 91687646, 1852951886, 4191405957,
+    1144622813, 1964089047, 1983711164, 982279936, 518506898, 3755482421, 1146753313, 2929206712, 890294739, 1916889493, 2887125739, 1767848779,
+    4220416848, 820754393, 2797119783, 931262717, 3806560848, 1898763983, 3316775846, 2509067536, 3600478831, 3997502592
+  );
+
   DATA_EXPAND : array[1..48] of TTestItem<UInt32, UInt32, UInt32> = (
     {Input1 = bytes to take from DATA_BYTES, Input2 = expansion factor, Expected = MurMur3(EXPANSION)}
     (Input1: 17;  Input2: 0; Expected: 3935905087),
@@ -1107,8 +1121,8 @@ begin
   for LCase in DATA_RANDOMHASH do begin
     LInput := TArrayTool<byte>.Copy(ParseBytes(DATA_BYTES), 0, LCase.Input);
     LOutput := LOptimized.Hash(LInput);
-    LNonce := LOptimized.GetNonce(LOptimized.CachedHeader);
-    AssertEquals(LNonce, LOptimized.CachedNonce);
+    LNonce := LOptimized.GetNonce(LOptimized.NextHeader);
+    AssertEquals(LNonce, LOptimized.NextNonce);
   end;
 end;
 
@@ -1130,13 +1144,30 @@ begin
     LOutput := LOptimized.Hash(LInput);
 
     // Test consistency of cached nonce hash with reference impl
-    LInput := LOptimized.CachedHeader;
+    LInput := LOptimized.NextHeader;
 
     // Test reference hash of cached header same as optimized
     AssertEquals(LReference.Hash(LInput), LOptimized.Hash(LInput));
   end;
 end;
 
+procedure TRandomHashFastTest.TestRandomHash_OptimalNonceSet;
+var
+  LExpectedNextNonce : UInt32;
+  LBuff : TBytes;
+  LHasher : TRandomHashFast;
+  LDisposables : TDisposables;
+begin
+  LBuff := ParseBytes(DATA_BYTES);
+  LHasher := LDisposables.AddObject( TRandomHashFast.Create ) as TRandomHashFast;
+  LHasher.Hash(LBuff);
+  for LExpectedNextNonce in DATA_RANDOMHASH_OPTIMAL_NONCESET do begin
+    AssertEquals(LExpectedNextNonce, LHasher.NextNonce);
+    //Write(Format(', %u', [LHasher.NextNonce]));
+    LBuff := LHasher.Hash(LHasher.NextHeader);
+  end;
+end;
+
 procedure TRandomHashFastTest.TestExpand;
 var
   LCase : TTestItem<UInt32, UInt32, UInt32>;
@@ -1482,7 +1513,7 @@ begin
   LHasher := LDisposables.AddObject( TRandomHashFast.Create ) as TRandomHashFast;
   LHasher.Hash(LBuff);
   for i := 1 to Pred(NUM_ITER) do
-    LBuff := LHasher.Hash(LHasher.CachedHeader);
+    LBuff := LHasher.Hash(LHasher.NextHeader);
   // no exceptions should occur
 end;
 
@@ -1491,11 +1522,11 @@ initialization
 {$IFDEF FPC}
   RegisterTest(TRandomHashTest);
   RegisterTest(TRandomHashFastTest);
-  RegisterTest(TRandomHashStressTest);
+  //RegisterTest(TRandomHashStressTest);
 {$ELSE}
   TDUnitX.RegisterTextFixture(TRandomHashTest);
   TDUnitX.RegisterTextFixture(TRandomHashFastTest);
-  TDUnitX.RegisterTextFixture(TRandomHashStressTest);
+  //TDUnitX.RegisterTextFixture(TRandomHashStressTest);
 {$ENDIF FPC}
 
 end.