forked from mbevand/silentarmy
-
Notifications
You must be signed in to change notification settings - Fork 16
/
verus_hash.cpp
181 lines (160 loc) · 4.93 KB
/
verus_hash.cpp
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// (C) 2018 The Verus Developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
/*
This provides the PoW hash function for Verus, a CPU-optimized hash
function with a Haraka V2 core. Unlike Haraka, which is made for short
inputs only, Verus Hash takes any length of input and produces a 256
bit output.
*/
#include <string.h>
//#include "common.h"
#include "verus_hash.h"
void (*CVerusHash::haraka512Function)(unsigned char *out, const unsigned char *in);
void CVerusHash::Hash(void *result, const void *data, size_t _len)
{
unsigned char buf[128];
unsigned char *bufPtr = buf;
int nextOffset = 64;
uint32_t pos = 0, len = _len;
unsigned char *bufPtr2 = bufPtr + nextOffset;
unsigned char *ptr = (unsigned char *)data;
// put our last result or zero at beginning of buffer each time
memset(bufPtr, 0, 32);
// digest up to 32 bytes at a time
for ( ; pos < len; pos += 32)
{
if (len - pos >= 32)
{
memcpy(bufPtr + 32, ptr + pos, 32);
}
else
{
int i = (int)(len - pos);
memcpy(bufPtr + 32, ptr + pos, i);
memset(bufPtr + 32 + i, 0, 32 - i);
}
(*haraka512Function)(bufPtr2, bufPtr);
bufPtr2 = bufPtr;
bufPtr += nextOffset;
nextOffset *= -1;
}
memcpy(result, bufPtr, 32);
};
void CVerusHash::init()
{
haraka512Function = &haraka512_port_zero;
}
CVerusHash &CVerusHash::Write(const unsigned char *data, size_t _len)
{
unsigned char *tmp;
uint32_t pos, len = _len;
// digest up to 32 bytes at a time
for ( pos = 0; pos < len; )
{
uint32_t room = 32 - curPos;
if (len - pos >= room)
{
memcpy(curBuf + 32 + curPos, data + pos, room);
(*haraka512Function)(result, curBuf);
tmp = curBuf;
curBuf = result;
result = tmp;
pos += room;
curPos = 0;
}
else
{
memcpy(curBuf + 32 + curPos, data + pos, len - pos);
curPos += len - pos;
pos = len;
}
}
return *this;
}
// to be declared and accessed from C
void verus_hash(void *result, const void *data, size_t len)
{
return CVerusHash::Hash(result, data, len);
}
void (*CVerusHashV2::haraka512Function)(unsigned char *out, const unsigned char *in);
void (*CVerusHashV2::haraka512KeyedFunction)(unsigned char *out, const unsigned char *in, const u128 *rc);
void (*CVerusHashV2::haraka256Function)(unsigned char *out, const unsigned char *in);
void CVerusHashV2::init()
{
if (IsCPUVerusOptimized())
{
load_constants();
haraka512Function = &haraka512;
haraka512KeyedFunction = &haraka512_keyed;
haraka256Function = &haraka256;
}
else
{
// load the haraka constants
load_constants_port();
haraka512Function = &haraka512_port;
haraka512KeyedFunction = &haraka512_port_keyed;
haraka256Function = &haraka256_port;
}
}
void CVerusHashV2::Hash(void *result, const void *data, size_t len)
{
unsigned char buf[128];
unsigned char *bufPtr = buf;
int pos = 0, nextOffset = 64;
unsigned char *bufPtr2 = bufPtr + nextOffset;
unsigned char *ptr = (unsigned char *)data;
// put our last result or zero at beginning of buffer each time
memset(bufPtr, 0, 32);
// digest up to 32 bytes at a time
for ( ; pos < len; pos += 32)
{
if (len - pos >= 32)
{
memcpy(bufPtr + 32, ptr + pos, 32);
}
else
{
int i = (int)(len - pos);
memcpy(bufPtr + 32, ptr + pos, i);
memset(bufPtr + 32 + i, 0, 32 - i);
}
(*haraka512Function)(bufPtr2, bufPtr);
bufPtr2 = bufPtr;
bufPtr += nextOffset;
nextOffset *= -1;
}
memcpy(result, bufPtr, 32);
};
CVerusHashV2 &CVerusHashV2::Write(const unsigned char *data, size_t len)
{
unsigned char *tmp;
// digest up to 32 bytes at a time
for ( int pos = 0; pos < len; )
{
int room = 32 - curPos;
if (len - pos >= room)
{
memcpy(curBuf + 32 + curPos, data + pos, room);
(*haraka512Function)(result, curBuf);
tmp = curBuf;
curBuf = result;
result = tmp;
pos += room;
curPos = 0;
}
else
{
memcpy(curBuf + 32 + curPos, data + pos, len - pos);
curPos += len - pos;
pos = len;
}
}
return *this;
}
// to be declared and accessed from C
void verus_hash_v2(void *result, const void *data, size_t len)
{
return CVerusHashV2::Hash(result, data, len);
}