Class hash

The class hash is a hash chain of the definition of the class.

Definition of a (Cairo 0) class

Cairo 0 classes are deprecated, and will no longer be supported after regenesis

The elements that define a class are:

API version

The version of the class, currently always 0.

Array of external functions entry points

An entry point is a pair (selector, offset), where offset is the offset of the instruction that should be called inside the class’s bytecode.

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.

Array of used builtin names

An ASCII-encode array, ordered by declaration.

Program hash

The starknet_keccak of the class’s program. The class’s program is the abi and program part of the .json file that the StarkNet compiler outputs when you run the following command:

$ starknet-compile --no_debug_info

The compiler outputs abi, entrypoint selectors and the program. For program hash, starknet_keccak of only abi and program needs to be calculated. To see the exact computation of this field, see contract_hash.py.

Bytecode

Represented by an array of field elements.

Computing the (Cairo 0) class hash

\[\begin{aligned} \text{class_hash} = h( & \text{api_version}, \\& \text{external_entry_points}, \\& \text{l1_handler_entry_points}, \\& \text{constructor_entry_points}, \\ & \text{builtin_names}, \\& \text{program_hash}, \\& \text{bytecode_hash}) \end{aligned}\]

Where

  • \(h\) is the Pedersen hash function

  • the Pedersen hash of an array is defined here

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

  • the program_hash is the starknet_keccak of the json described above

  • the bytecode_hash is simply the hash of the bytecode array

Definition of a (Cairo 1) class

Classes that were written in Cairo 1 are defined by their Sierra code ( resulting from the compilation of the Cairo code into Sierra, see here for more details).

The elements 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 1 compiler.

Sierra program

An array of field elements representing the Sierra instructions.

Computing the (Cairo 1) class hash

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

\[\begin{aligned} \text{class_hash} = h( & \text{contract_class_version}, \\& \text{external_entry_points}, \\& \text{l1_handler_entry_points}, \\& \text{constructor_entry_points}, \\ & \text{abi_hash}, \\&\text{sierra_program_hash}) \end{aligned}\]

Where

  • \(h\) is the Poseidon hash function

  • the Poseidon hash of an array is defined here

  • 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 simply the 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.