Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
fc::em::private_key Class Reference

an elliptic curve private key. More...

#include <elliptic_em.hpp>

Public Member Functions

 private_key ()
 
 private_key (private_key &&pk)
 
 private_key (const private_key &pk)
 
 ~private_key ()
 
private_keyoperator= (private_key &&pk)
 
private_keyoperator= (const private_key &pk)
 
private_key child (const fc::sha256 &offset) const
 
private_key_secret get_secret () const
 
 operator private_key_secret () const
 
fc::sha512 get_shared_secret (const public_key &pub) const
 
compact_signature sign_compact (const fc::sha256 &digest, bool require_canonical=true) const
 
public_key get_public_key () const
 
unsigned int fingerprint () const
 

Static Public Member Functions

static private_key generate ()
 
static private_key regenerate (const fc::sha256 &secret)
 
static private_key generate_from_seed (const fc::sha256 &seed, const fc::sha256 &offset=fc::sha256())
 

Friends

bool operator== (const private_key &a, const private_key &b)
 
bool operator!= (const private_key &a, const private_key &b)
 
bool operator< (const private_key &a, const private_key &b)
 

Detailed Description

Definition at line 90 of file elliptic_em.hpp.

Constructor & Destructor Documentation

◆ private_key() [1/3]

fc::em::private_key::private_key ( )

Definition at line 33 of file elliptic_em_impl_priv.cpp.

33{}
Here is the caller graph for this function:

◆ private_key() [2/3]

fc::em::private_key::private_key ( private_key && pk)

Definition at line 37 of file elliptic_em_impl_priv.cpp.

37: my( std::move( pk.my ) ) {}

◆ private_key() [3/3]

fc::em::private_key::private_key ( const private_key & pk)

Definition at line 35 of file elliptic_em_impl_priv.cpp.

35: my( pk.my ) {}

◆ ~private_key()

fc::em::private_key::~private_key ( )

Definition at line 39 of file elliptic_em_impl_priv.cpp.

39{}

Member Function Documentation

◆ child()

private_key fc::em::private_key::child ( const fc::sha256 & offset) const

Definition at line 114 of file elliptic_em_common.cpp.

115 {
118 fc::raw::pack( enc, offset );
119 return generate_from_seed( get_secret(), enc.result() );
120 }
static private_key generate_from_seed(const fc::sha256 &seed, const fc::sha256 &offset=fc::sha256())
public_key get_public_key() const
private_key_secret get_secret() const
void pack(Stream &s, const std::deque< T > &value)
Definition raw.hpp:531
Here is the call graph for this function:

◆ fingerprint()

unsigned int fc::em::private_key::fingerprint ( ) const
inline

Definition at line 142 of file elliptic_em.hpp.

142{ return get_public_key().fingerprint(); }
unsigned int fingerprint() const
Here is the call graph for this function:

◆ generate()

private_key fc::em::private_key::generate ( )
static

Definition at line 201 of file elliptic_em_common.cpp.

202 {
203 EC_KEY* k = EC_KEY_new_by_curve_name( NID_secp256k1 );
204 if( !k ) FC_THROW_EXCEPTION( exception, "Unable to generate EC key" );
205 if( !EC_KEY_generate_key( k ) )
206 {
207 FC_THROW_EXCEPTION( exception, "ecc key generation error" );
208
209 }
210
211 return private_key( k );
212 }
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ generate_from_seed()

private_key fc::em::private_key::generate_from_seed ( const fc::sha256 & seed,
const fc::sha256 & offset = fc::sha256() )
static

This method of generation enables creating a new private key in a deterministic manner relative to an initial seed. A public_key created from the seed can be multiplied by the offset to calculate the new public key without having to know the private key.

Definition at line 160 of file elliptic_em_common.cpp.

161 {
162 ssl_bignum z;
163 BN_bin2bn((unsigned char*)&offset, sizeof(offset), z);
164
165 ec_group group(EC_GROUP_new_by_curve_name(NID_secp256k1));
166 bn_ctx ctx(BN_CTX_new());
167 ssl_bignum order;
168 EC_GROUP_get_order(group, order, ctx);
169
170 // secexp = (seed + z) % order
171 ssl_bignum secexp;
172 BN_bin2bn((unsigned char*)&seed, sizeof(seed), secexp);
173 BN_add(secexp, secexp, z);
174 BN_mod(secexp, secexp, order, ctx);
175
176 fc::sha256 secret;
177 FC_ASSERT(BN_num_bytes(secexp) <= int64_t(sizeof(secret)));
178 auto shift = sizeof(secret) - BN_num_bytes(secexp);
179 BN_bn2bin(secexp, ((unsigned char*)&secret)+shift);
180 return regenerate( secret );
181 }
static private_key regenerate(const fc::sha256 &secret)
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
signed __int64 int64_t
Definition stdint.h:135
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_public_key()

public_key fc::em::private_key::get_public_key ( ) const

Definition at line 71 of file elliptic_em_impl_priv.cpp.

72 {
73 FC_ASSERT( my->_key != empty_priv );
75 size_t pub_len = sizeof(pub);
76 secp256k1_pubkey secp_pub;
77 FC_ASSERT( secp256k1_ec_pubkey_create( detail::_get_context(), &secp_pub, (unsigned char*) my->_key.data() ) );
78 secp256k1_ec_pubkey_serialize( detail::_get_context(), (unsigned char*)&pub, &pub_len, &secp_pub, SECP256K1_EC_COMPRESSED );
79 FC_ASSERT( pub_len == pub.size() );
80 return public_key(pub);
81 }
const secp256k1_context * _get_context()
fc::array< char, 33 > public_key_data
SECP256K1_API int secp256k1_ec_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey *pubkey, unsigned int flags) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Definition secp256k1.c:246
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Definition secp256k1.c:551
#define SECP256K1_EC_COMPRESSED
Definition secp256k1.h:201
bool pub
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_secret()

fc::sha256 fc::em::private_key::get_secret ( ) const

Definition at line 60 of file elliptic_em_impl_priv.cpp.

61 {
62 return my->_key;
63 }
Here is the caller graph for this function:

◆ get_shared_secret()

fc::sha512 fc::em::private_key::get_shared_secret ( const public_key & pub) const

Given a public key, calculatse a 512 bit shared secret between that key and this private key.

Definition at line 67 of file elliptic_em.cpp.

68 {
69 static const private_key_secret empty_priv;
70 FC_ASSERT( my->_key != empty_priv );
71 FC_ASSERT( other.my->_key != empty_pub );
72 secp256k1_pubkey secp_pubkey;
73 FC_ASSERT( secp256k1_ec_pubkey_parse( detail::_get_context(), &secp_pubkey, (unsigned char*)other.serialize().data, other.serialize().size() ) );
74 FC_ASSERT( secp256k1_ec_pubkey_tweak_mul( detail::_get_context(), &secp_pubkey, (unsigned char*) my->_key.data() ) );
75 public_key_data serialized_result;
76 size_t serialized_result_sz = sizeof(serialized_result);
77 secp256k1_ec_pubkey_serialize(detail::_get_context(), (unsigned char*)&serialized_result.data, &serialized_result_sz, &secp_pubkey, SECP256K1_EC_COMPRESSED );
78 FC_ASSERT( serialized_result_sz == sizeof(serialized_result) );
79 return fc::sha512::hash( serialized_result.begin() + 1, serialized_result.size() - 1 );
80 }
static sha512 hash(const char *d, uint32_t dlen)
Definition sha512.cpp:37
fc::sha256 private_key_secret
Definition elliptic.hpp:23
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *input, size_t inputlen) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Definition secp256k1.c:228
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Definition secp256k1.c:683
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator private_key_secret()

fc::em::private_key::operator private_key_secret ( ) const
inline

Definition at line 115 of file elliptic_em.hpp.

115{ return get_secret(); }
Here is the call graph for this function:

◆ operator=() [1/2]

private_key & fc::em::private_key::operator= ( const private_key & pk)

Definition at line 47 of file elliptic_em_impl_priv.cpp.

48 {
49 my = pk.my;
50 return *this;
51 }

◆ operator=() [2/2]

private_key & fc::em::private_key::operator= ( private_key && pk)

Definition at line 41 of file elliptic_em_impl_priv.cpp.

42 {
43 my = std::move( pk.my );
44 return *this;
45 }

◆ regenerate()

private_key fc::em::private_key::regenerate ( const fc::sha256 & secret)
static

Definition at line 53 of file elliptic_em_impl_priv.cpp.

54 {
56 self.my->_key = secret;
57 return self;
58 }
@ self
the connection is to itself
Definition protocol.hpp:48
Here is the caller graph for this function:

◆ sign_compact()

compact_signature fc::em::private_key::sign_compact ( const fc::sha256 & digest,
bool require_canonical = true ) const

Definition at line 91 of file elliptic_em_impl_priv.cpp.

92 {
93 FC_ASSERT( my->_key != empty_priv );
94 compact_signature result;
96 int recid;
97 unsigned int counter = 0;
98 do
99 {
100 FC_ASSERT( secp256k1_ecdsa_sign_recoverable( detail::_get_context(), &secp_sig, (unsigned char*) digest.data(), (unsigned char*) my->_key.data(), extended_nonce_function, &counter ));
102 } while( require_canonical && !public_key::is_canonical( result ) );
103
104 result.begin()[0] = 27 + 4 + recid;
105 return result;
106 }
const char * data() const
Definition sha256.cpp:31
fc::array< unsigned char, 65 > compact_signature
fc::sha256 digest(const T &value)
Definition digest.hpp:9
SECP256K1_API int secp256k1_ecdsa_recoverable_signature_serialize_compact(const secp256k1_context *ctx, unsigned char *output64, int *recid, const secp256k1_ecdsa_recoverable_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Definition main_impl.h:60
SECP256K1_API int secp256k1_ecdsa_sign_recoverable(const secp256k1_context *ctx, secp256k1_ecdsa_recoverable_signature *sig, const unsigned char *msghash32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Definition main_impl.h:123
Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ operator!=

bool operator!= ( const private_key & a,
const private_key & b )
friend

Definition at line 133 of file elliptic_em.hpp.

134 {
135 return a.get_secret() != b.get_secret();
136 }
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181

◆ operator<

bool operator< ( const private_key & a,
const private_key & b )
friend

Definition at line 137 of file elliptic_em.hpp.

138 {
139 return a.get_secret() < b.get_secret();
140 }

◆ operator==

bool operator== ( const private_key & a,
const private_key & b )
friend

Definition at line 129 of file elliptic_em.hpp.

130 {
131 return a.get_secret() == b.get_secret();
132 }

The documentation for this class was generated from the following files: