What is Cairo Lang? 10 Best Resources for Scaling dApps Using STARKs

Written by aleksandrmalyshev | Published 2022/08/10
Tech Story Tags: blockchain | programming | crypto | blockchain-technology | smart-contracts | cairo | software-development | software-engineering

TLDRIf you are looking for a cost-effective provable computation programming language for dApps, consider Cairo. It’s native to Provable Computation and includes low-level access to memory, underlying primitives, and functional-style programming. In this article you will find code examples and a list of resources how to learn Cairo in 2022via the TL;DR App

This year, cryptocurrency has experienced a rough ride — the value of Bitcoin, Ethereum, and other digital currencies has plummeted by over 60%. Even though the crypto market has taken a downturn, the industry is still growing. And yet, it's unfortunate that people think of smart contracts as just an extension of finance (DeFi) or a generalization of the Web (Web3), whereas they are platforms for composable computation.

Companies such as StarkWare are developing new methods for increasing blockchain scalability.

For instance, zk-STARKs or Zero Knowledge Scalable Transparent Argument of Knowledge is a trending innovation for financial privacy on the blockchain with the use of fast, scalable computations.

What are zk-Starks?

The innovation behind zk-STARKs is in operating as a Layer2 network over the Ethereum blockchain, which is usually called Mainnet. Zero-Knowledge allows users and developers to use all benefits of security and composability of the Mainnet.

Generally, this is the result of the cryptographic algorithms and hardcore mathematics lying within this algorithm. Those who are familiar with Ethereum know that Solidity is the main programming language for smart contracts.

In the same way, StarkNet has its own native language for dApps called Cairo, which is designed to scale Ethereum. Its advantage is the provable computation in the same way that Solidity has enabled composable computation. In simpler terms, Solidity language interfaces are primarily developed for contract-to-contract interaction, which exactly means “composable” computation.

In the Ethereum network, each validation requires a re-run of a transaction. In comparison, StarkNet offers much more economical operations, where transactions are validated by verifying that they have been executed with a certain outcome. It became possible with ZK Roll-ups, enabling a new paradigm called Provable Computation.

What is Cairo lang?

If you are looking for a cost-effective provable computation programming language, consider Cairo. It’s native to Provable Computation and includes low-level access to memory, underlying primitives, and functional-style programming. Listed below, are some of the benefits of Cairo:

  • It is a specially designed, provable language for smart contracts
  • It is not bounded by the EVM
  • Its functions are imported rather than inherited
  • It supports Solidity to Cairo transpiler
  • It includes full low-level access to underlying primitives

In its purest form, Cairo's source code looks similar to ASM code. The only values that may change over time are held within designated registers:

  • ap (allocation pointer) - points to a yet-unused memory cell.
  • fp (frame pointer) - points to the frame of the current function. The addresses of all the function’s arguments and local variables are relative to the value of this register. When a function starts, it is equal to ap. But unlike ap, the value of fp remains the same throughout the scope of a function.
  • pc (program counter) - points to the current instruction.

For example, if I want to calculate x^4 + x, using an allocation pointer, I can directly access the memory:

[ap] = 3; ap++
[ap] = [ap - 1] * [ap - 1]; ap++
[ap] = [ap - 1] * [ap - 1]; ap++
[ap] = [ap - 1] + [ap - 3]; ap++

Here's one more interesting example of how loops are organized in Cairo. In the example below, you can see the Cairo function that computes the sum of the elements of an array:

# Computes the sum of the memory elements at addresses:
# arr + 0, arr + 1, ..., arr + (size - 1)

func array_sum(arr:felt*, size) ->(sum:felt):
	if size == 0:
		return(sum=0)
	end

	# size is not zero
	let(sum_of_rest)= array_sum(arr=arr + 1, size=size - 1)
	return (sum=[arr] + sum_of_rest)
end

The recursion is used in Cairo as a replacement for the loop structure. The main reason for this is that Cairo’s memory is immutable, so there is no chance to change a memory cell in the future. For those who are not familiar with raw assembler source code, C-style programming languages, or functional programming languages such as Haskell or F#, it may be a bit scary at first. Yet, it is a strong advantage for developing memory-effective code.

Jumping from the raw Cairo code to StarkNet, you open a syntax sugar straight out of the box:

%builtins output
from starkware.cairo.common.serialize import serialize_word

func main{output_ptr : felt*}():
    tempvar x = 41
    serialize_word(x)
    return ()
end

If you're interested in learning more about the Cairo syntax, I encourage you to read the official documentation and a nice repo starknet-cairo-101.

Top 10 resources to help you learn Cairo in 2022

Cairo is a modern programming language that was created in 2020 and started its rise to popularity in 2022, with the growth of the StarkNet ecosystem. Are you interested in learning Cairo in 2022? Here are 10 resources about Cairo programming and StarkWare for you to check out:

  1. Narcissa – a Q&A forum with payable bounties for questions and answers about Cairo which the promising “crypto StackOverflow” is launching this August.
  2. Cairo Resources List – a curated list of awesome Cairo-related resources, libraries, tools, and more.
  3. Setting up the environment - a guide on how to install Cairo and StarkNet
  4. Starkware Cairo Whitepaper – a must-read for all Web3 enthusiasts
  5. Cairo Resource Guide – the official list with documentation, playground, whitepaper, and other resources
  6. starknet-erc721 – a workshop for learning how to deploy and customize the ERC721 token on StarkNet.
  7. How Cairo Works - a low-level explanation of Cairo's mechanics.
  8. Hello Cairo - tutorials for writing various simple Cairo contracts.
  9. Cairo Playground - an in-browser Cairo IDE, examples, and puzzles.
  10. Practical StarkNet lessons learned - helpful tips for new StarkNet/Cairo programmers.

Here are some notable GitHub and social communities related to Cairo:

  • Cairo Gang – the Cairo Devs Telegram community.

  • Cairo Lang Twitter – a Twitter community for Cairo-related discussions and topics to follow.

  • StarkNet Discord – the StarkNet server, where all participants discuss and share.

  • Cairo Resource Guide – the official list with documentation, playground, whitepaper, and many other resources.

  • Awesome Starknet – a curated list of StarkNet projects.

  • Cairo Goldmine – a comprehensive, annotated list of repositories of the StarkNet ecosystem.

  • starknet-libs - a curated list of useful libraries to develop on StarkNet.

What are your thoughts on Cairo? Let me know in the comments!

About the author

Aleksandr Malyshev is a software engineer and entrepreneur, who specializes in backend development and growth hacking. He is an experienced lead at Open Innovations who hosts and organizes well-known Eastern Europe software competitions. Aleksandr is the former Executive Officer at Steinbeis Consulting Center AI (STAI) in Stuttgart. Currently, he is working in Web3 and is involved in researching new technologies.


Written by aleksandrmalyshev | Backend Engineer, Growth Hacker, ex-CTO. Experienced in digital, food, data science. Disrupting the world with tech 🚀
Published by HackerNoon on 2022/08/10