-
Notifications
You must be signed in to change notification settings - Fork 26
/
kernel_profanity.hpp
531 lines (434 loc) · 14.5 KB
/
kernel_profanity.hpp
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#ifndef HPP_KERNEL_PROFANITY
#define HPP_KERNEL_PROFANITY
#include <string>
const std::string kernel_profanity = R"(
#define MP_WORDS 8
#define MP_BITS 32
#define bswap32(n) (rotate(n & 0x00FF00FF, 24U)|(rotate(n, 8U) & 0x00FF00FF))
typedef uint mp_word;
typedef struct {
mp_word d[MP_WORDS];
} mp_number;
__constant const mp_number mod = { {0xfffffc2f, 0xfffffffe, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff} };
__constant const mp_number tripleNegativeGx = { {0xbb17b196, 0xf2287bec, 0x76958573, 0xf82c096e, 0x946adeea, 0xff1ed83e, 0x1269ccfa, 0x92c4cc83 } };
__constant const mp_number doubleNegativeGy = { {0x09de52bf, 0xc7705edf, 0xb2f557cc, 0x05d0976e, 0xe3ddeeae, 0x44b60807, 0xb2b87735, 0x6f8a4b11} };
__constant const mp_number negativeGy = { {0x04ef2777, 0x63b82f6f, 0x597aabe6, 0x02e84bb7, 0xf1eef757, 0xa25b0403, 0xd95c3b9a, 0xb7c52588 } };
mp_word mp_sub(mp_number * const r, const mp_number * const a, const mp_number * const b) {
mp_word t, c = 0;
for (mp_word i = 0; i < MP_WORDS; ++i) {
t = a->d[i] - b->d[i] - c;
c = t > a->d[i] ? 1 : (t == a->d[i] ? c : 0);
r->d[i] = t;
}
return c;
}
mp_word mp_sub_mod(mp_number * const r) {
mp_number mod = { {0xfffffc2f, 0xfffffffe, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff} };
mp_word t, c = 0;
for (mp_word i = 0; i < MP_WORDS; ++i) {
t = r->d[i] - mod.d[i] - c;
c = t > r->d[i] ? 1 : (t == r->d[i] ? c : 0);
r->d[i] = t;
}
return c;
}
void mp_mod_sub(mp_number * const r, const mp_number * const a, const mp_number * const b) {
mp_word i, t, c = 0;
for (i = 0; i < MP_WORDS; ++i) {
t = a->d[i] - b->d[i] - c;
c = t < a->d[i] ? 0 : (t == a->d[i] ? c : 1);
r->d[i] = t;
}
if (c) {
c = 0;
for (i = 0; i < MP_WORDS; ++i) {
r->d[i] += mod.d[i] + c;
c = r->d[i] < mod.d[i] ? 1 : (r->d[i] == mod.d[i] ? c : 0);
}
}
}
void mp_mod_sub_const(mp_number * const r, __constant const mp_number * const a, const mp_number * const b) {
mp_word i, t, c = 0;
for (i = 0; i < MP_WORDS; ++i) {
t = a->d[i] - b->d[i] - c;
c = t < a->d[i] ? 0 : (t == a->d[i] ? c : 1);
r->d[i] = t;
}
if (c) {
c = 0;
for (i = 0; i < MP_WORDS; ++i) {
r->d[i] += mod.d[i] + c;
c = r->d[i] < mod.d[i] ? 1 : (r->d[i] == mod.d[i] ? c : 0);
}
}
}
void mp_mod_sub_gx(mp_number * const r, const mp_number * const a) {
mp_word i, t, c = 0;
t = a->d[0] - 0x16f81798; c = t < a->d[0] ? 0 : (t == a->d[0] ? c : 1); r->d[0] = t;
t = a->d[1] - 0x59f2815b - c; c = t < a->d[1] ? 0 : (t == a->d[1] ? c : 1); r->d[1] = t;
t = a->d[2] - 0x2dce28d9 - c; c = t < a->d[2] ? 0 : (t == a->d[2] ? c : 1); r->d[2] = t;
t = a->d[3] - 0x029bfcdb - c; c = t < a->d[3] ? 0 : (t == a->d[3] ? c : 1); r->d[3] = t;
t = a->d[4] - 0xce870b07 - c; c = t < a->d[4] ? 0 : (t == a->d[4] ? c : 1); r->d[4] = t;
t = a->d[5] - 0x55a06295 - c; c = t < a->d[5] ? 0 : (t == a->d[5] ? c : 1); r->d[5] = t;
t = a->d[6] - 0xf9dcbbac - c; c = t < a->d[6] ? 0 : (t == a->d[6] ? c : 1); r->d[6] = t;
t = a->d[7] - 0x79be667e - c; c = t < a->d[7] ? 0 : (t == a->d[7] ? c : 1); r->d[7] = t;
if (c) {
c = 0;
for (i = 0; i < MP_WORDS; ++i) {
r->d[i] += mod.d[i] + c;
c = r->d[i] < mod.d[i] ? 1 : (r->d[i] == mod.d[i] ? c : 0);
}
}
}
void mp_mod_sub_gy(mp_number * const r, const mp_number * const a) {
mp_word i, t, c = 0;
t = a->d[0] - 0xfb10d4b8; c = t < a->d[0] ? 0 : (t == a->d[0] ? c : 1); r->d[0] = t;
t = a->d[1] - 0x9c47d08f - c; c = t < a->d[1] ? 0 : (t == a->d[1] ? c : 1); r->d[1] = t;
t = a->d[2] - 0xa6855419 - c; c = t < a->d[2] ? 0 : (t == a->d[2] ? c : 1); r->d[2] = t;
t = a->d[3] - 0xfd17b448 - c; c = t < a->d[3] ? 0 : (t == a->d[3] ? c : 1); r->d[3] = t;
t = a->d[4] - 0x0e1108a8 - c; c = t < a->d[4] ? 0 : (t == a->d[4] ? c : 1); r->d[4] = t;
t = a->d[5] - 0x5da4fbfc - c; c = t < a->d[5] ? 0 : (t == a->d[5] ? c : 1); r->d[5] = t;
t = a->d[6] - 0x26a3c465 - c; c = t < a->d[6] ? 0 : (t == a->d[6] ? c : 1); r->d[6] = t;
t = a->d[7] - 0x483ada77 - c; c = t < a->d[7] ? 0 : (t == a->d[7] ? c : 1); r->d[7] = t;
if (c) {
c = 0;
for (i = 0; i < MP_WORDS; ++i) {
r->d[i] += mod.d[i] + c;
c = r->d[i] < mod.d[i] ? 1 : (r->d[i] == mod.d[i] ? c : 0);
}
}
}
mp_word mp_add(mp_number * const r, const mp_number * const a) {
mp_word c = 0;
for (mp_word i = 0; i < MP_WORDS; ++i) {
r->d[i] += a->d[i] + c;
c = r->d[i] < a->d[i] ? 1 : (r->d[i] == a->d[i] ? c : 0);
}
return c;
}
mp_word mp_add_mod(mp_number * const r) {
mp_word c = 0;
for (mp_word i = 0; i < MP_WORDS; ++i) {
r->d[i] += mod.d[i] + c;
c = r->d[i] < mod.d[i] ? 1 : (r->d[i] == mod.d[i] ? c : 0);
}
return c;
}
mp_word mp_add_more(mp_number * const r, mp_word * const extraR, const mp_number * const a, const mp_word * const extraA) {
const mp_word c = mp_add(r, a);
*extraR += *extraA + c;
return *extraR < *extraA ? 1 : (*extraR == *extraA ? c : 0);
}
mp_word mp_gte(const mp_number * const a, const mp_number * const b) {
mp_word l = 0, g = 0;
for (mp_word i = 0; i < MP_WORDS; ++i) {
if (a->d[i] < b->d[i]) l |= (1 << i);
if (a->d[i] > b->d[i]) g |= (1 << i);
}
return g >= l;
}
void mp_shr_extra(mp_number * const r, mp_word * const e) {
r->d[0] = (r->d[1] << 31) | (r->d[0] >> 1);
r->d[1] = (r->d[2] << 31) | (r->d[1] >> 1);
r->d[2] = (r->d[3] << 31) | (r->d[2] >> 1);
r->d[3] = (r->d[4] << 31) | (r->d[3] >> 1);
r->d[4] = (r->d[5] << 31) | (r->d[4] >> 1);
r->d[5] = (r->d[6] << 31) | (r->d[5] >> 1);
r->d[6] = (r->d[7] << 31) | (r->d[6] >> 1);
r->d[7] = (*e << 31) | (r->d[7] >> 1);
*e >>= 1;
}
void mp_shr(mp_number * const r) {
r->d[0] = (r->d[1] << 31) | (r->d[0] >> 1);
r->d[1] = (r->d[2] << 31) | (r->d[1] >> 1);
r->d[2] = (r->d[3] << 31) | (r->d[2] >> 1);
r->d[3] = (r->d[4] << 31) | (r->d[3] >> 1);
r->d[4] = (r->d[5] << 31) | (r->d[4] >> 1);
r->d[5] = (r->d[6] << 31) | (r->d[5] >> 1);
r->d[6] = (r->d[7] << 31) | (r->d[6] >> 1);
r->d[7] >>= 1;
}
mp_word mp_mul_word_add_extra(mp_number * const r, const mp_number * const a, const mp_word w, mp_word * const extra) {
mp_word cM = 0;
mp_word cA = 0;
mp_word tM = 0;
for (mp_word i = 0; i < MP_WORDS; ++i) {
tM = (a->d[i] * w + cM);
cM = mul_hi(a->d[i], w) + (tM < cM);
r->d[i] += tM + cA;
cA = r->d[i] < tM ? 1 : (r->d[i] == tM ? cA : 0);
}
*extra += cM + cA;
return *extra < cM ? 1 : (*extra == cM ? cA : 0);
}
void mp_mul_mod_word_sub(mp_number * const r, const mp_word w, const bool withModHigher) {
mp_number mod = { { 0xfffffc2f, 0xfffffffe, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff} };
mp_number modhigher = { {0x00000000, 0xfffffc2f, 0xfffffffe, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff} };
mp_word cM = 0;
mp_word cS = 0;
mp_word tS = 0;
mp_word tM = 0;
mp_word cA = 0;
for (mp_word i = 0; i < MP_WORDS; ++i) {
tM = (mod.d[i] * w + cM);
cM = mul_hi(mod.d[i], w) + (tM < cM);
tM += (withModHigher ? modhigher.d[i] : 0) + cA;
cA = tM < (withModHigher ? modhigher.d[i] : 0) ? 1 : (tM == (withModHigher ? modhigher.d[i] : 0) ? cA : 0);
tS = r->d[i] - tM - cS;
cS = tS > r->d[i] ? 1 : (tS == r->d[i] ? cS : 0);
r->d[i] = tS;
}
}
void mp_mod_mul(mp_number * const r, const mp_number * const X, const mp_number * const Y) {
mp_number Z = { {0} };
mp_word extraWord;
for (int i = MP_WORDS - 1; i >= 0; --i) {
extraWord = Z.d[7]; Z.d[7] = Z.d[6]; Z.d[6] = Z.d[5]; Z.d[5] = Z.d[4]; Z.d[4] = Z.d[3]; Z.d[3] = Z.d[2]; Z.d[2] = Z.d[1]; Z.d[1] = Z.d[0]; Z.d[0] = 0;
bool overflow = mp_mul_word_add_extra(&Z, X, Y->d[i], &extraWord);
mp_mul_mod_word_sub(&Z, extraWord, overflow);
}
*r = Z;
}
void mp_mod_inverse(mp_number * const r) {
mp_number A = { { 1 } };
mp_number C = { { 0 } };
mp_number v = mod;
mp_word extraA = 0;
mp_word extraC = 0;
while (r->d[0] || r->d[1] || r->d[2] || r->d[3] || r->d[4] || r->d[5] || r->d[6] || r->d[7]) {
while (!(r->d[0] & 1)) {
mp_shr(r);
if (A.d[0] & 1) {
extraA += mp_add_mod(&A);
}
mp_shr_extra(&A, &extraA);
}
while (!(v.d[0] & 1)) {
mp_shr(&v);
if (C.d[0] & 1) {
extraC += mp_add_mod(&C);
}
mp_shr_extra(&C, &extraC);
}
if (mp_gte(r, &v)) {
mp_sub(r, r, &v);
mp_add_more(&A, &extraA, &C, &extraC);
}
else {
mp_sub(&v, &v, r);
mp_add_more(&C, &extraC, &A, &extraA);
}
}
while (extraC) {
extraC -= mp_sub_mod(&C);
}
v = mod;
mp_sub(r, &v, &C);
}
typedef struct {
mp_number x;
mp_number y;
} point;
void point_add(point * const r, point * const p, point * const o) {
mp_number tmp;
mp_number newX;
mp_number newY;
mp_mod_sub(&tmp, &o->x, &p->x);
mp_mod_inverse(&tmp);
mp_mod_sub(&newX, &o->y, &p->y);
mp_mod_mul(&tmp, &tmp, &newX);
mp_mod_mul(&newX, &tmp, &tmp);
mp_mod_sub(&newX, &newX, &p->x);
mp_mod_sub(&newX, &newX, &o->x);
mp_mod_sub(&newY, &p->x, &newX);
mp_mod_mul(&newY, &newY, &tmp);
mp_mod_sub(&newY, &newY, &p->y);
r->x = newX;
r->y = newY;
}
typedef struct {
uint found;
uint foundId;
uchar foundHash[20];
} result;
void profanity_init_seed(__global const point * const precomp, point * const p, bool * const pIsFirst, const size_t precompOffset, const ulong seed) {
point o;
for (uchar i = 0; i < 8; ++i) {
const uchar shift = i * 8;
const uchar byte = (seed >> shift) & 0xFF;
if (byte) {
o = precomp[precompOffset + i * 255 + byte - 1];
if (*pIsFirst) {
*p = o;
*pIsFirst = false;
}
else {
point_add(p, p, &o);
}
}
}
}
__kernel void profanity_init(__global const point * const precomp, __global mp_number * const pDeltaX, __global mp_number * const pPrevLambda, __global result * const pResult, const ulong4 seed) {
const size_t id = get_global_id(0);
point p;
bool bIsFirst = true;
mp_number tmp1, tmp2;
point tmp3;
profanity_init_seed(precomp, &p, &bIsFirst, 8 * 255 * 0, seed.x);
profanity_init_seed(precomp, &p, &bIsFirst, 8 * 255 * 1, seed.y);
profanity_init_seed(precomp, &p, &bIsFirst, 8 * 255 * 2, seed.z);
profanity_init_seed(precomp, &p, &bIsFirst, 8 * 255 * 3, seed.w + id);
mp_mod_sub_gx(&tmp1, &p.x);
mp_mod_inverse(&tmp1);
mp_mod_sub_gy(&tmp2, &p.y);
mp_mod_mul(&tmp1, &tmp1, &tmp2);
tmp3 = precomp[0];
point_add(&p, &tmp3, &p);
mp_mod_sub_gx(&p.x, &p.x);
pDeltaX[id] = p.x;
pPrevLambda[id] = tmp1;
for (uchar i = 0; i < PROFANITY_MAX_SCORE + 1; ++i) {
pResult[i].found = 0;
}
}
__kernel void profanity_inverse(__global const mp_number * const pDeltaX, __global mp_number * const pInverse) {
const size_t id = get_global_id(0) * PROFANITY_INVERSE_SIZE;
mp_number negativeDoubleGy = { {0x09de52bf, 0xc7705edf, 0xb2f557cc, 0x05d0976e, 0xe3ddeeae, 0x44b60807, 0xb2b87735, 0x6f8a4b11 } };
mp_number copy1, copy2;
mp_number buffer[PROFANITY_INVERSE_SIZE];
mp_number buffer2[PROFANITY_INVERSE_SIZE];
buffer[0] = pDeltaX[id];
for (uint i = 1; i < PROFANITY_INVERSE_SIZE; ++i) {
buffer2[i] = pDeltaX[id + i];
mp_mod_mul(&buffer[i], &buffer2[i], &buffer[i - 1]);
}
copy1 = buffer[PROFANITY_INVERSE_SIZE - 1];
mp_mod_inverse(©1);
mp_mod_mul(©1, ©1, &negativeDoubleGy);
for (uint i = PROFANITY_INVERSE_SIZE - 1; i > 0; --i) {
mp_mod_mul(©2, ©1, &buffer[i - 1]);
mp_mod_mul(©1, ©1, &buffer2[i]);
pInverse[id + i] = copy2;
}
pInverse[id] = copy1;
}
__kernel void profanity_iterate(
__global mp_number * const pDeltaX,
__global mp_number * const pInverse,
__global mp_number * const pPrevLambda)
{
const size_t id = get_global_id(0);
mp_number negativeGx = { {0xe907e497, 0xa60d7ea3, 0xd231d726, 0xfd640324, 0x3178f4f8, 0xaa5f9d6a, 0x06234453, 0x86419981 } };
ethhash h = { { 0 } };
mp_number dX = pDeltaX[id];
mp_number tmp = pInverse[id];
mp_number lambda = pPrevLambda[id];
mp_mod_sub(&lambda, &tmp, &lambda);
mp_mod_mul(&tmp, &lambda, &lambda);
mp_mod_sub(&dX, &dX, &tmp);
mp_mod_sub_const(&dX, &tripleNegativeGx, &dX);
pDeltaX[id] = dX;
pPrevLambda[id] = lambda;
mp_mod_mul(&tmp, &lambda, &dX);
mp_mod_sub_const(&tmp, &negativeGy, &tmp);
mp_mod_sub(&dX, &dX, &negativeGx);
h.d[0] = bswap32(dX.d[MP_WORDS - 1]);
h.d[1] = bswap32(dX.d[MP_WORDS - 2]);
h.d[2] = bswap32(dX.d[MP_WORDS - 3]);
h.d[3] = bswap32(dX.d[MP_WORDS - 4]);
h.d[4] = bswap32(dX.d[MP_WORDS - 5]);
h.d[5] = bswap32(dX.d[MP_WORDS - 6]);
h.d[6] = bswap32(dX.d[MP_WORDS - 7]);
h.d[7] = bswap32(dX.d[MP_WORDS - 8]);
h.d[8] = bswap32(tmp.d[MP_WORDS - 1]);
h.d[9] = bswap32(tmp.d[MP_WORDS - 2]);
h.d[10] = bswap32(tmp.d[MP_WORDS - 3]);
h.d[11] = bswap32(tmp.d[MP_WORDS - 4]);
h.d[12] = bswap32(tmp.d[MP_WORDS - 5]);
h.d[13] = bswap32(tmp.d[MP_WORDS - 6]);
h.d[14] = bswap32(tmp.d[MP_WORDS - 7]);
h.d[15] = bswap32(tmp.d[MP_WORDS - 8]);
h.d[16] ^= 0x01;
sha3_keccakf(&h);
pInverse[id].d[0] = h.d[3];
pInverse[id].d[1] = h.d[4];
pInverse[id].d[2] = h.d[5];
pInverse[id].d[3] = h.d[6];
pInverse[id].d[4] = h.d[7];
}
void profanity_result_update(
const size_t id,
__global const uchar * const hash,
__global result * const pResult,
const uchar score,
const uchar scoreMax)
{
if (score && score > scoreMax) {
uchar hasResult = atomic_inc(&pResult[score].found);
if (hasResult == 0) {
pResult[score].foundId = id;
for (int i = 0; i < 20; ++i) {
pResult[score].foundHash[i] = hash[i];
}
}
}
}
__kernel void profanity_score_matching(
__global mp_number * const pInverse,
__global result * const pResult,
__constant const uchar * const data1,
__constant const uchar * const data2,
const uchar scoreMax,
const uchar matchingCount,
const uchar prefixCount,
const uchar suffixCount)
{
const size_t id = get_global_id(0);
__global const uchar * hash = pInverse[id].d;
uchar * const hash_temp = pInverse[id].d;
uchar tron_hash[25];
ethhash_to_tronhash(hash_temp, tron_hash);
char tron_hash_address[34];
base58_encode(tron_hash, tron_hash_address, 25);
char matchingHash[20];
uint j = 0;
for (uint i = 0; i < 34; i++){
if(i < 10 || i >= 24){
matchingHash[j] = tron_hash_address[i];
j++;
}
}
for(uint j = 0; j < matchingCount; j++) {
uint scorePrefix = 0;
uint scoreSuffix = 0;
uint scoreTotal = 0;
uint dataIndex = 0;
if(prefixCount > 0) {
for (uint i = 0; i < 10; ++i) {
dataIndex = j * 20 + i;
if (data1[dataIndex] > 0 && (matchingHash[i] & data1[dataIndex]) == data2[dataIndex]) {
++scorePrefix;
} else {
break;
}
}
}
if(suffixCount > 0) {
for (uint i = 19; i > 10; --i) {
dataIndex = j * 20 + i;
if (data1[dataIndex] > 0 && (matchingHash[i] & data1[dataIndex]) == data2[dataIndex]) {
++scoreSuffix;
} else {
break;
}
}
}
if(scorePrefix >= prefixCount && scoreSuffix >= suffixCount){
scoreTotal = scoreMax + 1;
profanity_result_update(id, hash, pResult, scoreTotal, scoreMax);
break;
}
}
}
)";
#endif /* HPP_KERNEL_PROFANITY */