Skip to content

Commit ef078a0

Browse files
apollo_starknet_os_program: define and use OsGlobalContext (#10666)
1 parent d429c34 commit ef078a0

File tree

6 files changed

+105
-84
lines changed

6 files changed

+105
-84
lines changed

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ from starkware.starknet.core.os.os_config.os_config import StarknetOsConfig
1717
struct CompiledClassFactsBundle {
1818
n_compiled_class_facts: felt,
1919
compiled_class_facts: CompiledClassFact*,
20+
builtin_costs: felt*,
2021
n_deprecated_compiled_class_facts: felt,
2122
deprecated_compiled_class_facts: DeprecatedCompiledClassFact*,
2223
}
@@ -50,17 +51,30 @@ struct BlockContext {
5051
execute_deprecated_syscalls_ptr: felt*,
5152
}
5253

53-
// Returns a BlockContext instance.
54-
//
55-
// 'syscall_handler' and 'block_input' should be passed as hint variables.
56-
func get_block_context{range_check_ptr}(
54+
// Holds global context for the OS execution.
55+
struct OsGlobalContext {
56+
// OS config available globally for all blocks.
57+
starknet_os_config: StarknetOsConfig*,
58+
starknet_os_config_hash: felt,
59+
60+
// Compiled class facts available globally for all blocks.
61+
compiled_class_facts_bundle: CompiledClassFactsBundle*,
62+
63+
// Parameters for select_builtins.
64+
builtin_params: BuiltinParams*,
65+
// A function pointer to the 'execute_syscalls' function.
5766
execute_syscalls_ptr: felt*,
67+
// A function pointer to the 'execute_deprecated_syscalls' function.
5868
execute_deprecated_syscalls_ptr: felt*,
59-
compiled_class_facts_bundle: CompiledClassFactsBundle*,
60-
starknet_os_config: StarknetOsConfig*,
61-
) -> (block_context: BlockContext*) {
69+
}
70+
71+
// Returns a BlockContext instance.
72+
//
73+
// 'syscall_handler' should be passed as a hint variable.
74+
func get_block_context{range_check_ptr}(os_global_context: OsGlobalContext*) -> (
75+
block_context: BlockContext*
76+
) {
6277
alloc_locals;
63-
let (builtin_params) = get_builtin_params();
6478
tempvar block_number = nondet %{ syscall_handler.block_info.block_number %};
6579
tempvar block_timestamp = nondet %{ syscall_handler.block_info.block_timestamp %};
6680
let (divided_block_number, _) = unsigned_div_rem(block_number, VALIDATE_BLOCK_NUMBER_ROUNDING);
@@ -69,8 +83,9 @@ func get_block_context{range_check_ptr}(
6983
block_timestamp, VALIDATE_TIMESTAMP_ROUNDING
7084
);
7185
tempvar block_timestamp_for_validate = divided_block_timestamp * VALIDATE_TIMESTAMP_ROUNDING;
86+
let compiled_class_facts_bundle = os_global_context.compiled_class_facts_bundle;
7287
local block_context: BlockContext = BlockContext(
73-
builtin_params=builtin_params,
88+
builtin_params=os_global_context.builtin_params,
7489
n_compiled_class_facts=compiled_class_facts_bundle.n_compiled_class_facts,
7590
compiled_class_facts=compiled_class_facts_bundle.compiled_class_facts,
7691
n_deprecated_compiled_class_facts=compiled_class_facts_bundle.n_deprecated_compiled_class_facts,
@@ -85,9 +100,9 @@ func get_block_context{range_check_ptr}(
85100
block_timestamp=block_timestamp_for_validate,
86101
sequencer_address=0,
87102
),
88-
starknet_os_config=[starknet_os_config],
89-
execute_syscalls_ptr=execute_syscalls_ptr,
90-
execute_deprecated_syscalls_ptr=execute_deprecated_syscalls_ptr,
103+
starknet_os_config=[os_global_context.starknet_os_config],
104+
execute_syscalls_ptr=os_global_context.execute_syscalls_ptr,
105+
execute_deprecated_syscalls_ptr=os_global_context.execute_deprecated_syscalls_ptr,
91106
);
92107

93108
let (__fp__, _) = get_fp_and_pc();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ struct BuiltinParams {
7272
static_assert SelectableBuiltins.SIZE == BuiltinEncodings.SIZE;
7373
static_assert SelectableBuiltins.SIZE == BuiltinInstanceSizes.SIZE;
7474

75-
func get_builtin_params() -> (builtin_params: BuiltinParams*) {
75+
func get_builtin_params() -> BuiltinParams* {
7676
alloc_locals;
7777
let (local __fp__, _) = get_fp_and_pc();
7878
@@ -105,7 +105,7 @@ func get_builtin_params() -> (builtin_params: BuiltinParams*) {
105105
local builtin_params: BuiltinParams = BuiltinParams(
106106
builtin_encodings=&builtin_encodings, builtin_instance_sizes=&builtin_instance_sizes
107107
);
108-
return (builtin_params=&builtin_params);
108+
return &builtin_params;
109109
}
110110

111111
// Updates the builtins listed in `selected_encodings` (the "selected" builtins) in `builtin_ptrs`

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

Lines changed: 67 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ from starkware.starknet.core.aggregator.combine_blocks import combine_blocks
2222
from starkware.starknet.core.os.block_context import (
2323
BlockContext,
2424
CompiledClassFactsBundle,
25+
OsGlobalContext,
2526
get_block_context,
2627
)
28+
from starkware.starknet.core.os.builtins import get_builtin_params
2729
from starkware.starknet.core.os.constants import (
2830
BLOCK_HASH_CONTRACT_ADDRESS,
2931
STORED_BLOCK_HASH_BUFFER,
@@ -94,56 +96,38 @@ func main{
9496
let initial_range_check_ptr = range_check_ptr;
9597
let range_check_ptr = range_check_ptr + 1;
9698

97-
local n_blocks = nondet %{ len(os_input.block_inputs) %};
98-
99-
let (
100-
local n_compiled_class_facts, local compiled_class_facts, local builtin_costs
101-
) = guess_compiled_class_facts();
99+
local public_keys: felt*;
100+
local n_public_keys: felt;
101+
%{ fill_public_keys_array(os_hints['public_keys'], public_keys, n_public_keys) %}
102102

103-
let (
104-
local n_deprecated_compiled_class_facts, deprecated_compiled_class_facts
105-
) = deprecated_load_compiled_class_facts();
103+
// Build OS global context.
104+
let os_global_context = get_os_global_context(
105+
n_public_keys=n_public_keys, public_keys=public_keys
106+
);
106107

108+
// Execute blocks.
109+
local n_blocks = nondet %{ len(os_input.block_inputs) %};
107110
let (local os_outputs: OsOutput*) = alloc();
108111
%{
109112
from starkware.starknet.core.os.execution_helper import StateUpdatePointers
110113
state_update_pointers = StateUpdatePointers(segments=segments)
111114
%}
112-
local compiled_class_facts_bundle: CompiledClassFactsBundle* = new CompiledClassFactsBundle(
113-
n_compiled_class_facts=n_compiled_class_facts,
114-
compiled_class_facts=compiled_class_facts,
115-
n_deprecated_compiled_class_facts=n_deprecated_compiled_class_facts,
116-
deprecated_compiled_class_facts=deprecated_compiled_class_facts,
117-
);
118-
119-
local public_keys: felt*;
120-
local n_public_keys: felt;
121-
%{ fill_public_keys_array(os_hints['public_keys'], public_keys, n_public_keys) %}
122-
let (public_keys_hash) = get_public_keys_hash{hash_ptr=pedersen_ptr}(
123-
n_public_keys=n_public_keys, public_keys=public_keys
124-
);
125-
local starknet_os_config: StarknetOsConfig* = new StarknetOsConfig(
126-
chain_id=nondet %{ os_hints_config.starknet_os_config.chain_id %},
127-
fee_token_address=nondet %{ os_hints_config.starknet_os_config.fee_token_address %},
128-
public_keys_hash=public_keys_hash,
129-
);
130-
131115
local initial_txs_range_check_ptr = nondet %{ segments.add_temp_segment() %};
132116
let txs_range_check_ptr = initial_txs_range_check_ptr;
133117
with txs_range_check_ptr {
134118
execute_blocks(
135119
n_blocks=n_blocks,
136120
os_output_per_block_dst=os_outputs,
137-
compiled_class_facts_bundle=compiled_class_facts_bundle,
138-
starknet_os_config=starknet_os_config,
121+
os_global_context=os_global_context,
139122
);
140123
}
141124

142125
// Validate the guessed compile class facts.
126+
let compiled_class_facts_bundle = os_global_context.compiled_class_facts_bundle;
143127
validate_compiled_class_facts_post_execution(
144128
n_compiled_class_facts=compiled_class_facts_bundle.n_compiled_class_facts,
145129
compiled_class_facts=compiled_class_facts_bundle.compiled_class_facts,
146-
builtin_costs=builtin_costs,
130+
builtin_costs=compiled_class_facts_bundle.builtin_costs,
147131
);
148132

149133
// Guess whether to use KZG commitment scheme and whether to output the full state.
@@ -245,12 +229,7 @@ func execute_blocks{
245229
add_mod_ptr: ModBuiltin*,
246230
mul_mod_ptr: ModBuiltin*,
247231
txs_range_check_ptr,
248-
}(
249-
n_blocks: felt,
250-
os_output_per_block_dst: OsOutput*,
251-
compiled_class_facts_bundle: CompiledClassFactsBundle*,
252-
starknet_os_config: StarknetOsConfig*,
253-
) {
232+
}(n_blocks: felt, os_output_per_block_dst: OsOutput*, os_global_context: OsGlobalContext*) {
254233
%{ print(f"execute_blocks: {ids.n_blocks} blocks remaining.") %}
255234
if (n_blocks == 0) {
256235
return ();
@@ -284,17 +263,7 @@ func execute_blocks{
284263
let contract_class_changes_start = contract_class_changes;
285264

286265
// Build block context.
287-
let (execute_syscalls_ptr) = get_label_location(label_value=execute_syscalls);
288-
let (execute_deprecated_syscalls_ptr) = get_label_location(
289-
label_value=execute_deprecated_syscalls
290-
);
291-
292-
let (block_context: BlockContext*) = get_block_context(
293-
execute_syscalls_ptr=execute_syscalls_ptr,
294-
execute_deprecated_syscalls_ptr=execute_deprecated_syscalls_ptr,
295-
compiled_class_facts_bundle=compiled_class_facts_bundle,
296-
starknet_os_config=starknet_os_config,
297-
);
266+
let (block_context: BlockContext*) = get_block_context(os_global_context=os_global_context);
298267

299268
// Pre-process block.
300269
with contract_state_changes {
@@ -342,17 +311,6 @@ func execute_blocks{
342311

343312
%{ vm_exit_scope() %}
344313

345-
// Compute the general config hash.
346-
// This is done here to avoid passing pedersen_ptr to serialize_output_header.
347-
let hash_ptr = pedersen_ptr;
348-
with hash_ptr {
349-
let (starknet_os_config_hash) = get_starknet_os_config_hash(
350-
starknet_os_config=&block_context.starknet_os_config
351-
);
352-
}
353-
354-
let pedersen_ptr = hash_ptr;
355-
356314
// All blocks inside of a multi block should be off-chain and therefore
357315
// should not be compressed.
358316
assert os_output_per_block_dst[0] = OsOutput(
@@ -363,7 +321,7 @@ func execute_blocks{
363321
prev_block_hash=nondet %{ block_input.prev_block_hash %},
364322
new_block_hash=nondet %{ block_input.new_block_hash %},
365323
os_program_hash=0,
366-
starknet_os_config_hash=starknet_os_config_hash,
324+
starknet_os_config_hash=os_global_context.starknet_os_config_hash,
367325
use_kzg_da=FALSE,
368326
full_output=TRUE,
369327
),
@@ -375,8 +333,7 @@ func execute_blocks{
375333
return execute_blocks(
376334
n_blocks=n_blocks - 1,
377335
os_output_per_block_dst=&os_output_per_block_dst[1],
378-
compiled_class_facts_bundle=compiled_class_facts_bundle,
379-
starknet_os_config=starknet_os_config,
336+
os_global_context=os_global_context,
380337
);
381338
}
382339

@@ -499,3 +456,52 @@ func migrate_classes_to_v2_casm_hash{
499456
migrate_classes_to_v2_casm_hash(n_classes=n_classes - 1, block_context=block_context);
500457
return ();
501458
}
459+
460+
// Returns an OsGlobalContext instance.
461+
//
462+
// Note: the compiled class facts are guessed here, and must be validated post-execution.
463+
func get_os_global_context{
464+
pedersen_ptr: HashBuiltin*, range_check_ptr, poseidon_ptr: PoseidonBuiltin*
465+
}(n_public_keys: felt, public_keys: felt*) -> OsGlobalContext* {
466+
alloc_locals;
467+
// Compiled class facts bundle.
468+
let (n_compiled_class_facts, compiled_class_facts, builtin_costs) = guess_compiled_class_facts(
469+
);
470+
let (
471+
n_deprecated_compiled_class_facts, deprecated_compiled_class_facts
472+
) = deprecated_load_compiled_class_facts();
473+
474+
// Starknet OS config.
475+
let (public_keys_hash) = get_public_keys_hash{hash_ptr=pedersen_ptr}(
476+
n_public_keys=n_public_keys, public_keys=public_keys
477+
);
478+
tempvar starknet_os_config = new StarknetOsConfig(
479+
chain_id=nondet %{ os_hints_config.starknet_os_config.chain_id %},
480+
fee_token_address=nondet %{ os_hints_config.starknet_os_config.fee_token_address %},
481+
public_keys_hash=public_keys_hash,
482+
);
483+
let (starknet_os_config_hash) = get_starknet_os_config_hash{hash_ptr=pedersen_ptr}(
484+
starknet_os_config=starknet_os_config
485+
);
486+
487+
// Function pointers.
488+
let (execute_syscalls_ptr) = get_label_location(label_value=execute_syscalls);
489+
let (execute_deprecated_syscalls_ptr) = get_label_location(
490+
label_value=execute_deprecated_syscalls
491+
);
492+
tempvar os_global_context: OsGlobalContext* = new OsGlobalContext(
493+
starknet_os_config=starknet_os_config,
494+
starknet_os_config_hash=starknet_os_config_hash,
495+
compiled_class_facts_bundle=new CompiledClassFactsBundle(
496+
n_compiled_class_facts=n_compiled_class_facts,
497+
compiled_class_facts=compiled_class_facts,
498+
builtin_costs=builtin_costs,
499+
n_deprecated_compiled_class_facts=n_deprecated_compiled_class_facts,
500+
deprecated_compiled_class_facts=deprecated_compiled_class_facts,
501+
),
502+
builtin_params=get_builtin_params(),
503+
execute_syscalls_ptr=execute_syscalls_ptr,
504+
execute_deprecated_syscalls_ptr=execute_deprecated_syscalls_ptr,
505+
);
506+
return os_global_context;
507+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"os": "0x1f116bf592157589d5d2c79176d708a6340861db822635032840804250a557",
2+
"os": "0x4d27b51144962502babd7b1f2e5e4f78363f96044874fdd4c75d5064cb4e8c2",
33
"aggregator": "0x267b62790ec5ca1b3f8889835a87808086a2c385abc62e562c3a7910a37d313",
44
"aggregator_with_prefix": "0x1780e4ffea136af2dd609b5399a7d73dd42432fd00ea9c2da146ac5269efb70"
55
}

crates/starknet_os/src/hints/enum_definition.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ vm_enter_scope(dict(
600600
(
601601
SegmentsAddTemp,
602602
segments_add_temp,
603-
indoc! {r#"memory[fp + 12] = to_felt_or_relocatable(segments.add_temp_segment())"#
603+
indoc! {r#"memory[fp + 5] = to_felt_or_relocatable(segments.add_temp_segment())"#
604604
}
605605
),
606606
(
@@ -1114,7 +1114,7 @@ define_hint_enum!(
11141114
(
11151115
WriteUseKzgDaToMemory,
11161116
write_use_kzg_da_to_memory,
1117-
indoc! {r#"memory[fp + 24] = to_felt_or_relocatable(os_hints_config.use_kzg_da and (
1117+
indoc! {r#"memory[fp + 17] = to_felt_or_relocatable(os_hints_config.use_kzg_da and (
11181118
not os_hints_config.full_output
11191119
))"#}
11201120
),
@@ -1607,7 +1607,7 @@ ids.contract_class_component_hashes = segments.gen_arg(class_component_hashes)"#
16071607
(
16081608
GetBlocksNumber,
16091609
get_n_blocks,
1610-
r#"memory[fp + 0] = to_felt_or_relocatable(len(os_input.block_inputs))"#
1610+
r#"memory[fp + 3] = to_felt_or_relocatable(len(os_input.block_inputs))"#
16111611
),
16121612
(
16131613
GetNClassHashesToMigrate,
@@ -1617,7 +1617,7 @@ ids.contract_class_component_hashes = segments.gen_arg(class_component_hashes)"#
16171617
(
16181618
WriteFullOutputToMemory,
16191619
write_full_output_to_memory,
1620-
indoc! {r#"memory[fp + 25] = to_felt_or_relocatable(os_hints_config.full_output)"#}
1620+
indoc! {r#"memory[fp + 18] = to_felt_or_relocatable(os_hints_config.full_output)"#}
16211621
),
16221622
(
16231623
ConfigureKzgManager,

crates/starknet_os/src/hints/nondet_offsets.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ pub(crate) static NONDET_FP_OFFSETS: LazyLock<HashMap<AllHints, usize>> = LazyLo
2121
HashMap::from([
2222
(AllHints::OsHint(OsHint::OsInputTransactions), 12),
2323
(AllHints::OsHint(OsHint::ReadAliasFromKey), 0),
24-
(AllHints::StatelessHint(StatelessHint::SegmentsAddTemp), 12),
24+
(AllHints::StatelessHint(StatelessHint::SegmentsAddTemp), 5),
2525
(AllHints::OsHint(OsHint::SetFpPlus4ToTxNonce), 4),
26-
(AllHints::OsHint(OsHint::GetBlocksNumber), 0),
26+
(AllHints::OsHint(OsHint::GetBlocksNumber), 3),
2727
(AllHints::OsHint(OsHint::TxAccountDeploymentDataLen), 4),
28-
(AllHints::OsHint(OsHint::WriteFullOutputToMemory), 25),
29-
(AllHints::OsHint(OsHint::WriteUseKzgDaToMemory), 24),
28+
(AllHints::OsHint(OsHint::WriteFullOutputToMemory), 18),
29+
(AllHints::OsHint(OsHint::WriteUseKzgDaToMemory), 17),
3030
])
3131
});
3232

0 commit comments

Comments
 (0)