The Maths Behind the Bitcoin Blockchain (Part 2)

In the previous article I explored how Elliptical Curve Cryptography (ECC) is used within the bitcoin protocol in order to generate public/private key pairs. In this next part of ‘The Maths Behind the Bitcoin Blockchain’ we’ll explore how bitcoin addresses are created and tackle a common misconception, that bitcoin address = bitcoin wallet. In the next piece in the series we’ll explore more about wallet structures and uses as well as maths behind bitcoin transactions.

Recap

Let’s first recall that the use of ECC on the bitcoin curve secp256k1 allowed us to generate a private key from a known generator point g and an end point k (public key), where the private key was the number of point multiplications which were required to transform g to k (for a full explanation of this please refer to the previous article).

What is important to note here is that the output is two keys, each with a length of 64 characters (256 bits) and in hexadecimal format. As such the permitted characters are letters A-F and numbers 0–9. An example private key is therefore:

5Kb8kLf9zgWQnogidDA76MzPL6TsZZY36hWXMssSzNydYXYB9KF

An an example public key is therefore:

02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc

However this is often where the confusion begins. Many people often conflate the term public key with bitcoin address and even more conflate the terms ‘address’ and ‘wallet’. Let’s first dig into how this public key becomes a bitcoin address…

Bitcoin Address Generation

Step 1. Perform SHA-256 on the public key

Step 2. Perform RIPEMD-160 on this result

Step 3. Extend with version byte

Step 4. Re-perform SHA-256 on the result

Step 5. Perform SHA-256 again (note the first 4 bytes of the result are referred to as the ‘check sum’

Step 6. Take the check sum and add it to the end of the result from step 3. This is the 25 bit binary bitcoin address

Step 7. Convert from a byte strong to base58

Let’s break this down…

SHA-256

SHA-256 is an algorithm which takes an input of any length and hashes it to produce a result of fixed length — 256 bits. Importantly this is a one-way function, therefore it is not possible to ‘decrypt’ the result and obtain the original input.

If I use an input of ‘taralovesbitcoin’ through SHA-256 the result is:

55F4DF78531BF106C7732E652B35BBA8954F5FE3E062423825F13A969E472658

However if I use an input of ‘taralovesbitcoins’, the result is:

77BBEC1BC0A3CD2F16D6B3E718F62C8073A5EE89F634D443043D2E35E14F43B5

Note that just the addition of an ‘s’ provides a completely distinct result. This is referred to as the ‘Avalanche Effect’ in which a small change to the input, produces a drastic change to the output.

RIPEMD-160

As you may suspect, RIPEMD-160 is another algorithm which hashes an input and, perhaps unsurprisingly given its name, produces a hash of length 160 bits.

Given the input ‘taralovesbitcoin’ the output using RIPEMD-160 is:

98F1CEC950AF5E04D2A575C6587BE7AEBB9037AD

and again we can see the avalanche effect when the input is slightly altered to ‘taralovesbitcoins

EE64B8841F3AA14B070B5CE4D0A1C0999F1854D7

You may be asking, why the bitcoin protocol uses two cryptographic hash algorithms instead of just one. The reason is that it’s a method of ensuring that, should RIPEMD-160 and bitcoin’s signing algorithm (ECDSA — which I will explore in the next part of this series) misbehave together, SHA-256 adds a further level of security. It’s therefore a braces and belt approach to security.

Version Byte

The version byte is a signal of the bitcoin codebase being used. These are available in hexadecimal or as a leading symbol (alphanumerical) and demonstrate where the address is to be used. For example:

  • An address on the main net will be prefixed with 00 (or 05 if it is a multisig wallet — explanation below)
  • An address for the test net will be prefixed with 6F

Full list here

As such, step 3 mandates that the version byte signifying where and the type of address is added to the front of the result.

Base58

We have already explores the hexadecimal notation for public and private keys, however another method of displaying addresses (and keys) is converting to base58. As such, the characters 0 (zero), O (capital o), I (capital i) and l (lower case L) are not permitted. This is due to how similar they look and thus removes any possibility that the user misreads them. Number plates in the UK employ a similar method to avoid ambiguity.

Worked Example

Now that we’ve pulled out the key terms within the address generation process, let’s re-run with an example public key (taken from here):

0250863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b2352

Step 1. Perform SHA-256 on the public key

0b7c28c9b7290c98d7438e70b3d3f7c848fbd7d1dc194ff83f4f7cc9b1378e98

Step 2. Perform RIPEMD-160 on this result

f54a5851e9372b87810a8e60cdd2e7cfd80b6e31

Step 3. Extend with version byte

(00)f54a5851e9372b87810a8e60cdd2e7cfd80b6e31

Step 4. Re-perform SHA-256 on the result

ad3c854da227c7e99c4abfad4ea41d71311160df2e415e713318c70d67c6b41c

Step 5. Perform SHA-256 again (note the first 4 bytes of the result are referred to as the ‘check sum’

(c7f18fe8)fcbed6396741e58ad259b5cb16b7fd7f041904147ba1dcffabf747fd

Step 6. Take the check sum and add it to the end of the result from step 3. This is the 25 bit binary bitcoin address

(00)f54a5851e9372b87810a8e60cdd2e7cfd80b6e31(c7f18fe8)

Step 7. Convert from a byte strong to base58

1PMycacnJaSqwwJqjawXBErnLsZ7RkXUAs

Address Formats

Each cryptocurrency has a format for the resulting address. Bitcoin addresses must be between 26–35 characters and pre-fixed with a 1, 3 or bc1. It is worth noting there that bitcoin addresses pre-fixed with a ‘3’, denote a multi-sig address (explained later in the series).

In comparison, ether addresses are 40 characters long and pre-fixed with 0x, as such they are not in base58.

Litecoin, which took its core code from the bitcoin protocol has addresses derived in the same manner, however the version byte used will be 30 for the mainnet or 6F for the testnet and the pre-fiex is either 1 or M.

Ripple addresses have 25–25 characters, are within base58 and are pre-fied with r.

Bitcoin cash, a fork from the main bitcoin core code on 1st August 2017, is also in base58 and pre-fixed with a 1, bitcoincash or q.

Number of Possible Addresses

Going back to bitcoin (and also applicable to Litecoin and bitcoin cash), because we are using RIPEMD-160 on SHA-256 we are therefore creating a 160 hash of the 256 bit public key. As such there are

possible addresses which can be created. Whilst this number may feel small, in comparison there are between

atoms in the entire universe! (N.B 2¹⁶⁰ is c. 5 x 2¹²⁸). It’s therefore safe to assume there can be sufficient addresses for all requirements.

The Difference Between Addresses and Wallets

As a recap, a public and private key pair are generated using ECC and we can use a combination of the SHA-256, RIPEMD-160 and base58 encoding to create the address from the public key. As such, the address is a representation of the public key.

This address is provided by crypto users within a transaction in order to receive funds and is the publicly available information within the blockchain. In one of the next articles where we explore transaction information we will dig a little deeper into the information shown in a transaction.

A collection of addresses is referred to as a wallet. These addresses could be connected e.g they may be generated from a seed value, or distinct e.g many unrelated public/private key pairs, however if they are stored together then we refer to them as a wallet.

We will explore this further in the next article where I outline the different structures and types of wallet.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store