Rebase + upgrade to latest libsecp256k1 API

This commit is contained in:
Peter Conrad 2015-05-07 15:59:21 +02:00
parent a164a55c86
commit d9f6b7a526
4 changed files with 17 additions and 15 deletions

View file

@ -7,6 +7,8 @@
namespace fc { namespace ecc { namespace detail { namespace fc { namespace ecc { namespace detail {
const secp256k1_context_t* _get_context();
void _init_lib(); void _init_lib();
class private_key_impl class private_key_impl

View file

@ -72,7 +72,7 @@ namespace fc { namespace ecc {
FC_ASSERT( my->_key != empty_priv ); FC_ASSERT( my->_key != empty_priv );
public_key_data pub; public_key_data pub;
unsigned int pk_len; unsigned int pk_len;
FC_ASSERT( secp256k1_ec_pubkey_create( (unsigned char*) pub.begin(), (int*) &pk_len, (unsigned char*) my->_key.data(), 1 ) ); FC_ASSERT( secp256k1_ec_pubkey_create( detail::_get_context(), (unsigned char*) pub.begin(), (int*) &pk_len, (unsigned char*) my->_key.data(), 1 ) );
FC_ASSERT( pk_len == pub.size() ); FC_ASSERT( pk_len == pub.size() );
return public_key(pub); return public_key(pub);
} }
@ -93,7 +93,7 @@ namespace fc { namespace ecc {
unsigned int counter = 0; unsigned int counter = 0;
do do
{ {
FC_ASSERT( secp256k1_ecdsa_sign_compact( (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(), (unsigned char*) result.begin() + 1, (unsigned char*) my->_key.data(), extended_nonce_function, &counter, &recid ));
} while( !public_key::is_canonical( result ) ); } while( !public_key::is_canonical( result ) );
result.begin()[0] = 27 + 4 + recid; result.begin()[0] = 27 + 4 + recid;
return result; return result;

View file

@ -16,13 +16,13 @@
namespace fc { namespace ecc { namespace fc { namespace ecc {
namespace detail namespace detail
{ {
static int init_secp256k1() { const secp256k1_context_t* _get_context() {
secp256k1_start(SECP256K1_START_VERIFY | SECP256K1_START_SIGN); static secp256k1_context_t* ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN);
return 1; return ctx;
} }
void _init_lib() { void _init_lib() {
static int init_s = init_secp256k1(); static const secp256k1_context_t* ctx = _get_context();
static int init_o = init_openssl(); static int init_o = init_openssl();
} }
} }
@ -33,7 +33,7 @@ namespace fc { namespace ecc {
FC_ASSERT( my->_key != empty_priv ); FC_ASSERT( my->_key != empty_priv );
FC_ASSERT( other.my->_key != nullptr ); FC_ASSERT( other.my->_key != nullptr );
public_key_data pub(other.serialize()); public_key_data pub(other.serialize());
FC_ASSERT( secp256k1_ec_pubkey_tweak_mul( (unsigned char*) pub.begin(), pub.size(), (unsigned char*) my->_key.data() ) ); FC_ASSERT( secp256k1_ec_pubkey_tweak_mul( detail::_get_context(), (unsigned char*) pub.begin(), pub.size(), (unsigned char*) my->_key.data() ) );
return fc::sha512::hash( pub.begin() + 1, pub.size() - 1 ); return fc::sha512::hash( pub.begin() + 1, pub.size() - 1 );
} }

View file

@ -15,13 +15,13 @@
namespace fc { namespace ecc { namespace fc { namespace ecc {
namespace detail namespace detail
{ {
static int init_secp256k1() { const secp256k1_context_t* _get_context() {
secp256k1_start(SECP256K1_START_VERIFY | SECP256K1_START_SIGN); static secp256k1_context_t* ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN);
return 1; return ctx;
} }
void _init_lib() { void _init_lib() {
static int init_s = init_secp256k1(); static const secp256k1_context_t* ctx = _get_context();
static int init_o = init_openssl(); static int init_o = init_openssl();
} }
@ -51,7 +51,7 @@ namespace fc { namespace ecc {
FC_ASSERT( my->_key != empty_priv ); FC_ASSERT( my->_key != empty_priv );
FC_ASSERT( other.my->_key != empty_pub ); FC_ASSERT( other.my->_key != empty_pub );
public_key_data pub(other.my->_key); public_key_data pub(other.my->_key);
FC_ASSERT( secp256k1_ec_pubkey_tweak_mul( (unsigned char*) pub.begin(), pub.size(), (unsigned char*) my->_key.data() ) ); FC_ASSERT( secp256k1_ec_pubkey_tweak_mul( detail::_get_context(), (unsigned char*) pub.begin(), pub.size(), (unsigned char*) my->_key.data() ) );
return fc::sha512::hash( pub.begin() + 1, pub.size() - 1 ); return fc::sha512::hash( pub.begin() + 1, pub.size() - 1 );
} }
@ -86,7 +86,7 @@ namespace fc { namespace ecc {
FC_ASSERT( my->_key != empty_pub ); FC_ASSERT( my->_key != empty_pub );
public_key_data new_key; public_key_data new_key;
memcpy( new_key.begin(), my->_key.begin(), new_key.size() ); memcpy( new_key.begin(), my->_key.begin(), new_key.size() );
FC_ASSERT( secp256k1_ec_pubkey_tweak_add( (unsigned char*) new_key.begin(), new_key.size(), (unsigned char*) digest.data() ) ); FC_ASSERT( secp256k1_ec_pubkey_tweak_add( detail::_get_context(), (unsigned char*) new_key.begin(), new_key.size(), (unsigned char*) digest.data() ) );
return public_key( new_key ); return public_key( new_key );
} }
@ -108,7 +108,7 @@ namespace fc { namespace ecc {
public_key_point_data dat; public_key_point_data dat;
unsigned int pk_len = my->_key.size(); unsigned int pk_len = my->_key.size();
memcpy( dat.begin(), my->_key.begin(), pk_len ); memcpy( dat.begin(), my->_key.begin(), pk_len );
FC_ASSERT( secp256k1_ec_pubkey_decompress( (unsigned char *) dat.begin(), (int*) &pk_len ) ); FC_ASSERT( secp256k1_ec_pubkey_decompress( detail::_get_context(), (unsigned char *) dat.begin(), (int*) &pk_len ) );
FC_ASSERT( pk_len == dat.size() ); FC_ASSERT( pk_len == dat.size() );
return dat; return dat;
} }
@ -146,7 +146,7 @@ namespace fc { namespace ecc {
} }
unsigned int pk_len; unsigned int pk_len;
FC_ASSERT( secp256k1_ecdsa_recover_compact( (unsigned char*) digest.data(), (unsigned char*) c.begin() + 1, (unsigned char*) my->_key.begin(), (int*) &pk_len, 1, (*c.begin() - 27) & 3 ) ); FC_ASSERT( secp256k1_ecdsa_recover_compact( detail::_get_context(), (unsigned char*) digest.data(), (unsigned char*) c.begin() + 1, (unsigned char*) my->_key.begin(), (int*) &pk_len, 1, (*c.begin() - 27) & 3 ) );
FC_ASSERT( pk_len == my->_key.size() ); FC_ASSERT( pk_len == my->_key.size() );
} }
} } } }