Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //batcoin.h #ifndef BATCOIN_H #define BATCOIN_H #include using namespace std; class BlockHeader { public: // Initializes a new BlockHeader to all zeroes BlockHeader(); //

C++

image text in transcribed

image text in transcribed

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//batcoin.h

#ifndef BATCOIN_H #define BATCOIN_H #include  using namespace std; class BlockHeader { public: // Initializes a new BlockHeader to all zeroes BlockHeader(); // Returns a string representation of the BlockHeader values. // For example: // // prev: 114 64 204 51 183 55 8 239 // nonce: 39 196 114 213 34 95 241 38 // string to_string(); // These store 8 bytes of information each, // storing each byte as an unsigned char. unsigned char prev[8]; // Previous BlockHeader hash value unsigned char nonce[8]; // The nonce for this BlockHeader }; // The hash function used in the Batcoin protocol. void bathash(BlockHeader b, unsigned char* result); #endif 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//batcoin.cpp

 #include  #include "batcoin.h" using namespace std; BlockHeader :: BlockHeader() { for (int i = 0; i > 1) ^ (result[0] >> 3) ^ (result[0] >> 6); T[3] = (result[4] >> 2) ^ (result[4] >> 5) ^ (result[4] >> 7); result[3] = result[3] + result[7] + T[0] + T[3]; T[4] = result[7] + T[0] + T[1] + T[2] + T[3]; for (int i = 6; i >= 0; --i) result[i+1] = result[i]; result[0] = T[4]; } } //////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//mine.h

 #ifndef MINE_H #define MINE_H #include "batcoin.h" // Returns a BlockHeader whose prev bytes are equal to the // output of bathash(prev) and that hashes to a set of bytes // where the first difficulty of them are equal to 0. BlockHeader mine(BlockHeader prev, int difficulty); #endif 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//main.cpp
 #include  #include "batcoin.h" #include "mine.h" using namespace std; inline void _test(const char* expression, const char* file, int line) { cerr  

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//mine.cpp

#include "mine.h"

// Returns a BlockHeader whose prev bytes are equal to the

// output of bathash(prev) and that hashes to a set of bytes

// where the first difficulty of them are equal to 0.

BlockHeader mine(BlockHeader prev, int difficulty)

{

BlockHeader b;

unsigned char result[8];

int dif = 0;

bathash(prev, result);

b.prev;

for (int i = 0; i

{

b.prev[i] = result[i];

}

for (int i = 0; i

{

b.nonce[i] = 0;

}

while (true)

{

bathash(b, result);

if (difficulty == 1)

{

if (b.nonce[0] == 0)

{

return b;

}

}

if (difficulty == 2)

{

}

if (difficulty == 3)

{

}

}

}

In this homework, you'l implement a mining algorithm for a simplified version of the Bitooin cryptocurrency called Batcoin. 1.1 Mining The computationally challenging task used by Batcoin is searching for a value, called a nonce. The nonce must have the property that when a certain hash function is applied to the nonce, the result begins with several zeroes. The minimum number of zeroes is specified by the difficulty parameter of the currency, which changes over time for market reasons. The hash function used by Batcoin is difficult to predict, so there are (probably) no shorteuts to finding a nonce with the desired property. Thus finding such a nonce (ie. mining a coin) consists of "brute force" trying of many different nonces until one is found. 1.2 Block headers and blockchains Each mined coin contains a block header consisting of the nonce, plus the hashed value of the previous block header. The nonce is valid if the bathashed value of the block header (consisting of the bathashed value of the previous block header and the nonce) starts with at least as many zeroes as the specified difficulty. Thus the block headers of mined coins form sequences called blockchains (see Figure 1). Bitooin uses just one blockchain that acts as a "global record" of who mined coins. Figure A portion of a blockchain for Batcoin at difficulty 2 (BlockHeaders hash to values that start with 2 zeroes). Successfully mining a coin means extending the blockchain by one new block header before anyone In this homework, you'l implement a mining algorithm for a simplified version of the Bitooin cryptocurrency called Batcoin. 1.1 Mining The computationally challenging task used by Batcoin is searching for a value, called a nonce. The nonce must have the property that when a certain hash function is applied to the nonce, the result begins with several zeroes. The minimum number of zeroes is specified by the difficulty parameter of the currency, which changes over time for market reasons. The hash function used by Batcoin is difficult to predict, so there are (probably) no shorteuts to finding a nonce with the desired property. Thus finding such a nonce (ie. mining a coin) consists of "brute force" trying of many different nonces until one is found. 1.2 Block headers and blockchains Each mined coin contains a block header consisting of the nonce, plus the hashed value of the previous block header. The nonce is valid if the bathashed value of the block header (consisting of the bathashed value of the previous block header and the nonce) starts with at least as many zeroes as the specified difficulty. Thus the block headers of mined coins form sequences called blockchains (see Figure 1). Bitooin uses just one blockchain that acts as a "global record" of who mined coins. Figure A portion of a blockchain for Batcoin at difficulty 2 (BlockHeaders hash to values that start with 2 zeroes). Successfully mining a coin means extending the blockchain by one new block header before anyone

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

From Herds To Insights Harnessing Data Analytics For Sustainable Livestock Farming

Authors: Prof Suresh Neethirajan

1st Edition

B0CFD6K6KK, 979-8857075487

More Books

Students also viewed these Databases questions