Bläddra i källkod

refactor ClpBits to ClpBitUtilities

Ugochukwu Mmaduekwe 2 veckor sedan
förälder
incheckning
0688c9eef6
42 ändrade filer med 872 tillägg och 939 borttagningar
  1. 1 1
      CryptoLib.Tests/Delphi.Tests/CryptoLib.Tests.dpr
  2. 4 4
      CryptoLib.Tests/src/Asn1/TagTests.pas
  3. 2 2
      CryptoLib.Tests/src/Math/ECPointTests.pas
  4. 10 10
      CryptoLib/src/Asn1/ClpAsn1Objects.pas
  5. 7 7
      CryptoLib/src/Asn1/ClpAsn1Streams.pas
  6. 5 5
      CryptoLib/src/Crypto/Encodings/ClpOaepEncoding.pas
  7. 7 7
      CryptoLib/src/Crypto/Encodings/ClpPkcs1Encoding.pas
  8. 2 2
      CryptoLib/src/Crypto/Engines/ClpAesEngine.pas
  9. 2 2
      CryptoLib/src/Crypto/Engines/ClpAesLightEngine.pas
  10. 2 2
      CryptoLib/src/Crypto/Engines/ClpSalsa20Engine.pas
  11. 3 3
      CryptoLib/src/Crypto/Generators/ClpDHKeyGeneratorHelper.pas
  12. 3 3
      CryptoLib/src/Crypto/Generators/ClpDHParametersHelper.pas
  13. 2 2
      CryptoLib/src/Crypto/Generators/ClpDsaKeyPairGenerator.pas
  14. 2 2
      CryptoLib/src/Crypto/Generators/ClpECKeyPairGenerator.pas
  15. 2 2
      CryptoLib/src/Crypto/Randoms/ClpSecureRandom.pas
  16. 2 2
      CryptoLib/src/Crypto/Signers/ClpDsaSigner.pas
  17. 441 0
      CryptoLib/src/GeneralUtilities/ClpBitUtilities.pas
  18. 7 7
      CryptoLib/src/GeneralUtilities/ClpConverters.pas
  19. 8 8
      CryptoLib/src/GeneralUtilities/ClpStringUtilities.pas
  20. 17 32
      CryptoLib/src/Math/ClpBigInteger.pas
  21. 8 8
      CryptoLib/src/Math/EC/ClpECAlgorithms.pas
  22. 12 12
      CryptoLib/src/Math/EC/ClpECC.pas
  23. 6 6
      CryptoLib/src/Math/EC/ClpECCompUtilities.pas
  24. 5 5
      CryptoLib/src/Math/EC/ClpLongArray.pas
  25. 2 2
      CryptoLib/src/Math/EC/Custom/Djb/ClpCurve25519Custom.pas
  26. 2 2
      CryptoLib/src/Math/EC/Custom/Sec/ClpSecP256K1Custom.pas
  27. 34 34
      CryptoLib/src/Math/EC/Custom/Sec/ClpSecP256R1Custom.pas
  28. 29 29
      CryptoLib/src/Math/EC/Custom/Sec/ClpSecP384R1Custom.pas
  29. 2 2
      CryptoLib/src/Math/EC/Custom/Sec/ClpSecP521R1Custom.pas
  30. 4 4
      CryptoLib/src/Math/EC/Custom/Sec/ClpSecT283Custom.pas
  31. 10 10
      CryptoLib/src/Math/EC/Multiplier/ClpMultipliers.pas
  32. 61 61
      CryptoLib/src/Math/EC/Rfc7748/ClpX25519Field.pas
  33. 37 37
      CryptoLib/src/Math/EC/Rfc8032/ClpEd25519.pas
  34. 2 2
      CryptoLib/src/Math/Field/ClpGenericPolynomialExtensionField.pas
  35. 0 493
      CryptoLib/src/Math/Raw/ClpBits.pas
  36. 49 49
      CryptoLib/src/Math/Raw/ClpNat.pas
  37. 32 32
      CryptoLib/src/Math/Raw/ClpNat192.pas
  38. 42 42
      CryptoLib/src/Math/Raw/ClpNat256.pas
  39. 1 1
      CryptoLib/src/Packages/Delphi/CryptoLib4PascalPackage.dpk
  40. 2 2
      CryptoLib/src/Packages/FPC/CryptoLib4PascalPackage.lpk
  41. 1 1
      CryptoLib/src/Packages/FPC/CryptoLib4PascalPackage.pas
  42. 2 2
      CryptoLib/src/X509/ClpX509Utilities.pas

+ 1 - 1
CryptoLib.Tests/Delphi.Tests/CryptoLib.Tests.dpr

@@ -49,7 +49,6 @@ uses
   ClpBigInteger in '..\..\CryptoLib\src\Math\ClpBigInteger.pas',
   ClpBigIntegers in '..\..\CryptoLib\src\Math\ClpBigIntegers.pas',
   ClpBitConverter in '..\..\CryptoLib\src\GeneralUtilities\ClpBitConverter.pas',
-  ClpBits in '..\..\CryptoLib\src\Math\Raw\ClpBits.pas',
   ClpBlockCipherModes in '..\..\CryptoLib\src\Crypto\Ciphers\ClpBlockCipherModes.pas',
   ClpBlowfishEngine in '..\..\CryptoLib\src\Crypto\Engines\ClpBlowfishEngine.pas',
   ClpBsiObjectIdentifiers in '..\..\CryptoLib\src\Asn1\Bsi\ClpBsiObjectIdentifiers.pas',
@@ -63,6 +62,7 @@ uses
   ClpCipherUtilities in '..\..\CryptoLib\src\Crypto\Ciphers\ClpCipherUtilities.pas',
   ClpCollectionUtilities in '..\..\CryptoLib\src\GeneralUtilities\ClpCollectionUtilities.pas',
   ClpConverters in '..\..\CryptoLib\src\GeneralUtilities\ClpConverters.pas',
+  ClpBitUtilities in '..\..\CryptoLib\src\GeneralUtilities\ClpBitUtilities.pas',
   ClpCryptLibObjectIdentifiers in '..\..\CryptoLib\src\Asn1\CryptLib\ClpCryptLibObjectIdentifiers.pas',
   ClpCryptoApiRandomGenerator in '..\..\CryptoLib\src\Rngs\ClpCryptoApiRandomGenerator.pas',
   ClpCryptoLibComparers in '..\..\CryptoLib\src\GeneralUtilities\ClpCryptoLibComparers.pas',

+ 4 - 4
CryptoLib.Tests/src/Asn1/TagTests.pas

@@ -37,7 +37,7 @@ uses
   ClpAsn1Streams,
   ClpSecureRandom,
   ClpISecureRandom,
-  ClpBits,
+  ClpBitUtilities,
   ClpEncoders,
   ClpCryptoLibTypes,
   CryptoLibTestBase;
@@ -158,8 +158,8 @@ begin
   LSR := TSecureRandom.Create();
   for I := 0 to 99 do
   begin
-    LTestTag := TBits.Asr32(LSR.NextInt32() and System.High(Int32), LSR.Next(26));
-    LApp := TDerTaggedObject.Create(False, TAsn1Tags.Application, LTestTag, TDerOctetString.Create(TCryptoLibByteArray.Create(1)));
+    LTestTag := TBitUtilities.Asr32(LSR.NextInt32() and System.High(Int32), LSR.Next(26));
+    LApp := TDerTaggedObject.Create(False, TAsn1Tags.Application, LTestTag, TDerOctetString.Create(TCryptoLibByteArray.Create(1)) as IDerOctetString);
     LApp := TAsn1TaggedObject.GetInstance(TAsn1Object.FromByteArray(LApp.GetEncoded())) as IAsn1TaggedObject;
 
     if not LApp.HasTag(TAsn1Tags.Application, LTestTag) then
@@ -168,7 +168,7 @@ begin
     end;
   end;
 
-  LTagged := TDerTaggedObject.Create(False, 34, TDerTaggedObject.Create(True, 1000, TDerInteger.One));
+  LTagged := TDerTaggedObject.Create(False, 34, TDerTaggedObject.Create(True, 1000, TDerInteger.One) as IDerTaggedObject);
   if not AreEqual(FTaggedInteger, LTagged.GetEncoded()) then
   begin
     Fail('incorrect encoding for implicit explicit tagged integer');

+ 2 - 2
CryptoLib.Tests/src/Math/ECPointTests.pas

@@ -34,7 +34,7 @@ uses
   TestFramework,
 {$ENDIF FPC}
   Generics.Collections,
-  ClpBits,
+  ClpBitUtilities,
   ClpCustomNamedCurves,
   ClpECNamedCurveTable,
   ClpCryptoLibTypes,
@@ -339,7 +339,7 @@ begin
   ImplTestMultiply(q, n.BitLength);
   ImplTestMultiply(infinity, n.BitLength);
 
-  logSize := 32 - TBits.NumberOfLeadingZeros(curve.FieldSize - 1);
+  logSize := 32 - TBitUtilities.NumberOfLeadingZeros(curve.FieldSize - 1);
   rounds := Max(2, Min(10, 32 - 3 * logSize));
 
   p := q;

+ 10 - 10
CryptoLib/src/Asn1/ClpAsn1Objects.pas

@@ -26,7 +26,7 @@ uses
   SysUtils,
   Math,
   DateUtils,
-  ClpBits,
+  ClpBitUtilities,
   ClpCryptoLibTypes,
   ClpBigInteger,
   ClpBigIntegers,
@@ -4782,7 +4782,7 @@ var
   LCopy: TCryptoLibGenericArray<IAsn1Encodable>;
 begin
   LOldCapacity := System.Length(FElements);
-  LNewCapacity := Math.Max(LOldCapacity, AMinCapacity + (TBits.Asr32(AMinCapacity, 1)));
+  LNewCapacity := Math.Max(LOldCapacity, AMinCapacity + (TBitUtilities.Asr32(AMinCapacity, 1)));
 
   System.SetLength(LCopy, LNewCapacity);
   for I := 0 to FElementCount - 1 do
@@ -8005,17 +8005,17 @@ begin
   begin
     LSize := 1;
     LVal := ALength;
-    LVal := TBits.Asr32(LVal, 8);
+    LVal := TBitUtilities.Asr32(LVal, 8);
     while LVal <> 0 do
     begin
       System.Inc(LSize);
-      LVal := TBits.Asr32(LVal, 8);
+      LVal := TBitUtilities.Asr32(LVal, 8);
     end;
     AOutStr.WriteByte(Byte(LSize or $80));
     I := (LSize - 1) * 8;
     while I >= 0 do
     begin
-      AOutStr.WriteByte(Byte(TBits.Asr32(ALength, I)));
+      AOutStr.WriteByte(Byte(TBitUtilities.Asr32(ALength, I)));
       System.Dec(I, 8);
     end;
   end
@@ -8964,13 +8964,13 @@ begin
     FContents[0] := 0;
     Exit;
   end;
-  LBits := 32 - TBits.NumberOfLeadingZeros(UInt32(ANamedBits));
+  LBits := 32 - TBitUtilities.NumberOfLeadingZeros(UInt32(ANamedBits));
   LBytes := (LBits + 7) div 8;
   System.SetLength(LData, 1 + LBytes);
   for I := 1 to LBytes - 1 do
   begin
     LData[I] := Byte(ANamedBits);
-    ANamedBits := TBits.Asr32(ANamedBits, 8);
+    ANamedBits := TBitUtilities.Asr32(ANamedBits, 8);
   end;
   LData[LBytes] := Byte(ANamedBits);
   LPadBits := 0;
@@ -9706,7 +9706,7 @@ begin
   LByteLen := System.Length(AStr);
   if (LByteLen and 1) <> 0 then
     raise EArgumentCryptoLibException.Create('malformed BMPString encoding encountered');
-  LCharLen := TBits.Asr32(LByteLen, 1);
+  LCharLen := TBitUtilities.Asr32(LByteLen, 1);
   System.SetLength(LCs, LCharLen);
   for I := 0 to LCharLen - 1 do
     LCs[I] := Char((AStr[2 * I] shl 8) or (AStr[2 * I + 1] and $FF));
@@ -14526,7 +14526,7 @@ begin
     1:
       Result := False;
   else
-    Result := (ShortInt(ABytes[0]) = TBits.Asr32(ShortInt(ABytes[1]), 7)) and (not AllowUnsafe());
+    Result := (ShortInt(ABytes[0]) = TBitUtilities.Asr32(ShortInt(ABytes[1]), 7)) and (not AllowUnsafe());
   end;
 end;
 
@@ -14536,7 +14536,7 @@ var
 begin
   LPos := 0;
   LLast := System.Length(ABytes) - 1;
-  while (LPos < LLast) and (ShortInt(ABytes[LPos]) = TBits.Asr32(ShortInt(ABytes[LPos + 1]), 7)) do
+  while (LPos < LLast) and (ShortInt(ABytes[LPos]) = TBitUtilities.Asr32(ShortInt(ABytes[LPos + 1]), 7)) do
   begin
     System.Inc(LPos);
   end;

+ 7 - 7
CryptoLib/src/Asn1/ClpAsn1Streams.pas

@@ -26,7 +26,7 @@ uses
   Math,
   SysUtils,
   ClpIAsn1Objects,
-  ClpBits,
+  ClpBitUtilities,
   ClpPlatformUtilities,
   ClpCryptoLibTypes,
   ClpStreams,
@@ -839,7 +839,7 @@ begin
   repeat
     System.Dec(LPos);
     LStack[LPos] := Byte(ADl);
-    ADl := TBits.Asr32(ADl, 8);
+    ADl := TBitUtilities.Asr32(ADl, 8);
   until ADl = 0;
 
   LCount := System.Length(LStack) - LPos;
@@ -867,7 +867,7 @@ begin
   LStack[LPos] := Byte(ATagNo and $7F);
   while ATagNo > 127 do
   begin
-    ATagNo := TBits.Asr32(ATagNo, 7);
+    ATagNo := TBitUtilities.Asr32(ATagNo, 7);
     System.Dec(LPos);
     LStack[LPos] := Byte((ATagNo and $7F) or $80);
   end;
@@ -897,9 +897,9 @@ begin
   end;
 
   Result := 2;
-  while TBits.Asr32(ADl, 8) > 0 do
+  while TBitUtilities.Asr32(ADl, 8) > 0 do
   begin
-    ADl := TBits.Asr32(ADl, 8);
+    ADl := TBitUtilities.Asr32(ADl, 8);
     System.Inc(Result);
   end;
 end;
@@ -913,9 +913,9 @@ begin
   end;
 
   Result := 2;
-  while TBits.Asr32(ATagNo, 7) > 0 do
+  while TBitUtilities.Asr32(ATagNo, 7) > 0 do
   begin
-    ATagNo := TBits.Asr32(ATagNo, 7);
+    ATagNo := TBitUtilities.Asr32(ATagNo, 7);
     System.Inc(Result);
   end;
 end;

+ 5 - 5
CryptoLib/src/Crypto/Encodings/ClpOaepEncoding.pas

@@ -23,7 +23,7 @@ interface
 
 uses
   SysUtils,
-  ClpBits,
+  ClpBitUtilities,
   ClpICipherParameters,
   ClpIParametersWithRandom,
   ClpIDigest,
@@ -236,12 +236,12 @@ begin
   defHashLen := System.Length(FDefHash);
 
   // Check reduced block size is valid
-  wrongMask := TBits.Asr32(GetReducedBlockSize(outBlockSize), 31);
+  wrongMask := TBitUtilities.Asr32(GetReducedBlockSize(outBlockSize), 31);
 
   SetLength(block, outBlockSize);
   data := FEngine.ProcessBlock(inBytes, inOff, inLen);
 
-  wrongMask := wrongMask or TBits.Asr32((System.Length(block) - System.Length(data)), 31);
+  wrongMask := wrongMask or TBitUtilities.Asr32((System.Length(block) - System.Length(data)), 31);
 
   copyLen := System.Length(data);
   if copyLen > System.Length(block) then
@@ -272,11 +272,11 @@ begin
   begin
     octet := block[index];
     // Mask will be 0xFFFFFFFF if octet is non-zero and start is (still) negative
-    shouldSetMask := TBits.Asr32((-octet) and start, 31);
+    shouldSetMask := TBitUtilities.Asr32((-octet) and start, 31);
     start := start + (index and shouldSetMask);
   end;
 
-  wrongMask := wrongMask or TBits.Asr32(start, 31);
+  wrongMask := wrongMask or TBitUtilities.Asr32(start, 31);
   Inc(start);
   wrongMask := wrongMask or (block[start] xor 1);
 

+ 7 - 7
CryptoLib/src/Crypto/Encodings/ClpPkcs1Encoding.pas

@@ -24,7 +24,7 @@ interface
 uses
   SysUtils,
   Math,
-  ClpBits,
+  ClpBitUtilities,
   ClpICipherParameters,
   ClpIAsymmetricKeyParameter,
   ClpIAsymmetricBlockCipher,
@@ -277,8 +277,8 @@ begin
   for i := 1 to System.Length(buf) - 1 do
   begin
     padByte := buf[i];
-    is0x00Mask := TBits.Asr32((padByte xor $00) - 1, 31);
-    is0xFFMask := TBits.Asr32((padByte xor $FF) - 1, 31);
+    is0x00Mask := TBitUtilities.Asr32((padByte xor $00) - 1, 31);
+    is0xFFMask := TBitUtilities.Asr32((padByte xor $FF) - 1, 31);
     lastPadPos := lastPadPos xor (i and (not foundZeroMask) and is0x00Mask);
     foundZeroMask := foundZeroMask or is0x00Mask;
     badPadSign := badPadSign or (not (foundZeroMask or is0xFFMask));
@@ -288,7 +288,7 @@ begin
   badPadSign := badPadSign or (lastPadPos - 9);
 
   plaintextLength := System.Length(buf) - 1 - lastPadPos;
