BitcoinMachine

Module 03

Comparison

Equality checks, numeric ordering, and range tests. Most real Bitcoin scripts end with a comparison — the final result on the stack is either true or false, and that determines whether the transaction is valid.

0 / 4 sections

What you'll learn

  • Distinguish OP_EQUAL (bytes) from OP_NUMEQUAL (numbers)
  • Use OP_EQUALVERIFY and OP_NUMEQUALVERIFY to assert equality inline
  • Combine OP_NOT, OP_0NOTEQUAL, OP_BOOLAND, OP_BOOLOR for boolean logic
  • Predict when a comparison leaves 0 vs 1 on the stack

01

Equality

OP_EQUAL compares the top two items byte-for-byte and pushes a boolean 1 (true) or 0 (false). OP_EQUALVERIFY does the same but immediately fails the script on inequality — no result is pushed. You'll see OP_EQUALVERIFY constantly in P2PKH.

OP_EQUAL

Byte-for-byte equality. Leaves 1 if equal, 0 if not.

Script
OP_5
OP_5
OP_EQUAL
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_5Pushes the number 5 onto the stack.
0 stepsReady
OP_EQUAL (false)

5 ≠ 6 → leaves 0.

Script
OP_5
OP_6
OP_EQUAL
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_5Pushes the number 5 onto the stack.
0 stepsReady
OP_NUMEQUAL

Numeric equality — interprets both items as CScriptNum. For small integers the result matches OP_EQUAL.

Script
OP_5
OP_5
OP_NUMEQUAL
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_5Pushes the number 5 onto the stack.
0 stepsReady
OP_EQUAL compares raw bytes; OP_NUMEQUAL decodes both as numbers first. They differ when items have different encodings for the same value.
Real Bitcoin

OP_EQUALVERIFY in the wild

Every P2PKH script ends with OP_EQUALVERIFY OP_CHECKSIG. The VERIFY variant lets the script fail immediately on a mismatch rather than leaving a false value to be checked later.

02

Ordering

Four opcodes test the relationship between two numbers. In each case, the second item is compared to the top: think of it as "is the second item [less than / greater than] the top?".

OP_LESSTHAN

Is 3 < 7? Yes → 1. Stack order: [second, top] = [3, 7].

Script
OP_3
OP_7
OP_LESSTHAN
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_3Pushes the number 3 onto the stack.
0 stepsReady
OP_GREATERTHAN

Is 7 > 3? Yes → 1.

Script
OP_7
OP_3
OP_GREATERTHAN
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_7Pushes the number 7 onto the stack.
0 stepsReady
OP_LESSTHANOREQUAL

Is 5 ≤ 5? Yes → 1. Equal counts.

Script
OP_5
OP_5
OP_LESSTHANOREQUAL
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_5Pushes the number 5 onto the stack.
0 stepsReady
OP_GREATERTHANOREQUAL

Is 4 ≥ 5? No → 0.

Script
OP_4
OP_5
OP_GREATERTHANOREQUAL
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_4Pushes the number 4 onto the stack.
0 stepsReady

03

Boolean logic

OP_NOT inverts boolean truthiness: 0 becomes 1, everything else becomes 0. OP_0NOTEQUAL converts any non-zero value to 1 and zero to 0 — useful for normalising comparison results.OP_VERIFY is the assertion that makes these checks binding.

OP_NOT

NOT(0) = 1.

Script
OP_0
OP_NOT
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_0Pushes an empty byte array (false / zero) onto the stack.
0 stepsReady
OP_NOT

NOT(non-zero) = 0. Any truthy value collapses to 0.

Script
OP_5
OP_NOT
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_5Pushes the number 5 onto the stack.
0 stepsReady
OP_0NOTEQUAL

Non-zero → 1. Normalises any truthy value to exactly 1.

Script
OP_7
OP_0NOTEQUAL
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_7Pushes the number 7 onto the stack.
0 stepsReady
Try building a comparison yourself. What script checks whether two values are not equal?

Ctrl+Enter to run

Script
OP_5
OP_6
OP_EQUAL
OP_NOT
Stack0 items

↑ top of stack

nothing here yet

press Step or Run to push an item

bottom
OP_5Pushes the number 5 onto the stack.
0 stepsReady

04

Your turn

Challenge

Prove equality

Write a script that pushes two identical values and uses a comparison opcode to leave exactly 1 (true) as the only item on the stack.

Ctrl+Enter to check

← Home