Bitcoin Mining in Programmers’ Point of View

Have you ever wondered why there is no airship roots to Australia from America through Pacific? Well, google it! If you found out the reason then try to comprehend that how people have mechanized to follow the rules and regulations 3rd parties have put in forward without considering the smart way of doing things. May be for our own good. Bit harsh naah! Anyway this post is to eleborate concepts behind Bitcoin in a nutshell which is another endeavor to overcome intermediaries. Lets have a quick introduction to this before get into the programming stuffs.

Bitcoin: Bitcoin is a form of digital currency which was found out recently as a medium for exchange over the air. This is mostly used in transaction processing and validation on peer-to-peer network exploiting cryptographic operations with the specifications and software of open source community. Bitcoin’s total base money supply is valued at $125 million currently. Bitcoin is simply an “SHA-256” hash in hexadecimal format (256 means the number of bits associated) and will also include private/public key known only to the user which is used to spend the coin.


Fig1. The input is cryptographically converted fixed sized string (hash value)

SHA-256: A bitcoin is exchanged or spent by a payee to a payer using an SHA-256 hashed address which points to a wallet (file) containing the bitcoins. First a group of transactions are broadcasted to bitcoin peer-to-peer network for validation. This will be continued until one node is said to found random SHA-256 hash which starts with a specific number of 0 bits. This is to limit the amount of computing power searching a suitable number. This hash is then coupled with a “nonce” which is a user adjustable number starts from 0 to 232. This will be again broadcasted in the peer-to-peer network and combined with the previous completed block hashes to generate a unique hash. As a reward for the node which created the hash, bitcoins and/or transactional fees will be granted.


Fig2. A customized bitcoin miner

Miners: The nodes which attempt to generate hash are called miners. They are generally implemented on digital design with GPU, FPGA and ASIC since the high computational overhead which makes normal PCs unable to cope with it. The generation is controlled to 10 minutes to comply with the standard controlled rate for hash block generation. The above figure shows a customized bitcoin miner employed to do mining.

To have a visual idea on how Bitcoins involve in transactions play the below video.

Programming: Well, now lets look at the programming aspect of this from the top view.

1) Collect all the block headers (transactions) you have in the peer-to-peer network.

2) Calculate the Merkel root using all the transactions by hashing them. This is done through Merkel tree.

3) Construct a new block header appending Merkel root, previously constructed hash, a ‘nonce’ and information like version, current time stamp and ‘target’ (difficulty). Altogether 80 bytes.

4) Hash this block header using SHA-256 algorithm two times by splitting into two parts.

5) Compare the newly generated hash (32 bytes) with the ‘target’. If the target requirement is met then the generated hash is taken as a valid one and broadcast to all other peer nodes to verify and save in a public ledger for future use. If the target is not met, then go to the step 3) and increment the nonce to hash it again until meet the target. The target is set according to the processing speeds of the miners in the network.

SHA-256 hash function plays a vital part in this algorithm. From Merkel tree construction to the iterative generation of hashes to meet the target, SHA-256 is called for thousand times. To reduce the number of calling the ‘Midstate‘ is calculated. A self-explanatory python code is also available. But it is hundred times slower when implemented on a CPU.

There are number of digital designs deployed to play the job of miners. Interestingly now the miners are ever easy to construct thanks to opensource + high level languages (C, C++ and SystemC) + HLS tools available. HLS stands for High Level Synthesis where it allows the programmers to code the logic in their favorite languages (C, C++) and synthesis into a HDL (Hardware Descriptive Language) like Verilog or VHDL. This is such an endeavor.

Bitcoin Miner Using Zedboard: There is already a plethora of implementations of miners to date. But to have a great efficiency, it is evident that even the open source community is marching towards digital designs to exploit parallelism massively. This project mainly focuses on making “proof-of-concept” Zedboard which rides a fully functional Bitcoin miner for the open source community. The functions of the code are illustrated in this link.


 Fig3. Another illustration of bitcoin transactions (please zoom if cannot read)

Below is a test bench written using Vivado HLS Tutorial format. The C code available in the above repo can be verify using this. To do that, save this code in a file (sha256_tb.c) in the directory ‘sha256.c’ lies.

#include <stdio.h>
#include <math.h>
#include "sha256.h"

int main() {
 FILE *fp;
 SHA256_CTX *ctx;
 uchar data[64] = "This is a test"; // Input
 uchar hash[64];                   // Output

 fp = fopen("out.dat", "w");
 sha256_top(data, hash);           // Calling sha256_top function

 int i;
 for(i=0; i<32; i++){
  if(hash[i] < 16)fprintf(fp, "0"); // Regulate the length ofbyte size
  fprintf(fp, "%x", hash[i]);
 fprintf(fp, "\n"); fclose(fp);

 printf("Comparing results against golden output\n");
 if(system("diff -w out.dat")){
  fprintf(stdout, "--------------------------------------------------\n");
  fprintf(stdout, "FAIL: Output does not match with the golden output\n");
  fprintf(stdout, "--------------------------------------------------\n");
  return 1;
 } else {
  fprintf(stdout, "-----------------------------------------------\n");
  fprintf(stdout, "PASS: The output matches with the golden output\n");
  fprintf(stdout, "-----------------------------------------------\n");
  return 0;

Generated hash value: d7316dc4154e2861218bbb31b54e32e24e0667d0de321c4a108acb8fbf82e7c2

Save this hash value in a file ‘’ along with the test bench. This contains the verified output for a given input (in this case a string, “This is a test”). Note that just having an output does not mean the end of process, rather it should be validated using ‘proof-of-work‘.

The readers are kindly advised to go through the codes and understand the concepts and algorithms. Since you have a code with HLS compatible, if you have a little knowledge on Vivado HLS you can end up with an SHA-256 IP too. Note again that the code in the repo may not be efficient to date. Yes? Why can’t? You can modify the code to comply with the latest development in miners.

It would be great, if you can go through the resource links that I have linked in this post. There are number of concepts under this topic which I have not covered like double spending, block chaining, security methods, etc. Finally I hope that you might have at least convinced yourself that I have written something. Something beyond that; oh; I am done!!! Feel free to raise any question under this topic.