-  Result := plaintextLength or TBits.Asr32(badPadSign, 31);
+  Result := plaintextLength or TBitUtilities.Asr32(badPadSign, 31);
 end;
 
 class function TPkcs1Encoding.CheckPkcs1Encoding2(
@@ -308,7 +308,7 @@ begin
   for i := 1 to System.Length(buf) - 1 do
   begin
     padByte := buf[i];
-    is0x00Mask := TBits.Asr32((padByte xor $00) - 1, 31);
+    is0x00Mask := TBitUtilities.Asr32((padByte xor $00) - 1, 31);
     lastPadPos := lastPadPos xor (i and (not foundZeroMask) and is0x00Mask);
     foundZeroMask := foundZeroMask or is0x00Mask;
   end;
@@ -317,7 +317,7 @@ begin
   badPadSign := badPadSign or (lastPadPos - 9);
 
   plaintextLength := System.Length(buf) - 1 - lastPadPos;
-  Result := plaintextLength or TBits.Asr32(badPadSign, 31);
+  Result := plaintextLength or TBitUtilities.Asr32(badPadSign, 31);
 end;
 
 class function TPkcs1Encoding.CheckPkcs1Encoding2(
@@ -341,7 +341,7 @@ begin
   // Last pad byte should be zero
   badPadSign := badPadSign or (-buf[lastPadPos]);
 
-  Result := TBits.Asr32(badPadSign, 31);
+  Result := TBitUtilities.Asr32(badPadSign, 31);
 end;
 
 function TPkcs1Encoding.DecodeBlockOrRandom(const input: TCryptoLibByteArray;

+ 2 - 2
CryptoLib/src/Crypto/Engines/ClpAesEngine.pas

@@ -28,7 +28,7 @@ uses
   ClpICipherParameters,
   ClpIKeyParameter,
   ClpCheck,
-  ClpBits,
+  ClpBitUtilities,
   ClpConverters,
   ClpArrayUtilities,
   ClpCryptoLibTypes;
@@ -296,7 +296,7 @@ implementation
 
 class function TAesEngine.Shift(r: UInt32; Shift: Int32): UInt32;
 begin
-  result := TBits.RotateRight32(r, Shift);
+  result := TBitUtilities.RotateRight32(r, Shift);
 end;
 
 class function TAesEngine.SubWord(x: UInt32): UInt32;

+ 2 - 2
CryptoLib/src/Crypto/Engines/ClpAesLightEngine.pas

@@ -28,7 +28,7 @@ uses
   ClpICipherParameters,
   ClpIKeyParameter,
   ClpCheck,
-  ClpBits,
+  ClpBitUtilities,
   ClpConverters,
   ClpArrayUtilities,
   ClpCryptoLibTypes;
@@ -205,7 +205,7 @@ implementation
 
 class function TAesLightEngine.Shift(r: UInt32; Shift: Int32): UInt32;
 begin
-  result := TBits.RotateRight32(r, Shift);
+  result := TBitUtilities.RotateRight32(r, Shift);
 end;
 
 class function TAesLightEngine.SubWord(x: UInt32): UInt32;

+ 2 - 2
CryptoLib/src/Crypto/Engines/ClpSalsa20Engine.pas

@@ -23,7 +23,7 @@ interface
 
 uses
   SysUtils,
-  ClpBits,
+  ClpBitUtilities,
   ClpCheck,
   ClpIStreamCipher,
   ClpISalsa20Engine,
@@ -329,7 +329,7 @@ end;
 
 class function TSalsa20Engine.R(x: UInt32; y: Int32): UInt32;
 begin
-  result := TBits.RotateLeft32(x, y);
+  result := TBitUtilities.RotateLeft32(x, y);
 end;
 
 procedure TSalsa20Engine.ResetCounter;

+ 3 - 3
CryptoLib/src/Crypto/Generators/ClpDHKeyGeneratorHelper.pas

@@ -24,7 +24,7 @@ interface
 uses
 
   ClpISecureRandom,
-  ClpBits,
+  ClpBitUtilities,
   ClpBigInteger,
   ClpBigIntegers,
   ClpECCompUtilities,
@@ -81,7 +81,7 @@ begin
 
   if (limit <> 0) then
   begin
-    minWeight := TBits.Asr32(limit, 2);
+    minWeight := TBitUtilities.Asr32(limit, 2);
 
     while True do
     begin
@@ -108,7 +108,7 @@ begin
   end;
   max := q.Subtract(TBigInteger.Two);
 
-  minWeight := TBits.Asr32(max.BitLength, 2);
+  minWeight := TBitUtilities.Asr32(max.BitLength, 2);
 
   while True do
   begin

+ 3 - 3
CryptoLib/src/Crypto/Generators/ClpDHParametersHelper.pas

@@ -26,7 +26,7 @@ uses
   ClpBigInteger,
   ClpBigIntegers,
   ClpECCompUtilities,
-  ClpBits,
+  ClpBitUtilities,
   ClpCryptoLibTypes;
 
 type
@@ -125,7 +125,7 @@ var
 begin
   retryFlag := False;
   qLength := size - 1;
-  minWeight := TBits.Asr32(size, 2);
+  minWeight := TBitUtilities.Asr32(size, 2);
 
   if (size <= 32) then
   begin
@@ -176,7 +176,7 @@ begin
         begin
           prime := LPrimeList[j];
           qRem := test mod prime;
-          if ((qRem = 0) or (qRem = TBits.Asr32(prime, 1))) then
+          if ((qRem = 0) or (qRem = TBitUtilities.Asr32(prime, 1))) then
           begin
             q := q.Add(FSix);
             retryFlag := True;

+ 2 - 2
CryptoLib/src/Crypto/Generators/ClpDsaKeyPairGenerator.pas

@@ -34,7 +34,7 @@ uses
   ClpIKeyGenerationParameters,
   ClpIDsaKeyGenerationParameters,
   ClpIAsymmetricCipherKeyPairGenerator,
-  ClpBits,
+  ClpBitUtilities,
   ClpBigInteger,
   ClpBigIntegers,
   ClpECCompUtilities,
@@ -110,7 +110,7 @@ begin
   One := TBigInteger.One;
   result := Default (TBigInteger);
   // B.1.2 Key Pair Generation by Testing Candidates
-  minWeight := TBits.Asr32(q.BitLength, 2);
+  minWeight := TBitUtilities.Asr32(q.BitLength, 2);
   while (True) do
   begin
     // TODO Prefer this method? (change test cases that used fixed random)

+ 2 - 2
CryptoLib/src/Crypto/Generators/ClpECKeyPairGenerator.pas

@@ -24,7 +24,7 @@ interface
 uses
   SysUtils,
   ClpBigInteger,
-  ClpBits,
+  ClpBitUtilities,
   ClpCryptoLibTypes,
   ClpECKeyParameters,
   ClpECCompUtilities,
@@ -134,7 +134,7 @@ var
   q: IECPoint;
 begin
   n := Fparameters.n;
-  minWeight := TBits.Asr32(n.BitLength, 2);
+  minWeight := TBitUtilities.Asr32(n.BitLength, 2);
 
   while (true) do
   begin

+ 2 - 2
CryptoLib/src/Crypto/Randoms/ClpSecureRandom.pas

@@ -26,7 +26,7 @@ uses
   SyncObjs,
   SysUtils,
   DateUtils,
-  ClpBits,
+  ClpBitUtilities,
   ClpCryptoLibTypes,
   ClpDateTimeUtilities,
   ClpIDigest,
@@ -161,7 +161,7 @@ begin
   if ((AMaxValue and (AMaxValue - 1)) = 0) then
   begin
     LBits := NextInt32() and System.High(Int32);
-    Result := Int32(TBits.Asr64((Int64(LBits) * AMaxValue), 31));
+    Result := Int32(TBitUtilities.Asr64((Int64(LBits) * AMaxValue), 31));
     Exit;
   end;
 

+ 2 - 2
CryptoLib/src/Crypto/Signers/ClpDsaSigner.pas

@@ -36,7 +36,7 @@ uses
   ClpParameterUtilities,
   ClpSecureRandom,
   ClpRandomDsaKCalculator,
-  ClpBits,
+  ClpBitUtilities,
   ClpBigInteger,
   ClpCryptoLibTypes;
 
@@ -124,7 +124,7 @@ function TDsaSigner.CalculateE(const AN: TBigInteger;
 var
   LLength: Int32;
 begin
-  LLength := Math.Min(System.length(AMessage), TBits.Asr32(AN.BitLength, 3));
+  LLength := Math.Min(System.length(AMessage), TBitUtilities.Asr32(AN.BitLength, 3));
   Result := TBigInteger.Create(1, AMessage, 0, LLength);
 end;
 

+ 441 - 0
CryptoLib/src/GeneralUtilities/ClpBitUtilities.pas

@@ -0,0 +1,441 @@
+{ *********************************************************************************** }
+{ *                              CryptoLib Library                                  * }
+{ *                Copyright (c) 2018 - 20XX Ugochukwu Mmaduekwe                    * }
+{ *                 Github Repository <https://github.com/Xor-el>                   * }
+
+{ *  Distributed under the MIT software license, see the accompanying file LICENSE  * }
+{ *          or visit http://www.opensource.org/licenses/mit-license.php.           * }
+
+{ *                              Acknowledgements:                                  * }
+{ *                                                                                 * }
+{ *      Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring     * }
+{ *                           development of this library                           * }
+
+{ * ******************************************************************************* * }
+
+(* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
+
+unit ClpBitUtilities;
+
+{$I ..\Include\CryptoLib.inc}
+
+interface
+
+type
+  TBitUtilities = class sealed(TObject)
+
+  public
+
+    class function ReverseBytesInt32(AValue: Int32): Int32; static; inline;
+    class function ReverseBitsUInt8(AValue: UInt8): UInt8; static; inline;
+    class function ReverseBytesUInt16(AValue: UInt16): UInt16; static; inline;
+    class function ReverseBytesUInt32(AValue: UInt32): UInt32; static; inline;
+    class function ReverseBytesUInt64(AValue: UInt64): UInt64; static; inline;
+
+    /// <summary>
+    /// Calculates Arithmetic shift right.
+    /// </summary>
+    /// <param name="AValue">Int32 value to compute 'Asr' on.</param>
+    /// <param name="AShiftBits">Byte, number of bits to shift value to.</param>
+    /// <returns>Shifted value.</returns>
+    /// <remarks>
+    /// Emulated Implementation was gotten from FreePascal sources
+    /// </remarks>
+
+    class function Asr32(AValue: Int32; AShiftBits: Byte): Int32; static; inline;
+
+    /// <summary>
+    /// Calculates Arithmetic shift right.
+    /// </summary>
+    /// <param name="AValue">Int64 value to compute 'Asr' on.</param>
+    /// <param name="AShiftBits">Byte, number of bits to shift value to.</param>
+    /// <returns>Shifted value.</returns>
+    /// <remarks>
+    /// Emulated Implementation was gotten from FreePascal sources
+    /// </remarks>
+
+    class function Asr64(AValue: Int64; AShiftBits: Byte): Int64; static; inline;
+
+    /// <summary>
+    /// Calculates Negative Left Shift. This was implemented to circumvent a
+    /// bug in FPC ARM when performing Shift Left on certain values with a
+    /// Negative Shift Bits. For example UInt32(1948415963) shl Int32(-2)
+    /// should give "3221225472" but in FPC ARM, It gives "0". In some C
+    /// Compilers, this is "Undefined"
+    /// </summary>
+    /// <param name="AValue">
+    /// Value to Perform Shift On
+    /// </param>
+    /// <param name="AShiftBits">
+    /// Integer, number of bits to shift value to. This Number <b>Must be
+    /// Negative</b>
+    /// </param>
+    /// <returns>
+    /// Shifted value.
+    /// </returns>
+
+    class function NegativeLeftShift32(AValue: UInt32; AShiftBits: Int32): UInt32; static; inline;
+
+    /// <summary>
+    /// Calculates Negative Right Shift. This was implemented to circumvent a
+    /// compiler issue when performing Shift Right on certain values with a
+    /// Negative Shift Bits. In some C Compilers, this is "Undefined"
+    /// </summary>
+    /// <param name="AValue">
+    /// Value to Perform Shift On
+    /// </param>
+    /// <param name="AShiftBits">
+    /// Integer, number of bits to shift value to. This Number <b>Must be
+    /// Negative</b>
+    /// </param>
+    /// <returns>
+    /// Shifted value.
+    /// </returns>
+
+    class function NegativeRightShift32(AValue: UInt32; AShiftBits: Int32): UInt32; static; inline;
+
+    /// <summary>
+    /// Calculates Negative Right Shift. This was implemented to circumvent a
+    /// compiler issue when performing Shift Right on certain values with a
+    /// Negative Shift Bits. In some C Compilers, this is "Undefined"
+    /// </summary>
+    /// <param name="AValue">
+    /// Value to Perform Shift On
+    /// </param>
+    /// <param name="AShiftBits">
+    /// Integer, number of bits to shift value to. This Number <b>Must be
+    /// Negative</b>
+    /// </param>
+    /// <returns>
+    /// Shifted value.
+    /// </returns>
+
+    class function NegativeRightShift64(AValue: UInt64; AShiftBits: Int32): UInt64; static; inline;
+
+    class function RotateLeft8(AValue: Byte; AN: Int32): Byte; static; inline;
+    class function RotateLeft32(AValue: UInt32; AN: Int32): UInt32; static; inline;
+    class function RotateLeft64(AValue: UInt64; AN: Int32): UInt64; static; inline;
+    class function RotateRight8(AValue: Byte; AN: Int32): Byte; static; inline;
+    class function RotateRight32(AValue: UInt32; AN: Int32): UInt32; static; inline;
+    class function RotateRight64(AValue: UInt64; AN: Int32): UInt64; static; inline;
+
+    class function NumberOfLeadingZeros(AValue: UInt32): Int32; static;
+
+    class function NumberOfTrailingZeros(AValue: UInt32): Int32; static;
+
+    class function PopCount(AValue: UInt32): Int32; static;
+  end;
+
+implementation
+
+{ TBitUtilities }
+
+class function TBitUtilities.ReverseBytesInt32(AValue: Int32): Int32;
+{$IFNDEF FPC}
+var
+  LI1, LI2, LI3, LI4: Int32;
+{$ENDIF FPC}
+begin
+{$IFDEF FPC}
+  Result := SwapEndian(AValue);
+{$ELSE}
+  LI1 := AValue and $FF;
+  LI2 := TBitUtilities.Asr32(AValue, 8) and $FF;
+  LI3 := TBitUtilities.Asr32(AValue, 16) and $FF;
+  LI4 := TBitUtilities.Asr32(AValue, 24) and $FF;
+
+  Result := (LI1 shl 24) or (LI2 shl 16) or (LI3 shl 8) or (LI4 shl 0);
+{$ENDIF FPC}
+end;
+
+class function TBitUtilities.ReverseBitsUInt8(AValue: UInt8): UInt8;
+begin
+  AValue := ((AValue shr 1) and $55) or ((AValue shl 1) and $AA);
+  AValue := ((AValue shr 2) and $33) or ((AValue shl 2) and $CC);
+  AValue := ((AValue shr 4) and $0F) or ((AValue shl 4) and $F0);
+  Result := AValue;
+end;
+
+class function TBitUtilities.ReverseBytesUInt16(AValue: UInt16): UInt16;
+begin
+{$IFDEF FPC}
+  Result := SwapEndian(AValue);
+{$ELSE}
+  Result := UInt16((AValue and UInt32($FF)) shl 8 or
+    (AValue and UInt32($FF00)) shr 8);
+{$ENDIF FPC}
+end;
+
+class function TBitUtilities.ReverseBytesUInt32(AValue: UInt32): UInt32;
+begin
+{$IFDEF FPC}
+  Result := SwapEndian(AValue);
+{$ELSE}
+  Result := (AValue and UInt32($000000FF)) shl 24 or (AValue and UInt32($0000FF00)
+    ) shl 8 or (AValue and UInt32($00FF0000)) shr 8 or
+    (AValue and UInt32($FF000000)) shr 24;
+{$ENDIF FPC}
+end;
+
+class function TBitUtilities.ReverseBytesUInt64(AValue: UInt64): UInt64;
+begin
+{$IFDEF FPC}
+  Result := SwapEndian(AValue);
+{$ELSE}
+  Result := (AValue and UInt64($00000000000000FF)) shl 56 or
+    (AValue and UInt64($000000000000FF00)) shl 40 or
+    (AValue and UInt64($0000000000FF0000)) shl 24 or
+    (AValue and UInt64($00000000FF000000)) shl 8 or
+    (AValue and UInt64($000000FF00000000)) shr 8 or
+    (AValue and UInt64($0000FF0000000000)) shr 24 or
+    (AValue and UInt64($00FF000000000000)) shr 40 or
+    (AValue and UInt64($FF00000000000000)) shr 56;
+{$ENDIF FPC}
+end;
+
+class function TBitUtilities.Asr32(AValue: Int32; AShiftBits: Byte): Int32;
+begin
+{$IFDEF FPC}
+  Result := SarLongInt(AValue, AShiftBits);
+{$ELSE}
+  Result := Int32(UInt32(UInt32(UInt32(AValue) shr (AShiftBits and 31)) or
+    (UInt32(Int32(UInt32(0 - UInt32(UInt32(AValue) shr 31)) and
+    UInt32(Int32(0 - (Ord((AShiftBits and 31) <> 0) { and 1 } )))))
+    shl (32 - (AShiftBits and 31)))));
+{$ENDIF FPC}
+end;
+
+class function TBitUtilities.Asr64(AValue: Int64; AShiftBits: Byte): Int64;
+begin
+{$IFDEF FPC}
+  Result := SarInt64(AValue, AShiftBits);
+{$ELSE}
+  Result := Int64(UInt64(UInt64(UInt64(AValue) shr (AShiftBits and 63)) or
+    (UInt64(Int64(UInt64(0 - UInt64(UInt64(AValue) shr 63)) and
+    UInt64(Int64(0 - (Ord((AShiftBits and 63) <> 0) { and 1 } )))))
+    shl (64 - (AShiftBits and 63)))));
+{$ENDIF FPC}
+end;
+
+class function TBitUtilities.NegativeLeftShift32(AValue: UInt32; AShiftBits: Int32): UInt32;
+begin
+{$IFDEF DEBUG}
+  System.Assert(AShiftBits < 0);
+{$ENDIF DEBUG}
+  Result := AValue shl (32 + AShiftBits);
+end;
+
+class function TBitUtilities.NegativeRightShift32(AValue: UInt32; AShiftBits: Int32): UInt32;
+begin
+{$IFDEF DEBUG}
+  System.Assert(AShiftBits < 0);
+{$ENDIF DEBUG}
+  Result := AValue shr (32 + AShiftBits);
+end;
+
+class function TBitUtilities.NegativeRightShift64(AValue: UInt64; AShiftBits: Int32): UInt64;
+begin
+{$IFDEF DEBUG}
+  System.Assert(AShiftBits < 0);
+{$ENDIF DEBUG}
+  Result := AValue shr (64 + AShiftBits);
+end;
+
+class function TBitUtilities.RotateLeft8(AValue: Byte; AN: Int32): Byte;
+begin
+{$IFDEF DEBUG}
+  System.Assert(AN >= 0);
+{$ENDIF DEBUG}
+{$IFDEF FPC}
+  Result := RolByte(AValue, AN);
+{$ELSE}
+{$IFNDEF SHIFT_OVERFLOW_BUG_FIXED}
+  AN := AN and 7;
+{$ENDIF SHIFT_OVERFLOW_BUG_FIXED}
+  Result := (AValue shl AN) or (AValue shr (8 - AN));
+{$ENDIF FPC}
+end;
+
+class function TBitUtilities.RotateLeft32(AValue: UInt32; AN: Int32): UInt32;
+begin
+{$IFDEF DEBUG}
+  System.Assert(AN >= 0);
+{$ENDIF DEBUG}
+{$IFDEF FPC}
+  Result := RolDWord(AValue, AN);
+{$ELSE}
+{$IFNDEF SHIFT_OVERFLOW_BUG_FIXED}
+  AN := AN and 31;
+{$ENDIF SHIFT_OVERFLOW_BUG_FIXED}
+  Result := (AValue shl AN) or (AValue shr (32 - AN));
+{$ENDIF FPC}
+end;
+
+class function TBitUtilities.RotateLeft64(AValue: UInt64; AN: Int32): UInt64;
+begin
+{$IFDEF DEBUG}
+  System.Assert(AN >= 0);
+{$ENDIF DEBUG}
+{$IFDEF FPC}
+  Result := RolQWord(AValue, AN);
+{$ELSE}
+{$IFNDEF SHIFT_OVERFLOW_BUG_FIXED}
+  AN := AN and 63;
+{$ENDIF SHIFT_OVERFLOW_BUG_FIXED}
+  Result := (AValue shl AN) or (AValue shr (64 - AN));
+{$ENDIF FPC}
+end;
+
+class function TBitUtilities.RotateRight8(AValue: Byte; AN: Int32): Byte;
+begin
+{$IFDEF DEBUG}
+  System.Assert(AN >= 0);
+{$ENDIF DEBUG}
+{$IFDEF FPC}
+  Result := RorByte(AValue, AN);
+{$ELSE}
+{$IFNDEF SHIFT_OVERFLOW_BUG_FIXED}
+  AN := AN and 7;
+{$ENDIF SHIFT_OVERFLOW_BUG_FIXED}
+  Result := (AValue shr AN) or (AValue shl (8 - AN));
+{$ENDIF FPC}
+end;
+
+class function TBitUtilities.RotateRight32(AValue: UInt32; AN: Int32): UInt32;
+begin
+{$IFDEF DEBUG}
+  System.Assert(AN >= 0);
+{$ENDIF DEBUG}
+{$IFDEF FPC}
+  Result := RorDWord(AValue, AN);
+{$ELSE}
+{$IFNDEF SHIFT_OVERFLOW_BUG_FIXED}
+  AN := AN and 31;
+{$ENDIF SHIFT_OVERFLOW_BUG_FIXED}
+  Result := (AValue shr AN) or (AValue shl (32 - AN));
+{$ENDIF FPC}
+end;
+
+class function TBitUtilities.RotateRight64(AValue: UInt64; AN: Int32): UInt64;
+begin
+{$IFDEF DEBUG}
+  System.Assert(AN >= 0);
+{$ENDIF DEBUG}
+{$IFDEF FPC}
+  Result := RorQWord(AValue, AN);
+{$ELSE}
+{$IFNDEF SHIFT_OVERFLOW_BUG_FIXED}
+  AN := AN and 63;
+{$ENDIF SHIFT_OVERFLOW_BUG_FIXED}
+  Result := (AValue shr AN) or (AValue shl (64 - AN));
+{$ENDIF FPC}
+end;
+
+class function TBitUtilities.NumberOfLeadingZeros(AValue: UInt32): Int32;
+{$IFNDEF FPC}
+var
+  LN: UInt32;
+{$ENDIF FPC}
+begin
+  if (AValue = 0) then
+  begin
+    Result := ((not AValue) shr (31 - 5)) and (1 shl 5);
+    Exit;
+  end;
+{$IFDEF FPC}
+  Result := BsrDWord(AValue) xor ((System.SizeOf(UInt32) * 8) - 1);
+  // this also works
+  //Result := ((SizeOf(UInt32) * 8) - 1) - BsrDWord(AValue);
+  // Result := ((System.SizeOf(UInt32) * 8) - 1) - BsrDWord(AValue);
+{$ELSE}
+  LN := 1;
+
+  if ((AValue shr 16) = 0) then
+  begin
+    LN := LN + 16;
+    AValue := AValue shl 16;
+  end;
+
+  if ((AValue shr 24) = 0) then
+  begin
+    LN := LN + 8;
+    AValue := AValue shl 8;
+  end;
+
+  if ((AValue shr 28) = 0) then
+  begin
+    LN := LN + 4;
+    AValue := AValue shl 4;
+  end;
+
+  if ((AValue shr 30) = 0) then
+  begin
+    LN := LN + 2;
+    AValue := AValue shl 2;
+  end;
+
+  Result := Int32(LN) - Int32(AValue shr 31);
+{$ENDIF FPC}
+end;
+
+class function TBitUtilities.NumberOfTrailingZeros(AValue: UInt32): Int32;
+{$IFNDEF FPC}
+var
+  LN: UInt32;
+{$ENDIF FPC}
+begin
+  if (AValue = 0) then
+  begin
+    Result := ((not AValue) shr (31 - 5)) and (1 shl 5);
+    Exit;
+  end;
+{$IFDEF FPC}
+  Result := BsfDWord(AValue);
+{$ELSE}
+  LN := 0;
+
+  if ((AValue and $0000FFFF) = 0) then
+  begin
+    LN := LN + 16;
+    AValue := AValue shr 16;
+  end;
+
+  if ((AValue and $000000FF) = 0) then
+  begin
+    LN := LN + 8;
+    AValue := AValue shr 8;
+  end;
+
+  if ((AValue and $0000000F) = 0) then
+  begin
+    LN := LN + 4;
+    AValue := AValue shr 4;
+  end;
+
+  if ((AValue and $00000003) = 0) then
+  begin
+    LN := LN + 2;
+    AValue := AValue shr 2;
+  end;
+
+  Result := Int32(LN + (AValue and 1) xor 1);
+{$ENDIF FPC}
+end;
+
+
+class function TBitUtilities.PopCount(AValue: UInt32): Int32;
+begin
+{$IFDEF FPC}
+  Result := PopCnt(AValue);
+{$ELSE}
+  AValue := AValue - ((AValue shr 1) and UInt32($55555555));
+  AValue := (AValue and UInt32($33333333)) + ((AValue shr 2) and UInt32($33333333));
+  AValue := (AValue + (AValue shr 4)) and UInt32($0F0F0F0F);
+  AValue := AValue + (AValue shr 8);
+  AValue := AValue + (AValue shr 16);
+  AValue := AValue and UInt32($3F);
+  Result := Int32(AValue);
+{$ENDIF FPC}
+end;
+
+end.

+ 7 - 7
CryptoLib/src/GeneralUtilities/ClpConverters.pas

@@ -26,7 +26,7 @@ uses
   StrUtils,
   SysUtils,
   ClpCryptoLibTypes,
-  ClpBits,
+  ClpBitUtilities,
   ClpBitConverter;
 
 resourcestring
@@ -156,7 +156,7 @@ begin
     ldest := PCardinal(PByte(dest) + dest_index);
     while lsrc < lend do
     begin
-      ldest^ := TBits.ReverseBytesUInt32(lsrc^);
+      ldest^ := TBitUtilities.ReverseBytesUInt32(lsrc^);
       System.Inc(ldest);
       System.Inc(lsrc);
     end;
@@ -195,7 +195,7 @@ begin
     ldest := PUInt64(PByte(dest) + dest_index);
     while lsrc < lend do
     begin
-      ldest^ := TBits.ReverseBytesUInt64(lsrc^);
+      ldest^ := TBitUtilities.ReverseBytesUInt64(lsrc^);
       System.Inc(ldest);
       System.Inc(lsrc);
     end;
@@ -220,7 +220,7 @@ end;
 class function TConverters.be2me_32(x: UInt32): UInt32;
 begin
   if TBitConverter.IsLittleEndian then
-    result := TBits.ReverseBytesUInt32(x)
+    result := TBitUtilities.ReverseBytesUInt32(x)
   else
     result := x;
 end;
@@ -228,7 +228,7 @@ end;
 class function TConverters.be2me_64(x: UInt64): UInt64;
 begin
   if TBitConverter.IsLittleEndian then
-    result := TBits.ReverseBytesUInt64(x)
+    result := TBitUtilities.ReverseBytesUInt64(x)
   else
     result := x;
 end;
@@ -256,7 +256,7 @@ end;
 class function TConverters.le2me_32(x: UInt32): UInt32;
 begin
   if not TBitConverter.IsLittleEndian then
-    result := TBits.ReverseBytesUInt32(x)
+    result := TBitUtilities.ReverseBytesUInt32(x)
   else
     result := x;
 end;
@@ -264,7 +264,7 @@ end;
 class function TConverters.le2me_64(x: UInt64): UInt64;
 begin
   if not TBitConverter.IsLittleEndian then
-    result := TBits.ReverseBytesUInt64(x)
+    result := TBitUtilities.ReverseBytesUInt64(x)
   else
     result := x;
 end;

+ 8 - 8
CryptoLib/src/GeneralUtilities/ClpStringUtilities.pas

@@ -24,7 +24,7 @@ interface
 uses
   SysUtils,
   StrUtils,
-  ClpBits,
+  ClpBitUtilities,
   ClpCryptoLibTypes;
 
 type
@@ -116,19 +116,19 @@ implementation
 
 class function TStringUtilities.GetStringHashCode(const AInput: string): Int32;
 var
-  LLowPoint, LHighPoint: Int32;
+  LStart, LEnd: Int32;
   LResult: UInt32;
 begin
   LResult := 0;
 
-  LLowPoint := 1;
-  LHighPoint := System.Length(AInput);
+  LStart := 1;
+  LEnd := System.Length(AInput);
 
-  while LLowPoint <= LHighPoint do
+  while LStart <= LEnd do
   begin
-    LResult := TBits.RotateLeft32(LResult, 5);
-    LResult := LResult xor UInt32(AInput[LLowPoint]);
-    System.Inc(LLowPoint);
+    LResult := TBitUtilities.RotateLeft32(LResult, 5);
+    LResult := LResult xor UInt32(AInput[LStart]);
+    System.Inc(LStart);
   end;
   Result := Int32(LResult);
 end;

+ 17 - 32
CryptoLib/src/Math/ClpBigInteger.pas

@@ -26,7 +26,7 @@ uses
   Math,
   ClpCryptoLibTypes,
   ClpConverters,
-  ClpBits,
+  ClpBitUtilities,
   ClpArrayUtilities,
   ClpISecureRandom,
   ClpIRandom;
@@ -445,13 +445,13 @@ end;
 
 class function TBigInteger.PopCount(const AValue: UInt32): Int32;
 begin
-  Result := TBits.PopCount(AValue);
+  Result := TBitUtilities.PopCount(AValue);
 end;
 
 class function TBigInteger.BitLen(const AValue: Byte): Int32;
 begin
   //Result := BitLengthTable[AValue];
-  Result := 32 - TBits.NumberOfLeadingZeros(AValue);
+  Result := 32 - TBitUtilities.NumberOfLeadingZeros(AValue);
 end;
 
 class function TBigInteger.BitLen(const AValue: UInt32): Int32;
@@ -477,7 +477,7 @@ begin
     Exit;
   end;
   Result := BitLengthTable[AValue]; *)
-  Result := 32 - TBits.NumberOfLeadingZeros(AValue);
+  Result := 32 - TBitUtilities.NumberOfLeadingZeros(AValue);
 end;
 
 class function TBigInteger.CreateUValueOf(const AValue: UInt32): TBigInteger;
@@ -852,7 +852,7 @@ begin
     System.Dec(LIV);
     LM := Int64(AX[LIT] and UIMASK) - Int64(AY[LIV] and UIMASK) + LBorrow;
     AX[LIT] := UInt32(LM);
-    LBorrow := Int32(TBits.Asr64(LM, 63));
+    LBorrow := Int32(TBitUtilities.Asr64(LM, 63));
   until LIV <= AYStart;
   if LBorrow <> 0 then
   begin
@@ -891,10 +891,7 @@ begin
     begin
       LNewMag[I] := AMag[I];
     end;
-    for I := LMagLen to System.Pred(System.Length(LNewMag)) do
-    begin
-      LNewMag[I] := 0;
-    end;
+    TArrayUtilities.Fill<UInt32>(LNewMag, LMagLen, System.Length(LNewMag), UInt32(0));
   end
   else
   begin
@@ -1061,10 +1058,7 @@ begin
     begin
       AMag[I] := AMag[I - LDelta];
     end;
-    for I := LNInts - 1 downto AStart do
-    begin
-      AMag[I] := 0;
-    end;
+    TArrayUtilities.Fill<UInt32>(AMag, AStart, LNInts, UInt32(0));
   end;
   if LNBits <> 0 then
   begin
@@ -1111,7 +1105,7 @@ begin
 
     if LShift > 0 then
     begin
-      System.SetLength(LICount, (TBits.Asr32(LShift, 5)) + 1);
+      System.SetLength(LICount, (TBitUtilities.Asr32(LShift, 5)) + 1);
       LICount[0] := UInt32(1) shl (LShift mod 32);
       LC := ShiftLeft(AY, LShift);
       LCBitLength := LCBitLength + LShift;
@@ -1195,10 +1189,7 @@ begin
   if LXYCmp = 0 then
   begin
     LCount := AddMagnitudes(LCount, FOne.FMagnitude);
-    for I := LXStart to System.Pred(System.Length(AX)) do
-    begin
-      AX[I] := 0;
-    end;
+    TArrayUtilities.Fill<UInt32>(AX, LXStart, System.Length(AX), UInt32(0));
   end;
   Result := LCount;
 end;
@@ -1303,10 +1294,7 @@ begin
   end;
   if LXYCmp = 0 then
   begin
-    for I := LXStart to System.Pred(System.Length(AX)) do
-    begin
-      AX[I] := 0;
-    end;
+    TArrayUtilities.Fill<UInt32>(AX, LXStart, System.Length(AX), UInt32(0));
   end;
   Result := AX;
 end;
@@ -1960,10 +1948,7 @@ begin
   end;
   LResLength := System.Length(FMagnitude) + System.Length(AValue.FMagnitude);
   System.SetLength(LRes, LResLength);
-  for I := 0 to System.Pred(System.Length(LRes)) do
-  begin
-    LRes[I] := 0;
-  end;
+  TArrayUtilities.Fill<UInt32>(LRes, 0, System.Length(LRes), UInt32(0));
   Multiply(LRes, FMagnitude, AValue.FMagnitude);
   LResSign := FSign xor AValue.FSign xor 1;
   Result := TBigInteger.Create(LResSign, LRes, True);
@@ -2235,7 +2220,7 @@ begin
     begin
       LY := LY.Multiply(LZ);
     end;
-    LExp := TBits.Asr32(LExp, 1);
+    LExp := TBitUtilities.Asr32(LExp, 1);
     if LExp = 0 then
       Break;
     LZ := LZ.Multiply(LZ);
@@ -2386,9 +2371,9 @@ begin
       Result := FZero;
     Exit;
   end;
-  LResultLength := TBits.Asr32((BitLength - AN + 31), 5);
+  LResultLength := TBitUtilities.Asr32((BitLength - AN + 31), 5);
   System.SetLength(LRes, LResultLength);
-  LNInts := TBits.Asr32(AN, 5);
+  LNInts := TBitUtilities.Asr32(AN, 5);
   LNBits := AN and 31;
   if LNBits = 0 then
   begin
@@ -2676,7 +2661,7 @@ var
 begin
   // Clone magnitude
   LMag := System.Copy(FMagnitude);
-  LMag[System.Length(LMag) - 1 - (TBits.Asr32(AN, 5))] := LMag[System.Length(LMag) - 1 - (TBits.Asr32(AN, 5))] xor (UInt32(1) shl (AN and 31));
+  LMag[System.Length(LMag) - 1 - (TBitUtilities.Asr32(AN, 5))] := LMag[System.Length(LMag) - 1 - (TBitUtilities.Asr32(AN, 5))] xor (UInt32(1) shl (AN and 31));
   Result := TBigInteger.Create(FSign, LMag, False);
 end;
 
@@ -2710,7 +2695,7 @@ begin
     LOffset := LOffset + 32;
   end;
 
-  LOffset := LOffset + TBits.NumberOfTrailingZeros(LWord);
+  LOffset := LOffset + TBitUtilities.NumberOfTrailingZeros(LWord);
  (*
   while (LWord and $FF) = 0 do
   begin
@@ -2851,7 +2836,7 @@ begin
     LMult := LMult shr 1;
     System.Inc(LZeros);
   end; *)
-  LTZ := TBits.NumberOfTrailingZeros(LMult);
+  LTZ := TBitUtilities.NumberOfTrailingZeros(LMult);
   LMult := LMult shr LTZ;
   LZeros := LZeros + UInt32(LTZ);
   // Combine multiplier and zeros: mult | (zeros << 8)

+ 8 - 8
CryptoLib/src/Math/EC/ClpECAlgorithms.pas

@@ -26,7 +26,7 @@ uses
   Math,
   ClpCryptoLibTypes,
   ClpBigInteger,
-  ClpBits,
+  ClpBitUtilities,
   ClpNat,
   ClpIECC,
   ClpECCompUtilities,
@@ -260,12 +260,12 @@ begin
     while j >= 0 do
     begin
 
-      secretBitK := BigK[TBits.Asr32(j, 5)] shr (j and $1F);
+      secretBitK := BigK[TBitUtilities.Asr32(j, 5)] shr (j and $1F);
       secretIndexK := secretIndexK xor (secretBitK shr 1);
       secretIndexK := secretIndexK shl 1;
       secretIndexK := secretIndexK xor secretBitK;
 
-      secretBitL := BigL[TBits.Asr32(j, 5)] shr (j and $1F);
+      secretBitL := BigL[TBitUtilities.Asr32(j, 5)] shr (j and $1F);
       secretIndexL := secretIndexL xor (secretBitL shr 1);
       secretIndexL := secretIndexL shl 1;
       secretIndexL := secretIndexL xor secretBitL;
@@ -319,8 +319,8 @@ begin
     jsfi := jsf[i];
 
     // NOTE: The shifting ensures the sign is extended correctly
-    kDigit := (TBits.Asr32((jsfi shl 24), 28));
-    lDigit := (TBits.Asr32((jsfi shl 28), 28));
+    kDigit := (TBitUtilities.Asr32((jsfi shl 24), 28));
+    lDigit := (TBitUtilities.Asr32((jsfi shl 28), 28));
 
     index := 4 + (kDigit * 3) + lDigit;
     R := R.TwicePlus(table[index]);
@@ -540,7 +540,7 @@ begin
         tableP := preCompP;
       end;
 
-      point := point.Add(tableP[TBits.Asr32(nP, 1)]);
+      point := point.Add(tableP[TBitUtilities.Asr32(nP, 1)]);
     end;
     if (wiQ <> 0) then
     begin
@@ -555,7 +555,7 @@ begin
         tableQ := preCompQ;
       end;
 
-      point := point.Add(tableQ[TBits.Asr32(nQ, 1)]);
+      point := point.Add(tableQ[TBitUtilities.Asr32(nQ, 1)]);
 
     end;
 
@@ -723,7 +723,7 @@ begin
           table := info.PreCompNeg;
         end;
 
-        point := point.Add(table[TBits.Asr32(n, 1)]);
+        point := point.Add(table[TBitUtilities.Asr32(n, 1)]);
       end;
     end;
 

+ 12 - 12
CryptoLib/src/Math/EC/ClpECC.pas

@@ -33,7 +33,7 @@ uses
   ClpCryptoLibTypes,
   ClpBigInteger,
   ClpBigIntegers,
-  ClpBits,
+  ClpBitUtilities,
   ClpIGlvEndomorphism,
   ClpECAlgorithms,
   ClpLongArray,
@@ -2217,7 +2217,7 @@ var
   P, n, z: TCryptoLibUInt32Array;
 begin
   bits := FieldSize;
-  len := TBits.Asr32((bits + 31), 5);
+  len := TBitUtilities.Asr32((bits + 31), 5);
   P := TNat.FromBigInteger(bits, Q);
   n := TNat.FromBigInteger(bits, x);
   z := TNat.Create(len);
@@ -2505,8 +2505,8 @@ begin
     raise EArgumentCryptoLibException.CreateRes(@SHalfTraceUndefinedForM);
   end;
 
-  n := TBits.Asr32((m + 1), 1);
-  K := 31 - TBits.NumberOfLeadingZeros(n);
+  n := TBitUtilities.Asr32((m + 1), 1);
+  K := 31 - TBitUtilities.NumberOfLeadingZeros(n);
   nk := 1;
 
   ht := Self as IECFieldElement;
@@ -2514,7 +2514,7 @@ begin
   begin
     ht := ht.SquarePow(nk shl 1).Add(ht);
     System.Dec(K);
-    nk := TBits.Asr32(n, K);
+    nk := TBitUtilities.Asr32(n, K);
 
     if ((nk and 1) <> 0) then
     begin
@@ -2537,7 +2537,7 @@ var
 begin
   m := FieldSize;
 
-  K := 31 - TBits.NumberOfLeadingZeros(m);
+  K := 31 - TBitUtilities.NumberOfLeadingZeros(m);
   mk := 1;
 
   tr := Self as IECFieldElement;
@@ -2546,7 +2546,7 @@ begin
     tr := tr.SquarePow(mk).Add(tr);
 
     System.Dec(K);
-    mk := TBits.Asr32(m, K);
+    mk := TBitUtilities.Asr32(m, K);
 
     if ((mk and 1) <> 0) then
     begin
@@ -2856,8 +2856,8 @@ function TECCurve.GetHashCode: {$IFDEF DELPHI}Int32; {$ELSE}PtrInt;
 {$ENDIF DELPHI}
 begin
   result := (field as TObject).GetHashCode()
-    xor Int32(TBits.RotateLeft32(a.ToBigInteger().GetHashCode(), 8))
-    xor Int32(TBits.RotateLeft32(b.ToBigInteger().GetHashCode(), 16));
+    xor Int32(TBitUtilities.RotateLeft32(a.ToBigInteger().GetHashCode(), 8))
+    xor Int32(TBitUtilities.RotateLeft32(b.ToBigInteger().GetHashCode(), 16));
 end;
 
 function TECCurve.GetMultiplier: IECMultiplier;
@@ -3772,7 +3772,7 @@ begin
   for i := 0 to System.Pred(Fm_size) do
   begin
 
-    MASK := Byte(TBits.Asr32((i xor index) - 1, 31));
+    MASK := Byte(TBitUtilities.Asr32((i xor index) - 1, 31));
 
     for j := 0 to System.Pred(FE_BYTES) do
     begin
@@ -3866,7 +3866,7 @@ begin
   for i := 0 to System.Pred(Fm_size) do
   begin
 
-    MASK := TBits.Asr32((i xor index) - 1, 31);
+    MASK := TBitUtilities.Asr32((i xor index) - 1, 31);
 
     for j := 0 to System.Pred(FE_LONGS) do
     begin
@@ -5031,7 +5031,7 @@ begin
 
         b := ecCurve.b;
 
-        if (b.BitLength < (TBits.Asr32(ecCurve.FieldSize, 1))) then
+        if (b.BitLength < (TBitUtilities.Asr32(ecCurve.FieldSize, 1))) then
         begin
           t1 := L1.Add(x1).Square();
 

+ 6 - 6
CryptoLib/src/Math/EC/ClpECCompUtilities.pas

@@ -24,7 +24,7 @@ interface
 uses
   SysUtils,
   Math,
-  ClpBits,
+  ClpBitUtilities,
   ClpIECC,
   ClpIPreCompInfo,
   ClpIWNafPreCompInfo,
@@ -493,7 +493,7 @@ var
   bits, highBit, &length, zeroes, i, digit: Int32;
   naf: TCryptoLibInt32Array;
 begin
-  if ((TBits.Asr32(k.BitLength, 16)) <> 0) then
+  if ((TBitUtilities.Asr32(k.BitLength, 16)) <> 0) then
   begin
     raise EArgumentCryptoLibException.CreateRes(@SInvalidBitLength);
   end;
@@ -506,7 +506,7 @@ begin
   _3k := k.ShiftLeft(1).Add(k);
 
   bits := _3k.BitLength;
-  System.SetLength(naf, TBits.Asr32(bits, 1));
+  System.SetLength(naf, TBitUtilities.Asr32(bits, 1));
 
   diff := _3k.&Xor(k);
 
@@ -572,7 +572,7 @@ begin
   begin
     raise EArgumentCryptoLibException.CreateRes(@SInvalidRange);
   end;
-  if ((TBits.Asr32(LK.BitLength, 16)) <> 0) then
+  if ((TBitUtilities.Asr32(LK.BitLength, 16)) <> 0) then
   begin
     raise EArgumentCryptoLibException.CreateRes(@SInvalidBitLength);
   end;
@@ -587,7 +587,7 @@ begin
   // 2^width and a mask and sign bit set accordingly
   pow2 := 1 shl width;
   mask := pow2 - 1;
-  sign := TBits.Asr32(pow2, 1);
+  sign := TBitUtilities.Asr32(pow2, 1);
 
   carry := false;
   length := 0;
@@ -788,7 +788,7 @@ begin
   // 2^width and a mask and sign bit set accordingly
   pow2 := 1 shl width;
   mask := pow2 - 1;
-  sign := TBits.Asr32(pow2, 1);
+  sign := TBitUtilities.Asr32(pow2, 1);
 
   carry := false;
   length := 0;

+ 5 - 5
CryptoLib/src/Math/EC/ClpLongArray.pas

@@ -26,7 +26,7 @@ uses
   SysUtils,
   StrUtils,
   Math,
-  ClpBits,
+  ClpBitUtilities,
   ClpBigInteger,
   ClpCryptoLibTypes;
 
@@ -718,7 +718,7 @@ var
   mLen, numBits, excessBits, kLen, kMax, kNext, wordWiseLimit, vectorableWords,
     vectorWiseWords: Int32;
 begin
-  mLen := TBits.Asr32((m + 63), 6);
+  mLen := TBitUtilities.Asr32((m + 63), 6);
   if (len < mLen) then
   begin
     Result := len;
@@ -745,7 +745,7 @@ begin
   end;
 
   wordWiseLimit := Math.Max(m, kMax + 64);
-  vectorableWords := TBits.Asr32((excessBits + Math.Min(numBits - wordWiseLimit,
+  vectorableWords := TBitUtilities.Asr32((excessBits + Math.Min(numBits - wordWiseLimit,
     m - kNext)), 6);
   if (vectorableWords > 1) then
   begin
@@ -1529,7 +1529,7 @@ begin
   // u(z) := a(z)
   uz := Copy();
 
-  t := TBits.Asr32((m + 63), 6);
+  t := TBitUtilities.Asr32((m + 63), 6);
 
   // v(z) := f(z)
   vz := TLongArray.Create(t);
@@ -2231,7 +2231,7 @@ begin
     Exit;
   end;
 
-  mLen := TBits.Asr32((m + 63), 6);
+  mLen := TBitUtilities.Asr32((m + 63), 6);
 
   System.SetLength(r, mLen shl 1);
   System.Move(Fm_ints[0], r[0], len * System.SizeOf(Int64));

+ 2 - 2
CryptoLib/src/Math/EC/Custom/Djb/ClpCurve25519Custom.pas

@@ -30,7 +30,7 @@ uses
   ClpBigInteger,
   ClpICurve25519Custom,
   ClpECCurveConstants,
-  ClpBits,
+  ClpBitUtilities,
   ClpEncoders,
   ClpArrayUtilities,
   ClpCryptoLibTypes;
@@ -1373,7 +1373,7 @@ begin
 
   for i := 0 to System.Pred(Fm_size) do
   begin
-    MASK := UInt32(TBits.Asr32((i xor index) - 1, 31));
+    MASK := UInt32(TBitUtilities.Asr32((i xor index) - 1, 31));
 
     for J := 0 to System.Pred(CURVE25519_FE_INTS) do
     begin

+ 2 - 2
CryptoLib/src/Math/EC/Custom/Sec/ClpSecP256K1Custom.pas

@@ -25,7 +25,7 @@ uses
   ClpMod,
   ClpEncoders,
   ClpNat,
-  ClpBits,
+  ClpBitUtilities,
   ClpNat256,
   ClpECC,
   ClpBigInteger,
@@ -1161,7 +1161,7 @@ begin
 
   for i := 0 to System.Pred(Fm_size) do
   begin
-    MASK := UInt32(TBits.Asr32((i xor index) - 1, 31));
+    MASK := UInt32(TBitUtilities.Asr32((i xor index) - 1, 31));
 
     for J := 0 to System.Pred(SECP256K1_FE_INTS) do
     begin

+ 34 - 34
CryptoLib/src/Math/EC/Custom/Sec/ClpSecP256R1Custom.pas

@@ -25,7 +25,7 @@ uses
   ClpMod,
   ClpEncoders,
   ClpNat,
-  ClpBits,
+  ClpBitUtilities,
   ClpNat256,
   ClpECC,
   ClpBigInteger,
@@ -330,34 +330,34 @@ var
 begin
   c := Int64(z[0]) + 1;
   z[0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   if (c <> 0) then
   begin
     c := c + Int64(z[1]);
     z[1] := UInt32(c);
-    c := TBits.Asr64(c, 32);
+    c := TBitUtilities.Asr64(c, 32);
     c := c + Int64(z[2]);
     z[2] := UInt32(c);
-    c := TBits.Asr64(c, 32);
+    c := TBitUtilities.Asr64(c, 32);
   end;
   c := c + (Int64(z[3]) - 1);
   z[3] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   if (c <> 0) then
   begin
     c := c + Int64(z[4]);
     z[4] := UInt32(c);
-    c := TBits.Asr64(c, 32);
+    c := TBitUtilities.Asr64(c, 32);
     c := c + Int64(z[5]);
     z[5] := UInt32(c);
-    c := TBits.Asr64(c, 32);
+    c := TBitUtilities.Asr64(c, 32);
   end;
   c := c + (Int64(z[6]) - 1);
   z[6] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[7]) + 1);
   z[7] := UInt32(c);
-  // c := TBits.Asr64(c, 32);
+  // c := TBitUtilities.Asr64(c, 32);
 end;
 
 class procedure TSecP256R1Field.Boot;
@@ -429,28 +429,28 @@ begin
 
   cc := cc + (Int64(xx[0]) - t3 - t7);
   z[0] := UInt32(cc);
-  cc := TBits.Asr64(cc, 32);
+  cc := TBitUtilities.Asr64(cc, 32);
   cc := cc + (Int64(xx[1]) + t1 - t4 - t6);
   z[1] := UInt32(cc);
-  cc := TBits.Asr64(cc, 32);
+  cc := TBitUtilities.Asr64(cc, 32);
   cc := cc + (Int64(xx[2]) + t2 - t5);
   z[2] := UInt32(cc);
-  cc := TBits.Asr64(cc, 32);
+  cc := TBitUtilities.Asr64(cc, 32);
   cc := cc + (Int64(xx[3]) + (t3 shl 1) + t7 - t6);
   z[3] := UInt32(cc);
-  cc := TBits.Asr64(cc, 32);
+  cc := TBitUtilities.Asr64(cc, 32);
   cc := cc + (Int64(xx[4]) + (t4 shl 1) + xx14 - t1);
   z[4] := UInt32(cc);
-  cc := TBits.Asr64(cc, 32);
+  cc := TBitUtilities.Asr64(cc, 32);
   cc := cc + (Int64(xx[5]) + (t5 shl 1) - t2);
   z[5] := UInt32(cc);
-  cc := TBits.Asr64(cc, 32);
+  cc := TBitUtilities.Asr64(cc, 32);
   cc := cc + (Int64(xx[6]) + (t6 shl 1) + t7);
   z[6] := UInt32(cc);
-  cc := TBits.Asr64(cc, 32);
+  cc := TBitUtilities.Asr64(cc, 32);
   cc := cc + (Int64(xx[7]) + (xx15 shl 1) + xx08 - t2 - t4);
   z[7] := UInt32(cc);
-  cc := TBits.Asr64(cc, 32);
+  cc := TBitUtilities.Asr64(cc, 32);
   cc := cc + n;
 
 {$IFDEF DEBUG}
@@ -505,34 +505,34 @@ begin
 
     cc := cc + (Int64(z[0]) + xx08);
     z[0] := UInt32(cc);
-    cc := TBits.Asr64(cc, 32);
+    cc := TBitUtilities.Asr64(cc, 32);
     if (cc <> 0) then
     begin
       cc := cc + Int64(z[1]);
       z[1] := UInt32(cc);
-      cc := TBits.Asr64(cc, 32);
+      cc := TBitUtilities.Asr64(cc, 32);
       cc := cc + Int64(z[2]);
       z[2] := UInt32(cc);
-      cc := TBits.Asr64(cc, 32);
+      cc := TBitUtilities.Asr64(cc, 32);
     end;
     cc := cc + (Int64(z[3]) - xx08);
     z[3] := UInt32(cc);
-    cc := TBits.Asr64(cc, 32);
+    cc := TBitUtilities.Asr64(cc, 32);
     if (cc <> 0) then
     begin
       cc := cc + Int64(z[4]);
       z[4] := UInt32(cc);
-      cc := TBits.Asr64(cc, 32);
+      cc := TBitUtilities.Asr64(cc, 32);
       cc := cc + Int64(z[5]);
       z[5] := UInt32(cc);
-      cc := TBits.Asr64(cc, 32);
+      cc := TBitUtilities.Asr64(cc, 32);
     end;
     cc := cc + (Int64(z[6]) - xx08);
     z[6] := UInt32(cc);
-    cc := TBits.Asr64(cc, 32);
+    cc := TBitUtilities.Asr64(cc, 32);
     cc := cc + (Int64(z[7]) + xx08);
     z[7] := UInt32(cc);
-    cc := TBits.Asr64(cc, 32);
+    cc := TBitUtilities.Asr64(cc, 32);
 
 {$IFDEF DEBUG}
     System.Assert((cc = 0) or (cc = 1));
@@ -581,34 +581,34 @@ var
 begin
   c := Int64(z[0]) - 1;
   z[0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   if (c <> 0) then
   begin
     c := c + Int64(z[1]);
     z[1] := UInt32(c);
-    c := TBits.Asr64(c, 32);
+    c := TBitUtilities.Asr64(c, 32);
     c := c + Int64(z[2]);
     z[2] := UInt32(c);
-    c := TBits.Asr64(c, 32);
+    c := TBitUtilities.Asr64(c, 32);
   end;
   c := c + (Int64(z[3]) + 1);
   z[3] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   if (c <> 0) then
   begin
     c := c + Int64(z[4]);
     z[4] := UInt32(c);
-    c := TBits.Asr64(c, 32);
+    c := TBitUtilities.Asr64(c, 32);
     c := c + Int64(z[5]);
     z[5] := UInt32(c);
-    c := TBits.Asr64(c, 32);
+    c := TBitUtilities.Asr64(c, 32);
   end;
   c := c + (Int64(z[6]) + 1);
   z[6] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[7]) - 1);
   z[7] := UInt32(c);
-  // c := TBits.Asr64(c, 32);
+  // c := TBitUtilities.Asr64(c, 32);
 end;
 
 class procedure TSecP256R1Field.Subtract(const x, y, z: TCryptoLibUInt32Array);
@@ -1304,7 +1304,7 @@ begin
 
   for i := 0 to System.Pred(Fm_size) do
   begin
-    MASK := UInt32(TBits.Asr32((i xor index) - 1, 31));
+    MASK := UInt32(TBitUtilities.Asr32((i xor index) - 1, 31));
 
     for J := 0 to System.Pred(SECP256R1_FE_INTS) do
     begin

+ 29 - 29
CryptoLib/src/Math/EC/Custom/Sec/ClpSecP384R1Custom.pas

@@ -25,7 +25,7 @@ uses
   ClpMod,
   ClpEncoders,
   ClpNat,
-  ClpBits,
+  ClpBitUtilities,
   ClpNat384,
   ClpECC,
   ClpBigInteger,
@@ -295,22 +295,22 @@ var
 begin
   c := Int64(z[0]) + 1;
   z[0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[1]) - 1);
   z[1] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   if (c <> 0) then
   begin
     c := c + Int64(z[2]);
     z[2] := UInt32(c);
-    c := TBits.Asr64(c, 32);
+    c := TBitUtilities.Asr64(c, 32);
   end;
   c := c + (Int64(z[3]) + 1);
   z[3] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[4]) + 1);
   z[4] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   if (c <> 0) then
   begin
     TNat.IncAt(12, z, 5);
@@ -339,22 +339,22 @@ var
 begin
   c := Int64(z[0]) - 1;
   z[0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[1]) + 1);
   z[1] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   if (c <> 0) then
   begin
     c := c + Int64(z[2]);
     z[2] := UInt32(c);
-    c := TBits.Asr64(c, 32);
+    c := TBitUtilities.Asr64(c, 32);
   end;
   c := c + (Int64(z[3]) - 1);
   z[3] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[4]) - 1);
   z[4] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   if (c <> 0) then
   begin
     TNat.DecAt(12, z, 5);
@@ -438,22 +438,22 @@ begin
 
     cc := cc + (Int64(z[0]) + xx12);
     z[0] := UInt32(cc);
-    cc := TBits.Asr64(cc, 32);
+    cc := TBitUtilities.Asr64(cc, 32);
     cc := cc + (Int64(z[1]) - xx12);
     z[1] := UInt32(cc);
-    cc := TBits.Asr64(cc, 32);
+    cc := TBitUtilities.Asr64(cc, 32);
     if (cc <> 0) then
     begin
       cc := cc + Int64(z[2]);
       z[2] := UInt32(cc);
-      cc := TBits.Asr64(cc, 32);
+      cc := TBitUtilities.Asr64(cc, 32);
     end;
     cc := cc + (Int64(z[3]) + xx12);
     z[3] := UInt32(cc);
-    cc := TBits.Asr64(cc, 32);
+    cc := TBitUtilities.Asr64(cc, 32);
     cc := cc + (Int64(z[4]) + xx12);
     z[4] := UInt32(cc);
-    cc := TBits.Asr64(cc, 32);
+    cc := TBitUtilities.Asr64(cc, 32);
 
 {$IFDEF DEBUG}
     System.Assert((cc = 0) or (cc = 1));
@@ -496,40 +496,40 @@ begin
   cc := 0;
   cc := cc + (Int64(xx[0]) + t7);
   z[0] := UInt32(cc);
-  cc := TBits.Asr64(cc, 32);
+  cc := TBitUtilities.Asr64(cc, 32);
   cc := cc + (Int64(xx[1]) + xx23 - t0 + t1);
   z[1] := UInt32(cc);
-  cc := TBits.Asr64(cc, 32);
+  cc := TBitUtilities.Asr64(cc, 32);
   cc := cc + (Int64(xx[2]) - xx21 - t1 + t2);
   z[2] := UInt32(cc);
-  cc := TBits.Asr64(cc, 32);
+  cc := TBitUtilities.Asr64(cc, 32);
   cc := cc + (Int64(xx[3]) - t2 + t3 + t7);
   z[3] := UInt32(cc);
-  cc := TBits.Asr64(cc, 32);
+  cc := TBitUtilities.Asr64(cc, 32);
   cc := cc + (Int64(xx[4]) + xx16 + xx21 + t1 - t3 + t7);
   z[4] := UInt32(cc);
-  cc := TBits.Asr64(cc, 32);
+  cc := TBitUtilities.Asr64(cc, 32);
   cc := cc + (Int64(xx[5]) - xx16 + t1 + t2 + t4);
   z[5] := UInt32(cc);
-  cc := TBits.Asr64(cc, 32);
+  cc := TBitUtilities.Asr64(cc, 32);
   cc := cc + (Int64(xx[6]) + xx18 - xx17 + t2 + t3);
   z[6] := UInt32(cc);
-  cc := TBits.Asr64(cc, 32);
+  cc := TBitUtilities.Asr64(cc, 32);
   cc := cc + (Int64(xx[7]) + xx16 + xx19 - xx18 + t3);
   z[7] := UInt32(cc);
-  cc := TBits.Asr64(cc, 32);
+  cc := TBitUtilities.Asr64(cc, 32);
   cc := cc + (Int64(xx[8]) + xx16 + xx17 + xx20 - xx19);
   z[8] := UInt32(cc);
-  cc := TBits.Asr64(cc, 32);
+  cc := TBitUtilities.Asr64(cc, 32);
   cc := cc + (Int64(xx[9]) + xx18 - xx20 + t4);
   z[9] := UInt32(cc);
-  cc := TBits.Asr64(cc, 32);
+  cc := TBitUtilities.Asr64(cc, 32);
   cc := cc + (Int64(xx[10]) + xx18 + xx19 - t5 + t6);
   z[10] := UInt32(cc);
-  cc := TBits.Asr64(cc, 32);
+  cc := TBitUtilities.Asr64(cc, 32);
   cc := cc + (Int64(xx[11]) + xx19 + xx20 - t6);
   z[11] := UInt32(cc);
-  cc := TBits.Asr64(cc, 32);
+  cc := TBitUtilities.Asr64(cc, 32);
   cc := cc + (n);
 
 {$IFDEF DEBUG}
@@ -1312,7 +1312,7 @@ begin
 
   for i := 0 to System.Pred(Fm_size) do
   begin
-    MASK := UInt32(TBits.Asr32((i xor index) - 1, 31));
+    MASK := UInt32(TBitUtilities.Asr32((i xor index) - 1, 31));
 
     for J := 0 to System.Pred(SECP384R1_FE_INTS) do
     begin

+ 2 - 2
CryptoLib/src/Math/EC/Custom/Sec/ClpSecP521R1Custom.pas

@@ -25,7 +25,7 @@ uses
   ClpNat,
   ClpMod,
   ClpEncoders,
-  ClpBits,
+  ClpBitUtilities,
   ClpNat512,
   ClpECC,
   ClpBigInteger,
@@ -1105,7 +1105,7 @@ begin
 
   for i := 0 to System.Pred(Fm_size) do
   begin
-    MASK := UInt32(TBits.Asr32((i xor index) - 1, 31));
+    MASK := UInt32(TBitUtilities.Asr32((i xor index) - 1, 31));
 
     for J := 0 to System.Pred(SECP521R1_FE_INTS) do
     begin

+ 4 - 4
CryptoLib/src/Math/EC/Custom/Sec/ClpSecT283Custom.pas

@@ -22,7 +22,7 @@ unit ClpSecT283Custom;
 interface
 
 uses
-  ClpBits,
+  ClpBitUtilities,
   ClpEncoders,
   ClpNat,
   ClpNat320,
@@ -716,13 +716,13 @@ begin
     j := UInt32(x shr k);
     g := u[j and 7] xor u[(j shr 3) and 7] shl 3 xor u[(j shr 6) and 7] shl 6;
     l := l xor ((g shl k));
-    h := h xor TBits.NegativeRightShift64(g, -k);
+    h := h xor TBitUtilities.NegativeRightShift64(g, -k);
 
     System.Dec(k, 9);
   until not(k > 0);
 
   h := h xor (((x and Int64($0100804020100800)) and
-    UInt64(TBits.Asr64(Int64(y) shl 7, 63))) shr 8);
+    UInt64(TBitUtilities.Asr64(Int64(y) shl 7, 63))) shr 8);
 
 {$IFDEF DEBUG}
   System.Assert((h shr 49) = 0);
