Integer Opcodes

Integer Opcodes

Author: Joonmo Yang<jmyang@codechain.io>

Created: 2019-10-30

1. Abstract

This CIP describes a new set of opcodes for CodeChain VM related to integer manipulation. It includes addition(ADD), and comparison(GT, LT) opcodes.

2. Motivation

One of the most common instructions in existing script systems are integer handling operations. CodeChain VM didn’t need them because commonly used scripts(eg. P2PK) don’t require such features. However, recent high-level language proposals like Vault need such features to be implemented. Adding the opcodes specified in this proposal will help adopting high-level languages for CodeChain and give more flexibility to the scripting system.

3. Specification

3.1. GT(0x12)

  1. Pop one value from the stack as an integer(refer this value as a)
  2. Pop one value from the stack as an integer(refer this value as b)
  3. Push true if a > b. Push false otherwise.

3.2. LT(0x13)

  1. Pop one value from the stack as an integer(refer this value as a)
  2. Pop one value from the stack as an integer(refer this value as b)
  3. Push true if a < b. Push false otherwise.

3.3. ADD(0x50)

  1. Pop two values from the stack as integers and add them(refer to this value as a)
  2. If a is larger than or equal to 264, script fails immediately.
  3. Push a to the stack

4. Example

  • PUSH 1 PUSH 2 GT → stack: [0x01]
  • PUSH 2 PUSH 1 GT → stack: [0x00]
  • PUSH 2 PUSH 1 LT → stack: [0x01]
  • PUSH 2 PUSH 1 ADD → stack: [0x03]

I think you should specify that integers are unsigned or handle the case of the underflow.

CodeChain Virtual Machine spec specifies that stack items are decoded as unsigned 64bit integer. We don’t have underflow since there are no subtraction opcode here.