瀏覽代碼

RandomHash2: new algorithm candidate

Herman Schoenfeld 6 年之前
父節點
當前提交
44c12a1356

+ 1 - 1
src/core/UCrypto.pas

@@ -826,7 +826,7 @@ begin
   if AHasher.HasCachedHash AND BytesEqual(AHasher.PeekCachedHash.Header, LInput) then
     LResult := AHasher.PopCachedHash.Hash
   else
-    LResult := AHasher.Hash(LInput);
+    LResult := AHasher.Compute(LInput); //AHasher.Hash(LInput);
 
   Move(LResult[0], ResultSha256[Low(ResultSha256)], 32);
 end;

+ 12 - 4
src/core/UPoolMinerThreads.pas

@@ -157,6 +157,7 @@ Type
   private
     FCPUDeviceThread : TCPUDeviceThread;
     FLock : TCriticalSection;
+    FJobNum : Integer;
   protected
     FCurrentMinerValuesForWork : TMinerValuesForWork;
     FInternalSha256 : TSHA256HASH;
@@ -748,6 +749,7 @@ begin
       cpu := TCPUOpenSSLMinerThread(l[i]);
       cpu.FLock.Acquire;
       try
+        Inc(cpu.FJobNum);
         cpu.FCurrentMinerValuesForWork := FMinerValuesForWork;
         cpu.FInternalSha256 := sflc;
         cpu.FInternalChunk := lc;
@@ -786,11 +788,11 @@ end;
 procedure TCPUOpenSSLMinerThread.BCExecute;
 Var
   ts : Cardinal;
-  i,roundsToDo : Integer;
+  i,roundsToDo, LRoundsPerformed : Integer;
   nonce : Cardinal;
   baseRealTC,baseHashingTC,finalHashingTC : TTickCount;
   resultPoW : TRawBytes;
-  //
+  LRoundJobNum : Integer;
   AuxStats : TMinerStats;
   dstep : Integer;
   LUseRandomHash : boolean;
@@ -820,7 +822,7 @@ begin
             if FCurrentMinerValuesForWork.version < CT_PROTOCOL_5 then
               roundsToDo := 20
             else
-              roundsToDo := 20;
+              roundsToDo := 200;
           end else begin
             roundsToDo := 10000;
           end;
@@ -841,7 +843,11 @@ begin
               FDigestStreamMsg.Write(ts,4);
               baseHashingTC:=TPlatform.GetTickCount;
               dstep := 4;
+              LRoundJobNum := FJobNum;
+              LRoundsPerformed := 0;
               for i := 1 to roundsToDo do begin
+                if LRoundJobNum <> FJobNum then
+                  break;
                 FDigestStreamMsg.Position := FDigestStreamMsg.Size - 4;
                 FDigestStreamMsg.Write(nonce,4);
                 if LUseRandomHash then begin
@@ -855,6 +861,7 @@ begin
                 end else begin
                   TCrypto.DoDoubleSha256(FDigestStreamMsg.Memory,FDigestStreamMsg.Size,resultPoW);
                 end;
+                Inc(LRoundsPerformed);
                 if (TBaseType.BinStrComp(resultPoW,FCurrentMinerValuesForWork.target_pow)<0) then begin
                   if (Terminated) Or (FCPUDeviceThread.Terminated) then exit;
                   dstep := 5;
@@ -897,7 +904,7 @@ begin
               finalHashingTC:=TPlatform.GetTickCount;
             end;
             AuxStats.Miners:=FCPUDeviceThread.FCPUs;
-            AuxStats.RoundsCount:=roundsToDo;
+            AuxStats.RoundsCount:=LRoundsPerformed;
             AuxStats.WorkingMillisecondsTotal:=TPlatform.GetTickCount - baseRealTC;
             AuxStats.WorkingMillisecondsHashing:= finalHashingTC - baseHashingTC;
             dstep := 9;
@@ -925,6 +932,7 @@ begin
   FDigestStreamMsg := TMemoryStream.Create;
   FMinNOnce := 0; FMaxNOnce:=$FFFFFFFF;
   FResetNOnce:=True;
+  FJobNum := 0;
   inherited Create(false);
 end;
 

+ 3 - 1
src/core/UPoolMining.pas

