I would like to understand how miniscript works by coding it myself. As I started studying it, I realized that miniscript is more or less a 1-for-1 abstraction of a subset of Script and that the policy language implementation would shed more light on how miniscript is beneficial.

I haven't found any specification on how the policy language to miniscript language compilation works, just what I've found here: http://bitcoin.sipa.be/miniscript/

Besides the raw code, is there a description of how the policy language compiles to miniscript?


There is no specification, or really documentation, for Miniscript other than sipa's website. There is no "correct" or singular way to compile a miniscript policy into miniscript. The two existing implementations actually go about compiling in different ways, although both arrive at the same result (occasionally they differ, but the two results are semantically the same but are equally optimal). The only thing to be concerned about are whether the miniscript is optimal, and whether it is semantically the same as the policy.

Checking whether it is semantically the same is trivial. A miniscript can be "lifted" into policy by dropping tags and types so it is easy to check that a compiled miniscript is semantically the same as the policy.

Checking whether a given miniscript is optimal generally boils down to its size and expected satisfaction size. Smaller is better, so a miniscript whose end scriptWitness size is the smallest is considered to be optimal. When considering policies that include probabilities, the expected value should be taken into account.

As for compiling from policy into miniscript itself, there are different strategies. One strategy (which I believe is actually done in one of the libraries) is to construct every possible miniscript for a given policy and just select the optimal one.

| improve this answer | |
  • Surprised such a policy compilation strategy wouldn't result in combinatorial blowup, especially if the policy is complex. So would it be correct to say policy -> miniscript is one-to-many but miniscript lifted to policy is many to one? That would indeed be very convenient. – Jimmy Song Mar 13 at 22:53
  • It's many-to-many both ways, and indeed - the compiler (both the C++ one and the Rust one) effectively try every possibility (within a fixed set of known strategies), using a dynamic programming approach to (mostly) avoid the exponential blowup. I'll try writing my own answer later here. – Pieter Wuille Mar 14 at 0:46
  • @PieterWuille , any chance you could shed more light? I'd at least like to try to write something in Python so I can learn it. – Jimmy Song Mar 27 at 3:22
  • 1
    James Chiang has written a test Miniscript implementation in Python that may be of interest. Not going to help you on the Policy to Miniscript compiler question though. Andrew Poelstra did discuss the compiler at London Bitcoin Devs and at Advancing Bitcoin but not in the sort of detail you seem to be looking for. – Michael Folkson Apr 10 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.