mirror of
https://github.com/monero-project/monero.git
synced 2025-05-02 17:24:50 -04:00
Make difficulty 128 bit instead of 64 bit
Based on Boolberry work by: jahrsg <jahr@jahr.me> cr.zoidberg <crypto.zoidberg@gmail.com>
This commit is contained in:
parent
e4b049da05
commit
91f4c7f45f
30 changed files with 787 additions and 62 deletions
|
@ -40,6 +40,7 @@
|
|||
#include <boost/archive/portable_binary_iarchive.hpp>
|
||||
#include <boost/archive/portable_binary_oarchive.hpp>
|
||||
#include "cryptonote_basic.h"
|
||||
#include "difficulty.h"
|
||||
#include "common/unordered_containers_boost_serialization.h"
|
||||
#include "crypto/crypto.h"
|
||||
#include "ringct/rctTypes.h"
|
||||
|
@ -346,6 +347,34 @@ namespace boost
|
|||
a & x.range_proof_type;
|
||||
a & x.bp_version;
|
||||
}
|
||||
|
||||
template <class Archive>
|
||||
inline void serialize(Archive &a, cryptonote::difficulty_type &x, const boost::serialization::version_type ver)
|
||||
{
|
||||
if (Archive::is_loading::value)
|
||||
{
|
||||
// load high part
|
||||
uint64_t v = 0;
|
||||
a & v;
|
||||
x = v;
|
||||
// load low part
|
||||
x = x << 64;
|
||||
a & v;
|
||||
x += v;
|
||||
}
|
||||
else
|
||||
{
|
||||
// store high part
|
||||
cryptonote::difficulty_type x_ = x >> 64;
|
||||
uint64_t v = x_.convert_to<uint64_t>();
|
||||
a & v;
|
||||
// store low part
|
||||
x_ = x << 64 >> 64;
|
||||
v = x_.convert_to<uint64_t>();
|
||||
a & v;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -102,7 +102,7 @@ namespace cryptonote {
|
|||
return a + b < a || (c && a + b == (uint64_t) -1);
|
||||
}
|
||||
|
||||
bool check_hash(const crypto::hash &hash, difficulty_type difficulty) {
|
||||
bool check_hash_64(const crypto::hash &hash, uint64_t difficulty) {
|
||||
uint64_t low, high, top, cur;
|
||||
// First check the highest word, this will most likely fail for a random hash.
|
||||
mul(swap64le(((const uint64_t *) &hash)[3]), difficulty, top, high);
|
||||
|
@ -119,7 +119,7 @@ namespace cryptonote {
|
|||
return !carry;
|
||||
}
|
||||
|
||||
difficulty_type next_difficulty(std::vector<std::uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties, size_t target_seconds) {
|
||||
uint64_t next_difficulty_64(std::vector<std::uint64_t> timestamps, std::vector<uint64_t> cumulative_difficulties, size_t target_seconds) {
|
||||
|
||||
if(timestamps.size() > DIFFICULTY_WINDOW)
|
||||
{
|
||||
|
@ -128,6 +128,87 @@ namespace cryptonote {
|
|||
}
|
||||
|
||||
|
||||
size_t length = timestamps.size();
|
||||
assert(length == cumulative_difficulties.size());
|
||||
if (length <= 1) {
|
||||
return 1;
|
||||
}
|
||||
static_assert(DIFFICULTY_WINDOW >= 2, "Window is too small");
|
||||
assert(length <= DIFFICULTY_WINDOW);
|
||||
sort(timestamps.begin(), timestamps.end());
|
||||
size_t cut_begin, cut_end;
|
||||
static_assert(2 * DIFFICULTY_CUT <= DIFFICULTY_WINDOW - 2, "Cut length is too large");
|
||||
if (length <= DIFFICULTY_WINDOW - 2 * DIFFICULTY_CUT) {
|
||||
cut_begin = 0;
|
||||
cut_end = length;
|
||||
} else {
|
||||
cut_begin = (length - (DIFFICULTY_WINDOW - 2 * DIFFICULTY_CUT) + 1) / 2;
|
||||
cut_end = cut_begin + (DIFFICULTY_WINDOW - 2 * DIFFICULTY_CUT);
|
||||
}
|
||||
assert(/*cut_begin >= 0 &&*/ cut_begin + 2 <= cut_end && cut_end <= length);
|
||||
uint64_t time_span = timestamps[cut_end - 1] - timestamps[cut_begin];
|
||||
if (time_span == 0) {
|
||||
time_span = 1;
|
||||
}
|
||||
uint64_t total_work = cumulative_difficulties[cut_end - 1] - cumulative_difficulties[cut_begin];
|
||||
assert(total_work > 0);
|
||||
uint64_t low, high;
|
||||
mul(total_work, target_seconds, low, high);
|
||||
// blockchain errors "difficulty overhead" if this function returns zero.
|
||||
// TODO: consider throwing an exception instead
|
||||
if (high != 0 || low + time_span - 1 < low) {
|
||||
return 0;
|
||||
}
|
||||
return (low + time_span - 1) / time_span;
|
||||
}
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#ifdef max
|
||||
#undef max
|
||||
#endif
|
||||
#endif
|
||||
|
||||
const difficulty_type max64bit(std::numeric_limits<std::uint64_t>::max());
|
||||
const boost::multiprecision::uint256_t max128bit(std::numeric_limits<boost::multiprecision::uint128_t>::max());
|
||||
const boost::multiprecision::uint512_t max256bit(std::numeric_limits<boost::multiprecision::uint256_t>::max());
|
||||
|
||||
#define FORCE_FULL_128_BITS
|
||||
|
||||
bool check_hash_128(const crypto::hash &hash, difficulty_type difficulty) {
|
||||
#ifndef FORCE_FULL_128_BITS
|
||||
// fast check
|
||||
if (difficulty >= max64bit && ((const uint64_t *) &hash)[3] > 0)
|
||||
return false;
|
||||
#endif
|
||||
// usual slow check
|
||||
boost::multiprecision::uint512_t hashVal = 0;
|
||||
#ifdef FORCE_FULL_128_BITS
|
||||
for(int i = 0; i < 4; i++) { // highest word is zero
|
||||
#else
|
||||
for(int i = 1; i < 4; i++) { // highest word is zero
|
||||
#endif
|
||||
hashVal <<= 64;
|
||||
hashVal |= swap64le(((const uint64_t *) &hash)[3 - i]);
|
||||
}
|
||||
return hashVal * difficulty <= max256bit;
|
||||
}
|
||||
|
||||
bool check_hash(const crypto::hash &hash, difficulty_type difficulty) {
|
||||
if (difficulty <= max64bit) // if can convert to small difficulty - do it
|
||||
return check_hash_64(hash, difficulty.convert_to<std::uint64_t>());
|
||||
else
|
||||
return check_hash_128(hash, difficulty);
|
||||
}
|
||||
|
||||
difficulty_type next_difficulty(std::vector<uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties, size_t target_seconds) {
|
||||
//cutoff DIFFICULTY_LAG
|
||||
if(timestamps.size() > DIFFICULTY_WINDOW)
|
||||
{
|
||||
timestamps.resize(DIFFICULTY_WINDOW);
|
||||
cumulative_difficulties.resize(DIFFICULTY_WINDOW);
|
||||
}
|
||||
|
||||
|
||||
size_t length = timestamps.size();
|
||||
assert(length == cumulative_difficulties.size());
|
||||
if (length <= 1) {
|
||||
|
@ -152,14 +233,10 @@ namespace cryptonote {
|
|||
}
|
||||
difficulty_type total_work = cumulative_difficulties[cut_end - 1] - cumulative_difficulties[cut_begin];
|
||||
assert(total_work > 0);
|
||||
uint64_t low, high;
|
||||
mul(total_work, target_seconds, low, high);
|
||||
// blockchain errors "difficulty overhead" if this function returns zero.
|
||||
// TODO: consider throwing an exception instead
|
||||
if (high != 0 || low + time_span - 1 < low) {
|
||||
return 0;
|
||||
}
|
||||
return (low + time_span - 1) / time_span;
|
||||
boost::multiprecision::uint256_t res = (boost::multiprecision::uint256_t(total_work) * target_seconds + time_span - 1) / time_span;
|
||||
if(res > max128bit)
|
||||
return 0; // to behave like previous implementation, may be better return max128bit?
|
||||
return res.convert_to<difficulty_type>();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -32,12 +32,13 @@
|
|||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <boost/multiprecision/cpp_int.hpp>
|
||||
|
||||
#include "crypto/hash.h"
|
||||
|
||||
namespace cryptonote
|
||||
{
|
||||
typedef std::uint64_t difficulty_type;
|
||||
typedef boost::multiprecision::uint128_t difficulty_type;
|
||||
|
||||
/**
|
||||
* @brief checks if a hash fits the given difficulty
|
||||
|
@ -51,6 +52,10 @@ namespace cryptonote
|
|||
*
|
||||
* @return true if valid, else false
|
||||
*/
|
||||
bool check_hash_64(const crypto::hash &hash, uint64_t difficulty);
|
||||
uint64_t next_difficulty_64(std::vector<std::uint64_t> timestamps, std::vector<uint64_t> cumulative_difficulties, size_t target_seconds);
|
||||
|
||||
bool check_hash_128(const crypto::hash &hash, difficulty_type difficulty);
|
||||
bool check_hash(const crypto::hash &hash, difficulty_type difficulty);
|
||||
difficulty_type next_difficulty(std::vector<std::uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties, size_t target_seconds);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue