]> Git Repo - cpuminer-multi.git/blob - algo/fresh.c
add rainforest algo (#29)
[cpuminer-multi.git] / algo / fresh.c
1 #include "miner.h"
2
3 #include <stdlib.h>
4 #include <stdint.h>
5 #include <string.h>
6 #include <stdio.h>
7
8 #include "sha3/sph_shavite.h"
9 #include "sha3/sph_simd.h"
10 #include "sha3/sph_echo.h"
11
12 //#define DEBUG_ALGO
13
14 extern void freshhash(void* output, const void* input, uint32_t len)
15 {
16         unsigned char hash[128]; // uint32_t hashA[16], hashB[16];
17         #define hashA hash
18         #define hashB hash+64
19
20         sph_shavite512_context ctx_shavite;
21         sph_simd512_context ctx_simd;
22         sph_echo512_context ctx_echo;
23
24         sph_shavite512_init(&ctx_shavite);
25         sph_shavite512(&ctx_shavite, input, len);
26         sph_shavite512_close(&ctx_shavite, hashA);
27
28         sph_simd512_init(&ctx_simd);
29         sph_simd512(&ctx_simd, hashA, 64);
30         sph_simd512_close(&ctx_simd, hashB);
31
32         sph_shavite512_init(&ctx_shavite);
33         sph_shavite512(&ctx_shavite, hashB, 64);
34         sph_shavite512_close(&ctx_shavite, hashA);
35
36         sph_simd512_init(&ctx_simd);
37         sph_simd512(&ctx_simd, hashA, 64);
38         sph_simd512_close(&ctx_simd, hashB);
39
40         sph_echo512_init(&ctx_echo);
41         sph_echo512(&ctx_echo, hashB, 64);
42         sph_echo512_close(&ctx_echo, hashA);
43
44         memcpy(output, hash, 32);
45 }
46
47 int scanhash_fresh(int thr_id, struct work *work, uint32_t max_nonce, uint64_t *hashes_done)
48 {
49         uint32_t _ALIGN(128) hash32[8];
50         uint32_t _ALIGN(128) endiandata[20];
51         uint32_t *pdata = work->data;
52         uint32_t *ptarget = work->target;
53
54         const uint32_t first_nonce = pdata[19];
55         const uint32_t Htarg = ptarget[7];
56         const uint32_t len = 80;
57         uint32_t n = pdata[19] - 1;
58
59         uint64_t htmax[] = {
60                 0,
61                 0xF,
62                 0xFF,
63                 0xFFF,
64                 0xFFFF,
65                 0x10000000
66         };
67         uint32_t masks[] = {
68                 0xFFFFFFFF,
69                 0xFFFFFFF0,
70                 0xFFFFFF00,
71                 0xFFFFF000,
72                 0xFFFF0000,
73                 0
74         };
75
76         // we need bigendian data...
77         for (int kk=0; kk < 32; kk++) {
78                 be32enc(&endiandata[kk], ((uint32_t*)pdata)[kk]);
79         };
80 #ifdef DEBUG_ALGO
81         if (Htarg != 0)
82                 printf("[%d] Htarg=%X\n", thr_id, Htarg);
83 #endif
84         for (int m=0; m < 6; m++) {
85                 if (Htarg <= htmax[m]) {
86                         uint32_t mask = masks[m];
87                         do {
88                                 pdata[19] = ++n;
89                                 be32enc(&endiandata[19], n);
90                                 freshhash(hash32, endiandata, len);
91 #ifndef DEBUG_ALGO
92                                 if ((!(hash32[7] & mask)) && fulltest(hash32, ptarget)) {
93                                         work_set_target_ratio(work, hash32);
94                                         *hashes_done = n - first_nonce + 1;
95                                         return 1;
96                                 }
97 #else
98                                 if (!(n % 0x1000) && !thr_id) printf(".");
99                                 if (!(hash32[7] & mask)) {
100                                         printf("[%d]",thr_id);
101                                         if (fulltest(hash32, ptarget)) {
102                                                 work_set_target_ratio(work, hash32);
103                                                 *hashes_done = n - first_nonce + 1;
104                                                 return 1;
105                                         }
106                                 }
107 #endif
108                         } while (n < max_nonce && !work_restart[thr_id].restart);
109                         // see blake.c if else to understand the loop on htmax => mask
110                         break;
111                 }
112         }
113
114         *hashes_done = n - first_nonce + 1;
115         pdata[19] = n;
116         return 0;
117 }
This page took 0.0367 seconds and 4 git commands to generate.