20#include <xrpl/basics/Buffer.h>
21#include <xrpl/basics/Slice.h>
22#include <xrpl/basics/base_uint.h>
23#include <xrpl/basics/contract.h>
24#include <xrpl/basics/strHex.h>
25#include <xrpl/beast/utility/rngfill.h>
26#include <xrpl/crypto/csprng.h>
27#include <xrpl/crypto/secure_erase.h>
28#include <xrpl/protocol/KeyType.h>
29#include <xrpl/protocol/PublicKey.h>
30#include <xrpl/protocol/SecretKey.h>
31#include <xrpl/protocol/Seed.h>
32#include <xrpl/protocol/detail/secp256k1.h>
33#include <xrpl/protocol/digest.h>
34#include <xrpl/protocol/tokens.h>
36#include <boost/utility/string_view.hpp>
63 LogicError(
"SecretKey::SecretKey: invalid size");
79 *
out++ = (v >> 16) & 0xff;
80 *
out++ = (v >> 8) & 0xff;
115 Throw<std::runtime_error>(
"Unable to derive generator from seed");
174 Throw<std::runtime_error>(
"Unable to derive generator from seed");
181 secp256k1_pubkey pubkey;
182 if (secp256k1_ec_pubkey_create(
184 LogicError(
"derivePublicKey: secp256k1_ec_pubkey_create failed");
188 if (secp256k1_ec_pubkey_serialize(
193 SECP256K1_EC_COMPRESSED) != 1)
194 LogicError(
"derivePublicKey: secp256k1_ec_pubkey_serialize failed");
211 if (secp256k1_ec_seckey_tweak_add(
232 LogicError(
"sign: secp256k1 required for digest signing");
234 BOOST_ASSERT(sk.
size() == 32);
235 secp256k1_ecdsa_signature sig_imp;
236 if (secp256k1_ecdsa_sign(
239 reinterpret_cast<unsigned char const*
>(
digest.data()),
240 reinterpret_cast<unsigned char const*
>(sk.
data()),
241 secp256k1_nonce_function_rfc6979,
243 LogicError(
"sign: secp256k1_ecdsa_sign failed");
245 unsigned char sig[72];
246 size_t len =
sizeof(sig);
247 if (secp256k1_ecdsa_signature_serialize_der(
249 LogicError(
"sign: secp256k1_ecdsa_signature_serialize_der failed");
273 secp256k1_ecdsa_signature sig_imp;
274 if (secp256k1_ecdsa_sign(
277 reinterpret_cast<unsigned char const*
>(
digest.data()),
278 reinterpret_cast<unsigned char const*
>(sk.
data()),
279 secp256k1_nonce_function_rfc6979,
281 LogicError(
"sign: secp256k1_ecdsa_sign failed");
283 unsigned char sig[72];
284 size_t len =
sizeof(sig);
285 if (secp256k1_ecdsa_signature_serialize_der(
288 "sign: secp256k1_ecdsa_signature_serialize_der failed");
326 LogicError(
"generateSecretKey: unknown key type");
335 secp256k1_pubkey pubkey_imp;
336 if (secp256k1_ec_pubkey_create(
339 reinterpret_cast<unsigned char const*
>(sk.
data())) != 1)
341 "derivePublicKey: secp256k1_ec_pubkey_create failed");
343 unsigned char pubkey[33];
345 if (secp256k1_ec_pubkey_serialize(
350 SECP256K1_EC_COMPRESSED) != 1)
352 "derivePublicKey: secp256k1_ec_pubkey_serialize failed");
357 unsigned char buf[33];
359 ed25519_publickey(sk.
data(), &buf[1]);
398 if (result.size() != 32)
Like std::vector<char> but better.
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
std::uint8_t const * data() const noexcept
Slice slice() const noexcept
std::uint8_t const * data() const
std::string to_string() const
Convert the secret key to a hexadecimal string.
Seeds are used to generate deterministic secret keys.
const_iterator end() const noexcept
const_iterator begin() const noexcept
std::uint8_t const * data() const
An immutable linear range of bytes.
std::uint8_t const * data() const noexcept
Return a pointer to beginning of the storage.
std::size_t size() const noexcept
Returns the number of bytes in the storage.
static constexpr std::size_t size()
Produces a sequence of secp256k1 key pairs.
uint256 calculateTweak(std::uint32_t seq) const
std::array< std::uint8_t, 33 > generator_
std::pair< PublicKey, SecretKey > operator()(std::size_t ordinal) const
Generate the nth key pair.
Generator(Seed const &seed)
void rngfill(void *buffer, std::size_t bytes, Generator &g)
void copy_uint32(std::uint8_t *out, std::uint32_t v)
uint256 deriveDeterministicRootKey(Seed const &seed)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
secp256k1_context const * secp256k1Context()
sha512_half_hasher_s::result_type sha512Half_s(Args const &... args)
Returns the SHA512-Half of a series of objects.
std::optional< AccountID > parseBase58(std::string const &s)
Parse AccountID from checked, base58 string.
std::pair< PublicKey, SecretKey > generateKeyPair(KeyType type, Seed const &seed)
Generate a key pair deterministically.
csprng_engine & crypto_prng()
The default cryptographically secure PRNG.
PublicKey derivePublicKey(KeyType type, SecretKey const &sk)
Derive the public key from a secret key.
SecretKey generateSecretKey(KeyType type, Seed const &seed)
Generate a new secret key deterministically.
Buffer sign(PublicKey const &pk, SecretKey const &sk, Slice const &message)
Generate a signature for a message.
std::string decodeBase58Token(std::string const &s, TokenType type)
std::optional< KeyType > publicKeyType(Slice const &slice)
Returns the type of public key.
std::string strHex(FwdIt begin, FwdIt end)
static Hasher::result_type digest(void const *data, std::size_t size) noexcept
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
SecretKey randomSecretKey()
Create a secret key using secure random numbers.
Buffer signDigest(PublicKey const &pk, SecretKey const &sk, uint256 const &digest)
Generate a signature for a message digest.
std::pair< PublicKey, SecretKey > randomKeyPair(KeyType type)
Create a key pair using secure random numbers.
sha512_half_hasher::result_type sha512Half(Args const &... args)
Returns the SHA512-Half of a series of objects.
void LogicError(std::string const &how) noexcept
Called when faulty logic causes a broken invariant.
void secure_erase(void *dest, std::size_t bytes)
Attempts to clear the given blob of memory.
Returns the SHA512-Half digest of a message.