Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: minor fixes in "Class hash" #556

Merged
merged 11 commits into from
Jun 16, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,6 @@

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

== Definition of a (Cairo 0) class

[IMPORTANT]
====
Cairo 0 classes are deprecated, and will no longer be supported after regenesis
====

The elements that define a class are:

[horizontal,labelwidth=35]
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.
+
[NOTE]
====
The selector is an identifier through which the function is callable in transactions or in other classes. The selector is the xref:../Hashing/hash-functions.adoc#starknet-keccak[starknet_keccak] hash of the function name, encoded in ASCII.
====
Array of xref:documentation:architecture_and_concepts:L1-L2_Communication/messaging-mechanism.adoc#l1-l2_messages[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 xref:../Hashing/hash-functions.adoc#starknet-keccak[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:
+
[source,shell]
----
$ 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 https://github.com/starkware-libs/cairo-lang/blob/7712b21fc3b1cb02321a58d0c0579f5370147a8b/src/starkware/starknet/core/os/contract_hash.py#L116[contract_hash.py^].
Bytecode:: Represented by an array of field elements.

=== Computing the (Cairo 0) class hash

[stem]
++++
\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

* stem:[$h$] is the xref:../Hashing/hash-functions.adoc#pedersen_hash[Pedersen] hash function
* the Pedersen hash of an array is defined xref:../Hashing/hash-functions.adoc#pedersen_array_hash[here]
* the hash of an entry point array stem:[$(\text{selector},\text{offset})_{i=1}^n$] is given by stem:[$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 xref:../Hashing/hash-functions.adoc#pedersen_array_hash[hash] of the bytecode array

[id="cairo1_class"]
== Definition of a (Cairo 1) class

Expand All @@ -67,10 +19,10 @@ Array of external functions entry points:: An entry point is a pair `(_selector_
+
[NOTE]
====
The selector is an identifier through which the function is callable in transactions or in other classes. The selector is the xref:../Hashing/hash-functions.adoc#starknet-keccak[starknet_keccak] hash of the function name, encoded in ASCII.
The selector is an identifier through which the function is callable in transactions or in other classes. The selector is the xref:../Hashing/hash-functions.adoc#starknet_keccak[starknet_keccak] hash of the function name, encoded in ASCII.
====
Array of xref:documentation:architecture_and_concepts:L1-L2_Communication/messaging-mechanism.adoc#l1-l2_message_fees[L1 handlers] entry points :: -
Array of constructors entry points :: Currently, the compiler allows only one constructor.
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:
+
[source,python]
Expand All @@ -84,7 +36,7 @@ This string is supplied by the user declaring the class (and is signed on as par
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.
Sierra program:: An array of field elements representing the Sierra instructions.

=== Computing the (Cairo 1) class hash

Expand All @@ -101,9 +53,8 @@ The hash of the class is the chain hash of its components, computed as follows:
Where

* stem:[$h$] is the xref:../Hashing/hash-functions.adoc#poseidon_hash[Poseidon] hash function
* the Poseidon hash of an array is defined xref:../Hashing/hash-functions.adoc#poseidon_array_hash[here]
* the hash of an entry point array stem:[$(selector,index)_{i=1}^n$] is given by stem:[$h(\text{selector}_1,\text{index}_1,...,\text{selector}_n,\text{index}_n)$]
* the `sierra_program_hash` is simply the xref:../Hashing/hash-functions.adoc#poseidon_array_hash[hash] of the bytecode array
* The hash of an entry point array stem:[$(selector,index)_{i=1}^n$] is given by stem:[$h(\text{selector}_1,\text{index}_1,...,\text{selector}_n,\text{index}_n)$]
* The `sierra_program_hash` is the xref:../Hashing/hash-functions.adoc#poseidon_hash[Poseidon] hash of the bytecode array

[NOTE]
====
Expand All @@ -112,3 +63,52 @@ the hashes of classes and other objects).
====

For more details, see the https://github.com/starkware-libs/cairo-lang/blob/7712b21fc3b1cb02321a58d0c0579f5370147a8b/src/starkware/starknet/core/os/contracts.cairo#L47[Cairo implementation].

== Definition of a (Cairo 0) class

[IMPORTANT]
====
Cairo 0 classes are deprecated, and will no longer be supported after regenesis.
====

The elements that define a class are:

[horizontal,labelwidth=35]
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.
+
[NOTE]
====
The selector is an identifier through which the function is callable in transactions or in other classes. The selector is the xref:../Hashing/hash-functions.adoc#starknet_keccak[starknet_keccak] hash of the function name, encoded in ASCII.
====
Array of xref:documentation:architecture_and_concepts:L1-L2_Communication/messaging-mechanism.adoc#l1-l2_messages[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 xref:../Hashing/hash-functions.adoc#starknet_keccak[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:
+
[source,shell]
----
$ starknet-compile --no_debug_info
----
+
The compiler outputs the abi, entrypoint selectors, and program. For the program hash, only the `starknet_keccak` of the abi and program needs to be calculated. To see the exact computation of this field, see https://github.com/starkware-libs/cairo-lang/blob/7712b21fc3b1cb02321a58d0c0579f5370147a8b/src/starkware/starknet/core/os/contract_hash.py#L116[contract_hash.py^].
Bytecode:: Represented by an array of field elements.

=== Computing the (Cairo 0) class hash

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

[stem]
++++
\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

* stem:[$h$] is the xref:../Hashing/hash-functions.adoc#pedersen_hash[Pedersen] hash function
* The hash of an entry point array stem:[$(\text{selector},\text{offset})_{i=1}^n$] is given by stem:[$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 the xref:../Hashing/hash-functions.adoc#pedersen_hash[Pedersen] hash of the bytecode array