Nov 24,2022 ·
6 min read
Page last updated 12 minutes ago
In 2020, we released Cairo, a Turing-complete programming language supporting verifiable computation. Cairo started as an assembly language and gradually became more expressive. Two months ago, we announced Cairo 1.0, which addresses some major issues in the current situation:
Today we mark the first milestone in reaching the above goals as we move the development to a public repo, and open source Cairo 1.0! Developers can now, for the first time, compile and execute simple Cairo 1.0 programs. This allows developers to start experimenting with Cairo 1.0 and gradually get accustomed to the new features, even if, at this phase, they cannot implement it on StarkNet just yet.
Currently, you can compile and execute basic native Cairo programs. While many of the syntax/language improvements are still underway, this allows getting used to Cairo 1.0 and enjoy upgrades as they come.
Note that writing StarkNet contracts is still unsupported. StarkNet syntax (storage variables / calling contracts / events and other system calls) will be added in the coming weeks.
To illustrate the differences between the old syntax and Cairo 1.0, we have chosen to show a few different implementations/flavors of finding the n’th Fibonacci number.
In Cairo 1.0, you can use rust-like match expressions. No longer will you fear if/else statements that may cause reference revocation!
While Cairo 0 worked with felts and pointers, in Cairo 1.0 we have native access to complex data types in the language. Below you can find an example that generates an array of the first n Fibonacci numbers.
As you can see above, rather than working directly with memory pointers, we use the
Array::<felt>\ type and the `array_append`function.
The following code illustrates the usage of structs in Cairo 1.0.
The following paragraph is meant for the Rustaceans among the audience. Cairo 1.0 manages memory in a similar way to rust. In particular, it uses the concepts of ownership and borrowing. Thus, By accessing a member of the `FibResult` struct (in this case, `result.value`), we’ve moved `result`, which means that unless FibResult is copyable, we can’t access it again in `result.index`. To overcome this, we add the `#[derive(Copy)]` attribute of the `FibResult` type. In future versions, we will add auto deconstruction for structs. This will allow moving ownership of one member without touching the others (in particular, the above code would compile even if `FibResult` didn’t have the copy attribute).
In particular, note that Cairo 1.0 is completely abstracting away the original (none deterministic read-only) memory model of Cairo.
The following code computes the n’th Fibonacci number, but unlike the previous examples, all the inputs are of the type uint128. Note that this solves a major pain point of handling uints in Cairo 0. Here, uint128 (and in the future uint256) are native types.
The addition of two 128 bit integers can cause an overflow. The above code uses the Option enum and the question mark operator to handle the case of overflow in one of the intermediate additions. Compare this to the current uint256 addition syntax, where the `unit256_check` function had to be called to guarantee soundness. In addition, in the near future, we will add the concept of `panic` to the language (similar to the panic macro in rust), and simple errors like addition overflow will be uncatchable and propagated automatically, which means that you won’t have to use `Option` or `?` when adding uints.
You can now compile and run currently supported Cairo 1.0 programs! Follow these instructions on how to use the `cairo-run` command. Note that under the hood, the Rust Cairo VM, developed by Lambdaclass, is used for execution.
You can find more examples to help you get started here. Note that this is only the first peek into the compiler development; in the coming weeks, we will improve the CLI alongside the compiler.
The focus of the first version of the Compiler, which is planned for early Q1, is supporting all existing functionality of StarkNet in Cairo 1.0. Additionally, we’re working on extending the capabilities of the Cairo 1.0 compiler. In the coming weeks, you can expect:
Make sure to stay tuned and track the compiler progress!
Share this post: