Skip to main content

Overview

The program module defines the structure of Aleo programs, including their components: functions, closures, instructions, structs, records, and mappings.

Program

Type Definition

Source: synthesizer/program/src/lib.rs:140-159

Initialization

Program::new

Creates a new empty program.
ProgramID<N>
required
The program identifier (e.g., “token.aleo”)
Source: synthesizer/program/src/lib.rs:278-293

Program::from_str

Parses a program from Aleo source code.

Example

Program Queries

Program::id

Returns the program ID.

Program::imports

Returns the program imports.

Program::mappings

Returns the program mappings.

Program::structs

Returns the program structs.

Program::records

Returns the program record types.

Program::closures

Returns the program closures.

Program::functions

Returns the program functions.

Special Programs

Program::credits

Returns the built-in credits.aleo program.
The credits program is a special first-class program that manages:
  • Public and private balances
  • Staking and delegation
  • Validator committees
  • Transaction fees
Source: synthesizer/program/src/lib.rs:297-299

Program Validation

Program::is_reserved_keyword

Checks if a name is a reserved keyword.
Reserved keywords include: input, output, function, closure, struct, record, mapping, etc. Source: synthesizer/program/src/lib.rs:193-266

Function

Type Definition

Source: synthesizer/program/src/function/mod.rs:34-46

Function::new

Creates a new function.
Source: synthesizer/program/src/function/mod.rs:49-51

Function Queries

Function::name

Returns the function name.

Function::inputs

Returns the function inputs.

Function::input_types

Returns the input value types.
Source: synthesizer/program/src/function/mod.rs:65-67

Function::instructions

Returns the function instructions.

Function::outputs

Returns the function outputs.

Function::output_types

Returns the output value types.
Source: synthesizer/program/src/function/mod.rs:85-87

Function::finalize_logic

Returns the optional finalize logic.
Source: synthesizer/program/src/function/mod.rs:95-97

Function Limits

Functions have network-defined limits:
  • Maximum inputs: N::MAX_INPUTS
  • Maximum outputs: N::MAX_OUTPUTS
  • Maximum instructions: N::MAX_INSTRUCTIONS

Closure

Type Definition

Closures are similar to functions but:
  • Cannot have finalize logic
  • Cannot produce records
  • Cannot access on-chain state
  • Can be called from functions or other closures
Source: synthesizer/program/src/closure/mod.rs:34-45

Closure::new

Creates a new closure.

Closure Queries

Closure::name

Returns the closure name.

Closure::inputs

Returns the closure inputs.

Closure::instructions

Returns the closure instructions.

Closure::outputs

Returns the closure outputs.

Closure::output_types

Returns the output register types.
Source: synthesizer/program/src/closure/mod.rs:74-76

Instruction

Instructions are the basic operations in Aleo programs.

Type Definition

Source: synthesizer/program/src/logic/instruction/mod.rs:59-200+

Instruction Categories

Arithmetic

  • Add, Sub, Mul, Div, Rem, Pow
  • Wrapped variants: AddWrapped, SubWrapped, etc.
  • Abs, AbsWrapped, Double, Square, Sqrt
  • Inv, Neg

Bitwise

  • And, Or, Xor, Nand, Nor
  • Not
  • Shl, Shr, ShlWrapped, ShrWrapped

Comparison

  • GreaterThan, GreaterThanOrEqual
  • LessThan, LessThanOrEqual
  • IsEq, IsNeq

Cryptographic

  • Hash: HashBHP256, HashBHP512, HashBHP768, HashBHP1024
  • Hash (Keccak): HashKeccak256, HashKeccak384, HashKeccak512
  • Hash (Pedersen): HashPED64, HashPED128
  • Hash (Poseidon): HashPSD2, HashPSD4, HashPSD8
  • Hash (SHA): HashSha3_256, HashSha3_384, HashSha3_512
  • Commit: CommitBHP256, CommitBHP512, CommitBHP768, CommitBHP1024
  • Commit (Pedersen): CommitPED64, CommitPED128

Signature Verification

  • SignVerify - Schnorr signatures
  • ECDSAVerifyDigest, ECDSAVerifyKeccak256, ECDSAVerifyKeccak384, ECDSAVerifyKeccak512
  • ECDSAVerifySha3_256, ECDSAVerifySha3_384, ECDSAVerifySha3_512
  • Ethereum variants: ECDSAVerifyDigestEth, ECDSAVerifyKeccak256Eth, etc.

Control Flow

  • Call - Call a closure or function
  • Async - Call finalize asynchronously

Type Operations

  • Cast - Cast operands to a type
  • CastLossy - Cast with lossy truncation
  • Ternary - Conditional selection

Assertions

  • AssertEq - Assert equality
  • AssertNeq - Assert inequality

Serialization

  • DeserializeBits, DeserializeBitsRaw
  • SerializeBits, SerializeBitsRaw

Instruction Example

Finalize

Type Definition

Finalize blocks execute on-chain after a function completes:
  • Can read/write mappings
  • Cannot access private data
  • Executed by validators during block production
Source: synthesizer/program/src/finalize/mod.rs

Finalize Commands

Finalize uses Command instead of Instruction:

Finalize Example

Mapping

Mappings store on-chain state.

Type Definition

Mapping Example

Input/Output

Input

Defines a function or closure input.

Output

Defines a function or closure output.

Value Types

ValueType

Represents the type of a function input/output.

PlaintextType

Represents the type of plaintext data.

RegisterType

Represents the type of a closure register.

Program Checksums

Programs have checksums for integrity verification.

Program::to_checksum

Computes the program checksum.
The checksum is computed using Keccak-256 over the program bytes. Source: synthesizer/program/src/to_checksum.rs

Program Restrictions

Programs are validated against several restrictions:
  • Reserved keywords cannot be used as names
  • Maximum instruction count per function
  • Maximum input/output count
  • Type consistency across calls
  • Dependency resolution (imports)
  • No circular imports

Example: Complete Program