TeachMeBitcoin

The Scripting Context (CScript)

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

The Scripting Context (CScript)

Bitcoin Script is a stack-based language that handles numbers in a very specific way. To save space, it uses a Variable-Length Little Endian format for integers placed on the stack.

1. Minimally Encoded Integers

In most programming languages, an integer is always 4 or 8 bytes. In Bitcoin Script, the number 1 is stored as a single byte: 0x01.

2. Little Endian Signed Magnitude

Bitcoin Script uses Little Endian for its math, but it also uses a "Sign Bit."

3. The "Arithmetic" Rule

When you use an opcode like OP_ADD:

  1. The node pops two items off the stack.

  2. It interprets them as Little Endian integers.

  3. It performs the addition.

  4. It pushes the result back to the stack as a Minimally Encoded Little Endian integer.

4. Opcodes and Data

It's important to distinguish between the OpCode and the Data.

5. Why this matters for Developers

If you are writing a script manually, you must ensure your numbers are encoded correctly.

Value Standard Int (4-byte LE) Script Int (Minimal LE)
1 01 00 00 00 01
127 7F 00 00 00 7F
128 80 00 00 00 80 00

In the final section, we will build a Python Context Auditor.

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