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

feat(starknet_os): fix enter_syscall hints #4100

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 39 additions & 37 deletions crates/starknet_os/src/hints/enum_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::hints::hint_implementation::block_context::{
get_block_mapping,
is_leaf,
load_class,
load_class_facts,
load_class_inner,
sequencer_address,
write_use_kzg_da_to_memory,
Expand All @@ -25,6 +24,7 @@ use crate::hints::hint_implementation::builtins::{
use crate::hints::hint_implementation::compiled_class::{
assert_end_of_bytecode_segments,
assign_bytecode_segments,
delete_memory_data,
iter_current_segment_info,
set_ap_to_segment_hash,
};
Expand Down Expand Up @@ -298,17 +298,6 @@ define_hint_enum!(
r#"memory[ap] = to_felt_or_relocatable(ids.request_block_number > \
ids.current_block_number - ids.STORED_BLOCK_HASH_BUFFER)"#
),
(
LoadClassFacts,
load_class_facts,
indoc! {r#"
ids.compiled_class_facts = segments.add()
ids.n_compiled_class_facts = len(os_input.compiled_classes)
vm_enter_scope({
'compiled_class_facts': iter(os_input.compiled_classes.items()),
'compiled_class_visited_pcs': os_input.compiled_class_visited_pcs,
})"#}
),
(
LoadClassInner,
load_class_inner,
Expand All @@ -318,27 +307,42 @@ define_hint_enum!(
get_compiled_class_struct,
)

compiled_class_hash, compiled_class = next(compiled_class_facts)
ids.n_compiled_class_facts = len(os_input.compiled_classes)
ids.compiled_class_facts = (compiled_class_facts_end := segments.add())
for i, (compiled_class_hash, compiled_class) in enumerate(
os_input.compiled_classes.items()
):
# Load the compiled class.
cairo_contract = get_compiled_class_struct(
identifiers=ids._context.identifiers,
compiled_class=compiled_class,
# Load the entire bytecode - the unaccessed segments will be overriden and skipped
# after the execution, in `validate_compiled_class_facts_post_execution`.
bytecode=compiled_class.bytecode,
)
segments.load_data(
ptr=ids.compiled_class_facts[i].address_,
data=(compiled_class_hash, segments.gen_arg(cairo_contract))
)

bytecode_segment_structure = create_bytecode_segment_structure(
bytecode=compiled_class.bytecode,
bytecode_segment_lengths=compiled_class.bytecode_segment_lengths,
visited_pcs=compiled_class_visited_pcs[compiled_class_hash],
)
bytecode_ptr = ids.compiled_class_facts[i].compiled_class.bytecode_ptr
# Compiled classes are expected to end with a `ret` opcode followed by a pointer to
# the builtin costs.
segments.load_data(
ptr=bytecode_ptr + cairo_contract.bytecode_length,
data=[0x208b7fff7fff7ffe, ids.builtin_costs]
)

cairo_contract = get_compiled_class_struct(
identifiers=ids._context.identifiers,
compiled_class=compiled_class,
bytecode=bytecode_segment_structure.bytecode_with_skipped_segments()
)
ids.compiled_class = segments.gen_arg(cairo_contract)"#}
# Load hints and debug info.
vm_load_program(
compiled_class.get_runnable_program(entrypoint_builtins=[]), bytecode_ptr)"#}
),
(
BytecodeSegmentStructure,
bytecode_segment_structure,
indoc! {r#"
vm_enter_scope({
"bytecode_segment_structure": bytecode_segment_structure
"bytecode_segment_structure": bytecode_segment_structures[ids.compiled_class_fact.hash]
})"#}
),
(
Expand Down Expand Up @@ -455,6 +459,7 @@ define_hint_enum!(
assert next(bytecode_segments, None) is None"#
}
),
(DeleteMemoryData, delete_memory_data, "del memory.data[ids.data_ptr]"),
(
IterCurrentSegmentInfo,
iter_current_segment_info,
Expand Down Expand Up @@ -965,11 +970,11 @@ segments.write_arg(ids.sha256_ptr_end, padding)"#}
)

# Prepare a short callable to save code duplication.
exit_syscall = lambda selector: execution_helper.os_logger.exit_syscall(
exit_syscall = lambda: execution_helper.os_logger.exit_syscall(
n_steps=current_step,
builtin_ptrs=ids.builtin_ptrs,
range_check_ptr=ids.range_check_ptr,
selector=selector,
selector=ids.selector,
)"#
}
),
Expand Down Expand Up @@ -1483,11 +1488,11 @@ memory[ap] = 1 if case != 'both' else 0"#
)

# Prepare a short callable to save code duplication.
exit_syscall = lambda selector: execution_helper.os_logger.exit_syscall(
exit_syscall = lambda: execution_helper.os_logger.exit_syscall(
n_steps=current_step,
builtin_ptrs=ids.builtin_ptrs,
range_check_ptr=ids.range_check_ptr,
selector=selector,
selector=ids.selector,
)"#
}
),
Expand Down Expand Up @@ -1545,16 +1550,13 @@ define_hint_extension_enum!(
LoadClass,
load_class,
indoc! {r#"
computed_hash = ids.compiled_class_fact.hash
expected_hash = compiled_class_hash
vm_exit_scope()

computed_hash = ids.hash
expected_hash = ids.compiled_class_fact.hash
assert computed_hash == expected_hash, (
"Computed compiled_class_hash is inconsistent with the hash in the os_input. "
f"Computed hash = {computed_hash}, Expected hash = {expected_hash}.")

vm_load_program(
compiled_class.get_runnable_program(entrypoint_builtins=[]),
ids.compiled_class.bytecode_ptr
)"#
f"Computed hash = {computed_hash}, Expected hash = {expected_hash}.")"#
}
),
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ use crate::hints::types::{HintArgs, HintExtensionArgs};

// Hint implementations.

pub fn load_class_facts(HintArgs { .. }: HintArgs<'_, '_, '_, '_, '_>) -> HintResult {
todo!()
}

pub fn load_class_inner(HintArgs { .. }: HintArgs<'_, '_, '_, '_, '_>) -> HintResult {
todo!()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ pub fn assert_end_of_bytecode_segments(
todo!()
}

pub fn delete_memory_data(HintArgs { .. }: HintArgs<'_, '_, '_, '_, '_>) -> HintResult {
// TODO(Yoni): Assert that the address was not accessed before.
todo!()
}

pub fn iter_current_segment_info(HintArgs { .. }: HintArgs<'_, '_, '_, '_, '_>) -> HintResult {
todo!()
}
Expand Down
Loading