3

For every input we can have multiple stack items, what do these represent?
As a reference this question is helpful

Are these defined here?
For eg. P2WPKH would have a signature followed by a pubkey, (so two stack items)

Albert S
  • 1,630
  • 12
  • 21

1 Answers1

1

BIP141 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.
Vojtěch Strnad
  • 8,292
  • 2
  • 12
  • 40
  • 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. – dassd Sep 18 '23 at 20:25
  • 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 Sep 18 '23 at 20:50
  • 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 Sep 18 '23 at 21:00
  • 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 Sep 18 '23 at 21:53
  • That depends on the implementation, as there are multiple equivalent ways of implementing the specification given in BIP141. Specifically, Bitcoin Core has an explicit check that the stack size is 2 and then feeds it into a P2PKH script. – Vojtěch Strnad Sep 18 '23 at 21:59
  • So there is an explicit check. I guess that should be added to Pieter's answer. – dassd Sep 18 '23 at 22:37
  • I proposed an edit that includes this check. – dassd Sep 18 '23 at 22:42
  • 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 Sep 18 '23 at 23:29
  • 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 Sep 18 '23 at 23:55