github.com/btcsuite/btcd@v0.24.0/txscript/opcode.go (about) 1 // Copyright (c) 2013-2017 The btcsuite developers 2 // Use of this source code is governed by an ISC 3 // license that can be found in the LICENSE file. 4 5 package txscript 6 7 import ( 8 "bytes" 9 "crypto/sha1" 10 "crypto/sha256" 11 "encoding/hex" 12 "errors" 13 "fmt" 14 "hash" 15 "strings" 16 17 "golang.org/x/crypto/ripemd160" 18 19 "github.com/btcsuite/btcd/btcec/v2" 20 "github.com/btcsuite/btcd/btcec/v2/ecdsa" 21 "github.com/btcsuite/btcd/chaincfg/chainhash" 22 "github.com/btcsuite/btcd/wire" 23 ) 24 25 // An opcode defines the information related to a txscript opcode. opfunc, if 26 // present, is the function to call to perform the opcode on the script. The 27 // current script is passed in as a slice with the first member being the opcode 28 // itself. 29 type opcode struct { 30 value byte 31 name string 32 length int 33 opfunc func(*opcode, []byte, *Engine) error 34 } 35 36 // These constants are the values of the official opcodes used on the btc wiki, 37 // in bitcoin core and in most if not all other references and software related 38 // to handling BTC scripts. 39 const ( 40 OP_0 = 0x00 // 0 41 OP_FALSE = 0x00 // 0 - AKA OP_0 42 OP_DATA_1 = 0x01 // 1 43 OP_DATA_2 = 0x02 // 2 44 OP_DATA_3 = 0x03 // 3 45 OP_DATA_4 = 0x04 // 4 46 OP_DATA_5 = 0x05 // 5 47 OP_DATA_6 = 0x06 // 6 48 OP_DATA_7 = 0x07 // 7 49 OP_DATA_8 = 0x08 // 8 50 OP_DATA_9 = 0x09 // 9 51 OP_DATA_10 = 0x0a // 10 52 OP_DATA_11 = 0x0b // 11 53 OP_DATA_12 = 0x0c // 12 54 OP_DATA_13 = 0x0d // 13 55 OP_DATA_14 = 0x0e // 14 56 OP_DATA_15 = 0x0f // 15 57 OP_DATA_16 = 0x10 // 16 58 OP_DATA_17 = 0x11 // 17 59 OP_DATA_18 = 0x12 // 18 60 OP_DATA_19 = 0x13 // 19 61 OP_DATA_20 = 0x14 // 20 62 OP_DATA_21 = 0x15 // 21 63 OP_DATA_22 = 0x16 // 22 64 OP_DATA_23 = 0x17 // 23 65 OP_DATA_24 = 0x18 // 24 66 OP_DATA_25 = 0x19 // 25 67 OP_DATA_26 = 0x1a // 26 68 OP_DATA_27 = 0x1b // 27 69 OP_DATA_28 = 0x1c // 28 70 OP_DATA_29 = 0x1d // 29 71 OP_DATA_30 = 0x1e // 30 72 OP_DATA_31 = 0x1f // 31 73 OP_DATA_32 = 0x20 // 32 74 OP_DATA_33 = 0x21 // 33 75 OP_DATA_34 = 0x22 // 34 76 OP_DATA_35 = 0x23 // 35 77 OP_DATA_36 = 0x24 // 36 78 OP_DATA_37 = 0x25 // 37 79 OP_DATA_38 = 0x26 // 38 80 OP_DATA_39 = 0x27 // 39 81 OP_DATA_40 = 0x28 // 40 82 OP_DATA_41 = 0x29 // 41 83 OP_DATA_42 = 0x2a // 42 84 OP_DATA_43 = 0x2b // 43 85 OP_DATA_44 = 0x2c // 44 86 OP_DATA_45 = 0x2d // 45 87 OP_DATA_46 = 0x2e // 46 88 OP_DATA_47 = 0x2f // 47 89 OP_DATA_48 = 0x30 // 48 90 OP_DATA_49 = 0x31 // 49 91 OP_DATA_50 = 0x32 // 50 92 OP_DATA_51 = 0x33 // 51 93 OP_DATA_52 = 0x34 // 52 94 OP_DATA_53 = 0x35 // 53 95 OP_DATA_54 = 0x36 // 54 96 OP_DATA_55 = 0x37 // 55 97 OP_DATA_56 = 0x38 // 56 98 OP_DATA_57 = 0x39 // 57 99 OP_DATA_58 = 0x3a // 58 100 OP_DATA_59 = 0x3b // 59 101 OP_DATA_60 = 0x3c // 60 102 OP_DATA_61 = 0x3d // 61 103 OP_DATA_62 = 0x3e // 62 104 OP_DATA_63 = 0x3f // 63 105 OP_DATA_64 = 0x40 // 64 106 OP_DATA_65 = 0x41 // 65 107 OP_DATA_66 = 0x42 // 66 108 OP_DATA_67 = 0x43 // 67 109 OP_DATA_68 = 0x44 // 68 110 OP_DATA_69 = 0x45 // 69 111 OP_DATA_70 = 0x46 // 70 112 OP_DATA_71 = 0x47 // 71 113 OP_DATA_72 = 0x48 // 72 114 OP_DATA_73 = 0x49 // 73 115 OP_DATA_74 = 0x4a // 74 116 OP_DATA_75 = 0x4b // 75 117 OP_PUSHDATA1 = 0x4c // 76 118 OP_PUSHDATA2 = 0x4d // 77 119 OP_PUSHDATA4 = 0x4e // 78 120 OP_1NEGATE = 0x4f // 79 121 OP_RESERVED = 0x50 // 80 122 OP_1 = 0x51 // 81 - AKA OP_TRUE 123 OP_TRUE = 0x51 // 81 124 OP_2 = 0x52 // 82 125 OP_3 = 0x53 // 83 126 OP_4 = 0x54 // 84 127 OP_5 = 0x55 // 85 128 OP_6 = 0x56 // 86 129 OP_7 = 0x57 // 87 130 OP_8 = 0x58 // 88 131 OP_9 = 0x59 // 89 132 OP_10 = 0x5a // 90 133 OP_11 = 0x5b // 91 134 OP_12 = 0x5c // 92 135 OP_13 = 0x5d // 93 136 OP_14 = 0x5e // 94 137 OP_15 = 0x5f // 95 138 OP_16 = 0x60 // 96 139 OP_NOP = 0x61 // 97 140 OP_VER = 0x62 // 98 141 OP_IF = 0x63 // 99 142 OP_NOTIF = 0x64 // 100 143 OP_VERIF = 0x65 // 101 144 OP_VERNOTIF = 0x66 // 102 145 OP_ELSE = 0x67 // 103 146 OP_ENDIF = 0x68 // 104 147 OP_VERIFY = 0x69 // 105 148 OP_RETURN = 0x6a // 106 149 OP_TOALTSTACK = 0x6b // 107 150 OP_FROMALTSTACK = 0x6c // 108 151 OP_2DROP = 0x6d // 109 152 OP_2DUP = 0x6e // 110 153 OP_3DUP = 0x6f // 111 154 OP_2OVER = 0x70 // 112 155 OP_2ROT = 0x71 // 113 156 OP_2SWAP = 0x72 // 114 157 OP_IFDUP = 0x73 // 115 158 OP_DEPTH = 0x74 // 116 159 OP_DROP = 0x75 // 117 160 OP_DUP = 0x76 // 118 161 OP_NIP = 0x77 // 119 162 OP_OVER = 0x78 // 120 163 OP_PICK = 0x79 // 121 164 OP_ROLL = 0x7a // 122 165 OP_ROT = 0x7b // 123 166 OP_SWAP = 0x7c // 124 167 OP_TUCK = 0x7d // 125 168 OP_CAT = 0x7e // 126 169 OP_SUBSTR = 0x7f // 127 170 OP_LEFT = 0x80 // 128 171 OP_RIGHT = 0x81 // 129 172 OP_SIZE = 0x82 // 130 173 OP_INVERT = 0x83 // 131 174 OP_AND = 0x84 // 132 175 OP_OR = 0x85 // 133 176 OP_XOR = 0x86 // 134 177 OP_EQUAL = 0x87 // 135 178 OP_EQUALVERIFY = 0x88 // 136 179 OP_RESERVED1 = 0x89 // 137 180 OP_RESERVED2 = 0x8a // 138 181 OP_1ADD = 0x8b // 139 182 OP_1SUB = 0x8c // 140 183 OP_2MUL = 0x8d // 141 184 OP_2DIV = 0x8e // 142 185 OP_NEGATE = 0x8f // 143 186 OP_ABS = 0x90 // 144 187 OP_NOT = 0x91 // 145 188 OP_0NOTEQUAL = 0x92 // 146 189 OP_ADD = 0x93 // 147 190 OP_SUB = 0x94 // 148 191 OP_MUL = 0x95 // 149 192 OP_DIV = 0x96 // 150 193 OP_MOD = 0x97 // 151 194 OP_LSHIFT = 0x98 // 152 195 OP_RSHIFT = 0x99 // 153 196 OP_BOOLAND = 0x9a // 154 197 OP_BOOLOR = 0x9b // 155 198 OP_NUMEQUAL = 0x9c // 156 199 OP_NUMEQUALVERIFY = 0x9d // 157 200 OP_NUMNOTEQUAL = 0x9e // 158 201 OP_LESSTHAN = 0x9f // 159 202 OP_GREATERTHAN = 0xa0 // 160 203 OP_LESSTHANOREQUAL = 0xa1 // 161 204 OP_GREATERTHANOREQUAL = 0xa2 // 162 205 OP_MIN = 0xa3 // 163 206 OP_MAX = 0xa4 // 164 207 OP_WITHIN = 0xa5 // 165 208 OP_RIPEMD160 = 0xa6 // 166 209 OP_SHA1 = 0xa7 // 167 210 OP_SHA256 = 0xa8 // 168 211 OP_HASH160 = 0xa9 // 169 212 OP_HASH256 = 0xaa // 170 213 OP_CODESEPARATOR = 0xab // 171 214 OP_CHECKSIG = 0xac // 172 215 OP_CHECKSIGVERIFY = 0xad // 173 216 OP_CHECKMULTISIG = 0xae // 174 217 OP_CHECKMULTISIGVERIFY = 0xaf // 175 218 OP_NOP1 = 0xb0 // 176 219 OP_NOP2 = 0xb1 // 177 220 OP_CHECKLOCKTIMEVERIFY = 0xb1 // 177 - AKA OP_NOP2 221 OP_NOP3 = 0xb2 // 178 222 OP_CHECKSEQUENCEVERIFY = 0xb2 // 178 - AKA OP_NOP3 223 OP_NOP4 = 0xb3 // 179 224 OP_NOP5 = 0xb4 // 180 225 OP_NOP6 = 0xb5 // 181 226 OP_NOP7 = 0xb6 // 182 227 OP_NOP8 = 0xb7 // 183 228 OP_NOP9 = 0xb8 // 184 229 OP_NOP10 = 0xb9 // 185 230 OP_CHECKSIGADD = 0xba // 186 231 OP_UNKNOWN187 = 0xbb // 187 232 OP_UNKNOWN188 = 0xbc // 188 233 OP_UNKNOWN189 = 0xbd // 189 234 OP_UNKNOWN190 = 0xbe // 190 235 OP_UNKNOWN191 = 0xbf // 191 236 OP_UNKNOWN192 = 0xc0 // 192 237 OP_UNKNOWN193 = 0xc1 // 193 238 OP_UNKNOWN194 = 0xc2 // 194 239 OP_UNKNOWN195 = 0xc3 // 195 240 OP_UNKNOWN196 = 0xc4 // 196 241 OP_UNKNOWN197 = 0xc5 // 197 242 OP_UNKNOWN198 = 0xc6 // 198 243 OP_UNKNOWN199 = 0xc7 // 199 244 OP_UNKNOWN200 = 0xc8 // 200 245 OP_UNKNOWN201 = 0xc9 // 201 246 OP_UNKNOWN202 = 0xca // 202 247 OP_UNKNOWN203 = 0xcb // 203 248 OP_UNKNOWN204 = 0xcc // 204 249 OP_UNKNOWN205 = 0xcd // 205 250 OP_UNKNOWN206 = 0xce // 206 251 OP_UNKNOWN207 = 0xcf // 207 252 OP_UNKNOWN208 = 0xd0 // 208 253 OP_UNKNOWN209 = 0xd1 // 209 254 OP_UNKNOWN210 = 0xd2 // 210 255 OP_UNKNOWN211 = 0xd3 // 211 256 OP_UNKNOWN212 = 0xd4 // 212 257 OP_UNKNOWN213 = 0xd5 // 213 258 OP_UNKNOWN214 = 0xd6 // 214 259 OP_UNKNOWN215 = 0xd7 // 215 260 OP_UNKNOWN216 = 0xd8 // 216 261 OP_UNKNOWN217 = 0xd9 // 217 262 OP_UNKNOWN218 = 0xda // 218 263 OP_UNKNOWN219 = 0xdb // 219 264 OP_UNKNOWN220 = 0xdc // 220 265 OP_UNKNOWN221 = 0xdd // 221 266 OP_UNKNOWN222 = 0xde // 222 267 OP_UNKNOWN223 = 0xdf // 223 268 OP_UNKNOWN224 = 0xe0 // 224 269 OP_UNKNOWN225 = 0xe1 // 225 270 OP_UNKNOWN226 = 0xe2 // 226 271 OP_UNKNOWN227 = 0xe3 // 227 272 OP_UNKNOWN228 = 0xe4 // 228 273 OP_UNKNOWN229 = 0xe5 // 229 274 OP_UNKNOWN230 = 0xe6 // 230 275 OP_UNKNOWN231 = 0xe7 // 231 276 OP_UNKNOWN232 = 0xe8 // 232 277 OP_UNKNOWN233 = 0xe9 // 233 278 OP_UNKNOWN234 = 0xea // 234 279 OP_UNKNOWN235 = 0xeb // 235 280 OP_UNKNOWN236 = 0xec // 236 281 OP_UNKNOWN237 = 0xed // 237 282 OP_UNKNOWN238 = 0xee // 238 283 OP_UNKNOWN239 = 0xef // 239 284 OP_UNKNOWN240 = 0xf0 // 240 285 OP_UNKNOWN241 = 0xf1 // 241 286 OP_UNKNOWN242 = 0xf2 // 242 287 OP_UNKNOWN243 = 0xf3 // 243 288 OP_UNKNOWN244 = 0xf4 // 244 289 OP_UNKNOWN245 = 0xf5 // 245 290 OP_UNKNOWN246 = 0xf6 // 246 291 OP_UNKNOWN247 = 0xf7 // 247 292 OP_UNKNOWN248 = 0xf8 // 248 293 OP_UNKNOWN249 = 0xf9 // 249 294 OP_SMALLINTEGER = 0xfa // 250 - bitcoin core internal 295 OP_PUBKEYS = 0xfb // 251 - bitcoin core internal 296 OP_UNKNOWN252 = 0xfc // 252 297 OP_PUBKEYHASH = 0xfd // 253 - bitcoin core internal 298 OP_PUBKEY = 0xfe // 254 - bitcoin core internal 299 OP_INVALIDOPCODE = 0xff // 255 - bitcoin core internal 300 ) 301 302 // Conditional execution constants. 303 const ( 304 OpCondFalse = 0 305 OpCondTrue = 1 306 OpCondSkip = 2 307 ) 308 309 // opcodeArray holds details about all possible opcodes such as how many bytes 310 // the opcode and any associated data should take, its human-readable name, and 311 // the handler function. 312 var opcodeArray = [256]opcode{ 313 // Data push opcodes. 314 OP_FALSE: {OP_FALSE, "OP_0", 1, opcodeFalse}, 315 OP_DATA_1: {OP_DATA_1, "OP_DATA_1", 2, opcodePushData}, 316 OP_DATA_2: {OP_DATA_2, "OP_DATA_2", 3, opcodePushData}, 317 OP_DATA_3: {OP_DATA_3, "OP_DATA_3", 4, opcodePushData}, 318 OP_DATA_4: {OP_DATA_4, "OP_DATA_4", 5, opcodePushData}, 319 OP_DATA_5: {OP_DATA_5, "OP_DATA_5", 6, opcodePushData}, 320 OP_DATA_6: {OP_DATA_6, "OP_DATA_6", 7, opcodePushData}, 321 OP_DATA_7: {OP_DATA_7, "OP_DATA_7", 8, opcodePushData}, 322 OP_DATA_8: {OP_DATA_8, "OP_DATA_8", 9, opcodePushData}, 323 OP_DATA_9: {OP_DATA_9, "OP_DATA_9", 10, opcodePushData}, 324 OP_DATA_10: {OP_DATA_10, "OP_DATA_10", 11, opcodePushData}, 325 OP_DATA_11: {OP_DATA_11, "OP_DATA_11", 12, opcodePushData}, 326 OP_DATA_12: {OP_DATA_12, "OP_DATA_12", 13, opcodePushData}, 327 OP_DATA_13: {OP_DATA_13, "OP_DATA_13", 14, opcodePushData}, 328 OP_DATA_14: {OP_DATA_14, "OP_DATA_14", 15, opcodePushData}, 329 OP_DATA_15: {OP_DATA_15, "OP_DATA_15", 16, opcodePushData}, 330 OP_DATA_16: {OP_DATA_16, "OP_DATA_16", 17, opcodePushData}, 331 OP_DATA_17: {OP_DATA_17, "OP_DATA_17", 18, opcodePushData}, 332 OP_DATA_18: {OP_DATA_18, "OP_DATA_18", 19, opcodePushData}, 333 OP_DATA_19: {OP_DATA_19, "OP_DATA_19", 20, opcodePushData}, 334 OP_DATA_20: {OP_DATA_20, "OP_DATA_20", 21, opcodePushData}, 335 OP_DATA_21: {OP_DATA_21, "OP_DATA_21", 22, opcodePushData}, 336 OP_DATA_22: {OP_DATA_22, "OP_DATA_22", 23, opcodePushData}, 337 OP_DATA_23: {OP_DATA_23, "OP_DATA_23", 24, opcodePushData}, 338 OP_DATA_24: {OP_DATA_24, "OP_DATA_24", 25, opcodePushData}, 339 OP_DATA_25: {OP_DATA_25, "OP_DATA_25", 26, opcodePushData}, 340 OP_DATA_26: {OP_DATA_26, "OP_DATA_26", 27, opcodePushData}, 341 OP_DATA_27: {OP_DATA_27, "OP_DATA_27", 28, opcodePushData}, 342 OP_DATA_28: {OP_DATA_28, "OP_DATA_28", 29, opcodePushData}, 343 OP_DATA_29: {OP_DATA_29, "OP_DATA_29", 30, opcodePushData}, 344 OP_DATA_30: {OP_DATA_30, "OP_DATA_30", 31, opcodePushData}, 345 OP_DATA_31: {OP_DATA_31, "OP_DATA_31", 32, opcodePushData}, 346 OP_DATA_32: {OP_DATA_32, "OP_DATA_32", 33, opcodePushData}, 347 OP_DATA_33: {OP_DATA_33, "OP_DATA_33", 34, opcodePushData}, 348 OP_DATA_34: {OP_DATA_34, "OP_DATA_34", 35, opcodePushData}, 349 OP_DATA_35: {OP_DATA_35, "OP_DATA_35", 36, opcodePushData}, 350 OP_DATA_36: {OP_DATA_36, "OP_DATA_36", 37, opcodePushData}, 351 OP_DATA_37: {OP_DATA_37, "OP_DATA_37", 38, opcodePushData}, 352 OP_DATA_38: {OP_DATA_38, "OP_DATA_38", 39, opcodePushData}, 353 OP_DATA_39: {OP_DATA_39, "OP_DATA_39", 40, opcodePushData}, 354 OP_DATA_40: {OP_DATA_40, "OP_DATA_40", 41, opcodePushData}, 355 OP_DATA_41: {OP_DATA_41, "OP_DATA_41", 42, opcodePushData}, 356 OP_DATA_42: {OP_DATA_42, "OP_DATA_42", 43, opcodePushData}, 357 OP_DATA_43: {OP_DATA_43, "OP_DATA_43", 44, opcodePushData}, 358 OP_DATA_44: {OP_DATA_44, "OP_DATA_44", 45, opcodePushData}, 359 OP_DATA_45: {OP_DATA_45, "OP_DATA_45", 46, opcodePushData}, 360 OP_DATA_46: {OP_DATA_46, "OP_DATA_46", 47, opcodePushData}, 361 OP_DATA_47: {OP_DATA_47, "OP_DATA_47", 48, opcodePushData}, 362 OP_DATA_48: {OP_DATA_48, "OP_DATA_48", 49, opcodePushData}, 363 OP_DATA_49: {OP_DATA_49, "OP_DATA_49", 50, opcodePushData}, 364 OP_DATA_50: {OP_DATA_50, "OP_DATA_50", 51, opcodePushData}, 365 OP_DATA_51: {OP_DATA_51, "OP_DATA_51", 52, opcodePushData}, 366 OP_DATA_52: {OP_DATA_52, "OP_DATA_52", 53, opcodePushData}, 367 OP_DATA_53: {OP_DATA_53, "OP_DATA_53", 54, opcodePushData}, 368 OP_DATA_54: {OP_DATA_54, "OP_DATA_54", 55, opcodePushData}, 369 OP_DATA_55: {OP_DATA_55, "OP_DATA_55", 56, opcodePushData}, 370 OP_DATA_56: {OP_DATA_56, "OP_DATA_56", 57, opcodePushData}, 371 OP_DATA_57: {OP_DATA_57, "OP_DATA_57", 58, opcodePushData}, 372 OP_DATA_58: {OP_DATA_58, "OP_DATA_58", 59, opcodePushData}, 373 OP_DATA_59: {OP_DATA_59, "OP_DATA_59", 60, opcodePushData}, 374 OP_DATA_60: {OP_DATA_60, "OP_DATA_60", 61, opcodePushData}, 375 OP_DATA_61: {OP_DATA_61, "OP_DATA_61", 62, opcodePushData}, 376 OP_DATA_62: {OP_DATA_62, "OP_DATA_62", 63, opcodePushData}, 377 OP_DATA_63: {OP_DATA_63, "OP_DATA_63", 64, opcodePushData}, 378 OP_DATA_64: {OP_DATA_64, "OP_DATA_64", 65, opcodePushData}, 379 OP_DATA_65: {OP_DATA_65, "OP_DATA_65", 66, opcodePushData}, 380 OP_DATA_66: {OP_DATA_66, "OP_DATA_66", 67, opcodePushData}, 381 OP_DATA_67: {OP_DATA_67, "OP_DATA_67", 68, opcodePushData}, 382 OP_DATA_68: {OP_DATA_68, "OP_DATA_68", 69, opcodePushData}, 383 OP_DATA_69: {OP_DATA_69, "OP_DATA_69", 70, opcodePushData}, 384 OP_DATA_70: {OP_DATA_70, "OP_DATA_70", 71, opcodePushData}, 385 OP_DATA_71: {OP_DATA_71, "OP_DATA_71", 72, opcodePushData}, 386 OP_DATA_72: {OP_DATA_72, "OP_DATA_72", 73, opcodePushData}, 387 OP_DATA_73: {OP_DATA_73, "OP_DATA_73", 74, opcodePushData}, 388 OP_DATA_74: {OP_DATA_74, "OP_DATA_74", 75, opcodePushData}, 389 OP_DATA_75: {OP_DATA_75, "OP_DATA_75", 76, opcodePushData}, 390 OP_PUSHDATA1: {OP_PUSHDATA1, "OP_PUSHDATA1", -1, opcodePushData}, 391 OP_PUSHDATA2: {OP_PUSHDATA2, "OP_PUSHDATA2", -2, opcodePushData}, 392 OP_PUSHDATA4: {OP_PUSHDATA4, "OP_PUSHDATA4", -4, opcodePushData}, 393 OP_1NEGATE: {OP_1NEGATE, "OP_1NEGATE", 1, opcode1Negate}, 394 OP_RESERVED: {OP_RESERVED, "OP_RESERVED", 1, opcodeReserved}, 395 OP_TRUE: {OP_TRUE, "OP_1", 1, opcodeN}, 396 OP_2: {OP_2, "OP_2", 1, opcodeN}, 397 OP_3: {OP_3, "OP_3", 1, opcodeN}, 398 OP_4: {OP_4, "OP_4", 1, opcodeN}, 399 OP_5: {OP_5, "OP_5", 1, opcodeN}, 400 OP_6: {OP_6, "OP_6", 1, opcodeN}, 401 OP_7: {OP_7, "OP_7", 1, opcodeN}, 402 OP_8: {OP_8, "OP_8", 1, opcodeN}, 403 OP_9: {OP_9, "OP_9", 1, opcodeN}, 404 OP_10: {OP_10, "OP_10", 1, opcodeN}, 405 OP_11: {OP_11, "OP_11", 1, opcodeN}, 406 OP_12: {OP_12, "OP_12", 1, opcodeN}, 407 OP_13: {OP_13, "OP_13", 1, opcodeN}, 408 OP_14: {OP_14, "OP_14", 1, opcodeN}, 409 OP_15: {OP_15, "OP_15", 1, opcodeN}, 410 OP_16: {OP_16, "OP_16", 1, opcodeN}, 411 412 // Control opcodes. 413 OP_NOP: {OP_NOP, "OP_NOP", 1, opcodeNop}, 414 OP_VER: {OP_VER, "OP_VER", 1, opcodeReserved}, 415 OP_IF: {OP_IF, "OP_IF", 1, opcodeIf}, 416 OP_NOTIF: {OP_NOTIF, "OP_NOTIF", 1, opcodeNotIf}, 417 OP_VERIF: {OP_VERIF, "OP_VERIF", 1, opcodeReserved}, 418 OP_VERNOTIF: {OP_VERNOTIF, "OP_VERNOTIF", 1, opcodeReserved}, 419 OP_ELSE: {OP_ELSE, "OP_ELSE", 1, opcodeElse}, 420 OP_ENDIF: {OP_ENDIF, "OP_ENDIF", 1, opcodeEndif}, 421 OP_VERIFY: {OP_VERIFY, "OP_VERIFY", 1, opcodeVerify}, 422 OP_RETURN: {OP_RETURN, "OP_RETURN", 1, opcodeReturn}, 423 OP_CHECKLOCKTIMEVERIFY: {OP_CHECKLOCKTIMEVERIFY, "OP_CHECKLOCKTIMEVERIFY", 1, opcodeCheckLockTimeVerify}, 424 OP_CHECKSEQUENCEVERIFY: {OP_CHECKSEQUENCEVERIFY, "OP_CHECKSEQUENCEVERIFY", 1, opcodeCheckSequenceVerify}, 425 426 // Stack opcodes. 427 OP_TOALTSTACK: {OP_TOALTSTACK, "OP_TOALTSTACK", 1, opcodeToAltStack}, 428 OP_FROMALTSTACK: {OP_FROMALTSTACK, "OP_FROMALTSTACK", 1, opcodeFromAltStack}, 429 OP_2DROP: {OP_2DROP, "OP_2DROP", 1, opcode2Drop}, 430 OP_2DUP: {OP_2DUP, "OP_2DUP", 1, opcode2Dup}, 431 OP_3DUP: {OP_3DUP, "OP_3DUP", 1, opcode3Dup}, 432 OP_2OVER: {OP_2OVER, "OP_2OVER", 1, opcode2Over}, 433 OP_2ROT: {OP_2ROT, "OP_2ROT", 1, opcode2Rot}, 434 OP_2SWAP: {OP_2SWAP, "OP_2SWAP", 1, opcode2Swap}, 435 OP_IFDUP: {OP_IFDUP, "OP_IFDUP", 1, opcodeIfDup}, 436 OP_DEPTH: {OP_DEPTH, "OP_DEPTH", 1, opcodeDepth}, 437 OP_DROP: {OP_DROP, "OP_DROP", 1, opcodeDrop}, 438 OP_DUP: {OP_DUP, "OP_DUP", 1, opcodeDup}, 439 OP_NIP: {OP_NIP, "OP_NIP", 1, opcodeNip}, 440 OP_OVER: {OP_OVER, "OP_OVER", 1, opcodeOver}, 441 OP_PICK: {OP_PICK, "OP_PICK", 1, opcodePick}, 442 OP_ROLL: {OP_ROLL, "OP_ROLL", 1, opcodeRoll}, 443 OP_ROT: {OP_ROT, "OP_ROT", 1, opcodeRot}, 444 OP_SWAP: {OP_SWAP, "OP_SWAP", 1, opcodeSwap}, 445 OP_TUCK: {OP_TUCK, "OP_TUCK", 1, opcodeTuck}, 446 447 // Splice opcodes. 448 OP_CAT: {OP_CAT, "OP_CAT", 1, opcodeDisabled}, 449 OP_SUBSTR: {OP_SUBSTR, "OP_SUBSTR", 1, opcodeDisabled}, 450 OP_LEFT: {OP_LEFT, "OP_LEFT", 1, opcodeDisabled}, 451 OP_RIGHT: {OP_RIGHT, "OP_RIGHT", 1, opcodeDisabled}, 452 OP_SIZE: {OP_SIZE, "OP_SIZE", 1, opcodeSize}, 453 454 // Bitwise logic opcodes. 455 OP_INVERT: {OP_INVERT, "OP_INVERT", 1, opcodeDisabled}, 456 OP_AND: {OP_AND, "OP_AND", 1, opcodeDisabled}, 457 OP_OR: {OP_OR, "OP_OR", 1, opcodeDisabled}, 458 OP_XOR: {OP_XOR, "OP_XOR", 1, opcodeDisabled}, 459 OP_EQUAL: {OP_EQUAL, "OP_EQUAL", 1, opcodeEqual}, 460 OP_EQUALVERIFY: {OP_EQUALVERIFY, "OP_EQUALVERIFY", 1, opcodeEqualVerify}, 461 OP_RESERVED1: {OP_RESERVED1, "OP_RESERVED1", 1, opcodeReserved}, 462 OP_RESERVED2: {OP_RESERVED2, "OP_RESERVED2", 1, opcodeReserved}, 463 464 // Numeric related opcodes. 465 OP_1ADD: {OP_1ADD, "OP_1ADD", 1, opcode1Add}, 466 OP_1SUB: {OP_1SUB, "OP_1SUB", 1, opcode1Sub}, 467 OP_2MUL: {OP_2MUL, "OP_2MUL", 1, opcodeDisabled}, 468 OP_2DIV: {OP_2DIV, "OP_2DIV", 1, opcodeDisabled}, 469 OP_NEGATE: {OP_NEGATE, "OP_NEGATE", 1, opcodeNegate}, 470 OP_ABS: {OP_ABS, "OP_ABS", 1, opcodeAbs}, 471 OP_NOT: {OP_NOT, "OP_NOT", 1, opcodeNot}, 472 OP_0NOTEQUAL: {OP_0NOTEQUAL, "OP_0NOTEQUAL", 1, opcode0NotEqual}, 473 OP_ADD: {OP_ADD, "OP_ADD", 1, opcodeAdd}, 474 OP_SUB: {OP_SUB, "OP_SUB", 1, opcodeSub}, 475 OP_MUL: {OP_MUL, "OP_MUL", 1, opcodeDisabled}, 476 OP_DIV: {OP_DIV, "OP_DIV", 1, opcodeDisabled}, 477 OP_MOD: {OP_MOD, "OP_MOD", 1, opcodeDisabled}, 478 OP_LSHIFT: {OP_LSHIFT, "OP_LSHIFT", 1, opcodeDisabled}, 479 OP_RSHIFT: {OP_RSHIFT, "OP_RSHIFT", 1, opcodeDisabled}, 480 OP_BOOLAND: {OP_BOOLAND, "OP_BOOLAND", 1, opcodeBoolAnd}, 481 OP_BOOLOR: {OP_BOOLOR, "OP_BOOLOR", 1, opcodeBoolOr}, 482 OP_NUMEQUAL: {OP_NUMEQUAL, "OP_NUMEQUAL", 1, opcodeNumEqual}, 483 OP_NUMEQUALVERIFY: {OP_NUMEQUALVERIFY, "OP_NUMEQUALVERIFY", 1, opcodeNumEqualVerify}, 484 OP_NUMNOTEQUAL: {OP_NUMNOTEQUAL, "OP_NUMNOTEQUAL", 1, opcodeNumNotEqual}, 485 OP_LESSTHAN: {OP_LESSTHAN, "OP_LESSTHAN", 1, opcodeLessThan}, 486 OP_GREATERTHAN: {OP_GREATERTHAN, "OP_GREATERTHAN", 1, opcodeGreaterThan}, 487 OP_LESSTHANOREQUAL: {OP_LESSTHANOREQUAL, "OP_LESSTHANOREQUAL", 1, opcodeLessThanOrEqual}, 488 OP_GREATERTHANOREQUAL: {OP_GREATERTHANOREQUAL, "OP_GREATERTHANOREQUAL", 1, opcodeGreaterThanOrEqual}, 489 OP_MIN: {OP_MIN, "OP_MIN", 1, opcodeMin}, 490 OP_MAX: {OP_MAX, "OP_MAX", 1, opcodeMax}, 491 OP_WITHIN: {OP_WITHIN, "OP_WITHIN", 1, opcodeWithin}, 492 493 // Crypto opcodes. 494 OP_RIPEMD160: {OP_RIPEMD160, "OP_RIPEMD160", 1, opcodeRipemd160}, 495 OP_SHA1: {OP_SHA1, "OP_SHA1", 1, opcodeSha1}, 496 OP_SHA256: {OP_SHA256, "OP_SHA256", 1, opcodeSha256}, 497 OP_HASH160: {OP_HASH160, "OP_HASH160", 1, opcodeHash160}, 498 OP_HASH256: {OP_HASH256, "OP_HASH256", 1, opcodeHash256}, 499 OP_CODESEPARATOR: {OP_CODESEPARATOR, "OP_CODESEPARATOR", 1, opcodeCodeSeparator}, 500 OP_CHECKSIG: {OP_CHECKSIG, "OP_CHECKSIG", 1, opcodeCheckSig}, 501 OP_CHECKSIGVERIFY: {OP_CHECKSIGVERIFY, "OP_CHECKSIGVERIFY", 1, opcodeCheckSigVerify}, 502 OP_CHECKMULTISIG: {OP_CHECKMULTISIG, "OP_CHECKMULTISIG", 1, opcodeCheckMultiSig}, 503 OP_CHECKMULTISIGVERIFY: {OP_CHECKMULTISIGVERIFY, "OP_CHECKMULTISIGVERIFY", 1, opcodeCheckMultiSigVerify}, 504 OP_CHECKSIGADD: {OP_CHECKSIGADD, "OP_CHECKSIGADD", 1, opcodeCheckSigAdd}, 505 506 // Reserved opcodes. 507 OP_NOP1: {OP_NOP1, "OP_NOP1", 1, opcodeNop}, 508 OP_NOP4: {OP_NOP4, "OP_NOP4", 1, opcodeNop}, 509 OP_NOP5: {OP_NOP5, "OP_NOP5", 1, opcodeNop}, 510 OP_NOP6: {OP_NOP6, "OP_NOP6", 1, opcodeNop}, 511 OP_NOP7: {OP_NOP7, "OP_NOP7", 1, opcodeNop}, 512 OP_NOP8: {OP_NOP8, "OP_NOP8", 1, opcodeNop}, 513 OP_NOP9: {OP_NOP9, "OP_NOP9", 1, opcodeNop}, 514 OP_NOP10: {OP_NOP10, "OP_NOP10", 1, opcodeNop}, 515 516 // Undefined opcodes. 517 OP_UNKNOWN187: {OP_UNKNOWN187, "OP_UNKNOWN187", 1, opcodeInvalid}, 518 OP_UNKNOWN188: {OP_UNKNOWN188, "OP_UNKNOWN188", 1, opcodeInvalid}, 519 OP_UNKNOWN189: {OP_UNKNOWN189, "OP_UNKNOWN189", 1, opcodeInvalid}, 520 OP_UNKNOWN190: {OP_UNKNOWN190, "OP_UNKNOWN190", 1, opcodeInvalid}, 521 OP_UNKNOWN191: {OP_UNKNOWN191, "OP_UNKNOWN191", 1, opcodeInvalid}, 522 OP_UNKNOWN192: {OP_UNKNOWN192, "OP_UNKNOWN192", 1, opcodeInvalid}, 523 OP_UNKNOWN193: {OP_UNKNOWN193, "OP_UNKNOWN193", 1, opcodeInvalid}, 524 OP_UNKNOWN194: {OP_UNKNOWN194, "OP_UNKNOWN194", 1, opcodeInvalid}, 525 OP_UNKNOWN195: {OP_UNKNOWN195, "OP_UNKNOWN195", 1, opcodeInvalid}, 526 OP_UNKNOWN196: {OP_UNKNOWN196, "OP_UNKNOWN196", 1, opcodeInvalid}, 527 OP_UNKNOWN197: {OP_UNKNOWN197, "OP_UNKNOWN197", 1, opcodeInvalid}, 528 OP_UNKNOWN198: {OP_UNKNOWN198, "OP_UNKNOWN198", 1, opcodeInvalid}, 529 OP_UNKNOWN199: {OP_UNKNOWN199, "OP_UNKNOWN199", 1, opcodeInvalid}, 530 OP_UNKNOWN200: {OP_UNKNOWN200, "OP_UNKNOWN200", 1, opcodeInvalid}, 531 OP_UNKNOWN201: {OP_UNKNOWN201, "OP_UNKNOWN201", 1, opcodeInvalid}, 532 OP_UNKNOWN202: {OP_UNKNOWN202, "OP_UNKNOWN202", 1, opcodeInvalid}, 533 OP_UNKNOWN203: {OP_UNKNOWN203, "OP_UNKNOWN203", 1, opcodeInvalid}, 534 OP_UNKNOWN204: {OP_UNKNOWN204, "OP_UNKNOWN204", 1, opcodeInvalid}, 535 OP_UNKNOWN205: {OP_UNKNOWN205, "OP_UNKNOWN205", 1, opcodeInvalid}, 536 OP_UNKNOWN206: {OP_UNKNOWN206, "OP_UNKNOWN206", 1, opcodeInvalid}, 537 OP_UNKNOWN207: {OP_UNKNOWN207, "OP_UNKNOWN207", 1, opcodeInvalid}, 538 OP_UNKNOWN208: {OP_UNKNOWN208, "OP_UNKNOWN208", 1, opcodeInvalid}, 539 OP_UNKNOWN209: {OP_UNKNOWN209, "OP_UNKNOWN209", 1, opcodeInvalid}, 540 OP_UNKNOWN210: {OP_UNKNOWN210, "OP_UNKNOWN210", 1, opcodeInvalid}, 541 OP_UNKNOWN211: {OP_UNKNOWN211, "OP_UNKNOWN211", 1, opcodeInvalid}, 542 OP_UNKNOWN212: {OP_UNKNOWN212, "OP_UNKNOWN212", 1, opcodeInvalid}, 543 OP_UNKNOWN213: {OP_UNKNOWN213, "OP_UNKNOWN213", 1, opcodeInvalid}, 544 OP_UNKNOWN214: {OP_UNKNOWN214, "OP_UNKNOWN214", 1, opcodeInvalid}, 545 OP_UNKNOWN215: {OP_UNKNOWN215, "OP_UNKNOWN215", 1, opcodeInvalid}, 546 OP_UNKNOWN216: {OP_UNKNOWN216, "OP_UNKNOWN216", 1, opcodeInvalid}, 547 OP_UNKNOWN217: {OP_UNKNOWN217, "OP_UNKNOWN217", 1, opcodeInvalid}, 548 OP_UNKNOWN218: {OP_UNKNOWN218, "OP_UNKNOWN218", 1, opcodeInvalid}, 549 OP_UNKNOWN219: {OP_UNKNOWN219, "OP_UNKNOWN219", 1, opcodeInvalid}, 550 OP_UNKNOWN220: {OP_UNKNOWN220, "OP_UNKNOWN220", 1, opcodeInvalid}, 551 OP_UNKNOWN221: {OP_UNKNOWN221, "OP_UNKNOWN221", 1, opcodeInvalid}, 552 OP_UNKNOWN222: {OP_UNKNOWN222, "OP_UNKNOWN222", 1, opcodeInvalid}, 553 OP_UNKNOWN223: {OP_UNKNOWN223, "OP_UNKNOWN223", 1, opcodeInvalid}, 554 OP_UNKNOWN224: {OP_UNKNOWN224, "OP_UNKNOWN224", 1, opcodeInvalid}, 555 OP_UNKNOWN225: {OP_UNKNOWN225, "OP_UNKNOWN225", 1, opcodeInvalid}, 556 OP_UNKNOWN226: {OP_UNKNOWN226, "OP_UNKNOWN226", 1, opcodeInvalid}, 557 OP_UNKNOWN227: {OP_UNKNOWN227, "OP_UNKNOWN227", 1, opcodeInvalid}, 558 OP_UNKNOWN228: {OP_UNKNOWN228, "OP_UNKNOWN228", 1, opcodeInvalid}, 559 OP_UNKNOWN229: {OP_UNKNOWN229, "OP_UNKNOWN229", 1, opcodeInvalid}, 560 OP_UNKNOWN230: {OP_UNKNOWN230, "OP_UNKNOWN230", 1, opcodeInvalid}, 561 OP_UNKNOWN231: {OP_UNKNOWN231, "OP_UNKNOWN231", 1, opcodeInvalid}, 562 OP_UNKNOWN232: {OP_UNKNOWN232, "OP_UNKNOWN232", 1, opcodeInvalid}, 563 OP_UNKNOWN233: {OP_UNKNOWN233, "OP_UNKNOWN233", 1, opcodeInvalid}, 564 OP_UNKNOWN234: {OP_UNKNOWN234, "OP_UNKNOWN234", 1, opcodeInvalid}, 565 OP_UNKNOWN235: {OP_UNKNOWN235, "OP_UNKNOWN235", 1, opcodeInvalid}, 566 OP_UNKNOWN236: {OP_UNKNOWN236, "OP_UNKNOWN236", 1, opcodeInvalid}, 567 OP_UNKNOWN237: {OP_UNKNOWN237, "OP_UNKNOWN237", 1, opcodeInvalid}, 568 OP_UNKNOWN238: {OP_UNKNOWN238, "OP_UNKNOWN238", 1, opcodeInvalid}, 569 OP_UNKNOWN239: {OP_UNKNOWN239, "OP_UNKNOWN239", 1, opcodeInvalid}, 570 OP_UNKNOWN240: {OP_UNKNOWN240, "OP_UNKNOWN240", 1, opcodeInvalid}, 571 OP_UNKNOWN241: {OP_UNKNOWN241, "OP_UNKNOWN241", 1, opcodeInvalid}, 572 OP_UNKNOWN242: {OP_UNKNOWN242, "OP_UNKNOWN242", 1, opcodeInvalid}, 573 OP_UNKNOWN243: {OP_UNKNOWN243, "OP_UNKNOWN243", 1, opcodeInvalid}, 574 OP_UNKNOWN244: {OP_UNKNOWN244, "OP_UNKNOWN244", 1, opcodeInvalid}, 575 OP_UNKNOWN245: {OP_UNKNOWN245, "OP_UNKNOWN245", 1, opcodeInvalid}, 576 OP_UNKNOWN246: {OP_UNKNOWN246, "OP_UNKNOWN246", 1, opcodeInvalid}, 577 OP_UNKNOWN247: {OP_UNKNOWN247, "OP_UNKNOWN247", 1, opcodeInvalid}, 578 OP_UNKNOWN248: {OP_UNKNOWN248, "OP_UNKNOWN248", 1, opcodeInvalid}, 579 OP_UNKNOWN249: {OP_UNKNOWN249, "OP_UNKNOWN249", 1, opcodeInvalid}, 580 581 // Bitcoin Core internal use opcode. Defined here for completeness. 582 OP_SMALLINTEGER: {OP_SMALLINTEGER, "OP_SMALLINTEGER", 1, opcodeInvalid}, 583 OP_PUBKEYS: {OP_PUBKEYS, "OP_PUBKEYS", 1, opcodeInvalid}, 584 OP_UNKNOWN252: {OP_UNKNOWN252, "OP_UNKNOWN252", 1, opcodeInvalid}, 585 OP_PUBKEYHASH: {OP_PUBKEYHASH, "OP_PUBKEYHASH", 1, opcodeInvalid}, 586 OP_PUBKEY: {OP_PUBKEY, "OP_PUBKEY", 1, opcodeInvalid}, 587 588 OP_INVALIDOPCODE: {OP_INVALIDOPCODE, "OP_INVALIDOPCODE", 1, opcodeInvalid}, 589 } 590 591 // opcodeOnelineRepls defines opcode names which are replaced when doing a 592 // one-line disassembly. This is done to match the output of the reference 593 // implementation while not changing the opcode names in the nicer full 594 // disassembly. 595 var opcodeOnelineRepls = map[string]string{ 596 "OP_1NEGATE": "-1", 597 "OP_0": "0", 598 "OP_1": "1", 599 "OP_2": "2", 600 "OP_3": "3", 601 "OP_4": "4", 602 "OP_5": "5", 603 "OP_6": "6", 604 "OP_7": "7", 605 "OP_8": "8", 606 "OP_9": "9", 607 "OP_10": "10", 608 "OP_11": "11", 609 "OP_12": "12", 610 "OP_13": "13", 611 "OP_14": "14", 612 "OP_15": "15", 613 "OP_16": "16", 614 } 615 616 // successOpcodes tracks the set of op codes that are to be interpreted as op 617 // codes that cause execution to automatically succeed. This map is used to 618 // quickly look up the op codes during script pre-processing. 619 var successOpcodes = map[byte]struct{}{ 620 OP_RESERVED: {}, // 80 621 OP_VER: {}, // 98 622 OP_CAT: {}, // 126 623 OP_SUBSTR: {}, // 127 624 OP_LEFT: {}, // 128 625 OP_RIGHT: {}, // 129 626 OP_INVERT: {}, // 131 627 OP_AND: {}, // 132 628 OP_OR: {}, // 133 629 OP_XOR: {}, // 134 630 OP_RESERVED1: {}, // 137 631 OP_RESERVED2: {}, // 138 632 OP_2MUL: {}, // 141 633 OP_2DIV: {}, // 142 634 OP_MUL: {}, // 149 635 OP_DIV: {}, // 150 636 OP_MOD: {}, // 151 637 OP_LSHIFT: {}, // 152 638 OP_RSHIFT: {}, // 153 639 OP_UNKNOWN187: {}, // 187 640 OP_UNKNOWN188: {}, // 188 641 OP_UNKNOWN189: {}, // 189 642 OP_UNKNOWN190: {}, // 190 643 OP_UNKNOWN191: {}, // 191 644 OP_UNKNOWN192: {}, // 192 645 OP_UNKNOWN193: {}, // 193 646 OP_UNKNOWN194: {}, // 194 647 OP_UNKNOWN195: {}, // 195 648 OP_UNKNOWN196: {}, // 196 649 OP_UNKNOWN197: {}, // 197 650 OP_UNKNOWN198: {}, // 198 651 OP_UNKNOWN199: {}, // 199 652 OP_UNKNOWN200: {}, // 200 653 OP_UNKNOWN201: {}, // 201 654 OP_UNKNOWN202: {}, // 202 655 OP_UNKNOWN203: {}, // 203 656 OP_UNKNOWN204: {}, // 204 657 OP_UNKNOWN205: {}, // 205 658 OP_UNKNOWN206: {}, // 206 659 OP_UNKNOWN207: {}, // 207 660 OP_UNKNOWN208: {}, // 208 661 OP_UNKNOWN209: {}, // 209 662 OP_UNKNOWN210: {}, // 210 663 OP_UNKNOWN211: {}, // 211 664 OP_UNKNOWN212: {}, // 212 665 OP_UNKNOWN213: {}, // 213 666 OP_UNKNOWN214: {}, // 214 667 OP_UNKNOWN215: {}, // 215 668 OP_UNKNOWN216: {}, // 216 669 OP_UNKNOWN217: {}, // 217 670 OP_UNKNOWN218: {}, // 218 671 OP_UNKNOWN219: {}, // 219 672 OP_UNKNOWN220: {}, // 220 673 OP_UNKNOWN221: {}, // 221 674 OP_UNKNOWN222: {}, // 222 675 OP_UNKNOWN223: {}, // 223 676 OP_UNKNOWN224: {}, // 224 677 OP_UNKNOWN225: {}, // 225 678 OP_UNKNOWN226: {}, // 226 679 OP_UNKNOWN227: {}, // 227 680 OP_UNKNOWN228: {}, // 228 681 OP_UNKNOWN229: {}, // 229 682 OP_UNKNOWN230: {}, // 230 683 OP_UNKNOWN231: {}, // 231 684 OP_UNKNOWN232: {}, // 232 685 OP_UNKNOWN233: {}, // 233 686 OP_UNKNOWN234: {}, // 234 687 OP_UNKNOWN235: {}, // 235 688 OP_UNKNOWN236: {}, // 236 689 OP_UNKNOWN237: {}, // 237 690 OP_UNKNOWN238: {}, // 238 691 OP_UNKNOWN239: {}, // 239 692 OP_UNKNOWN240: {}, // 240 693 OP_UNKNOWN241: {}, // 241 694 OP_UNKNOWN242: {}, // 242 695 OP_UNKNOWN243: {}, // 243 696 OP_UNKNOWN244: {}, // 244 697 OP_UNKNOWN245: {}, // 245 698 OP_UNKNOWN246: {}, // 246 699 OP_UNKNOWN247: {}, // 247 700 OP_UNKNOWN248: {}, // 248 701 OP_UNKNOWN249: {}, // 249 702 OP_SMALLINTEGER: {}, // 250 703 OP_PUBKEYS: {}, // 251 704 OP_UNKNOWN252: {}, // 252 705 OP_PUBKEYHASH: {}, // 253 706 OP_PUBKEY: {}, // 254 707 } 708 709 // disasmOpcode writes a human-readable disassembly of the provided opcode and 710 // data into the provided buffer. The compact flag indicates the disassembly 711 // should print a more compact representation of data-carrying and small integer 712 // opcodes. For example, OP_0 through OP_16 are replaced with the numeric value 713 // and data pushes are printed as only the hex representation of the data as 714 // opposed to including the opcode that specifies the amount of data to push as 715 // well. 716 func disasmOpcode(buf *strings.Builder, op *opcode, data []byte, compact bool) { 717 // Replace opcode which represent values (e.g. OP_0 through OP_16 and 718 // OP_1NEGATE) with the raw value when performing a compact disassembly. 719 opcodeName := op.name 720 if compact { 721 if replName, ok := opcodeOnelineRepls[opcodeName]; ok { 722 opcodeName = replName 723 } 724 725 // Either write the human-readable opcode or the parsed data in hex for 726 // data-carrying opcodes. 727 switch { 728 case op.length == 1: 729 buf.WriteString(opcodeName) 730 731 default: 732 buf.WriteString(hex.EncodeToString(data)) 733 } 734 735 return 736 } 737 738 buf.WriteString(opcodeName) 739 740 switch op.length { 741 // Only write the opcode name for non-data push opcodes. 742 case 1: 743 return 744 745 // Add length for the OP_PUSHDATA# opcodes. 746 case -1: 747 buf.WriteString(fmt.Sprintf(" 0x%02x", len(data))) 748 case -2: 749 buf.WriteString(fmt.Sprintf(" 0x%04x", len(data))) 750 case -4: 751 buf.WriteString(fmt.Sprintf(" 0x%08x", len(data))) 752 } 753 754 buf.WriteString(fmt.Sprintf(" 0x%02x", data)) 755 } 756 757 // ******************************************* 758 // Opcode implementation functions start here. 759 // ******************************************* 760 761 // opcodeDisabled is a common handler for disabled opcodes. It returns an 762 // appropriate error indicating the opcode is disabled. While it would 763 // ordinarily make more sense to detect if the script contains any disabled 764 // opcodes before executing in an initial parse step, the consensus rules 765 // dictate the script doesn't fail until the program counter passes over a 766 // disabled opcode (even when they appear in a branch that is not executed). 767 func opcodeDisabled(op *opcode, data []byte, vm *Engine) error { 768 str := fmt.Sprintf("attempt to execute disabled opcode %s", op.name) 769 return scriptError(ErrDisabledOpcode, str) 770 } 771 772 // opcodeReserved is a common handler for all reserved opcodes. It returns an 773 // appropriate error indicating the opcode is reserved. 774 func opcodeReserved(op *opcode, data []byte, vm *Engine) error { 775 str := fmt.Sprintf("attempt to execute reserved opcode %s", op.name) 776 return scriptError(ErrReservedOpcode, str) 777 } 778 779 // opcodeInvalid is a common handler for all invalid opcodes. It returns an 780 // appropriate error indicating the opcode is invalid. 781 func opcodeInvalid(op *opcode, data []byte, vm *Engine) error { 782 str := fmt.Sprintf("attempt to execute invalid opcode %s", op.name) 783 return scriptError(ErrReservedOpcode, str) 784 } 785 786 // opcodeFalse pushes an empty array to the data stack to represent false. Note 787 // that 0, when encoded as a number according to the numeric encoding consensus 788 // rules, is an empty array. 789 func opcodeFalse(op *opcode, data []byte, vm *Engine) error { 790 vm.dstack.PushByteArray(nil) 791 return nil 792 } 793 794 // opcodePushData is a common handler for the vast majority of opcodes that push 795 // raw data (bytes) to the data stack. 796 func opcodePushData(op *opcode, data []byte, vm *Engine) error { 797 vm.dstack.PushByteArray(data) 798 return nil 799 } 800 801 // opcode1Negate pushes -1, encoded as a number, to the data stack. 802 func opcode1Negate(op *opcode, data []byte, vm *Engine) error { 803 vm.dstack.PushInt(scriptNum(-1)) 804 return nil 805 } 806 807 // opcodeN is a common handler for the small integer data push opcodes. It 808 // pushes the numeric value the opcode represents (which will be from 1 to 16) 809 // onto the data stack. 810 func opcodeN(op *opcode, data []byte, vm *Engine) error { 811 // The opcodes are all defined consecutively, so the numeric value is 812 // the difference. 813 vm.dstack.PushInt(scriptNum((op.value - (OP_1 - 1)))) 814 return nil 815 } 816 817 // opcodeNop is a common handler for the NOP family of opcodes. As the name 818 // implies it generally does nothing, however, it will return an error when 819 // the flag to discourage use of NOPs is set for select opcodes. 820 func opcodeNop(op *opcode, data []byte, vm *Engine) error { 821 switch op.value { 822 case OP_NOP1, OP_NOP4, OP_NOP5, 823 OP_NOP6, OP_NOP7, OP_NOP8, OP_NOP9, OP_NOP10: 824 825 if vm.hasFlag(ScriptDiscourageUpgradableNops) { 826 str := fmt.Sprintf("%v reserved for soft-fork "+ 827 "upgrades", op.name) 828 return scriptError(ErrDiscourageUpgradableNOPs, str) 829 } 830 } 831 return nil 832 } 833 834 // popIfBool enforces the "minimal if" policy during script execution if the 835 // particular flag is set. If so, in order to eliminate an additional source 836 // of nuisance malleability, post-segwit for version 0 witness programs, we now 837 // require the following: for OP_IF and OP_NOT_IF, the top stack item MUST 838 // either be an empty byte slice, or [0x01]. Otherwise, the item at the top of 839 // the stack will be popped and interpreted as a boolean. 840 func popIfBool(vm *Engine) (bool, error) { 841 // When not in witness execution mode, not executing a v0 witness 842 // program, or not doing tapscript execution, or the minimal if flag 843 // isn't set pop the top stack item as a normal bool. 844 switch { 845 // Minimal if is always on for taproot execution. 846 case vm.isWitnessVersionActive(TaprootWitnessVersion): 847 break 848 849 // If this isn't the base segwit version, then we'll coerce the stack 850 // element as a bool as normal. 851 case !vm.isWitnessVersionActive(BaseSegwitWitnessVersion): 852 fallthrough 853 854 // If the minimal if flag isn't set, then we don't need any extra 855 // checks here. 856 case !vm.hasFlag(ScriptVerifyMinimalIf): 857 return vm.dstack.PopBool() 858 } 859 860 // At this point, a v0 or v1 witness program is being executed and the 861 // minimal if flag is set, so enforce additional constraints on the top 862 // stack item. 863 so, err := vm.dstack.PopByteArray() 864 if err != nil { 865 return false, err 866 } 867 868 // The top element MUST have a length of at least one. 869 if len(so) > 1 { 870 str := fmt.Sprintf("minimal if is active, top element MUST "+ 871 "have a length of at least, instead length is %v", 872 len(so)) 873 return false, scriptError(ErrMinimalIf, str) 874 } 875 876 // Additionally, if the length is one, then the value MUST be 0x01. 877 if len(so) == 1 && so[0] != 0x01 { 878 str := fmt.Sprintf("minimal if is active, top stack item MUST "+ 879 "be an empty byte array or 0x01, is instead: %v", 880 so[0]) 881 return false, scriptError(ErrMinimalIf, str) 882 } 883 884 return asBool(so), nil 885 } 886 887 // opcodeIf treats the top item on the data stack as a boolean and removes it. 888 // 889 // An appropriate entry is added to the conditional stack depending on whether 890 // the boolean is true and whether this if is on an executing branch in order 891 // to allow proper execution of further opcodes depending on the conditional 892 // logic. When the boolean is true, the first branch will be executed (unless 893 // this opcode is nested in a non-executed branch). 894 // 895 // <expression> if [statements] [else [statements]] endif 896 // 897 // Note that, unlike for all non-conditional opcodes, this is executed even when 898 // it is on a non-executing branch so proper nesting is maintained. 899 // 900 // Data stack transformation: [... bool] -> [...] 901 // Conditional stack transformation: [...] -> [... OpCondValue] 902 func opcodeIf(op *opcode, data []byte, vm *Engine) error { 903 condVal := OpCondFalse 904 if vm.isBranchExecuting() { 905 ok, err := popIfBool(vm) 906 if err != nil { 907 return err 908 } 909 910 if ok { 911 condVal = OpCondTrue 912 } 913 } else { 914 condVal = OpCondSkip 915 } 916 vm.condStack = append(vm.condStack, condVal) 917 return nil 918 } 919 920 // opcodeNotIf treats the top item on the data stack as a boolean and removes 921 // it. 922 // 923 // An appropriate entry is added to the conditional stack depending on whether 924 // the boolean is true and whether this if is on an executing branch in order 925 // to allow proper execution of further opcodes depending on the conditional 926 // logic. When the boolean is false, the first branch will be executed (unless 927 // this opcode is nested in a non-executed branch). 928 // 929 // <expression> notif [statements] [else [statements]] endif 930 // 931 // Note that, unlike for all non-conditional opcodes, this is executed even when 932 // it is on a non-executing branch so proper nesting is maintained. 933 // 934 // Data stack transformation: [... bool] -> [...] 935 // Conditional stack transformation: [...] -> [... OpCondValue] 936 func opcodeNotIf(op *opcode, data []byte, vm *Engine) error { 937 condVal := OpCondFalse 938 if vm.isBranchExecuting() { 939 ok, err := popIfBool(vm) 940 if err != nil { 941 return err 942 } 943 944 if !ok { 945 condVal = OpCondTrue 946 } 947 } else { 948 condVal = OpCondSkip 949 } 950 vm.condStack = append(vm.condStack, condVal) 951 return nil 952 } 953 954 // opcodeElse inverts conditional execution for other half of if/else/endif. 955 // 956 // An error is returned if there has not already been a matching OP_IF. 957 // 958 // Conditional stack transformation: [... OpCondValue] -> [... !OpCondValue] 959 func opcodeElse(op *opcode, data []byte, vm *Engine) error { 960 if len(vm.condStack) == 0 { 961 str := fmt.Sprintf("encountered opcode %s with no matching "+ 962 "opcode to begin conditional execution", op.name) 963 return scriptError(ErrUnbalancedConditional, str) 964 } 965 966 conditionalIdx := len(vm.condStack) - 1 967 switch vm.condStack[conditionalIdx] { 968 case OpCondTrue: 969 vm.condStack[conditionalIdx] = OpCondFalse 970 case OpCondFalse: 971 vm.condStack[conditionalIdx] = OpCondTrue 972 case OpCondSkip: 973 // Value doesn't change in skip since it indicates this opcode 974 // is nested in a non-executed branch. 975 } 976 return nil 977 } 978 979 // opcodeEndif terminates a conditional block, removing the value from the 980 // conditional execution stack. 981 // 982 // An error is returned if there has not already been a matching OP_IF. 983 // 984 // Conditional stack transformation: [... OpCondValue] -> [...] 985 func opcodeEndif(op *opcode, data []byte, vm *Engine) error { 986 if len(vm.condStack) == 0 { 987 str := fmt.Sprintf("encountered opcode %s with no matching "+ 988 "opcode to begin conditional execution", op.name) 989 return scriptError(ErrUnbalancedConditional, str) 990 } 991 992 vm.condStack = vm.condStack[:len(vm.condStack)-1] 993 return nil 994 } 995 996 // abstractVerify examines the top item on the data stack as a boolean value and 997 // verifies it evaluates to true. An error is returned either when there is no 998 // item on the stack or when that item evaluates to false. In the latter case 999 // where the verification fails specifically due to the top item evaluating 1000 // to false, the returned error will use the passed error code. 1001 func abstractVerify(op *opcode, vm *Engine, c ErrorCode) error { 1002 verified, err := vm.dstack.PopBool() 1003 if err != nil { 1004 return err 1005 } 1006 1007 if !verified { 1008 str := fmt.Sprintf("%s failed", op.name) 1009 return scriptError(c, str) 1010 } 1011 return nil 1012 } 1013 1014 // opcodeVerify examines the top item on the data stack as a boolean value and 1015 // verifies it evaluates to true. An error is returned if it does not. 1016 func opcodeVerify(op *opcode, data []byte, vm *Engine) error { 1017 return abstractVerify(op, vm, ErrVerify) 1018 } 1019 1020 // opcodeReturn returns an appropriate error since it is always an error to 1021 // return early from a script. 1022 func opcodeReturn(op *opcode, data []byte, vm *Engine) error { 1023 return scriptError(ErrEarlyReturn, "script returned early") 1024 } 1025 1026 // verifyLockTime is a helper function used to validate locktimes. 1027 func verifyLockTime(txLockTime, threshold, lockTime int64) error { 1028 // The lockTimes in both the script and transaction must be of the same 1029 // type. 1030 if !((txLockTime < threshold && lockTime < threshold) || 1031 (txLockTime >= threshold && lockTime >= threshold)) { 1032 str := fmt.Sprintf("mismatched locktime types -- tx locktime "+ 1033 "%d, stack locktime %d", txLockTime, lockTime) 1034 return scriptError(ErrUnsatisfiedLockTime, str) 1035 } 1036 1037 if lockTime > txLockTime { 1038 str := fmt.Sprintf("locktime requirement not satisfied -- "+ 1039 "locktime is greater than the transaction locktime: "+ 1040 "%d > %d", lockTime, txLockTime) 1041 return scriptError(ErrUnsatisfiedLockTime, str) 1042 } 1043 1044 return nil 1045 } 1046 1047 // opcodeCheckLockTimeVerify compares the top item on the data stack to the 1048 // LockTime field of the transaction containing the script signature 1049 // validating if the transaction outputs are spendable yet. If flag 1050 // ScriptVerifyCheckLockTimeVerify is not set, the code continues as if OP_NOP2 1051 // were executed. 1052 func opcodeCheckLockTimeVerify(op *opcode, data []byte, vm *Engine) error { 1053 // If the ScriptVerifyCheckLockTimeVerify script flag is not set, treat 1054 // opcode as OP_NOP2 instead. 1055 if !vm.hasFlag(ScriptVerifyCheckLockTimeVerify) { 1056 if vm.hasFlag(ScriptDiscourageUpgradableNops) { 1057 return scriptError(ErrDiscourageUpgradableNOPs, 1058 "OP_NOP2 reserved for soft-fork upgrades") 1059 } 1060 return nil 1061 } 1062 1063 // The current transaction locktime is a uint32 resulting in a maximum 1064 // locktime of 2^32-1 (the year 2106). However, scriptNums are signed 1065 // and therefore a standard 4-byte scriptNum would only support up to a 1066 // maximum of 2^31-1 (the year 2038). Thus, a 5-byte scriptNum is used 1067 // here since it will support up to 2^39-1 which allows dates beyond the 1068 // current locktime limit. 1069 // 1070 // PeekByteArray is used here instead of PeekInt because we do not want 1071 // to be limited to a 4-byte integer for reasons specified above. 1072 so, err := vm.dstack.PeekByteArray(0) 1073 if err != nil { 1074 return err 1075 } 1076 lockTime, err := MakeScriptNum(so, vm.dstack.verifyMinimalData, 5) 1077 if err != nil { 1078 return err 1079 } 1080 1081 // In the rare event that the argument needs to be < 0 due to some 1082 // arithmetic being done first, you can always use 1083 // 0 OP_MAX OP_CHECKLOCKTIMEVERIFY. 1084 if lockTime < 0 { 1085 str := fmt.Sprintf("negative lock time: %d", lockTime) 1086 return scriptError(ErrNegativeLockTime, str) 1087 } 1088 1089 // The lock time field of a transaction is either a block height at 1090 // which the transaction is finalized or a timestamp depending on if the 1091 // value is before the txscript.LockTimeThreshold. When it is under the 1092 // threshold it is a block height. 1093 err = verifyLockTime(int64(vm.tx.LockTime), LockTimeThreshold, 1094 int64(lockTime)) 1095 if err != nil { 1096 return err 1097 } 1098 1099 // The lock time feature can also be disabled, thereby bypassing 1100 // OP_CHECKLOCKTIMEVERIFY, if every transaction input has been finalized by 1101 // setting its sequence to the maximum value (wire.MaxTxInSequenceNum). This 1102 // condition would result in the transaction being allowed into the blockchain 1103 // making the opcode ineffective. 1104 // 1105 // This condition is prevented by enforcing that the input being used by 1106 // the opcode is unlocked (its sequence number is less than the max 1107 // value). This is sufficient to prove correctness without having to 1108 // check every input. 1109 // 1110 // NOTE: This implies that even if the transaction is not finalized due to 1111 // another input being unlocked, the opcode execution will still fail when the 1112 // input being used by the opcode is locked. 1113 if vm.tx.TxIn[vm.txIdx].Sequence == wire.MaxTxInSequenceNum { 1114 return scriptError(ErrUnsatisfiedLockTime, 1115 "transaction input is finalized") 1116 } 1117 1118 return nil 1119 } 1120 1121 // opcodeCheckSequenceVerify compares the top item on the data stack to the 1122 // LockTime field of the transaction containing the script signature 1123 // validating if the transaction outputs are spendable yet. If flag 1124 // ScriptVerifyCheckSequenceVerify is not set, the code continues as if OP_NOP3 1125 // were executed. 1126 func opcodeCheckSequenceVerify(op *opcode, data []byte, vm *Engine) error { 1127 // If the ScriptVerifyCheckSequenceVerify script flag is not set, treat 1128 // opcode as OP_NOP3 instead. 1129 if !vm.hasFlag(ScriptVerifyCheckSequenceVerify) { 1130 if vm.hasFlag(ScriptDiscourageUpgradableNops) { 1131 return scriptError(ErrDiscourageUpgradableNOPs, 1132 "OP_NOP3 reserved for soft-fork upgrades") 1133 } 1134 return nil 1135 } 1136 1137 // The current transaction sequence is a uint32 resulting in a maximum 1138 // sequence of 2^32-1. However, scriptNums are signed and therefore a 1139 // standard 4-byte scriptNum would only support up to a maximum of 1140 // 2^31-1. Thus, a 5-byte scriptNum is used here since it will support 1141 // up to 2^39-1 which allows sequences beyond the current sequence 1142 // limit. 1143 // 1144 // PeekByteArray is used here instead of PeekInt because we do not want 1145 // to be limited to a 4-byte integer for reasons specified above. 1146 so, err := vm.dstack.PeekByteArray(0) 1147 if err != nil { 1148 return err 1149 } 1150 stackSequence, err := MakeScriptNum(so, vm.dstack.verifyMinimalData, 5) 1151 if err != nil { 1152 return err 1153 } 1154 1155 // In the rare event that the argument needs to be < 0 due to some 1156 // arithmetic being done first, you can always use 1157 // 0 OP_MAX OP_CHECKSEQUENCEVERIFY. 1158 if stackSequence < 0 { 1159 str := fmt.Sprintf("negative sequence: %d", stackSequence) 1160 return scriptError(ErrNegativeLockTime, str) 1161 } 1162 1163 sequence := int64(stackSequence) 1164 1165 // To provide for future soft-fork extensibility, if the 1166 // operand has the disabled lock-time flag set, 1167 // CHECKSEQUENCEVERIFY behaves as a NOP. 1168 if sequence&int64(wire.SequenceLockTimeDisabled) != 0 { 1169 return nil 1170 } 1171 1172 // Transaction version numbers not high enough to trigger CSV rules must 1173 // fail. 1174 if uint32(vm.tx.Version) < 2 { 1175 str := fmt.Sprintf("invalid transaction version: %d", 1176 vm.tx.Version) 1177 return scriptError(ErrUnsatisfiedLockTime, str) 1178 } 1179 1180 // Sequence numbers with their most significant bit set are not 1181 // consensus constrained. Testing that the transaction's sequence 1182 // number does not have this bit set prevents using this property 1183 // to get around a CHECKSEQUENCEVERIFY check. 1184 txSequence := int64(vm.tx.TxIn[vm.txIdx].Sequence) 1185 if txSequence&int64(wire.SequenceLockTimeDisabled) != 0 { 1186 str := fmt.Sprintf("transaction sequence has sequence "+ 1187 "locktime disabled bit set: 0x%x", txSequence) 1188 return scriptError(ErrUnsatisfiedLockTime, str) 1189 } 1190 1191 // Mask off non-consensus bits before doing comparisons. 1192 lockTimeMask := int64(wire.SequenceLockTimeIsSeconds | 1193 wire.SequenceLockTimeMask) 1194 return verifyLockTime(txSequence&lockTimeMask, 1195 wire.SequenceLockTimeIsSeconds, sequence&lockTimeMask) 1196 } 1197 1198 // opcodeToAltStack removes the top item from the main data stack and pushes it 1199 // onto the alternate data stack. 1200 // 1201 // Main data stack transformation: [... x1 x2 x3] -> [... x1 x2] 1202 // Alt data stack transformation: [... y1 y2 y3] -> [... y1 y2 y3 x3] 1203 func opcodeToAltStack(op *opcode, data []byte, vm *Engine) error { 1204 so, err := vm.dstack.PopByteArray() 1205 if err != nil { 1206 return err 1207 } 1208 vm.astack.PushByteArray(so) 1209 1210 return nil 1211 } 1212 1213 // opcodeFromAltStack removes the top item from the alternate data stack and 1214 // pushes it onto the main data stack. 1215 // 1216 // Main data stack transformation: [... x1 x2 x3] -> [... x1 x2 x3 y3] 1217 // Alt data stack transformation: [... y1 y2 y3] -> [... y1 y2] 1218 func opcodeFromAltStack(op *opcode, data []byte, vm *Engine) error { 1219 so, err := vm.astack.PopByteArray() 1220 if err != nil { 1221 return err 1222 } 1223 vm.dstack.PushByteArray(so) 1224 1225 return nil 1226 } 1227 1228 // opcode2Drop removes the top 2 items from the data stack. 1229 // 1230 // Stack transformation: [... x1 x2 x3] -> [... x1] 1231 func opcode2Drop(op *opcode, data []byte, vm *Engine) error { 1232 return vm.dstack.DropN(2) 1233 } 1234 1235 // opcode2Dup duplicates the top 2 items on the data stack. 1236 // 1237 // Stack transformation: [... x1 x2 x3] -> [... x1 x2 x3 x2 x3] 1238 func opcode2Dup(op *opcode, data []byte, vm *Engine) error { 1239 return vm.dstack.DupN(2) 1240 } 1241 1242 // opcode3Dup duplicates the top 3 items on the data stack. 1243 // 1244 // Stack transformation: [... x1 x2 x3] -> [... x1 x2 x3 x1 x2 x3] 1245 func opcode3Dup(op *opcode, data []byte, vm *Engine) error { 1246 return vm.dstack.DupN(3) 1247 } 1248 1249 // opcode2Over duplicates the 2 items before the top 2 items on the data stack. 1250 // 1251 // Stack transformation: [... x1 x2 x3 x4] -> [... x1 x2 x3 x4 x1 x2] 1252 func opcode2Over(op *opcode, data []byte, vm *Engine) error { 1253 return vm.dstack.OverN(2) 1254 } 1255 1256 // opcode2Rot rotates the top 6 items on the data stack to the left twice. 1257 // 1258 // Stack transformation: [... x1 x2 x3 x4 x5 x6] -> [... x3 x4 x5 x6 x1 x2] 1259 func opcode2Rot(op *opcode, data []byte, vm *Engine) error { 1260 return vm.dstack.RotN(2) 1261 } 1262 1263 // opcode2Swap swaps the top 2 items on the data stack with the 2 that come 1264 // before them. 1265 // 1266 // Stack transformation: [... x1 x2 x3 x4] -> [... x3 x4 x1 x2] 1267 func opcode2Swap(op *opcode, data []byte, vm *Engine) error { 1268 return vm.dstack.SwapN(2) 1269 } 1270 1271 // opcodeIfDup duplicates the top item of the stack if it is not zero. 1272 // 1273 // Stack transformation (x1==0): [... x1] -> [... x1] 1274 // Stack transformation (x1!=0): [... x1] -> [... x1 x1] 1275 func opcodeIfDup(op *opcode, data []byte, vm *Engine) error { 1276 so, err := vm.dstack.PeekByteArray(0) 1277 if err != nil { 1278 return err 1279 } 1280 1281 // Push copy of data iff it isn't zero 1282 if asBool(so) { 1283 vm.dstack.PushByteArray(so) 1284 } 1285 1286 return nil 1287 } 1288 1289 // opcodeDepth pushes the depth of the data stack prior to executing this 1290 // opcode, encoded as a number, onto the data stack. 1291 // 1292 // Stack transformation: [...] -> [... <num of items on the stack>] 1293 // Example with 2 items: [x1 x2] -> [x1 x2 2] 1294 // Example with 3 items: [x1 x2 x3] -> [x1 x2 x3 3] 1295 func opcodeDepth(op *opcode, data []byte, vm *Engine) error { 1296 vm.dstack.PushInt(scriptNum(vm.dstack.Depth())) 1297 return nil 1298 } 1299 1300 // opcodeDrop removes the top item from the data stack. 1301 // 1302 // Stack transformation: [... x1 x2 x3] -> [... x1 x2] 1303 func opcodeDrop(op *opcode, data []byte, vm *Engine) error { 1304 return vm.dstack.DropN(1) 1305 } 1306 1307 // opcodeDup duplicates the top item on the data stack. 1308 // 1309 // Stack transformation: [... x1 x2 x3] -> [... x1 x2 x3 x3] 1310 func opcodeDup(op *opcode, data []byte, vm *Engine) error { 1311 return vm.dstack.DupN(1) 1312 } 1313 1314 // opcodeNip removes the item before the top item on the data stack. 1315 // 1316 // Stack transformation: [... x1 x2 x3] -> [... x1 x3] 1317 func opcodeNip(op *opcode, data []byte, vm *Engine) error { 1318 return vm.dstack.NipN(1) 1319 } 1320 1321 // opcodeOver duplicates the item before the top item on the data stack. 1322 // 1323 // Stack transformation: [... x1 x2 x3] -> [... x1 x2 x3 x2] 1324 func opcodeOver(op *opcode, data []byte, vm *Engine) error { 1325 return vm.dstack.OverN(1) 1326 } 1327 1328 // opcodePick treats the top item on the data stack as an integer and duplicates 1329 // the item on the stack that number of items back to the top. 1330 // 1331 // Stack transformation: [xn ... x2 x1 x0 n] -> [xn ... x2 x1 x0 xn] 1332 // Example with n=1: [x2 x1 x0 1] -> [x2 x1 x0 x1] 1333 // Example with n=2: [x2 x1 x0 2] -> [x2 x1 x0 x2] 1334 func opcodePick(op *opcode, data []byte, vm *Engine) error { 1335 val, err := vm.dstack.PopInt() 1336 if err != nil { 1337 return err 1338 } 1339 1340 return vm.dstack.PickN(val.Int32()) 1341 } 1342 1343 // opcodeRoll treats the top item on the data stack as an integer and moves 1344 // the item on the stack that number of items back to the top. 1345 // 1346 // Stack transformation: [xn ... x2 x1 x0 n] -> [... x2 x1 x0 xn] 1347 // Example with n=1: [x2 x1 x0 1] -> [x2 x0 x1] 1348 // Example with n=2: [x2 x1 x0 2] -> [x1 x0 x2] 1349 func opcodeRoll(op *opcode, data []byte, vm *Engine) error { 1350 val, err := vm.dstack.PopInt() 1351 if err != nil { 1352 return err 1353 } 1354 1355 return vm.dstack.RollN(val.Int32()) 1356 } 1357 1358 // opcodeRot rotates the top 3 items on the data stack to the left. 1359 // 1360 // Stack transformation: [... x1 x2 x3] -> [... x2 x3 x1] 1361 func opcodeRot(op *opcode, data []byte, vm *Engine) error { 1362 return vm.dstack.RotN(1) 1363 } 1364 1365 // opcodeSwap swaps the top two items on the stack. 1366 // 1367 // Stack transformation: [... x1 x2] -> [... x2 x1] 1368 func opcodeSwap(op *opcode, data []byte, vm *Engine) error { 1369 return vm.dstack.SwapN(1) 1370 } 1371 1372 // opcodeTuck inserts a duplicate of the top item of the data stack before the 1373 // second-to-top item. 1374 // 1375 // Stack transformation: [... x1 x2] -> [... x2 x1 x2] 1376 func opcodeTuck(op *opcode, data []byte, vm *Engine) error { 1377 return vm.dstack.Tuck() 1378 } 1379 1380 // opcodeSize pushes the size of the top item of the data stack onto the data 1381 // stack. 1382 // 1383 // Stack transformation: [... x1] -> [... x1 len(x1)] 1384 func opcodeSize(op *opcode, data []byte, vm *Engine) error { 1385 so, err := vm.dstack.PeekByteArray(0) 1386 if err != nil { 1387 return err 1388 } 1389 1390 vm.dstack.PushInt(scriptNum(len(so))) 1391 return nil 1392 } 1393 1394 // opcodeEqual removes the top 2 items of the data stack, compares them as raw 1395 // bytes, and pushes the result, encoded as a boolean, back to the stack. 1396 // 1397 // Stack transformation: [... x1 x2] -> [... bool] 1398 func opcodeEqual(op *opcode, data []byte, vm *Engine) error { 1399 a, err := vm.dstack.PopByteArray() 1400 if err != nil { 1401 return err 1402 } 1403 b, err := vm.dstack.PopByteArray() 1404 if err != nil { 1405 return err 1406 } 1407 1408 vm.dstack.PushBool(bytes.Equal(a, b)) 1409 return nil 1410 } 1411 1412 // opcodeEqualVerify is a combination of opcodeEqual and opcodeVerify. 1413 // Specifically, it removes the top 2 items of the data stack, compares them, 1414 // and pushes the result, encoded as a boolean, back to the stack. Then, it 1415 // examines the top item on the data stack as a boolean value and verifies it 1416 // evaluates to true. An error is returned if it does not. 1417 // 1418 // Stack transformation: [... x1 x2] -> [... bool] -> [...] 1419 func opcodeEqualVerify(op *opcode, data []byte, vm *Engine) error { 1420 err := opcodeEqual(op, data, vm) 1421 if err == nil { 1422 err = abstractVerify(op, vm, ErrEqualVerify) 1423 } 1424 return err 1425 } 1426 1427 // opcode1Add treats the top item on the data stack as an integer and replaces 1428 // it with its incremented value (plus 1). 1429 // 1430 // Stack transformation: [... x1 x2] -> [... x1 x2+1] 1431 func opcode1Add(op *opcode, data []byte, vm *Engine) error { 1432 m, err := vm.dstack.PopInt() 1433 if err != nil { 1434 return err 1435 } 1436 1437 vm.dstack.PushInt(m + 1) 1438 return nil 1439 } 1440 1441 // opcode1Sub treats the top item on the data stack as an integer and replaces 1442 // it with its decremented value (minus 1). 1443 // 1444 // Stack transformation: [... x1 x2] -> [... x1 x2-1] 1445 func opcode1Sub(op *opcode, data []byte, vm *Engine) error { 1446 m, err := vm.dstack.PopInt() 1447 if err != nil { 1448 return err 1449 } 1450 vm.dstack.PushInt(m - 1) 1451 1452 return nil 1453 } 1454 1455 // opcodeNegate treats the top item on the data stack as an integer and replaces 1456 // it with its negation. 1457 // 1458 // Stack transformation: [... x1 x2] -> [... x1 -x2] 1459 func opcodeNegate(op *opcode, data []byte, vm *Engine) error { 1460 m, err := vm.dstack.PopInt() 1461 if err != nil { 1462 return err 1463 } 1464 1465 vm.dstack.PushInt(-m) 1466 return nil 1467 } 1468 1469 // opcodeAbs treats the top item on the data stack as an integer and replaces it 1470 // it with its absolute value. 1471 // 1472 // Stack transformation: [... x1 x2] -> [... x1 abs(x2)] 1473 func opcodeAbs(op *opcode, data []byte, vm *Engine) error { 1474 m, err := vm.dstack.PopInt() 1475 if err != nil { 1476 return err 1477 } 1478 1479 if m < 0 { 1480 m = -m 1481 } 1482 vm.dstack.PushInt(m) 1483 return nil 1484 } 1485 1486 // opcodeNot treats the top item on the data stack as an integer and replaces 1487 // it with its "inverted" value (0 becomes 1, non-zero becomes 0). 1488 // 1489 // NOTE: While it would probably make more sense to treat the top item as a 1490 // boolean, and push the opposite, which is really what the intention of this 1491 // opcode is, it is extremely important that is not done because integers are 1492 // interpreted differently than booleans and the consensus rules for this opcode 1493 // dictate the item is interpreted as an integer. 1494 // 1495 // Stack transformation (x2==0): [... x1 0] -> [... x1 1] 1496 // Stack transformation (x2!=0): [... x1 1] -> [... x1 0] 1497 // Stack transformation (x2!=0): [... x1 17] -> [... x1 0] 1498 func opcodeNot(op *opcode, data []byte, vm *Engine) error { 1499 m, err := vm.dstack.PopInt() 1500 if err != nil { 1501 return err 1502 } 1503 1504 if m == 0 { 1505 vm.dstack.PushInt(scriptNum(1)) 1506 } else { 1507 vm.dstack.PushInt(scriptNum(0)) 1508 } 1509 return nil 1510 } 1511 1512 // opcode0NotEqual treats the top item on the data stack as an integer and 1513 // replaces it with either a 0 if it is zero, or a 1 if it is not zero. 1514 // 1515 // Stack transformation (x2==0): [... x1 0] -> [... x1 0] 1516 // Stack transformation (x2!=0): [... x1 1] -> [... x1 1] 1517 // Stack transformation (x2!=0): [... x1 17] -> [... x1 1] 1518 func opcode0NotEqual(op *opcode, data []byte, vm *Engine) error { 1519 m, err := vm.dstack.PopInt() 1520 if err != nil { 1521 return err 1522 } 1523 1524 if m != 0 { 1525 m = 1 1526 } 1527 vm.dstack.PushInt(m) 1528 return nil 1529 } 1530 1531 // opcodeAdd treats the top two items on the data stack as integers and replaces 1532 // them with their sum. 1533 // 1534 // Stack transformation: [... x1 x2] -> [... x1+x2] 1535 func opcodeAdd(op *opcode, data []byte, vm *Engine) error { 1536 v0, err := vm.dstack.PopInt() 1537 if err != nil { 1538 return err 1539 } 1540 1541 v1, err := vm.dstack.PopInt() 1542 if err != nil { 1543 return err 1544 } 1545 1546 vm.dstack.PushInt(v0 + v1) 1547 return nil 1548 } 1549 1550 // opcodeSub treats the top two items on the data stack as integers and replaces 1551 // them with the result of subtracting the top entry from the second-to-top 1552 // entry. 1553 // 1554 // Stack transformation: [... x1 x2] -> [... x1-x2] 1555 func opcodeSub(op *opcode, data []byte, vm *Engine) error { 1556 v0, err := vm.dstack.PopInt() 1557 if err != nil { 1558 return err 1559 } 1560 1561 v1, err := vm.dstack.PopInt() 1562 if err != nil { 1563 return err 1564 } 1565 1566 vm.dstack.PushInt(v1 - v0) 1567 return nil 1568 } 1569 1570 // opcodeBoolAnd treats the top two items on the data stack as integers. When 1571 // both of them are not zero, they are replaced with a 1, otherwise a 0. 1572 // 1573 // Stack transformation (x1==0, x2==0): [... 0 0] -> [... 0] 1574 // Stack transformation (x1!=0, x2==0): [... 5 0] -> [... 0] 1575 // Stack transformation (x1==0, x2!=0): [... 0 7] -> [... 0] 1576 // Stack transformation (x1!=0, x2!=0): [... 4 8] -> [... 1] 1577 func opcodeBoolAnd(op *opcode, data []byte, vm *Engine) error { 1578 v0, err := vm.dstack.PopInt() 1579 if err != nil { 1580 return err 1581 } 1582 1583 v1, err := vm.dstack.PopInt() 1584 if err != nil { 1585 return err 1586 } 1587 1588 if v0 != 0 && v1 != 0 { 1589 vm.dstack.PushInt(scriptNum(1)) 1590 } else { 1591 vm.dstack.PushInt(scriptNum(0)) 1592 } 1593 1594 return nil 1595 } 1596 1597 // opcodeBoolOr treats the top two items on the data stack as integers. When 1598 // either of them are not zero, they are replaced with a 1, otherwise a 0. 1599 // 1600 // Stack transformation (x1==0, x2==0): [... 0 0] -> [... 0] 1601 // Stack transformation (x1!=0, x2==0): [... 5 0] -> [... 1] 1602 // Stack transformation (x1==0, x2!=0): [... 0 7] -> [... 1] 1603 // Stack transformation (x1!=0, x2!=0): [... 4 8] -> [... 1] 1604 func opcodeBoolOr(op *opcode, data []byte, vm *Engine) error { 1605 v0, err := vm.dstack.PopInt() 1606 if err != nil { 1607 return err 1608 } 1609 1610 v1, err := vm.dstack.PopInt() 1611 if err != nil { 1612 return err 1613 } 1614 1615 if v0 != 0 || v1 != 0 { 1616 vm.dstack.PushInt(scriptNum(1)) 1617 } else { 1618 vm.dstack.PushInt(scriptNum(0)) 1619 } 1620 1621 return nil 1622 } 1623 1624 // opcodeNumEqual treats the top two items on the data stack as integers. When 1625 // they are equal, they are replaced with a 1, otherwise a 0. 1626 // 1627 // Stack transformation (x1==x2): [... 5 5] -> [... 1] 1628 // Stack transformation (x1!=x2): [... 5 7] -> [... 0] 1629 func opcodeNumEqual(op *opcode, data []byte, vm *Engine) error { 1630 v0, err := vm.dstack.PopInt() 1631 if err != nil { 1632 return err 1633 } 1634 1635 v1, err := vm.dstack.PopInt() 1636 if err != nil { 1637 return err 1638 } 1639 1640 if v0 == v1 { 1641 vm.dstack.PushInt(scriptNum(1)) 1642 } else { 1643 vm.dstack.PushInt(scriptNum(0)) 1644 } 1645 1646 return nil 1647 } 1648 1649 // opcodeNumEqualVerify is a combination of opcodeNumEqual and opcodeVerify. 1650 // 1651 // Specifically, treats the top two items on the data stack as integers. When 1652 // they are equal, they are replaced with a 1, otherwise a 0. Then, it examines 1653 // the top item on the data stack as a boolean value and verifies it evaluates 1654 // to true. An error is returned if it does not. 1655 // 1656 // Stack transformation: [... x1 x2] -> [... bool] -> [...] 1657 func opcodeNumEqualVerify(op *opcode, data []byte, vm *Engine) error { 1658 err := opcodeNumEqual(op, data, vm) 1659 if err == nil { 1660 err = abstractVerify(op, vm, ErrNumEqualVerify) 1661 } 1662 return err 1663 } 1664 1665 // opcodeNumNotEqual treats the top two items on the data stack as integers. 1666 // When they are NOT equal, they are replaced with a 1, otherwise a 0. 1667 // 1668 // Stack transformation (x1==x2): [... 5 5] -> [... 0] 1669 // Stack transformation (x1!=x2): [... 5 7] -> [... 1] 1670 func opcodeNumNotEqual(op *opcode, data []byte, vm *Engine) error { 1671 v0, err := vm.dstack.PopInt() 1672 if err != nil { 1673 return err 1674 } 1675 1676 v1, err := vm.dstack.PopInt() 1677 if err != nil { 1678 return err 1679 } 1680 1681 if v0 != v1 { 1682 vm.dstack.PushInt(scriptNum(1)) 1683 } else { 1684 vm.dstack.PushInt(scriptNum(0)) 1685 } 1686 1687 return nil 1688 } 1689 1690 // opcodeLessThan treats the top two items on the data stack as integers. When 1691 // the second-to-top item is less than the top item, they are replaced with a 1, 1692 // otherwise a 0. 1693 // 1694 // Stack transformation: [... x1 x2] -> [... bool] 1695 func opcodeLessThan(op *opcode, data []byte, vm *Engine) error { 1696 v0, err := vm.dstack.PopInt() 1697 if err != nil { 1698 return err 1699 } 1700 1701 v1, err := vm.dstack.PopInt() 1702 if err != nil { 1703 return err 1704 } 1705 1706 if v1 < v0 { 1707 vm.dstack.PushInt(scriptNum(1)) 1708 } else { 1709 vm.dstack.PushInt(scriptNum(0)) 1710 } 1711 1712 return nil 1713 } 1714 1715 // opcodeGreaterThan treats the top two items on the data stack as integers. 1716 // When the second-to-top item is greater than the top item, they are replaced 1717 // with a 1, otherwise a 0. 1718 // 1719 // Stack transformation: [... x1 x2] -> [... bool] 1720 func opcodeGreaterThan(op *opcode, data []byte, vm *Engine) error { 1721 v0, err := vm.dstack.PopInt() 1722 if err != nil { 1723 return err 1724 } 1725 1726 v1, err := vm.dstack.PopInt() 1727 if err != nil { 1728 return err 1729 } 1730 1731 if v1 > v0 { 1732 vm.dstack.PushInt(scriptNum(1)) 1733 } else { 1734 vm.dstack.PushInt(scriptNum(0)) 1735 } 1736 return nil 1737 } 1738 1739 // opcodeLessThanOrEqual treats the top two items on the data stack as integers. 1740 // When the second-to-top item is less than or equal to the top item, they are 1741 // replaced with a 1, otherwise a 0. 1742 // 1743 // Stack transformation: [... x1 x2] -> [... bool] 1744 func opcodeLessThanOrEqual(op *opcode, data []byte, vm *Engine) error { 1745 v0, err := vm.dstack.PopInt() 1746 if err != nil { 1747 return err 1748 } 1749 1750 v1, err := vm.dstack.PopInt() 1751 if err != nil { 1752 return err 1753 } 1754 1755 if v1 <= v0 { 1756 vm.dstack.PushInt(scriptNum(1)) 1757 } else { 1758 vm.dstack.PushInt(scriptNum(0)) 1759 } 1760 return nil 1761 } 1762 1763 // opcodeGreaterThanOrEqual treats the top two items on the data stack as 1764 // integers. When the second-to-top item is greater than or equal to the top 1765 // item, they are replaced with a 1, otherwise a 0. 1766 // 1767 // Stack transformation: [... x1 x2] -> [... bool] 1768 func opcodeGreaterThanOrEqual(op *opcode, data []byte, vm *Engine) error { 1769 v0, err := vm.dstack.PopInt() 1770 if err != nil { 1771 return err 1772 } 1773 1774 v1, err := vm.dstack.PopInt() 1775 if err != nil { 1776 return err 1777 } 1778 1779 if v1 >= v0 { 1780 vm.dstack.PushInt(scriptNum(1)) 1781 } else { 1782 vm.dstack.PushInt(scriptNum(0)) 1783 } 1784 1785 return nil 1786 } 1787 1788 // opcodeMin treats the top two items on the data stack as integers and replaces 1789 // them with the minimum of the two. 1790 // 1791 // Stack transformation: [... x1 x2] -> [... min(x1, x2)] 1792 func opcodeMin(op *opcode, data []byte, vm *Engine) error { 1793 v0, err := vm.dstack.PopInt() 1794 if err != nil { 1795 return err 1796 } 1797 1798 v1, err := vm.dstack.PopInt() 1799 if err != nil { 1800 return err 1801 } 1802 1803 if v1 < v0 { 1804 vm.dstack.PushInt(v1) 1805 } else { 1806 vm.dstack.PushInt(v0) 1807 } 1808 1809 return nil 1810 } 1811 1812 // opcodeMax treats the top two items on the data stack as integers and replaces 1813 // them with the maximum of the two. 1814 // 1815 // Stack transformation: [... x1 x2] -> [... max(x1, x2)] 1816 func opcodeMax(op *opcode, data []byte, vm *Engine) error { 1817 v0, err := vm.dstack.PopInt() 1818 if err != nil { 1819 return err 1820 } 1821 1822 v1, err := vm.dstack.PopInt() 1823 if err != nil { 1824 return err 1825 } 1826 1827 if v1 > v0 { 1828 vm.dstack.PushInt(v1) 1829 } else { 1830 vm.dstack.PushInt(v0) 1831 } 1832 1833 return nil 1834 } 1835 1836 // opcodeWithin treats the top 3 items on the data stack as integers. When the 1837 // value to test is within the specified range (left inclusive), they are 1838 // replaced with a 1, otherwise a 0. 1839 // 1840 // The top item is the max value, the second-top-item is the minimum value, and 1841 // the third-to-top item is the value to test. 1842 // 1843 // Stack transformation: [... x1 min max] -> [... bool] 1844 func opcodeWithin(op *opcode, data []byte, vm *Engine) error { 1845 maxVal, err := vm.dstack.PopInt() 1846 if err != nil { 1847 return err 1848 } 1849 1850 minVal, err := vm.dstack.PopInt() 1851 if err != nil { 1852 return err 1853 } 1854 1855 x, err := vm.dstack.PopInt() 1856 if err != nil { 1857 return err 1858 } 1859 1860 if x >= minVal && x < maxVal { 1861 vm.dstack.PushInt(scriptNum(1)) 1862 } else { 1863 vm.dstack.PushInt(scriptNum(0)) 1864 } 1865 return nil 1866 } 1867 1868 // calcHash calculates the hash of hasher over buf. 1869 func calcHash(buf []byte, hasher hash.Hash) []byte { 1870 hasher.Write(buf) 1871 return hasher.Sum(nil) 1872 } 1873 1874 // opcodeRipemd160 treats the top item of the data stack as raw bytes and 1875 // replaces it with ripemd160(data). 1876 // 1877 // Stack transformation: [... x1] -> [... ripemd160(x1)] 1878 func opcodeRipemd160(op *opcode, data []byte, vm *Engine) error { 1879 buf, err := vm.dstack.PopByteArray() 1880 if err != nil { 1881 return err 1882 } 1883 1884 vm.dstack.PushByteArray(calcHash(buf, ripemd160.New())) 1885 return nil 1886 } 1887 1888 // opcodeSha1 treats the top item of the data stack as raw bytes and replaces it 1889 // with sha1(data). 1890 // 1891 // Stack transformation: [... x1] -> [... sha1(x1)] 1892 func opcodeSha1(op *opcode, data []byte, vm *Engine) error { 1893 buf, err := vm.dstack.PopByteArray() 1894 if err != nil { 1895 return err 1896 } 1897 1898 hash := sha1.Sum(buf) 1899 vm.dstack.PushByteArray(hash[:]) 1900 return nil 1901 } 1902 1903 // opcodeSha256 treats the top item of the data stack as raw bytes and replaces 1904 // it with sha256(data). 1905 // 1906 // Stack transformation: [... x1] -> [... sha256(x1)] 1907 func opcodeSha256(op *opcode, data []byte, vm *Engine) error { 1908 buf, err := vm.dstack.PopByteArray() 1909 if err != nil { 1910 return err 1911 } 1912 1913 hash := sha256.Sum256(buf) 1914 vm.dstack.PushByteArray(hash[:]) 1915 return nil 1916 } 1917 1918 // opcodeHash160 treats the top item of the data stack as raw bytes and replaces 1919 // it with ripemd160(sha256(data)). 1920 // 1921 // Stack transformation: [... x1] -> [... ripemd160(sha256(x1))] 1922 func opcodeHash160(op *opcode, data []byte, vm *Engine) error { 1923 buf, err := vm.dstack.PopByteArray() 1924 if err != nil { 1925 return err 1926 } 1927 1928 hash := sha256.Sum256(buf) 1929 vm.dstack.PushByteArray(calcHash(hash[:], ripemd160.New())) 1930 return nil 1931 } 1932 1933 // opcodeHash256 treats the top item of the data stack as raw bytes and replaces 1934 // it with sha256(sha256(data)). 1935 // 1936 // Stack transformation: [... x1] -> [... sha256(sha256(x1))] 1937 func opcodeHash256(op *opcode, data []byte, vm *Engine) error { 1938 buf, err := vm.dstack.PopByteArray() 1939 if err != nil { 1940 return err 1941 } 1942 1943 vm.dstack.PushByteArray(chainhash.DoubleHashB(buf)) 1944 return nil 1945 } 1946 1947 // opcodeCodeSeparator stores the current script offset as the most recently 1948 // seen OP_CODESEPARATOR which is used during signature checking. 1949 // 1950 // This opcode does not change the contents of the data stack. 1951 func opcodeCodeSeparator(op *opcode, data []byte, vm *Engine) error { 1952 vm.lastCodeSep = int(vm.tokenizer.ByteIndex()) 1953 1954 if vm.taprootCtx != nil { 1955 vm.taprootCtx.codeSepPos = uint32(vm.tokenizer.OpcodePosition()) 1956 } 1957 1958 return nil 1959 } 1960 1961 // opcodeCheckSig treats the top 2 items on the stack as a public key and a 1962 // signature and replaces them with a bool which indicates if the signature was 1963 // successfully verified. 1964 // 1965 // The process of verifying a signature requires calculating a signature hash in 1966 // the same way the transaction signer did. It involves hashing portions of the 1967 // transaction based on the hash type byte (which is the final byte of the 1968 // signature) and the portion of the script starting from the most recent 1969 // OP_CODESEPARATOR (or the beginning of the script if there are none) to the 1970 // end of the script (with any other OP_CODESEPARATORs removed). Once this 1971 // "script hash" is calculated, the signature is checked using standard 1972 // cryptographic methods against the provided public key. 1973 // 1974 // Stack transformation: [... signature pubkey] -> [... bool] 1975 func opcodeCheckSig(op *opcode, data []byte, vm *Engine) error { 1976 pkBytes, err := vm.dstack.PopByteArray() 1977 if err != nil { 1978 return err 1979 } 1980 1981 fullSigBytes, err := vm.dstack.PopByteArray() 1982 if err != nil { 1983 return err 1984 } 1985 1986 // The signature actually needs needs to be longer than this, but at 1987 // least 1 byte is needed for the hash type below. The full length is 1988 // checked depending on the script flags and upon parsing the signature. 1989 // 1990 // This only applies if tapscript verification isn't active, as this 1991 // check is done within the sighash itself. 1992 if vm.taprootCtx == nil && len(fullSigBytes) < 1 { 1993 vm.dstack.PushBool(false) 1994 return nil 1995 } 1996 1997 var sigVerifier signatureVerifier 1998 switch { 1999 // If no witness program is active, then we're verifying under the 2000 // base consensus rules. 2001 case vm.witnessProgram == nil: 2002 sigVerifier, err = newBaseSigVerifier( 2003 pkBytes, fullSigBytes, vm, 2004 ) 2005 if err != nil { 2006 var scriptErr Error 2007 if errors.As(err, &scriptErr) { 2008 return err 2009 } 2010 2011 vm.dstack.PushBool(false) 2012 return nil 2013 } 2014 2015 // If the base segwit version is active, then we'll create the verifier 2016 // that factors in those new consensus rules. 2017 case vm.isWitnessVersionActive(BaseSegwitWitnessVersion): 2018 sigVerifier, err = newBaseSegwitSigVerifier( 2019 pkBytes, fullSigBytes, vm, 2020 ) 2021 if err != nil { 2022 var scriptErr Error 2023 if errors.As(err, &scriptErr) { 2024 return err 2025 } 2026 2027 vm.dstack.PushBool(false) 2028 return nil 2029 } 2030 2031 // Otherwise, this is routine tapscript execution. 2032 case vm.taprootCtx != nil: 2033 // Account for changes in the sig ops budget after this 2034 // execution, but only for non-empty signatures. 2035 if len(fullSigBytes) > 0 { 2036 if err := vm.taprootCtx.tallysigOp(); err != nil { 2037 return err 2038 } 2039 } 2040 2041 // Empty public keys immediately cause execution to fail. 2042 if len(pkBytes) == 0 { 2043 return scriptError(ErrTaprootPubkeyIsEmpty, "") 2044 } 2045 2046 // If this is tapscript execution, and the signature was 2047 // actually an empty vector, then we push on an empty vector 2048 // and continue execution from there, but only if the pubkey 2049 // isn't empty. 2050 if len(fullSigBytes) == 0 { 2051 vm.dstack.PushByteArray([]byte{}) 2052 return nil 2053 } 2054 2055 // If the constructor fails immediately, then it's because 2056 // the public key size is zero, so we'll fail all script 2057 // execution. 2058 sigVerifier, err = newBaseTapscriptSigVerifier( 2059 pkBytes, fullSigBytes, vm, 2060 ) 2061 if err != nil { 2062 return err 2063 } 2064 2065 default: 2066 // We skip segwit v1 in isolation here, as the v1 rules aren't 2067 // used in script execution (for sig verification) and are only 2068 // part of the top-level key-spend verification which we 2069 // already skipped. 2070 // 2071 // In other words, this path shouldn't ever be reached 2072 // 2073 // TODO(roasbeef): return an error? 2074 } 2075 2076 valid := sigVerifier.Verify() 2077 2078 switch { 2079 // For tapscript, and prior execution with null fail active, if the 2080 // signature is invalid, then this MUST be an empty signature. 2081 case !valid && vm.taprootCtx != nil && len(fullSigBytes) != 0: 2082 fallthrough 2083 case !valid && vm.hasFlag(ScriptVerifyNullFail) && len(fullSigBytes) > 0: 2084 str := "signature not empty on failed checksig" 2085 return scriptError(ErrNullFail, str) 2086 } 2087 2088 vm.dstack.PushBool(valid) 2089 return nil 2090 } 2091 2092 // opcodeCheckSigVerify is a combination of opcodeCheckSig and opcodeVerify. 2093 // The opcodeCheckSig function is invoked followed by opcodeVerify. See the 2094 // documentation for each of those opcodes for more details. 2095 // 2096 // Stack transformation: [... signature pubkey] -> [... bool] -> [...] 2097 func opcodeCheckSigVerify(op *opcode, data []byte, vm *Engine) error { 2098 err := opcodeCheckSig(op, data, vm) 2099 if err == nil { 2100 err = abstractVerify(op, vm, ErrCheckSigVerify) 2101 } 2102 return err 2103 } 2104 2105 // opcodeCheckSigAdd implements the OP_CHECKSIGADD operation defined in BIP 2106 // 342. This is a replacement for OP_CHECKMULTISIGVERIFY and OP_CHECKMULTISIG 2107 // that lends better to batch sig validation, as well as a possible future of 2108 // signature aggregation across inputs. 2109 // 2110 // The op code takes a public key, an integer (N) and a signature, and returns 2111 // N if the signature was the empty vector, and n+1 otherwise. 2112 // 2113 // Stack transformation: [... pubkey n signature] -> [... n | n+1 ] -> [...] 2114 func opcodeCheckSigAdd(op *opcode, data []byte, vm *Engine) error { 2115 // This op code can only be used if tapsript execution is active. 2116 // Before the soft fork, this opcode was marked as an invalid reserved 2117 // op code. 2118 if vm.taprootCtx == nil { 2119 str := fmt.Sprintf("attempt to execute invalid opcode %s", op.name) 2120 return scriptError(ErrReservedOpcode, str) 2121 } 2122 2123 // Pop the signature, integer n, and public key off the stack. 2124 pubKeyBytes, err := vm.dstack.PopByteArray() 2125 if err != nil { 2126 return err 2127 } 2128 accumulatorInt, err := vm.dstack.PopInt() 2129 if err != nil { 2130 return err 2131 } 2132 sigBytes, err := vm.dstack.PopByteArray() 2133 if err != nil { 2134 return err 2135 } 2136 2137 // Only non-empty signatures count towards the total tapscript sig op 2138 // limit. 2139 if len(sigBytes) != 0 { 2140 // Account for changes in the sig ops budget after this execution. 2141 if err := vm.taprootCtx.tallysigOp(); err != nil { 2142 return err 2143 } 2144 } 2145 2146 // Empty public keys immediately cause execution to fail. 2147 if len(pubKeyBytes) == 0 { 2148 return scriptError(ErrTaprootPubkeyIsEmpty, "") 2149 } 2150 2151 // If the signature is empty, then we'll just push the value N back 2152 // onto the stack and continue from here. 2153 if len(sigBytes) == 0 { 2154 vm.dstack.PushInt(accumulatorInt) 2155 return nil 2156 } 2157 2158 // Otherwise, we'll attempt to validate the signature as normal. 2159 // 2160 // If the constructor fails immediately, then it's because the public 2161 // key size is zero, so we'll fail all script execution. 2162 sigVerifier, err := newBaseTapscriptSigVerifier( 2163 pubKeyBytes, sigBytes, vm, 2164 ) 2165 if err != nil { 2166 return err 2167 } 2168 2169 valid := sigVerifier.Verify() 2170 2171 // If the signature is invalid, this we fail execution, as it should 2172 // have been an empty signature. 2173 if !valid { 2174 str := "signature not empty on failed checksig" 2175 return scriptError(ErrNullFail, str) 2176 } 2177 2178 // Otherwise, we increment the accumulatorInt by one, and push that 2179 // back onto the stack. 2180 vm.dstack.PushInt(accumulatorInt + 1) 2181 2182 return nil 2183 } 2184 2185 // parsedSigInfo houses a raw signature along with its parsed form and a flag 2186 // for whether or not it has already been parsed. It is used to prevent parsing 2187 // the same signature multiple times when verifying a multisig. 2188 type parsedSigInfo struct { 2189 signature []byte 2190 parsedSignature *ecdsa.Signature 2191 parsed bool 2192 } 2193 2194 // opcodeCheckMultiSig treats the top item on the stack as an integer number of 2195 // public keys, followed by that many entries as raw data representing the public 2196 // keys, followed by the integer number of signatures, followed by that many 2197 // entries as raw data representing the signatures. 2198 // 2199 // Due to a bug in the original Satoshi client implementation, an additional 2200 // dummy argument is also required by the consensus rules, although it is not 2201 // used. The dummy value SHOULD be an OP_0, although that is not required by 2202 // the consensus rules. When the ScriptStrictMultiSig flag is set, it must be 2203 // OP_0. 2204 // 2205 // All of the aforementioned stack items are replaced with a bool which 2206 // indicates if the requisite number of signatures were successfully verified. 2207 // 2208 // See the opcodeCheckSigVerify documentation for more details about the process 2209 // for verifying each signature. 2210 // 2211 // Stack transformation: 2212 // [... dummy [sig ...] numsigs [pubkey ...] numpubkeys] -> [... bool] 2213 func opcodeCheckMultiSig(op *opcode, data []byte, vm *Engine) error { 2214 // If we're doing tapscript execution, then this op code is disabled. 2215 if vm.taprootCtx != nil { 2216 str := fmt.Sprintf("OP_CHECKMULTISIG and " + 2217 "OP_CHECKMULTISIGVERIFY are disabled during " + 2218 "tapscript execution") 2219 return scriptError(ErrTapscriptCheckMultisig, str) 2220 } 2221 2222 numKeys, err := vm.dstack.PopInt() 2223 if err != nil { 2224 return err 2225 } 2226 2227 numPubKeys := int(numKeys.Int32()) 2228 if numPubKeys < 0 { 2229 str := fmt.Sprintf("number of pubkeys %d is negative", 2230 numPubKeys) 2231 return scriptError(ErrInvalidPubKeyCount, str) 2232 } 2233 if numPubKeys > MaxPubKeysPerMultiSig { 2234 str := fmt.Sprintf("too many pubkeys: %d > %d", 2235 numPubKeys, MaxPubKeysPerMultiSig) 2236 return scriptError(ErrInvalidPubKeyCount, str) 2237 } 2238 vm.numOps += numPubKeys 2239 if vm.numOps > MaxOpsPerScript { 2240 str := fmt.Sprintf("exceeded max operation limit of %d", 2241 MaxOpsPerScript) 2242 return scriptError(ErrTooManyOperations, str) 2243 } 2244 2245 pubKeys := make([][]byte, 0, numPubKeys) 2246 for i := 0; i < numPubKeys; i++ { 2247 pubKey, err := vm.dstack.PopByteArray() 2248 if err != nil { 2249 return err 2250 } 2251 pubKeys = append(pubKeys, pubKey) 2252 } 2253 2254 numSigs, err := vm.dstack.PopInt() 2255 if err != nil { 2256 return err 2257 } 2258 numSignatures := int(numSigs.Int32()) 2259 if numSignatures < 0 { 2260 str := fmt.Sprintf("number of signatures %d is negative", 2261 numSignatures) 2262 return scriptError(ErrInvalidSignatureCount, str) 2263 2264 } 2265 if numSignatures > numPubKeys { 2266 str := fmt.Sprintf("more signatures than pubkeys: %d > %d", 2267 numSignatures, numPubKeys) 2268 return scriptError(ErrInvalidSignatureCount, str) 2269 } 2270 2271 signatures := make([]*parsedSigInfo, 0, numSignatures) 2272 for i := 0; i < numSignatures; i++ { 2273 signature, err := vm.dstack.PopByteArray() 2274 if err != nil { 2275 return err 2276 } 2277 sigInfo := &parsedSigInfo{signature: signature} 2278 signatures = append(signatures, sigInfo) 2279 } 2280 2281 // A bug in the original Satoshi client implementation means one more 2282 // stack value than should be used must be popped. Unfortunately, this 2283 // buggy behavior is now part of the consensus and a hard fork would be 2284 // required to fix it. 2285 dummy, err := vm.dstack.PopByteArray() 2286 if err != nil { 2287 return err 2288 } 2289 2290 // Since the dummy argument is otherwise not checked, it could be any 2291 // value which unfortunately provides a source of malleability. Thus, 2292 // there is a script flag to force an error when the value is NOT 0. 2293 if vm.hasFlag(ScriptStrictMultiSig) && len(dummy) != 0 { 2294 str := fmt.Sprintf("multisig dummy argument has length %d "+ 2295 "instead of 0", len(dummy)) 2296 return scriptError(ErrSigNullDummy, str) 2297 } 2298 2299 // Get script starting from the most recent OP_CODESEPARATOR. 2300 script := vm.subScript() 2301 2302 // Remove the signature in pre version 0 segwit scripts since there is 2303 // no way for a signature to sign itself. 2304 if !vm.isWitnessVersionActive(0) { 2305 for _, sigInfo := range signatures { 2306 script = removeOpcodeByData(script, sigInfo.signature) 2307 } 2308 } 2309 2310 success := true 2311 numPubKeys++ 2312 pubKeyIdx := -1 2313 signatureIdx := 0 2314 for numSignatures > 0 { 2315 // When there are more signatures than public keys remaining, 2316 // there is no way to succeed since too many signatures are 2317 // invalid, so exit early. 2318 pubKeyIdx++ 2319 numPubKeys-- 2320 if numSignatures > numPubKeys { 2321 success = false 2322 break 2323 } 2324 2325 sigInfo := signatures[signatureIdx] 2326 pubKey := pubKeys[pubKeyIdx] 2327 2328 // The order of the signature and public key evaluation is 2329 // important here since it can be distinguished by an 2330 // OP_CHECKMULTISIG NOT when the strict encoding flag is set. 2331 2332 rawSig := sigInfo.signature 2333 if len(rawSig) == 0 { 2334 // Skip to the next pubkey if signature is empty. 2335 continue 2336 } 2337 2338 // Split the signature into hash type and signature components. 2339 hashType := SigHashType(rawSig[len(rawSig)-1]) 2340 signature := rawSig[:len(rawSig)-1] 2341 2342 // Only parse and check the signature encoding once. 2343 var parsedSig *ecdsa.Signature 2344 if !sigInfo.parsed { 2345 if err := vm.checkHashTypeEncoding(hashType); err != nil { 2346 return err 2347 } 2348 if err := vm.checkSignatureEncoding(signature); err != nil { 2349 return err 2350 } 2351 2352 // Parse the signature. 2353 var err error 2354 if vm.hasFlag(ScriptVerifyStrictEncoding) || 2355 vm.hasFlag(ScriptVerifyDERSignatures) { 2356 2357 parsedSig, err = ecdsa.ParseDERSignature(signature) 2358 } else { 2359 parsedSig, err = ecdsa.ParseSignature(signature) 2360 } 2361 sigInfo.parsed = true 2362 if err != nil { 2363 continue 2364 } 2365 sigInfo.parsedSignature = parsedSig 2366 } else { 2367 // Skip to the next pubkey if the signature is invalid. 2368 if sigInfo.parsedSignature == nil { 2369 continue 2370 } 2371 2372 // Use the already parsed signature. 2373 parsedSig = sigInfo.parsedSignature 2374 } 2375 2376 if err := vm.checkPubKeyEncoding(pubKey); err != nil { 2377 return err 2378 } 2379 2380 // Parse the pubkey. 2381 parsedPubKey, err := btcec.ParsePubKey(pubKey) 2382 if err != nil { 2383 continue 2384 } 2385 2386 // Generate the signature hash based on the signature hash type. 2387 var hash []byte 2388 if vm.isWitnessVersionActive(0) { 2389 var sigHashes *TxSigHashes 2390 if vm.hashCache != nil { 2391 sigHashes = vm.hashCache 2392 } else { 2393 sigHashes = NewTxSigHashes( 2394 &vm.tx, vm.prevOutFetcher, 2395 ) 2396 } 2397 2398 hash, err = calcWitnessSignatureHashRaw(script, sigHashes, hashType, 2399 &vm.tx, vm.txIdx, vm.inputAmount) 2400 if err != nil { 2401 return err 2402 } 2403 } else { 2404 hash = calcSignatureHash(script, hashType, &vm.tx, vm.txIdx) 2405 } 2406 2407 var valid bool 2408 if vm.sigCache != nil { 2409 var sigHash chainhash.Hash 2410 copy(sigHash[:], hash) 2411 2412 valid = vm.sigCache.Exists(sigHash, signature, pubKey) 2413 if !valid && parsedSig.Verify(hash, parsedPubKey) { 2414 vm.sigCache.Add(sigHash, signature, pubKey) 2415 valid = true 2416 } 2417 } else { 2418 valid = parsedSig.Verify(hash, parsedPubKey) 2419 } 2420 2421 if valid { 2422 // PubKey verified, move on to the next signature. 2423 signatureIdx++ 2424 numSignatures-- 2425 } 2426 } 2427 2428 if !success && vm.hasFlag(ScriptVerifyNullFail) { 2429 for _, sig := range signatures { 2430 if len(sig.signature) > 0 { 2431 str := "not all signatures empty on failed checkmultisig" 2432 return scriptError(ErrNullFail, str) 2433 } 2434 } 2435 } 2436 2437 vm.dstack.PushBool(success) 2438 return nil 2439 } 2440 2441 // opcodeCheckMultiSigVerify is a combination of opcodeCheckMultiSig and 2442 // opcodeVerify. The opcodeCheckMultiSig is invoked followed by opcodeVerify. 2443 // See the documentation for each of those opcodes for more details. 2444 // 2445 // Stack transformation: 2446 // [... dummy [sig ...] numsigs [pubkey ...] numpubkeys] -> [... bool] -> [...] 2447 func opcodeCheckMultiSigVerify(op *opcode, data []byte, vm *Engine) error { 2448 err := opcodeCheckMultiSig(op, data, vm) 2449 if err == nil { 2450 err = abstractVerify(op, vm, ErrCheckMultiSigVerify) 2451 } 2452 return err 2453 } 2454 2455 // OpcodeByName is a map that can be used to lookup an opcode by its 2456 // human-readable name (OP_CHECKMULTISIG, OP_CHECKSIG, etc). 2457 var OpcodeByName = make(map[string]byte) 2458 2459 func init() { 2460 // Initialize the opcode name to value map using the contents of the 2461 // opcode array. Also add entries for "OP_FALSE", "OP_TRUE", and 2462 // "OP_NOP2" since they are aliases for "OP_0", "OP_1", 2463 // and "OP_CHECKLOCKTIMEVERIFY" respectively. 2464 for _, op := range opcodeArray { 2465 OpcodeByName[op.name] = op.value 2466 } 2467 OpcodeByName["OP_FALSE"] = OP_FALSE 2468 OpcodeByName["OP_TRUE"] = OP_TRUE 2469 OpcodeByName["OP_NOP2"] = OP_CHECKLOCKTIMEVERIFY 2470 OpcodeByName["OP_NOP3"] = OP_CHECKSEQUENCEVERIFY 2471 }