Difficulty Explained

I’ve seen several places where bitcoin has been explained, but there is one thing about it that always seems kinda glossed over: mining. I know there have been other explanations on here but I’d like to throw in mine too. It seems either explanations are either too simple, saying that miners “solve a difficult math problem” or are too arcane, talking about such-and-such hash function this, algebraically that etc. So hopefully this will be helpful if you were like me and were trying to wrap your head around the concept of mining bitcoins. I’ll address the basics of
-how they are mined
-what keeps them mined at a limited rate, and
-who sets the difficulty for this “mathematical problem”.

I’m writing this to be kind of a “hands on” approach so you can kinda see what’s going on. For all you technical people out there, please bear with me as i will be simplifying some concepts…Ok, first, i think the biggest misconception about mining bitcoins is that the analogy about computers solving difficult problems, while correct, is misleading.

Mining is more akin to rolling dice than solving problems.

To understand mining, one needs to understand what a hash function is. Put simple, a hash function takes an input and creates a seemingly random output, however the output is consistent every time you perform the function on a given input, and it is very difficult to determine an input, given only the output. What i want you to do right now is open up a calculator on your computer. In the calculator, take the square root of 3. You should get 1.73205080756887729352744634150. Now take the digits from the 5th place after the decimal all the way to the 10th place after the decimal. As you can see, its 508075. Now try that with another prime number, let’s say 11. sqrt(11) = 3.3166247903553998491149327366707, and the digits in place 5 through 10 are: 247903. This is basically a very simple (and weak) hash function. For any given prime number (in this case it has to be prime), we can find a number (the 6 digit resultant output) that seems to have nothing to do with it, but can be consistently calculated easily. If i were to give you the output 512754, what would be the input? If i didn’t tell you it was 13, you would have to resort to trial and error to find it.

For the next example, we are going to use a different hash function, the SHA-256. This is the actual function used for mining bitcoin. This function doesn’t need a prime number as its input; it can use basically any text. Ok now for some hands-on. Open chrome/safari/IE/Firefox and search Google/Bing/yahoo for “sha-256 calculator” and click on probably the first hit. For me, it was http://www.xorbin.com/tools/sha256-hash-calculator but any result should work and yield the same results.

There should be a text box. In that text box type “pay me, joe, 25 bitcoins. 000001” without the quotes, then press calculate. You should get 4699f0e443b7… as your result. Now, anyone can do that. Why on earth would anyone give you bitcoins for that? There’s nothing special about that output, it’s just a seemingly random string of numbers, right? Well how can we prove that you have done work, and therefore give “scarcity” to the output? We can agree to only accept outputs that start with a zero! Ok so now what i want you to do are change that last digit in your input (the 000001) and make it a 000002, and then recalculate. Keep going until your output starts with a zero. I got all the way to 000013. Now my output is 0ac980…. tada! Scarcity, and therefore, “proof of work”. I would just like to add here that obviously a bitcoin miner doesn’t use the words “pay me, joe…”, it uses computer code, and an receiving address, not a person’s name, but the gist is the same. Other data is also included in this hash input such as transactions, a timestamp, and, importantly, a reference to the previous solution so you can’t just reuse solutions. Transactions are a topic for another day.

Since no one knows how to take a given output to create a given input with this hash function, we can prove that it took some work to get an output that starts with a 0. “well that’s dumb” you may say “computers can calculate stuff like that super-duper fast,” you continue, “why, I’ll be rich within a day or so.” Easy slugger, bitcoin hash outputs need to start with about 14 zeroes at the time of this writing in order to be accepted by the network as a solution. When your computer finally does manage to find a solution, it sends the hash input that it found to other computers, which quickly check the hash output, and if it does, in fact, start with the required number of zeroes, it is relayed to other computers and so on, so forth, until it is propagated around the entire network. All computers now honor your address with 25 shiny new bitcoins. Also, no one can change “joe” to “bob” because that would change the entire hash output.

That number of zeroes that the output has to start with is known as the “difficulty.” right now, the entire network of miners makes about 30 trillion(!) attempts at this solution every SECOND. You can see the “hash rate” at sites like bitcoincharts.com. A solution (which yields 25 bitcoins to the finder) is found approximately every 10 minutes.

Ok, so where’s that jerk that’s making the difficulty so high!? I’d like to give him a piece of my mind! The difficulty is set by all bitcoin users. On an individual basis, (not by some consensus) each bitcoin user calculates the difficulty of solutions that they will accept and relay to other nodes. That doesn’t mean that different nodes will accept different difficulties. Each one calculates (and arrives at the exact same conclusion) the difficulty by checking the timestamp on the most recent solution it has received, and comparing it with the timestamp on a solution that came 2016 solutions before the current one. (There are 2016 10-minute periods in 2 weeks) The amount of time that has elapsed SHOULD be two weeks, because solutions ought to be coming in about every 10 minutes. If the elapsed time is, say, one week, it means the difficulty is too easy, and solutions are pouring in too fast, and the new difficulty will be TWICE that of the previous difficulty so that the next solutions pour in once about every 10 minutes, and 2016 solutions will occur over the next 2 week period.

And there you have it, predictable scarcity. The one quality that a unit of currency or barter MUST have to make it worth something, or at least a placeholder for something that is worth something.