-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
432 lines (328 loc) · 11.1 KB
/
main.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
template<typename T, typename T_half, int size>
class double_uint {
private:
void fromstring(const std::string &s);
std::string tostring(void) const;
public:
T lo_, hi_;
enum type_size {
tsize = size,
hsize = size / 2,
qsize = size / 4
};
double_uint(const double_uint &u) : lo_(u.lo_), hi_(u.hi_) {}
double_uint(const T_half &hi1, const T_half &hi2, const T_half &lo1, const T_half &lo2)
: lo_(T(lo2) + (T(lo1) << qsize)),
hi_(T(hi2) + (T(hi1) << qsize)) {}
double_uint(const T &hi, const T &lo) : lo_(lo), hi_(hi) {}
explicit double_uint(const T &lo) : lo_(lo), hi_() {}
explicit double_uint(const std::string &s) { fromstring(s); };
double_uint(void) : lo_(), hi_() {}
/* Hack - basically we are a little-endian integer type */
explicit double_uint(const unsigned char *c) { memcpy(this, c, sizeof(*this)); }
explicit double_uint(size_t len, const unsigned char *c) {
if (len >= sizeof(*this)) {
memcpy(this, c, sizeof(*this));
} else {
memcpy(this, c, len);
memset(reinterpret_cast<unsigned char *>(this) + len, 0, sizeof(*this) - len);
}
}
friend std::ostream &operator<<(std::ostream &o, const double_uint &x) {
return o << std::string(x);
}
double_uint &operator=(int x) {
lo_ = x;
hi_ = T();
return *this;
}
double_uint &operator=(const double_uint &u) {
lo_ = u.lo_;
hi_ = u.hi_;
return *this;
}
operator T() const { return lo_; }
operator T() { return lo_; }
operator const std::string() const { return tostring(); }
operator std::string() { return tostring(); }
double_uint operator+=(const double_uint &u) {
T old = lo_;
lo_ += u.lo_;
if (lo_ < old) hi_++;
hi_ += u.hi_;
return *this;
}
double_uint operator-=(const double_uint &u) {
T old = lo_;
lo_ -= u.lo_;
if (lo_ > old) hi_--;
hi_ -= u.lo_;
return *this;
}
double_uint operator+(const double_uint &u) const {
double_uint res(*this);
return res += u;
}
double_uint operator-(const double_uint &u) const {
double_uint res(*this);
return res -= u;
}
double_uint operator++(void) {
return *this += double_uint(1);
}
double_uint operator++(int) {
double_uint res(*this);
*this += double_uint(0, 1);
return res;
}
double_uint operator-(void) {
return double_uint(0) - *this;
}
double_uint operator+(void) const {
return *this;
}
double_uint operator<<=(int x) {
x &= (tsize - 1);
if (x >= hsize) {
hi_ = lo_ << (x - hsize);
lo_ = 0;
return *this;
}
hi_ <<= x;
hi_ += lo_ >> (hsize - x);
lo_ <<= x;
return *this;
}
double_uint operator>>=(int x) {
x &= (tsize - 1);
if (x >= hsize) {
lo_ = hi_ >> (x - hsize);
hi_ = 0;
return *this;
}
lo_ >>= x;
lo_ += hi_ << (hsize - x);
hi_ >>= x;
return *this;
}
double_uint operator<<(int x) const {
double_uint res(*this);
return res <<= x;
}
double_uint operator>>(int x) const {
double_uint res(*this);
return res >>= x;
}
static double_uint halfmul(T x, T y) {
T_half x1, x2, y1, y2;
x1 = x;
x2 = x >> qsize;
y1 = y;
y2 = y >> qsize;
T xx1(x1);
T xx2(x2);
T yy1(y1);
T yy2(y2);
double_uint p1, p2, p3, p4;
p1.lo_ = xx1 * yy1;
p2.lo_ = xx1 * yy2;
p3.lo_ = xx2 * yy1;
p4.lo_ = xx2 * yy2;
p2 <<= qsize;
p3 <<= qsize;
p4 <<= hsize;
return p1 + p2 + p3 + p4;
}
double_uint operator*=(const double_uint &u) {
double_uint p1 = halfmul(lo_, u.lo_);
double_uint p2 = halfmul(lo_, u.hi_);
double_uint p3 = halfmul(hi_, u.lo_);
p2 <<= hsize;
p3 <<= hsize;
return (*this = p1 + p2 + p3);
}
double_uint operator*(const double_uint &u) const {
double_uint res(*this);
return res *= u;
}
double_uint operator^=(const double_uint &u) {
lo_ ^= u.lo_;
hi_ ^= u.hi_;
return *this;
}
double_uint operator^(const double_uint &u) const {
double_uint res(*this);
return res ^= u;
}
/* Calculates (self * u, folded in half) xor u */
double_uint fold(const double_uint &u) const {
typedef double_uint<double_uint, T, tsize * 2> Tdouble;
Tdouble x(Tdouble::halfmul(*this, u));
return x.lo_ ^ x.hi_;
}
};
template<class T, class T_half, int size>
std::string double_uint<T, T_half, size>::tostring(void) const {
int num = size / 4;
static const char hex_symb[] = "0123456789ABCDEF";
/* Pad with zeros */
std::string s(num, '0');
double_uint<T, T_half, size> v(*this);
for (int i = num - 1; i >= 0; i--) {
s[i] = hex_symb[v.lo_ & 0xf];
v >>= 4;
}
return s;
}
template<class T, class T_half, int size>
void double_uint<T, T_half, size>::fromstring(const std::string &s) {
double_uint<T, T_half, size> temp;
*this = 0;
for (std::string::const_iterator i = s.begin(); i != s.end(); i++) {
if ((*i >= '0') && (*i <= '9')) {
temp = *i - '0';
} else if ((*i >= 'A') && (*i <= 'F')) {
temp = *i - 'A' + 10;
} else if ((*i >= 'a') && (*i <= 'f')) {
temp = *i - 'A' + 10;
} else {
throw std::runtime_error("Invalid hex character\n");
}
*this <<= 4;
*this += temp;
}
}
static std::string tostring(const __uint128_t &x) {
int num = 128 / 4;
static const char hex_symb[] = "0123456789ABCDEF";
/* Pad with zeros */
std::string s(num, '0');
__uint128_t v(x);
for (int i = num - 1; i >= 0; i--) {
s[i] = hex_symb[v & 0xf];
v >>= 4;
}
return s;
}
std::ostream &operator<<(std::ostream &o, const __uint128_t &x) {
return o << tostring(x);
}
typedef double_uint<__uint128_t, unsigned long long, 256> u256;
void hash_step(const u256 &i1, const u256 &i2, u256 &o1, u256 &o2) {
/*
* Cube roots of primes.
* (Square roots don't work as well when folded with zero.
* The high part becomes nearly all one bits.)
*
* They are scaled so that the uppermost bit is set.
* Then the lowest bit is also set, so that the constant is odd
*/
/* cbrt(2) */
static const u256 t1(0xa14517cc6b945711, 0x1eed5b8adf128686,
0x144788148b18fde0, 0x30c00661b7d16e9d);
/* cbrt(3) */
static const u256 t2(0xb89ba24891f7b2e6, 0xef3f8b62b71933e0,
0x50c4a6157ab766cc, 0xfa2ba143e9029653);
/* cbrt(5) */
static const u256 t3(0xdae07de7f6269d97, 0xed0ddb59924b141a,
0x0ae36687aa58c29f, 0xe8293af2918f493b);
/* cbrt(7) */
static const u256 t4(0xf4daedd2c0c4edde, 0x50536bb743875dac,
0xfdb214852ccf272e, 0x53a3540f5e5aa011);
/* cbrt(11) */
static const u256 t5(0x8e55b096fcd22d4e, 0x3c1e6d4936833117,
0x0ae1a0b51ea515b2, 0x6ef98efb6ebf35e3);
/* cbrt(13) */
static const u256 t6(0x967c447c6d817406, 0x7bc5196b06dc9887,
0x214ac2f50046dc65, 0x0f9bfa326367aeb7);
/* cbrt(17) */
static const u256 t7(0xa48fe0a92bc653e6, 0xec03c7ed7e59981b,
0x3e3a27d8d8e54797, 0xd607fe20b08d6175);
/* cbrt(19) */
static const u256 t8(0xaac717b5769b6046, 0x27896d0e27f2c11e,
0x281e73be041f0383, 0xa937169045fb3849);
/* cbrt(23) */
static const u256 t9(0xb601eaa628c0c090, 0xac51900eab494a5c,
0x236edd364b4df8c4, 0x5a0cdaed7df05aed);
o1 = i1;
o2 = i2;
o1 += (o2 ^ t1).fold(t1);
o2 += (o1 ^ t2).fold(t2);
o1 += (o2 ^ t3).fold(t3);
o2 += (o1 ^ t4).fold(t4);
o1 += (o2 ^ t5).fold(t5);
o2 += (o1 ^ t6).fold(t6);
o1 += (o2 ^ t7).fold(t7);
o2 += (o1 ^ t8).fold(t8);
o1 += (o2 ^ t9).fold(t9);
}
void hash_foldmul256(const std::vector<unsigned char> &input, unsigned char output[32]) {
size_t len = input.size();
size_t offset = 0;
/* Assume less than 64 levels of recursion for now */
u256 p1[64], p2[64];
unsigned long long filled = 0;
int i, j;
for (len = input.size();; len -= 32, offset += 32) {
u256 t1(len, offset);
u256 t2(len, &input[offset]);
hash_step(t1, t2, t1, t2);
filled++;
for (i = 0; i < 64; i++) {
if (filled & (1ull << i)) break;
/* These xors are irreversible */
hash_step(p1[i] ^ t2, t1 ^ p2[i], t1, t2);
}
p1[i] = t1;
p2[i] = t2;
if (len <= 32) break;
}
/* Find the first value */
for (i = 0; i < 64; i++) {
if (filled & (1ull << i)) break;
}
/* Handle the remaining partial evaluations */
for (j = i + 1; j < 64; j++) {
if (!(filled & (1ull << i))) continue;
/* These xors are irreversible */
hash_step(p1[i] ^ p2[j], p1[j] ^ p2[i], p1[i], p2[i]);
}
/* Final irreversible step - only return a single output */
memcpy(output, &p1[i], 32);
}
typedef vector<unsigned char> bufferType;
int main() {
u256 *ll1 = new u256('A', 'Cryptographic', 'Hash', 'Function');
u256 *ll2 = new u256(50000000, 6000000, 7000000, 8000000);
u256 *ll3 = new u256(9000000000, 10000000000, 11000000000, 12000000000);
u256 *ll4 = new u256(1300000000000000000, 14000000000000000000, 15000000000000000000, 16000000000000000000);
hash_step(*ll1, *ll2, *ll3, *ll4);
cout << tostring(ll1->lo_) << " : " << tostring(ll1->hi_) << endl;
cout << tostring(ll2->lo_) << " : " << tostring(ll2->hi_) << endl;
cout << tostring(ll3->lo_) << " : " << tostring(ll3->hi_) << endl;
cout << tostring(ll4->lo_) << " : " << tostring(ll4->hi_) << endl << endl << endl;
u256 str1 = (const u256 &) "Blue";
u256 str2 = (const u256 &) "Red";
u256 str3 = (const u256 &) "Orange";
u256 str4 = (const u256 &) "Yellow";
hash_step(str1, str2, str3, str4);
cout << tostring(str1.lo_) << " : " << tostring(str1.hi_) << endl;
cout << tostring(str2.lo_) << " : " << tostring(str2.hi_) << endl;
cout << tostring(str3.lo_) << " : " << tostring(str3.hi_) << endl;
cout << tostring(str4.lo_) << " : " << tostring(str4.hi_) << endl;
const char *testdata = "A Cryptographic Hash Function";
unsigned char *buffer = (unsigned char *) testdata;
std::size_t size = strlen((const char *) buffer);
std::vector<unsigned char> var(buffer, buffer + size);
unsigned char output[32];
hash_foldmul256(var, output);
cout << output << endl << endl << endl;
for (int i = 0; i < 32; i++) {
cout << hex << output[i];
}
return 0;
}