aleaxit / gmpy

General Multi-Precision arithmetic for Python 2.6+/3+ (GMP, MPIR, MPFR, MPC)
https://gmpy2.readthedocs.io/en/latest/
GNU Lesser General Public License v3.0
510 stars 86 forks source link

mpz object missing to_bytes() method #431

Closed PiRK closed 11 months ago

PiRK commented 11 months ago

When running tests on my project (Electrum ABC), a user is getting errors like this one:

======================================================================
ERROR: test_sweep (electrumabc.tests.test_wallet.TestSweep)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/pierre/dev/bitcoin-abc/electrum/electrumabc/tests/test_wallet.py", line 222, in test_sweep
    inputs, keypairs = sweep_preparations([WIF1], network)
  File "/home/pierre/dev/bitcoin-abc/electrum/electrumabc/wallet.py", line 179, in sweep_preparations
    find_utxos_for_privkey(txin_type, privkey, compressed)
  File "/home/pierre/dev/bitcoin-abc/electrum/electrumabc/wallet.py", line 170, in find_utxos_for_privkey
    pubkey = bitcoin.public_key_from_private_key(privkey, compressed)
  File "/home/pierre/dev/bitcoin-abc/electrum/electrumabc/bitcoin.py", line 732, in public_key_from_private_key
    public_key = GetPubKey(pkey.pubkey, compressed)
  File "/home/pierre/dev/bitcoin-abc/electrum/electrumabc/bitcoin.py", line 721, in GetPubKey
    return i2o_ECPublicKey(pubkey, compressed)
  File "/home/pierre/dev/bitcoin-abc/electrum/electrumabc/bitcoin.py", line 490, in i2o_ECPublicKey
    key = b"\x03" + pubkey.point.x().to_bytes(32, "big")
AttributeError: 'mpz' object has no attribute 'to_bytes'

I can reproduce if I install gmpy or gmpy2, but it works if none of them is installed. I guess ecdsa is using regular ints when gmpy is not available.

Is to_bytes() something you intend to support, so as to be a drop in replacement for python int?

Otherwise I will add a special case in my code.

casevh commented 11 months ago

It will be available in the next release. I should have an alpha version out in the next couple of days.

PiRK commented 11 months ago

Great, thanks!

casevh commented 11 months ago

I have published gmpy2 2.2.0a1 with binary wheels for all supported platforms.

skirpichev commented 11 months ago

@casevh, I think this could be closed.