Browse Source

refactorings in Ed25519

Ugochukwu Mmaduekwe 6 years ago
parent
commit
503efbd92a
1 changed files with 94 additions and 158 deletions
  1. 94 158
      CryptoLib/src/Math/EC/Rfc8032/ClpEd25519.pas

+ 94 - 158
CryptoLib/src/Math/EC/Rfc8032/ClpEd25519.pas

@@ -46,32 +46,7 @@ type
   strict private
 
   type
-    IPointAccum = interface(IInterface)
-      ['{8A257C67-674F-4F62-B937-4E83B46CAF36}']
-      function GetX: TCryptoLibInt32Array;
-      procedure SetX(const value: TCryptoLibInt32Array);
-      property X: TCryptoLibInt32Array read GetX write SetX;
-
-      function GetY: TCryptoLibInt32Array;
-      procedure SetY(const value: TCryptoLibInt32Array);
-      property Y: TCryptoLibInt32Array read GetY write SetY;
-
-      function GetZ: TCryptoLibInt32Array;
-      procedure SetZ(const value: TCryptoLibInt32Array);
-      property Z: TCryptoLibInt32Array read GetZ write SetZ;
-
-      function GetU: TCryptoLibInt32Array;
-      procedure SetU(const value: TCryptoLibInt32Array);
-      property U: TCryptoLibInt32Array read GetU write SetU;
-
-      function GetV: TCryptoLibInt32Array;
-      procedure SetV(const value: TCryptoLibInt32Array);
-      property V: TCryptoLibInt32Array read GetV write SetV;
-
-    end;
-
-  type
-    TPointAccum = class sealed(TInterfacedObject, IPointAccum)
+    TPointAccum = record
     private
       Fx, Fy, Fz, Fu, Fv: TCryptoLibInt32Array;
 
@@ -93,32 +68,11 @@ type
       property U: TCryptoLibInt32Array read GetU write SetU;
       property V: TCryptoLibInt32Array read GetV write SetV;
 
-      constructor Create();
+      class function CreatePointAccum(): TPointAccum; static;
     end;
 
   type
-    IPointExt = interface(IInterface)
-      ['{20C8CC66-D9E6-4626-B09D-BC500223F103}']
-      function GetX: TCryptoLibInt32Array;
-      procedure SetX(const value: TCryptoLibInt32Array);
-      property X: TCryptoLibInt32Array read GetX write SetX;
-
-      function GetY: TCryptoLibInt32Array;
-      procedure SetY(const value: TCryptoLibInt32Array);
-      property Y: TCryptoLibInt32Array read GetY write SetY;
-
-      function GetZ: TCryptoLibInt32Array;
-      procedure SetZ(const value: TCryptoLibInt32Array);
-      property Z: TCryptoLibInt32Array read GetZ write SetZ;
-
-      function GetT: TCryptoLibInt32Array;
-      procedure SetT(const value: TCryptoLibInt32Array);
-      property T: TCryptoLibInt32Array read GetT write SetT;
-
-    end;
-
-  type
-    TPointExt = class sealed(TInterfacedObject, IPointExt)
+    TPointExt = record
     private
       Fx, Fy, Fz, Ft: TCryptoLibInt32Array;
 
@@ -137,26 +91,11 @@ type
       property Z: TCryptoLibInt32Array read GetZ write SetZ;
       property T: TCryptoLibInt32Array read GetT write SetT;
 
-      constructor Create();
-    end;
-
-  type
-    IPointPrecomp = interface(IInterface)
-      ['{9EADAD66-FE44-4A1E-8458-3AE7D2AF14C2}']
-      function GetYpx_h: TCryptoLibInt32Array;
-      procedure SetYpx_h(const value: TCryptoLibInt32Array);
-      property Ypx_h: TCryptoLibInt32Array read GetYpx_h write SetYpx_h;
-      function GetYmx_h: TCryptoLibInt32Array;
-      procedure SetYmx_h(const value: TCryptoLibInt32Array);
-      property Ymx_h: TCryptoLibInt32Array read GetYmx_h write SetYmx_h;
-      function GetXyd: TCryptoLibInt32Array;
-      procedure SetXyd(const value: TCryptoLibInt32Array);
-      property Xyd: TCryptoLibInt32Array read GetXyd write SetXyd;
-
+      class function CreatePointExt(): TPointExt; static;
     end;
 
   type
