Generating a Public Key from a Private Key Using the Elliptic Curve Digital Signature Algorithm
The Elliptic Curve Digital Signature Algorithm (ECDSA), also known as Elliptic Curve Cryptography (ECC), is a widely used cryptographic algorithm for securely transmitting and storing data. In Chapter 4 of Mastering Bitcoin by Andreas M. Antonopoulos, you will learn about the mathematical foundations of ECDSA. One of the basic concepts of ECDSA is generating a public key from a private key.
What are Private Keys?
In cryptography, a private key is a secret key used to decrypt and authenticate messages or signatures. It is like a lock and key that allows only authorized people to access and verify the contents of encrypted data. A private key is usually represented as a large number, known as a private hash, that is unique to each user.
What are public keys?
A public key, on the other hand, is a unique identifier that is associated with a specific account or entity in an e-wallet or network. It is a kind of digital ID that allows users to share their private keys with others without compromising their security. A public key is usually represented as a smaller number, known as a public hash, that can be used for encryption and authentication.
Generating a public key from a private key using ECDSA
To generate a public key from a private key using the Elliptic Curve Digital Signature Algorithm (ECDSA), you need to follow these steps:
- Convert your private key to mathematical representation
: First, you will convert your private key from binary format to an elliptic curve digital signature scheme, such as secp256k1 or secp384r1.
- Calculate Public Exponent and Modulus: Based on the mathematical representation of the private key, you will calculate the public exponent and modulus for the ECDSA signature scheme (usually 2048 bits).
- Calculate Signature: Use the calculated public exponent and modulus to calculate the ECDSA signature from the private key.
Here is a step-by-step example in Python:
import elliptic
Load the secp256k1 curvecurve = ellipticCurve.EllipticCurve('secp256k1')
Convert the private key to a mathematical representation (e.g. secp256k1_point)private_key = elliptic_point.Point([20, 10], 'secp256k1')
public_key = curve.sign(private_key)
print(public_key.x)
In this example, we use Python’s “elliptic” library to load the secp256k1 curve and convert the private key to an elliptic curve digital signature scheme. Then we calculate the public exponent and modulus for the ECDSA signature scheme (in this case 2048 bits) based on the private key.
Verifying Public Keys
To verify the validity of a public key, you can perform the following steps:
- Calculate Signature: Use the calculated public exponent and modulus to calculate the ECDSA signature based on the private key.
- Compare Signature with Expected Signature: Compare the calculated signature with the expected signature associated with the same private key.
In Python:
def verify_public_key(public_key, private_key):
curve = ellipticCurve.EllipticCurve('secp256k1')
public_exponent = 65537
Example value of public exponentmodulus = curve.get_curve_type() 2*256
Example value of modulussignature = curve.sign(private_key, public_exponent, modulus)
expected_signature = elliptic_point.Point([20, 10], 'secp256k1').sign(private_key)
if signature == expected_signature:
return True
else:
return False
In this example we use Python’s “elliptic” library to load the secp256k1 curve and compute an ECDSA signature from the private key. We then compare the computed signature with the expected signature associated with the same private key.