Cairo lang learning notes
Setting up python environment:
python -m venv ~/cairo_venv
source ~/cairo_venv/bin/activateCompiling:
cairo-compile app.cairo --output app_compiled.jsonRunning:
cairo-run --program=app_compiled.json --print_output --print_info --relocate_prints --print_memoryFormat:
cairo-format -i app.cairoThere's a make helper for running examples in repo, e.g. running fib.cairo:
make run name=fib- 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
assertstatement is invoked, otherwise it sets the value - the deference operator is
[arr](in brackets), it returns the value at memory addressarr - the
outputbuiltin passes beginning of memory segment tomain - convention is to return the next output cell to last one of output cell, e.g.
output_ptr + 2ifoutput_ptr + 1set - 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
SIZEhas special meaning in some cases - there is no
<operator (use builtins) - use
assert_nn_le(x, y)to check 0 <= x <= y - use
alloc_localsas first statement is function to allow local variables throughout function - use
localto declare local variables and setalloc_localsat top of function - use
tempvarto allocate one memory cell for immediate use - use
letto define a reference; the expression is an alias and is not executedlet x = y * ywill not be invoked unliketempvar x = y * ywhich stores the result) apallocation pointer - points to an unused memory cellfp'frame pointer - points to frame of the current function.pcprogram 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
pcprogram counter starts at the beginning of program segment - cairo programs have an execution segment where registers
ap'andfpstart - memory cells; e.g.
arrpoints to first memory cell of array,arr + 1points to second memory cell, and so on