TeachMeBitcoin

OP_VERIFY - Fail-If-False Execution

From TeachMeBitcoin, the free encyclopedia Reading time: 3 min

9. OP_VERIFY — Fail-If-False Execution

Overview

OP_VERIFY is a script termination opcode. It pops the top stack element and checks if it is true. If it is false (zero or empty), the entire script fails immediately and the transaction is considered invalid. If it is true, execution continues normally — but the true value is consumed and discarded.

Opcode Reference

Opcode:     OP_VERIFY
Hex:        0x69
Byte value: 105 (decimal)
Category:   Flow Control / Termination

Core Behavior

Stack before: [ ..., <value> ]

OP_VERIFY:
  pop <value>
  if <value> == false:
    SCRIPT FAILS immediately
  else:
    continue execution (value is consumed)

Stack after (if not failed): [ ... ]

This is distinct from OP_IF in a fundamental way: OP_VERIFY has no branches. There is no "else" path. It is a hard assertion — either the condition holds and execution continues, or the script fails entirely.

The VERIFY Suffix Convention

Many opcodes in Bitcoin Script exist as a combination of another opcode plus OP_VERIFY. The naming convention adds "VERIFY" to the end:

OP_EQUALVERIFY   = OP_EQUAL + OP_VERIFY
OP_CHECKSIGVERIFY = OP_CHECKSIG + OP_VERIFY
OP_CHECKMULTISIGVERIFY = OP_CHECKMULTISIG + OP_VERIFY
OP_NUMEQUALVERIFY = OP_NUMEQUAL + OP_VERIFY

These combined opcodes perform the underlying operation and then immediately fail the script if the result is false. They are provided as single opcodes for script size efficiency.

; Manually written (using OP_EQUAL + OP_VERIFY):
<pubkey_hash>
OP_HASH160
OP_EQUAL
OP_VERIFY

; Equivalent compact form:
<pubkey_hash>
OP_HASH160
OP_EQUALVERIFY

OP_VERIFY in P2PKH Scripts

The classic Pay-to-Public-Key-Hash script uses OP_EQUALVERIFY (which wraps OP_VERIFY):

; P2PKH scriptPubKey:
OP_DUP
OP_HASH160
<pubKeyHash>
OP_EQUALVERIFY   ; fails here if pubkey doesn't match
OP_CHECKSIG      ; fails here if signature is invalid

; Unlocking script (scriptSig):
<signature> <pubkey>

The OP_EQUALVERIFY at step 4 asserts that the provided public key hashes to the expected value. If it does not, script execution terminates immediately — OP_CHECKSIG is never reached.

OP_VERIFY After Computation

A common pattern is to perform a computation and then use OP_VERIFY to assert the result:

; Assert that x + y == 10
<x>
<y>
OP_ADD
OP_10
OP_EQUAL
OP_VERIFY      ; fail if x+y != 10

; Or equivalently:
<x>
<y>
OP_ADD
OP_10
OP_NUMEQUALVERIFY

OP_VERIFY in Covenant-Like Scripts

OP_VERIFY is used in proposed covenant opcodes (like OP_CHECKTEMPLATEVERIFY / BIP 119) to assert that the spending transaction matches a specific template:

; OP_CTV usage (BIP 119):
<template_hash>
OP_CHECKTEMPLATEVERIFY   ; internally calls OP_VERIFY logic
; If transaction doesn't match template, script fails

Using OP_VERIFY for Precondition Checks

; Ensure a value is within bounds before proceeding
<min_value>
<input_value>
OP_GREATERTHANOREQUAL
OP_VERIFY                ; fail if input < min_value

<input_value>
<max_value>
OP_LESSTHANOREQUAL
OP_VERIFY                ; fail if input > max_value

; Only reaches here if min_value <= input_value <= max_value
☕ 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!