TeachMeBitcoin

OP_GREATERTHAN - Greater Than Comparison

From TeachMeBitcoin, the free encyclopedia Reading time: 1 min

13. OP_GREATERTHAN — Greater Than Comparison

Opcode Reference

Opcode:     OP_GREATERTHAN
Hex:        0xA0
Decimal:    160
Input:      a b
Output:     1 if a > b, else 0

Overview

OP_GREATERTHAN is the mirror of OP_LESSTHAN. It returns 1 if the second-from-top element is strictly greater than the top element.

Stack Examples

Stack: [b=3, a=10, ...]

OP_GREATERTHAN: Is 10 > 3? Yes → [1]

Stack: [b=10, a=3, ...]

OP_GREATERTHAN: Is 3 > 10? No → [0]

Stack: [b=5, a=5, ...]

OP_GREATERTHAN: Is 5 > 5? No → [0]  (strict greater-than)

Bitcoin Core Implementation

case OP_GREATERTHAN:
{
    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_GREATERTHAN enforces minimum value constraints:

Script: <user_value> <min_required> OP_GREATERTHAN OP_VERIFY
Meaning: user_value must be > min_required (strictly)

For "greater than or equal" semantics, use OP_GREATERTHANOREQUAL.

☕ 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!