2 * Copyright 2009 Colin Percival, 2011 ArtForz, 2011 pooler, 2012 Balthazar
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.
32 #include <xmmintrin.h>
34 #include "scrypt_mine.h"
40 extern bool fShutdown;
41 extern bool fGenerateBitcoins;
43 extern CBlockIndex* pindexBest;
44 extern uint32_t nTransactionsUpdated;
47 #if defined(__x86_64__)
50 #define SCRYPT_BUFFER_SIZE (3 * 131072 + 63)
52 extern "C" int scrypt_best_throughput();
53 extern "C" void scrypt_core(uint32_t *X, uint32_t *V);
54 extern "C" void scrypt_core_2way(uint32_t *X, uint32_t *Y, uint32_t *V);
55 extern "C" void scrypt_core_3way(uint32_t *X, uint32_t *Y, uint32_t *Z, uint32_t *V);
57 #elif defined(__i386__)
59 #define SCRYPT_BUFFER_SIZE (131072 + 63)
61 extern "C" void scrypt_core(uint32_t *X, uint32_t *V);
65 void *scrypt_buffer_alloc() {
66 return malloc(SCRYPT_BUFFER_SIZE);
69 void scrypt_buffer_free(void *scratchpad)
74 /* cpu and memory intensive function to transform a 80 byte buffer into a 32 byte output
75 scratchpad size needs to be at least 63 + (128 * r * p) + (256 * r + 64) + (128 * r * N) bytes
76 r = 1, p = 1, N = 1024
79 static void scrypt(const void* input, size_t inputlen, uint32_t *res, void *scratchpad)
83 V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63));
85 PBKDF2_SHA256((const uint8_t*)input, inputlen, (const uint8_t*)input, sizeof(block_header), 1, (uint8_t *)X, 128);
89 PBKDF2_SHA256((const uint8_t*)input, inputlen, (uint8_t *)X, 128, 1, (uint8_t*)res, 32);
92 void scrypt_hash(const void* input, size_t inputlen, uint32_t *res, void *scratchpad)
94 return scrypt(input, inputlen, res, scratchpad);
98 static void scrypt_2way(const void *input1, const void *input2, size_t input1len, size_t input2len, uint32_t *res1, uint32_t *res2, void *scratchpad)
101 uint32_t X[32], Y[32];
102 V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63));
104 PBKDF2_SHA256((const uint8_t*)input1, input1len, (const uint8_t*)input1, input1len, 1, (uint8_t *)X, 128);
105 PBKDF2_SHA256((const uint8_t*)input2, input2len, (const uint8_t*)input2, input2len, 1, (uint8_t *)Y, 128);
107 scrypt_core_2way(X, Y, V);
109 PBKDF2_SHA256((const uint8_t*)input1, input1len, (uint8_t *)X, 128, 1, (uint8_t*)res1, 32);
110 PBKDF2_SHA256((const uint8_t*)input2, input2len, (uint8_t *)Y, 128, 1, (uint8_t*)res2, 32);
113 static void scrypt_3way(const void *input1, const void *input2, const void *input3,
114 size_t input1len, size_t input2len, size_t input3len, uint32_t *res1, uint32_t *res2, uint32_t *res3,
118 uint32_t X[32], Y[32], Z[32];
119 V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63));
121 PBKDF2_SHA256((const uint8_t*)input1, input1len, (const uint8_t*)input1, input1len, 1, (uint8_t *)X, 128);
122 PBKDF2_SHA256((const uint8_t*)input2, input2len, (const uint8_t*)input2, input2len, 1, (uint8_t *)Y, 128);
123 PBKDF2_SHA256((const uint8_t*)input3, input3len, (const uint8_t*)input3, input3len, 1, (uint8_t *)Z, 128);
125 scrypt_core_3way(X, Y, Z, V);
127 PBKDF2_SHA256((const uint8_t*)input1, input1len, (uint8_t *)X, 128, 1, (uint8_t*)res1, 32);
128 PBKDF2_SHA256((const uint8_t*)input2, input2len, (uint8_t *)Y, 128, 1, (uint8_t*)res2, 32);
129 PBKDF2_SHA256((const uint8_t*)input3, input3len, (uint8_t *)Z, 128, 1, (uint8_t*)res3, 32);
133 unsigned int scanhash_scrypt(block_header *pdata, void *scratchbuf,
134 uint32_t max_nonce, uint32_t &hash_count,
135 void *result, block_header *res_header)
138 block_header data = *pdata;
140 unsigned char *hashc = (unsigned char *) &hash;
143 block_header data2 = *pdata;
145 unsigned char *hashc2 = (unsigned char *) &hash2;
147 block_header data3 = *pdata;
149 unsigned char *hashc3 = (unsigned char *) &hash3;
151 int throughput = scrypt_best_throughput();
161 if (throughput >= 2 && n < max_nonce) {
166 scrypt_3way(&data, &data2, &data3, 80, 80, 80, hash, hash2, hash3, scratchbuf);
169 if (hashc3[31] == 0 && hashc3[30] == 0) {
170 memcpy(result, hash3, 32);
178 scrypt_2way(&data, &data2, 80, 80, hash, hash2, scratchbuf);
182 if (hashc2[31] == 0 && hashc2[30] == 0) {
183 memcpy(result, hash2, 32);
188 scrypt(&data, 80, hash, scratchbuf);
192 scrypt(&data, 80, hash, scratchbuf);
195 if (hashc[31] == 0 && hashc[30] == 0) {
196 memcpy(result, hash, 32);
201 if (n >= max_nonce) {
202 hash_count = 0xffff + 1;
207 return (unsigned int) -1;