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

contains only the public point of an elliptic curve key. More...

#include <elliptic_em.hpp>

Public Member Functions

 public_key ()
 
 public_key (const public_key &k)
 
 ~public_key ()
 
public_key_data serialize () const
 
public_key_point_data serialize_ecc_point () const
 
 operator public_key_data () const
 
 public_key (const public_key_data &v)
 
 public_key (const public_key_point_data &v)
 
 public_key (const compact_signature &c, const fc::sha256 &digest, bool check_canonical=true)
 
 public_key (const compact_signature &c, const unsigned char *digest, bool check_canonical=true)
 
public_key child (const fc::sha256 &offset) const
 
bool valid () const
 
 public_key (public_key &&pk)
 
public_keyoperator= (public_key &&pk)
 
public_keyoperator= (const public_key &pk)
 
std::string to_base58 () const
 Allows to convert current public key object into base58 number.
 
unsigned int fingerprint () const
 

Static Public Member Functions

static std::string to_base58 (const public_key_data &key)
 
static public_key from_base58 (const std::string &b58)
 

Friends

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

Detailed Description

Definition at line 37 of file elliptic_em.hpp.

Constructor & Destructor Documentation

◆ public_key() [1/7]

fc::em::public_key::public_key ( )

Definition at line 83 of file elliptic_em.cpp.

83{}

◆ public_key() [2/7]

fc::em::public_key::public_key ( const public_key & k)

Definition at line 85 of file elliptic_em.cpp.

85: my( pk.my ) {}

◆ ~public_key()

fc::em::public_key::~public_key ( )

Definition at line 89 of file elliptic_em.cpp.

89{}

◆ public_key() [3/7]

fc::em::public_key::public_key ( const public_key_data & v)

Definition at line 136 of file elliptic_em.cpp.

137 {
138 my->_key = dat;
139 }

◆ public_key() [4/7]

fc::em::public_key::public_key ( const public_key_point_data & v)

Definition at line 120 of file elliptic_em.cpp.

121 {
122 const char* front = &dat.data[0];
123 if( *front == 0 ){}
124 else
125 {
126 EC_KEY *key = EC_KEY_new_by_curve_name( NID_secp256k1 );
127 key = o2i_ECPublicKey( &key, (const unsigned char**)&front, sizeof(dat) );
128 FC_ASSERT( key );
129 EC_KEY_set_conv_form( key, POINT_CONVERSION_COMPRESSED );
130 unsigned char* buffer = (unsigned char*) my->_key.begin();
131 i2o_ECPublicKey( key, &buffer ); // FIXME: questionable memory handling
132 EC_KEY_free( key );
133 }
134 }
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.

◆ public_key() [5/7]

fc::em::public_key::public_key ( const compact_signature & c,
const fc::sha256 & digest,
bool check_canonical = true )

Definition at line 141 of file elliptic_em.cpp.

142 {
143 int nV = c.data[0];
144 if (nV<27 || nV>=35)
145 FC_THROW_EXCEPTION( exception, "unable to reconstruct public key from signature" );
146
147 if( check_canonical )
148 {
149 FC_ASSERT( is_canonical( c ), "signature is not canonical" );
150 }
151
152 secp256k1_pubkey secp_pub;
154
155 FC_ASSERT( secp256k1_ecdsa_recoverable_signature_parse_compact( detail::_get_context(), &secp_sig, (unsigned char*)c.begin() + 1, (*c.begin() - 27) & 3) );
156 FC_ASSERT( secp256k1_ecdsa_recover( detail::_get_context(), &secp_pub, &secp_sig, (unsigned char*) digest.data() ) );
157
158 size_t serialized_result_sz = my->_key.size();
159 secp256k1_ec_pubkey_serialize( detail::_get_context(), (unsigned char*)&my->_key.data, &serialized_result_sz, &secp_pub, SECP256K1_EC_COMPRESSED );
160 FC_ASSERT( serialized_result_sz == my->_key.size() );
161 }
const char * data() const
Definition sha256.cpp:31
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
const secp256k1_context * _get_context()
fc::sha256 digest(const T &value)
Definition digest.hpp:9
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
#define SECP256K1_EC_COMPRESSED
Definition secp256k1.h:201
SECP256K1_API int secp256k1_ecdsa_recoverable_signature_parse_compact(const secp256k1_context *ctx, secp256k1_ecdsa_recoverable_signature *sig, const unsigned char *input64, int recid) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Definition main_impl.h:38
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_recover(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const secp256k1_ecdsa_recoverable_signature *sig, const unsigned char *msghash32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Definition main_impl.h:137
Here is the call graph for this function:

◆ public_key() [6/7]

fc::em::public_key::public_key ( const compact_signature & c,
const unsigned char * digest,
bool check_canonical = true )

Definition at line 163 of file elliptic_em.cpp.

163 {
164 int nV = c.data[0];
165 if (nV < 27 || nV >= 35) {
166 FC_THROW_EXCEPTION(exception, "unable to reconstruct public key from signature");
167 }
168
169 if (check_canonical) {
170 FC_ASSERT(is_canonical(c), "signature is not canonical");
171 }
172
173 // Declare the necessary secp256k1 variables for public key and signature
174 secp256k1_pubkey secp_pub;
176
177 // Parse the compact signature into a recoverable signature
180 &secp_sig,
181 (unsigned char*)c.begin() + 1,
182 (*c.begin() - 27) & 3));
183
184 // Recover the public key from the signature and digest
187 &secp_pub,
188 &secp_sig,
189 digest));
190
191 // Prepare to serialize the recovered public key
192 size_t serialized_result_sz = my->_key.size();
195 (unsigned char*)&my->_key.data,
196 &serialized_result_sz,
197 &secp_pub,
199
200 // Verify the serialized public key size
201 FC_ASSERT(serialized_result_sz == my->_key.size());
202 }
Here is the call graph for this function:

