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
Antwort Antwort
Seite 1 von 2  1 2      
sdean

Registriert seit: 5. Dez 2009
64 Beiträge
 
#1

from C# to delphi

  Alt 22. Dez 2022, 15:00
Delphi-Version: 11 Alexandria
Hello can someone help me porting this C# code into Delphi :

Code:
internal static ulong PQFactorize(ulong pq)
      {
         if (pq < 2) return 1;
         var random = new Random();
         ulong g = 0;
         for (int i = 0, iter = 0; i < 3 || iter < 1000; i++)
         {
            ulong q = (ulong)random.Next(17, 32) % (pq - 1);
            ulong x = ((ulong)random.Next() + (ulong)random.Next() << 31) % (pq - 1) + 1;
            ulong y = x;
            int lim = 1 << (Math.Min(5, i) + 18);
            for (int j = 1; j < lim; j++)
            {
               iter++;
               ulong res = q, a = x;
               while (x != 0)
               {
                  if ((x & 1) != 0)
                     res = (res + a) % pq;
                  a = (a + a) % pq;
                  x >>= 1;
               }
               x = res;
               ulong z = x < y ? pq + x - y : x - y;
               g = gcd(z, pq);
               if (g != 1)
                  break;

               if ((j & (j - 1)) == 0)
                  y = x;
            }
            if (g > 1 && g < pq)
               break;
         }
         if (g != 0)
         {
            ulong other = pq / g;
            if (other < g)
               g = other;
         }
         return g;

         static ulong gcd(ulong left, ulong right)
         {
            while (right != 0)
            {
               ulong num = left % right;
               left = right;
               right = num;
            }
            return left;
         }
      }
  Mit Zitat antworten Zitat
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.619 Beiträge
 
Delphi 12 Athens
 
#2

AW: from C# to delphi

  Alt 22. Dez 2022, 16:04
Completely untested (written in Notepad++):
Delphi-Quellcode:
function PQFactorize(pq: LongWord): LongWord;

  function gcd(left, right: LongWord): LongWord;
  var
    num: LongWord;
  begin
    while right <> 0 do
      begin
        num := left mod right;
        left := right;
        right := num;
      end;
    Result := left;
  end;

var
  g, q, x, y, z, res, a, other: LongWord;
  i, j, iter, lim: integer;
begin
  if pq < 2 then
    Exit(1);
  g := 0;
  i := 0;
  iter := 0;
  repeat
    q := LongWord(Random(16) + 17) mod (pq - 1);
    x := (LongWord(Random(MAXINT)) + LongWord(Random(MAXINT)) shl 31) mod (pq - 1) + 1;
    y := x;
    lim := 1 shl (math.Min(5, i) + 18);
    for j := 1 to lim - 1 do
      begin
        inc(iter);
        res := q;
        a := x;
        while x <> 0 do
          begin
            if (x and 1) <> 0 then
              res := (res + a) mod pq;
            a := (a + a) mod pq;
            x := x shr 1;
          end;
        x := res;
        if x < y then
          z := pq + x - y
        else
          z := x - y;
        g := gcd(z, pq);
        if g <> 1 then
          break;
        if (j and (j - 1)) = 0 then
          y := x;
      end;
    if (g > 1) and (g < pq) then
      break;
    inc(i);
  until (i >= 3) or (iter >= 1000);
  if (g <> 0) then
    begin
      other := pq div g;
      if (other < g) then
        g := other;
    end;
  Result := g;
end;
The keyword "static" implies that this is a static class function, so you have to declare it as such.
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
sdean

Registriert seit: 5. Dez 2009
64 Beiträge
 
#3

AW: from C# to delphi

  Alt 22. Dez 2022, 17:01
Completely untested (written in Notepad++):
Delphi-Quellcode:
function PQFactorize(pq: LongWord): LongWord;

  function gcd(left, right: LongWord): LongWord;
  var
    num: LongWord;
  begin
    while right <> 0 do
      begin
        num := left mod right;
        left := right;
        right := num;
      end;
    Result := left;
  end;

var
  g, q, x, y, z, res, a, other: LongWord;
  i, j, iter, lim: integer;
begin
  if pq < 2 then
    Exit(1);
  g := 0;
  i := 0;
  iter := 0;
  repeat
    q := LongWord(Random(16) + 17) mod (pq - 1);
    x := (LongWord(Random(MAXINT)) + LongWord(Random(MAXINT)) shl 31) mod (pq - 1) + 1;
    y := x;
    lim := 1 shl (math.Min(5, i) + 18);
    for j := 1 to lim - 1 do
      begin
        inc(iter);
        res := q;
        a := x;
        while x <> 0 do
          begin
            if (x and 1) <> 0 then
              res := (res + a) mod pq;
            a := (a + a) mod pq;
            x := x shr 1;
          end;
        x := res;
        if x < y then
          z := pq + x - y
        else
          z := x - y;
        g := gcd(z, pq);
        if g <> 1 then
          break;
        if (j and (j - 1)) = 0 then
          y := x;
      end;
    if (g > 1) and (g < pq) then
      break;
    inc(i);
  until (i >= 3) or (iter >= 1000);
  if (g <> 0) then
    begin
      other := pq div g;
      if (other < g) then
        g := other;
    end;
  Result := g;
end;
The keyword "static" implies that this is a static class function, so you have to declare it as such.
so many thanks for such a great effort .
  Mit Zitat antworten Zitat
sdean

