Introduction
What is the Cairo core library?
The Cairo core library provides the foundational building blocks for writing provable programs in Cairo. It offers essential utilities, data structures, mathematical functions, cryptographic tools, and system interactions making it suitable for both onchain and offchain development. Whether you are working on Starknet smart contracts, cryptographic applications, or general-purpose Cairo programs, the core library provides the fundamental tools needed.
you can access modules from the corelib in your program via the core prefix as follows
The core library is available to all Cairo packages by default, meaning its features are available by simply importing specific modules into your program as follows:
use core::array::Array;
fn main() {
let mut arr = Array::new();
arr.append(42);
}
How to use this documentation?
This documentation serves as a comprehensive reference for all components of the Cairo Core Library. It organizes functionality into modules, constants, functions, types, and traits, allowing developers to explore and understand available features efficiently. It is auto-generated from the core library's codebase using Scarb, so if you find a bug, have a feature request, or want to improve the documentation, simply report an issue or submit a pull request on either Cairo's or Scarb's GitHub repositories.
If you already know what you are looking for, the fastest way to find it is to use the search bar at the top of the page. Otherwise, you can take a quick tour of the documentation and start clicking on anything the seems interesting. If this is your first time, we recommend starting with browsing the library's modules and experimenting with their functionality in your Cairo programs.
Happy coding! π
A quick tour of the documentation
This documentation is structured into the following sections:
-
Modules β The main building blocks of the library This section includes all standard modules, each serving a distinct purpose:
-
Core Utilities:
core
β The base module with essential utilitiestraits
β Common behavior definitions used across multiple typesboolean
β Boolean logic operationsarray
β Dynamic data structures for storing and managing sequences of valuesdict
β Key-value storage structuresoption
β Represents optional valuesresult
β Used for error handling
-
Numerical and Mathematical Modules:
integer
β Fixed-size integer operations (u8
,u16
,u32
,u64
, etc.)math
β Core mathematical functionsops
β Arithmetic and logical operatorsnum
β Numeric utilities and traitscmp
β Comparisons and ordering
-
Cryptography and Hashing:
hash
β Generic hash utilities.poseidon
,pedersen
,keccak
,sha256
β Cryptographic hash functionsecdsa
β Signature verification and elliptic curve cryptography
-
Starknet-Specific Modules:
starknet
β Essential utilities for writing smart contractssyscalls
β Low-level Starknet system interactionsstorage
β On-chain storage managementevent
β Emitting events for contract execution trackingcontract_address
β Starknet contract address utilitiesaccount
β Account contract functionality
-
Other Utilities:
debug
β Debugging toolsfmt
β String formatting utilitiesserde
β Serialization and deserializationmetaprogramming
β Advanced compile-time utilitieszeroable
β Zero-initialized types
-
-
Constants β Predefined values for cryptographic and mathematical operations, , such as:
stark_curve::ALPHA, BETA, ORDER
β Constants for Stark curve operationsstarknet::VALIDATED
β The expected return value of a Starknet account's__validate__
function in case of success
-
Free Functions β Globally accessible functions that donβt belong to a specific trait, such as:
panic_with_felt252
β Triggers a panic with a given valueget_tx_info
β Returns the transaction info (hash, signature, etc.) for the current transaction
-
Structs β Data structures used throughout Cairo programs, such as:
Span<T>
β A lightweight view over a contiguous memory blockRange<T>
β Represents a range of valuesBlockInfo
,TxInfo
,ExecutionInfo
β Used for retrieving Starknet execution detailsEventEmitter
β Helps in emitting events from contracts
-
Enums β Enumerations for handling multiple possible values, such as:
Option<T>
β Represents eitherSome(value)
or NoneResult<T, E>
β Represents eitherOk(value)
orErr(error)
PanicResult
β Handles different types of panics in execution.
-
Type Aliases β Shorthand for commonly used types, such as:
usize
β Represents an unsigned integer type used for indexingSyscallResult<T>
β A standardized way of handling syscall return valuesNonZeroEcPoint
β Ensures that an elliptic curve point is non-zero
-
Traits β Shared behavior implementations for different types, such as:
PartialEq
,PartialOrd
β Used for comparisonsIterator
,IntoIterator
β Traits for iterating over collectionsSerialize
,Deserialize
β Serialization and deserialization behaviors
-
Impls β Concrete implementations of traits for various types, such as:
CircuitElementDrop
,CircuitElementCopy
β Implementations for circuit computationsSpanIndex
β Allows indexing intoSpan<T>
PedersenImpl
,PoseidonImpl
β Implementations for cryptographic hashing
-
Extern Types β Sierra types that are usable in high-level Cairo, such as:
Array<T>
β A collection of elements of the same type continuous in memory.Box<T>
β A wrapper that enables moving the type around of cheaplybytes31<T>
β Represents a 31-byte fixed-size byte type.
-
Extern Functions β Sierra libfuncs that are usable in high-level Cairo, such as:
hades_permutation
β Hades permutation for triplets offelt252
felt252_div
β Division operation forfelt252
storage_read_syscall
,storage_write_syscall
β Direct Starknet storage interactions