0% found this document useful (0 votes)
14 views12 pages

Learnmeabitcoin Technical Script

Bitcoin Script is a mini programming language used to lock and unlock outputs in Bitcoin transactions through locking scripts (ScriptPubKey) and unlocking scripts (ScriptSig or Witness). It consists of opcodes and data, allowing for complex transaction conditions, while standard scripts include P2PKH and Segwit variants. Non-standard scripts can be valid but are not relayed by nodes for safety reasons, and there are limits on script sizes to ensure transaction validity.

Uploaded by

ammahmoed4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views12 pages

Learnmeabitcoin Technical Script

Bitcoin Script is a mini programming language used to lock and unlock outputs in Bitcoin transactions through locking scripts (ScriptPubKey) and unlocking scripts (ScriptSig or Witness). It consists of opcodes and data, allowing for complex transaction conditions, while standard scripts include P2PKH and Segwit variants. Non-standard scripts can be valid but are not relayed by nodes for safety reasons, and there are limits on script sizes to ensure transaction validity.

Uploaded by

ammahmoed4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

9/16/25, 12:52 AM Bitcoin Script | A Mini Programming Language

Script
A mini programming language
Greg Walker 27 Aug 2025 Download PDF

Script is a mini programming language used as a locking mechanism for outputs in bitcoin transactions.

A locking script (ScriptPubKey) is placed on every transaction output.


An unlocking script (ScriptSig or Witness) must be provided to unlock an output (i.e. when used as an input to a transaction).

If a full script (unlocking + locking) is valid, the output is "unlocked" and can be spent.

Script Language
What is Script?

Script is a very basic programming language. It consists of two things:

1. Opcodes – Simple functions that operate on data.


2. Data – Such as public keys and signatures.

Here's a simple diagram of a typical P2PKH script used in Bitcoin:

1. Opcodes
Here's a quick list of all the opcodes available in the Script language (along with the corresponding hexadecimal byte used to
represent each one).

https://learnmeabitcoin.com/technical/script/ 1/12
9/16/25, 12:52 AM Bitcoin Script | A Mini Programming Language
Push Data (97) Control Flow (10) Stack Operators (19) Strings (5) Bitwise Logic (8)

00 OP_0 61 OP_NOP 6b OP_TOALTSTACK 7e OP_CAT 83 OP_INVERT

01 OP_PUSHBYTES_1 62 OP_VER 6c OP_FROMALTSTACK 7f OP_SUBSTR 84 OP_AND

02 OP_PUSHBYTES_2 63 OP_IF 6d OP_2DROP 80 OP_LEFT 85 OP_OR

03 OP_PUSHBYTES_3 64 OP_NOTIF 6e OP_2DUP 81 OP_RIGHT 86 OP_XOR

04 OP_PUSHBYTES_4 65 OP_VERIF 6f OP_3DUP 82 OP_SIZE 87 OP_EQUAL

05 OP_PUSHBYTES_5 66 OP_VERNOTIF 70 OP_2OVER 88 OP_EQUALVERIFY

Numeric (27) Cryptography (10) Other (80)

8b OP_1ADD a6 OP_RIPEMD160 b0 OP_NOP1

8c OP_1SUB a7 OP_SHA1 b1 OP_CHECKLOCKTIMEVERIFY

8d OP_2MUL a8 OP_SHA256 b2 OP_CHECKSEQUENCEVERIFY

8e OP_2DIV a9 OP_HASH160 b3 OP_NOP4

8f OP_NEGATE aa OP_HASH256 b4 OP_NOP5

90 OP_ABS ab OP_CODESEPARATOR b5 OP_NOP6

Note:
Opcodes highlighted in red have been disabled.
Check out Opcode Explained ↗ for detailed information about what each opcode does.
There's also a handy overview of all the opcodes on bitcoin.it/wiki/ ↗.
I've not included the OP_ prefixes for the opcodes in the diagrams on this page.

2. Data
The data elements inside a Script (e.g. public keys, signatures) have to be manually pushed on to the stack using an
opcode.

There are a few specific opcodes you can choose from to do this, and the one you use depends on how many bytes you want to push
on to the stack.

OP_0 to OP_16 : (single byte representing numbers 0 to 16) [hide]

The bytes 0x00 , and 0x51 to 0x60 (representing the numbers 0 to 16) have been given their own opcodes from OP_0 to
OP_16 .

These bytes are automatically pushed on to the stack, so you don't need to use any explicit push operation before them. For
example:

