Table of Contents
So far we have reviewed the history of the birth of the Blockchain, stopping at the most transcendent moments of the same and giving some keys and concepts about its operation that are essential to minimally understand the operation of this technology. In this unit, however, we will delve in detail into the functioning of the Blockchain and the parts that compose it seeking a much deeper understanding of the blockchain.
The GOALS of this unit are:
– To know the structure of a Blockchain and the parts that compose it.
– To understand the mechanism of transactions in the Blockchain.
– To learn about Hash functions and what is their role.
– To know the meaning of “Mining” in the Blockchain environment and how it works.
– To understand the role of “Public Key” and “Private Key”, how they work and their importance in the Blockchain ecosystem.
– To Understand the concepts “Hard Fork” and “Soft Fork” and their differences.
OPERATION AND STRUCTURE
Before we start going in depth, we can summarize what the Blockchain is as a ledger that uses cryptography and rewards to record the transactions that are generated in its network in a reliable, secure and tamper-proof way. This feature allows you to perform transactions between different parties without the need for trust between them or a third party.
Once we have a simple definition established, we will begin to unpack each part of the blockchain, establishing some primitives. Primitives are like the base material from which you build something. If you want to build a table, you will need wood, some screws or nails, a saw, a drill, paint, etc. Therefore, it would be wise to know how each one works before you build anything.
The most basic part of a blockchain is precisely the blocks. These are what shape and build a Blockchain. These blocks contain different types of data and basic information to maintain and continue with the structuring of the blockchain. When one of them is completed, it becomes an indivisible link and gives way to the generation of a new block.
In this way, the entire network runs in a cycle and all data is stored permanently. Each block contains records of some or all recent transactions, and a reference to the block that preceded it, which, together with the point-to-point verification system, makes all transactions that are recorded in the blocks of the chain impossible for any user to change or delete.
All this information contained in the blocks needs to be secure so that no one can access it to modify, steal or delete it, and that is where cryptography comes in.
Cryptography is the study of techniques for communicating securely in the presence of third parties. Like so many other things related to modern technology, it was the military who carried out a great deal of cryptographic research to ensure secure communications between their troops without enemies being able to read their telegrams or radio messages, even if they intercepted them, however, the history of cryptography goes back much further.
Cryptography comes from Greek and is composed of “kryptós” (secret) and “graphé” (graph or writing), which literally means “secret writing”. It is estimated that the origin of cryptography dates back to approximately 2,000BC as stone inscriptions, cuneiform tablets and papyri survive today that show that the ancient Egyptians, Hebrews, Babylonians and Assyrians devised proto-cryptographic systems both to deny information to the uninitiated and to enhance its meaning when revealed. It is believed that the first person to use a cipher considered “modern” was Julius Caesar (100 BC to 44 BC), who used this type of technique when he had to communicate with officials or different governors because he did not trust his messengers. Because of this, he devised a simple but very effective system, which consisted of substituting each letter of the text of his messages with another letter that was placed three positions ahead.
Since the time of Julius Caesar, cryptography has evolved enormously, always looking for new methods of safeguarding information in the most effective way possible.
Today, modern cryptography has four well-defined GOALS:
– Confidentiality: the information in the message cannot be understood by anyone other than the intended recipient.
– Integrity: By encrypting the message, it cannot be altered both in storage and on its way to the receiver without detection of such alteration.
– Non-repudiation: the sender of the information cannot later disassociate himself from the transmission and creation of the information sent.
– Authentication: The origin and destination of the information can be confirmed by the sender and receiver, as can their identities.
There are many procedures and protocols that try to fulfill all these GOALs, which are called cryptosystems. These cryptosystems, in addition to meeting these objectives also focuses on human behavior, for example, the choice of passwords difficult to guess with a number of certain characteristics, logging out of systems when you stop using a computer or not sharing sensitive information with people outside our personal or work environment.
Because we live in the internet age where all our data and information travels around the world, modern cryptography is used in practically everything we do, from the websites we visit to credit card chips to the passwords of our computers, mobiles and tablets. In all these cases, the GOAL is the same, to prevent not so much that the message is captured, but that its content is unreadable by the interceptor.
Once we know the procedure used to hide the information, we will analyze the method used to encrypt the information within the blocks of the Blockchain.
To achieve this GOAL, a mathematical function called hash function is used to encrypt the data. This hash function has three basic properties:
1- Its input can be of any size. For example, you could use a phrase like “My car is blue” as input or, conversely, you could use the full text of “Don Quixote” and both would work.
2- Using whatever size we want as input, the output size is the same and is determined by the same hashing algorithm being used. To keep it simple, we’ll use 256-bit output, since that’s the same as what Bitcoin uses.
Whether you entered the phrase “My car is blue” as input or used the entire text of “Don Quixote,” both would produce an output with the same size.
Using an algorithm called SHA 256, which is the one used by Bitcoin, the phrase “My car is blue” generates the output:
The first 407 words of “Don Quijote de la mancha” using the SHA 256 algorithm result in:
Although the Quixote input has 403 more words, you get the same output size, which is extremely useful.
3- It must be computationally efficient. For any given input size, it must be able to compute the output in a reasonable amount of time.
If I wanted to use the entire text of Don Quixote as input, the output should be given in a reasonable amount of time.
The properties described above are for a general type of hash function. General hash functions allow you to map information that occupies a large space, to a much smaller space and can be useful for storing and working with different types of data.
However, we will focus on cryptographic hash functions, since those are the ones used in Blockchains.
Apart from the above properties, a cryptographic hash function requires three additional properties: hiding, puzzle friendly and collision resistance.
Hashes may look random but they are actually deterministic. Therefore, their outputs are reproducible, so that whenever the same input is used, the same output will always be obtained. This means that two different parties who know the input can verify that the other party also knows the input by showing their hash to the other party.
Therefore, the first property we want for a cryptographic hash function is that it should be hidden. This means that, if the output is given to someone, there is no way for that person to find out what the input was.
That is, there is no way for an outside observer to know that 1444731d7a7894e07903d209ef60b2b8c8614d68d9ba20d0991ee239ac212fc0 means “My car is blue”.
However, I do know that the entry is “My car is blue”, then I can easily verify whether or not you know that by checking your hash.
The fact that if someone else sees the hash they can’t use it to figure out the input, allows those who do know the input to communicate securely in a public channel using the hashed output without worrying that a third party might figure it out.
The second property we need is that it be “puzzle friendly”. This means that, if someone wanted to generate a hash with the same result as “My car is blue”, it is practically impossible to find another input value that achieves exactly this same result.
The algorithm that would be needed to generate the hash would be to try all possible strings, first in order of length and then in alphabetical order, until one is obtained that has the same value. The odds of this happening are so high that it is more likely that an asteroid would hit the earth making us disappear.
In other words, cryptographic hash functions are extremely difficult to reverse, and it is precisely this that makes the main difference between a cryptographic hash with a hash used in data structures or a compression algorithm.
This is also part of the Blockchains arms race, as hackers are always looking to improve methods for inventing hash functions, so cryptographers, for their part, always have to be looking for better hash functions.
Having this capability is critical because, as we already know, the Blockchain is a ledger that must be tamper-proof and this property of Puzzle friendly is what enables this. If I wanted to replace an entry such as “My car is blue” with another one, it should be impossible to do so without other participants knowing about the existence of that change.
If we use a good cryptographic hash function, even the slightest changes that could be made to an input would result in completely different outputs, making the input resistant to manipulation.
In short, cryptographic hash functions appear random, cannot be reversed, and hide inputs, but, at the same time, they are completely deterministic, and it is precisely this that makes them so extremely useful.
Transitive Hash Functions within the Blockchain
So far we have seen how hashing works with a single piece of information such as words in a ledger or a password. What if we wanted to hash collections of data such as a bunch of documents or a sequence of transactions?
Let’s start with a linear sequence of things we want to hash consecutively. We start, for example, with a piece of data called A which when we hash we get A1.
Now we want to mix B, so we just add B (A1 + B) and hash everything together. Now we get a new hash which we will call B1 and which has information about A and B.
So, what we need is a hash function which in case of changing something about A or B results in a different final hash. This is a transitive hash function.
A Blockchain is a chain of hashes
When talking about Blockchains, one of the phrases that is used a lot and that we have already mentioned several times, is “minimize trust”. Hashes in general, and hash pointers in particular, are a central component of this property that allows us to minimize trust. A hash pointer is just a hash that points to some other data structure such as a previous block in the blockchain.
If we had a Blockchain of 100 blocks and the tenth block contained the hash of the entire book of Don Quixote, and we changed one letter in that book, it would change the hash pointer in that block and thus in all subsequent blocks.
Hash pointers ensure the integrity of the ledger (i.e., that it has not been modified). Blockchains have been called triple-entry bookkeeping because this hash pointer data structure is another, more difficult to forge way to ensure that the ledger reflects reality.
Now, what if we want to hash not just in a linear sequence, but in a sequence of sets like Bitcoin transactions in its block sequence? For this we need a new rule on how to traverse each data set, reducing the problem, again, to encrypting a consecutive series of things (A1, B1, etc.)
A Merkle tree, which we’ve already talked about, is exactly this. It allows us to group blocks of data and group them into a structure that looks like a tree branch with leaves.
In the same way that happens in a Blockchain, it is enough to know the hash point of the root of the tree, or Merkle root, to be able to check if any of the data in the rest of the tree has been manipulated. If one bit is changed, in one transaction, it changes every subsequent hash and the tampering would be revealed.
Using a Merkle tree is preferable to using only a summary of all transactions, because it allows a much faster and simpler test of whether a particular transaction is included in the set of all transactions.
Finally, we will address the third and last property required by a cryptographic hash function, collision resistance.
A collision is when two different inputs produce the same output. While the output size will be the same, the output itself will not be identical to other outputs. “My car is blue” and the entire text of Don Quixote produce output of the same size, but the characters are different.
If they were to produce exactly the same output and we were to send that ciphertext to another person, that person, when decoding it, would not know which is the correct message, “My car is blue” or the full text of Don Quixote.
It should also be noted that two similar inputs cannot result in similar outputs. For example, the Quixote hash is completely different from the Quixote hash by adding an “s” to any word in the first sentence of the book.
Continuing with the example, we have already seen that if we use SHA256 on the first 407 words of the book of “Don Quixote de la macha” it produces the hash:
If we add an “s” in the first sentence it would be “In a place in the Manchas” and leaving the rest of the text exactly the same, the hash it produces is:
This is much like how two “close” starting points in a chaotic system very quickly lead to two completely different future trajectories. It is this “chaotic encoding” property of hash functions that creates collision resistance. The outputs appear random and are therefore unlikely to collide, but they can map infinity to finite space, allowing us to compress data.
This is particularly important if you are sending money. The public key of a wallet is produced from the private key using a cryptographic hash function. It is not desirable that someone else can reverse engineer and thereby obtain your public key to discover your private key, and thereby gain control of all funds.
A hash function is considered collision resistant if it is not possible to find a collision. In theory, we know they exist because the number of inputs is infinite, but the number of outputs is finite: there are only so many ways to organize 64 characters.
However, in practice, the SHA 256 cryptographic hash function is resistant to collisions because no one has yet encountered one and the odds of it happening are astronomical. To give you an idea of how difficult this would be for this to happen, if every computer made by mankind had been trying to find a collision since the beginning of the universe, the odds of them finding one are lower than the odds of the Earth being destroyed by a giant meteorite in the next two seconds.
The Proof of Work
Satoshi Nakomoto’s greatest innovation, whoever he was, was to create a new rule and that was added to the existing ones. When a new set of transactions (a block) is added to the existing chain, an extremely difficult and impossible to skip mathematical problem must be solved.
This task is done by so-called “miners” and consists of finding the origin that gives rise to a given hash. When the origin is finally found, a new block is added to the chain and whoever has solved the “riddle” receives a reward in the form of the coin being mined. Solving the problem is what is called “proof of work,” which is a test of how much energy was used in trying to solve the problem.
For cryptographers, the invention of this “proof-of-work blockchain” was Satoshi Nakamoto’s biggest breakthrough.
As both the Blockchain proof-of-work and Bitcoin itself become more valuable, miners make more money, which incentivizes more miners to participate in the network, causing the difficulty of the problem to increase.
The blockchain with the proofs of work already accumulated is considered the true blockchain and is currently about 300 gigabytes in size, which can give us an idea of how much energy it has taken to generate all this amount of data.
The only benefit of having this large size is that, if someone wanted to forge the Bitcoin blockchain, they would have to spend the same amount of energy that has been spent so far to generate all the blocks that make up the Bitcoin blockchain with the same difficulty used in each of them, which virtually ensures that this will never happen.
Today, it would cost billions of euros to buy enough hardware and electricity to rewrite the Bitcoin blockchain from scratch.