diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -145,6 +145,7 @@ utilmoneystr.cpp utilstrencodings.cpp utiltime.cpp + util/bytevectorhash.cpp ) target_compile_definitions(util PUBLIC HAVE_CONFIG_H) diff --git a/src/Makefile.am b/src/Makefile.am --- a/src/Makefile.am +++ b/src/Makefile.am @@ -204,6 +204,7 @@ util.h \ utilmoneystr.h \ utiltime.h \ + util/bytevectorhash.h \ validation.h \ validationinterface.h \ versionbits.h \ @@ -452,6 +453,7 @@ utilmoneystr.cpp \ utilstrencodings.cpp \ utiltime.cpp \ + util/bytevectorhash.cpp \ $(BITCOIN_CORE_H) if GLIBC_BACK_COMPAT diff --git a/src/blockfilter.h b/src/blockfilter.h --- a/src/blockfilter.h +++ b/src/blockfilter.h @@ -9,9 +9,10 @@ #include #include #include +#include #include -#include +#include #include /** @@ -20,8 +21,8 @@ */ class GCSFilter { public: - typedef std::vector Element; - typedef std::set ElementSet; + typedef std::vector Element; + typedef std::unordered_set ElementSet; struct Params { uint64_t m_siphash_k0; diff --git a/src/util/bytevectorhash.h b/src/util/bytevectorhash.h new file mode 100644 --- /dev/null +++ b/src/util/bytevectorhash.h @@ -0,0 +1,26 @@ +// Copyright (c) 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_UTIL_BYTEVECTORHASH_H +#define BITCOIN_UTIL_BYTEVECTORHASH_H + +#include +#include + +/** + * Implementation of Hash named requirement for types that internally store a + * byte array. This may be used as the hash function in std::unordered_set or + * std::unordered_map over such types. Internally, this uses a random instance + * of SipHash-2-4. + */ +class ByteVectorHash final { +private: + uint64_t m_k0, m_k1; + +public: + ByteVectorHash(); + size_t operator()(const std::vector &input) const; +}; + +#endif // BITCOIN_UTIL_BYTEVECTORHASH_H diff --git a/src/util/bytevectorhash.cpp b/src/util/bytevectorhash.cpp new file mode 100644 --- /dev/null +++ b/src/util/bytevectorhash.cpp @@ -0,0 +1,17 @@ +// Copyright (c) 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 + +ByteVectorHash::ByteVectorHash() { + GetRandBytes(reinterpret_cast(&m_k0), sizeof(m_k0)); + GetRandBytes(reinterpret_cast(&m_k1), sizeof(m_k1)); +} + +size_t ByteVectorHash:: +operator()(const std::vector &input) const { + return CSipHasher(m_k0, m_k1).Write(input.data(), input.size()).Finalize(); +}