CHIP 2021-05 Bounded Looping Operations

I want to add to the discussion that loops would make array-datastructure abstractions much more useful in high level languages compiled to BCH script

‘for each element in array do x’, currently variable sized array abstractions are not really possible.

IMO the usecases in the CHIP should be updated, different people seem to have the understanding that this functionality isn’t really required yet by contract authors in practice - i strongly disagree but the CHIP is very much lacking in this regard as the use-cases are described in a very abstract way and haven’t been updated since 2021…

It would also be good to refer to the native introspection CHIP to emphasize that aggregation was expected to be delivered in this way:

While it would be possible to define aggregate operations like OP_TXINPUTVALUE and OP_TXOUTPUTVALUE to make this derived state accessible to all contracts, this proposal considers standardization of any derived state to be premature

now there’s also aggregation of CashTokens in the in- and outputs so it saves having to introduce atleast 4 different opcodes

Further it would become possible to construct tx.hashOutputs and tx.hashPrevouts like we had before native introspection. This could be useful in case a multi-step contract can commit to later payouts, like demo-ed in my ‘upgraded sha gate’ contract

5 Likes

Seems like an obvious include to me. Every programming language basically has/needs loops, I don’t know why BCH script would be any different. As long as it’s limited by VM Limits & fees (same as everything else) then I can’t see a problem.

I agree with Mathieu that some more examples with CashTokens would be helpful.

If we need examples of use cases, consider that the BLISS Jessica/Velma CashTokens will have additional functionality (eventually), and we’re going to need to loop over them.

Loop over BLISS CashTokens
if (committment) <= 100 then --do Jessica stuff--
if (committment) <= 200 then --do Velma stuff--
if (committment) <= 300 then --do ...2026 upgrade mascot stuff--

That kind of thing.

2 Likes

Regarding the usecases I’d like to add that looping operations are especially useful to have variable length Array types in high-level languages like CashScript. Then you can do operations for each element in the array.

With recent discussion of BitCann to create a domain name system on BCH, an issue has been a way to enforce UTF‑8 hex strings only contain allowed characters. Ideally this would be done with a looping construction performing checks for each character of the UTF‑8 hex string.

As said higher loops also allow to emulate math constructions like exponentiation for variable exponents.

I think loops would be especially useful for implementing ZK-cryptography operations in script

4 Likes