Skip to main content
3 of 4
added 34 characters in body

Can the public key be generated from the private key?

The calculation/formula i use in deriving a public key from the private key without importing any module in python3 script involves the following steps:

  1. Define the parameters of the secp256k1 elliptic curve:
p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
a = 0
b = 7
Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
Gy = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8
n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
  1. Initialize the variables:
Qx = Gx
Qy = Gy
  1. Define the private key:
private_key = 0x000000000000000000000000000000000000000000000000000000000000000f
  1. Perform the scalar multiplication using double-and-add algorithm:
for bit in bin(private_key)[3:]:
    # Perform point doubling on the elliptic curve
    # Calculate the slope
    s = (3 * Qx**2 + a) * pow(2 * Qy, -1, p) % p

    # Calculate the new point coordinates
    Rx = (s**2 - 2 * Qx) % p
    Ry = (s * (Qx - Rx) - Qy) % p

    # print every calculated points
    print ('\n[1]\nQx =',Qx,'\nQy =',Qy,'\nRx =',Rx,'\nRy =',Ry)

    Qx = Rx
    Qy = Ry

    if bit == '1':
        # Perform point addition on the elliptic curve
        # Calculate the slope
        s = (Qy - Gy) * pow(Qx - Gx, -1, p) % p

        # Calculate the new point coordinates
        Rx = (s**2 - Qx - Gx) % p
        Ry = (s * (Qx - Rx) - Qy) % p

        # print every calculated points
        print ('\n[2]\nQx =',Qx,'\nQy =',Qy,'\nRx =',Rx,'\nRy =',Ry)

        Qx = Rx
        Qy = Ry
  1. Initialize the variables:
x = Qx
y = Qy
  1. Convert x and y coordinate to hex:
public_key_hex = '04' + format(x, '064x') + format(y, '064x')
  1. Print results:
print ("\nPublic key (uncompressed) =", public_key_hex)

The complete/formated python3 script as follow:

# Define the parameters of the elliptic curve
p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
a = 0
b = 7
Gx = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
Gy = 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8
n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141

# Perform scalar multiplication on the elliptic curve
# Initialize the variables
Qx = Gx
Qy = Gy

private_key = 0x000000000000000000000000000000000000000000000000000000000000000f
print('\nExample = 3a6859fcfc9bb02981897c2d5bcb0ca1ded0c2eac4e89304411709615aea1420')
private_key = int('0x' + input('[Key]: '), 16)

# Perform the scalar multiplication using double-and-add algorithm
for bit in bin(private_key)[3:]:
    # Perform point doubling on the elliptic curve
    # Calculate the slope
    s = (3 * Qx**2 + a) * pow(2 * Qy, -1, p) % p

    # Calculate the new point coordinates
    Rx = (s**2 - 2 * Qx) % p
    Ry = (s * (Qx - Rx) - Qy) % p

    # print every calculated points
        print ('\n[2]\nQx =',Qx,'\nQy =',Qy,'\nRx =',Rx,'\nRy =',Ry)
    
    Qx = Rx
    Qy = Ry

    if bit == '1':
        # Perform point addition on the elliptic curve
        # Calculate the slope
        s = (Qy - Gy) * pow(Qx - Gx, -1, p) % p

        # Calculate the new point coordinates
        Rx = (s**2 - Qx - Gx) % p
        Ry = (s * (Qx - Rx) - Qy) % p
    
        # print every calculated points
        print ('\n[2]\nQx =',Qx,'\nQy =',Qy,'\nRx =',Rx,'\nRy =',Ry)
        

        Qx = Rx
        Qy = Ry

# Initialize the variables
x = Qx
y = Qy

# Convert coordinate to Hex
public_key_hex = '04' + format(x, '064x') + format(y, '064x')

# Print results
print ("\nPublic key (uncompressed) =", public_key_hex)

In the given Python script the main calculation used are as follow:

s = (3 * Qx**2) * pow(Qy*2, -1, p) % p
Rx = (s**2 - Qx*2) % p
Ry = (s * (Qx - Rx) - Qy) % p

s = (Qy - Gy) * pow(Qx - Gx, -1, p) % p
Rx = (s**2 - Qx - Gx) % p
Ry = (s * (Qx - Rx) - Qy) % p

Based on my understanding these calculations are used to derive the public key from the private key. If someone was able to reverse each calculations would they be able to obtain each bits of the binary representation of the private key then concentrate all the bits together then add a 0 or 1 to the beginning of the binary representation then convert it to decimal to obtain the private key?

This is because the Python script converts the private key to binary then removes the first bit then perform the above series of calculations for each bit in order to obtain the public key.

Could someone confirm if my understanding is correct?

Please answers should be base on the body of the question not the topic.