Cairo is a programming language specifically designed for a virtual CPU with the same name. Unlike conventional processors that adhere to the physical limitations of our world, the Cairo CPU is tailored to cryptographic constraints, enabling efficient verification of program execution. This unique feature allows you to carry out time-consuming operations on an untrusted machine and subsequently verify the results rapidly on a more affordable one. In the past, Cairo 0 directly compiled to CASM, the Cairo CPU assembly. However, with the introduction of Cairo 1, the language has become more high-level. It now first compiles to Sierra, an intermediate representation of Cairo, which is then further compiled to a safe subset of CASM. The purpose of Sierra is to ensure that your CASM code remains provable even in cases where the computation encounters failures.
Key differences with other programming languages:
Cairo stands apart from traditional programming languages, particularly in terms of overhead costs and its key benefits. Your program can be executed using two distinct methods:
-
When the prover executes the program, it behaves similarly to any other language. Due to Cairo's virtualized nature and the fact that its operations were not exclusively optimized for maximum efficiency, there might be a certain level of performance overhead. However, optimizing this aspect is not the most crucial concern.
-
When the generated proof is verified by a verifier, the process differs slightly. It needs to be as cost-effective as possible since it might be verified on numerous small machines. Fortunately, verification is faster than computation, and Cairo possesses some distinctive advantages to further enhance this efficiency. One notable advantage is non-determinism, which will be explored in more detail later in this book. Essentially, it allows the possibility of using a different algorithm for verification than the one used for computation.
-
At present, developers do not have the capability to write custom non-deterministic code. However, the standard library makes use of non-determinism to improve performance. For instance, in Cairo, sorting an array incurs the same cost as copying it. The verifier doesn't actually sort the array; instead, it only checks if the array is sorted, which is a less expensive operation.
Another distinguishing feature of the language is its memory model. In Cairo, memory access follows immutability principles, implying that once a value is written to memory, it becomes unchangeable. While Cairo 1 offers abstractions to assist developers in handling these constraints, it does not fully simulate mutability. Consequently, developers need to approach memory management and data structures thoughtfully in their programs to achieve optimal performance.
How to install Cairo?
The first step is to install Cairo. We have the option to download Cairo manually by either using the Cairo repository or an installation script. You’ll need an internet connection for the download.
-
For x86 Linux systems, you can conveniently download Cairo from the release binary here: github.com/starkware-libs/cairo/releases
-
For everyone else, we recommend compiling Cairo from the source as follows:
Step1:
# Start by defining the environment variable for CAIRO_ROOT
export CAIRO_ROOT="${HOME}/.cairo"
# Create a .cairo folder if it doesn't exist on your PC
mkdir $CAIRO_ROOT
# Clone the Cairo compiler repository in $CAIRO_ROOT (default root)
cd $CAIRO_ROOT && git clone git@github.com:starkware-libs/cairo.git .
# OPTIONAL/RECOMMENDED: If you really want to install any specific version of the compiler try it out
# Fetch all the versions
git fetch --all --tags
# View tags (you can also try this in the cairo compiler repo)
git describe --tags `git rev-list --tags`
# Checkout the version you want
git checkout tags/v2.0.0
# Generate release binaries
cargo build --all --release
Step 2: Incorporate Cairo 1.0 executables into your path.
PATH="$CAIRO_ROOT/target/release:$PATH
Hello-world in Cairo
Firstly create a project directory
For this, open a terminal and perform the following commands
mkdir ~/cairo_projects
cd ~/cairo_projects
mkdir hello_world
cd hello_world
Now open main.cairo file and write the following code
debug::PrintTrait;
fn main() { 'Hello, world!'.print(); }
To make this code compile and run save the file and go back to terminal and run the command
$ cairo-run main.cairo
Output:
Hello, world!
Explanation:
-
Firstly, in Cairo style, indentation is done with four spaces, not tabs.
-
Secondly, the print() function used here is a method sourced from the PrintTrait trait. This trait is imported from the Cairo core library and defines how different data types can be printed to the screen. In this case, the text "Hello, world!" Within Cairo, there is a concept of a 'short string,' which is an ASCII string fitting within Cairo's basic data type, known as felt252.. When we invoke 'Hello, world!'.print(), we are essentially calling the print() method within the felt252 implementation of the PrintTrait trait.
-
Thirdly, we observe the output of the 'Hello, world!' short string. By passing this short string as an argument to print(), it gets displayed on the screen.
-
Fourthly, the line is terminated with a semicolon (;), signifying the completion of this expression, and the next one is ready to commence. In Cairo code, most lines end with a semicolon.
Additional Reference
Here are some other referential videos that would help you to go forward with the cairo:
Frequently Asked Questions
Q1.Can I use Cairo with an integrated development environment (IDE)?
Yes, Cairo can be used with various IDEs, and there is a VS Code extension available for proper syntax highlighting and code navigation. Users can choose their preferred IDE for Cairo development.
Q2. What is the purpose of Sierra in Cairo 1?
Sierra is an intermediate representation of Cairo that compiles down to a safe subset of CASM (Cairo CPU assembly). It ensures that CASM remains provable, even when computations fail.
Q3. How does Cairo handle memory access?
Cairo's memory access is immutable, meaning once a value is written to memory, it cannot be changed. Although Cairo 1 offers abstractions to handle these limitations, it doesn't entirely emulate mutability. Developers need to carefully manage memory and data structures in their programs to optimize performance.
Q4. What can I do with Cairo?
One of Cairo's notable features is its ability to calculate reliable values on untrusted machines. One major use case is Starknet, a solution for Ethereum scaling. It enables running programs on a single node (prover) and generating proofs, which can be verified on Ethereum's smart contract with significantly less computational power.
Q5. What is Cairo, and what makes it unique?
Cairo is a programming language designed for a virtual CPU with the same name. It stands out because it was specifically created for cryptographic constraints, making it efficient in proving the execution of any program running on it. This enables performing time-consuming operations on an untrusted machine and verifying the results quickly on a cheaper machine.