diff --git a/src/Makefile.am b/src/Makefile.am --- a/src/Makefile.am +++ b/src/Makefile.am @@ -328,7 +328,9 @@ crypto/sha256.cpp \ crypto/sha256.h \ crypto/sha512.cpp \ - crypto/sha512.h + crypto/sha512.h \ + crypto/siphash.cpp \ + crypto/siphash.h if USE_ASM crypto_libbitcoin_crypto_base_a_SOURCES += crypto/sha256_sse4.cpp diff --git a/src/bench/crypto_hash.cpp b/src/bench/crypto_hash.cpp --- a/src/bench/crypto_hash.cpp +++ b/src/bench/crypto_hash.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include diff --git a/src/blockencodings.cpp b/src/blockencodings.cpp --- a/src/blockencodings.cpp +++ b/src/blockencodings.cpp @@ -8,7 +8,8 @@ #include #include #include -#include +#include +#include #include #include #include diff --git a/src/blockfilter.cpp b/src/blockfilter.cpp --- a/src/blockfilter.cpp +++ b/src/blockfilter.cpp @@ -3,7 +3,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include -#include +#include #include /// SerType used to serialize parameters in GCS filter encoding. diff --git a/src/coins.h b/src/coins.h --- a/src/coins.h +++ b/src/coins.h @@ -8,7 +8,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/coins.cpp b/src/coins.cpp --- a/src/coins.cpp +++ b/src/coins.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include diff --git a/src/crypto/CMakeLists.txt b/src/crypto/CMakeLists.txt --- a/src/crypto/CMakeLists.txt +++ b/src/crypto/CMakeLists.txt @@ -1,4 +1,4 @@ -# Copyright (c) 2017 The Bitcoin developers +# Copyright (c) 2017-2019 The Bitcoin developers project(crypto) @@ -13,6 +13,7 @@ sha256.cpp sha256_sse4.cpp sha512.cpp + siphash.cpp ) target_include_directories(crypto diff --git a/src/crypto/siphash.h b/src/crypto/siphash.h new file mode 100644 --- /dev/null +++ b/src/crypto/siphash.h @@ -0,0 +1,50 @@ +// Copyright (c) 2016-2018 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_CRYPTO_SIPHASH_H +#define BITCOIN_CRYPTO_SIPHASH_H + +#include + +#include + +/** SipHash-2-4 */ +class CSipHasher { +private: + uint64_t v[4]; + uint64_t tmp; + int count; + +public: + /** Construct a SipHash calculator initialized with 128-bit key (k0, k1) */ + CSipHasher(uint64_t k0, uint64_t k1); + /** + * Hash a 64-bit integer worth of data. + * It is treated as if this was the little-endian interpretation of 8 bytes. + * This function can only be used when a multiple of 8 bytes have been + * written so far. + */ + CSipHasher &Write(uint64_t data); + /** Hash arbitrary bytes. */ + CSipHasher &Write(const uint8_t *data, size_t size); + /** Compute the 64-bit SipHash-2-4 of the data written so far. The object + * remains untouched. */ + uint64_t Finalize() const; +}; + +/** Optimized SipHash-2-4 implementation for uint256. + * + * It is identical to: + * SipHasher(k0, k1) + * .Write(val.GetUint64(0)) + * .Write(val.GetUint64(1)) + * .Write(val.GetUint64(2)) + * .Write(val.GetUint64(3)) + * .Finalize() + */ +uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val); +uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256 &val, + uint32_t extra); + +#endif // BITCOIN_CRYPTO_SIPHASH_H diff --git a/src/hash.cpp b/src/crypto/siphash.cpp copy from src/hash.cpp copy to src/crypto/siphash.cpp --- a/src/hash.cpp +++ b/src/crypto/siphash.cpp @@ -1,91 +1,8 @@ -// Copyright (c) 2013-2016 The Bitcoin Core developers +// Copyright (c) 2016-2018 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. -#include - -#include -#include -#include - -inline uint32_t ROTL32(uint32_t x, int8_t r) { - return (x << r) | (x >> (32 - r)); -} - -uint32_t MurmurHash3(uint32_t nHashSeed, - const std::vector &vDataToHash) { - // The following is MurmurHash3 (x86_32), see - // http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp - uint32_t h1 = nHashSeed; - if (vDataToHash.size() > 0) { - const uint32_t c1 = 0xcc9e2d51; - const uint32_t c2 = 0x1b873593; - - const int nblocks = vDataToHash.size() / 4; - - //---------- - // body - const uint8_t *blocks = &vDataToHash[0] + nblocks * 4; - - for (int i = -nblocks; i; i++) { - uint32_t k1 = ReadLE32(blocks + i * 4); - - k1 *= c1; - k1 = ROTL32(k1, 15); - k1 *= c2; - - h1 ^= k1; - h1 = ROTL32(h1, 13); - h1 = h1 * 5 + 0xe6546b64; - } - - //---------- - // tail - const uint8_t *tail = (const uint8_t *)(&vDataToHash[0] + nblocks * 4); - - uint32_t k1 = 0; - - switch (vDataToHash.size() & 3) { - case 3: - k1 ^= tail[2] << 16; - // FALLTHROUGH - case 2: - k1 ^= tail[1] << 8; - // FALLTHROUGH - case 1: - k1 ^= tail[0]; - k1 *= c1; - k1 = ROTL32(k1, 15); - k1 *= c2; - h1 ^= k1; - } - } - - //---------- - // finalization - h1 ^= vDataToHash.size(); - h1 ^= h1 >> 16; - h1 *= 0x85ebca6b; - h1 ^= h1 >> 13; - h1 *= 0xc2b2ae35; - h1 ^= h1 >> 16; - - return h1; -} - -void BIP32Hash(const ChainCode &chainCode, uint32_t nChild, uint8_t header, - const uint8_t data[32], uint8_t output[64]) { - uint8_t num[4]; - num[0] = (nChild >> 24) & 0xFF; - num[1] = (nChild >> 16) & 0xFF; - num[2] = (nChild >> 8) & 0xFF; - num[3] = (nChild >> 0) & 0xFF; - CHMAC_SHA512(chainCode.begin(), chainCode.size()) - .Write(&header, 1) - .Write(data, 32) - .Write(num, 4) - .Finalize(output); -} +#include #define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b)))) diff --git a/src/hash.h b/src/hash.h --- a/src/hash.h +++ b/src/hash.h @@ -213,42 +213,4 @@ void BIP32Hash(const ChainCode &chainCode, uint32_t nChild, uint8_t header, const uint8_t data[32], uint8_t output[64]); -/** SipHash-2-4 */ -class CSipHasher { -private: - uint64_t v[4]; - uint64_t tmp; - int count; - -public: - /** Construct a SipHash calculator initialized with 128-bit key (k0, k1) */ - CSipHasher(uint64_t k0, uint64_t k1); - /** - * Hash a 64-bit integer worth of data. - * It is treated as if this was the little-endian interpretation of 8 bytes. - * This function can only be used when a multiple of 8 bytes have been - * written so far. - */ - CSipHasher &Write(uint64_t data); - /** Hash arbitrary bytes. */ - CSipHasher &Write(const uint8_t *data, size_t size); - /** Compute the 64-bit SipHash-2-4 of the data written so far. The object - * remains untouched. */ - uint64_t Finalize() const; -}; - -/** Optimized SipHash-2-4 implementation for uint256. - * - * It is identical to: - * SipHasher(k0, k1) - * .Write(val.GetUint64(0)) - * .Write(val.GetUint64(1)) - * .Write(val.GetUint64(2)) - * .Write(val.GetUint64(3)) - * .Finalize() - */ -uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val); -uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256 &val, - uint32_t extra); - #endif // BITCOIN_HASH_H diff --git a/src/hash.cpp b/src/hash.cpp --- a/src/hash.cpp +++ b/src/hash.cpp @@ -86,175 +86,3 @@ .Write(num, 4) .Finalize(output); } - -#define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b)))) - -#define SIPROUND \ - do { \ - v0 += v1; \ - v1 = ROTL(v1, 13); \ - v1 ^= v0; \ - v0 = ROTL(v0, 32); \ - v2 += v3; \ - v3 = ROTL(v3, 16); \ - v3 ^= v2; \ - v0 += v3; \ - v3 = ROTL(v3, 21); \ - v3 ^= v0; \ - v2 += v1; \ - v1 = ROTL(v1, 17); \ - v1 ^= v2; \ - v2 = ROTL(v2, 32); \ - } while (0) - -CSipHasher::CSipHasher(uint64_t k0, uint64_t k1) { - v[0] = 0x736f6d6570736575ULL ^ k0; - v[1] = 0x646f72616e646f6dULL ^ k1; - v[2] = 0x6c7967656e657261ULL ^ k0; - v[3] = 0x7465646279746573ULL ^ k1; - count = 0; - tmp = 0; -} - -CSipHasher &CSipHasher::Write(uint64_t data) { - uint64_t v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3]; - - assert(count % 8 == 0); - - v3 ^= data; - SIPROUND; - SIPROUND; - v0 ^= data; - - v[0] = v0; - v[1] = v1; - v[2] = v2; - v[3] = v3; - - count += 8; - return *this; -} - -CSipHasher &CSipHasher::Write(const uint8_t *data, size_t size) { - uint64_t v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3]; - uint64_t t = tmp; - int c = count; - - while (size--) { - t |= uint64_t(*(data++)) << (8 * (c % 8)); - c++; - if ((c & 7) == 0) { - v3 ^= t; - SIPROUND; - SIPROUND; - v0 ^= t; - t = 0; - } - } - - v[0] = v0; - v[1] = v1; - v[2] = v2; - v[3] = v3; - count = c; - tmp = t; - - return *this; -} - -uint64_t CSipHasher::Finalize() const { - uint64_t v0 = v[0], v1 = v[1], v2 = v[2], v3 = v[3]; - - uint64_t t = tmp | (uint64_t(count) << 56); - - v3 ^= t; - SIPROUND; - SIPROUND; - v0 ^= t; - v2 ^= 0xFF; - SIPROUND; - SIPROUND; - SIPROUND; - SIPROUND; - return v0 ^ v1 ^ v2 ^ v3; -} - -uint64_t SipHashUint256(uint64_t k0, uint64_t k1, const uint256 &val) { - /* Specialized implementation for efficiency */ - uint64_t d = val.GetUint64(0); - - uint64_t v0 = 0x736f6d6570736575ULL ^ k0; - uint64_t v1 = 0x646f72616e646f6dULL ^ k1; - uint64_t v2 = 0x6c7967656e657261ULL ^ k0; - uint64_t v3 = 0x7465646279746573ULL ^ k1 ^ d; - - SIPROUND; - SIPROUND; - v0 ^= d; - d = val.GetUint64(1); - v3 ^= d; - SIPROUND; - SIPROUND; - v0 ^= d; - d = val.GetUint64(2); - v3 ^= d; - SIPROUND; - SIPROUND; - v0 ^= d; - d = val.GetUint64(3); - v3 ^= d; - SIPROUND; - SIPROUND; - v0 ^= d; - v3 ^= uint64_t(4) << 59; - SIPROUND; - SIPROUND; - v0 ^= uint64_t(4) << 59; - v2 ^= 0xFF; - SIPROUND; - SIPROUND; - SIPROUND; - SIPROUND; - return v0 ^ v1 ^ v2 ^ v3; -} - -uint64_t SipHashUint256Extra(uint64_t k0, uint64_t k1, const uint256 &val, - uint32_t extra) { - /* Specialized implementation for efficiency */ - uint64_t d = val.GetUint64(0); - - uint64_t v0 = 0x736f6d6570736575ULL ^ k0; - uint64_t v1 = 0x646f72616e646f6dULL ^ k1; - uint64_t v2 = 0x6c7967656e657261ULL ^ k0; - uint64_t v3 = 0x7465646279746573ULL ^ k1 ^ d; - - SIPROUND; - SIPROUND; - v0 ^= d; - d = val.GetUint64(1); - v3 ^= d; - SIPROUND; - SIPROUND; - v0 ^= d; - d = val.GetUint64(2); - v3 ^= d; - SIPROUND; - SIPROUND; - v0 ^= d; - d = val.GetUint64(3); - v3 ^= d; - SIPROUND; - SIPROUND; - v0 ^= d; - d = (uint64_t(36) << 56) | extra; - v3 ^= d; - SIPROUND; - SIPROUND; - v0 ^= d; - v2 ^= 0xFF; - SIPROUND; - SIPROUND; - SIPROUND; - SIPROUND; - return v0 ^ v1 ^ v2 ^ v3; -} diff --git a/src/net.h b/src/net.h --- a/src/net.h +++ b/src/net.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include diff --git a/src/test/hash_tests.cpp b/src/test/hash_tests.cpp --- a/src/test/hash_tests.cpp +++ b/src/test/hash_tests.cpp @@ -1,7 +1,8 @@ -// Copyright (c) 2013-2016 The Bitcoin Core developers +// Copyright (c) 2013-2018 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include #include #include diff --git a/src/txmempool.h b/src/txmempool.h --- a/src/txmempool.h +++ b/src/txmempool.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include diff --git a/src/undo.h b/src/undo.h --- a/src/undo.h +++ b/src/undo.h @@ -11,6 +11,7 @@ #include #include #include +#include class CBlock; class CBlockIndex;