<p>Using <a href="https://github.com/mcdallas/cryptotools" rel="nofollow noreferrer">this</a> library (disclaimer: I am the author):</p>
<pre><code>&gt;&gt;&gt; from cryptotools import PublicKey

&gt;&gt;&gt; PublicKey.from_hex('025A2146590B80D1F0D97CC7104E702011AFFF21BFAF817F5C7002446369BA9DDC').hex(compressed=False)
'045a2146590b80d1f0d97cc7104e702011afff21bfaf817f5c7002446369ba9ddc9bd5dcd1b4a737244d6bb7b96e256391b8597d3a7972a6f8ca9096d4aea1f37e'

</code></pre>

CC BY-SA 4.0
2022-08-30T18:56:52.200Z
When trying to input an compressed PubKey in 0x hex format and later converted to string with str() into this function it outputs the following error:

in decode assert len(key) == 33, 'A compressed public key must be 33 bytes long'
AssertionError: A compressed public key must be 33 bytes long
So the compressed key has to be a string with '' single quotes. This way it works.
Nevertheless, thanks!
Maltoon Yezi
CC BY-SA 4.0
2022-08-30T11:55:47.193Z
2022-08-30T11:55:47.193Z
Mike D
<p>I've searched really hard for a Python script that coverts a compressed public key into uncompressed one, since I don't know how to code this.
I stumbled upon these links:</p>
<p><a href="https://stackoverflow.com/questions/43629265/deriving-an-ecdsa-uncompressed-public-key-from-a-compressed-one">https://stackoverflow.com/questions/43629265/deriving-an-ecdsa-uncompressed-public-key-from-a-compressed-one</a></p>
<p><a href="https://bitcoin.stackexchange.com/questions/86234/how-to-uncompress-a-public-key">How to uncompress a public key?</a></p>
<p><a href="https://bitcointalk.org/index.php?topic=644919.0" rel="nofollow noreferrer">https://bitcointalk.org/index.php?topic=644919.0</a></p>
<p>but all python scripts/functions either don't work or outputting incorrect values. I tried to debug them and failed.
Could you point out how to code something like this in Python or give an example of the working code?
Here are the values to play with (HEX):</p>
<pre><code>#Test case 1
#Compressed Pubkey: 025A2146590B80D1F0D97CC7104E702011AFFF21BFAF817F5C7002446369BA9DDC
'''
Right Uncompressed Pubkey:
045A2146590B80D1F0D97CC7104E702011AFFF21BFAF817F5C7002446369BA9DDC9BD5DCD1B4A737244D6BB7B96E256391B8597D3A7972A6F8CA9096D4AEA1F37E
'''

#Test case 2
#Compressed Pubkey: 035728F4692D85D411DF3643CD69FE05C411A0D507C7D814008F56C8F260AD7ED9
'''
Right Uncompressed Pubkey:
045728F4692D85D411DF3643CD69FE05C411A0D507C7D814008F56C8F260AD7ED99E2DF8D9CB1A575D55264692629AE22E518BC14AD02592941C13BE6755C72973
'''

#Test case 3
#Compressed Pubkey:039E87EB177890FDD788B95843ED53AD4FB6E877E3F730EF1E73593964C2AB9D15
'''
Right Uncompressed Pubkey:
049E87EB177890FDD788B95843ED53AD4FB6E877E3F730EF1E73593964C2AB9D15A3B647C8C4A0766420917B7B445CDCD6BFEC2900175C5534C6113954F3FF00D9
'''
</code></pre>

<public-key><python><secp256k1><serialization>
How to convert a compressed public key into uncompressed one in Python?
Maltoon Yezi
# How to convert a compressed public key into uncompressed one in Python?

(Asked by: username_0 on 2022-08-30)

I've searched really hard for a Python script that coverts a compressed public key into uncompressed one, since I don't know how to code this.
I stumbled upon these links:
https://stackoverflow.com/questions/43629265/deriving-an-ecdsa-uncompressed-public-key-from-a-compressed-one
How to uncompress a public key?
https://bitcointalk.org/index.php?topic=644919.0
but all python scripts/functions either don't work or outputting incorrect values. I tried to debug them and failed.
Could you point out how to code something like this in Python or give an example of the working code?
Here are the values to play with (HEX):
#Test case 1
#Compressed Pubkey: 025A2146590B80D1F0D97CC7104E702011AFFF21BFAF817F5C7002446369BA9DDC
'''
Right Uncompressed Pubkey:
045A2146590B80D1F0D97CC7104E702011AFFF21BFAF817F5C7002446369BA9DDC9BD5DCD1B4A737244D6BB7B96E256391B8597D3A7972A6F8CA9096D4AEA1F37E
'''

#Test case 2
#Compressed Pubkey: 035728F4692D85D411DF3643CD69FE05C411A0D507C7D814008F56C8F260AD7ED9
'''
Right Uncompressed Pubkey:
045728F4692D85D411DF3643CD69FE05C411A0D507C7D814008F56C8F260AD7ED99E2DF8D9CB1A575D55264692629AE22E518BC14AD02592941C13BE6755C72973
'''

#Test case 3
#Compressed Pubkey:039E87EB177890FDD788B95843ED53AD4FB6E877E3F730EF1E73593964C2AB9D15
'''
Right Uncompressed Pubkey:
049E87EB177890FDD788B95843ED53AD4FB6E877E3F730EF1E73593964C2AB9D15A3B647C8C4A0766420917B7B445CDCD6BFEC2900175C5534C6113954F3FF00D9
'''

**Tags:** public-key, python, secp256k1, serialization


---

## 1 answers

### Answer by username_1 on 2022-08-30. Score: 2 | Accepted answer

Using this library (disclaimer: I am the author):
>>> from cryptotools import PublicKey

>>> PublicKey.from_hex('025A2146590B80D1F0D97CC7104E702011AFFF21BFAF817F5C7002446369BA9DDC').hex(compressed=False)
'045a2146590b80d1f0d97cc7104e702011afff21bfaf817f5c7002446369ba9ddc9bd5dcd1b4a737244d6bb7b96e256391b8597d3a7972a6f8ca9096d4aea1f37e'

> **Comments:**
> **username_0**: When trying to input an compressed PubKey in 0x hex format and later converted to string with str() into this function it outputs the following error:  in decode assert len(key) == 33, 'A compressed public key must be 33 bytes long' AssertionError: A compressed public key must be 33 bytes long So the compressed key has to be a string with '' single quotes. This way it works. Nevertheless, thanks!  

---

