From f3e69d81a9596250123c53fc4deaf4eb827360fa Mon Sep 17 00:00:00 2001 From: theoreticalbts Date: Thu, 11 Aug 2016 11:53:43 -0400 Subject: [PATCH] sha256: Implement and test clz(), approx_log_32() --- CMakeLists.txt | 3 ++ include/fc/crypto/sha256.hpp | 22 +++++++++++-- src/crypto/sha256.cpp | 60 +++++++++++++++++++++++++++++++++++- tests/crypto/log_test.cpp | 43 ++++++++++++++++++++++++++ tests/crypto/log_test.py | 29 +++++++++++++++++ 5 files changed, 153 insertions(+), 4 deletions(-) create mode 100644 tests/crypto/log_test.cpp create mode 100755 tests/crypto/log_test.py diff --git a/CMakeLists.txt b/CMakeLists.txt index b53146f..3db7c86 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -382,6 +382,9 @@ target_link_libraries( udt_client fc udt ) add_executable( ecc_test tests/crypto/ecc_test.cpp ) target_link_libraries( ecc_test fc ) +add_executable( log_test tests/crypto/log_test.cpp ) +target_link_libraries( log_test fc ) + #add_executable( test_aes tests/aes_test.cpp ) #target_link_libraries( test_aes fc ${rt_library} ${pthread_library} ) #add_executable( test_sleep tests/sleep.cpp ) diff --git a/include/fc/crypto/sha256.hpp b/include/fc/crypto/sha256.hpp index 87fffa2..2af8a00 100644 --- a/include/fc/crypto/sha256.hpp +++ b/include/fc/crypto/sha256.hpp @@ -68,15 +68,31 @@ class sha256 friend bool operator > ( const sha256& h1, const sha256& h2 ); friend bool operator < ( const sha256& h1, const sha256& h2 ); - uint32_t pop_count() + uint32_t pop_count()const { return (uint32_t)(__builtin_popcountll(_hash[0]) + __builtin_popcountll(_hash[1]) + __builtin_popcountll(_hash[2]) + __builtin_popcountll(_hash[3])); } - - uint64_t _hash[4]; + + /** + * 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; + + uint64_t _hash[4]; }; typedef sha256 uint256; diff --git a/src/crypto/sha256.cpp b/src/crypto/sha256.cpp index 9c3b9e6..86bd014 100644 --- a/src/crypto/sha256.cpp +++ b/src/crypto/sha256.cpp @@ -97,7 +97,65 @@ namespace fc { bool operator == ( const sha256& h1, const sha256& h2 ) { 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; + } + + 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 ) { v = std::vector( (const char*)&bi, ((const char*)&bi) + sizeof(bi) ); diff --git a/tests/crypto/log_test.cpp b/tests/crypto/log_test.cpp new file mode 100644 index 0000000..ca9621b --- /dev/null +++ b/tests/crypto/log_test.cpp @@ -0,0 +1,43 @@ + +#include +#include + +#include +#include + +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; + } + ++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 new file mode 100755 index 0000000..ec5c7fe --- /dev/null +++ b/tests/crypto/log_test.py @@ -0,0 +1,29 @@ +#!/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)))