From 1daac3b4b5b567bfa74ad99e8d115b0c64334304 Mon Sep 17 00:00:00 2001 From: Peter Conrad Date: Sat, 2 Mar 2019 15:01:00 +0100 Subject: [PATCH] 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)))