Friday 25 May 2018

Bitcoin Block Hashing Algorithm (Part III)


In previous two articles, Part (I) and Part (II) of the Bitcoin Block Hashing Algorithm [(Read more here) & (Read more here)], some basic hashing algorithm and mining process were illustrated.  This article will demonstrate the hash verification steps by using Python script.

Three examples were selected from Bitcoin block explorer (Read more here).  The first example was the genesis block, and the other two were randomly selected.  The last example included a loop code to mimic the actual nonce solving process.  It took about 7 hours to find the nonce, by using a normal day-to-day laptop.  As such, one really needs a powerful system in order to be a miner!

Block #0 (Read more here)




Block #125989 (Read more here)




Block #522885 (Read more here)



Friday 18 May 2018

Bitcoin Block Hashing Algorithm (Part II)


In previous article (Read more here), the basic core hashing algorithm of Bitcoin using SHA256 was illustrated.  SHA256 hashing algorithm can cryptograph input messages of any size into a unique 64-character code.  Thus, contracts or transactions could be hashed and distributed on the Blockchain network.

This article will provide an overview of the Bitcoin hashing process using real-world example.  Each Bitcoin transaction info is publicly available on the internet.  One can use the Bitcoin Block Explorer (Read more here) to view the transaction info of every block (ledger) that is registered onto the Blockchain.  For example, the first Bitcoin block was created by the Bitcoin inventor, Sathoshi.  The block info is accessible on the internet and it is commonly regarded as the Genesis Block (Read more here).



As you may have already learnt from the previous article, input message is required to generate the unique hash for this transaction block.  The input message (block header) of Bitcoin transaction comprises of

  • Version info
  • Hash of Previous Block
  • Hash of Merkle Root
  • Time
  • Bits
  • Nonce

For the Genesis block,

  • the version info is 1;
  • the hash of previous block is 0 because there was no previous block;
  • hash of merkle root is ‘4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b’, which is the hashing results of the transaction details such as someone sending some bitcoins to another party. (Read more here);
  • the time when the transaction was accepted, which was 2009-01-03 18:15:05;
  • the bits that was provided by the algorithm to adjust the difficulty of the “mining” task, depending on the number of “miners” participating in the process, for this case was 486604799; and
  • finally the nonce, which is a guessing number that was “mined” by the miners.  The nonce is a moving target and depends on time, bits and merkle root, that will be updated roughly every ten minutes.  The successful miner is the one who could guess the nonce that will produce the output hash of certain length of leading zeros.   For the Genesis block, the nonce is 2083236893.

The following diagram shows the flow of the hashing and mining process.



The hash for Genesis block is ‘000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f’.  It has 10 leading zeros.  At the time of this article was written, the target leading zero for new hash was 18.

As we can see from the above diagram, every new block incorporates the info from the previous block.  Thus, changing a small info from a block that sits in the middle of the blockchain without impacting the entire chain is impossible.  As such, once the transaction is registered on the blockchain, it is theoretically irreversible.

Part III of this series will illustrate the Python script for the above real-world hashing algorithm.  Stay online!

Tuesday 8 May 2018

Bitcoin Block Hashing Algorithm (Part I)


By now, most people may have already heard about Bitcoin and Blockchain, knowing that it could be described as “distributed ledger”, with the characteristic of irreversible, pseudonymous, global and secure.  Another interesting characteristic of Bitcoin or Blockchain is the “mining” process.  The reason behind the irreversible and secure characteristics of Bitcoin and Blockchain is that for each transaction block to become valid, it requires huge computing power to solve the hash algorithm.
 
The “miner”, could be anyone whom has the computing power to solve the algorithm.  They are competing among each other to solve the hash.  The winner (the first one who solves the hash) will be rewarded with some amount of new Bitcoin, generated automatically by the Bitcoin Blockchain algorithm, and also transaction fees.

The core hash algorithm is based on SHA256.  Secure Hash Algorithm, is a set of cryptographic hash functions designed by the United States National Security Agency (NSA) (Read more here).  It is designed in a way that the input messages of almost any size (over 18 quintillion different values) are converted into a unique 64-character long code in hexadecimal format.  And the one-way process means it is impossible to obtain the input message by knowing the unique 64-character long code.  A simple analogy of this process is colour mixing.  One can produce a new unique colour by mixing various colours, but one cannot “unmix” the new unique colour back to their individual component.

Let’s look at some simple examples of SHA256 hashing using either the following Python script (a programming language) or an online hashing tool (Read more here)






The input message “abc” is hashed using SHA256 and the output of the hash is ‘ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad’.

A miniscule change to the input message will significantly alter the output hash.  For example, changing the ‘c’ of the input message to capital ‘C’ will produce an entirely new hash output '0a2432a1e349d8fdb9bfca91bba9e9f2836990fe937193d84deef26c6f3b8f76'.

Using the same approach, the entire Universal Declaration of Human Rights (Read more here) can be hashed into ‘c08345541d77c256c914d0ada1ea02497c7525efe0e5699d6ef6126a66b4ee59’.

.
.
.


As such, any contracts, agreements or transactions could be cryptographed and distributed on the Blockchain network with its unique tampered proof hashing algorithm.  The next article will illustrate more real-world examples of Bitcoin transaction algorithm.  Stay online!

Now here’s the quiz, what is the input message of the following hash?

1d0807cab52364ee6536ac5880e522765e66de90cab618797492afeb80fb89b2

Hint: “V*** *** **t*** M*****i*”