Built-in Functions
All built-in functions use lowercase snake_case names. Default parameter values (shown as = value) and keyword arguments (e.g., to_int(b, signed=True)) are supported on built-in functions. Every expression in the tables below is verified against the real interpreter.
| Category | Functions |
|---|---|
| Length & Allocation | len · zeros · fill |
| Structure Manipulation | concat · reverse · blocks · to_ints · from_ints |
| Bit Rotation | rol · ror |
| Comparison | ct_equals · compare |
| Integer Conversion | to_int · to_bytes |
| String & Hex Conversion | from_hex · to_hex · from_utf8 · to_utf8 · str |
| Integer Math | abs · min · max · pow · bit_length |
| Control & Failure | range · fail |
Length & Allocation
len
len(x: bytes | list[T]) → int — Returns the byte count of a bytes buffer or the element count of a list.
| Expression | Result |
|---|---|
len(h"01 02 03") | 3 |
len([10, 20]) | 2 |
len(h"") | 0 |
zeros
zeros(n: int) → bytes — Returns a byte array of n zero bytes (0x00). Fails with BYTES_INVALID_ARGUMENT if n < 0.
| Expression | Result |
|---|---|
zeros(4) | 00000000 |
zeros(-1) | Error BYTES_INVALID_ARGUMENT |
fill
fill(value: int, n: int) → bytes — Returns a byte array of length n filled with the byte value value (0..255). Fails with BYTES_INVALID_ARGUMENT if out of range.
| Expression | Result |
|---|---|
fill(0xAA, 3) | aaaaaa |
fill(256, 1) | Error BYTES_INVALID_ARGUMENT |
Structure Manipulation
concat
concat(parts: list[bytes]) → bytes — Joins a list of byte arrays in order. Fails with BYTES_SIZE_LIMIT if the result exceeds 16 MiB.
| Expression | Result |
|---|---|
concat([h"01", h"02 03"]) | 010203 |
reverse
reverse(b: bytes) → bytes — Reverses the byte order of b (endianness swap).
| Expression | Result |
|---|---|
reverse(h"01 02 03") | 030201 |
blocks
blocks(b: bytes, size: int) → list[bytes] — Splits b into chunks of size bytes from the start; the final chunk may be shorter than size. Fails with BYTES_INVALID_ARGUMENT if size ≤ 0.
| Expression | Result |
|---|---|
len(blocks(h"01 02 03 04 05", 2)) | 3 |
blocks(h"01 02 03 04 05", 2)[1] | 0304 |
blocks(h"01 02 03 04 05", 2)[2] | 05 (short final block) |
to_ints
to_ints(b: bytes) → list[int] — Converts each byte of b into an integer in 0..255.
| Expression | Result |
|---|---|
to_ints(h"01 ff")[1] | 255 |
len(to_ints(h"01 ff")) | 2 |
from_ints
from_ints(xs: list[int]) → bytes — Packs a list of integers (0..255) into a byte array. Fails with BYTES_INVALID_ARGUMENT if any element is outside 0..255.
| Expression | Result |
|---|---|
from_ints([1, 255]) | 01ff |
from_ints([256]) | Error BYTES_INVALID_ARGUMENT |
Bit Rotation
rol
rol(b: bytes, n: int) → bytes — Rotates the entire bit sequence of b left by n bits.
| Expression | Result |
|---|---|
rol(h"81", 1) | 03 (1000 0001 → 0000 0011) |
rol(h"01 81", 1) | 0302 |
ror
ror(b: bytes, n: int) → bytes — Rotates the entire bit sequence of b right by n bits.
| Expression | Result |
|---|---|
ror(h"01", 1) | 80 |
Comparison
ct_equals
ct_equals(a: bytes, b: bytes) → bool — Best-effort constant-time byte array equality check.
| Expression | Result |
|---|---|
ct_equals(h"01 02", h"01 02") | True |
ct_equals(h"01 02", h"01 03") | False |
compare
compare(a: bytes, b: bytes) → int — Lexicographical comparison returning -1, 0, or 1.
| Expression | Result |
|---|---|
compare(h"01 02", h"01 03") | -1 |
compare(h"01", h"01") | 0 |
compare(h"02", h"01 ff") | 1 |
Integer Conversion
to_int
to_int(b: bytes, endian: string = "big", signed: bool = False) → int — Interprets b as an integer (two's complement when signed=True; empty bytes yield 0).
| Expression | Result |
|---|---|
to_int(h"01 00") | 256 |
to_int(h"01 00", "little") | 1 |
to_int(h"ff", signed=True) | -1 |
to_int(h"ff") | 255 |
to_int(h"") | 0 |
to_bytes
to_bytes(n: int, length: int, endian: string = "big", signed: bool = False) → bytes — Encodes integer n into exactly length bytes. Fails with BYTES_INT_OVERFLOW if n does not fit.
| Expression | Result |
|---|---|
to_bytes(256, 2) | 0100 |
to_bytes(256, 2, "little") | 0001 |
to_bytes(-1, 1, signed=True) | ff |
to_bytes(42, 4) | 0000002a |
to_bytes(256, 1) | Error BYTES_INT_OVERFLOW |
to_bytes(-1, 1) | Error BYTES_INT_OVERFLOW |
String & Hex Conversion
from_hex
from_hex(s: string) → bytes — Decodes a hexadecimal string (spaces ignored, no 0x prefix, even digit count) to bytes. Fails with BYTES_INVALID_HEX on invalid input.
| Expression | Result |
|---|---|
from_hex("01 ff") | 01ff |
from_hex("0") | Error BYTES_INVALID_HEX |
from_hex("0xff") | Error BYTES_INVALID_HEX |
to_hex
to_hex(b: bytes, upper: bool = False) → string — Encodes bytes as a contiguous 2-digit-per-byte hex string.
| Expression | Result |
|---|---|
to_hex(h"0a ff") | 0aff |
to_hex(h"0a ff", upper=True) | 0AFF |
from_utf8
from_utf8(s: string) → bytes — Encodes a Unicode string into UTF-8 bytes.
| Expression | Result |
|---|---|
from_utf8("AB") | 4142 |
from_utf8("가") | eab080 |
to_utf8
to_utf8(b: bytes) → string — Strictly decodes UTF-8 bytes into a string; raises BYTES_INVALID_UTF8 on malformed sequences.
| Expression | Result |
|---|---|
to_utf8(h"41 42") | AB |
to_utf8(h"ff") | Error BYTES_INVALID_UTF8 |
str
str(n: int) → string — Formats an integer in base 10.
| Expression | Result |
|---|---|
str(-12) | -12 |
"len=" + str(3) | len=3 |
Integer Math
abs
abs(n: int) → int — Absolute value.
| Expression | Result |
|---|---|
abs(-5) | 5 |
min
min(a: int, b: int) → int — Minimum of two integers.
| Expression | Result |
|---|---|
min(3, 7) | 3 |
max
max(a: int, b: int) → int — Maximum of two integers.
| Expression | Result |
|---|---|
max(3, 7) | 7 |
pow
pow(base: int, exp: int, mod: int) → int — Modular exponentiation (baseexp mod m). Requires exp ≥ 0 and mod ≥ 1.
| Expression | Result |
|---|---|
pow(3, 4, 5) | 1 (81 mod 5) |
pow(2, 10, 1000) | 24 (1024 mod 1000) |
pow(2, -1, 5) | Error SCRIPT_INVALID_ARGUMENT |
bit_length
bit_length(n: int) → int — Minimum number of bits required to represent abs(n) (0 returns 0).
| Expression | Result |
|---|---|
bit_length(255) | 8 |
bit_length(256) | 9 |
bit_length(0) | 0 |
Control & Failure
range
range(n), range(start, stop), range(start, stop, step) — Integer sequence iterable for for loops (see for Loops and range).
up = 0
for i in range(5):
up += i
down = 0
for i in range(10, 0, -3):
down += i
fail
fail(code: string, message: string) — Immediately aborts script execution and puts the Script node into an ERROR state with the given code and message. User error codes cannot start with the reserved prefixes SCRIPT_ or BYTES_ (doing so raises SCRIPT_INVALID_ERROR_CODE).
if n == 0:
fail("ZERO_NOT_ALLOWED", "n must not be 0")
o = 100 // n