Skip to content

Instantly share code, notes, and snippets.

@soos3d
Last active October 18, 2022 20:51
Show Gist options
  • Save soos3d/25c18267d6698eb94dcc5bc2fe0ffd4d to your computer and use it in GitHub Desktop.
Save soos3d/25c18267d6698eb94dcc5bc2fe0ffd4d to your computer and use it in GitHub Desktop.
This Gist holds and introduction to the StarkNet ecosystem and the Cairo smart contract language.

Intro to StarkNet and Cairo

StarkNet is a permissionless decentralized ZK-Rollup operating as an L2 network over Ethereum, where any dApp can achieve unlimited scale for its computation. All this without compromising Ethereum’s composability and security.

Sources:

Table of contents

StarkNet basic architecture

Currently, the StarkNet architecture consists of the following core components:

  • Sequencer — a closed source component that decides what sequence of transactions will be included in the next block. The Sequencer sends the block to a Prover. This is done on L2.
  • Prover — a closed source component that computes a proof of the L2 state and submits it to the Verifier. This is done on L2.
  • Verifier — an open-source contract on Ethereum that verifies the proof.

StarkNet full node

The full node implementation is Pathfinder.

With the Pathfinder node, you get the following features on StarkNet:

  • Access to the full StarkNet state history, including contract code, storage, and transactions.
  • State verification using the L1 contracts.
  • Calculation of the StarkNet state’s Patricia-Merkle Trie root on a block-by-block basis and confirmation of the root against L1.
  • Ethereum-like RPC API.
  • Ability to run StarkNet functions without requiring a StarkNet transaction—execution against the local state.

How ZK-Rollups work

Zero-knowledge rollups or zk-proofs are a mathematical tool that allows systems to prove that certain transactions have been correctly settled and updated the state of the blockchain without actually processing those transactions.

By generating off-chain proof of validity for the transactions, ZK-rollups can save a significant amount of gas fees.

StartkNet transaction lifecycle

When a transaction is submitted in StarkNet, it goes to a sequencer node.

The sequencer takes batches of transactions and generates two things:

  • A list of changes caused by all the transactions in the batch (changes in storage, balances, etc ).
  • A proof that, if all transactions included in the batch are executed successfully against the previous state of the network, the result will be the list of changes listed before. After that, the proof and list of changes are sent to Ethereum where the rollup contract verifies them and updates the state.

The last part is important because that means that all Ethereum nodes are actually StarkNet verifiers. This also means that zk-proofs can be used in other EVM compatible blockchains and that StarkNet could potentially be deployed as an L2 solution for other protocols.

In StarkNet, the transactions are not recorded on-chain—only the state changes resulting from the transactions themselves are recorded on-chain in L1.

Programming language: Cairo

Cairo is not a smart contract language like Solidity, it’s a general-purpose language that allows you to write programs that can be executed by a StarkNet prover and generates a proof that can be verified later on by an Ethereum node.

Why use Cairo and not Solidity? It’s needed to generate mathematical proofs. Cairo requires Python and although the programs are written using the Cairo language, the tests are written in Python. It’s similar to smart contracts written in Solidity and tests in JavaScript.

Cairo is a programming language for writing provable programs, where one party can prove to another that a specific computation was executed correctly. Cairo and similar proof systems can be used to provide scalability to blockchains.

StarkNet uses the Cairo programming language both for its infrastructure and for writing StarkNet contracts.

The Nethermind Warp tool allows us to transpile Solidity to Cairo. Cairo playground

Install Cairo

To use Cairo, you need to have Python 3.9. Let's install it first:

Install Python 3.9

Update the packages list and install the prerequisites:

sudo apt update
sudo apt install software-properties-common

Add the deadsnakes PPA to your system’s sources list:

sudo add-apt-repository ppa:deadsnakes/ppa

When prompted, press [Enter] to continue.

Once the repository is enabled, you can install Python 3.9 by executing:

sudo apt install python3.9

Verify that the installation was successful by typing:

python3.9 --version

Should return something like this:

Python 3.9.1+

Create a Python 3.9 virtual environment

Install the Python 3.9 virtual environment.

sudo apt-get install python3.9-dev python3.9-venv

Create the virtual environment in your directory.

python3.9 -m venv starknet_cairo

And activate it

source starknet_cairo/bin/activate

Install Cairo

Use pip to install Cairo.

pip3 install cairo-lang

Install the following dependencies if you get any error

sudo apt install -y libgmp3-dev
pip3 install ecdsa fastecdsa sympy

Now Python 3.9 and Cairo are ready to go!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment