I tried several different approaches, from
def ToByteArray(x):
x = int(x)
return x.to_bytes((x.bit_length() + 7) // 8, byteorder='big')
or diving x by 256 and building a new bytearray
in a loop but it just feels slow compared to the conversion of a normal python int or in gmpy2 c++.
Isn't there something like an mpz_export
in c++? What is the fastest way to accomplish this?
Edit: The reason I need to convert it to bytes
is that hashlib
cannot hash mpz
. If there is another fast way to get a strong cryptographic (sha256) hash of an mpz
, without having to convert it to bytes
first, that might help aswell!