Skip to main content

Atoms

An atom is one of two fundamental data types in CLVM. Put simply, atom values represent a sequence of bytes.

There are multiple types of atoms:

  1. Atom - has no semantic meaning
  2. Bytes - explicitly treated as bytes
  3. Bytes32 - a Bytes value with a length of 32
  4. String - a UTF-8 encoded string
  5. PublicKey - a BLS12-381 public key with a length of 48 bytes
  6. Signature - a BLS12-381 signature with a length of 96 bytes
  7. K1PublicKey - a secp256k1 public key with a length of 33 bytes
  8. K1Signature - a secp256k1 signature with a length of 64 bytes
  9. R1PublicKey - a secp256r1 public key with a length of 33 bytes
  10. R1Signature - a secp256r1 signature with a length of 64 bytes
  11. Int - a signed integer of arbitrary size
  12. Bool - either true or false

Literals​

LiteralExampleType
Integer42, 1_000_000Int
Hex0xCAFE_F00DBytes (or Bytes32, PublicKey, or Signature, depending on the length)
Binary0b101010Int
Octal0o52Int
String"Hello, world!", 'Hello, world!'String
Booleantrue, falseBool
Nilnilnil

Underscores can be used anywhere in a number to make it easier to read. Hex literals with an odd number of digits are padded with a leading zero, so 0x1 is the same as 0x01.

A hex literal is bytes rather than an integer, so it can't be assigned to an Int without a cast. The compiler suggests writing the number instead, since 0x05 as Int is just 5.

Strictly speaking, the type of a literal is more specific than shown here. See Literal Types below.

Strings​

A string can be written with either double quotes ("...") or single quotes ('...'), and both mean the same thing. rue fmt uses double quotes unless single quotes would need fewer escapes.

Strings support the same escape sequences as Rust:

EscapeMeaning
\nLine feed
\rCarriage return
\tTab
\0The zero byte
\\Backslash
\", \'Double or single quote
\x41An ASCII character, from \x00 to \x7f
\u{1F600}A Unicode code point, with 1 to 6 hex digits
\ at end of lineSkips the line break and any whitespace at the start of the next line

Any other character after a backslash is an error. Since strings are UTF-8, a \u{...} escape can produce more than one byte, and \x is limited to ASCII. To write arbitrary bytes, use a hex literal instead.

fn quote(name: String) -> String {
'say "' + name + '"\n'
}

test fn escapes() {
assert quote("hi") == 'say "hi"\x0a';
assert "\u{1F600}".length == 4;
assert "a\tb\\c".length == 5;
}

Atom​

The plain Atom type should be used when you don't care about the semantics of the value. Any other atom type can be assigned to Atom without requiring a cast, whereas a cast is required to go from Atom to anything else.

tip

In most cases, you should use a concrete atom type like Bytes instead of Atom, since it more clearly expresses the intent and prevents making mistakes.

Bytes​

The Bytes type provides similar semantics to byte arrays or strings in other languages. If you add multiple Bytes values together, they will be concatenated. Other mathematical operations will yield a type error unless you cast to Int first. You should use Bytes when you don't know (or haven't checked) the length of the value.

There are a few ways to construct a value that is compatible with Bytes:

  1. Hex literals - 0xCAFEF00D
  2. UTF-8 encoded string literals - "Hello, world!"
  3. nil

You can also cast other atom types to Bytes when needed.

Bytes32​

The Bytes32 type represents any Bytes value with a length of 32.

If a hex literal or string literal produces a value that is exactly 32 bytes, it will be compatible with this type automatically. However, if you have an atom with an unknown length, you will need to check the type first:

fn main(value: Bytes) -> Bytes32 {
assert value is Bytes32;
value
}

This will emit code at runtime to check that the value has a length of 32.

danger

Bytes32 can only guarantee the length of trusted values. See the Security page for more details.

String​

The String type represents a UTF-8 encoded string. It's functionally equivalent to Bytes (and they can be assigned to each other without a cast), but provides a more descriptive name for working with text values, and hints to the compiler that the value is a string rather than a sequence of bytes (which is useful for error messages).

PublicKey​

The PublicKey type represents a BLS12-381 public key (also commonly referred to as a G1 element).

An atom must be 48 bytes long to be compatible with PublicKey, although there is typically no harm done by not verifying the length at runtime before using it in conditions or BLS opcodes, as long as it's not directly concatenated with other bytes.

Public keys can be added, subtracted, negated, and multiplied by an Int with the arithmetic operators. The INFINITY_G1 builtin is the identity element.

Signature​

The Signature type represents a BLS12-381 signature (also commonly referred to as a G2 element).

An atom must be 96 bytes long to be compatible with Signature, although there is typically no harm done by not verifying the length at runtime before using it in conditions or BLS opcodes, as long as it's not directly concatenated with other bytes.

Signatures support the same operators as public keys. The INFINITY_G2 builtin is the identity element.

