Browse Source

Accept only properly-sized BigInteger (no auto-reduction)

- minor refactoring
Ugochukwu Mmaduekwe 6 years ago
parent
commit
ad0422b9fe

+ 4 - 4
CryptoLib/src/Interfaces/ClpIEndoPreCompInfo.pas

@@ -29,11 +29,11 @@ type
   IEndoPreCompInfo = interface(IPreCompInfo)
   IEndoPreCompInfo = interface(IPreCompInfo)
     ['{84C79A80-8162-4079-8146-AA1D46A739ED}']
     ['{84C79A80-8162-4079-8146-AA1D46A739ED}']
 
 
-    function GetECEndomorphism: IECEndomorphism;
-    procedure SetECEndomorphism(const value: IECEndomorphism);
+    function GetEndomorphism: IECEndomorphism;
+    procedure SetEndomorphism(const value: IECEndomorphism);
 
 
-    property Endomorphism: IECEndomorphism read GetECEndomorphism
-      write SetECEndomorphism;
+    property Endomorphism: IECEndomorphism read GetEndomorphism
+      write SetEndomorphism;
 
 
     function GetMappedPoint: IECPoint;
     function GetMappedPoint: IECPoint;
     procedure SetMappedPoint(const value: IECPoint);
     procedure SetMappedPoint(const value: IECPoint);

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

@@ -421,12 +421,8 @@ end;
 
 
 class function TSecT283Field.FromBigInteger(const x: TBigInteger)
 class function TSecT283Field.FromBigInteger(const x: TBigInteger)
   : TCryptoLibUInt64Array;
   : TCryptoLibUInt64Array;
-var
-  z: TCryptoLibUInt64Array;
 begin
 begin
-  z := TNat320.FromBigInteger64(x);
-  Reduce37(z, 0);
-  result := z;
+  result := TNat.FromBigInteger64(283, x);
 end;
 end;
 
 
 class procedure TSecT283Field.Multiply(const x, y, z: TCryptoLibUInt64Array);
 class procedure TSecT283Field.Multiply(const x, y, z: TCryptoLibUInt64Array);

+ 6 - 6
CryptoLib/src/Math/EC/Endo/ClpEndoPreCompInfo.pas

@@ -35,16 +35,16 @@ type
     FEndomorphism: IECEndomorphism;
     FEndomorphism: IECEndomorphism;
     FMappedPoint: IECPoint;
     FMappedPoint: IECPoint;
 
 
-    function GetECEndomorphism: IECEndomorphism; inline;
-    procedure SetECEndomorphism(const value: IECEndomorphism); inline;
+    function GetEndomorphism: IECEndomorphism; inline;
+    procedure SetEndomorphism(const value: IECEndomorphism); inline;
 
 
     function GetMappedPoint: IECPoint; inline;
     function GetMappedPoint: IECPoint; inline;
     procedure SetMappedPoint(const value: IECPoint); inline;
     procedure SetMappedPoint(const value: IECPoint); inline;
 
 
   public
   public
 
 
-    property Endomorphism: IECEndomorphism read GetECEndomorphism
-      write SetECEndomorphism;
+    property Endomorphism: IECEndomorphism read GetEndomorphism
+      write SetEndomorphism;
     property MappedPoint: IECPoint read GetMappedPoint write SetMappedPoint;
     property MappedPoint: IECPoint read GetMappedPoint write SetMappedPoint;
   end;
   end;
 
 
@@ -52,7 +52,7 @@ implementation
 
 
 { TEndoPreCompInfo }
 { TEndoPreCompInfo }
 
 
-function TEndoPreCompInfo.GetECEndomorphism: IECEndomorphism;
+function TEndoPreCompInfo.GetEndomorphism: IECEndomorphism;
 begin
 begin
   result := FEndomorphism;
   result := FEndomorphism;
 end;
 end;
@@ -62,7 +62,7 @@ begin
   result := FMappedPoint;
   result := FMappedPoint;
 end;
 end;
 
 
-procedure TEndoPreCompInfo.SetECEndomorphism(const value: IECEndomorphism);
+procedure TEndoPreCompInfo.SetEndomorphism(const value: IECEndomorphism);
 begin
 begin
   FEndomorphism := value;
   FEndomorphism := value;
 end;
 end;

+ 28 - 0
CryptoLib/src/Math/Raw/ClpNat.pas

@@ -161,6 +161,9 @@ type
     class function FromBigInteger(bits: Int32; const x: TBigInteger)
     class function FromBigInteger(bits: Int32; const x: TBigInteger)
       : TCryptoLibUInt32Array; static;
       : TCryptoLibUInt32Array; static;
 
 
+    class function FromBigInteger64(bits: Int32; const x: TBigInteger)
+      : TCryptoLibUInt64Array; static;
+
     class function GetBit(const x: TCryptoLibUInt32Array; bit: Int32)
     class function GetBit(const x: TCryptoLibUInt32Array; bit: Int32)
       : UInt32; static;
       : UInt32; static;
 
 
@@ -1208,6 +1211,31 @@ begin
   end;
   end;
 end;
 end;
 
 
+class function TNat.FromBigInteger64(bits: Int32; const x: TBigInteger)
+  : TCryptoLibUInt64Array;
+var
+  len, I: Int32;
+  z: TCryptoLibUInt64Array;
+  Lx: TBigInteger;
+begin
+  Lx := x;
+  if ((Lx.SignValue < 0) or (Lx.BitLength > bits)) then
+  begin
+    raise EArgumentCryptoLibException.Create('');
+  end;
+
+  len := (bits + 63) shr 6;
+  z := Create64(len);
+  I := 0;
+  while (Lx.SignValue <> 0) do
+  begin
+    z[I] := Lx.Int64Value;
+    System.Inc(I);
+    Lx := Lx.ShiftRight(64);
+  end;
+  Result := z;
+end;
+
 class function TNat.GetBit(const x: TCryptoLibUInt32Array; bit: Int32): UInt32;
 class function TNat.GetBit(const x: TCryptoLibUInt32Array; bit: Int32): UInt32;
 var
 var
   w, b: Int32;
   w, b: Int32;