Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix simplifications #140

Merged
merged 12 commits into from
Aug 13, 2019
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ PROJECT( fc )
set( CMAKE_CXX_STANDARD 14 )
SET( CMAKE_CXX_STANDARD_REQUIRED ON )

if( "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" )
if( "${CMAKE_CXX_COMPILER_ID}" MATCHES "^(Apple)?Clang|GNU$" )

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CMake Warning (dev) in CMakeLists.txt:
  A logical block opening on the line

    /home/jmjatlanta/Development/cpp/bitshares-fc-1584/CMakeLists.txt:10 (if)

  closes on the line

    /home/jmjatlanta/Development/cpp/bitshares-fc-1584/CMakeLists.txt:14 (endif)

  with mis-matching arguments.

Perhaps C-n-P the "if" into the "else" and "endif". Tested with cmake 3.15.1.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, also in core

set( CMAKE_CXX_EXTENSIONS ON ) # for __int128 support
else( GNU )
else( "${CMAKE_CXX_COMPILER_ID}" MATCHES "^(Apple)?Clang|GNU$" )
set( CMAKE_CXX_EXTENSIONS OFF )
endif( GNU )
endif( "${CMAKE_CXX_COMPILER_ID}" MATCHES "^(Apple)?Clang|GNU$" )

MESSAGE(STATUS "Configuring project fc located in: ${CMAKE_CURRENT_SOURCE_DIR}")
SET( CMAKE_AUTOMOC OFF )
Expand Down
3 changes: 1 addition & 2 deletions include/fc/asio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ namespace asio {
*/
template<typename AsyncReadStream, typename MutableBufferSequence>
size_t read( AsyncReadStream& s, const MutableBufferSequence& buf ) {
promise<size_t>::ptr p(new promise<size_t>("fc::asio::read"));
promise<size_t>::ptr p = promise<size_t>::create("fc::asio::read");
boost::asio::async_read( s, buf, detail::read_write_handler(p) );
return p->wait();
}
Expand Down Expand Up @@ -249,7 +249,6 @@ namespace asio {
*/
template<typename SocketType, typename AcceptorType>
void accept( AcceptorType& acc, SocketType& sock ) {
//promise<boost::system::error_code>::ptr p( new promise<boost::system::error_code>("fc::asio::tcp::accept") );
promise<void>::ptr p = promise<void>::create("fc::asio::tcp::accept");
acc.async_accept( sock, boost::bind( fc::asio::detail::error_handler, p, _1 ) );
p->wait();
Expand Down
2 changes: 1 addition & 1 deletion include/fc/network/tcp_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace fc {
friend class tcp_server;
class impl;
#ifdef _WIN64
fc::fwd<impl,0x88> my;
fc::fwd<impl,0x98> my;
#else
fc::fwd<impl,0x54> my;
#endif
Expand Down
2 changes: 1 addition & 1 deletion include/fc/reflect/reflect.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ template<> struct reflector<ENUM> { \
{ \
i = boost::lexical_cast<int64_t>(s); \
} \
catch( const boost::bad_lexical_cast& e ) \
catch( const boost::bad_lexical_cast& ) \
{ \
fc::throw_bad_enum_cast( s, BOOST_PP_STRINGIZE(ENUM) ); \
} \
Expand Down
4 changes: 2 additions & 2 deletions include/fc/static_variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ static const std::array<typename Visitor::result_type(*)(Visitor&,Data),L>
init_wrappers( Visitor& v, Data d, typename Visitor::result_type(**funcs)(Visitor&,Data) = 0 )
{
std::array<typename Visitor::result_type(*)(Visitor&,Data),L> result{};
if( !funcs ) funcs = result.begin();
if( !funcs ) funcs = result.data();
*funcs++ = [] ( Visitor& v, Data d ) { return v( *reinterpret_cast<T*>( d ) ); };
init_wrappers<L,Visitor,Data,Types...>( v, d, funcs );
return result;
Expand All @@ -240,7 +240,7 @@ static const std::array<typename Visitor::result_type(*)(Visitor&,Data),L>
init_const_wrappers( Visitor& v, Data d, typename Visitor::result_type(**funcs)(Visitor&,Data) = 0 )
{
std::array<typename Visitor::result_type(*)(Visitor&,Data),L> result{};
if( !funcs ) funcs = result.begin();
if( !funcs ) funcs = result.data();
*funcs++ = [] ( Visitor& v, Data d ) { return v( *reinterpret_cast<const T*>( d ) ); };
init_const_wrappers<L,Visitor,Data,Types...>( v, d, funcs );
return result;
Expand Down
28 changes: 14 additions & 14 deletions src/crypto/elliptic_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ namespace fc { namespace ecc {
static chr37 _derive_message( unsigned char first, const unsigned char* key32, int i )
{
chr37 result;
unsigned char* dest = result.begin();
unsigned char* dest = result.data();
*dest++ = first;
memcpy( dest, key32, 32 ); dest += 32;
_put( &dest, i );
Expand All @@ -64,7 +64,7 @@ namespace fc { namespace ecc {

chr37 _derive_message( const public_key_data& key, int i )
{
return _derive_message( *key.begin(), key.begin() + 1, i );
return _derive_message( *key.data(), key.data() + 1, i );
}

static chr37 _derive_message( const private_key_secret& key, int i )
Expand Down Expand Up @@ -142,9 +142,9 @@ namespace fc { namespace ecc {
sha256 check = sha256::hash((char*) key.data(), sizeof(key));
static_assert(sizeof(key) + 4 == 37, "Elliptic public key size (or its hash) is incorrect");
detail::chr37 data;
memcpy(data.data(), key.begin(), key.size());
memcpy(data.begin() + key.size(), (const char*)check._hash, 4);
return fc::to_base58((char*) data.begin(), data.size());
memcpy(data.data(), key.data(), key.size());
memcpy(data.data() + key.size(), (const char*)check._hash, 4);
return fc::to_base58((char*) data.data(), data.size());
}

public_key public_key::from_base58( const std::string& b58 )
Expand All @@ -163,7 +163,7 @@ namespace fc { namespace ecc {
unsigned int public_key::fingerprint() const
{
public_key_data key = serialize();
ripemd160 hash = ripemd160::hash( sha256::hash( (char*) key.begin(), key.size() ) );
ripemd160 hash = ripemd160::hash( sha256::hash( (char*) key.data(), key.size() ) );
unsigned char* fp = (unsigned char*) hash._hash;
return (fp[0] << 24) | (fp[1] << 16) | (fp[2] << 8) | fp[3];
}
Expand Down Expand Up @@ -233,8 +233,8 @@ namespace fc { namespace ecc {
{
size_t buf_len = key.size() + 4;
char *buffer = (char*)alloca(buf_len);
memcpy( buffer, key.begin(), key.size() );
fc::sha256 double_hash = fc::sha256::hash( fc::sha256::hash( (char*) key.begin(), key.size() ));
memcpy( buffer, key.data(), key.size() );
fc::sha256 double_hash = fc::sha256::hash( fc::sha256::hash( (char*) key.data(), key.size() ));
memcpy( buffer + key.size(), double_hash.data(), 4 );
return fc::to_base58( buffer, buf_len );
}
Expand All @@ -260,14 +260,14 @@ namespace fc { namespace ecc {
extended_key_data extended_public_key::serialize_extended() const
{
extended_key_data result;
unsigned char* dest = (unsigned char*) result.begin();
unsigned char* dest = (unsigned char*) result.data();
detail::_put( &dest, BTC_EXT_PUB_MAGIC );
*dest++ = depth;
detail::_put( &dest, parent_fp );
detail::_put( &dest, child_num );
memcpy( dest, c.data(), c.data_size() ); dest += 32;
public_key_data key = serialize();
memcpy( dest, key.begin(), key.size() );
memcpy( dest, key.data(), key.size() );
return result;
}

Expand All @@ -293,7 +293,7 @@ namespace fc { namespace ecc {
fc::sha256 chain;
memcpy( chain.data(), ptr, chain.data_size() ); ptr += chain.data_size();
public_key_data key;
memcpy( key.begin(), ptr, key.size() );
memcpy( key.data(), ptr, key.size() );
return extended_public_key( key, chain, cn, fp, d );
}

Expand All @@ -311,7 +311,7 @@ namespace fc { namespace ecc {
{
const detail::chr37 data = detail::_derive_message( get_public_key().serialize(), i );
hmac_sha512 mac;
fc::sha512 l = mac.digest( c.data(), c.data_size(), (char*) data.begin(), data.size() );
fc::sha512 l = mac.digest( c.data(), c.data_size(), (char*) data.data(), data.size() );
return private_derive_rest( l, i );
}

Expand All @@ -320,14 +320,14 @@ namespace fc { namespace ecc {
hmac_sha512 mac;
private_key_secret key = get_secret();
const detail::chr37 data = detail::_derive_message( key, i );
fc::sha512 l = mac.digest( c.data(), c.data_size(), (char*) data.begin(), data.size() );
fc::sha512 l = mac.digest( c.data(), c.data_size(), (char*) data.data(), data.size() );
return private_derive_rest( l, i );
}

extended_key_data extended_private_key::serialize_extended() const
{
extended_key_data result;
unsigned char* dest = (unsigned char*) result.begin();
unsigned char* dest = (unsigned char*) result.data();
detail::_put( &dest, BTC_EXT_PRIV_MAGIC );
*dest++ = depth;
detail::_put( &dest, parent_fp );
Expand Down
9 changes: 6 additions & 3 deletions src/crypto/elliptic_impl_priv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ namespace fc { namespace ecc {
FC_ASSERT( my->_key != empty_priv );
public_key_data pub;
unsigned int pk_len;
FC_ASSERT( secp256k1_ec_pubkey_create( detail::_get_context(), (unsigned char*) pub.begin(), (int*) &pk_len, (unsigned char*) my->_key.data(), 1 ) );
FC_ASSERT( secp256k1_ec_pubkey_create( detail::_get_context(), pub.data(), (int*) &pk_len,
(unsigned char*) my->_key.data(), 1 ) );
FC_ASSERT( pk_len == pub.size() );
return public_key(pub);
}
Expand All @@ -93,9 +94,11 @@ namespace fc { namespace ecc {
unsigned int counter = 0;
do
{
FC_ASSERT( secp256k1_ecdsa_sign_compact( detail::_get_context(), (unsigned char*) digest.data(), (unsigned char*) result.begin() + 1, (unsigned char*) my->_key.data(), extended_nonce_function, &counter, &recid ));
FC_ASSERT( secp256k1_ecdsa_sign_compact( detail::_get_context(), (unsigned char*) digest.data(),
result.data() + 1, (unsigned char*) my->_key.data(),
extended_nonce_function, &counter, &recid ));
} while( require_canonical && !public_key::is_canonical( result ) );
result.begin()[0] = 27 + 4 + recid;
result.data()[0] = 27 + 4 + recid;
return result;
}

Expand Down
27 changes: 16 additions & 11 deletions src/crypto/elliptic_secp256k1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace fc { namespace ecc {
public_key_data _key;
};

typedef std::array<char,37> chr37;
typedef std::array<unsigned char,37> chr37;
chr37 _derive_message( const public_key_data& key, int i );
fc::sha256 _left( const fc::sha512& v );
fc::sha256 _right( const fc::sha512& v );
Expand All @@ -70,8 +70,9 @@ namespace fc { namespace ecc {
FC_ASSERT( my->_key != empty_priv );
FC_ASSERT( other.my->_key != empty_pub );
public_key_data pub(other.my->_key);
FC_ASSERT( secp256k1_ec_pubkey_tweak_mul( detail::_get_context(), pub.begin(), pub.size(), (unsigned char*) my->_key.data() ) );
return fc::sha512::hash( (char*) pub.begin() + 1, pub.size() - 1 );
FC_ASSERT( secp256k1_ec_pubkey_tweak_mul( detail::_get_context(), pub.data(), pub.size(),
(unsigned char*) my->_key.data() ) );
return fc::sha512::hash( (char*) pub.data() + 1, pub.size() - 1 );
}


Expand Down Expand Up @@ -104,8 +105,9 @@ namespace fc { namespace ecc {
{
FC_ASSERT( my->_key != empty_pub );
public_key_data new_key;
memcpy( new_key.begin(), my->_key.begin(), new_key.size() );
FC_ASSERT( secp256k1_ec_pubkey_tweak_add( detail::_get_context(), new_key.begin(), new_key.size(), (unsigned char*) digest.data() ) );
memcpy( new_key.data(), my->_key.data(), new_key.size() );
FC_ASSERT( secp256k1_ec_pubkey_tweak_add( detail::_get_context(), new_key.data(), new_key.size(),
(unsigned char*) digest.data() ) );
return public_key( new_key );
}

Expand All @@ -126,8 +128,8 @@ namespace fc { namespace ecc {
FC_ASSERT( my->_key != empty_pub );
public_key_point_data dat;
unsigned int pk_len = my->_key.size();
memcpy( dat.begin(), my->_key.begin(), pk_len );
FC_ASSERT( secp256k1_ec_pubkey_decompress( detail::_get_context(), dat.begin(), (int*) &pk_len ) );
memcpy( dat.data(), my->_key.data(), pk_len );
FC_ASSERT( secp256k1_ec_pubkey_decompress( detail::_get_context(), dat.data(), (int*) &pk_len ) );
FC_ASSERT( pk_len == dat.size() );
return dat;
}
Expand All @@ -142,7 +144,7 @@ namespace fc { namespace ecc {
key = o2i_ECPublicKey( &key, &front, sizeof(dat) );
FC_ASSERT( key );
EC_KEY_set_conv_form( key, POINT_CONVERSION_COMPRESSED );
unsigned char* buffer = my->_key.begin();
unsigned char* buffer = my->_key.data();
i2o_ECPublicKey( key, &buffer ); // FIXME: questionable memory handling
EC_KEY_free( key );
}
Expand All @@ -165,7 +167,9 @@ namespace fc { namespace ecc {
}

unsigned int pk_len;
FC_ASSERT( secp256k1_ecdsa_recover_compact( detail::_get_context(), (unsigned char*) digest.data(), c.begin() + 1, my->_key.begin(), (int*) &pk_len, 1, (*c.begin() - 27) & 3 ) );
FC_ASSERT( secp256k1_ecdsa_recover_compact( detail::_get_context(), (unsigned char*) digest.data(),
c.data() + 1, my->_key.data(), (int*) &pk_len, 1,
(*c.data() - 27) & 3 ) );
FC_ASSERT( pk_len == my->_key.size() );
}

Expand All @@ -178,10 +182,11 @@ namespace fc { namespace ecc {
hmac_sha512 mac;
public_key_data key = serialize();
const detail::chr37 data = detail::_derive_message( key, i );
fc::sha512 l = mac.digest( c.data(), c.data_size(), data.begin(), data.size() );
fc::sha512 l = mac.digest( c.data(), c.data_size(), (const char*) data.data(), data.size() );
fc::sha256 left = detail::_left(l);
FC_ASSERT( left < detail::get_curve_order() );
FC_ASSERT( secp256k1_ec_pubkey_tweak_add( detail::_get_context(), key.begin(), key.size(), (unsigned char*) left.data() ) > 0 );
FC_ASSERT( secp256k1_ec_pubkey_tweak_add( detail::_get_context(), key.data(), key.size(),
(unsigned char*) left.data() ) > 0 );
// FIXME: check validity - if left + key == infinity then invalid
extended_public_key result( key, detail::_right(l), i, fingerprint(), depth + 1 );
return result;
Expand Down
18 changes: 18 additions & 0 deletions src/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include <fc/reflect/variant.hpp>
#include <algorithm>

#ifdef __APPLE__
#include <boost/multiprecision/integer.hpp>
#endif

namespace fc
{

Expand Down Expand Up @@ -675,12 +679,26 @@ void from_variant( const variant& var, std::vector<char>& vo, uint32_t max_depth

void to_variant( const uint128_t& var, variant& vo, uint32_t max_depth )
{
#ifdef __APPLE__
boost::multiprecision::uint128_t helper = uint128_hi64( var );
helper <<= 64;
helper += uint128_lo64( var );
vo = boost::lexical_cast<std::string>( helper );
#else
vo = boost::lexical_cast<std::string>( var );
#endif
}

void from_variant( const variant& var, uint128_t& vo, uint32_t max_depth )
{
#ifdef __APPLE__
boost::multiprecision::uint128_t helper = boost::lexical_cast<boost::multiprecision::uint128_t>( var.as_string() );
vo = static_cast<uint64_t>( helper >> 64 );
vo <<= 64;
vo += static_cast<uint64_t>( helper & 0xffffffffffffffffULL );
#else
vo = boost::lexical_cast<uint128_t>( var.as_string() );
#endif
}

#ifdef __APPLE__
Expand Down
8 changes: 4 additions & 4 deletions tests/crypto/ecc_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ static void interop_do(const char * const data, size_t len) {
}

static void interop_do(const fc::ecc::public_key_data &data) {
interop_do((char*) data.begin(), data.size());
interop_do((char*) data.data(), data.size());
}

static void interop_do(const fc::ecc::private_key_secret &data) {
interop_do(data.data(), 32);
}

static void interop_do(const fc::ecc::public_key_point_data &data) {
interop_do((char*) data.begin(), data.size());
interop_do((char*) data.data(), data.size());
}

static void interop_do(const std::string &data) {
Expand All @@ -44,11 +44,11 @@ static void interop_do(const fc::sha512 &data) {

static void interop_do(fc::ecc::compact_signature &data) {
if (write_mode) {
interop_data.write((char*) data.begin(), data.size());
interop_data.write((char*) data.data(), data.size());
return;
}

interop_data.read((char*) data.begin(), data.size());
interop_data.read((char*) data.data(), data.size());
}

static void interop_file(const char * const name) {
Expand Down
10 changes: 5 additions & 5 deletions tests/hmac_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ static void run_test( const std::string& key, const std::string& data, const std
{

std::array<char,N> key_arr;
BOOST_CHECK_EQUAL( fc::from_hex( key, key_arr.begin(), key_arr.size() ), N );
BOOST_CHECK_EQUAL( fc::from_hex( key, key_arr.data(), key_arr.size() ), N );
std::array<char,M> data_arr;
BOOST_CHECK_EQUAL( fc::from_hex( data, data_arr.begin(), data_arr.size() ), M );
BOOST_CHECK_EQUAL( fc::from_hex( data, data_arr.data(), data_arr.size() ), M );

BOOST_CHECK_EQUAL( mac_224.digest( key_arr.begin(), N, data_arr.begin(), M ).str(), expect_224 );
BOOST_CHECK_EQUAL( mac_256.digest( key_arr.begin(), N, data_arr.begin(), M ).str(), expect_256 );
BOOST_CHECK_EQUAL( mac_512.digest( key_arr.begin(), N, data_arr.begin(), M ).str(), expect_512 );
BOOST_CHECK_EQUAL( mac_224.digest( key_arr.data(), N, data_arr.data(), M ).str(), expect_224 );
BOOST_CHECK_EQUAL( mac_256.digest( key_arr.data(), N, data_arr.data(), M ).str(), expect_256 );
BOOST_CHECK_EQUAL( mac_512.digest( key_arr.data(), N, data_arr.data(), M ).str(), expect_512 );
}

BOOST_AUTO_TEST_CASE(hmac_test_1)
Expand Down