Class hash

The class hash is a hash chain of the components that define the class.

Classes written in Cairo are compiled into Sierra code. The Sierra code generated is an intermediate representation of the class. This new contract class is then compiled by the sequencer, via the Sierra → Casm compiler, to generate the Cairo assembly associated with this class. The resulting Casm code is then executed by the Starknet OS.

For information on how the compiler converts code from Cairo to Sierra, see Cairo and Sierra.

Components of a Cairo class definition

The components that define a class are:

contract_class_version

The version of the contract class object. Currently, the Starknet OS supports version 0.1.0

Array of external functions entry points

An entry point is a pair (selector, function_idx), where function_idx is the index of the function inside the Sierra program.

The selector is an identifier through which the function is callable in transactions or in other classes. The selector is the starknet_keccak hash of the function name, encoded in ASCII.

Array of L1 handlers entry points

-

Array of constructors entry points

Currently, the compiler allows only one constructor.

ABI

A string representing the ABI of the class. The ABI hash (which affects the class hash) is given by:

starknet_keccak(bytes(ABI, "UTF-8"))

This string is supplied by the user declaring the class (and is signed on as part of the DECLARE transaction), and is not enforced to be the true ABI of the associated class. Without seeing the underlying source code (i.e. the Cairo code generating the class’s Sierra), this ABI should be treated as the "intended" ABI by the declaring party, which may be incorrect (intentionally or otherwise). The "honest" string would be the json serialization of the contract’s ABI as produced by the Cairo compiler.

Sierra program

An array of field elements representing the Sierra instructions.

Computing the Cairo class hash

The hash of the class is the chain hash of its components, computed as follows:

class_hash = ℎ(
    contract_class_version,
    external_entry_points,
    l1_handler_entry_points,
    constructor_entry_points,
    abi_hash,
    sierra_program_hash
)

Where

  • \(h\) is the Poseidon hash function

  • The hash of an entry point array \((selector,index)_{i=1}^n\) is given by \(h(\text{selector}_1,\text{index}_1,...,\text{selector}_n,\text{index}_n)\)

  • The sierra_program_hash is the Poseidon hash of the bytecode array

The Starknet OS currently supports contract class version 0.1.0, which is represented in the above hash computation as the ASCII encoding of the string CONTRACT_CLASS_V0.1.0 (hashing the version in this manner gives us domain separation between the hashes of classes and other objects).

For more details, see the Cairo implementation.