2

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:

OverflowError because of "int is to big to convert into C long".

Are there any ways 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.


[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.

G.F
  • 85
  • 9
  • 2
    Python uses BigInt when it gets too big to store in a real integer, it's software based solution, as CPU's cannot directly manipulate values that do not fit into their registers, and often have very limited or no support for types that are twice as big as maximum size, for example, CPU that can operate on up to 64bit integers, also still "supports" 128bit ints, that's the result of 64bit integer multiplication, but that's still not big enough for your use case. If Cython does not have BigInt support, find a library that does, which you can use with it. – Sahsahae Aug 06 '19 at 12:38
  • Thank you for answering me. So can't I do anything to get Cython to deal with integers like Python does? =( – G.F Aug 06 '19 at 13:28
  • If you don't set types on the variables then Cython should deal with the integers exactly like Python does. Of course you won't get much speed-up. If it doesn't work then you need to show some code to get more help. – DavidW Aug 06 '19 at 14:13
  • @DavidW At the beginning, I tried to declare variables like "cdef int n" but if I used huge integer, I got an overflow error. Two hours ago, I wrote "from cpython cimport int as Integer" and now if I declare variables like "cdef Integer n", Cython convert it into a pointer to a PyObject. The problem is that this is exactly the same result I get without declaring the variables and therefore I have no speed up... – G.F Aug 06 '19 at 16:32
  • Those are the two "easy" options (use smaller numbers, or use Python objects for the numbers). You could looking Sahsahae's suggestion of wrapping a different BigInt library - I know people have wrapped Gnu-Multiprecision with Cython, so that might be a place to start. – DavidW Aug 06 '19 at 20:27
  • @DavidW ok, I'll study it. Thank you =) – G.F Aug 07 '19 at 05:33

0 Answers0