BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface software
elliptic_impl_priv.cpp
Go to the documentation of this file.
1 #include <fc/fwd_impl.hpp>
2 
3 #include <secp256k1.h>
4 
6 
7 /* used by mixed + secp256k1 */
8 
9 namespace fc { namespace ecc {
10  namespace detail {
11 
13  {
14  _init_lib();
15  }
16 
18  {
19  _init_lib();
20  this->_key = cpy._key;
21  }
22 
24  {
25  _key = pk._key;
26  return *this;
27  }
28  }
29 
30  static const private_key_secret empty_priv;
31 
33 
34  private_key::private_key( const private_key& pk ) : my( pk.my ) {}
35 
36  private_key::private_key( private_key&& pk ) : my( std::move( pk.my ) ) {}
37 
39 
41  {
42  my = std::move( pk.my );
43  return *this;
44  }
45 
47  {
48  my = pk.my;
49  return *this;
50  }
51 
53  {
54  private_key self;
55  self.my->_key = secret;
56  return self;
57  }
58 
60  {
61  return my->_key;
62  }
63 
64  private_key::private_key( EC_KEY* k )
65  {
66  my->_key = get_secret( k );
67  EC_KEY_free(k);
68  }
69 
71  {
72  FC_ASSERT( my->_key != empty_priv );
73  public_key_data pub;
74  unsigned int pk_len;
75  FC_ASSERT( secp256k1_ec_pubkey_create( detail::_get_context(), pub.data(), (int*) &pk_len,
76  (unsigned char*) my->_key.data(), 1 ) );
77  FC_ASSERT( pk_len == pub.size() );
78  return public_key(pub);
79  }
80 
81  static int extended_nonce_function( unsigned char *nonce32, const unsigned char *msg32,
82  const unsigned char *key32, unsigned int attempt,
83  const void *data ) {
84  unsigned int* extra = (unsigned int*) data;
85  (*extra)++;
86  return secp256k1_nonce_function_default( nonce32, msg32, key32, *extra, nullptr );
87  }
88 
89  compact_signature private_key::sign_compact( const fc::sha256& digest, bool require_canonical )const
90  {
91  FC_ASSERT( my->_key != empty_priv );
92  compact_signature result;
93  int recid;
94  unsigned int counter = 0;
95  do
96  {
97  FC_ASSERT( secp256k1_ecdsa_sign_compact( detail::_get_context(), (unsigned char*) digest.data(),
98  result.data() + 1, (unsigned char*) my->_key.data(),
99  extended_nonce_function, &counter, &recid ));
100  } while( require_canonical && !public_key::is_canonical( result ) );
101  result.data()[0] = 27 + 4 + recid;
102  return result;
103  }
104 
105 }}
private_key_secret get_secret() const
const secp256k1_context_t * _get_context()
compact_signature sign_compact(const fc::sha256 &digest, bool require_canonical=true) const
private_key & operator=(private_key &&pk)
public_key get_public_key() const
fc::sha256 digest(const T &value)
Definition: digest.hpp:9
char * data() const
Definition: sha256.cpp:29
contains only the public point of an elliptic curve key.
Definition: elliptic.hpp:35
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
Definition: exception.hpp:345
static private_key regenerate(const fc::sha256 &secret)
private_key_impl & operator=(const private_key_impl &pk) BOOST_NOEXCEPT
Definition: api.hpp:15
an elliptic curve private key.
Definition: elliptic.hpp:89