Flowcharts?
Yes, why flowcharts?
I'll try to elucidate why and how we had this crazy idea of making flowcharts into as a complementary product of the AI code assistant. It all started with:
Taproot
Yes! The Taproot soft fork encodes bitcoin script into a Merkle Tree with MAST so we don't need to publish the whole bitcoin script and we can reveal just the necessary parts of the contract and the proofs of inclusion in the tree. This way, it is possible to have arbitrarily long contracts and the transaction can look just like any other regular transaction. But if it is possible to encode any contract in a tree, it means also that any contract can be easily represented as a flowchart.
Well, what is easier to understand, this code below or the tree-like flowchart following it?
For the sake of curiosity, this is the BOLT #3 received HTLC policy example from Sipa's documentation.
Miniscript policy:
or(pk(key_revocation),and(pk(key_remote),or(and(pk(key_local),hash160(H)),older(1008))))
Script equivalent:
<key_remote> OP_CHECKSIG OP_NOTIF
<key_revocation> OP_CHECKSIG
OP_ELSE
OP_IF
OP_DUP OP_HASH160 <HASH160(key_local)> OP_EQUALVERIFY OP_CHECKSIGVERIFY
OP_SIZE <20> OP_EQUALVERIFY OP_HASH160 <h> OP_EQUAL
OP_ELSE
<f003> OP_CHECKSEQUENCEVERIFY
OP_ENDIF
OP_ENDIF
Miniscript flowchart:
This is already much easier to understand. However, while I was doing it, I realized that this chart had too many "and"s and "or"s. Do we need so many levels or can I do a boolean symbolic simplification? Well, you guessed right, we can simplify it and not only this example. Miniscript has limitations that can make a direct flowchart transliteration unreasonably complex, for example:
-
Any "
or
" or "and
" can have only two arguments. So three "or
"s have to be nested likeor(pk(key_1),or(pk(key_2),pk(key_3))).
-
Thresh
function can be translated intoor
orand
if threshold parameter is 1 or equal to the number of policies, respectively.
-
These two mean that
thresh(1,pk(key_1),or(pk(key_2),pk(key_3)))
could just be a big and simpleor
.
So using this knowledge of equivalence and a library of symbolic mathematics, we can manipulate a boolean expression to get into the most mathematically simple form possible. And for the example above this is the result:
This changes the game completely! A BOLT #3 received HTLC policy sounded like rocket science, but now it is really simple to understand. This new simplified version has only two layers of decision and you can go with your fingers through the chart and check every condition before committing to any contract without a bitcoin core developer by your side.
The AI Hallucination Problem
When I was deciding what to do for the SatsHack hackathon, I had this idea of an AI specialized in miniscript. However, unlike most code, we cannot err a smart contract because your funds are at risk, which means that the AI cannot possibly hallucinate. Since this is a still unsolvable problem in AI research, I had to mitigate possible hallucinations on the user's end, by making a sanity check easy. And that's how I remembered my old idea of flowcharts to understand scripts.
That is why we did the flowchart and not only the AI and now that we did, I think it will be able to transform any layperson into a miniscript coder. What do you guys think?