AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

from C# to delphi

Ein Thema von sdean · begonnen am 22. Dez 2022 · letzter Beitrag vom 30. Jan 2023
 
QuickAndDirty

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

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
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 13:35 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz