Skip to content

Improvement of procedure TSHA512Base.Update

from 'lagprogramming'.

packages/fcl-hash/src/fpsha512.pp has

    procedure TSHA512Base.Update(PBuf: PByte; Size: UInt32);
    var
      Len: UInt32;
    begin
      Inc(TotalLength, Int64(UInt32(Size)) * 8);
      while Size > 0 do
      begin
        Len:=Sizeof(Buffer)-Index;
        if Len <= UInt32(Size) then
        begin
          Move(PBuf^, Buffer[Index], Len);
          Dec(Size, Len);
          Inc(PBuf, Len);
          Compress;
          Self.Index := 0;
        end else
        begin
          Move(PBuf^, Buffer[Index], Size);
          Inc(Self.Index, Size);
          Size := 0;
        end;
      end;
    end;

In order to exit the loop the original code sets a zero value to Size. The attached patch changes the line "Size := 0;" with "Break;". In addition to increased readability, the CPU will avoid a failed prediction of the "Size > 0" condition. sh.diff

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information