0

I have this Script P2PKH

<A Signature> <A Public Key> 
OP_DUP OP_HASH160 <A Public Key Hash> OP_EQUALVERIFY OP_CHECKSIG

The bitcoin.org documentation say this

The entire transaction's outputs, inputs, and script (from the most recently-executed OP_CODESEPARATOR to the end) are hashed. The signature used by OP_CHECKSIG must be a valid signature for this hash and public key. If it is, 1 is returned, 0 otherwise.

Now I want to understand how the code is running inside the stack and how the OP_CHECKSIG work.

My question comes from the fact, why use another operator (OP_CHECKSIG) if is used the operator OP_EQUALVERIFY to does control ?

But I have another question on OP_DUP, the documentation say this

Duplicates the top stack item.

Why when the bitcoin run the script need to duplicate the items inside the stack?

to close, exist other documentation on Bitcoin Script language?

1 Answer 1

4

Let's run through the program.

  • The stack starts as <A sig> <A pubkey>.
  • You run OP_DUP. The stack is now <A sig> <A pubkey> <A pubkey>.
  • You run OP_HASH160. The stack is now <A sig> <A pubkey> <A pubkeyhash>.
  • You push (A expected pubkeyhash). The stack is now <A sig> <A pubkey> <A pubkeyhash> <A expected pubkeyhash>.
  • You run OP_EQUALVERIFY. If the initial public key on the stack does matches the expected one, the execution aborts and the script fails. If it matches, the resulting stack is <A sig> <A pubkey>.
  • You run OP_CHECKSIG. If the signature matches, the resulting stack is 1, which means success.

Without the OP_DUP, it would be impossible to verify the public key both against the signature and against the expected public key hash.

1
  • This kind of stack-oriented scripting reminds me of Postscript which has operators such as dup, exch and pop. I have often used dup at the start of a Postscript subroutine so that a parameter can be used for multiple operations, each of which consumes items from the stack. Sep 17, 2019 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.