<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>

CC BY-SA 3.0
2018-03-25T03:57:30.297Z
2018-03-25T18:50:42.647Z
2018-03-25T18:50:42.647Z
Murch
<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>

CC BY-SA 3.0
2018-03-25T03:17:48.667Z
I believe you may need two `OP_0`'s at the start: one to be consumed by `OP_IF`, and one which is popped off by `OP_CHECKMULTISIG`. I'll turn this into a full answer once I've identified the issue.
Pieter Wuille
CC BY-SA 3.0
2018-03-25T03:33:35.827Z
Thanks for your response! I just tried it, and I'm still getting the same error: `PUSH TRANSACTION ERROR: 16: MANDATORY-SCRIPT-VERIFY-FLAG-FAILED (SIGNATURE MUST BE ZERO FOR FAILED CHECK(MULTI)SIG OPERATION)`
arik
CC BY-SA 3.0
2018-03-25T08:33:54.423Z
~30min in the Video Andreas explains the „IF“ handling of script. There must be a value before the IF statement. Either 1 for the If-branch or a 0 for the else branch. https://m.youtube.com/watch?v=yU3Sr07Qnxg
pebwindkraft
CC BY-SA 3.0
2018-03-25T18:45:45.423Z
@pebwindkraft: that should be an answer. ;)
Murch
CC BY-SA 3.0
2018-03-26T08:19:29.383Z
@murch: not chasing for credits anymore :-) Pieter and you have provided the answers, and this is just "a little bit" additional explanation, which might help the users to better understand the previous comments.
pebwindkraft
CC BY-SA 3.0
2018-03-26T16:34:49.937Z
@pebwindkraft: Fair enough. However, it's not only about the reputation, it's also that comments shouldn't be used to answer questions. ;)
Murch
<script><multi-signature><input-script>
How do I fix my check-multisig if-else output script?
arik
# How do I fix my check-multisig if-else output script?

(Asked by: username_0 on 2018-03-25)

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
    
    OP_CHECKSIG
OP_ELSE
    OP_2
    
    
    
    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   

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

**Tags:** script, multi-signature, input-script

> **Comments:**
> **username_1**: I believe you may need two `OP_0`'s at the start: one to be consumed by `OP_IF`, and one which is popped off by `OP_CHECKMULTISIG`. I'll turn this into a full answer once I've identified the issue.  
> **username_0**: Thanks for your response! I just tried it, and I'm still getting the same error: `PUSH TRANSACTION ERROR: 16: MANDATORY-SCRIPT-VERIFY-FLAG-FAILED (SIGNATURE MUST BE ZERO FOR FAILED CHECK(MULTI)SIG OPERATION)`  
> **username_2**: ~30min in the Video Andreas explains the „IF“ handling of script. There must be a value before the IF statement. Either 1 for the If-branch or a 0 for the else branch. https://m.youtube.com/watch?v=yU3Sr07Qnxg  
> **username_3**: @username_2: that should be an answer. ;)  
> **username_2**: @murch: not chasing for credits anymore :-) Pieter and you have provided the answers, and this is just "a little bit" additional explanation, which might help the users to better understand the previous comments.  
> **username_3**: @username_2: Fair enough. However, it's not only about the reputation, it's also that comments shouldn't be used to answer questions. ;)  

---

## 1 answers

### Answer by username_3 on 2018-03-25. Score: 5 | Accepted answer

`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](https://bitcoin.stackexchange.com/a/2318/5406), and to username_1 for getting me on the right track.


---

