user27931: I'm trying to create a custom script with if/else and a multisig operation in one branch. Currently, my script looks thus:
OP_IF
    <pub1>
    OP_CHECKSIG
OP_ELSE
    OP_2
    <pub2>
    <pub3>
    <pub4>
    OP_3
    OP_CHECKMULTISIG
OP_ENDIF

I have calculated a testnet address (2NF3yXy2MambEnJuY7eStLNcX92WSxytLvC) from this example script and sent testcoin there. Then, I tried spending the money with the following sig script:
OP_0 <sig2> <sig3> <redeemScript>

However, when trying to broadcast the transaction, I get an error: 

PUSH TRANSACTION ERROR: 16: MANDATORY-SCRIPT-VERIFY-FLAG-FAILED
  (SIGNATURE MUST BE ZERO FOR FAILED CHECK(MULTI)SIG OPERATION)

For reference, here's my raw transaction:
0100000001a492c575e44450a9be9ec4520ea365c53ea6f2018130d682ebf97bdf1b1a15ca01000000fd230100483045022100f502ecfd2b7bbb3e7092b320c568abe0d68dd7e613ab3efcb9e479b2641a73a502202b416bb86bce3d242084f8e24cbed76e5a2c5a0795a5bbd7f3366bf6d086ae4f0147304402201022c7548b55f3fbe3ddf427ad2ae5c6a776e2a63ee25376dca123d53d0f940f022041aa7967208ee8aee1811f02d6c7db0ad2872304133d46dbdd117e9bc09d8c97014c8f632102bc050e51811c2c454bf3487129b19d60b87f7a51fb434e6073def19dc55cf4b3ac67522102a906b96ea2df77aff1f84cc9949550f8d6b8f3db5acd625d12d70b6da4b94b1e2102046e854e69a49dd210dbf554f1e420893935b47123667a1834def9fe5d44358a210325db22e2aef6f143026500522431d4f348f911562316350852e6382da6a048a753ae68ffffffff01403fcb000000000017a91429b5c822f40f80cce593460927095ea6d8d720bc8700000000

Its sig script is this: 
00483045022100f502ecfd2b7bbb3e7092b320c568abe0d68dd7e613ab3efcb9e479b2641a73a502202b416bb86bce3d242084f8e24cbed76e5a2c5a0795a5bbd7f3366bf6d086ae4f0147304402201022c7548b55f3fbe3ddf427ad2ae5c6a776e2a63ee25376dca123d53d0f940f022041aa7967208ee8aee1811f02d6c7db0ad2872304133d46dbdd117e9bc09d8c97014c8f632102bc050e51811c2c454bf3487129b19d60b87f7a51fb434e6073def19dc55cf4b3ac67522102a906b96ea2df77aff1f84cc9949550f8d6b8f3db5acd625d12d70b6da4b94b1e2102046e854e69a49dd210dbf554f1e420893935b47123667a1834def9fe5d44358a210325db22e2aef6f143026500522431d4f348f911562316350852e6382da6a048a753ae68

Which decodes to

0
  3045022100f502ecfd2b7bbb3e7092b320c568abe0d68dd7e613ab3efcb9e479b2641a73a502202b416bb86bce3d242084f8e24cbed76e5a2c5a0795a5bbd7f3366bf6d086ae4f[ALL]
  304402201022c7548b55f3fbe3ddf427ad2ae5c6a776e2a63ee25376dca123d53d0f940f022041aa7967208ee8aee1811f02d6c7db0ad2872304133d46dbdd117e9bc09d8c97[ALL]
  632102bc050e51811c2c454bf3487129b19d60b87f7a51fb434e6073def19dc55cf4b3ac67522102a906b96ea2df77aff1f84cc9949550f8d6b8f3db5acd625d12d70b6da4b94b1e2102046e854e69a49dd210dbf554f1e420893935b47123667a1834def9fe5d44358a210325db22e2aef6f143026500522431d4f348f911562316350852e6382da6a048a753ae68

The last line is clearly visible as the redeem script, which in turn decodes to this:

OP_IF
  02bc050e51811c2c454bf3487129b19d60b87f7a51fb434e6073def19dc55cf4b3
  OP_CHECKSIG OP_ELSE 2
  02a906b96ea2df77aff1f84cc9949550f8d6b8f3db5acd625d12d70b6da4b94b1e
  02046e854e69a49dd210dbf554f1e420893935b47123667a1834def9fe5d44358a
  0325db22e2aef6f143026500522431d4f348f911562316350852e6382da6a048a7 3
  OP_CHECKMULTISIG OP_ENDIF

