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: 2026-01-01 04:18:06 Functions: 100.0 % 12 12
Branches: 91.2 % 556 507

             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                 :    32274470 : Type SanitizeType(Type e) {
      20         [ +  + ]:    32274470 :     int num_types = (e << "K"_mst) + (e << "V"_mst) + (e << "B"_mst) + (e << "W"_mst);
      21         [ +  + ]:    32274470 :     if (num_types == 0) return ""_mst; // No valid type, don't care about the rest
      22                 :    21644915 :     CHECK_NONFATAL(num_types == 1); // K, V, B, W all conflict with each other
      23   [ +  +  +  - ]:    36931091 :     CHECK_NONFATAL(!(e << "z"_mst) || !(e << "o"_mst)); // z conflicts with o
      24   [ +  +  +  - ]:    23056508 :     CHECK_NONFATAL(!(e << "n"_mst) || !(e << "z"_mst)); // n conflicts with z
      25   [ +  +  +  - ]:    23056508 :     CHECK_NONFATAL(!(e << "n"_mst) || !(e << "W"_mst)); // n conflicts with W
      26   [ +  +  +  - ]:    22321029 :     CHECK_NONFATAL(!(e << "V"_mst) || !(e << "d"_mst)); // V conflicts with d
      27   [ +  +  +  - ]:    21971682 :     CHECK_NONFATAL(!(e << "K"_mst) ||  (e << "u"_mst)); // K implies u
      28   [ +  +  +  - ]:    22321029 :     CHECK_NONFATAL(!(e << "V"_mst) || !(e << "u"_mst)); // V conflicts with u
      29   [ +  +  +  - ]:    34716927 :     CHECK_NONFATAL(!(e << "e"_mst) || !(e << "f"_mst)); // e conflicts with f
      30   [ +  +  +  - ]:    34716927 :     CHECK_NONFATAL(!(e << "e"_mst) ||  (e << "d"_mst)); // e implies d
      31   [ +  +  +  - ]:    22321029 :     CHECK_NONFATAL(!(e << "V"_mst) || !(e << "e"_mst)); // V conflicts with e
      32   [ +  +  +  - ]:    37910565 :     CHECK_NONFATAL(!(e << "d"_mst) || !(e << "f"_mst)); // d conflicts with f
      33   [ +  +  +  - ]:    22321029 :     CHECK_NONFATAL(!(e << "V"_mst) ||  (e << "f"_mst)); // V implies f
      34   [ +  +  +  - ]:    21971682 :     CHECK_NONFATAL(!(e << "K"_mst) ||  (e << "s"_mst)); // K implies s
      35   [ +  +  +  - ]:    36931091 :     CHECK_NONFATAL(!(e << "z"_mst) ||  (e << "m"_mst)); // z implies m
      36                 :    21644915 :     return e;
      37                 :             : }
      38                 :             : 
      39                 :    35111260 : 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         [ +  + ]:    35111260 :     if (fragment == Fragment::SHA256 || fragment == Fragment::HASH256) {
      43                 :       29901 :         CHECK_NONFATAL(data_size == 32);
      44         [ +  + ]:    35081359 :     } else if (fragment == Fragment::RIPEMD160 || fragment == Fragment::HASH160) {
      45                 :       32522 :         CHECK_NONFATAL(data_size == 20);
      46                 :             :     } else {
      47                 :    35048837 :         CHECK_NONFATAL(data_size == 0);
      48                 :             :     }
      49                 :             :     // Sanity check on k
      50         [ +  + ]:    35111260 :     if (fragment == Fragment::OLDER || fragment == Fragment::AFTER) {
      51                 :      179465 :         CHECK_NONFATAL(k >= 1 && k < 0x80000000UL);
      52         [ +  + ]:    34931795 :     } else if (fragment == Fragment::MULTI || fragment == Fragment::MULTI_A) {
      53   [ +  -  -  + ]:       68959 :         CHECK_NONFATAL(k >= 1 && k <= n_keys);
      54         [ +  + ]:    34862836 :     } else if (fragment == Fragment::THRESH) {
      55   [ +  -  -  + ]:      551057 :         CHECK_NONFATAL(k >= 1 && k <= n_subs);
      56                 :             :     } else {
      57                 :    34311779 :         CHECK_NONFATAL(k == 0);
      58                 :             :     }
      59                 :             :     // Sanity check on subs
      60                 :    35111260 :     if (fragment == Fragment::AND_V || fragment == Fragment::AND_B || fragment == Fragment::OR_B ||
      61   [ +  +  +  + ]:    35111260 :         fragment == Fragment::OR_C || fragment == Fragment::OR_I || fragment == Fragment::OR_D) {
      62                 :    10098689 :         CHECK_NONFATAL(n_subs == 2);
      63         [ +  + ]:    25012571 :     } else if (fragment == Fragment::ANDOR) {
      64                 :      515301 :         CHECK_NONFATAL(n_subs == 3);
      65                 :    24497270 :     } else if (fragment == Fragment::WRAP_A || fragment == Fragment::WRAP_S || fragment == Fragment::WRAP_C ||
      66         [ +  + ]:    24497270 :                fragment == Fragment::WRAP_D || fragment == Fragment::WRAP_V || fragment == Fragment::WRAP_J ||
      67                 :             :                fragment == Fragment::WRAP_N) {
      68                 :     9818928 :         CHECK_NONFATAL(n_subs == 1);
      69         [ +  + ]:    14678342 :     } else if (fragment != Fragment::THRESH) {
      70                 :    14127285 :         CHECK_NONFATAL(n_subs == 0);
      71                 :             :     }
      72                 :             :     // Sanity check on keys
      73         [ +  + ]:    34560203 :     if (fragment == Fragment::PK_K || fragment == Fragment::PK_H) {
      74                 :      285974 :         CHECK_NONFATAL(n_keys == 1);
      75         [ +  + ]:    34825286 :     } else if (fragment == Fragment::MULTI) {
      76                 :       49678 :         CHECK_NONFATAL(n_keys >= 1 && n_keys <= MAX_PUBKEYS_PER_MULTISIG);
      77                 :       49678 :         CHECK_NONFATAL(!IsTapscript(ms_ctx));
      78         [ +  + ]:    34775608 :     } else if (fragment == Fragment::MULTI_A) {
      79                 :       19281 :         CHECK_NONFATAL(n_keys >= 1 && n_keys <= MAX_PUBKEYS_PER_MULTI_A);
      80                 :       19281 :         CHECK_NONFATAL(IsTapscript(ms_ctx));
      81                 :             :     } else {
      82                 :    34756327 :         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   [ +  +  +  +  :    35111260 :     switch (fragment) {
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
             +  +  +  - ]
      89                 :      202121 :         case Fragment::PK_K: return "Konudemsxk"_mst;
      90                 :       83853 :         case Fragment::PK_H: return "Knudemsxk"_mst;
      91                 :       84227 :         case Fragment::OLDER: return
      92                 :       84227 :             "g"_mst.If(k & CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) |
      93         [ +  + ]:       84227 :             "h"_mst.If(!(k & CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG)) |
      94         [ +  + ]:      168454 :             "Bzfmxk"_mst;
      95                 :       95238 :         case Fragment::AFTER: return
      96                 :       95238 :             "i"_mst.If(k >= LOCKTIME_THRESHOLD) |
      97         [ +  + ]:       95238 :             "j"_mst.If(k < LOCKTIME_THRESHOLD) |
      98         [ +  + ]:      190476 :             "Bzfmxk"_mst;
      99                 :       13396 :         case Fragment::SHA256: return "Bonudmk"_mst;
     100                 :       15838 :         case Fragment::RIPEMD160: return "Bonudmk"_mst;
     101                 :       16505 :         case Fragment::HASH256: return "Bonudmk"_mst;
     102                 :       16684 :         case Fragment::HASH160: return "Bonudmk"_mst;
     103                 :     3889281 :         case Fragment::JUST_1: return "Bzufmxk"_mst;
     104                 :     9641183 :         case Fragment::JUST_0: return "Bzudemsxk"_mst;
     105                 :     1214585 :         case Fragment::WRAP_A: return
     106                 :     1214585 :             "W"_mst.If(x << "B"_mst) | // W=B_x
     107         [ +  + ]:     1214585 :             (x & "ghijk"_mst) | // g=g_x, h=h_x, i=i_x, j=j_x, k=k_x
     108                 :     2429170 :             (x & "udfems"_mst) | // u=u_x, d=d_x, f=f_x, e=e_x, m=m_x, s=s_x
     109         [ +  + ]:     1809281 :             "x"_mst; // x
     110                 :      433102 :         case Fragment::WRAP_S: return
     111                 :      433102 :             "W"_mst.If(x << "Bo"_mst) | // W=B_x*o_x
     112         [ +  + ]:      433102 :             (x & "ghijk"_mst) | // g=g_x, h=h_x, i=i_x, j=j_x, k=k_x
     113         [ +  + ]:      525528 :             (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                 :     2675056 :         case Fragment::WRAP_C: return
     115                 :     2675056 :             "B"_mst.If(x << "K"_mst) | // B=K_x
     116         [ +  + ]:     2675056 :             (x & "ghijk"_mst) | // g=g_x, h=h_x, i=i_x, j=j_x, k=k_x
     117                 :     5350112 :             (x & "ondfem"_mst) | // o=o_x, n=n_x, d=d_x, f=f_x, e=e_x, m=m_x
     118         [ +  + ]:     2917479 :             "us"_mst; // u, s
     119                 :      738553 :         case Fragment::WRAP_D: return
     120                 :      738553 :             "B"_mst.If(x << "Vz"_mst) | // B=V_x*z_x
     121         [ +  + ]:      738553 :             "o"_mst.If(x << "z"_mst) | // o=z_x
     122         [ +  + ]:      738553 :             "e"_mst.If(x << "f"_mst) | // e=f_x
     123         [ +  + ]:      738553 :             (x & "ghijk"_mst) | // g=g_x, h=h_x, i=i_x, j=j_x, k=k_x
     124                 :     1477106 :             (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   [ +  +  +  + ]:     1438133 :             "u"_mst.If(IsTapscript(ms_ctx)) |
     127                 :     1477106 :             "ndx"_mst; // n, d, x
     128                 :      831798 :         case Fragment::WRAP_V: return
     129                 :      831798 :             "V"_mst.If(x << "B"_mst) | // V=B_x
     130         [ +  + ]:      831798 :             (x & "ghijk"_mst) | // g=g_x, h=h_x, i=i_x, j=j_x, k=k_x
     131                 :     1663596 :             (x & "zonms"_mst) | // z=z_x, o=o_x, n=n_x, m=m_x, s=s_x
     132         [ +  + ]:     1341821 :             "fx"_mst; // f, x
     133                 :      675987 :         case Fragment::WRAP_J: return
     134                 :      675987 :             "B"_mst.If(x << "Bn"_mst) | // B=B_x*n_x
     135         [ +  + ]:      675987 :             "e"_mst.If(x << "f"_mst) | // e=f_x
     136         [ +  + ]:      675987 :             (x & "ghijk"_mst) | // g=g_x, h=h_x, i=i_x, j=j_x, k=k_x
     137                 :     1351974 :             (x & "oums"_mst) | // o=o_x, u=u_x, m=m_x, s=s_x
     138         [ +  + ]:     1351974 :             "ndx"_mst; // n, d, x
     139                 :     3249847 :         case Fragment::WRAP_N: return
     140                 :     3249847 :             (x & "ghijk"_mst) | // g=g_x, h=h_x, i=i_x, j=j_x, k=k_x
     141                 :     3249847 :             (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                 :     3249847 :             "ux"_mst; // u, x
     143                 :     3512636 :         case Fragment::AND_V: return
     144   [ +  +  +  + ]:     7396990 :             (y & "KVB"_mst).If(x << "V"_mst) | // B=V_x*B_y, V=V_x*V_y, K=V_x*K_y
     145         [ +  + ]:     3512636 :             (x & "n"_mst) | (y & "n"_mst).If(x << "z"_mst) | // n=n_x+z_x*n_y
     146         [ +  + ]:     3512636 :             ((x | y) & "o"_mst).If((x | y) << "z"_mst) | // o=o_x*z_y+z_x*o_y
     147         [ +  + ]:     3512636 :             (x & y & "dmz"_mst) | // d=d_x*d_y, m=m_x*m_y, z=z_x*z_y
     148         [ +  + ]:     3512636 :             ((x | y) & "s"_mst) | // s=s_x+s_y
     149         [ +  + ]:      402904 :             "f"_mst.If((y << "f"_mst) || (x << "s"_mst)) | // f=f_y+s_x
     150         [ +  + ]:     3512636 :             (y & "ux"_mst) | // u=u_y, x=x_y
     151         [ +  + ]:     3512636 :             ((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   [ +  +  +  + ]:     3512636 :             "k"_mst.If(((x & y) << "k"_mst) &&
     153   [ +  +  +  +  :      817464 :                 !(((x << "g"_mst) && (y << "h"_mst)) ||
             +  +  +  + ]
     154   [ +  +  +  +  :      409957 :                 ((x << "h"_mst) && (y << "g"_mst)) ||
                   +  + ]
     155   [ +  +  +  + ]:      408063 :                 ((x << "i"_mst) && (y << "j"_mst)) ||
     156   [ +  +  +  + ]:     3917760 :                 ((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                 :      435167 :         case Fragment::AND_B: return
     158                 :      435167 :             (x & "B"_mst).If(y << "W"_mst) | // B=B_x*W_y
     159   [ +  +  +  + ]:      621849 :             ((x | y) & "o"_mst).If((x | y) << "z"_mst) | // o=o_x*z_y+z_x*o_y
     160         [ +  + ]:      435167 :             (x & "n"_mst) | (y & "n"_mst).If(x << "z"_mst) | // n=n_x+z_x*n_y
     161   [ +  +  +  + ]:      472829 :             (x & y & "e"_mst).If((x & y) << "s"_mst) | // e=e_x*e_y*s_x*s_y
     162         [ +  + ]:      435167 :             (x & y & "dzm"_mst) | // d=d_x*d_y, z=z_x*z_y, m=m_x*m_y
     163   [ +  +  +  + ]:      414324 :             "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                 :      870334 :             ((x | y) & "s"_mst) | // s=s_x+s_y
     165         [ +  + ]:      435167 :             "ux"_mst | // u, x
     166         [ +  + ]:      435167 :             ((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   [ +  +  +  + ]:      435167 :             "k"_mst.If(((x & y) << "k"_mst) &&
     168   [ +  +  +  +  :      144464 :                 !(((x << "g"_mst) && (y << "h"_mst)) ||
             +  +  +  + ]
     169   [ +  +  +  +  :       72740 :                 ((x << "h"_mst) && (y << "g"_mst)) ||
                   +  + ]
     170   [ +  +  +  + ]:       70940 :                 ((x << "i"_mst) && (y << "j"_mst)) ||
     171   [ +  +  +  + ]:      505031 :                 ((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                 :      457318 :         case Fragment::OR_B: return
     173         [ +  + ]:      531220 :             "B"_mst.If(x << "Bd"_mst && y << "Wd"_mst) | // B=B_x*d_x*W_x*d_y
     174   [ +  +  +  + ]:      648131 :             ((x | y) & "o"_mst).If((x | y) << "z"_mst) | // o=o_x*z_y+z_x*o_y
     175         [ +  + ]:       58264 :             (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                 :      914636 :             (x & y & "zse"_mst) | // z=z_x*z_y, s=s_x*s_y, e=e_x*e_y
     177         [ +  + ]:      457318 :             "dux"_mst | // d, u, x
     178         [ +  + ]:      457318 :             ((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         [ +  + ]:      914636 :             (x & y & "k"_mst); // k=k_x*k_y
     180                 :      445788 :         case Fragment::OR_D: return
     181                 :      445788 :             (y & "B"_mst).If(x << "Bdu"_mst) | // B=B_y*B_x*d_x*u_x
     182   [ +  +  +  + ]:      573315 :             (x & "o"_mst).If(y << "z"_mst) | // o=o_x*z_y
     183         [ +  + ]:       60690 :             (x & y & "m"_mst).If(x << "e"_mst && (x | y) << "s"_mst) | // m=m_x*m_y*e_x*(s_x+s_y)
     184                 :      891576 :             (x & y & "zs"_mst) | // z=z_x*z_y, s=s_x*s_y
     185                 :      891576 :             (y & "ufde"_mst) | // u=u_y, f=f_y, d=d_y, e=e_y
     186         [ +  + ]:      445788 :             "x"_mst | // x
     187         [ +  + ]:      445788 :             ((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         [ +  + ]:      891576 :             (x & y & "k"_mst); // k=k_x*k_y
     189                 :      431877 :         case Fragment::OR_C: return
     190                 :      431877 :             (y & "V"_mst).If(x << "Bdu"_mst) | // V=V_y*B_x*u_x*d_x
     191   [ +  +  +  + ]:      534998 :             (x & "o"_mst).If(y << "z"_mst) | // o=o_x*z_y
     192         [ +  + ]:       45279 :             (x & y & "m"_mst).If(x << "e"_mst && (x | y) << "s"_mst) | // m=m_x*m_y*e_x*(s_x+s_y)
     193                 :      863754 :             (x & y & "zs"_mst) | // z=z_x*z_y, s=s_x*s_y
     194         [ +  + ]:      431877 :             "fx"_mst | // f, x
     195         [ +  + ]:      431877 :             ((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         [ +  + ]:      863754 :             (x & y & "k"_mst); // k=k_x*k_y
     197                 :     4815903 :         case Fragment::OR_I: return
     198                 :     4815903 :             (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                 :     4815903 :             "o"_mst.If((x & y) << "z"_mst) | // o=z_x*z_y
     200         [ +  + ]:     4815903 :             ((x | y) & "e"_mst).If((x | y) << "f"_mst) | // e=e_x*f_y+f_x*e_y
     201         [ +  + ]:     4815903 :             (x & y & "m"_mst).If((x | y) << "s"_mst) | // m=m_x*m_y*(s_x+s_y)
     202                 :     9631806 :             ((x | y) & "d"_mst) | // d=d_x+d_y
     203         [ +  + ]:     4815903 :             "x"_mst | // x
     204         [ +  + ]:     4815903 :             ((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         [ +  + ]:     9631806 :             (x & y & "k"_mst); // k=k_x*k_y
     206                 :      515301 :         case Fragment::ANDOR: return
     207         [ +  + ]:      662351 :             (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                 :     1030602 :             (x & y & z & "z"_mst) | // z=z_x*z_y*z_z
     209   [ +  +  +  + ]:      684172 :             ((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         [ +  + ]:      515301 :             (y & z & "u"_mst) | // u=u_y*u_z
     211   [ +  +  +  + ]:      515301 :             (z & "f"_mst).If((x << "s"_mst) || (y << "f"_mst)) | // f=(s_x+f_y)*f_z
     212         [ +  + ]:      515301 :             (z & "d"_mst) | // d=d_z
     213   [ +  +  +  + ]:      515301 :             (z & "e"_mst).If(x << "s"_mst || y << "f"_mst) | // e=e_z*(s_x+f_y)
     214         [ +  + ]:       97754 :             (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                 :     1030602 :             (z & (x | y) & "s"_mst) | // s=s_z*(s_x+s_y)
     216         [ +  + ]:      515301 :             "x"_mst | // x
     217         [ +  + ]:      515301 :             ((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   [ +  +  +  + ]:      515301 :             "k"_mst.If(((x & y & z) << "k"_mst) &&
     219   [ +  +  +  +  :      223657 :                 !(((x << "g"_mst) && (y << "h"_mst)) ||
             +  +  +  + ]
     220   [ +  +  +  +  :      113524 :                 ((x << "h"_mst) && (y << "g"_mst)) ||
                   +  + ]
     221   [ +  +  +  + ]:      111698 :                 ((x << "i"_mst) && (y << "j"_mst)) ||
     222   [ +  +  +  + ]:      624709 :                 ((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                 :       49678 :         case Fragment::MULTI: {
     224                 :       49678 :             return "Bnudemsk"_mst;
     225                 :             :         }
     226                 :       19281 :         case Fragment::MULTI_A: {
     227                 :       19281 :             return "Budemsk"_mst;
     228                 :             :         }
     229                 :      551057 :         case Fragment::THRESH: {
     230                 :      551057 :             bool all_e = true;
     231                 :      551057 :             bool all_m = true;
     232                 :      551057 :             uint32_t args = 0;
     233                 :      551057 :             uint32_t num_s = 0;
     234                 :      551057 :             Type acc_tl = "k"_mst;
     235   [ -  +  +  + ]:     1027935 :             for (size_t i = 0; i < sub_types.size(); ++i) {
     236         [ +  + ]:      901840 :                 Type t = sub_types[i];
     237   [ +  +  +  + ]:      901840 :                 if (!(t << (i ? "Wdu"_mst : "Bdu"_mst))) return ""_mst; // Require Bdu, Wdu, Wdu, ...
     238         [ +  + ]:      476878 :                 if (!(t << "e"_mst)) all_e = false;
     239         [ +  + ]:      476878 :                 if (!(t << "m"_mst)) all_m = false;
     240         [ +  + ]:      476878 :                 if (t << "s"_mst) num_s += 1;
     241   [ +  +  +  + ]:      476878 :                 args += (t << "z"_mst) ? 0 : (t << "o"_mst) ? 1 : 2;
     242                 :      476878 :                 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   [ +  +  +  + ]:      476878 :                     "k"_mst.If(((acc_tl & t) << "k"_mst) && ((k <= 1) ||
     247   [ +  +  +  +  :      440294 :                         ((k > 1) && !(((acc_tl << "g"_mst) && (t << "h"_mst)) ||
             +  +  +  + ]
     248   [ +  +  +  +  :      222091 :                         ((acc_tl << "h"_mst) && (t << "g"_mst)) ||
                   +  + ]
     249   [ +  +  +  + ]:      219667 :                         ((acc_tl << "i"_mst) && (t << "j"_mst)) ||
     250   [ +  +  +  + ]:      551840 :                         ((acc_tl << "j"_mst) && (t << "i"_mst))))));
     251                 :             :             }
     252                 :      252190 :             return "Bdu"_mst |
     253                 :      126095 :                    "z"_mst.If(args == 0) | // z=all z
     254   [ +  +  +  + ]:      141731 :                    "o"_mst.If(args == 1) | // o=all z except one o
     255   [ +  +  +  +  :      126095 :                    "e"_mst.If(all_e && num_s == n_subs) | // e=all e and all s
                   +  + ]
     256   [ +  +  +  + ]:      126095 :                    "m"_mst.If(all_e && all_m && num_s >= n_subs - k) | // m=all e, >=(n-k) s
     257   [ +  +  +  + ]:      216910 :                    "s"_mst.If(num_s >= n_subs - k + 1) |  // s= >=(n-k+1) s
     258                 :      126095 :                    acc_tl; // timelock info
     259                 :             :             }
     260                 :             :     }
     261                 :           0 :     assert(false);
     262                 :             : }
     263                 :             : 
     264                 :    32705307 : 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   [ +  +  +  +  :    32705307 :     switch (fragment) {
          +  +  +  +  +  
          +  +  +  +  +  
                   -  + ]
     267                 :             :         case Fragment::JUST_1:
     268                 :             :         case Fragment::JUST_0: return 1;
     269         [ +  + ]:      220067 :         case Fragment::PK_K: return IsTapscript(ms_ctx) ? 33 : 34;
     270                 :       93853 :         case Fragment::PK_H: return 3 + 21;
     271                 :      191422 :         case Fragment::OLDER:
     272         [ -  + ]:      191422 :         case Fragment::AFTER: return 1 + BuildScript(k).size();
     273                 :       38691 :         case Fragment::HASH256:
     274                 :       38691 :         case Fragment::SHA256: return 4 + 2 + 33;
     275                 :       41172 :         case Fragment::HASH160:
     276                 :       41172 :         case Fragment::RIPEMD160: return 4 + 2 + 21;
     277   [ -  +  +  - ]:      119136 :         case Fragment::MULTI: return 1 + BuildScript(n_keys).size() + BuildScript(k).size() + 34 * n_keys;
     278         [ -  + ]:       21982 :         case Fragment::MULTI_A: return (1 + 32 + 1) * n_keys + BuildScript(k).size() + 1;
     279                 :     3213411 :         case Fragment::AND_V: return subsize;
     280                 :      875325 :         case Fragment::WRAP_V: return subsize + (sub0typ << "x"_mst);
     281                 :     6585800 :         case Fragment::WRAP_S:
     282                 :     6585800 :         case Fragment::WRAP_C:
     283                 :     6585800 :         case Fragment::WRAP_N:
     284                 :     6585800 :         case Fragment::AND_B:
     285                 :     6585800 :         case Fragment::OR_B: return subsize + 1;
     286                 :     1315552 :         case Fragment::WRAP_A:
     287                 :     1315552 :         case Fragment::OR_C: return subsize + 2;
     288                 :     5561040 :         case Fragment::WRAP_D:
     289                 :     5561040 :         case Fragment::OR_D:
     290                 :     5561040 :         case Fragment::OR_I:
     291                 :     5561040 :         case Fragment::ANDOR: return subsize + 3;
     292                 :      674219 :         case Fragment::WRAP_J: return subsize + 4;
     293         [ -  + ]:      190147 :         case Fragment::THRESH: return subsize + n_subs + BuildScript(k).size();
     294                 :             :     }
     295                 :           0 :     assert(false);
     296                 :             : }
     297                 :             : 
     298                 :     4084792 : InputStack& InputStack::SetAvailable(Availability avail) {
     299                 :     4084792 :     available = avail;
     300         [ +  + ]:     4084792 :     if (avail == Availability::NO) {
     301                 :     3969266 :         stack.clear();
     302                 :     3969266 :         size = std::numeric_limits<size_t>::max();
     303                 :     3969266 :         has_sig = false;
     304                 :     3969266 :         malleable = false;
     305                 :     3969266 :         non_canon = false;
     306                 :             :     }
     307                 :     4084792 :     return *this;
     308                 :             : }
     309                 :             : 
     310                 :      188784 : InputStack& InputStack::SetWithSig() {
     311                 :      188784 :     has_sig = true;
     312                 :      188784 :     return *this;
     313                 :             : }
     314                 :             : 
     315                 :      179507 : InputStack& InputStack::SetNonCanon() {
     316                 :      179507 :     non_canon = true;
     317                 :      179507 :     return *this;
     318                 :             : }
     319                 :             : 
     320                 :       87805 : InputStack& InputStack::SetMalleable(bool x) {
     321                 :       87805 :     malleable = x;
     322                 :       87805 :     return *this;
     323                 :             : }
     324                 :             : 
     325                 :     5961076 : InputStack operator+(InputStack a, InputStack b) {
     326         [ +  - ]:     5961076 :     a.stack = Cat(std::move(a.stack), std::move(b.stack));
     327   [ +  +  +  + ]:     5961076 :     if (a.available != Availability::NO && b.available != Availability::NO) a.size += b.size;
     328                 :     5961076 :     a.has_sig |= b.has_sig;
     329                 :     5961076 :     a.malleable |= b.malleable;
     330                 :     5961076 :     a.non_canon |= b.non_canon;
     331   [ +  +  +  + ]:     5961076 :     if (a.available == Availability::NO || b.available == Availability::NO) {
     332                 :     3864332 :         a.SetAvailable(Availability::NO);
     333   [ +  -  -  + ]:     2096744 :     } else if (a.available == Availability::MAYBE || b.available == Availability::MAYBE) {
     334                 :           0 :         a.SetAvailable(Availability::MAYBE);
     335                 :             :     }
     336                 :     5961076 :     return a;
     337                 :             : }
     338                 :             : 
     339                 :     2980341 : 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         [ +  + ]:     2980341 :     if (a.available == Availability::NO) return b;
     342         [ +  + ]:     1155298 :     if (b.available == Availability::NO) return a;
     343                 :             :     // If only one of the solutions has a signature, we must pick the other one.
     344   [ +  +  +  + ]:      778304 :     if (!a.has_sig && b.has_sig) return a;
     345   [ +  +  +  + ]:      770444 :     if (!b.has_sig && a.has_sig) return b;
     346   [ +  +  +  - ]:      759794 :     if (!a.has_sig && !b.has_sig) {
     347                 :             :         // If neither solution requires a signature, the result is inevitably malleable.
     348                 :       68698 :         a.malleable = true;
     349                 :       68698 :         b.malleable = true;
     350                 :             :     } else {
     351                 :             :         // If both options require a signature, prefer the non-malleable one.
     352   [ +  +  +  + ]:      691096 :         if (b.malleable && !a.malleable) return a;
     353   [ +  +  +  + ]:      688760 :         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   [ +  -  +  - ]:      755504 :     if (a.available == Availability::YES && b.available == Availability::YES) {
     358         [ +  + ]:      790784 :         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                 :       60454 : std::optional<std::vector<Opcode>> DecomposeScript(const CScript& script)
     369                 :             : {
     370                 :       60454 :     std::vector<Opcode> out;
     371         [ +  + ]:      120908 :     CScript::const_iterator it = script.begin(), itend = script.end();
     372         [ +  + ]:    14541253 :     while (it != itend) {
     373                 :    14483590 :         std::vector<unsigned char> push_data;
     374                 :    14483590 :         opcodetype opcode;
     375   [ +  -  +  + ]:    14483590 :         if (!script.GetOp(it, opcode, push_data)) {
     376                 :        2486 :             return {};
     377         [ +  + ]:    14481104 :         } else if (opcode >= OP_1 && opcode <= OP_16) {
     378                 :             :             // Deal with OP_n (GetOp does not turn them into pushes).
     379         [ +  - ]:      658399 :             push_data.assign(1, CScript::DecodeOP_N(opcode));
     380         [ +  + ]:    13822705 :         } else if (opcode == OP_CHECKSIGVERIFY) {
     381                 :             :             // Decompose OP_CHECKSIGVERIFY into OP_CHECKSIG OP_VERIFY
     382         [ +  - ]:       73257 :             out.emplace_back(OP_CHECKSIG, std::vector<unsigned char>());
     383                 :       73257 :             opcode = OP_VERIFY;
     384         [ +  + ]:    13749448 :         } else if (opcode == OP_CHECKMULTISIGVERIFY) {
     385                 :             :             // Decompose OP_CHECKMULTISIGVERIFY into OP_CHECKMULTISIG OP_VERIFY
     386         [ +  - ]:        9893 :             out.emplace_back(OP_CHECKMULTISIG, std::vector<unsigned char>());
     387                 :        9893 :             opcode = OP_VERIFY;
     388         [ +  + ]:    13739555 :         } else if (opcode == OP_EQUALVERIFY) {
     389                 :             :             // Decompose OP_EQUALVERIFY into OP_EQUAL OP_VERIFY
     390         [ +  - ]:      187036 :             out.emplace_back(OP_EQUAL, std::vector<unsigned char>());
     391                 :      187036 :             opcode = OP_VERIFY;
     392         [ +  + ]:    13552519 :         } else if (opcode == OP_NUMEQUALVERIFY) {
     393                 :             :             // Decompose OP_NUMEQUALVERIFY into OP_NUMEQUAL OP_VERIFY
     394         [ +  - ]:        8782 :             out.emplace_back(OP_NUMEQUAL, std::vector<unsigned char>());
     395                 :        8782 :             opcode = OP_VERIFY;
     396         [ +  + ]:    13543737 :         } else if (IsPushdataOp(opcode)) {
     397   [ +  -  +  + ]:      303315 :             if (!CheckMinimalPush(push_data, opcode)) return {};
     398   [ +  +  +  +  :    13240422 :         } 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                 :          49 :             return {};
     401                 :             :         }
     402         [ +  - ]:    14480799 :         out.emplace_back(opcode, std::move(push_data));
     403                 :    14483590 :     }
     404                 :       57663 :     std::reverse(out.begin(), out.end());
     405                 :       57663 :     return out;
     406                 :       60454 : }
     407                 :             : 
     408                 :      187581 : std::optional<int64_t> ParseScriptNumber(const Opcode& in) {
     409         [ +  + ]:      187581 :     if (in.first == OP_0) {
     410                 :         215 :         return 0;
     411                 :             :     }
     412         [ +  + ]:      187366 :     if (!in.second.empty()) {
     413   [ +  +  -  + ]:      186957 :         if (IsPushdataOp(in.first) && !CheckMinimalPush(in.second, in.first)) return {};
     414                 :      186957 :         try {
     415         [ +  + ]:      186957 :             return CScriptNum(in.second, true).GetInt64();
     416         [ -  + ]:         977 :         } catch(const scriptnum_error&) {}
     417                 :             :     }
     418                 :        1386 :     return {};
     419                 :             : }
     420                 :             : 
     421                 :      610598 : int FindNextChar(std::span<const char> sp, const char m)
     422                 :             : {
     423         [ +  + ]:    16922291 :     for (int i = 0; i < (int)sp.size(); ++i) {
     424         [ +  + ]:    16922034 :         if (sp[i] == m) return i;
     425                 :             :         // We only search within the current parentheses
     426         [ +  + ]:    16337169 :         if (sp[i] == ')') break;
     427                 :             :     }
     428                 :             :     return -1;
     429                 :             : }
     430                 :             : 
     431                 :             : } // namespace internal
     432                 :             : } // namespace miniscript
        

Generated by: LCOV version 2.0-1