BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet 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 }}
fc::digest
fc::sha256 digest(const T &value)
Definition: digest.hpp:9
fc::ecc::private_key::operator=
private_key & operator=(private_key &&pk)
Definition: elliptic_impl_priv.cpp:40
fc
Definition: api.hpp:15
fc::ecc::private_key::get_secret
private_key_secret get_secret() const
Definition: elliptic_impl_priv.cpp:59
fc::sha256
Definition: sha256.hpp:10
fc::ecc::detail::_get_context
const secp256k1_context_t * _get_context()
Definition: elliptic_secp256k1.cpp:24
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::private_key
an elliptic curve private key.
Definition: elliptic.hpp:89
fc::ecc::detail::private_key_impl::_key
private_key_secret _key
Definition: _elliptic_impl_priv.hpp:22
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::sha256::data
char * data() const
Definition: sha256.cpp:29
fc::ecc::private_key::private_key
private_key()
Definition: elliptic_impl_priv.cpp:32
_elliptic_impl_priv.hpp
fc::ecc::detail::private_key_impl
Definition: _elliptic_impl_priv.hpp:14
fc::ecc::private_key::get_public_key
public_key get_public_key() const
Definition: elliptic_impl_priv.cpp:70
fc::ecc::detail::_init_lib
void _init_lib()
Definition: elliptic_secp256k1.cpp:29
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::detail::private_key_impl::operator=
private_key_impl & operator=(const private_key_impl &pk) BOOST_NOEXCEPT
Definition: elliptic_impl_priv.cpp:23
std
Definition: zeroed_array.hpp:76
fc::ecc::private_key::regenerate
static private_key regenerate(const fc::sha256 &secret)
Definition: elliptic_impl_priv.cpp:52
fc::ecc::detail::private_key_impl::private_key_impl
private_key_impl() BOOST_NOEXCEPT
Definition: elliptic_impl_priv.cpp:12
fwd_impl.hpp