#include #include #include #include "sha256.h" 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; } BYTE buf[SHA256_BLOCK_SIZE]; extern int BitcoinMining(BYTE *output, BYTE const *input, int size); int main() { unsigned int nonce; BYTE bitcoin_test_block1[] = { 0x01, 0x00, 0x00, 0x00, 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, 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, 0xc7, 0xf5, 0xd7, 0x4d, 0xf2, 0xb9, 0x44, 0x1a, 0x00, 0x00, 0x00, 0x00, }; for (nonce = 0; nonce < 0xFFFFFFFFu; nonce++) { // We don't care the *((unsigned int *)&bitcoin_test_block1[sizeof(bitcoin_test_block1) - 4]) = nonce; BitcoinBlockHashing(buf, bitcoin_test_block1, sizeof(bitcoin_test_block1)); if ((0 == *((unsigned int *)(buf + sizeof(buf) - 4))) && (0 == *((unsigned int *)(buf + sizeof(buf) - 8)))) { break; } } if (0xFFFFFFFFu == nonce) { *((unsigned int *)&bitcoin_test_block1[sizeof(bitcoin_test_block1) - 4]) = nonce; BitcoinBlockHashing(buf, bitcoin_test_block1, sizeof(bitcoin_test_block1)); if ((0 != *((unsigned int *)(buf + sizeof(buf) - 4))) || (0 != *((unsigned int *)(buf + sizeof(buf) - 8)))) { 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]); } BYTE bitcoin_test_block2[] = { 0x01, 0x00, 0x00, 0x00, 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, 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, 0xc7, 0xf5, 0xd7, 0x4d, 0xf2, 0xb9, 0x44, 0x1a, 0x00, 0x00, 0x00, 0x00, }; for (nonce = 0; nonce < 0xFFFFFFFFu; nonce++) { // We don't care the *((unsigned int *)&bitcoin_test_block2[sizeof(bitcoin_test_block2) - 4]) = nonce; BitcoinBlockHashing(buf, bitcoin_test_block2, sizeof(bitcoin_test_block2)); if ((0 == *((unsigned int *)(buf + sizeof(buf) - 4))) && (0 == *((unsigned int *)(buf + sizeof(buf) - 8)))) { break; } } if (0xFFFFFFFFu == nonce) { *((unsigned int *)&bitcoin_test_block2[sizeof(bitcoin_test_block2) - 4]) = nonce; BitcoinBlockHashing(buf, bitcoin_test_block2, sizeof(bitcoin_test_block2)); if ((0 != *((unsigned int *)(buf + sizeof(buf) - 4))) || (0 != *((unsigned int *)(buf + sizeof(buf) - 8)))) { printf("mining failed!\n"); return -1; } } printf("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]); } }