Skip to main content

Standard Library

Rue comes with a standard library, which is written in Rue itself. It provides types for the conditions a puzzle can output, functions for tree hashing and currying, and other common utilities.

The standard library is included by default, and can be disabled by setting std = false in the Rue.toml manifest. Like any other code, only the parts you use are included in the compiled program.

Prelude​

The items most commonly used in puzzles are in the prelude, which is available in every file without importing anything. This includes all of the conditions, condition opcodes, message flags, tree hashing functions, merkle proofs, list functions, and utilities.

Modules​

Everything in the standard library can also be referenced with a path through the std module, including a few modules that aren't part of the prelude:

ModuleContents
std::conditionsThe types, opcodes and message_flags modules for conditions.
std::tree_hashtree_hash, tree_hash_atom, and tree_hash_pair.
std::curry_tree_hashcurry_tree_hash.
std::listlength, reverse, and contains.
std::merkleMerkleProof, calculate_merkle_root, and simplify_merkle_proof.
std::utilsdeep_equal.
std::offerNotarizedPayment and Payment, for settlement payments in offers.
std::singletonSingletonStruct and the singleton lineage proofs.

For example:

import std::singleton::LineageProof;

fn main(proof: LineageProof) -> Int {
proof.parent_amount
}

Conditions​

A puzzle outputs a list of conditions, which tell the blockchain what the spend should do (such as creating coins) and what must be true for the spend to be valid (such as signatures and announcements). The standard library defines them as variants of the Condition enum, whose discriminants are the condition opcodes.

The variants are also exported by their own names, so CreateCoin { ... } is the same as Condition::CreateCoin { ... }. For example, this puzzle requires a signature on the conditions, and then outputs them:

fn main(public_key: PublicKey, conditions: List<Condition>) -> List<Condition> {
let agg_sig = AggSigMe { public_key, message: tree_hash(conditions) };
[agg_sig, ...conditions]
}

Since each condition is a variant, you don't need to specify its opcode when creating one, and you can tell the conditions apart with a type guard or a match. A match over a Condition has to handle every variant, or have a _ arm for the rest:

fn spent_amount(conditions: List<Condition>) -> Int {
for condition in conditions fold total = 0 {
match condition {
CreateCoin { amount } => total + amount,
ReserveFee { amount } => total + amount,
_ => total,
}
}
}

test fn spent() {
let conditions = [
CreateCoin { puzzle_hash: sha256("puzzle"), amount: 100 },
ReserveFee { amount: 5 },
AssertMyAmount { amount: 105 },
];
assert spent_amount(conditions) == 105;
}

The opcode isn't a field of the condition. If you need it, such as to handle raw conditions, cast the condition to a pair and read the first part:

fn opcode(condition: Condition) -> Int {
let (opcode, _) = condition as (Int, Any);
opcode
}

test fn opcodes() {
assert opcode(ReserveFee { amount: 1 }) == RESERVE_FEE;
}

These are the variants, with their opcodes and fields:

VariantOpcodeFields
Remark1...rest: Any = nil
AggSigParent43public_key: PublicKey, message: Bytes
AggSigPuzzle44public_key: PublicKey, message: Bytes
AggSigAmount45public_key: PublicKey, message: Bytes
AggSigPuzzleAmount46public_key: PublicKey, message: Bytes
AggSigParentAmount47public_key: PublicKey, message: Bytes
AggSigParentPuzzle48public_key: PublicKey, message: Bytes
AggSigUnsafe49public_key: PublicKey, message: Bytes
AggSigMe50public_key: PublicKey, message: Bytes
CreateCoin51puzzle_hash: Bytes32, amount: Int, ...memos: Memos | nil = nil
ReserveFee52amount: Int
CreateCoinAnnouncement60message: Bytes
AssertCoinAnnouncement61id: Bytes32
CreatePuzzleAnnouncement62message: Bytes
AssertPuzzleAnnouncement63id: Bytes32
AssertConcurrentSpend64coin_id: Bytes32
AssertConcurrentPuzzle65puzzle_hash: Bytes32
SendMessage66mode: Int, message: Bytes, ...receiver: List<Bytes | Int>
ReceiveMessage67mode: Int, message: Bytes, ...sender: List<Bytes | Int>
AssertMyCoinId70coin_id: Bytes32
AssertMyParentId71parent_id: Bytes32
AssertMyPuzzleHash72puzzle_hash: Bytes32
AssertMyAmount73amount: Int
AssertMyBirthSeconds74seconds: Int
AssertMyBirthHeight75height: Int
AssertEphemeral76None
AssertSecondsRelative80seconds: Int
AssertSecondsAbsolute81seconds: Int
AssertHeightRelative82height: Int
AssertHeightAbsolute83height: Int
AssertBeforeSecondsRelative84seconds: Int
AssertBeforeSecondsAbsolute85seconds: Int
AssertBeforeHeightRelative86height: Int
AssertBeforeHeightAbsolute87height: Int
Softfork90cost: Int, ...args: List<Any>

