-
Notifications
You must be signed in to change notification settings - Fork 10
/
serial_baseline.c
61 lines (52 loc) · 1.21 KB
/
serial_baseline.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <stdlib.h>
#include "sha256.h"
#include "utils.h"
#include "test.h"
#define NUM_EXPERIMENTS 7
//Define when you want to break out of the loop when the block is solved.
//#define MINING_MODE
int main(int argc, char *argv) {
int i, j, k;
unsigned char *data = test_block;
unsigned char hash[32], difficulty[32];
SHA256_CTX ctx;
Nonce_result nr;
initialize_nonce_result(&nr);
unsigned int nBits = ENDIAN_SWAP_32(*((unsigned int *) (data + 72)));
set_difficulty(difficulty, nBits);
int hashes = 1;
for(i=0; i<32; i++) {
tick();
for(j=0; j<hashes; j++) {
//Hash the block header
sha256_init(&ctx);
sha256_update(&ctx, data, 80);
sha256_final(&ctx, hash);
//Hash
sha256_init(&ctx);
sha256_update(&ctx, hash, 32);
sha256_final(&ctx, hash);
//Check the difficulty
k=0;
while(hash[k] == difficulty[k]) k++;
if(hash[k] < difficulty[k]) {
nr.nonce_found = true;
nr.nonce = j;
#ifdef MINING_MODE
break;
#endif
}
}
tock();
//Print hashes, execution time
printf("%d,%ld\n",hashes,get_execution_time());
hashes <<= 1;
}
if(nr.nonce_found) {
printf("Nonce found! %.8x\n", nr.nonce);
}
else {
printf("Nonce not found :(\n");
}
}