In: Computer Science
Explain the crypto-economy concept of blockchain? Explain how cryptographic hash function is applied to protect the integrity of blockchain transactions and to detect double-spending attack. State the property of the hash function that is relevant for each application.
A simple analogy for blockchains
Imagine a primitive village, where they don’t have money in our traditional sense. Instead, they engrave the details of each transaction onto a stone block, then cement it in place in the center of the village.
David swaps fifteen chickens for one of Sarah’s pigs. They engrave this information onto a block, then cement it in the town square. Now, anyone can see that David is the new owner of Sarah’s pigs, while Sarah is now the owner of the fifteen chickens. Since the information is public, there can be no disputes over who legally controls what.
The next day, Jessica trades Mark 100 kilograms of corn for a canoe. This is also engraved on a block, which is then cemented on top of the old block. Since everyone in the town will now be able to publicly verify that Jessica no longer owns the 100 kilograms of corn, she can’t try to sell it again if Mark goes away for a couple of days.
In the coming days, more and more transactions take place, and more blocks of stone with the transaction details engraved on them get cemented in place. Over time, the transaction stones start to form a tower.
All of the details are publicly available to everyone, and the people cannot change or take back the earlier transactions, because a bunch of blocks are cemented on top of them.
This village’s financial system may not be the easiest to use, but it gives everyone in the village a way to keep track of their transactions. It is a public ledger that keeps permanent records, which can’t be altered. One of the most important aspects is that it is decentralized. There is no central bank or government that is responsible for the transactions. It’s all done by the community.
There are a number of differences between blockchains and the above analogy, but it’s still a good starting point to get your head around what blockchains are and why they are useful.
One of the key contrasts is that blockchains aren’t on display in public, instead, anyone who wants to can store a copy of a blockchain on their computer. Blockchains use cryptography, computers and electricity to build the blocks, rather than stone and cement.
The most important aspects of blockchains are that they cannot be changed, aren’t controlled by any single entity, and everyone can view the transactions. These properties are why people believe that the technology has the potential to be used in a vast range of applications.
The core cryptographic concepts behind blockchains
Whether or not blockchains currently see a lot of real-world usage, they are still interesting applications of cryptography.
These days, countless blockchains each have their own unique variations. Since it isn’t possible to cover each of their individual aspects, we will focus on the core concepts and how they relate to cryptography. We will mainly be focusing on bitcoin, not because it is the best blockchain, but because it is the first one, which all of the others are based upon.
What is cryptography?
Before we dive in too deep, it’s important to cover the basics. Cryptography is the study and practice of keeping secret information away from adversaries. In the early days, it was done simply, using techniques such as changing each letter in a word to the letter that follows it in the alphabet. Under this type of scheme:
Hello
Becomes:
Ifmmp
If your recipient knows how to convert the coded message back to its original form and your recipient doesn’t, then you can assume that it is a safe way to communicate.
Over time, people have gotten much better at cracking codes. Technological advances also improved our code-breaking abilities significantly. In order to keep our information secure in the present day, we now have to use codes that are much more complex.
These codes include incredibly complicated algorithms such as AES or RSA, each of which involve a lot of math. They use computers to conduct both the encryption and decryption processes.
How can blockchains prevent double-spending?
By now, you hopefully have a reasonable idea about the underlying cryptographic processes that bitcoin and other blockchains use in their transactions. This brings us back to where we left off in our earlier example: How can Jason know whether or not the massage has already been redeemed? We refer to this as the double-spending problem.
On any decentralized, pseudonymous network, it’s expected that some people will cheat to try and enrich themselves. Bitcoin and other blockchains solve this problem with a peer-based verification process called mining.
To explain how this works, let’s stretch our earlier analogy a little bit further:
The best way to visualize the blockchain mining process is if, whenever a transaction is made, a copy of the contract is sent to everyone within the friendship group.
After a number of transactions have been made, each person would combine the transaction details into one folder. Everyone in the friendship group would then take the result from the previous folder of transactions, combine it with the current transaction details, and then try to solve a complex mathematical problem using these inputs.
The first person to get find the solution would then broadcast it to all of the other members of the friendship group, who are able to quickly verify whether they have included the correct transactions, and whether they have the right answer or not.
If the answer is correct, the person who succeeded first receives a reward. This reward is what incentivizes everyone to validate the transactions. If anyone tries to cheat, the rest of the group will find out, which means that cheaters have no chance of claiming the reward and that the effort would be wasted.
Once a person has successfully completed the mathematical problem and claimed their reward, the whole group begins collecting new transactions in another folder. Once they have enough, they combine them with the result from the previous folder and compete to solve a new mathematical problem in the hope of winning the next reward.
The result of the previous folder is included in the new one, and a chain of results is formed, which allows people to check the transaction histories and verify that everything along the chain is legitimate.
Since everyone keeps a copy that includes the transaction history, and the only effective financial incentive is to honestly contribute to the validation process, this prevents double-spending from occurring.
This whole process may seem inefficient, but thankfully everyone who makes a blockchain transaction doesn’t have to do this. The task is left to miners and it’s automated, so it doesn’t involve anywhere near as much work as our example does.
The above analogy is imperfect, because it’s a simplification of a relatively complex process. The main aim is to give you a visual idea of what is really going on.
Hashing
Hashing is the process of sending data through a hash function to produce a specific, essentially unique hash of a fixed length. In blockchain applications, we use cryptographic hash functions such as SHA-256.
Cryptographic hash functions have several important characteristics which make them useful:
As an example, if we put “Let’s eat dinner” through an online hash function, it gives us:
a754edc281dfb28ca15fa0662de2e238550151a6c49514491eb1f60ac725bf65
Every time we put it through, it will give us the same result. But if we change even one character, it returns a value that is completely different. “Let’s eat dinnet” gives us:
c323ef87e6a9e4fe8fede20738a7e3c7122f5ffa504bdf6c5541a33e76949812
So, we have this mathematical function with a range of interesting properties, but how is it useful in blockchain applications?
The properties of hashes allow us to:
These various features of hashes are used in four major areas of the bitcoin system: