Blockchain: The Basics and Technical Fundamentals

Milan Saxena
Coinmonks
9 min readJun 2, 2018

--

In this article, I’d like to explore the basics and technical fundamentals of one super hot topic in the tech industry: Blockchain. Many of you have probably heard of this term, but perhaps you’re confused or not exactly sure how this so-called “revolutionary technology” works. I think that the difficulty most people face is the lack of fundamental understanding of how this technology works. Many people know about the use cases of Blockchain, but many don’t really know what goes on “under the hood.” Today, I am going to give a simple introduction to the absolute, fundamental technical aspects of Blockchain.

Before we can understand what a Blockchain is, we must first visit one of the most fundamental data structures in Computer Science: the Linked List.

LINKED LIST SECTION

Linked List Diagram

So, why is it called a linked list? Well, it’s simply a list that contains information in a list format, but the information is arranged in a “linked” format. Why would that be necessary, or even a good idea?

Well, let’s think about a simple, grocery list. Say you wanted to go to the store to buy milk, eggs, and cucumbers. Then, your list would look something like this:

Grocery List

  • Milk
  • Eggs
  • Cucumbers

If we encoded this into a linked list, we would start at the first node that contains milk, and buy milk. Then we go to the next node and buy eggs. Finally, we would go next to the last node and buy cucumbers.

Let’s do a quick tutorial in Python showing this functionality of a linked list.

Creating a Linked List in Python

The code snippet above shows you how to define a simple linked list class in Python. For those of you who are unfamiliar with coding or Python, this code essentially defines the linked list object. The linked list object has two parameters called val and node.

In Python def __init__ specifies how we are going to initialize or create this object. We have defined in our implementation that we will create this object with a specific value, val, and a next, which will point to the next node in the linked list.

Below is an example of how to iterate through this kind of a linked list in Python.

Interactive grocery list example in Python

At the very end we get an error because we have approached the end of the list, and therefore there is no more information left. That’s why in the first diagram there is a null at the end of the linked list. All linked lists have this null value at the end of the list because that signals the end of the list.

An important fact to remark about this implementation is that we transverse the linked list from the first node to the last node. In our example, from milk to eggs, and then to cucumbers. You cannot go through nodes in any other order in a linked list.

Now that we’ve covered the basics of the linked list, let’s come back to the idea of the Blockchain. The reason we needed to understand the linked list data structure is because a Blockchain is actually a linked list, however there are some additions to the structure that make Blockchain a little bit different.

CRYPTOGRAPHY SECTION

Many of the additions that are included in Blockchain have to do with cryptography, and more specifically hashing.

What is cryptography? According to Wikipedia, cryptography “is the practice and study of techniques for secure communication in the presence of third parties called adversaries.”

Let’s consider for example you and your friend would like to chat by passing notes through class. Unfortunately, you are not sitting next to each other, so you have to pass the note through everyone else in the class before your friend can read it.

In order to preserve your conversations, the two of you come up with an idea. Instead of writing in standard English, you decide to encrypt each letter from the English language into a number from 1–26 and you decide to put brackets around each word.

Letter to Number Cipher

From there, you decide to send the message: “Hey how are you.”

You encrypt the message: “[8 5 25] [8 15 23] [1 18 5] [25 15 21]”

Then your friend gets the message, and he decrypts it using the numbers and turns them into letters.

What’s a potential problem here? Well interestingly enough we can see a lot of numbers in the 20s, and nothing past 25. Since there are 26 letters in the alphabet, perhaps one of the students in between you and friend will try and crack the code by matching up the numbers with the English alphabet. If someone does that, then this encryption scheme is broken.

To improve this encryption scheme, you can increase the amount of numbers from 1–26 to 1–52. Double the size of the numbers you use, and then every consecutive pair of numbers is equivalent to the same letter. So, 1 and 2 will both be “a,” 2 and 3 will be “b,” and so on.

How will we decide whether to use 1 or 2 for “a” or 2 or 3 for “b ?” Well, we can flip a coin. If it lands heads, we’ll pick the smaller number, and if it lands tails, we’ll pick the bigger number.

Using this new scheme, let’s encrypt “Hey how are you” again.

“Hey how are you” := “[16 9 50] [16 29 46] [1 36 10] [49 29 41]”

