@venezia is on PowPing!

PowPing is a place where you can earn Bitcoin simply by socializing, for FREE.
Never tried Bitcoin? It's OK! Just come, socialize, and earn Bitcoin.
Check out venezia's activities

Bitcoin Script

visit channel home
Total Economy: 0.06 USD
Like most other commonly used languages, we can implement very complex logic using control flow operators in Bitcoin Script. Can you use Bitcoin Script implement the classic foobar function? It inputs a number, and if the number is divisible by 3, returns "foo". If it is divisible by 5, returns "bar". If it can be divisible by 3 and 5, it returns "foobar". (Just keep the string as it is, no need to use hex bytes.)
Thinking about it even more, my last solution used 3 conditional branches with 3 additional math/comparison operations. If we accept that the final word could be "foo" and/or "bar" output separately, that can be reduced further to 2 and 2 -- 0F OP_DUP OP_3 OP_MOD OP_NOTIF 666f6f OP_SWAP OP_ENDIF OP_5 OP_MOD OP_NOTIF 626172 OP_ENDIF Optimization of Bitcoin Script is fun and really gets the brain going!
light replied:
(Technically, needs another OP_SWAP on the end to output foo before bar in the case that the input is divisible by both 3 and 5). I wish PowPing comments had an edit feature. :-)
adonsats replied:
as long as you use OP_NOTIF we are friend šŸ˜Ž when miniforth will FACTOR it, u will NERVER code in c++... NERVER šŸ¤
adonsats replied:
where is OP_CAT to sum foobar ?
light replied:
Well, that is what I stated. If we can accept foo and bar separately as outputs from the stack (in this hypothetical example) then no concatenation necessary. It saves on another comparison and conditional.
and with this code: OP_5 OP_5 OP_LESSTHAN OP_IF 0xff OP_ELSE OP_1 OP_ENDIF OP_DROP not working also. some explanation ?
light replied:
OP_5 OP_5 OP_LESSTHAN OP_IF FF OP_ELSE OP_1 OP_ENDIF OP_DROP
adonsats replied:
very clear now.
OP_5 OP_5 OP_LESSTHAN OP_IF 0xff OP_ELSE 0xee OP_ENDIF this code do not work. what is the problem here ?
light replied:
OP_5 OP_5 OP_LESSTHAN OP_IF FF OP_ELSE EE OP_ENDIF
adonsats replied:
okkkk. thanks ! hard to bee a hexadecimal coder šŸ¤Ŗ
I think I finally got it thanks to sCrypt BSV library testing! The numbers have to be expressed in hex form, for example, 15 (0F): 0F OP_DUP OP_3 OP_MOD OP_1 OP_LESSTHAN OP_IF 666f6f OP_SWAP OP_1 OP_TOALTSTACK OP_ENDIF OP_5 OP_MOD OP_1 OP_LESSTHAN OP_IF 6261720a OP_FROMALTSTACK OP_1 OP_ADD OP_TOALTSTACK OP_ENDIF OP_FROMALTSTACK OP_1 OP_GREATERTHAN OP_IF OP_CAT OP_ENDIF
venezia tipped:
0.03 USD
1 year ago
venezia replied:
Cool!
adonsats replied:
endif inside script! im learning... still cheaper šŸ¤“
light replied:
Yeah, I don't think you can nest IF/ELSE/ENDIF clauses because ENDIF operation is invalid if not preceded by an IF or ELSE. At least according to the Wiki documentation. "OP_ENDIF -- Ends an if/else block. All blocks must end, or the transaction is invalid. An OP_ENDIF without a prior matching OP_IF or OP_NOTIF is also invalid."
light replied:
OK, NOTIF can make that a little more efficient -- "x" OP_DUP OP_3 OP_MOD OP_NOTIF 666f6f OP_SWAP OP_1 OP_TOALTSTACK OP_ENDIF OP_5 OP_MOD OP_NOTIF 6261720a OP_FROMALTSTACK OP_1 OP_ADD OP_TOALTSTACK OP_ENDIF OP_FROMALTSTACK OP_1 OP_GREATERTHAN OP_IF OP_CAT OP_ENDIF 666f6f = foo, 6261720a = bar (in hex) input 03 gives: <Buffer 66 6f 6f> input 05 gives: <Buffer 62 61 72 0a> input 07 gives: Nothing on stack input 0F gives: <Buffer 66 6f 6f 62 61 72 0a> I guess there's probably other ways to solve this but I like using the alt stack. :-)
light replied:
Oops, 0a shouldn't be on the end of bar. It's 62 61 72. Other than that it's good. :-)
adonsats replied:
hehehe. your are very close to my code šŸ¤
light replied:
Yes, indeed. Also, no alt stack necessary -- 0F OP_DUP OP_3 OP_MOD OP_NOTIF 666f6f OP_SWAP OP_ENDIF OP_5 OP_MOD OP_NOTIF 626172 OP_ENDIF OP_DEPTH OP_1 OP_GREATERTHAN OP_IF OP_CAT OP_E NDIF
adonsats tipped:
0.01 USD
1 year ago
adonsats replied:
OP_5 and your code is not working. the first fromaltstack meet void. some test is necessary.
adonsats replied:
what tool are you using ? VScode ?
light replied:
05 OP_DUP OP_3 OP_MOD OP_NOTIF 666f6f OP_SWAP OP_ENDIF OP_5 OP_MOD OP_NOTIF 626172 OP_ENDIF OP_DEPTH OP_1 OP_GREATERTHAN OP_IF OP_CAT OP_ENDIF gives <Buffer 62 61 72> 03 gives <Buffer 66 6f 6f> 15 gives <Buffer 66 6f 6f 62 61 72> 04 gives nothing on stack.
light replied:
I'm using bsv library. I edit index.js like venezia pointed out in another post. I put this at the end of index.js -- let c = '0F OP_DUP OP_3 OP_MOD OP_NOTIF 666f6f OP_SWAP OP_ENDIF OP_5 OP_MOD OP_NOTIF 626172 OP_ENDIF OP_DEPTH OP_1 OP_GREATERTHAN OP_IF OP_CAT OP_ENDIF' let s = bsv.Script.fromASM(c); let i = bsv.Script.Interpreter(); i.flags = bsv.Script.Interpreter.SCRIPT_ENABLE_MAGNETIC_OPCODES | bsv.Script.Interpreter.SCRIPT_ENABLE_MONOLITH_OPCODES; i.script = s; i.evaluate(); console.log(i);
adonsats replied:
šŸ‘
adonsats replied:
oouuuuuulala! šŸ˜‚ that gives me some idea! with some html5 GUI we can visualise it like in scypt.io excellent js code.
: 3MOD 3 MOD ; : 5MOD 5 MOD ; x DUP 3MOD NOTIF "foo" ELSE DUP 5MOD NOTIF "BAR" ELSE DUP DUP 3MOD SWAP 5MOD AND IF "foobar" END_IF END_IF END_IF šŸ¤Ŗ
venezia tipped:
0.03 USD
1 year ago
adonsats replied:
there is not OP_THEN in bitcoin script ! not sure how many END_IF will end the script and why...but your comment are welcome šŸ˜Ž
light replied:
Is it possible to define new Forth words in Bitcoin script? I thought we were limited to the op codes we could use...
adonsats replied:
FORTH is a problem oriented programming language.
Once again, I'm a rank amateur here... I'll work on actually verifying these answers soon. :-) "x" OP_3 OP_MOD OP_1 OP_LESSTHAN OP_IF "foo" OP_5 OP_MOD OP_1 OP_LESSTHAN OP_IF "foobar" OP_ENDIF OP_ELSE OP_5 OP_MOD OP_1 OP_LESSTHAN OP_IF "bar" OP_ENDIF OP_ENDIF
venezia tipped:
0.02 USD
1 year ago
adonsats replied:
vrerify anf tell us...
light replied:
I realized on my way to get some lunch that my answer was not correct. "x" OP_3 OP_MOD OP_1 OP_LESSTHAN OP_IF "foo" OP_5 OP_MOD OP_1 OP_LESSTHAN OP_IF "bar" OP_CAT OP_ENDIF OP_ELSE OP_5 OP_MOD OP_1 OP_LESSTHAN OP_IF "bar" OP_ENDIF OP_ENDIF I'll work on actually verifying next. :-)
light replied:
Perhaps it's simpler still -- "x" OP_3 OP_MOD OP_IF "foo" OP_5 OP_MOD OP_IF "bar" OP_CAT OP_ENDIF OP_ELSE OP_5 OP_MOD OP_IF "bar" OP_ENDIF OP_ENDIF
light replied:
Wow, each time I look at it I see more problems. I need to figure out how to test/debug somehow -- "x" OP_DUP OP_3 OP_MOD OP_IF "foo" OP_DUP OP_5 OP_MOD OP_IF "bar" OP_CAT OP_ENDIF OP_ELSE OP_5 OP_MOD OP_IF "bar" OP_ENDIF OP_ENDIF
venezia replied:
Keep trying : D