How Bitcoin Works
Bitcoin ledger fundamentals
- The public ledger (also known as the blockchain) contains all transactions on the network.
- Everyone has access to and/or has a copy of the ledger that they can edit individually (Bitcoin nodes).
- All transaction history must always be known; this is to confirm balances and prevent overspending.
- When one user adds a transaction, it is broadcast to all other ledgers (nodes).
- Bitcoin trusts whichever ledger has the most computational work put into it. This prevents fraudulent ledgers from being accepted.
- Trust is dependent on computational work, and only the longest ledger is trusted. A modified ledger would require an infeasible amount of computational power to generate a valid, longer version.
Transaction verification
- There are public keys and private keys. A private/public key pair can be created nearly an infinite number of times. Anyone can create a private/public key pair.
- Wallet addresses can be derived from the public key as another layer of security and privacy.
- Digital signatures are used to sign transactions to prove that the owner verified that transaction; they are created with the private key.
- Valid signatures depend on a message (the transaction), a (usually sequential) unique ID, and the private key, using the elliptic curve digital signature algorithm.
- The dependence on the private key verifies that only people with access to it can produce that signature.
- The dependence on the message verifies that it cannot be copied to another message.
- The dependence on a unique ID verifies that any message and signature cannot be copied.
- To verify the signature, a function uses the message, signature, and public key to output true or false. A true result verifies that the signature was created with the corresponding private key.
Hash functions
- Mining (or hashing) is based on cryptographic hash functions.
- A hash function can take any string as an input and output another string with a fixed number of bits. The output is called the 'hash' or 'digest'. SHA256's output is 256 bits (32B).
- A hash function's output appears seemingly random and is completely unpredictable; any change to the input will completely change the output. Cryptographic hash functions will always produce the same output for any specific input.
- Cryptographic hash functions are also used to verify file integrity by finding the hash of the entire file. It is virtually impossible to have a different file with the same hash.
- Cryptographic hash functions, such as SHA256, can only find a specific output by guessing every possible input.
Proof of work
- The blockchain (ledger) can be visualized as a chain of blocks, which each contain: a list of transactions, the nonce (a guessed number, also called the 'proof of work'), and the hash of the previous block (to make it depend on the hash of all previous blocks).
- When the entire block is hashed with SHA256, the digest (a 256-bit binary number) must be less than or equal to a chosen number to be valid.
- The chosen number is based on the difficulty. It is chosen automatically every 2016 blocks to maintain an average of 10 minute block times, regardless of total computational power.
- The nonce is also known as the proof of work (PoW), because it proves that the miner who found it performed the computational work of guessing until they found a valid digest.
- Changing any information in a block would invalidate its hash and require rehashing that block, every block after it, and any new blocks being made.
- In order to successfully change a past transaction, you would have to be faster than everyone else combined in order to make your ledger (which is now unique and shorter) the longest again.
- This causes older blocks to take much more effort to change. A block is usually considered to be totally immutable after just a few hours.
Mining (hashing)
- Miners (hashers) operate a node that listens for transactions being broadcast and collects them into a block. If they can find a valid nonce and hash a valid answer in time, they will broadcast the block they made to be verified by others.
- The first transaction in every block goes to the successful miner as the 'block reward' for their computational effort in creating it; this is new currency being created.
Incentive and scarcity
- Every transaction can include a small fee that will go to whoever mines the block with that transaction. This incentivizes miners to include and prioritize your transaction in the next block.
- Bitcoin block rewards started at 50 bitcoins and halves every 210000 blocks, or roughly every 4 years. This event is called the halvening and will continue until there are 21000000 bitcoins in existence, at which point the block rewards will disappear and miners will rely on transaction fees as an incentive.