LCOV - code coverage report
Current view: top level - src - pubkey.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 98.9 % 267 264
Test Date: 2024-09-01 05:20:30 Functions: 100.0 % 22 22
Branches: 81.4 % 156 127

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2022 The Bitcoin Core developers
       2                 :             : // Copyright (c) 2017 The Zcash developers
       3                 :             : // Distributed under the MIT software license, see the accompanying
       4                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5                 :             : 
       6                 :             : #include <pubkey.h>
       7                 :             : 
       8                 :             : #include <hash.h>
       9                 :             : #include <secp256k1.h>
      10                 :             : #include <secp256k1_ellswift.h>
      11                 :             : #include <secp256k1_extrakeys.h>
      12                 :             : #include <secp256k1_recovery.h>
      13                 :             : #include <secp256k1_schnorrsig.h>
      14                 :             : #include <span.h>
      15                 :             : #include <uint256.h>
      16                 :             : #include <util/strencodings.h>
      17                 :             : 
      18                 :             : #include <algorithm>
      19                 :             : #include <cassert>
      20                 :             : 
      21                 :             : using namespace util::hex_literals;
      22                 :             : 
      23                 :             : namespace {
      24                 :             : 
      25                 :             : struct Secp256k1SelfTester
      26                 :             : {
      27                 :           1 :     Secp256k1SelfTester() {
      28                 :             :         /* Run libsecp256k1 self-test before using the secp256k1_context_static. */
      29                 :           1 :         secp256k1_selftest();
      30                 :           1 :     }
      31                 :           1 : } SECP256K1_SELFTESTER;
      32                 :             : 
      33                 :             : } // namespace
      34                 :             : 
      35                 :             : /** This function is taken from the libsecp256k1 distribution and implements
      36                 :             :  *  DER parsing for ECDSA signatures, while supporting an arbitrary subset of
      37                 :             :  *  format violations.
      38                 :             :  *
      39                 :             :  *  Supported violations include negative integers, excessive padding, garbage
      40                 :             :  *  at the end, and overly long length descriptors. This is safe to use in
      41                 :             :  *  Bitcoin because since the activation of BIP66, signatures are verified to be
      42                 :             :  *  strict DER before being passed to this module, and we know it supports all
      43                 :             :  *  violations present in the blockchain before that point.
      44                 :             :  */
      45                 :      198507 : int ecdsa_signature_parse_der_lax(secp256k1_ecdsa_signature* sig, const unsigned char *input, size_t inputlen) {
      46                 :      198507 :     size_t rpos, rlen, spos, slen;
      47                 :      198507 :     size_t pos = 0;
      48                 :      198507 :     size_t lenbyte;
      49                 :      198507 :     unsigned char tmpsig[64] = {0};
      50                 :      198507 :     int overflow = 0;
      51                 :             : 
      52                 :             :     /* Hack to initialize sig with a correctly-parsed but invalid signature. */
      53                 :      198507 :     secp256k1_ecdsa_signature_parse_compact(secp256k1_context_static, sig, tmpsig);
      54                 :             : 
      55                 :             :     /* Sequence tag byte */
      56   [ +  +  +  + ]:      198507 :     if (pos == inputlen || input[pos] != 0x30) {
      57                 :        2994 :         return 0;
      58                 :             :     }
      59                 :      195513 :     pos++;
      60                 :             : 
      61                 :             :     /* Sequence length bytes */
      62         [ +  + ]:      195513 :     if (pos == inputlen) {
      63                 :          81 :         return 0;
      64                 :             :     }
      65                 :      195432 :     lenbyte = input[pos++];
      66         [ +  + ]:      195432 :     if (lenbyte & 0x80) {
      67                 :        1524 :         lenbyte -= 0x80;
      68         [ +  + ]:        1524 :         if (lenbyte > inputlen - pos) {
      69                 :         513 :             return 0;
      70                 :             :         }
      71                 :        1011 :         pos += lenbyte;
      72                 :        1011 :     }
      73                 :             : 
      74                 :             :     /* Integer tag byte for R */
      75   [ +  +  +  + ]:      194919 :     if (pos == inputlen || input[pos] != 0x02) {
      76                 :        1403 :         return 0;
      77                 :             :     }
      78                 :      193516 :     pos++;
      79                 :             : 
      80                 :             :     /* Integer length for R */
      81         [ +  + ]:      193516 :     if (pos == inputlen) {
      82                 :          82 :         return 0;
      83                 :             :     }
      84                 :      193434 :     lenbyte = input[pos++];
      85         [ +  + ]:      193434 :     if (lenbyte & 0x80) {
      86                 :        2781 :         lenbyte -= 0x80;
      87         [ +  + ]:        2781 :         if (lenbyte > inputlen - pos) {
      88                 :        1226 :             return 0;
      89                 :             :         }
      90   [ +  +  +  + ]:        3523 :         while (lenbyte > 0 && input[pos] == 0) {
      91                 :        1968 :             pos++;
      92                 :        1968 :             lenbyte--;
      93                 :             :         }
      94                 :             :         static_assert(sizeof(size_t) >= 4, "size_t too small");
      95         [ +  + ]:        1555 :         if (lenbyte >= 4) {
      96                 :         291 :             return 0;
      97                 :             :         }
      98                 :        1264 :         rlen = 0;
      99         [ +  + ]:        3467 :         while (lenbyte > 0) {
     100                 :        2203 :             rlen = (rlen << 8) + input[pos];
     101                 :        2203 :             pos++;
     102                 :        2203 :             lenbyte--;
     103                 :             :         }
     104                 :        1264 :     } else {
     105                 :      190653 :         rlen = lenbyte;
     106                 :             :     }
     107         [ +  + ]:      191917 :     if (rlen > inputlen - pos) {
     108                 :         748 :         return 0;
     109                 :             :     }
     110                 :      191169 :     rpos = pos;
     111                 :      191169 :     pos += rlen;
     112                 :             : 
     113                 :             :     /* Integer tag byte for S */
     114   [ +  +  +  + ]:      191169 :     if (pos == inputlen || input[pos] != 0x02) {
     115                 :         960 :         return 0;
     116                 :             :     }
     117                 :      190209 :     pos++;
     118                 :             : 
     119                 :             :     /* Integer length for S */
     120         [ +  + ]:      190209 :     if (pos == inputlen) {
     121                 :         114 :         return 0;
     122                 :             :     }
     123                 :      190095 :     lenbyte = input[pos++];
     124         [ +  + ]:      190095 :     if (lenbyte & 0x80) {
     125                 :        1377 :         lenbyte -= 0x80;
     126         [ +  + ]:        1377 :         if (lenbyte > inputlen - pos) {
     127                 :         763 :             return 0;
     128                 :             :         }
     129   [ +  +  +  + ]:        2148 :         while (lenbyte > 0 && input[pos] == 0) {
     130                 :        1534 :             pos++;
     131                 :        1534 :             lenbyte--;
     132                 :             :         }
     133                 :             :         static_assert(sizeof(size_t) >= 4, "size_t too small");
     134         [ +  + ]:         614 :         if (lenbyte >= 4) {
     135                 :         121 :             return 0;
     136                 :             :         }
     137                 :         493 :         slen = 0;
     138         [ +  + ]:        1032 :         while (lenbyte > 0) {
     139                 :         539 :             slen = (slen << 8) + input[pos];
     140                 :         539 :             pos++;
     141                 :         539 :             lenbyte--;
     142                 :             :         }
     143                 :         493 :     } else {
     144                 :      188718 :         slen = lenbyte;
     145                 :             :     }
     146         [ +  + ]:      189211 :     if (slen > inputlen - pos) {
     147                 :        1541 :         return 0;
     148                 :             :     }
     149                 :      187670 :     spos = pos;
     150                 :             : 
     151                 :             :     /* Ignore leading zeroes in R */
     152   [ +  +  +  + ]:      225267 :     while (rlen > 0 && input[rpos] == 0) {
     153                 :       37597 :         rlen--;
     154                 :       37597 :         rpos++;
     155                 :             :     }
     156                 :             :     /* Copy R value */
     157         [ +  + ]:      187670 :     if (rlen > 32) {
     158                 :        1520 :         overflow = 1;
     159                 :        1520 :     } else {
     160                 :      186150 :         memcpy(tmpsig + 32 - rlen, input + rpos, rlen);
     161                 :             :     }
     162                 :             : 
     163                 :             :     /* Ignore leading zeroes in S */
     164   [ +  +  +  + ]:      209069 :     while (slen > 0 && input[spos] == 0) {
     165                 :       21399 :         slen--;
     166                 :       21399 :         spos++;
     167                 :             :     }
     168                 :             :     /* Copy S value */
     169         [ +  + ]:      187670 :     if (slen > 32) {
     170                 :        1718 :         overflow = 1;
     171                 :        1718 :     } else {
     172                 :      185952 :         memcpy(tmpsig + 64 - slen, input + spos, slen);
     173                 :             :     }
     174                 :             : 
     175         [ +  + ]:      187670 :     if (!overflow) {
     176                 :      184466 :         overflow = !secp256k1_ecdsa_signature_parse_compact(secp256k1_context_static, sig, tmpsig);
     177                 :      184466 :     }
     178         [ +  + ]:      187670 :     if (overflow) {
     179                 :             :         /* Overwrite the result again with a correctly-parsed but invalid
     180                 :             :            signature if parsing failed. */
     181                 :        4134 :         memset(tmpsig, 0, 64);
     182                 :        4134 :         secp256k1_ecdsa_signature_parse_compact(secp256k1_context_static, sig, tmpsig);
     183                 :        4134 :     }
     184                 :      187670 :     return 1;
     185                 :      198507 : }
     186                 :             : 
     187                 :             : /** Nothing Up My Sleeve (NUMS) point
     188                 :             :  *
     189                 :             :  *  NUMS_H is a point with an unknown discrete logarithm, constructed by taking the sha256 of 'g'
     190                 :             :  *  (uncompressed encoding), which happens to be a point on the curve.
     191                 :             :  *
     192                 :             :  *  For an example script for calculating H, refer to the unit tests in
     193                 :             :  *  ./test/functional/test_framework/crypto/secp256k1.py
     194                 :             :  */
     195                 :             : constexpr XOnlyPubKey XOnlyPubKey::NUMS_H{"50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0"_hex_u8};
     196                 :             : 
     197                 :      789629 : std::vector<CKeyID> XOnlyPubKey::GetKeyIDs() const
     198                 :             : {
     199                 :      789629 :     std::vector<CKeyID> out;
     200                 :             :     // For now, use the old full pubkey-based key derivation logic. As it is indexed by
     201                 :             :     // Hash160(full pubkey), we need to return both a version prefixed with 0x02, and one
     202                 :             :     // with 0x03.
     203                 :      789629 :     unsigned char b[33] = {0x02};
     204   [ +  -  +  -  :      789629 :     std::copy(m_keydata.begin(), m_keydata.end(), b + 1);
                   +  - ]
     205         [ +  - ]:      789629 :     CPubKey fullpubkey;
     206         [ +  - ]:      789629 :     fullpubkey.Set(b, b + 33);
     207   [ +  -  +  - ]:      789629 :     out.push_back(fullpubkey.GetID());
     208                 :      789629 :     b[0] = 0x03;
     209         [ +  - ]:      789629 :     fullpubkey.Set(b, b + 33);
     210   [ +  -  +  - ]:      789629 :     out.push_back(fullpubkey.GetID());
     211                 :      789629 :     return out;
     212         [ +  - ]:      789629 : }
     213                 :             : 
     214                 :      787329 : CPubKey XOnlyPubKey::GetEvenCorrespondingCPubKey() const
     215                 :             : {
     216                 :      787329 :     unsigned char full_key[CPubKey::COMPRESSED_SIZE] = {0x02};
     217                 :      787329 :     std::copy(begin(), end(), full_key + 1);
     218                 :      787329 :     return CPubKey{full_key};
     219                 :      787329 : }
     220                 :             : 
     221                 :     1068803 : bool XOnlyPubKey::IsFullyValid() const
     222                 :             : {
     223                 :     1068803 :     secp256k1_xonly_pubkey pubkey;
     224                 :     2137606 :     return secp256k1_xonly_pubkey_parse(secp256k1_context_static, &pubkey, m_keydata.data());
     225                 :     1068803 : }
     226                 :             : 
     227                 :        1876 : bool XOnlyPubKey::VerifySchnorr(const uint256& msg, Span<const unsigned char> sigbytes) const
     228                 :             : {
     229         [ +  - ]:        1876 :     assert(sigbytes.size() == 64);
     230                 :        1876 :     secp256k1_xonly_pubkey pubkey;
     231         [ +  + ]:        1876 :     if (!secp256k1_xonly_pubkey_parse(secp256k1_context_static, &pubkey, m_keydata.data())) return false;
     232                 :        1787 :     return secp256k1_schnorrsig_verify(secp256k1_context_static, sigbytes.data(), msg.begin(), 32, &pubkey);
     233                 :        1876 : }
     234                 :             : 
     235   [ +  -  +  - ]:           1 : static const HashWriter HASHER_TAPTWEAK{TaggedHash("TapTweak")};
     236                 :             : 
     237                 :     1059078 : uint256 XOnlyPubKey::ComputeTapTweakHash(const uint256* merkle_root) const
     238                 :             : {
     239         [ +  + ]:     1059078 :     if (merkle_root == nullptr) {
     240                 :             :         // We have no scripts. The actual tweak does not matter, but follow BIP341 here to
     241                 :             :         // allow for reproducible tweaking.
     242                 :     1051080 :         return (HashWriter{HASHER_TAPTWEAK} << m_keydata).GetSHA256();
     243                 :             :     } else {
     244                 :        7998 :         return (HashWriter{HASHER_TAPTWEAK} << m_keydata << *merkle_root).GetSHA256();
     245                 :             :     }
     246                 :     1059078 : }
     247                 :             : 
     248                 :        3943 : bool XOnlyPubKey::CheckTapTweak(const XOnlyPubKey& internal, const uint256& merkle_root, bool parity) const
     249                 :             : {
     250                 :        3943 :     secp256k1_xonly_pubkey internal_key;
     251         [ +  + ]:        3943 :     if (!secp256k1_xonly_pubkey_parse(secp256k1_context_static, &internal_key, internal.data())) return false;
     252                 :        2509 :     uint256 tweak = internal.ComputeTapTweakHash(&merkle_root);
     253                 :        2509 :     return secp256k1_xonly_pubkey_tweak_add_check(secp256k1_context_static, m_keydata.begin(), parity, &internal_key, tweak.begin());
     254                 :        3943 : }
     255                 :             : 
     256                 :     1056569 : std::optional<std::pair<XOnlyPubKey, bool>> XOnlyPubKey::CreateTapTweak(const uint256* merkle_root) const
     257                 :             : {
     258                 :     1056569 :     secp256k1_xonly_pubkey base_point;
     259         [ +  - ]:     1056569 :     if (!secp256k1_xonly_pubkey_parse(secp256k1_context_static, &base_point, data())) return std::nullopt;
     260                 :     1056569 :     secp256k1_pubkey out;
     261                 :     1056569 :     uint256 tweak = ComputeTapTweakHash(merkle_root);
     262         [ +  - ]:     1056569 :     if (!secp256k1_xonly_pubkey_tweak_add(secp256k1_context_static, &out, &base_point, tweak.data())) return std::nullopt;
     263                 :     1056569 :     int parity = -1;
     264                 :     1056569 :     std::pair<XOnlyPubKey, bool> ret;
     265                 :     1056569 :     secp256k1_xonly_pubkey out_xonly;
     266         [ +  - ]:     1056569 :     if (!secp256k1_xonly_pubkey_from_pubkey(secp256k1_context_static, &out_xonly, &parity, &out)) return std::nullopt;
     267                 :     1056569 :     secp256k1_xonly_pubkey_serialize(secp256k1_context_static, ret.first.begin(), &out_xonly);
     268   [ +  +  +  - ]:     1056569 :     assert(parity == 0 || parity == 1);
     269                 :     1056569 :     ret.second = parity;
     270                 :     1056569 :     return ret;
     271                 :     1056569 : }
     272                 :             : 
     273                 :             : 
     274                 :      103158 : bool CPubKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchSig) const {
     275         [ +  + ]:      103158 :     if (!IsValid())
     276                 :          12 :         return false;
     277                 :      103146 :     secp256k1_pubkey pubkey;
     278                 :      103146 :     secp256k1_ecdsa_signature sig;
     279         [ +  + ]:      103146 :     if (!secp256k1_ec_pubkey_parse(secp256k1_context_static, &pubkey, vch, size())) {
     280                 :        3414 :         return false;
     281                 :             :     }
     282         [ +  + ]:       99732 :     if (!ecdsa_signature_parse_der_lax(&sig, vchSig.data(), vchSig.size())) {
     283                 :       10248 :         return false;
     284                 :             :     }
     285                 :             :     /* libsecp256k1's ECDSA verification requires lower-S signatures, which have
     286                 :             :      * not historically been enforced in Bitcoin, so normalize them first. */
     287                 :       89484 :     secp256k1_ecdsa_signature_normalize(secp256k1_context_static, &sig, &sig);
     288                 :       89484 :     return secp256k1_ecdsa_verify(secp256k1_context_static, &sig, hash.begin(), &pubkey);
     289                 :      103158 : }
     290                 :             : 
     291                 :        1258 : bool CPubKey::RecoverCompact(const uint256 &hash, const std::vector<unsigned char>& vchSig) {
     292         [ +  + ]:        1258 :     if (vchSig.size() != COMPACT_SIGNATURE_SIZE)
     293                 :           2 :         return false;
     294                 :        1256 :     int recid = (vchSig[0] - 27) & 3;
     295                 :        1256 :     bool fComp = ((vchSig[0] - 27) & 4) != 0;
     296                 :        1256 :     secp256k1_pubkey pubkey;
     297                 :        1256 :     secp256k1_ecdsa_recoverable_signature sig;
     298         [ +  + ]:        1256 :     if (!secp256k1_ecdsa_recoverable_signature_parse_compact(secp256k1_context_static, &sig, &vchSig[1], recid)) {
     299                 :           1 :         return false;
     300                 :             :     }
     301         [ +  + ]:        1255 :     if (!secp256k1_ecdsa_recover(secp256k1_context_static, &pubkey, &sig, hash.begin())) {
     302                 :           8 :         return false;
     303                 :             :     }
     304                 :        1247 :     unsigned char pub[SIZE];
     305                 :        1247 :     size_t publen = SIZE;
     306                 :        1247 :     secp256k1_ec_pubkey_serialize(secp256k1_context_static, pub, &publen, &pubkey, fComp ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
     307                 :        1247 :     Set(pub, pub + publen);
     308                 :        1247 :     return true;
     309                 :        1258 : }
     310                 :             : 
     311                 :      314639 : bool CPubKey::IsFullyValid() const {
     312         [ +  + ]:      314639 :     if (!IsValid())
     313                 :       73560 :         return false;
     314                 :      241079 :     secp256k1_pubkey pubkey;
     315                 :      241079 :     return secp256k1_ec_pubkey_parse(secp256k1_context_static, &pubkey, vch, size());
     316                 :      314639 : }
     317                 :             : 
     318                 :       76715 : bool CPubKey::Decompress() {
     319         [ -  + ]:       76715 :     if (!IsValid())
     320                 :           0 :         return false;
     321                 :       76715 :     secp256k1_pubkey pubkey;
     322         [ +  + ]:       76715 :     if (!secp256k1_ec_pubkey_parse(secp256k1_context_static, &pubkey, vch, size())) {
     323                 :       40411 :         return false;
     324                 :             :     }
     325                 :       36304 :     unsigned char pub[SIZE];
     326                 :       36304 :     size_t publen = SIZE;
     327                 :       36304 :     secp256k1_ec_pubkey_serialize(secp256k1_context_static, pub, &publen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
     328                 :       36304 :     Set(pub, pub + publen);
     329                 :       36304 :     return true;
     330                 :       76715 : }
     331                 :             : 
     332                 :     2311491 : bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
     333         [ +  - ]:     2311491 :     assert(IsValid());
     334         [ +  - ]:     2311491 :     assert((nChild >> 31) == 0);
     335         [ +  - ]:     2311491 :     assert(size() == COMPRESSED_SIZE);
     336                 :     2311491 :     unsigned char out[64];
     337                 :     2311491 :     BIP32Hash(cc, nChild, *begin(), begin()+1, out);
     338                 :     2311491 :     memcpy(ccChild.begin(), out+32, 32);
     339                 :     2311491 :     secp256k1_pubkey pubkey;
     340         [ +  - ]:     2311491 :     if (!secp256k1_ec_pubkey_parse(secp256k1_context_static, &pubkey, vch, size())) {
     341                 :           0 :         return false;
     342                 :             :     }
     343         [ +  - ]:     2311491 :     if (!secp256k1_ec_pubkey_tweak_add(secp256k1_context_static, &pubkey, out)) {
     344                 :           0 :         return false;
     345                 :             :     }
     346                 :     2311491 :     unsigned char pub[COMPRESSED_SIZE];
     347                 :     2311491 :     size_t publen = COMPRESSED_SIZE;
     348                 :     2311491 :     secp256k1_ec_pubkey_serialize(secp256k1_context_static, pub, &publen, &pubkey, SECP256K1_EC_COMPRESSED);
     349                 :     2311491 :     pubkeyChild.Set(pub, pub + publen);
     350                 :     2311491 :     return true;
     351                 :     2311491 : }
     352                 :             : 
     353                 :        7500 : EllSwiftPubKey::EllSwiftPubKey(Span<const std::byte> ellswift) noexcept
     354                 :             : {
     355         [ +  - ]:        7500 :     assert(ellswift.size() == SIZE);
     356         [ +  - ]:        7500 :     std::copy(ellswift.begin(), ellswift.end(), m_pubkey.begin());
     357                 :        7500 : }
     358                 :             : 
     359                 :         400 : CPubKey EllSwiftPubKey::Decode() const
     360                 :             : {
     361                 :         400 :     secp256k1_pubkey pubkey;
     362                 :         400 :     secp256k1_ellswift_decode(secp256k1_context_static, &pubkey, UCharCast(m_pubkey.data()));
     363                 :             : 
     364                 :         400 :     size_t sz = CPubKey::COMPRESSED_SIZE;
     365                 :         400 :     std::array<uint8_t, CPubKey::COMPRESSED_SIZE> vch_bytes;
     366                 :             : 
     367                 :         400 :     secp256k1_ec_pubkey_serialize(secp256k1_context_static, vch_bytes.data(), &sz, &pubkey, SECP256K1_EC_COMPRESSED);
     368         [ +  - ]:         400 :     assert(sz == vch_bytes.size());
     369                 :             : 
     370                 :         400 :     return CPubKey{vch_bytes.begin(), vch_bytes.end()};
     371                 :         400 : }
     372                 :             : 
     373                 :     2471384 : void CExtPubKey::Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const {
     374                 :     2471384 :     code[0] = nDepth;
     375                 :     2471384 :     memcpy(code+1, vchFingerprint, 4);
     376                 :     2471384 :     WriteBE32(code+5, nChild);
     377                 :     2471384 :     memcpy(code+9, chaincode.begin(), 32);
     378         [ +  - ]:     2471384 :     assert(pubkey.size() == CPubKey::COMPRESSED_SIZE);
     379                 :     2471384 :     memcpy(code+41, pubkey.begin(), CPubKey::COMPRESSED_SIZE);
     380                 :     2471384 : }
     381                 :             : 
     382                 :      114948 : void CExtPubKey::Decode(const unsigned char code[BIP32_EXTKEY_SIZE]) {
     383                 :      114948 :     nDepth = code[0];
     384                 :      114948 :     memcpy(vchFingerprint, code+1, 4);
     385                 :      114948 :     nChild = ReadBE32(code+5);
     386                 :      114948 :     memcpy(chaincode.begin(), code+9, 32);
     387                 :      114948 :     pubkey.Set(code+41, code+BIP32_EXTKEY_SIZE);
     388   [ +  +  +  +  :      114948 :     if ((nDepth == 0 && (nChild != 0 || ReadLE32(vchFingerprint) != 0)) || !pubkey.IsFullyValid()) pubkey = CPubKey();
                   +  + ]
     389                 :      114948 : }
     390                 :             : 
     391                 :       13555 : void CExtPubKey::EncodeWithVersion(unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE]) const
     392                 :             : {
     393                 :       13555 :     memcpy(code, version, 4);
     394                 :       13555 :     Encode(&code[4]);
     395                 :       13555 : }
     396                 :             : 
     397                 :       24871 : void CExtPubKey::DecodeWithVersion(const unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE])
     398                 :             : {
     399                 :       24871 :     memcpy(version, code, 4);
     400                 :       24871 :     Decode(&code[4]);
     401                 :       24871 : }
     402                 :             : 
     403                 :     2310957 : bool CExtPubKey::Derive(CExtPubKey &out, unsigned int _nChild) const {
     404         [ -  + ]:     2310957 :     if (nDepth == std::numeric_limits<unsigned char>::max()) return false;
     405                 :     2310957 :     out.nDepth = nDepth + 1;
     406                 :     2310957 :     CKeyID id = pubkey.GetID();
     407                 :     2310957 :     memcpy(out.vchFingerprint, &id, 4);
     408                 :     2310957 :     out.nChild = _nChild;
     409                 :     2310957 :     return pubkey.Derive(out.pubkey, out.chaincode, _nChild, chaincode);
     410                 :     2310957 : }
     411                 :             : 
     412                 :       98623 : /* static */ bool CPubKey::CheckLowS(const std::vector<unsigned char>& vchSig) {
     413                 :       98623 :     secp256k1_ecdsa_signature sig;
     414         [ +  + ]:       98623 :     if (!ecdsa_signature_parse_der_lax(&sig, vchSig.data(), vchSig.size())) {
     415                 :         534 :         return false;
     416                 :             :     }
     417                 :       98089 :     return (!secp256k1_ecdsa_signature_normalize(secp256k1_context_static, nullptr, &sig));
     418                 :       98623 : }
        

Generated by: LCOV version 2.0-1