Cairo 1.0

Introduction

  • high-level Turing-complete langage for creating STARK-provable code

  • can be compiled down into “classic” Cairo 0

  • Cairo 1.0 was written top down; Cairo 0 was written bottom up

  • Sierra - safe intermediate representation

  • CASM - Cairo Assembly (essentially Cairo 0 without any syntactic sugar)

  • In Cairo 0, if an execution failed, the Cairo VM would stop running and the code wouldnt be provable

  • In Cairo 1, any Cairo 1 code that compiles is provable, even if it fails

  • Sierra was created to make sure that all programs created would end at some “legal” endpoint, so that every transaction is provable

  • why was rust chosen to be emulated by Cairo 1?

    • the immutable memory model of Cairo 1 is similar to the borrow checker of rust

    • in rust, every variable can only be owned by one entity at a time

    • prevents from writing segmentation faults

    • no garbage collector

  • immutable memory model

    • essentially what we are writing into the Cairo CPU are constraints over memory - means memory itself cannot change. any command that we run is just an additional constraint over the final memory

  • currently unable to write in-line assembly in cairo, no intention to integrate at the moment

  • if there are specific optimisations that one would like to make, it could be packaged as a different function, and be written into sierra directly (needs approval by starkware/community)

  • logically similar to adding a precompile to ethereum

  • felt252 is the only element type that exists in the language

    • felt = field element

    • 252 = contains 252 bits

  • felt252 is the basic building block of CASM

  • dont use felt252 when writing contracts; other higher level native types are more expensive but more recommended

  • the last statement in a function block is the return value of the block (assuming no semicolon suffix)

  • Snapshot type (similar to rust’s reference) — a type that creates a reference to an object at a given point in time, which can not be changed.

  • read operations can be done on them, but not write

  • Snapshot operator @ and de-snapping operator *

  • similar to const in other languages

  • array type in Cairo 1 is basically a queue, stored in a segment of memory

  • stack: currently no recursive types in Cairo 1, but when they are supported, will be some sort of linked list style object. elements will be distributed across memory, and wont be contiguous

Types

  • built-in types

    • u8, u16, u32, u64, u128, felt252

  • user-defined types

    • structs, enums

    • u256, bool

    • tuples: (), (u64), (u128, u32)

  • starknet

    • ContractAddress, ClassHash, Storage Address

  • short strings - another way to write numbers. good for assertions, revert reasons etc.

#[derive(...)]

  • some traits can be automatically implemented for a type:

    • Copy - an object’s snapshot and itself are the exact same thing

    • Drop - throw a variable away into the “trash” (dictionaries have to be squashed, cant use drop)

    • Clone - user-defined action. everything that has a copy has a clone action. copy is a lightweight substitute for clone. if you clone an array you will get an entirely new array; heavy operation

    • Destruct - everything that has a drop has a destruct action. dictionaries also have a destruct action that actually calls squash

    • PartialEq - equality operator

    • Serde

Starknet contracts

#[contract]
mod MyContract {
	struct Storage {
		Value: u64,
	} 

	#[constructor]
	fn init(value: u64) { ... } 

	#[view]
	fn getter() -> u64 { ... } 

	#[external]
	fn setter(value: u64) -> { ... } 
} 

Last updated