48 static std::unique_ptr<secp256k1_context, decltype(&secp256k1_context_destroy)> s_ctx{
59 static const h256 s_max{
"0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141"};
60 static const h256 s_zero;
62 return (v <= 1 && r > s_zero && s > s_zero && r < s_max && s < s_max);
74 std::array<byte, 65> serializedPubkey;
75 size_t serializedPubkeySize = serializedPubkey.size();
77 ctx, serializedPubkey.data(), &serializedPubkeySize,
80 assert(serializedPubkeySize == serializedPubkey.size());
82 assert(serializedPubkey[0] == 0x04);
84 return Public{&serializedPubkey[1], Public::ConstructFromPointer};
105 Secp256k1PP::get()->encrypt(_k, io);
106 o_cipher = std::move(io);
112 Secp256k1PP::get()->decrypt(_k, io);
115 o_plaintext = std::move(io);
127 Secp256k1PP::get()->encryptECIES(_k, _sharedMacData, io);
128 o_cipher = std::move(io);
139 if (!Secp256k1PP::get()->
decryptECIES(_k, _sharedMacData, io))
141 o_plaintext = std::move(io);
154 return decrypt(_k, _cipher, o_plain);
159 h128 iv(Nonce::get().makeInsecure());
165 if (_k.
size() != 16 && _k.
size() != 24 && _k.
size() != 32)
171 e.SetKeyWithIV(key, key.size(), _iv.
data());
173 e.ProcessData(ret.data(), _plain.
data(), _plain.
size());
176 catch (CryptoPP::Exception& _e)
178 cerr << _e.what() << endl;
185 if (_k.
size() != 16 && _k.
size() != 24 && _k.
size() != 32)
191 d.SetKeyWithIV(key, key.size(), _iv.
data());
193 d.ProcessData(ret.writable().data(), _cipher.
data(), _cipher.
size());
196 catch (CryptoPP::Exception& _e)
198 cerr << _e.what() << endl;
209 auto* ctx = getCtx();
218 std::array<byte, 65> serializedPubkey;
219 size_t serializedPubkeySize = serializedPubkey.size();
221 ctx, serializedPubkey.data(), &serializedPubkeySize,
224 assert(serializedPubkeySize == serializedPubkey.size());
226 assert(serializedPubkey[0] == 0x04);
228 return Public{&serializedPubkey[1], Public::ConstructFromPointer};
231 static const u256 c_secp256k1n(
"115792089237316195423570985008687907852837564279074904382605163141518161494337");
235 auto* ctx = getCtx();
245 ss.
v =
static_cast<byte>(v);
246 if (ss.s > c_secp256k1n / 2)
248 ss.v =
static_cast<byte>(ss.v ^ 1);
249 ss.s =
h256(c_secp256k1n -
u256(ss.s));
251 assert(ss.s <= c_secp256k1n / 2);
260 return _p ==
recover(_s, _hash);
270 reinterpret_cast<byte const*
>(_pass.data()),
276 BOOST_THROW_EXCEPTION(CryptoException() <<
errinfo_comment(
"Key derivation failed."));
284 reinterpret_cast<uint8_t const*>(_pass.data()),
294 BOOST_THROW_EXCEPTION(CryptoException() <<
errinfo_comment(
"Key derivation failed."));
328 sha3(s.ref(), s.ref());
330 if (!s || !_hash || !_priv)
331 BOOST_THROW_EXCEPTION(InvalidState());
342 BOOST_THROW_EXCEPTION(InvalidState());
345 return sha3(~m_value);
350 auto* ctx = getCtx();
351 static_assert(
sizeof(
Secret) == 32,
"Invalid Secret type size");
353 std::array<byte, 65> serializedPubKey{{0x04}};
354 std::copy(_r.
asArray().begin(), _r.
asArray().end(), serializedPubKey.begin() + 1);
357 std::array<byte, 33> compressedPoint;
361 r = secp256k1_ecdh_raw(ctx, compressedPoint.data(), &rawPubkey, _s.
data());
364 std::copy(compressedPoint.begin() + 1, compressedPoint.end(), o_s.
writable().
data());
SECP256K1_API int secp256k1_ecdsa_recoverable_signature_parse_compact(const secp256k1_context *ctx, secp256k1_ecdsa_recoverable_signature *sig, const unsigned char *input64, int recid) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse a compact ECDSA signature (64 bytes + recovery id).
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
void encryptSym(Secret const &_k, bytesConstRef _plain, bytes &o_cipher)
Symmetric encryption.
vector_ref< _T const > ref(_T const &_t)
byte const * data() const
Class file for modes of operation.
h160 right160(h256 const &_t)
Convert the given value into h160 (160-bit unsigned integer) using the right 20 bytes.
bytesSec pbkdf2(std::string const &_pass, bytes const &_salt, unsigned _iterations, unsigned _dkLen=32)
Derive key via PBKDF2.
bool decryptECIES(Secret const &_k, bytesConstRef _cipher, bytes &o_plaintext)
Decrypt payload using ECIES standard with AES128-CTR.
bool verify(Public const &_k, Signature const &_s, h256 const &_hash)
Verify signature.
Simple class that represents a "key pair".
Opaque data structured that holds a parsed ECDSA signature, supporting pubkey recovery.
secure_vector< byte > bytesSec
h160 Address
An Ethereum address: 20 bytes.
SecureFixedHash< 32 > sha3Secure(bytesConstRef _input)
std::pair< bytes, h128 > encryptSymNoAuth(SecureFixedHash< 16 > const &_k, bytesConstRef _plain)
Encrypts payload with random IV/ctr using AES128-CTR.
static KeyPair create()
Create a new, randomly generated object.
SecureFixedHash< 32 > Secret
std::hash for asio::adress
assert(len-trim+(2 *lenIndices)<=WIDTH)
void encryptECIES(Public const &_k, bytesConstRef _plain, bytes &o_cipher)
Encrypt payload using ECIES standard with AES128-CTR.
SECP256K1_API int secp256k1_ec_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey *pubkey, unsigned int flags) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize a pubkey object into a serialized byte sequence.
#define SECP256K1_CONTEXT_SIGN
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute the public key for a secret key.
Public toPublic(Secret const &_secret)
Convert a secret key into the public key equivalent.
#define SECP256K1_EC_UNCOMPRESSED
bytesConstRef ref() const
int libscrypt_scrypt(const uint8_t *passwd, size_t passwdlen, const uint8_t *salt, size_t saltlen, uint64_t N, uint32_t r, uint32_t p, uint8_t *buf, size_t buflen)
crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen): Compute scrypt(passwd[0 ...
bool decryptSym(Secret const &_k, bytesConstRef _cipher, bytes &o_plaintext)
Symmetric decryption.
static SecureFixedHash< T > random()
Block cipher mode of operation aggregate.
std::lock_guard< std::mutex > Guard
KeyPair()=default
Null constructor.
Class file for the AES cipher (Rijndael)
Address ZeroAddress
The zero address.
SECP256K1_API int secp256k1_ecdsa_sign_recoverable(const secp256k1_context *ctx, secp256k1_ecdsa_recoverable_signature *sig, const unsigned char *msg32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create a recoverable ECDSA signature.
Public recover(Signature const &_sig, h256 const &_hash)
Recovers Public key from signed message hash.
std::vector< byte > bytes
std::vector< unsigned char > toBytes() const
vector_ref< byte const > bytesConstRef
static KeyPair fromEncryptedSeed(bytesConstRef _seed, std::string const &_password)
Create from an encrypted seed.
h256 kdf(Secret const &_priv, h256 const &_hash)
Key derivation.
FixedHash< T > & writable()
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *input, size_t inputlen) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse a variable-length public key into the pubkey object.
Address toAddress(std::string const &_s)
Convert the given string into an address.
SECP256K1_API int secp256k1_ecdsa_recoverable_signature_serialize_compact(const secp256k1_context *ctx, unsigned char *output64, int *recid, const secp256k1_ecdsa_recoverable_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize an ECDSA signature in compact format (64 bytes + recovery id).
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Address toAddress(Public const &_public)
Convert a public key to address.
std::vector< T > & writable()
bytes rlpList()
Export a list of items in RLP format, returning a byte array.
Address const & address() const
Retrieve the associated address of the public key.
bytesSec scrypt(std::string const &_pass, bytes const &_salt, uint64_t _n, uint32_t _r, uint32_t _p, unsigned _dkLen)
Derive key via Scrypt.
Classes for SHA-1 and SHA-2 family of message digests.
#define SECP256K1_CONTEXT_VERIFY
Flags to pass to secp256k1_context_create.
Signature sign(Secret const &_k, h256 const &_hash)
Returns siganture of message hash.
bytesSec decryptAES128CTR(bytesConstRef _k, h128 const &_iv, bytesConstRef _cipher)
Decrypts payload with specified IV/ctr using AES128-CTR.
Password based key derivation functions.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_recover(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const secp256k1_ecdsa_recoverable_signature *sig, const unsigned char *msg32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Recover an ECDSA public key from a signature.
void encrypt(Public const &_k, bytesConstRef _plain, bytes &o_cipher)
Encrypts plain text using Public key.
bytes aesDecrypt(bytesConstRef _cipher, std::string const &_password, unsigned _rounds=2000, bytesConstRef _salt=bytesConstRef())
bool decrypt(Secret const &_k, bytesConstRef _cipher, bytes &o_plaintext)
Decrypts cipher using Secret key.
boost::error_info< struct tag_comment, std::string > errinfo_comment
void agree(Secret const &_s, Public const &_r, Secret &o_s)
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
bytes encryptAES128CTR(bytesConstRef _k, h128 const &_iv, bytesConstRef _plain)
Encrypts payload with specified IV/ctr using AES128-CTR.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdh(const secp256k1_context *ctx, unsigned char *result, const secp256k1_pubkey *pubkey, const unsigned char *privkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Compute an EC Diffie-Hellman secret in constant time Returns: 1: exponentiation was successful 0: sca...
std::array< byte, N > & asArray()
void sha3mac(bytesConstRef _secret, bytesConstRef _plain, bytesRef _output)
Calculate SHA3-256 MAC.
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object.
bool isValid() const noexcept
Opaque data structure that holds a parsed and valid public key.