BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface 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();
41  public_key_data serialize()const;
42  public_key_point_data serialize_ecc_point()const;
43 
44  operator public_key_data()const { return serialize(); }
45 
46 
47  public_key( const public_key_data& v );
48  public_key( const public_key_point_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 );
59  public_key& operator=( 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 
97  private_key& operator=( private_key&& pk );
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;
154  extended_public_key derive_normal_child( int i ) const;
155 
156  extended_key_data serialize_extended() const;
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 
174  extended_public_key get_extended_public_key()const;
175 
176  extended_private_key derive_child( int i ) const;
177  extended_private_key derive_normal_child( int i ) const;
178  extended_private_key derive_hardened_child( int i ) const;
179 
180  extended_key_data serialize_extended() const;
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 
219  bool verify_range_proof_rewind( blind_factor_type& blind_out,
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 );
227  range_proof_info range_get_info( 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) )
public_key_data serialize() const
std::string to_base58(const char *d, size_t s)
Definition: base58.cpp:612
bool verify_range(uint64_t &min_val, uint64_t &max_val, const commitment_type &commit, const range_proof_type &proof)
private_key_secret get_secret() const
void pack(Stream &s, const flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:11
#define FC_REFLECT(TYPE, MEMBERS)
Specializes fc::reflector for TYPE.
Definition: reflect.hpp:386
zero_initialized_array< unsigned char, 78 > extended_key_data
Definition: elliptic.hpp:29
bool verify_sum(const std::vector< commitment_type > &commits, const std::vector< commitment_type > &neg_commits, int64_t excess)
void unpack(Stream &s, fc::ecc::private_key &pk, uint32_t _max_depth)
Definition: elliptic.hpp:256
range_proof_info range_get_info(const range_proof_type &proof)
fc::sha256 blind_factor_type
Definition: elliptic.hpp:21
friend bool operator<(const private_key &a, const private_key &b)
Definition: elliptic.hpp:134
Defines types and macros used to provide reflection.
void unpack(Stream &s, flat_set< T, A... > &value, uint32_t _max_depth)
Definition: flat.hpp:23
void pack(Stream &s, const fc::ecc::private_key &pk, uint32_t _max_depth)
Definition: elliptic.hpp:265
std::string to_base58() const
Definition: elliptic.hpp:159
FC_REFLECT_TYPENAME(fc::log_message)
commitment_type blind(const blind_factor_type &blind, uint64_t value)
friend bool operator==(const private_key &a, const private_key &b)
Definition: elliptic.hpp:126
fc::sha256 digest(const T &value)
Definition: digest.hpp:9
void to_variant(const flat_set< T, A... > &var, variant &vo, uint32_t _max_depth)
Definition: flat.hpp:105
contains only the public point of an elliptic curve key.
Definition: elliptic.hpp:35
zero_initialized_array< unsigned char, 65 > public_key_point_data
the full non-compressed version of the ECC point
Definition: elliptic.hpp:25
zero_initialized_array< unsigned char, 33 > commitment_type
Definition: elliptic.hpp:22
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object&#39;s.
Definition: variant.hpp:198
std::string to_base58() const
Definition: elliptic.hpp:183
unsigned int fingerprint() const
static private_key regenerate(const fc::sha256 &secret)
std::vector< char > range_proof_type
Definition: elliptic.hpp:28
unsigned int fingerprint() const
Definition: elliptic.hpp:139
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)
zero_initialized_array< unsigned char, 33 > public_key_data
Definition: elliptic.hpp:23
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)
blind_factor_type blind_sum(const std::vector< blind_factor_type > &blinds, uint32_t non_neg)
void from_variant(const variant &var, flat_set< T, A... > &vo, uint32_t _max_depth)
Definition: flat.hpp:116
std::vector< char > from_base58(const std::string &base58_str)
Definition: base58.cpp:622
Definition: api.hpp:15
fc::sha256 private_key_secret
Definition: elliptic.hpp:24
friend bool operator!=(const public_key &a, const public_key &b)
Definition: elliptic.hpp:66
an elliptic curve private key.
Definition: elliptic.hpp:89
zero_initialized_array< unsigned char, 65 > compact_signature
Definition: elliptic.hpp:27
zero_initialized_array< unsigned char, 72 > signature
Definition: elliptic.hpp:26
friend bool operator==(const public_key &a, const public_key &b)
Definition: elliptic.hpp:62
friend bool operator!=(const private_key &a, const private_key &b)
Definition: elliptic.hpp:130