ASM Hex

OP_1

Caution: The bytes used to represent the opcodes OP_1 to OP_16 are not the hexadecimal equivalent of these numbers
(i.e. 0x01 to 0x10 ). Instead, the opcodes for these numbers have been assigned bytes in the range 0x51 to 0x60 . It's a bit
awkward, I know, but that's how they've been assigned.

OP_PUSHBYTES_X : 1 to 75 bytes [hide]

The OP_PUSHBYTES_X opcodes ( 0x01 to 0x4b ) are used to push up to 75 bytes on to the stack.
https://learnmeabitcoin.com/technical/script/ 2/12
9/16/25, 12:52 AM Bitcoin Script | A Mini Programming Language
Just replace the X with the number of upcoming bytes you want pushed on to the stack.

For example, here I'm pushing 20 bytes on to the stack:

ASM Hex

OP_PUSHBYTES_20
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Tip: This is the most commonly used opcode for pushing data on to the stack.

OP_PUSHDATA1 : 76 to 255 bytes [hide]

The OP_PUSHDATA1 opcode ( 0x4c ) is followed by 1 byte indicating the number of bytes you want pushed on to the stack,
followed by the actual bytes.

This is used when you want to push more data on the stack than you can with OP_PUSHBYTES_X .

For example, here I'm pushing 76 bytes on to the stack:

ASM Hex

OP_PUSHDATA1
4c
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

OP_PUSHDATA2 : 256 to 65535 bytes [hide]

The OP_PUSHDATA2 opcode ( 0x4d ) works in the same was as OP_PUSHDATA1 , except it's followed by 2 bytes to indicate the
number of upcoming bytes to be pushed on to the stack.

This is used when you want to push more data on the stack than you can with OP_PUSHDATA1 .

For example, here I'm pushing 256 bytes on to the stack:

ASM Hex

OP_PUSHDATA2
0001
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Tip: The 2 bytes after OP_PUSHDATA2 are in little-endian byte order. For example, 256 converted to hexadecimal is
0100 , and in little-endian that's 0001 .

OP_PUSHDATA4 : 65536 to 4294967295 bytes [hide]

The OP_PUSHDATA4 opcode ( 0x4e ) works in the same was as OP_PUSHDATA2 , but is followed by 4 bytes to indicate the number
of upcoming bytes to be pushed on to the stack.

This is used when you want to push more data on the stack than you can with OP_PUSHDATA2 .

For example, here I'm pushing 65536 bytes on to the stack (the actual data push is not shown as it would be pretty long):

https://learnmeabitcoin.com/technical/script/ 3/12
9/16/25, 12:52 AM Bitcoin Script | A Mini Programming Language

ASM Hex

OP_PUSHDATA4 00000100 [65536 bytes]

Tip: The 4 bytes after OP_PUSHDATA4 are in little-endian byte order. For example, 65536 converted to hexadecimal is
00010000 , and in little-endian that's 00000100 .

Caution: OP_PUSHDATA4 is pretty useless, because data pushes in Bitcoin scripts are limited ↘ to 520 bytes.

Note: For simplicity, the diagrams on this page do not show the OP_PUSHBYTES_X opcodes. However, these push operations are
required to push data on to the stack.

Caution:

Minimal push operations


You should always use the smallest push operation available to push data on to the stack.

For example, there are multiple ways to push the number 8 on to the stack:

OP_8
OP_PUSHBYTES_1 08
OP_PUSHDATA1 01 08
OP_PUSHDATA2 0100 08
OP_PUSHDATA4 01000000 08

Similarly, there are multiple ways to push two bytes on to the stack:

OP_PUSHBYTES_2 aaaa
OP_PUSHDATA1 02 aaaa
OP_PUSHDATA2 0200 aaaa
OP_PUSHDATA4 02000000 aaaa

The first operation in both examples uses the fewest bytes to push data on to the stack, and you should always use the most
efficient option you can.

Note: This rule was introduced in BIP 62 ↗ , and is enforced by the CheckMinimalPush() function in script.cpp ↗ . This
is a standardness rule, so whilst using a less efficient operation does not make the Script technically invalid, nodes will not
accept any transactions into their mempool that use anything other than minimal push operations.

Execution
How is Script executed?

A complete script is run from left-to-right. As it runs, it makes use of a data structure called a stack ↗.

Data is pushed on to the stack.