◆ public_key() [7/7]

fc::em::public_key::public_key ( public_key && pk)

Definition at line 87 of file elliptic_em.cpp.

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

Member Function Documentation

◆ child()

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

◆ fingerprint()

unsigned int fc::em::public_key::fingerprint ( ) const

Definition at line 145 of file elliptic_em_common.cpp.

146 {
148 ripemd160 hash = ripemd160::hash( sha256::hash( key.begin(), key.size() ) );
149 unsigned char* fp = (unsigned char*) hash._hash;
150 return (fp[0] << 24) | (fp[1] << 16) | (fp[2] << 8) | fp[3];
151 }
public_key_data serialize() const
static ripemd160 hash(const fc::sha512 &h)
Definition ripemd160.cpp:44
static sha256 hash(const char *d, uint32_t dlen)
Definition sha256.cpp:44
fc::array< char, 33 > public_key_data
constexpr const char ripemd160[]
Here is the call graph for this function:
Here is the caller graph for this function:

◆ from_base58()

public_key fc::em::public_key::from_base58 ( const std::string & b58)
static

Definition at line 132 of file elliptic_em_common.cpp.

133 {
134 array<char, 37> data;
135 size_t s = fc::from_base58(b58, (char*)&data, sizeof(data) );
136 FC_ASSERT( s == sizeof(data) );
137
139 uint32_t check = (uint32_t)sha256::hash(data.data, sizeof(key))._hash[0];
140 FC_ASSERT( memcmp( (char*)&check, data.data + sizeof(key), sizeof(check) ) == 0 );
141 memcpy( (char*)key.data, data.data, sizeof(key) );
142 return from_key_data(key);
143 }
uint64_t _hash[4]
Definition sha256.hpp:100
std::vector< char > from_base58(const std::string &base58_str)
Definition base58.cpp:628
unsigned int uint32_t
Definition stdint.h:126
uint8_t key[16]
Definition yubico_otp.c:41
char * s
memcpy((char *) pInfo->slotDescription, s, l)
Here is the call graph for this function:

◆ operator public_key_data()

fc::em::public_key::operator public_key_data ( ) const
inline

Definition at line 47 of file elliptic_em.hpp.

47{ return serialize(); }
Here is the call graph for this function:

◆ operator=() [1/2]

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

Definition at line 91 of file elliptic_em.cpp.

92 {
93 my = pk.my;
94 return *this;
95 }

◆ operator=() [2/2]

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

Definition at line 97 of file elliptic_em.cpp.

98 {
99 my = pk.my;
100 return *this;
101 }

◆ serialize()

public_key_data fc::em::public_key::serialize ( ) const

Definition at line 114 of file elliptic_em.cpp.

115 {
116 FC_ASSERT( my->_key != empty_pub );
117 return my->_key;
118 }
Here is the caller graph for this function:

◆ serialize_ecc_point()

public_key_point_data fc::em::public_key::serialize_ecc_point ( ) const

Definition at line 282 of file elliptic_em_impl_pub.cpp.

283 {
285 if( !my->_key ) return dat;
286 EC_KEY_set_conv_form( my->_key, POINT_CONVERSION_UNCOMPRESSED );
287 char* front = &dat.data[0];
288 i2o_ECPublicKey( my->_key, (unsigned char**)&front ); // FIXME: questionable memory handling
289 return dat;
290 }
fc::array< char, 65 > public_key_point_data
the full non-compressed version of the ECC point

◆ to_base58() [1/2]

std::string fc::em::public_key::to_base58 ( ) const

Definition at line 108 of file elliptic_em.cpp.

109 {
110 FC_ASSERT( my->_key != empty_pub );
111 return to_base58( my->_key );
112 }
std::string to_base58() const
Allows to convert current public key object into base58 number.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ to_base58() [2/2]

std::string fc::em::public_key::to_base58 ( const public_key_data & key)
static

Definition at line 122 of file elliptic_em_common.cpp.

123 {
124 uint32_t check = (uint32_t)sha256::hash(key.data, sizeof(key))._hash[0];
125 static_assert(sizeof(key) + sizeof(check) == 37, ""); // hack around gcc bug: key.size() should be constexpr, but isn't
126 array<char, 37> data;
127 memcpy(data.data, key.begin(), key.size());
128 memcpy(data.begin() + key.size(), (const char*)&check, sizeof(check));
129 return fc::to_base58(data.begin(), data.size(), fc::yield_function_t());
130 }
std::string to_base58(const char *d, size_t s, const fc::yield_function_t &yield)
Definition base58.cpp:618
Here is the call graph for this function:

◆ valid()

bool fc::em::public_key::valid ( ) const

Definition at line 103 of file elliptic_em.cpp.

104 {
105 return my->_key != empty_pub;
106 }
Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ operator!=

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

Definition at line 67 of file elliptic_em.hpp.

68 {
69 return a.serialize() != b.serialize();
70 }
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181

◆ operator==

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

Definition at line 63 of file elliptic_em.hpp.

64 {
65 return a.serialize() == b.serialize();
66 }

◆ private_key

friend class private_key
friend

Definition at line 80 of file elliptic_em.hpp.


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