TARAXA
Loading...
Searching...
No Matches
Common.h
Go to the documentation of this file.
1// Aleth: Ethereum C++ client, tools and libraries.
2// Copyright 2014-2019 Aleth Authors.
3// Licensed under the GNU General Public License, Version 3.
8#pragma once
9
10#include <libdevcore/Address.h>
11#include <libdevcore/Common.h>
14
15#include <mutex>
16
17namespace dev {
18
20
23using Public = h512;
24
28
32
37
39 SignatureStruct() = default;
40 SignatureStruct(Signature const& _s) { *(h520*)this = _s; }
41 SignatureStruct(h256 const& _r, h256 const& _s, byte _v) : r(_r), s(_s), v(_v) {}
43 v = ::byte(u256(s) >> 255);
44 s = u256(s) & ((u256(1) << 255) - 1);
45 }
46 operator Signature() const { return *reinterpret_cast<h520 const*>(this); }
47
49 bool isValid() const noexcept;
50
53 ::byte v = 0;
54};
55
59
64 CompactSignatureStruct(h256 const& _r, h256 const& _vs) : r(_r), vs(_vs) {}
65 operator CompactSignature() const { return *reinterpret_cast<h512 const*>(this); }
66
68 bool isValid() const noexcept;
69
71 h256 vs;
72};
73
76
79
82
85
88
92
95
98
101
105
108
112
115
118
121
129
137
139Public recover(Signature const& _sig, h256 const& _hash);
140
142Signature sign(Secret const& _k, h256 const& _hash);
143
145bool verify(Public const& _k, Signature const& _s, h256 const& _hash);
146
147// Verify signature with compressed public key
148bool verify(PublicCompressed const& _key, h512 const& _signature, h256 const& _hash);
149
154class KeyPair {
155 public:
159 KeyPair(Secret const& _sec);
160
162 static KeyPair create();
163
165 static KeyPair fromEncryptedSeed(bytesConstRef _seed, std::string const& _password);
166
167 Secret const& secret() const { return m_secret; }
168
170 Public const& pub() const { return m_public; }
171
173 Address const& address() const { return m_address; }
174
175 bool operator==(KeyPair const& _c) const { return m_public == _c.m_public; }
176 bool operator!=(KeyPair const& _c) const { return m_public != _c.m_public; }
177
178 private:
182};
183
184namespace crypto {
185
187
189h256 kdf(Secret const& _priv, h256 const& _hash);
190
200class Nonce {
201 public:
203 static Secret get() {
204 static Nonce s;
205 return s.next();
206 }
207
208 private:
209 Nonce() = default;
210
212 Secret next();
213
214 std::mutex x_value;
216};
217
218namespace ecdh {
219
220bool agree(Secret const& _s, Public const& _r, Secret& o_s) noexcept;
221
222}
223
224namespace ecies {
225
226bytes kdf(Secret const& _z, bytes const& _s1, unsigned kdByteLen);
227
228}
229} // namespace crypto
230} // namespace dev
Definition Common.h:154
bool operator==(KeyPair const &_c) const
Definition Common.h:175
Secret const & secret() const
Definition Common.h:167
Address m_address
Definition Common.h:181
Public m_public
Definition Common.h:180
bool operator!=(KeyPair const &_c) const
Definition Common.h:176
Secret m_secret
Definition Common.h:179
Public const & pub() const
Retrieve the public key.
Definition Common.h:170
Address const & address() const
Retrieve the associated address of the public key.
Definition Common.h:173
Generator for non-repeating nonce material. The Nonce class should only be used when a non-repeating ...
Definition Common.h:200
static Secret get()
Returns the next nonce (might be read from a file).
Definition Common.h:203
Secret m_value
Definition Common.h:215
std::mutex x_value
Definition Common.h:214
Secret next()
Definition Common.cpp:266
Definition Common.h:51
Definition vector_ref.h:23
uint8_t byte
Definition Common.h:25
#define DEV_SIMPLE_EXCEPTION(X)
Definition Exceptions.h:25
bool agree(Secret const &_s, Public const &_r, Secret &o_s) noexcept
Definition Common.cpp:276
h256 kdf(Secret const &_priv, h256 const &_hash)
Key derivation.
Definition Common.cpp:255
Definition Address.h:13
FixedHash< 33 > PublicCompressed
Definition Common.h:27
SecureFixedHash< 32 > Secret
Definition Common.h:19
h512 CompactSignature
Definition Common.h:36
h520 Signature
Definition Common.h:31
Address toAddress(Public const &_public)
Convert a public key to address.
Definition Common.cpp:95
void assertThrowAux(bool _condition, ::std::string const &_errorDescription, unsigned _line, char const *_file, char const *_function)
Definition Assertions.h:53
Public recover(Signature const &_sig, h256 const &_hash)
Recovers Public key from signed message hash.
Definition Common.cpp:169
h512 toCompact(const Signature &_s)
Definition Common.cpp:214
h512 Public
Definition Common.h:23
bytesSec decryptSymNoAuth(SecureFixedHash< 16 > const &_k, h128 const &_iv, bytesConstRef _cipher)
Decrypts payload with specified IV/ctr using AES128-CTR.
Definition Common.h:131
bool verify(Public const &_k, Signature const &_s, h256 const &_hash)
Verify signature.
Definition Common.cpp:220
bool decryptECIES(Secret const &_k, bytesConstRef _cipher, bytes &o_plaintext)
Decrypt payload using ECIES standard with AES128-CTR.
Definition Common.cpp:123
bytes encryptAES128CTR(bytesConstRef _k, h128 const &_iv, bytesConstRef _plain)
Encrypts payload with specified IV/ctr using AES128-CTR.
Definition Common.cpp:139
bytesSec decryptAES128CTR(bytesConstRef _k, h128 const &_iv, bytesConstRef _cipher)
Decrypts payload with specified IV/ctr using AES128-CTR.
Definition Common.cpp:154
FixedHash< 65 > h520
Definition FixedHash.h:451
std::vector<::byte > bytes
Definition Common.h:46
Public toPublic(Secret const &_secret)
Convert a secret key into the public key equivalent.
Definition Common.cpp:57
void encrypt(Public const &_k, bytesConstRef _plain, bytes &o_cipher)
Encrypts plain text using Public key.
Definition Common.cpp:99
Signature sign(Secret const &_k, h256 const &_hash)
Returns siganture of message hash.
Definition Common.cpp:195
bool decrypt(Secret const &_k, bytesConstRef _cipher, bytes &o_plaintext)
Decrypts cipher using Secret key.
Definition Common.cpp:105
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void > > u256
Definition Common.h:98
std::pair< bytes, h128 > encryptSymNoAuth(SecureFixedHash< 16 > const &_k, bytesConstRef _plain)
Encrypts payload with random IV/ctr using AES128-CTR.
Definition Common.cpp:134
PublicCompressed toPublicCompressed(Secret const &_secret)
Convert a secret key into the public key in compressed format.
Definition Common.cpp:85
FixedHash< 32 > h256
Definition FixedHash.h:453
std::vector< Secret > Secrets
A vector of secrets.
Definition Common.h:75
void encryptECIES(Public const &_k, bytesConstRef _plain, bytes &o_cipher)
Encrypt payload using ECIES standard with AES128-CTR.
Definition Common.cpp:113
FixedHash< 64 > h512
Definition FixedHash.h:452
std::hash for asio::adress
Definition FixedHash.h:483
Definition Common.h:60
CompactSignatureStruct(h256 const &_r, h256 const &_vs)
Definition Common.h:64
CompactSignatureStruct(const Signature &_s)
Definition Common.h:63
CompactSignatureStruct(const CompactSignature &_s)
Definition Common.h:62
Definition Common.h:38
h256 r
Definition Common.h:51
SignatureStruct(Signature const &_s)
Definition Common.h:40
SignatureStruct(const CompactSignature &_s)
Definition Common.h:42
bool isValid() const noexcept
Definition Common.cpp:43
SignatureStruct()=default
h256 s
Definition Common.h:52
SignatureStruct(h256 const &_r, h256 const &_s, byte _v)
Definition Common.h:41
::byte v
Definition Common.h:53