Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2020 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 : #include <util/strencodings.h> 8 : 9 : #include <string> 10 : 11 104536 : std::string GetOpName(opcodetype opcode) 12 : { 13 104536 : switch (opcode) 14 : { 15 : // push value 16 0 : case OP_0 : return "0"; 17 0 : case OP_PUSHDATA1 : return "OP_PUSHDATA1"; 18 0 : case OP_PUSHDATA2 : return "OP_PUSHDATA2"; 19 0 : case OP_PUSHDATA4 : return "OP_PUSHDATA4"; 20 0 : case OP_1NEGATE : return "-1"; 21 8 : case OP_RESERVED : return "OP_RESERVED"; 22 2 : case OP_1 : return "1"; 23 23 : case OP_2 : return "2"; 24 23 : case OP_3 : return "3"; 25 0 : case OP_4 : return "4"; 26 0 : case OP_5 : return "5"; 27 0 : case OP_6 : return "6"; 28 0 : case OP_7 : return "7"; 29 0 : case OP_8 : return "8"; 30 0 : case OP_9 : return "9"; 31 0 : case OP_10 : return "10"; 32 0 : case OP_11 : return "11"; 33 0 : case OP_12 : return "12"; 34 0 : case OP_13 : return "13"; 35 0 : case OP_14 : return "14"; 36 0 : case OP_15 : return "15"; 37 0 : case OP_16 : return "16"; 38 : 39 : // control 40 8 : case OP_NOP : return "OP_NOP"; 41 8 : case OP_VER : return "OP_VER"; 42 10 : case OP_IF : return "OP_IF"; 43 8 : case OP_NOTIF : return "OP_NOTIF"; 44 8 : case OP_VERIF : return "OP_VERIF"; 45 8 : case OP_VERNOTIF : return "OP_VERNOTIF"; 46 10 : case OP_ELSE : return "OP_ELSE"; 47 10 : case OP_ENDIF : return "OP_ENDIF"; 48 8 : case OP_VERIFY : return "OP_VERIFY"; 49 284 : case OP_RETURN : return "OP_RETURN"; 50 : 51 : // stack ops 52 8 : case OP_TOALTSTACK : return "OP_TOALTSTACK"; 53 8 : case OP_FROMALTSTACK : return "OP_FROMALTSTACK"; 54 8 : case OP_2DROP : return "OP_2DROP"; 55 8 : case OP_2DUP : return "OP_2DUP"; 56 8 : case OP_3DUP : return "OP_3DUP"; 57 8 : case OP_2OVER : return "OP_2OVER"; 58 8 : case OP_2ROT : return "OP_2ROT"; 59 8 : case OP_2SWAP : return "OP_2SWAP"; 60 8 : case OP_IFDUP : return "OP_IFDUP"; 61 8 : case OP_DEPTH : return "OP_DEPTH"; 62 11 : case OP_DROP : return "OP_DROP"; 63 20281 : case OP_DUP : return "OP_DUP"; 64 8 : case OP_NIP : return "OP_NIP"; 65 8 : case OP_OVER : return "OP_OVER"; 66 8 : case OP_PICK : return "OP_PICK"; 67 8 : case OP_ROLL : return "OP_ROLL"; 68 8 : case OP_ROT : return "OP_ROT"; 69 8 : case OP_SWAP : return "OP_SWAP"; 70 8 : case OP_TUCK : return "OP_TUCK"; 71 : 72 : // splice ops 73 8 : case OP_CAT : return "OP_CAT"; 74 8 : case OP_SPLIT : return "OP_SPLIT"; 75 8 : case OP_SIZE : return "OP_SIZE"; 76 : 77 : // conversion ops 78 8 : case OP_NUM2BIN : return "OP_NUM2BIN"; 79 8 : case OP_BIN2NUM : return "OP_BIN2NUM"; 80 : 81 : // bit logic 82 8 : case OP_INVERT : return "OP_INVERT"; 83 8 : case OP_AND : return "OP_AND"; 84 8 : case OP_OR : return "OP_OR"; 85 8 : case OP_XOR : return "OP_XOR"; 86 10953 : case OP_EQUAL : return "OP_EQUAL"; 87 20279 : case OP_EQUALVERIFY : return "OP_EQUALVERIFY"; 88 8 : case OP_RESERVED1 : return "OP_RESERVED1"; 89 8 : case OP_RESERVED2 : return "OP_RESERVED2"; 90 : 91 : // numeric 92 8 : case OP_1ADD : return "OP_1ADD"; 93 8 : case OP_1SUB : return "OP_1SUB"; 94 8 : case OP_2MUL : return "OP_2MUL"; 95 8 : case OP_2DIV : return "OP_2DIV"; 96 8 : case OP_NEGATE : return "OP_NEGATE"; 97 8 : case OP_ABS : return "OP_ABS"; 98 36 : case OP_NOT : return "OP_NOT"; 99 8 : case OP_0NOTEQUAL : return "OP_0NOTEQUAL"; 100 8 : case OP_ADD : return "OP_ADD"; 101 8 : case OP_SUB : return "OP_SUB"; 102 8 : case OP_MUL : return "OP_MUL"; 103 8 : case OP_DIV : return "OP_DIV"; 104 8 : case OP_MOD : return "OP_MOD"; 105 8 : case OP_LSHIFT : return "OP_LSHIFT"; 106 8 : case OP_RSHIFT : return "OP_RSHIFT"; 107 8 : case OP_BOOLAND : return "OP_BOOLAND"; 108 8 : case OP_BOOLOR : return "OP_BOOLOR"; 109 8 : case OP_NUMEQUAL : return "OP_NUMEQUAL"; 110 8 : case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY"; 111 8 : case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL"; 112 8 : case OP_LESSTHAN : return "OP_LESSTHAN"; 113 8 : case OP_GREATERTHAN : return "OP_GREATERTHAN"; 114 8 : case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL"; 115 8 : case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL"; 116 8 : case OP_MIN : return "OP_MIN"; 117 8 : case OP_MAX : return "OP_MAX"; 118 8 : case OP_WITHIN : return "OP_WITHIN"; 119 : 120 : // crypto 121 8 : case OP_RIPEMD160 : return "OP_RIPEMD160"; 122 8 : case OP_SHA1 : return "OP_SHA1"; 123 8 : case OP_SHA256 : return "OP_SHA256"; 124 31224 : case OP_HASH160 : return "OP_HASH160"; 125 8 : case OP_HASH256 : return "OP_HASH256"; 126 8 : case OP_CODESEPARATOR : return "OP_CODESEPARATOR"; 127 20683 : case OP_CHECKSIG : return "OP_CHECKSIG"; 128 10 : case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY"; 129 55 : case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG"; 130 8 : case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY"; 131 : 132 : // expansion 133 8 : case OP_NOP1 : return "OP_NOP1"; 134 14 : case OP_CHECKLOCKTIMEVERIFY : return "OP_CHECKLOCKTIMEVERIFY"; 135 8 : case OP_CHECKSEQUENCEVERIFY : return "OP_CHECKSEQUENCEVERIFY"; 136 8 : case OP_NOP4 : return "OP_NOP4"; 137 8 : case OP_NOP5 : return "OP_NOP5"; 138 8 : case OP_NOP6 : return "OP_NOP6"; 139 8 : case OP_NOP7 : return "OP_NOP7"; 140 12 : case OP_NOP8 : return "OP_NOP8"; 141 8 : case OP_NOP9 : return "OP_NOP9"; 142 8 : case OP_NOP10 : return "OP_NOP10"; 143 : 144 8 : case OP_CHECKDATASIG : return "OP_CHECKDATASIG"; 145 8 : case OP_CHECKDATASIGVERIFY : return "OP_CHECKDATASIGVERIFY"; 146 : 147 0 : case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE"; 148 : 149 : default: 150 0 : return "OP_UNKNOWN"; 151 : } 152 104536 : } 153 : 154 7885367 : unsigned int CScript::GetSigOpCount(bool fAccurate) const 155 : { 156 7885367 : unsigned int n = 0; 157 7885367 : const_iterator pc = begin(); 158 7885367 : opcodetype lastOpcode = OP_INVALIDOPCODE; 159 35062310 : while (pc < end()) 160 : { 161 : opcodetype opcode; 162 27176983 : if (!GetOp(pc, opcode)) 163 40 : break; 164 27176943 : if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY) 165 6247757 : n++; 166 20929186 : else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY) 167 : { 168 42367 : if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16) 169 409 : n += DecodeOP_N(lastOpcode); 170 : else 171 41958 : n += MAX_PUBKEYS_PER_MULTISIG; 172 42367 : } 173 27176943 : lastOpcode = opcode; 174 : } 175 7885367 : return n; 176 : } 177 : 178 133575 : unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const 179 : { 180 133575 : if (!IsPayToScriptHash()) 181 0 : return GetSigOpCount(true); 182 : 183 : // This is a pay-to-script-hash scriptPubKey; 184 : // get the last item that the scriptSig 185 : // pushes onto the stack: 186 133575 : const_iterator pc = scriptSig.begin(); 187 133575 : std::vector<unsigned char> vData; 188 288314 : while (pc < scriptSig.end()) 189 : { 190 : opcodetype opcode; 191 154739 : if (!scriptSig.GetOp(pc, opcode, vData)) 192 0 : return 0; 193 154739 : if (opcode > OP_16) 194 0 : return 0; 195 : } 196 : 197 : /// ... and return its opcount: 198 133575 : CScript subscript(vData.begin(), vData.end()); 199 133575 : return subscript.GetSigOpCount(true); 200 133575 : } 201 : 202 15204 : bool CScript::IsPayToPublicKeyHash() const 203 : { 204 : // Extra-fast test for pay-to-pubkey-hash CScripts: 205 30108 : return (this->size() == 25 && 206 14904 : (*this)[0] == OP_DUP && 207 14903 : (*this)[1] == OP_HASH160 && 208 14903 : (*this)[2] == 0x14 && 209 14903 : (*this)[23] == OP_EQUALVERIFY && 210 14902 : (*this)[24] == OP_CHECKSIG); 211 : } 212 : 213 6779624 : bool CScript::IsPayToScriptHash() const 214 : { 215 : // Extra-fast test for pay-to-script-hash CScripts: 216 7354879 : return (this->size() == 23 && 217 575143 : (*this)[0] == OP_HASH160 && 218 575070 : (*this)[1] == 0x14 && 219 575126 : (*this)[22] == OP_EQUAL); 220 : } 221 : 222 6 : bool CScript::IsPayToPublicKey() const 223 : { 224 : // Test for pay-to-pubkey CScript with both 225 : // compressed or uncompressed pubkey 226 6 : if (this->size() == 35) { 227 6 : return ((*this)[1] == 0x02 || (*this)[1] == 0x03) && 228 3 : (*this)[34] == OP_CHECKSIG; 229 : } 230 3 : if (this->size() == 67) { 231 2 : return (*this)[1] == 0x04 && 232 1 : (*this)[66] == OP_CHECKSIG; 233 : } 234 2 : return false; 235 6 : } 236 : 237 423817 : bool CScript::IsPushOnly(const_iterator pc) const 238 : { 239 879611 : while (pc < end()) 240 : { 241 : opcodetype opcode; 242 457047 : if (!GetOp(pc, opcode)) 243 25 : return false; 244 : // Note that IsPushOnly() *does* consider OP_RESERVED to be a 245 : // push-type opcode, however execution of OP_RESERVED fails, so 246 : // it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to 247 : // the P2SH special validation code being executed. 248 457022 : if (opcode > OP_16) 249 1228 : return false; 250 : } 251 422564 : return true; 252 423817 : } 253 : 254 330403 : bool CScript::IsPushOnly() const 255 : { 256 330403 : return this->IsPushOnly(begin()); 257 : } 258 : 259 50705944 : bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) 260 : { 261 50705944 : opcodeRet = OP_INVALIDOPCODE; 262 50705944 : if (pvchRet) 263 8495572 : pvchRet->clear(); 264 50705944 : if (pc >= end) 265 2203111 : return false; 266 : 267 : // Read instruction 268 48502833 : if (end - pc < 1) 269 0 : return false; 270 48502833 : unsigned int opcode = *pc++; 271 : 272 : // Immediate operand 273 48502833 : if (opcode <= OP_PUSHDATA4) 274 : { 275 13449744 : unsigned int nSize = 0; 276 13449744 : if (opcode < OP_PUSHDATA1) 277 : { 278 13408841 : nSize = opcode; 279 13408841 : } 280 40903 : else if (opcode == OP_PUSHDATA1) 281 : { 282 5091 : if (end - pc < 1) 283 0 : return false; 284 5091 : nSize = *pc++; 285 5091 : } 286 35812 : else if (opcode == OP_PUSHDATA2) 287 : { 288 3041 : if (end - pc < 2) 289 0 : return false; 290 3041 : nSize = ReadLE16(&pc[0]); 291 3041 : pc += 2; 292 3041 : } 293 32771 : else if (opcode == OP_PUSHDATA4) 294 : { 295 32771 : if (end - pc < 4) 296 0 : return false; 297 32771 : nSize = ReadLE32(&pc[0]); 298 32771 : pc += 4; 299 32771 : } 300 13449744 : if (end - pc < 0 || (unsigned int)(end - pc) < nSize) 301 6142 : return false; 302 13451258 : if (pvchRet) 303 3589165 : CScriptBase::assign_to(pc, pc + nSize, *pvchRet); 304 13451258 : pc += nSize; 305 13451258 : } 306 : 307 48504347 : opcodeRet = static_cast<opcodetype>(opcode); 308 48504347 : return true; 309 50713600 : } 310 : 311 54957 : bool CScriptNum::IsMinimallyEncoded(const std::vector<uint8_t>& vch, const size_t nMaxNumSize) 312 : { 313 54957 : if (vch.size() > nMaxNumSize) { 314 21 : return false; 315 : } 316 : 317 54936 : if (vch.size() > 0) { 318 : // Check that the number is encoded with the minimum possible number 319 : // of bytes. 320 : // 321 : // If the most-significant-byte - excluding the sign bit - is zero 322 : // then we're not minimal. Note how this test also rejects the 323 : // negative-zero encoding, 0x80. 324 36740 : if ((vch.back() & 0x7f) == 0) { 325 : // One exception: if there's more than one byte and the most 326 : // significant bit of the second-most-significant-byte is set it 327 : // would conflict with the sign bit. An example of this case is 328 : // +-255, which encode to 0xff00 and 0xff80 respectively. 329 : // (big-endian). 330 4211 : if (vch.size() <= 1 || (vch[vch.size() - 2] & 0x80) == 0) { 331 799 : return false; 332 : } 333 3412 : } 334 35941 : } 335 : 336 54137 : return true; 337 54957 : } 338 : 339 42618 : bool CScriptNum::MinimallyEncode(std::vector<uint8_t>& data) 340 : { 341 42618 : if (data.size() == 0) { 342 8463 : return false; 343 : } 344 : 345 : // If the last byte is not 0x00 or 0x80, we are minimally encoded. 346 34155 : uint8_t last = data.back(); 347 34155 : if (last & 0x7f) { 348 8741 : return false; 349 : } 350 : 351 : // If the script is one byte long, then we have a zero, which encodes as an 352 : // empty array. 353 25414 : if (data.size() == 1) { 354 86 : data = {}; 355 86 : return true; 356 : } 357 : 358 : // If the next byte has its sign bit set, then we are minimally encoded. 359 25328 : if (data[data.size() - 2] & 0x80) { 360 95 : return false; 361 : } 362 : 363 : // We are not minimally encoded, we need to figure out how much to trim. 364 8661620 : for (size_t i = data.size() - 1; i > 0; i--) { 365 : // We found a non zero byte, time to encode. 366 8649146 : if (data[i - 1] != 0) { 367 12759 : if (data[i - 1] & 0x80) { 368 : // We found a byte with its sign bit set so we need one more 369 : // byte. 370 89 : data[i++] = last; 371 89 : } else { 372 : // the sign bit is clear, we can use it. 373 12670 : data[i - 1] |= last; 374 : } 375 : 376 12759 : data.resize(i); 377 12759 : return true; 378 : } 379 8636387 : } 380 : 381 : // If the whole thing is zeros, then we have a zero (empty array). 382 12474 : data = {}; 383 12474 : return true; 384 42618 : } 385 : 386 2782020 : bool CheckMinimalPush(const std::vector<unsigned char>& data, opcodetype opcode) { 387 : // Excludes OP_1NEGATE, OP_1-16 since they are by definition minimal 388 2782020 : assert(0 <= opcode && opcode <= OP_PUSHDATA4); 389 2782020 : if (data.size() == 0) { 390 : // Should have used OP_0. 391 7583 : return opcode == OP_0; 392 2774437 : } else if (data.size() == 1 && data[0] >= 1 && data[0] <= 16) { 393 : // Should have used OP_1 .. OP_16. 394 217 : return false; 395 2774220 : } else if (data.size() == 1 && data[0] == 0x81) { 396 : // Should have used OP_1NEGATE. 397 19 : return false; 398 2774201 : } else if (data.size() <= 75) { 399 : // Must have used a direct push (opcode indicating number of bytes pushed + those bytes). 400 2773202 : return opcode == data.size(); 401 999 : } else if (data.size() <= 255) { 402 : // Must have used OP_PUSHDATA. 403 564 : return opcode == OP_PUSHDATA1; 404 435 : } else if (data.size() <= 65535) { 405 : // Must have used OP_PUSHDATA2. 406 435 : return opcode == OP_PUSHDATA2; 407 : } 408 0 : return true; 409 2782020 : }