Explorar o código

some ec related fixes

Ugochukwu Mmaduekwe hai 4 días
pai
achega
d3f5bec2c6

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

@@ -876,7 +876,7 @@ end;
 
 constructor TFp.Create;
 begin
-  Fq := TBigInteger.Create('29');
+  Fq := TBigInteger.Create('1063');
 
   Fa := TBigInteger.Create('4');
 
@@ -890,7 +890,7 @@ begin
 
   FInfinity := Fcurve.infinity;
 
-  FpointSource := TCryptoLibInt32Array.Create(5, 22, 16, 27, 13, 6, 14, 6);
+  FpointSource := TCryptoLibInt32Array.Create(1, 5, 4, 10, 234, 1024, 817, 912);
 
   System.SetLength(Fp, System.Length(FpointSource) div 2);
 

+ 44 - 51
CryptoLib/src/Math/EC/ClpECPoint.pas

@@ -75,9 +75,14 @@ type
     function GetCurveCoordinateSystem: Int32; virtual;
     function CreateScaledPoint(const ASx, ASy: IECFieldElement): IECPoint; virtual;
 
-    function RawXCoord: IECFieldElement; inline;
-    function RawYCoord: IECFieldElement; inline;
-    function RawZCoords: TCryptoLibGenericArray<IECFieldElement>; inline;
+    function GetRawXCoord: IECFieldElement; inline;
+    function GetRawYCoord: IECFieldElement; inline;
+    function GetRawZCoords: TCryptoLibGenericArray<IECFieldElement>; inline;
+
+    property RawXCoord: IECFieldElement read GetRawXCoord;
+    property RawYCoord: IECFieldElement read GetRawYCoord;
+    property RawZCoords: TCryptoLibGenericArray<IECFieldElement> read GetRawZCoords;
+
     procedure CheckNormalized; virtual;
 
     function Detach: IECPoint; virtual; abstract;  // called from GetDetachedPoint on normalized point
@@ -96,8 +101,6 @@ type
     function GetYCoord: IECFieldElement; virtual;
     property XCoord: IECFieldElement read GetXCoord;
     property YCoord: IECFieldElement read GetYCoord;
-    function GetRawXCoord: IECFieldElement; virtual;
-    function GetRawYCoord: IECFieldElement; virtual;
     function GetZCoord(AIndex: Int32): IECFieldElement; virtual;
     function GetZCoords: TCryptoLibGenericArray<IECFieldElement>; virtual;
 
@@ -326,27 +329,17 @@ begin
     Result := FCurve.CoordinateSystem;
 end;
 
-function TECPoint.RawXCoord: IECFieldElement;
-begin
-  Result := FX;
-end;
-
-function TECPoint.RawYCoord: IECFieldElement;
-begin
-  Result := FY;
-end;
-
 function TECPoint.GetRawXCoord: IECFieldElement;
 begin
-  Result := RawXCoord;
+  Result := FX;
 end;
 
 function TECPoint.GetRawYCoord: IECFieldElement;
 begin
-  Result := RawYCoord;
+  Result := FY;
 end;
 
-function TECPoint.RawZCoords: TCryptoLibGenericArray<IECFieldElement>;
+function TECPoint.GetRawZCoords: TCryptoLibGenericArray<IECFieldElement>;
 begin
   Result := FZs;
 end;
@@ -454,7 +447,7 @@ var
   LZ: IECFieldElement;
   LB, LZInv: IECFieldElement;
 begin
-  if GetIsInfinity then
+  if IsInfinity then
     Exit(Self as IECPoint);
 
   LCoord := GetCurveCoordinateSystem();
@@ -464,7 +457,7 @@ begin
   else
     begin
       LZ := RawZCoords[0];
-      if LZ.GetIsOne then
+      if LZ.IsOne then
         Exit(Self as IECPoint);
 
       if FCurve = nil then
