]> Git Repo - cpuminer-multi.git/blob - blake.c
Update README.md
[cpuminer-multi.git] / blake.c
1 #include "cpuminer-config.h"
2 #include "miner.h"
3
4 #include <string.h>
5 #include <stdint.h>
6
7 #include "sha3/sph_blake.h"
8
9 /* Move init out of loop, so init once externally, and then use one single memcpy with that bigger memory block */
10 typedef struct {
11         sph_blake256_context    blake1;
12 } blakehash_context_holder;
13
14 blakehash_context_holder base_contexts;
15
16 void init_blakehash_contexts()
17 {
18     sph_blake256_init(&base_contexts.blake1);
19 }
20
21 static void blakehash(void *state, const void *input)
22 {
23     blakehash_context_holder ctx;
24 //an array of uint32
25     uint32_t hashA[8];
26         
27
28 //do one memcopy to get fresh contexts, its faster even with a larger block then issuing 9 memcopies
29     memcpy(&ctx, &base_contexts, sizeof(base_contexts));
30
31     sph_blake256 (&ctx.blake1, input, 80);
32     sph_blake256_close (&ctx.blake1, hashA);     //0
33     memcpy(state, hashA, 32);
34
35 }
36
37 int scanhash_blake(int thr_id, uint32_t *pdata, const uint32_t *ptarget,
38         uint32_t max_nonce, unsigned long *hashes_done)
39 {
40         uint32_t n = pdata[19] - 1;
41         const uint32_t first_nonce = pdata[19];
42         const uint32_t Htarg = ptarget[7];
43         uint32_t hash64[8] __attribute__((aligned(32)));
44         uint32_t endiandata[32];
45         
46         //char testdata[] = {"\x70\x00\x00\x00\x5d\x38\x5b\xa1\x14\xd0\x79\x97\x0b\x29\xa9\x41\x8f\xd0\x54\x9e\x7d\x68\xa9\x5c\x7f\x16\x86\x21\xa3\x14\x20\x10\x00\x00\x00\x00\x57\x85\x86\xd1\x49\xfd\x07\xb2\x2f\x3a\x8a\x34\x7c\x51\x6d\xe7\x05\x2f\x03\x4d\x2b\x76\xff\x68\xe0\xd6\xec\xff\x9b\x77\xa4\x54\x89\xe3\xfd\x51\x17\x32\x01\x1d\xf0\x73\x10\x00"};
47         
48         //we need bigendian data...
49         //lessons learned: do NOT endianchange directly in pdata, this will all proof-of-works be considered as stale from minerd.... 
50         int kk=0;
51         for (; kk < 32; kk++)
52         {
53                 be32enc(&endiandata[kk], ((uint32_t*)pdata)[kk]);
54         };
55
56 //      if (opt_debug) 
57 //      {
58 //              applog(LOG_DEBUG, "Thr: %02d, firstN: %08x, maxN: %08x, ToDo: %d", thr_id, first_nonce, max_nonce, max_nonce-first_nonce);
59 //      }
60         
61         /* I'm to lazy to put the loop in an inline function... so dirty copy'n'paste.... */
62         /* i know that i could set a variable, but i don't know how the compiler will optimize it, not that then the cpu needs to load the value *everytime* in a register */
63         if (ptarget[7]==0) {
64                 do {
65                         pdata[19] = ++n;
66                         be32enc(&endiandata[19], n); 
67                         blakehash(hash64, &endiandata);
68                         if (((hash64[7]&0xFFFFFFFF)==0) && 
69                                         fulltest(hash64, ptarget)) {
70                                 *hashes_done = n - first_nonce + 1;
71                                 return true;
72                         }
73                 } while (n < max_nonce && !work_restart[thr_id].restart);
74         }
75         else if (ptarget[7]<=0xF)
76         {
77                 do {
78                         pdata[19] = ++n;
79                         be32enc(&endiandata[19], n); 
80                         blakehash(hash64, &endiandata);
81                         if (((hash64[7]&0xFFFFFFF0)==0) &&
82                                         fulltest(hash64, ptarget)) {
83                                 *hashes_done = n - first_nonce + 1;
84                                 return true;
85                         }
86                 } while (n < max_nonce && !work_restart[thr_id].restart);       
87         } 
88         else if (ptarget[7]<=0xFF) 
89         {
90                 do {
91                         pdata[19] = ++n;
92                         be32enc(&endiandata[19], n); 
93                         blakehash(hash64, &endiandata);
94                         if (((hash64[7]&0xFFFFFF00)==0) && 
95                                         fulltest(hash64, ptarget)) {
96                                 *hashes_done = n - first_nonce + 1;
97                                 return true;
98                         }
99                 } while (n < max_nonce && !work_restart[thr_id].restart);       
100         } 
101         else if (ptarget[7]<=0xFFF) 
102         {
103                 do {
104                         pdata[19] = ++n;
105                         be32enc(&endiandata[19], n); 
106                         blakehash(hash64, &endiandata);
107                         if (((hash64[7]&0xFFFFF000)==0) && 
108                                         fulltest(hash64, ptarget)) {
109                                 *hashes_done = n - first_nonce + 1;
110                                 return true;
111                         }
112                 } while (n < max_nonce && !work_restart[thr_id].restart);       
113
114         }
115         else if (ptarget[7]<=0xFFFF)
116         {
117                 do {
118                         pdata[19] = ++n;
119                         be32enc(&endiandata[19], n); 
120                         blakehash(hash64, &endiandata);
121                         if (((hash64[7]&0xFFFF0000)==0) && 
122                                         fulltest(hash64, ptarget)) {
123                                 *hashes_done = n - first_nonce + 1;
124                                 return true;
125                         }
126                 } while (n < max_nonce && !work_restart[thr_id].restart);       
127
128         }
129         else 
130         {
131                 do {
132                         pdata[19] = ++n;
133                         be32enc(&endiandata[19], n); 
134                         blakehash(hash64, &endiandata);
135                         if (fulltest(hash64, ptarget)) {
136                                 *hashes_done = n - first_nonce + 1;
137                                 return true;
138                         }
139                 } while (n < max_nonce && !work_restart[thr_id].restart);       
140         }
141
142         
143         *hashes_done = n - first_nonce + 1;
144         pdata[19] = n;
145         return 0;
146 }
This page took 0.040225 seconds and 4 git commands to generate.