diff --git a/include/fc/crypto/bigint.hpp b/include/fc/crypto/bigint.hpp index e4fdd8b..debc869 100644 --- a/include/fc/crypto/bigint.hpp +++ b/include/fc/crypto/bigint.hpp @@ -11,7 +11,7 @@ namespace fc { public: bigint( const std::vector& bige ); bigint( const char* bige, uint32_t l ); - bigint( unsigned long i ); + bigint(uint64_t value); bigint( ); bigint( const bigint& c ); bigint( bigint&& c ); diff --git a/include/fc/network/tcp_socket.hpp b/include/fc/network/tcp_socket.hpp index 2fd235e..f9dbce0 100644 --- a/include/fc/network/tcp_socket.hpp +++ b/include/fc/network/tcp_socket.hpp @@ -49,7 +49,7 @@ namespace fc { friend class tcp_server; class impl; #ifdef _WIN64 - fc::fwd my; + fc::fwd my; #else fc::fwd my; #endif diff --git a/src/crypto/bigint.cpp b/src/crypto/bigint.cpp index 0682992..b76ba92 100644 --- a/src/crypto/bigint.cpp +++ b/src/crypto/bigint.cpp @@ -6,6 +6,16 @@ #include +#ifdef _MSC_VER +# include +# define bswap_64(x) _byteswap_uint64(x) +#elif defined(__APPLE__) +# include +# define bswap_64(x) OSSwapInt64(x) +#else +# include +#endif + namespace fc { bigint::bigint( const char* bige, uint32_t l ) { n = BN_bin2bn( (const unsigned char*)bige, l, NULL ); @@ -28,9 +38,10 @@ namespace fc { return BN_dup( n ); } - bigint::bigint( unsigned long i ) - :n(BN_new()) { - BN_set_word( n, i ); + bigint::bigint(uint64_t value) + { + uint64_t big_endian_value = bswap_64(value); + n = BN_bin2bn((const unsigned char*)&big_endian_value, sizeof(big_endian_value), NULL); } bigint::bigint( const bigint& c ) { @@ -47,7 +58,15 @@ namespace fc { } bool bigint::is_negative()const { return BN_is_negative(n); } - int64_t bigint::to_int64()const { return BN_get_word(n); } + + int64_t bigint::to_int64() const + { + 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); + } int64_t bigint::log2()const { return BN_num_bits(n); } bool bigint::operator < ( const bigint& c )const {