LAB 2 : Generating private, public keys and addresses
Part I : Generating private keys
1. Write the following python code and execute it:
from bitcoin import *
my_private_key = random_key()
print(my_private_key)
This generate random numbers of 256 bits, known as private keys. It consists of 64
hexadecimal digitis, each of 4 bits.
Part II : Generating a pair of keys (private and public)
Public keys are derived from the private keys using the elliptic Curve formula:
public_key=private_key*G,
G is the generator of the Elliptic Curve.
The public key is a point of the curve, with two coordinates X and Y.
2. Write the following python code:
#import bitcoin
from bitcoin import *
#Generate a private key
my_private_key=random_key()
#display the private key
print("Private Key:%s\n" %my_private_key)
#Generate a public key, derived from private key
my_public_key=privtopub(my_private_key)
#display the public key
print("Public Key:%s\n" %my_public_key)
3. Run the code
Notice that the public key is larger than the private key. Why?
Part III: Generating a bitcoin address
4. Write the following code:
#import bitcoin
from bitcoin import *
#Generate a private key
my_private_key=random_key()
#display the private key
print("Private Key:%s\n" %my_private_key)
#Generate a public key, derived from private key
my_public_key=privtopub(my_private_key)
#display the public key
print("Public Key:%s\n" %my_public_key)
#Generate a Bitcoin Address
my_bitcoin_address=pubtoaddr(my_public_key)
#display the Bitcoin address
print("Bitcoin Address Key:%s\n" %my_bitcoin_address)
5. Run the code:
NB: Here the Bitcoin address is encoded in Base58 format, not in hexadecimal
format
6. Copy the same public key and pass it through a SHA-256 hash function, then
a RIPEMD-160 function:
Here is my public key:
Public
Key:04c2c95e5975ec319c82e7f64ead574a013396c55f0ff41070d66db902ed87b61e
f62e714bb3eb5488ae139fea6d19f05b36bcfce33a4c63f3dbb73af096c98db3
I am using the following website for hash functions:
https://www.fileformat.info/tool/hash.htm
- SHA-256(public_key)=
265a72a68d9e1fd642526b05c0522cbf418e4435838ac5ffd5e59c2b794cea7e
- RIPEMD-160(SHA-256(public_key))=
f3ecd1b6bb62b2bc29b502946276d8b50cc37d3d
7. Convert your Bitcoin address from Base58Check to Hexadecimal using the
following website: https://www.better-converter.com/Encoders-
Decoders/Base58Check-to-Hexadecimal-Decoder
8. Compare the result
The highlighted part is the checksum