Mining Hashes and Nonces - the evolution of Cryptocurrency

in #bitcoin7 years ago (edited)

If I have seen further it is by standing on the shoulders of Giants.

Isaac Newtown

To Satoshi (the author of the first blockchain paper), it was so important to credit the ideas of those before him, he chased down key references to ensure they were appropriately credited. Read his email exchange with Wei Dai as an example.

So to learn about blockchain, I thought we should try to learn as Satoshi did - by going through the ideas that influenced his paper.

Wei Dai and the Distributed Ledger

Wei Dai (a computer engineer) wrote one of the first papers on cryptocurrency in 1998 and shared it with fellow cypherpunks.

He wanted to solve a pretty common problem. Imagine this - Bob is selling his house to Alice and Alice wants to pay by cheque. Does that sound like a good idea?

Only Alice's bank knows if she can afford it and even if she could, there's nothing stopping her from spending the money before the cheque is processed (the "double spending" problem).

Companies have invented various ways to get around this. If Alice paid by credit card, Bob would need a machine that can communicate back and forth with Alice's bank to confirm if the money is there, then the bank would need to mark the money as unavailable and Bob and Alice's bank would sort it out at a later time. It's a clumsy solution and hardly secure; credit card details are public and both fraud and data breaches happen all the time.

Wei Dai proposed a simpler solution with "b-money". He suggested we "broadcast" all of our transactions to a shared record.

Quoting from Wei Dai's paper:

If Alice (owner of pseudonym K_A) wishes to transfer X units of money to Bob (owner of pseudonym K_B), she broadcasts the message "I give X units of money to K_B" signed by K_A.
Upon the broadcast of this message, everyone debits K_A's account by X units and credits K_B's account by X units, unless this would create a negative balance in K_A's account in which case the message is ignored.

Of course Wei Dai also suggested a lot more too. Nevertheless as an MTV gen kid I'd like to take a break and flick on to our next channel (hashes). I'm pretty impressed so far and think I've got the basic sense of a distributed ledger.

Hashes

What's a hash? It's kinda of fun - so let's create one .

Go to this website and type in anything you want. No matter what you enter into the input field, it should produce a "hash" in the SHA 256 output with the same number of characters. You can type one word or throw in Dostoevsky's the Brothers Karamazov (which I will finish one day...) and somehow both will get a unique code of the same length. Super cool.

Now watch as this hash tool can help us check if a ledger is legit (verification).

InputHash (SHA 256)
Alice sent X to Bob5a6ec75ed27 33221e5333 893f698199 3fd6c1a92 a7c53f76e344d8215a98eb25
Alice sent X to Bob, Bob sent X to Jefff935b504333 33148c452d 05c3f85004 bb4136cfc 71baee4e45b84af4a8bfce76
Alice sent X to Bob, Bob sent X to Jeff, Jeff sent X to Sarah6b4d1b63c27 3c5f7fac31 651b2d18ad 58e885702 36b7d8b7160df65dc0d1233a

If I wanted to sneak in a small change to the the left column, I'll also need to change the right column. You see how the right column serves as a neat way of recording the chain of all transactions?

Summing this up in are three quotes from one of the papers Satoshi referenced.

Improving the Efficiency and Reliability of Digital Time-Stamping:

Causality fixes events in time. If an event was determined by certain earlier events, and determines certain subsequent events, then the event is sandwiched securely into its place in history

The principal tool we use in specifying digital time-stamping schemes... is the idea of a cryptographic hash function. This is a function compressing digital documents of arbitrary length to bit-strings of a fixed length, for which it is computationally infeasible to find two different documents that are mapped by the function to the same hash value

Suppose we combine the hash values of two users’ documents into one new hash value, and publicize only the combined hash value.... Either participant, by saving his or her own document as well as the other contributing hash value, can later establish that the document existed before the time when the combined hash value was publicized.

Again it gets more complex and Satoshi referenced four papers that contributed to his knowledge of hashes and timestamps. But again, for now, we're going to flick to the next channel and revisit it later when we're ready. The key takeaway is that hashes provide a neat record that are fixed in length (no matter how long the input is).

Adam Back, Hashcash and the Nonce

We all hate email spam. Adam Back came up with an idea to solve it.

Suppose everyone had to spend a bit of time solving a problem by trial and error before they could send an email; people would sent fewer emails right? The solution to the problem 'proves' that the sender cared enough to invest time and effort. Advertisers would need to take a sniper approach to email targeting vs. a machine gun approach (spamming everyone).

With cryptocurrency, we can use this concept known as a 'proof of work' to stop dodgy people sending fake transactions and to make it really hard for anyone to change the ledger history.

How does it work?

Remember our last table - let's call this a 'block' of transactions.

In order to add this block to a chain (a series of blocks), we need to create a proof of work, something to make sure that the person adding a block isn't a spammer and to make it extra difficult to change the block sometime in the future.

A simple illustration of Adam's method is to take the hash of the last hash (i.e. 6b4d1b63c273c5f7fac31651b2d18ad58e88570236b7d8b7160df65dc0d1233a) and find a number which, when inserted, results in a new hash that starts with four leading zeros. Why? Just because it's hard - the only way to do this is by trial and error.

I tried to find a Nonce (a number) which, when added to our hash resulted in a new hash with at least one leading zero. But unfortunately I had no luck - it would take me ages.

And you guessed it - this is Bitcoin mining. When the problem is solved the miner gets a transaction fee (and also for now some free coins). Because it takes time to solve the puzzle, heaps of miners compete with eachother to be the first to guess the solution - in doing so they all make the system more secure.

Bitcoin is programed to make this challenge harder and harder with each block, a concept known as accumulated difficulty. Currently, if you use your computer's CPU to 'guess' via trial and error, it will take something like 98 years! That's why the miners for bitcoin use special equipment to do the job - Application Specific Integrated Circuits ("ASIC"). It's just gotten too hard to 'mine' with a computer these days.

Putting it all together

Satoshi sums up all these ideas in a few steps that would go on to define Bitcoin.

" The steps to run the network are as follows:

  1. New transactions are broadcast to all nodes.
  2. Each node collects new transactions into a block.
  3. Each node works on finding a difficult proof-of-work for its block.
  4. When a node finds a proof-of-work, it broadcasts the block to all nodes.
  5. Nodes accept the block only if all transactions in it are valid and not already spent.
  6. Nodes express their acceptance of the block by working on creating the next block in the
    chain, using the hash of the accepted block as the previous hash."

Now you're probably still not clear on exactly what bitcoin is yet (let alone who does what and why) nonetheless I hope I've provided a vague understanding of the underlying concepts.

To me, the key take aways are that all transactions are broadcast through a shared/distributed ledger. The hash tool is also pretty cool as it helps verify the chain and underpins the 'proof of work' problem. Miners compete to solve the proof of work - ensuring there's more than one miner checking and updating the ledger.

By putting it all together, Bitcoin effectively solved the double spending problem and created a system that people can trust.

This was part 2 of a series I'm writing on cryptocurrency. Check out part 1 and part 3. 😎

Upvotes are much appreciated for motivation and please comment for corrections/questions.