/***************************************************************************************************** * Description: Bitcoin Mining Test * * Author: Dengxue Yan, Washington University in St. Louis * * Email: Dengxue.Yan@wustl.edu * * Rev History: * * Dengxue Yan 10-20-2016 15:30 -- 1.00 Create *****************************************************************************************************/ //==================================================================================================== // Declaration of header files //---------------------------------------------------------------------------------------------------- // Lib headers #include #include #include // Self-defined headers #include "sha256.h" //==================================================================================================== // Declaration of local functions, these functions can not be accessed from outside //---------------------------------------------------------------------------------------------------- static int ArraySwap(BYTE *input, int size); static int BitcoinMingTest(void); //==================================================================================================== // Declaration of external functions called by this file //---------------------------------------------------------------------------------------------------- extern int BitcoinBlockHashing(BYTE *output, BYTE const *input, int size); //==================================================================================================== // Definition of local variables, these variables can not be accessed from outside //==================================================================================================== static BYTE buf[SHA256_BLOCK_SIZE]; //==================================================================================================== // Functions implementation //---------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------- // Interface functions //---------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------- // Function: main // Input: // Output: // Return: //Description: system entry // // Dengxue Yan 10-20-2016 15:30 Create //---------------------------------------------------------------------------------------------------- int main() { return BitcoinMingTest(); } //---------------------------------------------------------------------------------------------------- // Local functions realize //---------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------- // Function: ArraySwap // Input: BYTE *input // int size // Output: // Return: int //Description: // // Dengxue Yan 10-20-2016 15:30 Create //---------------------------------------------------------------------------------------------------- static int ArraySwap(BYTE *input, int size) { int index; BYTE byte_temp; for (index = 0; index < size / 2; index++) { byte_temp = input[index]; input[index] = input[SHA256_BLOCK_SIZE - 1 - index]; input[SHA256_BLOCK_SIZE - 1 - index] = byte_temp; } return 0; } //---------------------------------------------------------------------------------------------------- // Function: BitcoinMingTest // Input: void // Output: void // Return: int //Description: // // Dengxue Yan 10-20-2016 15:30 Create //---------------------------------------------------------------------------------------------------- static int BitcoinMingTest(void) { int index; unsigned int nonce; BYTE bitcoin_test_block1[] = { 0x01, 0x00, 0x00, 0x00,// ver 0x81, 0xcd, 0x02, 0xab, 0x7e, 0x56, 0x9e, 0x8b, 0xcd, 0x93, 0x17, 0xe2, 0xfe, 0x99, 0xf2, 0xde, 0x44, 0xd4, 0x9a, 0xb2, 0xb8, 0x85, 0x1b, 0xa4, 0xa3, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,// hashPrevBlock 0xe3, 0x20, 0xb6, 0xc2, 0xff, 0xfc, 0x8d, 0x75, 0x04, 0x23, 0xdb, 0x8b, 0x1e, 0xb9, 0x42, 0xae, 0x71, 0x0e, 0x95, 0x1e, 0xd7, 0x97, 0xf7, 0xaf, 0xfc, 0x88, 0x92, 0xb0, 0xf1, 0xfc, 0x12, 0x2b,// hashMerkleRoot 0xc7, 0xf5, 0xd7, 0x4d,// Time 0xf2, 0xb9, 0x44, 0x1a,// Bits (Target) 0x00, 0x00, 0x00, 0x00// Nonce(You need find the proper value of Nonce) }; // 0x42a14695 for (nonce = 0; nonce < 0xFFFFFFFFu; nonce++) { // We don't care the Endianess of nonce bitcoin_test_block1[sizeof(bitcoin_test_block1) - 1] = (nonce & 0xFF); bitcoin_test_block1[sizeof(bitcoin_test_block1) - 2] = ((nonce >> 8) & 0xFF); bitcoin_test_block1[sizeof(bitcoin_test_block1) - 3] = ((nonce >> 16) & 0xFF); bitcoin_test_block1[sizeof(bitcoin_test_block1) - 4] = ((nonce >> 24) & 0xFF); BitcoinBlockHashing(buf, bitcoin_test_block1, sizeof(bitcoin_test_block1)); if (0u == *((buf + sizeof(buf) - 1))) { break; } } if (0xFFFFFFFFu == nonce) { bitcoin_test_block1[sizeof(bitcoin_test_block1) - 1] = 0xFF; bitcoin_test_block1[sizeof(bitcoin_test_block1) - 2] = 0xFF; bitcoin_test_block1[sizeof(bitcoin_test_block1) - 3] = 0xFF; bitcoin_test_block1[sizeof(bitcoin_test_block1) - 4] = 0xFF; BitcoinBlockHashing(buf, bitcoin_test_block1, sizeof(bitcoin_test_block1)); if (0u == *((buf + sizeof(buf) - 1))) { printf("bitcoin_test_block1 mining failed!\n"); return -1; } } printf("bitcoin_test_block1 mining success at nonce=0x%08X\n", nonce); ArraySwap(buf, sizeof(buf)); for (index = 0; index < sizeof(buf); index++) { printf("%02x", (unsigned char)buf[index]); } printf("\n\n"); BYTE bitcoin_test_block2[] = { 0x02, 0x00, 0x00, 0x00,// ver 0x7e, 0xf0, 0x55, 0xe1, 0x67, 0x4d, 0x2e, 0x65, 0x51, 0xdb, 0xa4, 0x1c, 0xd2, 0x14, 0xde, 0xbb, 0xee, 0x34, 0xae, 0xb5, 0x44, 0xc7, 0xec, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,// hashPrevBlock 0xd3, 0x99, 0x89, 0x63, 0xf8, 0x0c, 0x5b, 0xab, 0x43, 0xfe, 0x8c, 0x26, 0x22, 0x8e, 0x98, 0xd0, 0x30, 0xed, 0xf4, 0xdc, 0xbe, 0x48, 0xa6, 0x66, 0xf5, 0xc3, 0x9e, 0x2d, 0x7a, 0x88, 0x5c, 0x91,// hashMerkleRoot 0x02, 0xc8, 0x6d, 0x53,// Time 0x6c, 0x89, 0x00, 0x19,// Bits (Target) 0x00, 0x00, 0x00, 0x00// Nonce(You need find the proper value of Nonce) }; // 0x593a470d for (nonce = 0; nonce < 0xFFFFFFFFu; nonce++) { bitcoin_test_block2[sizeof(bitcoin_test_block2) - 1] = (nonce & 0xFF); bitcoin_test_block2[sizeof(bitcoin_test_block2) - 2] = ((nonce >> 8) & 0xFF); bitcoin_test_block2[sizeof(bitcoin_test_block2) - 3] = ((nonce >> 16) & 0xFF); bitcoin_test_block2[sizeof(bitcoin_test_block2) - 4] = ((nonce >> 24) & 0xFF); BitcoinBlockHashing(buf, bitcoin_test_block2, sizeof(bitcoin_test_block2)); if (0u == *((buf + sizeof(buf) - 1))) { break; } } if (0xFFFFFFFFu == nonce) { bitcoin_test_block2[sizeof(bitcoin_test_block2) - 1] = 0xFF; bitcoin_test_block2[sizeof(bitcoin_test_block2) - 2] = 0xFF; bitcoin_test_block2[sizeof(bitcoin_test_block2) - 3] = 0xFF; bitcoin_test_block2[sizeof(bitcoin_test_block2) - 4] = 0xFF; BitcoinBlockHashing(buf, bitcoin_test_block2, sizeof(bitcoin_test_block2)); if (0u == *((buf + sizeof(buf) - 1))) { printf("bitcoin_test_block2 mining failed!\n"); return -1; } } printf("bitcoin_test_block2 mining success at nonce=0x%08X\n", nonce); ArraySwap(buf, sizeof(buf)); for (index = 0; index < sizeof(buf); index++) { printf("%02x", (unsigned char)buf[index]); } printf("\n\n"); return 0; }