Headers cleanup
[novacoin.git] / src / crypto / scrypt / intrin / scrypt-intrin.cpp
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 #ifdef __ARM_NEON
31 #include <sse2neon.h>
32 #else
33 #include <emmintrin.h>
34 #endif
35
36 #include "scrypt.h"
37
38 #include <openssl/evp.h>
39
40 static inline uint32_t le32dec(const void *pp)
41 {
42     const uint8_t *p = (uint8_t const *)pp;
43     return ((uint32_t)(p[0]) + ((uint32_t)(p[1]) << 8) +
44     ((uint32_t)(p[2]) << 16) + ((uint32_t)(p[3]) << 24));
45 }
46
47 static inline void le32enc(void *pp, uint32_t x)
48 {
49     uint8_t *p = (uint8_t *)pp;
50     p[0] = x & 0xff;
51     p[1] = (x >> 8) & 0xff;
52     p[2] = (x >> 16) & 0xff;
53     p[3] = (x >> 24) & 0xff;
54 }
55
56 static inline void xor_salsa8_sse2(__m128i B[4], const __m128i Bx[4])
57 {
58     __m128i X0 = B[0] = _mm_xor_si128(B[0], Bx[0]);
59     __m128i X1 = B[1] = _mm_xor_si128(B[1], Bx[1]);
60     __m128i X2 = B[2] = _mm_xor_si128(B[2], Bx[2]);
61     __m128i X3 = B[3] = _mm_xor_si128(B[3], Bx[3]);
62
63     for (uint32_t i = 0; i < 8; i += 2) {
64         /* Operate on "columns". */
65         __m128i T = _mm_add_epi32(X0, X3);
66         X1 = _mm_xor_si128(X1, _mm_slli_epi32(T, 7));
67         X1 = _mm_xor_si128(X1, _mm_srli_epi32(T, 25));
68         T = _mm_add_epi32(X1, X0);
69         X2 = _mm_xor_si128(X2, _mm_slli_epi32(T, 9));
70         X2 = _mm_xor_si128(X2, _mm_srli_epi32(T, 23));
71         T = _mm_add_epi32(X2, X1);
72         X3 = _mm_xor_si128(X3, _mm_slli_epi32(T, 13));
73         X3 = _mm_xor_si128(X3, _mm_srli_epi32(T, 19));
74         T = _mm_add_epi32(X3, X2);
75         X0 = _mm_xor_si128(X0, _mm_slli_epi32(T, 18));
76         X0 = _mm_xor_si128(X0, _mm_srli_epi32(T, 14));
77
78         /* Rearrange data. */
79         X1 = _mm_shuffle_epi32(X1, 0x93);
80         X2 = _mm_shuffle_epi32(X2, 0x4E);
81         X3 = _mm_shuffle_epi32(X3, 0x39);
82
83         /* Operate on "rows". */
84         T = _mm_add_epi32(X0, X1);
85         X3 = _mm_xor_si128(X3, _mm_slli_epi32(T, 7));
86         X3 = _mm_xor_si128(X3, _mm_srli_epi32(T, 25));
87         T = _mm_add_epi32(X3, X0);
88         X2 = _mm_xor_si128(X2, _mm_slli_epi32(T, 9));
89         X2 = _mm_xor_si128(X2, _mm_srli_epi32(T, 23));
90         T = _mm_add_epi32(X2, X3);
91         X1 = _mm_xor_si128(X1, _mm_slli_epi32(T, 13));
92         X1 = _mm_xor_si128(X1, _mm_srli_epi32(T, 19));
93         T = _mm_add_epi32(X1, X2);
94         X0 = _mm_xor_si128(X0, _mm_slli_epi32(T, 18));
95         X0 = _mm_xor_si128(X0, _mm_srli_epi32(T, 14));
96
97         /* Rearrange data. */
98         X1 = _mm_shuffle_epi32(X1, 0x39);
99         X2 = _mm_shuffle_epi32(X2, 0x4E);
100         X3 = _mm_shuffle_epi32(X3, 0x93);
101     }
102
103     B[0] = _mm_add_epi32(B[0], X0);
104     B[1] = _mm_add_epi32(B[1], X1);
105     B[2] = _mm_add_epi32(B[2], X2);
106     B[3] = _mm_add_epi32(B[3], X3);
107 }
108
109 uint256 scrypt_blockhash(const uint8_t* input)
110 {
111     uint8_t scratchpad[SCRYPT_BUFFER_SIZE];
112     __m128i *V = (__m128i *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63));
113
114     uint8_t B[128];
115     void *const tmp = const_cast<uint8_t*>(input);
116     PKCS5_PBKDF2_HMAC(static_cast<const char*>(tmp), 80, input, 80, 1, EVP_sha256(), 128, B);
117
118     union {
119         __m128i i128[8];
120         uint32_t u32[32];
121     } X;
122     uint32_t i, k;
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]);
126         }
127     }
128
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]);
134     }
135     for (i = 0; i < 1024; i++) {
136         uint32_t 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]);
141     }
142
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]);
146         }
147     }
148
149     uint256 result = 0;
150     PKCS5_PBKDF2_HMAC(static_cast<const char*>(tmp), 80, B, 128, 1, EVP_sha256(), 32, (unsigned char*)&result);
151
152     return result;
153 }