Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
elliptic_em.hpp
Go to the documentation of this file.
1#pragma once
8#include <fc/fwd.hpp>
9#include <fc/array.hpp>
10#include <fc/io/raw_fwd.hpp>
12
13namespace fc {
14
15 namespace em {
16 namespace detail
17 {
18 class public_key_impl;
19 class private_key_impl;
20 }
21
22 typedef fc::sha256 blind_factor_type;
25 typedef fc::sha256 private_key_secret;
30 typedef fc::sha256 blinded_hash;
31 typedef fc::sha256 blind_signature;
32
38 {
39 public:
40 public_key();
41 public_key(const public_key& k);
43// bool verify( const fc::sha256& digest, const signature& sig );
46
47 operator public_key_data()const { return serialize(); }
48
49
50 public_key( const public_key_data& v );
52 public_key( const compact_signature& c, const fc::sha256& digest, bool check_canonical = true );
53 public_key( const compact_signature& c, const unsigned char* digest, bool check_canonical = true );
54
55 public_key child( const fc::sha256& offset )const;
56
57 bool valid()const;
58
59 public_key( public_key&& pk );
61 public_key& operator=( const public_key& pk );
62
63 inline friend bool operator==( const public_key& a, const public_key& b )
64 {
65 return a.serialize() == b.serialize();
66 }
67 inline friend bool operator!=( const public_key& a, const public_key& b )
68 {
69 return a.serialize() != b.serialize();
70 }
71
73 std::string to_base58() const;
74 static std::string to_base58( const public_key_data &key );
75 static public_key from_base58( const std::string& b58 );
76
77 unsigned int fingerprint() const;
78
79 private:
80 friend class private_key;
81 static public_key from_key_data( const public_key_data& v );
82 static bool is_canonical( const compact_signature& c );
84 };
85
91 {
92 public:
95 private_key( const private_key& pk );
97
99 private_key& operator=( const private_key& pk );
100
101 static private_key generate();
102 static private_key regenerate( const fc::sha256& secret );
103
104 private_key child( const fc::sha256& offset )const;
105
111 static private_key generate_from_seed( const fc::sha256& seed, const fc::sha256& offset = fc::sha256() );
112
113 private_key_secret get_secret()const; // get the private key secret
114
115 operator private_key_secret ()const { return get_secret(); }
116
121 fc::sha512 get_shared_secret( const public_key& pub )const;
122
123// signature sign( const fc::sha256& digest )const;
124 compact_signature sign_compact( const fc::sha256& digest, bool require_canonical = true )const;
125// bool verify( const fc::sha256& digest, const signature& sig );
126
128
129 inline friend bool operator==( const private_key& a, const private_key& b )
130 {
131 return a.get_secret() == b.get_secret();
132 }
133 inline friend bool operator!=( const private_key& a, const private_key& b )
134 {
135 return a.get_secret() != b.get_secret();
136 }
137 inline friend bool operator<( const private_key& a, const private_key& b )
138 {
139 return a.get_secret() < b.get_secret();
140 }
141
142 unsigned int fingerprint() const { return get_public_key().fingerprint(); }
143
144 private:
145 private_key( EC_KEY* k );
146 static fc::sha256 get_secret( const EC_KEY * const k );
148 };
149
153 struct public_key_shim : public crypto::shim<public_key_data> {
155
156 bool valid()const {
157 return public_key(_data).valid();
158 }
159 };
160
161 struct signature_shim : public crypto::shim<compact_signature> {
164
165 public_key_type recover(const sha256& digest, bool check_canonical) const {
166 // "x19Ethereum Signed Message:\n" is the header used by the `eth_sign` RPC call. We have converted the string to its hex value to save a step.
167 // std::string erc155_message_prefix = "19457468657265756d205369676e6564204d6573736167653a0a3332";
168 uint8_t eth_prefix[28] = {
169 25, 69, 116, 104, 101, 114, 101,
170 117, 109, 32, 83, 105, 103, 110,
171 101, 100, 32, 77, 101, 115, 115,
172 97, 103, 101, 58, 10, 51, 50
173 };
174 // Hash (keccak256) the msg string
175
176 // unsigned char msg_digest_result = digest.data();
177 unsigned char eth_prefixed_msg_raw[28 + 32];
178 std::copy(std::begin(eth_prefix), std::end(eth_prefix), std::begin(eth_prefixed_msg_raw));
179 std::copy((unsigned char*) digest.data(), (unsigned char*) digest.data() + 32, std::begin(eth_prefixed_msg_raw) + 28);
180
181 SHA3_CTX msg_ctx;
182 keccak_init(&msg_ctx);
183 keccak_update(&msg_ctx, eth_prefixed_msg_raw, 60);
184
185 // Hash the newly created raw message.
186 unsigned char msg_digest_result[32];
187 keccak_final(&msg_ctx, msg_digest_result);
188
189 // sha256 msg_digest
190 return public_key_type(public_key(_data, msg_digest_result, false).serialize());
191 }
192 };
193
194 struct private_key_shim : public crypto::shim<private_key_secret> {
195 using crypto::shim<private_key_secret>::shim;
198
199 signature_type sign( const sha256& digest, bool require_canonical = true ) const
200 {
201 return signature_type(private_key::regenerate(_data).sign_compact(digest, require_canonical));
202 }
203
208
213
215 {
216 return private_key_shim(private_key::generate().get_secret());
217 }
218
219 };
220
221 } // namespace ecc
222 void to_variant( const em::private_key& var, variant& vo );
223 void from_variant( const variant& var, em::private_key& vo );
224 void to_variant( const em::public_key& var, variant& vo );
225 void from_variant( const variant& var, em::public_key& vo );
226
227 namespace raw
228 {
229 template<typename Stream>
231 {
233 fc::raw::unpack(s,ser);
234 pk = fc::em::public_key( ser );
235 }
236
237 template<typename Stream>
238 void pack( Stream& s, const fc::em::public_key& pk)
239 {
240 fc::raw::pack( s, pk.serialize() );
241 }
242
243 template<typename Stream>
245 {
246 fc::sha256 sec;
247 unpack( s, sec );
249 }
250
251 template<typename Stream>
252 void pack( Stream& s, const fc::em::private_key& pk)
253 {
254 fc::raw::pack( s, pk.get_secret() );
255 }
256
257 } // namespace raw
258
259} // namespace fc
260#include <fc/reflect/reflect.hpp>
261
an elliptic curve private key.
static private_key generate_from_seed(const fc::sha256 &seed, const fc::sha256 &offset=fc::sha256())
friend bool operator<(const private_key &a, const private_key &b)
friend bool operator!=(const private_key &a, const private_key &b)
public_key get_public_key() const
private_key_secret get_secret() const
static private_key regenerate(const fc::sha256 &secret)
private_key & operator=(private_key &&pk)
static private_key generate()
fc::sha512 get_shared_secret(const public_key &pub) const
unsigned int fingerprint() const
private_key child(const fc::sha256 &offset) const
compact_signature sign_compact(const fc::sha256 &digest, bool require_canonical=true) const
friend bool operator==(const private_key &a, const private_key &b)
contains only the public point of an elliptic curve key.
unsigned int fingerprint() const
friend bool operator==(const public_key &a, const public_key &b)
std::string to_base58() const
Allows to convert current public key object into base58 number.
friend bool operator!=(const public_key &a, const public_key &b)
static public_key from_base58(const std::string &b58)
public_key child(const fc::sha256 &offset) const
public_key & operator=(public_key &&pk)
public_key_point_data serialize_ecc_point() const
public_key_data serialize() const
Used to forward declare value types.
Definition fwd.hpp:11
const char * data() const
Definition sha256.cpp:31
Concept for reading and writing characters.
void keccak_init(SHA3_CTX *ctx)
Definition keccak256.cpp:75
void keccak_update(SHA3_CTX *ctx, const unsigned char *msg, uint16_t size)
void keccak_final(SHA3_CTX *ctx, unsigned char *result)
fc::array< char, 33 > public_key_data
fc::array< char, 72 > signature
fc::array< char, 65 > public_key_point_data
the full non-compressed version of the ECC point
fc::array< unsigned char, 65 > compact_signature
fc::array< char, 78 > extended_key_data
fc::array< char, 33 > commitment_type
void unpack(Stream &s, std::deque< T > &value)
Definition raw.hpp:540
void pack(Stream &s, const std::deque< T > &value)
Definition raw.hpp:531
namespace sysio::chain
Definition authority.cpp:3
fc::sha256 digest(const T &value)
Definition digest.hpp:9
void from_variant(const fc::variant &v, sysio::chain::chain_id_type &cid)
void to_variant(const sysio::chain::shared_public_key &var, fc::variant &vo)
Definition authority.cpp:4
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
#define FC_REFLECT_TYPENAME(TYPE)
Definition reflect.hpp:320
#define FC_REFLECT_DERIVED(TYPE, INHERITS, MEMBERS)
Specializes fc::reflector for TYPE where type inherits other reflected classes.
Definition reflect.hpp:298
unsigned char uint8_t
Definition stdint.h:124
const data_type & serialize() const
Definition common.hpp:188
static private_key_shim generate()
signature_type sign(const sha256 &digest, bool require_canonical=true) const
public_key_type get_public_key() const
public_key_shim public_key_type
sha512 generate_shared_secret(const public_key_type &pub_key) const
signature_shim signature_type
public_key_shim public_key_type
public_key_type recover(const sha256 &digest, bool check_canonical) const
char * s