#hash #starknet #ethereum #cryptography #web3

pathfinder-class-hash

Pathfinder's class hash computation and verification

2 releases

new 0.15.4-beta.1 Feb 10, 2025
0.15.4-beta.0 Feb 7, 2025

#392 in Magic Beans

Download history 173/week @ 2025-02-05

173 downloads per month

MIT/Apache

4MB
76K SLoC

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

Implementation Details

Cairo Class Hash

The Cairo class hash computation follows these steps:

  1. The contract definition is prepared by removing debug info and handling special cases for Cairo 0.8+ attributes
  2. The prepared definition is serialized to JSON with Python-compatible formatting
  3. A truncated Keccak hash is computed from the serialized JSON
  4. Entry points, builtins, and bytecode are processed through hash chains
  5. The final class hash is computed by combining all components

Sierra Class Hash

The Sierra class hash computation is simpler:

  1. The contract version is validated
  2. Entry points are processed in order
  3. The ABI string is hashed
  4. The Sierra program is hashed
  5. 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.

Dependencies

~9–16MB
~194K SLoC