StarkWare
Published in

StarkWare

Open Sourcing Cairo 1.0!

TL;DR

  • We now present a first look into the Cairo 1.0 compiler. You can now start experimenting with basic Cairo 1.0 code
  • Cairo 1.0 at its core is very similar to Rust
  • Consider it a first taste, not a release. More improvements are on the way. The first version of the compiler is planned for early Q1 next year.
  • Cairo 1.0 is not supported on StarkNet, yet. It will be supported on StarkNet in Q1 next year.

Intro

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:

  • The existing compiler is developed in the same repo as StarkNet itself, making it harder to track language changes. The Cairo 1.0 compiler is written from the ground up, allowing for faster feature development and for more community involvement.
  • Every computation is now provable. Currently, a Cairo program may fail with specific inputs (e.g. by reaching an `assert 1=2` instruction in some computation branch), rendering the computation unprovable. With Cairo 1.0, programs are provable in every possible branch. This is particularly important for DOS protection and censorship resistance in StarkNet.

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.

Current Capabilities

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.

Code Examples

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.

Example I: match expressions

In Cairo 1.0, you can use rust-like match expressions. No longer will you fear if/else statements that may cause reference revocation!

Example II: Data types

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.

Example III: structs and ownership

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.

Example IV: Error propagation

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.

Try it yourself

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.

Future Plans

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:

  • Loops
  • New library functions
  • Improved language server
  • A native notion of StarkNet gas

Make sure to stay tuned and track the compiler progress!

--

--

Developing the Full Proof Stack for STARK

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store