From 327377cb2d36938ee52bab9c880bb3c11d5bfbe1 Mon Sep 17 00:00:00 2001 From: "Nikolaos D. Bougalis" Date: Wed, 28 Mar 2018 17:35:19 -0700 Subject: [PATCH] Use xxhash and remove unused hash functions: We had several hash functions implemented, including SipHash, SpookyHash and FNV1a. Default to using xxhash and remove the code for the remaining hash functions. --- src/ripple/beast/hash/fnv1a.h | 70 --- src/ripple/beast/hash/impl/siphash.cpp | 165 ------- src/ripple/beast/hash/impl/spookyv2.cpp | 361 -------------- src/ripple/beast/hash/impl/spookyv2.h | 301 ------------ src/ripple/beast/hash/siphash.h | 62 --- src/ripple/beast/hash/spooky.h | 61 --- src/ripple/beast/hash/uhash.h | 4 +- src/ripple/beast/hash/xxhasher.h | 8 - src/ripple/beast/unity/beast_hash_unity.cpp | 5 - src/test/beast/hash_append_test.cpp | 491 -------------------- src/test/beast/hash_speed_test.cpp | 120 ----- src/test/unity/beast_test_unity2.cpp | 2 - 12 files changed, 2 insertions(+), 1648 deletions(-) delete mode 100644 src/ripple/beast/hash/fnv1a.h delete mode 100644 src/ripple/beast/hash/impl/siphash.cpp delete mode 100644 src/ripple/beast/hash/impl/spookyv2.cpp delete mode 100644 src/ripple/beast/hash/impl/spookyv2.h delete mode 100644 src/ripple/beast/hash/siphash.h delete mode 100644 src/ripple/beast/hash/spooky.h delete mode 100644 src/test/beast/hash_append_test.cpp delete mode 100644 src/test/beast/hash_speed_test.cpp diff --git a/src/ripple/beast/hash/fnv1a.h b/src/ripple/beast/hash/fnv1a.h deleted file mode 100644 index 6b3f0ef87..000000000 --- a/src/ripple/beast/hash/fnv1a.h +++ /dev/null @@ -1,70 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of Beast: https://github.com/vinniefalco/Beast - Copyright 2014, Howard Hinnant , - Vinnie Falco -#include -#include - -namespace beast { - -// See http://www.isthe.com/chongo/tech/comp/fnv/ -// -class fnv1a -{ -private: - std::uint64_t state_ = 14695981039346656037ULL; - -public: - using result_type = std::size_t; - - fnv1a() = default; - - template ::value>* = nullptr> - explicit - fnv1a (Seed seed) - { - append (&seed, sizeof(seed)); - } - - void - operator() (void const* key, std::size_t len) noexcept - { - unsigned char const* p = - static_cast(key); - unsigned char const* const e = p + len; - for (; p < e; ++p) - state_ = (state_ ^ *p) * 1099511628211ULL; - } - - explicit - operator std::size_t() noexcept - { - return static_cast(state_); - } -}; - -} // beast - -#endif diff --git a/src/ripple/beast/hash/impl/siphash.cpp b/src/ripple/beast/hash/impl/siphash.cpp deleted file mode 100644 index 546ad7c1c..000000000 --- a/src/ripple/beast/hash/impl/siphash.cpp +++ /dev/null @@ -1,165 +0,0 @@ -//------------------------------- siphash.h ------------------------------------ -// -// This software is in the public domain. The only restriction on its use is -// that no one can remove it from the public domain by claiming ownership of it, -// including the original authors. -// -// There is no warranty of correctness on the software contained herein. Use -// at your own risk. -// -// Derived from: -// -// SipHash reference C implementation -// -// Written in 2012 by Jean-Philippe Aumasson -// Daniel J. Bernstein -// -// To the extent possible under law, the author(s) have dedicated all copyright -// and related and neighboring rights to this software to the public domain -// worldwide. This software is distributed without any warranty. -// -// You should have received a copy of the CC0 Public Domain Dedication along -// with this software. If not, see -// . -// -//------------------------------------------------------------------------------ - -#include -#include -#include -#include - -// namespace acme is used to demonstrate example code. It is not proposed. - -namespace beast { -namespace detail { - -using u64 = std::uint64_t; -using u32 = std::uint32_t; -using u8 = std::uint8_t; - -inline -u64 -rotl(u64 x, u64 b) -{ - return (x << b) | (x >> (64 - b)); -} - -inline -u64 -u8to64_le(const u8* p) -{ -#if BEAST_LITTLE_ENDIAN - return *static_cast(static_cast(p)); -#else - return static_cast(p[7]) << 56 | static_cast(p[6]) << 48 | - static_cast(p[5]) << 40 | static_cast(p[4]) << 32 | - static_cast(p[3]) << 24 | static_cast(p[2]) << 16 | - static_cast(p[1]) << 8 | static_cast(p[0]); -#endif -} - -inline -void -sipround(u64& v0, u64& v1, u64& v2, u64& v3) -{ - 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); -} - -} // detail - -siphash::siphash(std::uint64_t k0, std::uint64_t k1) noexcept -{ - v3_ ^= k1; - v2_ ^= k0; - v1_ ^= k1; - v0_ ^= k0; -} - -void -siphash::operator() (void const* key, std::size_t inlen) noexcept -{ - using namespace detail; - u8 const* in = static_cast(key); - total_length_ += inlen; - if (bufsize_ + inlen < 8) - { - std::copy(in, in+inlen, buf_ + bufsize_); - bufsize_ += inlen; - return; - } - if (bufsize_ > 0) - { - auto t = 8 - bufsize_; - std::copy(in, in+t, buf_ + bufsize_); - u64 m = u8to64_le( buf_ ); - v3_ ^= m; - sipround(v0_, v1_, v2_, v3_); - sipround(v0_, v1_, v2_, v3_); - v0_ ^= m; - in += t; - inlen -= t; - } - bufsize_ = inlen & 7; - u8 const* const end = in + (inlen - bufsize_); - for ( ; in != end; in += 8 ) - { - u64 m = u8to64_le( in ); - v3_ ^= m; - sipround(v0_, v1_, v2_, v3_); - sipround(v0_, v1_, v2_, v3_); - v0_ ^= m; - } - std::copy(end, end + bufsize_, buf_); -} - -siphash::operator std::size_t() noexcept -{ - using namespace detail; - std::size_t b = static_cast(total_length_) << 56; - switch(bufsize_) - { - case 7: - b |= static_cast(buf_[6]) << 48; - case 6: - b |= static_cast(buf_[5]) << 40; - case 5: - b |= static_cast(buf_[4]) << 32; - case 4: - b |= static_cast(buf_[3]) << 24; - case 3: - b |= static_cast(buf_[2]) << 16; - case 2: - b |= static_cast(buf_[1]) << 8; - case 1: - b |= static_cast(buf_[0]); - case 0: - break; - } - v3_ ^= b; - sipround(v0_, v1_, v2_, v3_); - sipround(v0_, v1_, v2_, v3_); - v0_ ^= b; - v2_ ^= 0xff; - sipround(v0_, v1_, v2_, v3_); - sipround(v0_, v1_, v2_, v3_); - sipround(v0_, v1_, v2_, v3_); - sipround(v0_, v1_, v2_, v3_); - b = v0_ ^ v1_ ^ v2_ ^ v3_; - return b; -} - -} // beast diff --git a/src/ripple/beast/hash/impl/spookyv2.cpp b/src/ripple/beast/hash/impl/spookyv2.cpp deleted file mode 100644 index e546f5d18..000000000 --- a/src/ripple/beast/hash/impl/spookyv2.cpp +++ /dev/null @@ -1,361 +0,0 @@ -// Spooky Hash -// A 128-bit noncryptographic hash, for checksums and table lookup -// By Bob Jenkins. Public domain. -// Oct 31 2010: published framework, disclaimer ShortHash isn't right -// Nov 7 2010: disabled ShortHash -// Oct 31 2011: replace End, ShortMix, ShortEnd, enable ShortHash again -// April 10 2012: buffer overflow on platforms without unaligned reads -// July 12 2012: was passing out variables in final to in/out in short -// July 30 2012: I reintroduced the buffer overflow -// August 5 2012: SpookyV2: d = should be d += in short hash, and remove extra mix from long hash - -#include -#include - -#ifdef _MSC_VER -#pragma warning (push) -#pragma warning (disable: 4127) // conditional expression is constant -#pragma warning (disable: 4244) // conversion from 'size_t' to 'uint8', possible loss of data -#endif - -#define ALLOW_UNALIGNED_READS 1 - -// -// short hash ... it could be used on any message, -// but it's used by Spooky just for short messages. -// -void SpookyHash::Short( - const void *message, - size_t length, - uint64 *hash1, - uint64 *hash2) -{ - uint64 buf[2*sc_numVars]; - union - { - const uint8 *p8; - uint32 *p32; - uint64 *p64; - size_t i; - } u; - - u.p8 = (const uint8 *)message; - - if (!ALLOW_UNALIGNED_READS && (u.i & 0x7)) - { - memcpy(buf, message, length); - u.p64 = buf; - } - - size_t remainder = length%32; - uint64 a=*hash1; - uint64 b=*hash2; - uint64 c=sc_const; - uint64 d=sc_const; - - if (length > 15) - { - const uint64 *end = u.p64 + (length/32)*4; - - // handle all complete sets of 32 bytes - for (; u.p64 < end; u.p64 += 4) - { - c += u.p64[0]; - d += u.p64[1]; - ShortMix(a,b,c,d); - a += u.p64[2]; - b += u.p64[3]; - } - - //Handle the case of 16+ remaining bytes. - if (remainder >= 16) - { - c += u.p64[0]; - d += u.p64[1]; - ShortMix(a,b,c,d); - u.p64 += 2; - remainder -= 16; - } - } - - // Handle the last 0..15 bytes, and its length - d += ((uint64)length) << 56; - switch (remainder) - { - case 15: - d += ((uint64)u.p8[14]) << 48; - case 14: - d += ((uint64)u.p8[13]) << 40; - case 13: - d += ((uint64)u.p8[12]) << 32; - case 12: - d += u.p32[2]; - c += u.p64[0]; - break; - case 11: - d += ((uint64)u.p8[10]) << 16; - case 10: - d += ((uint64)u.p8[9]) << 8; - case 9: - d += (uint64)u.p8[8]; - case 8: - c += u.p64[0]; - break; - case 7: - c += ((uint64)u.p8[6]) << 48; - case 6: - c += ((uint64)u.p8[5]) << 40; - case 5: - c += ((uint64)u.p8[4]) << 32; - case 4: - c += u.p32[0]; - break; - case 3: - c += ((uint64)u.p8[2]) << 16; - case 2: - c += ((uint64)u.p8[1]) << 8; - case 1: - c += (uint64)u.p8[0]; - break; - case 0: - c += sc_const; - d += sc_const; - } - ShortEnd(a,b,c,d); - *hash1 = a; - *hash2 = b; -} - - - - -// do the whole hash in one call -void SpookyHash::Hash128( - const void *message, - size_t length, - uint64 *hash1, - uint64 *hash2) -{ - if (length < sc_bufSize) - { - Short(message, length, hash1, hash2); - return; - } - - uint64 h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11; - uint64 buf[sc_numVars]; - uint64 *end; - union - { - const uint8 *p8; - uint64 *p64; - size_t i; - } u; - size_t remainder; - - h0=h3=h6=h9 = *hash1; - h1=h4=h7=h10 = *hash2; - h2=h5=h8=h11 = sc_const; - - u.p8 = (const uint8 *)message; - end = u.p64 + (length/sc_blockSize)*sc_numVars; - - // handle all whole sc_blockSize blocks of bytes - if (ALLOW_UNALIGNED_READS || ((u.i & 0x7) == 0)) - { - while (u.p64 < end) - { - Mix(u.p64, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11); - u.p64 += sc_numVars; - } - } - else - { - while (u.p64 < end) - { - memcpy(buf, u.p64, sc_blockSize); - Mix(buf, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11); - u.p64 += sc_numVars; - } - } - - // handle the last partial block of sc_blockSize bytes - remainder = (length - ((const uint8 *)end-(const uint8 *)message)); - memcpy(buf, end, remainder); - memset(((uint8 *)buf)+remainder, 0, sc_blockSize-remainder); - ((uint8 *)buf)[sc_blockSize-1] = - static_cast(remainder); - - // do some final mixing - End(buf, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11); - *hash1 = h0; - *hash2 = h1; -} - - - -// init spooky state -void SpookyHash::Init(uint64 seed1, uint64 seed2) -{ - m_length = 0; - m_remainder = 0; - m_state[0] = seed1; - m_state[1] = seed2; -} - - -// add a message fragment to the state -void SpookyHash::Update(const void *message, size_t length) -{ - uint64 h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11; - size_t newLength = length + m_remainder; - uint8 remainder; - union - { - const uint8 *p8; - uint64 *p64; - size_t i; - } u; - const uint64 *end; - - // Is this message fragment too short? If it is, stuff it away. - if (newLength < sc_bufSize) - { - memcpy(&((uint8 *)m_data)[m_remainder], message, length); - m_length = length + m_length; - m_remainder = (uint8)newLength; - return; - } - - // init the variables - if (m_length < sc_bufSize) - { - h0=h3=h6=h9 = m_state[0]; - h1=h4=h7=h10 = m_state[1]; - h2=h5=h8=h11 = sc_const; - } - else - { - h0 = m_state[0]; - h1 = m_state[1]; - h2 = m_state[2]; - h3 = m_state[3]; - h4 = m_state[4]; - h5 = m_state[5]; - h6 = m_state[6]; - h7 = m_state[7]; - h8 = m_state[8]; - h9 = m_state[9]; - h10 = m_state[10]; - h11 = m_state[11]; - } - m_length = length + m_length; - - // if we've got anything stuffed away, use it now - if (m_remainder) - { - uint8 prefix = sc_bufSize-m_remainder; - memcpy(&(((uint8 *)m_data)[m_remainder]), message, prefix); - u.p64 = m_data; - Mix(u.p64, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11); - Mix(&u.p64[sc_numVars], h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11); - u.p8 = ((const uint8 *)message) + prefix; - length -= prefix; - } - else - { - u.p8 = (const uint8 *)message; - } - - // handle all whole blocks of sc_blockSize bytes - end = u.p64 + (length/sc_blockSize)*sc_numVars; - remainder = (uint8)(length-((const uint8 *)end-u.p8)); - if (ALLOW_UNALIGNED_READS || (u.i & 0x7) == 0) - { - while (u.p64 < end) - { - Mix(u.p64, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11); - u.p64 += sc_numVars; - } - } - else - { - while (u.p64 < end) - { - memcpy(m_data, u.p8, sc_blockSize); - Mix(m_data, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11); - u.p64 += sc_numVars; - } - } - - // stuff away the last few bytes - m_remainder = remainder; - memcpy(m_data, end, remainder); - - // stuff away the variables - m_state[0] = h0; - m_state[1] = h1; - m_state[2] = h2; - m_state[3] = h3; - m_state[4] = h4; - m_state[5] = h5; - m_state[6] = h6; - m_state[7] = h7; - m_state[8] = h8; - m_state[9] = h9; - m_state[10] = h10; - m_state[11] = h11; -} - - -// report the hash for the concatenation of all message fragments so far -void SpookyHash::Final(uint64 *hash1, uint64 *hash2) -{ - // init the variables - if (m_length < sc_bufSize) - { - *hash1 = m_state[0]; - *hash2 = m_state[1]; - Short( m_data, m_length, hash1, hash2); - return; - } - - const uint64 *data = (const uint64 *)m_data; - uint8 remainder = m_remainder; - - uint64 h0 = m_state[0]; - uint64 h1 = m_state[1]; - uint64 h2 = m_state[2]; - uint64 h3 = m_state[3]; - uint64 h4 = m_state[4]; - uint64 h5 = m_state[5]; - uint64 h6 = m_state[6]; - uint64 h7 = m_state[7]; - uint64 h8 = m_state[8]; - uint64 h9 = m_state[9]; - uint64 h10 = m_state[10]; - uint64 h11 = m_state[11]; - - if (remainder >= sc_blockSize) - { - // m_data can contain two blocks; handle any whole first block - Mix(data, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11); - data += sc_numVars; - remainder -= sc_blockSize; - } - - // mix in the last partial block, and the length mod sc_blockSize - memset(&((uint8 *)data)[remainder], 0, (sc_blockSize-remainder)); - - ((uint8 *)data)[sc_blockSize-1] = remainder; - - // do some final mixing - End(data, h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11); - - *hash1 = h0; - *hash2 = h1; -} - -#ifdef _MSC_VER -#pragma warning (pop) -#endif diff --git a/src/ripple/beast/hash/impl/spookyv2.h b/src/ripple/beast/hash/impl/spookyv2.h deleted file mode 100644 index de198b43d..000000000 --- a/src/ripple/beast/hash/impl/spookyv2.h +++ /dev/null @@ -1,301 +0,0 @@ -// -// SpookyHash: a 128-bit noncryptographic hash function -// By Bob Jenkins, public domain -// Oct 31 2010: alpha, framework + SpookyHash::Mix appears right -// Oct 31 2011: alpha again, Mix only good to 2^^69 but rest appears right -// Dec 31 2011: beta, improved Mix, tested it for 2-bit deltas -// Feb 2 2012: production, same bits as beta -// Feb 5 2012: adjusted definitions of uint* to be more portable -// Mar 30 2012: 3 bytes/cycle, not 4. Alpha was 4 but wasn't thorough enough. -// August 5 2012: SpookyV2 (different results) -// -// Up to 3 bytes/cycle for long messages. Reasonably fast for short messages. -// All 1 or 2 bit deltas achieve avalanche within 1% bias per output bit. -// -// This was developed for and tested on 64-bit x86-compatible processors. -// It assumes the processor is little-endian. There is a macro -// controlling whether unaligned reads are allowed (by default they are). -// This should be an equally good hash on big-endian machines, but it will -// compute different results on them than on little-endian machines. -// -// Google's CityHash has similar specs to SpookyHash, and CityHash is faster -// on new Intel boxes. MD4 and MD5 also have similar specs, but they are orders -// of magnitude slower. CRCs are two or more times slower, but unlike -// SpookyHash, they have nice math for combining the CRCs of pieces to form -// the CRCs of wholes. There are also cryptographic hashes, but those are even -// slower than MD5. -// - -#ifndef BEAST_HASH_SPOOKYV2_H_INCLUDED -#define BEAST_HASH_SPOOKYV2_H_INCLUDED - -#include - -#ifdef _MSC_VER -# define INLINE __forceinline - typedef unsigned __int64 uint64; - typedef unsigned __int32 uint32; - typedef unsigned __int16 uint16; - typedef unsigned __int8 uint8; -#else -#include -# define INLINE inline - typedef uint64_t uint64; - typedef uint32_t uint32; - typedef uint16_t uint16; - typedef uint8_t uint8; -#endif - - -class SpookyHash -{ -public: - // - // SpookyHash: hash a single message in one call, produce 128-bit output - // - static void Hash128( - const void *message, // message to hash - size_t length, // length of message in bytes - uint64 *hash1, // in/out: in seed 1, out hash value 1 - uint64 *hash2); // in/out: in seed 2, out hash value 2 - - // - // Hash64: hash a single message in one call, return 64-bit output - // - static uint64 Hash64( - const void *message, // message to hash - size_t length, // length of message in bytes - uint64 seed) // seed - { - uint64 hash1 = seed; - Hash128(message, length, &hash1, &seed); - return hash1; - } - - // - // Hash32: hash a single message in one call, produce 32-bit output - // - static uint32 Hash32( - const void *message, // message to hash - size_t length, // length of message in bytes - uint32 seed) // seed - { - uint64 hash1 = seed, hash2 = seed; - Hash128(message, length, &hash1, &hash2); - return (uint32)hash1; - } - - // - // Init: initialize the context of a SpookyHash - // - void Init( - uint64 seed1, // any 64-bit value will do, including 0 - uint64 seed2); // different seeds produce independent hashes - - // - // Update: add a piece of a message to a SpookyHash state - // - void Update( - const void *message, // message fragment - size_t length); // length of message fragment in bytes - - - // - // Final: compute the hash for the current SpookyHash state - // - // This does not modify the state; you can keep updating it afterward - // - // The result is the same as if SpookyHash() had been called with - // all the pieces concatenated into one message. - // - void Final( - uint64 *hash1, // out only: first 64 bits of hash value. - uint64 *hash2); // out only: second 64 bits of hash value. - - // - // left rotate a 64-bit value by k bytes - // - static INLINE uint64 Rot64(uint64 x, int k) - { - return (x << k) | (x >> (64 - k)); - } - - // - // This is used if the input is 96 bytes long or longer. - // - // The internal state is fully overwritten every 96 bytes. - // Every input bit appears to cause at least 128 bits of entropy - // before 96 other bytes are combined, when run forward or backward - // For every input bit, - // Two inputs differing in just that input bit - // Where "differ" means xor or subtraction - // And the base value is random - // When run forward or backwards one Mix - // I tried 3 pairs of each; they all differed by at least 212 bits. - // - static INLINE void Mix( - const uint64 *data, - uint64 &s0, uint64 &s1, uint64 &s2, uint64 &s3, - uint64 &s4, uint64 &s5, uint64 &s6, uint64 &s7, - uint64 &s8, uint64 &s9, uint64 &s10,uint64 &s11) - { - s0 += data[0]; s2 ^= s10; s11 ^= s0; s0 = Rot64(s0,11); s11 += s1; - s1 += data[1]; s3 ^= s11; s0 ^= s1; s1 = Rot64(s1,32); s0 += s2; - s2 += data[2]; s4 ^= s0; s1 ^= s2; s2 = Rot64(s2,43); s1 += s3; - s3 += data[3]; s5 ^= s1; s2 ^= s3; s3 = Rot64(s3,31); s2 += s4; - s4 += data[4]; s6 ^= s2; s3 ^= s4; s4 = Rot64(s4,17); s3 += s5; - s5 += data[5]; s7 ^= s3; s4 ^= s5; s5 = Rot64(s5,28); s4 += s6; - s6 += data[6]; s8 ^= s4; s5 ^= s6; s6 = Rot64(s6,39); s5 += s7; - s7 += data[7]; s9 ^= s5; s6 ^= s7; s7 = Rot64(s7,57); s6 += s8; - s8 += data[8]; s10 ^= s6; s7 ^= s8; s8 = Rot64(s8,55); s7 += s9; - s9 += data[9]; s11 ^= s7; s8 ^= s9; s9 = Rot64(s9,54); s8 += s10; - s10 += data[10]; s0 ^= s8; s9 ^= s10; s10 = Rot64(s10,22); s9 += s11; - s11 += data[11]; s1 ^= s9; s10 ^= s11; s11 = Rot64(s11,46); s10 += s0; - } - - // - // Mix all 12 inputs together so that h0, h1 are a hash of them all. - // - // For two inputs differing in just the input bits - // Where "differ" means xor or subtraction - // And the base value is random, or a counting value starting at that bit - // The final result will have each bit of h0, h1 flip - // For every input bit, - // with probability 50 +- .3% - // For every pair of input bits, - // with probability 50 +- 3% - // - // This does not rely on the last Mix() call having already mixed some. - // Two iterations was almost good enough for a 64-bit result, but a - // 128-bit result is reported, so End() does three iterations. - // - static INLINE void EndPartial( - uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3, - uint64 &h4, uint64 &h5, uint64 &h6, uint64 &h7, - uint64 &h8, uint64 &h9, uint64 &h10,uint64 &h11) - { - h11+= h1; h2 ^= h11; h1 = Rot64(h1,44); - h0 += h2; h3 ^= h0; h2 = Rot64(h2,15); - h1 += h3; h4 ^= h1; h3 = Rot64(h3,34); - h2 += h4; h5 ^= h2; h4 = Rot64(h4,21); - h3 += h5; h6 ^= h3; h5 = Rot64(h5,38); - h4 += h6; h7 ^= h4; h6 = Rot64(h6,33); - h5 += h7; h8 ^= h5; h7 = Rot64(h7,10); - h6 += h8; h9 ^= h6; h8 = Rot64(h8,13); - h7 += h9; h10^= h7; h9 = Rot64(h9,38); - h8 += h10; h11^= h8; h10= Rot64(h10,53); - h9 += h11; h0 ^= h9; h11= Rot64(h11,42); - h10+= h0; h1 ^= h10; h0 = Rot64(h0,54); - } - - static INLINE void End( - const uint64 *data, - uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3, - uint64 &h4, uint64 &h5, uint64 &h6, uint64 &h7, - uint64 &h8, uint64 &h9, uint64 &h10,uint64 &h11) - { - h0 += data[0]; h1 += data[1]; h2 += data[2]; h3 += data[3]; - h4 += data[4]; h5 += data[5]; h6 += data[6]; h7 += data[7]; - h8 += data[8]; h9 += data[9]; h10 += data[10]; h11 += data[11]; - EndPartial(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11); - EndPartial(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11); - EndPartial(h0,h1,h2,h3,h4,h5,h6,h7,h8,h9,h10,h11); - } - - // - // The goal is for each bit of the input to expand into 128 bits of - // apparent entropy before it is fully overwritten. - // n trials both set and cleared at least m bits of h0 h1 h2 h3 - // n: 2 m: 29 - // n: 3 m: 46 - // n: 4 m: 57 - // n: 5 m: 107 - // n: 6 m: 146 - // n: 7 m: 152 - // when run forwards or backwards - // for all 1-bit and 2-bit diffs - // with diffs defined by either xor or subtraction - // with a base of all zeros plus a counter, or plus another bit, or random - // - static INLINE void ShortMix(uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3) - { - h2 = Rot64(h2,50); h2 += h3; h0 ^= h2; - h3 = Rot64(h3,52); h3 += h0; h1 ^= h3; - h0 = Rot64(h0,30); h0 += h1; h2 ^= h0; - h1 = Rot64(h1,41); h1 += h2; h3 ^= h1; - h2 = Rot64(h2,54); h2 += h3; h0 ^= h2; - h3 = Rot64(h3,48); h3 += h0; h1 ^= h3; - h0 = Rot64(h0,38); h0 += h1; h2 ^= h0; - h1 = Rot64(h1,37); h1 += h2; h3 ^= h1; - h2 = Rot64(h2,62); h2 += h3; h0 ^= h2; - h3 = Rot64(h3,34); h3 += h0; h1 ^= h3; - h0 = Rot64(h0,5); h0 += h1; h2 ^= h0; - h1 = Rot64(h1,36); h1 += h2; h3 ^= h1; - } - - // - // Mix all 4 inputs together so that h0, h1 are a hash of them all. - // - // For two inputs differing in just the input bits - // Where "differ" means xor or subtraction - // And the base value is random, or a counting value starting at that bit - // The final result will have each bit of h0, h1 flip - // For every input bit, - // with probability 50 +- .3% (it is probably better than that) - // For every pair of input bits, - // with probability 50 +- .75% (the worst case is approximately that) - // - static INLINE void ShortEnd(uint64 &h0, uint64 &h1, uint64 &h2, uint64 &h3) - { - h3 ^= h2; h2 = Rot64(h2,15); h3 += h2; - h0 ^= h3; h3 = Rot64(h3,52); h0 += h3; - h1 ^= h0; h0 = Rot64(h0,26); h1 += h0; - h2 ^= h1; h1 = Rot64(h1,51); h2 += h1; - h3 ^= h2; h2 = Rot64(h2,28); h3 += h2; - h0 ^= h3; h3 = Rot64(h3,9); h0 += h3; - h1 ^= h0; h0 = Rot64(h0,47); h1 += h0; - h2 ^= h1; h1 = Rot64(h1,54); h2 += h1; - h3 ^= h2; h2 = Rot64(h2,32); h3 += h2; - h0 ^= h3; h3 = Rot64(h3,25); h0 += h3; - h1 ^= h0; h0 = Rot64(h0,63); h1 += h0; - } - -private: - - // - // Short is used for messages under 192 bytes in length - // Short has a low startup cost, the normal mode is good for long - // keys, the cost crossover is at about 192 bytes. The two modes were - // held to the same quality bar. - // - static void Short( - const void *message, // message (array of bytes, not necessarily aligned) - size_t length, // length of message (in bytes) - uint64 *hash1, // in/out: in the seed, out the hash value - uint64 *hash2); // in/out: in the seed, out the hash value - - // number of uint64's in internal state - static const size_t sc_numVars = 12; - - // size of the internal state - static const size_t sc_blockSize = sc_numVars*8; - - // size of buffer of unhashed data, in bytes - static const size_t sc_bufSize = 2*sc_blockSize; - - // - // sc_const: a constant which: - // * is not zero - // * is odd - // * is a not-very-regular mix of 1's and 0's - // * does not need any other special mathematical properties - // - static const uint64 sc_const = 0xdeadbeefdeadbeefUL; - - uint64 m_data[2*sc_numVars]; // unhashed data, for partial messages - uint64 m_state[sc_numVars]; // internal state of the hash - size_t m_length; // total length of the input so far - uint8 m_remainder; // length of unhashed data stashed in m_data -}; - -#endif // BEAST_SPOOKYV2_H_INCLUDED diff --git a/src/ripple/beast/hash/siphash.h b/src/ripple/beast/hash/siphash.h deleted file mode 100644 index a87803d2d..000000000 --- a/src/ripple/beast/hash/siphash.h +++ /dev/null @@ -1,62 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of Beast: https://github.com/vinniefalco/Beast - Copyright 2014, Howard Hinnant , - Vinnie Falco - -#include -#include - -namespace beast { - -// See https://131002.net/siphash/ -class siphash -{ -private: - std::uint64_t v0_ = 0x736f6d6570736575ULL; - std::uint64_t v1_ = 0x646f72616e646f6dULL; - std::uint64_t v2_ = 0x6c7967656e657261ULL; - std::uint64_t v3_ = 0x7465646279746573ULL; - unsigned char buf_[8]; - unsigned bufsize_ = 0; - unsigned total_length_ = 0; - -public: - using result_type = std::size_t; - - static beast::endian const endian = beast::endian::native; - - siphash() = default; - - explicit - siphash (std::uint64_t k0, std::uint64_t k1 = 0) noexcept; - - void - operator() (void const* key, std::size_t len) noexcept; - - explicit - operator std::size_t() noexcept; -}; - -} // beast - -#endif diff --git a/src/ripple/beast/hash/spooky.h b/src/ripple/beast/hash/spooky.h deleted file mode 100644 index 29672d4ab..000000000 --- a/src/ripple/beast/hash/spooky.h +++ /dev/null @@ -1,61 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of Beast: https://github.com/vinniefalco/Beast - Copyright 2014, Howard Hinnant , - Vinnie Falco -#include - -namespace beast { - -// See http://burtleburtle.net/bob/hash/spooky.html -class spooky -{ -private: - SpookyHash state_; - -public: - using result_type = std::size_t; - static beast::endian const endian = beast::endian::native; - - spooky (std::size_t seed1 = 1, std::size_t seed2 = 2) noexcept - { - state_.Init (seed1, seed2); - } - - void - operator() (void const* key, std::size_t len) noexcept - { - state_.Update (key, len); - } - - explicit - operator std::size_t() noexcept - { - std::uint64_t h1, h2; - state_.Final (&h1, &h2); - return static_cast (h1); - } -}; - -} // beast - -#endif diff --git a/src/ripple/beast/hash/uhash.h b/src/ripple/beast/hash/uhash.h index 51be50752..206311bc5 100644 --- a/src/ripple/beast/hash/uhash.h +++ b/src/ripple/beast/hash/uhash.h @@ -22,12 +22,12 @@ #define BEAST_HASH_UHASH_H_INCLUDED #include -#include +#include namespace beast { // Universal hash function -template +template struct uhash { using result_type = typename Hasher::result_type; diff --git a/src/ripple/beast/hash/xxhasher.h b/src/ripple/beast/hash/xxhasher.h index a3161d485..12cca51cb 100644 --- a/src/ripple/beast/hash/xxhasher.h +++ b/src/ripple/beast/hash/xxhasher.h @@ -20,12 +20,6 @@ #ifndef BEAST_HASH_XXHASHER_H_INCLUDED #define BEAST_HASH_XXHASHER_H_INCLUDED -#ifndef BEAST_NO_XXHASH -#define BEAST_NO_XXHASH 0 -#endif - -#if ! BEAST_NO_XXHASH - #include #include #include @@ -84,5 +78,3 @@ public: } // beast #endif - -#endif diff --git a/src/ripple/beast/unity/beast_hash_unity.cpp b/src/ripple/beast/unity/beast_hash_unity.cpp index 117bae2ec..ac1cedb2d 100644 --- a/src/ripple/beast/unity/beast_hash_unity.cpp +++ b/src/ripple/beast/unity/beast_hash_unity.cpp @@ -18,9 +18,4 @@ //============================================================================== #include -#include - -#if ! BEAST_NO_XXHASH #include -#endif -#include diff --git a/src/test/beast/hash_append_test.cpp b/src/test/beast/hash_append_test.cpp deleted file mode 100644 index b5eff7997..000000000 --- a/src/test/beast/hash_append_test.cpp +++ /dev/null @@ -1,491 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of Beast: https://github.com/vinniefalco/Beast - Copyright 2013, Vinnie Falco - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ -//============================================================================== - -#if BEAST_INCLUDE_BEASTCONFIG -#include -#endif - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace beast { - -template -class block_stream -{ -private: - Block m_block; - std::size_t m_size; - - std::size_t - needed() const noexcept - { - return sizeof(Block) - m_size; - } - - void* - tail() noexcept - { - return ((char *)&m_block) + m_size; - } - -protected: - void - finish() - { - if (m_size > 0) - { - // zero-pad - memset (tail(), 0, needed()); - static_cast (this)->process_block (m_block); - } - } - -public: - block_stream () - : m_size(0) - { - } - - void - operator() (void const* data, std::size_t bytes) noexcept - { - // handle leftovers - if (m_size > 0) - { - std::size_t const n (std::min (needed(), bytes)); - std::memcpy (tail(), data, n); - data = ((char const*)data) + n; - bytes -= n; - m_size += n; - - if (m_size < sizeof(Block)) - return; - - static_cast (this)->process_block (m_block); - } - - // loop over complete blocks - while (bytes >= sizeof(Block)) - { - m_block = *((Block const*)data); - static_cast (this)->process_block (m_block); - data = ((char const*)data) + sizeof(m_block); - bytes -= sizeof(m_block); - } - - // save leftovers - if (bytes > 0) - { - memcpy (tail(), data, bytes); - m_size += bytes; - } - } -}; - -//------------------------------------------------------------------------------ - -namespace hash_append_tests { - -template class fnv1a_imp; - -template <> -class fnv1a_imp<64> -{ -private: - std::uint64_t state_ = 14695981039346656037u; - -public: - static beast::endian const endian = beast::endian::native; - - void - operator() (void const* key, std::size_t len) noexcept - { - unsigned char const* p = static_cast(key); - unsigned char const* const e = p + len; - for (; p < e; ++p) - state_ = (state_ ^ *p) * 1099511628211u; - } - - explicit - operator std::size_t() noexcept - { - return state_; - } -}; - -template <> -class fnv1a_imp<32> -{ -private: - std::uint32_t state_ = 2166136261; - -public: - static beast::endian const endian = beast::endian::native; - - void - operator() (void const* key, std::size_t len) noexcept - { - unsigned char const* p = static_cast(key); - unsigned char const* const e = p + len; - for (; p < e; ++p) - state_ = (state_ ^ *p) * 16777619; - } - - explicit - operator std::size_t() noexcept - { - return state_; - } -}; - -class fnv1a - : public fnv1a_imp -{ -public: -}; - -class jenkins1 -{ -private: - std::size_t state_ = 0; - -public: - static beast::endian const endian = beast::endian::native; - - void - operator() (void const* key, std::size_t len) noexcept - { - unsigned char const* p = static_cast (key); - unsigned char const* const e = p + len; - for (; p < e; ++p) - { - state_ += *p; - state_ += state_ << 10; - state_ ^= state_ >> 6; - } - } - - explicit - operator std::size_t() noexcept - { - state_ += state_ << 3; - state_ ^= state_ >> 11; - state_ += state_ << 15; - return state_; - } -}; - -class spooky -{ -private: - SpookyHash state_; - -public: - static beast::endian const endian = beast::endian::native; - - spooky(std::size_t seed1 = 1, std::size_t seed2 = 2) noexcept - { - state_.Init(seed1, seed2); - } - - void - operator()(void const* key, std::size_t len) noexcept - { - state_.Update(key, len); - } - - explicit - operator std::size_t() noexcept - { - std::uint64_t h1, h2; - state_.Final(&h1, &h2); - return h1; - } - -}; - -template < - class PRNG = std::conditional_t < - sizeof(std::size_t)==sizeof(std::uint64_t), - std::mt19937_64, - std::mt19937 - > -> -class prng_hasher - : public block_stream > -{ -private: - std::size_t m_seed; - PRNG m_prng; - - using base = block_stream >; - friend base; - - // compress - void - process_block (std::size_t block) - { - m_prng.seed (m_seed + block); - m_seed = m_prng(); - } - -public: - prng_hasher (std::size_t seed = 0) - : m_seed (seed) - { - } - - void - operator() (void const* data, std::size_t bytes) noexcept - { - base::operator() (data, bytes); - } - - explicit - operator std::size_t() noexcept - { - base::finish(); - return m_seed; - } -}; - -class SlowKey -{ -private: - std::tuple date_; - std::vector > data_; - -public: - SlowKey() - { - static beast::xor_shift_engine eng; - std::uniform_int_distribution yeardata(1900, 2014); - std::uniform_int_distribution monthdata(1, 12); - std::uniform_int_distribution daydata(1, 28); - std::uniform_int_distribution veclen(0, 100); - std::uniform_int_distribution int1data(1, 10); - std::uniform_int_distribution int2data(-3, 5000); - std::get<0>(date_) = yeardata(eng); - std::get<1>(date_) = (unsigned char)monthdata(eng); - std::get<2>(date_) = (unsigned char)daydata(eng); - data_.resize(veclen(eng)); - for (auto& p : data_) - { - p.first = int1data(eng); - p.second = int2data(eng); - } - } - - // Hook into the system like this - template - friend - void - hash_append (Hasher& h, SlowKey const& x) noexcept - { - using beast::hash_append; - hash_append (h, x.date_, x.data_); - } - - friend - bool operator< (SlowKey const& x, SlowKey const& y) noexcept - { - return std::tie(x.date_, x.data_) < std::tie(y.date_, y.data_); - } - - // Hook into the std::system like this - friend struct std::hash; - friend struct X_fnv1a; -}; - -struct FastKey -{ -private: - std::array m_values; - -public: - FastKey() - { - static beast::xor_shift_engine eng; - for (auto& v : m_values) - v = eng(); - } - - friend - bool - operator< (FastKey const& x, FastKey const& y) noexcept - { - return x.m_values < y.m_values; - } -}; - -} // hash_append_tests - -//------------------------------------------------------------------------------ - -template -struct is_contiguously_hashable - : std::integral_constant, - HashAlgorithm>::value> -{ -}; - -//------------------------------------------------------------------------------ - -class hash_append_test : public unit_test::suite -{ -public: - using SlowKey = hash_append_tests::SlowKey; - using FastKey = hash_append_tests::FastKey; - - struct results_t - { - results_t() - : collision_factor (0) - , distribution_factor (0) - , elapsed (0) - { - } - - float collision_factor; - float distribution_factor; - float windowed_score; - std::chrono::milliseconds elapsed; - }; - - // Generate a set of keys - template - std::set - make_keys (std::size_t count) - { - std::set keys; - while (count--) - keys.emplace(); - return keys; - } - - // Generate a set of hashes from a container - template - std::vector - make_hashes (Keys const& keys) - { - std::vector hashes; - hashes.reserve (keys.size()); - for (auto const& key : keys) - { - Hasher h; - hash_append (h, key); - hashes.push_back (static_cast (h)); - } - return hashes; - } - - template - void - measure_hashes (results_t& results, Hashes const& hashes) - { - results.collision_factor = - hash_metrics::collision_factor ( - hashes.begin(), hashes.end()); - - results.distribution_factor = - hash_metrics::distribution_factor ( - hashes.begin(), hashes.end()); - - results.windowed_score = - hash_metrics::windowed_score ( - hashes.begin(), hashes.end()); - } - - template - void - measure_keys (results_t& results, Keys const& keys) - { - auto const start ( - std::chrono::high_resolution_clock::now()); - - auto const hashes (make_hashes (keys)); - - results.elapsed = std::chrono::duration_cast ( - std::chrono::high_resolution_clock::now() - start); - - measure_hashes (results, hashes); - } - - template - void - test_hasher (std::string const& name, std::size_t n) - { - results_t results; - auto const keys (make_keys (n)); - measure_keys (results, keys); - report (name, results); - } - - void - report (std::string const& name, results_t const& results) - { - log << - std::left << - std::setw (39) << name << " | " << - std::right << - std::setw (13) << std::setprecision (5) << - results.collision_factor << " | " << - std::setw (13) << std::setprecision (5) << - results.distribution_factor << " | " << - std::setw (13) << std::setprecision (5) << - results.windowed_score << " | " << - std::left << - results.elapsed.count() << std::endl; - pass (); - } - - void - run() - { - log << - "name | collision | distribution | windowed | time (milliseconds)" << '\n' << - "----------------------------------------+---------------+---------------+---------------+--------------------" << std::endl; - - //test_hasher , SlowKey> ("prng_hasher ", 10000); - //test_hasher , FastKey> ("prng_hasher ", 100000); - - test_hasher ("jenkins1 ", 1000000); - test_hasher ("spooky ", 1000000); - test_hasher ("fnv1a ", 1000000); - - test_hasher ("jenkins1 ", 1000000); - test_hasher ("spooky ", 1000000); - test_hasher ("fnv1a ", 1000000); - } -}; - -BEAST_DEFINE_TESTSUITE_MANUAL(hash_append,container,beast); - -} diff --git a/src/test/beast/hash_speed_test.cpp b/src/test/beast/hash_speed_test.cpp deleted file mode 100644 index 428959240..000000000 --- a/src/test/beast/hash_speed_test.cpp +++ /dev/null @@ -1,120 +0,0 @@ -//------------------------------------------------------------------------------ -/* - This file is part of Beast: https://github.com/vinniefalco/Beast - Copyright 2013, Vinnie Falco - - Permission to use, copy, modify, and/or distribute this software for any - purpose with or without fee is hereby granted, provided that the above - copyright notice and this permission notice appear in all copies. - - THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -*/ -//============================================================================== - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace beast { - -class hash_speed_test : public beast::unit_test::suite -{ -public: - template - static - void - rngfill (void* buffer, std::size_t bytes, - Generator& g) - { - using result_type = - typename Generator::result_type; - while (bytes >= sizeof(result_type)) - { - auto const v = g(); - std::memcpy(buffer, &v, sizeof(v)); - buffer = reinterpret_cast< - std::uint8_t*>(buffer) + sizeof(v); - bytes -= sizeof(v); - } - if (bytes > 0) - { - auto const v = g(); - std::memcpy(buffer, &v, bytes); - } - } - - template > - static - void - rngfill (std::array& a, Generator& g) - { - using result_type = - typename Generator::result_type; - auto i = N / sizeof(result_type); - result_type* p = - reinterpret_cast(a.data()); - while (i--) - *p++ = g(); - } - using clock_type = - std::chrono::high_resolution_clock; - template - void - test (std::string const& what, std::size_t n) - { - using namespace std; - using namespace std::chrono; - xor_shift_engine g(1); - array key; - auto const start = clock_type::now(); - while(n--) - { - rngfill (key, g); - Hasher h; - h(key.data(), KeySize); - volatile size_t temp = - static_cast(h); - (void)temp; - } - auto const elapsed = clock_type::now() - start; - log << setw(12) << what << " " << - duration(elapsed).count() << "s" << std::endl; - } - - void - run() - { - enum - { - N = 100000000 - }; - - #if ! BEAST_NO_XXHASH - test ("xxhash", N); - #endif - test ("fnv1a", N); - test ("siphash", N); - pass(); - } -}; - -BEAST_DEFINE_TESTSUITE_MANUAL(hash_speed,container,beast); - -} // beast diff --git a/src/test/unity/beast_test_unity2.cpp b/src/test/unity/beast_test_unity2.cpp index bcf8d469a..edc1ceb26 100644 --- a/src/test/unity/beast_test_unity2.cpp +++ b/src/test/unity/beast_test_unity2.cpp @@ -21,8 +21,6 @@ #include #include #include -#include -#include #include #include #include