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
This commit is contained in:
Sergey Kroshnin 2019-05-09 22:38:26 +03:00 committed by GitHub
commit 8ce0b7c426
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 23 additions and 43 deletions

3
.gitmodules vendored
View file

@ -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

View file

@ -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

@ -1 +0,0 @@
Subproject commit 0f2dd4a1cb103e3fc8c55c855b821eb24c6d82c3

View file

@ -42,7 +42,6 @@ add_library (nano_lib
work.cpp)
target_link_libraries (nano_lib
xxhash
ed25519
crypto_lib
blake2

View file

@ -5,8 +5,6 @@
#include <boost/endian/conversion.hpp>
#include <crypto/xxhash/xxhash.h>
/** Compare blocks, first by type, then content. This is an optimization over dynamic_cast, which is very slow on some platforms. */
namespace
{

View file

@ -1,7 +1,5 @@
#include <nano/lib/interface.h>
#include <crypto/xxhash/xxhash.h>
#include <crypto/ed25519-donna/ed25519.h>
#include <boost/property_tree/json_parser.hpp>

View file

@ -1,5 +1,6 @@
#pragma once
#include <nano/crypto_lib/random_pool.hpp>
#include <nano/lib/config.hpp>
#include <nano/lib/interface.h>
#include <nano/secure/common.hpp>
@ -8,8 +9,6 @@
#include <bitset>
#include <crypto/xxhash/xxhash.h>
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;
}

View file

@ -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)

View file

@ -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 */