-    TPointPrecomp = class sealed(TInterfacedObject, IPointPrecomp)
+    TPointPrecomp = record
     private
       Fypx_h, Fymx_h, Fxyd: TCryptoLibInt32Array;
 
@@ -172,7 +111,7 @@ type
       property Ymx_h: TCryptoLibInt32Array read GetYmx_h write SetYmx_h;
       property Xyd: TCryptoLibInt32Array read GetXyd write SetXyd;
 
-      constructor Create();
+      class function CreatePointPrecomp(): TPointPrecomp; static;
     end;
 
   const
@@ -204,7 +143,7 @@ type
     FB_x, FB_y, FC_d, FC_d2, FC_d4: TCryptoLibInt32Array;
     FPrecompLock: TCriticalSection;
     // TODO[ed25519] Convert to PointPrecomp
-    FPrecompBaseTable: TCryptoLibGenericArray<IPointExt>;
+    FPrecompBaseTable: TCryptoLibGenericArray<TPointExt>;
     FPrecompBase: TCryptoLibInt32Array;
 
     class function CalculateS(const r, k, s: TCryptoLibByteArray)
@@ -230,7 +169,7 @@ type
       static; inline;
 
     class function DecodePointVar(const p: TCryptoLibByteArray; pOff: Int32;
-      ANegate: Boolean; const r: IPointExt): Boolean; static;
+      ANegate: Boolean; var r: TPointExt): Boolean; static;
 
     class procedure DecodeScalar(const k: TCryptoLibByteArray; kOff: Int32;
       const n: TCryptoLibUInt32Array); static; inline;
@@ -247,7 +186,7 @@ type
     class procedure Encode56(n: UInt64; const bs: TCryptoLibByteArray;
       off: Int32); static; inline;
 
-    class procedure EncodePoint(const p: IPointAccum;
+    class procedure EncodePoint(var p: TPointAccum;
       const r: TCryptoLibByteArray; rOff: Int32); static;
 
     class function GetWnaf(const n: TCryptoLibUInt32Array; width: Int32)
@@ -275,39 +214,37 @@ type
       const ctx: TCryptoLibCustomByteArrayBuffer; phflag: Byte;
       const m: TCryptoLibByteArray; mOff, mLen: Int32): Boolean;
 
-    class procedure PointAddVar(negate: Boolean; const p: IPointExt;
-      const r: IPointAccum); overload; static;
+    class procedure PointAddVar(negate: Boolean; var p: TPointExt;
+      var r: TPointAccum); overload; static;
 
-    class procedure PointAddVar(negate: Boolean; const p, q, r: IPointExt);
+    class procedure PointAddVar(negate: Boolean; var p, q, r: TPointExt);
       overload; static;
 
-    class procedure PointAddPrecomp(const p: IPointPrecomp;
-      const r: IPointAccum); overload; static;
+    class procedure PointAddPrecomp(var p: TPointPrecomp; var r: TPointAccum);
+      overload; static;
 
-    class function PointCopy(const p: IPointAccum): IPointExt; overload;
+    class function PointCopy(var p: TPointAccum): TPointExt; overload;
       static; inline;
 
-    class function PointCopy(const p: IPointExt): IPointExt; overload;
+    class function PointCopy(var p: TPointExt): TPointExt; overload;
       static; inline;
 
-    class procedure PointDouble(const r: IPointAccum); static;
+    class procedure PointDouble(var r: TPointAccum); static;
 
-    class procedure PointExtendXY(const p: IPointAccum); overload;
-      static; inline;
+    class procedure PointExtendXY(var p: TPointAccum); overload; static; inline;
 
-    class procedure PointExtendXY(const p: IPointExt); overload; static; inline;
+    class procedure PointExtendXY(var p: TPointExt); overload; static; inline;
 
     class procedure PointLookup(block, index: Int32;
-      const p: IPointPrecomp); static;
+      var p: TPointPrecomp); static;
 
-    class function PointPrecompVar(const p: IPointExt; count: Int32)
-      : TCryptoLibGenericArray<IPointExt>; static;
+    class function PointPrecompVar(var p: TPointExt; count: Int32)
+      : TCryptoLibGenericArray<TPointExt>; static;
 
