From 8ce0b7c4261cf2196549394464d70f8af9fd002e Mon Sep 17 00:00:00 2001 From: Sergey Kroshnin Date: Thu, 9 May 2019 22:38:26 +0300 Subject: [PATCH] Replace xxHash with blake2b (#1919) * Replace xxHash with blake2b * Delete xxhash submodule & folder * Update random_64 initialization * Using ledger constants to generate random 128 bit union * Protect random_128 () with mutex * Don't call random_128 () twice * Use static mutex for random_128 * Update endpoint_hash_raw functions * Naming * Functions order & static random_constants --- .gitmodules | 3 --- CMakeLists.txt | 4 ---- crypto/xxhash | 1 - nano/lib/CMakeLists.txt | 1 - nano/lib/blocks.cpp | 2 -- nano/lib/interface.cpp | 2 -- nano/node/common.hpp | 51 +++++++++++++++++------------------------ nano/secure/common.cpp | 1 + nano/secure/common.hpp | 1 + 9 files changed, 23 insertions(+), 43 deletions(-) delete mode 160000 crypto/xxhash diff --git a/.gitmodules b/.gitmodules index 5b71d8f83..37ef9b39c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -11,9 +11,6 @@ [submodule "crypto/phc-winner-argon2"] path = crypto/phc-winner-argon2 url = https://github.com/nanocurrency/phc-winner-argon2.git -[submodule "crypto/xxhash"] - path = crypto/xxhash - url = https://github.com/Cyan4973/xxHash.git [submodule "gtest"] path = gtest url = https://github.com/google/googletest.git diff --git a/CMakeLists.txt b/CMakeLists.txt index c15f62b92..d5956b0de 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -264,10 +264,6 @@ target_include_directories(argon2 PUBLIC crypto/phc-winner-argon2/include) target_include_directories(argon2 PUBLIC crypto/phc-winner-argon2/src) target_include_directories(argon2 PUBLIC crypto/blake2) -add_library (xxhash - crypto/xxhash/xxhash.c - crypto/xxhash/xxhash.h) - add_library (lmdb lmdb/libraries/liblmdb/lmdb.h lmdb/libraries/liblmdb/mdb.c diff --git a/crypto/xxhash b/crypto/xxhash deleted file mode 160000 index 0f2dd4a1c..000000000 --- a/crypto/xxhash +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 0f2dd4a1cb103e3fc8c55c855b821eb24c6d82c3 diff --git a/nano/lib/CMakeLists.txt b/nano/lib/CMakeLists.txt index 8b9ed53e8..91b80e3a6 100644 --- a/nano/lib/CMakeLists.txt +++ b/nano/lib/CMakeLists.txt @@ -42,7 +42,6 @@ add_library (nano_lib work.cpp) target_link_libraries (nano_lib - xxhash ed25519 crypto_lib blake2 diff --git a/nano/lib/blocks.cpp b/nano/lib/blocks.cpp index 28eb2fa8e..e424ca94c 100644 --- a/nano/lib/blocks.cpp +++ b/nano/lib/blocks.cpp @@ -5,8 +5,6 @@ #include -#include - /** Compare blocks, first by type, then content. This is an optimization over dynamic_cast, which is very slow on some platforms. */ namespace { diff --git a/nano/lib/interface.cpp b/nano/lib/interface.cpp index 1d4864a4f..d858d1981 100644 --- a/nano/lib/interface.cpp +++ b/nano/lib/interface.cpp @@ -1,7 +1,5 @@ #include -#include - #include #include diff --git a/nano/node/common.hpp b/nano/node/common.hpp index 680e7fc3b..0163b45bf 100644 --- a/nano/node/common.hpp +++ b/nano/node/common.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -8,8 +9,6 @@ #include -#include - namespace nano { using endpoint = boost::asio::ip::udp::endpoint; @@ -22,40 +21,32 @@ bool parse_tcp_endpoint (std::string const &, nano::tcp_endpoint &); namespace { +uint64_t ip_address_hash_raw (boost::asio::ip::address const & ip_a, uint16_t port = 0) +{ + static nano::random_constants constants; + assert (ip_a.is_v6 ()); + uint64_t result; + nano::uint128_union address; + address.bytes = ip_a.to_v6 ().to_bytes (); + blake2b_state state; + blake2b_init (&state, sizeof (result)); + blake2b_update (&state, constants.random_128.bytes.data (), constants.random_128.bytes.size ()); + if (port != 0) + { + blake2b_update (&state, &port, sizeof (port)); + } + blake2b_update (&state, address.bytes.data (), address.bytes.size ()); + blake2b_final (&state, &result, sizeof (result)); + return result; +} uint64_t endpoint_hash_raw (nano::endpoint const & endpoint_a) { - assert (endpoint_a.address ().is_v6 ()); - nano::uint128_union address; - address.bytes = endpoint_a.address ().to_v6 ().to_bytes (); - XXH64_state_t * const state = XXH64_createState (); - XXH64_reset (state, 0); - XXH64_update (state, address.bytes.data (), address.bytes.size ()); - auto port (endpoint_a.port ()); - XXH64_update (state, &port, sizeof (port)); - auto result (XXH64_digest (state)); - XXH64_freeState (state); + uint64_t result (ip_address_hash_raw (endpoint_a.address (), endpoint_a.port ())); return result; } uint64_t endpoint_hash_raw (nano::tcp_endpoint const & endpoint_a) { - assert (endpoint_a.address ().is_v6 ()); - nano::uint128_union address; - address.bytes = endpoint_a.address ().to_v6 ().to_bytes (); - XXH64_state_t * const state = XXH64_createState (); - XXH64_reset (state, 0); - XXH64_update (state, address.bytes.data (), address.bytes.size ()); - auto port (endpoint_a.port ()); - XXH64_update (state, &port, sizeof (port)); - auto result (XXH64_digest (state)); - XXH64_freeState (state); - return result; -} -uint64_t ip_address_hash_raw (boost::asio::ip::address const & ip_a) -{ - assert (ip_a.is_v6 ()); - nano::uint128_union bytes; - bytes.bytes = ip_a.to_v6 ().to_bytes (); - auto result (XXH64 (bytes.bytes.data (), bytes.bytes.size (), 0)); + uint64_t result (ip_address_hash_raw (endpoint_a.address (), endpoint_a.port ())); return result; } diff --git a/nano/secure/common.cpp b/nano/secure/common.cpp index 2fb95a9b9..beb058a20 100644 --- a/nano/secure/common.cpp +++ b/nano/secure/common.cpp @@ -99,6 +99,7 @@ burn_account (0) nano::random_constants::random_constants () { nano::random_pool::generate_block (not_an_account.bytes.data (), not_an_account.bytes.size ()); + nano::random_pool::generate_block (random_128.bytes.data (), random_128.bytes.size ()); } nano::node_constants::node_constants (nano::network_constants & network_constants) diff --git a/nano/secure/common.hpp b/nano/secure/common.hpp index aabbcc6c6..b109d093c 100644 --- a/nano/secure/common.hpp +++ b/nano/secure/common.hpp @@ -339,6 +339,7 @@ class random_constants public: random_constants (); nano::account not_an_account; + nano::uint128_union random_128; }; /** Node related constants whose value depends on the active network */