Is my sig script incorrect? Is the output script invalid? How can I fix this and make it work?
Thank you very much in advance!
EDIT: After taking into consideration Pieter's comment but placing the extra OP_0 right after the two signatures in the sig script, it worked. Here's the new decoded sig script:

0
  3045022100f502ecfd2b7bbb3e7092b320c568abe0d68dd7e613ab3efcb9e479b2641a73a502202b416bb86bce3d242084f8e24cbed76e5a2c5a0795a5bbd7f3366bf6d086ae4f[ALL]
  304402201022c7548b55f3fbe3ddf427ad2ae5c6a776e2a63ee25376dca123d53d0f940f022041aa7967208ee8aee1811f02d6c7db0ad2872304133d46dbdd117e9bc09d8c97[ALL]
  0
  632102bc050e51811c2c454bf3487129b19d60b87f7a51fb434e6073def19dc55cf4b3ac67522102a906b96ea2df77aff1f84cc9949550f8d6b8f3db5acd625d12d70b6da4b94b1e2102046e854e69a49dd210dbf554f1e420893935b47123667a1834def9fe5d44358a210325db22e2aef6f143026500522431d4f348f911562316350852e6382da6a048a753ae68

And here's the link to the transaction: https://testnet.smartbit.com.au/tx/1894cb91fde60432e59d60f30de503e8b6fbe5d1756bbf0ef7302b57bf31d195

user5406: OP_IF consumes a number from the top of the stack.
The OP_IF will evaluate to true if the number is not 0, and to false if the number is 0. 
 H/T for this answer by David Schwartz, and  to Pieter Wuille for getting me on the right track.


27931: <p>I'm trying to create a custom script with if/else and a multisig operation in one branch. Currently, my script looks thus:</p>

<pre><code>OP_IF
    &lt;pub1&gt;
    OP_CHECKSIG
OP_ELSE
    OP_2
    &lt;pub2&gt;
    &lt;pub3&gt;
    &lt;pub4&gt;
    OP_3
    OP_CHECKMULTISIG
OP_ENDIF
</code></pre>

<p>I have calculated a testnet address (<code>2NF3yXy2MambEnJuY7eStLNcX92WSxytLvC</code>) from this example script and sent testcoin there. Then, I tried spending the money with the following sig script:</p>

<pre><code>OP_0 &lt;sig2&gt; &lt;sig3&gt; &lt;redeemScript&gt;
</code></pre>

<p>However, when trying to broadcast the transaction, I get an error: </p>

<blockquote>
  <p>PUSH TRANSACTION ERROR: 16: MANDATORY-SCRIPT-VERIFY-FLAG-FAILED
  (SIGNATURE MUST BE ZERO FOR FAILED CHECK(MULTI)SIG OPERATION)</p>
</blockquote>

<p>For reference, here's my raw transaction:</p>

<pre><code>0100000001a492c575e44450a9be9ec4520ea365c53ea6f2018130d682ebf97bdf1b1a15ca01000000fd230100483045022100f502ecfd2b7bbb3e7092b320c568abe0d68dd7e613ab3efcb9e479b2641a73a502202b416bb86bce3d242084f8e24cbed76e5a2c5a0795a5bbd7f3366bf6d086ae4f0147304402201022c7548b55f3fbe3ddf427ad2ae5c6a776e2a63ee25376dca123d53d0f940f022041aa7967208ee8aee1811f02d6c7db0ad2872304133d46dbdd117e9bc09d8c97014c8f632102bc050e51811c2c454bf3487129b19d60b87f7a51fb434e6073def19dc55cf4b3ac67522102a906b96ea2df77aff1f84cc9949550f8d6b8f3db5acd625d12d70b6da4b94b1e2102046e854e69a49dd210dbf554f1e420893935b47123667a1834def9fe5d44358a210325db22e2aef6f143026500522431d4f348f911562316350852e6382da6a048a753ae68ffffffff01403fcb000000000017a91429b5c822f40f80cce593460927095ea6d8d720bc8700000000
</code></pre>

<p>Its sig script is this: </p>