@@ -837,7 +837,9 @@ begin
             TLog.NewLog(ltInfo,ClassName,sJobInfo+' - Found a solution for block '+IntToStr(nbOperations.OperationBlock.block));
           end else TLog.NewLog(lterror,ClassName,sJobInfo+Format(' Calculated Pow > Min PoW ->  %s > %s',
             [TCrypto.ToHexaString(P^.OperationsComp.OperationBlock.proof_of_work),TCrypto.ToHexaString(_targetPoW)]));
-        end else TLog.NewLog(lterror,ClassName,sJobInfo+Format(' Timestamp %d < MinTimestamp %d',[_timestamp,P^.SentMinTimestamp]));
+        end else begin
+          TLog.NewLog(lterror,ClassName,sJobInfo+Format(' Timestamp %d < MinTimestamp %d',[_timestamp,P^.SentMinTimestamp]));
+        end;
         dec(i);
       end;
     Finally

+ 16 - 28
src/core/URandomHash2.pas

@@ -130,7 +130,7 @@ type
       FMurmurHash3_x86_32 : IHash;
       FHashAlg : array[0..17] of IHash;  // declared here to avoid race-condition during mining
       FCachedHeaderTemplate : TBytes;
-      FCachedHashes : TDictionary<UInt32, TCachedHash>;
+      FCachedHashes : TList<TCachedHash>;
 
       function GetCachedHashes : TArray<TCachedHash>; inline;
       function ContencateByteArrays(const AChunk1, AChunk2: TBytes): TBytes; inline;
@@ -146,8 +146,6 @@ type
       function Compress(const AInputs: TArray<TBytes>): TBytes; inline;
       function SetLastDWordLE(const ABytes: TBytes; AValue: UInt32): TBytes; inline;
       function GetLastDWordLE(const ABytes: TBytes) : UInt32; inline;
-      function Checksum(const AInput: TBytes): UInt32; overload; inline;
-      function Checksum(const AInput: TArray<TBytes>): UInt32; overload; inline;
       function ComputeVeneerRound(const ARoundOutputs : TArray<TBytes>) : TBytes; inline;
       function Hash(const ABlockHeader: TBytes; ARound: Int32; out AFoundLastRound : Int32) : TArray<TBytes>; overload;
     public
@@ -182,7 +180,7 @@ constructor TRandomHash2.Create;
 begin
   FMurmurHash3_x86_32 := THashFactory.THash32.CreateMurmurHash3_x86_32();
   SetLength(Self.FCachedHeaderTemplate, 0);
-  FCachedHashes := TDictionary<UInt32, TCachedHash>.Create;
+  FCachedHashes := TList<TCachedHash>.Create;
   FHashAlg[0] := THashFactory.TCrypto.CreateSHA2_256();
   FHashAlg[1] := THashFactory.TCrypto.CreateSHA2_384();
   FHashAlg[2] := THashFactory.TCrypto.CreateSHA2_512();
@@ -262,9 +260,9 @@ begin
   LRoundOutputs := LDisposables.AddObject( TList<TBytes>.Create() ) as TList<TBytes>;
   LGen := LDisposables.AddObject( TMersenne32.Create(0) ) as TMersenne32;
   if ARound = 1 then begin
-    LSeed := Checksum(ABlockHeader);
+    LRoundInput := FHashAlg[0].ComputeBytes(ABlockHeader).GetBytes;
+    LSeed := GetLastDWordLE( LRoundInput );
     LGen.Initialize(LSeed);
-    LRoundInput := ABlockHeader;
   end else begin
     LParentOutputs := Hash(ABlockHeader, ARound - 1, AFoundLastRound);
     if AFoundLastRound > 0 then
@@ -272,7 +270,7 @@ begin
       Exit(LParentOutputs);
 
     // Add parent round outputs to this round outputs
-    LSeed := Checksum(LParentOutputs);
+    LSeed := GetLastDWordLE( LParentOutputs[High(LParentOutputs)] );
     LGen.Initialize(LSeed);
     LRoundOutputs.AddRange( LParentOutputs );
 
@@ -289,11 +287,11 @@ begin
         LCachedHash.Header := LNeighbourNonceHeader;
         LCachedHash.Hash := ComputeVeneerRound(LNeighborOutputs);
         // if header is different (other than nonce), clear cache
