1

According to this Q&A there are no loops in bitcoin script: The Bitcoin scripting system is purposefully not Turing-complete - why?

I was reading the comments in a reddit post and realized few for loops could be replaced by if else statements to achieve something similar: https://www.reddit.com/r/Bitcoin/comments/rikjh0/any_one_smart_enough_to_answer_this/hoy8z52

What are the issues with this approach? Has anyone tried this?

1
  • 5
    Unrolling a loop isn't really a loop... Also, in some languages, it is possible to have an infinite for loop, which is not possible in Bitcoin.
    – Ava Chow
    Commented Dec 19, 2021 at 2:54

2 Answers 2

1

You can code the loop...

for i=1 to 10
   print "Hello #",i
endfor

...as...

print "Hello #1"
print "Hello #2"
print "Hello #3"
print "Hello #4"
print "Hello #5"
print "Hello #6"
print "Hello #7"
print "Hello #8"
print "Hello #9"
print "Hello #10"

They are equivalent programs. You can do this even for complicated loops with a variable end point...

for i=1 to y
   print "Hello #",i
end for

...could be rewritten as...

if y>=1 then print "Hello #",1
if y>=2 then print "Hello #",2
if y>=3 then print "Hello #",3
...(up to the maximum possible value of y)...

This is called "loop unrolling" and you can theoretically do this for any finite loop, but as you can see in the examples above the code can get very large.

In bitcoin, the size of your script is both theoretically and practically limited in several places, so this ultimately limits the maximum number of loop iterations you can unroll in a bitcoin script, but it is possible to implement a low iteration count loop like for i=1 to 5 in bitcoin script using unrolling.

5
  • How about this? One is using loop and other isn't: pastebin.com/raw/HzzAx96B
    – user103136
    Commented Dec 24, 2021 at 15:30
  • @Prayank That shows two examples of how a loop can be implemented. The first is using recursion, and the second is using a while loop (same as above). Both of these loops could be trivially unrolled since in both cases the number of times the loop is iterated is only 2. .
    – bigjosh
    Commented Dec 28, 2021 at 17:55
  • Makes sense. I wish at least the recursion part was possible with Bitcoin scripts.
    – user103136
    Commented Dec 29, 2021 at 0:47
  • It is possible to implement recursion on the bitcoin blockchain, but only if you are creative and patient. Do you have a specific problem you are trying to solve?
    – bigjosh
    Commented Dec 30, 2021 at 2:16
  • I would love to read more about ways to implement recursion in Bitcoin. Specific problem: I think if-else, CHECKSIG and recursion could be used solve lot of problems but I am interested in improving my project which is an app to bet on cricket matches using bitcoin and dlc. Can share details in chat if you are interested.
    – user103136
    Commented Dec 30, 2021 at 4:05
1

Loops (while loops) in this sense are meant as potentially infinite. For loops are not such loops. In other words, in what is here called for loop, one does know the number of iterations of the execution of the inner block of the loop WITHOUT inspecting the code of the inner block. In what is here called while loop this number of iterations is not known without inspecting the code of the inner block. The difference then is that in case of while loops it is in general not possible to decide whether such a loop ends or not (aka halting problem).

As Andrew mentioned above, this should not be confused with a specific syntax of some languages (i.e. where you write for keyword, it does not imply you are writing what I refer to here as for loop). If this is confusing, you can look up Pascal language and understand those types of loops as Pascal loops.

Languages that do not have while loops are not Turing complete. And Bitcoin script does not have such a loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.