Raven Core  3.0.0
P2P Digital Currency
whirlpool.c
Go to the documentation of this file.
1 #include "miner.h"
2 #include "algo-gate-api.h"
3 
4 #include <stdlib.h>
5 #include <stdint.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include "sph_whirlpool.h"
9 
10 typedef struct {
11  sph_whirlpool_context whirl1;
12  sph_whirlpool_context whirl2;
13  sph_whirlpool_context whirl3;
14  sph_whirlpool_context whirl4;
16 
17 static whirlpool_ctx_holder whirl_ctx;
18 static __thread sph_whirlpool_context whirl1_mid_ctx;
19 
21 {
22  sph_whirlpool1_init( &whirl_ctx.whirl1 );
23  sph_whirlpool1_init( &whirl_ctx.whirl2 );
24  sph_whirlpool1_init( &whirl_ctx.whirl3 );
25  sph_whirlpool1_init( &whirl_ctx.whirl4 );
26 }
27 
28 void whirlpool_hash(void *state, const void *input)
29 {
31  memcpy( &ctx, &whirl_ctx, sizeof(whirl_ctx) );
32 
33  const int midlen = 64;
34  const int tail = 80 - midlen;
35  unsigned char hash[128]; // uint32_t hashA[16], hashB[16];
36 #define hashB hash+64
37 
38  // copy cached midstate
39  memcpy( &ctx.whirl1, &whirl1_mid_ctx, sizeof whirl1_mid_ctx );
40  sph_whirlpool1( &ctx.whirl1, input + midlen, tail );
41  sph_whirlpool1_close(&ctx.whirl1, hash);
42 
43  sph_whirlpool1(&ctx.whirl2, hash, 64);
44  sph_whirlpool1_close(&ctx.whirl2, hashB);
45 
46  sph_whirlpool1(&ctx.whirl3, hashB, 64);
47  sph_whirlpool1_close(&ctx.whirl3, hash);
48 
49  sph_whirlpool1(&ctx.whirl4, hash, 64);
50  sph_whirlpool1_close(&ctx.whirl4, hash);
51 
52  memcpy(state, hash, 32);
53 }
54 
55 void whirlpool_midstate( const void* input )
56 {
57  memcpy( &whirl1_mid_ctx, &whirl_ctx.whirl1, sizeof whirl1_mid_ctx );
58  sph_whirlpool1( &whirl1_mid_ctx, input, 64 );
59 }
60 
61 
62 int scanhash_whirlpool(int thr_id, struct work* work, uint32_t max_nonce, unsigned long *hashes_done)
63 {
64  uint32_t _ALIGN(128) endiandata[20];
65  uint32_t* pdata = work->data;
66  uint32_t* ptarget = work->target;
67  const uint32_t first_nonce = pdata[19];
68  uint32_t n = first_nonce - 1;
69 
70 // if (opt_benchmark)
71 // ((uint32_t*)ptarget)[7] = 0x0000ff;
72 
73  for (int i=0; i < 19; i++)
74  be32enc(&endiandata[i], pdata[i]);
75 
76  whirlpool_midstate( endiandata );
77 
78  do {
79  const uint32_t Htarg = ptarget[7];
80  uint32_t vhash[8];
81  pdata[19] = ++n;
82  be32enc(&endiandata[19], n );
83  whirlpool_hash(vhash, endiandata);
84 
85  if (vhash[7] <= Htarg && fulltest(vhash, ptarget)) {
86 // work_set_target_ratio(work, vhash);
87  *hashes_done = n - first_nonce + 1;
88  return true;
89  }
90 
91  } while ( n < max_nonce && !work_restart[thr_id].restart);
92 
93  *hashes_done = n - first_nonce + 1;
94  pdata[19] = n;
95  return 0;
96 }
97 
98 bool register_whirlpool_algo( algo_gate_t* gate )
99 {
100  gate->scanhash = (void*)&scanhash_whirlpool;
101  gate->hash = (void*)&whirlpool_hash;
103  return true;
104 };
sph_whirlpool_context whirl4
Definition: whirlpool.c:14
bool register_whirlpool_algo(algo_gate_t *gate)
Definition: whirlpool.c:98
sph_whirlpool_context whirl2
Definition: whirlpool.c:12
sph_whirlpool_context whirl1
Definition: whirlpool.c:11
WHIRLPOOL interface.
#define hashB
sph_whirlpool_context whirl3
Definition: whirlpool.c:13
void * memcpy(void *a, const void *b, size_t c)
void whirlpool_midstate(const void *input)
Definition: whirlpool.c:55
int scanhash_whirlpool(int thr_id, struct work *work, uint32_t max_nonce, unsigned long *hashes_done)
Definition: whirlpool.c:62
void init_whirlpool_ctx()
Definition: whirlpool.c:20
void whirlpool_hash(void *state, const void *input)
Definition: whirlpool.c:28