Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
scrypt-sse2.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2009 Colin Percival, 2011 ArtForz, 2012-2013 pooler
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * This file was originally written by Colin Percival as part of the Tarsnap
27  * online backup system.
28  */
29 
30 #include "scrypt.h"
31 #include <stdlib.h>
32 #include <stdint.h>
33 #include <string.h>
34 #include <openssl/sha.h>
35 
36 #include <emmintrin.h>
37 
38 static inline void xor_salsa8_sse2(__m128i B[4], const __m128i Bx[4])
39 {
40  __m128i X0, X1, X2, X3;
41  __m128i T;
42  int i;
43 
44  X0 = B[0] = _mm_xor_si128(B[0], Bx[0]);
45  X1 = B[1] = _mm_xor_si128(B[1], Bx[1]);
46  X2 = B[2] = _mm_xor_si128(B[2], Bx[2]);
47  X3 = B[3] = _mm_xor_si128(B[3], Bx[3]);
48 
49  for (i = 0; i < 8; i += 2) {
50  /* Operate on "columns". */
51  T = _mm_add_epi32(X0, X3);
52  X1 = _mm_xor_si128(X1, _mm_slli_epi32(T, 7));
53  X1 = _mm_xor_si128(X1, _mm_srli_epi32(T, 25));
54  T = _mm_add_epi32(X1, X0);
55  X2 = _mm_xor_si128(X2, _mm_slli_epi32(T, 9));
56  X2 = _mm_xor_si128(X2, _mm_srli_epi32(T, 23));
57  T = _mm_add_epi32(X2, X1);
58  X3 = _mm_xor_si128(X3, _mm_slli_epi32(T, 13));
59  X3 = _mm_xor_si128(X3, _mm_srli_epi32(T, 19));
60  T = _mm_add_epi32(X3, X2);
61  X0 = _mm_xor_si128(X0, _mm_slli_epi32(T, 18));
62  X0 = _mm_xor_si128(X0, _mm_srli_epi32(T, 14));
63 
64  /* Rearrange data. */
65  X1 = _mm_shuffle_epi32(X1, 0x93);
66  X2 = _mm_shuffle_epi32(X2, 0x4E);
67  X3 = _mm_shuffle_epi32(X3, 0x39);
68 
69  /* Operate on "rows". */
70  T = _mm_add_epi32(X0, X1);
71  X3 = _mm_xor_si128(X3, _mm_slli_epi32(T, 7));
72  X3 = _mm_xor_si128(X3, _mm_srli_epi32(T, 25));
73  T = _mm_add_epi32(X3, X0);
74  X2 = _mm_xor_si128(X2, _mm_slli_epi32(T, 9));
75  X2 = _mm_xor_si128(X2, _mm_srli_epi32(T, 23));
76  T = _mm_add_epi32(X2, X3);
77  X1 = _mm_xor_si128(X1, _mm_slli_epi32(T, 13));
78  X1 = _mm_xor_si128(X1, _mm_srli_epi32(T, 19));
79  T = _mm_add_epi32(X1, X2);
80  X0 = _mm_xor_si128(X0, _mm_slli_epi32(T, 18));
81  X0 = _mm_xor_si128(X0, _mm_srli_epi32(T, 14));
82 
83  /* Rearrange data. */
84  X1 = _mm_shuffle_epi32(X1, 0x39);
85  X2 = _mm_shuffle_epi32(X2, 0x4E);
86  X3 = _mm_shuffle_epi32(X3, 0x93);
87  }
88 
89  B[0] = _mm_add_epi32(B[0], X0);
90  B[1] = _mm_add_epi32(B[1], X1);
91  B[2] = _mm_add_epi32(B[2], X2);
92  B[3] = _mm_add_epi32(B[3], X3);
93 }
94 
95 void scrypt_1024_1_1_256_sp_sse2(const char *input, char *output, char *scratchpad)
96 {
97  uint8_t B[128];
98  union {
99  __m128i i128[8];
100  uint32_t u32[32];
101  } X;
102  __m128i *V;
103  uint32_t i, j, k;
104 
105  V = (__m128i *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63));
106 
107  PBKDF2_SHA256((const uint8_t *)input, 80, (const uint8_t *)input, 80, 1, B, 128);
108 
109  for (k = 0; k < 2; k++) {
110  for (i = 0; i < 16; i++) {
111  X.u32[k * 16 + i] = le32dec(&B[(k * 16 + (i * 5 % 16)) * 4]);
112  }
113  }
114 
115  for (i = 0; i < 1024; i++) {
116  for (k = 0; k < 8; k++)
117  V[i * 8 + k] = X.i128[k];
118  xor_salsa8_sse2(&X.i128[0], &X.i128[4]);
119  xor_salsa8_sse2(&X.i128[4], &X.i128[0]);
120  }
121  for (i = 0; i < 1024; i++) {
122  j = 8 * (X.u32[16] & 1023);
123  for (k = 0; k < 8; k++)
124  X.i128[k] = _mm_xor_si128(X.i128[k], V[j + k]);
125  xor_salsa8_sse2(&X.i128[0], &X.i128[4]);
126  xor_salsa8_sse2(&X.i128[4], &X.i128[0]);
127  }
128 
129  for (k = 0; k < 2; k++) {
130  for (i = 0; i < 16; i++) {
131  le32enc(&B[(k * 16 + (i * 5 % 16)) * 4], X.u32[k * 16 + i]);
132  }
133  }
134 
135  PBKDF2_SHA256((const uint8_t *)input, 80, B, 128, 1, (uint8_t *)output, 32);
136 }
void PBKDF2_SHA256(const uint8_t *passwd, size_t passwdlen, const uint8_t *salt, size_t saltlen, uint64_t c, uint8_t *buf, size_t dkLen)
PBKDF2_SHA256(passwd, passwdlen, salt, saltlen, c, buf, dkLen): Compute PBKDF2(passwd, salt, c, dkLen) using HMAC-SHA256 as the PRF, and write the output to buf.
Definition: scrypt.cpp:137
unsigned char uint8_t
Definition: stdint.h:19
unsigned int uint32_t
Definition: stdint.h:21
#define X(name)
Definition: net.cpp:606
void scrypt_1024_1_1_256_sp_sse2(const char *input, char *output, char *scratchpad)
Definition: scrypt-sse2.cpp:95