From 0c69c9b61b9d300c4f1f71afbc80604c6940399b Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Fri, 1 Mar 2019 18:29:39 +0100 Subject: [PATCH 01/16] Removed architecture-dependent hash64 method --- include/fc/crypto/sha256.hpp | 2 -- src/crypto/sha256.cpp | 6 ------ 2 files changed, 8 deletions(-) diff --git a/include/fc/crypto/sha256.hpp b/include/fc/crypto/sha256.hpp index bdc6737..07c5bdd 100644 --- a/include/fc/crypto/sha256.hpp +++ b/include/fc/crypto/sha256.hpp @@ -117,8 +117,6 @@ namespace raw { void to_variant( const sha256& bi, variant& v, uint32_t max_depth ); void from_variant( const variant& v, sha256& bi, uint32_t max_depth ); - uint64_t hash64(const char* buf, size_t len); - } // fc namespace std { diff --git a/src/crypto/sha256.cpp b/src/crypto/sha256.cpp index c3edd9b..d5d7975 100644 --- a/src/crypto/sha256.cpp +++ b/src/crypto/sha256.cpp @@ -205,12 +205,6 @@ namespace fc { memcpy( &bi, ve.data(), std::min(ve.size(),sizeof(bi)) ); } - uint64_t hash64(const char* buf, size_t len) - { - sha256 sha_value = sha256::hash(buf,len); - return sha_value._hash[0]; - } - template<> unsigned int hmac::internal_block_size() const { return 64; } } //end namespace fc From b75f29438b20eb9b15e74dc98854c2318d279e66 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Sat, 2 Mar 2019 14:55:30 +0100 Subject: [PATCH 02/16] Avoid endian-dependent access to hash value in key checksum test --- src/crypto/elliptic_common.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/crypto/elliptic_common.cpp b/src/crypto/elliptic_common.cpp index 1e74981..13bf256 100644 --- a/src/crypto/elliptic_common.cpp +++ b/src/crypto/elliptic_common.cpp @@ -139,11 +139,11 @@ namespace fc { namespace ecc { std::string public_key::to_base58( const public_key_data &key ) { - uint32_t check = (uint32_t)sha256::hash(key.data, sizeof(key))._hash[0]; - static_assert(sizeof(key) + sizeof(check) == 37, "Elliptic public key size (or its hash) is incorrect"); + sha256 check = sha256::hash(key.data, sizeof(key)); + static_assert(sizeof(key) + 4 == 37, "Elliptic public key size (or its hash) is incorrect"); array data; memcpy(data.data, key.begin(), key.size()); - memcpy(data.begin() + key.size(), (const char*)&check, sizeof(check)); + memcpy(data.begin() + key.size(), (const char*)check._hash, 4); return fc::to_base58(data.begin(), data.size()); } @@ -154,8 +154,8 @@ namespace fc { namespace ecc { FC_ASSERT( s == sizeof(data) ); public_key_data key; - uint32_t check = (uint32_t)sha256::hash(data.data, sizeof(key))._hash[0]; - FC_ASSERT( memcmp( (char*)&check, data.data + sizeof(key), sizeof(check) ) == 0 ); + sha256 check = sha256::hash(data.data, sizeof(key)); + FC_ASSERT( memcmp( (char*)check._hash, data.data + sizeof(key), 4 ) == 0 ); memcpy( (char*)key.data, data.data, sizeof(key) ); return from_key_data(key); } From 1daac3b4b5b567bfa74ad99e8d115b0c64334304 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Sat, 2 Mar 2019 15:01:00 +0100 Subject: [PATCH 03/16] Removed unused endian-dependent code --- include/fc/crypto/sha224.hpp | 1 - include/fc/crypto/sha256.hpp | 27 --------- src/crypto/sha256.cpp | 94 ----------------------------- tests/CMakeLists.txt | 3 - tests/crypto/log_test.cpp | 114 ----------------------------------- tests/crypto/log_test.py | 29 --------- 6 files changed, 268 deletions(-) delete mode 100644 tests/crypto/log_test.cpp delete mode 100755 tests/crypto/log_test.py diff --git a/include/fc/crypto/sha224.hpp b/include/fc/crypto/sha224.hpp index 97a36b8..490e005 100644 --- a/include/fc/crypto/sha224.hpp +++ b/include/fc/crypto/sha224.hpp @@ -64,7 +64,6 @@ class sha224 friend bool operator >= ( const sha224& h1, const sha224& h2 ); friend bool operator > ( const sha224& h1, const sha224& h2 ); friend bool operator < ( const sha224& h1, const sha224& h2 ); - friend std::size_t hash_value( const sha224& v ) { return uint64_t(v._hash[1])<<32 | v._hash[2]; } uint32_t _hash[7]; }; diff --git a/include/fc/crypto/sha256.hpp b/include/fc/crypto/sha256.hpp index 07c5bdd..353fc3a 100644 --- a/include/fc/crypto/sha256.hpp +++ b/include/fc/crypto/sha256.hpp @@ -67,33 +67,6 @@ class sha256 friend bool operator > ( const sha256& h1, const sha256& h2 ); friend bool operator < ( const sha256& h1, const sha256& h2 ); - uint32_t pop_count()const - { - return (uint32_t)(__builtin_popcountll(_hash[0]) + - __builtin_popcountll(_hash[1]) + - __builtin_popcountll(_hash[2]) + - __builtin_popcountll(_hash[3])); - } - - /** - * Count leading zero bits - */ - uint16_t clz()const; - - /** - * Approximate (log_2(x) + 1) * 2**24. - * - * Detailed specs: - * - Return 0 when x == 0. - * - High 8 bits of result simply counts nonzero bits. - * - Low 24 bits of result are the 24 bits of input immediately after the most significant 1 in the input. - * - If above would require reading beyond the end of the input, zeros are used instead. - */ - uint32_t approx_log_32()const; - - void set_to_inverse_approx_log_32( uint32_t x ); - static double inverse_approx_log_32_double( uint32_t x ); - uint64_t _hash[4]; }; diff --git a/src/crypto/sha256.cpp b/src/crypto/sha256.cpp index d5d7975..db0a18a 100644 --- a/src/crypto/sha256.cpp +++ b/src/crypto/sha256.cpp @@ -99,100 +99,6 @@ namespace fc { return memcmp( h1._hash, h2._hash, sizeof(h1._hash) ) == 0; } - uint32_t sha256::approx_log_32()const - { - uint16_t lzbits = clz(); - if( lzbits >= 0x100 ) - return 0; - uint8_t nzbits = 0xFF-lzbits; - size_t offset = (size_t) (lzbits >> 3); - uint8_t* my_bytes = (uint8_t*) data(); - size_t n = data_size(); - uint32_t y = (uint32_t( my_bytes[offset ] ) << 0x18) - | (uint32_t(offset+1 < n ? my_bytes[offset+1] : 0) << 0x10) - | (uint32_t(offset+2 < n ? my_bytes[offset+2] : 0) << 0x08) - | (uint32_t(offset+3 < n ? my_bytes[offset+3] : 0) ) - ; - // - // lzbits&7 == 7 : 00000001 iff nzbits&7 == 0 - // lzbits&7 == 6 : 0000001x iff nzbits&7 == 1 - // lzbits&7 == 5 : 000001xx iff nzbits&7 == 2 - // - y >>= (nzbits & 7); - y ^= 1 << 0x18; - y |= uint32_t( nzbits ) << 0x18; - return y; - } - - void sha256::set_to_inverse_approx_log_32( uint32_t x ) - { - uint8_t nzbits = uint8_t( x >> 0x18 ); - _hash[0] = 0; - _hash[1] = 0; - _hash[2] = 0; - _hash[3] = 0; - if( nzbits == 0 ) - return; - uint8_t x0 = uint8_t((x ) & 0xFF); - uint8_t x1 = uint8_t((x >> 0x08) & 0xFF); - uint8_t x2 = uint8_t((x >> 0x10) & 0xFF); - uint8_t* my_bytes = (uint8_t*) data(); - my_bytes[0x1F] = x0; - my_bytes[0x1E] = x1; - my_bytes[0x1D] = x2; - my_bytes[0x1C] = 1; - - if( nzbits <= 0x18 ) - { - (*this) = (*this) >> (0x18 - nzbits); - } - else - (*this) = (*this) << (nzbits - 0x18); - } - - double sha256::inverse_approx_log_32_double( uint32_t x ) - { - uint8_t nzbits = uint8_t( x >> 0x18 ); - if( nzbits == 0 ) - return 0.0; - uint32_t b = 1 << 0x18; - uint32_t y = (x & (b-1)) | b; - return std::ldexp( y, int( nzbits ) - 0x18 ); - } - - uint16_t sha256::clz()const - { - const uint8_t* my_bytes = (uint8_t*) data(); - size_t size = data_size(); - size_t lzbits = 0; - static const uint8_t char2lzbits[] = { - // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 - 8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - }; - - size_t i = 0; - - while( true ) - { - uint8_t c = my_bytes[i]; - lzbits += char2lzbits[c]; - if( c != 0 ) - break; - ++i; - if( i >= size ) - return 0x100; - } - - return lzbits; - } - void to_variant( const sha256& bi, variant& v, uint32_t max_depth ) { to_variant( std::vector( (const char*)&bi, ((const char*)&bi) + sizeof(bi) ), v, max_depth ); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 708b3d5..e968bb3 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -23,9 +23,6 @@ target_link_libraries( hmac_test fc ) add_executable( ecc_test crypto/ecc_test.cpp ) target_link_libraries( ecc_test fc ) -add_executable( log_test crypto/log_test.cpp ) -target_link_libraries( log_test fc ) - #add_executable( test_aes aes_test.cpp ) #target_link_libraries( test_aes fc ${rt_library} ${pthread_library} ) #add_executable( test_sleep sleep.cpp ) diff --git a/tests/crypto/log_test.cpp b/tests/crypto/log_test.cpp deleted file mode 100644 index 654c95c..0000000 --- a/tests/crypto/log_test.cpp +++ /dev/null @@ -1,114 +0,0 @@ - -#include - -#include -#include - -#include -#include -#include - -uint64_t endian_reverse( uint64_t x ) -{ - uint64_t x0 = ((x ) & 0xFF); - uint64_t x1 = ((x >> 0x08) & 0xFF); - uint64_t x2 = ((x >> 0x10) & 0xFF); - uint64_t x3 = ((x >> 0x18) & 0xFF); - uint64_t x4 = ((x >> 0x20) & 0xFF); - uint64_t x5 = ((x >> 0x28) & 0xFF); - uint64_t x6 = ((x >> 0x30) & 0xFF); - uint64_t x7 = ((x >> 0x38) & 0xFF); - - return (x0 << 0x38) - | (x1 << 0x30) - | (x2 << 0x28) - | (x3 << 0x20) - | (x4 << 0x18) - | (x5 << 0x10) - | (x6 << 0x08) - | (x7 ); -} - -int main(int argc, char**argv, char** envp) -{ - std::ifstream infile("log_test.txt"); - uint32_t ref_clz; - std::string str_h; - uint32_t ref_log; - uint32_t cases = 0; - uint32_t errors = 0; - - while( true ) - { - if( !(infile >> std::hex >> ref_clz) ) - break; - if( !(infile >> str_h) ) - break; - if( !(infile >> std::hex >> ref_log) ) - break; - fc::sha256 h(str_h); - if( ref_clz != h.clz() ) - { - std::cerr << "got error on clz(" << str_h << ")" << std::endl; - ++errors; - } - if( ref_log != h.approx_log_32() ) - { - std::cerr << "got error on log(" << str_h << ")" << std::endl; - ++errors; - } - double d_ilog_h_test = h.inverse_approx_log_32_double( ref_log ); - h.set_to_inverse_approx_log_32( ref_log ); - if( ref_log != h.approx_log_32() ) - { - std::cerr << "got error on ilog(" << ref_log << ")" << std::endl; - ++errors; - } - - std::string str_ilog_h = h.str(); - boost::multiprecision::uint256_t u256_ilog_h( "0x" + str_ilog_h ); - double d_ilog_h_ref = u256_ilog_h.template convert_to(); - if( d_ilog_h_ref != d_ilog_h_test ) - { - std::cerr << "got error on d_ilog(" << ref_log << ")" << std::endl; - ++errors; - } - - if( h != fc::sha256() ) - { - fc::sha256 h_before = h; - if( h._hash[3] == 0 ) - { - if( h._hash[2] == 0 ) - { - if( h._hash[1] == 0 ) - { - h._hash[0] = endian_reverse( endian_reverse( h._hash[0] )-1 ); - } - h._hash[1] = endian_reverse( endian_reverse( h._hash[1] )-1 ); - } - h._hash[2] = endian_reverse( endian_reverse( h._hash[2] )-1 ); - } - h._hash[3] = endian_reverse( endian_reverse( h._hash[3] )-1 ); - bool ok = (h.approx_log_32() < ref_log); - if( !ok ) - { - std::cerr << "got error on logm1 for " << ref_log << std::endl; - std::cerr << "h0:" << str_h << std::endl; - std::cerr << "h1:" << h_before.str() << std::endl; - std::cerr << "h2:" << h.str() << std::endl; - std::cerr << "ref_log:" << std::hex << std::setw(8) << ref_log << std::endl; - std::cerr << "log(h) :" << std::hex << std::setw(8) << h.approx_log_32() << std::endl; - std::cerr << std::endl; - ++errors; - } - } - - ++cases; - } - - std::cerr << "sha256_log_test checked " << cases << " cases, got " << errors << " errors" << std::endl; - if( errors ) - return 1; - return 0; -} diff --git a/tests/crypto/log_test.py b/tests/crypto/log_test.py deleted file mode 100755 index ec5c7fe..0000000 --- a/tests/crypto/log_test.py +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env python3 - -# Independent implementation of algorithm -# To create log_test.txt, run ./log_test.py > log_test.txt - -import random - -rand = random.Random(1234) - -result = set() - -result.add((0, 256)) -result.add(((1 << 256)-1, 0)) -for i in range(256): - y = (1 << i) - result.add((y, 255-i)) - for j in range(32): - result.add((y+rand.randrange(0, y), 255-i)) - -def get_sem_32(y): - bs = "{:0256b}".format(y) - if "1" not in bs: - return 0 - bs += 32*"0" - i = bs.index("1") - return ((255-i) << 24) | int(bs[i+1:i+25], 2) - -for y, lz in sorted(result): - print("{:02x}".format(lz), "{:064x}".format(y), "{:08x}".format(get_sem_32(y))) From 41bdac7d58450f4d31a3a768250bc3fb6ee6f1f8 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Sat, 2 Mar 2019 17:45:23 +0100 Subject: [PATCH 04/16] Removed bitutil because architecture-independent byte order reversal rarely makes sense, and boost::endian provides a better alternative --- include/fc/bitutil.hpp | 28 ---------------------------- 1 file changed, 28 deletions(-) delete mode 100644 include/fc/bitutil.hpp diff --git a/include/fc/bitutil.hpp b/include/fc/bitutil.hpp deleted file mode 100644 index 4d6c3ab..0000000 --- a/include/fc/bitutil.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once -#include - -namespace fc { - -inline uint64_t endian_reverse_u64( uint64_t x ) -{ - return (((x >> 0x38) & 0xFF) ) - | (((x >> 0x30) & 0xFF) << 0x08) - | (((x >> 0x28) & 0xFF) << 0x10) - | (((x >> 0x20) & 0xFF) << 0x18) - | (((x >> 0x18) & 0xFF) << 0x20) - | (((x >> 0x10) & 0xFF) << 0x28) - | (((x >> 0x08) & 0xFF) << 0x30) - | (((x ) & 0xFF) << 0x38) - ; -} - -inline uint32_t endian_reverse_u32( uint32_t x ) -{ - return (((x >> 0x18) & 0xFF) ) - | (((x >> 0x10) & 0xFF) << 0x08) - | (((x >> 0x08) & 0xFF) << 0x10) - | (((x ) & 0xFF) << 0x18) - ; -} - -} // namespace fc From 8f1abdd4be1d57918574170e240ddbd080c37267 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Sat, 2 Mar 2019 18:56:07 +0100 Subject: [PATCH 05/16] Delete unused backup(?) file --- include/fc/io/datastream_back.hpp | 178 ------------------------------ 1 file changed, 178 deletions(-) delete mode 100644 include/fc/io/datastream_back.hpp diff --git a/include/fc/io/datastream_back.hpp b/include/fc/io/datastream_back.hpp deleted file mode 100644 index 8c4e092..0000000 --- a/include/fc/io/datastream_back.hpp +++ /dev/null @@ -1,178 +0,0 @@ -#pragma once -#include -#include -#include - -namespace fc { - -/** - * The purpose of this datastream is to provide a fast, effecient, means - * of calculating the amount of data "about to be written" and then - * writing it. This means having two modes of operation, "test run" where - * you call the entire pack sequence calculating the size, and then - * actually packing it after doing a single allocation. - */ -template -class datastream { - public: - datastream( T start, size_t s ) - :_start(start),_pos(start),_end(start+s){}; - - - inline void skip( size_t s ){ _pos += s; } - inline bool read( char* d, size_t s ) { - if( size_t(_end - _pos) >= (size_t)s ) { - memcpy( d, _pos, s ); - _pos += s; - return true; - } - detail::throw_datastream_range_error( _end-start, int64_t(-((_end-_pos) - 1))) - return false; - } - - inline bool write( const char* d, size_t s ) { - if( _end - _pos >= (int32_t)s ) { - memcpy( _pos, d, s ); - _pos += s; - return true; - } - detail::throw_datastream_range_error( _end-start, int64_t(-((_end-_pos) - 1))) - return false; - } - - inline bool put(char c) { - if( _pos < _end ) { - *_pos = c; - ++_pos; - return true; - } - detail::throw_datastream_range_error( _end-start, int64_t(-((_end-_pos) - 1))) - } - - inline bool get( unsigned char& c ) { return get( *(char*)&c ); } - inline bool get( char& c ) { - if( _pos < _end ) { - c = *_pos; - ++_pos; - return true; - } - detail::throw_datastream_range_error( _end-start, int64_t(-((_end-_pos) - 1))) - } - - T pos()const { return _pos; } - inline bool valid()const { return _pos <= _end && _pos >= _start; } - inline bool seekp(size_t p) { _pos = _start + p; return _pos <= _end; } - inline size_t tellp()const { return _pos - _start; } - inline size_t remaining()const { return _end - _pos; } - private: - T _start; - T _pos; - T _end; -}; - -template<> -class datastream { - public: - datastream( size_t init_size = 0):_size(init_size){}; - inline bool skip( size_t s ) { _size += s; return true; } - inline bool write( const char* d,size_t s ) { _size += s; return true; } - inline bool put(char c) { ++_size; return true; } - inline bool valid()const { return true; } - inline bool seekp(size_t p) { _size = p; return true; } - inline size_t tellp()const { return _size; } - inline size_t remaining()const { return 0; } - private: - size_t _size; -}; - -template -inline datastream& operator<<(datastream& ds, const int32_t& d) { - ds.write( (const char*)&d, sizeof(d) ); - return ds; -} -template -inline datastream& operator>>(datastream& ds, int32_t& d) { - ds.read((char*)&d, sizeof(d) ); - return ds; -} -template -inline datastream& operator<<(datastream& ds, const uint32_t& d) { - ds.write( (const char*)&d, sizeof(d) ); - return ds; -} - -template -inline datastream& operator>>(datastream& ds, uint32_t& d) { - ds.read((char*)&d, sizeof(d) ); - return ds; -} -template -inline datastream& operator<<(datastream& ds, const int64_t& d) { - ds.write( (const char*)&d, sizeof(d) ); - return ds; -} - -template -inline datastream& operator>>(datastream& ds, int64_t& d) { - ds.read((char*)&d, sizeof(d) ); - return ds; -} -template -inline datastream& operator<<(datastream& ds, const uint64_t& d) { - ds.write( (const char*)&d, sizeof(d) ); - return ds; -} - -template -inline datastream& operator>>(datastream& ds, uint64_t& d) { - ds.read((char*)&d, sizeof(d) ); - return ds; -} -template -inline datastream& operator<<(datastream& ds, const int16_t& d) { - ds.write( (const char*)&d, sizeof(d) ); - return ds; -} - -template -inline datastream& operator>>(datastream& ds, int16_t& d) { - ds.read((char*)&d, sizeof(d) ); - return ds; -} -template -inline datastream& operator<<(datastream& ds, const uint16_t& d) { - ds.write( (const char*)&d, sizeof(d) ); - return ds; -} - -template -inline datastream& operator>>(datastream& ds, uint16_t& d) { - ds.read((char*)&d, sizeof(d) ); - return ds; -} -template -inline datastream& operator<<(datastream& ds, const int8_t& d) { - ds.write( (const char*)&d, sizeof(d) ); - return ds; -} - -template -inline datastream& operator>>(datastream& ds, int8_t& d) { - ds.read((char*)&d, sizeof(d) ); - return ds; -} -template -inline datastream& operator<<(datastream& ds, const uint8_t& d) { - ds.write( (const char*)&d, sizeof(d) ); - return ds; -} - -template -inline datastream& operator>>(datastream& ds, uint8_t& d) { - ds.read((char*)&d, sizeof(d) ); - return ds; -} - - -} // namespace fc - From 9a0d92820d753d1b02b5cd0b58558b631ebcf7fe Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Sun, 3 Mar 2019 18:42:17 +0100 Subject: [PATCH 06/16] Fixes for endianness --- CMakeLists.txt | 1 + include/fc/bloom_filter.hpp | 5 -- include/fc/io/datastream.hpp | 89 -------------------- include/fc/io/raw.hpp | 154 ++++++++++++++++++++++++++-------- include/fc/io/raw_fwd.hpp | 4 + include/fc/io/raw_variant.hpp | 7 +- include/fc/real128.hpp | 14 +++- include/fc/uint128.hpp | 16 ++-- src/byteswap.hpp | 11 --- src/crypto/bigint.cpp | 13 +-- src/io/iostream.cpp | 61 -------------- src/io/sstream.cpp | 25 ------ src/uint128.cpp | 15 +++- tests/bloom_test.cpp | 10 +-- 14 files changed, 169 insertions(+), 256 deletions(-) delete mode 100644 src/byteswap.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1b62e3b..0acb9f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,7 @@ SET (ORIGINAL_LIB_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES}) SET(BOOST_COMPONENTS) LIST(APPEND BOOST_COMPONENTS thread date_time filesystem system program_options chrono unit_test_framework context iostreams regex) +# boost::endian is also required, but FindBoost can't handle header-only libs SET( Boost_USE_STATIC_LIBS ON CACHE STRING "ON or OFF" ) # Configure secp256k1-zkp diff --git a/include/fc/bloom_filter.hpp b/include/fc/bloom_filter.hpp index 3728d9c..a52f2da 100644 --- a/include/fc/bloom_filter.hpp +++ b/include/fc/bloom_filter.hpp @@ -617,11 +617,6 @@ inline bloom_filter operator ^ (const bloom_filter& a, const bloom_filter& b) } // namespace fc - -FC_REFLECT( fc::bloom_filter, (salt_)(bit_table_)(salt_count_)(table_size_)(raw_table_size_)(projected_element_count_)(inserted_element_count_)(random_seed_)(desired_false_positive_probability_) ) -FC_REFLECT( fc::bloom_parameters::optimal_parameters_t, (number_of_hashes)(table_size) ) -FC_REFLECT( fc::bloom_parameters, (minimum_size)(maximum_size)(minimum_number_of_hashes)(maximum_number_of_hashes)(projected_element_count)(false_positive_probability)(random_seed)(optimal_parameters) ) - /* Note 1: If it can be guaranteed that bits_per_char will be of the form 2^n then diff --git a/include/fc/io/datastream.hpp b/include/fc/io/datastream.hpp index 75a4269..0bf36a1 100644 --- a/include/fc/io/datastream.hpp +++ b/include/fc/io/datastream.hpp @@ -89,94 +89,5 @@ class datastream { size_t _size; }; -template -inline datastream& operator<<(datastream& ds, const int32_t& d) { - ds.write( (const char*)&d, sizeof(d) ); - return ds; -} -template -inline datastream& operator>>(datastream& ds, int32_t& d) { - ds.read((char*)&d, sizeof(d) ); - return ds; -} -template -inline datastream& operator<<(datastream& ds, const uint32_t& d) { - ds.write( (const char*)&d, sizeof(d) ); - return ds; -} - -template -inline datastream& operator>>(datastream& ds, uint32_t& d) { - ds.read((char*)&d, sizeof(d) ); - return ds; -} -template -inline datastream& operator<<(datastream& ds, const int64_t& d) { - ds.write( (const char*)&d, sizeof(d) ); - return ds; -} - -template -inline datastream& operator>>(datastream& ds, int64_t& d) { - ds.read((char*)&d, sizeof(d) ); - return ds; -} -template -inline datastream& operator<<(datastream& ds, const uint64_t& d) { - ds.write( (const char*)&d, sizeof(d) ); - return ds; -} - -template -inline datastream& operator>>(datastream& ds, uint64_t& d) { - ds.read((char*)&d, sizeof(d) ); - return ds; -} -template -inline datastream& operator<<(datastream& ds, const int16_t& d) { - ds.write( (const char*)&d, sizeof(d) ); - return ds; -} - -template -inline datastream& operator>>(datastream& ds, int16_t& d) { - ds.read((char*)&d, sizeof(d) ); - return ds; -} -template -inline datastream& operator<<(datastream& ds, const uint16_t& d) { - ds.write( (const char*)&d, sizeof(d) ); - return ds; -} - -template -inline datastream& operator>>(datastream& ds, uint16_t& d) { - ds.read((char*)&d, sizeof(d) ); - return ds; -} -template -inline datastream& operator<<(datastream& ds, const int8_t& d) { - ds.write( (const char*)&d, sizeof(d) ); - return ds; -} - -template -inline datastream& operator>>(datastream& ds, int8_t& d) { - ds.read((char*)&d, sizeof(d) ); - return ds; -} -template -inline datastream& operator<<(datastream& ds, const uint8_t& d) { - ds.write( (const char*)&d, sizeof(d) ); - return ds; -} - -template -inline datastream& operator>>(datastream& ds, uint8_t& d) { - ds.read((char*)&d, sizeof(d) ); - return ds; -} - - } // namespace fc diff --git a/include/fc/io/raw.hpp b/include/fc/io/raw.hpp index 052110c..9d9dd35 100644 --- a/include/fc/io/raw.hpp +++ b/include/fc/io/raw.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include #include #include @@ -89,72 +90,66 @@ namespace fc { template inline void pack( Stream& s, const fc::time_point_sec& tp, uint32_t _max_depth ) { - uint32_t usec = tp.sec_since_epoch(); - s.write( (const char*)&usec, sizeof(usec) ); + pack( s, tp.sec_since_epoch(), _max_depth ); } template inline void unpack( Stream& s, fc::time_point_sec& tp, uint32_t _max_depth ) { try { uint32_t sec; - s.read( (char*)&sec, sizeof(sec) ); + unpack( s, sec, _max_depth ); tp = fc::time_point() + fc::seconds(sec); } FC_RETHROW_EXCEPTIONS( warn, "" ) } template inline void pack( Stream& s, const fc::time_point& tp, uint32_t _max_depth ) { - uint64_t usec = tp.time_since_epoch().count(); - s.write( (const char*)&usec, sizeof(usec) ); + pack( s, tp.time_since_epoch().count(), _max_depth ); } template inline void unpack( Stream& s, fc::time_point& tp, uint32_t _max_depth ) { try { uint64_t usec; - s.read( (char*)&usec, sizeof(usec) ); + unpack( s, usec, _max_depth ); tp = fc::time_point() + fc::microseconds(usec); } FC_RETHROW_EXCEPTIONS( warn, "" ) } template inline void pack( Stream& s, const fc::microseconds& usec, uint32_t _max_depth ) { - uint64_t usec_as_int64 = usec.count(); - s.write( (const char*)&usec_as_int64, sizeof(usec_as_int64) ); + pack( s, usec.count(), _max_depth ); } template inline void unpack( Stream& s, fc::microseconds& usec, uint32_t _max_depth ) { try { uint64_t usec_as_int64; - s.read( (char*)&usec_as_int64, sizeof(usec_as_int64) ); + unpack( s, usec_as_int64, _max_depth ); usec = fc::microseconds(usec_as_int64); } FC_RETHROW_EXCEPTIONS( warn, "" ) } template - inline void pack( Stream& s, const fc::array& v, uint32_t _max_depth ) { - s.write((const char*)&v.data[0],N*sizeof(T)); + inline void pack( Stream& s, const fc::array& v, uint32_t _max_depth ) = delete; + template + inline void pack( Stream& s, const fc::array& v, uint32_t _max_depth ) { + s.write( &v.data[0], N ); } - - template - inline void pack( Stream& s, const std::shared_ptr& v, uint32_t _max_depth ) - { - FC_ASSERT( _max_depth > 0 ); - fc::raw::pack( s, *v, _max_depth - 1 ); - } - - template - inline void pack( Stream& s, const std::shared_ptr& v, uint32_t _max_depth ) - { - FC_ASSERT( _max_depth > 0 ); - fc::raw::pack( s, *v, _max_depth - 1 ); + template + inline void pack( Stream& s, const fc::array& v, uint32_t _max_depth ) { + s.write( (char*)&v.data[0], N ); } template - inline void unpack( Stream& s, fc::array& v, uint32_t _max_depth ) - { try { - s.read((char*)&v.data[0],N*sizeof(T)); - } FC_RETHROW_EXCEPTIONS( warn, "fc::array", ("type",fc::get_typename::name())("length",N) ) } + inline void unpack( Stream& s, fc::array& v, uint32_t _max_depth ) = delete; + template + inline void unpack( Stream& s, fc::array& v, uint32_t _max_depth ) { try { + s.read( &v.data[0], N ); + } FC_RETHROW_EXCEPTIONS( warn, "fc::array", ("length",N) ) } + template + inline void unpack( Stream& s, fc::array& v, uint32_t _max_depth ) { try { + s.read( (char*)&v.data[0], N ); + } FC_RETHROW_EXCEPTIONS( warn, "fc::array", ("length",N) ) } template inline void unpack( Stream& s, std::shared_ptr& v, uint32_t _max_depth ) @@ -162,7 +157,14 @@ namespace fc { FC_ASSERT( _max_depth > 0 ); v = std::make_shared(); fc::raw::unpack( s, *v, _max_depth - 1 ); - } FC_RETHROW_EXCEPTIONS( warn, "std::shared_ptr", ("type",fc::get_typename::name()) ) } + } FC_RETHROW_EXCEPTIONS( warn, "std::shared_ptr<${type}>", ("type",fc::get_typename::name()) ) } + + template + inline void pack( Stream& s, const std::shared_ptr& v, uint32_t _max_depth ) + { + FC_ASSERT( _max_depth > 0 ); + fc::raw::pack( s, *v, _max_depth - 1 ); + } template inline void unpack( Stream& s, std::shared_ptr& v, uint32_t _max_depth ) @@ -345,12 +347,96 @@ namespace fc { template<> struct if_class { template - static inline void pack( Stream& s, const T& v, uint32_t _max_depth ) { - s.write( (char*)&v, sizeof(v) ); - } + static inline void pack( Stream& s, const T v, uint32_t _max_depth ) = delete; template - static inline void unpack( Stream& s, T& v, uint32_t _max_depth ) { - s.read( (char*)&v, sizeof(v) ); + static inline void unpack( Stream& s, T& v, uint32_t _max_depth ) = delete; + template + static inline void pack( Stream& s, const int64_t v, uint32_t _max_depth ) { + boost::endian::little_int64_buf_t tmp; + tmp = v; + s.write( (char*)&tmp, sizeof(tmp) ); + } + template + static inline void unpack( Stream& s, int64_t& v, uint32_t _max_depth ) { + boost::endian::little_int64_buf_t tmp; + s.read( (char*)&tmp, sizeof(tmp) ); + v = tmp.value(); + } + template + static inline void pack( Stream& s, const uint64_t v, uint32_t _max_depth ) { + boost::endian::little_uint64_buf_t tmp; + tmp = v; + s.write( (char*)&tmp, sizeof(tmp) ); + } + template + static inline void unpack( Stream& s, uint64_t& v, uint32_t _max_depth ) { + boost::endian::little_uint64_buf_t tmp; + s.read( (char*)&tmp, sizeof(tmp) ); + v = tmp.value(); + } + template + static inline void pack( Stream& s, const int32_t v, uint32_t _max_depth ) { + boost::endian::little_int32_buf_t tmp; + tmp = v; + s.write( (char*)&tmp, sizeof(tmp) ); + } + template + static inline void unpack( Stream& s, int32_t& v, uint32_t _max_depth ) { + boost::endian::little_int32_buf_t tmp; + s.read( (char*)&tmp, sizeof(tmp) ); + v = tmp.value(); + } + template + static inline void pack( Stream& s, const uint32_t v, uint32_t _max_depth ) { + boost::endian::little_uint32_buf_t tmp; + tmp = v; + s.write( (char*)&tmp, sizeof(tmp) ); + } + template + static inline void unpack( Stream& s, uint32_t& v, uint32_t _max_depth ) { + boost::endian::little_uint32_buf_t tmp; + s.read( (char*)&tmp, sizeof(tmp) ); + v = tmp.value(); + } + template + static inline void pack( Stream& s, const int16_t v, uint32_t _max_depth ) { + boost::endian::little_int16_buf_t tmp; + tmp = v; + s.write( (char*)&tmp, sizeof(tmp) ); + } + template + static inline void unpack( Stream& s, int16_t& v, uint32_t _max_depth ) { + boost::endian::little_int16_buf_t tmp; + s.read( (char*)&tmp, sizeof(tmp) ); + v = tmp.value(); + } + template + static inline void pack( Stream& s, const uint16_t v, uint32_t _max_depth ) { + boost::endian::little_uint16_buf_t tmp; + tmp = v; + s.write( (char*)&tmp, sizeof(tmp) ); + } + template + static inline void unpack( Stream& s, uint16_t& v, uint32_t _max_depth ) { + boost::endian::little_uint16_buf_t tmp; + s.read( (char*)&tmp, sizeof(tmp) ); + v = tmp.value(); + } + template + static inline void pack( Stream& s, const int8_t v, uint32_t _max_depth ) { + s.write( (char*)&v, 1 ); + } + template + static inline void unpack( Stream& s, int8_t& v, uint32_t _max_depth ) { + s.read( (char*)&v, 1 ); + } + template + static inline void pack( Stream& s, const uint8_t v, uint32_t _max_depth ) { + s.write( (char*)&v, 1 ); + } + template + static inline void unpack( Stream& s, uint8_t& v, uint32_t _max_depth ) { + s.read( (char*)&v, 1 ); } }; diff --git a/include/fc/io/raw_fwd.hpp b/include/fc/io/raw_fwd.hpp index 17f2395..c1c1442 100644 --- a/include/fc/io/raw_fwd.hpp +++ b/include/fc/io/raw_fwd.hpp @@ -120,6 +120,10 @@ namespace fc { template inline void pack( Stream& s, const fc::array& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); template inline void unpack( Stream& s, fc::array& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH); + template inline void pack( Stream& s, const fc::array& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); + template inline void unpack( Stream& s, fc::array& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH); + template inline void pack( Stream& s, const fc::array& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); + template inline void unpack( Stream& s, fc::array& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH); template inline void pack( Stream& s, const shared_ptr& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); diff --git a/include/fc/io/raw_variant.hpp b/include/fc/io/raw_variant.hpp index f4a7e02..b9a1bf3 100644 --- a/include/fc/io/raw_variant.hpp +++ b/include/fc/io/raw_variant.hpp @@ -26,7 +26,7 @@ namespace fc { namespace raw { } virtual void handle( const double& v )const { - fc::raw::pack( s, v, max_depth ); + FC_THROW_EXCEPTION( invalid_arg_exception, "Can't pack double!" ); } virtual void handle( const bool& v )const { @@ -86,10 +86,7 @@ namespace fc { namespace raw { } case variant::double_type: { - double val; - raw::unpack( s, val, _max_depth ); - v = val; - return; + FC_THROW_EXCEPTION( invalid_arg_exception, "Can't unpack double!" ); } case variant::bool_type: { diff --git a/include/fc/real128.hpp b/include/fc/real128.hpp index e445ab6..995c006 100644 --- a/include/fc/real128.hpp +++ b/include/fc/real128.hpp @@ -32,6 +32,10 @@ namespace fc { uint64_t to_uint64()const; + template + inline void pack( Stream& s, uint32_t _max_depth=FC_PACK_MAX_DEPTH ) { + pack( s, fixed, _max_depth ); + } private: uint128 fixed; }; @@ -43,13 +47,15 @@ namespace fc { { template inline void pack( Stream& s, const real128& value_to_pack, uint32_t _max_depth=FC_PACK_MAX_DEPTH ) - { s.write( (char*)&value_to_pack, sizeof(value_to_pack) ); } + { value_to_pack.pack( s, _max_depth ); } template inline void unpack( Stream& s, real128& value_to_unpack, uint32_t _max_depth=FC_PACK_MAX_DEPTH ) - { s.read( (char*)&value_to_unpack, sizeof(value_to_unpack) ); } + { + uint128_t delegate; + unpack( s, delegate, _max_depth ); + value_to_unpack = fc::real128::from_fixed( delegate ); + } } - - } // namespace fc diff --git a/include/fc/uint128.hpp b/include/fc/uint128.hpp index 9c49bd6..42c8e3d 100644 --- a/include/fc/uint128.hpp +++ b/include/fc/uint128.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #ifdef _MSC_VER #pragma warning (push) @@ -127,9 +128,15 @@ namespace fc namespace raw { template - inline void pack( Stream& s, const uint128& u, uint32_t _max_depth=FC_PACK_MAX_DEPTH ) { s.write( (char*)&u, sizeof(u) ); } + inline void pack( Stream& s, const uint128& u, uint32_t _max_depth=FC_PACK_MAX_DEPTH ) { + pack( s, u.hi, _max_depth ); + pack( s, u.lo, _max_depth ); + } template - inline void unpack( Stream& s, uint128& u, uint32_t _max_depth=FC_PACK_MAX_DEPTH ) { s.read( (char*)&u, sizeof(u) ); } + inline void unpack( Stream& s, uint128& u, uint32_t _max_depth=FC_PACK_MAX_DEPTH ) { + unpack( s, u.hi, _max_depth ); + unpack( s, u.lo, _max_depth ); + } } size_t city_hash_size_t(const char *buf, size_t len); @@ -140,10 +147,7 @@ namespace std template<> struct hash { - size_t operator()( const fc::uint128& s )const - { - return fc::city_hash_size_t((char*)&s, sizeof(s)); - } + size_t operator()( const fc::uint128& s )const; }; } diff --git a/src/byteswap.hpp b/src/byteswap.hpp deleted file mode 100644 index e75b8e3..0000000 --- a/src/byteswap.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#pragma once - -#ifdef _WIN32 -# include -# define bswap_64(x) _byteswap_uint64(x) -#elif defined(__APPLE__) -# include -# define bswap_64(x) OSSwapInt64(x) -#else -# include -#endif diff --git a/src/crypto/bigint.cpp b/src/crypto/bigint.cpp index fd5a688..4c0589b 100644 --- a/src/crypto/bigint.cpp +++ b/src/crypto/bigint.cpp @@ -4,7 +4,8 @@ #include #include -#include "../byteswap.hpp" + +#include namespace fc { bigint::bigint( const char* bige, uint32_t l ) { @@ -30,7 +31,8 @@ namespace fc { bigint::bigint(uint64_t value) { - uint64_t big_endian_value = bswap_64(value); + boost::endian::big_uint64_buf_t big_endian_value; + big_endian_value = value; n = BN_bin2bn((const unsigned char*)&big_endian_value, sizeof(big_endian_value), NULL); } @@ -53,9 +55,10 @@ namespace fc { { FC_ASSERT(BN_num_bits(n) <= 63); size_t size = BN_num_bytes(n); - uint64_t abs_value = 0; - BN_bn2bin(n, (unsigned char*)&abs_value + (sizeof(uint64_t) - size)); - return BN_is_negative(n) ? -(int64_t)bswap_64(abs_value) : bswap_64(abs_value); + boost::endian::big_int64_buf_t abs_value; + abs_value = 0; + BN_bn2bin(n, (unsigned char*)&abs_value + (sizeof(abs_value) - size)); + return BN_is_negative(n) ? -abs_value.value() : abs_value.value(); } int64_t bigint::log2()const { return BN_num_bits(n); } diff --git a/src/io/iostream.cpp b/src/io/iostream.cpp index 4c4a0cc..c44fc83 100644 --- a/src/io/iostream.cpp +++ b/src/io/iostream.cpp @@ -264,67 +264,6 @@ namespace fc { return o; } - istream& operator>>( istream& o, double& v ) - { - assert(false && "not implemented"); - return o; - } - - istream& operator>>( istream& o, float& v ) - { - assert(false && "not implemented"); - return o; - } - - istream& operator>>( istream& o, int64_t& v ) - { - assert(false && "not implemented"); - return o; - } - - istream& operator>>( istream& o, uint64_t& v ) - { - assert(false && "not implemented"); - return o; - } - - istream& operator>>( istream& o, int32_t& v ) - { - assert(false && "not implemented"); - return o; - } - - istream& operator>>( istream& o, uint32_t& v ) - { - assert(false && "not implemented"); - return o; - } - - istream& operator>>( istream& o, int16_t& v ) - { - assert(false && "not implemented"); - return o; - } - - istream& operator>>( istream& o, uint16_t& v ) - { - assert(false && "not implemented"); - return o; - } - - istream& operator>>( istream& o, int8_t& v ) - { - assert(false && "not implemented"); - return o; - } - - istream& operator>>( istream& o, uint8_t& v ) - { - assert(false && "not implemented"); - return o; - } - - char istream::get() { char tmp; diff --git a/src/io/sstream.cpp b/src/io/sstream.cpp index 70977c0..1f9a87e 100644 --- a/src/io/sstream.cpp +++ b/src/io/sstream.cpp @@ -76,31 +76,6 @@ namespace fc { void stringstream::close(){ my->ss.flush(); }; void stringstream::flush(){ my->ss.flush(); }; - /* - istream& stringstream::read( char* buf, size_t len ) { - my->ss.read(buf,len); - return *this; - } - istream& stringstream::read( int64_t& v ) { my->ss >> v; return *this; } - istream& stringstream::read( uint64_t& v ) { my->ss >> v; return *this; } - istream& stringstream::read( int32_t& v ) { my->ss >> v; return *this; } - istream& stringstream::read( uint32_t& v ) { my->ss >> v; return *this; } - istream& stringstream::read( int16_t& v ) { my->ss >> v; return *this; } - istream& stringstream::read( uint16_t& v ) { my->ss >> v; return *this; } - istream& stringstream::read( int8_t& v ) { my->ss >> v; return *this; } - istream& stringstream::read( uint8_t& v ) { my->ss >> v; return *this; } - istream& stringstream::read( float& v ) { my->ss >> v; return *this; } - istream& stringstream::read( double& v ) { my->ss >> v; return *this; } - istream& stringstream::read( bool& v ) { my->ss >> v; return *this; } - istream& stringstream::read( char& v ) { my->ss >> v; return *this; } - istream& stringstream::read( std::string& v ) { my->ss >> *reinterpret_cast(&v); return *this; } - - ostream& stringstream::write( const std::string& s) { - my->ss.write( s.c_str(), s.size() ); - return *this; - } - */ - char stringstream::peek() { char c = my->ss.peek(); diff --git a/src/uint128.cpp b/src/uint128.cpp index 5b12a4d..fa5b795 100644 --- a/src/uint128.cpp +++ b/src/uint128.cpp @@ -1,10 +1,10 @@ #include #include #include +#include #include #include -#include "byteswap.hpp" namespace fc { @@ -119,7 +119,9 @@ namespace fc uint128::operator bigint()const { - auto tmp = uint128( bswap_64( hi ), bswap_64( lo ) ); + boost::endian::big_uint64_buf_t tmp[2]; + tmp[0] = hi; + tmp[1] = lo; bigint bi( (char*)&tmp, sizeof(tmp) ); return bi; } @@ -367,6 +369,15 @@ namespace fc } // namespace fc +namespace std { + size_t hash::operator()( const fc::uint128& s )const + { + boost::endian::little_uint64_buf_t tmp[2]; + tmp[0] = s.hi; + tmp[1] = s.lo; + return fc::city_hash_size_t((char*)&tmp, sizeof(tmp)); + } +} /* * Portions of the above code were adapted from the work of Evan Teran. diff --git a/tests/bloom_test.cpp b/tests/bloom_test.cpp index 1ba7e0d..2ea566a 100644 --- a/tests/bloom_test.cpp +++ b/tests/bloom_test.cpp @@ -59,15 +59,7 @@ BOOST_AUTO_TEST_CASE(bloom_test_1) ++count; } } -// wdump((filter)); - auto packed_filter = fc::raw::pack(filter); -// wdump((packed_filter.size())); -// wdump((packed_filter)); - std::stringstream out; -// std::string str = fc::json::to_string(packed_filter); - auto b64 = fc::base64_encode( packed_filter.data(), packed_filter.size() ); - for( uint32_t i = 0; i < b64.size(); i += 1024 ) - out << '"' << b64.substr( i, 1024 ) << "\",\n"; + // FIXME: this doesn't really test anything. } catch ( const fc::exception& e ) { From f0c6976aeafb32c693c6161558bada627af8c9e8 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Thu, 7 Mar 2019 22:14:06 +0100 Subject: [PATCH 07/16] Removed superfluous return statement --- include/fc/io/raw.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/include/fc/io/raw.hpp b/include/fc/io/raw.hpp index 9d9dd35..9b686f7 100644 --- a/include/fc/io/raw.hpp +++ b/include/fc/io/raw.hpp @@ -774,7 +774,6 @@ namespace fc { FC_ASSERT( _max_depth > 0 ); datastream ds( d, s ); fc::raw::unpack( ds, v, _max_depth - 1 ); - return v; } FC_RETHROW_EXCEPTIONS( warn, "error unpacking ${type}", ("type",fc::get_typename::name() ) ) } template From 8cbc2a2e22ad254fa7980b91094ce1bf84b1b05b Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Mon, 1 Apr 2019 10:16:35 +0200 Subject: [PATCH 08/16] Pulled CMake check for -latomic --- .gitignore | 2 +- CMakeModules/CheckLibcxxAtomic.cmake | 57 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 CMakeModules/CheckLibcxxAtomic.cmake diff --git a/.gitignore b/.gitignore index 8f529ee..a8e1180 100644 --- a/.gitignore +++ b/.gitignore @@ -36,7 +36,7 @@ Release/ CMakeCache.txt CMakeFiles Makefile -*.cmake +cmake_install.cmake *.cbp libfc.a diff --git a/CMakeModules/CheckLibcxxAtomic.cmake b/CMakeModules/CheckLibcxxAtomic.cmake new file mode 100644 index 0000000..596225d --- /dev/null +++ b/CMakeModules/CheckLibcxxAtomic.cmake @@ -0,0 +1,57 @@ +# Taken from https://chromium.googlesource.com/chromium/llvm-project/libcxx/+/refs/heads/master/cmake/Modules/CheckLibcxxAtomic.cmake +# Apache License v2.0 with LLVM Exceptions + +INCLUDE(CheckCXXSourceCompiles) + +# Sometimes linking against libatomic is required for atomic ops, if +# the platform doesn't support lock-free atomics. +# +# We could modify LLVM's CheckAtomic module and have it check for 64-bit +# atomics instead. However, we would like to avoid careless uses of 64-bit +# atomics inside LLVM over time on 32-bit platforms. + +function(check_cxx_atomics varname) + set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs -std=c++11 -nostdinc++ -isystem ${LIBCXX_SOURCE_DIR}/include") + if (${LIBCXX_GCC_TOOLCHAIN}) + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --gcc-toolchain=${LIBCXX_GCC_TOOLCHAIN}") + endif() + if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize) + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all") + endif() + if (CMAKE_C_FLAGS MATCHES -fsanitize-coverage OR CMAKE_CXX_FLAGS MATCHES -fsanitize-coverage) + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize-coverage=edge,trace-cmp,indirect-calls,8bit-counters") + endif() + check_cxx_source_compiles(" +#include +#include +std::atomic x; +std::atomic y; +int main(int, char**) { + return x + y; +} +" ${varname}) + set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) +endfunction(check_cxx_atomics) + +# Perform the check for 64bit atomics without libatomic. It may have been +# added to the required libraries during in the configuration of LLVM, which +# would cause the check for CXX atomics without libatomic to incorrectly pass. +set(OLD_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES}) +list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "atomic") +check_cxx_atomics(LIBCXX_HAVE_CXX_ATOMICS_WITHOUT_LIB) +set(CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES}) + +check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB) +# If not, check if the library exists, and atomics work with it. +if(NOT LIBCXX_HAVE_CXX_ATOMICS_WITHOUT_LIB) + if(LIBCXX_HAS_ATOMIC_LIB) + list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic") + check_cxx_atomics(LIBCXX_HAVE_CXX_ATOMICS_WITH_LIB) + if (NOT LIBCXX_HAVE_CXX_ATOMICS_WITH_LIB) + message(WARNING "Host compiler must support std::atomic!") + endif() + else() + message(WARNING "Host compiler appears to require libatomic, but cannot find it.") + endif() +endif() From 89796afb010cc51ff51be5682bf7b639747c9e7b Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Mon, 1 Apr 2019 14:32:08 +0200 Subject: [PATCH 09/16] Adapted + integrated libatomic check --- CMakeLists.txt | 11 +++++++++-- CMakeModules/CheckLibcxxAtomic.cmake | 27 ++++++++++----------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0acb9f1..da33719 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,8 @@ INCLUDE(GetPrerequisites) INCLUDE( VersionMacros ) INCLUDE( SetupTargetMacros ) INCLUDE(GetGitRevisionDescription) +INCLUDE(CheckLibraryExists) +INCLUDE(CheckLibcxxAtomic) get_git_head_revision(GIT_REFSPEC FC_GIT_REVISION_SHA) get_git_unix_timestamp(FC_GIT_REVISION_UNIX_TIMESTAMP) @@ -159,7 +161,7 @@ IF( WIN32 ) SET(Boost_LIBRARIES ${BOOST_LIBRARIES_TEMP} ${Boost_LIBRARIES}) ENDIF() - set( PLATFORM_SPECIFIC_LIBS wsock32.lib ws2_32.lib userenv.lib) + LIST(APPEND PLATFORM_SPECIFIC_LIBS wsock32.lib ws2_32.lib userenv.lib) # iphlpapi.lib ELSE(WIN32) @@ -186,6 +188,9 @@ IF(NOT "$ENV{OPENSSL_ROOT_DIR}" STREQUAL "") message(STATUS "Setting up OpenSSL root and include vars to ${OPENSSL_ROOT_DIR}, ${OPENSSL_INCLUDE_DIR}") ENDIF() +IF( LIBCXX_HAVE_CXX_ATOMICS_WITH_LIB ) + LIST( APPEND PLATFORM_SPECIFIC_LIBS atomic ) +ENDIF( LIBCXX_HAVE_CXX_ATOMICS_WITH_LIB ) find_package(OpenSSL REQUIRED) @@ -383,7 +388,9 @@ target_include_directories(fc IF(NOT WIN32) set(LINK_USR_LOCAL_LIB -L/usr/local/lib) ENDIF() -target_link_libraries( fc PUBLIC ${LINK_USR_LOCAL_LIB} ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} ${ZLIB_LIBRARIES} ${PLATFORM_SPECIFIC_LIBS} ${RPCRT4} ${CMAKE_DL_LIBS} ${rt_library} ${editline_libraries} secp256k1 ) +target_link_libraries( fc PUBLIC ${LINK_USR_LOCAL_LIB} ${Boost_LIBRARIES} ${OPENSSL_LIBRARIES} ${ZLIB_LIBRARIES} + ${PLATFORM_SPECIFIC_LIBS} ${RPCRT4} ${CMAKE_DL_LIBS} ${rt_library} + ${editline_libraries} secp256k1 ${CMAKE_REQUIRED_LIBRARIES} ) if(MSVC) set_source_files_properties( src/network/http/websocket.cpp PROPERTIES COMPILE_FLAGS "/bigobj" ) diff --git a/CMakeModules/CheckLibcxxAtomic.cmake b/CMakeModules/CheckLibcxxAtomic.cmake index 596225d..0e55627 100644 --- a/CMakeModules/CheckLibcxxAtomic.cmake +++ b/CMakeModules/CheckLibcxxAtomic.cmake @@ -5,14 +5,10 @@ INCLUDE(CheckCXXSourceCompiles) # Sometimes linking against libatomic is required for atomic ops, if # the platform doesn't support lock-free atomics. -# -# We could modify LLVM's CheckAtomic module and have it check for 64-bit -# atomics instead. However, we would like to avoid careless uses of 64-bit -# atomics inside LLVM over time on 32-bit platforms. function(check_cxx_atomics varname) set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) - set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs -std=c++11 -nostdinc++ -isystem ${LIBCXX_SOURCE_DIR}/include") + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11") if (${LIBCXX_GCC_TOOLCHAIN}) set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --gcc-toolchain=${LIBCXX_GCC_TOOLCHAIN}") endif() @@ -24,27 +20,24 @@ function(check_cxx_atomics varname) endif() check_cxx_source_compiles(" #include -#include -std::atomic x; -std::atomic y; +#include + +boost::lockfree::queue q; int main(int, char**) { - return x + y; + uint32_t* a; + uint32_t* b; + q.push(a); + q.pop(b); } " ${varname}) set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) endfunction(check_cxx_atomics) -# Perform the check for 64bit atomics without libatomic. It may have been -# added to the required libraries during in the configuration of LLVM, which -# would cause the check for CXX atomics without libatomic to incorrectly pass. -set(OLD_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES}) -list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "atomic") +# Perform the check for 64bit atomics without libatomic. check_cxx_atomics(LIBCXX_HAVE_CXX_ATOMICS_WITHOUT_LIB) -set(CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES}) - -check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB) # If not, check if the library exists, and atomics work with it. if(NOT LIBCXX_HAVE_CXX_ATOMICS_WITHOUT_LIB) + check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB) if(LIBCXX_HAS_ATOMIC_LIB) list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic") check_cxx_atomics(LIBCXX_HAVE_CXX_ATOMICS_WITH_LIB) From cb9a96755df4ecae681db4669ad2aecef9c13671 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Wed, 3 Apr 2019 18:04:00 +0200 Subject: [PATCH 10/16] Add proper serialization for boost::endian::endian_buffer --- include/fc/io/raw.hpp | 12 ++++++++++++ include/fc/io/raw_fwd.hpp | 5 +++++ include/fc/variant.hpp | 14 ++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/include/fc/io/raw.hpp b/include/fc/io/raw.hpp index 9b686f7..5b3bfc6 100644 --- a/include/fc/io/raw.hpp +++ b/include/fc/io/raw.hpp @@ -676,6 +676,18 @@ namespace fc { } } + template + void pack( Stream& s, const boost::endian::endian_buffer& v, uint32_t _max_depth ) + { + FC_ASSERT( _max_depth > 0 ); + s.write( v.data(), sizeof(v) ); + } + template + void unpack( Stream& s, boost::endian::endian_buffer& v, uint32_t _max_depth ) + { + FC_ASSERT( _max_depth > 0 ); + s.read( v.data(), sizeof(v) ); + } template diff --git a/include/fc/io/raw_fwd.hpp b/include/fc/io/raw_fwd.hpp index c1c1442..f8cf921 100644 --- a/include/fc/io/raw_fwd.hpp +++ b/include/fc/io/raw_fwd.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include #include #include @@ -117,6 +118,10 @@ namespace fc { template inline void pack( Stream& s, const char* v, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); template inline void pack( Stream& s, const std::vector& value, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); template inline void unpack( Stream& s, std::vector& value, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); + template + inline void pack( Stream& s, const boost::endian::endian_buffer& v, uint32_t _max_depth ); + template + inline void unpack( Stream& s, boost::endian::endian_buffer& v, uint32_t _max_depth ); template inline void pack( Stream& s, const fc::array& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); template inline void unpack( Stream& s, fc::array& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH); diff --git a/include/fc/variant.hpp b/include/fc/variant.hpp index c98e796..8da0b2a 100644 --- a/include/fc/variant.hpp +++ b/include/fc/variant.hpp @@ -12,6 +12,7 @@ #include #include +#include #include #ifdef FC_ASSERT @@ -165,6 +166,19 @@ namespace fc template void from_variant( const variant& v, std::pair& p, uint32_t max_depth ); + template + void to_variant( const boost::endian::endian_buffer& var, variant& vo, uint32_t max_depth ) + { + to_variant( var.value(), vo, max_depth ); + } + template + void from_variant( const variant& var, boost::endian::endian_buffer& vo, uint32_t max_depth ) + { + T tmp; + from_variant( var, tmp, max_depth ); + vo = tmp; + } + /** * @brief stores null, int64, uint64, double, bool, string, std::vector, * and variant_object's. From 006f048ac539676a45ca19dcb65a21c06631eb09 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Sat, 6 Apr 2019 09:55:23 +0200 Subject: [PATCH 11/16] Fixed cityhash endianness --- src/crypto/city.cpp | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/src/crypto/city.cpp b/src/crypto/city.cpp index 971b7bd..af3a91b 100644 --- a/src/crypto/city.cpp +++ b/src/crypto/city.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #if defined(__SSE4_2__) && defined(__x86_64__) #include @@ -63,18 +64,6 @@ inline uint64_t Hash128to64(const uint128& x) { return b; } -static uint64_t UNALIGNED_LOAD64(const char *p) { - uint64_t result; - memcpy(&result, p, sizeof(result)); - return result; -} - -static uint32_t UNALIGNED_LOAD32(const char *p) { - uint32_t result; - memcpy(&result, p, sizeof(result)); - return result; -} - #ifdef _WIN32 #include @@ -121,14 +110,6 @@ static uint32_t UNALIGNED_LOAD32(const char *p) { #endif -#ifdef WORDS_BIGENDIAN -#define uint32_in_expected_order(x) (bswap_32(x)) -#define uint64_in_expected_order(x) (bswap_64(x)) -#else -#define uint32_in_expected_order(x) (x) -#define uint64_in_expected_order(x) (x) -#endif - #if !defined(LIKELY) #if HAVE_BUILTIN_EXPECT #define LIKELY(x) (__builtin_expect(!!(x), 1)) @@ -138,11 +119,15 @@ static uint32_t UNALIGNED_LOAD32(const char *p) { #endif static uint64_t Fetch64(const char *p) { - return uint64_in_expected_order(UNALIGNED_LOAD64(p)); + boost::endian::little_uint64_buf_t result; + memcpy(&result, p, sizeof(result)); + return result.value(); } static uint32_t Fetch32(const char *p) { - return uint32_in_expected_order(UNALIGNED_LOAD32(p)); + boost::endian::little_uint32_buf_t result; + memcpy(&result, p, sizeof(result)); + return result.value(); } // Some primes between 2^63 and 2^64 for various uses. From 5a9cf326964167b0a4331f00ddd9e3f7121fde9c Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Sun, 7 Apr 2019 10:48:40 +0200 Subject: [PATCH 12/16] Fixed AES IV handling --- src/crypto/aes.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/crypto/aes.cpp b/src/crypto/aes.cpp index 2dbae6c..7bb5d20 100644 --- a/src/crypto/aes.cpp +++ b/src/crypto/aes.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #ifndef OPENSSL_THREADS @@ -52,7 +53,10 @@ void aes_encoder::init( const fc::sha256& key, const fc::uint128& init_value ) * In this example we are using 256 bit AES (i.e. a 256 bit key). The * IV size for *most* modes is the same as the block size. For AES this * is 128 bits */ - if(1 != EVP_EncryptInit_ex(my->ctx, EVP_aes_256_cbc(), NULL, (unsigned char*)&key, (unsigned char*)&init_value)) + boost::endian::little_uint64_buf_t iv[2]; + iv[0] = init_value.hi; + iv[1] = init_value.lo; + if(1 != EVP_EncryptInit_ex(my->ctx, EVP_aes_256_cbc(), NULL, (unsigned char*)&key, (const unsigned char*)iv[0].data())) { FC_THROW_EXCEPTION( aes_exception, "error during aes 256 cbc encryption init", ("s", ERR_error_string( ERR_get_error(), nullptr) ) ); @@ -117,7 +121,10 @@ void aes_decoder::init( const fc::sha256& key, const fc::uint128& init_value ) * In this example we are using 256 bit AES (i.e. a 256 bit key). The * IV size for *most* modes is the same as the block size. For AES this * is 128 bits */ - if(1 != EVP_DecryptInit_ex(my->ctx, EVP_aes_256_cbc(), NULL, (unsigned char*)&key, (unsigned char*)&init_value)) + boost::endian::little_uint64_buf_t iv[2]; + iv[0] = init_value.hi; + iv[1] = init_value.lo; + if(1 != EVP_DecryptInit_ex(my->ctx, EVP_aes_256_cbc(), NULL, (unsigned char*)&key, (const unsigned char*)iv[0].data())) { FC_THROW_EXCEPTION( aes_exception, "error during aes 256 cbc encryption init", ("s", ERR_error_string( ERR_get_error(), nullptr) ) ); From 9568948350fad00d4bddbd969b910b318b325cbf Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Sun, 7 Apr 2019 18:50:47 +0200 Subject: [PATCH 13/16] Back hashes with boost endian buffers --- include/fc/crypto/ripemd160.hpp | 4 ++-- include/fc/crypto/sha1.hpp | 3 ++- include/fc/crypto/sha224.hpp | 4 ++-- include/fc/crypto/sha256.hpp | 5 +++-- include/fc/crypto/sha512.hpp | 3 ++- src/crypto/ripemd160.cpp | 10 +++++----- src/crypto/sha1.cpp | 10 +++++----- src/crypto/sha224.cpp | 2 +- src/crypto/sha256.cpp | 8 ++++---- src/crypto/sha512.cpp | 16 ++++++++-------- 10 files changed, 34 insertions(+), 31 deletions(-) diff --git a/include/fc/crypto/ripemd160.hpp b/include/fc/crypto/ripemd160.hpp index 389d677..58d8b18 100644 --- a/include/fc/crypto/ripemd160.hpp +++ b/include/fc/crypto/ripemd160.hpp @@ -1,5 +1,5 @@ #pragma once - +#include #include #include #include @@ -68,7 +68,7 @@ class ripemd160 friend bool operator > ( const ripemd160& h1, const ripemd160& h2 ); friend bool operator < ( const ripemd160& h1, const ripemd160& h2 ); - uint32_t _hash[5]; + boost::endian::little_uint32_buf_t _hash[5]; }; namespace raw { diff --git a/include/fc/crypto/sha1.hpp b/include/fc/crypto/sha1.hpp index 46b76e4..7209231 100644 --- a/include/fc/crypto/sha1.hpp +++ b/include/fc/crypto/sha1.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include #include @@ -64,7 +65,7 @@ class sha1 friend bool operator > ( const sha1& h1, const sha1& h2 ); friend bool operator < ( const sha1& h1, const sha1& h2 ); - uint32_t _hash[5]; + boost::endian::little_uint32_buf_t _hash[5]; }; class variant; diff --git a/include/fc/crypto/sha224.hpp b/include/fc/crypto/sha224.hpp index 490e005..772e9f6 100644 --- a/include/fc/crypto/sha224.hpp +++ b/include/fc/crypto/sha224.hpp @@ -1,5 +1,5 @@ #pragma once -#include +#include #include #include @@ -65,7 +65,7 @@ class sha224 friend bool operator > ( const sha224& h1, const sha224& h2 ); friend bool operator < ( const sha224& h1, const sha224& h2 ); - uint32_t _hash[7]; + boost::endian::little_uint32_buf_t _hash[7]; }; namespace raw { diff --git a/include/fc/crypto/sha256.hpp b/include/fc/crypto/sha256.hpp index 353fc3a..a382ad0 100644 --- a/include/fc/crypto/sha256.hpp +++ b/include/fc/crypto/sha256.hpp @@ -1,6 +1,7 @@ #pragma once +#include #include -#include +#include #include namespace fc @@ -67,7 +68,7 @@ class sha256 friend bool operator > ( const sha256& h1, const sha256& h2 ); friend bool operator < ( const sha256& h1, const sha256& h2 ); - uint64_t _hash[4]; + boost::endian::little_uint64_buf_t _hash[4]; }; namespace raw { diff --git a/include/fc/crypto/sha512.hpp b/include/fc/crypto/sha512.hpp index 9751a56..10c9ea0 100644 --- a/include/fc/crypto/sha512.hpp +++ b/include/fc/crypto/sha512.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include namespace fc @@ -62,7 +63,7 @@ class sha512 friend bool operator > ( const sha512& h1, const sha512& h2 ); friend bool operator < ( const sha512& h1, const sha512& h2 ); - uint64_t _hash[8]; + boost::endian::little_uint64_buf_t _hash[8]; }; namespace raw { diff --git a/src/crypto/ripemd160.cpp b/src/crypto/ripemd160.cpp index b76de34..fe6c588 100644 --- a/src/crypto/ripemd160.cpp +++ b/src/crypto/ripemd160.cpp @@ -76,11 +76,11 @@ ripemd160 operator << ( const ripemd160& h1, uint32_t i ) { } ripemd160 operator ^ ( const ripemd160& h1, const ripemd160& h2 ) { ripemd160 result; - result._hash[0] = h1._hash[0] ^ h2._hash[0]; - result._hash[1] = h1._hash[1] ^ h2._hash[1]; - result._hash[2] = h1._hash[2] ^ h2._hash[2]; - result._hash[3] = h1._hash[3] ^ h2._hash[3]; - result._hash[4] = h1._hash[4] ^ h2._hash[4]; + result._hash[0] = h1._hash[0].value() ^ h2._hash[0].value(); + result._hash[1] = h1._hash[1].value() ^ h2._hash[1].value(); + result._hash[2] = h1._hash[2].value() ^ h2._hash[2].value(); + result._hash[3] = h1._hash[3].value() ^ h2._hash[3].value(); + result._hash[4] = h1._hash[4].value() ^ h2._hash[4].value(); return result; } bool operator >= ( const ripemd160& h1, const ripemd160& h2 ) { diff --git a/src/crypto/sha1.cpp b/src/crypto/sha1.cpp index 983edc4..d8337e5 100644 --- a/src/crypto/sha1.cpp +++ b/src/crypto/sha1.cpp @@ -60,11 +60,11 @@ sha1 operator << ( const sha1& h1, uint32_t i ) { } sha1 operator ^ ( const sha1& h1, const sha1& h2 ) { sha1 result; - result._hash[0] = h1._hash[0] ^ h2._hash[0]; - result._hash[1] = h1._hash[1] ^ h2._hash[1]; - result._hash[2] = h1._hash[2] ^ h2._hash[2]; - result._hash[3] = h1._hash[3] ^ h2._hash[3]; - result._hash[4] = h1._hash[4] ^ h2._hash[4]; + result._hash[0] = h1._hash[0].value() ^ h2._hash[0].value(); + result._hash[1] = h1._hash[1].value() ^ h2._hash[1].value(); + result._hash[2] = h1._hash[2].value() ^ h2._hash[2].value(); + result._hash[3] = h1._hash[3].value() ^ h2._hash[3].value(); + result._hash[4] = h1._hash[4].value() ^ h2._hash[4].value(); return result; } bool operator >= ( const sha1& h1, const sha1& h2 ) { diff --git a/src/crypto/sha224.cpp b/src/crypto/sha224.cpp index b1096b6..54be117 100644 --- a/src/crypto/sha224.cpp +++ b/src/crypto/sha224.cpp @@ -60,7 +60,7 @@ namespace fc { sha224 operator ^ ( const sha224& h1, const sha224& h2 ) { sha224 result; for( uint32_t i = 0; i < 7; ++i ) - result._hash[i] = h1._hash[i] ^ h2._hash[i]; + result._hash[i] = h1._hash[i].value() ^ h2._hash[i].value(); return result; } bool operator >= ( const sha224& h1, const sha224& h2 ) { diff --git a/src/crypto/sha256.cpp b/src/crypto/sha256.cpp index db0a18a..6d35035 100644 --- a/src/crypto/sha256.cpp +++ b/src/crypto/sha256.cpp @@ -77,10 +77,10 @@ namespace fc { } sha256 operator ^ ( const sha256& h1, const sha256& h2 ) { sha256 result; - result._hash[0] = h1._hash[0] ^ h2._hash[0]; - result._hash[1] = h1._hash[1] ^ h2._hash[1]; - result._hash[2] = h1._hash[2] ^ h2._hash[2]; - result._hash[3] = h1._hash[3] ^ h2._hash[3]; + result._hash[0] = h1._hash[0].value() ^ h2._hash[0].value(); + result._hash[1] = h1._hash[1].value() ^ h2._hash[1].value(); + result._hash[2] = h1._hash[2].value() ^ h2._hash[2].value(); + result._hash[3] = h1._hash[3].value() ^ h2._hash[3].value(); return result; } bool operator >= ( const sha256& h1, const sha256& h2 ) { diff --git a/src/crypto/sha512.cpp b/src/crypto/sha512.cpp index 4b82efe..d499314 100644 --- a/src/crypto/sha512.cpp +++ b/src/crypto/sha512.cpp @@ -59,14 +59,14 @@ namespace fc { } sha512 operator ^ ( const sha512& h1, const sha512& h2 ) { sha512 result; - result._hash[0] = h1._hash[0] ^ h2._hash[0]; - result._hash[1] = h1._hash[1] ^ h2._hash[1]; - result._hash[2] = h1._hash[2] ^ h2._hash[2]; - result._hash[3] = h1._hash[3] ^ h2._hash[3]; - result._hash[4] = h1._hash[4] ^ h2._hash[4]; - result._hash[5] = h1._hash[5] ^ h2._hash[5]; - result._hash[6] = h1._hash[6] ^ h2._hash[6]; - result._hash[7] = h1._hash[7] ^ h2._hash[7]; + result._hash[0] = h1._hash[0].value() ^ h2._hash[0].value(); + result._hash[1] = h1._hash[1].value() ^ h2._hash[1].value(); + result._hash[2] = h1._hash[2].value() ^ h2._hash[2].value(); + result._hash[3] = h1._hash[3].value() ^ h2._hash[3].value(); + result._hash[4] = h1._hash[4].value() ^ h2._hash[4].value(); + result._hash[5] = h1._hash[5].value() ^ h2._hash[5].value(); + result._hash[6] = h1._hash[6].value() ^ h2._hash[6].value(); + result._hash[7] = h1._hash[7].value() ^ h2._hash[7].value(); return result; } bool operator >= ( const sha512& h1, const sha512& h2 ) { From 532093bda58427d6d14e36ea5e1a9a9ac8db784c Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Sun, 7 Apr 2019 22:31:37 +0200 Subject: [PATCH 14/16] Added missing serialization for std::shared_ptr --- include/fc/io/raw.hpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/include/fc/io/raw.hpp b/include/fc/io/raw.hpp index 5b3bfc6..27b376f 100644 --- a/include/fc/io/raw.hpp +++ b/include/fc/io/raw.hpp @@ -151,6 +151,13 @@ namespace fc { s.read( (char*)&v.data[0], N ); } FC_RETHROW_EXCEPTIONS( warn, "fc::array", ("length",N) ) } + template + inline void pack( Stream& s, const std::shared_ptr& v, uint32_t _max_depth ) + { + FC_ASSERT( _max_depth > 0 ); + fc::raw::pack( s, *v, _max_depth - 1 ); + } + template inline void unpack( Stream& s, std::shared_ptr& v, uint32_t _max_depth ) { try { From 1ae9169d08ff0153108cdfa0baf10aa0a75921df Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Thu, 11 Apr 2019 12:25:27 +0200 Subject: [PATCH 15/16] Removed log_test from travis because it no longer exists --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index a8cf4fc..63cf756 100644 --- a/.travis.yml +++ b/.travis.yml @@ -33,7 +33,6 @@ script: - "tests/api 2>&1 | grep -vE 'callback result 9|remote_calc->add. 4, 5 .: 9|set callback|] \\.$'" - tests/hmac_test 2>&1 | cat - tests/ecc_test README.md 2>&1 | cat - - tests/log_test 2>&1 | cat - 'find CMakeFiles/fc.dir -type d | while read d; do gcov -o "$d" "${d/CMakeFiles*.dir/./}"/*.cpp; done >/dev/null' - 'which sonar-scanner && sonar-scanner || true' - ccache -s From 379e59f6618a46722f949460d35311121085803d Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Sun, 21 Apr 2019 10:41:30 +0200 Subject: [PATCH 16/16] Moved delete declaration to raw_fwd --- include/fc/io/raw.hpp | 4 ---- include/fc/io/raw_fwd.hpp | 6 ++++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/include/fc/io/raw.hpp b/include/fc/io/raw.hpp index 27b376f..696f398 100644 --- a/include/fc/io/raw.hpp +++ b/include/fc/io/raw.hpp @@ -129,8 +129,6 @@ namespace fc { usec = fc::microseconds(usec_as_int64); } FC_RETHROW_EXCEPTIONS( warn, "" ) } - template - inline void pack( Stream& s, const fc::array& v, uint32_t _max_depth ) = delete; template inline void pack( Stream& s, const fc::array& v, uint32_t _max_depth ) { s.write( &v.data[0], N ); @@ -140,8 +138,6 @@ namespace fc { s.write( (char*)&v.data[0], N ); } - template - inline void unpack( Stream& s, fc::array& v, uint32_t _max_depth ) = delete; template inline void unpack( Stream& s, fc::array& v, uint32_t _max_depth ) { try { s.read( &v.data[0], N ); diff --git a/include/fc/io/raw_fwd.hpp b/include/fc/io/raw_fwd.hpp index f8cf921..30c67db 100644 --- a/include/fc/io/raw_fwd.hpp +++ b/include/fc/io/raw_fwd.hpp @@ -123,8 +123,10 @@ namespace fc { template inline void unpack( Stream& s, boost::endian::endian_buffer& v, uint32_t _max_depth ); - template inline void pack( Stream& s, const fc::array& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); - template inline void unpack( Stream& s, fc::array& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH); + template + inline void pack( Stream& s, const fc::array& v, uint32_t _max_depth ) = delete; + template + inline void unpack( Stream& s, fc::array& v, uint32_t _max_depth ) = delete; template inline void pack( Stream& s, const fc::array& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH ); template inline void unpack( Stream& s, fc::array& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH); template inline void pack( Stream& s, const fc::array& v, uint32_t _max_depth=FC_PACK_MAX_DEPTH );