OP_ADD - Adding Two Numbers
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:
-
Pop the top element from the stack. Call it
b. -
Pop the next element from the stack. Call it
a. -
Decode both
aandbas CScriptNum integers (with a default 4-byte maximum size). -
Compute
result = a + b. -
Encode the result back as a CScriptNum byte array.
-
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
-
Both operands must fit within 4 bytes (by default). Scripts attempting to add numbers requiring 5+ byte representation will fail unless the
SCRIPT_VERIFY_MINIMALDATAflag context is configured appropriately. -
Overflow: Bitcoin Script does not have unlimited integer precision. If the sum exceeds the 32-bit signed range (approximately ±2.1 billion), the script fails.
-
An empty byte vector on the stack is interpreted as zero. So
OP_ADDwith an empty element and<5>produces<5>.
Empty vector + 5:
Stack: [0x05, 0x] (0x = empty = 0)
OP_ADD
Result: 5
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: