14 #define BTC_EXT_PUB_MAGIC (0x0488B21E)
15 #define BTC_EXT_PRIV_MAGIC (0x0488ADE4)
17 namespace fc {
namespace ecc {
25 memcpy( result.
data(), v.
data(), 32 );
32 memcpy( result.
data(), v.
data() + 32, 32 );
36 static void _put(
unsigned char** dest,
unsigned int i)
38 *(*dest)++ = (i >> 24) & 0xff;
39 *(*dest)++ = (i >> 16) & 0xff;
40 *(*dest)++ = (i >> 8) & 0xff;
41 *(*dest)++ = i & 0xff;
44 static unsigned int _get(
unsigned char** src )
46 unsigned int result = *(*src)++ << 24;
47 result |= *(*src)++ << 16;
48 result |= *(*src)++ << 8;
53 static chr37 _derive_message(
unsigned char first,
const unsigned char* key32,
int i )
56 unsigned char* dest = result.data();
58 memcpy( dest, key32, 32 ); dest += 32;
65 return _derive_message( *key.data(), key.data() + 1, i );
70 return _derive_message( 0, (
unsigned char*) key.
data(), i );
75 static const ec_group secp256k1( EC_GROUP_new_by_curve_name( NID_secp256k1 ) );
82 bn_ctx ctx(BN_CTX_new());
84 FC_ASSERT( EC_GROUP_get_order( group, order, ctx ) );
100 bn_ctx ctx(BN_CTX_new());
102 FC_ASSERT( EC_GROUP_get_order( group, order, ctx ) );
103 BN_rshift1( order, order );
141 static_assert(
sizeof(key) + 4 == 37,
"Elliptic public key size (or its hash) is incorrect");
143 memcpy(data.data(), key.data(), key.size());
144 memcpy(data.data() + key.size(), (
const char*)check.
_hash, 4);
156 FC_ASSERT( memcmp( (
char*)check.
_hash, data.data() + key.size(), 4 ) == 0 );
157 memcpy( (
char*)key.data(), data.data(), key.size() );
158 return from_key_data(key);
165 unsigned char* fp = (
unsigned char*) hash.
_hash;
166 return (fp[0] << 24) | (fp[1] << 16) | (fp[2] << 8) | fp[3];
170 return !(c[1] & 0x80)
171 && !(c[1] == 0 && !(c[2] & 0x80))
173 && !(c[33] == 0 && !(c[34] & 0x80));
179 BN_bin2bn((
unsigned char*)&offset,
sizeof(offset), z);
181 ec_group group(EC_GROUP_new_by_curve_name(NID_secp256k1));
182 bn_ctx ctx(BN_CTX_new());
184 EC_GROUP_get_order(group, order, ctx);
188 BN_bin2bn((
unsigned char*)&seed,
sizeof(seed), secexp);
189 BN_add(secexp, secexp, z);
190 BN_mod(secexp, secexp, order, ctx);
193 FC_ASSERT(BN_num_bytes(secexp) <= int64_t(
sizeof(secret)));
194 auto shift =
sizeof(secret) - BN_num_bytes(secexp);
195 BN_bn2bin(secexp, ((
unsigned char*)&secret)+shift);
207 const BIGNUM* bn = EC_KEY_get0_private_key(k);
212 int nbytes = BN_num_bytes(bn);
213 BN_bn2bin(bn, &((
unsigned char*)&sec)[32-nbytes] );
219 EC_KEY* k = EC_KEY_new_by_curve_name( NID_secp256k1 );
221 if( !EC_KEY_generate_key( k ) )
232 char buffer[std::tuple_size<extended_key_data>::value + 4];
233 memcpy( buffer, key.data(), key.size() );
235 memcpy( buffer + key.size(), double_hash.
data(), 4 );
239 static void _parse_extended_data(
unsigned char* buffer, std::string base58 )
241 memset( buffer, 0, 78 );
244 for (
char c : decoded )
246 if ( i >= 78 || i > decoded.size() - 4 ) {
break; }
253 FC_ASSERT( !(i&0x80000000),
"Can't derive hardened public key!" );
260 unsigned char* dest = (
unsigned char*) result.data();
263 detail::_put( &dest, parent_fp );
264 detail::_put( &dest, child_num );
265 memcpy( dest, c.data(), c.data_size() ); dest += 32;
267 memcpy( dest, key.data(), key.size() );
283 unsigned char buffer[78];
284 unsigned char* ptr = buffer;
285 _parse_extended_data( buffer, base58 );
288 int fp = detail::_get( &ptr );
289 int cn = detail::_get( &ptr );
293 memcpy( key.data(), ptr, key.size() );
311 fc::sha512 l = mac.
digest( c.data(), c.data_size(), (
char*) data.data(), data.size() );
312 return private_derive_rest( l, i );
319 const detail::chr37 data = detail::_derive_message( key, i );
320 fc::sha512 l = mac.
digest( c.data(), c.data_size(), (
char*) data.data(), data.size() );
321 return private_derive_rest( l, i );
327 unsigned char* dest = (
unsigned char*) result.data();
330 detail::_put( &dest, parent_fp );
331 detail::_put( &dest, child_num );
332 memcpy( dest, c.data(), c.data_size() ); dest += 32;
351 unsigned char buffer[78];
352 unsigned char* ptr = buffer;
353 _parse_extended_data( buffer, base58 );
356 int fp = detail::_get( &ptr );
357 int cn = detail::_get( &ptr );