@@ -537,13 +530,13 @@ end;
 function TECPoint.GetAffineXCoord: IECFieldElement;
 begin
   CheckNormalized();
-  Result := FX;
+  Result := XCoord;
 end;
 
 function TECPoint.GetAffineYCoord: IECFieldElement;
 begin
   CheckNormalized();
-  Result := FY;
+  Result := YCoord;
 end;
 
 function TECPoint.GetEncoded: TCryptoLibByteArray;
@@ -988,9 +981,9 @@ var
   LZ3Squared, LW3: IECFieldElement;
   LZs: TCryptoLibGenericArray<IECFieldElement>;
 begin
-  if GetIsInfinity then
+  if IsInfinity then
     Exit(AB);
-  if AB.GetIsInfinity then
+  if AB.IsInfinity then
     Exit(Self as IECPoint);
   if (Self as IECPoint) = AB then
     Exit(Twice());
@@ -999,17 +992,17 @@ begin
   LCoord := GetCurveCoordinateSystem();
   X1 := RawXCoord;
   Y1 := RawYCoord;
-  X2 := AB.GetRawXCoord;
-  Y2 := AB.GetRawYCoord;
+  X2 := AB.RawXCoord;
+  Y2 := AB.RawYCoord;
 
   case LCoord of
     TECCurveConstants.COORD_AFFINE:
     begin
       Dx := X2.Subtract(X1);
       Dy := Y2.Subtract(Y1);
-      if Dx.GetIsZero then
+      if Dx.IsZero then
       begin
-        if Dy.GetIsZero then
+        if Dy.IsZero then
           Exit(Twice());
         Exit(LCurve.Infinity);
       end;
@@ -1035,9 +1028,9 @@ begin
       LV := LV1.Subtract(LV2);
 
       // Check if b == this or b == -this
-      if LV.GetIsZero then
+      if LV.IsZero then
       begin
-        if LU.GetIsZero then
+        if LU.IsZero then
           Exit(Twice());
         Exit(LCurve.Infinity);
       end;
@@ -1134,9 +1127,9 @@ begin
         LR := LS1b.Subtract(LS2b);
 
         // Check if b == this or b == -this
-        if LH.GetIsZero then
+        if LH.IsZero then
         begin
-          if LR.GetIsZero then
+          if LR.IsZero then
             Exit(Twice());
           Exit(LCurve.Infinity);
         end;
@@ -1187,11 +1180,11 @@ var
   LY1Squared, LT2, La4, La4Neg, LM: IECFieldElement;
   LZ1Squared, LZ1Pow4: IECFieldElement;
 begin
-  if GetIsInfinity then
+  if IsInfinity then
     Exit(Self as IECPoint);
 
   Y1 := RawYCoord;
-  if Y1.GetIsZero then
+  if Y1.IsZero then
     Exit(FCurve.Infinity);
 
   LCurve := FCurve;
@@ -1215,7 +1208,7 @@ begin
 
       // TODO Optimize for small negative a4 and -3
       LW := LCurve.A;
-      if (not LW.GetIsZero) and (not LZ1IsOne) then
+      if (not LW.IsZero) and (not LZ1IsOne) then
         LW := LW.Multiply(Z1.Square());
       LW := LW.Add(Three(X1.Square()));
 
@@ -1261,7 +1254,7 @@ begin
         begin
           LM := LM.Add(La4);
         end
-        else if not La4.GetIsZero then
+        else if not La4.IsZero then
         begin
           if LZ1IsOne then LZ1Squared := Z1 else LZ1Squared := Z1.Square();
           LZ1Pow4 := LZ1Squared.Square();
@@ -1302,13 +1295,13 @@ var
 begin
   if (Self as IECPoint) = AB then
     Exit(ThreeTimes());
-  if GetIsInfinity then
+  if IsInfinity then
     Exit(AB);
-  if AB.GetIsInfinity then
+  if AB.IsInfinity then
     Exit(Twice());
 
   Y1 := RawYCoord;
-  if Y1.GetIsZero then
+  if Y1.IsZero then
     Exit(AB);
 
   LCurve := FCurve;
@@ -1324,9 +1317,9 @@ begin
       Dx := X2.Subtract(X1);
       Dy := Y2.Subtract(Y1);
 
-      if Dx.GetIsZero then
+      if Dx.IsZero then
       begin
-        if Dy.GetIsZero then
+        if Dy.IsZero then
           Exit(ThreeTimes());
         Exit(Self as IECPoint);
       end;
@@ -1334,7 +1327,7 @@ begin
       LX := Dx.Square();
       LY := Dy.Square();
       Ld := LX.Multiply(Two(X1).Add(X2)).Subtract(LY);
-      if Ld.GetIsZero then
+      if Ld.IsZero then
         Exit(LCurve.Infinity);
 
       LBigD := Ld.Multiply(Dx);
@@ -1363,11 +1356,11 @@ var
   X1, Y1: IECFieldElement;
   L2Y1, LX, LZ, LY, Ld, LBigD, LI, LL1, LL2, X4, Y4: IECFieldElement;
 begin
-  if GetIsInfinity then
+  if IsInfinity then
     Exit(Self as IECPoint);
 
   Y1 := RawYCoord;
-  if Y1.GetIsZero then
+  if Y1.IsZero then
     Exit(Self as IECPoint);
 
   LCurve := FCurve;
@@ -1384,7 +1377,7 @@ begin
       LY := LZ.Square();
 
       Ld := Three(X1).Multiply(LX).Subtract(LY);
-      if Ld.GetIsZero then
+      if Ld.IsZero then
         Exit(LCurve.Infinity);
 
       LBigD := Ld.Multiply(L2Y1);
@@ -1418,7 +1411,7 @@ var
 begin
   if AE < 0 then
     raise EArgumentCryptoLibException.Create('cannot be negative');
-  if (AE = 0) or GetIsInfinity then
+  if (AE = 0) or IsInfinity then
     Exit(Self as IECPoint);
   if AE = 1 then
     Exit(Twice());
@@ -1426,7 +1419,7 @@ begin
   LCurve := FCurve;
 
   Y1 := RawYCoord;
-  if Y1.GetIsZero then
+  if Y1.IsZero then
     Exit(LCurve.Infinity);
 
   LCoord := GetCurveCoordinateSystem();
@@ -1461,7 +1454,7 @@ begin
 
   for I := 0 to AE - 1 do
   begin
-    if Y1.GetIsZero then
+    if Y1.IsZero then
       Exit(LCurve.Infinity);
 
     X1Squared := X1.Square();
@@ -1472,7 +1465,7 @@ begin
     L4T := L2Y1Squared.Square();
     L8T := Two(L4T);
 
-    if not W1.GetIsZero then
+    if not W1.IsZero then
     begin
       LM := LM.Add(W1);
       W1 := Two(L8T.Multiply(W1));
@@ -1517,7 +1510,7 @@ function TFpPoint.Negate: IECPoint;
 var
   LCoord: Int32;
 begin
-  if GetIsInfinity then
+  if IsInfinity then
     Exit(Self as IECPoint);
   LCoord := GetCurveCoordinateSystem();
   if LCoord <> TECCurveConstants.COORD_AFFINE then
@@ -1547,7 +1540,7 @@ begin
     Z := RawZCoords[0];
     LZIsOne := Z.IsOne;
 
-    if X.GetIsZero then
+    if X.IsZero then
     begin
       // NOTE: For x == 0, we expect the affine-y instead of the lambda-y
       Lhs := Y.Square();

+ 17 - 10
CryptoLib/src/Math/EC/Multiplier/ClpWNafUtilities.pas

@@ -522,7 +522,7 @@ var
 begin
   if AK.SignValue = 0 then
   begin
