How to Verify P2SH Signature I am trying to debug an issue where i believe i might have a bad signature but i am trying to figure out how to approach it.


What is this a signature of. Private key + what (redeem script? entire raw hex?)
How can i verify that this is a correct partial signature (N 2 of 3) P2SH transaction.


I have 3 signatures. I want to test each signature separately to see which are bad or good. How would one approach that? I have read through many posts here but i have not found any solutions.
To answer your first question:

A P2SH transaction activates a special rule in Bitcoin nodes. When a Script_Publick_Key comes with this pattern:
OP_HASH_160, &lt;HASH&gt;, &lt;OP_EQUAL&gt;
 then the node runs the information in a different way.

First, it runs as it usually does. It puts the ScriptSig on top of the Script_Public_Key. The ScriptSig must follow this form:

OP_0
&lt;Signature 1&gt;
&lt;Signature 2&gt;
...
&lt;Signature (m)&gt;
&lt;SerializedRedeemScript&gt;


Due to Script rules of evaluation, the first item to be evaluated is going to be the &lt;SerializedRedeemScript&gt;. If the hash160 of this serialized script matches the hash in the Script_Publick_Key then the first round is valid and older nodes would stop here. However, BIP0016-compliant nodes will later interpret the serialized version of the redeem script (&lt;SerializedRedeemScript&gt;) as actual Script commands, and the process will repeat this time with the redeem_script commands and the ScriptSig but without the &lt;SerializedRedeemScript&gt; since it was already consumed during the first round of validation. So, let's talk about this redeem_script.

The redeem script must specify how many private keys can sign a transaction (n), how many signatures are necessary to legitimately sign a transaction (m), and all the public keys that corresponds to the private keys that can sign the transaction. The form of the redeem script is as follows:

&lt;OP_m&gt;
&lt;PublicKey 1&gt;
&lt;PublicKey 2&gt;
...
&lt; PublicKey (n)&gt;
&lt;OP_n&gt;
&lt;OP_CHECKMULTISIG&gt;


Since we have only consumed the &lt;SerializedRedeemScript&gt; in the first round of validation, then we still have our OP_0 and signatures left, and the final stack of commands, which is a combination of the redeem script and what is left from the ScripSig, will look like this:

OP_0
&lt;Signature 1&gt;
&lt;Signature 2&gt;
...
&lt;Signature (m)&gt;
&lt;OP_m&gt;
&lt;PublicKey 1&gt;
&lt;PublicKey 2&gt;
...
&lt; PublicKey (n)&gt;
&lt;OP_n&gt;
&lt;OP_CHECKMULTISIG&gt;


The first OP_0 is necessary although it has no meaning at all. It is necessary because of a bug called Off-By-One bug. This was done by accident by Satoshi Nakamoto.

The signatures and public keys will be evaluated by the &lt;OP_CHECKMULTISIG&gt;. 

How can i verify that this is a correct partial signature (N 2 of 3) P2SH transaction.


If you want to check signature by signature, then you can try to do this by first calculating the transaction hash (z) and then checking your single signature with each one of the public keys from the redeem script through OP_CHECKSIG instead of the OP_CHECKMULTISIG. If your signature is valid for a specific public key then your signature is valid. If your signature is not valid for any of the public keys then it is not valid.