Browse Source

some minor optimizations.

* minor optimizations
* bumped version number
Ugochukwu Mmaduekwe 7 years ago
parent
commit
d4c136f57e

+ 35 - 35
CryptoLib.Tests/src/Others/SignerUtilitiesTests.pas

@@ -28,7 +28,7 @@ uses
 {$ELSE}
   TestFramework,
 {$ENDIF FPC}
-  //ClpBase64,
+  ClpBase64,
   ClpBigInteger,
   ClpCryptoLibTypes,
   ClpISigner,
@@ -84,40 +84,40 @@ implementation
 procedure TTestSignerUtilities.SetUp;
 begin
   inherited;
-  // FECParraGX := TBigInteger.Create
-  // (TBase64.Decode('D/qWPNyogWzMM7hkK+35BcPTWFc9Pyf7vTs8uaqv'));
-  // FECParraGY := TBigInteger.Create
-  // (TBase64.Decode('AhQXGxb1olGRv6s1LPRfuatMF+cx3ZTGgzSE/Q5R'));
-  // FECParraH := TBigInteger.Create(TBase64.Decode('AQ:=:='));
-  // FECParraN := TBigInteger.Create
-  // (TBase64.Decode('f///////////////f///nl6an12QcfvRUiaIkJ0L'));
-  // FECPubQX := TBigInteger.Create
-  // (TBase64.Decode('HWWi17Yb+Bm3PYr/DMjLOYNFhyOwX1QY7ZvqqM+l'));
-  // FECPubQY := TBigInteger.Create
-  // (TBase64.Decode('JrlJfxu3WGhqwtL/55BOs/wsUeiDFsvXcGhB8DGx'));
-  // FECPrivD := TBigInteger.Create
-  // (TBase64.Decode('GYQmd/NF1B+He1iMkWt3by2Az6Eu07t0ynJ4YCAo'));
-
-  FECParraGX := TBigInteger.Create(TCryptoLibByteArray.Create(15, 250, 150, 60,
-    220, 168, 129, 108, 204, 51, 184, 100, 43, 237, 249, 5, 195, 211, 88, 87,
-    61, 63, 39, 251, 189, 59, 60, 185, 170, 175));
-  FECParraGY := TBigInteger.Create(TCryptoLibByteArray.Create(2, 20, 23, 27, 22,
-    245, 162, 81, 145, 191, 171, 53, 44, 244, 95, 185, 171, 76, 23, 231, 49,
-    221, 148, 198, 131, 52, 132, 253, 14, 81));
-
-  FECParraH := TBigInteger.Create(TCryptoLibByteArray.Create(1));
-  FECParraN := TBigInteger.Create(TCryptoLibByteArray.Create(127, 255, 255, 255,
-    255, 255, 255, 255, 255, 255, 255, 255, 127, 255, 255, 158, 94, 154, 159,
-    93, 144, 113, 251, 209, 82, 38, 136, 144, 157, 11));
-  FECPubQX := TBigInteger.Create(TCryptoLibByteArray.Create(29, 101, 162, 215,
-    182, 27, 248, 25, 183, 61, 138, 255, 12, 200, 203, 57, 131, 69, 135, 35,
-    176, 95, 84, 24, 237, 155, 234, 168, 207, 165));
-  FECPubQY := TBigInteger.Create(TCryptoLibByteArray.Create(38, 185, 73, 127,
-    27, 183, 88, 104, 106, 194, 210, 255, 231, 144, 78, 179, 252, 44, 81, 232,
-    131, 22, 203, 215, 112, 104, 65, 240, 49, 177));
-  FECPrivD := TBigInteger.Create(TCryptoLibByteArray.Create(25, 132, 38, 119,
-    243, 69, 212, 31, 135, 123, 88, 140, 145, 107, 119, 111, 45, 128, 207, 161,
-    46, 211, 187, 116, 202, 114, 120, 96, 32, 40));
+  FECParraGX := TBigInteger.Create
+    (TBase64.Decode('D/qWPNyogWzMM7hkK+35BcPTWFc9Pyf7vTs8uaqv'));
+  FECParraGY := TBigInteger.Create
+    (TBase64.Decode('AhQXGxb1olGRv6s1LPRfuatMF+cx3ZTGgzSE/Q5R'));
+  FECParraH := TBigInteger.Create(TBase64.Decode('AQ:=:='));
+  FECParraN := TBigInteger.Create
+    (TBase64.Decode('f///////////////f///nl6an12QcfvRUiaIkJ0L'));
+  FECPubQX := TBigInteger.Create
+    (TBase64.Decode('HWWi17Yb+Bm3PYr/DMjLOYNFhyOwX1QY7ZvqqM+l'));
+  FECPubQY := TBigInteger.Create
+    (TBase64.Decode('JrlJfxu3WGhqwtL/55BOs/wsUeiDFsvXcGhB8DGx'));
+  FECPrivD := TBigInteger.Create
+    (TBase64.Decode('GYQmd/NF1B+He1iMkWt3by2Az6Eu07t0ynJ4YCAo'));
+
+  // FECParraGX := TBigInteger.Create(TCryptoLibByteArray.Create(15, 250, 150, 60,
+  // 220, 168, 129, 108, 204, 51, 184, 100, 43, 237, 249, 5, 195, 211, 88, 87,
+  // 61, 63, 39, 251, 189, 59, 60, 185, 170, 175));
+  // FECParraGY := TBigInteger.Create(TCryptoLibByteArray.Create(2, 20, 23, 27, 22,
+  // 245, 162, 81, 145, 191, 171, 53, 44, 244, 95, 185, 171, 76, 23, 231, 49,
+  // 221, 148, 198, 131, 52, 132, 253, 14, 81));
+  //
+  // FECParraH := TBigInteger.Create(TCryptoLibByteArray.Create(1));
+  // FECParraN := TBigInteger.Create(TCryptoLibByteArray.Create(127, 255, 255, 255,
+  // 255, 255, 255, 255, 255, 255, 255, 255, 127, 255, 255, 158, 94, 154, 159,
+  // 93, 144, 113, 251, 209, 82, 38, 136, 144, 157, 11));
+  // FECPubQX := TBigInteger.Create(TCryptoLibByteArray.Create(29, 101, 162, 215,
+  // 182, 27, 248, 25, 183, 61, 138, 255, 12, 200, 203, 57, 131, 69, 135, 35,
+  // 176, 95, 84, 24, 237, 155, 234, 168, 207, 165));
+  // FECPubQY := TBigInteger.Create(TCryptoLibByteArray.Create(38, 185, 73, 127,
+  // 27, 183, 88, 104, 106, 194, 210, 255, 231, 144, 78, 179, 252, 44, 81, 232,
+  // 131, 22, 203, 215, 112, 104, 65, 240, 49, 177));
+  // FECPrivD := TBigInteger.Create(TCryptoLibByteArray.Create(25, 132, 38, 119,
+  // 243, 69, 212, 31, 135, 123, 88, 140, 145, 107, 119, 111, 45, 128, 207, 161,
+  // 46, 211, 187, 116, 202, 114, 120, 96, 32, 40));
 
   Fcurve := TFpCurve.Create
     (TBigInteger.Create

+ 1 - 1
CryptoLib/src/Asn1/ClpDerBmpString.pas

@@ -123,7 +123,7 @@ begin
     raise EArgumentNilCryptoLibException.CreateRes(@SEmptyInput);
   end;
 
-  System.SetLength(cs, System.Length(astr) div 2);
+  System.SetLength(cs, System.Length(astr) shr 1);
 
   i := 0;
 

+ 1 - 1
CryptoLib/src/Math/ClpBigInteger.pas

@@ -3355,7 +3355,7 @@ begin
   System.Assert(bits > 2);
   System.Assert(TestBit(0));
 {$ENDIF DEBUG}
-  iterations := ((certainty - 1) div 2) + 1;
+  iterations := ((certainty - 1) shr 1) + 1;
   if (randomlySelected) then
   begin
     if bits >= 1024 then

+ 1 - 1
CryptoLib/src/Math/EC/Abc/ClpTnaf.pas

@@ -331,7 +331,7 @@ var
   _k: Int32;
   ns, gs, hs, js, gsPlusJs, ls: TBigInteger;
 begin
-  _k := ((m + 5) div 2) + c;
+  _k := ((m + 5) shr 1) + c;
   ns := k.ShiftRight(m - _k - 2 + a);
 
   gs := s.Multiply(ns);

+ 9 - 5
CryptoLib/src/Packages/FPC/CryptoLib4PascalPackage.lpk

@@ -24,7 +24,7 @@
 
  Acknowledgements: 
 Thanks to Sphere 10 Software (http://sphere10.com) for sponsoring the development of this library "/>
-    <Version Major="1" Minor="2"/>
+    <Version Major="1" Minor="3"/>
     <Files Count="281">
       <Item1>
         <Filename Value="..\..\Asn1\ClpAsn1Encodable.pas"/>
@@ -1151,14 +1151,18 @@ Thanks to Sphere 10 Software (http://sphere10.com) for sponsoring the developmen
         <UnitName Value="ClpIECSchnorrSigner"/>
       </Item281>
     </Files>
-    <RequiredPkgs Count="2">
+    <RequiredPkgs Count="3">
       <Item1>
-        <PackageName Value="HashLib4PascalPackage"/>
-        <MinVersion Major="2" Minor="3" Valid="True"/>
+        <PackageName Value="SimpleBaseLib4PascalPackage"/>
+        <MinVersion Major="1" Minor="2" Valid="True"/>
       </Item1>
       <Item2>
-        <PackageName Value="FCL"/>
+        <PackageName Value="HashLib4PascalPackage"/>
+        <MinVersion Major="2" Minor="3" Valid="True"/>
       </Item2>
+      <Item3>
+        <PackageName Value="FCL"/>
+      </Item3>
     </RequiredPkgs>
     <UsageOptions>
       <UnitPath Value="$(PkgOutDir)"/>

+ 1 - 1
CryptoLib/src/Utils/ClpBigIntegers.pas

@@ -148,7 +148,7 @@ begin
     Exit;
   end;
 
-  if (min.BitLength > (max.BitLength div 2)) then
+  if (min.BitLength > (max.BitLength shr 1)) then
   begin
     Result := CreateRandomInRange(TBigInteger.Zero, max.Subtract(min),
       random).Add(min);

+ 3 - 59
CryptoLib/src/Utils/Encoders/ClpBase64.pas

@@ -22,17 +22,7 @@ unit ClpBase64;
 interface
 
 uses
-{$IFDEF DELPHIXE7_UP}
-  System.NetEncoding,
-{$ENDIF DELPHIXE7_UP}
-{$IFDEF DELPHI}
-  Classes,
-  EncdDecd,
-{$ENDIF DELPHI}
-{$IFDEF FPC}
-  base64,
-{$ENDIF FPC}
-  SysUtils,
+  SbpBase64,
   ClpCryptoLibTypes;
 
 type
@@ -52,58 +42,12 @@ implementation
 class function TBase64.Decode(const Input:
 {$IFDEF FPC}UnicodeString{$ELSE}String{$ENDIF FPC}): TCryptoLibByteArray;
 begin
-{$IFDEF DELPHIXE7_UP}
-  Result := TNetEncoding.base64.DecodeStringToBytes(Input);
-{$ELSE}
-{$IFDEF DELPHI}
-  Result := DecodeBase64(Input);
-{$ENDIF DELPHI}
-{$ENDIF DELPHIXE7_UP}
-{$IFDEF FPC}
-  Result := TEncoding.UTF8.GetBytes
-    (UnicodeString(DecodeStringBase64(String(Input))));
-{$ENDIF FPC}
+  result := SbpBase64.TBase64.Default.Decode(Input);
 end;
 
 class function TBase64.Encode(Input: TCryptoLibByteArray): String;
-var
-{$IFDEF DELPHIXE7_UP}
-  TempHolder: TCryptoLibByteArray;
-{$ELSE}
-{$IFDEF DELPHI}
-  TempHolder: TBytesStream;
-{$ENDIF DELPHI}
-{$ENDIF DELPHIXE7_UP}
-{$IFDEF FPC}
-  TempHolder: String;
-{$ENDIF FPC}
 begin
-{$IFDEF DELPHIXE7_UP}
-  TempHolder := Input;
-{$ELSE}
-{$IFDEF DELPHI}
-  TempHolder := TBytesStream.Create(Input);
-{$ENDIF DELPHI}
-{$ENDIF DELPHIXE7_UP}
-{$IFDEF FPC}
-  TempHolder := EncodeStringBase64(String(TEncoding.UTF8.GetString(Input)));
-{$ENDIF FPC}
-{$IFDEF DELPHIXE7_UP}
-  Result := StringReplace(TNetEncoding.base64.EncodeBytesToString(TempHolder),
-    sLineBreak, '', [rfReplaceAll]);
-{$ELSE}
-{$IFDEF DELPHI}
-  try
-    Result := StringReplace(String(EncodeBase64(TempHolder.Memory,
-      TempHolder.Size)), sLineBreak, '', [rfReplaceAll]);
-  finally
-    TempHolder.Free;
-  end;
-{$ENDIF DELPHI}
-{$ENDIF DELPHIXE7_UP}
-{$IFDEF FPC}
-  Result := TempHolder;
-{$ENDIF FPC}
+  result := SbpBase64.TBase64.Default.Encode(Input);
 end;
 
 end.

+ 3 - 5
CryptoLib/src/Utils/Encoders/ClpHex.pas

@@ -22,8 +22,7 @@ unit ClpHex;
 interface
 
 uses
-  Classes,
-  ClpConverters,
+  SbpBase16,
   ClpCryptoLibTypes;
 
 type
@@ -40,13 +39,12 @@ implementation
 
 class function THex.Decode(const Hex: String): TCryptoLibByteArray;
 begin
-  System.SetLength(result, System.length(Hex) shr 1);
-  HexToBin(PChar(Hex), @result[0], System.length(result));
+  result := TBase16.Decode(Hex);
 end;
 
 class function THex.Encode(Input: TCryptoLibByteArray): String;
 begin
-  result := TConverters.ConvertBytesToHexString(Input, False);
+  result := TBase16.EncodeUpper(Input);
 end;
 
 end.