How Does Bitcoin Work?10 Dec 2016
disclaimer This post aims to introduce the functionality of bitcoin along with a general one for a non-technical reader.
If you want just the technical stuff, skip to the “How” section.
How do we start our transaction? By having alice declare it to the world.
So Alice prepares a message stating, “Alice is sending bitcoin
82303348 to Bob.” She then encrypts this message and sends it out to everyone on the bitcoin network.
Everyone receives this message, including Bob. When bob gets this message, he knows that Alice sent the money, and so does everyone else. As other people on the network hear that message, each adds it to a list of pending transactions that they’ve been told about, but which haven’t yet been approved by the whole network. The approval process starts with other observers on the network.
Let’s say that an observer on the network, David, checks his copy of the list, and can see that each transaction is valid. He would like to help out by broadcasting news of that validity to the entire network.
However, before being able to tell anyone as part of the validation protocol David is required to solve an intentionally hard computational puzzle called the proof-of-work. Without the solution to that puzzle, the rest of the network won’t accept his validation of the transaction.
Without a solution to that puzzle, the rest of the network won’t accept his validation of a transaction.
The puzzle that he has to solve, is something like the following:
Let $h$ be a fixed hash function known to everyone.
note: a hash function is an operation that attempts to produce a unique signature for each possible input. The hash function used in the bitcoin protocol, for example, is SHA-256.
Suppose we add a fixed value $x=0$ to a block of transactions $L$ in a peer’s queue. where $h(L+x) = out$
for simplicity, we’ll use $L$=
hello world, but this would work the same for any $L$ that could be represented numerically. Even say, a number that encodes a bunch of people sending each other money.
h('Hello, world!0') = 1312af178c253f84028d480a6adc1e25e81caa44c749ec81976192e2ec934c64
Now if we set $x=1$ and try that value
h('Hello, world!1') = e9afc424b79e4f6ab42d99c81156d3a17228d6e1eef4139be78e948a9332a7d8
The puzzle solution might be to find a value of $x$ that creates 4 consecutive zeroes at the beginning of the output value.
Note that this is a problem that can take a long time to find the answer to, but can be checked very quickly.
If we keep incrementing $x$ and recalculating the SHA value, we won’t actually find an acceptable nonce for this value of $L$ until we get all the way up to $x=4250$
h('Hello, world!4250') = 0000c3af42fc31103f1fdc0151fa747ff87349a4714df7cc52ea464e12dcd4e9
you can check this on your own machine as well with the
echo -n 'Hello, world!4250' | sha256sum 0000c3af42fc31103f1fdc0151fa747ff87349a4714df7cc52ea464e12dcd4e9
It’s important to remember that this is a hypothetical example, and that instead of requiring leading zeroes, the Bitcoin proof-of-work puzzle requires the hash of a transaction block’s header to be lower than or equal to a number known as the target. This target is automatically adjusted to ensure that a Bitcoin block takes, on average, about ten minutes to validate.
So it would take a computer a while to find the right value for $x$, but once we know the answer, other computers can use this $x$ value to see if it’s right, and if it is we can say that David’s computer has proof of work for this block of transactions $L$. When he has the answer he sends it out to the other computers on the bitcoin network.
Everyone has similar records of all transactions (including the one david’s computer just worked on) that they keep and manage as they hear about them from everyone else, and remember which ones are validated. This /public list of transactions that everyone shares is called the blockchain.
The proof of work puzzle of calculating a nonce ($x$) for a specific block of transactions is called mining. And the protocol gives monetary rewards to those who mine blocks, making the currency more reliable as other people constantly validate your transactions and share that information with everyone else; and eventually getting to Bob! This is why it tends to take a couple minutes for someone to confirm it when bitcoin is send to them.
There’s plenty more to this process than what we’ve gone over here, but I hope this was an easy to understand introduction to bitcoin that ignores most of the fluff. If you’re interested, the blockchain ledger itself is managed through a merkle tree data structure which is the first place to start reading if you want to learn more.