2 * Copyright 2009 Colin Percival, 2011 ArtForz, 2012-2013 pooler
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * This file was originally written by Colin Percival as part of the Tarsnap
27 * online backup system.
33 #include <emmintrin.h>
38 static inline uint32_t le32dec(const void *pp)
40 const uint8_t *p = (uint8_t const *)pp;
41 return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) +
42 ((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24));
45 static inline void le32enc(void *pp, uint32_t x)
47 uint8_t *p = (uint8_t *)pp;
49 p[1] = (x >> 8) & 0xff;
50 p[2] = (x >> 16) & 0xff;
51 p[3] = (x >> 24) & 0xff;
54 static inline void xor_salsa8_sse2(__m128i B[4], const __m128i Bx[4])
56 __m128i X0 = B[0] = _mm_xor_si128(B[0], Bx[0]);
57 __m128i X1 = B[1] = _mm_xor_si128(B[1], Bx[1]);
58 __m128i X2 = B[2] = _mm_xor_si128(B[2], Bx[2]);
59 __m128i X3 = B[3] = _mm_xor_si128(B[3], Bx[3]);
61 for (uint32_t i = 0; i < 8; i += 2) {
62 /* Operate on "columns". */
63 __m128i T = _mm_add_epi32(X0, X3);
64 X1 = _mm_xor_si128(X1, _mm_slli_epi32(T, 7));
65 X1 = _mm_xor_si128(X1, _mm_srli_epi32(T, 25));
66 T = _mm_add_epi32(X1, X0);
67 X2 = _mm_xor_si128(X2, _mm_slli_epi32(T, 9));
68 X2 = _mm_xor_si128(X2, _mm_srli_epi32(T, 23));
69 T = _mm_add_epi32(X2, X1);
70 X3 = _mm_xor_si128(X3, _mm_slli_epi32(T, 13));
71 X3 = _mm_xor_si128(X3, _mm_srli_epi32(T, 19));
72 T = _mm_add_epi32(X3, X2);
73 X0 = _mm_xor_si128(X0, _mm_slli_epi32(T, 18));
74 X0 = _mm_xor_si128(X0, _mm_srli_epi32(T, 14));
77 X1 = _mm_shuffle_epi32(X1, 0x93);
78 X2 = _mm_shuffle_epi32(X2, 0x4E);
79 X3 = _mm_shuffle_epi32(X3, 0x39);
81 /* Operate on "rows". */
82 T = _mm_add_epi32(X0, X1);
83 X3 = _mm_xor_si128(X3, _mm_slli_epi32(T, 7));
84 X3 = _mm_xor_si128(X3, _mm_srli_epi32(T, 25));
85 T = _mm_add_epi32(X3, X0);
86 X2 = _mm_xor_si128(X2, _mm_slli_epi32(T, 9));
87 X2 = _mm_xor_si128(X2, _mm_srli_epi32(T, 23));
88 T = _mm_add_epi32(X2, X3);
89 X1 = _mm_xor_si128(X1, _mm_slli_epi32(T, 13));
90 X1 = _mm_xor_si128(X1, _mm_srli_epi32(T, 19));
91 T = _mm_add_epi32(X1, X2);
92 X0 = _mm_xor_si128(X0, _mm_slli_epi32(T, 18));
93 X0 = _mm_xor_si128(X0, _mm_srli_epi32(T, 14));
96 X1 = _mm_shuffle_epi32(X1, 0x39);
97 X2 = _mm_shuffle_epi32(X2, 0x4E);
98 X3 = _mm_shuffle_epi32(X3, 0x93);
101 B[0] = _mm_add_epi32(B[0], X0);
102 B[1] = _mm_add_epi32(B[1], X1);
103 B[2] = _mm_add_epi32(B[2], X2);
104 B[3] = _mm_add_epi32(B[3], X3);
107 uint256 scrypt_blockhash(const uint8_t* input)
109 uint8_t scratchpad[SCRYPT_BUFFER_SIZE];
110 __m128i *V = (__m128i *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63));
113 void *const tmp = const_cast<uint8_t*>(input);
114 PKCS5_PBKDF2_HMAC(static_cast<const char*>(tmp), 80, input, 80, 1, EVP_sha256(), 128, B);
121 for (k = 0; k < 2; k++) {
122 for (i = 0; i < 16; i++) {
123 X.u32[k * 16 + i] = le32dec(&B[(k * 16 + (i * 5 % 16)) * 4]);
127 for (i = 0; i < 1024; i++) {
128 for (k = 0; k < 8; k++)
129 V[i * 8 + k] = X.i128[k];
130 xor_salsa8_sse2(&X.i128[0], &X.i128[4]);
131 xor_salsa8_sse2(&X.i128[4], &X.i128[0]);
133 for (i = 0; i < 1024; i++) {
134 uint32_t j = 8 * (X.u32[16] & 1023);
135 for (k = 0; k < 8; k++)
136 X.i128[k] = _mm_xor_si128(X.i128[k], V[j + k]);
137 xor_salsa8_sse2(&X.i128[0], &X.i128[4]);
138 xor_salsa8_sse2(&X.i128[4], &X.i128[0]);
141 for (k = 0; k < 2; k++) {
142 for (i = 0; i < 16; i++) {
143 le32enc(&B[(k * 16 + (i * 5 % 16)) * 4], X.u32[k * 16 + i]);
148 PKCS5_PBKDF2_HMAC(static_cast<const char*>(tmp), 80, B, 128, 1, EVP_sha256(), 32, (unsigned char*)&result);