K1PublicKey​

The K1PublicKey type represents a secp256k1 public key.

An atom must be 33 bytes long to be compatible with K1PublicKey, although there is typically no harm done by not verifying the length at runtime before using it in conditions or SECP opcodes, as long as it's not directly concatenated with other bytes.

K1Signature​

The K1Signature type represents a secp256k1 signature.

An atom must be 64 bytes long to be compatible with K1Signature, although there is typically no harm done by not verifying the length at runtime before using it in conditions or SECP opcodes, as long as it's not directly concatenated with other bytes.

R1PublicKey​

The R1PublicKey type represents a secp256r1 public key.

An atom must be 33 bytes long to be compatible with R1PublicKey, although there is typically no harm done by not verifying the length at runtime before using it in conditions or SECP opcodes, as long as it's not directly concatenated with other bytes.

R1Signature​

The R1Signature type represents a secp256r1 signature.

An atom must be 64 bytes long to be compatible with R1Signature, although there is typically no harm done by not verifying the length at runtime before using it in conditions or SECP opcodes, as long as it's not directly concatenated with other bytes.

Int​

The Int type represents any integer value, positive or negative. You can use arithmetic, bitwise, and comparison operators with integers.

You can construct a value that is compatible with Int by using integer literals such as 42, as well as binary and octal literals.

You can also cast other atom types to Int when needed. For example, 0x05 as Int is 5.

warning

CLVM integers are stored as the shortest signed big-endian representation of their value. So a byte value like 0x00 or 0x80 is not the same as the integer 0 or 128 (which are nil and 0x0080 respectively). The bytes_int_confusion lint warns when you cast a literal like this to Int.

Bool​

Bool is simply a union of the literal values true and false. You can use logical operators with boolean values.

At runtime, true is represented as 1, and false is represented as nil.

Length​

Every atom type has a length field (except for unions of atoms such as Bool, which need to be cast first), which returns the number of bytes in the value as an Int:

fn main(value: Bytes) -> Int {
value.length
}

This compiles to the CLVM strlen operator.

Indexing and Slicing​

You can read a single byte of an atom by its index, or a range of bytes with a slice. Indexes start at 0, and a slice includes its start but not its end:

ExpressionResult
value[i]The byte at index i
value[start..end]The bytes from start up to end
value[start..]The bytes from start to the end
value[..end]The bytes from the start up to end

The result is always bytes, including for a single byte, which is a Bytes value of length 1 rather than an Int. Indexing or slicing a String gives a String, and any other atom type gives Bytes. The indexes must be Int values.

test fn slicing() {
let greeting = "hello, world";
let word: String = greeting[..5];
assert word == "hello";
assert greeting[7..] == "world";
assert greeting[4] == "o";

let bytes = 0xcafef00d;
assert bytes[1..3] == 0xfef0;
}

Both compile to the CLVM substr operator, which raises an error at runtime if an index is negative or past the end of the value, or if the start of a slice is after its end. A slice must have at least a start or an end, since value[..] would be the whole value.

Lists can't be indexed, since finding an item would walk the list and hide the cost of doing so. Destructure the list or use .first and .rest instead.

Never​

Never is the type with no values. It's not an atom, but it's useful alongside them: an expression of type Never never finishes, such as a raise or a call to a function that always raises. Since no value ever reaches it, Never can be assigned to every other type. See Functions That Never Return.

Nothing can be assigned or cast to Never, and a value can't be checked against it with a type guard. A list of Never (List<Never>) can only be empty, so it can be assigned to any other list type.

Representation​

Because the various atom types differ solely by semantic meaning, there can be overlap in their CLVM representation. This is important to know when you need to differentiate between different values at runtime.

For example, the values nil, false, 0, "", and [] all have the same runtime representation (even though each of them are semantically different and would require a cast to one another).

This is also why a type guard can't distinguish between Int and nil, since 0 and nil are the same value.

Literal Types​

In most languages, the type of literal values is general enough to cover all other values. For example, 42 would have the type Int. This is for a few reasons:

  1. It allows for reassignment to variables whose type is inferred
  2. It allows multiple branches of conditional expressions to have the same type
  3. It prevents the type of object fields from being too strict

However, in Rue it's not possible to reassign to a variable, and we can use union types to alleviate some of the frustration that would be caused by a type being too specific.

Thus, every literal value has a corresponding type - for example, 42 has the type 42. This can be surprising in some situations, but it's also important to make the language work. A literal type is assignable to its general type (such as Int), but not the other way around.

You can also write literal types yourself, which is useful for values that must be exactly one of a few options:

type Mode = 0 | 1 | 2;

fn main(mode: Mode) -> Bool {
mode == 2
}

It allows for more efficient type checking, disambiguation of values at runtime, and preventing certain logic errors at compile time. For example, a struct field with a literal type can tell structs apart in a type guard, and the discriminants of enum variants work the same way, which is how the type of a condition can be narrowed.