-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel_src.cl
More file actions
476 lines (423 loc) · 11.4 KB
/
kernel_src.cl
File metadata and controls
476 lines (423 loc) · 11.4 KB
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
#define S_1 0u
#define S_2 0u
#define S_3 0u
#define S_4 0u
#define S_5 0u
#define S_6 0u
#define S_7 179u
#define S_8 97u
#define S_9 25u
#define S_10 76u
#define S_11 254u
#define S_12 99u
#define S_13 18u
#define S_14 238u
#define S_15 50u
#define S_16 16u
#define S_17 213u
#define S_18 60u
#define S_19 21u
#define S_20 170u
#define S_21 39u
#define S_22 108u
#define S_23 232u
#define S_24 126u
#define S_25 4u
#define S_26 183u
#define S_27 167u
#define S_28 74u
#define S_29 105u
#define S_30 226u
#define S_31 137u
#define S_32 134u
#define S_33 137u
#define S_34 213u
#define S_35 245u
#define S_36 245u
#define S_37 86u
#define S_38 35u
#define S_39 220u
#define S_40 164u
#define S_53 29u
#define S_54 236u
#define S_55 188u
#define S_56 240u
#define S_57 75u
#define S_58 53u
#define S_59 93u
#define S_60 80u
#define S_61 12u
#define S_62 188u
#define S_63 59u
#define S_64 216u
#define S_65 60u
#define S_66 137u
#define S_67 37u
#define S_68 69u
#define S_69 180u
#define S_70 223u
#define S_71 52u
#define S_72 189u
#define S_73 91u
#define S_74 44u
#define S_75 157u
#define S_76 145u
#define S_77 185u
#define S_78 247u
#define S_79 248u
#define S_80 22u
#define S_81 94u
#define S_82 32u
#define S_83 149u
#define S_84 195u
#define LEADING_ZEROES 5
#define TOTAL_ZEROES 0
#define SUCCESS_CONDITION() hasLeading(digest)
#define MAX_NONCE 32u
/*
Copyright 2018 Lip Wee Yeo Amano
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* Based on the following, with small tweaks and optimizations:
* https://github.com/lwYeo/SoliditySHA3Miner/blob/master/SoliditySHA3Miner/Miner/Kernels/OpenCL/sha3KingKernel.cl
*
* Originally modified for OpenCL processing by lwYeo
*
* Original implementer: David Leon Gil
*
* License: CC0, attribution kindly requested. Blame taken too, but not
* liability.
*/
/**
* Credit to [createXcrunch](https://github.com/HrikB/createXcrunch) and [create2crunch](https://github.com/0age/create2crunch)
* from which the original kernel source code has been forked from. This version is modified specifically to support
*/
/******** Keccak-f[1600] (for finding efficient Ethereum addresses) ********/
#define OPENCL_PLATFORM_UNKNOWN 0
#define OPENCL_PLATFORM_AMD 2
#ifndef PLATFORM
# define PLATFORM OPENCL_PLATFORM_UNKNOWN
#endif
#if PLATFORM == OPENCL_PLATFORM_AMD
# pragma OPENCL EXTENSION cl_amd_media_ops : enable
#endif
typedef union _nonce_t
{
ulong uint64_t;
uint uint32_t[2];
uchar uint8_t[8];
} nonce_t;
#if PLATFORM == OPENCL_PLATFORM_AMD
static inline ulong rol(const ulong x, const uint s)
{
uint2 output;
uint2 x2 = as_uint2(x);
output = (s > 32u) ? amd_bitalign((x2).yx, (x2).xy, 64u - s) : amd_bitalign((x2).xy, (x2).yx, 32u - s);
return as_ulong(output);
}
#else
#define rol(x, s) (((x) << s) | ((x) >> (64u - s)))
#endif
#define rol1(x) rol(x, 1u)
#define theta_(m, n, o) \
t = b[m] ^ rol1(b[n]); \
a[o + 0] ^= t; \
a[o + 5] ^= t; \
a[o + 10] ^= t; \
a[o + 15] ^= t; \
a[o + 20] ^= t; \
#define theta() \
b[0] = a[0] ^ a[5] ^ a[10] ^ a[15] ^ a[20]; \
b[1] = a[1] ^ a[6] ^ a[11] ^ a[16] ^ a[21]; \
b[2] = a[2] ^ a[7] ^ a[12] ^ a[17] ^ a[22]; \
b[3] = a[3] ^ a[8] ^ a[13] ^ a[18] ^ a[23]; \
b[4] = a[4] ^ a[9] ^ a[14] ^ a[19] ^ a[24]; \
theta_(4, 1, 0); \
theta_(0, 2, 1); \
theta_(1, 3, 2); \
theta_(2, 4, 3); \
theta_(3, 0, 4);
#define rhoPi_(m, n) t = b[0]; b[0] = a[m]; a[m] = rol(t, n); \
#define rhoPi() t = a[1]; b[0] = a[10]; a[10] = rol1(t); \
rhoPi_(7, 3); \
rhoPi_(11, 6); \
rhoPi_(17, 10); \
rhoPi_(18, 15); \
rhoPi_(3, 21); \
rhoPi_(5, 28); \
rhoPi_(16, 36); \
rhoPi_(8, 45); \
rhoPi_(21, 55); \
rhoPi_(24, 2); \
rhoPi_(4, 14); \
rhoPi_(15, 27); \
rhoPi_(23, 41); \
rhoPi_(19, 56); \
rhoPi_(13, 8); \
rhoPi_(12, 25); \
rhoPi_(2, 43); \
rhoPi_(20, 62); \
rhoPi_(14, 18); \
rhoPi_(22, 39); \
rhoPi_(9, 61); \
rhoPi_(6, 20); \
rhoPi_(1, 44);
#define chi_(n) \
b[0] = a[n + 0]; \
b[1] = a[n + 1]; \
b[2] = a[n + 2]; \
b[3] = a[n + 3]; \
b[4] = a[n + 4]; \
a[n + 0] = b[0] ^ ((~b[1]) & b[2]); \
a[n + 1] = b[1] ^ ((~b[2]) & b[3]); \
a[n + 2] = b[2] ^ ((~b[3]) & b[4]); \
a[n + 3] = b[3] ^ ((~b[4]) & b[0]); \
a[n + 4] = b[4] ^ ((~b[0]) & b[1]);
#define chi() chi_(0); chi_(5); chi_(10); chi_(15); chi_(20);
#define iota(x) a[0] ^= x;
#define iteration(x) theta(); rhoPi(); chi(); iota(x);
static inline void keccakf(ulong *a)
{
ulong b[5];
ulong t;
iteration(0x0000000000000001); // iteration 1
iteration(0x0000000000008082); // iteration 2
iteration(0x800000000000808a); // iteration 3
iteration(0x8000000080008000); // iteration 4
iteration(0x000000000000808b); // iteration 5
iteration(0x0000000080000001); // iteration 6
iteration(0x8000000080008081); // iteration 7
iteration(0x8000000000008009); // iteration 8
iteration(0x000000000000008a); // iteration 9
iteration(0x0000000000000088); // iteration 10
iteration(0x0000000080008009); // iteration 11
iteration(0x000000008000000a); // iteration 12
iteration(0x000000008000808b); // iteration 13
iteration(0x800000000000008b); // iteration 14
iteration(0x8000000000008089); // iteration 15
iteration(0x8000000000008003); // iteration 16
iteration(0x8000000000008002); // iteration 17
iteration(0x8000000000000080); // iteration 18
iteration(0x000000000000800a); // iteration 19
iteration(0x800000008000000a); // iteration 20
iteration(0x8000000080008081); // iteration 21
iteration(0x8000000000008080); // iteration 22
iteration(0x0000000080000001); // iteration 23
// iteration 24 (partial)
#define o ((uint *)(a))
// Theta (partial)
b[0] = a[0] ^ a[5] ^ a[10] ^ a[15] ^ a[20];
b[1] = a[1] ^ a[6] ^ a[11] ^ a[16] ^ a[21];
b[2] = a[2] ^ a[7] ^ a[12] ^ a[17] ^ a[22];
b[3] = a[3] ^ a[8] ^ a[13] ^ a[18] ^ a[23];
b[4] = a[4] ^ a[9] ^ a[14] ^ a[19] ^ a[24];
a[0] ^= b[4] ^ rol1(b[1]);
a[6] ^= b[0] ^ rol1(b[2]);
a[12] ^= b[1] ^ rol1(b[3]);
a[18] ^= b[2] ^ rol1(b[4]);
a[24] ^= b[3] ^ rol1(b[0]);
// Rho Pi (partial)
o[3] = (o[13] >> 20) | (o[12] << 12);
a[2] = rol(a[12], 43);
a[3] = rol(a[18], 21);
a[4] = rol(a[24], 14);
// Chi (partial)
o[3] ^= ((~o[5]) & o[7]);
o[4] ^= ((~o[6]) & o[8]);
o[5] ^= ((~o[7]) & o[9]);
o[6] ^= ((~o[8]) & o[0]);
o[7] ^= ((~o[9]) & o[1]);
#undef o
}
#define hasTotal(d) ( \
(!(d[0])) + (!(d[1])) + (!(d[2])) + (!(d[3])) + \
(!(d[4])) + (!(d[5])) + (!(d[6])) + (!(d[7])) + \
(!(d[8])) + (!(d[9])) + (!(d[10])) + (!(d[11])) + \
(!(d[12])) + (!(d[13])) + (!(d[14])) + (!(d[15])) + \
(!(d[16])) + (!(d[17])) + (!(d[18])) + (!(d[19])) \
>= TOTAL_ZEROES)
#define HAS_LAST_THREE_EQUAL_TO(d) ((d[17] == 0xa9) && (d[18] == 0x01) && (d[19] == 0x2a))
#if LEADING_ZEROES == 8
#define hasLeading(d) (!(((uint*)d)[0]) && !(((uint*)d)[1]))
#elif LEADING_ZEROES == 7
#define hasLeading(d) (!(((uint*)d)[0]) && !(((uint*)d)[1] & 0x00ffffffu))
#elif LEADING_ZEROES == 6
#define hasLeading(d) (!(((uint*)d)[0]) && !(((uint*)d)[1] & 0x0000ffffu))
#elif LEADING_ZEROES == 5
#define hasLeading(d) (!(((uint*)d)[0]) && !(((uint*)d)[1] & 0x000000ffu))
#elif LEADING_ZEROES == 4
#define hasLeading(d) (!(((uint*)d)[0]))
#elif LEADING_ZEROES == 3
#define hasLeading(d) (!(((uint*)d)[0] & 0x00ffffffu))
#elif LEADING_ZEROES == 2
#define hasLeading(d) (!(((uint*)d)[0] & 0x0000ffffu))
#elif LEADING_ZEROES == 1
#define hasLeading(d) (!(((uint*)d)[0] & 0x000000ffu))
#else
static inline bool hasLeading(uchar const *d)
{
#pragma unroll
for (uint i = 0; i < LEADING_ZEROES; ++i) {
if (d[i] != 0) return false;
}
return true;
}
#endif
static inline bool hasLeading3(uchar const *d)
{
#pragma unroll
for (uint i = 0; i < 3; ++i) {
if (d[i] != 0) return false;
}
return true;
}
__kernel void hashMessage(
__constant uchar const *d_message,
__constant uint const *d_nonce,
__global volatile ulong *restrict solutions
) {
ulong spongeBuffer[25];
#define sponge ((uchar *) spongeBuffer)
#define digest (sponge + 12)
nonce_t nonce;
// write the control character
sponge[0] = 0xffu;
sponge[1] = S_1;
sponge[2] = S_2;
sponge[3] = S_3;
sponge[4] = S_4;
sponge[5] = S_5;
sponge[6] = S_6;
sponge[7] = S_7;
sponge[8] = S_8;
sponge[9] = S_9;
sponge[10] = S_10;
sponge[11] = S_11;
sponge[12] = S_12;
sponge[13] = S_13;
sponge[14] = S_14;
sponge[15] = S_15;
sponge[16] = S_16;
sponge[17] = S_17;
sponge[18] = S_18;
sponge[19] = S_19;
sponge[20] = S_20;
sponge[21] = S_21;
sponge[22] = S_22;
sponge[23] = S_23;
sponge[24] = S_24;
sponge[25] = S_25;
sponge[26] = S_26;
sponge[27] = S_27;
sponge[28] = S_28;
sponge[29] = S_29;
sponge[30] = S_30;
sponge[31] = S_31;
sponge[32] = S_32;
sponge[33] = S_33;
sponge[34] = S_34;
sponge[35] = S_35;
sponge[36] = S_36;
sponge[37] = S_37;
sponge[38] = S_38;
sponge[39] = S_39;
sponge[40] = S_40;
sponge[41] = d_message[0];
sponge[42] = d_message[1];
sponge[43] = d_message[2];
sponge[44] = d_message[3];
// populate the nonce
nonce.uint32_t[0] = get_global_id(0);
nonce.uint32_t[1] = d_nonce[0];
// populate the body of the message with the nonce
sponge[45] = nonce.uint8_t[0];
sponge[46] = nonce.uint8_t[1];
sponge[47] = nonce.uint8_t[2];
sponge[48] = nonce.uint8_t[3];
sponge[49] = nonce.uint8_t[4];
sponge[50] = nonce.uint8_t[5];
sponge[51] = nonce.uint8_t[6];
sponge[52] = nonce.uint8_t[7];
sponge[53] = S_53;
sponge[54] = S_54;
sponge[55] = S_55;
sponge[56] = S_56;
sponge[57] = S_57;
sponge[58] = S_58;
sponge[59] = S_59;
sponge[60] = S_60;
sponge[61] = S_61;
sponge[62] = S_62;
sponge[63] = S_63;
sponge[64] = S_64;
sponge[65] = S_65;
sponge[66] = S_66;
sponge[67] = S_67;
sponge[68] = S_68;
sponge[69] = S_69;
sponge[70] = S_70;
sponge[71] = S_71;
sponge[72] = S_72;
sponge[73] = S_73;
sponge[74] = S_74;
sponge[75] = S_75;
sponge[76] = S_76;
sponge[77] = S_77;
sponge[78] = S_78;
sponge[79] = S_79;
sponge[80] = S_80;
sponge[81] = S_81;
sponge[82] = S_82;
sponge[83] = S_83;
sponge[84] = S_84;
// begin padding based on message length
sponge[85] = 0x01u;
// fill padding
#pragma unroll
for (int i = 86; i < 135; ++i)
sponge[i] = 0;
// end padding
sponge[135] = 0x80u;
// fill remaining sponge state with zeroes
#pragma unroll
for (int i = 136; i < 200; ++i)
sponge[i] = 0;
// Apply keccakf
keccakf(spongeBuffer);
uchar deployProxy[20];
#pragma unroll
for (int i = 0; i < 20; ++i)
deployProxy[i] = digest[i];
for (uchar create1Nonce = 1; create1Nonce <= MAX_NONCE; ++create1Nonce) {
sponge[0] = 0xd6u;
sponge[1] = 0x94u;
#pragma unroll
for (int i = 0; i < 20; ++i)
sponge[i + 2] = deployProxy[i];
sponge[22] = create1Nonce;
sponge[23] = 0x01u;
#pragma unroll
for (int i = 24; i < 135; ++i)
sponge[i] = 0;
sponge[135] = 0x80u;
#pragma unroll
for (int i = 136; i < 200; ++i)
sponge[i] = 0;
keccakf(spongeBuffer);
// determine if the address meets the constraints
if ((HAS_LAST_THREE_EQUAL_TO(digest) && hasLeading3(digest)) || hasLeading(digest)) {
solutions[0] = nonce.uint64_t;
solutions[1] = (ulong) create1Nonce;
break;
}
}
}