TeachMeBitcoin

OP_ADD - Adding Two Numbers

From TeachMeBitcoin, the free encyclopedia Reading time: 3 min

1. OP_ADD — Adding Two Numbers

Opcode Reference

Opcode:     OP_ADD
Hex:        0x93
Decimal:    147
Input:      a b
Output:     a + b

Overview

OP_ADD is one of the most fundamental arithmetic opcodes in Bitcoin Script. It pops the top two elements from the stack, interprets both as integers encoded in the CScriptNum format, adds them together, and pushes the result back onto the stack. Despite its simplicity, OP_ADD has real utility in scripts that need to enforce conditions based on composite numeric values.

How It Works

When the Bitcoin Script interpreter encounters OP_ADD, it performs the following sequence of steps:

  1. Pop the top element from the stack. Call it b.

  2. Pop the next element from the stack. Call it a.

  3. Decode both a and b as CScriptNum integers (with a default 4-byte maximum size).

  4. Compute result = a + b.

  5. Encode the result back as a CScriptNum byte array.

  6. Push the encoded result onto the stack.

If either element cannot be decoded as a valid script number (e.g., it exceeds 4 bytes in length), the script immediately fails.

Stack Visualization

Before OP_ADD:
  Stack (top → bottom): [ 5 | 3 | ... ]

After OP_ADD:
  Stack (top → bottom): [ 8 | ... ]

Bitcoin Core Implementation

In the Bitcoin Core source, OP_ADD is handled in script/interpreter.cpp inside the EvalScript function:

case OP_ADD:
case OP_SUB:
{
    if (stack.size() < 2)
        return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);
    CScriptNum bn1(stacktop(-2), fRequireMinimal);
    CScriptNum bn2(stacktop(-1), fRequireMinimal);
    CScriptNum bn(0);
    switch (opcode)
    {
        case OP_ADD:
            bn = bn1 + bn2;
            break;
        case OP_SUB:
            bn = bn1 - bn2;
            break;
        default:
            break;
    }
    popstack(stack);
    popstack(stack);
    stack.push_back(bn.getvch());
}
break;

Practical Use Cases

OP_ADD is used in multi-condition locking scripts. A practical pattern is to verify that two separately provided numbers sum to a specific value:

Script (locking):   OP_ADD OP_10 OP_EQUAL
Unlocking script:   <3> <7>

Execution:
  Push 3 → Stack: [3]
  Push 7 → Stack: [7, 3]
  OP_ADD  → Stack: [10]
  OP_10   → Stack: [10, 10]
  OP_EQUAL → Stack: [1]  (TRUE — script succeeds)

Another example involves timestamp range validation or counter logic in covenant-style scripts. While Bitcoin's script is intentionally limited, OP_ADD plays a role in combining numeric witnesses to produce verifiable aggregate values.

Edge Cases and Limits

Empty vector + 5:
  Stack: [0x05, 0x]  (0x = empty = 0)
  OP_ADD
  Result: 5
☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!