I want to know how I can build a Python script that can Calculate Public Key ECDSA from a Private Key (HEX) and the result is similar to the imagen

enter image description here

1 Answer 1


This has been answered already in:
(How do you get a Bitcoin Public Key from a Private Key)

Look at the answer of @user1658887 for example.

EDIT: Given the fact that you're asking for the public key output in uncompressed hex format explicitly, here are some wrapping code lines around the the stuff that user1658887 posted in the answer referenced above:

import binascii
import struct
import sys

# code taken from user1658887's answer at: https://bitcoin.stackexchange.com/questions/25024/how-do-you-get-a-bitcoin-public-key-from-a-private-key

def sk_to_pk(sk):
    Derive the public key of a secret key on the secp256k1 curve.

        sk: An integer representing the secret key (also known as secret

        A coordinate (x, y) on the curve repesenting the public key
      for the given secret key.

        ValueError: The secret key is not in the valid range [1,N-1].
    # base point (generator)
    G = (0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,

    # field prime

    # order
    N = (1 << 256) - 0x14551231950B75FC4402DA1732FC9BEBF

    # check if the key is valid
    if not(0 < sk < N):
        msg = "{} is not a valid key (not in range [1, {}])"
        raise ValueError(msg.format(hex(sk), hex(N-1)))

    # addition operation on the elliptic curve
    # see: https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Point_addition
    # note that the coordinates need to be given modulo P and that division is
    # done by computing the multiplicative inverse, which can be done with
    # x^-1 = x^(P-2) mod P using fermat's little theorem (the pow function of
    # python can do this efficiently even for very large P)
    def add(p, q):
        px, py = p
        qx, qy = q
        if p == q:
            lam = (3 * px * px) * pow(2 * py, P - 2, P)
            lam = (qy - py) * pow(qx - px, P - 2, P)
        rx = lam**2 - px - qx
        ry = lam * (px - rx) - py
        return rx % P, ry % P

    # compute G * sk with repeated addition
    # by using the binary representation of sk this can be done in 256
    # iterations (double-and-add)
    ret = None
    for i in xrange(256):
        if sk & (1 << i):
            if ret is None:
                ret = G
            ret = add(ret, G)
        G = add(G, G)

    return ret

if len(sys.argv) >= 2:
    sk = long(sys.argv[1], 16)
    raise ValueError('Please provide the private key in hex format as a 1st parameter')

pk = sk_to_pk(sk)
pkx, pky = pk
ybyte=0x02 if pky % 2 == 0 else 0x03       # can be used when a compressed pubkey is desired
hexpk = "%s%s%s" % ("04",hex(pkx),hex(pky))    # 04 indicates that an uncompressed pubkey is desired
print (hexpk.replace("0x","",2).replace("L","",2));

Which gives you your desired result:

~/$ python priv2pub.py B54FD9915A8B6870647705E91D4D953BA34B2B53F4681FDEFD50ECC13507BEFC
  • Disculpame pero no esta respondido de la forma que deseaba..Yo estoy suministrando la Clave Privada y desde alli queria generar la llave publica descomprilda de 130 caracteres.. Mientra que la respuesta que esta publicada.... el script genera de forma aleatoria la clave privada y la llave publica..... eso no es lo que quiero Jun 29, 2020 at 14:25
  • es exactamente lo que el script hace: La forma de uso es: python priv2pub.py <clave privada> y el resultado es la llave publica (130 caracteres). Mira el 'EDIT' de mi respuesta.
    – citizenfu
    Jun 29, 2020 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.