<pre><code>00483045022100f502ecfd2b7bbb3e7092b320c568abe0d68dd7e613ab3efcb9e479b2641a73a502202b416bb86bce3d242084f8e24cbed76e5a2c5a0795a5bbd7f3366bf6d086ae4f0147304402201022c7548b55f3fbe3ddf427ad2ae5c6a776e2a63ee25376dca123d53d0f940f022041aa7967208ee8aee1811f02d6c7db0ad2872304133d46dbdd117e9bc09d8c97014c8f632102bc050e51811c2c454bf3487129b19d60b87f7a51fb434e6073def19dc55cf4b3ac67522102a906b96ea2df77aff1f84cc9949550f8d6b8f3db5acd625d12d70b6da4b94b1e2102046e854e69a49dd210dbf554f1e420893935b47123667a1834def9fe5d44358a210325db22e2aef6f143026500522431d4f348f911562316350852e6382da6a048a753ae68
</code></pre>

<p>Which decodes to</p>

<blockquote>
  <p>0
  3045022100f502ecfd2b7bbb3e7092b320c568abe0d68dd7e613ab3efcb9e479b2641a73a502202b416bb86bce3d242084f8e24cbed76e5a2c5a0795a5bbd7f3366bf6d086ae4f[ALL]
  304402201022c7548b55f3fbe3ddf427ad2ae5c6a776e2a63ee25376dca123d53d0f940f022041aa7967208ee8aee1811f02d6c7db0ad2872304133d46dbdd117e9bc09d8c97[ALL]
  632102bc050e51811c2c454bf3487129b19d60b87f7a51fb434e6073def19dc55cf4b3ac67522102a906b96ea2df77aff1f84cc9949550f8d6b8f3db5acd625d12d70b6da4b94b1e2102046e854e69a49dd210dbf554f1e420893935b47123667a1834def9fe5d44358a210325db22e2aef6f143026500522431d4f348f911562316350852e6382da6a048a753ae68</p>
</blockquote>

<p>The last line is clearly visible as the redeem script, which in turn decodes to this:</p>

<blockquote>
  <p>OP_IF
  02bc050e51811c2c454bf3487129b19d60b87f7a51fb434e6073def19dc55cf4b3
  OP_CHECKSIG OP_ELSE 2
  02a906b96ea2df77aff1f84cc9949550f8d6b8f3db5acd625d12d70b6da4b94b1e
  02046e854e69a49dd210dbf554f1e420893935b47123667a1834def9fe5d44358a
  0325db22e2aef6f143026500522431d4f348f911562316350852e6382da6a048a7 3
  OP_CHECKMULTISIG OP_ENDIF</p>
</blockquote>

<p>Is my sig script incorrect? Is the output script invalid? How can I fix this and make it work?</p>

<p>Thank you very much in advance!</p>

<p>EDIT: After taking into consideration Pieter's comment but placing the extra OP_0 right after the two signatures in the sig script, it worked. Here's the new decoded sig script:</p>

<blockquote>
  <p>0
  3045022100f502ecfd2b7bbb3e7092b320c568abe0d68dd7e613ab3efcb9e479b2641a73a502202b416bb86bce3d242084f8e24cbed76e5a2c5a0795a5bbd7f3366bf6d086ae4f[ALL]
  304402201022c7548b55f3fbe3ddf427ad2ae5c6a776e2a63ee25376dca123d53d0f940f022041aa7967208ee8aee1811f02d6c7db0ad2872304133d46dbdd117e9bc09d8c97[ALL]
  0
  632102bc050e51811c2c454bf3487129b19d60b87f7a51fb434e6073def19dc55cf4b3ac67522102a906b96ea2df77aff1f84cc9949550f8d6b8f3db5acd625d12d70b6da4b94b1e2102046e854e69a49dd210dbf554f1e420893935b47123667a1834def9fe5d44358a210325db22e2aef6f143026500522431d4f348f911562316350852e6382da6a048a753ae68</p>
</blockquote>

<p>And here's the link to the transaction: <a href="https://testnet.smartbit.com.au/tx/1894cb91fde60432e59d60f30de503e8b6fbe5d1756bbf0ef7302b57bf31d195" rel="nofollow noreferrer">https://testnet.smartbit.com.au/tx/1894cb91fde60432e59d60f30de503e8b6fbe5d1756bbf0ef7302b57bf31d195</a></p>
 5406: <p><code>OP_IF</code> consumes a number from the top of the stack.<br>
The <code>OP_IF</code> will evaluate to <code>true</code> if the number is <code>not 0</code>, and to <code>false</code> if the number is <code>0</code>. </p>

<p><sup> H/T for <a href="https://bitcoin.stackexchange.com/a/2318/5406">this answer by David Schwartz</a>, and  to Pieter Wuille for getting me on the right track.
</sup></p>
