<blockquote>
  <p>Does that mean that not all validations follow just plain "concatenate unlocking and locking script and execute' method of validation? As a beginner I'd expect that for uniformity and security all transactions should follow this same uniform rule. Based on the quote, it looks like the P2SH (or possibly other types) scripts are evaluated in a different manner?</p>
</blockquote>

<p>All scripts begin with the same verification process; the concatenate and evaluate. However P2SH has a special extra step. For P2SH outputs, the redeemScript (as part of the first script evaluation) is pushed to the top of the stack. It is then popped from the stack and evaluated as a script with whatever items are remaining in the stack after the first script validation.</p>

<p>Outputs are determined to be P2SH by the template of the output script. All P2SH outputs follow the same template</p>

<pre><code>OP_HASH160 &lt;hash&gt; OP_EQUAL
</code></pre>

<p>Any output script that is in this template will be evaluated as a P2SH output. This means that after the first script evaluation pass (concatenate and evaluate), the top stack item is popped and evaluated as a script.</p>

<blockquote>
  <p>Say there's a very old legacy node that's not P2SH enabled. The transaction above would still evaluate to true (if the provided redeem script is right) as the stack would have a TRUE on top at the end of execution?</p>
</blockquote>

<p>Kind of. Nodes that don't support P2SH would still validate the script as passing because the stack is non-empty and the top stack item is not 0 (OP_FALSE).</p>

CC BY-SA 3.0
2017-08-11T18:30:42.797Z
2017-08-11T18:30:42.797Z
Ava Chow
<blockquote>
  <p>Does that mean that not all validations follow just plain "concatenate unlocking and locking script and execute' method of validation? As a beginner I'd expect that for uniformity and security all transactions should follow this same uniform rule. </p>
</blockquote>

<p>Nothing uses the "concatenate unlocking and locking script and execute". That approach was abandoned in Bitcoin v0.3.7 (released in July 2010), as it was the reason for <a href="https://bitcoin.stackexchange.com/questions/38037/what-is-the-1-return-bug">one</a> of the two theft-enabling bugs Bitcoin had in its infancy.</p>

<p>The current approach is to first execute the unlocking script, look at the resulting stack, and then execute the locking script with that stack as initial stack. The resulting top stack item determines whether validation succeeded or not.</p>

<blockquote>
  <p>Based on the quote, it looks like the P2SH (or possibly other types) scripts are evaluated in a different manner? </p>
</blockquote>

<p>It is not so much a different rule - just an additional rule. Because of backward compatibility, network rule upgrades can only add additional restrictions on previously-valid scripts. They can't make invalid scripts valid, as those would risk splitting the network in two (look up "softforks").</p>

<blockquote>
  <p>If the answer is yes, how does the Bitcoin node know which type of unlocking/locking script type should be applied for each particular transaction?</p>
</blockquote>

<p>Whenever a spend (a) validates using normal rules and (b) has a locking script that satisfies a specific pattern, defined in <a href="https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki" rel="nofollow noreferrer">BIP16</a>, an addition P2SH-specific execution follows, whose validation result is what matters.</p>

<blockquote>
  <p>Also, if the answer is yes, are there other deviations from the plain "concatenate and execute" rule?</p>
</blockquote>

<p>This same approach is used for a much more recent network rule improvement, called SegWit, specified in <a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki" rel="nofollow noreferrer">BIP141</a>, which is expected to be activated on the network around August 22nd 2017.</p>

<blockquote>
  <p>Say there's a very old legacy node that's not P2SH enabled. The transaction above would still evaluate to true (if the provided redeem script is right) as the stack would have a TRUE on top at the end of execution? </p>
</blockquote>

<p>Yes, by definition, because P2SH only adds additional restrictions. The specific P2SH locking script just checks whether the spender knows the "embedded" unlocking script (also called redeemscript).</p>

CC BY-SA 3.0
2018-03-23T16:47:39.767Z
> as it was the reason for one of the two theft-enabling bugs Bitcoin had in its infancy.    <-- interesting, what is the other theft-enabling bug you referred to? Are these the only direct-theft-enabling bugs that Bitcoin ever had?
bgd223
CC BY-SA 3.0
2017-08-11T18:31:44.680Z
2017-08-11T18:31:44.680Z
Pieter Wuille
<p>"Mastering Bitcoin" by Antonopoulos in its <a href="https://github.com/bitcoinbook/bitcoinbook/blob/second_edition/ch07.asciidoc#pay-to-script-hash-p2sh" rel="nofollow noreferrer">P2SH section</a> describes how the unlocking and locking scripts are executed and evaluated by nodes. The author gives this as example of an unlocking script:</p>

<pre><code>&lt;Sig1&gt; &lt;Sig2&gt; &lt;redeem-script&gt;
</code></pre>

<p>and the locking script:</p>

<pre><code>HASH160 &lt;20-byte hash of redeem script&gt; EQUAL
</code></pre>

<p>Then there's this:</p>

<blockquote>
  <p>The two scripts are combined in two stages. First, the redeem script is checked against the locking script to make sure the hash matches: [...]</p>
</blockquote>

<p>Questions</p>

<ul>
<li>Does that mean that not all validations follow just plain "concatenate unlocking and locking script and execute' method of validation? As a beginner I'd expect that for uniformity and security all transactions should follow this same uniform rule.  Based on the quote, it looks like the P2SH (or possibly other types) scripts are evaluated in a different manner? </li>
<li>If the answer is yes, how does the Bitcoin node know which type of unlocking/locking script type should be applied for each particular transaction?</li>
<li>Also, if the answer is yes, are there other deviations from the plain "concatenate and execute" rule?</li>
<li>Say there's a very old legacy node that's not P2SH enabled. The transaction above would still evaluate to true (if the provided redeem script is right) as the stack would have   a TRUE on top at the end of execution? </li>
</ul>

<script><p2sh>
How does P2SH unlocking work?
bgd223
# How does P2SH unlocking work?

(Asked by: username_0 on 2017-08-11)

"Mastering Bitcoin" by Antonopoulos in its [P2SH section](https://github.com/bitcoinbook/bitcoinbook/blob/second_edition/ch07.asciidoc#pay-to-script-hash-p2sh) describes how the unlocking and locking scripts are executed and evaluated by nodes. The author gives this as example of an unlocking script:

```
<Sig1> <Sig2> <redeem-script>

```

and the locking script:

```
HASH160 <20-byte hash of redeem script> EQUAL

```

Then there's this:

> The two scripts are combined in two stages. First, the redeem script is checked against the locking script to make sure the hash matches: [...]

Questions

* Does that mean that not all validations follow just plain "concatenate unlocking and locking script and execute' method of validation? As a beginner I'd expect that for uniformity and security all transactions should follow this same uniform rule. Based on the quote, it looks like the P2SH (or possibly other types) scripts are evaluated in a different manner?
* If the answer is yes, how does the Bitcoin node know which type of unlocking/locking script type should be applied for each particular transaction?
* Also, if the answer is yes, are there other deviations from the plain "concatenate and execute" rule?
* Say there's a very old legacy node that's not P2SH enabled. The transaction above would still evaluate to true (if the provided redeem script is right) as the stack would have a TRUE on top at the end of execution?

**Tags:** script, p2sh


---

## 2 answers

### Answer by username_1 on 2017-08-11. Score: 1

> Does that mean that not all validations follow just plain "concatenate unlocking and locking script and execute' method of validation? As a beginner I'd expect that for uniformity and security all transactions should follow this same uniform rule. Based on the quote, it looks like the P2SH (or possibly other types) scripts are evaluated in a different manner?

All scripts begin with the same verification process; the concatenate and evaluate. However P2SH has a special extra step. For P2SH outputs, the redeemScript (as part of the first script evaluation) is pushed to the top of the stack. It is then popped from the stack and evaluated as a script with whatever items are remaining in the stack after the first script validation.

Outputs are determined to be P2SH by the template of the output script. All P2SH outputs follow the same template

```
OP_HASH160 <hash> OP_EQUAL

```

Any output script that is in this template will be evaluated as a P2SH output. This means that after the first script evaluation pass (concatenate and evaluate), the top stack item is popped and evaluated as a script.

> Say there's a very old legacy node that's not P2SH enabled. The transaction above would still evaluate to true (if the provided redeem script is right) as the stack would have a TRUE on top at the end of execution?

Kind of. Nodes that don't support P2SH would still validate the script as passing because the stack is non-empty and the top stack item is not 0 (OP\_FALSE).


---

### Answer by username_2 on 2017-08-11. Score: 1

> Does that mean that not all validations follow just plain "concatenate unlocking and locking script and execute' method of validation? As a beginner I'd expect that for uniformity and security all transactions should follow this same uniform rule.

Nothing uses the "concatenate unlocking and locking script and execute". That approach was abandoned in Bitcoin v0.3.7 (released in July 2010), as it was the reason for [one](https://bitcoin.stackexchange.com/questions/38037/what-is-the-1-return-bug) of the two theft-enabling bugs Bitcoin had in its infancy.

The current approach is to first execute the unlocking script, look at the resulting stack, and then execute the locking script with that stack as initial stack. The resulting top stack item determines whether validation succeeded or not.

> Based on the quote, it looks like the P2SH (or possibly other types) scripts are evaluated in a different manner?

It is not so much a different rule - just an additional rule. Because of backward compatibility, network rule upgrades can only add additional restrictions on previously-valid scripts. They can't make invalid scripts valid, as those would risk splitting the network in two (look up "softforks").

> If the answer is yes, how does the Bitcoin node know which type of unlocking/locking script type should be applied for each particular transaction?

Whenever a spend (a) validates using normal rules and (b) has a locking script that satisfies a specific pattern, defined in [BIP16](https://github.com/bitcoin/bips/blob/master/bip-0016.mediawiki), an addition P2SH-specific execution follows, whose validation result is what matters.

> Also, if the answer is yes, are there other deviations from the plain "concatenate and execute" rule?

This same approach is used for a much more recent network rule improvement, called SegWit, specified in [BIP141](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki), which is expected to be activated on the network around August 22nd 2017.

> Say there's a very old legacy node that's not P2SH enabled. The transaction above would still evaluate to true (if the provided redeem script is right) as the stack would have a TRUE on top at the end of execution?

Yes, by definition, because P2SH only adds additional restrictions. The specific P2SH locking script just checks whether the spender knows the "embedded" unlocking script (also called redeemscript).

> **Comments:**
> **username_0**: > as it was the reason for one of the two theft-enabling bugs Bitcoin had in its infancy.    <-- interesting, what is the other theft-enabling bug you referred to? Are these the only direct-theft-enabling bugs that Bitcoin ever had?  

---

