Browse Source

minor "constantification" of parameters.

Ugochukwu Mmaduekwe 7 years ago
parent
commit
05c2621121
1 changed files with 36 additions and 31 deletions
  1. 36 31
      CryptoLib/src/Math/ClpBigInteger.pas

+ 36 - 31
CryptoLib/src/Math/ClpBigInteger.pas

@@ -231,10 +231,10 @@ type
     class function ModPowBarrett(const b, e, m: TBigInteger)
     class function ModPowBarrett(const b, e, m: TBigInteger)
       : TBigInteger; static;
       : TBigInteger; static;
 
 
-    class function ReduceBarrett(x: TBigInteger; const m, mr, yu: TBigInteger)
-      : TBigInteger; static;
+    class function ReduceBarrett(const x: TBigInteger;
+      const m, mr, yu: TBigInteger): TBigInteger; static;
 
 
-    class function ModPowMonty(b: TBigInteger; const e, m: TBigInteger;
+    class function ModPowMonty(const b: TBigInteger; const e, m: TBigInteger;
       convert: Boolean): TBigInteger; static;
       convert: Boolean): TBigInteger; static;
 
 
     class function GetWindowList(const mag: TCryptoLibInt32Array;
     class function GetWindowList(const mag: TCryptoLibInt32Array;
@@ -311,8 +311,8 @@ type
     class procedure AppendZeroExtendedString(var sl: TStringList;
     class procedure AppendZeroExtendedString(var sl: TStringList;
       const s: String; minLength: Int32); static; inline;
       const s: String; minLength: Int32); static; inline;
 
 
-    class procedure ToString(sl: TStringList; radix: Int32;
-      moduli: TList<TBigInteger>; scale: Int32; const pos: TBigInteger);
+    class procedure ToString(var sl: TStringList; radix: Int32;
+      var moduli: TList<TBigInteger>; scale: Int32; const pos: TBigInteger);
       overload; static;
       overload; static;
 
 
     class function CreateUValueOf(value: UInt64): TBigInteger; static;
     class function CreateUValueOf(value: UInt64): TBigInteger; static;
@@ -393,7 +393,7 @@ type
     function Min(const value: TBigInteger): TBigInteger;
     function Min(const value: TBigInteger): TBigInteger;
     function &Mod(const m: TBigInteger): TBigInteger;
     function &Mod(const m: TBigInteger): TBigInteger;
     function ModInverse(const m: TBigInteger): TBigInteger;
     function ModInverse(const m: TBigInteger): TBigInteger;
-    function ModPow(e: TBigInteger; const m: TBigInteger): TBigInteger;
+    function ModPow(const e, m: TBigInteger): TBigInteger;
 
 
     function Multiply(const val: TBigInteger): TBigInteger; overload;
     function Multiply(const val: TBigInteger): TBigInteger; overload;
     function Square(): TBigInteger; overload;
     function Square(): TBigInteger; overload;
@@ -1344,8 +1344,8 @@ begin
   sl.Add(s);
   sl.Add(s);
 end;
 end;
 
 
-class procedure TBigInteger.ToString(sl: TStringList; radix: Int32;
-  moduli: TList<TBigInteger>; scale: Int32; const pos: TBigInteger);
+class procedure TBigInteger.ToString(var sl: TStringList; radix: Int32;
+  var moduli: TList<TBigInteger>; scale: Int32; const pos: TBigInteger);
 var
 var
   s: String;
   s: String;
   qr: TCryptoLibGenericArray<TBigInteger>;
   qr: TCryptoLibGenericArray<TBigInteger>;
@@ -2467,10 +2467,12 @@ begin
   Result := x;
   Result := x;
 end;
 end;
 
 
-function TBigInteger.ModPow(e: TBigInteger; const m: TBigInteger): TBigInteger;
+function TBigInteger.ModPow(const e, m: TBigInteger): TBigInteger;
 var
 var
   negExp: Boolean;
   negExp: Boolean;
+  le: TBigInteger;
 begin
 begin
+  le := e;
   if (m.Fsign < 1) then
   if (m.Fsign < 1) then
   begin
   begin
     raise EArithmeticCryptoLibException.CreateRes(@SModulusPositive);
     raise EArithmeticCryptoLibException.CreateRes(@SModulusPositive);
@@ -2482,7 +2484,7 @@ begin
     Exit;
     Exit;
   end;
   end;
 
 
-  if (e.Fsign = 0) then
+  if (le.Fsign = 0) then
   begin
   begin
     Result := One;
     Result := One;
     Exit;
     Exit;
@@ -2494,23 +2496,23 @@ begin
     Exit;
     Exit;
   end;
   end;
 
 
-  negExp := e.Fsign < 0;
+  negExp := le.Fsign < 0;
   if (negExp) then
   if (negExp) then
   begin
   begin
-    e := e.Negate();
+    le := le.Negate();
   end;
   end;
 
 
   Result := &Mod(m);
   Result := &Mod(m);
 
 
-  if (not e.Equals(One)) then
+  if (not le.Equals(One)) then
   begin
   begin
     if ((m.Fmagnitude[System.length(m.Fmagnitude) - 1] and 1) = 0) then
     if ((m.Fmagnitude[System.length(m.Fmagnitude) - 1] and 1) = 0) then
     begin
     begin
-      Result := ModPowBarrett(Result, e, m);
+      Result := ModPowBarrett(Result, le, m);
     end
     end
     else
     else
     begin
     begin
-      Result := ModPowMonty(Result, e, m, True);
+      Result := ModPowMonty(Result, le, m, True);
 
 
     end;
     end;
   end;
   end;
@@ -2608,8 +2610,8 @@ begin
   Result := y;
   Result := y;
 end;
 end;
 
 
-class function TBigInteger.ModPowMonty(b: TBigInteger; const e, m: TBigInteger;
-  convert: Boolean): TBigInteger;
+class function TBigInteger.ModPowMonty(const b: TBigInteger;
+  const e, m: TBigInteger; convert: Boolean): TBigInteger;
 var
 var
   n, powR, extraBits, expLength, numPowers, i, window, mult, lastZeroes,
   n, powR, extraBits, expLength, numPowers, i, window, mult, lastZeroes,
     windowPos, bits, j: Int32;
     windowPos, bits, j: Int32;
@@ -2617,7 +2619,9 @@ var
   mDash: UInt32;
   mDash: UInt32;
   yAccum, zVal, tmp, zSquared, windowList, yVal: TCryptoLibInt32Array;
   yAccum, zVal, tmp, zSquared, windowList, yVal: TCryptoLibInt32Array;
   oddPowers: TCryptoLibMatrixInt32Array;
   oddPowers: TCryptoLibMatrixInt32Array;
+  Lb: TBigInteger;
 begin
 begin
+  Lb := b;
   n := System.length(m.Fmagnitude);
   n := System.length(m.Fmagnitude);
   powR := 32 * n;
   powR := 32 * n;
   smallMontyModulus := (m.BitLength + 2) <= powR;
   smallMontyModulus := (m.BitLength + 2) <= powR;
@@ -2626,12 +2630,12 @@ begin
   // tmp = this * R mod m
   // tmp = this * R mod m
   if (convert) then
   if (convert) then
   begin
   begin
-    b := b.ShiftLeft(powR).Remainder(m);
+    Lb := Lb.ShiftLeft(powR).Remainder(m);
   end;
   end;
 
 
   System.SetLength(yAccum, n + 1);
   System.SetLength(yAccum, n + 1);
 
 
-  zVal := b.Fmagnitude;
+  zVal := Lb.Fmagnitude;
 {$IFDEF DEBUG}
 {$IFDEF DEBUG}
   System.Assert(System.length(zVal) <= n);
   System.Assert(System.length(zVal) <= n);
 {$ENDIF DEBUG}
 {$ENDIF DEBUG}
@@ -3659,17 +3663,18 @@ begin
   Result := RabinMillerTest(certainty, random, false);
   Result := RabinMillerTest(certainty, random, false);
 end;
 end;
 
 
-class function TBigInteger.ReduceBarrett(x: TBigInteger;
+class function TBigInteger.ReduceBarrett(const x: TBigInteger;
   const m, mr, yu: TBigInteger): TBigInteger;
   const m, mr, yu: TBigInteger): TBigInteger;
 var
 var
   xLen, mLen, k: Int32;
   xLen, mLen, k: Int32;
-  q1, q2, q3, r1, r2, r3: TBigInteger;
+  q1, q2, q3, r1, r2, r3, lx: TBigInteger;
 begin
 begin
-  xLen := x.BitLength;
+  lx := x;
+  xLen := lx.BitLength;
   mLen := m.BitLength;
   mLen := m.BitLength;
   if (xLen < mLen) then
   if (xLen < mLen) then
   begin
   begin
-    Result := x;
+    Result := lx;
     Exit;
     Exit;
   end;
   end;
 
 
@@ -3677,27 +3682,27 @@ begin
   begin
   begin
     k := System.length(m.Fmagnitude);
     k := System.length(m.Fmagnitude);
 
 
-    q1 := x.DivideWords(k - 1);
+    q1 := lx.DivideWords(k - 1);
     q2 := q1.Multiply(yu); // TODO Only need partial multiplication here
     q2 := q1.Multiply(yu); // TODO Only need partial multiplication here
     q3 := q2.DivideWords(k + 1);
     q3 := q2.DivideWords(k + 1);
 
 
-    r1 := x.RemainderWords(k + 1);
+    r1 := lx.RemainderWords(k + 1);
     r2 := q3.Multiply(m); // TODO Only need partial multiplication here
     r2 := q3.Multiply(m); // TODO Only need partial multiplication here
     r3 := r2.RemainderWords(k + 1);
     r3 := r2.RemainderWords(k + 1);
 
 
-    x := r1.Subtract(r3);
-    if (x.Fsign < 0) then
+    lx := r1.Subtract(r3);
+    if (lx.Fsign < 0) then
     begin
     begin
-      x := x.Add(mr);
+      lx := lx.Add(mr);
     end;
     end;
   end;
   end;
 
 
-  while (x.CompareTo(m) >= 0) do
+  while (lx.CompareTo(m) >= 0) do
   begin
   begin
-    x := x.Subtract(m);
+    lx := lx.Subtract(m);
   end;
   end;
 
 
-  Result := x;
+  Result := lx;
 end;
 end;
 
 
 function TBigInteger.Remainder(const x, y: TCryptoLibInt32Array)
 function TBigInteger.Remainder(const x, y: TCryptoLibInt32Array)