1
$\begingroup$

I want to factorize very huge numbers (e.g. 100-bits, 200-bits numbers) with Cython.

Hi everyone, I implemented the Elliptic Curve Method for factorization into Python 3.6. Now, I want to speed up my code using Cython (version 29.13). I'm a beginner in Cython world but I know that Cython works better if I define type for variables. So, I converted all my Python classes into Cython classes and now I would like to typize variables. I read that Cython automatically convert the "cdef int" declaration into classical Python integer with infinite length but this is not the case. When I try to factorize numbers like this "5192296858543544183479685583896053", I get an OverflowError because of "int is to big to convert into C long". There exists any ways to declare huge integer to speed up my code? The only variables without type declaration are the variables that could be very huge integer.

PS: I have already tried to use the cpython type uPY_LONG_LONG (unsigned long long) but it was useless because I always got the same error.

Thank you for paying attention to me and thanks in advance for your answers.


[UPDATE]

If I declare something like this:

cdef int function():
    cdef int a
    a = 2**100
    return a

I get an OverflowError because of 2**100 is too huge to cast it into an integer.

If I import the long type from cpython, I get the same error:

from cpython import long as Long
cdef Long function():
      cdef Long a
      a = 2**100
      return a

If I import the int type from cpython, I get no error but I have no speed up:

from cpython import int as Integer
cdef Integer function():
      cdef Integer a
      a = 2**100
      return a

If I analyse the C++ code created as translation, I notice that the variable a has been declared as a pointer to a PyObject. This is exactly the same translation that I get if I don't declare the variable. So, maybe in this context there are no difference. I cannot improve all the for loops I use because of I have something like this:

for x in range(p):
     .....

But if p is a huge integer and Cython declare p and x as pointers to PyObject, Cython can translate this loop into a C loop to speed up it.

$\endgroup$
6
  • $\begingroup$ Use python to convert the big integer to a string. Pass the string to cython and use one of the biginter packages, usually gmp or pari or one of the object oriented wrappers, and implement your computation using these libraries. This should work, I do not know if it is the most elegant way to do this. $\endgroup$ Aug 6 '19 at 13:32
  • $\begingroup$ @LutzL Thank you for this tip. So, I must implement the translation logic by myself... I thought there was a way to get Cython to do it automatically. $\endgroup$
    – G.F
    Aug 6 '19 at 13:37
  • $\begingroup$ Yes, that seems to be the wider wisdom. In the end, this will be something like a python wrapper around C or C++ code implementing the heavy computations. // There might be something that get translated automatically, but that just glues the python interna a little better together, like a just-in-time compiler would do. You get a speed-up, but nothing dramatic. $\endgroup$ Aug 6 '19 at 13:51
  • $\begingroup$ @LutzL Ok, I'll study Cython better to learn how to develop a wrapper to do it. I hope to speed up my Cython library with this. Thank you very much =) $\endgroup$
    – G.F
    Aug 6 '19 at 16:20
  • $\begingroup$ Python is already using optimized libraries for large integer arithmetic. Have you benchmarked your code to find out which parts take the most time? Big integer arithmetic using an external library is likely to be within a small integer factor of python's in terms of performance. $\endgroup$
    – Kirill
    Aug 6 '19 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.