The Cryptography behind Bitcoin Addresses
Hello world, in this article we will see how our BTC addresses work! πͺ
In brief, a BTC address is generated using cryptographic algorithms based on elliptic curves. The three main steps to generate a public and unique BTC address are the following:
- Generation of a Private Key
- Generation of the Public Key
- Generation of a Public Address
Let's dive into the details.
Generation of a Private Key
The Private Key (Kpr) is a randomly generated 256-bit number, it should be truly random and unpredictable to ensure security. For instance, this is a valid BTC private key (in hex and binary format):
Generation of the Public Key
The Public Key (Kpub) is derived from the previous Private Key using Elliptic Curve Cryptography (ECC), specifically the secp256k1 curve (click here for more details).
Kpub = Kpr * G, where G is the generator point on the secp256k1 curve. Be aware that the * symbol does not represent the ordinary multiplication between integers, but the elliptic curve multiplication, which is totally different.
There are two different formats for the Public Key:
- Uncompressed (Legacy): includes both the x and y coordinates (i.e., 04 || x || y).
- Compressed: includes only the x coordinate and a prefix to indicate the parity (odd or even) of the y coordinate (i.e., 03 or 02 || x)
This is an example of Public Key derived from the Private Key above described above:
Please note that the Private Key cannot be recovered by starting from the Public Key, the math protects you and it only works one way. This is the reason why the Public Key can be shared with anyone, while the Private Key should be kept secret.
Generation of a Public Address
The last step is the generation of a Public Address which is a user-friendly representation of the Public Key. This involves the following steps:
- Compute the SHA-256 hash of the Public Key
- Compute the RIPEMD-160 hash of the result from the previous step.
- Add a network byte in front of the RIPEMD-160 hash (0x00 for Bitcoin mainnet).
- Compute a double SHA-256 hash of the result from step 3, and take the first 4 bytes of this hash.
- Append the checksum to the end of the extended RIPEMD-160 hash with the network byte.
- Encode the result using Base58Check encoding to produce the final Bitcoin address.
In formulas:
This is an example of Bitcoin Address:
Some interesting resources:
Please share your opinion below and letβs build a supportive and informative community together! π€