<p><a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#witness-program" rel="nofollow noreferrer">BIP141</a> defines requirements for witness stacks as follows:</p>
<ul>
<li>In P2WPKH, the witness stack must consist of exactly 2 items (signature and public key).</li>
<li>In P2WSH, the witness stack must have at least one item (witness script), while the remaining items are used as the initial stack for script execution.</li>
</ul>

CC BY-SA 4.0
2023-09-18T20:25:32.880Z
Are you sure there is a check (requirement) for P2WPKH that exactly 2 elements are in the witness data? Pieter Wuille did not mention that requirement for P2WPKH in [his answer](https://bitcoin.stackexchange.com/a/101146/144381).
dassd
CC BY-SA 4.0
2023-09-18T20:50:21.683Z
Yes. From the specification: "The witness must consist of exactly 2 items (≤ 520 bytes each). The first one a signature, and the second one a public key."
Vojtěch Strnad
CC BY-SA 4.0
2023-09-18T21:00:55.080Z
In his answer, Pieter interprets P2WPKH as a P2PKH script with the witness stack as the initial stack, with the requirement that the resulting stack must have exactly one element. That implicitly requires that the witness stack has exactly two elements, because any other length will either fail the script or result in more than one item on the resulting stack.
Vojtěch Strnad
CC BY-SA 4.0
2023-09-18T21:53:45.673Z
Yes, I completely agree with you that there is an **implicit check** if 2 items are in witness data, because if it is not, the script will not pass. We did not understand. I meant whether there is an **explicit check**, like for example: witness_data.count == 2. Something similar to how P2WSH has a check to see if the witness data is empty.
dassd
CC BY-SA 4.0
2023-09-18T21:59:59.277Z
That depends on the implementation, as there are multiple equivalent ways of implementing the specification given in BIP141. Specifically, [Bitcoin Core](https://github.com/bitcoin/bitcoin/blob/v25.0/src/script/interpreter.cpp#L1895) has an explicit check that the stack size is 2 and then feeds it into a P2PKH script.
Vojtěch Strnad
CC BY-SA 4.0
2023-09-18T22:37:57.143Z
So there is an explicit check. I guess that should be added to Pieter's answer.
dassd
CC BY-SA 4.0
2023-09-18T22:42:55.290Z
I proposed an edit that includes this check.
dassd
CC BY-SA 4.0
2023-09-18T23:29:43.670Z
Pieter's answer already requires that the resulting stack has exactly one element, so a requirement that the initial stack has exactly two elements is redundant. Again, there are multiple algorithms that equivalently implement the specification.
Vojtěch Strnad
CC BY-SA 4.0
2023-09-18T23:55:54.490Z
Yes, I absolutely agree that Pieter by that implicitly said that there must be exactly 2 elements, however, I don't think it's bad to state it explicitly since such an explicit check exists. I also agree that there are several different ways to properly implement the spec, but I think **we should only look at the referent, which is bitcoin core**. However, in the end, whether it's an implicit check (the script simply won't work if there's a different number of elements than 2) or an explicit one, or both (case in bitcoin core), it boils down to the same thing in this case.
dassd
CC BY-SA 4.0
2023-09-18T19:00:48.930Z
2023-09-18T19:00:48.930Z
Vojtěch Strnad
<p>For every input we can have multiple stack items, what do these represent?<br>
As a reference <a href="https://bitcoin.stackexchange.com/questions/55232/parsing-a-segwit-transaction-what-is-the-size-of-witness-data">this question</a> is helpful</p>

<p>Are these defined <a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#examples" rel="nofollow noreferrer">here</a>?<br>
For eg. P2WPKH would have a signature followed by a pubkey, (so two stack items)</p>

<segregated-witness><witness-data>
Parsing a SegWit transaction, what are stack items in witness data
Albert S
# Parsing a SegWit transaction, what are stack items in witness data

(Asked by: username_0 on 2017-07-20)

For every input we can have multiple stack items, what do these represent?  
As a reference [this question](https://bitcoin.stackexchange.com/questions/55232/parsing-a-segwit-transaction-what-is-the-size-of-witness-data) is helpful

Are these defined [here](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#examples)?  
For eg. P2WPKH would have a signature followed by a pubkey, (so two stack items)

**Tags:** segregated-witness, witness-data


---

## 1 answers

### Answer by username_1 on 2023-09-18. Score: 1

[BIP141](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#witness-program) defines requirements for witness stacks as follows:

* In P2WPKH, the witness stack must consist of exactly 2 items (signature and public key).
* In P2WSH, the witness stack must have at least one item (witness script), while the remaining items are used as the initial stack for script execution.

> **Comments:**
> **username_2**: Are you sure there is a check (requirement) for P2WPKH that exactly 2 elements are in the witness data? Pieter Wuille did not mention that requirement for P2WPKH in [his answer](https://bitcoin.stackexchange.com/a/101146/144381).  
> **username_1**: Yes. From the specification: "The witness must consist of exactly 2 items (≤ 520 bytes each). The first one a signature, and the second one a public key."  
> **username_1**: In his answer, Pieter interprets P2WPKH as a P2PKH script with the witness stack as the initial stack, with the requirement that the resulting stack must have exactly one element. That implicitly requires that the witness stack has exactly two elements, because any other length will either fail the script or result in more than one item on the resulting stack.  
> **username_2**: Yes, I completely agree with you that there is an **implicit check** if 2 items are in witness data, because if it is not, the script will not pass. We did not understand. I meant whether there is an **explicit check**, like for example: witness_data.count == 2. Something similar to how P2WSH has a check to see if the witness data is empty.  
> **username_1**: That depends on the implementation, as there are multiple equivalent ways of implementing the specification given in BIP141. Specifically, [Bitcoin Core](https://github.com/bitcoin/bitcoin/blob/v25.0/src/script/interpreter.cpp#L1895) has an explicit check that the stack size is 2 and then feeds it into a P2PKH script.  
> **username_2**: So there is an explicit check. I guess that should be added to Pieter's answer.  
> **username_2**: I proposed an edit that includes this check.  
> **username_1**: Pieter's answer already requires that the resulting stack has exactly one element, so a requirement that the initial stack has exactly two elements is redundant. Again, there are multiple algorithms that equivalently implement the specification.  
> **username_2**: Yes, I absolutely agree that Pieter by that implicitly said that there must be exactly 2 elements, however, I don't think it's bad to state it explicitly since such an explicit check exists. I also agree that there are several different ways to properly implement the spec, but I think **we should only look at the referent, which is bitcoin core**. However, in the end, whether it's an implicit check (the script simply won't work if there's a different number of elements than 2) or an explicit one, or both (case in bitcoin core), it boils down to the same thing in this case.  

---

