MAST Merkelized Alternative Script Tree

Published by Mario Oettler on

Merkelized Alternative Script Tree (MAST) was introduced in BIP 341.

For this topic, you need the following prerequisits:

MAST stands for Merkelized Abstract Syntax Tree. In the context of Bitcoin, however, it is used as Merkelized Alternative Script Tree.

The concept involves dividing a script into separate execution branches, each of which is then hashed and combined into a Merkle tree that represents the structure of the underlying code. In Bitcoin the leaves represent user-defined conditions that must be met to be able to spend a specific unspent transaction output.

Construction of a MAST

The figure below illustrates an example of a Merkelized alternative script tree (MAST) containing three scripts (conditions) in its leaves.

The construction of a MAST is similar to that of a binary Merkle tree. Leaves and nodes are hashed in pairs.  

First, the scripts are individually hashed.  Then, each leaf hash is concatenated with the hash of its neighbor (either the left neighbor or the right neighbor) and hashed again. This resulting hash is then concatenated with the hash of the neighbor node at the same level. The following figure illustrates a MAST.

Merkelized alternative Script tree. Leaves and nodes are concatenated (||) pairwise and hashed. In some levels, additional information can be added before hashing.

The hashing rules may specify that certain tags or additional information be concatenated at certain levels to the pre-image before hashing.

Spending from a MAST

To spend an UTXO, the user must prove that he meets the conditions of at least one branch (leaf/script). This is done by providing the script of the chosen branch (e.g. script B), the necessary data for the script to execute correctly, and the Merkle path to the script root (=Merkle root). In this example, the user would need to provide the information in the green fields, if they wish to spend from script B. The Merkle Root (red) is provided in the transaction.

If the user wants to spend the UTXO by providing the script B, they must provide all data to execute script B correctly, the hash of script A and the hash of script C. The yellow nodes can be calculated with the provided data and finally compared to the root of the MAST (red).

With the following tool, you can construct your own Merkelized Alternative Script Tree (MAST) by adding data to its leaves.

MAST Tool

This tool allows you to create a MAST (Merkelized Abstract Script Tree) for Bitcoin Taproot. The MAST follows the scheme explained in BIP 114. https://github.com/bitcoin/bips/blob/master/bip-0114.mediawiki Nodes are double hashed and prefixed with the number of child nodes. You can add new nodes (hashes and leaves) to the MAST and delete them from the MAST by hovering over a node and selecting the preferred option. Note that deleting a parent node will also delete all of its child nodes. The input accepts hexadecimal values (with or without a 0x prefix). The output is an array of data. You can use this in libraries such as bitcoinjs-lib.

Below, you can create a MAST from a given Array by inserting the Array into the input and clicking the button. The Array should contain the elements in the order in which they should be hashed, and should have the format of {"output": "Value"}.


MAST Treearray:
Categories: