<p>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.</p>

<p>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.</p>

<p>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 <code>scriptWitness</code> size is the smallest is considered to be optimal. When considering policies that include probabilities, the expected value should be taken into account.</p>

<p>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.</p>

CC BY-SA 4.0
2020-03-13T22:53:13.980Z
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
CC BY-SA 4.0
2020-03-14T00:46:11.630Z
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
CC BY-SA 4.0
2020-03-27T03:22:10.143Z
@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
CC BY-SA 4.0
2020-04-10T19:25:44.753Z
James Chiang has written a test [Miniscript implementation](https://github.com/bitcoin/bitcoin/pull/17975) 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](https://diyhpl.us/wiki/transcripts/london-bitcoin-devs/2020-02-04-andrew-poelstra-miniscript/) and at [Advancing Bitcoin](https://diyhpl.us/wiki/transcripts/advancing-bitcoin/2020/2020-02-07-andrew-poelstra-miniscript/) but not in the sort of detail you seem to be looking for.
Michael Folkson
CC BY-SA 4.0
2020-03-13T18:54:44.837Z
2020-03-13T18:54:44.837Z
Ava Chow
<p>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.</p>

<p>I haven't found any specification on how the policy language to miniscript language compilation works, just what I've found here: <a href="http://bitcoin.sipa.be/miniscript/" rel="nofollow noreferrer">http://bitcoin.sipa.be/miniscript/</a></p>

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

<script><miniscript>
Where can I find the miniscript policy language specification?
Jimmy Song
# Where can I find the miniscript policy language specification?

(Asked by: username_0 on 2020-03-13)

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?

**Tags:** script, miniscript


---

## 1 answers

### Answer by username_1 on 2020-03-13. Score: 5

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.

> **Comments:**
> **username_0**: 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.  
> **username_2**: 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.  
> **username_0**: @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.  
> **username_3**: James Chiang has written a test [Miniscript implementation](https://github.com/bitcoin/bitcoin/pull/17975) 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](https://diyhpl.us/wiki/transcripts/london-bitcoin-devs/2020-02-04-andrew-poelstra-miniscript/) and at [Advancing Bitcoin](https://diyhpl.us/wiki/transcripts/advancing-bitcoin/2020/2020-02-07-andrew-poelstra-miniscript/) but not in the sort of detail you seem to be looking for.  

---