@@ -1626,7 +1626,7 @@ begin
 
   for i := 0 to System.Pred(Fm_size) do
   begin
-    MASK := UInt64(Int64(TBits.Asr32((i xor index) - 1, 31)));
+    MASK := UInt64(Int64(TBitUtilities.Asr32((i xor index) - 1, 31)));
 
     for j := 0 to System.Pred(SECT283K1_FE_LONGS) do
     begin

+ 10 - 10
CryptoLib/src/Math/EC/Multiplier/ClpMultipliers.pas

@@ -24,7 +24,7 @@ interface
 uses
   SysUtils,
   ClpBigInteger,
-  ClpBits,
+  ClpBitUtilities,
   ClpNat,
   ClpTnaf,
   ClpIECC,
@@ -301,7 +301,7 @@ begin
     while j >= 0 do
     begin
 
-      secretBit := LK[TBits.Asr32(j, 5)] shr (j and $1F);
+      secretBit := LK[TBitUtilities.Asr32(j, 5)] shr (j and $1F);
       secretIndex := secretIndex xor (secretBit shr 1);
       secretIndex := secretIndex shl 1;
       secretIndex := secretIndex xor secretBit;
@@ -405,7 +405,7 @@ begin
   begin
     System.Dec(i);
     wi := wnaf[i];
