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.
30 #include <emmintrin.h>
33 static inline uint32_t le32dec(const void *pp)
35 const uint8_t *p = (uint8_t const *)pp;
36 return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) +
37 ((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24));
40 static inline void le32enc(void *pp, uint32_t x)
42 uint8_t *p = (uint8_t *)pp;
44 p[1] = (x >> 8) & 0xff;
45 p[2] = (x >> 16) & 0xff;
46 p[3] = (x >> 24) & 0xff;
49 static inline void xor_salsa8_sse2(__m128i B[4], const __m128i Bx[4])
51 __m128i X0, X1, X2, X3;
55 X0 = B[0] = _mm_xor_si128(B[0], Bx[0]);
56 X1 = B[1] = _mm_xor_si128(B[1], Bx[1]);
57 X2 = B[2] = _mm_xor_si128(B[2], Bx[2]);
58 X3 = B[3] = _mm_xor_si128(B[3], Bx[3]);
60 for (i = 0; i < 8; i += 2) {
61 /* Operate on "columns". */
62 T = _mm_add_epi32(X0, X3);
63 X1 = _mm_xor_si128(X1, _mm_slli_epi32(T, 7));
64 X1 = _mm_xor_si128(X1, _mm_srli_epi32(T, 25));
65 T = _mm_add_epi32(X1, X0);
66 X2 = _mm_xor_si128(X2, _mm_slli_epi32(T, 9));
67 X2 = _mm_xor_si128(X2, _mm_srli_epi32(T, 23));
68 T = _mm_add_epi32(X2, X1);
69 X3 = _mm_xor_si128(X3, _mm_slli_epi32(T, 13));
70 X3 = _mm_xor_si128(X3, _mm_srli_epi32(T, 19));
71 T = _mm_add_epi32(X3, X2);
72 X0 = _mm_xor_si128(X0, _mm_slli_epi32(T, 18));
73 X0 = _mm_xor_si128(X0, _mm_srli_epi32(T, 14));
76 X1 = _mm_shuffle_epi32(X1, 0x93);
77 X2 = _mm_shuffle_epi32(X2, 0x4E);
78 X3 = _mm_shuffle_epi32(X3, 0x39);
80 /* Operate on "rows". */
81 T = _mm_add_epi32(X0, X1);
82 X3 = _mm_xor_si128(X3, _mm_slli_epi32(T, 7));
83 X3 = _mm_xor_si128(X3, _mm_srli_epi32(T, 25));
84 T = _mm_add_epi32(X3, X0);
85 X2 = _mm_xor_si128(X2, _mm_slli_epi32(T, 9));
86 X2 = _mm_xor_si128(X2, _mm_srli_epi32(T, 23));
87 T = _mm_add_epi32(X2, X3);
88 X1 = _mm_xor_si128(X1, _mm_slli_epi32(T, 13));
89 X1 = _mm_xor_si128(X1, _mm_srli_epi32(T, 19));
90 T = _mm_add_epi32(X1, X2);
91 X0 = _mm_xor_si128(X0, _mm_slli_epi32(T, 18));
92 X0 = _mm_xor_si128(X0, _mm_srli_epi32(T, 14));
95 X1 = _mm_shuffle_epi32(X1, 0x39);
96 X2 = _mm_shuffle_epi32(X2, 0x4E);
97 X3 = _mm_shuffle_epi32(X3, 0x93);
100 B[0] = _mm_add_epi32(B[0], X0);
101 B[1] = _mm_add_epi32(B[1], X1);
102 B[2] = _mm_add_epi32(B[2], X2);
103 B[3] = _mm_add_epi32(B[3], X3);
106 uint256 scrypt_blockhash(const uint8_t* input)
109 uint8_t scratchpad[SCRYPT_BUFFER_SIZE];
118 V = (__m128i *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63));
120 void *const tmp = const_cast<uint8_t*>(input);
121 PKCS5_PBKDF2_HMAC(static_cast<const char*>(tmp), 80, input, 80, 1, EVP_sha256(), 128, B);
123 for (k = 0; k < 2; k++) {
124 for (i = 0; i < 16; i++) {
125 X.u32[k * 16 + i] = le32dec(&B[(k * 16 + (i * 5 % 16)) * 4]);
129 for (i = 0; i < 1024; i++) {
130 for (k = 0; k < 8; k++)
131 V[i * 8 + k] = X.i128[k];
132 xor_salsa8_sse2(&X.i128[0], &X.i128[4]);
133 xor_salsa8_sse2(&X.i128[4], &X.i128[0]);
135 for (i = 0; i < 1024; i++) {
136 j = 8 * (X.u32[16] & 1023);
137 for (k = 0; k < 8; k++)
138 X.i128[k] = _mm_xor_si128(X.i128[k], V[j + k]);
139 xor_salsa8_sse2(&X.i128[0], &X.i128[4]);
140 xor_salsa8_sse2(&X.i128[4], &X.i128[0]);
143 for (k = 0; k < 2; k++) {
144 for (i = 0; i < 16; i++) {
145 le32enc(&B[(k * 16 + (i * 5 % 16)) * 4], X.u32[k * 16 + i]);
149 PKCS5_PBKDF2_HMAC(static_cast<const char*>(tmp), 80, B, 128, 1, EVP_sha256(), 32, (unsigned char*)&result);