41 const uint8_t *seckey,
size_t seckeylen) {
42 const uint8_t *end = seckey + seckeylen;
45 if (end - seckey < 1 || *seckey != 0x30u) {
50 if (end - seckey < 1 || !(*seckey & 0x80u)) {
53 ptrdiff_t lenb = *seckey & ~0x80u;
55 if (lenb < 1 || lenb > 2) {
58 if (end - seckey < lenb) {
62 ptrdiff_t len = seckey[lenb - 1] | (lenb > 1 ? seckey[lenb - 2] << 8 : 0u);
64 if (end - seckey < len) {
68 if (end - seckey < 3 || seckey[0] != 0x02u || seckey[1] != 0x01u ||
74 if (end - seckey < 2 || seckey[0] != 0x04u) {
77 ptrdiff_t oslen = seckey[1];
79 if (oslen > 32 || end - seckey < oslen) {
82 memcpy(out32 + (32 - oslen), seckey, oslen);
101 size_t *seckeylen,
const uint8_t *key32,
105 size_t pubkeylen = 0;
112 static const uint8_t begin[] = {0x30, 0x81, 0xD3, 0x02,
113 0x01, 0x01, 0x04, 0x20};
114 static const uint8_t middle[] = {
115 0xA0, 0x81, 0x85, 0x30, 0x81, 0x82, 0x02, 0x01, 0x01, 0x30, 0x2C,
116 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x01, 0x01, 0x02, 0x21,
117 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
118 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
119 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFC, 0x2F,
120 0x30, 0x06, 0x04, 0x01, 0x00, 0x04, 0x01, 0x07, 0x04, 0x21, 0x02,
121 0x79, 0xBE, 0x66, 0x7E, 0xF9, 0xDC, 0xBB, 0xAC, 0x55, 0xA0, 0x62,
122 0x95, 0xCE, 0x87, 0x0B, 0x07, 0x02, 0x9B, 0xFC, 0xDB, 0x2D, 0xCE,
123 0x28, 0xD9, 0x59, 0xF2, 0x81, 0x5B, 0x16, 0xF8, 0x17, 0x98, 0x02,
124 0x21, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
125 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xBA, 0xAE, 0xDC, 0xE6,
126 0xAF, 0x48, 0xA0, 0x3B, 0xBF, 0xD2, 0x5E, 0x8C, 0xD0, 0x36, 0x41,
127 0x41, 0x02, 0x01, 0x01, 0xA1, 0x24, 0x03, 0x22, 0x00};
128 uint8_t *ptr = seckey;
129 memcpy(ptr, begin,
sizeof(begin));
130 ptr +=
sizeof(begin);
131 memcpy(ptr, key32, 32);
133 memcpy(ptr, middle,
sizeof(middle));
134 ptr +=
sizeof(middle);
139 *seckeylen = ptr - seckey;
142 static const uint8_t begin[] = {0x30, 0x82, 0x01, 0x13, 0x02,
143 0x01, 0x01, 0x04, 0x20};
144 static const uint8_t middle[] = {
145 0xA0, 0x81, 0xA5, 0x30, 0x81, 0xA2, 0x02, 0x01, 0x01, 0x30, 0x2C,
146 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x01, 0x01, 0x02, 0x21,
147 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
148 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
149 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFC, 0x2F,
150 0x30, 0x06, 0x04, 0x01, 0x00, 0x04, 0x01, 0x07, 0x04, 0x41, 0x04,
151 0x79, 0xBE, 0x66, 0x7E, 0xF9, 0xDC, 0xBB, 0xAC, 0x55, 0xA0, 0x62,
152 0x95, 0xCE, 0x87, 0x0B, 0x07, 0x02, 0x9B, 0xFC, 0xDB, 0x2D, 0xCE,
153 0x28, 0xD9, 0x59, 0xF2, 0x81, 0x5B, 0x16, 0xF8, 0x17, 0x98, 0x48,
154 0x3A, 0xDA, 0x77, 0x26, 0xA3, 0xC4, 0x65, 0x5D, 0xA4, 0xFB, 0xFC,
155 0x0E, 0x11, 0x08, 0xA8, 0xFD, 0x17, 0xB4, 0x48, 0xA6, 0x85, 0x54,
156 0x19, 0x9C, 0x47, 0xD0, 0x8F, 0xFB, 0x10, 0xD4, 0xB8, 0x02, 0x21,
157 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
158 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xBA, 0xAE, 0xDC, 0xE6, 0xAF,
159 0x48, 0xA0, 0x3B, 0xBF, 0xD2, 0x5E, 0x8C, 0xD0, 0x36, 0x41, 0x41,
160 0x02, 0x01, 0x01, 0xA1, 0x44, 0x03, 0x42, 0x00};
161 uint8_t *ptr = seckey;
162 memcpy(ptr, begin,
sizeof(begin));
163 ptr +=
sizeof(begin);
164 memcpy(ptr, key32, 32);
166 memcpy(ptr, middle,
sizeof(middle));
167 ptr +=
sizeof(middle);
172 *seckeylen = ptr - seckey;
205 seckey.resize(seckeylen);
227 uint8_t compact_sig[64];
238 return compact_sig[0] < 0x80;
242 bool grind, uint32_t test_case)
const {
248 uint8_t extra_entropy[32] = {0};
251 uint32_t counter = 0;
255 (!grind && test_case) ? extra_entropy :
nullptr);
266 vchSig.data(), &nSigLen, &
sig);
267 vchSig.resize(nSigLen);
272 uint32_t test_case) {
277 uint8_t extra_entropy[32] = {0};
288 uint32_t test_case)
const {
293 uint32_t test_case)
const {
306 std::string str =
"Bitcoin key verification\n";
310 std::vector<uint8_t> vchSig;
316 std::vector<uint8_t> &vchSig)
const {
336 bool fSkipCheck =
false) {
338 seckey.data(), seckey.size())) {
355 std::vector<uint8_t, secure_allocator<uint8_t>> vout(64);
356 if ((nChild >> 31) == 0) {
364 memcpy(ccChild.
begin(), vout.data() + 32, 32);
365 memcpy((uint8_t *)keyChild.
begin(),
begin(), 32);
382 static const uint8_t hashkey[] = {
'B',
'i',
't',
'c',
'o',
'i',
383 'n',
' ',
's',
'e',
'e',
'd'};
384 std::vector<uint8_t, secure_allocator<uint8_t>> vout(64);
387 .Finalize(vout.data());
388 key.
Set(vout.data(), vout.data() + 32,
true);
408 code[5] = (
nChild >> 24) & 0xFF;
409 code[6] = (
nChild >> 16) & 0xFF;
410 code[7] = (
nChild >> 8) & 0xFF;
411 code[8] = (
nChild >> 0) & 0xFF;
421 nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8];
441 std::vector<uint8_t, secure_allocator<uint8_t>> vseed(32);
A hasher class for HMAC-SHA-512.
CHMAC_SHA512 & Write(const uint8_t *data, size_t len)
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
CHash256 & Write(Span< const uint8_t > input)
void Finalize(Span< uint8_t > output)
An encapsulated secp256k1 private key.
static bool Check(const uint8_t *vch)
Check whether the 32-byte array pointed to by vch is valid keydata.
bool Negate()
Negate private key.
static const unsigned int SIZE
secp256k1:
bool SignCompact(const uint256 &hash, std::vector< uint8_t > &vchSig) const
Create a compact ECDSA signature (65 bytes), which allows reconstructing the used public key.
unsigned int size() const
Simple read-only vector-like interface.
bool IsValid() const
Check whether this private key is valid.
static CKey MakeCompressedKey()
Produce a valid compressed key.
bool SignECDSA(const uint256 &hash, std::vector< uint8_t > &vchSig, bool grind=true, uint32_t test_case=0) const
Create a DER-serialized ECDSA signature.
bool fValid
see www.keylength.com script supports up to 75 for single byte push
static CKey MakeUncompressedKey()
Produce a valid uncompressed key.
const uint8_t * begin() const
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized OpenSSL private key data).
static const unsigned int COMPRESSED_SIZE
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
bool fCompressed
Whether the public key corresponding to this private key is (to be) compressed.
CPubKey GetPubKey() const
Compute the public key from a private key.
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
bool SignSchnorr(const uint256 &hash, SchnorrSig &sig, uint32_t test_case=0) const
Create a Schnorr signature.
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
bool Load(const CPrivKey &privkey, const CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
std::vector< uint8_t, secure_allocator< uint8_t > > keydata
The actual byte data.
A reference to a CKey: the Hash160 of its serialized public key.
An encapsulated public key.
bool IsCompressed() const
Check whether this is a compressed public key.
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
static constexpr unsigned int SCHNORR_SIZE
static constexpr unsigned int COMPRESSED_SIZE
bool VerifyECDSA(const uint256 &hash, const std::vector< uint8_t > &vchSig) const
Verify a DER-serialized ECDSA signature (~72 bytes).
static constexpr unsigned int SIZE
secp256k1:
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
const uint8_t * begin() const
static constexpr unsigned int SIGNATURE_SIZE
static constexpr unsigned int COMPACT_SIGNATURE_SIZE
A Span is an object that can refer to a contiguous sequence of objects.
constexpr std::size_t size() const noexcept
constexpr C * data() const noexcept
const uint8_t * data() const
static void WriteLE32(uint8_t *ptr, uint32_t x)
void BIP32Hash(const ChainCode &chainCode, uint32_t nChild, uint8_t header, const uint8_t data[32], uint8_t output[64])
int ec_seckey_import_der(const secp256k1_context *ctx, uint8_t *out32, const uint8_t *seckey, size_t seckeylen)
These functions are taken from the libsecp256k1 distribution and are very ugly.
static bool DoSignSchnorr(const CKey &key, const uint256 &hash, uint8_t *buf, uint32_t test_case)
static secp256k1_context * secp256k1_context_sign
static CKey validKey(bool compressed)
bool SigHasLowR(const secp256k1_ecdsa_signature *sig)
bool ECC_InitSanityCheck()
Check that required EC support is available at runtime.
void ECC_Start()
Initialize the elliptic curve support.
int ec_seckey_export_der(const secp256k1_context *ctx, uint8_t *seckey, size_t *seckeylen, const uint8_t *key32, bool compressed)
This serializes to a DER encoding of the ECPrivateKey type from section C.4 of SEC 1 http://www....
void ECC_Stop()
Deinitialize the elliptic curve support.
std::array< uint8_t, CPubKey::SCHNORR_SIZE > SchnorrSig
a Schnorr signature
std::vector< uint8_t, secure_allocator< uint8_t > > CPrivKey
secure_allocator is defined in allocators.h CPrivKey is a serialized private key, with all parameters...
const unsigned int BIP32_EXTKEY_SIZE
void GetRandBytes(Span< uint8_t > bytes) noexcept
================== BASE RANDOMNESS GENERATION FUNCTIONS ====================
void GetStrongRandBytes(Span< uint8_t > bytes) noexcept
Gather entropy from various sources, feed it into the internal PRNG, and generate random data using i...
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1)
Destroy a secp256k1 context object (created in dynamically allocated memory).
#define SECP256K1_CONTEXT_SIGN
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32) SECP256K1_ARG_NONNULL(1)
Updates the context randomization to protect against side-channel leakage.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_negate(const secp256k1_context *ctx, unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Negates a secret key in place.
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.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(const secp256k1_context *ctx, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Verify an ECDSA secret key.
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object (in dynamically allocated memory).
SECP256K1_API int secp256k1_ecdsa_sign(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, 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 an ECDSA signature.
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.
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
#define SECP256K1_EC_UNCOMPRESSED
SECP256K1_API const secp256k1_nonce_function secp256k1_nonce_function_rfc6979
An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
SECP256K1_API int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize an ECDSA signature in DER format.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_add(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a secret key by adding tweak to it.
SECP256K1_API int secp256k1_ecdsa_signature_serialize_compact(const secp256k1_context *ctx, unsigned char *output64, const secp256k1_ecdsa_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Serialize an ECDSA signature in compact (64 byte) format.
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).
SECP256K1_API int secp256k1_ecdsa_sign_recoverable(const secp256k1_context *ctx, secp256k1_ecdsa_recoverable_signature *sig, const unsigned char *msghash32, 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.
SECP256K1_API int secp256k1_schnorr_sign(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msghash32, 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 signature using a custom EC-Schnorr-SHA256 construction.
uint8_t * UCharCast(char *c)
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span{std::forward< V >(v)}))
Like the Span constructor, but for (const) uint8_t member types only.
void Encode(uint8_t code[BIP32_EXTKEY_SIZE]) const
CExtPubKey Neuter() const
bool Derive(CExtKey &out, unsigned int nChild) const
void Decode(const uint8_t code[BIP32_EXTKEY_SIZE])
uint8_t vchFingerprint[4]
void SetSeed(Span< const std::byte > seed)
uint8_t vchFingerprint[4]
Opaque data structured that holds a parsed ECDSA signature, supporting pubkey recovery.
Opaque data structured that holds a parsed ECDSA signature.
Opaque data structure that holds a parsed and valid public key.