I have some large numbers that I am using for Elliptic Curve Cryptography. So far I have been doing all the computations in plain python which natively support arbitrary sized numbers by the use of "bignum" integer type. However, I would like to vectorize some of these operations (for performance reasons) and would like to use an array containing 256 bit unsigned integers. But AFAIK the maximum size of an integer in numpy is 64-bits by using np.longlong (or np.ulonglong for the unsigned version). An idea from the following post was to represent each 256-bit number as an array with four 64-bit integers:
large = 105951305240504794066266398962584786593081186897777398483830058739006966285013
arr = np.array([large.to_bytes(32,'little')]).view('P')
arr
Output:
array([18196013122530525909, 15462736877728584896, 12869567647602165677,
16879016735257358861], dtype=uint64)
And I can convert it back to native python "bignum" by doing:
int.from_bytes(arr.tobytes(), 'little')
Output:
105951305240504794066266398962584786593081186897777398483830058739006966285013
However, I would like to do some operations before converting it back to "bignum":
arr2 = arr + 1
int.from_bytes(arr2.tobytes(), 'little')
Output:
105951305240504794072543500697971467357257258687906003363414235534976478560982
Which is ofcourse not equal to large + 1
Is it possible to handle 256-bit numbers in numpy? If not, does there exist any suitable workarounds?