forked from zone117x/node-multi-hashing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimesr.cpp
189 lines (155 loc) · 4.17 KB
/
primesr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <openssl/sha.h>
#include "bignum.h"
#include "uint1024.h"
#include "primesr.h"
#define BEGIN(a) ((char*)&(a))
#define END(a) ((char*)&((&(a))[1]))
#define UBEGIN(a) ((unsigned char*)&(a))
#define UEND(a) ((unsigned char*)&((&(a))[1]))
#define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
// This is needed because the foreach macro can't get over the comma in pair<t1, t2>
#define PAIRTYPE(t1, t2) std::pair<t1, t2>
typedef unsigned int bitsType;
typedef uint256 offsetType;
template<typename T1>
inline uint256 Hash(const T1 pbegin, const T1 pend)
{
static unsigned char pblank[1];
uint256 hash1;
SHA256((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), (unsigned char*)&hash1);
uint256 hash2;
SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
return hash2;
}
class CBlockHeader
{
public:
// header
static const int CURRENT_VERSION=2;
int nVersion;
uint256 hashPrevBlock;
uint256 hashMerkleRoot;
bitsType nBits;
int64_t nTime;
offsetType nOffset;
CBlockHeader()
{
SetNull();
}
void SetNull()
{
nVersion = CBlockHeader::CURRENT_VERSION;
hashPrevBlock = 0;
hashMerkleRoot = 0;
nTime = 0;
nBits = 0;
nOffset = 0;
}
uint256 GetHash() const;
uint256 GetHashForPoW() const;
};
class CBlock : public CBlockHeader
{
public:
CBlock()
{
SetNull();
}
CBlock(const CBlockHeader &header)
{
SetNull();
*((CBlockHeader*)this) = header;
}
CBlockHeader GetBlockHeader() const
{
CBlockHeader block;
block.nVersion = nVersion;
block.hashPrevBlock = hashPrevBlock;
block.hashMerkleRoot = hashMerkleRoot;
block.nTime = nTime;
block.nBits = nBits;
block.nOffset = nOffset;
return block;
}
};
uint256 CBlockHeader::GetHash() const
{
return Hash(BEGIN(nVersion), END(nOffset));
}
uint256 CBlockHeader::GetHashForPoW() const
{
return Hash(BEGIN(nVersion), BEGIN(nOffset));
}
unsigned int generatePrimeBase( CBigNum &bnTarget, uint256 hash, bitsType compactBits )
{
bnTarget = 1;
bnTarget <<= 8;
for ( int i = 0; i < 256; i++ )
{
bnTarget = (bnTarget << 1) + (hash.GetLow32() & 1);
hash >>= 1;
}
CBigNum nBits;
nBits.SetCompact(compactBits);
const unsigned int significativeDigits = 265;
unsigned int trailingZeros = nBits.getuint();
if( trailingZeros < significativeDigits )
return 0;
trailingZeros -= significativeDigits;
bnTarget <<= trailingZeros;
return trailingZeros;
}
int CheckProofOfWork(uint256 hash, bitsType compactBits, uint256 delta)
{
CBigNum bnTarget;
unsigned int trailingZeros = generatePrimeBase( bnTarget, hash, compactBits );
if (trailingZeros < 16 || trailingZeros > 20000){
return 0;
}
CBigNum bigDelta = CBigNum(delta);
bnTarget += bigDelta;
if( (bnTarget % 210) != 97 ){
return 0;
}
if( BN_is_prime_fasttest( &bnTarget, 1, NULL, NULL, NULL, 1) != 1 )
{
return 0;
}
int primes = 1;
bnTarget += 4;
if( BN_is_prime_fasttest( &bnTarget, 1, NULL, NULL, NULL, 1) == 1 )
{
primes += 1;
}
bnTarget += 2;
if( BN_is_prime_fasttest( &bnTarget, 1, NULL, NULL, NULL, 1) == 1 )
{
primes += 1;
}
bnTarget += 4;
if( BN_is_prime_fasttest( &bnTarget, 1, NULL, NULL, NULL, 1) == 1 )
{
primes += 1;
}
bnTarget += 2;
if( BN_is_prime_fasttest( &bnTarget, 1, NULL, NULL, NULL, 1) == 1 )
{
primes += 1;
}
bnTarget += 4;
if( BN_is_prime_fasttest( &bnTarget, 4, NULL, NULL, NULL, 1) == 1 )
{
primes += 1;
}
return primes;
}
int PrimesDifficulty(const char *input, uint32_t len)
{
std::vector<unsigned char> blockData(input, input + len);
CBlock* pblock = (CBlock*)&blockData[0];
return CheckProofOfWork( pblock->GetHashForPoW(), pblock->nBits, pblock->nOffset);
}