-    digit := TBits.Asr32(wi, 16);
+    digit := TBitUtilities.Asr32(wi, 16);
     zeroes := wi and $FFFF;
 
     n := System.Abs(digit);
@@ -421,7 +421,7 @@ begin
     // Optimization can only be used for values in the lower half of the table
     if ((n shl 2) < (1 shl width)) then
     begin
-      highest := 32 - TBits.NumberOfLeadingZeros(n);
+      highest := 32 - TBitUtilities.NumberOfLeadingZeros(n);
 
       // TODO Get addition/doubling cost ratio from curve and compare to 'scale' to see if worth substituting?
       scale := width - highest;
@@ -429,13 +429,13 @@ begin
 
       i1 := ((1 shl (width - 1)) - 1);
       i2 := (lowBits shl scale) + 1;
-      R := table[TBits.Asr32(i1, 1)].add(table[TBits.Asr32(i2, 1)]);
+      R := table[TBitUtilities.Asr32(i1, 1)].add(table[TBitUtilities.Asr32(i2, 1)]);
 
       zeroes := zeroes - scale;
     end
     else
     begin
-      R := table[TBits.Asr32(n, 1)];
+      R := table[TBitUtilities.Asr32(n, 1)];
     end;
 
     R := R.TimesPow2(zeroes);
