LCOV - code coverage report
Current view: top level - src/script - signingprovider.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 261 360 72.5 %
Date: 2023-10-05 15:40:34 Functions: 32 51 62.7 %
Branches: 239 454 52.6 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :            : // Copyright (c) 2009-2022 The Bitcoin Core 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 <script/keyorigin.h>
       7                 :            : #include <script/interpreter.h>
       8                 :            : #include <script/signingprovider.h>
       9                 :            : 
      10                 :            : #include <logging.h>
      11                 :            : 
      12                 :        173 : const SigningProvider& DUMMY_SIGNING_PROVIDER = SigningProvider();
      13                 :            : 
      14                 :            : template<typename M, typename K, typename V>
      15                 :      10871 : bool LookupHelper(const M& map, const K& key, V& value)
      16                 :            : {
      17                 :      10871 :     auto it = map.find(key);
      18   [ +  +  +  +  :      10871 :     if (it != map.end()) {
          +  +  +  +  +  
                      + ]
      19                 :       8950 :         value = it->second;
      20                 :       8950 :         return true;
      21                 :            :     }
      22                 :       1921 :     return false;
      23                 :      10871 : }
      24                 :            : 
      25                 :          0 : bool HidingSigningProvider::GetCScript(const CScriptID& scriptid, CScript& script) const
      26                 :            : {
      27                 :          0 :     return m_provider->GetCScript(scriptid, script);
      28                 :            : }
      29                 :            : 
      30                 :          0 : bool HidingSigningProvider::GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const
      31                 :            : {
      32                 :          0 :     return m_provider->GetPubKey(keyid, pubkey);
      33                 :            : }
      34                 :            : 
      35                 :          0 : bool HidingSigningProvider::GetKey(const CKeyID& keyid, CKey& key) const
      36                 :            : {
      37         [ #  # ]:          0 :     if (m_hide_secret) return false;
      38                 :          0 :     return m_provider->GetKey(keyid, key);
      39                 :          0 : }
      40                 :            : 
      41                 :          0 : bool HidingSigningProvider::GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const
      42                 :            : {
      43         [ #  # ]:          0 :     if (m_hide_origin) return false;
      44                 :          0 :     return m_provider->GetKeyOrigin(keyid, info);
      45                 :          0 : }
      46                 :            : 
      47                 :          0 : bool HidingSigningProvider::GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const
      48                 :            : {
      49                 :          0 :     return m_provider->GetTaprootSpendData(output_key, spenddata);
      50                 :            : }
      51                 :          0 : bool HidingSigningProvider::GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const
      52                 :            : {
      53                 :          0 :     return m_provider->GetTaprootBuilder(output_key, builder);
      54                 :            : }
      55                 :            : 
      56                 :        535 : bool FlatSigningProvider::GetCScript(const CScriptID& scriptid, CScript& script) const { return LookupHelper(scripts, scriptid, script); }
      57                 :         86 : bool FlatSigningProvider::GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const { return LookupHelper(pubkeys, keyid, pubkey); }
      58                 :       3537 : bool FlatSigningProvider::GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const
      59                 :            : {
      60                 :       3537 :     std::pair<CPubKey, KeyOriginInfo> out;
      61         [ +  - ]:       3537 :     bool ret = LookupHelper(origins, keyid, out);
      62         [ +  + ]:       3537 :     if (ret) info = std::move(out.second);
      63                 :       3537 :     return ret;
      64                 :       3537 : }
      65                 :       6609 : bool FlatSigningProvider::GetKey(const CKeyID& keyid, CKey& key) const { return LookupHelper(keys, keyid, key); }
      66                 :        104 : bool FlatSigningProvider::GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const
      67                 :            : {
      68                 :        104 :     TaprootBuilder builder;
      69   [ +  -  +  + ]:        104 :     if (LookupHelper(tr_trees, output_key, builder)) {
      70         [ +  - ]:         99 :         spenddata = builder.GetSpendData();
      71                 :         99 :         return true;
      72                 :            :     }
      73                 :          5 :     return false;
      74                 :        277 : }
      75                 :          0 : bool FlatSigningProvider::GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const
      76                 :            : {
      77                 :          0 :     return LookupHelper(tr_trees, output_key, builder);
      78                 :            : }
      79                 :            : 
      80                 :       2873 : FlatSigningProvider& FlatSigningProvider::Merge(FlatSigningProvider&& b)
      81                 :            : {
      82                 :       2873 :     scripts.merge(b.scripts);
      83                 :       2873 :     pubkeys.merge(b.pubkeys);
      84                 :       2873 :     keys.merge(b.keys);
      85                 :       2873 :     origins.merge(b.origins);
      86                 :       2873 :     tr_trees.merge(b.tr_trees);
      87                 :       2873 :     return *this;
      88                 :            : }
      89                 :            : 
      90                 :       4619 : void FillableSigningProvider::ImplicitlyLearnRelatedKeyScripts(const CPubKey& pubkey)
      91                 :            : {
      92                 :       4619 :     AssertLockHeld(cs_KeyStore);
      93                 :       4619 :     CKeyID key_id = pubkey.GetID();
      94                 :            :     // This adds the redeemscripts necessary to detect P2WPKH and P2SH-P2WPKH
      95                 :            :     // outputs. Technically P2WPKH outputs don't have a redeemscript to be
      96                 :            :     // spent. However, our current IsMine logic requires the corresponding
      97                 :            :     // P2SH-P2WPKH redeemscript to be present in the wallet in order to accept
      98                 :            :     // payment even to P2WPKH outputs.
      99                 :            :     // Also note that having superfluous scripts in the keystore never hurts.
     100                 :            :     // They're only used to guide recursion in signing and IsMine logic - if
     101                 :            :     // a script is present but we can't do anything with it, it has no effect.
     102                 :            :     // "Implicitly" refers to fact that scripts are derived automatically from
     103                 :            :     // existing keys, and are present in memory, even without being explicitly
     104                 :            :     // loaded (e.g. from a file).
     105         [ +  + ]:       4619 :     if (pubkey.IsCompressed()) {
     106         [ +  - ]:       2448 :         CScript script = GetScriptForDestination(WitnessV0KeyHash(key_id));
     107                 :            :         // This does not use AddCScript, as it may be overridden.
     108         [ +  - ]:       2448 :         CScriptID id(script);
     109         [ +  - ]:       2448 :         mapScripts[id] = std::move(script);
     110                 :       2448 :     }
     111                 :       4619 : }
     112                 :            : 
     113                 :        275 : bool FillableSigningProvider::GetPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
     114                 :            : {
     115                 :        275 :     CKey key;
     116   [ +  -  +  + ]:        275 :     if (!GetKey(address, key)) {
     117                 :        187 :         return false;
     118                 :            :     }
     119         [ +  - ]:         88 :     vchPubKeyOut = key.GetPubKey();
     120                 :         88 :     return true;
     121                 :        275 : }
     122                 :            : 
     123                 :       4619 : bool FillableSigningProvider::AddKeyPubKey(const CKey& key, const CPubKey &pubkey)
     124                 :            : {
     125                 :       4619 :     LOCK(cs_KeyStore);
     126   [ +  -  +  -  :       4619 :     mapKeys[pubkey.GetID()] = key;
                   +  - ]
     127         [ +  - ]:       4619 :     ImplicitlyLearnRelatedKeyScripts(pubkey);
     128                 :            :     return true;
     129                 :       4619 : }
     130                 :            : 
     131                 :        220 : bool FillableSigningProvider::HaveKey(const CKeyID &address) const
     132                 :            : {
     133                 :        220 :     LOCK(cs_KeyStore);
     134         [ +  - ]:        220 :     return mapKeys.count(address) > 0;
     135                 :        220 : }
     136                 :            : 
     137                 :         88 : std::set<CKeyID> FillableSigningProvider::GetKeys() const
     138                 :            : {
     139                 :         88 :     LOCK(cs_KeyStore);
     140                 :         88 :     std::set<CKeyID> set_address;
     141         [ +  + ]:        132 :     for (const auto& mi : mapKeys) {
     142         [ +  - ]:         44 :         set_address.insert(mi.first);
     143                 :            :     }
     144                 :         88 :     return set_address;
     145         [ +  - ]:         88 : }
     146                 :            : 
     147                 :        553 : bool FillableSigningProvider::GetKey(const CKeyID &address, CKey &keyOut) const
     148                 :            : {
     149                 :        553 :     LOCK(cs_KeyStore);
     150         [ +  - ]:        553 :     KeyMap::const_iterator mi = mapKeys.find(address);
     151         [ +  + ]:        553 :     if (mi != mapKeys.end()) {
     152         [ +  - ]:        183 :         keyOut = mi->second;
     153                 :        183 :         return true;
     154                 :            :     }
     155                 :        370 :     return false;
     156                 :        553 : }
     157                 :            : 
     158                 :          8 : bool FillableSigningProvider::AddCScript(const CScript& redeemScript)
     159                 :            : {
     160         [ -  + ]:          8 :     if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
     161                 :          0 :         return error("FillableSigningProvider::AddCScript(): redeemScripts > %i bytes are invalid", MAX_SCRIPT_ELEMENT_SIZE);
     162                 :            : 
     163                 :          8 :     LOCK(cs_KeyStore);
     164   [ +  -  +  -  :          8 :     mapScripts[CScriptID(redeemScript)] = redeemScript;
                   +  - ]
     165                 :          8 :     return true;
     166                 :          8 : }
     167                 :            : 
     168                 :          0 : bool FillableSigningProvider::HaveCScript(const CScriptID& hash) const
     169                 :            : {
     170                 :          0 :     LOCK(cs_KeyStore);
     171         [ #  # ]:          0 :     return mapScripts.count(hash) > 0;
     172                 :          0 : }
     173                 :            : 
     174                 :          0 : std::set<CScriptID> FillableSigningProvider::GetCScripts() const
     175                 :            : {
     176                 :          0 :     LOCK(cs_KeyStore);
     177                 :          0 :     std::set<CScriptID> set_script;
     178         [ #  # ]:          0 :     for (const auto& mi : mapScripts) {
     179         [ #  # ]:          0 :         set_script.insert(mi.first);
     180                 :            :     }
     181                 :          0 :     return set_script;
     182         [ #  # ]:          0 : }
     183                 :            : 
     184                 :       2011 : bool FillableSigningProvider::GetCScript(const CScriptID &hash, CScript& redeemScriptOut) const
     185                 :            : {
     186                 :       2011 :     LOCK(cs_KeyStore);
     187         [ +  - ]:       2011 :     ScriptMap::const_iterator mi = mapScripts.find(hash);
     188         [ +  + ]:       2011 :     if (mi != mapScripts.end())
     189                 :            :     {
     190         [ +  - ]:          8 :         redeemScriptOut = (*mi).second;
     191                 :          8 :         return true;
     192                 :            :     }
     193                 :       2003 :     return false;
     194                 :       2011 : }
     195                 :            : 
     196                 :        651 : CKeyID GetKeyForDestination(const SigningProvider& store, const CTxDestination& dest)
     197                 :            : {
     198                 :            :     // Only supports destinations which map to single public keys:
     199                 :            :     // P2PKH, P2WPKH, P2SH-P2WPKH, P2TR
     200         [ +  + ]:        651 :     if (auto id = std::get_if<PKHash>(&dest)) {
     201                 :         97 :         return ToKeyID(*id);
     202                 :            :     }
     203         [ +  + ]:        554 :     if (auto witness_id = std::get_if<WitnessV0KeyHash>(&dest)) {
     204                 :          3 :         return ToKeyID(*witness_id);
     205                 :            :     }
     206         [ +  + ]:        551 :     if (auto script_hash = std::get_if<ScriptHash>(&dest)) {
     207                 :          6 :         CScript script;
     208         [ +  - ]:          6 :         CScriptID script_id = ToScriptID(*script_hash);
     209         [ +  - ]:          6 :         CTxDestination inner_dest;
     210   [ +  -  -  +  :          6 :         if (store.GetCScript(script_id, script) && ExtractDestination(script, inner_dest)) {
             #  #  #  # ]
     211         [ #  # ]:          0 :             if (auto inner_witness_id = std::get_if<WitnessV0KeyHash>(&inner_dest)) {
     212         [ #  # ]:          0 :                 return ToKeyID(*inner_witness_id);
     213                 :            :             }
     214                 :          0 :         }
     215         [ -  + ]:          6 :     }
     216         [ +  + ]:        551 :     if (auto output_key = std::get_if<WitnessV1Taproot>(&dest)) {
     217                 :          5 :         TaprootSpendData spenddata;
     218         [ +  - ]:          5 :         CPubKey pub;
     219   [ +  -  #  # ]:          5 :         if (store.GetTaprootSpendData(*output_key, spenddata)
     220   [ -  +  #  # ]:          5 :             && !spenddata.internal_key.IsNull()
     221   [ #  #  #  # ]:          0 :             && spenddata.merkle_root.IsNull()
     222   [ #  #  #  # ]:          0 :             && store.GetPubKeyByXOnly(spenddata.internal_key, pub)) {
     223         [ #  # ]:          0 :             return pub.GetID();
     224                 :            :         }
     225         [ -  + ]:          5 :     }
     226                 :        551 :     return CKeyID();
     227                 :        651 : }
     228                 :            : 
     229                 :          0 : void MultiSigningProvider::AddProvider(std::unique_ptr<SigningProvider> provider)
     230                 :            : {
     231                 :          0 :     m_providers.push_back(std::move(provider));
     232                 :          0 : }
     233                 :            : 
     234                 :          0 : bool MultiSigningProvider::GetCScript(const CScriptID& scriptid, CScript& script) const
     235                 :            : {
     236         [ #  # ]:          0 :     for (const auto& provider: m_providers) {
     237         [ #  # ]:          0 :         if (provider->GetCScript(scriptid, script)) return true;
     238                 :            :     }
     239                 :          0 :     return false;
     240                 :          0 : }
     241                 :            : 
     242                 :          0 : bool MultiSigningProvider::GetPubKey(const CKeyID& keyid, CPubKey& pubkey) const
     243                 :            : {
     244         [ #  # ]:          0 :     for (const auto& provider: m_providers) {
     245         [ #  # ]:          0 :         if (provider->GetPubKey(keyid, pubkey)) return true;
     246                 :            :     }
     247                 :          0 :     return false;
     248                 :          0 : }
     249                 :            : 
     250                 :            : 
     251                 :          0 : bool MultiSigningProvider::GetKeyOrigin(const CKeyID& keyid, KeyOriginInfo& info) const
     252                 :            : {
     253         [ #  # ]:          0 :     for (const auto& provider: m_providers) {
     254         [ #  # ]:          0 :         if (provider->GetKeyOrigin(keyid, info)) return true;
     255                 :            :     }
     256                 :          0 :     return false;
     257                 :          0 : }
     258                 :            : 
     259                 :          0 : bool MultiSigningProvider::GetKey(const CKeyID& keyid, CKey& key) const
     260                 :            : {
     261         [ #  # ]:          0 :     for (const auto& provider: m_providers) {
     262         [ #  # ]:          0 :         if (provider->GetKey(keyid, key)) return true;
     263                 :            :     }
     264                 :          0 :     return false;
     265                 :          0 : }
     266                 :            : 
     267                 :          0 : bool MultiSigningProvider::GetTaprootSpendData(const XOnlyPubKey& output_key, TaprootSpendData& spenddata) const
     268                 :            : {
     269         [ #  # ]:          0 :     for (const auto& provider: m_providers) {
     270         [ #  # ]:          0 :         if (provider->GetTaprootSpendData(output_key, spenddata)) return true;
     271                 :            :     }
     272                 :          0 :     return false;
     273                 :          0 : }
     274                 :            : 
     275                 :          0 : bool MultiSigningProvider::GetTaprootBuilder(const XOnlyPubKey& output_key, TaprootBuilder& builder) const
     276                 :            : {
     277         [ #  # ]:          0 :     for (const auto& provider: m_providers) {
     278         [ #  # ]:          0 :         if (provider->GetTaprootBuilder(output_key, builder)) return true;
     279                 :            :     }
     280                 :          0 :     return false;
     281                 :          0 : }
     282                 :            : 
     283                 :      17950 : /*static*/ TaprootBuilder::NodeInfo TaprootBuilder::Combine(NodeInfo&& a, NodeInfo&& b)
     284                 :            : {
     285                 :      17950 :     NodeInfo ret;
     286                 :            :     /* Iterate over all tracked leaves in a, add b's hash to their Merkle branch, and move them to ret. */
     287         [ +  + ]:      75343 :     for (auto& leaf : a.leaves) {
     288         [ +  - ]:      57393 :         leaf.merkle_branch.push_back(b.hash);
     289         [ +  - ]:      57393 :         ret.leaves.emplace_back(std::move(leaf));
     290                 :            :     }
     291                 :            :     /* Iterate over all tracked leaves in b, add a's hash to their Merkle branch, and move them to ret. */
     292         [ +  + ]:      70747 :     for (auto& leaf : b.leaves) {
     293         [ +  - ]:      52797 :         leaf.merkle_branch.push_back(a.hash);
     294         [ +  - ]:      52797 :         ret.leaves.emplace_back(std::move(leaf));
     295                 :            :     }
     296   [ +  -  +  -  :      17950 :     ret.hash = ComputeTapbranchHash(a.hash, b.hash);
                   +  - ]
     297                 :      17950 :     return ret;
     298         [ +  - ]:      17950 : }
     299                 :            : 
     300                 :          0 : void TaprootSpendData::Merge(TaprootSpendData other)
     301                 :            : {
     302                 :            :     // TODO: figure out how to better deal with conflicting information
     303                 :            :     // being merged.
     304   [ #  #  #  # ]:          0 :     if (internal_key.IsNull() && !other.internal_key.IsNull()) {
     305                 :          0 :         internal_key = other.internal_key;
     306                 :          0 :     }
     307   [ #  #  #  # ]:          0 :     if (merkle_root.IsNull() && !other.merkle_root.IsNull()) {
     308                 :          0 :         merkle_root = other.merkle_root;
     309                 :          0 :     }
     310         [ #  # ]:          0 :     for (auto& [key, control_blocks] : other.scripts) {
     311                 :          0 :         scripts[key].merge(std::move(control_blocks));
     312                 :            :     }
     313                 :          0 : }
     314                 :            : 
     315                 :      26174 : void TaprootBuilder::Insert(TaprootBuilder::NodeInfo&& node, int depth)
     316                 :            : {
     317   [ -  +  +  - ]:      26174 :     assert(depth >= 0 && (size_t)depth <= TAPROOT_CONTROL_MAX_NODE_COUNT);
     318                 :            :     /* We cannot insert a leaf at a lower depth while a deeper branch is unfinished. Doing
     319                 :            :      * so would mean the Add() invocations do not correspond to a DFS traversal of a
     320                 :            :      * binary tree. */
     321         [ +  + ]:      26174 :     if ((size_t)depth + 1 < m_branch.size()) {
     322                 :        476 :         m_valid = false;
     323                 :        476 :         return;
     324                 :            :     }
     325                 :            :     /* As long as an entry in the branch exists at the specified depth, combine it and propagate up.
     326                 :            :      * The 'node' variable is overwritten here with the newly combined node. */
     327   [ +  +  +  +  :      43648 :     while (m_valid && m_branch.size() > (size_t)depth && m_branch[depth].has_value()) {
                   +  + ]
     328                 :      17950 :         node = Combine(std::move(node), std::move(*m_branch[depth]));
     329                 :      17950 :         m_branch.pop_back();
     330         [ +  + ]:      17950 :         if (depth == 0) m_valid = false; /* Can't propagate further up than the root */
     331                 :      17950 :         --depth;
     332                 :            :     }
     333         [ +  + ]:      25698 :     if (m_valid) {
     334                 :            :         /* Make sure the branch is big enough to place the new node. */
     335         [ +  + ]:      25079 :         if (m_branch.size() <= (size_t)depth) m_branch.resize((size_t)depth + 1);
     336         [ -  + ]:      25079 :         assert(!m_branch[depth].has_value());
     337                 :      25079 :         m_branch[depth] = std::move(node);
     338                 :      25079 :     }
     339                 :      26174 : }
     340                 :            : 
     341                 :        129 : /*static*/ bool TaprootBuilder::ValidDepths(const std::vector<int>& depths)
     342                 :            : {
     343                 :        129 :     std::vector<bool> branch;
     344         [ +  + ]:        556 :     for (int depth : depths) {
     345                 :            :         // This inner loop corresponds to effectively the same logic on branch
     346                 :            :         // as what Insert() performs on the m_branch variable. Instead of
     347                 :            :         // storing a NodeInfo object, just remember whether or not there is one
     348                 :            :         // at that depth.
     349   [ +  -  -  + ]:        427 :         if (depth < 0 || (size_t)depth > TAPROOT_CONTROL_MAX_NODE_COUNT) return false;
     350         [ +  - ]:        427 :         if ((size_t)depth + 1 < branch.size()) return false;
     351   [ +  +  +  -  :        743 :         while (branch.size() > (size_t)depth && branch[depth]) {
                   +  + ]
     352         [ +  - ]:        316 :             branch.pop_back();
     353         [ -  + ]:        316 :             if (depth == 0) return false;
     354                 :        316 :             --depth;
     355                 :            :         }
     356   [ +  +  +  - ]:        427 :         if (branch.size() <= (size_t)depth) branch.resize((size_t)depth + 1);
     357   [ +  -  +  - ]:        427 :         assert(!branch[depth]);
     358         [ +  - ]:        427 :         branch[depth] = true;
     359                 :            :     }
     360                 :            :     // And this check corresponds to the IsComplete() check on m_branch.
     361   [ +  +  +  -  :        129 :     return branch.size() == 0 || (branch.size() == 1 && branch[0]);
                   +  - ]
     362                 :        129 : }
     363                 :            : 
     364                 :      78047 : TaprootBuilder& TaprootBuilder::Add(int depth, Span<const unsigned char> script, int leaf_version, bool track)
     365                 :            : {
     366         [ +  - ]:      78047 :     assert((leaf_version & ~TAPROOT_LEAF_MASK) == 0);
     367         [ +  + ]:      78047 :     if (!IsValid()) return *this;
     368                 :            :     /* Construct NodeInfo object with leaf hash and (if track is true) also leaf information. */
     369                 :      26174 :     NodeInfo node;
     370         [ +  - ]:      26174 :     node.hash = ComputeTapleafHash(leaf_version, script);
     371   [ -  +  +  -  :      26174 :     if (track) node.leaves.emplace_back(LeafInfo{std::vector<unsigned char>(script.begin(), script.end()), leaf_version, {}});
                   -  + ]
     372                 :            :     /* Insert into the branch. */
     373         [ +  - ]:      26174 :     Insert(std::move(node), depth);
     374                 :      26174 :     return *this;
     375                 :      78047 : }
     376                 :            : 
     377                 :          0 : TaprootBuilder& TaprootBuilder::AddOmitted(int depth, const uint256& hash)
     378                 :            : {
     379         [ #  # ]:          0 :     if (!IsValid()) return *this;
     380                 :            :     /* Construct NodeInfo object with the hash directly, and insert it into the branch. */
     381                 :          0 :     NodeInfo node;
     382                 :          0 :     node.hash = hash;
     383         [ #  # ]:          0 :     Insert(std::move(node), depth);
     384                 :          0 :     return *this;
     385                 :          0 : }
     386                 :            : 
     387                 :        218 : TaprootBuilder& TaprootBuilder::Finalize(const XOnlyPubKey& internal_key)
     388                 :            : {
     389                 :            :     /* Can only call this function when IsComplete() is true. */
     390         [ +  - ]:        218 :     assert(IsComplete());
     391                 :        218 :     m_internal_key = internal_key;
     392         [ +  + ]:        218 :     auto ret = m_internal_key.CreateTapTweak(m_branch.size() == 0 ? nullptr : &m_branch[0]->hash);
     393         [ -  + ]:        218 :     assert(ret.has_value());
     394                 :        218 :     std::tie(m_output_key, m_parity) = *ret;
     395                 :        218 :     return *this;
     396                 :            : }
     397                 :            : 
     398                 :        218 : WitnessV1Taproot TaprootBuilder::GetOutput() { return WitnessV1Taproot{m_output_key}; }
     399                 :            : 
     400                 :         99 : TaprootSpendData TaprootBuilder::GetSpendData() const
     401                 :            : {
     402         [ +  - ]:         99 :     assert(IsComplete());
     403         [ -  + ]:         99 :     assert(m_output_key.IsFullyValid());
     404                 :         99 :     TaprootSpendData spd;
     405   [ +  +  +  -  :         99 :     spd.merkle_root = m_branch.size() == 0 ? uint256() : m_branch[0]->hash;
                   +  - ]
     406                 :         99 :     spd.internal_key = m_internal_key;
     407         [ +  + ]:         99 :     if (m_branch.size()) {
     408                 :            :         // If any script paths exist, they have been combined into the root m_branch[0]
     409                 :            :         // by now. Compute the control block for each of its tracked leaves, and put them in
     410                 :            :         // spd.scripts.
     411   [ +  -  +  + ]:        489 :         for (const auto& leaf : m_branch[0]->leaves) {
     412                 :        394 :             std::vector<unsigned char> control_block;
     413         [ +  - ]:        394 :             control_block.resize(TAPROOT_CONTROL_BASE_SIZE + TAPROOT_CONTROL_NODE_SIZE * leaf.merkle_branch.size());
     414                 :        394 :             control_block[0] = leaf.leaf_version | (m_parity ? 1 : 0);
     415   [ +  -  +  -  :        394 :             std::copy(m_internal_key.begin(), m_internal_key.end(), control_block.begin() + 1);
                   +  - ]
     416         [ +  + ]:        394 :             if (leaf.merkle_branch.size()) {
     417   [ +  -  +  - ]:        734 :                 std::copy(leaf.merkle_branch[0].begin(),
     418         [ +  - ]:        367 :                           leaf.merkle_branch[0].begin() + TAPROOT_CONTROL_NODE_SIZE * leaf.merkle_branch.size(),
     419                 :        367 :                           control_block.begin() + TAPROOT_CONTROL_BASE_SIZE);
     420                 :        367 :             }
     421   [ +  -  +  -  :        394 :             spd.scripts[{leaf.script, leaf.leaf_version}].insert(std::move(control_block));
                   +  - ]
     422                 :        394 :         }
     423                 :         95 :     }
     424                 :         99 :     return spd;
     425         [ +  - ]:         99 : }
     426                 :            : 
     427                 :         99 : std::optional<std::vector<std::tuple<int, std::vector<unsigned char>, int>>> InferTaprootTree(const TaprootSpendData& spenddata, const XOnlyPubKey& output)
     428                 :            : {
     429                 :            :     // Verify that the output matches the assumed Merkle root and internal key.
     430         [ +  + ]:         99 :     auto tweak = spenddata.internal_key.CreateTapTweak(spenddata.merkle_root.IsNull() ? nullptr : &spenddata.merkle_root);
     431   [ +  -  -  + ]:         99 :     if (!tweak || tweak->first != output) return std::nullopt;
     432                 :            :     // If the Merkle root is 0, the tree is empty, and we're done.
     433                 :         99 :     std::vector<std::tuple<int, std::vector<unsigned char>, int>> ret;
     434   [ +  -  +  +  :         99 :     if (spenddata.merkle_root.IsNull()) return ret;
                   +  - ]
     435                 :            : 
     436                 :            :     /** Data structure to represent the nodes of the tree we're going to build. */
     437                 :       1342 :     struct TreeNode {
     438                 :            :         /** Hash of this node, if known; 0 otherwise. */
     439                 :            :         uint256 hash;
     440                 :            :         /** The left and right subtrees (note that their order is irrelevant). */
     441                 :            :         std::unique_ptr<TreeNode> sub[2];
     442                 :            :         /** If this is known to be a leaf node, a pointer to the (script, leaf_ver) pair.
     443                 :            :          *  nullptr otherwise. */
     444                 :        671 :         const std::pair<std::vector<unsigned char>, int>* leaf = nullptr;
     445                 :            :         /** Whether or not this node has been explored (is known to be a leaf, or known to have children). */
     446                 :        671 :         bool explored = false;
     447                 :            :         /** Whether or not this node is an inner node (unknown until explored = true). */
     448                 :            :         bool inner;
     449                 :            :         /** Whether or not we have produced output for this subtree. */
     450                 :        671 :         bool done = false;
     451                 :            :     };
     452                 :            : 
     453                 :            :     // Build tree from the provided branches.
     454         [ +  - ]:         95 :     TreeNode root;
     455                 :         95 :     root.hash = spenddata.merkle_root;
     456         [ +  + ]:        625 :     for (const auto& [key, control_blocks] : spenddata.scripts) {
     457                 :        857 :         const auto& [script, leaf_ver] = key;
     458         [ +  + ]:        530 :         for (const auto& control : control_blocks) {
     459                 :            :             // Skip script records with nonsensical leaf version.
     460   [ +  -  +  -  :        327 :             if (leaf_ver < 0 || leaf_ver >= 0x100 || leaf_ver & 1) continue;
                   -  + ]
     461                 :            :             // Skip script records with invalid control block sizes.
     462   [ +  -  +  -  :        327 :             if (control.size() < TAPROOT_CONTROL_BASE_SIZE || control.size() > TAPROOT_CONTROL_MAX_SIZE ||
                   -  + ]
     463                 :        327 :                 ((control.size() - TAPROOT_CONTROL_BASE_SIZE) % TAPROOT_CONTROL_NODE_SIZE) != 0) continue;
     464                 :            :             // Skip script records that don't match the control block.
     465   [ -  +  -  + ]:        654 :             if ((control[0] & TAPROOT_LEAF_MASK) != leaf_ver) continue;
     466                 :            :             // Skip script records that don't match the provided Merkle root.
     467   [ +  -  +  -  :        654 :             const uint256 leaf_hash = ComputeTapleafHash(leaf_ver, script);
                   +  - ]
     468   [ +  -  +  - ]:        327 :             const uint256 merkle_root = ComputeTaprootMerkleRoot(control, leaf_hash);
     469   [ +  -  -  + ]:        327 :             if (merkle_root != spenddata.merkle_root) continue;
     470                 :            : 
     471                 :        327 :             TreeNode* node = &root;
     472                 :        327 :             size_t levels = (control.size() - TAPROOT_CONTROL_BASE_SIZE) / TAPROOT_CONTROL_NODE_SIZE;
     473         [ +  + ]:       1230 :             for (size_t depth = 0; depth < levels; ++depth) {
     474                 :            :                 // Can't descend into a node which we already know is a leaf.
     475   [ +  +  +  - ]:        903 :                 if (node->explored && !node->inner) return std::nullopt;
     476                 :            : 
     477                 :            :                 // Extract partner hash from Merkle branch in control block.
     478         [ +  - ]:        903 :                 uint256 hash;
     479         [ +  - ]:        903 :                 std::copy(control.begin() + TAPROOT_CONTROL_BASE_SIZE + (levels - 1 - depth) * TAPROOT_CONTROL_NODE_SIZE,
     480                 :        903 :                           control.begin() + TAPROOT_CONTROL_BASE_SIZE + (levels - depth) * TAPROOT_CONTROL_NODE_SIZE,
     481         [ +  - ]:        903 :                           hash.begin());
     482                 :            : 
     483         [ +  + ]:        903 :                 if (node->sub[0]) {
     484                 :            :                     // Descend into the existing left or right branch.
     485                 :        615 :                     bool desc = false;
     486         [ -  + ]:        821 :                     for (int i = 0; i < 2; ++i) {
     487   [ +  -  +  +  :        821 :                         if (node->sub[i]->hash == hash || (node->sub[i]->hash.IsNull() && node->sub[1-i]->hash != hash)) {
          +  -  +  +  +  
                -  +  + ]
     488                 :        615 :                             node->sub[i]->hash = hash;
     489         [ +  - ]:        615 :                             node = &*node->sub[1-i];
     490                 :        615 :                             desc = true;
     491                 :        615 :                             break;
     492                 :            :                         }
     493                 :        206 :                     }
     494         [ +  - ]:        615 :                     if (!desc) return std::nullopt; // This probably requires a hash collision to hit.
     495                 :        615 :                 } else {
     496                 :            :                     // We're in an unexplored node. Create subtrees and descend.
     497                 :        288 :                     node->explored = true;
     498                 :        288 :                     node->inner = true;
     499         [ +  - ]:        288 :                     node->sub[0] = std::make_unique<TreeNode>();
     500         [ +  - ]:        288 :                     node->sub[1] = std::make_unique<TreeNode>();
     501                 :        288 :                     node->sub[1]->hash = hash;
     502         [ +  - ]:        288 :                     node = &*node->sub[0];
     503                 :            :                 }
     504                 :        903 :             }
     505                 :            :             // Cannot turn a known inner node into a leaf.
     506         [ +  - ]:        327 :             if (node->sub[0]) return std::nullopt;
     507                 :        327 :             node->explored = true;
     508                 :        327 :             node->inner = false;
     509                 :        327 :             node->leaf = &key;
     510                 :        327 :             node->hash = leaf_hash;
     511                 :            :         }
     512                 :            :     }
     513                 :            : 
     514                 :            :     // Recursive processing to turn the tree into flattened output. Use an explicit stack here to avoid
     515                 :            :     // overflowing the call stack (the tree may be 128 levels deep).
     516         [ -  + ]:         95 :     std::vector<TreeNode*> stack{&root};
     517         [ +  + ]:       1322 :     while (!stack.empty()) {
     518                 :       1233 :         TreeNode& node = *stack.back();
     519         [ +  + ]:       1233 :         if (!node.explored) {
     520                 :            :             // Unexplored node, which means the tree is incomplete.
     521                 :          6 :             return std::nullopt;
     522         [ +  + ]:       1227 :         } else if (!node.inner) {
     523                 :            :             // Leaf node; produce output.
     524         [ +  - ]:        364 :             ret.emplace_back(stack.size() - 1, node.leaf->first, node.leaf->second);
     525                 :        364 :             node.done = true;
     526                 :        364 :             stack.pop_back();
     527   [ +  +  +  +  :       1273 :         } else if (node.sub[0]->done && !node.sub[1]->done && !node.sub[1]->explored && !node.sub[1]->hash.IsNull() &&
          +  +  +  -  +  
                -  +  + ]
     528   [ +  -  +  -  :         46 :                    ComputeTapbranchHash(node.sub[1]->hash, node.sub[1]->hash) == node.hash) {
             +  -  +  - ]
     529                 :            :             // Whenever there are nodes with two identical subtrees under it, we run into a problem:
     530                 :            :             // the control blocks for the leaves underneath those will be identical as well, and thus
     531                 :            :             // they will all be matched to the same path in the tree. The result is that at the location
     532                 :            :             // where the duplicate occurred, the left child will contain a normal tree that can be explored
     533                 :            :             // and processed, but the right one will remain unexplored.
     534                 :            :             //
     535                 :            :             // This situation can be detected, by encountering an inner node with unexplored right subtree
     536                 :            :             // with known hash, and H_TapBranch(hash, hash) is equal to the parent node (this node)'s hash.
     537                 :            :             //
     538                 :            :             // To deal with this, simply process the left tree a second time (set its done flag to false;
     539                 :            :             // noting that the done flag of its children have already been set to false after processing
     540                 :            :             // those). To avoid ending up in an infinite loop, set the done flag of the right (unexplored)
     541                 :            :             // subtree to true.
     542                 :         40 :             node.sub[0]->done = false;
     543                 :         40 :             node.sub[1]->done = true;
     544   [ +  +  +  + ]:        863 :         } else if (node.sub[0]->done && node.sub[1]->done) {
     545                 :            :             // An internal node which we're finished with.
     546                 :        263 :             node.sub[0]->done = false;
     547                 :        263 :             node.sub[1]->done = false;
     548                 :        263 :             node.done = true;
     549                 :        263 :             stack.pop_back();
     550         [ +  + ]:        823 :         } else if (!node.sub[0]->done) {
     551                 :            :             // An internal node whose left branch hasn't been processed yet. Do so first.
     552   [ +  -  +  - ]:        325 :             stack.push_back(&*node.sub[0]);
     553         [ +  - ]:        560 :         } else if (!node.sub[1]->done) {
     554                 :            :             // An internal node whose right branch hasn't been processed yet. Do so first.
     555   [ +  -  +  - ]:        235 :             stack.push_back(&*node.sub[1]);
     556                 :        235 :         }
     557                 :            :     }
     558                 :            : 
     559         [ +  - ]:         89 :     return ret;
     560                 :         99 : }
     561                 :            : 
     562                 :          0 : std::vector<std::tuple<uint8_t, uint8_t, std::vector<unsigned char>>> TaprootBuilder::GetTreeTuples() const
     563                 :            : {
     564         [ #  # ]:          0 :     assert(IsComplete());
     565                 :          0 :     std::vector<std::tuple<uint8_t, uint8_t, std::vector<unsigned char>>> tuples;
     566         [ #  # ]:          0 :     if (m_branch.size()) {
     567         [ #  # ]:          0 :         const auto& leaves = m_branch[0]->leaves;
     568         [ #  # ]:          0 :         for (const auto& leaf : leaves) {
     569         [ #  # ]:          0 :             assert(leaf.merkle_branch.size() <= TAPROOT_CONTROL_MAX_NODE_COUNT);
     570                 :          0 :             uint8_t depth = (uint8_t)leaf.merkle_branch.size();
     571                 :          0 :             uint8_t leaf_ver = (uint8_t)leaf.leaf_version;
     572   [ #  #  #  # ]:          0 :             tuples.push_back(std::make_tuple(depth, leaf_ver, leaf.script));
     573                 :            :         }
     574                 :          0 :     }
     575                 :          0 :     return tuples;
     576         [ #  # ]:          0 : }

Generated by: LCOV version 1.14