BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
elliptic.hpp
Go to the documentation of this file.
1 #pragma once
3 #include <fc/crypto/bigint.hpp>
4 #include <fc/crypto/openssl.hpp>
5 #include <fc/crypto/sha256.hpp>
6 #include <fc/crypto/sha512.hpp>
7 #include <fc/fwd.hpp>
8 #include <fc/io/raw_fwd.hpp>
9 
10 #include <array>
11 
12 namespace fc {
13 
14  namespace ecc {
15  namespace detail
16  {
17  class public_key_impl;
18  class private_key_impl;
19  }
20 
28  typedef std::vector<char> range_proof_type;
30 
35  class public_key
36  {
37  public:
38  public_key();
39  public_key(const public_key& k);
40  ~public_key();
43 
44  operator public_key_data()const { return serialize(); }
45 
46 
47  public_key( const public_key_data& v );
49  public_key( const compact_signature& c, const fc::sha256& digest, bool check_canonical = true );
50 
51  public_key child( const fc::sha256& offset )const;
52 
53  bool valid()const;
56  public_key add( const fc::sha256& offset )const;
57 
58  public_key( public_key&& pk );
60  public_key& operator=( const public_key& pk );
61 
62  inline friend bool operator==( const public_key& a, const public_key& b )
63  {
64  return a.serialize() == b.serialize();
65  }
66  inline friend bool operator!=( const public_key& a, const public_key& b )
67  {
68  return a.serialize() != b.serialize();
69  }
70 
72  std::string to_base58() const;
73  static std::string to_base58( const public_key_data &key );
74  static public_key from_base58( const std::string& b58 );
75 
76  unsigned int fingerprint() const;
77 
78  private:
79  friend class private_key;
80  static public_key from_key_data( const public_key_data& v );
81  static bool is_canonical( const compact_signature& c );
83  };
84 
90  {
91  public:
92  private_key();
93  private_key( private_key&& pk );
94  private_key( const private_key& pk );
95  ~private_key();
96 
98  private_key& operator=( const private_key& pk );
99 
100  static private_key generate();
101  static private_key regenerate( const fc::sha256& secret );
102 
103  private_key child( const fc::sha256& offset )const;
104 
110  static private_key generate_from_seed( const fc::sha256& seed, const fc::sha256& offset = fc::sha256() );
111 
112  private_key_secret get_secret()const; // get the private key secret
113 
114  operator private_key_secret ()const { return get_secret(); }
115 
120  fc::sha512 get_shared_secret( const public_key& pub )const;
121 
122  compact_signature sign_compact( const fc::sha256& digest, bool require_canonical = true )const;
123 
124  public_key get_public_key()const;
125 
126  inline friend bool operator==( const private_key& a, const private_key& b )
127  {
128  return a.get_secret() == b.get_secret();
129  }
130  inline friend bool operator!=( const private_key& a, const private_key& b )
131  {
132  return a.get_secret() != b.get_secret();
133  }
134  inline friend bool operator<( const private_key& a, const private_key& b )
135  {
136  return a.get_secret() < b.get_secret();
137  }
138 
139  unsigned int fingerprint() const { return get_public_key().fingerprint(); }
140 
141  private:
142  private_key( EC_KEY* k );
143  static fc::sha256 get_secret( const EC_KEY * const k );
145  };
146 
148  {
149  public:
150  extended_public_key( const public_key& k, const sha256& c,
151  int child = 0, int parent_fp = 0, uint8_t depth = 0 );
152 
153  extended_public_key derive_child( int i ) const;
155 
157  static extended_public_key deserialize( const extended_key_data& data );
158  std::string str() const;
159  std::string to_base58() const { return str(); }
160  static extended_public_key from_base58( const std::string& base58 );
161 
162  private:
163  sha256 c;
164  int child_num, parent_fp;
165  uint8_t depth;
166  };
167 
169  {
170  public:
171  extended_private_key( const private_key& k, const sha256& c,
172  int child = 0, int parent_fp = 0, uint8_t depth = 0 );
173 
175 
176  extended_private_key derive_child( int i ) const;
179 
181  static extended_private_key deserialize( const extended_key_data& data );
182  std::string str() const;
183  std::string to_base58() const { return str(); }
184  static extended_private_key from_base58( const std::string& base58 );
185  static extended_private_key generate_master( const std::string& seed );
186  static extended_private_key generate_master( const char* seed, uint32_t seed_len );
187 
188  private:
189  extended_private_key private_derive_rest( const fc::sha512& hash,
190  int num ) const;
191  sha256 c;
192  int child_num, parent_fp;
193  uint8_t depth;
194  };
195 
197  {
198  int64_t exp;
199  int64_t mantissa;
200  uint64_t min_value;
201  uint64_t max_value;
202  };
203 
204  commitment_type blind( const blind_factor_type& blind, uint64_t value );
205  blind_factor_type blind_sum( const std::vector<blind_factor_type>& blinds, uint32_t non_neg );
207  bool verify_sum( const std::vector<commitment_type>& commits, const std::vector<commitment_type>& neg_commits, int64_t excess );
208  bool verify_range( uint64_t& min_val, uint64_t& max_val, const commitment_type& commit, const range_proof_type& proof );
209 
210  range_proof_type range_proof_sign( uint64_t min_value,
211  const commitment_type& commit,
212  const blind_factor_type& commit_blind,
213  const blind_factor_type& nonce,
214  int8_t base10_exp,
215  uint8_t min_bits,
216  uint64_t actual_value
217  );
218 
220  uint64_t& value_out,
221  string& message_out,
222  const blind_factor_type& nonce,
223  uint64_t& min_val,
224  uint64_t& max_val,
225  commitment_type commit,
226  const range_proof_type& proof );
228 
229 
230 
231  } // namespace ecc
232  void to_variant( const ecc::private_key& var, variant& vo, uint32_t max_depth );
233  void from_variant( const variant& var, ecc::private_key& vo, uint32_t max_depth );
234  void to_variant( const ecc::public_key& var, variant& vo, uint32_t max_depth );
235  void from_variant( const variant& var, ecc::public_key& vo, uint32_t max_depth );
236 
237  namespace raw
238  {
239  template<typename Stream>
240  void unpack( Stream& s, fc::ecc::public_key& pk, uint32_t _max_depth )
241  {
242  FC_ASSERT( _max_depth > 0 );
244  fc::raw::unpack( s, ser, _max_depth - 1 );
245  pk = fc::ecc::public_key( ser );
246  }
247 
248  template<typename Stream>
249  void pack( Stream& s, const fc::ecc::public_key& pk, uint32_t _max_depth )
250  {
251  FC_ASSERT( _max_depth > 0 );
252  fc::raw::pack( s, pk.serialize(), _max_depth - 1 );
253  }
254 
255  template<typename Stream>
256  void unpack( Stream& s, fc::ecc::private_key& pk, uint32_t _max_depth )
257  {
258  FC_ASSERT( _max_depth > 0 );
259  fc::sha256 sec;
260  unpack( s, sec, _max_depth - 1 );
262  }
263 
264  template<typename Stream>
265  void pack( Stream& s, const fc::ecc::private_key& pk, uint32_t _max_depth )
266  {
267  FC_ASSERT( _max_depth > 0 );
268  fc::raw::pack( s, pk.get_secret(), _max_depth - 1 );
269  }
270 
271  } // namespace raw
272 
273 } // namespace fc
274 #include <fc/reflect/reflect.hpp>
275 
278 FC_REFLECT( fc::ecc::range_proof_info, (exp)(mantissa)(min_value)(max_value) )
fc::ecc::extended_public_key::derive_child
extended_public_key derive_child(int i) const
Definition: elliptic_common.cpp:251
fc::ecc::extended_public_key::derive_normal_child
extended_public_key derive_normal_child(int i) const
Definition: elliptic_secp256k1.cpp:178
zeroed_array.hpp
fc::ecc::private_key::operator!=
friend bool operator!=(const private_key &a, const private_key &b)
Definition: elliptic.hpp:130
fc::ecc::public_key::operator=
public_key & operator=(public_key &&pk)
Definition: elliptic_secp256k1.cpp:91
fc::ecc::verify_range
bool verify_range(uint64_t &min_val, uint64_t &max_val, const commitment_type &commit, const range_proof_type &proof)
Definition: elliptic_secp256k1.cpp:235
fc::ecc::private_key::fingerprint
unsigned int fingerprint() const
Definition: elliptic.hpp:139
fc::digest
fc::sha256 digest(const T &value)
Definition: digest.hpp:9
fc::ecc::extended_public_key::serialize_extended
extended_key_data serialize_extended() const
Definition: elliptic_common.cpp:257
fc::ecc::extended_public_key
Definition: elliptic.hpp:147
fc::ecc::range_proof_info
Definition: elliptic.hpp:196
fc::ecc::public_key::fingerprint
unsigned int fingerprint() const
Definition: elliptic_common.cpp:161
fc::ecc::extended_private_key::derive_child
extended_private_key derive_child(int i) const
Definition: elliptic_common.cpp:302
fc::ecc::range_proof_sign
range_proof_type range_proof_sign(uint64_t min_value, const commitment_type &commit, const blind_factor_type &commit_blind, const blind_factor_type &nonce, int8_t base10_exp, uint8_t min_bits, uint64_t actual_value)
Definition: elliptic_secp256k1.cpp:240
fc::ecc::signature
zero_initialized_array< unsigned char, 72 > signature
Definition: elliptic.hpp:26
fc::ecc::blind_sum
blind_factor_type blind_sum(const std::vector< blind_factor_type > &blinds, uint32_t non_neg)
Definition: elliptic_secp256k1.cpp:215
FC_REFLECT_TYPENAME
FC_REFLECT_TYPENAME(fc::log_message)
fc::raw::unpack
void unpack(Stream &s, flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:23
fc::ecc::private_key::operator=
private_key & operator=(private_key &&pk)
Definition: elliptic_impl_priv.cpp:40
bigint.hpp
openssl.hpp
fwd.hpp
fc
Definition: api.hpp:15
fc::ecc::public_key::~public_key
~public_key()
Definition: elliptic_secp256k1.cpp:83
fc::ecc::blind
commitment_type blind(const blind_factor_type &blind, uint64_t value)
Definition: elliptic_secp256k1.cpp:208
fc::ecc::private_key::get_secret
private_key_secret get_secret() const
Definition: elliptic_impl_priv.cpp:59
fc::ecc::private_key_secret
fc::sha256 private_key_secret
Definition: elliptic.hpp:24
fc::ecc::private_key::get_shared_secret
fc::sha512 get_shared_secret(const public_key &pub) const
Definition: elliptic_secp256k1.cpp:66
fc::ecc::private_key::generate_from_seed
static private_key generate_from_seed(const fc::sha256 &seed, const fc::sha256 &offset=fc::sha256())
Definition: elliptic_common.cpp:176
fc::ecc::range_proof_info::max_value
uint64_t max_value
Definition: elliptic.hpp:201
fc::sha256
Definition: sha256.hpp:10
fc::ecc::public_key::to_base58
std::string to_base58() const
Allows to convert current public key object into base58 number.
Definition: elliptic_secp256k1.cpp:112
fc::ecc::public_key::valid
bool valid() const
Definition: elliptic_secp256k1.cpp:97
fc::zero_initialized_array< unsigned char, 33 >
fc::ecc::public_key
contains only the public point of an elliptic curve key.
Definition: elliptic.hpp:35
fc::ecc::public_key_point_data
zero_initialized_array< unsigned char, 65 > public_key_point_data
the full non-compressed version of the ECC point
Definition: elliptic.hpp:25
fc::from_variant
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
reflect.hpp
Defines types and macros used to provide reflection.
fc::ecc::extended_public_key::deserialize
static extended_public_key deserialize(const extended_key_data &data)
Definition: elliptic_common.cpp:271
fc::ecc::extended_public_key::from_base58
static extended_public_key from_base58(const std::string &base58)
Definition: elliptic_common.cpp:281
fc::ecc::detail::public_key_impl
Definition: _elliptic_impl_pub.hpp:13
fc::ecc::public_key::serialize_ecc_point
public_key_point_data serialize_ecc_point() const
Definition: elliptic_secp256k1.cpp:124
fc::ecc::private_key
an elliptic curve private key.
Definition: elliptic.hpp:89
sha512.hpp
fc::ecc::private_key::generate
static private_key generate()
Definition: elliptic_common.cpp:217
fc::sha512
Definition: sha512.hpp:9
fc::ecc::range_get_info
range_proof_info range_get_info(const range_proof_type &proof)
Definition: elliptic_secp256k1.cpp:291
fc::ecc::public_key::add
public_key add(const fc::sha256 &offset) const
Definition: elliptic_secp256k1.cpp:102
fc::ecc::extended_private_key::to_base58
std::string to_base58() const
Definition: elliptic.hpp:183
fc::ecc::verify_range_proof_rewind
bool verify_range_proof_rewind(blind_factor_type &blind_out, uint64_t &value_out, string &message_out, const blind_factor_type &nonce, uint64_t &min_val, uint64_t &max_val, commitment_type commit, const range_proof_type &proof)
Definition: elliptic_secp256k1.cpp:264
fc::ecc::private_key::operator<
friend bool operator<(const private_key &a, const private_key &b)
Definition: elliptic.hpp:134
fc::ecc::private_key::sign_compact
compact_signature sign_compact(const fc::sha256 &digest, bool require_canonical=true) const
Definition: elliptic_impl_priv.cpp:89
fc::ecc::public_key_data
zero_initialized_array< unsigned char, 33 > public_key_data
Definition: elliptic.hpp:23
fc::ecc::range_proof_info::exp
int64_t exp
Definition: elliptic.hpp:198
fc::ecc::public_key::child
public_key child(const fc::sha256 &offset) const
Definition: elliptic_common.cpp:121
fc::ecc::commitment_type
zero_initialized_array< unsigned char, 33 > commitment_type
Definition: elliptic.hpp:22
fc::ecc::private_key::private_key
private_key()
Definition: elliptic_impl_priv.cpp:32
fc::ecc::extended_public_key::extended_public_key
extended_public_key(const public_key &k, const sha256 &c, int child=0, int parent_fp=0, uint8_t depth=0)
Definition: elliptic_secp256k1.cpp:174
fc::to_variant
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
fc::ecc::detail::private_key_impl
Definition: _elliptic_impl_priv.hpp:14
fc::ecc::extended_private_key::deserialize
static extended_private_key deserialize(const extended_key_data &data)
Definition: elliptic_common.cpp:339
fc::ecc::private_key::child
private_key child(const fc::sha256 &offset) const
Definition: elliptic_common.cpp:130
fc::ecc::public_key::operator!=
friend bool operator!=(const public_key &a, const public_key &b)
Definition: elliptic.hpp:66
fc::ecc::extended_private_key::derive_normal_child
extended_private_key derive_normal_child(int i) const
Definition: elliptic_common.cpp:307
fc::ecc::private_key::get_public_key
public_key get_public_key() const
Definition: elliptic_impl_priv.cpp:70
fc::ecc::extended_private_key::get_extended_public_key
extended_public_key get_extended_public_key() const
Definition: elliptic_common.cpp:297
fc::ecc::extended_private_key::str
std::string str() const
Definition: elliptic_common.cpp:344
fc::ecc::extended_public_key::str
std::string str() const
Definition: elliptic_common.cpp:276
FC_ASSERT
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
fc::ecc::private_key::~private_key
~private_key()
Definition: elliptic_impl_priv.cpp:38
fc::ecc::public_key::operator==
friend bool operator==(const public_key &a, const public_key &b)
Definition: elliptic.hpp:62
fc::ecc::range_proof_info::mantissa
int64_t mantissa
Definition: elliptic.hpp:199
fc::ecc::extended_private_key::serialize_extended
extended_key_data serialize_extended() const
Definition: elliptic_common.cpp:324
fc::variant
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition: variant.hpp:198
fc::ecc::extended_private_key::extended_private_key
extended_private_key(const private_key &k, const sha256 &c, int child=0, int parent_fp=0, uint8_t depth=0)
Definition: elliptic_secp256k1.cpp:193
fc::ecc::extended_private_key::derive_hardened_child
extended_private_key derive_hardened_child(int i) const
Definition: elliptic_common.cpp:315
fc::ecc::public_key::from_base58
static public_key from_base58(const std::string &b58)
Definition: elliptic_common.cpp:148
fc::ecc::public_key::public_key
public_key()
Definition: elliptic_secp256k1.cpp:77
fc::ecc::blind_factor_type
fc::sha256 blind_factor_type
Definition: elliptic.hpp:21
raw_fwd.hpp
FC_REFLECT
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition: reflect.hpp:388
fc::ecc::compact_signature
zero_initialized_array< unsigned char, 65 > compact_signature
Definition: elliptic.hpp:27
fc::ecc::extended_private_key::from_base58
static extended_private_key from_base58(const std::string &base58)
Definition: elliptic_common.cpp:349
fc::ecc::range_proof_type
std::vector< char > range_proof_type
Definition: elliptic.hpp:28
fc::ecc::range_proof_info::min_value
uint64_t min_value
Definition: elliptic.hpp:200
fc::ecc::extended_private_key::generate_master
static extended_private_key generate_master(const std::string &seed)
Definition: elliptic_common.cpp:366
fc::ecc::extended_key_data
zero_initialized_array< unsigned char, 78 > extended_key_data
Definition: elliptic.hpp:29
fc::ecc::private_key::regenerate
static private_key regenerate(const fc::sha256 &secret)
Definition: elliptic_impl_priv.cpp:52
fc::ecc::extended_public_key::to_base58
std::string to_base58() const
Definition: elliptic.hpp:159
fc::ecc::verify_sum
bool verify_sum(const std::vector< commitment_type > &commits, const std::vector< commitment_type > &neg_commits, int64_t excess)
Definition: elliptic_secp256k1.cpp:225
fc::ecc::extended_private_key
Definition: elliptic.hpp:168
fc::fwd
Used to forward declare value types.
Definition: fwd.hpp:10
fc::ecc::private_key::operator==
friend bool operator==(const private_key &a, const private_key &b)
Definition: elliptic.hpp:126
fc::ecc::public_key::serialize
public_key_data serialize() const
Definition: elliptic_secp256k1.cpp:118
fc::raw::pack
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11
sha256.hpp