Bitcoin Script provides some Boolean operators.
Since the raw script is typeless, we can do some special operations, such as adding and subtracting boolean values.
Can you implement OP_BOOLAND and OP_BOOLOR with other OPCODEs ?

Bitcoin Script provides some Boolean operators.
Since the raw script is typeless, we can do some special operations, such as adding and subtracting boolean values.
Can you implement OP_BOOLAND and OP_BOOLOR with other OPCODEs ?

venezia
2 years ago

This is another solution. : )
```
OP_MUL OP_0 OP_NUMEQUAL OP_NOT
```

light replied:

So simple! Nice!

adonsats replied:

you WIN :)
really, you win this time

light
2 years ago

OK, I think I understand now, maybe :-)
I'm probably going about this the wrong way but this does seem to work. I tried without duplicating the stack elements tho and I don't get the same results... Not sure exactly why yet :-)
"x" "y" OP_DUP OP_IF OP_SWAP OP_DUP OP_IF OP_DROP OP_DROP OP_1 OP_ELSE OP_DROP OP_DROP OP_0 OP_ENDIF OP_ELSE OP_DROP OP_DROP OP_0 OP_ENDIF
Script: OP_1 OP_1 OP_DUP OP_IF OP_SWAP OP_DUP OP_IF OP_DROP OP_DROP OP_1 OP_ELSE OP_DROP OP_DROP OP_0 OP_ENDIF OP_ELSE OP_DROP OP_DROP OP_0 OP_ENDIF
Stack: <Buffer 01> Alt: undefined
Script: OP_1 OP_7 OP_DUP OP_IF OP_SWAP OP_DUP OP_IF OP_DROP OP_DROP OP_1 OP_ELSE OP_DROP OP_DROP OP_0 OP_ENDIF OP_ELSE OP_DROP OP_DROP OP_0 OP_ENDIF
Stack: <Buffer 01> Alt: undefined
Script: OP_0 OP_7 OP_DUP OP_IF OP_SWAP OP_DUP OP_IF OP_DROP OP_DROP OP_1 OP_ELSE OP_DROP OP_DROP OP_0 OP_ENDIF OP_ELSE OP_DROP OP_DROP OP_0 OP_ENDIF
Stack: <Buffer > Alt: undefined
Script: OP_0 OP_0 OP_DUP OP_IF OP_SWAP OP_DUP OP_IF OP_DROP OP_DROP OP_1 OP_ELSE OP_DROP OP_DROP OP_0 OP_ENDIF OP_ELSE OP_DROP OP_DROP OP_0 OP_ENDIF
Stack: <Buffer > Alt: undefined

venezia tipped:

0.01
USD

2 years ago
light replied:

I guess having Buffer null is not quite the same as having Buffer 00 but it's pretty much the same result. Likely super inefficient though. :-)

light replied:

Ah yes, so just replace OP_0 with 00 and it gives the result intended. I'm not sure why this is the way it behaves but whatever!

venezia replied:

Great! I think your solution should be correct. It conforms to the definition of bool and: If neither is false, return true.

venezia replied:

All values equal to zero is considered false, and the null buffer is the most common false value.

light
2 years ago

This is as simple as:
$ node index.js
Script: 01 00 OP_AND
Stack: <Buffer 00> Alt: undefined -- 0
Binary: 00000000
$ node index.js
Script: 01 00 OP_OR
Stack: <Buffer 01> Alt: undefined -- 1
Binary: 00000001

venezia replied:

I can give a counterexample ðŸ˜‰
01 02 OP_AND
stack: [ <Buffer 00> ]
01 02 OP_BOOLAND
stack: [ <Buffer 01> ]

light replied:

LOL, yes it breaks if the inputs are anything other than 0 or 1, however, is that really a boolean operation at that point?

adonsats
2 years ago

ðŸ¤“ this is flip-flop logic circuit ðŸ¤ª