hermann on Sat, 09 Sep 2023 16:09:13 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Manually transpile Python code to GP: what about class / __name__ ?


I have transpiled all non-class functions from Python to GP for RSA_numbers_factored repo.

validate() does test all implemented functions, outputs which RSA_numbers are
present and validates all RSA number identities:
https://github.com/Hermann-SW/RSA_numbers_factored

hermann@i7-11850h:~/RSA_numbers_factored/pari$ gp -q
? \r RSA_numbers_factored.gp

with p-1 and q-1 factorizations (n=p*q): 25
59 digits, 79 digits,100 digits,110 digits,120 digits,129 digits,130 digits, 140 digits,150 digits,155 digits,160 digits,170 digits,576 bits ,180 digits, 190 digits,640 bits ,200 digits,210 digits,704 bits ,220 digits,230 digits,
232 digits,768 bits  ,240 digits,250 digits,

without (p-1) and (q-1) factorizations, but p and q: 0

have not been factored sofar: 31
260 digits,270 digits,896 bits  ,280 digits,290 digits,300 digits,
309 digits,1024 bits ,310 digits,320 digits,330 digits,340 digits,350 digits, 360 digits,370 digits,380 digits,390 digits,400 digits,410 digits,420 digits, 430 digits,440 digits,450 digits,460 digits,1536 bits ,470 digits,480 digits,
490 digits,500 digits,617 digits,2048 bits  (=617 digits)

validate(rsa): ✓
?


What remains is transpilation of Python class RSA.
Is there a "class" concept in GP?

It seems so, as "." is not a normal name character:

? .=7
  ***   syntax error, unexpected '=', expecting end of file: .=7
  ***                                                         ^--
? c.=7
*** syntax error, unexpected real number, expecting end of file: c.=7 *** ^---
? .x=7
*** syntax error, unexpected variable name, expecting end of file: .x=7 *** ^---
? c.x=7
(c)->7
? c.x
7
?

Even an init() can be simulated:

? c(y)={c.y=y;};
? c(5);
? c.y
5
?

Is there a "this"?
Is there a constructor mechanism?


I have this at end of RSA_numbers_factored.py:

if __name__ == "__main__":
    RSA().validate()

It executes member function validate only when called "python RSA_numbers_factored.py".
It does not call validate() when being imported.

Is a similar mechanism and/or trick available for GP?

If I use "##", then "gp -q < script" breaks, while "\r script" is fine.
Not sure how to make use of this difference in GP.


Regards,

Hermann.


P.S:
I learned in other threads on how to generate Python combinations in GP,
leaving out arguments and formatting according tutorial.

I transpiled this Python function

def sqtst(L: List[int], k: int, dbg: int = 0) -> None:
...
    assert len(L) >= k
    for s in combinations(range(len(L)), k):
        LS = list(chain(*[sq2(L[x]) for x in s]))
        S = square_sums(LS, uniq=True)
        if dbg >= 1:
            if dbg >= 3:
                print(s)
            if dbg >= 2:
                print(LS)
            print(S)
        assert 2 ** (k - 1) == len(S)

to this GP function doing the same:

sqtst(L,k,dbg=0)=
{
...
    assert(#L>=k);
    my(LS,S);
    forsubset([#L,k],s,
        LS=concat([sq2(L[x])|x<-s]);
        S=square_sums(LS,,,1);
        if(dbg>=1,
            if(dbg>=3,
                print(s));
            if(dbg>=2,
                print(LS));
            print(S));
        assert(2^(k-1)==#S));
}