Miniscript Fragment
One building block of that dialect, representing a single condition.
Basically
A Miniscript fragment is one building block in Bitcoin's Miniscript language. It describes a single spending condition or a rule that joins other conditions.
Key Takeaways
- A Miniscript fragment is an atomic unit of Miniscript that represents a single spending condition, such as a key check, hash preimage, or timelock. It can also be a logical combinator, such as and, or, or threshold, that joins other fragments together.
- Every fragment carries a type from the system B, V, K, or W, which describes its stack behaviour. These types enforce composition rules at compile time so that only valid Bitcoin Script can be produced.
- The policy compiler converts human-readable spending policies into an optimised tree of fragments. It chooses the encoding that minimises witness size and transaction fees for the most likely spending path.
What Is a Miniscript Fragment?
A Miniscript fragment is the smallest meaningful piece of a Miniscript expression. Each fragment maps directly to a specific sequence of Bitcoin Script opcodes. Its formally defined properties let you reason about correctness, security, and spending cost without executing the script.
Pieter Wuille, Andrew Poelstra, and Sanket Kanjalkar designed Miniscript because raw Bitcoin Script is powerful but dangerous to compose by hand. A developer writing a 2-of-3 multisig with a timelock fallback in raw Script must manually arrange opcodes like OP_IF, OP_CHECKMULTISIG, and OP_CHECKSEQUENCEVERIFY while ensuring the stack is clean at every branch. Fragments abstract this away as proven-correct building blocks, and the type system guarantees that combining them produces valid Script.
Fragments sit at the middle layer of a three-tier architecture. At the top, policy language lets users express spending conditions in plain terms, and the policy compiler translates these into a tree of Miniscript fragments. At the bottom, each fragment maps one-to-one to Bitcoin Script opcodes that the network can verify.
How It Works
Fragments fall into two broad categories. Leaf fragments encode a single condition, while combinator fragments join sub-expressions together with logical operators.
Leaf Fragments
Leaf fragments represent the atomic spending conditions that Bitcoin supports. They sit at the tips of a Miniscript expression tree.
Key check fragments verify signatures. They require a valid signature for a given public key:
Hash preimage fragments require the spender to reveal a value. The revealed value's hash must match a committed digest:
Timelock fragments enforce time-based spending restrictions. They control when spending can occur:
Multisig fragments handle multi-party signing directly. They encode conditions that involve multiple signers:
Combinator Fragments
Combinator fragments join sub-expressions with logical operators. They build complex spending policies from simpler pieces:
The Type System
Every Miniscript fragment has one of four base types that describe its stack behaviour. The type system makes fragments safely composable: the compiler checks types at every node in the expression tree and rejects combinations that would produce invalid Script.
Beyond the four base types, fragments carry type modifiers that encode additional guarantees: z (zero-arg, consumes nothing from the stack), o (one-arg), n (non-zero on satisfaction), d (has a dissatisfaction path), and u (unit, pushes exactly 1 on satisfaction). Malleability properties s (signed), f (forced), and e (expressive) are tracked separately to ensure that the final script cannot be manipulated by a third party.
Wrapper Fragments
Wrappers are special fragments that transform the type of a sub-expression without changing its spending semantics, written as a single-letter prefix followed by a colon. BIP 379 defines ten wrappers: a:, s:, c:, d:, v:, j:, n:, t:, l:, and u:. Examples follow:
Wrappers can be chained by dropping the intermediate colons. The expression dv:older(144) applies d: then v: to the older(144) fragment. Three wrappers (t:, l:, and u:) are syntactic sugar for common patterns.
The common shorthand pk(key) is actually c:pk_k(key): the raw key fragment pk_k wrapped with c: to add an OP_CHECKSIG. The shorthand pkh(key) is c:pk_h(key).
From Policy to Fragment Tree
The policy compiler translates human-readable policies into optimised fragment trees. You can see this in a 2-of-3 multisig with a timelock recovery path:
The 99@ and 1@ annotations tell the compiler that the multisig path is used 99% of the time, so it should be optimised for lower witness size. The compiler explores many valid fragment encodings and selects the one that minimises spending cost for the weighted probabilities.
The compiler can produce output using Miniscript fragments. A possible compiled output follows:
Each fragment in the tree maps to specific opcodes. The andor fragment becomes an OP_NOTIF/OP_ELSE/OP_ENDIF structure, pk(Alice) becomes <Alice_pubkey> OP_CHECKSIG, s:pk(Bob) swaps the stack and checks Bob's key, and older(52560) becomes <52560> OP_CHECKSEQUENCEVERIFY.
Fragment-to-Script Mapping
Every fragment has a deterministic mapping to Bitcoin Script opcodes, which is one of Miniscript's defining properties. The table shows common fragments and their Script translations:
Descriptor Wallets
Descriptor wallets use output descriptors to define exactly how funds can be spent. Miniscript fragments are the building blocks inside descriptors like wsh() and tr(). A descriptor such as wsh(andor(pk(A), s:pk(B), and_v(v:pk(C), older(1000)))) precisely defines a spending policy using composable fragments, and any wallet that understands Miniscript can sign for it.
Corporate Treasury
Businesses can encode governance rules directly into Bitcoin spending conditions. A corporate treasury might require 3-of-5 board members to approve spending, with a 6-month timelock fallback to 2-of-5. The thresh and older fragments encode this logic without trusted intermediaries.
Inheritance Planning
Time-locked recovery paths let heirs access funds after a predefined period without the original key holders. Combining multi-party key checks with the older fragment creates dead-man-switch patterns where funds become accessible to a recovery path if the primary signers do not move them within the timelock window.
Hash-Locked Contracts
The sha256 and hash256 fragments enable hash time-locked contracts and atomic swap patterns. Combined with timelocks and key checks, they can express conditional payments where revealing a preimage on one chain unlocks funds on another.
Hardware Signing Device Compatibility
Miniscript fragments have formally defined properties, so hardware wallets can analyse an arbitrary spending script to determine whether a specific signing path is safe. Hardware signing devices can therefore support complex policies without custom firmware for each use case.
Why It Matters
Before Miniscript, constructing anything beyond simple pay-to-key or basic multisig scripts in Bitcoin required deep expertise in Script's stack machine. Errors were common and often undetectable until funds were at risk. Fragments solve this by providing a vocabulary of proven-correct components with a type system that prevents invalid compositions.
Bitcoin Core, the Bitcoin Dev Kit (BDK), and multiple hardware wallets now support Miniscript, making fragments a practical tool for wallet developers. Platforms like Spark benefit from this infrastructure because it standardises how complex spending conditions are expressed, analysed, and signed across the Bitcoin ecosystem.
The research article on Miniscript and Bitcoin spending policies gives a deeper exploration of how policies compile into fragment trees. It also explains how the compiler optimises for spending cost.
Script Size Limits
Complex fragment trees produce large scripts. P2WSH scripts are limited to 10,000 bytes, and each Tapscript leaf has a similar budget. Deeply nested policies with many branches or large threshold sets can exceed these limits, forcing developers to restructure their spending conditions.
Timelock Mixing Restrictions
Bitcoin enforces a rule that absolute and relative timelocks using block heights cannot be mixed with timelocks using timestamps in the same transaction. The Miniscript type system tracks this and will reject fragment compositions that violate this constraint, but developers must still be aware of the limitation when designing policies.
Witness Size and Fee Impact
Every fragment adds opcodes and data to the witness. More complex policies mean larger witnesses and higher fee rates, and less-likely paths like timelock recovery may have significantly higher fees even though the compiler optimises for the most likely spending path. Developers should evaluate witness size across all spending paths, not just the primary one.
P2WSH vs. Tapscript Differences
Some fragments behave differently depending on the script context. The multi fragment uses OP_CHECKMULTISIG, which is only available in P2WSH, while under Taproot, multi_a must be used instead because it relies on OP_CHECKSIGADD. The type system and compiler handle this distinction, but developers must choose the correct context when defining output descriptors.
Specification Status
Miniscript is formalised in BIP 379. Miniscript has been widely adopted in production wallets and Bitcoin Core, but developers should track the specification for updates to fragment definitions or type rules.
The glossary entry is for informational purposes only and does not constitute financial or investment advice. You should always do your own research before using any protocol or technology.