Bitcoin and Blockchain
Bitcoin is a digital decentralized currency that has transformed finance. Because it’s decentralized there’s no government that issues bitcoins, and there’re no banks that manage accounts.
Bitcoin is often used for investing, but it’s life changing for migrant workers.
There are over 250 million international migrants who transfer money to their family back in their home country. This is known as remittance. The problem with remittance is that fees can be as high as 10%, and it takes weeks for the money to reach its destination. Bitcoin allows migrant workers to send money to their families with almost no fees, with transfers taking less than an hour.
Remittance is just one possible application of a Blockchain (the tech underlying Bitcoin). To understand what else is possible with Blockchain this article will explain how it works by showing you how you’d design your own decentralized currency.
Ledgers: Keeping Track of Who Owes Who
Why is making a decentralized currency so hard?
Let’s take a look at a basic example of currency without banks or governments, and some of the problems with it.
If you and your friends exchanged money frequently, like covering the dinner bill for each other, it would be inconvenient to exchange cash all the time. So you might keep track of who owes who on a ledger (a list of financial transactions).
The ledger could be on a website that anybody can see and add to.
If Bezos paid $14 for Elon’s free-trade coffee, then either of them could add “Elon pays Bezos $14” to the ledger.
At the end of the month, you’d all pay cash to the people you owe.
Our ledger rules so far:
- Anybody can add a transaction
- Anybody can view the ledger
- Settle up with cash at the end of the month
Digital Signatures: Controlling Your Own Money
There’s a big problem with our ledger protocol.
“Anybody can add a line” means that Elon could add “Bezos pays Elon $420” even if Bezos never agreed to.
To fix this we can say that whoever is sending money has to add their digital signature to the transaction.
At first glance it might seem like digital signatures wouldn’t work. Couldn’t people just copy your signature?
This is where the first bit of cryptography comes in. Everybody would generate a public / private key pair. Like the name suggests, everybody knows your public key, but only you know your private key.
Handwritten signatures look the same no matter what document you’re signing. Digital signatures are a lot stronger. They’re a long string of 0s and 1s, and changing the message slightly will completely change the signature.
Without diving into the math, signing and verifying transactions is pretty simple. All you need to know is what functions are.
A function is an operation that takes in an input (a sad cat) does something to it (pets it) and then has an output (a happy cat).
To sign a message you give a “sign function” your message and your secret key, and it’ll output a digital signature. Since the signature depends on your secret key, only you can create that signature. And because the signature also depends on the message, nobody can copy your signature and forge it on another message.
To verify a message you give a “verify function” a message, the signature on the message, and the public key of the person who signed it. This function will then indicate whether or not the signature is valid.
An important point to note is that although the public key is used to verify if a signature came from its associated private key, there’s no way to deduce the private key from its public key.
Our new ledger protocol:
- Anybody can add a line with a transaction
- Anybody can view the ledger
- Settle up with cash at the end of the month
- Only signed transactions are valid
Timestamps: Preventing Duplicate Transactions
There’s a small problem with how our ledger and signature system currently works. Since digital signatures only depend on the message and secret key, signature message pairs can be duplicated.
If Bill put on the ledger “Bill owes Elon $420” and signed it, Elon could copy the entire transaction, the message AND the signature, to get more money.
The fix to this is pretty simple. Each transaction has an identifier that increases by one each transaction. The 42nd transaction would then have the identifier “42” attached to the beginning of the message.
Now, if somebody tries to copy a transaction to get more money, the signature will be invalid, because you can’t repeat the identifier “42”, and changing it to “43” will completely change the signature.
Cryptocurrencies: No Overspending!
“Settle up with cash at the end of the month”
What about if somebody doesn’t settle up at the end of the month, and disappears forever?
To prevent somebody from taking advantage of this, we can make a rule that people can buy “Ledger Coin” (our own cryptocurrency) with cash, that all transactions are made in Ledger Coin, and that overspending isn’t allowed.
Now, “settling up” is done automatically, since all transactions are made in Ledger Coin.
At any point in time, you can turn your Ledger Coin into cash, but you can only make transactions with Ledger Coin that you have.
Wait! How can ledger coin have any actual value?
Ledger coin and any other currency has value because people think they do. If people are willing to give cash or services in exchange for your ledger coin then it’s valuable.
Right now the ledger is on a website. If the website goes down or gets hacked, nobody will be able to trade currency. If the website owner changes the rules, then everybody could suffer.
Centralized systems only need one point of failure to be compromised. Decentralizing the ledger solves this problem, by giving everyone a copy of the ledger. Everybody has a copy of the ledger, and is in charge of keeping their own copy up to date.
Now, when Elon wants to pay Bezos $10 for his window, he’ll broadcast the message “Elon pays Bezos $10” to everyone.
There’s a really big problem with this approach. When you see the message “Elon pays You $5” how can you be sure that everybody else got this message, and that you’ll be able to use these Ledger Coins with others? Maybe Elon only sent that message to you, and nobody else.
We need a protocol that tells how to accept or reject transactions and in what order, to ensure that your copy of the ledger will be the same as everybody else’s in the world.
The solution? Trust the ledger with the most computational work put into it.
To do this, all we need to use is something called a cryptographic hash function and some clever tricks.
A cryptographic hash function takes in a message, does some crazy math to it, and outputs a hash (a string of 0s and 1s) with a fixed length.
The most commonly used hash function, SHA256, will turn any message into a string of bits 256 characters long. The output is called a “hash” and it looks random, but it’s not. Every time you give the message “Blockchain” to SHA256 it will output the same hash.
Changing the message going into SHA256 will change the hash in a completely unpredictable way. Importantly, when given a hash there’s no way to figure out what message generated it. If you wanted to create a hash that started with five 0s, you’d have to randomly guess and check messages.
Proof of Work
The reason why SHA256 is useful is because it can be used to prove that a large amount of computational work went into a specific list of transactions.
Imagine someone shows you a list of transactions and says “I found a special number, that when you put it at the end of this transaction, and apply SHA256 to the whole thing, the hash starts with 30 zeros.”
For a random message, the odds that the hash will start with 30 zeros is 1/2³⁰, which is about one in a billion.
Because the hash of a message after applying SHA256 is essentially random, the only way to find a special number that gives a hash with 30 zeros is by guessing and checking. That means the person had to go through about a billion different numbers to find a special one.
To verify that the number is special, you just have to run SHA256 on the list of transactions with the special number at the end, and check that the hash starts with 30 zeroes.
SHA256 allows us to verify that somebody put in a lot of computational work for a specific message, without having to the same work again.
This is called “Proof of Work”.
Block + Chain
Let’s go back to our decentralized ledger.
There might be a lot of potential ledgers being broadcasted. Some might be from honest people, and others fraudulent.
As a rule, we’re only going to trust the ledger with the most computational work put into it. To do this we’ll organize a ledger into blocks. Each block has a list of transactions and a proof of work that makes the hash of the entire block start with a large amount of zeroes.
Just like how a transaction needs a signature to be valid, a block needs a proof of work to be valid.
To form a chain of blocks, the header of each block will contain the hash of the previous block. Now if a criminal tried to change any transaction in a past block, its hash would change, which will make its proof of work invalid and will also change the header of the next block and its hash. This would make all the following blocks have invalid proof of works.
Proof of work makes it computationally infeasible to modify past blocks, making them effectively unalterable. This is known as immutability and is why blockchains are secure for transacting money and are censorship-resistant.
Since each of the blocks are linked tightly to each other, we call this type of ledger a blockchain.
In practice, there’s only a subset of users who compute proof of works. These people are known as miners.
Miners listen for transactions, bundle them into a block, and then start computing a proof of work for their block. If they find a valid proof of work before anyone else they’ll broadcast their block to the entire world. If somebody else creates a valid block before they do, they’ll update their ledger and get started on the next block in the chain.
Computing proof of works for blocks takes a lot of computing power and energy. To incentivise miners, there are block rewards. The creator of any block is rewarded with some Ledger Coin for their work.
This is the only way that new ledger coins are introduced into the blockchain network. There’s no central government that mints new coins. New coins come into being from miners who create blocks.
We now have a full outline for how a decentralized currency system works!
- Anyone can submit a transaction to be put in a block
- Transactions are signed so users can only spend their own money
- Users can only spend money that they actually have
- Proof of work shows that computational work has been put into a block
- Blocks are chained together so that past transactions cannot be modified
- Miners are users who bundle transactions into blocks and find valid proof of works. They’re rewarded with cryptocurrency when they “mine” a block
- Honest users will always trust the longest chain of blocks
If you’d like to play around with Blockchain concepts like hashing or mining, this website has great interactive demos.
Blockchain and Beyond
Blockchains aren’t only limited to Bitcoin. Ethereum is another technology that uses blockchain to allow users to do almost any computation in a decentralized and immutable manner. It allows for decentralized computing, file storage, investing and more.
But that’s a story for another time :)
Thanks for reading!
- Connect on LinkedIn and Twitter
- Check out liamhinzman.com
- Donate crypto 😉 0xA5a8fddcFfa22f59599452aE4FE915Ba4c832FeE
Banking without Banks: Blockchain Simply Explained was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.