https://learnmeabitcoin.com/technical/script/ 4/12
9/16/25, 12:52 AM Bitcoin Script | A Mini Programming Language

Opcodes pop elements off the stack, do something with them, then optionally push new elements back on to the stack.

Validity
A complete script is determined to be valid or invalid after it has finished executing.

A script is valid if the only element left on the stack is a OP_1 (or any non-zero value).

A script is invalid if:

1. The final stack is empty


2. The only element left on the stack is OP_0
3. There is more than one element left on the stack at the end of execution.
4. The script exits prematurely (e.g. OP_RETURN ).

Here's an example of the execution of a complete P2PKH script:

https://learnmeabitcoin.com/technical/script/ 5/12
9/16/25, 12:52 AM Bitcoin Script | A Mini Programming Language

Location
Where is Script used in Bitcoin?

A locking script (ScriptPubKey) is placed on every output you create in a transaction:

An unlocking script (ScriptSig or Witness) must be provided for every input you want to spend in a transaction:

Every node will then combine and run these two scripts for each input in each transaction they receive to make sure they validate.

If the unlocking scripts on inputs do not successfully unlock the locking scripts on the outputs being spent, then the transaction is
considered invalid and will not be relayed (or mined into a block).

Tip:

The unlocking script goes first.

https://learnmeabitcoin.com/technical/script/ 6/12
9/16/25, 12:52 AM Bitcoin Script | A Mini Programming Language
Even though the unlocking script is provided after the initial locking script (in terms of how transactions work), we actually put
the unlocking script first when we execute the full script.

Usage
Why do we use Script in Bitcoin?

Why do we use a mini programming language for locking up bitcoins? Why not just use a simple public key and signature comparison
and do away with all of these opcodes and stacks?

Well, because by using Script you can create different types of locks by using different combinations of OP_CODES .

For example, here are some cool locking scripts you can create:

1. Math Puzzle
To spend this output, you need to provide two numbers that add up to 8.

Caution: This may not be the most secure locking script in the world, as anyone can figure it out and unlock it.

2. Hash Puzzle
Here you just need something that hashes to the same result as what's inside the locking script.

3. Hash Collision Puzzle


This is a cool one. You can unlock it by providing two different strings of data that produce the same hash result.

In other words, it acts as an incentive to find a "hash collision ↗".

https://learnmeabitcoin.com/technical/script/ 7/12
9/16/25, 12:52 AM Bitcoin Script | A Mini Programming Language

Note: This particular locking script can be found on this output. However, the script has been wrapped in a P2SH locking script,
so you can't actually see the original locking script (until someone unlocks them).

Caution: All of the above locking scripts are non-standard. Whilst these scripts are valid (and can be mined on to the
blockchain), typical Bitcoin Core ↗ nodes will not relay them from their memory pools, which makes it difficult for them to get
mined in the first place.

Standard Scripts
What are the most common Script patterns in Bitcoin?

Despite being able to create a variety of different locking scripts with various combinations of OPCODES , most nodes will only relay a
handful of "standard scripts":

Legacy

P2PK (Pay To Public Key)


P2PKH (Pay To Public Key Hash)

https://learnmeabitcoin.com/technical/script/ 8/12
9/16/25, 12:52 AM Bitcoin Script | A Mini Programming Language
P2MS (Pay To Multisig)
P2SH (Pay To Script Hash)
OP_RETURN

These were the standard scripts available in bitcoin between 2009 and 2016 (before the Segwit upgrade).

They are unlocked via the ScriptSig field.

Tip: Although these are not used as much today (in favor of the newer Segwit scripts below), they're still perfectly valid and you
can use them for locking up your coins if you want to.

Segwit

P2WPKH (Pay To Witness Public Key Hash)


P2WSH (Pay To Witness Script Hash)
P2TR (Pay To Taproot)

These standard scripts were introduced after the Segwit upgrade in 2016 and the Taproot upgrade in 2021. They were basically
introduced to replace the legacy P2PKH and P2SH locking scripts above.

They are unlocked via the Witness field.

Note: These locking scripts do not actually use the fully-fledged Script language.

Instead, each locking script has its own specific pattern, and they are executed in a set way internally when being unlocked. So
they have actually done away with the Script language to a degree (except for when you wrap a fully-fledged script inside a
P2WSH).

So when I said "why not just use a simple public key and signature comparison and do away with all of these opcodes and
stacks?", well, that's what these scripts have done.

Why don't nodes relay non-standard scripts? [hide]

I know, it's a shame.

