How Bitcoin works?

Bitcoin icon

       Bitcoin, a form of digital or virtual currency, was introduced to the world in 2009 by an enigmatic figure using the pseudonym Satoshi Nakamoto. Unlike traditional currencies such as the Dollar or Euro, Bitcoin lacks a physical form and operates in a decentralized system- free from governmental control or third-party intermediaries. This essay will delve into the intricacies of Bitcoin, how it functions, and how transactions are processed.

Basic Concepts

Hash function

       Collision-resistant, hiding and puzzle-friendly are the security properties of hash function which is used extensively in Bitcoin, below are some examples:

Block Hashing: Each block in the Bitcoin blockchain contains a unique identifier called the block hash. This hash is computed by applying the SHA-256 hash function to the block header, which includes information such as the previous block hash, timestamp, transaction data, and a nonce (a random number used in the mining process). The resulting hash serves as a digital fingerprint of the block and ensures its integrity.

Transaction Verification: SHA-256 is used to verify the integrity of individual transactions. Each transaction has its own hash, which is computed by hashing the transaction data. These transaction hashes are then included in the block, and the block hash is calculated based on all the transactions in the block. This allows participants in the network to verify that the transactions within a block have not been tampered with.

Proof-of-Work (Mining): Bitcoin mining involves solving a computationally intensive mathematical puzzle known as the proof-of-work. Miners compete to find a nonce value that, when combined with the block header, produces a hash that meets certain criteria (e.g., a hash with a certain number of leading zeros). Miners repeatedly hash the block header using SHA-256, changing the nonce value until the desired hash is found. This process requires significant computational power and helps secure the network by making it difficult to manipulate the blockchain’s history.

Address Generation: Bitcoin addresses are derived from public keys using a series of cryptographic operations, including SHA-256. The public key is hashed twice, first using SHA-256 and then using RIPEMD-160 (another hash function), resulting in a shorter address representation. This ensures that each Bitcoin address is unique and provides a level of privacy by not revealing the original public key.

Peer-to-Peer (P2P) Network

       P2P network is a decentralized network which peers can communicate directly with each other without any central servers. In a P2P network, every participating node acts both as a client and a server, allowing for direct sharing of resources, data, and services among the connected peers.

Blockchain

       Blockchain technology is a decentralized and distributed ledger system that securely records and verifies transactions across multiple participants in a network. Originally introduced as the underlying technology for cryptocurrencies like Bitcoin, blockchain has since found applications in various industries beyond finance.

     Distributed ledger is a digital ledger that contains a continuously growing list of transactions, called blocks. These blocks are linked together in a linear and chronological order, forming a chain of blocks. Each participant in the network maintains a copy of the blockchain, creating a distributed ledger that is shared and synchronized across the network.

Consensus

     In Bitcoin, the consensus mechanism used is called Proof of Work (PoW). Consensus mechanisms are protocols or algorithms that enable participants in a blockchain network to agree on the state of the blockchain and validate transactions. Here’s how the Proof of Work consensus mechanism works in Bitcoin:

Mining Nodes: Bitcoin relies on a network of decentralized nodes, also known as miners. Miners are participants who use computational power and specialized hardware to solve complex mathematical puzzles.

Mining Process: Miners compete to find a solution to a mathematical problem, known as the proof-of-work puzzle. In the case of Bitcoin, the puzzle involves finding a nonce (a random number) that, when combined with the block header data, produces a hash value that meets specific criteria. This criteria typically requires the resulting hash to have a certain number of leading zeros.

Difficulty Adjustment: The difficulty of the proof-of-work puzzle is adjusted dynamically by the network every 2,016 blocks (approximately every two weeks) to maintain a consistent block generation time. If miners collectively find blocks faster than the target time, the difficulty increases. Conversely, if blocks are found slower than the target time, the difficulty decreases.

Consensus and Longest Chain Rule: Once a miner finds a valid solution to the puzzle, they broadcast the new block to the network. Other miners then verify the solution and add the block to their local copy of the blockchain. The consensus is achieved by following the “longest chain rule,” where nodes accept the longest valid chain as the authoritative version of the blockchain. This ensures that the majority of the network agrees on the order and validity of transactions.

Incentives: Miners are incentivized to participate in the mining process through block rewards and transaction fees. When a miner successfully mines a new block, they are rewarded with a certain number of newly created bitcoins, which serves as the block reward. Additionally, miners may collect transaction fees associated with the transactions included in the block. These incentives motivate miners to dedicate their computational resources to secure the network and validate transactions.

Transactions routing

     The routing of Bitcoin transactions involves the process of propagating and confirming transactions across the decentralized network of nodes. Firstly, a Bitcoin transaction is initiated when a user creates and signs a transaction using a Bitcoin wallet. The transaction includes information such as the sender’s address, the recipient’s address, the amount being sent, and any transaction fees. Once the transaction is created, it is broadcasted to the Bitcoin network. The sender’s Bitcoin wallet sends the transaction to one or more connected nodes, which then propagate it further. Each node that receives the transaction verifies its validity by checking factors such as whether the sender has sufficient funds and whether the transaction follows the Bitcoin protocol rules. If the transaction is valid, the node relays it to its connected peers in the network. Valid transactions are collected in each node’s mempool (short for “memory pool”). The mempool is a temporary storage area where pending transactions wait to be included in a block by miners. After that, miners, specialized nodes with computational power, collect transactions from the mempool to include them in the next block they are attempting to mine. Miners typically prioritize transactions based on the transaction fees attached to them. Transactions with higher fees have a higher chance of being included in the next block. Once a miner successfully mines a new block, they include a set of transactions from the mempool into that block. The transactions are ordered and recorded in the block’s data structure, forming the transaction history of the blockchain. Then, the miner broadcasts the newly mined block to the network, and other nodes receive and validate the block. If the block is deemed valid, it is added to each node’s local copy of the blockchain. Lastly, subsequent blocks are mined and added to the blockchain, the transaction becomes more secure and confirmed. The number of confirmations indicates the number of blocks that have been added on top of the block containing the transaction. Typically, the more confirmations a transaction has, the more irreversible it becomes.