Skip to main content

Overview

Leo provides a minimal standard library focused on zero-knowledge circuit operations. Most functionality is expressed through built-in operations and Aleo VM primitives.

Built-in Functions

Mapping Operations

Mapping operations are available in finalizer functions for interacting with on-chain storage.

Mapping::get

Retrieve a value from a mapping by key.
If the key doesn’t exist, Mapping::get will cause the transaction to fail. Use Mapping::get_or_use for safe access.

Mapping::get_or_use

Retrieve a value from a mapping, or return a default if the key doesn’t exist.
Parameters:
  • mapping: The mapping to query
  • key: The key to look up
  • default: The value to return if the key doesn’t exist

Mapping::set

Store a value in a mapping.
Properties:
  • Creates the key if it doesn’t exist
  • Overwrites the existing value if the key exists
  • Changes are committed on-chain

Mapping::remove

Remove a key-value pair from a mapping.

Random Number Generation

The ChaCha namespace provides cryptographically secure randomness.

ChaCha::rand_bool

Generate a random boolean value.
Random values are generated deterministically based on the transaction’s randomness seed, ensuring reproducibility.

ChaCha::rand_u8 / rand_u16 / rand_u32 / rand_u64 / rand_u128

Generate random unsigned integers.

ChaCha::rand_i8 / rand_i16 / rand_i32 / rand_i64 / rand_i128

Generate random signed integers.

ChaCha::rand_field

Generate a random field element.

ChaCha::rand_scalar

Generate a random scalar value.

Assertions

assert

Assert that a condition is true. If false, the program execution fails.
Use Cases:
  • Input validation
  • Invariant checking
  • Access control
  • Boundary conditions

assert_eq

Assert that two values are equal.

assert_ne

Assert that two values are not equal.

Built-in Constants

Group Constants

group::GEN

The generator point of the elliptic curve group.

Operator Overloading

Leo supports standard operators for built-in types:

Arithmetic Operators

Arithmetic operations can overflow. Leo uses safe arithmetic by default, causing the proof to fail on overflow.

Comparison Operators

Logical Operators

Bitwise Operators

Context Variables

self.caller

The address of the account that invoked the current function.
Use Cases:
  • Authorization checks
  • Tracking function invokers
  • Access control

self.signer

The address of the account that signed the transaction.

block.height (Finalizers Only)

The current block height, available only in finalizer functions.
Use Cases:
  • Time-based logic
  • Expiration checks
  • Epoch-based features

Type Conversion

as Operator

Explicitly cast between compatible types.
Supported Conversions:
  • Integer to integer (any size)
  • Integer to field
  • Integer to scalar
  • Field to integer (with potential loss)

No Standard Library Imports

Unlike many languages, Leo does not require importing standard library functions. All built-in functions are available in the global namespace.

Limitations

The following features are NOT available in Leo’s standard library:
  • File I/O operations
  • Network operations
  • Dynamic memory allocation
  • Floating-point arithmetic
  • String manipulation (beyond literals)
  • Date/time functions (use block.height instead)
  • External library imports

Future Additions

The Leo standard library is intentionally minimal. Future versions may include:
  • Hash functions (Poseidon, SHA-256, etc.)
  • Signature verification utilities
  • Merkle tree operations
  • Additional cryptographic primitives

Built-in Types

Complete type reference

Examples

See standard library in use