Leo Syntax Cheatsheet
1. File Import
In Aleo, you can import modules or other program files to utilize their functions, records, and other definitions in your program. This is similar to import statements in other programming languages like Python or JavaScript.
import foo.aleo;
This statement imports the foo.aleo
file, allowing you to use its contents in your current program.
2. Programs
An Aleo program is defined with the program
keyword followed by the program name. Inside the program block, you can define various elements such as records, functions, and transitions.
program hello.aleo { // code }
This defines a new program named hello.aleo
. The program's code goes inside the curly braces.
3. Data Types
Aleo supports several primitive data types that are commonly used in blockchain programming.
- Boolean: Represents a true or false value.
- Unsigned Integer: Represents non-negative integers with different bit sizes (e.g.,
u8
,u32
). - Field Element: Represents elements of a finite field, useful in cryptographic operations.
- Group Element: Represents elements of a cryptographic group.
- Scalar Element: Used in elliptic curve cryptography operations.
- Address: Represents a user or contract address on the Aleo blockchain.
let b: bool = false; // boolean let i: u8 = 1u8; // unsigned integer let a: field = 1field; // field element let g: group = 0group; // group element let s: scalar = 1scalar; // scalar element let receiver: address = aleo1ezamst4pjgj9zfxqq0fwfj8a4cjuqndmasgata3hggzqygggnyfq6kmyd4; // address
Type Casting Type casting allows you to convert a value from one type to another. This is useful when you need to operate on values of different types.
let a: u8 = 1u8; let b: u32 = a as u32; // cast 1u8 to 1u32
Primitive types include: address
, bool
, field
, group
, i8
, i16
, i32
, i64
, i128
, u8
, u16
, u32
, u64
, u128
, scalar
. Casting is possible between these types, ensuring flexibility in handling different data types.
4. Records
Records in Aleo are similar to structs in other languages. They are used to group related data together under one name.
record token { owner: address, amount: u64, }
This defines a token
record with two fields: owner
(an address) and amount
(an unsigned 64-bit integer).
5. Structs
Structs are another way to group related data, providing a way to model more complex data structures.
struct message { sender: address, object: u64, }
This defines a message
struct with a sender
(an address) and an object
(an unsigned 64-bit integer).
6. Arrays
Arrays in Aleo are collections of elements of the same type.
let arr: [bool; 2] = [true, false];
This creates an array arr
of two boolean values, true
and false
.
7. Transitions
Transitions are special functions that define state changes in the blockchain. They can take public or private inputs and produce a new state.
transition mint_public( public receiver: address, public amount: u64, ) -> token { // Your code here }
This mint_public
transition takes a receiver
address and an amount
, then returns a token
record. The public
keyword indicates that these inputs are visible to everyone.
8. Functions
Functions in Aleo are reusable blocks of code that perform specific tasks. They can accept parameters and return values.
function compute(a: u64, b: u64) -> u64 { return a + b; }
This compute
function takes two unsigned 64-bit integers and returns their sum.
9. Inline Functions
Inline functions are a more restricted type of function meant for simple, reusable code snippets. They can only be called within other inline functions or regular functions.
inline foo( a: field, b: field, ) -> field { return a + b; }
This foo
inline function adds two field elements and returns the result.
- Transition functions can call functions and inline functions.
- Function functions can only call inline functions.
- Inline functions can only call other inline functions.
- Recursive calls (direct or indirect) are not allowed to prevent infinite loops and ensure predictable execution.
10. For Loops
For loops in Aleo iterate over a range of values.
let count: u32 = 0u32; for i: u32 in 0u32..5u32 { count += 1u32; }
This loop increments count
from 0 to 5.
11. Mappings
Mappings in Aleo are similar to dictionaries or hash maps in other languages. They associate keys with values.
mapping balances: address => u64; let contains_bal: bool = Mapping::contains(balances, receiver); let get_bal: u64 = Mapping::get(balances, receiver); let get_or_use_bal: u64 = Mapping::get_or_use(balances, receiver, 0u64); let set_bal: () = Mapping::set(balances, receiver, 100u64); let remove_bal: () = Mapping::remove(balances, receiver);
This snippet defines a balances
mapping from addresses to unsigned 64-bit integers and demonstrates various operations on the mapping.
12. Commands
Commands in Aleo allow you to perform specific blockchain operations, such as asserting conditions or generating random numbers.
transition matches(height: u32) -> Future { return check_height_matches(height); } async function check_height_matches(height: u32) { assert_eq(height, block.height); // block.height returns the latest block height } let g: group = group::GEN; // the group generator let result: u32 = ChaCha::rand_u32(); // generate a random u32 let owner: address = self.caller; // address of the program function caller let hash: field = BHP256::hash_to_field(1u32); // hash any type to a field let commit: group = Pedersen64::commit_to_group(1u64, 1scalar); // commit using a scalar as a blinding factor let a: bool = true; assert(a); // assert the value of a is true let a: u8 = 1u8; let b: u8 = 2u8; assert_eq(a, a); // assert a and b are equal assert_neq(a, b); // assert a and b are not equal
Commands include assertions and various cryptographic operations essential for blockchain functionality.
13. Operators
Operators in Aleo allow you to perform arithmetic, bitwise, and logical operations on data.
let sum: u64 = a + b; // arithmetic addition let diff: u64 = a - b; // arithmetic subtraction let prod: u64 = a * b; // arithmetic multiplication let quot: u64 = a / b; // arithmetic division let remainder: u64 = a % b; // arithmetic remainder let neg: u64 = -a; // negation let bitwise_and: u64 = a & b; // bitwise AND let bitwise_or: u64 = a | b; // bitwise OR let bitwise_xor: u64 = a ^ b; // bitwise XOR let bitwise_not: u64 = !a; // bitwise NOT let logical_and: bool = a && b; // logical AND let logical_or: bool = a || b; // logical OR let eq: bool = a == b; // equality let neq: bool = a != b; // non-equality let lt: bool = a < b; // less than let lte: bool = a <= b; // less than or equal let gt: bool = a > b; // greater than let gte: bool = a >= b; // greater than or equal
This section demonstrates the use of various operators for performing different types of operations on data in Aleo.