<p>Let's start from the beginning.</p>

<p><strong>p2pk</strong></p>

<p>If you look at the initial blocks on the chain, you'll notice that the coinbase transaction output goes to a pay-to-pubkey lockscript (also known as output or encumberment)</p>

<p>A p2pk locking script is simply <code>PUSH &lt;pubkey&gt; OP_CHECKSIG</code>. To spend this output, one simply needs to provide a valid signature. The scriptsig would contains <code>PUSH sig</code>, which when combined with the locking script produces <code>PUSH sig PUSH &lt;pubkey&gt; OP_CHECKSIG</code>. If the signature provided is valid for the given pubkey, you can prove both ownership of coins and intent to spend in a single move.</p>

<p><strong>p2pkh</strong></p>

<p>p2pk was superseded (perhaps not the correct term, since Bitcoin 0.1 contained support for both p2pk and p2pkh) by pay-to-pubkey-hash. This has a couple of advantages over the vanilla p2pk. For one, it reduced the size of the locking script. Since the utxo set must contain the locking script for validation purposes, this results in direct space savings. For another, hashing the public key adds a layer of protection against any future ecdsa key recovery attacks that may be developed, as you would also need to break the HASH160 operation to recover the public key first.</p>

<p>Using p2pkh comes with some additional complexity. The locking script now takes the form of <code>OP_DUP OP_HASH160 PUSH &lt;PubkeyHash&gt; OP_EQUALVERIFY OP_CHECKSIG</code>. Since we no longer have the actual public key in the script, an unlocking script must prove two things: </p>

<ol>
<li>That it holds the correct private key </li>
<li>That it intends to spend the coins</li>
</ol>

<p>For 1, we must prove that the public key hash in the script corresponds to the public key hash of the key used to perform the signature. For 2, we must verify that the signature is valid against that public key.</p>

<p>To achieve this, the unlocking script takes the form <code>PUSH sig PUSH pubkey</code>. When combined with the locking script, this yields <code>PUSH sig PUSH pubkey OP_DUP OP_HASH160 PUSH &lt;PubkeyHash&gt; OP_EQUALVERIFY OP_CHECKSIG</code></p>

<p>Now, during evaluation, the pubkey is duplicated. The duplicate is hashed and compared against the hash stored in the locking script. If the hash is valid, the signature is validated against the provided public key. This flow ensures that the same public key is used signature verification and the comparison against the hash in the locking script, thus fulfilling both requirements.</p>

<p><em>p2sh</em></p>

<p>pay-to-script-hash was developed to provide a standardized way of using more advanced bitcoin scripts. For this example, let us focus on the multisig p2sh. A typical, 1 of 2 multisig p2sh output's locking script will be similar to <code>OP_HASH160 PUSH &lt;hash&gt; OP_EQUAL</code>. This doesn't contain any public keys, or even a signature checking op code, so what's going on here?</p>

<p>The secret lies in the redeem script. Each p2sh address is backed by a redeem script, and the <code>hash</code> value in the locking script is a hash of this redeem script.</p>

<p>When spending from a p2sh address, you must provide an unlocking script that validates against the redeem script, and the redeem script itself. For our 1of2 multisig address, a redeem script looks like <code>OP_1 PUSH pubkey1 PUSH pubkey2 OP_2 OP_CHECKMULTISIG</code>. This entire script is hashed for the locking script. Note that since the pubkeys contained in this script are already hashed as part of the entire redeem script, we do not need to hash them separately, as we do with p2pkh.</p>

<p>When spending the output, we would then provide: <code>OP_0 PUSH sig PUSH redeemscript</code>. This results in a final script of  <code>OP_0 PUSH sig PUSH redeemscript OP_HASH160 PUSH &lt;hash&gt; OP_EQUAL</code>. During evaluation:</p>

<ol>
<li>The unlocking script and locking script are combined. This results in the signatures and the serialized redeemscript being pushed to the stack. Note that since <code>PUSH redeemscript</code> treats the redeemscript as normal data, the op codes inside the redeem script are not interpreted as op codes in this step.</li>
<li>The serialized redeem script is hashed and validated against the locking script</li>
<li>The signatures are validated against the popped stack, which contains the serialized redeem script without its push op code, and thus interprets it correctly as a bitcoin script.</li>
</ol>

<p>Following this order of operations provides the same guarantee as the p2pkh output - That the transaction intends to spend the coins, and that the keys involved are the same keys that were committed to during the coins' locking.</p>

CC BY-SA 4.0
2018-10-20T23:49:53.243Z
2018-10-20T23:49:53.243Z
Raghav Sood
<p>In an input spending a P2PKH output, if you were to only check that the signature is a valid signature with the public key in the input, then an attacker could use their own public key and produce a valid signature and thus a valid transaction under this model. The public key is not retrieved from the output script nor is it provided by the output; rather it is provided in the input. Thus you must ensure that the provided public key is indeed the correct public key, hence the check that it's hash matches the hash provided in the output.</p>

