BG_LEFTBG_RIGHT
17 September 2022
7 min read

Koselig with Cairo

Welcome to Part 4 of our Back to Basics series, 'Koselig with Cairo.' Hang on tight folks as we have got something exciting lined up for you ⚑️

Koselig with Cairo

By reading the title, you must have already started visualizing pyramids, mummies, and deserts. But today, we will not explore the deserts and the life of the Pharaohs but rather deep dive into Cairo, a programming language that has taken the L2 ecosystem by a storm πŸŒͺ

Introduction to Cairo

Cairo is a language invented by StarkWare, which was founded in 2018 to scale Ethereum using STARK proofs. We've also covered this in our previous blog, State of StarkWare. You can read it here.

Alright, now let's kickstart with Cairo!

The name Cairo comes from the term β€œCPU AIR” β€” an AIR implementing the concept of a CPU. It is an efficient and practical von Neumann architecture that can be used with the STARK proof system to generate proofs of computational integrity.

The Cairo program produces a trace that can then be sent to a trustless prover, which generates a STARK proof for the validity of the statement. The proof can then be verified using a verifier (which may or may not be on-chain).

Cairo: Deep Dive

Alright, now we've got the basic understanding of Cairo. So, let's dive deep and address the following questions one by one:

1. Why learn Cairo?

Today, most dApps are basically a Solidity contract implementing some sort of logic, some frontend, and maybe a backend. These dApps, when successful, inevitably face the scalability problem.

Now, dApps are solving their scalability problems by turning to proof-based L2 scalability solutions. An off-chain component takes over some of the more complex parts of the business logic and communicates with the on-chain smart contracts without giving up on security.

Before Cairo, creating a proof system that covers a specific business logic was hard. With Cairo, the barriers to using proofs to achieve scalability are much lower. You write your complex logic in Cairo, get it proved off-chain, and once that proof is validated on-chain, your smart contract application can use the result trustlessly.

The Cairo blog titled β€œCairo for Blockchain Developers” mentions β€”

The role of the dApp's smart contract changes β€” from the component in charge of executing expensive business logic, it becomes the component in charge of dealing with the inexpensive consequences of that business logic.

Marquee investor Ryan Berckman also shared his views on StarkNet below:

2. How does Cairo work?

In Cairo, a new concept originated called The Shared Prover (or SHARP). It was released by StarkWare back on February 16, 2021.

The SHARP is the connecting link between the Cairo code and the Solidity smart contract. It has three main components β€” a prover (off-chain), a verifier smart contract (on-chain), and a fact registry contract (on-chain).

Below, ZK Daily explains SHARP in simple terms:

Now, let's see how the computation works under the hood:

  • Step 1: The prover takes Cairo's execution trace, proves that it's valid, and sends this proof to the verifier.
  • Step 2: The on-chain verifier then verifies the Proof.
  • Step 3: Next, the verifier writes a fact that attests to the proof's validity in the fact registry contract.
  • Step 4: Finally, the dApp's smart contract checks if this fact exists in order to rely on the computation that was executed off-chain.
Cairo for Blockchain Developers
Source: Cairo for Blockchain Developers

Let's try to understand this concept via an example 🧐

Imagine a blockchain-based Wordle, where you'll get tokens for guessing the correct word. In today's tech, you'll have to write the entire game logic in Solidity. Needless to say, that's an expensive affair.

Instead, you can write this logic in Cairo and execute it off-chain. Here's how:

  1. At first, the Cairo program will check the word.
  2. Next, it will activate the SHARP to generate proof and verify it on-chain.
  3. After that, it'll write a fact which is an approval stamp that this word was the right one.
  4. Lastly, Wordle's smart contracts will check that this fact exists and pay the tokens to the winner.

3. Advantages of Cairo

The Cairo WhitePaper gives us a detailed explanation of Cairo's benefits. Let us check out a few important ones below:

  • Efficient: The Cairo instruction set was chosen so that the corresponding AIR will be as efficient as possible.
  • Practical: Cairo supports conditional branches, memory, function calls, and recursion.
  • Production-grade: Cairo is the backbone of multiple cryptocurrency systems that run over the Ethereum blockchain.
  • Builtins: The Cairo architecture supports the implementation of predefined operations directly, as a set of equations, instead of implementing them with Cairo code.
  • Efficient public memory: In Cairo's memory implementation, each memory cell that should be shared with the verifier adds a verification cost of only 4 arithmetic operations.

Cairo in action!

We think the best way to learn a programming language is by writing some code. We've also added comments beside every syntax and logic so that you can seamlessly understand the code.

Input

Output

BIG wins by Cairo

Cairo is fully functional and has already shown results at the production level for many applications!

Have a look at some of the achievements of Cairo ✨

Introduction to Cairo YT Video
Source: Introduction to Cairo YT Video

Cairo 1.0

Recently, StarkWare announced Cairo 1.0, a new version of Cairo. The update will have:

  • Sierra (Safe Intermediate Representation), a new intermediate representation layer between Cairo 1.0 and Cairo byte code.
  • A brand new compiler that will allow the developers to do more in a simpler way.
  • Overall improvement to the programming language (e.g. improved syntaxes, more intuitive libraries, better compiler integration, etc.).

To know more about Cairo 1.0 check out this medium blog.

Cairo across the globe

  • πŸŽͺ Cairo 101 Workshop
  • πŸ—“ March 14th-15th, 2022
  • πŸ’Ό StarkWare Co-founder & President Eli Ben Sasson explaining Cairo technicalities
Cairo 101 Workshop
Source: Cairo 101 Workshop

  • πŸŽͺ StarknetCC
  • πŸ—“ July 22, 2022
  • πŸ’Ό Cairo's co-creator @PapiniShahar talks about the next evolutions of Cairo

StarkCon

StarkCon is a community-driven conference by ZKX and StarkWare to raise awareness about Cairo and the StarkNet Ecosystem across the globe. The event brings together the leading voices of StarkNet and will have the best of developers, builders, founders, investors, and global media in attendance. Recently, they announced a $50K grant for developers and builders to boost Cairo's development and adoption in India.

Cairo repo

Now, you've got a brief about Cairo and realized the potential it holds!

But, we guess you don't want to stop your journey of learning Cairo here 🧐

So, we've collated some of the best resources to help you learn more about Cairo:

  1. StarkWare's official Cairo documentation
  2. Learn Cairo by playing games
  3. Cairo's GitHub Repo
  4. StarkNet Edu Newsletter
  5. CairoLang Twitter Page
  6. HackerNoon Cairo Repo
  7. Perama's Notes
  8. StarkNet tutorials
  9. Cairo by example
  10. StarkWare'e Dev Advocate Henri's Twitter
  11. Cairo Goldmine GitHub
  12. https://github.com/gakonst/awesome-starknet
  13. https://github.com/lightshiftdev/starknet-bootcamp

Alright, folks β€” That's a wrap for the fourth edition of our Back to Basics Series. For now, let's learn about Cairo and work towards a decentralized future. WAGMI πŸ™Œ

About ZKX

ZKX is a permissionless protocol for derivatives built on StarkNet, with a decentralized order book and a unique way to offer complex financial instruments as swaps. The protocol is powered by a DAO and will provide an elevated trading experience with gamified leaderboards and unique liquid governance. ZKX's mission is to democratize access to global yields through its offerings to anyone, anywhere.

Twitter | Discord | Telegram | Website

Join our waitlist