danicoin/src/CryptoNoteCore/Difficulty.cpp

63 lines
1.7 KiB
C++
Raw Normal View History

// Copyright (c) 2011-2016 The Cryptonote developers
2014-03-03 22:07:58 +00:00
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <vector>
2015-05-27 12:08:46 +00:00
#include "Common/int-util.h"
2014-03-03 22:07:58 +00:00
#include "crypto/hash.h"
2015-07-30 15:22:07 +00:00
#include "CryptoNoteConfig.h"
#include "Difficulty.h"
2014-03-03 22:07:58 +00:00
2015-05-27 12:08:46 +00:00
namespace CryptoNote {
2014-03-03 22:07:58 +00:00
using std::uint64_t;
using std::vector;
2015-07-30 15:22:07 +00:00
#if defined(__SIZEOF_INT128__)
2014-03-03 22:07:58 +00:00
static inline void mul(uint64_t a, uint64_t b, uint64_t &low, uint64_t &high) {
2015-07-30 15:22:07 +00:00
typedef unsigned __int128 uint128_t;
uint128_t res = (uint128_t) a * (uint128_t) b;
low = (uint64_t) res;
high = (uint64_t) (res >> 64);
2014-03-03 22:07:58 +00:00
}
#else
static inline void mul(uint64_t a, uint64_t b, uint64_t &low, uint64_t &high) {
2015-07-30 15:22:07 +00:00
low = mul128(a, b, &high);
2014-03-03 22:07:58 +00:00
}
#endif
static inline bool cadd(uint64_t a, uint64_t b) {
return a + b < a;
}
static inline bool cadc(uint64_t a, uint64_t b, bool c) {
return a + b < a || (c && a + b == (uint64_t) -1);
}
2015-07-30 15:22:07 +00:00
bool check_hash(const Crypto::Hash &hash, difficulty_type difficulty) {
2014-03-03 22:07:58 +00:00
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);
if (high != 0) {
return false;
}
mul(swap64le(((const uint64_t *) &hash)[0]), difficulty, low, cur);
mul(swap64le(((const uint64_t *) &hash)[1]), difficulty, low, high);
bool carry = cadd(cur, low);
cur = high;
mul(swap64le(((const uint64_t *) &hash)[2]), difficulty, low, high);
carry = cadc(cur, low, carry);
carry = cadc(high, top, carry);
return !carry;
}
}