<p>In multisig, the public keys are provided in the input, in addition to the signatures. They are in the redeemScript. To avoid the same issues (of the attacker providing their own public keys), the redeemScript is hashed and the hash is in the output script. There is a comparison there to ensure that the redeemScript is the correct script.</p>

<p>There is an output type where you do just provide a signature. This output type is a script where the public key is in the output so there is no need to provide the public key in the input. Only the signature is needed. This is known as a Pay to Pubkey (P2PK) output. These are not widely used because public keys are large and this makes it harder to give them out.</p>

CC BY-SA 4.0
2018-10-21T00:07:06.517Z
I see. I think I was confused by the [Bitcoin Developer Guide](https://bitcoin.org/en/developer-guide#standard-transactions). I understand that P2SH multisig has the pk's in the redeemScript input. I was confused by this section, which makes it look like the scriptSig only contains signatures: Pubkey script: <m> <A pubkey> [B pubkey] [C pubkey...] <n> OP_CHECKMULTISIG; Signature script: OP_0 <A sig> [B sig] [C sig...]. Does this mean that this scriptPK and scriptSig are not actually used in practice? But rather the P2SH multisig?
acnalb
CC BY-SA 4.0
2018-10-21T00:24:58.017Z
In practice P2SH multisig is used. Having the multisig in the output (known as bare multisig) has almost never been used.
Ava Chow
CC BY-SA 4.0
2018-10-20T23:37:26.253Z
2018-10-20T23:37:26.253Z
Ava Chow
<p>It is clear to me why it's potentially unsafe to only use a check on == in scriptPubKey. For one, not verifying a signature in the redeem script means that someone could impersonate the redeemer. From the <a href="https://bitcoin.org/en/developer-guide#transactions" rel="nofollow noreferrer">Bitcoin developer guide</a>: </p>

<blockquote>
  <p>This lets the pubkey script verify that Bob owns the private key which created the public key.</p>
  
  <p>Bob’s secp256k1 signature doesn’t just prove Bob controls his private key; it also makes the non-signature-script parts of his transaction tamper-proof so Bob can safely broadcast them over the peer-to-peer network.</p>
</blockquote>

<p>Why would it be unsafe to only use a check on V(sigA, pkA, m={txprev_txid, txprev_outid, txprev_scriptPK, tx_scriptPubKey, tx_amount}_skA) == 1? It seems like this is what we're doing in multisig. I.e., have a ScriptSig that consists only of (a) signature(s) instead of sig and pk like in P2PKH.</p>

<p>Why don't we just check signatures instead of checking both pkhash and a signature? </p>

<transactions><p2pkh>
Checking signature only vs. P2PKH
acnalb
# Checking signature only vs. P2PKH

(Asked by: username_0 on 2018-10-20)

It is clear to me why it's potentially unsafe to only use a check on == in scriptPubKey. For one, not verifying a signature in the redeem script means that someone could impersonate the redeemer. From the [Bitcoin developer guide](https://bitcoin.org/en/developer-guide#transactions):

> This lets the pubkey script verify that Bob owns the private key which created the public key.
>
> Bob’s secp256k1 signature doesn’t just prove Bob controls his private key; it also makes the non-signature-script parts of his transaction tamper-proof so Bob can safely broadcast them over the peer-to-peer network.

Why would it be unsafe to only use a check on V(sigA, pkA, m={txprev\_txid, txprev\_outid, txprev\_scriptPK, tx\_scriptPubKey, tx\_amount}\_skA) == 1? It seems like this is what we're doing in multisig. I.e., have a ScriptSig that consists only of (a) signature(s) instead of sig and pk like in P2PKH.

Why don't we just check signatures instead of checking both pkhash and a signature?

**Tags:** transactions, p2pkh


---

## 2 answers

### Answer by username_1 on 2018-10-20. Score: 1 | Accepted answer

Let's start from the beginning.

**p2pk**

If you look at the initial blocks on the chain, you'll notice that the coinbase transaction output goes to a pay-to-pubkey lockscript (also known as output or encumberment)

A p2pk locking script is simply `PUSH <pubkey> OP_CHECKSIG`. To spend this output, one simply needs to provide a valid signature. The scriptsig would contains `PUSH sig`, which when combined with the locking script produces `PUSH sig PUSH <pubkey> OP_CHECKSIG`. If the signature provided is valid for the given pubkey, you can prove both ownership of coins and intent to spend in a single move.

**p2pkh**

p2pk was superseded (perhaps not the correct term, since Bitcoin 0.1 contained support for both p2pk and p2pkh) by pay-to-pubkey-hash. This has a couple of advantages over the vanilla p2pk. For one, it reduced the size of the locking script. Since the utxo set must contain the locking script for validation purposes, this results in direct space savings. For another, hashing the public key adds a layer of protection against any future ecdsa key recovery attacks that may be developed, as you would also need to break the HASH160 operation to recover the public key first.

Using p2pkh comes with some additional complexity. The locking script now takes the form of `OP_DUP OP_HASH160 PUSH <PubkeyHash> OP_EQUALVERIFY OP_CHECKSIG`. Since we no longer have the actual public key in the script, an unlocking script must prove two things:

1. That it holds the correct private key
2. That it intends to spend the coins

For 1, we must prove that the public key hash in the script corresponds to the public key hash of the key used to perform the signature. For 2, we must verify that the signature is valid against that public key.

To achieve this, the unlocking script takes the form `PUSH sig PUSH pubkey`. When combined with the locking script, this yields `PUSH sig PUSH pubkey OP_DUP OP_HASH160 PUSH <PubkeyHash> OP_EQUALVERIFY OP_CHECKSIG`

Now, during evaluation, the pubkey is duplicated. The duplicate is hashed and compared against the hash stored in the locking script. If the hash is valid, the signature is validated against the provided public key. This flow ensures that the same public key is used signature verification and the comparison against the hash in the locking script, thus fulfilling both requirements.

*p2sh*

pay-to-script-hash was developed to provide a standardized way of using more advanced bitcoin scripts. For this example, let us focus on the multisig p2sh. A typical, 1 of 2 multisig p2sh output's locking script will be similar to `OP_HASH160 PUSH <hash> OP_EQUAL`. This doesn't contain any public keys, or even a signature checking op code, so what's going on here?

The secret lies in the redeem script. Each p2sh address is backed by a redeem script, and the `hash` value in the locking script is a hash of this redeem script.

When spending from a p2sh address, you must provide an unlocking script that validates against the redeem script, and the redeem script itself. For our 1of2 multisig address, a redeem script looks like `OP_1 PUSH pubkey1 PUSH pubkey2 OP_2 OP_CHECKMULTISIG`. This entire script is hashed for the locking script. Note that since the pubkeys contained in this script are already hashed as part of the entire redeem script, we do not need to hash them separately, as we do with p2pkh.

When spending the output, we would then provide: `OP_0 PUSH sig PUSH redeemscript`. This results in a final script of `OP_0 PUSH sig PUSH redeemscript OP_HASH160 PUSH <hash> OP_EQUAL`. During evaluation:

1. The unlocking script and locking script are combined. This results in the signatures and the serialized redeemscript being pushed to the stack. Note that since `PUSH redeemscript` treats the redeemscript as normal data, the op codes inside the redeem script are not interpreted as op codes in this step.
2. The serialized redeem script is hashed and validated against the locking script
3. The signatures are validated against the popped stack, which contains the serialized redeem script without its push op code, and thus interprets it correctly as a bitcoin script.

Following this order of operations provides the same guarantee as the p2pkh output - That the transaction intends to spend the coins, and that the keys involved are the same keys that were committed to during the coins' locking.


---

### Answer by username_2 on 2018-10-20. Score: 1

In an input spending a P2PKH output, if you were to only check that the signature is a valid signature with the public key in the input, then an attacker could use their own public key and produce a valid signature and thus a valid transaction under this model. The public key is not retrieved from the output script nor is it provided by the output; rather it is provided in the input. Thus you must ensure that the provided public key is indeed the correct public key, hence the check that it's hash matches the hash provided in the output.

In multisig, the public keys are provided in the input, in addition to the signatures. They are in the redeemScript. To avoid the same issues (of the attacker providing their own public keys), the redeemScript is hashed and the hash is in the output script. There is a comparison there to ensure that the redeemScript is the correct script.

There is an output type where you do just provide a signature. This output type is a script where the public key is in the output so there is no need to provide the public key in the input. Only the signature is needed. This is known as a Pay to Pubkey (P2PK) output. These are not widely used because public keys are large and this makes it harder to give them out.

> **Comments:**
> **username_0**: I see. I think I was confused by the [Bitcoin Developer Guide](https://bitcoin.org/en/developer-guide#standard-transactions). I understand that P2SH multisig has the pk's in the redeemScript input. I was confused by this section, which makes it look like the scriptSig only contains signatures: Pubkey script: <m> <A pubkey> [B pubkey] [C pubkey...] <n> OP_CHECKMULTISIG; Signature script: OP_0 <A sig> [B sig] [C sig...]. Does this mean that this scriptPK and scriptSig are not actually used in practice? But rather the P2SH multisig?  
> **username_2**: In practice P2SH multisig is used. Having the multisig in the output (known as bare multisig) has almost never been used.  

---

