Awesome
Cairo Learning
Cairo lang learning notes
Environment
Setting up python environment:
python -m venv ~/cairo_venv
source ~/cairo_venv/bin/activate
Compiling:
cairo-compile app.cairo --output app_compiled.json
Running:
cairo-run --program=app_compiled.json --print_output --print_info --relocate_prints --print_memory
Format:
cairo-format -i app.cairo
Examples
There's a make helper for running examples in repo, e.g. running fib.cairo
:
make run name=fib
Notes
- parentheses in return statements are required
- return statement is required to return values
- must always have return statement even if no return values
- function return statements need to be in parentheses and indicate how many values are returned
- cannot call functions are part of expressions (i.e.
foo(bar()
) will not work) - cannot call functions in a loop
- cairo memory is immutable
- if values were previously set then
assert
statement is invoked, otherwise it sets the value - the deference operator is
[arr]
(in brackets), it returns the value at memory addressarr
- the
output
builtin passes beginning of memory segment tomain
- convention is to return the next output cell to last one of output cell, e.g.
output_ptr + 2
ifoutput_ptr + 1
set - by default variables are type field element
felt
- a field element is integer in range P/2 < x < P/2 where P is a very large (prime) number (252-bit number) with 76 decimal digits.
- const
SIZE
has special meaning in some cases - there is no
<
operator (use builtins) - use
assert_nn_le(x, y)
to check 0 <= x <= y - use
alloc_locals
as first statement is function to allow local variables throughout function - use
local
to declare local variables and setalloc_locals
at top of function - use
tempvar
to allocate one memory cell for immediate use - use
let
to define a reference; the expression is an alias and is not executedlet x = y * y
will not be invoked unliketempvar x = y * y
which stores the result) ap
allocation pointer - points to an unused memory cellfp'
frame pointer - points to frame of the current function.pc
program counter - points to the current instruction%[ <pythone-code> %]
evaluates python code, e.g.const value = %[ 2 * 3 %] + 100
...
will cancel the arguments check and use args from stack instead, i.e.bar(); foo(..., y=5)
where bar returns 4- a hint is a block of python code that will be executed by provder before the next instruction
- hints are visible to the prover
- access secret inputs in hint with
program_input['secret']
- a segment is a continuous chunk of memory
- cairo programs are kept in memory called the program segment
- the
pc
program counter starts at the beginning of program segment - cairo programs have an execution segment where registers
ap'
andfp
start - memory cells; e.g.
arr
points to first memory cell of array,arr + 1
points to second memory cell, and so on