-    System.SetLength(Result, 0);
+    Result := nil;
     Exit;
   end;
   L3k := AK.ShiftLeft(1).Add(AK);
@@ -552,7 +552,7 @@ class function TWNafUtilities.GenerateWindowNaf(AWidth: Int32;
 var
   LPow2, LMask, LSign, LLength, LPos: Int32;
   LK: TBigInteger;
-  LDigit: Int32;
+  LDigit, LResultLength: Int32;
   LCarry: Boolean;
 begin
   if AWidth = 2 then
@@ -564,10 +564,12 @@ begin
     raise EArgumentCryptoLibException.Create('must be in the range [2, 8]');
   if AK.SignValue = 0 then
   begin
-    System.SetLength(Result, 0);
+    Result := nil;
     Exit;
   end;
-  System.SetLength(Result, AK.BitLength + 1);
+  LResultLength := AK.BitLength + 1;
+  System.SetLength(Result, LResultLength);
+  TArrayUtilities.Fill<Byte>(Result, 0, LResultLength, Byte(0));
   LPow2 := 1 shl AWidth;
   LMask := LPow2 - 1;
   LSign := TBitOperations.Asr32(LPow2, 1);
@@ -609,6 +611,7 @@ var
 begin
   LDigits := Math.Max(AG.BitLength, AH.BitLength) + 1;
   System.SetLength(LJsf, LDigits);
+  TArrayUtilities.Fill<Byte>(LJsf, 0, LDigits, Byte(0));
   LK0 := AG;
   LK1 := AH;
   LJ := 0;
@@ -683,18 +686,20 @@ class function TWNafUtilities.GenerateCompactNaf(const AK: TBigInteger): TCrypto
 var
   L3k, LDiff: TBigInteger;
   LBits, LHighBit, LLength, LZeroes, LI: Int32;
-  LDigit: Int32;
+  LDigit, LResultLength: Int32;
 begin
   if TBitOperations.Asr32(AK.BitLength, 16) <> 0 then
     raise EArgumentCryptoLibException.Create('must have bitlength < 2^16');
   if AK.SignValue = 0 then
   begin
-    System.SetLength(Result, 0);
+    Result := nil;
     Exit;
   end;
   L3k := AK.ShiftLeft(1).Add(AK);
   LBits := L3k.BitLength;
-  System.SetLength(Result, TBitOperations.Asr32(LBits, 1));
+  LResultLength := TBitOperations.Asr32(LBits, 1);
+  System.SetLength(Result, LResultLength);
+  TArrayUtilities.Fill<Int32>(Result, 0, LResultLength, Int32(0));
   LDiff := L3k.&Xor(AK);
   LHighBit := LBits - 1;
   LLength := 0;
@@ -726,7 +731,7 @@ end;
 class function TWNafUtilities.GenerateCompactWindowNaf(AWidth: Int32;
   const AK: TBigInteger): TCryptoLibInt32Array;
 var
-  LPow2, LMask, LSign, LLength, LPos, LDigit, LZeroes: Int32;
+  LPow2, LMask, LSign, LLength, LPos, LDigit, LZeroes, LResultLength: Int32;
   LK: TBigInteger;
   LCarry: Boolean;
 begin
@@ -741,10 +746,12 @@ begin
     raise EArgumentCryptoLibException.Create('must have bitlength < 2^16');
   if AK.SignValue = 0 then
   begin
-    System.SetLength(Result, 0);
+    Result := nil;
     Exit;
   end;
-  System.SetLength(Result, AK.BitLength div AWidth + 1);
+  LResultLength := AK.BitLength div AWidth + 1;
+  System.SetLength(Result, LResultLength);
+  TArrayUtilities.Fill<Int32>(Result, 0, LResultLength, Int32(0));
   LPow2 := 1 shl AWidth;
   LMask := LPow2 - 1;
   LSign := TBitOperations.Asr32(LPow2, 1);