cairo icon indicating copy to clipboard operation
cairo copied to clipboard

feat: add CASM serialization of Cairo programs

Open zmalatrax opened this issue 8 months ago • 10 comments

This PR adds a new command sierra-compile-json which outputs a JSON with everything needed to run a Cairo v2.6.4 program in any environment (i.e. outside of Rust).

Rationale: There is currently no straightforward way to execute a Cairo program outside of a Rust project. It is much needed to provide other implementations of the Cairo VM a way to run Cairo programs the same way StarkNet contracts can.

I believe that such feature belongs to the Cairo compiler rather than an external project (e.g. Universal-Sierra-Compiler).

It follows the same structure as *.compiledContractClass.json output from the command starknet-sierra-compile. The fields of the *.casm.json json output are:

  • prime
  • compiler_version
  • bytecode
  • hints
  • pythonic_hints
  • entrypoint
  • builtins

The last two fields are the exact same as offset and builtins from the elements of entry_points_by_type (besides the selector)

When using the command sierra-compile-json, a flag gas-usage-check enables the GasBuiltin, as this builtin is not mandatory for Cairo programs(?)

I haven't added the bytecode_segment_lengths as it is only used in StarkNet contracts with multiple entrypoints (to be confirmed, might be a wrong assumption).

Example *.casm.json outputs can be found here


This change is Reviewable

zmalatrax avatar Jun 28 '24 15:06 zmalatrax