Browse Source

some minor fixes.

Ugochukwu Mmaduekwe 7 years ago
parent
commit
120c295c6b

+ 9 - 1
CryptoLib.Samples/Delphi.Samples/UsageSamples.dpr

@@ -415,7 +415,15 @@ uses
   ClpNat320 in '..\..\CryptoLib\src\Math\Raw\ClpNat320.pas',
   ClpNat256 in '..\..\CryptoLib\src\Math\Raw\ClpNat256.pas',
   ClpAesLightEngine in '..\..\CryptoLib\src\Crypto\Engines\ClpAesLightEngine.pas',
-  ClpIAesLightEngine in '..\..\CryptoLib\src\Interfaces\ClpIAesLightEngine.pas';
+  ClpIAesLightEngine in '..\..\CryptoLib\src\Interfaces\ClpIAesLightEngine.pas',
+  ClpCustomNamedCurves in '..\..\CryptoLib\src\Crypto\EC\ClpCustomNamedCurves.pas',
+  ClpSecP256K1Field in '..\..\CryptoLib\src\Math\EC\Custom\Sec\ClpSecP256K1Field.pas',
+  ClpSecP256K1FieldElement in '..\..\CryptoLib\src\Math\EC\Custom\Sec\ClpSecP256K1FieldElement.pas',
+  ClpISecP256K1FieldElement in '..\..\CryptoLib\src\Interfaces\ClpISecP256K1FieldElement.pas',
+  ClpSecP256K1Point in '..\..\CryptoLib\src\Math\EC\Custom\Sec\ClpSecP256K1Point.pas',
+  ClpISecP256K1Point in '..\..\CryptoLib\src\Interfaces\ClpISecP256K1Point.pas',
+  ClpSecP256K1Curve in '..\..\CryptoLib\src\Math\EC\Custom\Sec\ClpSecP256K1Curve.pas',
+  ClpISecP256K1Curve in '..\..\CryptoLib\src\Interfaces\ClpISecP256K1Curve.pas';
 
 begin
   try

+ 3 - 2
CryptoLib.Samples/src/UsageExamples.pas

@@ -82,7 +82,8 @@ uses
   ClpIAsymmetricCipherKeyPairGenerator,
   ClpArrayUtils,
   ClpHex,
-  ClpSecNamedCurves;
+  ClpSecNamedCurves,
+  ClpCustomNamedCurves;
 
 type
   TUsageExamples = class sealed(TObject)
@@ -980,7 +981,7 @@ end;
 class constructor TUsageExamples.UsageExamples;
 begin
   FRandom := TSecureRandom.Create();
-  FCurve := TSecNamedCurves.GetByName(CurveName);
+  FCurve := TCustomNamedCurves.GetByName(CurveName);
 end;
 
 end.

+ 0 - 1
CryptoLib/src/Interfaces/ClpISecP256K1Curve.pas

@@ -23,7 +23,6 @@ interface
 
 uses
   ClpIECInterface,
-  ClpIECFieldElement,
   ClpBigInteger,
   ClpCryptoLibTypes;
 

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

@@ -43,7 +43,7 @@ type
       zPos: Int32): UInt32; overload; static; inline;
 
     class function Add33At(len: Int32; x: UInt32; z: TCryptoLibUInt32Array;
-      zOff, zPos: Int32): UInt32; overload; static; inline;
+      zOff, zPos: Int32): UInt32; overload; static;
 
     class function Add33To(len: Int32; x: UInt32; z: TCryptoLibUInt32Array)
       : UInt32; overload; static; inline;
@@ -132,10 +132,10 @@ type
       overload; static; inline;
 
     class function IncAt(len: Int32; z: TCryptoLibUInt32Array; zPos: Int32)
-      : UInt32; overload; static; inline;
+      : UInt32; overload; static;
 
     class function IncAt(len: Int32; z: TCryptoLibUInt32Array;
-      zOff, zPos: Int32): UInt32; overload; static; inline;
+      zOff, zPos: Int32): UInt32; overload; static;
 
     class function IsOne(len: Int32; x: TCryptoLibUInt32Array): Boolean;
       static; inline;