This scheme is more robust compared to the original since it is still possible for your friend to decrypt the message, but now we have introduced some randomness into the choice of which numbers we use. This randomness will make our scheme more difficult to crack. Randomness is very important in cryptographic functions since we want them to be secure and unbreakable.

Now how does this play a role in Blockchain? Well, in addition to the original linked list structure, each block in the Blockchain contains the hash of the previous block.

What is a hash? Well, a hash is the output of a special function called a hash function. A hash function takes in an input and returns an output that is unique to that input. Each input has exactly one unique output. For example, let’s say we started a mobile app and we decided that we wanted to hash all the users.

Let’s say our user list looks something like this:

User List

  • John Smith
  • Sally Bridges
  • Mark Saban

A simple hash function we can use is f(x) = x + 1, where x is the number of existing users. Therefore, the user John Smith will be hashed to the number 1, Sally Bridges to the number 2, and Mark Saban to the number 3.

So, essentially, the numbers 1, 2, and 3 uniquely identify our three users, and there can be no “mix-ups.” In other words, there can be no instances where 3 identifies John Smith instead of Mark Saban. There also can’t be any hash collisions, which occurs when two different inputs have the same hash value.

What are some problems with security here? Let’s say some adversary comes along and decides that he/she wants to try and access private information of the users on the app by discovering their hash.

Let’s say that the adversary has access to his/her particular hash. To try and crack the hash function we built, the adversary makes multiple accounts very quickly by writing a program that creates five separate accounts within the span of one second. Let’s assume within that second, no one signed up for an account. The adversary now will see that each successive hash is different by the previous hash by 1. From this, the adversary can tell that our function takes the existing number of users and increments it by one, and sets that as the hash for a new user. With this information, the adversary can play around with numbers and try to find private information about users with their hashes.

Putting all this together, we arrive at cryptographic hash functions. These functions are designed to address all of the issues we’ve gone over, and many more. From Wikipedia, “The ideal cryptographic hash function has five main properties:

  • it is deterministic so the same message always results in the same hash
  • it is quick to compute the hash value for any given message
  • it is infeasible to generate a message from its hash value except by trying all possible messages
  • a small change to a message should change the hash value so extensively that the new hash value appears uncorrelated with the old hash value
  • it is infeasible to find two different messages with the same hash value”

Here is a great example from Wikipedia about how SHA-1 cryptographic hash function computes the hash of different messages:

SHA-1 Hash Function Example

As we can see, small changes to the message result in an entirely different hash. That is exactly what we want for our cryptographic hash function.

The relevance of cryptographic hash functions to Blockchain is that they are used to hash each block, and then that hash in stored in the next block in the Blockchain. Cryptographic hash functions are very secure, and that’s why they are necessary and so important in Blockchain. It is what makes Blockchain secure.

Today, the SHA-3 family of cryptographic hash functions is the standard set for hashing.

MERKLE TREES SECTION

I want to go over another topic called Merkle Trees, which is used in the Bitcoin Blockchain. We should start by going over what the Tree data structure is in Computer Science.

A Tree data structure that contains various nodes that connect to other nodes. Data is stored within these nodes. Let’s take a look at an example:

An example of a Tree Data Structure from Wikipedia

Merkle Trees are also like the original Tree Data Structure, but they are referred to also as “hash trees” because each node in a Merkle Tree contains a hash. Every block in the Blockchain contains a Merkle Tree that records all of its transactions.

Wikipedia: Merkle Tree Example

As you can see above, the Merkle Tree is used by decomposing the hash of a transaction, and following it all the way down to the Merkle Tree to where the data lie.

Why use a Merkle Tree instead of a list to store transactions? Well, if we used a list, then we would have to traverse the entire list every time we wanted to verify a transaction on a block. Instead, we can use this tree structure to find data much faster by only considering half of the tree each time.

In the example above, if the hash starts with 1, we go to the right branch. If it’s 1 again then we go again to the right branch. This way, searching for the hash of transactions in our tree is a lot quicker than iterating through a list.

CONCLUSION

Putting all that information together, let’s take a look at a simplified version of the Bitcoin Blockchain:

Image result for blockchain diagram
https://bitcoin.org/en/developer-guide

We have covered each concept in this diagram! I hope you understand Blockchain technology better after reading this article.

Now, I hope you can form your own opinions on this technology, and decide how important it could be in the future. If you liked this story, please clap, comment, and share this article on Facebook and Twitter. Thank you for reading!

--

--