@@ -40,24 +40,32 @@ std::array<uint8_t, 32> BlockHeader::powHash(const int32_t powVersion) const {
4040
4141mpz_class BlockHeader::target (const int32_t powVersion) const {
4242 const uint32_t difficultyIntegerPart (decodeBits (bits, powVersion));
43+ uint32_t trailingZeros;
44+ const std::array<uint8_t , 32 > hash (powHash (powVersion));
4345 mpz_class target;
44- if (powVersion == -1 )
46+ if (powVersion == -1 ) {
47+ if (difficultyIntegerPart < 265U ) return 0 ;
4548 target = 256 ;
49+ for (uint64_t i (0 ) ; i < 256 ; i++) {
50+ target <<= 1 ;
51+ if ((hash[i/8 ] >> (i % 8 )) & 1 )
52+ target.get_mpz_t ()->_mp_d [0 ]++;
53+ }
54+ trailingZeros = difficultyIntegerPart - 265U ;
55+ }
4656 else if (powVersion == 1 ) {
57+ if (difficultyIntegerPart < 264U ) return 0 ;
4758 const uint32_t df (bits & 255U );
4859 target = 256 + ((10U *df*df*df + 7383U *df*df + 5840720U *df + 3997440U ) >> 23U );
60+ target <<= 256 ;
61+ mpz_class hashGmp;
62+ mpz_import (hashGmp.get_mpz_t (), 32 , -1 , sizeof (uint8_t ), 0 , 0 , hash.begin ());
63+ target += hashGmp;
64+ trailingZeros = difficultyIntegerPart - 264U ;
4965 }
5066 else
5167 return 0 ;
5268
53- const std::array<uint8_t , 32 > hash (powHash (powVersion));
54- for (uint64_t i (0 ) ; i < 256 ; i++) {
55- target <<= 1 ;
56- if ((hash[i/8 ] >> (i % 8 )) & 1 )
57- target.get_mpz_t ()->_mp_d [0 ]++;
58- }
59- if (difficultyIntegerPart < 265U ) return 0 ;
60- const uint32_t trailingZeros (difficultyIntegerPart - 265U );
6169 target <<= trailingZeros;
6270 return target;
6371}
@@ -147,15 +155,15 @@ bool BMClient::getJob(Job& job, const bool dummy) {
147155 job.height = _height;
148156 job.difficulty = _difficulty;
149157 const uint64_t difficultyAsInteger (std::round (65536 .*job.difficulty ));
150- // Target: (in binary) 1 . Leading Digits L (16 bits) . Height (32 bits) . Requests (32 bits) . (Difficulty - 81 ) zeros = 2^(Difficulty - 81 )(2^80 + 2^64*L + 2^32*Height + Requests)
158+ // Target: (in binary) 1 . Leading Digits L (16 bits) . Height (32 bits) . Requests (32 bits) . (Difficulty - 80 ) zeros = 2^(Difficulty - 80 )(2^80 + 2^64*L + 2^32*Height + Requests)
151159 job.target = 1 ;
152160 job.target <<= 16 ;
153161 job.target += static_cast <uint16_t >(std::round (std::pow (2 ., 16 . + static_cast <double >(difficultyAsInteger % 65536 )/65536 .)) - 65536 .);
154162 job.target <<= 32 ;
155163 job.target += job.height ;
156164 job.target <<= 32 ;
157165 job.target += _requests;
158- job.target <<= (difficultyAsInteger/65536ULL - 81ULL );
166+ job.target <<= (difficultyAsInteger/65536ULL - 80ULL );
159167 job.primeCountTarget = _pattern.size ();
160168 job.primeCountMin = job.primeCountTarget ;
161169 if (!dummy) _requests++;
@@ -165,7 +173,7 @@ bool BMClient::getJob(Job& job, const bool dummy) {
165173bool SearchClient::getJob (Job& job, const bool ) {
166174 job.height = 1 ;
167175 job.difficulty = _difficulty;
168- // Target: (in binary) 1 . Leading Digits L (16 bits) . 80 Random Bits . (Difficulty - 97 ) zeros = 2^(Difficulty - 97 )*(2^96 + 2^80*L + Random)
176+ // Target: (in binary) 1 . Leading Digits L (16 bits) . 80 Random Bits . (Difficulty - 96 ) zeros = 2^(Difficulty - 96 )*(2^96 + 2^80*L + Random)
169177 const uint64_t difficultyAsInteger (std::round (65536 .*job.difficulty ));
170178 std::array<uint8_t , 10 > random;
171179 for (auto &byte : random) byte = rand (0x00 , 0xFF );
@@ -176,7 +184,7 @@ bool SearchClient::getJob(Job& job, const bool) {
176184 job.target <<= 16 ;
177185 job.target += reinterpret_cast <uint16_t *>(random.data ())[4 - i];
178186 }
179- job.target <<= (job.difficulty - 97 );
187+ job.target <<= (job.difficulty - 96 );
180188 job.primeCountTarget = _pattern.size ();
181189 job.primeCountMin = job.primeCountTarget ;
182190 return true ;
0 commit comments