Skip to content

Commit 7596aa4

Browse files
apollo_starknet_os_program: address review comments
1 parent 9a8b99c commit 7596aa4

File tree

21 files changed

+108
-107
lines changed

21 files changed

+108
-107
lines changed

crates/apollo_batcher/src/commitment_manager/commitment_manager_impl.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -399,18 +399,21 @@ impl<S: StateCommitterTrait> CommitmentManager<S> {
399399
}
400400
true => {
401401
info!("Finalizing commitment for block {height} with calculating block hash.");
402-
let (parent_hash, partial_block_hash_components) =
402+
let (previous_block_hash, partial_block_hash_components) =
403403
storage_reader.get_parent_hash_and_partial_block_hash_components(height)?;
404-
let parent_hash = parent_hash.ok_or_else(|| {
404+
let previous_block_hash = previous_block_hash.ok_or_else(|| {
405405
CommitmentManagerError::MissingBlockHash(height.prev().expect(
406406
"For the genesis block, the block hash is constant and should not be \
407407
fetched from storage.",
408408
))
409409
})?;
410410
let partial_block_hash_components = partial_block_hash_components
411411
.ok_or(CommitmentManagerError::MissingPartialBlockHashComponents(height))?;
412-
let block_hash =
413-
calculate_block_hash(&partial_block_hash_components, global_root, parent_hash)?;
412+
let block_hash = calculate_block_hash(
413+
&partial_block_hash_components,
414+
global_root,
415+
previous_block_hash,
416+
)?;
414417
Ok(FinalBlockCommitment { height, block_hash: Some(block_hash), global_root })
415418
}
416419
}

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/block_context.cairo

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ func get_block_context{range_check_ptr}(os_global_context: OsGlobalContext*) ->
6666
block_info.block_timestamp, VALIDATE_TIMESTAMP_ROUNDING
6767
);
6868
tempvar block_timestamp_for_validate = divided_block_timestamp * VALIDATE_TIMESTAMP_ROUNDING;
69-
let compiled_class_facts_bundle = os_global_context.compiled_class_facts_bundle;
7069
local block_context: BlockContext = BlockContext(
7170
os_global_context=[os_global_context],
7271
block_info_for_execute=block_info,

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/block_hash.cairo

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ struct BlockHeaderCommitments {
1010
event_commitment: felt,
1111
receipt_commitment: felt,
1212
state_diff_commitment: felt,
13-
concatenated_counts: felt,
13+
// Packed encoding of transaction count, event count, state diff length, and L1 data
14+
// availability mode.
15+
packed_lengths: felt,
1416
}
1517

1618
// Calculates the block hash given the top level components.
@@ -19,25 +21,28 @@ func calculate_block_hash{poseidon_ptr: PoseidonBuiltin*}(
1921
header_commitments: BlockHeaderCommitments*,
2022
gas_prices_hash: felt,
2123
state_root: felt,
22-
parent_hash: felt,
24+
previous_block_hash: felt,
2325
starknet_version: felt,
2426
) -> felt {
27+
static_assert BlockInfo.SIZE == 3;
28+
static_assert BlockHeaderCommitments.SIZE == 5;
29+
2530
let hash_state = hash_init();
2631
with hash_state {
2732
hash_update_single(BLOCK_HASH_VERSION);
2833
hash_update_single(block_info.block_number);
2934
hash_update_single(state_root);
3035
hash_update_single(block_info.sequencer_address);
3136
hash_update_single(block_info.block_timestamp);
32-
hash_update_single(header_commitments.concatenated_counts);
37+
hash_update_single(header_commitments.packed_lengths);
3338
hash_update_single(header_commitments.state_diff_commitment);
3439
hash_update_single(header_commitments.transaction_commitment);
3540
hash_update_single(header_commitments.event_commitment);
3641
hash_update_single(header_commitments.receipt_commitment);
3742
hash_update_single(gas_prices_hash);
3843
hash_update_single(starknet_version);
3944
hash_update_single(0);
40-
hash_update_single(parent_hash);
45+
hash_update_single(previous_block_hash);
4146
}
4247

4348
let block_hash = hash_finalize(hash_state=hash_state);
@@ -51,11 +56,13 @@ func get_block_hashes{poseidon_ptr: PoseidonBuiltin*}(block_info: BlockInfo*, st
5156
previous_block_hash: felt, new_block_hash: felt
5257
) {
5358
alloc_locals;
54-
local parent_hash;
59+
local previous_block_hash;
5560
// Currently, the header commitments and gas prices are not computed by the OS.
5661
// TODO(Yoni, 1/1/2027): compute the header commitments and gas prices.
5762
local header_commitments: BlockHeaderCommitments*;
5863
local gas_prices_hash;
64+
// TODO(Yoni): move to global context, and consider enforcing a specific version for the
65+
// non-virtual OS.
5966
local starknet_version;
6067

6168
%{ GetBlockHashes %}
@@ -65,11 +72,11 @@ func get_block_hashes{poseidon_ptr: PoseidonBuiltin*}(block_info: BlockInfo*, st
6572
header_commitments=header_commitments,
6673
gas_prices_hash=gas_prices_hash,
6774
state_root=state_root,
68-
parent_hash=parent_hash,
75+
previous_block_hash=previous_block_hash,
6976
starknet_version=starknet_version,
7077
);
7178

7279
%{ CheckBlockHashConsistency %}
7380

74-
return (previous_block_hash=parent_hash, new_block_hash=block_hash);
81+
return (previous_block_hash=previous_block_hash, new_block_hash=block_hash);
7582
}

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/constants.cairo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const STORED_BLOCK_HASH_BUFFER = 10;
6666

6767
// Allowed virtual OS program hashes for client-side proving.
6868
const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_0 = (
69-
0x06972cfa5c07f702981678547574e239a24ad8bb53cc081ad738ccb10839fd1a
69+
0x0391095dffec88985e40bfa640aa05fd05ed050fcee5b79c27f492de3a68b77f
7070
);
7171
const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_LEN = 1;
7272

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/execution/execution_constraints.cairo

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from starkware.cairo.common.bool import FALSE, TRUE
44
from starkware.cairo.common.dict_access import DictAccess
5-
from starkware.cairo.common.math import assert_le, assert_not_zero
5+
from starkware.cairo.common.math import assert_le, assert_nn_le, assert_not_zero
66
from starkware.starknet.core.os.constants import (
77
ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_0,
88
ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_LEN,
@@ -53,10 +53,12 @@ func check_proof_facts{range_check_ptr, contract_state_changes: DictAccess*}(
5353
assert os_output_header.output_version = VIRTUAL_OS_OUTPUT_VERSION;
5454
}
5555

56-
// validate that the proof facts block number is not too recent
57-
// (the first check is to avoid underflow in the second one).
58-
assert_le(STORED_BLOCK_HASH_BUFFER, current_block_number);
59-
assert_le(os_output_header.base_block_number, current_block_number - STORED_BLOCK_HASH_BUFFER);
56+
// Validate that the proof facts block number is not too recent.
57+
// (This is a sanity check - the following non-zero check ensures that the block hash is
58+
// not trivial).
59+
assert_nn_le(
60+
os_output_header.base_block_number, current_block_number - STORED_BLOCK_HASH_BUFFER
61+
);
6062
// Not all block hashes are stored in the contract; Make sure the requested one is not trivial.
6163
assert_not_zero(os_output_header.base_block_hash);
6264

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/execution/syscall_impls.cairo

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@ from starkware.cairo.common.cairo_secp.signature import (
1616
)
1717
from starkware.cairo.common.dict import dict_read, dict_update
1818
from starkware.cairo.common.dict_access import DictAccess
19-
from starkware.cairo.common.math import (
20-
assert_le,
21-
assert_lt,
22-
assert_nn,
23-
assert_nn_le,
24-
unsigned_div_rem,
25-
)
19+
from starkware.cairo.common.math import assert_lt, assert_nn, assert_nn_le, unsigned_div_rem
2620
from starkware.cairo.common.memcpy import memcpy
2721
from starkware.cairo.common.secp256r1.constants import SECP_PRIME_HIGH as SECP256R1_PRIME_HIGH
2822
from starkware.cairo.common.secp256r1.constants import SECP_PRIME_LOW as SECP256R1_PRIME_LOW
@@ -691,6 +685,8 @@ func execute_storage_write{
691685
return ();
692686
}
693687

688+
// Verifies that the block hash for the given block number equals the expected block hash,
689+
// by adding a read entry to the contract_state_changes dict with it.
694690
func read_block_hash_from_storage{contract_state_changes: DictAccess*}(
695691
block_number: felt, expected_block_hash: felt
696692
) {
@@ -754,7 +750,7 @@ func execute_get_block_hash{
754750
return ();
755751
}
756752

757-
assert_le(request_block_number + STORED_BLOCK_HASH_BUFFER, current_block_number);
753+
assert_nn_le(request_block_number, current_block_number - STORED_BLOCK_HASH_BUFFER);
758754

759755
// Gas reduction has succeeded and the request is valid; write the response header.
760756
let response_header = cast(syscall_ptr, ResponseHeader*);

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/execution/transaction_impls.cairo

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,14 @@ func charge_fee{
170170
func get_account_tx_common_fields(
171171
block_context: BlockContext*, tx_hash_prefix: felt, sender_address: felt
172172
) -> CommonTxFields* {
173-
tempvar resource_bounds: ResourceBounds*;
174-
tempvar tip;
175-
tempvar paymaster_data_length;
176-
tempvar paymaster_data: felt*;
177-
tempvar nonce_data_availability_mode;
178-
tempvar fee_data_availability_mode;
179-
tempvar nonce;
173+
alloc_locals;
174+
local resource_bounds: ResourceBounds*;
175+
local tip;
176+
local paymaster_data_length;
177+
local paymaster_data: felt*;
178+
local nonce_data_availability_mode;
179+
local fee_data_availability_mode;
180+
local nonce;
180181
%{ LoadCommonTxFields %}
181182
%{ LoadTxNonceAccount %}
182183
tempvar common_tx_fields = new CommonTxFields(

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/os.cairo

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ func main{
125125
os_outputs=os_outputs,
126126
n_public_keys=n_public_keys,
127127
public_keys=public_keys,
128-
os_global_context=os_global_context,
129128
);
130129

131130
// The following code deals with the problem that untrusted code (contract code) could

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/os_utils.cairo

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ func get_block_os_output_header{poseidon_ptr: PoseidonBuiltin*}(
127127
// NOTE: both the previous block hash and previous state root are guessed, and the OS
128128
// does not verify their consistency (unlike the new hash and root).
129129
// The consumer of the OS output should verify both.
130+
// TODO(Yoni): verify the consistency of the previous block hash and state root, and remove the
131+
// state roots from the OS output header.
130132
let (prev_block_hash, new_block_hash) = get_block_hashes{poseidon_ptr=poseidon_ptr}(
131133
block_info=block_context.block_info_for_execute, state_root=state_update_output.final_root
132134
);
@@ -150,13 +152,7 @@ func get_block_os_output_header{poseidon_ptr: PoseidonBuiltin*}(
150152
// Processes OS outputs by combining blocks and serializing the result.
151153
func process_os_output{
152154
output_ptr: felt*, range_check_ptr, ec_op_ptr: EcOpBuiltin*, poseidon_ptr: PoseidonBuiltin*
153-
}(
154-
n_blocks: felt,
155-
os_outputs: OsOutput*,
156-
n_public_keys: felt,
157-
public_keys: felt*,
158-
os_global_context: OsGlobalContext*,
159-
) {
155+
}(n_blocks: felt, os_outputs: OsOutput*, n_public_keys: felt, public_keys: felt*) {
160156
alloc_locals;
161157
// Guess whether to use KZG commitment scheme and whether to output the full state.
162158
// TODO(meshi): Once use_kzg_da field is used in the OS for the computation of fees and block

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/os_utils__virtual.cairo

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,17 @@ func hash_messages_to_l1_recursive{output_ptr: felt*, poseidon_ptr: PoseidonBuil
2727

2828
// Hash the message (header + payload).
2929
// The message consists of: from_address, to_address, payload_size, ...payload.
30-
let message_size = MessageToL1Header.SIZE + payload_size;
30+
local message_size = MessageToL1Header.SIZE + payload_size;
3131
let (message_hash) = poseidon_hash_many(
3232
n=message_size, elements=cast(messages_ptr_start, felt*)
3333
);
3434

3535
// Store the hash and advance output_ptr.
36-
assert [output_ptr] = message_hash;
36+
assert output_ptr[0] = message_hash;
3737
let output_ptr = &output_ptr[1];
3838

3939
// Move to the next message.
40-
let next_message_ptr = cast(
41-
messages_ptr_start + MessageToL1Header.SIZE + payload_size, MessageToL1Header*
42-
);
40+
let next_message_ptr = cast(messages_ptr_start + message_size, MessageToL1Header*);
4341

4442
// Recursively process the remaining messages.
4543
return hash_messages_to_l1_recursive(
@@ -65,16 +63,16 @@ func get_block_os_output_header{poseidon_ptr: PoseidonBuiltin*}(
6563
state_update_output: CommitmentUpdate*,
6664
os_global_context: OsGlobalContext*,
6765
) -> OsOutputHeader* {
68-
// Calculate the block hash based on the block info and the **initial** state root.
69-
let (_prev_block_hash, block_hash) = get_block_hashes{poseidon_ptr=poseidon_ptr}(
66+
// Calculate the previous block hash based on the block info and the **initial** state root.
67+
let (_prev_prev_block_hash, prev_block_hash) = get_block_hashes{poseidon_ptr=poseidon_ptr}(
7068
block_info=block_context.block_info_for_execute, state_root=state_update_output.initial_root
7169
);
7270

7371
tempvar os_output_header = new OsOutputHeader(
7472
state_update_output=state_update_output,
7573
prev_block_number=block_context.block_info_for_execute.block_number,
7674
new_block_number=0,
77-
prev_block_hash=block_hash,
75+
prev_block_hash=prev_block_hash,
7876
new_block_hash=0,
7977
os_program_hash=0,
8078
starknet_os_config_hash=os_global_context.starknet_os_config_hash,
@@ -88,13 +86,7 @@ func get_block_os_output_header{poseidon_ptr: PoseidonBuiltin*}(
8886
// Outputs the virtual OS header and the messages to L1.
8987
func process_os_output{
9088
output_ptr: felt*, range_check_ptr, ec_op_ptr: EcOpBuiltin*, poseidon_ptr: PoseidonBuiltin*
91-
}(
92-
n_blocks: felt,
93-
os_outputs: OsOutput*,
94-
n_public_keys: felt,
95-
public_keys: felt*,
96-
os_global_context: OsGlobalContext*,
97-
) {
89+
}(n_blocks: felt, os_outputs: OsOutput*, n_public_keys: felt, public_keys: felt*) {
9890
alloc_locals;
9991
assert n_public_keys = 0;
10092

@@ -123,7 +115,7 @@ func process_os_output{
123115
output_version=VIRTUAL_OS_OUTPUT_VERSION,
124116
base_block_number=header.prev_block_number,
125117
base_block_hash=header.prev_block_hash,
126-
starknet_os_config_hash=os_global_context.starknet_os_config_hash,
118+
starknet_os_config_hash=header.starknet_os_config_hash,
127119
n_messages_to_l1=n_messages_to_l1,
128120
);
129121

0 commit comments

Comments
 (0)