-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwork_handler.c
More file actions
146 lines (130 loc) · 3.97 KB
/
Copy pathwork_handler.c
File metadata and controls
146 lines (130 loc) · 3.97 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "work_handler.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <inttypes.h>
#include "server.h"
#include "input_handler.h"
Queue *working_queue;
bool abort_flag;
void init_work_handler(Queue *queue) {
working_queue = queue;
abort_flag = false;
}
bool check_solution(SOLN_ARGS sol_args) {
/* This is what we will need to hash */
BYTE to_check[TOTAL_SIZE];
uint256_init(to_check);
int64 temp_solution = sol_args.nonce;
for (int i = 0; i < BYTE_LENGTH; i++) {
to_check[i] = sol_args.seed[i];
}
for(int i = TOTAL_SIZE - 1; i >= BYTE_LENGTH; i--) {
to_check[i] = temp_solution & 0xFF;
temp_solution = temp_solution >> 8;
}
/* Finding alpha and beta for the target */
int32 difficulty = sol_args.difficulty;
int32 alpha;
BYTE beta[BYTE_LENGTH];
uint256_init(beta);
alpha = ((((1 << 8) - 1) << 24) & difficulty) >> 24;
int32 beta_num = ((1 << 24) - 1) & difficulty;
fprintf(stdout, "Number beta is: %d\n", beta_num);
for(int i = BYTE_LENGTH - 1; i >= 0; i--) {
beta[i] = beta_num & 0xFF;
beta_num = beta_num >> 8;
}
fprintf(stdout, "Alpha is: %d\n", alpha);
fprintf(stdout, "Beta is:");
print_uint256(beta);
fprintf(stdout, "\n");
/* Need to find the exponent using alpha and beta */
BYTE target[BYTE_LENGTH];
uint256_init(target);
int32 exponent = 8 * (alpha - 3);
BYTE exponent_num[32];
uint256_init(exponent_num);
BYTE base[32];
uint256_init(base);
base[31] = 2;
uint256_exp(exponent_num, base, exponent);
uint256_mul(target, beta, exponent_num);
/* Now finding the hash */
BYTE hash[SHA256_BLOCK_SIZE];
uint256_init(hash);
BYTE hash_hash[SHA256_BLOCK_SIZE];
uint256_init(hash_hash);
SHA256_CTX *ctx = malloc(sizeof *ctx);
// First hash
sha256_init(ctx);
sha256_update(ctx, to_check, TOTAL_SIZE);
sha256_final(ctx, hash);
// Second hash
sha256_init(ctx);
sha256_update(ctx, hash, SHA256_BLOCK_SIZE);
sha256_final(ctx, hash_hash);
if (sha256_compare(target, hash_hash) > 0)
return true;
return false;
}
void do_work(Helper *args) {
fprintf(stdout, "%llu\n", args->start);
fprintf(stdout, "%llu\n", args->end);
for(int64 i = args->start; i <= args->end; i++) {
if(abort_flag == true) {
fprintf(stdout, "Setting abort flag to true");
return;
}
fflush(stdout);
int64 i_cpy = i;
for(int j = TOTAL_SIZE - 1; j >= BYTE_LENGTH; j--) {
args->x[j] = i_cpy & 0XFF;
i_cpy = i_cpy >> 8;
}
//fprintf(stdout, "%lld\n", i);
BYTE hash[SHA256_BLOCK_SIZE];
uint256_init(hash);
BYTE hash_hash[SHA256_BLOCK_SIZE];
uint256_init(hash_hash);
SHA256_CTX *ctx = malloc(sizeof *ctx);
// First hash
sha256_init(ctx);
sha256_update(ctx, args->x, TOTAL_SIZE);
sha256_final(ctx, hash);
// Second hash
sha256_init(ctx);
sha256_update(ctx, hash, SHA256_BLOCK_SIZE);
sha256_final(ctx, hash_hash);
if (sha256_compare(args->target, hash_hash) > 0) {
fprintf(stdout, "Hello\n");
char *message = malloc(sizeof(char) * (SOLN_LEN + 1));
sprintf(message, "SOLN %08x ", args->difficulty);
char *t = malloc(sizeof(char) * BYTE_LENGTH);
for(int i = 0; i < BYTE_LENGTH; i++) {
sprintf(t, "%02x", args->x[i]);
strcat(message, t);
}
sprintf(t, " %016llx", i);
strcat(message, t);
fflush(stdout);
my_send(message, args->sd);
return;
}
}
if(abort_flag == true) {
abort_flag = false;
}
return;
}
void set_abort_flag_true() {
abort_flag = true;
}
void set_abort_flag_false() {
abort_flag = false;
}