<p>A <strong>valid</strong> Miniscript is any correctly typed Miniscript.</p>
<p>A <strong>sane</strong> Miniscript needs to be <strong>valid</strong>, consensus and
standardness-compliant (e.g. number of operations and script size),
have non-malleable solutions, not mix different timelock units on a particular branch of the script and not have duplicate keys.</p>
<p>From <a href="https://github.com/bitcoin/bitcoin/blob/master/src/script/miniscript.h#L866" rel="nofollow noreferrer">miniscript.h</a>:</p>
<pre><code>//! Whether the apparent policy of this node matches its script semantics. Doesn't guarantee it is a safe script on its own.
bool IsSaneSubexpression() const { return ValidSatisfactions() &amp;&amp; IsNonMalleable() &amp;&amp; CheckTimeLocksMix() &amp;&amp; CheckDuplicateKey(); }

//! Check whether this node is safe as a script on its own.
bool IsSane() const { return IsValidTopLevel() &amp;&amp; IsSaneSubexpression() &amp;&amp; NeedsSignature(); }
</code></pre>
<p>A Miniscript is sane if it is of the form X OR Y with X containing a block check and Y containing a time check but X AND Y would be unspendable. e.g. <code>(A signs) OR ((B signs) AND (timelock) AND (heightlock))</code> would not be considered sane. Even though it can be spent by A, its apparent policy doesn't match the actual script (whose actual policy is just &quot;A signs&quot;, because the other branch is unusable)</p>
<p>The <code>Node::IsSane()</code> method has the docstring &quot;Whether the apparent policy of this node matches its script semantics.&quot;</p>
<p>Thanks to participants for answering this at the <a href="https://bitcoincore.reviews/24148" rel="nofollow noreferrer">Bitcoin Core PR review club</a>.</p>

CC BY-SA 4.0
2022-05-25T16:26:47.547Z
2022-09-13T10:10:08.943Z
2022-09-13T10:10:08.943Z
Michael Folkson
<p>A WIP &quot;Work In Progress&quot; that may be of interest to you is the Miniscript Templates repo: <a href="https://github.com/Blockstream/miniscript-templates" rel="nofollow noreferrer">https://github.com/Blockstream/miniscript-templates</a></p>
<p>One of the goals is to curate miniscript policies that are &quot;sane&quot;, &quot;valid&quot; and &quot;useful&quot;. :)</p>
<p>Feel free to comment on PRs as they are posted!</p>
<p><a href="https://github.com/Blockstream/miniscript-templates/pull/2" rel="nofollow noreferrer">https://github.com/Blockstream/miniscript-templates/pull/2</a></p>
<p><a href="https://github.com/Blockstream/miniscript-templates" rel="nofollow noreferrer">miniscript-templates</a></p>

CC BY-SA 4.0
2023-11-30T16:10:30.707Z
2023-11-30T16:10:30.707Z
RandyMcMillan
<p>What is a <strong>sane</strong> Miniscript? How does it differ to a <strong>valid</strong> Miniscript?</p>
<p>This was asked during a Bitcoin Core PR <a href="https://bitcoincore.reviews/24148" rel="nofollow noreferrer">review club</a> on Miniscript hosted by stickies-v on May 18th 2022.</p>

<script><terminology><miniscript>
What is a sane Miniscript? How does it differ to a valid Miniscript?
Michael Folkson
# What is a sane Miniscript? How does it differ to a valid Miniscript?

(Asked by: username_0 on 2022-05-25)

What is a **sane** Miniscript? How does it differ to a **valid** Miniscript?

This was asked during a Bitcoin Core PR [review club](https://bitcoincore.reviews/24148) on Miniscript hosted by stickies-v on May 18th 2022.

**Tags:** script, terminology, miniscript


---

## 2 answers

### Answer by username_0 on 2022-05-25. Score: 2 | Accepted answer

A **valid** Miniscript is any correctly typed Miniscript.

A **sane** Miniscript needs to be **valid**, consensus and
standardness-compliant (e.g. number of operations and script size),
have non-malleable solutions, not mix different timelock units on a particular branch of the script and not have duplicate keys.

From [miniscript.h](https://github.com/bitcoin/bitcoin/blob/master/src/script/miniscript.h#L866):

```
//! Whether the apparent policy of this node matches its script semantics. Doesn't guarantee it is a safe script on its own.
bool IsSaneSubexpression() const { return ValidSatisfactions() && IsNonMalleable() && CheckTimeLocksMix() && CheckDuplicateKey(); }

//! Check whether this node is safe as a script on its own.
bool IsSane() const { return IsValidTopLevel() && IsSaneSubexpression() && NeedsSignature(); }

```

A Miniscript is sane if it is of the form X OR Y with X containing a block check and Y containing a time check but X AND Y would be unspendable. e.g. `(A signs) OR ((B signs) AND (timelock) AND (heightlock))` would not be considered sane. Even though it can be spent by A, its apparent policy doesn't match the actual script (whose actual policy is just "A signs", because the other branch is unusable)

The `Node::IsSane()` method has the docstring "Whether the apparent policy of this node matches its script semantics."

Thanks to participants for answering this at the [Bitcoin Core PR review club](https://bitcoincore.reviews/24148).


---

### Answer by username_1 on 2023-11-30. Score: 1

A WIP "Work In Progress" that may be of interest to you is the Miniscript Templates repo: <https://github.com/Blockstream/miniscript-templates>

One of the goals is to curate miniscript policies that are "sane", "valid" and "useful". :)

Feel free to comment on PRs as they are posted!

<https://github.com/Blockstream/miniscript-templates/pull/2>

[miniscript-templates](https://github.com/Blockstream/miniscript-templates)


---

