Wednesday, December 6, 2017

Cryptocurrency and Blockchain - Role of Cryptography

In my last post, I tried to establish the basics of the Cryptocurrency and Blockchain. Many of you would be asking the question why is it called Cryptocurrency and how is Cryptography used in this whole scenario. This post will try to explain this.

Before I delve into the details, let's have a quick look at what cryptography is? As per Wikipedia definition, Cryptography is the practice and study of techniques for secure communication in the presence of third parties called adversaries, i.e., it is a technique to send a message from one party to another in the presence of third parties, but only understandable to the side it is intended.
For example, if I am presenting something to a customer and one of my colleagues says, "Excellent Point, Rajan.", It means "Shut Up!!". This communication is possible because we have agreed the keywords and its meaning in advance. However, in the real word cryptography, it is far more complicated than just a sequence of phrases.

Coming back to point why these electronic currencies are called Cryptocurrencies because Cryptography is the fundamental part of how these work.
Let's understand the two cryptographic concepts before we dig deeper, Cryptographic hash functions and Digital Signatures.

A cryptographic hash function takes an arbitrary input string and return a fixed length string and have following properties:
1. The function is computationally efficient, i.e., the hash is generated within a reasonable time.
2. It is extremely difficult to reverse engineer the input by looking at the output.
3. It is extremely unlikely to find two strings that have same hash output.
In simple terms, the hash function is a  black box that take any length string input and produces a fixed length string output.

Digital Signature is pretty much like a paper signature albeit in a computer/soft format. It is however implemented as set of pairs of keys (public and private). Anyone can generate a new key-pair using a computer and publish the public key to everyone. The owner of public key will create a new transaction / document and sign it with his/her private key. Once signed it can only be unlocked by the public key. So if you can open a message with a particular public key, one can be sure that the message has come from owner of the public key. This way the public key can act as an identity of someone in the cryptocurrency network.

The actual details involve a lot more complex maths but to keep the post simple, let's just take these two concepts and try to understand the cryptocurrencies.

Recall the cryptocurrency description in my earlier post. Each node/participant in the network will have an identity as their public keys. One thing to note here is that a person can create multiple public/private keys and can assume multiple identities in the network, but let's ignore that for now.  Everytime someone needs to publish a transactions to transfer cryptocurrency; they will need to sign the transaction so that validating/miner node can check that you have right to initiate the transaction.

There will be many transactions published by many nodes simultaneously. These transactions (bundled together) will be picked by the miner nodes to validate the transactions and compute the cryptographic hash of the transaction block + hash of previous block.
But here is the catch... the output hash has to be of a such that it starts with 6 zeroes. (Note: 6 Zeros is just an example, the actual number is decided by the network depending upon how difficult the network need to this computation to be and is adjusted periodically)
- To generate that specific hash, the miner node will add a number ( called nonace ) to the overall block and compute the hash
-  If the output hash starts with 6 zeros then the node publishes the block, claims the mining incentive e.g. 12.5 BTC in case of bitcoin.
- If the output is not the desired hash then change the nonance and  try again.
- Miner keeps on repeating till either some other node publishes a new block or the node finds the required hash.

Now if you look at the steps above, the miner will have to compute the hash multiple times, each time with a different nonance, to get the required hash. Since there are many nodes trying it at the same time, it is extremely unlikely that the same node will get to publish two consecutive blocks. This way the randomness is created in the network the chances of someone controlling the network go down exponentially.

There, however, is still a possibility if more then 50% nodes are controlled by a single owner and can play maliciously. But, if you re-think, the person controlling the network will be at loss if he/she plays maliciously, because by doing so the other nodes will lose faith in the network and stop participating and as a result the value of currency will go down.

Please remember, the miner nodes are paid in cryptocurrency and not fiat currency, so it is in their interest to keep the network honest.

That's it for this post. I will come back with next post on how a blockchain looks like and how the consensus between network nodes is achieved on which transaction to be included in the block and which block to be appended to the chain.

