ELI12 crypto - part 3

Blocks

In the last newsletter, we talked about how hash functions work and some of the benefits. We’ll mainly use them to confirm data hasn’t changed — either accidentally or maliciously — at some point in its life. Remember, a single character change will make a hash wildly different.

So we’re starting to build up an understanding of the foundational technologies of blockchains.

Visit https://andersbrownworth.com/blockchain/block. You’ll see something like this, and don’t worry if your values are different or the box is red.

This widget lives a primitive existence, governed by one rule: it’s happy (green) when the hash starts with four ‘0’ and angry (red) when it doesn’t. It calculates its hash by using the values in the block, nonce, and data fields together.

Go ahead and type in the Data field “Alice pays Bob 10.00”.

As soon as we start typing, the widget angers, since the hash isn’t starting with ‘0000’.

Knowing what you now know about hashes — that they change with each new character entered — what can we do to try to get the hash to start with ‘0000’?

We could add extra characters into the Data field in the hopes that, eventually, a hash will happen to start with our four leading zeros.

That’s kind of working. I have one leading zero, but that’s not four, plus I now have extra garbage in my data. And it took some time, with me adding an ‘a’, eyeballing the hash, rinse, repeat. So that’s not great. How do we solve this puzzle, where the only way to get a new hash is to change something but we don’t want to change the data itself? We need something else we can change.

Thus, the Nonce. The nonce’s sole purpose for existing here is to give us something we can change so that the hash will change as we seek out our ‘0000’ prefix.

[If you typed extra ‘A’s in your data like me, remove them back out so we just have the Alice paying Bob line]

Click the Mine button.

Green! The hash has our leading ‘0000’! Balance has been restored to the simple widget’s universe.

What did Mine do? Simple. It set the nonce to 1, checked to see if the hash starts with ‘0000’, and, if not, incremented nonce to 2. Then repeats that in a loop, incrementing nonce each time. In our example, the “miner” had to try 75,394 different nonce values before we finally achieved our goal.

A few takeaways:

  1. When you clicked Mine, it probably took a few seconds, depending on how fast your computer is. But if you feed the data back to the block (block=1,nonce=75394,data=”Alice pays Bob 10.00”), the hash is computed instantly. We’d call this Hard to calculate, easy to verify.

  2. This nonce-changing trick is why you hear about bitcoin consuming the electricity of a country or whatever. Bitcoin miners are doing the same thing (change the nonce, look at hash, repeat), only the problem is much, much harder; an acceptable bitcoin solution needs many more leading ‘0’s than our four, so they’re extremely rare.

The “leading zeros” notion is part of what’s known as the Proof of Work (or PoW) system, which we’ll talk about more in a future edition.

In the next edition, we’ll “chain” these “blocks” together. 😉

One last note: if you’re seeing different results than the screenshot, it might be because I didn’t hit enter after typing “Alice pays Bob 10.00”. Remember, in a hash, each character matters, even the hidden return character.