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 ⚡️
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
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.
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:
- At first, the Cairo program will check the word.
- Next, it will activate the SHARP to generate proof and verify it on-chain.
- After that, it’ll write a fact which is an approval stamp that this word was the right one.
- 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.
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 ✨
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
- 🎪 Layer Two Amsterdam
- 🗓 April 19th-20th, 2022
- 💼 ZKX CTO BitPactum talked about building Defi 3.0 using Cairo
- 🎪 StarknetCC
- 🗓 July 22, 2022
- 💼 Cairo’s co-creator @PapiniShahar talks about the next evolutions of Cairo
- 🎪 Web3Conf India
- 🗓 August 12th-14th, 2022
- 💼 Nethermind engineer Swapni Raj talking about Cairo’s benefits
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.
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:
- StarkWare’s official Cairo documentation
- Learn Cairo by playing games
- Cairo’s GitHub Repo
- StarkNet Edu Newsletter
- CairoLang Twitter Page
- HackerNoon Cairo Repo
- Perama’s Notes
- StarkNet tutorials
- Cairo by example
- StarkWare’e Dev Advocate Henri’s Twitter
- Cairo Goldmine GitHub
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 🙌
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.