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.
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.
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:
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:
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.
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:
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!
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.