36 size_t rpos, rlen, spos, slen;
39 unsigned char tmpsig[64] = {0};
46 if (pos == inputlen || input[pos] != 0x30) {
52 if (pos == inputlen) {
55 lenbyte = input[pos++];
58 if (lenbyte > inputlen - pos) {
65 if (pos == inputlen || input[pos] != 0x02) {
71 if (pos == inputlen) {
74 lenbyte = input[pos++];
77 if (lenbyte > inputlen - pos) {
80 while (lenbyte > 0 && input[pos] == 0) {
84 static_assert(
sizeof(
size_t) >= 4,
"size_t too small");
90 rlen = (rlen << 8) + input[pos];
97 if (rlen > inputlen - pos) {
104 if (pos == inputlen || input[pos] != 0x02) {
110 if (pos == inputlen) {
113 lenbyte = input[pos++];
114 if (lenbyte & 0x80) {
116 if (lenbyte > inputlen - pos) {
119 while (lenbyte > 0 && input[pos] == 0) {
123 static_assert(
sizeof(
size_t) >= 4,
"size_t too small");
128 while (lenbyte > 0) {
129 slen = (slen << 8) + input[pos];
136 if (slen > inputlen - pos) {
142 while (rlen > 0 && input[rpos] == 0) {
150 memcpy(tmpsig + 32 - rlen, input + rpos, rlen);
154 while (slen > 0 && input[spos] == 0) {
162 memcpy(tmpsig + 64 - slen, input + spos, slen);
171 memset(tmpsig, 0, 64);
185 std::vector<CKeyID> out;
189 unsigned char b[33] = {0x02};
192 fullpubkey.
Set(b, b + 33);
193 out.push_back(fullpubkey.GetID());
195 fullpubkey.Set(b, b + 33);
196 out.push_back(fullpubkey.GetID());
218 if (merkle_root ==
nullptr) {
231 uint256 tweak =
internal.ComputeTapTweakHash(&merkle_root);
243 std::pair<XOnlyPubKey, bool>
ret;
247 assert(parity == 0 || parity == 1);
258 assert(secp256k1_context_verify &&
"secp256k1_context_verify must be initialized to use CPubKey.");
274 int recid = (vchSig[0] - 27) & 3;
275 bool fComp = ((vchSig[0] - 27) & 4) != 0;
278 assert(secp256k1_context_verify &&
"secp256k1_context_verify must be initialized to use CPubKey.");
285 unsigned char pub[
SIZE];
286 size_t publen =
SIZE;
288 Set(pub, pub + publen);
296 assert(secp256k1_context_verify &&
"secp256k1_context_verify must be initialized to use CPubKey.");
304 assert(secp256k1_context_verify &&
"secp256k1_context_verify must be initialized to use CPubKey.");
308 unsigned char pub[
SIZE];
309 size_t publen =
SIZE;
311 Set(pub, pub + publen);
317 assert((nChild >> 31) == 0);
319 unsigned char out[64];
321 memcpy(ccChild.
begin(), out+32, 32);
323 assert(secp256k1_context_verify &&
"secp256k1_context_verify must be initialized to use CPubKey.");
333 pubkeyChild.
Set(pub, pub + publen);
368 if (
nDepth == std::numeric_limits<unsigned char>::max())
return false;
378 assert(secp256k1_context_verify &&
"secp256k1_context_verify must be initialized to use CPubKey.");
390 assert(secp256k1_context_verify ==
nullptr);
392 assert(secp256k1_context_verify !=
nullptr);
401 assert(secp256k1_context_verify !=
nullptr);
403 secp256k1_context_verify =
nullptr;
408 return secp256k1_context_verify;
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).
static constexpr unsigned int SIZE
secp256k1:
unsigned char vchFingerprint[4]
SECP256K1_API int secp256k1_ecdsa_signature_normalize(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sigout, const secp256k1_ecdsa_signature *sigin) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3)
Convert a signature to a normalized lower-S form.
void DecodeWithVersion(const unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE])
constexpr C * end() const noexcept
unsigned char vch[SIZE]
see www.keylength.com script supports up to 75 for single byte push
int ecdsa_signature_parse_der_lax(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *input, size_t inputlen)
This function is taken from the libsecp256k1 distribution and implements DER parsing for ECDSA signat...
uint256 ComputeTapTweakHash(const uint256 *merkle_root) const
Compute the Taproot tweak as specified in BIP341, with *this as internal key:
Opaque data structured that holds a parsed ECDSA signature, supporting pubkey recovery.
void Set(const T pbegin, const T pend)
Initialize a public key using begin/end iterators to byte data.
constexpr std::size_t size() const noexcept
void EncodeWithVersion(unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE]) const
static void WriteBE32(unsigned char *ptr, uint32_t x)
XOnlyPubKey()=default
Construct an empty x-only pubkey.
static bool CheckLowS(const std::vector< unsigned char > &vchSig)
Check whether a signature is normalized (lower-S).
bool Derive(CPubKey &pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child pubkey.
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1)
Destroy a secp256k1 context object (created in dynamically allocated memory).
static const HashWriter HASHER_TAPTWEAK
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.
const unsigned char * data() const
bool VerifySchnorr(const uint256 &msg, Span< const unsigned char > sigbytes) const
Verify a Schnorr signature against this public key.
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 *msghash32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Recover an ECDSA public key from a signature.
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
const unsigned char * begin() const
bool CheckTapTweak(const XOnlyPubKey &internal, const uint256 &merkle_root, bool parity) const
Verify that this is a Taproot tweaked output point, against a specified internal key, Merkle root, and parity.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_tweak_add(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a public key by adding tweak times the generator to it.
#define SECP256K1_EC_UNCOMPRESSED
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
static constexpr unsigned int COMPRESSED_SIZE
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid()) ...
Opaque data structure that holds a parsed and valid "x-only" public key.
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Recover a public key from a compact signature.
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
static secp256k1_context * ctx
A writer stream (for serialization) that computes a 256-bit hash.
An encapsulated public key.
static uint32_t ReadBE32(const unsigned char *ptr)
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
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.
Opaque data structured that holds a parsed ECDSA signature.
static constexpr unsigned int COMPACT_SIGNATURE_SIZE
bool IsFullyValid() const
Determine if this pubkey is fully valid.
#define SECP256K1_CONTEXT_VERIFY
Flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size, and secp256k1_context...
constexpr C * begin() const noexcept
bool Derive(CExtPubKey &out, unsigned int nChild) const
SECP256K1_API int secp256k1_ecdsa_signature_parse_compact(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *input64) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse an ECDSA signature in compact (64 bytes) format.
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
constexpr C * data() const noexcept
const unsigned int BIP32_EXTKEY_SIZE
A reference to a CKey: the Hash160 of its serialized public key.
static uint32_t ReadLE32(const unsigned char *ptr)
const unsigned int BIP32_EXTKEY_WITH_VERSION_SIZE
HashWriter TaggedHash(const std::string &tag)
Return a HashWriter primed for tagged hashes (as specified in BIP 340).
std::optional< std::pair< XOnlyPubKey, bool > > CreateTapTweak(const uint256 *merkle_root) const
Construct a Taproot tweaked output point with this point as internal 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 SECP256K1_WARN_UNUSED_RESULT int secp256k1_schnorrsig_verify(const secp256k1_context *ctx, const unsigned char *sig64, const unsigned char *msg, size_t msglen, const secp256k1_xonly_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(5)
Verify a Schnorr signature.
const secp256k1_context * GetVerifyContext()
Access to the internal secp256k1 context used for verification.
std::vector< CKeyID > GetKeyIDs() const
Returns a list of CKeyIDs for the CPubKeys that could have been used to create this XOnlyPubKey...
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(const secp256k1_context *ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const secp256k1_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Verify an ECDSA signature.
bool Decompress()
Turn this public key into an uncompressed public key.
Opaque data structure that holds a parsed and valid public key.
const unsigned char * data() const