script – Internal working of OP_IF and OP_ELSE


Internal working of conditionals in Bitcoin Script

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

To understand the functioning, let’s first contemplate a single-boolean conditioned execution of opcodes. The interpreter will iterate by means of 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 change into true and successfully execute “the else {} department”. On the contrary if it was true, it would change into 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: to be able to assist nested conditionals, this isn’t a single boolean however a vector of booleans which are 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 factor 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 signifies that nothing prevents to toggle two instances in a row. That’s, you may 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 will 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 truly 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 delicate fork (Tapscript) lifts the restrict of 201 OP codes. This was made potential by the elimination of the aforementioned quadratic behaviour.

Supply hyperlink

Leave a reply