Update to 0.3.0 (New upstream + new RPC calls)
[novacoin.git] / src / scrypt_mine.cpp
1 /*-
2  * Copyright 2009 Colin Percival, 2011 ArtForz, 2011 pooler, 2013 Balthazar
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 <stdlib.h>
31 #include <stdint.h>
32 #include <xmmintrin.h>
33
34 #include "scrypt_mine.h"
35 #include "pbkdf2.h"
36
37 #include "util.h"
38 #include "net.h"
39
40 extern bool fShutdown;
41 extern bool fGenerateBitcoins;
42
43 extern CBlockIndex* pindexBest;
44 extern uint32_t nTransactionsUpdated;
45
46
47 #if defined(__x86_64__)
48
49 #define SCRYPT_3WAY
50 #define SCRYPT_BUFFER_SIZE (3 * 131072 + 63)
51
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);
56
57 #elif defined(__i386__)
58
59 #define SCRYPT_BUFFER_SIZE (131072 + 63)
60
61 extern  "C" void scrypt_core(uint32_t *X, uint32_t *V);
62
63 #endif
64
65 void *scrypt_buffer_alloc() {
66     return malloc(SCRYPT_BUFFER_SIZE);
67 }
68
69 void scrypt_buffer_free(void *scratchpad)
70 {
71     free(scratchpad);
72 }
73
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
77  */
78
79 static void scrypt(const void* input, size_t inputlen, uint32_t *res, void *scratchpad)
80 {
81     uint32_t *V;
82     uint32_t X[32];
83     V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63));
84
85     PBKDF2_SHA256((const uint8_t*)input, inputlen, (const uint8_t*)input, sizeof(block_header), 1, (uint8_t *)X, 128);
86
87     scrypt_core(X, V);
88
89     PBKDF2_SHA256((const uint8_t*)input, inputlen, (uint8_t *)X, 128, 1, (uint8_t*)res, 32);
90 }
91
92 void scrypt_hash(const void* input, size_t inputlen, uint32_t *res, void *scratchpad)
93 {
94     return scrypt(input, inputlen, res, scratchpad);
95 }
96
97 #ifdef SCRYPT_3WAY
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)
99 {
100     uint32_t *V;
101     uint32_t X[32], Y[32];
102     V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63));
103
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);
106
107     scrypt_core_2way(X, Y, V);
108
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);
111 }
112
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,
115    void *scratchpad)
116 {
117     uint32_t *V;
118     uint32_t X[32], Y[32], Z[32];
119     V = (uint32_t *)(((uintptr_t)(scratchpad) + 63) & ~ (uintptr_t)(63));
120
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);
124
125     scrypt_core_3way(X, Y, Z, V);
126
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);
130 }
131 #endif
132
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)
136 {
137     hash_count = 0;
138     block_header data = *pdata;
139     uint32_t hash[8];
140     unsigned char *hashc = (unsigned char *) &hash;
141
142 #ifdef SCRYPT_3WAY
143     block_header data2 = *pdata;
144     uint32_t hash2[8];
145     unsigned char *hashc2 = (unsigned char *) &hash2;
146
147     block_header data3 = *pdata;
148     uint32_t hash3[8];
149     unsigned char *hashc3 = (unsigned char *) &hash3;
150
151     int throughput = scrypt_best_throughput();
152 #endif
153
154     uint32_t n = 0;
155
156     while (true) {
157
158         data.nonce = n++;
159
160 #ifdef SCRYPT_3WAY
161         if (throughput >= 2 && n < max_nonce) {
162             data2.nonce = n++;
163             if(throughput >= 3)
164             {
165                 data3.nonce = n++;
166                 scrypt_3way(&data, &data2, &data3, 80, 80, 80, hash, hash2, hash3, scratchbuf);
167                 hash_count += 3;
168
169                 if (hashc3[31] == 0 && hashc3[30] == 0) {
170                     memcpy(result, hash3, 32);
171                     *res_header = data3;
172
173                     return data3.nonce;
174                 }
175             }
176             else
177             {
178                 scrypt_2way(&data, &data2, 80, 80, hash, hash2, scratchbuf);
179                 hash_count += 2;
180             }
181
182             if (hashc2[31] == 0 && hashc2[30] == 0) {
183                 memcpy(result, hash2, 32);
184
185                 return data2.nonce;
186             }
187         } else {
188             scrypt(&data, 80, hash, scratchbuf);
189             hash_count += 1;
190         }
191 #else
192         scrypt(&data, 80, hash, scratchbuf);
193         hash_count += 1;
194 #endif
195         if (hashc[31] == 0 && hashc[30] == 0) {
196             memcpy(result, hash, 32);
197
198             return data.nonce;
199         }
200
201         if (n >= max_nonce) {
202             hash_count = 0xffff + 1;
203             break;
204         }
205     }
206
207     return (unsigned int) -1;
208 }