hermann on Tue, 13 Jun 2023 20:56:33 +0200 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
How to best dynamically make use of cypari2? |
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.