-
Notifications
You must be signed in to change notification settings - Fork 1
/
clm_hash2.c
229 lines (190 loc) · 5.52 KB
/
clm_hash2.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <sys/types.h>
#include <stdint.h>
#include <string.h>
#include <x86intrin.h>
#include "clm_hash2.h"
static const __m128i k1 = { 0xc3a5c85c97cb3127, 0xb492b66fbe98f273 };
static const __m128i s1 = { 0x9ae16a3b2f90404f, 0x9ae16a3b2f90404f };
static const __m128i s2 = { 0xc949d7c7509e6557, 0xc949d7c7509e6557 };
static const __m128i s3 = { 0xff51afd7ed558ccd, 0xff51afd7ed558ccd };
static const __m128i s4 = { 0xc4ceb9fe1a85ec53, 0xc4ceb9fe1a85ec53 };
static const __m128i s5 = { 0xc05bbd6b47c57574, 0xf83c1e9e4a934534 };
static const __m128i s6 = { 0xa224e4507e645d91, 0xe7a9131a4b842813 };
static const __m128i s7 = { 0x58f082e3580f347e, 0xceb5795349196aff };
static const __m128i s8 = { 0xb549200b5168588f, 0xfd5c07200540ada5 };
static inline void
mix1024(__m128i * const h1, __m128i * const h2, __m128i * const h3,
__m128i * const h4, __m128i * const h5, __m128i * const h6,
__m128i * const h7, __m128i * const h8)
{
__m128i x1, x2, x3, x4, x5, x6, x7, x8;
x1 = _mm_xor_si128(*h1, k1);
x2 = _mm_xor_si128(*h2, k1);
x3 = _mm_xor_si128(*h3, k1);
x4 = _mm_xor_si128(*h4, k1);
x5 = _mm_xor_si128(*h5, k1);
x6 = _mm_xor_si128(*h6, k1);
x7 = _mm_xor_si128(*h7, k1);
x8 = _mm_xor_si128(*h8, k1);
*h1 = _mm_clmulepi64_si128(x1, x2, 0x10);
*h2 = _mm_clmulepi64_si128(x2, x3, 0x10);
*h3 = _mm_clmulepi64_si128(x3, x4, 0x10);
*h4 = _mm_clmulepi64_si128(x4, x5, 0x10);
*h5 = _mm_clmulepi64_si128(x5, x6, 0x10);
*h6 = _mm_clmulepi64_si128(x6, x7, 0x10);
*h7 = _mm_clmulepi64_si128(x7, x8, 0x10);
*h8 = _mm_clmulepi64_si128(x8, x1, 0x10);
}
static inline __m128i
final128(const __m128i h)
{
__m128i a = h;
a = _mm_xor_si128(a, _mm_srli_epi32(a, 16));
a = _mm_mullo_epi32(a, k1);
a = _mm_xor_si128(a, _mm_srli_epi32(a, 16));
a = _mm_mullo_epi32(a, s1);
a = _mm_xor_si128(a, _mm_srli_epi32(a, 16));
return a;
}
void
clm_hash2_start_seed(clm_hash2_state_t * const state, const void * const seed)
{
__m128i s;
s = _mm_loadu_si128(seed);
state->x1 = _mm_xor_si128(s1, s);
state->x2 = _mm_xor_si128(s2, s);
state->x3 = _mm_xor_si128(s3, s);
state->x4 = _mm_xor_si128(s4, s);
state->x5 = _mm_xor_si128(s5, s);
state->x6 = _mm_xor_si128(s6, s);
state->x7 = _mm_xor_si128(s7, s);
state->x8 = _mm_xor_si128(s8, s);
state->block_size = sizeof(state->buf);
state->count = 0;
state->total_len = 0;
}
void
clm_hash2_start(clm_hash2_state_t * const state)
{
uint64_t seed[2] = { 0, 0 };
clm_hash2_start_seed(state, seed);
}
void
clm_hash2_update(clm_hash2_state_t * const state, const void * const data,
const size_t len)
{
__m128i *v;
__m128i x1, x2, x3, x4, x5, x6, x7, x8;
const unsigned char *curr = data;
size_t llen;
uint32_t left;
if (data == NULL || len == 0)
return;
state->total_len += len;
if (state->count + len < state->block_size) {
memcpy(state->buf.chars + state->count, data, len);
state->count += len;
return;
}
llen = len;
if (state->count > 0) {
left = state->block_size - state->count;
memcpy(state->buf.chars + state->count, data, left);
llen -= left;
curr += left;
}
x1 = state->x1;
x2 = state->x2;
x3 = state->x3;
x4 = state->x4;
x5 = state->x5;
x6 = state->x6;
x7 = state->x7;
x8 = state->x8;
if (state->count > 0) {
v = state->buf.ints;
x1 = _mm_xor_si128(x1, v[0]);
x2 = _mm_xor_si128(x2, v[1]);
x3 = _mm_xor_si128(x3, v[2]);
x4 = _mm_xor_si128(x4, v[3]);
x5 = _mm_xor_si128(x5, v[4]);
x6 = _mm_xor_si128(x6, v[5]);
x7 = _mm_xor_si128(x7, v[6]);
x8 = _mm_xor_si128(x8, v[7]);
mix1024(&x1, &x2, &x3, &x4, &x5, &x6, &x7, &x8);
state->count = 0;
}
while (llen >= state->block_size) {
v = (__m128i_u *)curr;
x1 = _mm_xor_si128(x1, _mm_loadu_si128(&v[0]));
x2 = _mm_xor_si128(x2, _mm_loadu_si128(&v[1]));
x3 = _mm_xor_si128(x3, _mm_loadu_si128(&v[2]));
x4 = _mm_xor_si128(x4, _mm_loadu_si128(&v[3]));
x5 = _mm_xor_si128(x5, _mm_loadu_si128(&v[4]));
x6 = _mm_xor_si128(x6, _mm_loadu_si128(&v[5]));
x7 = _mm_xor_si128(x7, _mm_loadu_si128(&v[6]));
x8 = _mm_xor_si128(x8, _mm_loadu_si128(&v[7]));
mix1024(&x1, &x2, &x3, &x4, &x5, &x6, &x7, &x8);
curr += state->block_size;
llen -= state->block_size;
}
state->x1 = x1;
state->x2 = x2;
state->x3 = x3;
state->x4 = x4;
state->x5 = x5;
state->x6 = x6;
state->x7 = x7;
state->x8 = x8;
if (llen > 0) {
memcpy(state->buf.chars, curr, llen);
state->count = llen;
}
}
static const unsigned char padding[256] = { 0x80 };
void
clm_hash2_end(clm_hash2_state_t * const state, clm_hash2_t * const hash)
{
__m128i x1, x2, x3, x4, x5, x6, x7, x8;
__m128i h[8];
uint64_t total_len;
uint32_t len, i;
total_len = state->total_len;
len = (state->count < 120) ? 120 - state->count : 248 - state->count;
clm_hash2_update(state, padding, len);
clm_hash2_update(state, &total_len, 8);
x1 = state->x1;
x2 = state->x2;
x3 = state->x3;
x4 = state->x4;
x5 = state->x5;
x6 = state->x6;
x7 = state->x7;
x8 = state->x8;
for (i = 0; i < 9; i++)
mix1024(&x1, &x2, &x3, &x4, &x5, &x6, &x7, &x8);
h[0] = final128(x1);
h[1] = final128(x2);
h[2] = final128(x3);
h[3] = final128(x4);
h[4] = final128(x5);
h[5] = final128(x6);
h[6] = final128(x7);
h[7] = final128(x8);
memcpy(hash, h, sizeof(h));
}
void
clm_hash2_seed(const void * const data, const size_t len,
const void * const seed, clm_hash2_t * const hash)
{
clm_hash2_state_t clm_hash;
clm_hash2_start_seed(&clm_hash, seed);
clm_hash2_update(&clm_hash, data, len);
clm_hash2_end(&clm_hash, hash);
}
void
clm_hash2(const void * const data, const size_t len, clm_hash2_t * const hash)
{
uint64_t seed[2] = { 0, 0 };
clm_hash2_seed(data, len, seed, hash);
}