Bitcoin Script

Philosophy

  • Stack-based.
  • Non-Turing completeness: Mitigates malicious scripts on the network.

Generically, the expression that must be satisfied in Bitcoin in order to spend a coin is known as an “encumbrance”.

Condition for validity

  • No failure in executing scripts.
  • Top element of stack is true.

The Execution Stack

  • Holds byte vectors in little-endian format.

  • Max size: 520 bytes.

  • Opcodes taking integers and returning bool: Max size 4 bytes.

  • Addition/ Subtraction overflows and results in 5 byte integer in stack. (How is it handled?)

    Flow control

    • OP_IF/OP_NOTIF: If the top stack value is not False, the statements are executed. The top stack value is removed.
    • OP_VERIFY: Marks transaction as invalid if top stack value is not true. The top stack value is removed.
    • OP_RETURN: Since 0.12, standard relay rules allow a single output with OP_RETURN, that contains any sequence of push statements (or OP_RESERVED) after the OP_RETURN provided the total scriptPubKey length is at most 83 bytes. 🔗

    Stack

    What is Alt Stack?

    Supports generic stack operations with interesting ones like OP_ROLL and OP_PICK and some (really) redundant ones. Possible Reason*: Efficiency while executing script.

    Arithmetic

    • Signed 32-bit integers, might overflow. Failure if > 4 bytes. Our script has all the basic arithmetic operations available.
  • OP_CHECKLOCKTIMEVERIFY and OP_CHECKSEQUENCEVERIFY -> powerful tools for enabling smart contracts.

Example scripts

  1. TODO

Bibliography

  1. Alt Script Wiki