Difference between revisions of "OP CODESEPARATOR"

Line 1: Line 1:
 
OP_CODESEPARATOR is an opcode used in the script evaluation engine to determine what parts of a script must be hashed to create the message used when creating [[Digital Signatures (ECDSA)]] for Bitcoin transactions. The opcode is inserted automatically between the ScriptSig and ScriptPubKey prior to the evaluation taking place, however further instances of OP_CODESEPARATOR can be inserted into scripts to removing prior script sections from the message. This can allow transactions with multiple signatures to be built additively.
 
OP_CODESEPARATOR is an opcode used in the script evaluation engine to determine what parts of a script must be hashed to create the message used when creating [[Digital Signatures (ECDSA)]] for Bitcoin transactions. The opcode is inserted automatically between the ScriptSig and ScriptPubKey prior to the evaluation taking place, however further instances of OP_CODESEPARATOR can be inserted into scripts to removing prior script sections from the message. This can allow transactions with multiple signatures to be built additively.
  
==Example==
+
==Examples==
The following is a basic [[Bitcoin Transactions#Pay to Public Key|P2PK]] script:
+
The following is a basic [[Bitcoin Transactions#Pay to Public Key|P2PKH]] script:
  
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
 
! ScriptSig
 
! ScriptSig
! Inserted OCS
+
! Auto inserted OCS
 
! ScriptPubKey
 
! ScriptPubKey
 
|-
 
|-
| SIG1
+
| SIG1 PK1
 
| OP_CODESEPARATOR  
 
| OP_CODESEPARATOR  
| PK1 OP_CHECKSIG
+
| OP_DUP OP_HASH160 <pkh1> OP_EQUALVERIFY OP_CHECKSIG
 
|}
 
|}
  
The OP_CODESEPARATOR shown would normally be added by the script interpreter prior to the script being evaluated.
+
The OP_CODESEPARATOR is added between the ScriptSig and ScriptPubKey by the script interpreter so that it can determine what part of the message must be checked against the signature. Everything that follows the OP_CODESEPARATOR is included in the message hash.
  
This script can be added to without SIG1 needing to be modified in the following way:
+
This next example shows a more complex script with an addition OP_CODESEPARATOR which separates [[Bitcoin Transactions#Pay to Public Key Hash|public key hash check]] and an [[Bitcoin Transactions#Pay to R-Puzzle Hash|R-Puzzle]]:
 
+
{| class="wikitable"
SIG1 SIG2 OP_CODESEPARATOR PK2 OP_CHECKSIG OP_CODESEPARATOR PK1 OP_CHECKSIG  
+
|-
 
+
! ScriptSig part 2
In this second revision, we add SIG2 to the top of the stack. The OP_CHECKSIG after PK2 would sign back to the OP_CODESEPARATOR added by the interpreter, covering the whole ScriptPubKey, however the second OP_CHECKSIG would only sign back to the preceding OP_CODESEPARATOR, removing the added PK2 and OP_CHECKSIG from the signature message.
+
! ScriptSig part 1
 +
! Auto inserted OCS
 +
! ScriptPubKey part 1
 +
! OCS
 +
! ScriptPubKey part 2
 +
|-
 +
| SIG1 PK1
 +
| OP_CODESEPARATOR
 +
| OP_DUP OP_HASH160 <pkh1> OP_EQUALVERIFY OP_CHECKSIG
 +
| OP_CODESEPARATOR
 +
| OP_DUP OP_HASH160 <pkh2> OP_EQUALVERIFY OP_CHECKSIG
 +
|}
 +
 +
In this second example, the OP_CODESEPARATOR which is placed between the two elements of the ScriptPubKey removes the requirement for the keyholder unlocking ScriptPubkey part 2 to sign ScriptPubKey part 1. The keyholder unlocking ScriptPubKey part 1 must sign the entire ScriptPubKey message to unlock the UTXO.

Revision as of 09:58, 6 January 2020

OP_CODESEPARATOR is an opcode used in the script evaluation engine to determine what parts of a script must be hashed to create the message used when creating Digital Signatures (ECDSA) for Bitcoin transactions. The opcode is inserted automatically between the ScriptSig and ScriptPubKey prior to the evaluation taking place, however further instances of OP_CODESEPARATOR can be inserted into scripts to removing prior script sections from the message. This can allow transactions with multiple signatures to be built additively.

Examples

The following is a basic P2PKH script:

ScriptSig Auto inserted OCS ScriptPubKey
SIG1 PK1 OP_CODESEPARATOR OP_DUP OP_HASH160 <pkh1> OP_EQUALVERIFY OP_CHECKSIG

The OP_CODESEPARATOR is added between the ScriptSig and ScriptPubKey by the script interpreter so that it can determine what part of the message must be checked against the signature. Everything that follows the OP_CODESEPARATOR is included in the message hash.

This next example shows a more complex script with an addition OP_CODESEPARATOR which separates public key hash check and an R-Puzzle:

ScriptSig part 2 ScriptSig part 1 Auto inserted OCS ScriptPubKey part 1 OCS ScriptPubKey part 2
SIG1 PK1 OP_CODESEPARATOR OP_DUP OP_HASH160 <pkh1> OP_EQUALVERIFY OP_CHECKSIG OP_CODESEPARATOR OP_DUP OP_HASH160 <pkh2> OP_EQUALVERIFY OP_CHECKSIG

In this second example, the OP_CODESEPARATOR which is placed between the two elements of the ScriptPubKey removes the requirement for the keyholder unlocking ScriptPubkey part 2 to sign ScriptPubKey part 1. The keyholder unlocking ScriptPubKey part 1 must sign the entire ScriptPubKey message to unlock the UTXO.