Proposed evaluating stack-supplied bytecode. The proposal was withdrawn in favor of the later Functions design.
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
1763208000MTP, (2025-11-15T12:00:00.000Z) onchipnet. - Activation is proposed for
1778846400MTP, (2026-05-15T12:00:00.000Z) on the BCH network (mainnet),testnet3,testnet4, andscalenet.
Technical Specification
The OP_EVAL opcode is defined at codepoint 0x62 (98) with the following behavior:
- Pop the top item from the stack to interpret as bytecode.1
- Preserve the active bytecode (A.K.A.
script), instruction pointer (A.K.A. program counter orpc), 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 subjectsOP_EVALevaluations to the existing control stack depth limit of100.) - 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.
- 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
- If the stack is empty, error. If the top stack item has a length of zero: error if the control stack depth limit of
100would be exceeded by a non-empty evaluation; otherwise, continue after theOP_EVALinstruction. - 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.
OP_EVALed bytecode otherwise shares the context of the parent evaluation:- Evaluations performed by
OP_EVALcan 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_IFor furtherOP_EVALs) within the evaluation remains restricted by the 100-item depth limit. - Executed
OP_CODESEPARATORoperations record the index of the current instruction pointer (A.K.A.pc) within theOP_EVALed bytecode as the last executed code separator (A.K.A.pbegincodehash). - The
OP_ACTIVEBYTECODEoperation produces the serialization of the active bytecode beginning from the last executed code separator. - 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).
- Evaluations performed by
- 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_IFwithin the evalation is not resolved by a matchingOP_ENDIFwithin the same evaluation.) - 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_EVALoperations may be resolved within the same virtual machine step (e.g. if a childOP_EVALis the final instruction of a parentOP_EVALevaluation).
Rationale
Evaluations of Alternatives
Risk Assessment
- Appendix: 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:
- C++:
- Bitcoin Cash Node (BCHN) – A professional, miner-friendly node that solves practical problems for Bitcoin Cash. Merge Request !1937.
- JavaScript/TypeScript:
- Libauth – An ultra-lightweight, zero-dependency JavaScript library for Bitcoin Cash. Branch
next. - Bitauth IDE – An online IDE for bitcoin (cash) contracts. Branch
next.
- Libauth – An ultra-lightweight, zero-dependency JavaScript library for Bitcoin Cash. Branch
Feedback & Reviews
Changelog
This section summarizes the evolution of this document.
- v1.0.1 – 2025-05-02
- Clarify description (#1)
- Add Rationale
- Add Evaluation of Alternatives
- Add Risk Assessment
- Commit latest test vectors
- Link to BCHN implementation
- v1.0.0 – 2024-12-12
- Initial publication
Copyright
This document is placed in the public domain.