A condition without fields, such as AssertEphemeral, is declared with empty braces, so it's written as AssertEphemeral {}, like the others.

The memos of a CreateCoin condition are wrapped in a Memos struct, which has a single value: Any field:

fn main(puzzle_hash: Bytes32) -> CreateCoin {
CreateCoin { puzzle_hash, amount: 1, memos: Memos { value: [puzzle_hash] } }
}

See the Chia documentation for what each condition does.

Opcodes​

The opcode of each condition is also available as a constant, named in SCREAMING_SNAKE_CASE (such as CREATE_COIN, which is 51). This is useful when working with raw conditions.

Message Flags​

The mode of a SendMessage or ReceiveMessage condition specifies which parts of the sender and receiver coins are committed to. The following constants can be combined with the | operator:

ConstantValue
SENDER_COIN0b111000
SENDER_PARENT0b100000
SENDER_PUZZLE0b010000
SENDER_AMOUNT0b001000
RECEIVER_COIN0b000111
RECEIVER_PARENT0b000100
RECEIVER_PUZZLE0b000010
RECEIVER_AMOUNT0b000001

For example:

fn main(puzzle_hash: Bytes32) -> SendMessage {
SendMessage { mode: SENDER_COIN | RECEIVER_PUZZLE, message: "hello", receiver: [puzzle_hash] }
}

Tree Hashing​

The tree hash of a value is the standard way to hash CLVM values, and the tree hash of a program is its puzzle hash.

FunctionDescription
tree_hash(value: Any) -> Bytes32Computes the tree hash of any value, recursively.
tree_hash_atom(value: Bytes) -> Bytes32Computes the tree hash of an atom.
tree_hash_pair(first: Bytes32, rest: Bytes32) -> Bytes32Computes the tree hash of a pair, given the tree hashes of its first and rest.

If you already know that a value is an atom, or have the tree hashes of the values in a pair, tree_hash_atom and tree_hash_pair are much cheaper than tree_hash.

Currying​

curry_tree_hash(mod_hash: Bytes32, parameters: List<Bytes32>) -> Bytes32 computes the puzzle hash of a program with the given parameters curried in, given the tree hash of the program and the tree hash of each parameter.

This is how a puzzle can calculate the puzzle hash of another puzzle (or a new version of itself), without needing to reveal the whole program:

fn main(mod_hash: Bytes32, owner: Bytes32) -> Bytes32 {
curry_tree_hash(mod_hash, [tree_hash_atom(mod_hash), tree_hash_atom(owner)])
}

Merkle Proofs​

ItemDescription
MerkleProofA struct with a path: Int, followed by the list of sibling hashes up the tree.
calculate_merkle_root(path: Int, current_hash: Bytes32, additional_steps: List<Bytes32>) -> Bytes32Computes the root of a merkle tree, given the hash of a node and its siblings.
simplify_merkle_proof(leaf: Bytes32, proof: MerkleProof) -> Bytes32Computes the root of a merkle tree, given a leaf value and a proof.

Lists​

FunctionDescription
length<T>(list: List<T>) -> IntReturns the number of items in the list.
reverse<T>(list: List<T>) -> List<T>Returns the items of the list in the opposite order.
contains(list: List<Bytes>, item: Bytes) -> BoolReturns whether any item of the list is equal to item, by ==.

Each of these walks the whole list, so its cost grows with the length of the list. contains compares atoms, so it works with lists of Bytes, Bytes32 and String, but a list of Int needs a cast, as in contains(values as List<Bytes>, 2 as Bytes). Lists are joined with spreads, as in [...a, ...b], and most other list operations are a for expression.

test fn lists() {
let hashes = [sha256("a"), sha256("b")];
assert length(hashes) == 2;
assert contains(hashes, sha256("b"));
assert deep_equal(reverse([1, 2, 3]), [3, 2, 1]);
}

If your program defines its own function with the same name as one of these, such as contains, its own function is used instead.

Utilities​

FunctionDescription
deep_equal(a: Any, b: Any) -> BoolReturns whether two values are structurally equal, including pairs.

Offers​

The std::offer module contains types for the settlement payments puzzle, which is used by offers:

StructFields
NotarizedPaymentnonce: Bytes32, ...payments: List<Payment>
Paymentpuzzle_hash: Bytes32, amount: Int, ...memos: Memos | nil

Singletons​

The std::singleton module contains types used by the singleton puzzle:

ItemDescription
SingletonStructThe singleton's mod_hash, launcher_id, and ...launcher_puzzle_hash.
LineageProofProves the parent was the same singleton, with parent_parent_coin_info, parent_inner_puzzle_hash, and parent_amount.
EveProofProves the parent was the launcher, with parent_parent_coin_info and parent_amount.
ProofA union of LineageProof and EveProof.

See the singleton example for how these are used.