-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_mine.c
68 lines (54 loc) · 1.27 KB
/
test_mine.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
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>
#include <error.h>
#include <assert.h>
#include <string.h>
#include <linux/types.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <linux/udp.h>
#include "mine.h"
#include "block_123.h"
static inline int mine(struct sha256_ctx *sum,
struct block_header *bh,
__u8 buf[BUF_SIZE_BYTES],
__u8 difficulty[32])
{
int ret, i;
for (i = 0; i < 65536; i++) {
memcpy(buf, bh, sizeof(*bh));
ret = hash_block(sum, buf);
if (ret)
return -1;
if (meets_difficulty(sum, difficulty))
return i;
bh->nonce++;
}
return -1;
}
int main(int argc, char **argv)
{
/* to fit sizeof(struct block_header) and padding */
__u8 buf[BUF_SIZE_BYTES] = {};
struct block_header bh;
struct sha256_ctx sum;
__u8 difficulty[32];
int ret;
block_123_init(&bh, difficulty);
/* go back 15 nonces */
bh.nonce -= 15;
ret = mine(&sum, &bh, buf, difficulty);
printf("mine() = %d\n", ret);
if (ret >= 0)
sha256_print(&sum);
assert(sum.h[0] == 0x00000000 &&
sum.h[1] == 0xa3bbe4fd &&
sum.h[2] == 0x1da16a29 &&
sum.h[3] == 0xdbdaba01 &&
sum.h[4] == 0xcc35d6fc &&
sum.h[5] == 0x74ee17f7 &&
sum.h[6] == 0x94cf3aab &&
sum.h[7] == 0x94f7aaa0);
printf("ok\n");
return 0;
}