-        if NOT BytesEqual(FCachedHeaderTemplate, LCachedHash.Header, 0, 32 - 4) then
+        if NOT BytesEqual(FCachedHeaderTemplate, LCachedHash.Header, 0, 32 - 4) then begin
           FCachedHashes.Clear;
-        FCachedHeaderTemplate := SetLastDWordLE(LCachedHash.Header, 0);
-        if NOT FCachedHashes.ContainsKey(LCachedHash.Nonce) then
-          FCachedHashes.Add(LCachedHash.Nonce, LCachedHash);
+          FCachedHeaderTemplate := SetLastDWordLE(LCachedHash.Header, 0);
+        end;
+        FCachedHashes.Add(LCachedHash);
       end;
     end;
     // Compress the parent/neighbouring outputs to form this rounds input
@@ -351,7 +349,7 @@ begin
            (ABytes[LLen - 1] SHL 24);
 end;
 
-function TRandomHash2.Checksum(const AInput: TBytes): UInt32;
+(*function TRandomHash2.Checksum(const AInput: TBytes): UInt32;
 begin
   Result := Checksum( TArray<TBytes>.Create( AInput ) );
 end;
@@ -360,7 +358,7 @@ function TRandomHash2.Checksum(const AInput : TArray<TBytes>): UInt32;
 begin
   // Checksum is the MurMur3 of the compression
   Result := FMurmurHash3_x86_32.ComputeBytes(Compress(AInput)).GetUInt32;
-end;
+end;*)
 
 function TRandomHash2.Compress(const AInputs : TArray<TBytes>): TBytes;
 var
@@ -382,7 +380,7 @@ end;
 
 function TRandomHash2.GetCachedHashes : TArray<TCachedHash>;
 begin
-  Result := FCachedHashes.Values.ToArray;
+  Result := FCachedHashes.ToArray;
 end;
 
 function TRandomHash2.HasCachedHash : Boolean;
@@ -391,24 +389,14 @@ begin
 end;
 
 function TRandomHash2.PopCachedHash : TCachedHash;
-var
- LItem : TRandomHash2.TCachedHash;
 begin
-  for LItem in FCachedHashes.Values do begin
-    Result := LItem;
-    break;
-  end;
-  FCachedHashes.Remove(Result.Nonce);
+  Result := FCachedHashes.Last;
+  FCachedHashes.Delete(FCachedHashes.Count - 1);
 end;
 
 function TRandomHash2.PeekCachedHash : TCachedHash;
-var
- LItem : TRandomHash2.TCachedHash;
 begin
-  for LItem in FCachedHashes.Values do begin
-    Result := LItem;
-    break;
-  end;
+  Result := FCachedHashes.Last;
 end;
 
 function TRandomHash2.ContencateByteArrays(const AChunk1, AChunk2: TBytes): TBytes;
@@ -542,7 +530,7 @@ var
   LGen: TMersenne32;
   LDisposables : TDisposables;
 begin
-  LSeed := Checksum(AInput);
+  LSeed := GetLastDWordLE(AInput);
   LGen := LDisposables.AddObject( TMersenne32.Create (LSeed) ) as TMersenne32;
   LSize := Length(AInput) + (AExpansionFactor * M);
   LOutput := Copy(AInput);

+ 13 - 3
src/gui-classic/UFRMDiagnosticTool.dfm

@@ -3,7 +3,7 @@ object FRMDiagnosticTool: TFRMDiagnosticTool
   Top = 0
   Caption = 'FRMDiagnosticTool'
   ClientHeight = 324
-  ClientWidth = 570
+  ClientWidth = 735
   Color = clBtnFace
   Font.Charset = DEFAULT_CHARSET
   Font.Color = clWindowText
@@ -13,7 +13,7 @@ object FRMDiagnosticTool: TFRMDiagnosticTool
   OldCreateOrder = False
   OnCreate = FormCreate
   DesignSize = (
-    570
+    735
     324)
   PixelsPerInch = 96
   TextHeight = 13
@@ -38,10 +38,11 @@ object FRMDiagnosticTool: TFRMDiagnosticTool
   object txtLog: TMemo
     Left = 8
     Top = 39
