Overview
The program module defines the structure of Aleo programs, including their components: functions, closures, instructions, structs, records, and mappings.Program
Type Definition
Initialization
Program::new
Creates a new empty program.ProgramID<N>
required
The program identifier (e.g., “token.aleo”)
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.- Public and private balances
- Staking and delegation
- Validator committees
- Transaction fees
Program Validation
Program::is_reserved_keyword
Checks if a name is a reserved keyword.input, output, function, closure, struct, record, mapping, etc.
Source: synthesizer/program/src/lib.rs:193-266
Function
Type Definition
Function::new
Creates a new function.Function Queries
Function::name
Returns the function name.Function::inputs
Returns the function inputs.Function::input_types
Returns the input value types.Function::instructions
Returns the function instructions.Function::outputs
Returns the function outputs.Function::output_types
Returns the output value types.Function::finalize_logic
Returns the optional finalize logic.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
- Cannot have finalize logic
- Cannot produce records
- Cannot access on-chain state
- Can be called from functions or other closures
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.Instruction
Instructions are the basic operations in Aleo programs.Type Definition
Instruction Categories
Arithmetic
Add,Sub,Mul,Div,Rem,Pow- Wrapped variants:
AddWrapped,SubWrapped, etc. Abs,AbsWrapped,Double,Square,SqrtInv,Neg
Bitwise
And,Or,Xor,Nand,NorNotShl,Shr,ShlWrapped,ShrWrapped
Comparison
GreaterThan,GreaterThanOrEqualLessThan,LessThanOrEqualIsEq,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 signaturesECDSAVerifyDigest,ECDSAVerifyKeccak256,ECDSAVerifyKeccak384,ECDSAVerifyKeccak512ECDSAVerifySha3_256,ECDSAVerifySha3_384,ECDSAVerifySha3_512- Ethereum variants:
ECDSAVerifyDigestEth,ECDSAVerifyKeccak256Eth, etc.
Control Flow
Call- Call a closure or functionAsync- Call finalize asynchronously
Type Operations
Cast- Cast operands to a typeCastLossy- Cast with lossy truncationTernary- Conditional selection
Assertions
AssertEq- Assert equalityAssertNeq- Assert inequality
Serialization
DeserializeBits,DeserializeBitsRawSerializeBits,SerializeBitsRaw
Instruction Example
Finalize
Type Definition
- Can read/write mappings
- Cannot access private data
- Executed by validators during block production
Finalize Commands
Finalize usesCommand 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.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