https://learnmeabitcoin.com/technical/script/ 9/12
9/16/25, 12:52 AM Bitcoin Script | A Mini Programming Language
However, not every combination of OP_CODE has been thoroughly tested. So if nodes relayed every non-standard script they
received, it would introduce the risk of an attack from someone spamming the network with scripts that take a long time to verify.
This could "clog up" nodes and bring the network to a halt.

On the other hand, the standard scripts have been thoroughly tested and can be validated quickly. So the whole non-relaying of
non-standard transactions is just a safety measure.

Tip: Non-standard scripts are valid, they are just not relayed. Even though a non-standard transaction does not get
relayed between memory pools, it can still be mined into a block. So if you want a transaction with a non-standard script to
be added to the blockchain, you either need to send it directly to a miner who will mine it for you, or mine it on to the
blockchain yourself.

Limits
What is the maximum size of a script?

Scripts in bitcoin have certain limits on their size. There are two types of limits:

1. Validity Limits. Any transaction that contains a script that exceeds these limits will be considered invalid. This means it will be
rejected by nodes and cannot be mined into the blockchain.
2. Standardness Limits. A transaction can break these limits and still be mined into the blockchain, but nodes will consider them
non-standard and will refuse to relay them between memory pools.

1. Validity Limits

The maximum size of a script is 10,000 bytes. A combined ScriptPubKey + ScriptSig cannot be greater than 10,000 bytes.
The maximum number of OP_CODES is 201. This limit does not include operations that push data on to the stack.
The maximum size of a single element is 520 bytes. You cannot push any data on to the stack that exceeds 520 bytes.
This limit is most relevant when constructing a Redeem Script for a P2SH, as a Redeem Script is a data push of a script.
The maximum number of stack items is 1,000. A complete script can contain more than 1,000 elements, but you cannot
construct a script that will push over 1,000 items on to the stack at any one time.

These limits can be found in script.h ↗ .

Note: These limits only apply during script execution. So you could place a ScriptPubKey on an output that exceeds these
limits and it would get mined. But when you come to try and spend it as an input (i.e. when the script is being executed), the
spending transaction will be considered invalid. In other words, the output would be unspendable.

2. Standardness Limits

https://learnmeabitcoin.com/technical/script/ 10/12
9/16/25, 12:52 AM Bitcoin Script | A Mini Programming Language

The ScriptSig can be a maximum of 1,650 bytes. So if you need to use more than 1,650 bytes of data and/or OP_CODES to
unlock an output, nodes will not relay your transaction.
The ScriptPubKey has no limit (other than being restricted by the block size limit). So it's perfectly fine to create a
ridiculously large ScriptPubKey if you want to, but remember that it's going to be unspendable if it exceeds any of the validity
limits above.

So whilst it's valid for a transaction to exceed these limits, it's going to be difficult to get it mined into the blockchain unless you can
mine it yourself or get a miner to add it to the blockchain for you.

These limits can be found in policy.h ↗ .

Summary
Script is just a mini programming language used in Bitcoin to provide the locking mechanism for outputs.

Every output is given a "locking script".


You must then provide an "unlocking script" in the transaction that wants to spend that output.

When a node receives the spending transaction, it will combine both of these scripts together and run them. If a OP_1 (and nothing
else) is left on the top of the stack after the script has finished executing, then the script is valid and the output can be spent.

The newer segwit locking scripts have moved away from using traditional Script slightly, but the Script language is still used for
legacy locking scripts, and can also continue to be used within the newer P2WSH locking script.

The script is actually a predicate. It's just an equation that evaluates to true or false. Predicate is a long and unfamiliar word so I
called it script.
– Satoshi Nakamoto, bitcointalk.org ↗

Resources
Opcode Explained ↗ – A complete guide to opcodes and what each one does.
bitcoin.it/wiki/Script ↗
The Bitcoin Script language (pt.1) ↗
Mastering Bitcoin: Chapter 7 (Authorization and Authentication) ↗
Is a script spendable if multiple items are left on the stack? ↗
Why can non-standard transactions be mined but not relayed? ↗

isStandard() ↗

Minsc ↗ — A useful tool for constructing complex Bitcoin scripts using a high-level language based on Miniscript ↗.

https://learnmeabitcoin.com/technical/script/ 11/12
9/16/25, 12:52 AM Bitcoin Script | A Mini Programming Language

https://learnmeabitcoin.com/technical/script/ 12/12

You might also like