Skip to content
Developer Handbook

Cash Improvement Proposals

OP_EVAL Function Evaluation

On this page

CHIP-2024-12 · Withdrawn · Script

Proposed evaluating stack-supplied bytecode. The proposal was withdrawn in favor of the later Functions design.

Original proposal

Specification

    Title: Function Evaluation
    Type: Standards
    Layer: Consensus
    Maintainer: Jason Dreyzehner
    Status: Draft
    Initial Publication Date: 2024-12-12
    Latest Revision Date: 2024-05-02
    Version: 1.0.1

Summary

This proposal introduces the OP_EVAL operation, enabling Bitcoin Cash contract bytecode to be factored into reusable functions.

Motivation & Benefits

  • Improved privacy and operational security – By enabling contracts to hide unused code paths, vaulting systems can be designed to leak less information about security measures, current assets, and transaction history, improving privacy and operational security for end users.

  • Improved auditability – Without reusable functions, existing contracts are forced to unnecessarily duplicate code segments, often with additional stack management operations that can obfuscate unintended behavior or complicate formal verification. By enabling function evaluation, this proposal enables contracts to be written using more succinct, auditable patterns.

  • Reduced transaction sizes – By eliminating duplicated bytecode, contract lengths can be optimized for a wide variety of use cases: finite field arithmetic, pairing-based cryptography, zero-knowledge proof systems, homomorphic encryption, post-quantum cryptography, and other important applications for the future security and competitiveness of Bitcoin Cash.

Deployment

Deployment of this specification is proposed for the May 2026 upgrade.

  • Activation is proposed for 1763208000 MTP, (2025-11-15T12:00:00.000Z) on chipnet.
  • Activation is proposed for 1778846400 MTP, (2026-05-15T12:00:00.000Z) on the BCH network (mainnet), testnet3, testnet4, and scalenet.

Technical Specification

The OP_EVAL opcode is defined at codepoint 0x62 (98) with the following behavior:

  1. Pop the top item from the stack to interpret as bytecode.1
  2. Preserve the active bytecode (A.K.A. script), instruction pointer (A.K.A. program counter or pc), and index of the last executed code separator (A.K.A. pbegincodehash) by pushing them to the top of the control stack.2 (Note that this subjects OP_EVAL evaluations to the existing control stack depth limit of 100.)
  3. Reset the instruction pointer and last executed code separator, then evaluate the stack-provided bytecode as if it were the active bytecode.3 If the bytecode is malformed (i.e. a push operation requires more bytes than are available in the remaining segment of bytecode to be parsed), error.
  4. When the evaluation is complete4, restore the original bytecode, instruction pointer, and last executed code separator, then continue evaluation after the OP_EVAL instruction.5
Clarifications
  1. If the stack is empty, error. If the top stack item has a length of zero: error if the control stack depth limit of 100 would be exceeded by a non-empty evaluation; otherwise, continue after the OP_EVAL instruction.
  2. Note that this requires the control stack to be capable of holding a new stack frame data type to preserve the state of the parent evaluation.
  3. OP_EVALed bytecode otherwise shares the context of the parent evaluation:
    1. Evaluations performed by OP_EVAL can be understood as efficient function calls: the evaluation may modify the stack and alternate stack without limitation, and control stack usage (e.g. OP_IF/OP_END_IF or further OP_EVALs) within the evaluation remains restricted by the 100-item depth limit.
    2. Executed OP_CODESEPARATOR operations record the index of the current instruction pointer (A.K.A. pc) within the OP_EVALed bytecode as the last executed code separator (A.K.A. pbegincodehash).
    3. The OP_ACTIVEBYTECODE operation produces the serialization of the active bytecode beginning from the last executed code separator.
    4. In signature operations, the covered bytecode includes only the active bytecode beginning from the last executed code separator (or if none have been executed in current evaluation, the full active bytecode).
  4. For an OP_EVAL evaluation to complete successfully, the top element remaining on the control stack must be a stack frame to resume after the evaluation. (E.g. Validation fails if an OP_IF within the evalation is not resolved by a matching OP_ENDIF within the same evaluation.)
  5. After an active bytecode has been fully evaluated, the next stack frame is resumed (restoring its bytecode, instruction pointer, and last executed code separator values) until all stack frames are complete (the control stack is empty) or the evaluation has produced some error. Note that two or more OP_EVAL operations may be resolved within the same virtual machine step (e.g. if a child OP_EVAL is the final instruction of a parent OP_EVAL evaluation).

Rationale

Evaluations of Alternatives

Risk Assessment

Test Vectors

This proposal includes a suite of functional tests and benchmarks to verify the performance of all operations within virtual machine implementations.

Implementations

Please see the following implementations for examples and additional test vectors:

Feedback & Reviews

Changelog

This section summarizes the evolution of this document.

This document is placed in the public domain.