ivy-lang / ivy-bitcoin

A high-level language and IDE for writing Bitcoin smart contracts.
MIT License
293 stars 38 forks source link

Add more Integer operations (+, -, >, <, min, max) #5

Open TBB1987 opened 6 years ago

TBB1987 commented 6 years ago

Hi, I'm wondering if Ivy can handle other arithmetic functions and integer values, to write functions like is_greater(valA: int, valB: int)? I know that the related opcodes do already exist (OP_MIN), but in the Ivy docs I've not found nothing similar. Thank you

danrobinson commented 6 years ago

We intentionally left out the Integer types and the Integer operations because, as far as we can tell, there is no practical use for them. If anyone can think of an application for them, or any reason to include them, I'd be happy to prioritize adding them back in.

Melvillian commented 6 years ago

@danrobinson < (OP_LESSTHAN) would be useful for doing block chain-adjudicated policy limits. For instance, imagine Bob manages a wallet and wants Alice to also sign off for transactions sent from the wallet, but Bob doesn't want to go through the hassle of bothering Alice to sign when spending unspents whose value is under some threshold (e.g. < 0.01 BTC). They would need a Script like:

<val> OP_LESSTHAN <threshold>
IF
  <bob_public_key> OP_CHECKSIGVERIFY
ELSE
  2 <bob_public_key> <alice_public_key> 2 OP_CHECKMULTISIG
END_IF
danrobinson commented 6 years ago

How does <val> even get in there? There is currently no way in Bitcoin Script to get the value of the output being spent. The only way would be to hardcode it in at the time the address is created, in which case the whole exercise is pointless, since the only permissible path would be known at compile time, so you can save on bytes by only allowing one path or the other. (This also would mean that there would be no way to guarantee that value in the script matched the value of the unspent output; a sender could easily send more or less BTC to the same address.)

Melvillian commented 6 years ago

Thanks for pointing that out @danrobinson. I was assuming since segwit inputs commit to the value being spent that that information would be available to Script, but that's clearly not true. Any idea if this is on the roadmap for a future segwit tx version upgrade?

danrobinson commented 6 years ago

Update: I've added an Integer type, along with the size function (http://docs.ivy-lang.org/bitcoin/language/Functions.html). This is mostly because Lightning uses the OP_SIZE (as part of a branch condition, so in a way that doesn't directly translate to Ivy, but that's another topic). Right now it can only be compared with ==, but will consider adding support for other comparison and arithmetic operators if there seems to be a use for them.