@@ -445,7 +445,7 @@ begin
   begin
     System.Dec(i);
     wi := wnaf[i];
-    digit := TBits.Asr32(wi, 16);
+    digit := TBitUtilities.Asr32(wi, 16);
     zeroes := wi and $FFFF;
 
     n := System.Abs(digit);
@@ -458,7 +458,7 @@ begin
       table := preComp;
     end;
 
-    lr := table[TBits.Asr32(n, 1)];
+    lr := table[TBitUtilities.Asr32(n, 1)];
 
     R := R.TwicePlus(lr);
     R := R.TimesPow2(zeroes);
@@ -514,11 +514,11 @@ begin
 
       if ui > 0 then
       begin
-        x := pu[TBits.Asr32(ui, 1)];
+        x := pu[TBitUtilities.Asr32(ui, 1)];
       end
       else
       begin
-        x := puNeg[TBits.Asr32(-ui, 1)];
+        x := puNeg[TBitUtilities.Asr32(-ui, 1)];
       end;
 
       q := q.add(x) as IAbstractF2mPoint;

+ 61 - 61
CryptoLib/src/Math/EC/Rfc7748/ClpX25519Field.pas

@@ -22,7 +22,7 @@ unit ClpX25519Field;
 interface
 
 uses
-  ClpBits,
+  ClpBitUtilities,
   ClpConverters,
   ClpArrayUtilities,
   ClpCryptoLibTypes;
@@ -198,28 +198,28 @@ begin
   z8 := z[8];
   z9 := z[9];
 
-  z3 := z3 + TBits.Asr32(z2, 25);
+  z3 := z3 + TBitUtilities.Asr32(z2, 25);
   z2 := z2 and M25;
-  z5 := z5 + TBits.Asr32(z4, 25);
+  z5 := z5 + TBitUtilities.Asr32(z4, 25);
   z4 := z4 and M25;
-  z8 := z8 + TBits.Asr32(z7, 25);
+  z8 := z8 + TBitUtilities.Asr32(z7, 25);
   z7 := z7 and M25;
   // z0 := z0 + (z9 shr 24) * 19; z9 := z9 and M24;
-  z0 := z0 + (TBits.Asr32(z9, 25) * 38);
+  z0 := z0 + (TBitUtilities.Asr32(z9, 25) * 38);
   z9 := z9 and M25;
 
-  z1 := z1 + TBits.Asr32(z0, 26);
+  z1 := z1 + TBitUtilities.Asr32(z0, 26);
   z0 := z0 and M26;
-  z6 := z6 + TBits.Asr32(z5, 26);
+  z6 := z6 + TBitUtilities.Asr32(z5, 26);
   z5 := z5 and M26;
 
-  z2 := z2 + TBits.Asr32(z1, 26);
+  z2 := z2 + TBitUtilities.Asr32(z1, 26);
   z1 := z1 and M26;
-  z4 := z4 + TBits.Asr32(z3, 26);
+  z4 := z4 + TBitUtilities.Asr32(z3, 26);
   z3 := z3 and M26;
-  z7 := z7 + TBits.Asr32(z6, 26);
+  z7 := z7 + TBitUtilities.Asr32(z6, 26);
   z6 := z6 and M26;
-  z9 := z9 + TBits.Asr32(z8, 26);
+  z9 := z9 + TBitUtilities.Asr32(z8, 26);
   z8 := z8 and M26;
 
   z[0] := z0;
@@ -257,7 +257,7 @@ var
   mask, i: Int32;
 begin
 {$IFDEF DEBUG}
-  System.Assert(TBits.Asr32(ANegate, 1) = 0);
+  System.Assert(TBitUtilities.Asr32(ANegate, 1) = 0);
 {$ENDIF DEBUG}
   mask := 0 - ANegate;
   for i := 0 to System.Pred(Size) do
@@ -293,7 +293,7 @@ var
   mask, i, ai, bi, dummy: Int32;
 begin
 {$IFDEF DEBUG}
-  System.Assert(TBits.Asr32(swap, 1) = 0);
+  System.Assert(TBitUtilities.Asr32(swap, 1) = 0);
   System.Assert(a <> b);
 {$ENDIF DEBUG}
   mask := 0 - swap;
@@ -411,7 +411,7 @@ begin
   begin
     d := d or x[i];
   end;
-  d := (TBits.Asr32(d, 1)) or (d and 1);
+  d := (TBitUtilities.Asr32(d, 1)) or (d and 1);
   Result := (d - 1) shr 31;
 end;
 
@@ -439,39 +439,39 @@ begin
 
   c0 := Int64(x2) * y;
   x2 := Int32(c0) and M25;
-  c0 := TBits.Asr64(c0, 25);
+  c0 := TBitUtilities.Asr64(c0, 25);
   c1 := Int64(x4) * y;
   x4 := Int32(c1) and M25;
-  c1 := TBits.Asr64(c1, 25);
+  c1 := TBitUtilities.Asr64(c1, 25);
   c2 := Int64(x7) * y;
   x7 := Int32(c2) and M25;
-  c2 := TBits.Asr64(c2, 25);
-  // c3 := Int64(x9) * y; x9 := Int32(c3) and M24; c3 := TBits.Asr64(c3 , 24);
+  c2 := TBitUtilities.Asr64(c2, 25);
+  // c3 := Int64(x9) * y; x9 := Int32(c3) and M24; c3 := TBitUtilities.Asr64(c3 , 24);
   // c3 := c3 * 19;
   c3 := Int64(x9) * y;
   x9 := Int32(c3) and M25;
-  c3 := TBits.Asr64(c3, 25);
+  c3 := TBitUtilities.Asr64(c3, 25);
   c3 := c3 * 38;
 
   c3 := c3 + (Int64(x0) * y);
   z[0] := Int32(c3) and M26;
-  c3 := TBits.Asr64(c3, 26);
+  c3 := TBitUtilities.Asr64(c3, 26);
   c1 := c1 + (Int64(x5) * y);
   z[5] := Int32(c1) and M26;
-  c1 := TBits.Asr64(c1, 26);
+  c1 := TBitUtilities.Asr64(c1, 26);
 
   c3 := c3 + (Int64(x1) * y);
   z[1] := Int32(c3) and M26;
-  c3 := TBits.Asr64(c3, 26);
+  c3 := TBitUtilities.Asr64(c3, 26);
   c0 := c0 + (Int64(x3) * y);
   z[3] := Int32(c0) and M26;
-  c0 := TBits.Asr64(c0, 26);
+  c0 := TBitUtilities.Asr64(c0, 26);
   c1 := c1 + (Int64(x6) * y);
   z[6] := Int32(c1) and M26;
-  c1 := TBits.Asr64(c1, 26);
+  c1 := TBitUtilities.Asr64(c1, 26);
   c2 := c2 + (Int64(x8) * y);
   z[8] := Int32(c2) and M26;
-  c2 := TBits.Asr64(c2, 26);
+  c2 := TBitUtilities.Asr64(c2, 26);
 
   z[2] := x2 + Int32(c3);
   z[4] := x4 + Int32(c0);
@@ -588,41 +588,41 @@ begin
 
   t := a8 + (c3 - a3);
   z8 := Int32(t) and M26;
-  t := TBits.Asr64(t, 26);
+  t := TBitUtilities.Asr64(t, 26);
   // t       := t + (a9 + (c4 - a4));
   t := t + ((c4 - a4) - b4);
-  // z9       := Int32(t) and M24; t := TBits.Asr64(t , 24);
+  // z9       := Int32(t) and M24; t := TBitUtilities.Asr64(t , 24);
   // t        := a0 + (t + ((c5 - a5) shl 1)) * 19;
   z9 := Int32(t) and M25;
-  t := TBits.Asr64(t, 25);
+  t := TBitUtilities.Asr64(t, 25);
   t := a0 + (t + c5 - a5) * 38;
   z[0] := Int32(t) and M26;
-  t := TBits.Asr64(t, 26);
+  t := TBitUtilities.Asr64(t, 26);
   t := t + (a1 + (c6 - a6) * 38);
   z[1] := Int32(t) and M26;
-  t := TBits.Asr64(t, 26);
+  t := TBitUtilities.Asr64(t, 26);
   t := t + (a2 + (c7 - a7) * 38);
   z[2] := Int32(t) and M25;
-  t := TBits.Asr64(t, 25);
+  t := TBitUtilities.Asr64(t, 25);
   t := t + (a3 + (c8 - a8) * 38);
   z[3] := Int32(t) and M26;
-  t := TBits.Asr64(t, 26);
+  t := TBitUtilities.Asr64(t, 26);
   // t       := t + (a4 - (a9 * 38));
   t := t + (a4 + b4 * 38);
   z[4] := Int32(t) and M25;
-  t := TBits.Asr64(t, 25);
+  t := TBitUtilities.Asr64(t, 25);
   t := t + (a5 + (c0 - a0));
   z[5] := Int32(t) and M26;
-  t := TBits.Asr64(t, 26);
+  t := TBitUtilities.Asr64(t, 26);
   t := t + (a6 + (c1 - a1));
   z[6] := Int32(t) and M26;
-  t := TBits.Asr64(t, 26);
+  t := TBitUtilities.Asr64(t, 26);
   t := t + (a7 + (c2 - a2));
   z[7] := Int32(t) and M25;
-  t := TBits.Asr64(t, 25);
+  t := TBitUtilities.Asr64(t, 25);
   t := t + z8;
   z[8] := Int32(t) and M26;
-  t := TBits.Asr64(t, 26);
+  t := TBitUtilities.Asr64(t, 26);
   z[9] := z9 + Int32(t);
 end;
 
@@ -640,11 +640,11 @@ class procedure TX25519Field.Normalize(const z: TCryptoLibInt32Array);
 var
   x: Int32;
 begin
-  x := TBits.Asr32(z[9], 23) and 1;
+  x := TBitUtilities.Asr32(z[9], 23) and 1;
   Reduce(z, x);
   Reduce(z, -x);
 {$IFDEF DEBUG}
-  System.Assert(TBits.Asr32(z[9], 24) = 0);
+  System.Assert(TBitUtilities.Asr32(z[9], 24) = 0);
 {$ENDIF DEBUG}
 end;
 
@@ -710,36 +710,36 @@ begin
   z9 := z[9];
   t := z9;
   z9 := t and M24;
-  t := TBits.Asr32(t, 24);
+  t := TBitUtilities.Asr32(t, 24);
   t := t + c;
   t := t * 19;
   t := t + z[0];
   z[0] := t and M26;
-  t := TBits.Asr32(t, 26);
+  t := TBitUtilities.Asr32(t, 26);
   t := t + z[1];
   z[1] := t and M26;
