| 1 | // Copyright (c) 2014-2022, The Monero Project |
| 2 | // |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers |
| 30 | |
| 31 | #include <algorithm> |
| 32 | #include <cassert> |
| 33 | #include <cstddef> |
| 34 | #include <cstdint> |
| 35 | #include <vector> |
| 36 | |
| 37 | #include "int-util.h" |
| 38 | #include "crypto/hash.h" |
| 39 | #include "cryptonote_config.h" |
| 40 | #include "difficulty.h" |
| 41 | |
| 42 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 43 | #define MONERO_DEFAULT_LOG_CATEGORY "difficulty" |
| 44 | |
| 45 | namespace cryptonote { |
| 46 | |
| 47 | using std::size_t; |
| 48 | using std::uint64_t; |
| 49 | using std::vector; |
| 50 | |
| 51 | #if defined(__x86_64__) |
| 52 | static inline void mul(uint64_t a, uint64_t b, uint64_t &low, uint64_t &high) { |
| 53 | low = mul128(a, b, &high); |
| 54 | } |
| 55 | |
| 56 | #else |
| 57 | |
| 58 | static inline void mul(uint64_t a, uint64_t b, uint64_t &low, uint64_t &high) { |
| 59 | // __int128 isn't part of the standard, so the previous function wasn't portable. mul128() in Windows is fine, |
| 60 | // but this portable function should be used elsewhere. Credit for this function goes to latexi95. |
| 61 | |
| 62 | uint64_t aLow = a & 0xFFFFFFFF; |
| 63 | uint64_t aHigh = a >> 32; |
| 64 | uint64_t bLow = b & 0xFFFFFFFF; |
| 65 | uint64_t bHigh = b >> 32; |
| 66 | |
| 67 | uint64_t res = aLow * bLow; |
| 68 | uint64_t lowRes1 = res & 0xFFFFFFFF; |
| 69 | uint64_t carry = res >> 32; |
| 70 | |
| 71 | res = aHigh * bLow + carry; |
| 72 | uint64_t highResHigh1 = res >> 32; |
| 73 | uint64_t highResLow1 = res & 0xFFFFFFFF; |
| 74 | |
| 75 | res = aLow * bHigh; |
| 76 | uint64_t lowRes2 = res & 0xFFFFFFFF; |
| 77 | carry = res >> 32; |
| 78 | |
| 79 | res = aHigh * bHigh + carry; |
| 80 | uint64_t highResHigh2 = res >> 32; |
| 81 | uint64_t highResLow2 = res & 0xFFFFFFFF; |
| 82 | |
| 83 | //Addition |
| 84 | |
| 85 | uint64_t r = highResLow1 + lowRes2; |
| 86 | carry = r >> 32; |
| 87 | low = (r << 32) | lowRes1; |
| 88 | r = highResHigh1 + highResLow2 + carry; |
| 89 | uint64_t d3 = r & 0xFFFFFFFF; |
| 90 | carry = r >> 32; |
| 91 | r = highResHigh2 + carry; |
| 92 | high = d3 | (r << 32); |
| 93 | } |
| 94 | |
| 95 | #endif |
| 96 | |
| 97 | static inline bool cadd(uint64_t a, uint64_t b) { |
| 98 | return a + b < a; |
| 99 | } |
| 100 | |
| 101 | static inline bool cadc(uint64_t a, uint64_t b, bool c) { |
| 102 | return a + b < a || (c && a + b == (uint64_t) -1); |
| 103 | } |
| 104 | |
| 105 | bool check_hash_64(const crypto::hash &hash, uint64_t difficulty) { |
| 106 | uint64_t low, high, top, cur; |
| 107 | // First check the highest word, this will most likely fail for a random hash. |
| 108 | mul(swap64le(((const uint64_t *) &hash)[3]), difficulty, top, high); |
| 109 | if (high != 0) { |
| 110 | return false; |
| 111 | } |
| 112 | mul(swap64le(((const uint64_t *) &hash)[0]), difficulty, low, cur); |
| 113 | mul(swap64le(((const uint64_t *) &hash)[1]), difficulty, low, high); |
| 114 | bool carry = cadd(cur, low); |
| 115 | cur = high; |
| 116 | mul(swap64le(((const uint64_t *) &hash)[2]), difficulty, low, high); |
| 117 | carry = cadc(cur, low, carry); |
| 118 | carry = cadc(high, top, carry); |
| 119 | return !carry; |
| 120 | } |
| 121 | |
| 122 | uint64_t next_difficulty_64(std::vector<std::uint64_t> timestamps, std::vector<uint64_t> cumulative_difficulties, size_t target_seconds) { |
| 123 | |
| 124 | if(timestamps.size() > DIFFICULTY_WINDOW) |
| 125 | { |
| 126 | timestamps.resize(DIFFICULTY_WINDOW); |
| 127 | cumulative_difficulties.resize(DIFFICULTY_WINDOW); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | size_t length = timestamps.size(); |
| 132 | assert(length == cumulative_difficulties.size()); |
| 133 | if (length <= 1) { |
| 134 | return 1; |
| 135 | } |
| 136 | static_assert(DIFFICULTY_WINDOW >= 2, "Window is too small"); |
| 137 | assert(length <= DIFFICULTY_WINDOW); |
| 138 | sort(timestamps.begin(), timestamps.end()); |
| 139 | size_t cut_begin, cut_end; |
| 140 | static_assert(2 * DIFFICULTY_CUT <= DIFFICULTY_WINDOW - 2, "Cut length is too large"); |
| 141 | if (length <= DIFFICULTY_WINDOW - 2 * DIFFICULTY_CUT) { |
| 142 | cut_begin = 0; |
| 143 | cut_end = length; |
| 144 | } else { |
| 145 | cut_begin = (length - (DIFFICULTY_WINDOW - 2 * DIFFICULTY_CUT) + 1) / 2; |
| 146 | cut_end = cut_begin + (DIFFICULTY_WINDOW - 2 * DIFFICULTY_CUT); |
| 147 | } |
| 148 | assert(/*cut_begin >= 0 &&*/ cut_begin + 2 <= cut_end && cut_end <= length); |
| 149 | uint64_t time_span = timestamps[cut_end - 1] - timestamps[cut_begin]; |
| 150 | if (time_span == 0) { |
| 151 | time_span = 1; |
| 152 | } |
| 153 | uint64_t total_work = cumulative_difficulties[cut_end - 1] - cumulative_difficulties[cut_begin]; |
| 154 | assert(total_work > 0); |
| 155 | uint64_t low, high; |
| 156 | mul(total_work, target_seconds, low, high); |
| 157 | // blockchain errors "difficulty overhead" if this function returns zero. |
| 158 | // TODO: consider throwing an exception instead |
| 159 | if (high != 0 || low + time_span - 1 < low) { |
| 160 | return 0; |
| 161 | } |
| 162 | return (low + time_span - 1) / time_span; |
| 163 | } |
| 164 | |
| 165 | #if defined(_MSC_VER) |
| 166 | #ifdef max |
| 167 | #undef max |
| 168 | #endif |
| 169 | #endif |
| 170 | |
| 171 | const difficulty_type max64bit(std::numeric_limits<std::uint64_t>::max()); |
| 172 | const boost::multiprecision::uint256_t max128bit(std::numeric_limits<boost::multiprecision::uint128_t>::max()); |
| 173 | const boost::multiprecision::uint512_t max256bit(std::numeric_limits<boost::multiprecision::uint256_t>::max()); |
| 174 | |
| 175 | #define FORCE_FULL_128_BITS |
| 176 | |
| 177 | bool check_hash_128(const crypto::hash &hash, difficulty_type difficulty) { |
| 178 | #ifndef FORCE_FULL_128_BITS |
| 179 | // fast check |
| 180 | if (difficulty >= max64bit && ((const uint64_t *) &hash)[3] > 0) |
| 181 | return false; |
| 182 | #endif |
| 183 | // usual slow check |
| 184 | boost::multiprecision::uint512_t hashVal = 0; |
| 185 | #ifdef FORCE_FULL_128_BITS |
| 186 | for(int i = 0; i < 4; i++) { // highest word is zero |
| 187 | #else |
| 188 | for(int i = 1; i < 4; i++) { // highest word is zero |
| 189 | #endif |
| 190 | hashVal <<= 64; |
| 191 | hashVal |= swap64le(((const uint64_t *) &hash)[3 - i]); |
| 192 | } |
| 193 | return hashVal * difficulty <= max256bit; |
| 194 | } |
| 195 | |
| 196 | bool check_hash(const crypto::hash &hash, difficulty_type difficulty) { |
| 197 | if (difficulty <= max64bit) // if can convert to small difficulty - do it |
| 198 | return check_hash_64(hash, difficulty.convert_to<std::uint64_t>()); |
| 199 | else |
| 200 | return check_hash_128(hash, difficulty); |
| 201 | } |
| 202 | |
| 203 | difficulty_type next_difficulty(std::vector<uint64_t> timestamps, std::vector<difficulty_type> cumulative_difficulties, size_t target_seconds) { |
| 204 | //cutoff DIFFICULTY_LAG |
| 205 | if(timestamps.size() > DIFFICULTY_WINDOW) |
| 206 | { |
| 207 | timestamps.resize(DIFFICULTY_WINDOW); |
| 208 | cumulative_difficulties.resize(DIFFICULTY_WINDOW); |
| 209 | } |
| 210 | |
| 211 | |
| 212 | size_t length = timestamps.size(); |
| 213 | assert(length == cumulative_difficulties.size()); |
| 214 | if (length <= 1) { |
| 215 | return 1; |
| 216 | } |
| 217 | static_assert(DIFFICULTY_WINDOW >= 2, "Window is too small"); |
| 218 | assert(length <= DIFFICULTY_WINDOW); |
| 219 | sort(timestamps.begin(), timestamps.end()); |
| 220 | size_t cut_begin, cut_end; |
| 221 | static_assert(2 * DIFFICULTY_CUT <= DIFFICULTY_WINDOW - 2, "Cut length is too large"); |
| 222 | if (length <= DIFFICULTY_WINDOW - 2 * DIFFICULTY_CUT) { |
| 223 | cut_begin = 0; |
| 224 | cut_end = length; |
| 225 | } else { |
| 226 | cut_begin = (length - (DIFFICULTY_WINDOW - 2 * DIFFICULTY_CUT) + 1) / 2; |
| 227 | cut_end = cut_begin + (DIFFICULTY_WINDOW - 2 * DIFFICULTY_CUT); |
| 228 | } |
| 229 | assert(/*cut_begin >= 0 &&*/ cut_begin + 2 <= cut_end && cut_end <= length); |
| 230 | uint64_t time_span = timestamps[cut_end - 1] - timestamps[cut_begin]; |
| 231 | if (time_span == 0) { |
| 232 | time_span = 1; |
| 233 | } |
| 234 | difficulty_type total_work = cumulative_difficulties[cut_end - 1] - cumulative_difficulties[cut_begin]; |
| 235 | assert(total_work > 0); |
| 236 | boost::multiprecision::uint256_t res = (boost::multiprecision::uint256_t(total_work) * target_seconds + time_span - 1) / time_span; |
| 237 | if(res > max128bit) |
| 238 | return 0; // to behave like previous implementation, may be better return max128bit? |
| 239 | return res.convert_to<difficulty_type>(); |
| 240 | } |
| 241 | |
| 242 | std::string hex(difficulty_type v) |
| 243 | { |
| 244 | static const char chars[] = "0123456789abcdef"; |
| 245 | std::string s; |
| 246 | while (v > 0) |
| 247 | { |
| 248 | s.push_back(chars[(v & 0xf).convert_to<unsigned>()]); |
| 249 | v >>= 4; |
| 250 | } |
| 251 | if (s.empty()) |
| 252 | s += "0"; |
| 253 | std::reverse(s.begin(), s.end()); |
| 254 | return "0x" + s; |
| 255 | } |
| 256 | |
| 257 | } |