forked from bitshares/bitshares-pts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
momentum.cpp
90 lines (76 loc) · 2.93 KB
/
momentum.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
#include <boost/unordered_map.hpp>
#include <iostream>
#include <openssl/sha.h>
#include "momentum.h"
namespace bts
{
#define MAX_MOMENTUM_NONCE (1<<26)
#define SEARCH_SPACE_BITS 50
#define BIRTHDAYS_PER_HASH 8
std::vector< std::pair<uint32_t,uint32_t> > momentum_search( uint256 midHash )
{
semiOrderedMap somap;
somap.allocate(4);
std::vector< std::pair<uint32_t,uint32_t> > results;
char hash_tmp[sizeof(midHash)+4];
memcpy((char*)&hash_tmp[4], (char*)&midHash, sizeof(midHash) );
uint32_t* index = (uint32_t*)hash_tmp;
for( uint32_t i = 0; i < MAX_MOMENTUM_NONCE; )
{
if(i%1048576==0){
boost::this_thread::interruption_point();
}
*index = i;
uint64_t result_hash[8];
SHA512((unsigned char*)hash_tmp, sizeof(hash_tmp), (unsigned char*)result_hash);
for( uint32_t x = 0; x < 8; ++x )
{
uint64_t birthday = result_hash[x] >> (64-SEARCH_SPACE_BITS);
uint32_t nonce = i+x;
//boost::unordered_map<uint64_t,uint32_t>::const_iterator itr = found.find( birthday );
uint64_t foundMatch=somap.checkAdd( birthday, nonce );
if( foundMatch != 0 ){
results.push_back( std::make_pair( foundMatch, nonce ) );
}
}
i += BIRTHDAYS_PER_HASH;
}
//for( auto itr = results.begin(); itr != results.end(); ++itr )
//{
// assert( momentum_verify( midHash, itr->first, itr->second ) );
// }
//somap.destroy();
return results;
}
uint64_t getBirthdayHash(const uint256& midHash, uint32_t a)
{
uint32_t index = a - (a%8);
char hash_tmp[sizeof(midHash)+4];
// std::cerr<<"midHash size:" <<sizeof(midHash)<<"\n";
memcpy(&hash_tmp[4], (char*)&midHash, sizeof(midHash) );
memcpy(&hash_tmp[0], (char*)&index, sizeof(index) );
uint64_t result_hash[8];
// for( uint32_t i = 0; i < sizeof(hash_tmp); ++i )
// {
// std::cerr<<" "<<uint16_t((((unsigned char*)hash_tmp)[i]));
// }
// std::cerr<<"\n";
SHA512((unsigned char*)hash_tmp, sizeof(hash_tmp), (unsigned char*)&result_hash);
// std::cerr<<"result_hash "<<a<<" "<<a%8<<" --- ";
// for( uint32_t i = 0; i < 8; ++i ) std::cerr<<result_hash[i]<<" ";
// std::cerr<<"\n";
uint64_t r = result_hash[a%BIRTHDAYS_PER_HASH]>>(64-SEARCH_SPACE_BITS);
// std::cerr<<"bdayresult: "<<r<<"\n";
return r;
}
bool momentum_verify( uint256 head, uint32_t a, uint32_t b ){
// std::cerr<<"verify "<<a<<" and "<<b<<" mid: "<<head.ToString()<<"\n";
// std::cerr<<" "<<getBirthdayHash(head,a)<<" "<<getBirthdayHash(head,b)<<"\n";
if( a == b ) return false;
if( a > MAX_MOMENTUM_NONCE ) return false;
if( b > MAX_MOMENTUM_NONCE ) return false;
bool r = (getBirthdayHash(head,a) == getBirthdayHash(head,b));
// std::cerr<< "####### Verified "<<int(r)<<"\n";
return r;
}
}