-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
executable file
·62 lines (47 loc) · 1.63 KB
/
main.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
62
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <time.h>
#include "pow.h"
#include "sha256.h"
#define START 25
#define COUNT 5
void check_hash(char *hash, struct proof *proofs, int sequence, int c, clock_t start) {
for (int i = 0; i < c; i++) {
if (proofs[i].found) continue;
int n = proofs[i].length;
clock_t time = clock() - start;
double time_taken = ((double) time) / CLOCKS_PER_SEC;
if (check_pow(hash, n)) {
proofs[i].time = time_taken;
proofs[i].found = true;
proofs[i].sequence = sequence;
proofs[i].hash = hash;
print_proof(proofs[i]);
return;
}
}
}
int main(int argc, char *argv[]) {
struct proof samples[COUNT];
init_proofs(samples, START, COUNT);
clock_t start = clock();
bool random = argc == 1 && argv[0] == "--random";
int nonce = 0;
while (true) {
char macondo[] = "Muchos años después, frente al pelotón de fusilamiento, el Coronel Aureliano Buendía había de "
"recordar aquella tarde remota en que su padre lo llevó a conocer el hielo. Macondo era entonces una "
"aldea de %d";
if (random) nonce = 0; //TODO: Get nonce randomly
else nonce++;
int length_nonce = (int) floor(log10(fabs(nonce))) + 1;
int n = sizeof(macondo) + length_nonce;
char plain[n];
sprintf(plain, macondo, nonce);
char hash[65];
sha256(plain, hash);
check_hash(hash, samples, nonce, COUNT, start);
if (proofs_completed(samples, COUNT)) break;
}
return 0;
}