Branch data Line data Source code
1 : : // Copyright (c) 2019-2022 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 <string>
6 : : #include <vector>
7 : : #include <script/script.h>
8 : : #include <script/miniscript.h>
9 : : #include <serialize.h>
10 : :
11 : : #include <assert.h>
12 : :
13 : : namespace miniscript {
14 : : namespace internal {
15 : :
16 : 190050 : Type SanitizeType(Type e) {
17 : 190050 : int num_types = (e << "K"_mst) + (e << "V"_mst) + (e << "B"_mst) + (e << "W"_mst);
18 [ + + ]: 190050 : if (num_types == 0) return ""_mst; // No valid type, don't care about the rest
19 [ + - ]: 144674 : assert(num_types == 1); // K, V, B, W all conflict with each other
20 [ + + ][ + - ]: 144674 : assert(!(e << "z"_mst) || !(e << "o"_mst)); // z conflicts with o
21 [ + + ][ + - ]: 144674 : assert(!(e << "n"_mst) || !(e << "z"_mst)); // n conflicts with z
22 [ + + ][ + - ]: 144674 : assert(!(e << "n"_mst) || !(e << "W"_mst)); // n conflicts with W
23 [ + + ][ + - ]: 144674 : assert(!(e << "V"_mst) || !(e << "d"_mst)); // V conflicts with d
24 [ + + ][ + - ]: 144674 : assert(!(e << "K"_mst) || (e << "u"_mst)); // K implies u
25 [ + + ][ + - ]: 144674 : assert(!(e << "V"_mst) || !(e << "u"_mst)); // V conflicts with u
26 [ + + ][ + - ]: 144674 : assert(!(e << "e"_mst) || !(e << "f"_mst)); // e conflicts with f
27 [ + + ][ + - ]: 144674 : assert(!(e << "e"_mst) || (e << "d"_mst)); // e implies d
28 [ + + ][ + - ]: 144674 : assert(!(e << "V"_mst) || !(e << "e"_mst)); // V conflicts with e
29 [ + + ][ + - ]: 144674 : assert(!(e << "d"_mst) || !(e << "f"_mst)); // d conflicts with f
30 [ + + ][ + - ]: 144674 : assert(!(e << "V"_mst) || (e << "f"_mst)); // V implies f
31 [ + + ][ + - ]: 144674 : assert(!(e << "K"_mst) || (e << "s"_mst)); // K implies s
32 [ + + ][ - + ]: 144674 : assert(!(e << "z"_mst) || (e << "m"_mst)); // z implies m
33 : 144674 : return e;
34 : 190050 : }
35 : :
36 : 190188 : Type ComputeType(Fragment fragment, Type x, Type y, Type z, const std::vector<Type>& sub_types, uint32_t k,
37 : : size_t data_size, size_t n_subs, size_t n_keys, MiniscriptContext ms_ctx) {
38 : : // Sanity check on data
39 [ + + ][ + + ]: 190188 : if (fragment == Fragment::SHA256 || fragment == Fragment::HASH256) {
40 [ + - ]: 150 : assert(data_size == 32);
41 [ + + ][ + + ]: 190188 : } else if (fragment == Fragment::RIPEMD160 || fragment == Fragment::HASH160) {
42 [ + - ]: 2 : assert(data_size == 20);
43 : 2 : } else {
44 [ + - ]: 190036 : assert(data_size == 0);
45 : : }
46 : : // Sanity check on k
47 [ + + ][ + + ]: 190188 : if (fragment == Fragment::OLDER || fragment == Fragment::AFTER) {
48 [ + + ][ + - ]: 654 : assert(k >= 1 && k < 0x80000000UL);
49 [ + + ][ + + ]: 190188 : } else if (fragment == Fragment::MULTI || fragment == Fragment::MULTI_A) {
50 [ - + ][ + - ]: 143 : assert(k >= 1 && k <= n_keys);
51 [ + + ]: 189534 : } else if (fragment == Fragment::THRESH) {
52 [ - + ][ + - ]: 1368 : assert(k >= 1 && k <= n_subs);
53 : 1368 : } else {
54 [ + - ]: 188023 : assert(k == 0);
55 : : }
56 : : // Sanity check on subs
57 [ + + ][ + + ]: 352276 : if (fragment == Fragment::AND_V || fragment == Fragment::AND_B || fragment == Fragment::OR_B ||
[ + + ][ + + ]
58 [ + + ][ + + ]: 185457 : fragment == Fragment::OR_C || fragment == Fragment::OR_I || fragment == Fragment::OR_D) {
59 [ + - ]: 28202 : assert(n_subs == 2);
60 [ + + ]: 190188 : } else if (fragment == Fragment::ANDOR) {
61 [ + - ]: 1172 : assert(n_subs == 3);
62 [ + + ][ + + ]: 246817 : } else if (fragment == Fragment::WRAP_A || fragment == Fragment::WRAP_S || fragment == Fragment::WRAP_C ||
[ + + ][ + + ]
63 [ + + ][ + + ]: 151420 : fragment == Fragment::WRAP_D || fragment == Fragment::WRAP_V || fragment == Fragment::WRAP_J ||
[ + + ]
64 : 84831 : fragment == Fragment::WRAP_N) {
65 [ + - ]: 95820 : assert(n_subs == 1);
66 [ + + ]: 160814 : } else if (fragment != Fragment::THRESH) {
67 [ - + ]: 63626 : assert(n_subs == 0);
68 : 63626 : }
69 : : // Sanity check on keys
70 [ + + ][ + + ]: 190188 : if (fragment == Fragment::PK_K || fragment == Fragment::PK_H) {
71 [ + - ]: 2391 : assert(n_keys == 1);
72 [ + + ]: 190188 : } else if (fragment == Fragment::MULTI) {
73 [ - + ][ + - ]: 137 : assert(n_keys >= 1 && n_keys <= MAX_PUBKEYS_PER_MULTISIG);
74 [ + - ]: 137 : assert(!IsTapscript(ms_ctx));
75 [ + + ]: 187797 : } else if (fragment == Fragment::MULTI_A) {
76 [ - + ][ + - ]: 6 : assert(n_keys >= 1 && n_keys <= MAX_PUBKEYS_PER_MULTI_A);
77 [ + - ]: 6 : assert(IsTapscript(ms_ctx));
78 : 6 : } else {
79 [ + - ]: 187654 : assert(n_keys == 0);
80 : : }
81 : :
82 : : // Below is the per-fragment logic for computing the expression types.
83 : : // It heavily relies on Type's << operator (where "X << a_mst" means
84 : : // "X has all properties listed in a").
85 [ + + + + : 190188 : switch (fragment) {
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + - ]
86 : 2099 : case Fragment::PK_K: return "Konudemsxk"_mst;
87 : 154 : case Fragment::PK_H: return "Knudemsxk"_mst;
88 : 400 : case Fragment::OLDER: return
89 : 800 : "g"_mst.If(k & CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) |
90 : 800 : "h"_mst.If(!(k & CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG)) |
91 : 400 : "Bzfmxk"_mst;
92 : 116 : case Fragment::AFTER: return
93 : 232 : "i"_mst.If(k >= LOCKTIME_THRESHOLD) |
94 : 232 : "j"_mst.If(k < LOCKTIME_THRESHOLD) |
95 : 116 : "Bzfmxk"_mst;
96 : 1 : case Fragment::SHA256: return "Bonudmk"_mst;
97 : 1 : case Fragment::RIPEMD160: return "Bonudmk"_mst;
98 : 11 : case Fragment::HASH256: return "Bonudmk"_mst;
99 : 1 : case Fragment::HASH160: return "Bonudmk"_mst;
100 : 20296 : case Fragment::JUST_1: return "Bzufmxk"_mst;
101 : 40404 : case Fragment::JUST_0: return "Bzudemsxk"_mst;
102 : 3923 : case Fragment::WRAP_A: return
103 : 7846 : "W"_mst.If(x << "B"_mst) | // W=B_x
104 : 7846 : (x & "ghijk"_mst) | // g=g_x, h=h_x, i=i_x, j=j_x, k=k_x
105 : 7846 : (x & "udfems"_mst) | // u=u_x, d=d_x, f=f_x, e=e_x, m=m_x, s=s_x
106 : 3923 : "x"_mst; // x
107 : 1789 : case Fragment::WRAP_S: return
108 : 3578 : "W"_mst.If(x << "Bo"_mst) | // W=B_x*o_x
109 : 3578 : (x & "ghijk"_mst) | // g=g_x, h=h_x, i=i_x, j=j_x, k=k_x
110 : 1789 : (x & "udfemsx"_mst); // u=u_x, d=d_x, f=f_x, e=e_x, m=m_x, s=s_x, x=x_x
111 : 3682 : case Fragment::WRAP_C: return
112 : 7364 : "B"_mst.If(x << "K"_mst) | // B=K_x
113 : 7364 : (x & "ghijk"_mst) | // g=g_x, h=h_x, i=i_x, j=j_x, k=k_x
114 : 7364 : (x & "ondfem"_mst) | // o=o_x, n=n_x, d=d_x, f=f_x, e=e_x, m=m_x
115 : 3682 : "us"_mst; // u, s
116 : 7323 : case Fragment::WRAP_D: return
117 : 14646 : "B"_mst.If(x << "Vz"_mst) | // B=V_x*z_x
118 : 14646 : "o"_mst.If(x << "z"_mst) | // o=z_x
119 : 14646 : "e"_mst.If(x << "f"_mst) | // e=f_x
120 : 14646 : (x & "ghijk"_mst) | // g=g_x, h=h_x, i=i_x, j=j_x, k=k_x
121 : 14646 : (x & "ms"_mst) | // m=m_x, s=s_x
122 : : // NOTE: 'd:' is 'u' under Tapscript but not P2WSH as MINIMALIF is only a policy rule there.
123 : 14646 : "u"_mst.If(IsTapscript(ms_ctx)) |
124 : 7323 : "ndx"_mst; // n, d, x
125 : 3492 : case Fragment::WRAP_V: return
126 : 6984 : "V"_mst.If(x << "B"_mst) | // V=B_x
127 : 6984 : (x & "ghijk"_mst) | // g=g_x, h=h_x, i=i_x, j=j_x, k=k_x
128 : 6984 : (x & "zonms"_mst) | // z=z_x, o=o_x, n=n_x, m=m_x, s=s_x
129 : 3492 : "fx"_mst; // f, x
130 : 55774 : case Fragment::WRAP_J: return
131 : 111548 : "B"_mst.If(x << "Bn"_mst) | // B=B_x*n_x
132 : 111548 : "e"_mst.If(x << "f"_mst) | // e=f_x
133 : 111548 : (x & "ghijk"_mst) | // g=g_x, h=h_x, i=i_x, j=j_x, k=k_x
134 : 111548 : (x & "oums"_mst) | // o=o_x, u=u_x, m=m_x, s=s_x
135 : 55774 : "ndx"_mst; // n, d, x
136 : 19837 : case Fragment::WRAP_N: return
137 : 39674 : (x & "ghijk"_mst) | // g=g_x, h=h_x, i=i_x, j=j_x, k=k_x
138 : 39674 : (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
139 : 19837 : "ux"_mst; // u, x
140 : 3344 : case Fragment::AND_V: return
141 : 6964 : (y & "KVB"_mst).If(x << "V"_mst) | // B=V_x*B_y, V=V_x*V_y, K=V_x*K_y
142 : 10446 : (x & "n"_mst) | (y & "n"_mst).If(x << "z"_mst) | // n=n_x+z_x*n_y
143 : 6964 : ((x | y) & "o"_mst).If((x | y) << "z"_mst) | // o=o_x*z_y+z_x*o_y
144 : 6964 : (x & y & "dmz"_mst) | // d=d_x*d_y, m=m_x*m_y, z=z_x*z_y
145 : 3544 : ((x | y) & "s"_mst) | // s=s_x+s_y
146 [ + + ]: 3482 : "f"_mst.If((y << "f"_mst) || (x << "s"_mst)) | // f=f_y+s_x
147 : 6964 : (y & "ux"_mst) | // u=u_y, x=x_y
148 : 5583 : ((x | y) & "ghij"_mst) | // g=g_x+g_y, h=h_x+h_y, i=i_x+i_y, j=j_x+j_y
149 [ + + ]: 5583 : "k"_mst.If(((x & y) << "k"_mst) &&
150 [ + + ][ + + ]: 4476 : !(((x << "g"_mst) && (y << "h"_mst)) ||
151 [ + + ]: 2237 : ((x << "h"_mst) && (y << "g"_mst)) ||
152 [ + + ]: 4269 : ((x << "i"_mst) && (y << "j"_mst)) ||
153 [ + + ]: 2101 : ((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)
154 : 673 : case Fragment::AND_B: return
155 : 1346 : (x & "B"_mst).If(y << "W"_mst) | // B=B_x*W_y
156 : 1346 : ((x | y) & "o"_mst).If((x | y) << "z"_mst) | // o=o_x*z_y+z_x*o_y
157 : 2019 : (x & "n"_mst) | (y & "n"_mst).If(x << "z"_mst) | // n=n_x+z_x*n_y
158 : 1346 : (x & y & "e"_mst).If((x & y) << "s"_mst) | // e=e_x*e_y*s_x*s_y
159 : 1310 : (x & y & "dzm"_mst) | // d=d_x*d_y, z=z_x*z_y, m=m_x*m_y
160 [ + + ][ + + ]: 673 : "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
161 : 1346 : ((x | y) & "s"_mst) | // s=s_x+s_y
162 : 1346 : "ux"_mst | // u, x
163 : 720 : ((x | y) & "ghij"_mst) | // g=g_x+g_y, h=h_x+h_y, i=i_x+i_y, j=j_x+j_y
164 [ + + ]: 720 : "k"_mst.If(((x & y) << "k"_mst) &&
165 [ - + ][ # # ]: 94 : !(((x << "g"_mst) && (y << "h"_mst)) ||
166 [ - + ]: 47 : ((x << "h"_mst) && (y << "g"_mst)) ||
167 [ - + ]: 94 : ((x << "i"_mst) && (y << "j"_mst)) ||
168 [ + - ]: 47 : ((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)
169 : 576 : case Fragment::OR_B: return
170 [ + + ]: 576 : "B"_mst.If(x << "Bd"_mst && y << "Wd"_mst) | // B=B_x*d_x*W_x*d_y
171 : 813 : ((x | y) & "o"_mst).If((x | y) << "z"_mst) | // o=o_x*z_y+z_x*o_y
172 [ + + ]: 576 : (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)
173 : 1152 : (x & y & "zse"_mst) | // z=z_x*z_y, s=s_x*s_y, e=e_x*e_y
174 : 1152 : "dux"_mst | // d, u, x
175 : 1152 : ((x | y) & "ghij"_mst) | // g=g_x+g_y, h=h_x+h_y, i=i_x+i_y, j=j_x+j_y
176 : 576 : (x & y & "k"_mst); // k=k_x*k_y
177 : 102 : case Fragment::OR_D: return
178 : 204 : (y & "B"_mst).If(x << "Bdu"_mst) | // B=B_y*B_x*d_x*u_x
179 : 179 : (x & "o"_mst).If(y << "z"_mst) | // o=o_x*z_y
180 [ + + ]: 102 : (x & y & "m"_mst).If(x << "e"_mst && (x | y) << "s"_mst) | // m=m_x*m_y*e_x*(s_x+s_y)
181 : 204 : (x & y & "zs"_mst) | // z=z_x*z_y, s=s_x*s_y
182 : 204 : (y & "ufde"_mst) | // u=u_y, f=f_y, d=d_y, e=e_y
183 : 204 : "x"_mst | // x
184 : 204 : ((x | y) & "ghij"_mst) | // g=g_x+g_y, h=h_x+h_y, i=i_x+i_y, j=j_x+j_y
185 : 102 : (x & y & "k"_mst); // k=k_x*k_y
186 : 221 : case Fragment::OR_C: return
187 : 442 : (y & "V"_mst).If(x << "Bdu"_mst) | // V=V_y*B_x*u_x*d_x
188 : 399 : (x & "o"_mst).If(y << "z"_mst) | // o=o_x*z_y
189 [ + + ]: 221 : (x & y & "m"_mst).If(x << "e"_mst && (x | y) << "s"_mst) | // m=m_x*m_y*e_x*(s_x+s_y)
190 : 442 : (x & y & "zs"_mst) | // z=z_x*z_y, s=s_x*s_y
191 : 442 : "fx"_mst | // f, x
192 : 442 : ((x | y) & "ghij"_mst) | // g=g_x+g_y, h=h_x+h_y, i=i_x+i_y, j=j_x+j_y
193 : 221 : (x & y & "k"_mst); // k=k_x*k_y
194 : 23148 : case Fragment::OR_I: return
195 : 46296 : (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
196 : 46296 : "o"_mst.If((x & y) << "z"_mst) | // o=z_x*z_y
197 : 46296 : ((x | y) & "e"_mst).If((x | y) << "f"_mst) | // e=e_x*f_y+f_x*e_y
198 : 46296 : (x & y & "m"_mst).If((x | y) << "s"_mst) | // m=m_x*m_y*(s_x+s_y)
199 : 46296 : ((x | y) & "d"_mst) | // d=d_x+d_y
200 : 46296 : "x"_mst | // x
201 : 46296 : ((x | y) & "ghij"_mst) | // g=g_x+g_y, h=h_x+h_y, i=i_x+i_y, j=j_x+j_y
202 : 23148 : (x & y & "k"_mst); // k=k_x*k_y
203 : 1172 : case Fragment::ANDOR: return
204 : 2344 : (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
205 : 2344 : (x & y & z & "z"_mst) | // z=z_x*z_y*z_z
206 : 2344 : ((x | (y & z)) & "o"_mst).If((x | (y & z)) << "z"_mst) | // o=o_x*z_y*z_z+z_x*o_y*o_z
207 : 2229 : (y & z & "u"_mst) | // u=u_y*u_z
208 [ + + ]: 1172 : (z & "f"_mst).If((x << "s"_mst) || (y << "f"_mst)) | // f=(s_x+f_y)*f_z
209 : 2229 : (z & "d"_mst) | // d=d_z
210 [ + + ]: 1976 : (z & "e"_mst).If(x << "s"_mst || y << "f"_mst) | // e=e_z*(s_x+f_y)
211 [ + + ]: 1172 : (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)
212 : 2344 : (z & (x | y) & "s"_mst) | // s=s_z*(s_x+s_y)
213 : 2344 : "x"_mst | // x
214 : 1784 : ((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
215 [ + + ]: 1784 : "k"_mst.If(((x & y & z) << "k"_mst) &&
216 [ - + ][ + + ]: 1242 : !(((x << "g"_mst) && (y << "h"_mst)) ||
217 [ - + ]: 630 : ((x << "h"_mst) && (y << "g"_mst)) ||
218 [ + + ]: 1242 : ((x << "i"_mst) && (y << "j"_mst)) ||
219 [ + + ]: 612 : ((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)
220 : : case Fragment::MULTI: {
221 : 137 : return "Bnudemsk"_mst;
222 : : }
223 : : case Fragment::MULTI_A: {
224 : 6 : return "Budemsk"_mst;
225 : : }
226 : : case Fragment::THRESH: {
227 : 1368 : bool all_e = true;
228 : 1368 : bool all_m = true;
229 : 1368 : uint32_t args = 0;
230 : 1368 : uint32_t num_s = 0;
231 : 1368 : Type acc_tl = "k"_mst;
232 [ + + ]: 2323 : for (size_t i = 0; i < sub_types.size(); ++i) {
233 : 1590 : Type t = sub_types[i];
234 [ + + ][ + + ]: 1590 : if (!(t << (i ? "Wdu"_mst : "Bdu"_mst))) return ""_mst; // Require Bdu, Wdu, Wdu, ...
235 [ + + ]: 955 : if (!(t << "e"_mst)) all_e = false;
236 [ + + ]: 955 : if (!(t << "m"_mst)) all_m = false;
237 [ + + ]: 955 : if (t << "s"_mst) num_s += 1;
238 [ + + ]: 955 : args += (t << "z"_mst) ? 0 : (t << "o"_mst) ? 1 : 2;
239 : 1910 : acc_tl = ((acc_tl | t) & "ghij"_mst) |
240 : : // Thresh contains a combination of timelocks if it has threshold > 1 and
241 : : // it contains two different children that have different types of timelocks
242 : : // Note how if any of the children don't have "k", the parent also does not have "k"
243 [ - + ][ + + ]: 2016 : "k"_mst.If(((acc_tl & t) << "k"_mst) && ((k <= 1) ||
244 [ - + ][ - + ]: 212 : ((k > 1) && !(((acc_tl << "g"_mst) && (t << "h"_mst)) ||
[ # # ]
245 [ - + ]: 106 : ((acc_tl << "h"_mst) && (t << "g"_mst)) ||
246 [ - + ]: 212 : ((acc_tl << "i"_mst) && (t << "j"_mst)) ||
247 [ + - ]: 106 : ((acc_tl << "j"_mst) && (t << "i"_mst))))));
248 : 955 : }
249 : 2199 : return "Bdu"_mst |
250 : 1466 : "z"_mst.If(args == 0) | // z=all z
251 : 1320 : "o"_mst.If(args == 1) | // o=all z except one o
252 [ + + ]: 1320 : "e"_mst.If(all_e && num_s == n_subs) | // e=all e and all s
253 [ + + ][ + + ]: 733 : "m"_mst.If(all_e && all_m && num_s >= n_subs - k) | // m=all e, >=(n-k) s
254 : 1466 : "s"_mst.If(num_s >= n_subs - k + 1) | // s= >=(n-k+1) s
255 : 733 : acc_tl; // timelock info
256 : : }
257 : : }
258 : 0 : assert(false);
259 : 190050 : }
260 : :
261 : 190050 : size_t ComputeScriptLen(Fragment fragment, Type sub0typ, size_t subsize, uint32_t k, size_t n_subs,
262 : : size_t n_keys, MiniscriptContext ms_ctx) {
263 [ + + + + : 190050 : switch (fragment) {
+ + + + +
+ + + + +
+ - ]
264 : : case Fragment::JUST_1:
265 : 60700 : case Fragment::JUST_0: return 1;
266 : 2099 : case Fragment::PK_K: return IsTapscript(ms_ctx) ? 33 : 34;
267 : 154 : case Fragment::PK_H: return 3 + 21;
268 : : case Fragment::OLDER:
269 [ + - ]: 516 : case Fragment::AFTER: return 1 + BuildScript(k).size();
270 : : case Fragment::HASH256:
271 : 12 : case Fragment::SHA256: return 4 + 2 + 33;
272 : : case Fragment::HASH160:
273 : 2 : case Fragment::RIPEMD160: return 4 + 2 + 21;
274 [ + - ][ + - ]: 137 : case Fragment::MULTI: return 1 + BuildScript(n_keys).size() + BuildScript(k).size() + 34 * n_keys;
[ - + ]
275 [ + - ]: 6 : case Fragment::MULTI_A: return (1 + 32 + 1) * n_keys + BuildScript(k).size() + 1;
276 : 3344 : case Fragment::AND_V: return subsize;
277 : 3492 : case Fragment::WRAP_V: return subsize + (sub0typ << "x"_mst);
278 : : case Fragment::WRAP_S:
279 : : case Fragment::WRAP_C:
280 : : case Fragment::WRAP_N:
281 : : case Fragment::AND_B:
282 : 26557 : case Fragment::OR_B: return subsize + 1;
283 : : case Fragment::WRAP_A:
284 : 4144 : case Fragment::OR_C: return subsize + 2;
285 : : case Fragment::WRAP_D:
286 : : case Fragment::OR_D:
287 : : case Fragment::OR_I:
288 : 31745 : case Fragment::ANDOR: return subsize + 3;
289 : 55774 : case Fragment::WRAP_J: return subsize + 4;
290 [ + - ]: 1368 : case Fragment::THRESH: return subsize + n_subs + BuildScript(k).size();
291 : : }
292 : 0 : assert(false);
293 : 190050 : }
294 : :
295 : 8 : InputStack& InputStack::SetAvailable(Availability avail) {
296 : 8 : available = avail;
297 [ + - ]: 8 : if (avail == Availability::NO) {
298 : 8 : stack.clear();
299 : 8 : size = std::numeric_limits<size_t>::max();
300 : 8 : has_sig = false;
301 : 8 : malleable = false;
302 : 8 : non_canon = false;
303 : 8 : }
304 : 8 : return *this;
305 : : }
306 : :
307 : 0 : InputStack& InputStack::SetWithSig() {
308 : 0 : has_sig = true;
309 : 0 : return *this;
310 : : }
311 : :
312 : 0 : InputStack& InputStack::SetNonCanon() {
313 : 0 : non_canon = true;
314 : 0 : return *this;
315 : : }
316 : :
317 : 8 : InputStack& InputStack::SetMalleable(bool x) {
318 : 8 : malleable = x;
319 : 8 : return *this;
320 : : }
321 : :
322 : 0 : InputStack operator+(InputStack a, InputStack b) {
323 [ # # ]: 0 : a.stack = Cat(std::move(a.stack), std::move(b.stack));
324 [ # # ][ # # ]: 0 : if (a.available != Availability::NO && b.available != Availability::NO) a.size += b.size;
325 : 0 : a.has_sig |= b.has_sig;
326 [ + - ]: 2 : a.malleable |= b.malleable;
327 : 0 : a.non_canon |= b.non_canon;
328 [ + - ][ - + ]: 2 : if (a.available == Availability::NO || b.available == Availability::NO) {
[ + - ][ + - ]
[ # # ][ # # ]
329 : 0 : a.SetAvailable(Availability::NO);
330 [ + - ][ # # ]: 2 : } else if (a.available == Availability::MAYBE || b.available == Availability::MAYBE) {
[ # # ]
331 : 0 : a.SetAvailable(Availability::MAYBE);
332 : 2 : }
333 : 0 : return a;
334 [ + - ][ + - ]: 2 : }
335 : :
336 : 0 : InputStack operator|(InputStack a, InputStack b) {
337 : : // If only one is invalid, pick the other one. If both are invalid, pick an arbitrary one.
338 [ # # ]: 0 : if (a.available == Availability::NO) return b;
339 [ # # ]: 0 : if (b.available == Availability::NO) return a;
340 : : // If only one of the solutions has a signature, we must pick the other one.
341 [ # # ][ # # ]: 0 : if (!a.has_sig && b.has_sig) return a;
342 [ # # ][ # # ]: 0 : if (!b.has_sig && a.has_sig) return b;
343 [ # # ][ # # ]: 0 : if (!a.has_sig && !b.has_sig) {
344 : : // If neither solution requires a signature, the result is inevitably malleable.
345 : 0 : a.malleable = true;
346 : 0 : b.malleable = true;
347 : 0 : } else {
348 : : // If both options require a signature, prefer the non-malleable one.
349 [ # # ][ # # ]: 0 : if (b.malleable && !a.malleable) return a;
350 [ # # ][ # # ]: 0 : if (a.malleable && !b.malleable) return b;
351 : : }
352 : : // Between two malleable or two non-malleable solutions, pick the smaller one between
353 : : // YESes, and the bigger ones between MAYBEs. Prefer YES over MAYBE.
354 [ # # ][ # # ]: 0 : if (a.available == Availability::YES && b.available == Availability::YES) {
355 [ # # ]: 0 : return std::move(a.size <= b.size ? a : b);
356 [ # # ][ # # ]: 0 : } else if (a.available == Availability::MAYBE && b.available == Availability::MAYBE) {
357 [ # # ]: 0 : return std::move(a.size >= b.size ? a : b);
358 [ # # ]: 0 : } else if (a.available == Availability::YES) {
359 : 0 : return a;
360 : : } else {
361 : 0 : return b;
362 : : }
363 : 0 : }
364 : :
365 : 118 : std::optional<std::vector<Opcode>> DecomposeScript(const CScript& script)
366 : : {
367 : 118 : std::vector<Opcode> out;
368 [ + - ][ + - ]: 118 : CScript::const_iterator it = script.begin(), itend = script.end();
369 [ + - ][ + + ]: 59199 : while (it != itend) {
370 : 59081 : std::vector<unsigned char> push_data;
371 : : opcodetype opcode;
372 [ + - ][ + - ]: 59081 : if (!script.GetOp(it, opcode, push_data)) {
373 : 0 : return {};
374 [ + + ][ + + ]: 59081 : } else if (opcode >= OP_1 && opcode <= OP_16) {
375 : : // Deal with OP_n (GetOp does not turn them into pushes).
376 [ + - ][ + - ]: 831 : push_data.assign(1, CScript::DecodeOP_N(opcode));
377 [ + + ]: 59081 : } else if (opcode == OP_CHECKSIGVERIFY) {
378 : : // Decompose OP_CHECKSIGVERIFY into OP_CHECKSIG OP_VERIFY
379 [ + - ]: 3 : out.emplace_back(OP_CHECKSIG, std::vector<unsigned char>());
380 : 3 : opcode = OP_VERIFY;
381 [ + + ]: 58250 : } else if (opcode == OP_CHECKMULTISIGVERIFY) {
382 : : // Decompose OP_CHECKMULTISIGVERIFY into OP_CHECKMULTISIG OP_VERIFY
383 [ + - ]: 2 : out.emplace_back(OP_CHECKMULTISIG, std::vector<unsigned char>());
384 : 2 : opcode = OP_VERIFY;
385 [ + + ]: 58247 : } else if (opcode == OP_EQUALVERIFY) {
386 : : // Decompose OP_EQUALVERIFY into OP_EQUAL OP_VERIFY
387 [ + - ]: 32 : out.emplace_back(OP_EQUAL, std::vector<unsigned char>());
388 : 32 : opcode = OP_VERIFY;
389 [ + - ]: 58245 : } else if (opcode == OP_NUMEQUALVERIFY) {
390 : : // Decompose OP_NUMEQUALVERIFY into OP_NUMEQUAL OP_VERIFY
391 [ # # ]: 0 : out.emplace_back(OP_NUMEQUAL, std::vector<unsigned char>());
392 : 0 : opcode = OP_VERIFY;
393 [ + - ][ + + ]: 58213 : } else if (IsPushdataOp(opcode)) {
394 [ + - ][ + - ]: 420 : if (!CheckMinimalPush(push_data, opcode)) return {};
395 [ + - ][ + + ]: 58213 : } else if (it != itend && (opcode == OP_CHECKSIG || opcode == OP_CHECKMULTISIG || opcode == OP_EQUAL || opcode == OP_NUMEQUAL) && (*it == OP_VERIFY)) {
[ + + ][ + + ]
[ + + ][ - + ]
[ + - ][ + - ]
396 : : // Rule out non minimal VERIFY sequences
397 : 0 : return {};
398 : : }
399 [ + - ]: 59081 : out.emplace_back(opcode, std::move(push_data));
400 [ - + ]: 59081 : }
401 [ + - ]: 118 : std::reverse(out.begin(), out.end());
402 : 118 : return out;
403 : 118 : }
404 : :
405 : 85 : std::optional<int64_t> ParseScriptNumber(const Opcode& in) {
406 [ + - ]: 85 : if (in.first == OP_0) {
407 : 0 : return 0;
408 : : }
409 [ - + ]: 85 : if (!in.second.empty()) {
410 [ + + ][ + - ]: 85 : if (IsPushdataOp(in.first) && !CheckMinimalPush(in.second, in.first)) return {};
411 : : try {
412 [ + - ][ + - ]: 85 : return CScriptNum(in.second, true).GetInt64();
413 [ # # ]: 0 : } catch(const scriptnum_error&) {}
414 : 0 : }
415 : 0 : return {};
416 : 85 : }
417 : :
418 : 9291 : int FindNextChar(Span<const char> sp, const char m)
419 : : {
420 [ + + ]: 345677 : for (int i = 0; i < (int)sp.size(); ++i) {
421 [ + + ]: 345668 : if (sp[i] == m) return i;
422 : : // We only search within the current parentheses
423 [ + + ]: 336494 : if (sp[i] == ')') break;
424 : 336386 : }
425 : 117 : return -1;
426 : 9291 : }
427 : :
428 : : } // namespace internal
429 : : } // namespace miniscript
|