Fabcoin Core  0.16.2
P2P Digital Currency
internal.h
Go to the documentation of this file.
1 #pragma once
2 #include "compiler.h"
3 #include "endian.h"
4 #include "ethash.h"
5 #include <stdio.h>
6 
7 #define ENABLE_SSE 0
8 
9 #if defined(_M_X64) && ENABLE_SSE
10 #include <smmintrin.h>
11 #elif defined(__MIC__)
12 #include <immintrin.h>
13 #endif
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 // compile time settings
20 #define NODE_WORDS (64/4)
21 #define MIX_WORDS (ETHASH_MIX_BYTES/4)
22 #define MIX_NODES (MIX_WORDS / NODE_WORDS)
23 #include <stdint.h>
24 
25 typedef union node {
26  uint8_t bytes[NODE_WORDS * 4];
27  uint32_t words[NODE_WORDS];
28  uint64_t double_words[NODE_WORDS / 2];
29 
30 #if defined(_M_X64) && ENABLE_SSE
31  __m128i xmm[NODE_WORDS/4];
32 #elif defined(__MIC__)
33  __m512i zmm[NODE_WORDS/16];
34 #endif
35 
36 } node;
37 
38 static inline uint8_t ethash_h256_get(ethash_h256_t const* hash, unsigned int i)
39 {
40  return hash->b[i];
41 }
42 
43 static inline void ethash_h256_set(ethash_h256_t* hash, unsigned int i, uint8_t v)
44 {
45  hash->b[i] = v;
46 }
47 
48 static inline void ethash_h256_reset(ethash_h256_t* hash)
49 {
50  memset(hash, 0, 32);
51 }
52 
53 // Returns if hash is less than or equal to boundary (2^256/difficulty)
54 static inline bool ethash_check_difficulty(
55  ethash_h256_t const* hash,
56  ethash_h256_t const* boundary
57 )
58 {
59  // Boundary is big endian
60  for (int i = 0; i < 32; i++) {
61  if (ethash_h256_get(hash, i) == ethash_h256_get(boundary, i)) {
62  continue;
63  }
64  return ethash_h256_get(hash, i) < ethash_h256_get(boundary, i);
65  }
66  return true;
67 }
68 
79  ethash_h256_t const* header_hash,
80  uint64_t const nonce,
81  ethash_h256_t const* mix_hash,
82  ethash_h256_t const* boundary
83 );
84 
85 struct ethash_light {
86  void* cache;
87  uint64_t cache_size;
88  uint64_t block_number;
89 };
90 
100 ethash_light_t ethash_light_new_internal(uint64_t cache_size, ethash_h256_t const* seed);
101 
112  ethash_light_t light,
113  uint64_t full_size,
114  ethash_h256_t const header_hash,
115  uint64_t nonce
116 );
117 
118 struct ethash_full {
119  FILE* file;
120  uint64_t file_size;
122 };
123 
142  char const* dirname,
143  ethash_h256_t const seed_hash,
144  uint64_t full_size,
145  ethash_light_t const light,
146  ethash_callback_t callback
147 );
148 
150  node* const ret,
151  uint32_t node_index,
152  ethash_light_t const cache
153 );
154 
155 void ethash_quick_hash(
156  ethash_h256_t* return_hash,
157  ethash_h256_t const* header_hash,
158  const uint64_t nonce,
159  ethash_h256_t const* mix_hash
160 );
161 
162 uint64_t ethash_get_datasize(uint64_t const block_number);
163 uint64_t ethash_get_cachesize(uint64_t const block_number);
164 
175  void* mem,
176  uint64_t full_size,
177  ethash_light_t const light,
178  ethash_callback_t callback
179 );
180 
181 #ifdef __cplusplus
182 }
183 #endif
union node node
FILE * file
Definition: internal.h:119
ethash_full_t ethash_full_new_internal(char const *dirname, ethash_h256_t const seed_hash, uint64_t full_size, ethash_light_t const light, ethash_callback_t callback)
Allocate and initialize a new ethash_full handler.
Definition: internal.c:466
bool ethash_compute_full_data(void *mem, uint64_t full_size, ethash_light_t const light, ethash_callback_t callback)
Compute the memory data for a full node&#39;s memory.
Definition: internal.c:188
uint64_t cache_size
Definition: internal.h:87
uint64_t ethash_get_cachesize(uint64_t const block_number)
Definition: internal.c:82
bool ethash_quick_check_difficulty(ethash_h256_t const *header_hash, uint64_t const nonce, ethash_h256_t const *mix_hash, ethash_h256_t const *boundary)
Difficulty quick check for POW preverification.
Definition: internal.c:354
void ethash_quick_hash(ethash_h256_t *return_hash, ethash_h256_t const *header_hash, const uint64_t nonce, ethash_h256_t const *mix_hash)
Definition: internal.c:328
uint64_t double_words[NODE_WORDS/2]
Definition: internal.h:28
node * data
Definition: internal.h:121
#define NODE_WORDS
Definition: internal.h:20
ethash_light_t ethash_light_new_internal(uint64_t cache_size, ethash_h256_t const *seed)
Allocate and initialize a new ethash_light handler.
Definition: internal.c:367
Type of a seedhash/blockhash e.t.c.
Definition: ethash.h:48
void ethash_calculate_dag_item(node *const ret, uint32_t node_index, ethash_light_t const cache)
Definition: internal.c:125
uint32_t words[NODE_WORDS]
Definition: internal.h:27
uint64_t file_size
Definition: internal.h:120
int(* ethash_callback_t)(unsigned)
Definition: ethash.h:62
uint8_t b[32]
Definition: ethash.h:48
ethash_return_value_t ethash_light_compute_internal(ethash_light_t light, uint64_t full_size, ethash_h256_t const header_hash, uint64_t nonce)
Calculate the light client data.
Definition: internal.c:417
uint64_t block_number
Definition: internal.h:88
uint8_t bytes[NODE_WORDS *4]
Definition: internal.h:26
void * cache
Definition: internal.h:86
uint64_t ethash_get_datasize(uint64_t const block_number)
Definition: internal.c:76
Definition: internal.h:25