Browse Source

GLV Type B cleanup

Ugochukwu Mmaduekwe 6 years ago
parent
commit
04ebc1bd8d

+ 12 - 8
CryptoLib/src/Interfaces/ClpIGlvTypeBParameters.pas

@@ -30,20 +30,24 @@ type
   IGlvTypeBParameters = interface(IInterface)
     ['{089AC2AB-15A1-47F5-BED0-C09EA77BECB9}']
 
-    function GetBeta: TBigInteger;
-    function GetBits: Int32;
     function GetG1: TBigInteger;
     function GetG2: TBigInteger;
+    function GetV1A: TBigInteger;
+    function GetV1B: TBigInteger;
+    function GetV2A: TBigInteger;
+    function GetV2B: TBigInteger;
     function GetLambda: TBigInteger;
-    function GetV1: TCryptoLibGenericArray<TBigInteger>;
-    function GetV2: TCryptoLibGenericArray<TBigInteger>;
+    function GetBeta: TBigInteger;
+    function GetBits: Int32;
 
-    property beta: TBigInteger read GetBeta;
-    property lambda: TBigInteger read GetLambda;
-    property v1: TCryptoLibGenericArray<TBigInteger> read GetV1;
-    property v2: TCryptoLibGenericArray<TBigInteger> read GetV2;
     property g1: TBigInteger read GetG1;
     property g2: TBigInteger read GetG2;
+    property V1A: TBigInteger read GetV1A;
+    property V1B: TBigInteger read GetV1B;
+    property V2A: TBigInteger read GetV2A;
+    property V2B: TBigInteger read GetV2B;
+    property lambda: TBigInteger read GetLambda;
+    property beta: TBigInteger read GetBeta;
     property bits: Int32 read GetBits;
 
   end;

+ 18 - 15
CryptoLib/src/Math/EC/Endo/ClpGlvTypeBEndomorphism.pas

@@ -40,9 +40,8 @@ type
 
   strict protected
   var
-    Fm_parameters: IGlvTypeBParameters;
-    Fm_pointMap: IECPointMap;
-    Fm_curve: IECCurve;
+    FParameters: IGlvTypeBParameters;
+    FPointMap: IECPointMap;
 
     function CalculateB(const k, g: TBigInteger; t: Int32)
       : TBigInteger; virtual;
@@ -76,6 +75,7 @@ begin
   begin
     b := b.Add(TBigInteger.One);
   end;
+
   if negative then
   begin
     Result := b.Negate();
@@ -90,9 +90,13 @@ constructor TGlvTypeBEndomorphism.Create(const curve: IECCurve;
   const parameters: IGlvTypeBParameters);
 begin
   Inherited Create();
-  Fm_curve := curve;
-  Fm_parameters := parameters;
-  Fm_pointMap := TScaleXPointMap.Create(curve.FromBigInteger(parameters.Beta));
+  (*
+    * NOTE: 'curve' MUST only be used to create a suitable ECFieldElement. Due to the way
+    * ECCurve configuration works, 'curve' will not be the actual instance of ECCurve that the
+    * endomorphism is being used with.
+  *)
+  FParameters := parameters;
+  FPointMap := TScaleXPointMap.Create(curve.FromBigInteger(parameters.Beta));
 end;
 
 function TGlvTypeBEndomorphism.DecomposeScalar(const k: TBigInteger)
@@ -100,16 +104,15 @@ function TGlvTypeBEndomorphism.DecomposeScalar(const k: TBigInteger)
 var
   bits: Int32;
   b1, b2, a, b: TBigInteger;
-  v1, v2: TCryptoLibGenericArray<TBigInteger>;
+  p: IGlvTypeBParameters;
 begin
-  bits := Fm_parameters.bits;
-  b1 := CalculateB(k, Fm_parameters.G1, bits);
-  b2 := CalculateB(k, Fm_parameters.G2, bits);
+  bits := FParameters.bits;
+  b1 := CalculateB(k, FParameters.G1, bits);
+  b2 := CalculateB(k, FParameters.G2, bits);
 
-  v1 := Fm_parameters.v1;
-  v2 := Fm_parameters.v2;
-  a := k.Subtract((b1.Multiply(v1[0])).Add(b2.Multiply(v2[0])));
-  b := (b1.Multiply(v1[1])).Add(b2.Multiply(v2[1])).Negate();
+  p := FParameters;
+  a := k.subtract((b1.Multiply(p.V1A)).Add(b2.Multiply(p.V2A)));
+  b := (b1.Multiply(p.V1B)).Add(b2.Multiply(p.V2B)).Negate();
 
   Result := TCryptoLibGenericArray<TBigInteger>.Create(a, b);
 end;
@@ -126,7 +129,7 @@ end;
 
 function TGlvTypeBEndomorphism.GetPointMap: IECPointMap;
 begin
-  Result := Fm_pointMap;
+  Result := FPointMap;
 end;
 
 end.

+ 67 - 33
CryptoLib/src/Math/EC/Endo/ClpGlvTypeBParameters.pas

@@ -26,22 +26,29 @@ uses
   ClpIGlvTypeBParameters,
   ClpCryptoLibTypes;
 
+resourcestring
+  SInvalidParameters = '"%s" must consist of exactly 2 (initialized) values';
+
 type
   TGlvTypeBParameters = class sealed(TInterfacedObject, IGlvTypeBParameters)
 
   strict private
