Expand description
Class hash computation for Cairo and Sierra contracts.
This crate provides functionality to compute class hashes for both Cairo 0.x and Sierra (Cairo 1.x+) contracts in the Starknet ecosystem. The class hash is a unique identifier for a contract’s code that is used throughout the Starknet protocol.
§Class Hash Types
There are two main types of class hashes:
- Cairo 0.x class hashes - Computed for legacy Cairo contracts
- Sierra class hashes - Computed for newer Cairo 1.x+ contracts using the Sierra intermediate representation
§Main Components
compute_class_hash
- The main entry point for computing class hashes for both Cairo and Sierra contractsComputedClassHash
- An enum representing the computed hash for either contract typePreparedCairoContractDefinition
- A prepared Cairo contract definition ready for hashingRawCairoContractDefinition
- An unprepared Cairo contract definition
§Implementation Details
§Cairo Class Hash
The Cairo class hash computation follows these steps:
- The contract definition is prepared by removing debug info and handling special cases for Cairo 0.8+ attributes
- The prepared definition is serialized to JSON with Python-compatible formatting
- A truncated Keccak hash is computed from the serialized JSON
- Entry points, builtins, and bytecode are processed through hash chains
- The final class hash is computed by combining all components
§Sierra Class Hash
The Sierra class hash computation is simpler:
- The contract version is validated
- Entry points are processed in order
- The ABI string is hashed
- The Sierra program is hashed
- All components are combined into the final hash
§Compatibility
This crate maintains compatibility with the official Starknet implementation and includes extensive test vectors to ensure hash computation matches the network’s expectations.
See the official Starknet documentation for more details on class hash computation.
Modules§
- from_
parts - Helpers to compute class hashes from the parts that compose a Cairo or Sierra contract
- json
- Helpers to parse and serialize the parts that compose a Cairo or Sierra contract
Structs§
- Prepared
Cairo Contract Definition - A prepared Cairo contract definition ready for class hash computation.
- RawCairo
Contract Definition - An unprepared Cairo contract definition.
Enums§
- Computed
Class Hash - Computed class hash
Functions§
- compute_
cairo_ class_ hash - Computes the class hash for given Cairo class definition.
- compute_
cairo_ hinted_ class_ hash - Compute class hash for a Cairo contract definition
- compute_
class_ hash - Computes the starknet class hash for given class definition JSON blob.
- compute_
sierra_ class_ hash - Computes the class hash for a Sierra class definition.
- parse_
contract_ definition - Parse either a Sierra or a Cairo contract definition.
- prepare_
json_ contract_ definition - Prepares a Cairo contract definition for class hash computation by applying necessary transformations.
- truncated_
keccak - Computes a truncated Keccak hash compatible with Starknet’s field element representation.