Bitcoin under the hood

Prathmesh Deshpande
6 min readApr 19, 2021

Introduction

I wrote a brief overview on Bitcoin in my recent post. In this article, I am going to put the working of Bitcoin Blockchain in more detail. I have referred to the white paper by Satoshi Nakamoto that explains the original concept.

Photo by Dmitry Demidko on Unsplash

Before diving into blockchains and bitcoins, let’s first see a few important concepts.

  1. Public-key cryptography

Definition:

“Public-key cryptography, or asymmetric cryptography, is a cryptographic system which uses pairs of keys: public keys (which may be known to others), and private keys (which may never be known by any except the owner). The generation of such key pairs depends on cryptographic algorithms which are based on mathematical problems termed one-way functions. Effective security requires keeping the private key private; the public key can be openly distributed without compromising security.” (From Wikipedia)

So here’s how public-key cryptography algorithms work:

Let’s consider a scenario in which A wants to send a message to B.

In public-key cryptography algorithms, Both A and B have two keys, a public key and a private key.

When A wants to send an encrypted message to B, A will use public key of B to encrypt and send the encrypted message to B. B will then use their private key to decrypt and read the message.

The security is ensured from the fact that, A message encrypted with B’s public key, can only be decrypted using B’s private key. Hence as long as the private key of the receiver is not compromised, the encrypted message is nearly impossible to decrypt, even if it is intercepted.

2. Digital Signature

This is an algorithm used to prove that a particular message was signed by a particular user.

The way this works is you use your private key to sign the message and then broadcast the original message along with the signed message to other users. Now the other users can verify that you signed the message by using your public key and the original message.

The way public-key cryptography works ensures that the encrypted message upon decryption using sender’s public key, will result in the original message only if the encrypted message was signed using sender’s private key.

Hence if the original message is obtained by receivers after decryption, they can be maximally sure that private-key owner signed the original message.

By FlippyFlink — Combined changed the image https://en.wikipedia.org/wiki/File:Public_key_encryption.svg from encryption to signing., CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=78867393

3. One-way hash function

A one-way hash is essentially a computation that can be done easily in one direction, but is very hard to reverse.

For example, if you multiply two arbitrarily large prime numbers, it takes only seconds to perform the multiplication, but if you are given a number that is a product of two arbitrarily large prime numbers, finding the two factors takes exponential time.

So if you have a encryption function that uses product of random, large prime numbers as a key to encrypt the data, decrypting the data will be very hard. Hence the output can be considered to be a secure one-way hash.

Bitcoin

A bitcoin blockchain (also called ‘ledger’) is a collection of sequential transactions of bitcoins, secured by one-way hashes.

The ledger consists records of transactions between different people(also called nodes) on the bitcoin network.

Lets say the ledger has an entry

  1. X pays Y → 50 BTC

Now there are essentially two things we need to verify with respect to the above entry

a. Does X have 50 BTC?

b. Is it really X saying that they have paid Y or is Y dishonest

The first verification is done by checking all previous transactions (in the network) and calculating that, at the time when X made the entry, if X had more than or equal to 50 BTC.

The second part is verified using digital signature (See above section for explanation). X adds a signature to the entry and broadcasts this to other users (called nodes), all nodes can the independently verify the signature using X’s public key.

There’s an additional core part of the entry in the ledger called the timestamp server. This is timestamp of the entry of the transaction which is also used as an input in the hashing function to ensure all entries are unique. Every new timestamp uses previous timestamp hash and hence reinforces the previous entries. This is analogous to a primary key in the relational databases. This prevents Y from copying the whole entry multiple times and adding fraudulent transactions. Without this unique key, once X signs the transaction, Y can copy the whole transaction many times because the Digital Signature verification will always pass for the same input.

Another important aspect of the bitcoin blockchain is it’s decentralized nature. So that raises the question, Who will maintain and update the ledger? If it is a single node or an entity, the system essentially reduces to traditional banking systems.

So in a bitcoin network, every node maintains it’s own copy of a ledger and listens for new transactions to add into it. Now this runs into a famous computer science problem known as the ‘Byzantine Generals Problem’.

The problem can be thought of as follows:

Consider we have 4 nodes in our Bitcoin Network, and let’s say node 1 listens to a transaction “X pays Y → 50 BTC “, how can node 1 be sure that all other nodes received the same transaction, in short how can node 1 decide to trust the data it listens to?

This is the main problem of the decentralized currency which the bitcoin algorithm solves.

The solution proposed in the algorithm is called “Proof of work”. This essentially consists of solving a mathematical puzzle based on cryptography.

In simple terms, it consists of finding a number which when used as an input with all entries in the ledger, gives a hash with consisting of n zero bits. Finding such a number has no optimized algorithm. Hence the node which finds such a number actually find the number by brute-force method. When a node finds this number, it broadcasts this to all other nodes. Now in case two nodes find the number and have different transaction records, the algorithm dictates that, the chain with more proof-of-work should be considered valid. In the Bitcoin network, the n is decided such that a new block is created every 10 minutes.

In exchange of using the computational power of finding this number, the node is rewarded with a predetermined number of BTC. This is called bitcoin mining and it is essentially how Bitcoins come into existence. If you look at the first ever bitcoin transaction, the ledger has no transactions, and there is only one record — a reward to the miner.

Now how does this help our node 1?

If the node gets the transaction “X pays Y → 50 BTCwith proof-of-work attached with it, it can be sure that all other nodes will also figure out this to be the longest current chain and use the same data as node 1. This process of validating transactions is also called consensus.

The above example with only one entry is very simplistic. In an actual bitcoin chain, there are about 2400 transactions recorded in a ledger, before it is considered “full”. When this limit is reached, a new ledger can be added in order to accommodate more transactions.

Lastly, how can new blocks be generated?

Well all nodes have access to unconfirmed transactions, which are essentially transactions requested but not yet verified by consensus. So each node can pick up these transactions and create new blocks by calculating the proof-of-work and broadcasting their blocks. In order to ensure that changing a previous block is very difficult, each new block uses the previous block’s one-way hash in it’s header, hence chaining the next block to the previous one. As we have seen before, in case of conflicts about which chain to select as valid, the chain with more proof-of-work is always selected.

Conclusion

So there you have it! You now have a better understanding of the core concepts, Public-key cryptography, Digital Signature, Proof-of-work, ledgers ,blockchain and an overview of what actually happens in a bitcoin network under the hood. There is still a lot to explore like Merkle Trees, Forks, 51% Attack, etc. I’ll leave that up to you.

As always I’ll leave some resources as well as the link to the white paper down below.

Until next time!

Reference Links:

  1. The best video on the subject — Thanks a lot Grant Sanderson :)
  2. Original white paper

--

--

Prathmesh Deshpande

Computer Science Graduate. Birds+Wildlife nerd. Passionate Photographer. I am more active here: https://prathmesh6.substack.com/ ✉: prathu10@gmail.com