Coverage Report

Created: 2026-04-29 19:21

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