Restore disabled script opcodes, May 2018

layout: specification
title: Restore disabled script opcodes, May 2018
category: spec
date: 2018-04-05
activation: 1526400000
version: 0.4
updated: 2018-05-23

Introduction

In 2010 and 2011 the discovery of serious bugs prompted the deactivation of many opcodes in the Bitcoin script language. It is our intention to restore the functionality that some of these opcodes provided in Bitcoin Cash. Rather than simply re-enable the opcodes, the functionality that they provide has been re-examined and in some cases the opcodes have been re-designed or new opcodes have been added to address specific issues.

This document contains the specifications for the opcodes that are to be added in the May 2018 protocol upgrade. We anticipate that additional opcodes will be proposed for the November 2018, or later, protocol upgrades.

The opcodes that are to be added are:

Word OpCode Hex Input Output Description
OP_CAT 126 0x7e x1 x2 out Concatenates two byte sequences
OP_SPLIT 127 0x7f x n x1 x2 Split byte sequence x at position n
OP_AND 132 0x84 x1 x2 out Boolean AND between each bit of the inputs
OP_OR 133 0x85 x1 x2 out Boolean OR between each bit of the inputs
OP_XOR 134 0x86 x1 x2 out Boolean EXCLUSIVE OR between each bit of the inputs
OP_DIV 150 0x96 a b out a is divided by b
OP_MOD 151 0x97 a b out return the remainder after a is divided by b
OP_NUM2BIN 128 0x80 a b out convert numeric value a into byte sequence of length b
OP_BIN2NUM 129 0x81 x out convert byte sequence x into a numeric value

Splice operations: OP_CAT, OP_SPLIT**

Bitwise logic: OP_AND, OP_OR, OP_XOR

Arithmetic: OP_DIV, OP_MOD

New operations:

Further discussion of the purpose of these new operations can be found below under bitwise operations.

** A new operation, OP_SPLIT, has been designed as a replacement for OP_SUBSTR, OP_LEFTand OP_RIGHT. The original operations can be implemented with varying combinations of OP_SPLIT, OP_SWAP and OP_DROP.

Script data types

It should be noted that in script operation data values on the stack are interpreted as either byte sequences or numeric values. All data on the stack is interpreted as a byte sequence unless specifically stated as being interpreted as a numeric value.

For accuracy in this specification, a byte sequences is presented as {0x01, 0x02, 0x03}. This sequence is three bytes long, it begins with a byte of value 1 and ends with a byte of value 3.

The numeric value type has specific limitations:

  1. The used encoding is little endian with an explicit sign bit (the highest bit of the last byte).
  2. They cannot exceed 4 bytes in length.
  3. They must be encoded using the shortest possible byte length (no zero padding)
    1. There is one exception to rule 3: if there is more than one byte and the most significant bit of the second-most-significant-byte is set it would conflict with the sign bit. In this case a single 0x00 or 0x80 byte is allowed to the left.
  4. Zero is encoded as a zero length byte sequence. Single byte positive or negative zero (0x00 or 0x80) are not allowed.

The new opcode x OP_BIN2NUM -> out can be used convert a byte sequence into a numeric value where required.

The new opcode x n OP_NUM2BIN can be used to convert a numeric value into a zero padded byte sequence of length n whilst preserving the sign bit.

Definitions

Specification

Global conditions apply to all operations. These conditions must be checked by the implementation when it is possible that they will occur:

These unit tests should be included for every operation:

  1. executing the operation with an input element of length greater than MAX_SCRIPT_ELEMENT_SIZE will fail
  2. executing the operation with an insufficient number of operands on the stack causes a failure

Operand consumption

In all cases where not explicitly stated otherwise the operand stack elements are consumed by the operation and replaced with the output.

Splice operations

OP_CAT

Opcode (decimal): 126
Opcode (hex): 0x7e

Concatenates two operands.

x1 x2 OP_CAT -> out

Examples:

The operator must fail if len(out) > MAX_SCRIPT_ELEMENT_SIZE. The operation cannot output elements that violate the constraint on the element size.

Note that the concatenation of a zero length operand is valid.

Impact of successful execution:

