| 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 <cstddef> |
| 32 | #include <fstream> |
| 33 | #include <iomanip> |
| 34 | #include <ios> |
| 35 | #include <string> |
| 36 | #include <cfenv> |
| 37 | |
| 38 | #include "misc_log_ex.h" |
| 39 | #include "warnings.h" |
| 40 | #include "crypto/hash.h" |
| 41 | #include "crypto/variant2_int_sqrt.h" |
| 42 | #include "../io.h" |
| 43 | |
| 44 | using namespace std; |
| 45 | using namespace crypto; |
| 46 | typedef crypto::hash chash; |
| 47 | |
| 48 | struct V4_Data |
| 49 | { |
| 50 | const void* data; |
| 51 | size_t length; |
| 52 | uint64_t height; |
| 53 | }; |
| 54 | |
| 55 | PUSH_WARNINGS |
| 56 | DISABLE_VS_WARNINGS(4297) |
| 57 | extern "C" { |
| 58 | static void hash_tree(const void *data, size_t length, char *hash) { |
| 59 | if ((length & 31) != 0) { |
| 60 | throw ios_base::failure("Invalid input length for tree_hash"); |
| 61 | } |
| 62 | tree_hash((const char (*)[crypto::HASH_SIZE]) data, length >> 5, hash); |
| 63 | } |
| 64 | static void cn_slow_hash_0(const void *data, size_t length, char *hash) { |
| 65 | return cn_slow_hash(data, length, hash, 0/*variant*/, 0/*prehashed*/, 0/*height*/); |
| 66 | } |
| 67 | static void cn_slow_hash_1(const void *data, size_t length, char *hash) { |
| 68 | return cn_slow_hash(data, length, hash, 1/*variant*/, 0/*prehashed*/, 0/*height*/); |
| 69 | } |
| 70 | static void cn_slow_hash_2(const void *data, size_t length, char *hash) { |
| 71 | return cn_slow_hash(data, length, hash, 2/*variant*/, 0/*prehashed*/, 0/*height*/); |
| 72 | } |
| 73 | static void cn_slow_hash_4(const void *data, size_t, char *hash) { |
| 74 | const V4_Data* p = reinterpret_cast<const V4_Data*>(data); |
| 75 | return cn_slow_hash(p->data, p->length, hash, 4/*variant*/, 0/*prehashed*/, p->height); |
| 76 | } |
| 77 | } |
| 78 | POP_WARNINGS |
| 79 | |
| 80 | extern "C" typedef void hash_f(const void *, size_t, char *); |
| 81 | struct hash_func { |
| 82 | const string name; |
| 83 | hash_f &f; |
| 84 | } hashes[] = {{"fast", cn_fast_hash}, {"slow", cn_slow_hash_0}, {"tree", hash_tree}, |
| 85 | {"extra-blake", hash_extra_blake}, {"extra-groestl", hash_extra_groestl}, |
| 86 | {"extra-jh", hash_extra_jh}, {"extra-skein", hash_extra_skein}, |
| 87 | {"slow-1", cn_slow_hash_1}, {"slow-2", cn_slow_hash_2}, {"slow-4", cn_slow_hash_4}}; |
| 88 | |
| 89 | int test_variant2_int_sqrt(); |
| 90 | int test_variant2_int_sqrt_ref(); |
| 91 | |
| 92 | int main(int argc, char *argv[]) { |
| 93 | TRY_ENTRY(); |
| 94 | |
| 95 | hash_f *f; |
| 96 | hash_func *hf; |
| 97 | fstream input; |
| 98 | vector<char> data; |
| 99 | chash expected, actual; |
| 100 | size_t test = 0; |
| 101 | bool error = false; |
| 102 | if (argc != 3) { |
| 103 | if ((argc == 2) && (strcmp(argv[1], "variant2_int_sqrt") == 0)) { |
| 104 | if (test_variant2_int_sqrt_ref() != 0) { |
| 105 | return 1; |
| 106 | } |
| 107 | const int round_modes[3] = { FE_DOWNWARD, FE_TONEAREST, FE_UPWARD }; |
| 108 | for (int i = 0; i < 3; ++i) { |
| 109 | std::fesetround(round_modes[i]); |
| 110 | const int result = test_variant2_int_sqrt(); |
| 111 | if (result != 0) { |
| 112 | cerr << "FPU round mode was set to "; |
| 113 | switch (round_modes[i]) { |
| 114 | case FE_DOWNWARD: |
| 115 | cerr << "FE_DOWNWARD"; |
| 116 | break; |
| 117 | case FE_TONEAREST: |
| 118 | cerr << "FE_TONEAREST"; |
| 119 | break; |
| 120 | case FE_UPWARD: |
| 121 | cerr << "FE_UPWARD"; |
| 122 | break; |
| 123 | default: |
| 124 | cerr << "unknown"; |
| 125 | break; |
| 126 | } |
| 127 | cerr << endl; |
| 128 | return result; |
| 129 | } |
| 130 | } |
| 131 | return 0; |
| 132 | } |
| 133 | cerr << "Wrong number of arguments" << endl; |
| 134 | return 1; |
| 135 | } |
| 136 | for (hf = hashes;; hf++) { |
| 137 | if (hf >= &hashes[sizeof(hashes) / sizeof(hash_func)]) { |
| 138 | cerr << "Unknown function" << endl; |
| 139 | return 1; |
| 140 | } |
| 141 | if (argv[1] == hf->name) { |
| 142 | f = &hf->f; |
| 143 | break; |
| 144 | } |
| 145 | } |
| 146 | input.open(argv[2], ios_base::in); |
| 147 | for (;;) { |
| 148 | ++test; |
| 149 | input.exceptions(ios_base::badbit); |
| 150 | get(input, expected); |
| 151 | if (input.rdstate() & ios_base::eofbit) { |
| 152 | break; |
| 153 | } |
| 154 | input.exceptions(ios_base::badbit | ios_base::failbit | ios_base::eofbit); |
| 155 | input.clear(input.rdstate()); |
| 156 | get(input, data); |
| 157 | if (f == cn_slow_hash_4) { |
| 158 | V4_Data d; |
| 159 | d.data = data.data(); |
| 160 | d.length = data.size(); |
| 161 | get(input, d.height); |
| 162 | f(&d, 0, (char *) &actual); |
| 163 | } else { |
| 164 | f(data.data(), data.size(), (char *) &actual); |
| 165 | } |
| 166 | if (expected != actual) { |
| 167 | size_t i; |
| 168 | cerr << "Hash mismatch on test " << test << endl << "Input: "; |
| 169 | if (data.size() == 0) { |
| 170 | cerr << "empty"; |
| 171 | } else { |
| 172 | for (i = 0; i < data.size(); i++) { |
| 173 | cerr << setbase(16) << setw(2) << setfill('0') << int(static_cast<unsigned char>(data[i])); |
| 174 | } |
| 175 | } |
| 176 | cerr << endl << "Expected hash: "; |
| 177 | for (i = 0; i < 32; i++) { |
| 178 | cerr << setbase(16) << setw(2) << setfill('0') << int(reinterpret_cast<unsigned char *>(&expected)[i]); |
| 179 | } |
| 180 | cerr << endl << "Actual hash: "; |
| 181 | for (i = 0; i < 32; i++) { |
| 182 | cerr << setbase(16) << setw(2) << setfill('0') << int(reinterpret_cast<unsigned char *>(&actual)[i]); |
| 183 | } |
| 184 | cerr << endl; |
| 185 | error = true; |
| 186 | } |
| 187 | } |
| 188 | return error ? 1 : 0; |
| 189 | CATCH_ENTRY_L0("main", 1); |
| 190 | } |
| 191 | |
| 192 | #if defined(__x86_64__) || (defined(_MSC_VER) && defined(_WIN64)) |
| 193 | |
| 194 | #include <emmintrin.h> |
| 195 | |
| 196 | #if defined(_MSC_VER) || defined(__MINGW32__) |
| 197 | #include <intrin.h> |
| 198 | #else |
| 199 | #include <wmmintrin.h> |
| 200 | #endif |
| 201 | |
| 202 | #endif |
| 203 | |
| 204 | static inline bool test_variant2_int_sqrt_sse(const uint64_t sqrt_input, const uint64_t correct_result) |
| 205 | { |
| 206 | #if defined(__x86_64__) || (defined(_MSC_VER) && defined(_WIN64)) |
| 207 | uint64_t sqrt_result; |
| 208 | VARIANT2_INTEGER_MATH_SQRT_STEP_SSE2(); |
| 209 | VARIANT2_INTEGER_MATH_SQRT_FIXUP(sqrt_result); |
| 210 | if (sqrt_result != correct_result) { |
| 211 | cerr << "Integer sqrt (SSE2 version) returned incorrect result for N = " << sqrt_input << endl; |
| 212 | cerr << "Expected result: " << correct_result << endl; |
| 213 | cerr << "Returned result: " << sqrt_result << endl; |
| 214 | return false; |
| 215 | } |
| 216 | #endif |
| 217 | |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | static inline bool test_variant2_int_sqrt_fp64(const uint64_t sqrt_input, const uint64_t correct_result) |
| 222 | { |
| 223 | #if defined DBL_MANT_DIG && (DBL_MANT_DIG >= 50) |
| 224 | uint64_t sqrt_result; |
| 225 | VARIANT2_INTEGER_MATH_SQRT_STEP_FP64(); |
| 226 | VARIANT2_INTEGER_MATH_SQRT_FIXUP(sqrt_result); |
| 227 | if (sqrt_result != correct_result) { |
| 228 | cerr << "Integer sqrt (FP64 version) returned incorrect result for N = " << sqrt_input << endl; |
| 229 | cerr << "Expected result: " << correct_result << endl; |
| 230 | cerr << "Returned result: " << sqrt_result << endl; |
| 231 | return false; |
| 232 | } |
| 233 | #endif |
| 234 | |
| 235 | return true; |
| 236 | } |
| 237 | |
| 238 | static inline bool test_variant2_int_sqrt_ref(const uint64_t sqrt_input, const uint64_t correct_result) |
| 239 | { |
| 240 | uint64_t sqrt_result; |
| 241 | VARIANT2_INTEGER_MATH_SQRT_STEP_REF(); |
| 242 | if (sqrt_result != correct_result) { |
| 243 | cerr << "Integer sqrt (reference version) returned incorrect result for N = " << sqrt_input << endl; |
| 244 | cerr << "Expected result: " << correct_result << endl; |
| 245 | cerr << "Returned result: " << sqrt_result << endl; |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | return true; |
| 250 | } |
| 251 | |
| 252 | static inline bool test_variant2_int_sqrt(const uint64_t sqrt_input, const uint64_t correct_result) |
| 253 | { |
| 254 | if (!test_variant2_int_sqrt_sse(sqrt_input, correct_result)) { |
| 255 | return false; |
| 256 | } |
| 257 | if (!test_variant2_int_sqrt_fp64(sqrt_input, correct_result)) { |
| 258 | return false; |
| 259 | } |
| 260 | |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | int test_variant2_int_sqrt() |
| 265 | { |
| 266 | if (!test_variant2_int_sqrt(0, 0)) { |
| 267 | return 1; |
| 268 | } |
| 269 | if (!test_variant2_int_sqrt(1ULL << 63, 1930543745UL)) { |
| 270 | return 1; |
| 271 | } |
| 272 | if (!test_variant2_int_sqrt(uint64_t(-1), 3558067407UL)) { |
| 273 | return 1; |
| 274 | } |
| 275 | |
| 276 | for (uint64_t i = 1; i <= 3558067407UL; ++i) { |
| 277 | // "i" is integer part of "sqrt(2^64 + n) * 2 - 2^33" |
| 278 | // n = (i/2 + 2^32)^2 - 2^64 |
| 279 | |
| 280 | const uint64_t i0 = i >> 1; |
| 281 | uint64_t n1; |
| 282 | if ((i & 1) == 0) { |
| 283 | // n = (i/2 + 2^32)^2 - 2^64 |
| 284 | // n = i^2/4 + 2*2^32*i/2 + 2^64 - 2^64 |
| 285 | // n = i^2/4 + 2^32*i |
| 286 | // i is even, so i^2 is divisible by 4: |
| 287 | // n = (i^2 >> 2) + (i << 32) |
| 288 | |
| 289 | // int_sqrt_v2(i^2/4 + 2^32*i - 1) must be equal to i - 1 |
| 290 | // int_sqrt_v2(i^2/4 + 2^32*i) must be equal to i |
| 291 | n1 = i0 * i0 + (i << 32) - 1; |
| 292 | } |
| 293 | else { |
| 294 | // n = (i/2 + 2^32)^2 - 2^64 |
| 295 | // n = i^2/4 + 2*2^32*i/2 + 2^64 - 2^64 |
| 296 | // n = i^2/4 + 2^32*i |
| 297 | // i is odd, so i = i0*2+1 (i0 = i >> 1) |
| 298 | // n = (i0*2+1)^2/4 + 2^32*i |
| 299 | // n = (i0^2*4+i0*4+1)/4 + 2^32*i |
| 300 | // n = i0^2+i0+1/4 + 2^32*i |
| 301 | // i0^2+i0 + 2^32*i < n < i0^2+i0+1 + 2^32*i |
| 302 | |
| 303 | // int_sqrt_v2(i0^2+i0 + 2^32*i) must be equal to i - 1 |
| 304 | // int_sqrt_v2(i0^2+i0+1 + 2^32*i) must be equal to i |
| 305 | n1 = i0 * i0 + i0 + (i << 32); |
| 306 | } |
| 307 | |
| 308 | if (!test_variant2_int_sqrt(n1, i - 1)) { |
| 309 | return 1; |
| 310 | } |
| 311 | if (!test_variant2_int_sqrt(n1 + 1, i)) { |
| 312 | return 1; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | int test_variant2_int_sqrt_ref() |
| 320 | { |
| 321 | if (!test_variant2_int_sqrt_ref(0, 0)) { |
| 322 | return 1; |
| 323 | } |
| 324 | if (!test_variant2_int_sqrt_ref(1ULL << 63, 1930543745UL)) { |
| 325 | return 1; |
| 326 | } |
| 327 | if (!test_variant2_int_sqrt_ref(uint64_t(-1), 3558067407UL)) { |
| 328 | return 1; |
| 329 | } |
| 330 | |
| 331 | // Reference version is slow, so we test only every 83th edge case |
| 332 | // "i += 83" because 1 + 83 * 42868282 = 3558067407 |
| 333 | for (uint64_t i = 1; i <= 3558067407UL; i += 83) { |
| 334 | const uint64_t i0 = i >> 1; |
| 335 | uint64_t n1; |
| 336 | if ((i & 1) == 0) { |
| 337 | n1 = i0 * i0 + (i << 32) - 1; |
| 338 | } |
| 339 | else { |
| 340 | n1 = i0 * i0 + i0 + (i << 32); |
| 341 | } |
| 342 | |
| 343 | if (!test_variant2_int_sqrt_ref(n1, i - 1)) { |
| 344 | return 1; |
| 345 | } |
| 346 | if (!test_variant2_int_sqrt_ref(n1 + 1, i)) { |
| 347 | return 1; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | return 0; |
| 352 | } |