Nonce

Introduction

Suppose you have to abuse your friend in public and you don’t want others to know that you are abusing. What will you do? Probably, you will come with a new word such that your friend will understand that you are abusing but the others will not. This word you just have coined is for the moment and might not be used again, ever. These types of words are termed as Nonce words in English language. The same concept is utilized in cryptographic communication to improve its security. In this article we will explore more about it.

What is a Nonce in Cryptography

In cryptography, a nonce is a random number that can be used only once in a cryptographic communication.

Nonce

In the diagram shown above, we have tried to explain the use of nonce in a cryptic way of login.

Nonce in Blockchain

The literal meaning of nonce, as we have discussed earlier, is “unique for particular occasion”. A nonce is a number which has to be guessed in such a way that when added before the hashed value of current block should produce a value whose hashed value is less than the difficulty (it is also a part of block and is regenerated for every block), its length should be equal to the hashed block length i.e. 64 character which is equal to SHA-256 length in case of Bitcoin.

How to Implement Nonce in Blockchain

As a programmer, you might be wondering how to implement a nonce in your blockchain. Here, we will just write a small script to show the use of nonce. We will be publishing a tutorial to develop a new blockchain end to end, and there we will try to explore the development part.

class Block {
      constructor(timestamp, data) {
          this.index = 0;
          this.timestamp = timestamp;
          this.data = data;
          this.previousHash = "0";
          this.hash = this.calculateHash();
          this.nonce = 0;
      }
  

      calculateHash() {
  return SHA256(this.index + this.previousHash + this.timestamp + this.data + this.nonce).toString();

      }
  

      mineBlock(difficulty) {
var target = difficulty.toString(); //Create a string with difficulty
  while(!hash.substring( 0, difficulty).equals(target)) {
      nonce ++;
      hash = calculateHash();
    }

      }
  }

 

In the JavaScript code mentioned above, we have created an initial block (constructor) where the value of nonce is set as 0. In the mineBlock section, we have increased the value of nonce until hash target is reached. This is done to find the nonce and add the transaction block in the blockchain.

How a Miner Finds a Nonce

A miner follows the steps below to solve/find a nonce:

  • The person(miner) must guess a value of nonce (it can start with 000…0001)
  • Get the hash value of block header of previous block (000dceb75a135c…)
  • Append that nonce before the hash of block header (000…0001000dceb75a135c…)
  • Rehash the above appended value (xyz)
  • Get the difficulty value from the block header (abc)
  • Check if the above rehashed value is less than the difficulty value or not (xyz < abc)
  • If yes, then a new block is added in the current Blockchain of the Bitcoin and this person is rewarded some bitcoins for his work

Advantages

  • It makes Blockchain Transaction tamper proof and secure
  • It enables secure and valid addition of a transaction block

Disadvantages

  • It slows down the process of blockchain transaction
  • It requires heavy computer mechanism to solve a nonce after certain level of transaction which results in heavy electricity utilization

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.