The limit on the length of the output prevents the memory exhaustion attack and results in the operation having less impact on stack size than existing OP_DUP operators.

Unit tests:

  1. maxlen_x y OP_CAT -> failure. Concatenating any operand except an empty vector, including a single byte value (e.g. OP_1), onto a maximum sized array causes failure
  2. large_x large_y OP_CAT -> failure. Concatenating two operands, where the total length is greater than MAX_SCRIPT_ELEMENT_SIZE, causes failure
  3. OP_0 OP_0 OP_CAT -> OP_0. Concatenating two empty arrays results in an empty array
  4. x OP_0 OP_CAT -> x. Concatenating an empty array onto any operand results in the operand, including when len(x) = MAX_SCRIPT_ELEMENT_SIZE
  5. OP_0 x OP_CAT -> x. Concatenating any operand onto an empty array results in the operand, including when len(x) = MAX_SCRIPT_ELEMENT_SIZE
  6. x y OP_CAT -> concat(x,y). Concatenating two operands generates the correct result

OP_SPLIT

OP_SPLIT replaces OP_SUBSTR and uses it's opcode.

Opcode (decimal): 127
Opcode (hex): 0x7f

Split the operand at the given position. This operation is the exact inverse of OP_CAT

x n OP_SPLIT -> x1 x2

where n is interpreted as a numeric value

Examples:

Notes:

The operator must fail if:

Impact of successful execution:

Unit tests:

Bitwise logic

The bitwise logic operators expect 'byte sequence' operands. The operands must be the same length.

OP_AND

Opcode (decimal): 132
Opcode (hex): 0x84

Boolean and between each bit in the operands.

x1 x2 OP_AND -> out

Notes:

The operator must fail if:

  1. len(x1) != len(x2). The two operands must be the same size.

Impact of successful execution:

Unit tests:

  1. x1 x2 OP_AND -> failure, where len(x1) != len(x2). The two operands must be the same size.
  2. x1 x2 OP_AND -> x1 & x2. Check valid results.

OP_OR

Opcode (decimal): 133
Opcode (hex): 0x85

Boolean or between each bit in the operands.

x1 x2 OP_OR -> out

The operator must fail if:

  1. len(x1) != len(x2). The two operands must be the same size.

Impact of successful execution:

Unit tests:

  1. x1 x2 OP_OR -> failure, where len(x1) != len(x2). The two operands must be the same size.
  2. x1 x2 OP_OR -> x1 | x2. Check valid results.

OP_XOR

Opcode (decimal): 134
Opcode (hex): 0x86

Boolean xor between each bit in the operands.

x1 x2 OP_XOR -> out

The operator must fail if:

  1. len(x1) != len(x2). The two operands must be the same size.

Impact of successful execution:

Unit tests:

  1. x1 x2 OP_XOR -> failure, where len(x1) != len(x2). The two operands must be the same size.
  2. x1 x2 OP_XOR -> x1 xor x2. Check valid results.

Arithmetic

Note about canonical form and floor division

Operands for all arithmetic operations are assumed to be numeric values and must be in canonical form See data types for more information.

Floor division

Note that when considering integer division and modulo operations with negative operands, the rules applied in the C language and most languages (with Python being a notable exception) differ from the strict mathematical definition. Script follows the C language set of rules. Namely:

  1. Non-integer quotients are rounded towards zero
  2. The equation (a/b)*b + a%b == a is satisfied by the results
  3. From the above equation it follows that: a%b == a - (a/b)*b
  4. In practice if a is negative for the modulo operator the result will be negative or zero.

OP_DIV

Opcode (decimal): 150
Opcode (hex): 0x96

Return the integer quotient of a and b. If the result would be a non-integer it is rounded towards zero.

a b OP_DIV -> out

where a and b are interpreted as numeric values

The operator must fail if:

  1. !isnum(a) || !isnum(b). Fail if either operand is not a numeric value.
  2. b == 0. Fail if b is equal to any type of zero.

Impact of successful execution:

Unit tests:

  1. a b OP_DIV -> failure where !isnum(a) or !isnum(b). Both operands must be numeric values
  2. a 0 OP_DIV -> failure. Division by positive zero (all sizes), negative zero (all sizes), OP_0
  3. 27 7 OP_DIV -> 3, 27 -7 OP_DIV -> -3, -27 7 OP_DIV -> -3, -27 -7 OP_DIV -> 3. Check negative operands.

