OP_LESSTHANOREQUAL - Less Than or Equal
From TeachMeBitcoin, the free encyclopedia
Reading time: 1 min
14. OP_LESSTHANOREQUAL — Less Than or Equal
Opcode Reference
Opcode: OP_LESSTHANOREQUAL
Hex: 0xA1
Decimal: 161
Input: a b
Output: 1 if a <= b, else 0
Overview
OP_LESSTHANOREQUAL pushes 1 if the second-from-top element is less than or equal to the top element. This is the non-strict version of OP_LESSTHAN.
Stack Examples
Stack: [b=10, a=5] → Is 5 <= 10? Yes → [1] Stack: [b=5, a=5] → Is 5 <= 5? Yes → [1] (equal case also true) Stack: [b=3, a=10] → Is 10 <= 3? No → [0]
Bitcoin Core Implementation
case OP_LESSTHANOREQUAL:
{
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_LESSTHANOREQUAL is critical for inclusive upper-bound checks. For example, verifying that a fee does not exceed a maximum:
<computed_fee> <fee_cap> OP_LESSTHANOREQUAL 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