StarkNet State
The state of StarkNet consists of:
- contract instances: a mapping between addresses (251 bit field elements) and contract’s state.
- contract classes: a mapping between class hash and the class definition
A contract state consists of:
- class_hash (defines the functionality)
- contract storage (a key-value mapping where the key/values are field elements)
With the above definition, we can provide a brief sketch of StarkNet’s transition function. A transaction transitions the system from state to state if:
- is an invoke transaction, and the storage of is the result of executing the target contract code with respect to the previous state (the arguments, contract address, and the specific entry point are part of the transaction)
- is a deploy transaction, and contains the new contract’s state at the contract’s address. Additionally, the storage of is updated according to the execution of the contract’s constructor.
- is a declare transaction, and contains the class hash and definition in the contract classes mapping
State Commitment​
The state commitment is a digest which uniquely (up to hash collisions) encodes the state. In StarkNet, the commitment is the root of a binary Merkle-Patricia tree of height 251. Like Ethereum, this is a 2-level structure where the contract address determines the path from the root to the leaf encoding the contract state. The information stored in the leaf is:
Where:
- is the hash of the contract’s definition discussed here
- is the root of another Merkle-Patricia tree of height 251 that is constructed from the contract’s storage
- is the Pedersen hash function.
info
Note that, as of the time of writing, the commitment does not include classes information. This will be added in future versions.
Merkle-Patricia tree​
Specifications​
As mentioned above, our commitment scheme uses a binary Merkle-Patricia tree with the Pedersen hash function. Each node in the tree is represented by a triplet . The actual data is placed in the leaves, and a leaf node with data is encoded by the triplet . Empty nodes (leaves or internal) are encoded by the zero triplet . A subtree rooted at a node has a single non-empty subtree, rooted at the node obtained by following the path specified by .
is an integer in , and the binary expansion of indicates how should we proceed along the tree, where the first step is indicated by the most significant bit, and are interpreted as left, right correspondingly. Note that the reason that length is specified and cannot be deduced from is that we’re dealing with field elements of fixed size (251 bits each). For a node with , following leads the highest node whose both right and left child are none empty.
The following rules specify how the tree is constructed from a given set of leaves:
The hash of a node , denoted by , is:
Note that any arithmetic operations in the above are done in our field.
We can now proceed to recursively define the nodes in the trie. The triplet representing the parent of the nodes , is given by:
Example Trie​
We now show an example of the construction of a height 3 Merkle-Patricia tree from the leaves :
Where . Note that in our example there is no skipping from the root (length is zero), so the final commitment to the tree will be .
Suppose that we want to prove, with respect to the commitment we have just computed, that the value of the leaf whose path is given by is . In a standard Merkle tree, the proof would have consisted of data from three nodes (siblings along the path to the root). Here, since the tree is sparse, we only need to send the two children of the root . This suffices to reproduce the commitment , and since the height of the tree, , is known and fixed, we know that the path of length specified by the right child leads us to the desired leaf.