-  t := TBits.Asr32(t, 26);
+  t := TBitUtilities.Asr32(t, 26);
   t := t + z[2];
   z[2] := t and M25;
-  t := TBits.Asr32(t, 25);
+  t := TBitUtilities.Asr32(t, 25);
   t := t + z[3];
   z[3] := t and M26;
-  t := TBits.Asr32(t, 26);
+  t := TBitUtilities.Asr32(t, 26);
   t := t + z[4];
   z[4] := t and M25;
-  t := TBits.Asr32(t, 25);
+  t := TBitUtilities.Asr32(t, 25);
   t := t + z[5];
   z[5] := t and M26;
-  t := TBits.Asr32(t, 26);
+  t := TBitUtilities.Asr32(t, 26);
   t := t + z[6];
   z[6] := t and M26;
-  t := TBits.Asr32(t, 26);
+  t := TBitUtilities.Asr32(t, 26);
   t := t + z[7];
   z[7] := t and M25;
-  t := TBits.Asr32(t, 25);
+  t := TBitUtilities.Asr32(t, 25);
   t := t + z[8];
   z[8] := t and M26;
-  t := TBits.Asr32(t, 26);
+  t := TBitUtilities.Asr32(t, 26);
   t := t + z9;
   z[9] := t;
 end;
@@ -827,41 +827,41 @@ begin
 
   t := a8 + (c3 - a3);
   z8 := Int32(t) and M26;
-  t := TBits.Asr64(t, 26);
+  t := TBitUtilities.Asr64(t, 26);
   // t  = t + (a9 + (c4 - a4));
   t := t + ((c4 - a4) - b4);
-  // z9  := Int32(t) and M24; t := TBits.Asr64(t , 24);
+  // z9  := Int32(t) and M24; t := TBitUtilities.Asr64(t , 24);
   // t  := a0 + (t + ((c5 - a5) shl 1)) * 19;
   z9 := Int32(t) and M25;
-  t := TBits.Asr64(t, 25);
+  t := TBitUtilities.Asr64(t, 25);
   t := a0 + (t + c5 - a5) * 38;
   z[0] := Int32(t) and M26;
-  t := TBits.Asr64(t, 26);
+  t := TBitUtilities.Asr64(t, 26);
   t := t + (a1 + (c6 - a6) * 38);
   z[1] := Int32(t) and M26;
-  t := TBits.Asr64(t, 26);
+  t := TBitUtilities.Asr64(t, 26);
   t := t + (a2 + (c7 - a7) * 38);
   z[2] := Int32(t) and M25;
-  t := TBits.Asr64(t, 25);
+  t := TBitUtilities.Asr64(t, 25);
   t := t + (a3 + (c8 - a8) * 38);
   z[3] := Int32(t) and M26;
-  t := TBits.Asr64(t, 26);
+  t := TBitUtilities.Asr64(t, 26);
   // t       := t + (a4 - a9 * 38);
   t := t + (a4 + b4 * 38);
   z[4] := Int32(t) and M25;
-  t := TBits.Asr64(t, 25);
+  t := TBitUtilities.Asr64(t, 25);
   t := t + (a5 + (c0 - a0));
   z[5] := Int32(t) and M26;
-  t := TBits.Asr64(t, 26);
+  t := TBitUtilities.Asr64(t, 26);
   t := t + (a6 + (c1 - a1));
   z[6] := Int32(t) and M26;
-  t := TBits.Asr64(t, 26);
+  t := TBitUtilities.Asr64(t, 26);
   t := t + (a7 + (c2 - a2));
   z[7] := Int32(t) and M25;
-  t := TBits.Asr64(t, 25);
+  t := TBitUtilities.Asr64(t, 25);
   t := t + z8;
   z[8] := Int32(t) and M26;
-  t := TBits.Asr64(t, 26);
+  t := TBitUtilities.Asr64(t, 26);
   z[9] := z9 + Int32(t);
 end;
 

+ 37 - 37
CryptoLib/src/Math/EC/Rfc8032/ClpEd25519.pas

@@ -24,7 +24,7 @@ interface
 uses
   SyncObjs,
   ClpNat,
-  ClpBits,
+  ClpBitUtilities,
   ClpNat256,
   ClpInterleave,
   ClpDigestUtilities,
