Coverage Report

Created: 2026-05-30 09:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/script/script.cpp
Line
Count
Source
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
// Copyright (c) 2009-present The Bitcoin Core developers
3
// Distributed under the MIT software license, see the accompanying
4
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6
#include <script/script.h>
7
8
#include <crypto/common.h>
9
#include <crypto/hex_base.h>
10
#include <hash.h>
11
#include <uint256.h>
12
#include <util/hash_type.h>
13
14
#include <compare>
15
#include <string>
16
17
157k
CScriptID::CScriptID(const CScript& in) : BaseHash(Hash160(in)) {}
18
19
std::string GetOpName(opcodetype opcode)
20
21.5M
{
21
21.5M
    switch (opcode)
22
21.5M
    {
23
    // push value
24
0
    case OP_0                      : return "0";
25
0
    case OP_PUSHDATA1              : return "OP_PUSHDATA1";
26
0
    case OP_PUSHDATA2              : return "OP_PUSHDATA2";
27
0
    case OP_PUSHDATA4              : return "OP_PUSHDATA4";
28
0
    case OP_1NEGATE                : return "-1";
29
13
    case OP_RESERVED               : return "OP_RESERVED";
30
21.4M
    case OP_1                      : return "1";
31
2.05k
    case OP_2                      : return "2";
32
3.03k
    case OP_3                      : return "3";
33
4.00k
    case OP_4                      : return "4";
34
5.00k
    case OP_5                      : return "5";
35
6.00k
    case OP_6                      : return "6";
36
7.00k
    case OP_7                      : return "7";
37
8.00k
    case OP_8                      : return "8";
38
9.00k
    case OP_9                      : return "9";
39
10.0k
    case OP_10                     : return "10";
40
1
    case OP_11                     : return "11";
41
0
    case OP_12                     : return "12";
42
0
    case OP_13                     : return "13";
43
0
    case OP_14                     : return "14";
44
1
    case OP_15                     : return "15";
45
9
    case OP_16                     : return "16";
46
47
    // control
48
12
    case OP_NOP                    : return "OP_NOP";
49
12
    case OP_VER                    : return "OP_VER";
50
17
    case OP_IF                     : return "OP_IF";
51
19
    case OP_NOTIF                  : return "OP_NOTIF";
52
12
    case OP_VERIF                  : return "OP_VERIF";
53
12
    case OP_VERNOTIF               : return "OP_VERNOTIF";
54
20
    case OP_ELSE                   : return "OP_ELSE";
55
24
    case OP_ENDIF                  : return "OP_ENDIF";
56
19
    case OP_VERIFY                 : return "OP_VERIFY";
57
1.89k
    case OP_RETURN                 : return "OP_RETURN";
58
59
    // stack ops
60
17
    case OP_TOALTSTACK             : return "OP_TOALTSTACK";
61
17
    case OP_FROMALTSTACK           : return "OP_FROMALTSTACK";
62
12
    case OP_2DROP                  : return "OP_2DROP";
63
12
    case OP_2DUP                   : return "OP_2DUP";
64
12
    case OP_3DUP                   : return "OP_3DUP";
65
12
    case OP_2OVER                  : return "OP_2OVER";
66
12
    case OP_2ROT                   : return "OP_2ROT";
67
12
    case OP_2SWAP                  : return "OP_2SWAP";
68
14
    case OP_IFDUP                  : return "OP_IFDUP";
69
12
    case OP_DEPTH                  : return "OP_DEPTH";
70
29
    case OP_DROP                   : return "OP_DROP";
71
436
    case OP_DUP                    : return "OP_DUP";
72
12
    case OP_NIP                    : return "OP_NIP";
73
12
    case OP_OVER                   : return "OP_OVER";
74
12
    case OP_PICK                   : return "OP_PICK";
75
12
    case OP_ROLL                   : return "OP_ROLL";
76
12
    case OP_ROT                    : return "OP_ROT";
77
13
    case OP_SWAP                   : return "OP_SWAP";
78
12
    case OP_TUCK                   : return "OP_TUCK";
79
80
    // splice ops
81
12
    case OP_CAT                    : return "OP_CAT";
82
12
    case OP_SUBSTR                 : return "OP_SUBSTR";
83
12
    case OP_LEFT                   : return "OP_LEFT";
84
12
    case OP_RIGHT                  : return "OP_RIGHT";
85
16
    case OP_SIZE                   : return "OP_SIZE";
86
87
    // bit logic
88
12
    case OP_INVERT                 : return "OP_INVERT";
89
12
    case OP_AND                    : return "OP_AND";
90
12
    case OP_OR                     : return "OP_OR";
91
12
    case OP_XOR                    : return "OP_XOR";
92
453
    case OP_EQUAL                  : return "OP_EQUAL";
93
439
    case OP_EQUALVERIFY            : return "OP_EQUALVERIFY";
94
12
    case OP_RESERVED1              : return "OP_RESERVED1";
95
12
    case OP_RESERVED2              : return "OP_RESERVED2";
96
97
    // numeric
98
12
    case OP_1ADD                   : return "OP_1ADD";
99
12
    case OP_1SUB                   : return "OP_1SUB";
100
12
    case OP_2MUL                   : return "OP_2MUL";
101
12
    case OP_2DIV                   : return "OP_2DIV";
102
12
    case OP_NEGATE                 : return "OP_NEGATE";
103
12
    case OP_ABS                    : return "OP_ABS";
104
36
    case OP_NOT                    : return "OP_NOT";
105
14
    case OP_0NOTEQUAL              : return "OP_0NOTEQUAL";
106
14
    case OP_ADD                    : return "OP_ADD";
107
12
    case OP_SUB                    : return "OP_SUB";
108
12
    case OP_MUL                    : return "OP_MUL";
109
12
    case OP_DIV                    : return "OP_DIV";
110
12
    case OP_MOD                    : return "OP_MOD";
111
12
    case OP_LSHIFT                 : return "OP_LSHIFT";
112
12
    case OP_RSHIFT                 : return "OP_RSHIFT";
113
16
    case OP_BOOLAND                : return "OP_BOOLAND";
114
12
    case OP_BOOLOR                 : return "OP_BOOLOR";
115
12
    case OP_NUMEQUAL               : return "OP_NUMEQUAL";
116
12
    case OP_NUMEQUALVERIFY         : return "OP_NUMEQUALVERIFY";
117
12
    case OP_NUMNOTEQUAL            : return "OP_NUMNOTEQUAL";
118
12
    case OP_LESSTHAN               : return "OP_LESSTHAN";
119
12
    case OP_GREATERTHAN            : return "OP_GREATERTHAN";
120
12
    case OP_LESSTHANOREQUAL        : return "OP_LESSTHANOREQUAL";
121
12
    case OP_GREATERTHANOREQUAL     : return "OP_GREATERTHANOREQUAL";
122
12
    case OP_MIN                    : return "OP_MIN";
123
12
    case OP_MAX                    : return "OP_MAX";
124
12
    case OP_WITHIN                 : return "OP_WITHIN";
125
126
    // crypto
127
12
    case OP_RIPEMD160              : return "OP_RIPEMD160";
128
12
    case OP_SHA1                   : return "OP_SHA1";
129
14
    case OP_SHA256                 : return "OP_SHA256";
130
873
    case OP_HASH160                : return "OP_HASH160";
131
12
    case OP_HASH256                : return "OP_HASH256";
132
12
    case OP_CODESEPARATOR          : return "OP_CODESEPARATOR";
133
750
    case OP_CHECKSIG               : return "OP_CHECKSIG";
134
23
    case OP_CHECKSIGVERIFY         : return "OP_CHECKSIGVERIFY";
135
98
    case OP_CHECKMULTISIG          : return "OP_CHECKMULTISIG";
136
12
    case OP_CHECKMULTISIGVERIFY    : return "OP_CHECKMULTISIGVERIFY";
137
138
    // expansion
139
12
    case OP_NOP1                   : return "OP_NOP1";
140
19
    case OP_CHECKLOCKTIMEVERIFY    : return "OP_CHECKLOCKTIMEVERIFY";
141
23
    case OP_CHECKSEQUENCEVERIFY    : return "OP_CHECKSEQUENCEVERIFY";
142
12
    case OP_NOP4                   : return "OP_NOP4";
143
12
    case OP_NOP5                   : return "OP_NOP5";
144
12
    case OP_NOP6                   : return "OP_NOP6";
145
12
    case OP_NOP7                   : return "OP_NOP7";
146
16
    case OP_NOP8                   : return "OP_NOP8";
147
12
    case OP_NOP9                   : return "OP_NOP9";
148
12
    case OP_NOP10                  : return "OP_NOP10";
149
150
    // Opcode added by BIP 342 (Tapscript)
151
1
    case OP_CHECKSIGADD            : return "OP_CHECKSIGADD";
152
153
0
    case OP_INVALIDOPCODE          : return "OP_INVALIDOPCODE";
154
155
21.5M
    } // no default case, so the compiler can warn about missing cases
156
1
    return "OP_UNKNOWN";
157
21.5M
}
158
159
unsigned int CScript::GetSigOpCount(bool fAccurate) const
160
2.11M
{
161
2.11M
    unsigned int n = 0;
162
2.11M
    const_iterator pc = begin();
163
2.11M
    opcodetype lastOpcode = OP_INVALIDOPCODE;
164
451M
    while (pc < end())
165
449M
    {
166
449M
        opcodetype opcode;
167
449M
        if (!GetOp(pc, opcode))
168
145
            break;
169
449M
        if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
170
156M
            n++;
171
292M
        else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
172
26.2k
        {
173
26.2k
            if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
174
371
                n += DecodeOP_N(lastOpcode);
175
25.8k
            else
176
25.8k
                n += MAX_PUBKEYS_PER_MULTISIG;
177
26.2k
        }
178
449M
        lastOpcode = opcode;
179
449M
    }
180
2.11M
    return n;
181
2.11M
}
182
183
unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
184
64.0k
{
185
64.0k
    if (!IsPayToScriptHash())
186
50.1k
        return GetSigOpCount(true);
187
188
    // This is a pay-to-script-hash scriptPubKey;
189
    // get the last item that the scriptSig
190
    // pushes onto the stack:
191
13.8k
    const_iterator pc = scriptSig.begin();
192
13.8k
    std::vector<unsigned char> vData;
193
37.1k
    while (pc < scriptSig.end())
194
23.2k
    {
195
23.2k
        opcodetype opcode;
196
23.2k
        if (!scriptSig.GetOp(pc, opcode, vData))
197
0
            return 0;
198
23.2k
        if (opcode > OP_16)
199
1
            return 0;
200
23.2k
    }
201
202
    /// ... and return its opcount:
203
13.8k
    CScript subscript(vData.begin(), vData.end());
204
13.8k
    return subscript.GetSigOpCount(true);
205
13.8k
}
206
207
bool CScript::IsPayToAnchor() const
208
48.4k
{
209
48.4k
    return (this->size() == 4 &&
210
48.4k
        (*this)[0] == OP_1 &&
211
48.4k
        (*this)[1] == 0x02 &&
212
48.4k
        (*this)[2] == 0x4e &&
213
48.4k
        (*this)[3] == 0x73);
214
48.4k
}
215
216
bool CScript::IsPayToAnchor(int version, const std::vector<unsigned char>& program)
217
7.11k
{
218
7.11k
    return version == 1 &&
219
7.11k
        program.size() == 2 &&
220
7.11k
        program[0] == 0x4e &&
221
7.11k
        program[1] == 0x73;
222
7.11k
}
223
224
bool CScript::IsPayToScriptHash() const
225
1.80M
{
226
    // Extra-fast test for pay-to-script-hash CScripts:
227
1.80M
    return (this->size() == 23 &&
228
1.80M
            (*this)[0] == OP_HASH160 &&
229
1.80M
            (*this)[1] == 0x14 &&
230
1.80M
            (*this)[22] == OP_EQUAL);
231
1.80M
}
232
233
bool CScript::IsPayToWitnessScriptHash() const
234
35.5k
{
235
    // Extra-fast test for pay-to-witness-script-hash CScripts:
236
35.5k
    return (this->size() == 34 &&
237
35.5k
            (*this)[0] == OP_0 &&
238
35.5k
            (*this)[1] == 0x20);
239
35.5k
}
240
241
bool CScript::IsPayToTaproot() const
242
47.4k
{
243
47.4k
    return (this->size() == 34 &&
244
47.4k
            (*this)[0] == OP_1 &&
245
47.4k
            (*this)[1] == 0x20);
246
47.4k
}
247
248
// A witness program is any valid CScript that consists of a 1-byte push opcode
249
// followed by a data push between 2 and 40 bytes.
250
bool CScript::IsWitnessProgram(int& version, std::vector<unsigned char>& program) const
251
1.89M
{
252
1.89M
    if (this->size() < 4 || this->size() > 42) {
253
76.9k
        return false;
254
76.9k
    }
255
1.81M
    if ((*this)[0] != OP_0 && ((*this)[0] < OP_1 || (*this)[0] > OP_16)) {
256
467k
        return false;
257
467k
    }
258
1.35M
    if ((size_t)((*this)[1] + 2) == this->size()) {
259
1.34M
        version = DecodeOP_N((opcodetype)(*this)[0]);
260
1.34M
        program = std::vector<unsigned char>(this->begin() + 2, this->end());
261
1.34M
        return true;
262
1.34M
    }
263
696
    return false;
264
1.35M
}
265
266
bool CScript::IsPushOnly(const_iterator pc) const
267
323k
{
268
153M
    while (pc < end())
269
153M
    {
270
153M
        opcodetype opcode;
271
153M
        if (!GetOp(pc, opcode))
272
308
            return false;
273
        // Note that IsPushOnly() *does* consider OP_RESERVED to be a
274
        // push-type opcode, however execution of OP_RESERVED fails, so
275
        // it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to
276
        // the P2SH special validation code being executed.
277
153M
        if (opcode > OP_16)
278
10.0k
            return false;
279
153M
    }
280
313k
    return true;
281
323k
}
282
283
bool CScript::IsPushOnly() const
284
311k
{
285
311k
    return this->IsPushOnly(begin());
286
311k
}
287
288
std::string CScriptWitness::ToString() const
289
3.90k
{
290
3.90k
    std::string ret = "CScriptWitness(";
291
18.3k
    for (unsigned int i = 0; i < stack.size(); i++) {
292
14.4k
        if (i) {
293
11.2k
            ret += ", ";
294
11.2k
        }
295
14.4k
        ret += HexStr(stack[i]);
296
14.4k
    }
297
3.90k
    return ret + ")";
298
3.90k
}
299
300
bool CScript::HasValidOps() const
301
230k
{
302
230k
    CScript::const_iterator it = begin();
303
24.5M
    while (it < end()) {
304
24.3M
        opcodetype opcode;
305
24.3M
        std::vector<unsigned char> item;
306
24.3M
        if (!GetOp(it, opcode, item) || opcode > MAX_OPCODE || item.size() > MAX_SCRIPT_ELEMENT_SIZE) {
307
1.40k
            return false;
308
1.40k
        }
309
24.3M
    }
310
228k
    return true;
311
230k
}
312
313
bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet)
314
689M
{
315
689M
    opcodeRet = OP_INVALIDOPCODE;
316
689M
    if (pvchRet)
317
70.5M
        pvchRet->clear();
318
689M
    if (pc >= end)
319
491k
        return false;
320
321
    // Read instruction
322
688M
    if (end - pc < 1)
323
0
        return false;
324
688M
    unsigned int opcode = *pc++;
325
326
    // Immediate operand
327
688M
    if (opcode <= OP_PUSHDATA4)
328
8.23M
    {
329
8.23M
        unsigned int nSize = 0;
330
8.23M
        if (opcode < OP_PUSHDATA1)
331
8.17M
        {
332
8.17M
            nSize = opcode;
333
8.17M
        }
334
64.2k
        else if (opcode == OP_PUSHDATA1)
335
23.6k
        {
336
23.6k
            if (end - pc < 1)
337
295
                return false;
338
23.3k
            nSize = *pc++;
339
23.3k
        }
340
40.5k
        else if (opcode == OP_PUSHDATA2)
341
22.6k
        {
342
22.6k
            if (end - pc < 2)
343
0
                return false;
344
22.6k
            nSize = ReadLE16(&pc[0]);
345
22.6k
            pc += 2;
346
22.6k
        }
347
17.9k
        else if (opcode == OP_PUSHDATA4)
348
17.9k
        {
349
17.9k
            if (end - pc < 4)
350
0
                return false;
351
17.9k
            nSize = ReadLE32(&pc[0]);
352
17.9k
            pc += 4;
353
17.9k
        }
354
8.23M
        if (end - pc < 0 || (unsigned int)(end - pc) < nSize)
355
871
            return false;
356
8.23M
        if (pvchRet)
357
3.75M
            pvchRet->assign(pc, pc + nSize);
358
8.23M
        pc += nSize;
359
8.23M
    }
360
361
688M
    opcodeRet = static_cast<opcodetype>(opcode);
362
688M
    return true;
363
688M
}
364
365
bool IsOpSuccess(const opcodetype& opcode)
366
11.9M
{
367
11.9M
    return opcode == 80 || opcode == 98 || (opcode >= 126 && opcode <= 129) ||
368
11.9M
           (opcode >= 131 && opcode <= 134) || (opcode >= 137 && opcode <= 138) ||
369
11.9M
           (opcode >= 141 && opcode <= 142) || (opcode >= 149 && opcode <= 153) ||
370
11.9M
           (opcode >= 187 && opcode <= 254);
371
11.9M
}
372
373
835k
bool CheckMinimalPush(const std::vector<unsigned char>& data, opcodetype opcode) {
374
    // Excludes OP_1NEGATE, OP_1-16 since they are by definition minimal
375
835k
    assert(0 <= opcode && opcode <= OP_PUSHDATA4);
376
835k
    if (data.size() == 0) {
377
        // Should have used OP_0.
378
202k
        return opcode == OP_0;
379
632k
    } else if (data.size() == 1 && data[0] >= 1 && data[0] <= 16) {
380
        // Should have used OP_1 .. OP_16.
381
2.07k
        return false;
382
630k
    } else if (data.size() == 1 && data[0] == 0x81) {
383
        // Should have used OP_1NEGATE.
384
213
        return false;
385
630k
    } else if (data.size() <= 75) {
386
        // Must have used a direct push (opcode indicating number of bytes pushed + those bytes).
387
628k
        return opcode == data.size();
388
628k
    } else if (data.size() <= 255) {
389
        // Must have used OP_PUSHDATA.
390
1.05k
        return opcode == OP_PUSHDATA1;
391
1.05k
    } else if (data.size() <= 65535) {
392
        // Must have used OP_PUSHDATA2.
393
433
        return opcode == OP_PUSHDATA2;
394
433
    }
395
0
    return true;
396
835k
}