bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 73쪽 중 65쪽
* Using the [Universal Deployer Contract (UDC)](https://old-docs.openzeppelin.com/contracts-cairo/3.0.0-alpha.2/udc), which requires an existing account to send the `INVOKE` transaction
Upon receiving one of these transactions, the sequencer performs the following steps:
1. Runs the respective validation function in the contract, as follows:
* When deploying with the `DEPLOY_ACCOUNT` transaction type, the sequencer executes the `__validate_deploy__` function in the deployed contract.
* When deploying using the UDC, the sequencer executes the `__validate__` function in the contract of the sender's address.
2. Executes the constructor with the given arguments.
3. Charges fees from the new account address.
<Note>
If you use a `DEPLOY_ACCOUNT` transaction, the fees are paid from the address of the deployed account. If you use the UDC, which requires an `INVOKE` transaction, the fees are paid from the sender's account. For information on the differences between V1 and V3 `INVOKE` transactions, see [`INVOKE` transaction](/learn/protocol/transactions#transaction_types) in *Transaction types*.
</Note>
4. Sets the account's nonce as follows:
* `1`, when deployed with a `DEPLOY_ACCOUNT` transaction
* `0`, when deployed with the UDC
# Blocks
Source: https://docs.starknet.io/learn/protocol/blocks
## Overview
A Starknet block is a list of transactions and a block header that contains [multiple fields](/learn/protocol/blocks#block-header-fields), including the [hash of the previous block](/learn/protocol/blocks#block-hash) and a [commitment to their state diff](/learn/protocol/blocks#state-diff-commitment), as well as [commitments to the transactions, events, and receipts](/learn/protocol/blocks#transactions%2C-events%2C-and-receipts-commitments) it includes.
## Block header fields
| Name | Type | Description |
| - | - | - |
| `block_number` | `u64` | The number of this block (i.e., its height) |
| `parent_block_hash` | `felt252` | The [hash](/learn/protocol/blocks#block-hash) of the block's parent |
| `global_state_root` | `felt252` | A [commitment](/learn/protocol/state#state-commitment) to the new state |
| `sequencer_address` | `ContractAddress` | The Starknet address of the sequencer that created the block |
| `block_timestamp` | `Timestamp` | The time at which the sequencer began building the block, in seconds since the Unix epoch |
| `transaction_count` | `u32` | The number of transactions in the block |
| `events_count` | `u32` | The number of events in the block. |
| `state_diff_length` | `u32` | The total amount of storage diffs, nonce updates, deployed contracts, and declared classes |
| `state_diff_commitment` | `felt252` | A [commitment](/learn/protocol/blocks#state-diff-hash) to the state diff of the block |
| `transactions_commitment` | `felt252` | A [commitment](/learn/protocol/blocks#transactions%2C-events%2C-and-receipts-commitments) to the transactions included in the block |
| `events_commitment` | `felt252` | A [commitment](/learn/protocol/blocks#transactions%2C-events%2C-and-receipts-commitments) to the events included in the block |
| `receipts_commitment` | `felt252` | A [commitment](/learn/protocol/blocks#transactions%2C-events%2C-and-receipts-commitments) to the receipts of the transactions included in the block |
| `l1_gas_price` | `(u128, u128)` | The price of L1 gas that was used while constructing the block, where the first value is the price in wei and the second is the price in fri (see [Fees](/learn/protocol/fees/) for more details) |
| `l1_data_gas_price` | `(u128, u128)` | The price of L1 blob gas that was used while constructing the block, where the first value is the price in wei and the second is the price in fri (see [Fees](/learn/protocol/fees/) for more details) |
| `l2_gas_price` | `(u128, u128)` | The price of L2 gas that was used while constructing the block, where the first value is the price in wei and the second is the price in fri (see [Fees](/learn/protocol/fees/) for more details) |
| `l1_da_mode` | `String` | `CALLDATA` or `BLOB`, depending on how Starknet state diffs are sent to L1 |
| `protocol_version` | `String` | The version of the Starknet protocol used when creating the block |
## Block hash
A block hash is defined by:
```
h(
"STARKNET_BLOCK_HASH1",
block_number,
global_state_root,
sequencer_address,
block_timestamp,
transaction_count || event_count || state_diff_length || l1_da_mode,
state_diff_commitment,
transactions_commitment
events_commitment,
receipts_commitment,
gas_prices_hash,
protocol_version,
0,
parent_block_hash
)
```
Where:
* `h` is the [Poseidon hash function](/learn/protocol/cryptography#poseidon-hash)
* `transaction_count || event_count || state_diff_length || l1_da_mode` is the concatenation of the 64-bit representations of `transaction_count`, `event_count`, and `state_diff_length` with 0 if `l1_da_mode` is `CALLDATA` and 1 if `l1_da_mode` is `BLOB`
* `gas_prices_hash` is defined by:
```
h(
"STARKNET_GAS_PRICES0",
l1_gas_price_wei,
l1_gas_price_fri,
l1_data_gas_price_wei,
l1_data_gas_price_fri,
l2_gas_price_wei,
l2_gas_price_fri
)
```
where `h` is the [Poseidon hash function](/learn/protocol/cryptography#poseidon-hash) and `l1_gas_price_wei`, `l1_gas_price_fri`, `l1_data_gas_price_wei`, `l1_data_gas_price_fri`, `l2_gas_price_wei`, and `l2_gas_price_fri` are the wei and fri denominated prices for the three resource types.
<Tip>
For a reference implementation, see [`block_hash_calculator.rs`](https://github.com/starkware-libs/sequencer/blob/bb361ec67396660d5468fd088171913e11482708/crates/starknet_api/src/block_hash/block_hash_calculator.rs#L68) in the Starknet sequencer's GitHub repository
</Tip>
## State diff commitment
The state diff commitment is defined by:
```
h(
"STARKNET_STATE_DIFF0",
s,
deployed_or_replaced_address_1, ... , deployed_or_replaced_address_s,
d,
declared_or_migrated_class_hash_1, declared_or_migrated_compiled_class_hash_1..., declared_or_migrated_class_hash_d, declared_or_migrated_class_hash_d,
1,
0,
n,
c_1, k_1_1, v_1_1, ... k_1_m1, v_1_m1, ... ,c_n, k_n_1, v_n_1, ... ,k_n_mn, v_n_mn,
k,
account1, new_nonce1, ... ,accountk, new_noncek
)
```
Where:
* `h` is the Poseidon hash function
* `s` is the number of deployed and replaced contracts in the block
* `deployed_or_replaced_address1, ... , deployed_or_replaced_addresss` are the deployed and replaced addresses sorted in numerical increasing order according to the addresses
* For a replaced address, each value is `replaced_contract_address, new_class_hash`
* `d` is the number of declared and migrated classes in the block
* `declared_or_migrated_class_hash1, declared_or_migrated_compiled_class_hash1..., declared_or_migrated_class_hashd, declared_or_migrated_class_hashd` are the declared or replaced class hashes sorted in a numerical increasing order.
* `declared_or_migrated_class_hash` is the hash of the Sierra code
* `declared_or_migrated_compiled_class_hash` is the hash of the CASM code
* `1` and `0` are placeholders that may be used in the future
* `n` is the number of contracts whose storage was updated
* `c_i, k_i_1, v_i_1, … , k_i_mi,v_i_mi` are `mi` updates `(k_i_1, v_i_1), … ,(k_i_mi, v_i_mi)` to contract addresses `c_i`. Where `k_i_j` is the `j`-th key and `v_i_j` is the `j`-th new value
* `k` is the number of accounts whose nonce was updatedTransactions, events, and receipts commitments
## **Transactions, events, and receipts commitments**
The commitment to transactions, the commitment to events and the commitment to receipts are all roots of height-64 binary Merkle Patricia tries where the leaf at index `i` corresponds to:
* For transactions: `h(transaction_hash, signature)` for the `i`'th transaction included in the block, where [transaction\_hash is calculated as follows](/learn/cheatsheets/transactions-reference#hash-calculation) and `h` is the [Poseidon hash function](/learn/protocol/cryptography#poseidon-hash). Note that the signature is itself a (possibly empty) array of field elements.
* For events: The [event hash](/learn/protocol/blocks#event-hash) of the `i`'th emitted event included in the block
* For receipts: The [receipt hash](/learn/protocol/blocks#receipt-hash) of the `i`'th transaction receipt included in the block
### Event hash
The hash of an [event](https://book.cairo-lang.org/ch101-03-contract-events.html) emitted by a contract whose address is `emitter_address` and a transaction whose hash is `tx_hash` is defined by:
```
h(
emitter_address,
tx_hash,
h(keys),
h(data)
)
```
Where `h` is the [Poseidon hash function](/learn/protocol/cryptography#poseidon-hash).
### Receipt hash
The hash of a [transaction receipt](/learn/protocol/transactions#transaction-receipt) is defined by:
```
h(
transaction_hash,
actual_fee,
h(messages),
sn_keccak(revert_reason),
h(l2_gas_consumed, l1_gas_consumed, l1_data_gas_consumed)
)
```
Where:
* `h` is the [Poseidon hash function](/learn/protocol/cryptography#poseidon-hash)
* `h(messages)` for `messages = (from1, to1, payload1), … , (fromn, ton, payloadn)` is defined by:
```
h(
n,
from1, to1, h(len(payload1) || payload1), ... , fromn, ton, h(len(payloadn) || payloadn)
)
```
# Cryptography
Source: https://docs.starknet.io/learn/protocol/cryptography
## Overview
Starknet is built on advanced cryptographic primitives that enable scalable, trustless computation using STARK proofs. These include a custom [prime field](#the_stark_field) and [elliptic curve](#the_stark_curve) and multiple [hash functions](#hash_functions) optimized for zero-knowledge performance.
## The STARK field
The STARK field is the finite field $\mathbb{F}_{P}$, where:
$P$ = $2^{251} + 17 \cdot 2^{192} + 1$ = 3618502788666131213697322783095070105623107215331596699973092056135872020481
<Note>
The `felt252` type in Cairo refers to elements of the STARK field.
</Note>
## The STARK curve
The STARK curve is an elliptic curve defined over the STARK field by:
$y^{2} \equiv x^{3} + \alpha \cdot x + \beta \left(\right. mod P \left.\right)$
where:
* $\alpha$ = 1
* $\beta$ = 3141592653589793238462643383279502884197169399375105820974944592307816406665
The curve's order is 3618502788666131213697322783095070105526743751716087489154079457884512865583, and the generator point $G$ in the Elliptic Curve Digital Signature Algorithm (ECDSA) implementation that is used in Cairo with respect to it is defined by:
* $G_{x}$ = 874739451078007766457464989774322083649278607533249481151382481072868806602
* $G_{y}$ = 152666792071518830868575557812948353041420400780739481342941381225525861407
<Note>
The STARK curve is commonly used in smart contracts, but is not distinguished by the Starknet protocol.
</Note>
## Hash functions
There are three hash functions used throughout Starknet's specifications that map inputs to elements in the [STARK field](/learn/protocol/cryptography#the-stark-field).
### Starknet Keccak
Starknet's version of the Keccak hash function, commonly denoted by $\text{sn}_\text{keccak}$, is defined as the first 250 bits of Ethereum's [keccak256](https://github.com/ethereum/eth-hash).
### Pedersen hash
Starknet's version of the Pedersen hash function is then defined by:
$h \left(\right. a , b \left.\right) = \left(\left[\right. \text{shift}_\text{point} + a_{l o w} \cdot P_{0} + a_{h i g h} \cdot P 1 + b_{l o w} \cdot P 2 + b_{h i g h} \cdot P 3 \left]\right.\right)_{x}$
where:
* $a_{l o w}$ and $b_{l o w}$ are the 248 low of $a$ and $b$, respectively
* $a_{h i g h}$ and $b_{h i g h}$ are the 4 high bits of $a$ and $b$, respectively
* The values of the constants $\text{shift}_\text{point} , P_{0} , P_{1} , P_{2} , P_{3}$ can be found in [fast\_pedersen\_hash.py](https://github.com/starkware-libs/cairo-lang/blob/master/src/starkware/crypto/signature/fast_pedersen_hash.py)
* $\left[\right. P \left]\right._{x}$ denotes the $x$ coordinate of point $P$
#### Array hashing
Let $h$ denote the pedersen hash function, then given an array $a_{1} , . . . , a_{n}$ of $n$ field elements we define $h \left(\right. a_{1} , . . . , a_{n} \left.\right)$ to be:
$h \left(\right. . . . h \left(\right. h \left(\right. 0 , a_{1} \left.\right) , a_{2} \left.\right) , . . . , a_{n} \left.\right) , n \left.\right)$
### Poseidon hash
<Tip>
Poseidon is a family of hash functions designed to be very efficient as algebraic circuits. As such, they can be very useful in ZK-proving systems such as STARKs.
</Tip>
Starknet's version of the Poseidon hash function is based on a three-element state Hades permutation and defined of up to 2 elements by:
$\text{poseidon} \left(\right. x \left.\right) := \left(\left[\right. \text{hades}_\text{permutation} \left(\right. x , 0 , 1 \left.\right) \left]\right.\right)_{0}$
$\text{poseidon} \left(\right. x , y \left.\right) := \left(\left[\right. \text{hades}_\text{permutation} \left(\right. x , y , 2 \left.\right) \left]\right.\right)_{0}$
Where $\left[\right. \cdot \left]\right._{j}$ denotes taking the $j$'th coordinate of a tuple.
#### Array hashing
Let $\text{hades} : \mathbb{F}_{P}^{3} \rightarrow \mathbb{F}_{P}^{3}$ denote the Hades permutation with Starknet's parameters, then given an array $a_{1} , . . . , a_{n}$ of $n$ field elements we define $\text{poseidon} \left(\right. a_{1} , . . . , a_{n} \left.\right)$ to be the first coordinate of $H \left(\right. a_{1} , . . . , a_{n} ; 0 , 0 , 0 \left.\right)$, where:
$$
H(a_1, \ldots, a_n; s_1, s_2, s_3) =
\begin{cases}
H(a_3, \ldots, a_n; \text{hades}(s_1 + a_1, s_2 + a_2, s_3)) & \text{if } n \geq 2 \\
\text{hades}(s_1 + a_1, s_2 + 1, s_3) & \text{if } n = 1 \\
\text{hades}(s_1 + 1, s_2, s_3) & \text{if } n = 0
\end{cases}
$$
<Tip>
For reference implementations of the above see [poseidon\_hash.py](https://github.com/starkware-libs/cairo-lang/blob/12ca9e91bbdc8a423c63280949c7e34382792067/src/starkware/cairo/common/poseidon_hash.py#L46) and [poseidon.cairo](https://github.com/starkware-libs/cairo-lang/blob/12ca9e91bbdc8a423c63280949c7e34382792067/src/starkware/cairo/common/builtin_poseidon/poseidon.cairo#L28) in the cairo-lang GitHub repository.
</Tip>
#### Additional resources
* [Parameters for defining the Poseidon permutation used in Starknet](https://github.com/starkware-industries/poseidon/blob/main/poseidon3.txt)
* [Implementation in C and assembly by CryptoExperts](https://github.com/CryptoExperts/poseidon)
# Data availability
Source: https://docs.starknet.io/learn/protocol/data-availability
## Overview
Starknet is a Validity Rollup, which means that after consolidating and proving a set of changes to Starknet's state, it updates the latest proven state on Ethereum. Alongside the proof, it sends the [formatted](#state_diff_format) difference between the previous and new states, also known as *state diff*, allowing anyone monitoring Ethereum to use this data and reconstruct Starknet's current state.
## State diff format
### Pre-v0.11.0
Before Starknet version 0.11.0, state diffs contained information on every contract whose storage was updated, along with additional information on contract deployments. Those differences were sent as part of the calldata of the transaction that registered the proof on Ethereum, in a `uint256[]` array that included:
* The number of contracts that were deployed
* The address and class hash of each deployed contract
* The number of contracts whose storage was updated
* For each contract whose storage was updated:
* Its address
* A `uint256` value that encodes both the number of its storage updates and its updated nonce, defined as follows:
$$
\underset{\text{128 bits}}{\underbrace{0 \ldots 0}}
\mid
\underset{\text{64 bits}}{\underbrace{\text{new nonce}}}
\mid
\underset{\text{64 bits}}{\underbrace{\#\text{ of storage updates}}}_{\text{LSB}}
$$
* The key and new value for each storage update
This format is demonstrated by the following example:
```json theme={null}
[
2, (1)
2472939307328371039455977650994226407024607754063562993856224077254594995194, (2)
1336043477925910602175429627555369551262229712266217887481529642650907574765, (3)
1, (4)
2019172390095051323869047481075102003731246132997057518965927979101413600827, (5)
18446744073709551617, (6)
5, (7)
102, (8)
]
```
1. The number of deployed contracts
2. The address of the first, and only, contract whose state changed
3. The class hash of the first, and only, contract whose state changed
4. The number of contracts whose storage was updated
5. The address of the first, and only, contract whose storage was updated
6. The `uint256` value that encodes the number of storage updates and updated nonce of the first, and only, contract whose storage was updated, which equals $2^{64} + 1 = 0^{128} \left|\right. 0^{63} 1 \left|\right. 0^{63} 1$ and therefore:
* One storage cell was updated
* The updated nonce is `1`
7. The key of the first, and only, storage update of the first, and only, contract whose storage was updated
8. The new value of the first, and only, storage update of the first, and only, contract whose storage was updated
### v0.11.0
In Starknet version 0.11.0, state diffs changed to contain information on every contract whose storage was updated and additional information on contract deployments. This information was still sent as part of the calldata of the transaction that registered the proof on Ethereum, in a `uint256[]` array that included:
* The number of contracts whose storage was updated
* For contract whose storage was updated:
* Its contract address
* A single 32-byte word that includes its updated nonce and meta information about the storage updates, defined as follows:
$$
\underset{\text{127 bits}}{\underbrace{0 \ldots 0}}
\mid
\underset{\text{1 bit}}{\underbrace{\text{class information flag}}}
\mid
\underset{\text{64 bits}}{\underbrace{\text{new nonce}}}
\mid
\underset{\text{64 bits}}{\underbrace{\#\text{ of storage updates}}}_{\text{LSB}}
$$
where the class information flag's value is `1` if the contract was deployed or replaced and `0` otherwise (i.e., there were only storage updates). When set to `1`, the new class hash occupies the next entry in the array
* The key and new value for each storage update
* The number of classes that were declared
* The class hash and compiled class hash of each declared class
This format is demonstrated by the following example:
```json theme={null}
[
1, (1)
2019172390095051323869047481075102003731246132997057518965927979101413600827, (2)
18446744073709551617, (3)
100, (4)
200, (5)
1, (6)
1351148242645005540004162531550805076995747746087542030095186557536641755046, (7)
558404273560404778508455254030458021013656352466216690688595011803280448032 (8)
]
```
1. The number of contracts whose storage was updated
2. The address of the first, and only, contract whose storage changed
3. The 32-byte word that encodes the updated nonce and meta information about the storage updates of the first, and only, contract whose storage was updated, which equals $2^{64} + 1 = 0^{127} \left|\right. 0 \left|\right. 0^{63} 1 \left|\right. 0^{63} 1$ and therefore:- One storage cell was update
* The new nonce is `1`
* The class information flag is `0`, which means that the contract was not deployed or replaced and therefore the next word shouldn't be treated as the class hash
4. The key of the first, and only, storage update of the first, and only, contract whose storage was updated
5. The new value of the first and only storage update of the first and only contract whose storage was updated These two elements, `100` and `200`, encode the storage update, where the value of key `100` is set to `200`.
6. The number of classes that were declared
7. The class hash of the first, and only, declared class
8. The compiled class hash of the first, and only, declared class
### v0.13.1
In Starknet version 0.13.1, sending state diffs to Ethereum changed from using calldata to us프로젝트로 돌아가기