-    function GetBeta: TBigInteger; inline;
-    function GetBits: Int32; inline;
     function GetG1: TBigInteger; inline;
     function GetG2: TBigInteger; inline;
+    function GetV1A: TBigInteger; inline;
+    function GetV1B: TBigInteger; inline;
+    function GetV2A: TBigInteger; inline;
+    function GetV2B: TBigInteger; inline;
     function GetLambda: TBigInteger; inline;
-    function GetV1: TCryptoLibGenericArray<TBigInteger>; inline;
-    function GetV2: TCryptoLibGenericArray<TBigInteger>; inline;
+    function GetBeta: TBigInteger; inline;
+    function GetBits: Int32; inline;
+
+    class procedure CheckVector(const v: TCryptoLibGenericArray<TBigInteger>;
+      const name: String); static;
+
   strict protected
-    Fm_beta, Fm_lambda: TBigInteger;
-    Fm_v1, Fm_v2: TCryptoLibGenericArray<TBigInteger>;
-    Fm_g1, Fm_g2: TBigInteger;
-    Fm_bits: Int32;
+    Fbeta, Flambda, Fg1, Fg2, Fv1A, Fv1B, Fv2A, Fv2B: TBigInteger;
+    Fbits: Int32;
 
   public
     constructor Create(const beta, lambda: TBigInteger;
@@ -50,12 +57,14 @@ type
 
     destructor Destroy; override;
 
-    property beta: TBigInteger read GetBeta;
-    property lambda: TBigInteger read GetLambda;
-    property v1: TCryptoLibGenericArray<TBigInteger> read GetV1;
-    property v2: TCryptoLibGenericArray<TBigInteger> read GetV2;
     property g1: TBigInteger read GetG1;
     property g2: TBigInteger read GetG2;
+    property V1A: TBigInteger read GetV1A;
+    property V1B: TBigInteger read GetV1B;
+    property V2A: TBigInteger read GetV2A;
+    property V2B: TBigInteger read GetV2B;
+    property lambda: TBigInteger read GetLambda;
+    property beta: TBigInteger read GetBeta;
     property bits: Int32 read GetBits;
 
   end;
@@ -64,17 +73,32 @@ implementation
 
 { TGlvTypeBParameters }
 
+class procedure TGlvTypeBParameters.CheckVector
+  (const v: TCryptoLibGenericArray<TBigInteger>; const name: String);
+begin
+  if ((v = Nil) or (System.length(v) <> 2) or (not v[0].IsInitialized) or
+    (not v[1].IsInitialized)) then
+  begin
+    raise EArgumentCryptoLibException.CreateResFmt(@SInvalidParameters, [name]);
+  end;
+end;
+
 constructor TGlvTypeBParameters.Create(const beta, lambda: TBigInteger;
   const v1, v2: TCryptoLibGenericArray<TBigInteger>; const g1, g2: TBigInteger;
   bits: Int32);
 begin
-  Fm_beta := beta;
-  Fm_lambda := lambda;
-  Fm_v1 := v1;
-  Fm_v2 := v2;
-  Fm_g1 := g1;
-  Fm_g2 := g2;
-  Fm_bits := bits;
+  CheckVector(v1, 'v1');
+  CheckVector(v2, 'v2');
+
+  Fbeta := beta;
+  Flambda := lambda;
+  Fv1A := v1[0];
+  Fv1B := v1[1];
+  Fv2A := v2[0];
+  Fv2B := v2[1];
+  Fg1 := g1;
+  Fg2 := g2;
+  Fbits := bits;
 end;
 
 destructor TGlvTypeBParameters.Destroy;
@@ -82,39 +106,49 @@ begin
   inherited Destroy;
 end;
 
-function TGlvTypeBParameters.GetBeta: TBigInteger;
+function TGlvTypeBParameters.GetG1: TBigInteger;
 begin
-  Result := Fm_beta;
+  Result := Fg1;
 end;
 
-function TGlvTypeBParameters.GetBits: Int32;
+function TGlvTypeBParameters.GetG2: TBigInteger;
 begin
-  Result := Fm_bits;
+  Result := Fg2;
 end;
 
-function TGlvTypeBParameters.GetG1: TBigInteger;
+function TGlvTypeBParameters.GetV1A: TBigInteger;
 begin
-  Result := Fm_g1;
+  Result := Fv1A;
 end;
 
-function TGlvTypeBParameters.GetG2: TBigInteger;
+function TGlvTypeBParameters.GetV1B: TBigInteger;
 begin
-  Result := Fm_g2;
+  Result := Fv1B;
 end;
 
-function TGlvTypeBParameters.GetLambda: TBigInteger;
+function TGlvTypeBParameters.GetV2A: TBigInteger;
 begin
-  Result := Fm_lambda;
+  Result := Fv2A;
 end;
 
-function TGlvTypeBParameters.GetV1: TCryptoLibGenericArray<TBigInteger>;
+function TGlvTypeBParameters.GetV2B: TBigInteger;
 begin
-  Result := Fm_v1;
+  Result := Fv2B;
 end;
 
-function TGlvTypeBParameters.GetV2: TCryptoLibGenericArray<TBigInteger>;
+function TGlvTypeBParameters.GetBeta: TBigInteger;
+begin
+  Result := Fbeta;
+end;
+
+function TGlvTypeBParameters.GetBits: Int32;
+begin
+  Result := Fbits;
+end;
+
+function TGlvTypeBParameters.GetLambda: TBigInteger;
 begin
-  Result := Fm_v2;
+  Result := Flambda;
 end;
 
 end.