walnuthq / cairovm.codes

Source code for cairovm.codes — a web app to compile Cairo programs into Sierra and CASM with step-through execution capabilities.
https://cairovm.codes
MIT License
29 stars 24 forks source link

Add costs to libfunctions #173

Open kwkr opened 1 month ago

kwkr commented 1 month ago

There is a field costs returned on the requests that runs the code that contains the information about costs for specific libfunctions. The costs object looks as follows:

{
  "costs": {
    "function_costs": {
      "main::main::main": {
        "const_cost": 200,
        "step": 0,
        "hole": 0,
        "range_checks": 0,
        "pedersen": 0,
        "poseidon": 0,
        "bitwise": 0,
        "ec_op": 0
      }
    },
    "variable_costs": {},
    "statements_costs": {
      "0": [
        {
          "statement_type": "Invocation",
          "statement_index": 0,
          "costs": {
            "const_cost": 0,
            "step": 0,
            "hole": 0,
            "range_checks": 0,
            "pedersen": 0,
            "poseidon": 0,
            "bitwise": 0,
            "ec_op": 0
          }
        }
      ],
      "1": [
        {
          "statement_type": "Invocation",
          "statement_index": 1,
          "costs": {
            "const_cost": 0,
            "step": 0,
            "hole": 0,
            "range_checks": 0,
            "pedersen": 0,
            "poseidon": 0,
            "bitwise": 0,
            "ec_op": 0
          }
        }
      ],

      "2": [
        {
          "statement_type": "Invocation",
          "statement_index": 2,
          "costs": {
            "const_cost": 100,
            "step": 0,
            "hole": 0,
            "range_checks": 0,
            "pedersen": 0,
            "poseidon": 0,
            "bitwise": 0,
            "ec_op": 0
          }
        }
      ],
      "3": [
        {
          "statement_type": "Invocation",
          "statement_index": 3,
          "costs": {
            "const_cost": 0,
            "step": 0,
            "hole": 0,
            "range_checks": 0,
            "pedersen": 0,
            "poseidon": 0,
            "bitwise": 0,
            "ec_op": 0
          }
        }
      ],
      "4": [
        {
          "statement_type": "Invocation",
          "statement_index": 4,
          "costs": {
            "const_cost": 100,
            "step": 0,
            "hole": 0,
            "range_checks": 0,
            "pedersen": 0,
            "poseidon": 0,
            "bitwise": 0,
            "ec_op": 0
          }
        }
      ],
      "5": [
        {
          "statement_type": "Return",
          "statement_index": 5,
          "costs": {
            "const_cost": 0,
            "step": 0,
            "hole": 0,
            "range_checks": 0,
            "pedersen": 0,
            "poseidon": 0,
            "bitwise": 0,
            "ec_op": 0
          }
        }
      ]
    }
  }
}

The costs should be included next to the specific statements in the Sierra code here: image For this purpose, the property statements_costs should be used. It contains a mapping statement_index => array of costs for a specific statement. If there are multiple elements in the array, they should be just listed next to each other as they represent costs for different branches. The value that should be displayed is the const_cost from the costs object.

The costs above are for the following sample code

use core::felt252;

fn main() -> felt252 {
    let n = 2 + 3;
    n
}