## How can I test if a bitcoin address is compressed or not?

8

4

1D8VSXHFAWNhAbruwUMbNp4hsXAnRNkYvX
12T15tvMGaymMX97X9KxVzyLF323r8v6Hg


One is compressed, one is not.

How can I test which is which? (Preferably using python 2.7)

10

You cannot determine (from only a base58check address) if the corresponding public key is compressed.

A "normal" pay-to-public-key-hash (P2PKH) is a cryptographic hash of either a compressed or an uncompressed public key. Because hashes cannot be feasibly reversed, you cannot tell if the public key, before being hashed, was compressed or uncompressed.

More specifically, a public key in Bitcoin is a pair integers (x,y). For uncompressed public keys, these integers are encoded as 256-bit unsigned big-endian ints, concatenated together, and then prepended with a single 0x04 byte. The result is 65 bytes long.

For compressed public keys, only the x coordinate is encoded (like above, as 256-bit unsigned big-endian int). It turns out that the y coordinate can only be one of two values, one even and one odd. Instead of prepending a single 0x04 byte, a single 0x02 or 0x03 byte is prepended depending on y's value (0x02 for even, 0x03 for odd). The result is 33 bytes long.

Finally, these bytes (from one of the two paragraphs above) go through the address conversion process described here in the bitcoin.org Developer Guide, which includes taking an irreversible cryptographic hash (actually, two hashes).

Note that in order to redeem a UTXO associated with a P2PKH address, the redeemer must create a transaction which includes the unhashed public key, either compressed or uncompressed. If such a tx exists in the blockchain, it can be connected to its "normal" base58check address. This is one of the reasons why address reuse is discouraged: once you redeem a UTXO, you "reveal" your public key, which is slightly more sensitive than your bitcoin address.

4

Background info:

Given just the address itself, no, there is no way to tell if it was generated from a compressed or an uncompressed public key. This is calculated from the base58 encoding of the RIPEMD(SHA256(public_key_bytes)). Since the hash isn't reversible, without the public key already in hand, you won't be able to get any more information.

Just look at the base58 decoded data:

$decodeBase58 12T15tvMGaymMX97X9KxVzyLF323r8v6Hg 000FE302E0D96D9F9AA71738A426A9696EB49AD458679BAFD3$ decodeBase58 1D8VSXHFAWNhAbruwUMbNp4hsXAnRNkYvX
00850C296420DC9B02CE237ED630B89815E0803EA3BED0F344


The version byte is 0x00 in both, then there's the 20 bytes of the hash, and a 4 byte checksum. None of this reveals any information about what information was hashed.

1

An address is the hash of the pubkey (plus checksum and versioning). Hashes are irreversible and randomly distributed so there is no way to determined the pubkey from the pubkeyhash.

0

As the others mentioned, the short answer is no, but from a WIF(Wallet import format) key you can determine if the address is compressed or not by checking the last byte if it is 0x01.