Bitcoin, a Decentralized Digital Currency. Before diving into how does Bitcoin work? You are advised to read through "What is Bitcoin?" Which provides you with a synopsis and a short explanation of what is bitcoin and about its network.
You might have heard that no government issues it and no banks are required to manage it or control it, neither to verify its transactions. And astonishingly we don't even know who exactly was 'Satoshi', who invented it. Thus, we can walk through various questions to get an understanding of that (this explanation wont include high level technical details which require a deeper explanatory write-up).
Lets start by taking an example approach, where you are transacting with your friends and need to keep track of the payments.
So, lets consider you have been using a Ledger to keep track of your payments and each transaction with your friends Charlie and Bob. At first the ledger and task of updating it depended completely on trust, but at some moment you decide that you need to make this process trust-less. So you bring in some "Cryptography" or some sort of code to balance and circumvent that element of 'Trust'. Now rather than trusting humans, you set a code which manages that trust part for you. This gives rise to the model similar to what Bitcoin uses, Trustless Model (no central authority or trust required between two parties exchanging value with each other).
Satoshi designed the first cryptocurrency and still the only one today which relies completely on a Decentralized Trustless Model. Technically, to use Bitcoin, you don't need to know what exactly happens at each point of the system, similar to the banking system when you use your bank card to transact. As a digital payment network, the user friendliness mainly depends on the surface layer applications and services (such as Mobile/Computer applications for wallets etc.).
But what makes Bitcoin different than the banking system then? The backbone of the Bitcoin Network is not a central authority or government elected group of people. It's a very clever decentralized trustless verification system utilising Mathematics. These elements of mathematics are part of the field of Cryptography. Mathematics is universal, and unchangeable by human decisions. 1+1=2 and its agreed upon universally, no-one can change that.
Let's begin with two things first, Ledgers and Digital Signatures.
Assuming you have four friends, who share resources, go out to dinner or exchange goods with each other, tracking each and every transaction among each of the four can be tiring. Imagine, you gave your friend Bob 10€, and now he owes you that 10€. But rather than paying you back, he asks Charlie to pay you 10€, which Charlie owes Bob or he asks Alice to pay you 10€ because Alice owes 20€ to Charlie. This gets confusing and difficult to track. So you start keeping a written record of transactions called a 'Ledger'. This ledger is open to all and records the total transactions for the month, and at the end of the month, you all tally the total paid or total owed and settle the final amounts. So this follows two main rules:
Anyone can add a transactions (new line) to the ledger.
And at the end of every month you need to settle all the transactions.
But the problem faced here is how do you prevent anyone from adding false transactions (false lines) to this ledger. How do you stop Alice to go adding that "Bob owes Alice 50€", without Bob knowing or approving that it's true or not? You need to trust that all people involved will be truthful and add only the real transactions or records to it.
To solve this issue of trust, we use something called 'Digital Signatures' (based on Cryptography). Rather than hand signatures, the digital signatures here should be able to highlight that Bob has seen the newly added transaction and he approves of it. And doing so should be seamless and it should be infeasible for anyone to forge the signatures.
But cant a digital signature be copied? It's digital right? Cant it be simply read by a computer program which can copy and create an exactly the same version of it? How do you prevent the forging of these signatures?
This is solved by the way of 'Public' and 'Private' keys. The private key is sort of a secret key. Thus, the private key is kept secret and only accessible by the rightful owner of it.
In the physical world, all your hand-signed signatures look the same no matter the relation to the actual message it is being used for. In the digital world, digital signatures are much stronger - they are a function of the 'message' and 'your private key', which together change the signature characteristics based on the message. A private key ensures that no matter the message, only you can produce a signature that corresponds to that key. And the signature has the bits and pieces of identifiable characteristics which highlight the use of 'private key' and for which 'message' the signature was generated for. So technically no one can copy one of your signatures and use it on another message.
But how do we verify this message and signature is valid? Here comes the role of the Public key. Public key can only be generated with the use of the Private key. So it recognizes which key it belongs to. So during verification, pairing and using the 'public key' with the 'message' and 'signature' produces indication of 'True' or 'False'. 'True' the message and signature is valid with respect to the private key, or 'False' it is not valid with respect to the private key. With the cryptographic function using 256-bits, it becomes infeasible to be able to copy or generate same secret key (private key).
Now, we know that Alice cant copy signature of Bob and falsify the transactions without his approval. But what she still can do is copy the same line of message, with same signature and add it to the ledger. So to solve this, the system needs to include a 'Unique ID' which identifies that transaction. Even if the transaction is copied, the unique ID can be used to verify which is the true transaction.
So now we have solved the problem of legitimacy of each entry into the ledger. But what happens if Charlie racks up a lot of debt and at the end of the month refuses to pay anyone? As the final settlement depends on settlement in cash. Maybe we can devise a way to prevent people from spending more than they take in? Then we don't have to settle everything in cash.
Let's make everyone add 50€ or 100€ to the pot. Assuming everyone added 100€. So we create entries into the ledger saying - Everyone gets 100€ to spend.
No overspending is allowed. As they all gave in 100€, the maximum they can spend is 100€.
What Bitcoin due to cryptography introduced is maintaining this ledger in an optimal way, with verification of signatures, no overspending etc. Thus, removing any dependability on cash. As more and more users use this ledger, they can live their whole lives without cash with only using this ledger. Using the terminology of Bitcoin, this will be referred to as 'Ledger Currency = bitcoin' and even it is also divisible up to 100 millionth units called 'satoshis'. Satoshi's are to bitcoin, what cents are to euros. 1 Bitcoin = 100000000 Satoshi.
In today's world, you can exchange a bitcoin for dollars, euros etc. It works similar to exchanging euros to dollars or vice versa. bitcoin is its own independent currency running on its own 'Decentralized Ledger' called 'Bitcoin' network.
If Bitcoin is decentralized then how does it manage to add new transactions to its ledger without trust? Wouldn't maintaining the ledger require a central location or authority to be trusted?
Bitcoin manages this by letting everyone their own personal copy of the ledger. If Alice pays Bob 100€, you broadcast the transaction to everyone and they update their ledger. But inherently this makes it complicated and difficult to manage and making everyone believe it's a true transaction or records it in the same order. So how does Bitcoin manages this so efficiently?
Bitcoin offers a solution via introducing the policy that whichever ledger has the most computational work put into it. This is called 'Hash Function', which involves computational work (solving mathematical equations) as the basis of keeping the ledgers non-conflicting and saved from any fraud. This is also famously called 'Proof of Work' concept. This is the heart of Bitcoin.
In simple words, a 'Hash Function' is a mathematical equation which takes in any message, and gives out a string of bits (random group of 0s and 1s - as this is computer's language). The output is often called as 'hash' or 'digest'. It looks random but it's not random, as it always gives same output for a specific given input. Even if you change one character in a message, it will give out a completely different output. These output changes are highly unpredictable. And in cryptography, it is infeasible to compute/calculate in the reverse direction. The only way to find the input from the output is by 'Guess and Check', which requires an extensive amount of work. Bitcoin uses something called 'SHA-256' which also forms the heart of many security protocols worldwide.
To find a correct hash to match with the ledger, it will require an extensive amount of work, thus coining the term 'Proof of Work'. So going back to the example of the 4 friends, using Bitcoin as ledger. It works by dividing the transactions into 'Blocks' (smaller groups of transactions). These blocks fit together with the respective 'hash' generated with 'Proof of Work', only then it is a valid block. Each next block contains the hash of previous block as its header (to mark a proper sequence for those blocks). And if you change any signature, or transaction in one block, you will change the whole hash sent to the next block (for its header), and which will change that for the next block as well and so on. Which will require redoing all of the work. This chain of blocks is called a 'Block-Chain'.
The way Bitcoin is designed, it allows anyone to do the work of adding new blocks and helping update the ledger. To reward the new block creator for all their work, Satoshi introduced a special transaction that gets added to the ledger with each block, giving the worker some bitcoin (for example 10€ for each new block). This is called the 'Block Reward'. This was a clever inclusion by Satoshi, to bootstrap millions into using Bitcoin and competing with others for this reward (as the value of Bitcoin increases due to its use. This works more like a lottery than a formulated method to ensure finding the blocks. And to get their transactions included into the ledger, everyone pays the worker a small fee. These workers are known as 'Miners'. The design of Bitcoin reduces the block reward after every 210,000 blocks (or roughly 4 years). As more and more users come in the whole reward market shifts to only fees.
As everyone is maintaining their own copy of the blocks, and miners are working non-stop trying to find best solutions to the hash function, updating their own ledger at the same time. They offer their ledgers as records to be updated to offer, and in case of conflicting ledgers (chains of blocks), the longest one is considered a valid one. Everyone gives preference to the chain which has more work put into it. Thus, removing any central authority and trust is only put into the 'Computational Work'. If Charlie tries to send Alice wrong information about the blocks (to fool him), Alice can verify it by comparing his chain, the chain information sent by Charlie and chain information sent by others. So Alice can reject the information sent by Charlie. The probability of Charlie winning the block finding lottery every time are tremendously low (as everyone else is also working to get their blocks added). Charlie will need to own more than 50% computational power than whole network (51% attack) to increase his chances before Alice finds out he cheating. At that point Alice and others can decide to reject Charlie's chain and continue on a trusted one they have been following. As more and more miners (workers) are on the network and competing with each other, more strong the network grows. Miners who aren't winning the lottery most of the time are also providing security to the whole network by controlling a part of the total computing power which can be misused by a bad actor. They are keeping the network safe.
Bitcoin adds new blocks roughly every 10 minutes, which creates only one winner of this lottery every 10 minutes. Other than miners, there are users who just manage a complete copy of the ledger and the Bitcoin software, which checks if the blocks the provided by the miners are following the consensus and don't include any fraudulent transactions. These are called 'Nodes'. The node runners and the users are the ones who control the network. Miners are workers who work for the nodes. With a low Bitcoin block size, the chain grows in a known and economical way and thus reducing the difficulty of entry for the users who want to add more nodes to keep the network decentralized.