pythonpublic-key-encryptionelliptic-curveecdsa

How to get an elliptic curve public key from a private key


So, I need to get a public key from a corresponding 256 bit number using ECC spec256k1.

So, lets say I get a private key using a sha256 from any passphrase, like this:

>>> import hashlib
>>> private_key = hashlib.sha3_256(b"Led Zeppelin - No Quarter").hexdigest()
>>> private_key
'c0b279f18074de51d075b152c8ce78b7bddb284e8cfde19896162abec0a0acce'

How do I get a public key from that private key? I need to print the public key as a string.


Solution

  • pip install fastecdsa
    
    from fastecdsa import keys, curve,ecdsa
    priv_key, pub_key = keys.gen_keypair(curve.secp256k1) 
    
    print(pub_key)
    

    which yields

    X: 0xcc228e1a4c8e187a0deeabcd6e43bc8f7b6bdd91b8f823912f2de188fba054e6
    Y: 0x7995a9d3866a8fa11a9af933c76216a908995ec5cec6ed7d3056b787fa7d39d7
    

    Supported Primitives

    Curves over Prime Fields - Source

    Name                      Class
    
    P192 / secp192r1          fastecdsa.curve.P192  
    P224 / secp224r1          fastecdsa.curve.P224  
    P256 / secp256r1          fastecdsa.curve.P256  
    P384 / secp384r1          fastecdsa.curve.P384  
    P521 / secp521r1          fastecdsa.curve.P521 
    secp192k1                 fastecdsa.curve.secp192k1     
    secp224k1                 fastecdsa.curve.secp224k1     
    secp256k1 (bitcoin curve) fastecdsa.curve.secp256k1     
    brainpoolP160r1           fastecdsa.curve.brainpoolP160r1   
    brainpoolP192r1           fastecdsa.curve.brainpoolP192r1   
    brainpoolP224r1           fastecdsa.curve.brainpoolP224r1   
    brainpoolP256r1           fastecdsa.curve.brainpoolP256r1   
    brainpoolP320r1           fastecdsa.curve.brainpoolP320r1   
    brainpoolP384r1           fastecdsa.curve.brainpoolP384r1
    brainpoolP512r1           fastecdsa.curve.brainpoolP512r1