@@ -829,7 +829,7 @@ class function TEd25519.GetWindow4(const X: TCryptoLibUInt32Array;
 var
   W, b: Int32;
 begin
-  W := TBits.Asr32(n, 3);
+  W := TBitUtilities.Asr32(n, 3);
   b := (n and 7) shl 2;
   result := (X[W] shr b) and 15;
 end;
@@ -841,7 +841,7 @@ var
 begin
   W := GetWindow4(X, n);
 
-  LSign := (TBits.Asr32(W, (PrecompTeeth - 1))) xor 1;
+  LSign := (TBitUtilities.Asr32(W, (PrecompTeeth - 1))) xor 1;
   abs := (W xor -LSign) and PrecompMask;
 
 {$IFDEF DEBUG}
@@ -853,7 +853,7 @@ begin
 
   while i < PrecompPoints do
   begin
-    cond := TBits.Asr32(((i xor abs) - 1), 31);
+    cond := TBitUtilities.Asr32(((i xor abs) - 1), 31);
     TX25519Field.CMov(cond, table, off, r.X, 0);
     off := off + TX25519Field.SIZE;
     TX25519Field.CMov(cond, table, off, r.Y, 0);
@@ -1479,7 +1479,7 @@ begin
 
   for i := 0 to System.Pred(PrecompPoints) do
   begin
-    cond := TBits.Asr32(((i xor index) - 1), 31);
+    cond := TBitUtilities.Asr32(((i xor index) - 1), 31);
     TX25519Field.CMov(cond, FPrecompBase, off, p.Ypx_h, 0);
     off := off + TX25519Field.SIZE;
     TX25519Field.CMov(cond, FPrecompBase, off, p.Ymx_h, 0);
@@ -1731,7 +1731,7 @@ begin
   x12 := x12 - (x18 * L3); // x12:32/31
   x13 := x13 - (x18 * L4); // x13:28/21
 
-  x17 := x17 + TBits.Asr64(x16, 28); // x17:28/--
+  x17 := x17 + TBitUtilities.Asr64(x16, 28); // x17:28/--
   x16 := x16 and Int64(M28L); // x16:28/--
   x08 := x08 - (x17 * L0); // x08:54/32
   x09 := x09 - (x17 * L1); // x09:52/51
@@ -1746,7 +1746,7 @@ begin
   x10 := x10 - (x16 * L3); // x10:55/52
   x11 := x11 - (x16 * L4); // x11:51/41
 
-  x15 := x15 + TBits.Asr64(x14, 28); // x15:28/--
+  x15 := x15 + TBitUtilities.Asr64(x14, 28); // x15:28/--
   x14 := x14 and Int64(M28L); // x14:28/--
   x06 := x06 - (x15 * L0); // x06:54/32
   x07 := x07 - (x15 * L1); // x07:54/53
@@ -1761,7 +1761,7 @@ begin
   x08 := x08 - (x14 * L3); // x08:56/51
   x09 := x09 - (x14 * L4); // x09:56/--
 
-  x13 := x13 + TBits.Asr64(x12, 28); // x13:28/22
+  x13 := x13 + TBitUtilities.Asr64(x12, 28); // x13:28/22
   x12 := x12 and Int64(M28L); // x12:28/--
   x04 := x04 - (x13 * L0); // x04:54/49
   x05 := x05 - (x13 * L1); // x05:54/53
@@ -1769,7 +1769,7 @@ begin
   x07 := x07 - (x13 * L3); // x07:56/52
   x08 := x08 - (x13 * L4); // x08:56/52
 
-  x12 := x12 + TBits.Asr64(x11, 28); // x12:28/24
+  x12 := x12 + TBitUtilities.Asr64(x11, 28); // x12:28/24
   x11 := x11 and Int64(M28L); // x11:28/--
   x03 := x03 - (x12 * L0); // x03:54/49
   x04 := x04 - (x12 * L1); // x04:54/51
@@ -1777,7 +1777,7 @@ begin
   x06 := x06 - (x12 * L3); // x06:56/52
   x07 := x07 - (x12 * L4); // x07:56/53
 
-  x11 := x11 + TBits.Asr64(x10, 28); // x11:29/--
+  x11 := x11 + TBitUtilities.Asr64(x10, 28); // x11:29/--
   x10 := x10 and Int64(M28L); // x10:28/--
   x02 := x02 - (x11 * L0); // x02:55/32
   x03 := x03 - (x11 * L1); // x03:55/--
@@ -1785,7 +1785,7 @@ begin
   x05 := x05 - (x11 * L3); // x05:56/52
   x06 := x06 - (x11 * L4); // x06:56/53
 
-  x10 := x10 + TBits.Asr64(x09, 28); // x10:29/--
+  x10 := x10 + TBitUtilities.Asr64(x09, 28); // x10:29/--
   x09 := x09 and Int64(M28L); // x09:28/--
   x01 := x01 - (x10 * L0); // x01:55/28
   x02 := x02 - (x10 * L1); // x02:55/54
@@ -1793,12 +1793,12 @@ begin
   x04 := x04 - (x10 * L3); // x04:57/--
   x05 := x05 - (x10 * L4); // x05:56/53
 
-  x08 := x08 + TBits.Asr64(x07, 28); // x08:56/53
+  x08 := x08 + TBitUtilities.Asr64(x07, 28); // x08:56/53
   x07 := x07 and Int64(M28L); // x07:28/--
-  x09 := x09 + TBits.Asr64(x08, 28); // x09:29/25
+  x09 := x09 + TBitUtilities.Asr64(x08, 28); // x09:29/25
   x08 := x08 and Int64(M28L); // x08:28/--
 
-  T := TBits.Asr64(x08, 27) and Int64(1);
+  T := TBitUtilities.Asr64(x08, 27) and Int64(1);
   x09 := x09 + T; // x09:29/26
 
   x00 := x00 - (x09 * L0); // x00:55/53
@@ -1807,23 +1807,23 @@ begin
   x03 := x03 - (x09 * L3); // x03:57/--
   x04 := x04 - (x09 * L4); // x04:57/42
 
-  x01 := x01 + TBits.Asr64(x00, 28);
+  x01 := x01 + TBitUtilities.Asr64(x00, 28);
   x00 := x00 and Int64(M28L);
-  x02 := x02 + TBits.Asr64(x01, 28);
+  x02 := x02 + TBitUtilities.Asr64(x01, 28);
   x01 := x01 and Int64(M28L);
-  x03 := x03 + TBits.Asr64(x02, 28);
+  x03 := x03 + TBitUtilities.Asr64(x02, 28);
   x02 := x02 and Int64(M28L);
-  x04 := x04 + TBits.Asr64(x03, 28);
+  x04 := x04 + TBitUtilities.Asr64(x03, 28);
   x03 := x03 and Int64(M28L);
-  x05 := x05 + TBits.Asr64(x04, 28);
+  x05 := x05 + TBitUtilities.Asr64(x04, 28);
   x04 := x04 and Int64(M28L);
-  x06 := x06 + TBits.Asr64(x05, 28);
+  x06 := x06 + TBitUtilities.Asr64(x05, 28);
   x05 := x05 and Int64(M28L);
-  x07 := x07 + TBits.Asr64(x06, 28);
+  x07 := x07 + TBitUtilities.Asr64(x06, 28);
   x06 := x06 and Int64(M28L);
-  x08 := x08 + TBits.Asr64(x07, 28);
+  x08 := x08 + TBitUtilities.Asr64(x07, 28);
   x07 := x07 and Int64(M28L);
-  x09 := TBits.Asr64(x08, 28);
+  x09 := TBitUtilities.Asr64(x08, 28);
   x08 := x08 and Int64(M28L);
 
   x09 := x09 - T;
@@ -1837,21 +1837,21 @@ begin
   x03 := x03 + (x09 and L3);
   x04 := x04 + (x09 and L4);
 
-  x01 := x01 + TBits.Asr64(x00, 28);
+  x01 := x01 + TBitUtilities.Asr64(x00, 28);
   x00 := x00 and Int64(M28L);
-  x02 := x02 + TBits.Asr64(x01, 28);
+  x02 := x02 + TBitUtilities.Asr64(x01, 28);
   x01 := x01 and Int64(M28L);
-  x03 := x03 + TBits.Asr64(x02, 28);
+  x03 := x03 + TBitUtilities.Asr64(x02, 28);
   x02 := x02 and Int64(M28L);
-  x04 := x04 + TBits.Asr64(x03, 28);
+  x04 := x04 + TBitUtilities.Asr64(x03, 28);
   x03 := x03 and Int64(M28L);
-  x05 := x05 + TBits.Asr64(x04, 28);
+  x05 := x05 + TBitUtilities.Asr64(x04, 28);
   x04 := x04 and Int64(M28L);
-  x06 := x06 + TBits.Asr64(x05, 28);
+  x06 := x06 + TBitUtilities.Asr64(x05, 28);
   x05 := x05 and Int64(M28L);
-  x07 := x07 + TBits.Asr64(x06, 28);
+  x07 := x07 + TBitUtilities.Asr64(x06, 28);
   x06 := x06 and Int64(M28L);
-  x08 := x08 + TBits.Asr64(x07, 28);
+  x08 := x08 + TBitUtilities.Asr64(x07, 28);
   x07 := x07 and Int64(M28L);
 
   System.SetLength(result, ScalarBytes);
@@ -1877,7 +1877,7 @@ begin
 
 {$IFDEF DEBUG}
   System.Assert((n[0] and 7) = 0);
-  System.Assert((TBits.Asr32(n[ScalarUints - 1], 30)) = 1);
+  System.Assert((TBitUtilities.Asr32(n[ScalarUints - 1], 30)) = 1);
 {$ENDIF DEBUG}
   TNat.ShiftDownBits(ScalarUints, n, 3, 1);
 
@@ -1890,7 +1890,7 @@ begin
   System.Assert(c2 = (1 shl 31));
 {$ENDIF}
 {$IFDEF DEBUG}
-  System.Assert((TBits.Asr32(n[ScalarUints - 1], 28)) = 1);
+  System.Assert((TBitUtilities.Asr32(n[ScalarUints - 1], 28)) = 1);
 {$ENDIF DEBUG}
   PointCopy(p, r);
 
@@ -2030,8 +2030,8 @@ begin
     wb := ws_b[bit];
     if (wb <> 0) then
     begin
-      LSign := TBits.Asr32(wb, 31);
-      index := TBits.Asr32((wb xor LSign), 1);
+      LSign := TBitUtilities.Asr32(wb, 31);
+      index := TBitUtilities.Asr32((wb xor LSign), 1);
 
       PointAddVar((LSign <> 0), FPrecompBaseTable[index], r);
     end;
@@ -2039,8 +2039,8 @@ begin
     wp := ws_p[bit];
     if (wp <> 0) then
     begin
-      LSign := TBits.Asr32(wp, 31);
-      index := TBits.Asr32((wp xor LSign), 1);
+      LSign := TBitUtilities.Asr32(wp, 31);
+      index := TBitUtilities.Asr32((wp xor LSign), 1);
 
       PointAddVar((LSign <> 0), tp[index], r);
     end;

+ 2 - 2
CryptoLib/src/Math/Field/ClpGenericPolynomialExtensionField.pas

@@ -22,7 +22,7 @@ unit ClpGenericPolynomialExtensionField;
 interface
 
 uses
-  ClpBits,
+  ClpBitUtilities,
   ClpBigInteger,
   ClpIFiniteField,
   ClpIPolynomial,
@@ -116,7 +116,7 @@ function TGenericPolynomialExtensionField.GetHashCode: {$IFDEF DELPHI}Int32;
 {$ELSE}PtrInt; {$ENDIF DELPHI}
 begin
   Result := (subfield as TObject).GetHashCode()
-    xor Int32(TBits.RotateLeft32((MinimalPolynomial as TObject)
+    xor Int32(TBitUtilities.RotateLeft32((MinimalPolynomial as TObject)
     .GetHashCode(), 16));
 end;
 

+ 0 - 493
CryptoLib/src/Math/Raw/ClpBits.pas

@@ -1,493 +0,0 @@
-{ *********************************************************************************** }
-{ *                              CryptoLib Library                                  * }
-{ *                Copyright (c) 2018 - 20XX Ugochukwu Mmaduekwe                    * }
-{ *                 Github Repository <https://github.com/Xor-el>                   * }
-
-{ *  Distributed under the MIT software license, see the accompanying file LICENSE  * }
-{ *          or visit http://www.opensource.org/licenses/mit-license.php.           * }
-
-{ *                              Acknowledgements:                                  * }
-{ *                                                                                 * }
-{ *      Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring     * }
-{ *                           development of this library                           * }
-
-{ * ******************************************************************************* * }
-
-(* &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& *)
-
-unit ClpBits;
-
-{$I ..\..\Include\CryptoLib.inc}
-
-interface
-
-type
-  TBits = class sealed(TObject)
-
-  public
-
-    class function ReverseBytesInt32(Value: Int32): Int32; static; inline;
-    class function ReverseBitsUInt8(Value: UInt8): UInt8; static; inline;
-    class function ReverseBytesUInt16(Value: UInt16): UInt16; static; inline;
-    class function ReverseBytesUInt32(Value: UInt32): UInt32; static; inline;
-    class function ReverseBytesUInt64(Value: UInt64): UInt64; static; inline;
-
-    /// <summary>
-    /// Reverse a ByteArray.
-    /// </summary>
-    /// Implementation was found here <see cref="http://stackoverflow.com/a/12969282" />
-    /// <param name="Source">Pointer to Input Array.</param>
-    /// <param name="Dest">Pointer to Destination Array.</param>
-    /// <param name="Size">Size of the Array to Reverse.</param>
-
-    class procedure ReverseByteArray(Source, Dest: Pointer;
-      Size: Int64); static;
-
-    /// <summary>
-    /// Calculates Arithmetic shift right.
-    /// </summary>
-    /// <param name="AValue">Int32 value to compute 'Asr' on.</param>
-    /// <param name="AShiftBits">Byte, number of bits to shift value to.</param>
-    /// <returns>Shifted value.</returns>
-    /// <remarks>
-    /// Emulated Implementation was gotten from FreePascal sources
-    /// </remarks>
-
-    class function Asr32(AValue: Int32; AShiftBits: Byte): Int32;
-      static; inline;
-
-    /// <summary>
-    /// Calculates Arithmetic shift right.
-    /// </summary>
-    /// <param name="AValue">Int64 value to compute 'Asr' on.</param>
-    /// <param name="AShiftBits">Byte, number of bits to shift value to.</param>
-    /// <returns>Shifted value.</returns>
-    /// <remarks>
-    /// Emulated Implementation was gotten from FreePascal sources
-    /// </remarks>
-
-    class function Asr64(AValue: Int64; AShiftBits: Byte): Int64;
-      static; inline;
-
-    /// <summary>
-    /// Calculates Negative Left Shift. This was implemented to circumvent a
-    /// bug in FPC ARM when performing Shift Left on certain values with a
-    /// Negative Shift Bits. For example UInt32(1948415963) shl Int32(-2)
-    /// should give "3221225472" but in FPC ARM, It gives "0". In some C
-    /// Compilers, this is "Undefined"
-    /// </summary>
-    /// <param name="Value">
-    /// Value to Perform Shift On
-    /// </param>
-    /// <param name="ShiftBits">
-    /// Integer, number of bits to shift value to. This Number <b>Must be
-    /// Negative</b>
-    /// </param>
-    /// <param name="value">
-    /// UInt32 value to compute 'NLS' on.
-    /// </param>
-    /// <returns>
-    /// Shifted value.
-    /// </returns>
-
-    class function NegativeLeftShift32(Value: UInt32; ShiftBits: Int32): UInt32;
-      static; inline;
-
-    /// <summary>
-    /// Calculates Negative Right Shift. This was implemented to circumvent a
-    /// compiler issue when performing Shift Right on certain values with a
-    /// Negative Shift Bits. In some C Compilers, this is "Undefined"
-    /// </summary>
-    /// <param name="Value">
-    /// Value to Perform Shift On
-    /// </param>
-    /// <param name="ShiftBits">
-    /// Integer, number of bits to shift value to. This Number <b>Must be
-    /// Negative</b>
-    /// </param>
-    /// <param name="value">
-    /// UInt32 value to compute 'NRS' on.
-    /// </param>
-    /// <returns>
-    /// Shifted value.
-    /// </returns>
-
-    class function NegativeRightShift32(Value: UInt32; ShiftBits: Int32)
-      : UInt32; static; inline;
-
-    /// <summary>
-    /// Calculates Negative Right Shift. This was implemented to circumvent a
-    /// compiler issue when performing Shift Right on certain values with a
-    /// Negative Shift Bits. In some C Compilers, this is "Undefined"
-    /// </summary>
-    /// <param name="Value">
-    /// Value to Perform Shift On
-    /// </param>
-    /// <param name="ShiftBits">
-    /// Integer, number of bits to shift value to. This Number <b>Must be
-    /// Negative</b>
-    /// </param>
-    /// <param name="value">
-    /// UInt64 value to compute 'NRS' on.
-    /// </param>
-    /// <returns>
-    /// Shifted value.
-    /// </returns>
-
-    class function NegativeRightShift64(Value: UInt64; ShiftBits: Int32)
-      : UInt64; static; inline;
-
-    class function RotateLeft8(a_value: Byte; a_n: Int32): Byte; static; inline;
-    class function RotateLeft32(a_value: UInt32; a_n: Int32): UInt32;
-      static; inline;
-    class function RotateLeft64(a_value: UInt64; a_n: Int32): UInt64;
-      static; inline;
-    class function RotateRight8(a_value: Byte; a_n: Int32): Byte;
-      static; inline;
-    class function RotateRight32(a_value: UInt32; a_n: Int32): UInt32;
-      static; inline;
-    class function RotateRight64(a_value: UInt64; a_n: Int32): UInt64;
-      static; inline;
-
-    class function NumberOfLeadingZeros(a_value: UInt32): Int32; static;
-
-    class function NumberOfTrailingZeros(a_value: UInt32): Int32; static;
-
-    class function PopCount(AValue: UInt32): Int32; static;
-  end;
-
-implementation
-
-{ TBits }
-
-class procedure TBits.ReverseByteArray(Source, Dest: Pointer; Size: Int64);
-var
-  ptr_src, ptr_dest: PByte;
-begin
-  ptr_src := PByte(Source);
-  ptr_dest := PByte(Dest);
-  System.Inc(ptr_dest, Size - 1);
-  while Size > 0 do
-  begin
-    ptr_dest^ := ptr_src^;
-    System.Inc(ptr_src);
-    System.Dec(ptr_dest);
-    System.Dec(Size);
-  end;
-end;
-
-class function TBits.ReverseBytesInt32(Value: Int32): Int32;
-{$IFNDEF FPC}
-var
-  i1, i2, i3, i4: Int32;
-{$ENDIF FPC}
-begin
-{$IFDEF FPC}
-  Result := SwapEndian(Value);
-{$ELSE}
-  i1 := Value and $FF;
-  i2 := TBits.Asr32(Value, 8) and $FF;
-  i3 := TBits.Asr32(Value, 16) and $FF;
-  i4 := TBits.Asr32(Value, 24) and $FF;
-
-  Result := (i1 shl 24) or (i2 shl 16) or (i3 shl 8) or (i4 shl 0);
-{$ENDIF FPC}
-end;
-
-class function TBits.ReverseBitsUInt8(Value: UInt8): UInt8;
-begin
-  Value := ((Value shr 1) and $55) or ((Value shl 1) and $AA);
-  Value := ((Value shr 2) and $33) or ((Value shl 2) and $CC);
-  Value := ((Value shr 4) and $0F) or ((Value shl 4) and $F0);
-  Result := Value;
-end;
-
-class function TBits.ReverseBytesUInt16(Value: UInt16): UInt16;
-begin
-{$IFDEF FPC}
-  Result := SwapEndian(Value);
-{$ELSE}
-  Result := UInt16((Value and UInt32($FF)) shl 8 or
-    (Value and UInt32($FF00)) shr 8);
-{$ENDIF FPC}
-end;
-
-class function TBits.ReverseBytesUInt32(Value: UInt32): UInt32;
-begin
-{$IFDEF FPC}
-  Result := SwapEndian(Value);
-{$ELSE}
-  Result := (Value and UInt32($000000FF)) shl 24 or (Value and UInt32($0000FF00)
-    ) shl 8 or (Value and UInt32($00FF0000)) shr 8 or
-    (Value and UInt32($FF000000)) shr 24;
-{$ENDIF FPC}
-end;
-
-class function TBits.ReverseBytesUInt64(Value: UInt64): UInt64;
-begin
-{$IFDEF FPC}
-  Result := SwapEndian(Value);
-{$ELSE}
-  Result := (Value and UInt64($00000000000000FF)) shl 56 or
-    (Value and UInt64($000000000000FF00)) shl 40 or
-    (Value and UInt64($0000000000FF0000)) shl 24 or
-    (Value and UInt64($00000000FF000000)) shl 8 or
-    (Value and UInt64($000000FF00000000)) shr 8 or
-    (Value and UInt64($0000FF0000000000)) shr 24 or
-    (Value and UInt64($00FF000000000000)) shr 40 or
-    (Value and UInt64($FF00000000000000)) shr 56;
-{$ENDIF FPC}
-end;
-
-class function TBits.Asr32(AValue: Int32; AShiftBits: Byte): Int32;
-
-begin
-{$IFDEF FPC}
-  Result := SarLongInt(AValue, AShiftBits);
-{$ELSE}
-  Result := Int32(UInt32(UInt32(UInt32(AValue) shr (AShiftBits and 31)) or
-    (UInt32(Int32(UInt32(0 - UInt32(UInt32(AValue) shr 31)) and
-    UInt32(Int32(0 - (Ord((AShiftBits and 31) <> 0) { and 1 } )))))
-    shl (32 - (AShiftBits and 31)))));
-{$ENDIF FPC}
-end;
-
-class function TBits.Asr64(AValue: Int64; AShiftBits: Byte): Int64;
-begin
-{$IFDEF FPC}
-  Result := SarInt64(AValue, AShiftBits);
-{$ELSE}
-  Result := Int64(UInt64(UInt64(UInt64(AValue) shr (AShiftBits and 63)) or
-    (UInt64(Int64(UInt64(0 - UInt64(UInt64(AValue) shr 63)) and
-    UInt64(Int64(0 - (Ord((AShiftBits and 63) <> 0) { and 1 } )))))
-    shl (64 - (AShiftBits and 63)))));
-{$ENDIF FPC}
-end;
-
-class function TBits.NegativeLeftShift32(Value: UInt32;
-  ShiftBits: Int32): UInt32;
-begin
-{$IFDEF DEBUG}
-  System.Assert(ShiftBits < 0);
-{$ENDIF DEBUG}
-  Result := Value shl (32 + ShiftBits);
-end;
-
-class function TBits.NegativeRightShift32(Value: UInt32;
-  ShiftBits: Int32): UInt32;
-begin
-{$IFDEF DEBUG}
-  System.Assert(ShiftBits < 0);
-{$ENDIF DEBUG}
-  Result := Value shr (32 + ShiftBits);
-end;
-
-class function TBits.NegativeRightShift64(Value: UInt64;
-  ShiftBits: Int32): UInt64;
-begin
-{$IFDEF DEBUG}
-  System.Assert(ShiftBits < 0);
-{$ENDIF DEBUG}
-  Result := Value shr (64 + ShiftBits);
-end;
-
-class function TBits.RotateLeft8(a_value: Byte; a_n: Int32): Byte;
-begin
-{$IFDEF DEBUG}
-  System.Assert(a_n >= 0);
-{$ENDIF DEBUG}
-{$IFDEF FPC}
-  Result := RolByte(a_value, a_n);
-{$ELSE}
-  a_n := a_n and 7;
-  Result := (a_value shl a_n) or (a_value shr (8 - a_n));
-{$ENDIF FPC}
-end;
-
-class function TBits.RotateLeft32(a_value: UInt32; a_n: Int32): UInt32;
-begin
-{$IFDEF DEBUG}
-  System.Assert(a_n >= 0);
-{$ENDIF DEBUG}
-{$IFDEF FPC}
-  Result := RolDWord(a_value, a_n);
-{$ELSE}
-{$IFNDEF SHIFT_OVERFLOW_BUG_FIXED}
-  a_n := a_n and 31;
-{$ENDIF SHIFT_OVERFLOW_BUG_FIXED}
-  Result := (a_value shl a_n) or (a_value shr (32 - a_n));
-{$ENDIF FPC}
-end;
-
-class function TBits.RotateLeft64(a_value: UInt64; a_n: Int32): UInt64;
-begin
-{$IFDEF DEBUG}
-  System.Assert(a_n >= 0);
-{$ENDIF DEBUG}
-{$IFDEF FPC}
-  Result := RolQWord(a_value, a_n);
-{$ELSE}
-{$IFNDEF SHIFT_OVERFLOW_BUG_FIXED}
-  a_n := a_n and 63;
-{$ENDIF SHIFT_OVERFLOW_BUG_FIXED}
-  Result := (a_value shl a_n) or (a_value shr (64 - a_n));
-{$ENDIF FPC}
-end;
-
-class function TBits.RotateRight8(a_value: Byte; a_n: Int32): Byte;
-begin
-{$IFDEF DEBUG}
-  System.Assert(a_n >= 0);
-{$ENDIF DEBUG}
-{$IFDEF FPC}
-  Result := RorByte(a_value, a_n);
-{$ELSE}
-  a_n := a_n and 7;
-  Result := (a_value shr a_n) or (a_value shl (8 - a_n));
-{$ENDIF FPC}
-end;
-
-class function TBits.RotateRight32(a_value: UInt32; a_n: Int32): UInt32;
-begin
-{$IFDEF DEBUG}
-  System.Assert(a_n >= 0);
-{$ENDIF DEBUG}
-{$IFDEF FPC}
-  Result := RorDWord(a_value, a_n);
-{$ELSE}
-{$IFNDEF SHIFT_OVERFLOW_BUG_FIXED}
-  a_n := a_n and 31;
-{$ENDIF SHIFT_OVERFLOW_BUG_FIXED}
-  Result := (a_value shr a_n) or (a_value shl (32 - a_n));
-{$ENDIF FPC}
-end;
-
-class function TBits.RotateRight64(a_value: UInt64; a_n: Int32): UInt64;
-begin
-{$IFDEF DEBUG}
-  System.Assert(a_n >= 0);
-{$ENDIF DEBUG}
-{$IFDEF FPC}
-  Result := RorQWord(a_value, a_n);
-{$ELSE}
-{$IFNDEF SHIFT_OVERFLOW_BUG_FIXED}
-  a_n := a_n and 63;
-{$ENDIF SHIFT_OVERFLOW_BUG_FIXED}
-  Result := (a_value shr a_n) or (a_value shl (64 - a_n));
-{$ENDIF FPC}
-end;
-
-class function TBits.NumberOfLeadingZeros(a_value: UInt32): Int32;
-{$IFNDEF FPC}
-var
-  n: UInt32;
-{$ENDIF FPC}
-begin
-
-  if (a_value = 0) then
-  begin
-    Result := ((not a_value) shr (31 - 5)) and (1 shl 5);
-    Exit;
-  end;
-
-{$IFDEF FPC}
-  Result := BsrDWord(a_value) xor ((System.SizeOf(UInt32) * 8) - 1);
-  // this also works
-    //Result := ((SizeOf(UInt32) * 8) - 1) - BsrDWord(a_value);
-  // Result := ((System.SizeOf(UInt32) * 8) - 1) - BsrDWord(a_value);
-{$ELSE}
-  n := 1;
-  if ((a_value shr 16) = 0) then
-  begin
-    n := n + 16;
-    a_value := a_value shl 16;
-  end;
-
-  if ((a_value shr 24) = 0) then
-  begin
-    n := n + 8;
-    a_value := a_value shl 8;
-  end;
-
-  if ((a_value shr 28) = 0) then
-  begin
-    n := n + 4;
-    a_value := a_value shl 4;
-  end;
-
-  if ((a_value shr 30) = 0) then
-  begin
-    n := n + 2;
-    a_value := a_value shl 2;
-  end;
-
-  Result := Int32(n) - Int32(a_value shr 31);
-{$ENDIF FPC}
-end;
-
-class function TBits.NumberOfTrailingZeros(a_value: UInt32): Int32;
-{$IFNDEF FPC}
-var
-  n: UInt32;
-{$ENDIF FPC}
-begin
-  if (a_value = 0) then
-  begin
-    // Same trick as leading zeros: returns 32
-    Result := ((not a_value) shr (31 - 5)) and (1 shl 5);
-    Exit;
-  end;
-
-{$IFDEF FPC}
-  // BSF = Bit Scan Forward -> index of least-significant 1 bit
-  Result := BsfDWord(a_value);
-{$ELSE}
-  n := 0;
-
-  if ((a_value and $0000FFFF) = 0) then
-  begin
-    n := n + 16;
-    a_value := a_value shr 16;
-  end;
-
-  if ((a_value and $000000FF) = 0) then
-  begin
-    n := n + 8;
-    a_value := a_value shr 8;
-  end;
-
-  if ((a_value and $0000000F) = 0) then
-  begin
-    n := n + 4;
-    a_value := a_value shr 4;
-  end;
-
-  if ((a_value and $00000003) = 0) then
-  begin
-    n := n + 2;
-    a_value := a_value shr 2;
-  end;
-
-  Result := Int32(n + (a_value and 1) xor 1);
-{$ENDIF FPC}
-end;
-
-
-class function TBits.PopCount(AValue: UInt32): Int32;
-begin
-{$IFDEF FPC}
-  Result := PopCnt(AValue);
-{$ELSE}
-  // Population count (number of set bits) using bit manipulation
-  Result := AValue;
-  Result := Result - ((Result shr 1) and UInt32($55555555));
-  Result := (Result and UInt32($33333333)) + ((Result shr 2) and UInt32($33333333));
-  Result := (Result + (Result shr 4)) and UInt32($0F0F0F0F);
-  Result := Result + (Result shr 8);
-  Result := Result + (Result shr 16);
-  Result := Result and UInt32($3F);
-  Result := Int32(Result);
-{$ENDIF FPC}
-end;
-
-end.

+ 49 - 49
CryptoLib/src/Math/Raw/ClpNat.pas

@@ -23,7 +23,7 @@ interface
 
 uses
   ClpConverters,
-  ClpBits,
+  ClpBitUtilities,
   ClpBigInteger,
   ClpArrayUtilities,
   ClpCryptoLibTypes;
@@ -1200,7 +1200,7 @@ begin
     raise EArgumentCryptoLibException.Create('');
   end;
 
-  len := TBits.Asr32((bits + 31), 5);
+  len := TBitUtilities.Asr32((bits + 31), 5);
   Result := Create(len);
   I := 0;
   while (Lx.SignValue <> 0) do
@@ -1246,7 +1246,7 @@ begin
     Exit;
   end;
 
-  w := TBits.Asr32(bit, 5);
+  w := TBitUtilities.Asr32(bit, 5);
   if ((w < 0) or (w >= System.Length(x))) then
   begin
     Result := 0;
@@ -1574,11 +1574,11 @@ begin
   while (I >= 0) do
   begin
     next := x[xOff + I];
-    z[zOff + I] := (next shr (bits)) or (TBits.NegativeLeftShift32(c, -bits));
+    z[zOff + I] := (next shr (bits)) or (TBitUtilities.NegativeLeftShift32(c, -bits));
     c := next;
     System.Dec(I);
   end;
-  Result := TBits.NegativeLeftShift32(c, -bits);
+  Result := TBitUtilities.NegativeLeftShift32(c, -bits);
 end;
 
 class function TNat.ShiftDownBits(len: Int32; const z: TCryptoLibUInt32Array;
@@ -1595,11 +1595,11 @@ begin
   while (I >= 0) do
   begin
     next := z[I];
-    z[I] := (next shr bits) or (TBits.NegativeLeftShift32(c, -bits));
+    z[I] := (next shr bits) or (TBitUtilities.NegativeLeftShift32(c, -bits));
     c := next;
     System.Dec(I);
   end;
-  Result := TBits.NegativeLeftShift32(c, -bits);
+  Result := TBitUtilities.NegativeLeftShift32(c, -bits);
 end;
 
 class function TNat.ShiftDownBits(len: Int32; const x: TCryptoLibUInt32Array;
@@ -1616,11 +1616,11 @@ begin
   while (I >= 0) do
   begin
     next := x[I];
-    z[I] := (next shr bits) or (TBits.NegativeLeftShift32(c, -bits));
+    z[I] := (next shr bits) or (TBitUtilities.NegativeLeftShift32(c, -bits));
     c := next;
     System.Dec(I);
   end;
-  Result := TBits.NegativeLeftShift32(c, -bits);
+  Result := TBitUtilities.NegativeLeftShift32(c, -bits);
 end;
 
 class function TNat.ShiftDownBits(len: Int32; const z: TCryptoLibUInt32Array;
@@ -1637,11 +1637,11 @@ begin
   while (I >= 0) do
   begin
     next := z[zOff + I];
-    z[zOff + I] := (next shr bits) or (TBits.NegativeLeftShift32(c, -bits));
+    z[zOff + I] := (next shr bits) or (TBitUtilities.NegativeLeftShift32(c, -bits));
     c := next;
     System.Dec(I);
   end;
-  Result := TBits.NegativeLeftShift32(c, -bits);
+  Result := TBitUtilities.NegativeLeftShift32(c, -bits);
 end;
 
 class function TNat.ShiftDownWord(len: Int32; const z: TCryptoLibUInt32Array;
@@ -1750,10 +1750,10 @@ begin
   for I := 0 to System.Pred(len) do
   begin
     next := x[xOff + I];
-    z[zOff + I] := (next shl bits) or (TBits.NegativeRightShift32(c, -bits));
+    z[zOff + I] := (next shl bits) or (TBitUtilities.NegativeRightShift32(c, -bits));
     c := next;
   end;
-  Result := TBits.NegativeRightShift32(c, -bits);
+  Result := TBitUtilities.NegativeRightShift32(c, -bits);
 end;
 
 class function TNat.ShiftUpBits(len: Int32; const x: TCryptoLibUInt32Array;
@@ -1768,10 +1768,10 @@ begin
   for I := 0 to System.Pred(len) do
   begin
     next := x[I];
-    z[I] := (next shl bits) or (TBits.NegativeRightShift32(c, -bits));
+    z[I] := (next shl bits) or (TBitUtilities.NegativeRightShift32(c, -bits));
     c := next;
   end;
-  Result := TBits.NegativeRightShift32(c, -bits);
+  Result := TBitUtilities.NegativeRightShift32(c, -bits);
 end;
 
 class function TNat.ShiftUpBits(len: Int32; const z: TCryptoLibUInt32Array;
@@ -1786,10 +1786,10 @@ begin
   for I := 0 to System.Pred(len) do
   begin
     next := z[I];
-    z[I] := (next shl bits) or (TBits.NegativeRightShift32(c, -bits));
+    z[I] := (next shl bits) or (TBitUtilities.NegativeRightShift32(c, -bits));
     c := next;
   end;
-  Result := TBits.NegativeRightShift32(c, -bits);
+  Result := TBitUtilities.NegativeRightShift32(c, -bits);
 end;
 
 class function TNat.ShiftUpBits(len: Int32; const z: TCryptoLibUInt32Array;
@@ -1804,10 +1804,10 @@ begin
   for I := 0 to System.Pred(len) do
   begin
     next := z[zOff + I];
-    z[zOff + I] := (next shl bits) or (TBits.NegativeRightShift32(c, -bits));
+    z[zOff + I] := (next shl bits) or (TBitUtilities.NegativeRightShift32(c, -bits));
     c := next;
   end;
-  Result := TBits.NegativeRightShift32(c, -bits);
+  Result := TBitUtilities.NegativeRightShift32(c, -bits);
 end;
 
 class function TNat.ShiftUpBits64(len: Int32; const x: TCryptoLibUInt64Array;
@@ -1823,10 +1823,10 @@ begin
   for I := 0 to System.Pred(len) do
   begin
     next := x[xOff + I];
-    z[zOff + I] := (next shl bits) or (TBits.NegativeRightShift32(c, -bits));
+    z[zOff + I] := (next shl bits) or (TBitUtilities.NegativeRightShift32(c, -bits));
     c := next;
   end;
-  Result := TBits.NegativeRightShift32(c, -bits);
+  Result := TBitUtilities.NegativeRightShift32(c, -bits);
 end;
 
 class function TNat.ShiftUpBits64(len: Int32; const z: TCryptoLibUInt64Array;
@@ -1841,10 +1841,10 @@ begin
   for I := 0 to System.Pred(len) do
   begin
     next := z[zOff + I];
-    z[zOff + I] := (next shl bits) or (TBits.NegativeRightShift32(c, -bits));
+    z[zOff + I] := (next shl bits) or (TBitUtilities.NegativeRightShift32(c, -bits));
     c := next;
   end;
-  Result := TBits.NegativeRightShift32(c, -bits);
+  Result := TBitUtilities.NegativeRightShift32(c, -bits);
 end;
 
 class procedure TNat.Square(len: Int32; const x, zz: TCryptoLibUInt32Array);
@@ -1946,7 +1946,7 @@ begin
   begin
     c := c + (Int64(x[xOff + I]) - y[yOff + I]);
     z[zOff + I] := UInt32(c);
-    c := TBits.Asr64(c, 32);
+    c := TBitUtilities.Asr64(c, 32);
   end;
   Result := Int32(c);
 end;
@@ -1962,7 +1962,7 @@ begin
   begin
     c := c + (Int64(x[I]) - y[I]);
     z[I] := UInt32(c);
-    c := TBits.Asr64(c, 32);
+    c := TBitUtilities.Asr64(c, 32);
   end;
   Result := Int32(c);
 end;
@@ -1977,10 +1977,10 @@ begin
 {$ENDIF DEBUG}
   c := Int64(z[zPos + 0]) - x;
   z[zPos + 0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[zPos + 1]) - 1);
   z[zPos + 1] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   if c = 0 then
   begin
     Result := 0;
@@ -2001,9 +2001,9 @@ begin
 {$ENDIF DEBUG}
   c := Int64(z[zOff + zPos]) - x;
   z[zOff + zPos] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[zOff + zPos + 1]) - 1);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   if c = 0 then
   begin
     Result := 0;
@@ -2021,10 +2021,10 @@ var
 begin
   c := Int64(z[0]) - x;
   z[0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[1]) - 1);
   z[1] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   if c = 0 then
   begin
     Result := 0;
@@ -2043,10 +2043,10 @@ var
 begin
   c := Int64(z[zOff + 0]) - x;
   z[zOff + 0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[zOff + 1]) - 1);
   z[zOff + 1] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   if c = 0 then
   begin
     Result := 0;
@@ -2068,7 +2068,7 @@ begin
   begin
     c := c + (Int64(z[I]) - x[I] - y[I]);
     z[I] := UInt32(c);
-    c := TBits.Asr64(c, 32);
+    c := TBitUtilities.Asr64(c, 32);
   end;
   Result := Int32(c);
 end;
@@ -2085,7 +2085,7 @@ begin
   begin
     c := c + (Int64(z[zOff + I]) - x[xOff + I] - y[yOff + I]);
     z[zOff + I] := UInt32(c);
-    c := TBits.Asr64(c, 32);
+    c := TBitUtilities.Asr64(c, 32);
   end;
   Result := Int32(c);
 end;
@@ -2100,10 +2100,10 @@ begin
 {$ENDIF DEBUG}
   c := Int64(z[zOff + zPos]) - Int64(x and M);
   z[zOff + zPos] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[zOff + zPos + 1]) - Int64(x shr 32));
   z[zOff + zPos + 1] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   if c = 0 then
   begin
     Result := 0;
@@ -2124,10 +2124,10 @@ begin
 {$ENDIF DEBUG}
   c := Int64(z[zPos + 0]) - Int64(x and M);
   z[zPos + 0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[zPos + 1]) - Int64(x shr 32));
   z[zPos + 1] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   if c = 0 then
   begin
     Result := 0;
@@ -2146,10 +2146,10 @@ var
 begin
   c := Int64(z[zOff + 0]) - Int64(x and M);
   z[zOff + 0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[zOff + 1]) - Int64(x shr 32));
   z[zOff + 1] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   if c = 0 then
   begin
     Result := 0;
@@ -2167,10 +2167,10 @@ var
 begin
   c := Int64(z[0]) - Int64(x and M);
   z[0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[1]) - Int64(x shr 32));
   z[1] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   if c = 0 then
   begin
     Result := 0;
@@ -2192,7 +2192,7 @@ begin
   begin
     c := c + (Int64(z[I]) - x[I]);
     z[I] := UInt32(c);
-    c := TBits.Asr64(c, 32);
+    c := TBitUtilities.Asr64(c, 32);
   end;
   Result := Int32(c);
 end;
@@ -2208,7 +2208,7 @@ begin
   begin
     c := c + (Int64(z[zOff + I]) - x[xOff + I]);
     z[zOff + I] := UInt32(c);
-    c := TBits.Asr64(c, 32);
+    c := TBitUtilities.Asr64(c, 32);
   end;
   Result := Int32(c);
 end;
@@ -2223,7 +2223,7 @@ begin
 {$ENDIF DEBUG}
   c := Int64(z[zPos]) - x;
   z[zPos] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   if c = 0 then
   begin
     Result := 0;
@@ -2245,7 +2245,7 @@ begin
 {$ENDIF DEBUG}
   c := Int64(z[zOff + zPos]) - x;
   z[zOff + zPos] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   if c = 0 then
   begin
     Result := 0;
@@ -2264,7 +2264,7 @@ var
 begin
   c := Int64(z[zOff + 0]) - x;
   z[zOff + 0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
 
   if c = 0 then
   begin
@@ -2284,7 +2284,7 @@ var
 begin
   c := Int64(z[0]) - x;
   z[0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
 
   if c = 0 then
   begin

+ 32 - 32
CryptoLib/src/Math/Raw/ClpNat192.pas

@@ -22,7 +22,7 @@ unit ClpNat192;
 interface
 
 uses
-  ClpBits,
+  ClpBitUtilities,
   ClpNat,
   ClpBigInteger,
   ClpConverters,
@@ -457,7 +457,7 @@ begin
     result := x[0] and 1;
     Exit;
   end;
-  w := TBits.Asr32(bit, 5);
+  w := TBitUtilities.Asr32(bit, 5);
   if ((w < 0) or (w >= 6)) then
   begin
     result := 0;
@@ -1249,22 +1249,22 @@ begin
   c := 0;
   c := c + (Int64(x[0]) - y[0]);
   z[0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[1]) - y[1]);
   z[1] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[2]) - y[2]);
   z[2] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[3]) - y[3]);
   z[3] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[4]) - y[4]);
   z[4] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[5]) - y[5]);
   z[5] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   result := Int32(c);
 end;
 