-    Width = 554
+    Width = 719
     Height = 277
     Anchors = [akLeft, akTop, akRight, akBottom]
     TabOrder = 2
+    ExplicitWidth = 554
   end
   object btnRHC: TButton
     Left = 280
@@ -52,4 +53,13 @@ object FRMDiagnosticTool: TFRMDiagnosticTool
     TabOrder = 3
     OnClick = btnRHCClick
   end
+  object btnEntropy: TButton
+    Left = 472
+    Top = 8
+    Width = 186
+    Height = 25
+    Caption = 'Entropy Tests'
+    TabOrder = 4
+    OnClick = btnEntropyClick
+  end
 end

+ 40 - 1
src/gui-classic/UFRMDiagnosticTool.pas

@@ -16,10 +16,12 @@ type
     btnRH2: TButton;
     txtLog: TMemo;
     btnRHC: TButton;
+    btnEntropy: TButton;
     procedure btnRH2Click(Sender: TObject);
     procedure btnRHClick(Sender: TObject);
     procedure FormCreate(Sender: TObject);
     procedure btnRHCClick(Sender: TObject);
+    procedure btnEntropyClick(Sender: TObject);
   private
     { Private declarations }
     FDisposables : TDisposables;
@@ -90,7 +92,7 @@ type
 
 implementation
 
-uses UBaseTypes;
+uses UBaseTypes, UCrypto;
 
 {$R *.dfm}
 
@@ -205,6 +207,43 @@ begin
   end;
 end;
 
+procedure TFRMDiagnosticTool.btnEntropyClick(Sender: TObject);
+
+  procedure SetLastDWordLE(var ABytes: TBytes;  AValue: UInt32);
+  var
+    LHeaderLength : Integer;
+  begin
+    // NOTE: NONCE is last 4 bytes of header!
+
+    // If digest not big enough to contain a nonce, just return the clone
+    LHeaderLength := Length(ABytes);
+    if LHeaderLength < 4 then
+      exit;
+
+    // Overwrite the nonce in little-endian
+    ABytes[LHeaderLength - 4] := Byte(AValue);
+    ABytes[LHeaderLength - 3] := (AValue SHR 8) AND 255;
+    ABytes[LHeaderLength - 2] := (AValue SHR 16) AND 255;
+    ABytes[LHeaderLength - 1] := (AValue SHR 24) AND 255;
+  end;
+
+
+var LIn, LOut : TBytes; i : Integer; TXT : String;
+begin
+  SetLength(LIn, 200);
+  FillChar(LIn, 200, 1);
+
+  TXT := '';
+  for I := 1 to 10 do begin
+    LOut := TRandomHash2.Compute(LIn);
+    TXT := TXT + Format('RH2( %s ) = %s %s', [ TCrypto.ToHexaString(LIn), TCrypto.ToHexaString(LOut), sLineBreak]);
+    SetLastDWordLE(LIn, I);
+  end;
+
+  txtLog.Text := TXT;
+
+end;
+
 procedure TFRMDiagnosticTool.btnRH2Click(Sender: TObject);
 begin
   if FRH2Thread.Suspended then begin

+ 1 - 1
src/pascalcoin_miner.pp

@@ -58,7 +58,7 @@ type
   end;
 
 Const
-  CT_MINER_VERSION = {$IFDEF PRODUCTION}'4.0'{$ELSE}{$IFDEF TESTNET}'4.0 TESTNET'{$ELSE}ERROR{$ENDIF}{$ENDIF};
+  CT_MINER_VERSION = {$IFDEF PRODUCTION}'5.0'{$ELSE}{$IFDEF TESTNET}'5.0 TESTNET'{$ELSE}ERROR{$ENDIF}{$ENDIF};
   CT_Line_DeviceStatus = 3;
   CT_Line_ConnectionStatus = 4;
   CT_Line_MinerValues = 7;

二進制
src/pascalcoin_wallet_classic.res


+ 0 - 1
src/pascalcoin_wallet_experimental.lpi

@@ -641,7 +641,6 @@
       <Unit103>
         <Filename Value="core\URandomHash2.pas"/>
         <IsPartOfProject Value="True"/>
-        <UnitName Value="URandomHash"/>
       </Unit103>
     </Units>
   </ProjectOptions>