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:
- StarkNet basic architecture
- StarkNet full node
- How ZK-Rollups work
- StartkNet transaction lifecycle
- Programming language: Cairo
- Install Cairo
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.
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.
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.
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.
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
To use Cairo, you need to have Python 3.9. Let's install it first:
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+
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
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!