@@ -1277,22 +1277,22 @@ begin
   c := 0;
   c := c + (Int64(x[xOff + 0]) - y[yOff + 0]);
   z[zOff + 0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[xOff + 1]) - y[yOff + 1]);
   z[zOff + 1] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[xOff + 2]) - y[yOff + 2]);
   z[zOff + 2] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[xOff + 3]) - y[yOff + 3]);
   z[zOff + 3] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[xOff + 4]) - y[yOff + 4]);
   z[zOff + 4] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[xOff + 5]) - y[yOff + 5]);
   z[zOff + 5] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   result := Int32(c);
 end;
 
@@ -1303,22 +1303,22 @@ begin
   c := 0;
   c := c + (Int64(z[0]) - x[0] - y[0]);
   z[0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[1]) - x[1] - y[1]);
   z[1] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[2]) - x[2] - y[2]);
   z[2] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[3]) - x[3] - y[3]);
   z[3] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[4]) - x[4] - y[4]);
   z[4] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[5]) - x[5] - y[5]);
   z[5] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   result := Int32(c);
 end;
 
@@ -1329,22 +1329,22 @@ begin
   c := 0;
   c := c + (Int64(z[0]) - x[0]);
   z[0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[1]) - x[1]);
   z[1] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[2]) - x[2]);
   z[2] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[3]) - x[3]);
   z[3] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[4]) - x[4]);
   z[4] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[5]) - x[5]);
   z[5] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   result := Int32(c);
 end;
 
@@ -1356,22 +1356,22 @@ begin
   c := 0;
   c := c + (Int64(z[zOff + 0]) - x[xOff + 0]);
   z[zOff + 0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[zOff + 1]) - x[xOff + 1]);
   z[zOff + 1] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[zOff + 2]) - x[xOff + 2]);
   z[zOff + 2] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[zOff + 3]) - x[xOff + 3]);
   z[zOff + 3] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[zOff + 4]) - x[xOff + 4]);
   z[zOff + 4] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[zOff + 5]) - x[xOff + 5]);
   z[zOff + 5] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   result := Int32(c);
 end;
 

+ 42 - 42
CryptoLib/src/Math/Raw/ClpNat256.pas

@@ -24,7 +24,7 @@ interface
 uses
   ClpNat,
   ClpConverters,
-  ClpBits,
+  ClpBitUtilities,
   ClpBigInteger,
   ClpArrayUtilities,
   ClpCryptoLibTypes;
@@ -574,7 +574,7 @@ begin
     result := 0;
     Exit;
   end;
-  w := TBits.Asr32(bit, 5);
+  w := TBitUtilities.Asr32(bit, 5);
   b := bit and 31;
   result := (x[w] shr b) and 1;
 end;
@@ -1573,28 +1573,28 @@ begin
   c := 0;
   c := c + (Int64(x[0]) - y[0]);
   z[0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[1]) - y[1]);
   z[1] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[2]) - y[2]);
   z[2] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[3]) - y[3]);
   z[3] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[4]) - y[4]);
   z[4] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[5]) - y[5]);
   z[5] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[6]) - y[6]);
   z[6] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[7]) - y[7]);
   z[7] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   result := Int32(c);
 end;
 
@@ -1607,28 +1607,28 @@ begin
   c := 0;
   c := c + (Int64(x[xOff + 0]) - y[yOff + 0]);
   z[zOff + 0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[xOff + 1]) - y[yOff + 1]);
   z[zOff + 1] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[xOff + 2]) - y[yOff + 2]);
   z[zOff + 2] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[xOff + 3]) - y[yOff + 3]);
   z[zOff + 3] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[xOff + 4]) - y[yOff + 4]);
   z[zOff + 4] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[xOff + 5]) - y[yOff + 5]);
   z[zOff + 5] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[xOff + 6]) - y[yOff + 6]);
   z[zOff + 6] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(x[xOff + 7]) - y[yOff + 7]);
   z[zOff + 7] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   result := Int32(c);
 end;
 
@@ -1639,28 +1639,28 @@ begin
   c := 0;
   c := c + (Int64(z[0]) - x[0] - y[0]);
   z[0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[1]) - x[1] - y[1]);
   z[1] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[2]) - x[2] - y[2]);
   z[2] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[3]) - x[3] - y[3]);
   z[3] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[4]) - x[4] - y[4]);
   z[4] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[5]) - x[5] - y[5]);
   z[5] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[6]) - x[6] - y[6]);
   z[6] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[7]) - x[7] - y[7]);
   z[7] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   result := Int32(c);
 end;
 
@@ -1671,28 +1671,28 @@ begin
   c := 0;
   c := c + (Int64(z[0]) - x[0]);
   z[0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[1]) - x[1]);
   z[1] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[2]) - x[2]);
   z[2] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[3]) - x[3]);
   z[3] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[4]) - x[4]);
   z[4] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[5]) - x[5]);
   z[5] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[6]) - x[6]);
   z[6] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[7]) - x[7]);
   z[7] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   result := Int32(c);
 end;
 
@@ -1704,28 +1704,28 @@ begin
   c := 0;
   c := c + (Int64(z[zOff + 0]) - x[xOff + 0]);
   z[zOff + 0] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[zOff + 1]) - x[xOff + 1]);
   z[zOff + 1] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[zOff + 2]) - x[xOff + 2]);
   z[zOff + 2] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[zOff + 3]) - x[xOff + 3]);
   z[zOff + 3] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[zOff + 4]) - x[xOff + 4]);
   z[zOff + 4] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[zOff + 5]) - x[xOff + 5]);
   z[zOff + 5] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[zOff + 6]) - x[xOff + 6]);
   z[zOff + 6] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   c := c + (Int64(z[zOff + 7]) - x[xOff + 7]);
   z[zOff + 7] := UInt32(c);
-  c := TBits.Asr64(c, 32);
+  c := TBitUtilities.Asr64(c, 32);
   result := Int32(c);
 end;
 

+ 1 - 1
CryptoLib/src/Packages/Delphi/CryptoLib4PascalPackage.dpk

@@ -44,7 +44,7 @@ contains
   ClpStreams in '..\..\Utils\ClpStreams.pas',
   ClpPlatform in '..\..\Utils\ClpPlatform.pas',
   ClpCollectionUtilities in '..\..\Utils\ClpCollectionUtilities.pas',
-  ClpBits in '..\..\Utils\ClpBits.pas',
+  ClpBitUtilities in '..\..\GeneralUtilities\ClpBitUtilities.pas',
   ClpGeneratorUtilities in '..\..\Security\ClpGeneratorUtilities.pas',
   ClpParameterUtilities in '..\..\Security\ClpParameterUtilities.pas',
   ClpBitConverter in '..\..\Utils\ClpBitConverter.pas',

+ 2 - 2
CryptoLib/src/Packages/FPC/CryptoLib4PascalPackage.lpk

@@ -423,8 +423,8 @@ Thanks to Sphere 10 Software (http://www.sphere10.com/) for sponsoring the devel
         <UnitName Value="ClpBitConverter"/>
       </Item99>
       <Item100>
-        <Filename Value="..\..\Utils\ClpBits.pas"/>
-        <UnitName Value="ClpBits"/>
+        <Filename Value="..\..\GeneralUtilities\ClpBitUtilities.pas"/>
+        <UnitName Value="ClpBitUtilities"/>
       </Item100>
       <Item101>
         <Filename Value="..\..\Utils\ClpConverters.pas"/>

+ 1 - 1
CryptoLib/src/Packages/FPC/CryptoLib4PascalPackage.pas

@@ -39,7 +39,7 @@ uses
   ClpWTauNafPreCompInfo, ClpFiniteFields, ClpGenericPolynomialExtensionField, 
   ClpGF2Polynomial, ClpPrimeField, ClpMod, ClpNat, ClpDigestUtilities, 
   ClpRandom, ClpSecureRandom, ClpSignerUtilities, ClpArrayUtils, 
-  ClpBigIntegers, ClpBitConverter, ClpBits, ClpConverters, ClpCryptoLibTypes, 
+  ClpBigIntegers, ClpBitConverter, ClpBitUtilities, ClpConverters, ClpCryptoLibTypes, 
   ClpTimes, ClpOSRandom, ClpRandomNumberGenerator, ClpSetWeakRef, 
   ClpParameterUtilities, ClpGeneratorUtilities, ClpCipherUtilities, 
   ClpIAesEngine, ClpIParametersWithIV, ClpIPaddedBufferedBlockCipher, 

+ 2 - 2
CryptoLib/src/X509/ClpX509Utilities.pas

@@ -25,7 +25,7 @@ uses
   SysUtils,
   Classes,
   Generics.Collections,
-  ClpBits,
+  ClpBitUtilities,
   ClpAsn1Objects,
   ClpIAsn1Objects,
   ClpIX509Asn1Objects,
@@ -301,7 +301,7 @@ begin
   SetLength(LBytes, LByteLen);
   for I := 0 to System.High(AId) do
     if AId[I] then
-      LBytes[TBits.Asr32(I, 3)] := LBytes[TBits.Asr32(I, 3)] or Byte(TBits.Asr32($80, (I and 7)));
+      LBytes[TBitUtilities.Asr32(I, 3)] := LBytes[TBitUtilities.Asr32(I, 3)] or Byte(TBitUtilities.Asr32($80, (I and 7)));
   LPad := (8 - System.Length(AId)) and 7;
   Result := TDerBitString.Create(LBytes, LPad);
 end;