Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
elliptic_r1.hpp
Go to the documentation of this file.
1#pragma once
7#include <fc/fwd.hpp>
8#include <fc/array.hpp>
9#include <fc/io/raw_fwd.hpp>
10
11namespace fc {
12
13 namespace crypto { namespace r1 {
14 namespace detail
15 {
16 class public_key_impl;
17 class private_key_impl;
18 }
19
21 typedef fc::sha256 private_key_secret;
25
26 int ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, ECDSA_SIG *ecsig, const unsigned char *msg, int msglen, int recid, int check);
27
33 {
34 public:
35 public_key();
36 public_key(const public_key& k);
38 bool verify( const fc::sha256& digest, const signature& sig );
40
41 operator public_key_data()const { return serialize(); }
42
43
44 public_key( const public_key_data& v );
46 public_key( const compact_signature& c, const fc::sha256& digest, bool check_canonical = true );
47
48 bool valid()const;
49 public_key mult( const fc::sha256& offset );
50 public_key add( const fc::sha256& offset )const;
51
52 public_key( public_key&& pk );
54 public_key& operator=( const public_key& pk );
55
56 inline friend bool operator==( const public_key& a, const public_key& b )
57 {
58 return a.serialize() == b.serialize();
59 }
60 inline friend bool operator!=( const public_key& a, const public_key& b )
61 {
62 return a.serialize() != b.serialize();
63 }
64
66 std::string to_base58() const;
67 static public_key from_base58( const std::string& b58 );
68
69 private:
70 friend class private_key;
71 friend compact_signature signature_from_ecdsa(const EC_KEY* key, const public_key_data& pub_data, fc::ecdsa_sig& sig, const fc::sha256& d);
73 };
74
80 {
81 public:
84 private_key( const private_key& pk );
86
88 private_key& operator=( const private_key& pk );
89
90 static private_key generate();
91 static private_key regenerate( const fc::sha256& secret );
92
98 static private_key generate_from_seed( const fc::sha256& seed, const fc::sha256& offset = fc::sha256() );
99
100 private_key_secret get_secret()const; // get the private key secret
101
102 operator private_key_secret ()const { return get_secret(); }
103
108 fc::sha512 get_shared_secret( const public_key& pub )const;
109
110 signature sign( const fc::sha256& digest )const;
112 bool verify( const fc::sha256& digest, const signature& sig );
113
115
116 inline friend bool operator==( const private_key& a, const private_key& b )
117 {
118 return a.get_secret() == b.get_secret();
119 }
120 inline friend bool operator!=( const private_key& a, const private_key& b )
121 {
122 return a.get_secret() != b.get_secret();
123 }
124 inline friend bool operator<( const private_key& a, const private_key& b )
125 {
126 return a.get_secret() < b.get_secret();
127 }
128
129 private:
131 };
132
136 struct public_key_shim : public crypto::shim<public_key_data> {
138
139 bool valid()const {
140 return public_key(_data).valid();
141 }
142 };
143
144 struct signature_shim : public crypto::shim<compact_signature> {
147
148 public_key_type recover(const sha256& digest, bool check_canonical) const {
149 return public_key_type(public_key(_data, digest, check_canonical).serialize());
150 }
151 };
152
153 struct private_key_shim : public crypto::shim<private_key_secret> {
154 using crypto::shim<private_key_secret>::shim;
157
158 signature_type sign( const sha256& digest, bool require_canonical = true ) const
159 {
160 return signature_type(private_key::regenerate(_data).sign_compact(digest));
161 }
162
167
172
174 {
175 return private_key_shim(private_key::generate().get_secret());
176 }
177 };
178
179 //key here is just an optimization for getting the curve's parameters from an already constructed curve
180 compact_signature signature_from_ecdsa(const EC_KEY* key, const public_key_data& pub, fc::ecdsa_sig& sig, const fc::sha256& d);
181
182 } // namespace r1
183 } // namespace crypto
184 void to_variant( const crypto::r1::private_key& var, variant& vo );
185 void from_variant( const variant& var, crypto::r1::private_key& vo );
186 void to_variant( const crypto::r1::public_key& var, variant& vo );
187 void from_variant( const variant& var, crypto::r1::public_key& vo );
188
189 namespace raw
190 {
191 template<typename Stream>
198
199 template<typename Stream>
201 {
202 fc::raw::pack( s, pk.serialize() );
203 }
204
205 template<typename Stream>
207 {
208 fc::sha256 sec;
209 unpack( s, sec );
211 }
212
213 template<typename Stream>
215 {
216 fc::raw::pack( s, pk.get_secret() );
217 }
218
219 } // namespace raw
220
221} // namespace fc
222#include <fc/reflect/reflect.hpp>
223
an elliptic curve private key.
friend bool operator<(const private_key &a, const private_key &b)
bool verify(const fc::sha256 &digest, const signature &sig)
signature sign(const fc::sha256 &digest) const
friend bool operator!=(const private_key &a, const private_key &b)
static private_key generate_from_seed(const fc::sha256 &seed, const fc::sha256 &offset=fc::sha256())
private_key_secret get_secret() const
public_key get_public_key() const
fc::sha512 get_shared_secret(const public_key &pub) const
compact_signature sign_compact(const fc::sha256 &digest) const
static private_key regenerate(const fc::sha256 &secret)
static private_key generate()
private_key & operator=(private_key &&pk)
friend bool operator==(const private_key &a, const private_key &b)
contains only the public point of an elliptic curve key.
public_key & operator=(public_key &&pk)
bool verify(const fc::sha256 &digest, const signature &sig)
public_key add(const fc::sha256 &offset) const
static public_key from_base58(const std::string &b58)
friend compact_signature signature_from_ecdsa(const EC_KEY *key, const public_key_data &pub_data, fc::ecdsa_sig &sig, const fc::sha256 &d)
public_key_data serialize() 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)
public_key mult(const fc::sha256 &offset)
Used to forward declare value types.
Definition fwd.hpp:11
Concept for reading and writing characters.
fc::array< char, 33 > public_key_data
int ECDSA_SIG_recover_key_GFp(EC_KEY *eckey, ECDSA_SIG *ecsig, const unsigned char *msg, int msglen, int recid, int check)
compact_signature signature_from_ecdsa(const EC_KEY *key, const public_key_data &pub, fc::ecdsa_sig &sig, const fc::sha256 &d)
fc::array< char, 65 > public_key_point_data
the full non-compressed version of the ECC point
fc::array< char, 72 > signature
fc::array< unsigned char, 65 > compact_signature
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
public_key_type get_public_key() const
signature_type sign(const sha256 &digest, bool require_canonical=true) const
static private_key_shim generate()
sha512 generate_shared_secret(const public_key_type &pub_key) const
public_key_type recover(const sha256 &digest, bool check_canonical) const
const data_type & serialize() const
Definition common.hpp:188
char * s