-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathh2.c
152 lines (115 loc) · 4.73 KB
/
h2.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
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
147
148
149
150
151
152
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "tiny_sha3/sha3.h"
#define HEADER_SIZE 80
#define HASH_OUT_SIZE 32
#define P0_SIZE 64
#define N_ITER 8
#define N_SUBSET P0_SIZE*N_ITER
#define N_ROT 32
#define N_INDEXES 4096
#define BYTE_ALIGNMENT 16
const char* dat_file_name = "verthash.dat";
const char* input_header_hex = "000000203a297b4b7685170d7644b43e5a6056234cc2414edde454a87580e1967d14c1078c13ea916117b0608732f3f65c2e03b81322efc0a62bcee77d8a9371261970a58a5a715da80e031b02560ad8";
static inline uint32_t fnv1a(const uint32_t a, const uint32_t b) {
return (a ^ b) * 0x1000193;
}
void verthash_hash(const unsigned char* blob_bytes, const size_t blob_size, const unsigned char* input, unsigned char* output) {
unsigned char p1[HASH_OUT_SIZE];
sha3(&input[0], HEADER_SIZE, &p1[0], HASH_OUT_SIZE);
unsigned char p0[N_SUBSET];
unsigned char input_header[HEADER_SIZE];
memcpy(input_header, input, HEADER_SIZE);
for(size_t i = 0; i < N_ITER; i++) {
input_header[0] += 1;
sha3(&input_header[0], HEADER_SIZE, p0+i*P0_SIZE, P0_SIZE);
}
uint32_t* p0_index = (uint32_t*)p0;
uint32_t seek_indexes[N_INDEXES];
for(size_t x = 0; x < N_ROT; x++) {
memcpy(seek_indexes+x*(N_SUBSET/sizeof(uint32_t)), p0, N_SUBSET);
for(size_t y = 0; y < N_SUBSET/sizeof(uint32_t); y++) {
*(p0_index + y) = (*(p0_index + y) << 1) | (1 & (*(p0_index + y) >> 31));
}
}
uint32_t* p1_32 = (uint32_t*)p1;
uint32_t* blob_bytes_32 = (uint32_t*)blob_bytes;
uint32_t value_accumulator = 0x811c9dc5;
const uint32_t mdiv = ((blob_size - HASH_OUT_SIZE)/BYTE_ALIGNMENT) + 1;
for(size_t i = 0; i < N_INDEXES; i++) {
const uint32_t offset = (fnv1a(seek_indexes[i], value_accumulator) % mdiv) * BYTE_ALIGNMENT/sizeof(uint32_t);
for(size_t i2 = 0; i2 < HASH_OUT_SIZE/sizeof(uint32_t); i2++) {
const uint32_t value = *(blob_bytes_32 + offset + i2);
uint32_t* p1_ptr = p1_32 + i2;
*p1_ptr = fnv1a(*p1_ptr, value);
value_accumulator = fnv1a(value_accumulator, value);
}
}
memcpy(output, p1, HASH_OUT_SIZE);
}
int main() {
FILE* datfile = fopen(dat_file_name, "rb");
fseek(datfile, 0, SEEK_END);
const size_t datfile_sz = ftell(datfile);
fseek(datfile, 0, SEEK_SET);
printf("allocating buffer...\n");
unsigned char* blob_bytes = malloc(datfile_sz);
printf("reading data file...\n");
const size_t bytes_read = fread(blob_bytes, 1, datfile_sz, datfile);
if(bytes_read != datfile_sz) {
return -1;
}
fclose(datfile);
unsigned char input_header[HEADER_SIZE];
for(size_t count = 0; count < HEADER_SIZE; count++) {
sscanf(input_header_hex + count*2, "%2hhx", &input_header[count]);
}
printf("SHA3 hashes...\n");
clock_t start, end;
double cpu_time_used_hashing, cpu_time_used_mem;
start = clock();
unsigned char p1[HASH_OUT_SIZE];
sha3(&input_header[0], HEADER_SIZE, &p1[0], HASH_OUT_SIZE);
unsigned char p0[N_SUBSET];
for(size_t i = 0; i < N_ITER; i++) {
input_header[0] += 1;
sha3(&input_header[0], HEADER_SIZE, p0+i*P0_SIZE, P0_SIZE);
}
uint32_t* p0_index = (uint32_t*)p0;
uint32_t seek_indexes[N_INDEXES];
for(size_t x = 0; x < N_ROT; x++) {
memcpy(seek_indexes+x*(N_SUBSET/sizeof(uint32_t)), p0, N_SUBSET);
for(size_t y = 0; y < N_SUBSET/sizeof(uint32_t); y++) {
*(p0_index + y) = (*(p0_index + y) << 1) | (1 & (*(p0_index + y) >> 31));
}
}
end = clock();
cpu_time_used_hashing = ((double) (end - start)) / CLOCKS_PER_SEC;
printf("memory seeks...\n");
start = clock();
uint32_t* p1_32 = (uint32_t*)p1;
uint32_t* blob_bytes_32 = (uint32_t*)blob_bytes;
uint32_t value_accumulator = 0x811c9dc5;
const uint32_t mdiv = ((datfile_sz - HASH_OUT_SIZE)/BYTE_ALIGNMENT) + 1;
for(size_t i = 0; i < N_INDEXES; i++) {
const uint32_t offset = (fnv1a(seek_indexes[i], value_accumulator) % mdiv) * BYTE_ALIGNMENT/sizeof(uint32_t);
for(size_t i2 = 0; i2 < HASH_OUT_SIZE/sizeof(uint32_t); i2++) {
const uint32_t value = *(blob_bytes_32 + offset + i2);
uint32_t* p1_ptr = p1_32 + i2;
*p1_ptr = fnv1a(*p1_ptr, value);
value_accumulator = fnv1a(value_accumulator, value);
}
}
end = clock();
cpu_time_used_mem = ((double) (end - start)) / CLOCKS_PER_SEC;
for(size_t i = 0; i < HASH_OUT_SIZE; i++) {
printf("%02X", p1[i]);
}
printf("\n");
printf("hash time: %f, mem time: %f, ratio: %f\n", cpu_time_used_hashing, cpu_time_used_mem, cpu_time_used_hashing/(cpu_time_used_hashing+cpu_time_used_mem));
free(blob_bytes);
return 0;
}