PS: There are many technical details in each step, but I keep my posts purposely simple to be understandable by everyone. Please comment if you would like me to start another blogpost so go into the technical details.

Wednesday, November 22, 2017

Cryptocurrency and Blockchain - The Basics

Cryptocurrency is a making news headline literally everyday especially since Bitcoin have surged almost 950% in last one year.  However, many people have wondered what is the relationship between Cryptocurrencies (e.g. Bitcoin) and blockchain and how it all started.  Let me try and explain. 

Back in 2007/08, during sub-prime crisis, a need was felt for a decentralized mechanism to circumvent the trust issues that caused the whole financial meltdown. That’s when someone with pseudo name "Satoshi Nakamoto" published a whitepaper for peer-to-peer distributed network which is open and transparent to everyone. Which essentially is the basis of the all cryptocurrencies and blockchain in general. 

Now to understand the how it works, let's looks at how people in Yap, a Pacific Island, used to trade back in days. They used a limestone called Rai stone, with a hole in the middle, to represent value.

The stones were big, sometimes more than size of a car, so it was not possible to carry them. However, each stone has an owner and everyone in the community knows who it belongs to, but was rarely moved.  The ownership of the stone was changed in presence of minimum number of community members and was inscribed on the stone. Since, inscribing on the stone was laborious task, the person doing it, the inscriber, was given a reward in form of the part ownership of the stone. 
Note:- Parts of this story are created to help understand the concept.

Since, anyone could have carved a new stone and generated the currency, the supply of the stones kept increasing and the value decreasing, the currency became useless after a certain time.

The concept of cryptocurrency is somewhat similar where everyone knows what everyone else have, using a distributed public ledger, eliminating the need of central authority like a bank. At the same time, the problem of too much generation of the currency is controlled using the techniques halving, which I will explain later in the article.

Let’s take the analogy of Rai stone and see how the Cryptocurrencies work. The stone is the currency, albeit in software form, and inscription on it the open electronic ledger. The people who believes in that system are called nodes, the participating computers. The community is a set of computer nodes, where each one has a copy of the ledger. Each ledger contains every transaction since the beginning of the currency. The inscribers in this case are special computer nodes, called Miners.

When the transfer of the ownership must happen, one of the parties submits the transaction to the community, along with attached is a mathematical puzzle.  The transaction is published to all the nodes. The miner modes start solving the puzzle at the same time. The first one to solve the puzzle publishes the solution to the network and claims the reward. At that point rest of the nodes just validates the answer and agrees to add the transaction to the chain.

One important factor to note is that node solving the puzzle also checks if the transaction is valid, that does the initiating party have enough balance to make the transfer. The node can do this validation as it has a copy of all the transactions from beginning of the currency. The transactions are published for validations in a block, every 10 seconds in case of Bitcoin, and the whole ledger is a chain of blocks, hence the name Blockchain.

To solve the problem of over generation, cryptocurrencies use a process called halving. In this process, the award made to the Miner node is halved every 210K blocks. The current reward for a Bitcoin miner is 12.5 bitcoins, which will be halved to 6.25 on 20th June 2020 and subsequently halved again to 3.125 and so on. 

Every cryptocurrency has a hard limit as to how many currency units will be produced ever. e.g. there will be maximum 21 million Bitcoin produced. The limit is expected to reach by 2140 at which point there will be no more Bitcoins produced and the only once in the circulation will remain.

I hope it would have given you a glimpse of how cryptocurrencies work. 

But, many question will pop into your head like:
-       Where and how the cryptography is used in this?
-       What kind of mathematical puzzles given to be solved by Miners?
-       How does all other nodes agree to the solution given by one of the miners?

I will be publishing a series of blogs to answer these questions.  


 Author: Rajan Kashyap, (Twitter, LinkedIn) is a software architect with expertise on cloud, Blockchain, Cryptocurrencies and other new technologies.