Vincent Delecroix on Tue, 13 Jun 2023 22:59:57 +0200 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: How to best dynamically make use of cypari2? |
A more pythonic way is try: import cypari2 except ImportError: cypari2 = None if cypari2 is None: # cypari2 not available ... else: # cypari2 is available ... Le 13/06/2023 à 20:51, hermann@stamm-wilbrandt.de a écrit :
I want to add a function to a RSA_numbers_factored python lib. Determining sum of squares from "sqrt(-1) (mod p)" takes 42min with Python "gcd()" on gaussian integers, but only few milliseconds for 388342-digit prime with Pari "halfgcd()" not available in Python. So I want to use "halfgcd()" when cpyari2 is available, and if not, I want to use Python "gcd()" on gaussian integers. Here is demonstration without cypari2: Type "help", "copyright", "credits" or "license" for more information.from dyn_cypari2 import * to_sum2sqs(12, 29)(2, 5)Here with cypari2, imported before importing dyn_cypari2: Type "help", "copyright", "credits" or "license" for more information.import cypari2 from dyn_cypari2 import * to_sum2sqs(12, 29)pari (5, -2)I did it this way, is that OK? Are there alternatives? $ cat dyn_cypari2.py from sys import modules if "cypari2" in modules: import cypari2 pari = cypari2.Pari() def to_sum2sqs(sqrtm1, p): print("pari") [M,V] = pari.halfgcd(sqrtm1, p) return V[1], M[1,0] else: from sympy import gcd, I def to_sum2sqs(sqrtm1, p): return gcd(p, sqrtm1+I).as_real_imag() def to_sqrtm1(xy, p): return xy[0] * pow(xy[1], -1, p) % p $ Regards, Hermann.