script – Interior working of OP_IF and OP_ELSE


Interior working of conditionals in Bitcoin Script

The conditionals in Bitcoin Script are conceptually applied utilizing a stack of booleans, every boolean indicating whether or not to execute the present opcode when iterating via the Script for every depth of the conditional (nested IF / ELSE).

To understand the functioning, let’s first think about a single-boolean conditioned execution of opcodes. The interpreter will iterate via the Script by setting the boolean to true (do execute ops) initially. When encountering an IF it would set the boolean to the worth of the pushed knowledge previous the IF, casted to a boolean (CastToBool within the reference implementation).
If this was true it would proceed the execution of the next opcodes, successfully executing “the if {} department”.
If it was false, it would droop execution till …. It encounters an ELSE! Encountering an ELSE will toggle the worldwide boolean suspending execution. If it was beforehand false, it would grow to be true and successfully execute “the else {} department”. On the contrary if it was true, it would grow to be false and droop execution of “the else {} department”.

With a little bit of ASCII artwork (fExec is the worldwide boolean telling whether or not to really execute the OPs):

Worth of fExec:  true    true      true         true  true    true         false      false            ???

Worth of fExec:  true    true      true         true  false    false       true      true             ???


Word how i marked the worth of fExec as ??? when encountering OP_ENDIF. That is as a result of the actual implementation is barely extra sophisticated: so as to assist nested conditionals, this isn’t a single boolean however a vector of booleans which might be used to find out execution. That’s:

  • OP_IF will push a brand new boolean to the vector
  • OP_ELSE will toggle the final boolean of the vector
  • OP_ENDIF will pop the final ingredient of the vector

Going additional

A number of ELSE for a single IF

Word that the implementation of the ELSE being a single toggle it implies that nothing prevents to toggle two instances in a row. That’s, you’ll be able to have many following ELSE opcodes earlier than the ENDIF. Two following ELSEs would successfully toggle the final boolean twice, therefore executing the next opcodes as it could for “the if {} department”.

Quadratic behaviour when computing fExec from the vector of booleans

The implementation of the conditional logic was modified in 2020 by Pieter Wuille to repair a quadratic behaviour discovered by Segio Demian Lerner.
Subsequently the precise implementation doesn’t really use a stack for the booleans anymore, moderately it shops the “high boolean” in a devoted knowledge construction imitating the earlier implementation.

Barely associated Tapscript rule

The proposed new addition to the Script guidelines as a part of the proposed Taproot comfortable fork (Tapscript) lifts the restrict of 201 OP codes. This was made doable by the elimination of the aforementioned quadratic behaviour.

Supply hyperlink

Leave a reply