TeachMeBitcoin

OP_GREATERTHANOREQUAL - Greater Than or Equal

From TeachMeBitcoin, the free encyclopedia Reading time: 1 min

15. OP_GREATERTHANOREQUAL — Greater Than or Equal

Opcode Reference

Opcode:     OP_GREATERTHANOREQUAL
Hex:        0xA2
Decimal:    162
Input:      a b
Output:     1 if a >= b, else 0

Overview

OP_GREATERTHANOREQUAL returns 1 if the second-from-top is greater than or equal to the top. This is the non-strict version of OP_GREATERTHAN.

Stack Examples

Stack: [b=3, a=10] → Is 10 >= 3? Yes → [1] Stack: [b=5, a=5] → Is 5 >= 5? Yes → [1] (equal case also true) Stack: [b=10, a=3] → Is 3 >= 10? No → [0]

Bitcoin Core Implementation

case OP_GREATERTHANOREQUAL:
{
    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_GREATERTHANOREQUAL is often used to verify that a value meets or exceeds a minimum threshold. The OP_CHECKLOCKTIMEVERIFY and OP_CHECKSEQUENCEVERIFY soft forks internally rely on this kind of comparison logic when checking that nLockTime or sequence numbers are at or above a minimum:

Script concept: <nLockTime_from_tx> <minimum_locktime> OP_GREATERTHANOREQUAL OP_VERIFY
☕ 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!