-    class procedure PointSetNeutral(const p: IPointAccum); overload;
+    class procedure PointSetNeutral(var p: TPointAccum); overload;
       static; inline;
 
-    class procedure PointSetNeutral(const p: IPointExt); overload;
-      static; inline;
+    class procedure PointSetNeutral(var p: TPointExt); overload; static; inline;
 
     class procedure PruneScalar(const n: TCryptoLibByteArray; nOff: Int32;
       const r: TCryptoLibByteArray); static; inline;
@@ -316,13 +253,13 @@ type
       : TCryptoLibByteArray; static;
 
     class procedure ScalarMultBase(const k: TCryptoLibByteArray;
-      const r: IPointAccum); static;
+      var r: TPointAccum); static;
 
     class procedure ScalarMultBaseEncoded(const k, r: TCryptoLibByteArray;
       rOff: Int32); static; inline;
 
     class procedure ScalarMultStraussVar(const nb, np: TCryptoLibUInt32Array;
-      const p: IPointExt; const r: IPointAccum); static;
+      var p: TPointExt; var r: TPointAccum); static;
 
     class function ValidateDigestOutputSize(const ADigest: IDigest)
       : TCryptoLibByteArray; static; inline;
@@ -422,13 +359,13 @@ implementation
 
 { TEd25519.TPointExt }
 
-constructor TEd25519.TPointExt.Create;
+class function TEd25519.TPointExt.CreatePointExt(): TPointExt;
 begin
-  Inherited Create();
-  Fx := TX25519Field.Create();
-  Fy := TX25519Field.Create();
-  Fz := TX25519Field.Create();
-  Ft := TX25519Field.Create();
+  result := Default (TPointExt);
+  result.Fx := TX25519Field.Create();
+  result.Fy := TX25519Field.Create();
+  result.Fz := TX25519Field.Create();
+  result.Ft := TX25519Field.Create();
 end;
 
 function TEd25519.TPointExt.GetT: TCryptoLibInt32Array;
@@ -473,14 +410,14 @@ end;
 
 { TEd25519.TPointAccum }
 
-constructor TEd25519.TPointAccum.Create;
+class function TEd25519.TPointAccum.CreatePointAccum(): TPointAccum;
 begin
-  Inherited Create();
-  Fx := TX25519Field.Create();
-  Fy := TX25519Field.Create();
-  Fz := TX25519Field.Create();
-  Fu := TX25519Field.Create();
-  Fv := TX25519Field.Create();
+  result := Default (TPointAccum);
+  result.Fx := TX25519Field.Create();
+  result.Fy := TX25519Field.Create();
+  result.Fz := TX25519Field.Create();
+  result.Fu := TX25519Field.Create();
+  result.Fv := TX25519Field.Create();
 end;
 
 function TEd25519.TPointAccum.GetU: TCryptoLibInt32Array;
@@ -535,12 +472,12 @@ end;
 
 { TEd25519.TPointPrecomp }
 
-constructor TEd25519.TPointPrecomp.Create;
+class function TEd25519.TPointPrecomp.CreatePointPrecomp(): TPointPrecomp;
 begin
-  Inherited Create();
-  Fypx_h := TX25519Field.Create();
-  Fymx_h := TX25519Field.Create();
-  Fxyd := TX25519Field.Create();
+  result := Default (TPointPrecomp);
+  result.Fypx_h := TX25519Field.Create();
+  result.Fymx_h := TX25519Field.Create();
+  result.Fxyd := TX25519Field.Create();
 end;
 
 function TEd25519.TPointPrecomp.GetXyd: TCryptoLibInt32Array;
@@ -754,21 +691,21 @@ begin
   result := n;
 end;
 
-class procedure TEd25519.PointExtendXY(const p: IPointAccum);
+class procedure TEd25519.PointExtendXY(var p: TPointAccum);
 begin
   TX25519Field.One(p.Z);
   TX25519Field.Copy(p.X, 0, p.U, 0);
   TX25519Field.Copy(p.Y, 0, p.V, 0);
 end;
 