Pay attention to sign. 4. check valid results for operands of different lengths 0..4

OP_MOD

Opcode (decimal): 151
Opcode (hex): 0x97

Returns the remainder after dividing a by b. The output will be represented using the least number of bytes required.

a b OP_MOD -> out

where a and b are interpreted as numeric values

The operator must fail if:

  1. !isnum(a) || !isnum(b). Fail if either operand is not a numeric value.
  2. b == 0. Fail if b is equal to any type of zero.

Impact of successful execution:

Unit tests:

  1. a b OP_MOD -> failure where !isnum(a) or !isnum(b). Both operands must be numeric values.
  2. a 0 OP_MOD -> failure. Division by positive zero (all sizes), negative zero (all sizes), OP_0
  3. 27 7 OP_MOD -> 6, 27 -7 OP_MOD -> 6, -27 7 OP_MOD -> -6, -27 -7 OP_MOD -> -6. Check negative operands.

Pay attention to sign. 4. check valid results for operands of different lengths 0..4 and returning result zero

New operations

OP_NUM2BIN

OP_NUM2BIN replaces OP_LEFT and uses it's opcode

Opcode (decimal): 128
Opcode (hex): 0x80

Convert the numeric value into a byte sequence of a certain size, taking account of the sign bit. The byte sequence produced uses the little-endian encoding.

a b OP_NUM2BIN -> x

where a and b are interpreted as numeric values. a is the value to be converted to a byte sequence, it can be up to MAX_SCRIPT_ELEMENT_SIZE long and does not need to be minimally encoded. b is the desired size of the result, it must be minimally encoded and <= 4 bytes long. It must be possible for the value a to be encoded in a byte sequence of length b without loss of data.

See also OP_BIN2NUM.

Examples:

The operator must fail if:

  1. b is not a minimally encoded numeric value.
  2. b < len(minimal_encoding(a)). a must be able to fit into b bytes.
  3. b > MAX_SCRIPT_ELEMENT_SIZE. The result would be too large.

Impact of successful execution:

Unit tests:

  1. a b OP_NUM2BIN -> failure where !isnum(b). b must be a minimally encoded numeric value.
  2. 256 1 OP_NUM2BIN -> failure. Trying to produce a byte sequence which is smaller than the minimum size needed to contain the numeric value.
  3. 1 (MAX_SCRIPT_ELEMENT_SIZE+1) OP_NUM2BIN -> failure. Trying to produce an array which is too large.
  4. other valid parameters with various results

OP_BIN2NUM

OP_BIN2NUM replaces OP_RIGHT and uses it's opcode

Opcode (decimal): 129
Opcode (hex): 0x81

Convert the byte sequence into a numeric value, including minimal encoding. The byte sequence must encode the value in little-endian encoding.

a OP_BIN2NUM -> x

See also OP_NUM2BIN.

Notes:

Examples:

The operator must fail if:

1.the numeric value is out of the range of acceptable numeric values (currently size is limited to 4 bytes)

Impact of successful execution:

Unit tests:

  1. a OP_BIN2NUM -> failure, when a is a byte sequence whose numeric value is too large to fit into the numeric value type, for both positive and negative values.
  2. {0x00} OP_BIN2NUM -> OP_0. Byte sequences, of various lengths, consisting only of zeros should produce an OP_0 (zero length array).
  3. {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} OP_BIN2NUM -> 1. A large byte sequence, whose numeric value would fit in the numeric value type, is a valid operand.
  4. The same test as above, where the length of the input byte sequence is equal to MAX_SCRIPT_ELEMENT_SIZE.
  5. {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80} OP_BIN2NUM -> -1. Same as above, for negative values.
  6. {0x80} OP_BIN2NUM -> OP_0. Negative zero, in a byte sequence, should produce zero.
  7. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80} OP_BIN2NUM -> OP_0. Large negative zero, in a byte sequence, should produce zero.
  8. other valid parameters with various results

Reference implementation

References

OP_CODES: https://en.bitcoin.it/wiki/Script#Opcodes