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