@@ -1447,10 +1447,10 @@ begin
   for I := 0 to System.Pred(len) do
   begin
     next := x[xOff + I];
-    z[zOff + I] := (next shl bits) or (c shr (-bits));
+    z[zOff + I] := (next shl bits) or (TBits.NegativeRightShift32(c, -bits));
     c := next;
   end;
-  Result := c shr (-bits);
+  Result := TBits.NegativeRightShift32(c, -bits);
 end;
 
 class function TNat.ShiftUpBits(len: Int32; x: TCryptoLibUInt32Array;
@@ -1465,10 +1465,10 @@ begin
   for I := 0 to System.Pred(len) do
   begin
     next := x[I];
-    z[I] := (next shl bits) or (c shr (-bits));
+    z[I] := (next shl bits) or (TBits.NegativeRightShift32(c, -bits));
     c := next;
   end;
-  Result := c shr (-bits);
+  Result := TBits.NegativeRightShift32(c, -bits);
 end;
 
 class function TNat.ShiftUpBits(len: Int32; z: TCryptoLibUInt32Array;
@@ -1483,10 +1483,10 @@ begin
   for I := 0 to System.Pred(len) do
   begin
     next := z[I];
-    z[I] := (next shl bits) or (c shr (-bits));
+    z[I] := (next shl bits) or (TBits.NegativeRightShift32(c, -bits));
     c := next;
   end;
-  Result := c shr (-bits);
+  Result := TBits.NegativeRightShift32(c, -bits);
 end;
 
 class function TNat.ShiftUpBits(len: Int32; z: TCryptoLibUInt32Array;
@@ -1501,10 +1501,10 @@ begin
   for I := 0 to System.Pred(len) do
   begin
     next := z[zOff + I];
-    z[zOff + I] := (next shl bits) or (c shr (-bits));
+    z[zOff + I] := (next shl bits) or (TBits.NegativeRightShift32(c, -bits));
     c := next;
   end;
-  Result := c shr (-bits);
+  Result := TBits.NegativeRightShift32(c, -bits);
 end;
 
 class function TNat.ShiftUpBits64(len: Int32; x: TCryptoLibUInt64Array;
@@ -1519,10 +1519,10 @@ begin
   for I := 0 to System.Pred(len) do
   begin
     next := x[xOff + I];
-    z[zOff + I] := (next shl bits) or (c shr (-bits));
+    z[zOff + I] := (next shl bits) or (TBits.NegativeRightShift32(c, -bits));
     c := next;
   end;
-  Result := c shr (-bits);
+  Result := TBits.NegativeRightShift32(c, -bits);
 end;
 
 class function TNat.ShiftUpBits64(len: Int32; z: TCryptoLibUInt64Array;
@@ -1537,10 +1537,10 @@ begin
   for I := 0 to System.Pred(len) do
   begin
     next := z[zOff + I];
-    z[zOff + I] := (next shl bits) or (c shr (-bits));
+    z[zOff + I] := (next shl bits) or (TBits.NegativeRightShift32(c, -bits));
     c := next;
   end;
-  Result := c shr (-bits);
+  Result := TBits.NegativeRightShift32(c, -bits);
 end;
 
 class procedure TNat.Square(len: Int32; x, zz: TCryptoLibUInt32Array);

+ 37 - 0
CryptoLib/src/Utils/ClpBits.pas

@@ -69,6 +69,9 @@ type
     /// 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>
@@ -83,6 +86,28 @@ type
     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;
+
     class function RotateLeft32(a_value: UInt32; a_n: Int32): UInt32; overload;
       static; inline;
     class function RotateLeft64(a_value: UInt64; a_n: Int32): UInt64; overload;
@@ -215,9 +240,21 @@ 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.RotateLeft32(a_value: UInt32; a_n: Int32): UInt32;
 begin
 {$IFDEF DEBUG}