TeachMeBitcoin

OP_NUMEQUAL - Numeric Equality Test

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

9. OP_NUMEQUAL — Numeric Equality Test

Opcode Reference

Opcode:     OP_NUMEQUAL
Hex:        0x9C
Decimal:    156
Input:      a b
Output:     1 if a == b (numerically), else 0

Overview

OP_NUMEQUAL tests whether two stack elements represent the same integer value. Unlike OP_EQUAL (which does a raw byte comparison), OP_NUMEQUAL decodes both operands as CScriptNum integers and compares their numeric values. This means that different byte encodings of the same number will still compare as equal.

Stack Examples

Input: [5, 5]   → OP_NUMEQUAL → [1]
Input: [5, 6]   → OP_NUMEQUAL → [0]
Input: [-3, -3] → OP_NUMEQUAL → [1]
Input: [0, 0]   → OP_NUMEQUAL → [1]

Difference from OP_EQUAL

OP_EQUAL compares raw bytes:
  [0x05] vs [0x05] → 1  (same bytes)
  [0x05] vs [0x0500] → 0  (different bytes, even though non-minimal)

OP_NUMEQUAL compares decoded integers:
  Both inputs are decoded via CScriptNum first
  Non-minimal encodings of the same number compare equal numerically
  (though in practice, SCRIPT_VERIFY_MINIMALDATA rejects non-minimal encodings anyway)

Bitcoin Core Implementation

case OP_NUMEQUAL:
{
    if (stack.size() < 2)
        return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION);
    CScriptNum bn1(stacktop(-2), fRequireMinimal);
    CScriptNum bn2(stacktop(-1), fRequireMinimal);
    popstack(stack);
    popstack(stack);
    stack.push_back((bn1 == bn2) ? vchTrue : vchFalse);
}
break;

Use Cases

OP_NUMEQUAL is the right opcode when comparing integer results of arithmetic operations. Since arithmetic opcodes like OP_ADD and OP_SUB produce CScriptNum outputs, the follow-up comparison should use OP_NUMEQUAL:

Script: OP_ADD <10> OP_NUMEQUAL
Unlocking: <3> <7>
Meaning: 3 + 7 must equal 10
☕ 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!