Thema: Delphi from C# to delphi

Einzelnen Beitrag anzeigen

QuickAndDirty

Registriert seit: 13. Jan 2004
Ort: Hamm(Westf)
1.889 Beiträge
 
Delphi 12 Athens
 
#3

AW: from C# to delphi

  Alt 29. Dez 2022, 14:40
A quick and dirty approach...my default type of approach...
Lines with comments, you have to check yourself.
I assume that u use the Rudy's TBiginteger project. But I'm not familiar with it.yet I know that he implemented all the operators.
Never tested or compiled the code.
I kept all the tranlations in the same line. so the code blocks aren't nicely indented and line breaks are missing, but they are in the same line like the original code that they represent.
I think you need Delphi 11 for Inline declarations to work.
Delphi-Quellcode:
      Function TMyClassHelper.IsProbablePrime(n:TBigInteger):boolean;
      Begin
         var n_minus_one:TBiginteger := n -1;
         if (n_minus_one.Sign <= 0) then Begin Result := false; Exit; end; // look for TBiginteger sign yourself

         var s:Integer := 0;
         var d:TBiginteger := n_minus_one;
         While d.IsEven Do Begin d.shr(1); inc(s); end;// look for TBiginteger IsEven yourself

         var bitLen:Integer := n.GetBitLength;// use code completion to find n.GetBitLength
         var randomBytes : TByteDynArray; SetLength(randomBytes, (bitlen div 8) +1);
         var lastByteMask:Byte := Byte( ((1 shl Integer(bitLen mod 8)) -1) );
         var a:TBigInteger;
         if (MillerRabinIterations < 15) then
            Begin Result := 15; Exit; End;
         for var i:Integer := 0 TO MillerRabinIterations-1 do
         Begin
            repeat
               Encryption.RNG.GetBytes(randomBytes); //??
               randomBytes[^1] &= lastByteMask; //??
               a := TBigInteger.Create(randomBytes);
            Until ( (a < 3) or (a >= n_minus_one) );
            a := a-1;

            var x:TBigInteger; x.ModPow(a, d, n);// Ithink TBigInteger.Pow(d,n) exists but for mod you might have to use the "mod" operator
            if (x = 1) or (x = n_minus_one) then continue;

            var r:Integer;
            for r := s -1 downto 1 do // looks weird "s-1 downto 1"
            Begin
               var x:TBigInteger; x.ModPow(x, 2, n);// Ithink TBigInteger.Pow(d,n) exists but for mod you might have to use the "mod" operator
               if x = 1 then Begin result := false; exit; end;
               if x = n_minus_one then break;
            end;
            if r = 0 then Begin Result := false; exit; end;
         end;
         result := true;
      end;
Andreas
Monads? Wtf are Monads?

Geändert von QuickAndDirty (29. Dez 2022 um 15:02 Uhr)
  Mit Zitat antworten Zitat