Registriert seit: 5. Dez 2009
64 Beiträge
 
#4

AW: from C# to delphi

  Alt 22. Dez 2022, 21:47
So many thanks , and what about this please :
Code:
public static bool IsProbablePrime(this BigInteger n)
      {
         var n_minus_one = n - BigInteger.One;
         if (n_minus_one.Sign <= 0) return false;

         int s;
         var d = n_minus_one;
         for (s = 0; d.IsEven; s++) d >>= 1;

         var bitLen = n.GetBitLength();
         var randomBytes = new byte[bitLen / 8 + 1];
         var lastByteMask = (byte)((1 << (int)(bitLen % 8)) - 1);
         BigInteger a;
         if (MillerRabinIterations < 15)
            return false;
         for (int i = 0; i < MillerRabinIterations; i++)
         {
            do
            {
               Encryption.RNG.GetBytes(randomBytes);
               randomBytes[^1] &= lastByteMask;
               a = new BigInteger(randomBytes);
            }
            while (a < 3 || a >= n_minus_one);
            a--;

            var x = BigInteger.ModPow(a, d, n);
            if (x.IsOne || x == n_minus_one) continue;

            int r;
            for (r = s - 1; r > 0; r--)
            {
               x = BigInteger.ModPow(x, 2, n);
               if (x.IsOne) return false;
               if (x == n_minus_one) break;
            }
            if (r == 0) return false;
         }
         return true;
      }
Again thank you
  Mit Zitat antworten Zitat
Alt 23. Dez 2022, 02:41     Erstellt von adhoccaroling
Dieser Beitrag wurde von TBx gelöscht. - Grund: Verdacht auf SPAM und den damit verbundenen verschwenderischen Umgang von wertvollen Bits und Bytes
Alt 23. Dez 2022, 02:44     Erstellt von adhoccaroling
Dieser Beitrag wurde von TBx gelöscht. - Grund: Verdacht auf SPAM und den damit verbundenen verschwenderischen Umgang von wertvollen Bits und Bytes
Benutzerbild von DeddyH
DeddyH

Registriert seit: 17. Sep 2006
Ort: Barchfeld
27.619 Beiträge
 
Delphi 12 Athens
 
#5

AW: from C# to delphi

  Alt 23. Dez 2022, 06:53
This would be difficult to translate, because AFAIK there is no builtin BigInteger-Type in Delphi. Also I don' t know if there are any RNG-implementations without external libraries.
Detlef
"Ich habe Angst vor dem Tag, an dem die Technologie unsere menschlichen Interaktionen übertrumpft. Die Welt wird eine Generation von Idioten bekommen." (Albert Einstein)
Dieser Tag ist längst gekommen
  Mit Zitat antworten Zitat
sdean

Registriert seit: 5. Dez 2009
64 Beiträge
 
#6

AW: from C# to delphi

  Alt 23. Dez 2022, 07:48
This would be difficult to translate, because AFAIK there is no builtin BigInteger-Type in Delphi. Also I don' t know if there are any RNG-implementations without external libraries.
Can this great delphi implementation help :
http://rvelthuis.de/programs/bigintegers.html
  Mit Zitat antworten Zitat
TurboMagic

Registriert seit: 28. Feb 2016
Ort: Nordost Baden-Württemberg
2.936 Beiträge
 
Delphi 12 Athens
 
#7

AW: from C# to delphi

  Alt 23. Dez 2022, 09:08
This would be difficult to translate, because AFAIK there is no builtin BigInteger-Type in Delphi. Also I don' t know if there are any RNG-implementations without external libraries.
Is RNG a random number generator?
If yes, why isn't the built in random suitable? Because of the big number range?
  Mit Zitat antworten Zitat
generic

Registriert seit: 24. Mär 2004
Ort: bei Hannover
2.416 Beiträge
 
Delphi XE5 Professional
 
#8

AW: from C# to delphi

  Alt 28. Dez 2022, 22:42
Hello can someone help me porting this C# code into Delphi
Why to convert that, when you can use that code directly.
You have many options to do this.
1) Create a webservice and call it from Delphi.
2) Create a COM-Server and use it from Delphi. With Delphi's import tlb tool you don't need to write any definitions and with dotnet RegAsm.exe you can generate a TLB file. If you don't want to register the COM Server you can also use SideBySide Configuration.
Coding BOTT - Video Tutorials rund um das Programmieren - https://www.youtube.com/@codingbott

Geändert von generic (28. Dez 2022 um 22:59 Uhr)
  Mit Zitat antworten Zitat
QuickAndDirty

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

AW: from C# to delphi

  Alt 29. Dez 2022, 14:26
@generic: I suspect he wants a native implementation to achieve a speed increase.
@sdean: I guess you want to convert a bunch of functions. Think about automating the process! At least a bit. Like dictionary style.

The other way could have been to use a decompiler of the .net assembly to decompile it into Delphi.net code or Oxygene Code. You could apply some finishing touches to this delphi like code and be done.
I'm just not sure if you have a version of Delphi that supports Delphi.net...I think Delphi.net went the way of Kylix? I also don't know if Oxygen is still alive???

How big is the C# library/assembly/Project you are currently converting?

If you just want to use a C# assembly in a Delphi project you might just want to use https://www.crystalnet-tech.com/runt...library4delphi
Andreas
Monads? Wtf are Monads?

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

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

AW: from C# to delphi

  Alt 29. Dez 2022, 15: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 16:02 Uhr)
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2      


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:40 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz