LCOV - code coverage report
Current view: top level - src/script - miniscript.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 97.6 % 333 325
Test Date: 2025-08-01 04:15:35 Functions: 100.0 % 12 12
Branches: 91.3 % 550 502

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2019-present The Bitcoin Core developers
       2                 :             : // Distributed under the MIT software license, see the accompanying
       3                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       4                 :             : 
       5                 :             : #include <limits>
       6                 :             : #include <vector>
       7                 :             : 
       8                 :             : #include <primitives/transaction.h>
       9                 :             : #include <script/miniscript.h>
      10                 :             : #include <script/script.h>
      11                 :             : #include <script/solver.h>
      12                 :             : #include <span.h>
      13                 :             : #include <util/check.h>
      14                 :             : #include <util/vector.h>
      15                 :             : 
      16                 :             : namespace miniscript {
      17                 :             : namespace internal {
      18                 :             : 
      19                 :    26283331 : Type SanitizeType(Type e) {
      20         [ +  + ]:    26283331 :     int num_types = (e << "K"_mst) + (e << "V"_mst) + (e << "B"_mst) + (e << "W"_mst);
      21         [ +  + ]:    26283331 :     if (num_types == 0) return ""_mst; // No valid type, don't care about the rest
      22                 :    17492589 :     CHECK_NONFATAL(num_types == 1); // K, V, B, W all conflict with each other
      23   [ +  +  +  - ]:    30298649 :     CHECK_NONFATAL(!(e << "z"_mst) || !(e << "o"_mst)); // z conflicts with o
      24   [ +  +  +  - ]:    18596320 :     CHECK_NONFATAL(!(e << "n"_mst) || !(e << "z"_mst)); // n conflicts with z
      25   [ +  +  +  - ]:    18596320 :     CHECK_NONFATAL(!(e << "n"_mst) || !(e << "W"_mst)); // n conflicts with W
      26   [ +  +  +  - ]:    18000884 :     CHECK_NONFATAL(!(e << "V"_mst) || !(e << "d"_mst)); // V conflicts with d
      27   [ +  +  +  - ]:    17734769 :     CHECK_NONFATAL(!(e << "K"_mst) ||  (e << "u"_mst)); // K implies u
      28   [ +  +  +  - ]:    18000884 :     CHECK_NONFATAL(!(e << "V"_mst) || !(e << "u"_mst)); // V conflicts with u
      29   [ +  +  +  - ]:    28533371 :     CHECK_NONFATAL(!(e << "e"_mst) || !(e << "f"_mst)); // e conflicts with f
      30   [ +  +  +  - ]:    28533371 :     CHECK_NONFATAL(!(e << "e"_mst) ||  (e << "d"_mst)); // e implies d
      31   [ +  +  +  - ]:    18000884 :     CHECK_NONFATAL(!(e << "V"_mst) || !(e << "e"_mst)); // V conflicts with e
      32   [ +  +  +  - ]:    30929794 :     CHECK_NONFATAL(!(e << "d"_mst) || !(e << "f"_mst)); // d conflicts with f
      33   [ +  +  +  - ]:    18000884 :     CHECK_NONFATAL(!(e << "V"_mst) ||  (e << "f"_mst)); // V implies f
      34   [ +  +  +  - ]:    17734769 :     CHECK_NONFATAL(!(e << "K"_mst) ||  (e << "s"_mst)); // K implies s
      35   [ +  +  +  - ]:    30298649 :     CHECK_NONFATAL(!(e << "z"_mst) ||  (e << "m"_mst)); // z implies m
      36                 :    17492589 :     return e;
      37                 :             : }
      38                 :             : 
      39                 :    29120121 : Type ComputeType(Fragment fragment, Type x, Type y, Type z, const std::vector<Type>& sub_types, uint32_t k,
      40                 :             :                  size_t data_size, size_t n_subs, size_t n_keys, MiniscriptContext ms_ctx) {
      41                 :             :     // Sanity check on data
      42         [ +  + ]:    29120121 :     if (fragment == Fragment::SHA256 || fragment == Fragment::HASH256) {
      43                 :       29527 :         CHECK_NONFATAL(data_size == 32);
      44         [ +  + ]:    29090594 :     } else if (fragment == Fragment::RIPEMD160 || fragment == Fragment::HASH160) {
      45                 :       29714 :         CHECK_NONFATAL(data_size == 20);
      46                 :             :     } else {
      47                 :    29060880 :         CHECK_NONFATAL(data_size == 0);
      48                 :             :     }
      49                 :             :     // Sanity check on k
      50         [ +  + ]:    29120121 :     if (fragment == Fragment::OLDER || fragment == Fragment::AFTER) {
      51                 :      144444 :         CHECK_NONFATAL(k >= 1 && k < 0x80000000UL);
      52         [ +  + ]:    28975677 :     } else if (fragment == Fragment::MULTI || fragment == Fragment::MULTI_A) {
      53   [ +  -  -  + ]:       53278 :         CHECK_NONFATAL(k >= 1 && k <= n_keys);
      54         [ +  + ]:    28922399 :     } else if (fragment == Fragment::THRESH) {
      55   [ +  -  -  + ]:      522941 :         CHECK_NONFATAL(k >= 1 && k <= n_subs);
      56                 :             :     } else {
      57                 :    28399458 :         CHECK_NONFATAL(k == 0);
      58                 :             :     }
      59                 :             :     // Sanity check on subs
      60                 :    29120121 :     if (fragment == Fragment::AND_V || fragment == Fragment::AND_B || fragment == Fragment::OR_B ||
      61   [ +  +  +  + ]:    29120121 :         fragment == Fragment::OR_C || fragment == Fragment::OR_I || fragment == Fragment::OR_D) {
      62                 :     8613351 :         CHECK_NONFATAL(n_subs == 2);
      63         [ +  + ]:    20506770 :     } else if (fragment == Fragment::ANDOR) {
      64                 :      494088 :         CHECK_NONFATAL(n_subs == 3);
      65                 :    20012682 :     } else if (fragment == Fragment::WRAP_A || fragment == Fragment::WRAP_S || fragment == Fragment::WRAP_C ||
      66         [ +  + ]:    20012682 :                fragment == Fragment::WRAP_D || fragment == Fragment::WRAP_V || fragment == Fragment::WRAP_J ||
      67                 :             :                fragment == Fragment::WRAP_N) {
      68                 :     7282391 :         CHECK_NONFATAL(n_subs == 1);
      69         [ +  + ]:    12730291 :     } else if (fragment != Fragment::THRESH) {
      70                 :    12207350 :         CHECK_NONFATAL(n_subs == 0);
      71                 :             :     }
      72                 :             :     // Sanity check on keys
      73         [ +  + ]:    28597180 :     if (fragment == Fragment::PK_K || fragment == Fragment::PK_H) {
      74                 :      204431 :         CHECK_NONFATAL(n_keys == 1);
      75         [ +  + ]:    28915690 :     } else if (fragment == Fragment::MULTI) {
      76                 :       40069 :         CHECK_NONFATAL(n_keys >= 1 && n_keys <= MAX_PUBKEYS_PER_MULTISIG);
      77                 :       40069 :         CHECK_NONFATAL(!IsTapscript(ms_ctx));
      78         [ +  + ]:    28875621 :     } else if (fragment == Fragment::MULTI_A) {
      79                 :       13209 :         CHECK_NONFATAL(n_keys >= 1 && n_keys <= MAX_PUBKEYS_PER_MULTI_A);
      80                 :       13209 :         CHECK_NONFATAL(IsTapscript(ms_ctx));
      81                 :             :     } else {
      82                 :    28862412 :         CHECK_NONFATAL(n_keys == 0);
      83                 :             :     }
      84                 :             : 
      85                 :             :     // Below is the per-fragment logic for computing the expression types.
      86                 :             :     // It heavily relies on Type's << operator (where "X << a_mst" means
      87                 :             :     // "X has all properties listed in a").
      88   [ +  +  +  +  :    29120121 :     switch (fragment) {
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
             +  +  +  - ]
      89                 :      140690 :         case Fragment::PK_K: return "Konudemsxk"_mst;
      90                 :       63741 :         case Fragment::PK_H: return "Knudemsxk"_mst;
      91                 :       70106 :         case Fragment::OLDER: return
      92                 :       70106 :             "g"_mst.If(k & CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) |
      93         [ +  + ]:       70106 :             "h"_mst.If(!(k & CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG)) |
      94         [ +  + ]:      140212 :             "Bzfmxk"_mst;
      95                 :       74338 :         case Fragment::AFTER: return
      96                 :       74338 :             "i"_mst.If(k >= LOCKTIME_THRESHOLD) |
      97         [ +  + ]:       74338 :             "j"_mst.If(k < LOCKTIME_THRESHOLD) |
      98         [ +  + ]:      148676 :             "Bzfmxk"_mst;
      99                 :       12684 :         case Fragment::SHA256: return "Bonudmk"_mst;
     100                 :       13815 :         case Fragment::RIPEMD160: return "Bonudmk"_mst;
     101                 :       16843 :         case Fragment::HASH256: return "Bonudmk"_mst;
     102                 :       15899 :         case Fragment::HASH160: return "Bonudmk"_mst;
     103                 :     2986169 :         case Fragment::JUST_1: return "Bzufmxk"_mst;
     104                 :     8759787 :         case Fragment::JUST_0: return "Bzudemsxk"_mst;
     105                 :      752890 :         case Fragment::WRAP_A: return
     106                 :      752890 :             "W"_mst.If(x << "B"_mst) | // W=B_x
     107         [ +  + ]:      752890 :             (x & "ghijk"_mst) | // g=g_x, h=h_x, i=i_x, j=j_x, k=k_x
     108                 :     1505780 :             (x & "udfems"_mst) | // u=u_x, d=d_x, f=f_x, e=e_x, m=m_x, s=s_x
     109         [ +  + ]:     1160619 :             "x"_mst; // x
     110                 :      393510 :         case Fragment::WRAP_S: return
     111                 :      393510 :             "W"_mst.If(x << "Bo"_mst) | // W=B_x*o_x
     112         [ +  + ]:      393510 :             (x & "ghijk"_mst) | // g=g_x, h=h_x, i=i_x, j=j_x, k=k_x
     113         [ +  + ]:      476109 :             (x & "udfemsx"_mst); // u=u_x, d=d_x, f=f_x, e=e_x, m=m_x, s=s_x, x=x_x
     114                 :     2143423 :         case Fragment::WRAP_C: return
     115                 :     2143423 :             "B"_mst.If(x << "K"_mst) | // B=K_x
     116         [ +  + ]:     2143423 :             (x & "ghijk"_mst) | // g=g_x, h=h_x, i=i_x, j=j_x, k=k_x
     117                 :     4286846 :             (x & "ondfem"_mst) | // o=o_x, n=n_x, d=d_x, f=f_x, e=e_x, m=m_x
     118         [ +  + ]:     2311414 :             "us"_mst; // u, s
     119                 :      558433 :         case Fragment::WRAP_D: return
     120                 :      558433 :             "B"_mst.If(x << "Vz"_mst) | // B=V_x*z_x
     121         [ +  + ]:      558433 :             "o"_mst.If(x << "z"_mst) | // o=z_x
     122         [ +  + ]:      558433 :             "e"_mst.If(x << "f"_mst) | // e=f_x
     123         [ +  + ]:      558433 :             (x & "ghijk"_mst) | // g=g_x, h=h_x, i=i_x, j=j_x, k=k_x
     124                 :     1116866 :             (x & "ms"_mst) | // m=m_x, s=s_x
     125                 :             :             // NOTE: 'd:' is 'u' under Tapscript but not P2WSH as MINIMALIF is only a policy rule there.
     126   [ +  +  +  + ]:     1088957 :             "u"_mst.If(IsTapscript(ms_ctx)) |
     127                 :     1116866 :             "ndx"_mst; // n, d, x
     128                 :      668666 :         case Fragment::WRAP_V: return
     129                 :      668666 :             "V"_mst.If(x << "B"_mst) | // V=B_x
     130         [ +  + ]:      668666 :             (x & "ghijk"_mst) | // g=g_x, h=h_x, i=i_x, j=j_x, k=k_x
     131                 :     1337332 :             (x & "zonms"_mst) | // z=z_x, o=o_x, n=n_x, m=m_x, s=s_x
     132         [ +  + ]:     1051955 :             "fx"_mst; // f, x
     133                 :      630650 :         case Fragment::WRAP_J: return
     134                 :      630650 :             "B"_mst.If(x << "Bn"_mst) | // B=B_x*n_x
     135         [ +  + ]:      630650 :             "e"_mst.If(x << "f"_mst) | // e=f_x
     136         [ +  + ]:      630650 :             (x & "ghijk"_mst) | // g=g_x, h=h_x, i=i_x, j=j_x, k=k_x
     137                 :     1261300 :             (x & "oums"_mst) | // o=o_x, u=u_x, m=m_x, s=s_x
     138         [ +  + ]:     1261300 :             "ndx"_mst; // n, d, x
     139                 :     2134819 :         case Fragment::WRAP_N: return
     140                 :     2134819 :             (x & "ghijk"_mst) | // g=g_x, h=h_x, i=i_x, j=j_x, k=k_x
     141                 :     2134819 :             (x & "Bzondfems"_mst) | // B=B_x, z=z_x, o=o_x, n=n_x, d=d_x, f=f_x, e=e_x, m=m_x, s=s_x
     142                 :     2134819 :             "ux"_mst; // u, x
     143                 :     2800909 :         case Fragment::AND_V: return
     144   [ +  +  +  + ]:     5861746 :             (y & "KVB"_mst).If(x << "V"_mst) | // B=V_x*B_y, V=V_x*V_y, K=V_x*K_y
     145         [ +  + ]:     2800909 :             (x & "n"_mst) | (y & "n"_mst).If(x << "z"_mst) | // n=n_x+z_x*n_y
     146         [ +  + ]:     2800909 :             ((x | y) & "o"_mst).If((x | y) << "z"_mst) | // o=o_x*z_y+z_x*o_y
     147         [ +  + ]:     2800909 :             (x & y & "dmz"_mst) | // d=d_x*d_y, m=m_x*m_y, z=z_x*z_y
     148         [ +  + ]:     2800909 :             ((x | y) & "s"_mst) | // s=s_x+s_y
     149         [ +  + ]:      394000 :             "f"_mst.If((y << "f"_mst) || (x << "s"_mst)) | // f=f_y+s_x
     150         [ +  + ]:     2800909 :             (y & "ux"_mst) | // u=u_y, x=x_y
     151         [ +  + ]:     2800909 :             ((x | y) & "ghij"_mst) | // g=g_x+g_y, h=h_x+h_y, i=i_x+i_y, j=j_x+j_y
     152   [ +  +  +  + ]:     2800909 :             "k"_mst.If(((x & y) << "k"_mst) &&
     153   [ +  +  +  +  :      536021 :                 !(((x << "g"_mst) && (y << "h"_mst)) ||
             +  +  +  + ]
     154   [ +  +  +  +  :      269364 :                 ((x << "h"_mst) && (y << "g"_mst)) ||
                   +  + ]
     155   [ +  +  +  + ]:      267403 :                 ((x << "i"_mst) && (y << "j"_mst)) ||
     156   [ +  +  +  + ]:     3065439 :                 ((x << "j"_mst) && (y << "i"_mst)))); // k=k_x*k_y*!(g_x*h_y + h_x*g_y + i_x*j_y + j_x*i_y)
     157                 :      423796 :         case Fragment::AND_B: return
     158                 :      423796 :             (x & "B"_mst).If(y << "W"_mst) | // B=B_x*W_y
     159   [ +  +  +  + ]:      604237 :             ((x | y) & "o"_mst).If((x | y) << "z"_mst) | // o=o_x*z_y+z_x*o_y
     160         [ +  + ]:      423796 :             (x & "n"_mst) | (y & "n"_mst).If(x << "z"_mst) | // n=n_x+z_x*n_y
     161   [ +  +  +  + ]:      454056 :             (x & y & "e"_mst).If((x & y) << "s"_mst) | // e=e_x*e_y*s_x*s_y
     162         [ +  + ]:      423796 :             (x & y & "dzm"_mst) | // d=d_x*d_y, z=z_x*z_y, m=m_x*m_y
     163   [ +  +  +  + ]:      406143 :             "f"_mst.If(((x & y) << "f"_mst) || (x << "sf"_mst) || (y << "sf"_mst)) | // f=f_x*f_y + f_x*s_x + f_y*s_y
     164                 :      847592 :             ((x | y) & "s"_mst) | // s=s_x+s_y
     165         [ +  + ]:      423796 :             "ux"_mst | // u, x
     166         [ +  + ]:      423796 :             ((x | y) & "ghij"_mst) | // g=g_x+g_y, h=h_x+h_y, i=i_x+i_y, j=j_x+j_y
     167   [ +  +  +  + ]:      423796 :             "k"_mst.If(((x & y) << "k"_mst) &&
     168   [ +  +  +  +  :      116867 :                 !(((x << "g"_mst) && (y << "h"_mst)) ||
             +  +  +  + ]
     169   [ +  +  +  +  :       58879 :                 ((x << "h"_mst) && (y << "g"_mst)) ||
                   +  + ]
     170   [ +  +  +  + ]:       57197 :                 ((x << "i"_mst) && (y << "j"_mst)) ||
     171   [ +  +  +  + ]:      480148 :                 ((x << "j"_mst) && (y << "i"_mst)))); // k=k_x*k_y*!(g_x*h_y + h_x*g_y + i_x*j_y + j_x*i_y)
     172                 :      447110 :         case Fragment::OR_B: return
     173         [ +  + ]:      519667 :             "B"_mst.If(x << "Bd"_mst && y << "Wd"_mst) | // B=B_x*d_x*W_x*d_y
     174   [ +  +  +  + ]:      634381 :             ((x | y) & "o"_mst).If((x | y) << "z"_mst) | // o=o_x*z_y+z_x*o_y
     175         [ +  + ]:       51118 :             (x & y & "m"_mst).If((x | y) << "s"_mst && (x & y) << "e"_mst) | // m=m_x*m_y*e_x*e_y*(s_x+s_y)
     176                 :      894220 :             (x & y & "zse"_mst) | // z=z_x*z_y, s=s_x*s_y, e=e_x*e_y
     177         [ +  + ]:      447110 :             "dux"_mst | // d, u, x
     178         [ +  + ]:      447110 :             ((x | y) & "ghij"_mst) | // g=g_x+g_y, h=h_x+h_y, i=i_x+i_y, j=j_x+j_y
     179         [ +  + ]:      894220 :             (x & y & "k"_mst); // k=k_x*k_y
     180                 :      439901 :         case Fragment::OR_D: return
     181                 :      439901 :             (y & "B"_mst).If(x << "Bdu"_mst) | // B=B_y*B_x*d_x*u_x
     182   [ +  +  +  + ]:      566572 :             (x & "o"_mst).If(y << "z"_mst) | // o=o_x*z_y
     183         [ +  + ]:       62578 :             (x & y & "m"_mst).If(x << "e"_mst && (x | y) << "s"_mst) | // m=m_x*m_y*e_x*(s_x+s_y)
     184                 :      879802 :             (x & y & "zs"_mst) | // z=z_x*z_y, s=s_x*s_y
     185                 :      879802 :             (y & "ufde"_mst) | // u=u_y, f=f_y, d=d_y, e=e_y
     186         [ +  + ]:      439901 :             "x"_mst | // x
     187         [ +  + ]:      439901 :             ((x | y) & "ghij"_mst) | // g=g_x+g_y, h=h_x+h_y, i=i_x+i_y, j=j_x+j_y
     188         [ +  + ]:      879802 :             (x & y & "k"_mst); // k=k_x*k_y
     189                 :      422462 :         case Fragment::OR_C: return
     190                 :      422462 :             (y & "V"_mst).If(x << "Bdu"_mst) | // V=V_y*B_x*u_x*d_x
     191   [ +  +  +  + ]:      521921 :             (x & "o"_mst).If(y << "z"_mst) | // o=o_x*z_y
     192         [ +  + ]:       39842 :             (x & y & "m"_mst).If(x << "e"_mst && (x | y) << "s"_mst) | // m=m_x*m_y*e_x*(s_x+s_y)
     193                 :      844924 :             (x & y & "zs"_mst) | // z=z_x*z_y, s=s_x*s_y
     194         [ +  + ]:      422462 :             "fx"_mst | // f, x
     195         [ +  + ]:      422462 :             ((x | y) & "ghij"_mst) | // g=g_x+g_y, h=h_x+h_y, i=i_x+i_y, j=j_x+j_y
     196         [ +  + ]:      844924 :             (x & y & "k"_mst); // k=k_x*k_y
     197                 :     4079173 :         case Fragment::OR_I: return
     198                 :     4079173 :             (x & y & "VBKufs"_mst) | // V=V_x*V_y, B=B_x*B_y, K=K_x*K_y, u=u_x*u_y, f=f_x*f_y, s=s_x*s_y
     199                 :     4079173 :             "o"_mst.If((x & y) << "z"_mst) | // o=z_x*z_y
     200         [ +  + ]:     4079173 :             ((x | y) & "e"_mst).If((x | y) << "f"_mst) | // e=e_x*f_y+f_x*e_y
     201         [ +  + ]:     4079173 :             (x & y & "m"_mst).If((x | y) << "s"_mst) | // m=m_x*m_y*(s_x+s_y)
     202                 :     8158346 :             ((x | y) & "d"_mst) | // d=d_x+d_y
     203         [ +  + ]:     4079173 :             "x"_mst | // x
     204         [ +  + ]:     4079173 :             ((x | y) & "ghij"_mst) | // g=g_x+g_y, h=h_x+h_y, i=i_x+i_y, j=j_x+j_y
     205         [ +  + ]:     8158346 :             (x & y & "k"_mst); // k=k_x*k_y
     206                 :      494088 :         case Fragment::ANDOR: return
     207         [ +  + ]:      625707 :             (y & z & "BKV"_mst).If(x << "Bdu"_mst) | // B=B_x*d_x*u_x*B_y*B_z, K=B_x*d_x*u_x*K_y*K_z, V=B_x*d_x*u_x*V_y*V_z
     208                 :      988176 :             (x & y & z & "z"_mst) | // z=z_x*z_y*z_z
     209   [ +  +  +  + ]:      650816 :             ((x | (y & z)) & "o"_mst).If((x | (y & z)) << "z"_mst) | // o=o_x*z_y*z_z+z_x*o_y*o_z
     210         [ +  + ]:      494088 :             (y & z & "u"_mst) | // u=u_y*u_z
     211   [ +  +  +  + ]:      494088 :             (z & "f"_mst).If((x << "s"_mst) || (y << "f"_mst)) | // f=(s_x+f_y)*f_z
     212         [ +  + ]:      494088 :             (z & "d"_mst) | // d=d_z
     213   [ +  +  +  + ]:      494088 :             (z & "e"_mst).If(x << "s"_mst || y << "f"_mst) | // e=e_z*(s_x+f_y)
     214         [ +  + ]:       82633 :             (x & y & z & "m"_mst).If(x << "e"_mst && (x | y | z) << "s"_mst) | // m=m_x*m_y*m_z*e_x*(s_x+s_y+s_z)
     215                 :      988176 :             (z & (x | y) & "s"_mst) | // s=s_z*(s_x+s_y)
     216         [ +  + ]:      494088 :             "x"_mst | // x
     217         [ +  + ]:      494088 :             ((x | y | z) & "ghij"_mst) | // g=g_x+g_y+g_z, h=h_x+h_y+h_z, i=i_x+i_y+i_z, j=j_x+j_y_j_z
     218   [ +  +  +  + ]:      494088 :             "k"_mst.If(((x & y & z) << "k"_mst) &&
     219   [ +  +  +  +  :      184156 :                 !(((x << "g"_mst) && (y << "h"_mst)) ||
             +  +  +  + ]
     220   [ +  +  +  +  :       93318 :                 ((x << "h"_mst) && (y << "g"_mst)) ||
                   +  + ]
     221   [ +  +  +  + ]:       92071 :                 ((x << "i"_mst) && (y << "j"_mst)) ||
     222   [ +  +  +  + ]:      584086 :                 ((x << "j"_mst) && (y << "i"_mst)))); // k=k_x*k_y*k_z* !(g_x*h_y + h_x*g_y + i_x*j_y + j_x*i_y)
     223                 :       40069 :         case Fragment::MULTI: {
     224                 :       40069 :             return "Bnudemsk"_mst;
     225                 :             :         }
     226                 :       13209 :         case Fragment::MULTI_A: {
     227                 :       13209 :             return "Budemsk"_mst;
     228                 :             :         }
     229                 :      522941 :         case Fragment::THRESH: {
     230                 :      522941 :             bool all_e = true;
     231                 :      522941 :             bool all_m = true;
     232                 :      522941 :             uint32_t args = 0;
     233                 :      522941 :             uint32_t num_s = 0;
     234                 :      522941 :             Type acc_tl = "k"_mst;
     235         [ +  + ]:      887383 :             for (size_t i = 0; i < sub_types.size(); ++i) {
     236         [ +  + ]:      777699 :                 Type t = sub_types[i];
     237   [ +  +  +  + ]:      777699 :                 if (!(t << (i ? "Wdu"_mst : "Bdu"_mst))) return ""_mst; // Require Bdu, Wdu, Wdu, ...
     238         [ +  + ]:      364442 :                 if (!(t << "e"_mst)) all_e = false;
     239         [ +  + ]:      364442 :                 if (!(t << "m"_mst)) all_m = false;
     240         [ +  + ]:      364442 :                 if (t << "s"_mst) num_s += 1;
     241   [ +  +  +  + ]:      364442 :                 args += (t << "z"_mst) ? 0 : (t << "o"_mst) ? 1 : 2;
     242                 :      364442 :                 acc_tl = ((acc_tl | t) & "ghij"_mst) |
     243                 :             :                     // Thresh contains a combination of timelocks if it has threshold > 1 and
     244                 :             :                     // it contains two different children that have different types of timelocks
     245                 :             :                     // Note how if any of the children don't have "k", the parent also does not have "k"
     246   [ +  +  +  + ]:      364442 :                     "k"_mst.If(((acc_tl & t) << "k"_mst) && ((k <= 1) ||
     247   [ +  +  +  +  :      334957 :                         ((k > 1) && !(((acc_tl << "g"_mst) && (t << "h"_mst)) ||
             +  +  +  + ]
     248   [ +  +  +  +  :      169188 :                         ((acc_tl << "h"_mst) && (t << "g"_mst)) ||
                   +  + ]
     249   [ +  +  +  + ]:      167194 :                         ((acc_tl << "i"_mst) && (t << "j"_mst)) ||
     250   [ +  +  +  + ]:      419072 :                         ((acc_tl << "j"_mst) && (t << "i"_mst))))));
     251                 :             :             }
     252                 :      219368 :             return "Bdu"_mst |
     253                 :      109684 :                    "z"_mst.If(args == 0) | // z=all z
     254   [ +  +  +  + ]:      122249 :                    "o"_mst.If(args == 1) | // o=all z except one o
     255   [ +  +  +  +  :      109684 :                    "e"_mst.If(all_e && num_s == n_subs) | // e=all e and all s
                   +  + ]
     256   [ +  +  +  + ]:      109684 :                    "m"_mst.If(all_e && all_m && num_s >= n_subs - k) | // m=all e, >=(n-k) s
     257   [ +  +  +  + ]:      187175 :                    "s"_mst.If(num_s >= n_subs - k + 1) |  // s= >=(n-k+1) s
     258                 :      109684 :                    acc_tl; // timelock info
     259                 :             :             }
     260                 :             :     }
     261                 :           0 :     assert(false);
     262                 :             : }
     263                 :             : 
     264                 :    26674288 : size_t ComputeScriptLen(Fragment fragment, Type sub0typ, size_t subsize, uint32_t k, size_t n_subs,
     265                 :             :                         size_t n_keys, MiniscriptContext ms_ctx) {
     266   [ +  +  +  +  :    26674288 :     switch (fragment) {
          +  +  +  +  +  
          +  +  +  +  +  
                   -  + ]
     267                 :             :         case Fragment::JUST_1:
     268                 :             :         case Fragment::JUST_0: return 1;
     269         [ +  + ]:      156846 :         case Fragment::PK_K: return IsTapscript(ms_ctx) ? 33 : 34;
     270                 :       73093 :         case Fragment::PK_H: return 3 + 21;
     271                 :      155419 :         case Fragment::OLDER:
     272         [ -  + ]:      155419 :         case Fragment::AFTER: return 1 + BuildScript(k).size();
     273                 :       38289 :         case Fragment::HASH256:
     274                 :       38289 :         case Fragment::SHA256: return 4 + 2 + 33;
     275                 :       37671 :         case Fragment::HASH160:
     276                 :       37671 :         case Fragment::RIPEMD160: return 4 + 2 + 21;
     277   [ -  +  +  - ]:       99298 :         case Fragment::MULTI: return 1 + BuildScript(n_keys).size() + BuildScript(k).size() + 34 * n_keys;
     278         [ -  + ]:       15416 :         case Fragment::MULTI_A: return (1 + 32 + 1) * n_keys + BuildScript(k).size() + 1;
     279                 :     2495768 :         case Fragment::AND_V: return subsize;
     280                 :      706502 :         case Fragment::WRAP_V: return subsize + (sub0typ << "x"_mst);
     281                 :     4871876 :         case Fragment::WRAP_S:
     282                 :     4871876 :         case Fragment::WRAP_C:
     283                 :     4871876 :         case Fragment::WRAP_N:
     284                 :     4871876 :         case Fragment::AND_B:
     285                 :     4871876 :         case Fragment::OR_B: return subsize + 1;
     286                 :      840783 :         case Fragment::WRAP_A:
     287                 :      840783 :         case Fragment::OR_C: return subsize + 2;
     288                 :     4613335 :         case Fragment::WRAP_D:
     289                 :     4613335 :         case Fragment::OR_D:
     290                 :     4613335 :         case Fragment::OR_I:
     291                 :     4613335 :         case Fragment::ANDOR: return subsize + 3;
     292                 :      628868 :         case Fragment::WRAP_J: return subsize + 4;
     293         [ -  + ]:      161071 :         case Fragment::THRESH: return subsize + n_subs + BuildScript(k).size();
     294                 :             :     }
     295                 :           0 :     assert(false);
     296                 :             : }
     297                 :             : 
     298                 :     3739219 : InputStack& InputStack::SetAvailable(Availability avail) {
     299                 :     3739219 :     available = avail;
     300         [ +  + ]:     3739219 :     if (avail == Availability::NO) {
     301                 :     3626743 :         stack.clear();
     302                 :     3626743 :         size = std::numeric_limits<size_t>::max();
     303                 :     3626743 :         has_sig = false;
     304                 :     3626743 :         malleable = false;
     305                 :     3626743 :         non_canon = false;
     306                 :             :     }
     307                 :     3739219 :     return *this;
     308                 :             : }
     309                 :             : 
     310                 :      179560 : InputStack& InputStack::SetWithSig() {
     311                 :      179560 :     has_sig = true;
     312                 :      179560 :     return *this;
     313                 :             : }
     314                 :             : 
     315                 :      161899 : InputStack& InputStack::SetNonCanon() {
     316                 :      161899 :     non_canon = true;
     317                 :      161899 :     return *this;
     318                 :             : }
     319                 :             : 
     320                 :       80156 : InputStack& InputStack::SetMalleable(bool x) {
     321                 :       80156 :     malleable = x;
     322                 :       80156 :     return *this;
     323                 :             : }
     324                 :             : 
     325                 :     5621718 : InputStack operator+(InputStack a, InputStack b) {
     326         [ +  - ]:     5621718 :     a.stack = Cat(std::move(a.stack), std::move(b.stack));
     327   [ +  +  +  + ]:     5621718 :     if (a.available != Availability::NO && b.available != Availability::NO) a.size += b.size;
     328                 :     5621718 :     a.has_sig |= b.has_sig;
     329                 :     5621718 :     a.malleable |= b.malleable;
     330                 :     5621718 :     a.non_canon |= b.non_canon;
     331   [ +  +  +  + ]:     5621718 :     if (a.available == Availability::NO || b.available == Availability::NO) {
     332                 :     3529507 :         a.SetAvailable(Availability::NO);
     333   [ +  -  -  + ]:     2092211 :     } else if (a.available == Availability::MAYBE || b.available == Availability::MAYBE) {
     334                 :           0 :         a.SetAvailable(Availability::MAYBE);
     335                 :             :     }
     336                 :     5621718 :     return a;
     337                 :             : }
     338                 :             : 
     339                 :     2828916 : InputStack operator|(InputStack a, InputStack b) {
     340                 :             :     // If only one is invalid, pick the other one. If both are invalid, pick an arbitrary one.
     341         [ +  + ]:     2828916 :     if (a.available == Availability::NO) return b;
     342         [ +  + ]:     1165378 :     if (b.available == Availability::NO) return a;
     343                 :             :     // If only one of the solutions has a signature, we must pick the other one.
     344   [ +  +  +  + ]:      800948 :     if (!a.has_sig && b.has_sig) return a;
     345   [ +  +  +  + ]:      793866 :     if (!b.has_sig && a.has_sig) return b;
     346   [ +  +  +  - ]:      784640 :     if (!a.has_sig && !b.has_sig) {
     347                 :             :         // If neither solution requires a signature, the result is inevitably malleable.
     348                 :       67092 :         a.malleable = true;
     349                 :       67092 :         b.malleable = true;
     350                 :             :     } else {
     351                 :             :         // If both options require a signature, prefer the non-malleable one.
     352   [ +  +  +  + ]:      717548 :         if (b.malleable && !a.malleable) return a;
     353   [ +  +  +  + ]:      715076 :         if (a.malleable && !b.malleable) return b;
     354                 :             :     }
     355                 :             :     // Between two malleable or two non-malleable solutions, pick the smaller one between
     356                 :             :     // YESes, and the bigger ones between MAYBEs. Prefer YES over MAYBE.
     357   [ +  -  +  - ]:      780848 :     if (a.available == Availability::YES && b.available == Availability::YES) {
     358         [ +  + ]:      813236 :         return std::move(a.size <= b.size ? a : b);
     359   [ #  #  #  # ]:           0 :     } else if (a.available == Availability::MAYBE && b.available == Availability::MAYBE) {
     360         [ #  # ]:           0 :         return std::move(a.size >= b.size ? a : b);
     361         [ #  # ]:           0 :     } else if (a.available == Availability::YES) {
     362                 :           0 :         return a;
     363                 :             :     } else {
     364                 :           0 :         return b;
     365                 :             :     }
     366                 :             : }
     367                 :             : 
     368                 :       34236 : std::optional<std::vector<Opcode>> DecomposeScript(const CScript& script)
     369                 :             : {
     370                 :       34236 :     std::vector<Opcode> out;
     371         [ +  + ]:       68472 :     CScript::const_iterator it = script.begin(), itend = script.end();
     372         [ +  + ]:    12966988 :     while (it != itend) {
     373                 :    12934639 :         std::vector<unsigned char> push_data;
     374                 :    12934639 :         opcodetype opcode;
     375   [ +  -  +  + ]:    12934639 :         if (!script.GetOp(it, opcode, push_data)) {
     376                 :        1758 :             return {};
     377         [ +  + ]:    12932881 :         } else if (opcode >= OP_1 && opcode <= OP_16) {
     378                 :             :             // Deal with OP_n (GetOp does not turn them into pushes).
     379         [ +  - ]:      549814 :             push_data.assign(1, CScript::DecodeOP_N(opcode));
     380         [ +  + ]:    12383067 :         } else if (opcode == OP_CHECKSIGVERIFY) {
     381                 :             :             // Decompose OP_CHECKSIGVERIFY into OP_CHECKSIG OP_VERIFY
     382         [ +  - ]:       64583 :             out.emplace_back(OP_CHECKSIG, std::vector<unsigned char>());
     383                 :       64583 :             opcode = OP_VERIFY;
     384         [ +  + ]:    12318484 :         } else if (opcode == OP_CHECKMULTISIGVERIFY) {
     385                 :             :             // Decompose OP_CHECKMULTISIGVERIFY into OP_CHECKMULTISIG OP_VERIFY
     386         [ +  - ]:        6725 :             out.emplace_back(OP_CHECKMULTISIG, std::vector<unsigned char>());
     387                 :        6725 :             opcode = OP_VERIFY;
     388         [ +  + ]:    12311759 :         } else if (opcode == OP_EQUALVERIFY) {
     389                 :             :             // Decompose OP_EQUALVERIFY into OP_EQUAL OP_VERIFY
     390         [ +  - ]:       76245 :             out.emplace_back(OP_EQUAL, std::vector<unsigned char>());
     391                 :       76245 :             opcode = OP_VERIFY;
     392         [ +  + ]:    12235514 :         } else if (opcode == OP_NUMEQUALVERIFY) {
     393                 :             :             // Decompose OP_NUMEQUALVERIFY into OP_NUMEQUAL OP_VERIFY
     394         [ +  - ]:        5393 :             out.emplace_back(OP_NUMEQUAL, std::vector<unsigned char>());
     395                 :        5393 :             opcode = OP_VERIFY;
     396         [ +  + ]:    12230121 :         } else if (IsPushdataOp(opcode)) {
     397   [ +  -  +  + ]:      243070 :             if (!CheckMinimalPush(push_data, opcode)) return {};
     398   [ +  +  +  +  :    11987051 :         } else if (it != itend && (opcode == OP_CHECKSIG || opcode == OP_CHECKMULTISIG || opcode == OP_EQUAL || opcode == OP_NUMEQUAL) && (*it == OP_VERIFY)) {
                   +  + ]
     399                 :             :             // Rule out non minimal VERIFY sequences
     400                 :          10 :             return {};
     401                 :             :         }
     402         [ +  - ]:    12932752 :         out.emplace_back(opcode, std::move(push_data));
     403                 :    12934639 :     }
     404                 :       32349 :     std::reverse(out.begin(), out.end());
     405                 :       32349 :     return out;
     406                 :       34236 : }
     407                 :             : 
     408                 :      158757 : std::optional<int64_t> ParseScriptNumber(const Opcode& in) {
     409         [ +  + ]:      158757 :     if (in.first == OP_0) {
     410                 :          74 :         return 0;
     411                 :             :     }
     412         [ +  + ]:      158683 :     if (!in.second.empty()) {
     413   [ +  +  -  + ]:      158523 :         if (IsPushdataOp(in.first) && !CheckMinimalPush(in.second, in.first)) return {};
     414                 :      158523 :         try {
     415         [ +  + ]:      158523 :             return CScriptNum(in.second, true).GetInt64();
     416         [ -  + ]:         856 :         } catch(const scriptnum_error&) {}
     417                 :             :     }
     418                 :        1016 :     return {};
     419                 :             : }
     420                 :             : 
     421                 :      509119 : int FindNextChar(std::span<const char> sp, const char m)
     422                 :             : {
     423         [ +  + ]:    16933845 :     for (int i = 0; i < (int)sp.size(); ++i) {
     424         [ +  + ]:    16933571 :         if (sp[i] == m) return i;
     425                 :             :         // We only search within the current parentheses
     426         [ +  + ]:    16445972 :         if (sp[i] == ')') break;
     427                 :             :     }
     428                 :             :     return -1;
     429                 :             : }
     430                 :             : 
     431                 :             : } // namespace internal
     432                 :             : } // namespace miniscript
        

Generated by: LCOV version 2.0-1