-class procedure TEd25519.PointExtendXY(const p: IPointExt);
+class procedure TEd25519.PointExtendXY(var p: TPointExt);
 begin
   TX25519Field.One(p.Z);
   TX25519Field.Mul(p.X, p.Y, p.T);
 end;
 
 class function TEd25519.DecodePointVar(const p: TCryptoLibByteArray;
-  pOff: Int32; ANegate: Boolean; const r: IPointExt): Boolean;
+  pOff: Int32; ANegate: Boolean; var r: TPointExt): Boolean;
 var
   py: TCryptoLibByteArray;
   U, V: TCryptoLibInt32Array;
@@ -836,7 +773,7 @@ begin
   end;
 end;
 
-class procedure TEd25519.EncodePoint(const p: IPointAccum;
+class procedure TEd25519.EncodePoint(var p: TPointAccum;
   const r: TCryptoLibByteArray; rOff: Int32);
 var
   X, Y: TCryptoLibInt32Array;
@@ -874,9 +811,9 @@ end;
 class procedure TEd25519.ScalarMultBaseEncoded(const k, r: TCryptoLibByteArray;
   rOff: Int32);
 var
-  p: IPointAccum;
+  p: TPointAccum;
 begin
-  p := TPointAccum.Create();
+  p := TPointAccum.CreatePointAccum();
   ScalarMultBase(k, p);
   EncodePoint(p, r, rOff);
 end;
@@ -1062,8 +999,8 @@ function TEd25519.ImplVerify(const sig: TCryptoLibByteArray; sigOff: Int32;
 var
   r, s, h, k, check: TCryptoLibByteArray;
   nS, nA: TCryptoLibUInt32Array;
-  pA: IPointExt;
-  pR: IPointAccum;
+  pA: TPointExt;
+  pR: TPointAccum;
   d: IDigest;
 begin
   if (not CheckContextVar(ctx, phflag)) then
@@ -1087,7 +1024,7 @@ begin
     Exit;
   end;
 
-  pA := TPointExt.Create();
+  pA := TPointExt.CreatePointExt();
   if (not DecodePointVar(pk, pkOff, true, pA)) then
   begin
     result := false;
@@ -1112,7 +1049,7 @@ begin
 
   DecodeScalar(k, 0, nA);
 
-  pR := TPointAccum.Create();
+  pR := TPointAccum.CreatePointAccum();
   ScalarMultStraussVar(nS, nA, pA, pR);
 
   System.SetLength(check, PointBytes);
@@ -1122,8 +1059,8 @@ begin
   result := TArrayUtils.ConstantTimeAreEqual(check, r);
 end;
 
-class procedure TEd25519.PointAddPrecomp(const p: IPointPrecomp;
-  const r: IPointAccum);
+class procedure TEd25519.PointAddPrecomp(var p: TPointPrecomp;
+  var r: TPointAccum);
 var
   bigA, bigB, bigC, bigD, bigE, bigF, bigG: TCryptoLibInt32Array;
 begin
@@ -1148,7 +1085,7 @@ begin
   TX25519Field.Mul(bigE, bigF, r.Z);
 end;
 
-class procedure TEd25519.PointAddVar(negate: Boolean; const p, q, r: IPointExt);
+class procedure TEd25519.PointAddVar(negate: Boolean; var p, q, r: TPointExt);
 var
   bigA, bigB, bigC, bigD, bigE, bigF, bigG, bigH, c, d, f,
     g: TCryptoLibInt32Array;
@@ -1194,8 +1131,8 @@ begin
   TX25519Field.Mul(bigE, bigH, r.T);
 end;
 
-class procedure TEd25519.PointAddVar(negate: Boolean; const p: IPointExt;
-  const r: IPointAccum);
+class procedure TEd25519.PointAddVar(negate: Boolean; var p: TPointExt;
+  var r: TPointAccum);
 var
   bigA, bigB, bigC, bigD, bigE, bigF, bigG, bigH, c, d, f,
     g: TCryptoLibInt32Array;
@@ -1241,11 +1178,11 @@ begin
   TX25519Field.Mul(bigF, bigG, r.Z);
 end;
 
-class function TEd25519.PointCopy(const p: IPointAccum): IPointExt;
+class function TEd25519.PointCopy(var p: TPointAccum): TPointExt;
 var
-  r: IPointExt;
+  r: TPointExt;
 begin
-  r := TPointExt.Create();
+  r := TPointExt.CreatePointExt();
   TX25519Field.Copy(p.X, 0, r.X, 0);
   TX25519Field.Copy(p.Y, 0, r.Y, 0);
   TX25519Field.Copy(p.Z, 0, r.Z, 0);
@@ -1253,11 +1190,11 @@ begin
   result := r;
 end;
 
-class function TEd25519.PointCopy(const p: IPointExt): IPointExt;
+class function TEd25519.PointCopy(var p: TPointExt): TPointExt;
 var
-  r: IPointExt;
+  r: TPointExt;
 begin
-  r := TPointExt.Create();
+  r := TPointExt.CreatePointExt();
   TX25519Field.Copy(p.X, 0, r.X, 0);
   TX25519Field.Copy(p.Y, 0, r.Y, 0);
   TX25519Field.Copy(p.Z, 0, r.Z, 0);
@@ -1265,7 +1202,7 @@ begin
   result := r;
 end;
 
-class procedure TEd25519.PointDouble(const r: IPointAccum);
+class procedure TEd25519.PointDouble(var r: TPointAccum);
 var
   bigA, bigB, bigC, bigD, bigE, bigF, bigG: TCryptoLibInt32Array;
 begin
@@ -1292,8 +1229,7 @@ begin
   TX25519Field.Mul(bigE, bigF, r.Z);
 end;
 
-class procedure TEd25519.PointLookup(block, index: Int32;
-  const p: IPointPrecomp);
+class procedure TEd25519.PointLookup(block, index: Int32; var p: TPointPrecomp);
 var
   off, i, mask: Int32;
 begin
@@ -1315,17 +1251,17 @@ begin
   end;
 end;
 
-class function TEd25519.PointPrecompVar(const p: IPointExt; count: Int32)
-  : TCryptoLibGenericArray<IPointExt>;
+class function TEd25519.PointPrecompVar(var p: TPointExt; count: Int32)
+  : TCryptoLibGenericArray<TPointExt>;
 var
-  d: IPointExt;
-  table: TCryptoLibGenericArray<IPointExt>;
+  d: TPointExt;
+  table: TCryptoLibGenericArray<TPointExt>;
   i: Int32;
 begin
 {$IFDEF DEBUG}
   System.Assert(count > 0);
 {$ENDIF DEBUG}
-  d := TPointExt.Create();
+  d := TPointExt.CreatePointExt();
   PointAddVar(false, p, p, d);
 
   System.SetLength(table, count);
@@ -1333,13 +1269,13 @@ begin
   table[0] := PointCopy(p);
   for i := 1 to System.Pred(count) do
   begin
-    table[i] := TPointExt.Create() as IPointExt;
+    table[i] := TPointExt.CreatePointExt();
     PointAddVar(false, table[i - 1], d, table[i]);
   end;
   result := table;
 end;
 
-class procedure TEd25519.PointSetNeutral(const p: IPointAccum);
+class procedure TEd25519.PointSetNeutral(var p: TPointAccum);
 begin
   TX25519Field.Zero(p.X);
   TX25519Field.One(p.Y);
@@ -1348,7 +1284,7 @@ begin
   TX25519Field.One(p.V);
 end;
 
-class procedure TEd25519.PointSetNeutral(const p: IPointExt);
+class procedure TEd25519.PointSetNeutral(var p: TPointExt);
 begin
   TX25519Field.Zero(p.X);
   TX25519Field.One(p.Y);
@@ -1358,15 +1294,15 @@ end;
 
 class procedure TEd25519.Precompute;
 var
-  bigB: IPointExt;
-  p: IPointAccum;
+  bigB: TPointExt;
+  p: TPointAccum;
   X, Y: TCryptoLibInt32Array;
   off, b, T, s, k, Size, j, i: Int32;
-  // ds, points: TCryptoLibGenericArray<IPointExt>;
-  ds: array [0 .. (PrecompTeeth - 1)] of IPointExt;
-  points: array [0 .. (PrecompPoints - 1)] of IPointExt;
-  sum, q: IPointExt;
-  r: IPointPrecomp;
+  // ds, points: TCryptoLibGenericArray<TPointExt>;
+  ds: array [0 .. (PrecompTeeth - 1)] of TPointExt;
+  points: array [0 .. (PrecompPoints - 1)] of TPointExt;
+  sum, q: TPointExt;
+  r: TPointPrecomp;
 begin
   FPrecompLock.Acquire;
   try
@@ -1377,14 +1313,14 @@ begin
     end;
 
     // Precomputed table for the base point in verification ladder
-    bigB := TPointExt.Create();
+    bigB := TPointExt.CreatePointExt();
     TX25519Field.Copy(FB_x, 0, bigB.X, 0);
     TX25519Field.Copy(FB_y, 0, bigB.Y, 0);
     PointExtendXY(bigB);
 
     FPrecompBaseTable := PointPrecompVar(bigB, 1 shl (WnafWidthBase - 2));
 
-    p := TPointAccum.Create();
+    p := TPointAccum.CreatePointAccum();
     TX25519Field.Copy(FB_x, 0, p.X, 0);
     TX25519Field.Copy(FB_y, 0, p.Y, 0);
     PointExtendXY(p);
@@ -1397,7 +1333,7 @@ begin
     begin
       // System.SetLength(ds, PrecompTeeth); // **
 
-      sum := TPointExt.Create() as IPointExt;
+      sum := TPointExt.CreatePointExt();
       PointSetNeutral(sum);
 
       for T := 0 to System.Pred(PrecompTeeth) do
@@ -1431,7 +1367,7 @@ begin
         j := 0;
         while j < Size do
         begin
-          points[k] := TPointExt.Create() as IPointExt;
+          points[k] := TPointExt.CreatePointExt();
           PointAddVar(false, points[k - Size], ds[T], points[k]);
           System.Inc(k);
           System.Inc(j);
@@ -1454,7 +1390,7 @@ begin
         TX25519Field.Mul(q.X, Y, X);
         TX25519Field.Mul(q.Y, Y, Y);
 
-        r := TPointPrecomp.Create();
+        r := TPointPrecomp.CreatePointPrecomp();
         TX25519Field.Apm(Y, X, r.Ypx_h, r.Ymx_h);
         TX25519Field.Mul(X, Y, r.Xyd);
         TX25519Field.Mul(r.Xyd, FC_d4, r.Xyd);
@@ -1645,12 +1581,12 @@ begin
 end;
 
 class procedure TEd25519.ScalarMultBase(const k: TCryptoLibByteArray;
-  const r: IPointAccum);
+  var r: TPointAccum);
 var
   n: TCryptoLibUInt32Array;
   w, c1, c2: UInt32;
   i, cOff, b, LSign, abs: Int32;
-  p: IPointPrecomp;
+  p: TPointPrecomp;
 begin
   Precompute();
 
@@ -1673,7 +1609,7 @@ begin
     n[i] := TInterleave.Shuffle2(n[i]);
   end;
 
-  p := TPointPrecomp.Create();
+  p := TPointPrecomp.CreatePointPrecomp();
 
   cOff := (PrecompSpacing - 1) * PrecompTeeth;
   while true do
@@ -1710,23 +1646,23 @@ class procedure TEd25519.ScalarMultBaseYZ(const k: TCryptoLibByteArray;
   kOff: Int32; const Y, Z: TCryptoLibInt32Array);
 var
   n: TCryptoLibByteArray;
-  p: IPointAccum;
+  p: TPointAccum;
 begin
   System.SetLength(n, ScalarBytes);
   PruneScalar(k, kOff, n);
 
-  p := TPointAccum.Create();
+  p := TPointAccum.CreatePointAccum();
   ScalarMultBase(n, p);
   TX25519Field.Copy(p.Y, 0, Y, 0);
   TX25519Field.Copy(p.Z, 0, Z, 0);
 end;
 
 class procedure TEd25519.ScalarMultStraussVar(const nb,
-  np: TCryptoLibUInt32Array; const p: IPointExt; const r: IPointAccum);
+  np: TCryptoLibUInt32Array; var p: TPointExt; var r: TPointAccum);
 var
   width, bit, wb, wp, LSign, index: Int32;
   ws_b, ws_p: TCryptoLibShortIntArray;
-  tp: TCryptoLibGenericArray<IPointExt>;
+  tp: TCryptoLibGenericArray<TPointExt>;
 begin
   Precompute();