-
Notifications
You must be signed in to change notification settings - Fork 2
/
salsa_20_sidm.c
299 lines (261 loc) · 9.89 KB
/
salsa_20_sidm.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
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
/*
* Copyright 2013 [email protected]
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <immintrin.h>
#include "cpuminer-config.h"
#include "miner.h"
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#if defined(__x86_64__)
static inline void xor_salsa_sidm(__m128i *calc_18, __m128i *calc_13, __m128i *calc_9, __m128i *calc_7,
__m128i *calc_1, __m128i *calc_4, __m128i *calc_3, __m128i *calc_2)
{
int i;
__m128i _calc;
__m128i _shift_left;
__m128i row1 = _mm_xor_si128(*calc_18, *calc_1);;
__m128i row2 = _mm_xor_si128(*calc_7, *calc_2);;
__m128i row3 = _mm_xor_si128(*calc_9, *calc_3);;
__m128i row4 = _mm_xor_si128(*calc_13, *calc_4);;
*calc_18 = _mm_xor_si128(*calc_18, *calc_1);
*calc_7 = _mm_xor_si128(*calc_7, *calc_2);
*calc_9 = _mm_xor_si128(*calc_9, *calc_3);
*calc_13 = _mm_xor_si128(*calc_13, *calc_4);
for (i = 0; i < 8; i += 2) {
/* first row */
_calc = _mm_add_epi32(row1, row4);
_shift_left = _mm_slli_epi32(_calc, 7);
_calc = _mm_srli_epi32(_calc,(32 - 7));
row2 = _mm_xor_si128(row2, _calc);
row2 = _mm_xor_si128(row2, _shift_left);
/* second row */
_calc = _mm_add_epi32(row2, row1);
_shift_left = _mm_slli_epi32(_calc, 9);
_calc = _mm_srli_epi32(_calc,(32 - 9));
row3 = _mm_xor_si128(row3, _calc);
row3 = _mm_xor_si128(row3, _shift_left);
/* third row */
_calc = _mm_add_epi32(row3, row2);
_shift_left = _mm_slli_epi32(_calc, 13);
_calc = _mm_srli_epi32(_calc,(32 - 13));
row4 = _mm_xor_si128(row4, _calc);
row4 = _mm_xor_si128(row4, _shift_left);
/* fourth row */
_calc = _mm_add_epi32(row4, row3);
_shift_left = _mm_slli_epi32(_calc, 18);
_calc = _mm_srli_epi32(_calc,(32 - 18));
row1 = _mm_xor_si128(row1, _calc);
row1 = _mm_xor_si128(row1, _shift_left);
// transpose_matrix(row1, row2, row3, row4, row_to_column);
row2 = _mm_shuffle_epi32(row2,0x93);
row3 = _mm_shuffle_epi32(row3,0x4e);
row4 = _mm_shuffle_epi32(row4,0x39);
// end transpose
// switch *calc_13 and * calc_7 usage compared to rows
/* first column */
_calc = _mm_add_epi32(row1, row2);
_shift_left = _mm_slli_epi32(_calc, 7);
_calc = _mm_srli_epi32(_calc,(32 - 7));
row4 = _mm_xor_si128(row4, _calc);
row4 = _mm_xor_si128(row4, _shift_left);
/* second column */
_calc = _mm_add_epi32(row4, row1);
_shift_left = _mm_slli_epi32(_calc, 9);
_calc = _mm_srli_epi32(_calc,(32 - 9));
row3 = _mm_xor_si128(row3, _calc);
row3 = _mm_xor_si128(row3, _shift_left);
/* third column */
_calc = _mm_add_epi32(row3, row4);
_shift_left = _mm_slli_epi32(_calc, 13);
_calc = _mm_srli_epi32(_calc,(32 - 13));
row2 = _mm_xor_si128(row2, _calc);
row2 = _mm_xor_si128(row2, _shift_left);
/* fourth column */
_calc = _mm_add_epi32(row2, row3);
_shift_left = _mm_slli_epi32(_calc, 18);
_calc = _mm_srli_epi32(_calc,(32 - 18));
row1 = _mm_xor_si128(row1, _calc);
row1 = _mm_xor_si128(row1, _shift_left);
// transpose_matrix(row1, row2, row3, row4, row_to_column);
row2 = _mm_shuffle_epi32(row2,0x39);
row3 = _mm_shuffle_epi32(row3,0x4e);
row4 = _mm_shuffle_epi32(row4,0x93);
// end transpose
}
*calc_18 = _mm_add_epi32(*calc_18,row1);
*calc_7 = _mm_add_epi32(*calc_7, row2);
*calc_9 = _mm_add_epi32(*calc_9, row3);
*calc_13 = _mm_add_epi32(*calc_13, row4);
}
static inline void scrypt_core_sidm(__m128i *X /*, uint32_t *V*/)
{
uint32_t i, j;
__m128i scratch[1024 * 8];
__m128i *calc_1 = (__m128i*) &X[0];
__m128i *calc_2 = (__m128i*) &X[1];
__m128i *calc_3 = (__m128i*) &X[2];
__m128i *calc_4 = (__m128i*) &X[3];
__m128i *calc_11 = (__m128i*) &X[4];
__m128i *calc_21 = (__m128i*) &X[5];
__m128i *calc_31 = (__m128i*) &X[6];
__m128i *calc_41 = (__m128i*) &X[7];
__m128i _calc5;
__m128i _calc6;
__m128i _calc7;
__m128i _calc8;
/* transpose the data from *X */
_calc5 =_mm_blend_epi16(*calc_11, *calc_31, 0xf0);
_calc6 =_mm_blend_epi16(*calc_21, *calc_41, 0x0f);
_calc7 =_mm_blend_epi16(*calc_31, *calc_11, 0xf0);
_calc8 =_mm_blend_epi16(*calc_41, *calc_21, 0x0f);
*calc_11 = _mm_blend_epi16(_calc5, _calc8, 0xcc);
*calc_21 = _mm_blend_epi16(_calc6, _calc5, 0xcc);
*calc_31 = _mm_blend_epi16(_calc7, _calc6, 0xcc);
*calc_41 = _mm_blend_epi16(_calc8, _calc7, 0xcc);
_calc5 =_mm_blend_epi16(*calc_1, *calc_3, 0xf0);
_calc6 =_mm_blend_epi16(*calc_2, *calc_4, 0x0f);
_calc7 =_mm_blend_epi16(*calc_3, *calc_1, 0xf0);
_calc8 =_mm_blend_epi16(*calc_4, *calc_2, 0x0f);
*calc_1 = _mm_blend_epi16(_calc5, _calc8, 0xcc);
*calc_2 = _mm_blend_epi16(_calc6, _calc5, 0xcc);
*calc_3 = _mm_blend_epi16(_calc7, _calc6, 0xcc);
*calc_4 = _mm_blend_epi16(_calc8, _calc7, 0xcc);
for (i = 0; i < 1024; i++) {
scratch[i * 8 + 0] = *calc_1;
scratch[i * 8 + 1] = *calc_2;
scratch[i * 8 + 2] = *calc_3;
scratch[i * 8 + 3] = *calc_4;
scratch[i * 8 + 4] = *calc_11;
scratch[i * 8 + 5] = *calc_21;
scratch[i * 8 + 6] = *calc_31;
scratch[i * 8 + 7] = *calc_41;
xor_salsa_sidm( calc_1, calc_2, calc_3, calc_4,calc_11,calc_21,calc_31,calc_41);
xor_salsa_sidm(calc_11,calc_21,calc_31,calc_41, calc_1, calc_2, calc_3, calc_4);
}
for (i = 0; i < 1024; i++) {
j = 8 * (_mm_extract_epi16(*calc_11,0x00) & 1023);
*calc_1 = _mm_xor_si128(*calc_1, scratch[j]);
*calc_2 = _mm_xor_si128(*calc_2, scratch[j+1]);
*calc_3 = _mm_xor_si128(*calc_3, scratch[j+2]);
*calc_4 = _mm_xor_si128(*calc_4, scratch[j+3]);
*calc_11 = _mm_xor_si128(*calc_11, scratch[j+4]);
*calc_21 = _mm_xor_si128(*calc_21, scratch[j+5]);
*calc_31 = _mm_xor_si128(*calc_31, scratch[j+6]);
*calc_41 = _mm_xor_si128(*calc_41, scratch[j+7]);
xor_salsa_sidm( calc_1, calc_2, calc_3, calc_4,calc_11,calc_21,calc_31,calc_41);
xor_salsa_sidm(calc_11,calc_21,calc_31,calc_41, calc_1, calc_2, calc_3, calc_4);
}
_calc5 =_mm_blend_epi16(*calc_11, *calc_31, 0xf0);
_calc6 =_mm_blend_epi16(*calc_21, *calc_41, 0x0f);
_calc7 =_mm_blend_epi16(*calc_31, *calc_11, 0xf0);
_calc8 =_mm_blend_epi16(*calc_41, *calc_21, 0x0f);
*calc_11 = _mm_blend_epi16(_calc5, _calc8, 0xcc);
*calc_21 = _mm_blend_epi16(_calc6, _calc5, 0xcc);
*calc_31 = _mm_blend_epi16(_calc7, _calc6, 0xcc);
*calc_41 = _mm_blend_epi16(_calc8, _calc7, 0xcc);
_calc5 =_mm_blend_epi16(*calc_1, *calc_3, 0xf0);
_calc6 =_mm_blend_epi16(*calc_2, *calc_4, 0x0f);
_calc7 =_mm_blend_epi16(*calc_3, *calc_1, 0xf0);
_calc8 =_mm_blend_epi16(*calc_4, *calc_2, 0x0f);
*calc_1 = _mm_blend_epi16(_calc5, _calc8, 0xcc);
*calc_2 = _mm_blend_epi16(_calc6, _calc5, 0xcc);
*calc_3 = _mm_blend_epi16(_calc7, _calc6, 0xcc);
*calc_4 = _mm_blend_epi16(_calc8, _calc7, 0xcc);
}
#endif
static inline void xor_salsa8_org(uint32_t B[16], const uint32_t Bx[16])
{
uint32_t x00,x01,x02,x03,x04,x05,x06,x07,x08,x09,x10,x11,x12,x13,x14,x15;
int i;
x00 = (B[ 0] ^= Bx[ 0]);
x01 = (B[ 1] ^= Bx[ 1]);
x02 = (B[ 2] ^= Bx[ 2]);
x03 = (B[ 3] ^= Bx[ 3]);
x04 = (B[ 4] ^= Bx[ 4]);
x05 = (B[ 5] ^= Bx[ 5]);
x06 = (B[ 6] ^= Bx[ 6]);
x07 = (B[ 7] ^= Bx[ 7]);
x08 = (B[ 8] ^= Bx[ 8]);
x09 = (B[ 9] ^= Bx[ 9]);
x10 = (B[10] ^= Bx[10]);
x11 = (B[11] ^= Bx[11]);
x12 = (B[12] ^= Bx[12]);
x13 = (B[13] ^= Bx[13]);
x14 = (B[14] ^= Bx[14]);
x15 = (B[15] ^= Bx[15]);
for (i = 0; i < 8; i += 2) {
#define R(a, b) (((a) << (b)) | ((a) >> (32 - (b))))
// /* Operate on columns. */
/* 0,4,8,12 *//* 1,5,9,13 *//* 2,6,10,14 *//* 3,7,11,15 */
x04 ^= R(x00+x12, 7); x09 ^= R(x05+x01, 7); x14 ^= R(x10+x06, 7); x03 ^= R(x15+x11, 7);
x08 ^= R(x04+x00, 9); x13 ^= R(x09+x05, 9); x02 ^= R(x14+x10, 9); x07 ^= R(x03+x15, 9);
x12 ^= R(x08+x04,13); x01 ^= R(x13+x09,13); x06 ^= R(x02+x14,13); x11 ^= R(x07+x03,13);
x00 ^= R(x12+x08,18); x05 ^= R(x01+x13,18); x10 ^= R(x06+x02,18); x15 ^= R(x11+x07,18);
//
// /* Operate on rows. */
// /* 1,2,3,0 *//* 6,7,4,5 *//* 11,8,9,10 *//* 12,13,14,15 */
x01 ^= R(x00+x03, 7); x06 ^= R(x05+x04, 7); x11 ^= R(x10+x09, 7); x12 ^= R(x15+x14, 7);
//
x02 ^= R(x01+x00, 9); x07 ^= R(x06+x05, 9); x08 ^= R(x11+x10, 9); x13 ^= R(x12+x15, 9);
//
x03 ^= R(x02+x01,13); x04 ^= R(x07+x06,13); x09 ^= R(x08+x11,13); x14 ^= R(x13+x12,13);
//
x00 ^= R(x03+x02,18); x05 ^= R(x04+x07,18); x10 ^= R(x09+x08,18); x15 ^= R(x14+x13,18);
#undef R
}
B[ 0] += x00;
B[ 1] += x01;
B[ 2] += x02;
B[ 3] += x03;
B[ 4] += x04;
B[ 5] += x05;
B[ 6] += x06;
B[ 7] += x07;
B[ 8] += x08;
B[ 9] += x09;
B[10] += x10;
B[11] += x11;
B[12] += x12;
B[13] += x13;
B[14] += x14;
B[15] += x15;
}
static inline void scrypt_core_org(uint32_t *X, uint32_t *V)
{
uint32_t i, j, k;
for (i = 0; i < 1024; i++) {
memcpy(&V[i * 32], X, 128);
xor_salsa8_org(&X[0], &X[16]);
xor_salsa8_org(&X[16], &X[0]);
}
for (i = 0; i < 1024; i++) {
j = 32 * (X[16] & 1023);
for (k = 0; k < 32; k++)
X[k] ^= V[j + k];
xor_salsa8_org(&X[0], &X[16]);
xor_salsa8_org(&X[16], &X[0]);
}
}