There has been a tremendous rise in interest in cryptocurrencies over the last couple of years.
Bitcoin is one of many cryptocurrencies in existence today.
In this post I will try to explain at a high-level how Bitcoin works.
WHAT IS A BITCOIN?
Bitcoin is an electronic coin and was first explained by Satoshi Nakamoto in 2008 in a whitepaper Bitcoin: A Peer-to-Peer Electronic Cash System.
A Bitcoin itself is not a discrete thing but a chain of transactions.
If you own 2 Bitcoins than that means that it can be mathematically proven that at some point in time your balance of BTCs was updated to 2 BTCs and you have not spent them yet.
This is analogous to a traditional electronic bank transfer. When you transfer money between accounts than no physical transfer of money takes place but the balances are updated.
However, unlike a bank there is no 3rd party per se that is trusted by both the sender and the receiver. Bitcoin network uses cryptography to implement this trust.
OVERVIEW OF CRYPTOGRAPHY
Cryptography is used to send encrypted or hidden messages over a network e.g. Internet.
Asymmetric cryptography depends on a pair of keys (long passwords that are impossible to guess) : private and a public key. A public key can be shared over the network but private key is only known to the owner. A message encrypted using a public key can ONLY be decrypted using the corresponding private key and vice versa.
Hashing is a mathematical algorithm to reduce a message into a short and fixed length alphanumeric string called hash code. If the original message is altered even a bit it will result in a different hash code. If the has code of the original message and the received message match than it can be safely determined that the message was not tampered with.
Now by using cryptography if the sender of the original message encrypts this hash code then the receiver can decrypt it using the senders public key. If it decrypts than it can be safely assumed that message was sent by the original sender.
A wallet is used to generate an address where you or others can transfer bitcoins. A wallet will let you create as many addresses as you want. An address contains a public-private key pair.
An address of a wallet is represented by the public part of the key pair.
Coin transfers and trust checking happens over the Bitcoin network. The network consists of nodes. Every BTC wallet is a node on the Bitcoin network.
Besides wallets there are nodes called the miners. These miners have the computational power to process transfer of a pool or block of Bitcoins. Once these miners process the transactions they add the block to an electronic ledger that stores all the transactions that have taken place on Bitcoin network. This ledger system is called Blockchain and ensures that a wallet can not spend the same coin twice: double-spend problem.
WHAT HAPPENS DURING A BITCOIN TRANSACTION?
Lets say Alice's Bitcoin balance is 12 BTCs and she wants to send 2 BTC to Bob.
A message is broadcast to the Bitcoin network. The message is signed using Alice's private key so that she cannot later deny the transaction. The message also can have multiple inputs and exactly 2 outputs.
Inputs contain all the transactions to Alice that resulted in her having a balance of 12 BTC. These transactions are further preceded by the previous transactions and so on. The idea is that if you know all the transactions then you can determine the balance in a particular wallet.
The 2 outputs contains intent to transfer 2 BTC to Bob's wallet address and the transfer of balance (10 BTC) back to Alice. In case of mining fees e.g. 1 BTC, balance back to Alice will be 9.
Miners to work: Miners pool all the transactions at a particular point in time into something that is called a block.
Each block has an associated mathematical puzzle that miners need to solve. The puzzle is mathematically very difficult to solve and takes a lot of computing power to solve.
The miner who can solve the puzzle first gets the reward/fee.
Once a puzzle is solved the block is sent to all the miner nodes for verification. Once verified the miners add the block to their copy of the blockchain.
Bitcoin network has a way to synchronize all copies of Blockchain.
It is important to note that all of the blocks in a blockchain are time stamped and linked to the previous and next transaction and hence forming a chain. The time stamp is important in order to ensure there is no double spending of coins.
The work involved to get a right to add the block to the blockchain is key to maintain the integrity of blockchain. If anyone tampers with a block on the blockchain then they have to do this work for every block that follows. Due to the amount of computation and time required to solve the puzzle or show proof of work a hacker's copy of blockchain will never be able to catch up with other copies on the network.
Bitcoin is the first electronic coin that provides a way to transfer money without requiring a central trusted party.
Showing proof of work as a requisite to update blockchain is the most secure way of adding transactions to the block chain since it leverages cryptography algorithms that have been in use and vetted for a very long time.
Proof of work is however very time consuming and other electronic coins use faster methods like proof of stake or existence. But, it is important to note that these use algorithms that are very new and not fully vetted.