TeachMeBitcoin

OP_MIN - Returning the Smaller Value

From TeachMeBitcoin, the free encyclopedia Reading time: 1 min

16. OP_MIN — Returning the Smaller Value

Opcode Reference

Opcode:     OP_MIN
Hex:        0xA3
Decimal:    163
Input:      a b
Output:     min(a, b)

Overview

OP_MIN pops the top two stack elements and pushes whichever is the smaller numeric value. If both are equal, either value (the same) is pushed.

Stack Examples

Input: [b=10, a=3]  → OP_MIN → [3]
Input: [b=3, a=10]  → OP_MIN → [3]
Input: [b=5, a=5]   → OP_MIN → [5]
Input: [b=-1, a=1]  → OP_MIN → [-1]

Bitcoin Core Implementation

case OP_MIN:
{
    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 ? bn1 : bn2).getvch());
}
break;

Use Cases

OP_MIN is useful for capping a value at a maximum threshold — by taking the minimum of the submitted value and the cap, you ensure the result never exceeds the cap:

<user_value> <cap> OP_MIN
→ result is at most <cap>

In payment channel scripts, OP_MIN can be used to select the lesser of two timeout values or enforce conservative limits on numeric witnesses.

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