Skip to content

chore(starknet_os): fix hints of simplified compiled class hash #5014

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

Merged
merged 1 commit into from
Mar 18, 2025
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
59 changes: 23 additions & 36 deletions crates/starknet_os/src/hints/enum_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,8 @@ define_hint_enum!(
bytecode_segment_structure,
indoc! {r#"
vm_enter_scope({
"bytecode_segment_structure": bytecode_segment_structures[ids.compiled_class_fact.hash]
"bytecode_segment_structure": bytecode_segment_structures[ids.compiled_class_fact.hash],
"is_segment_used_callback": is_segment_used_callback
})"#}
),
(
Expand Down Expand Up @@ -521,14 +522,22 @@ define_hint_enum!(
assert next(bytecode_segments, None) is None"#
}
),
(DeleteMemoryData, delete_memory_data, "del memory.data[ids.data_ptr]"),
(
DeleteMemoryData,
delete_memory_data,
indoc! {r#"
# Sanity check.
assert not is_accessed(ids.data_ptr), "The segment is skipped but was accessed."
del memory.data[ids.data_ptr]"#
}
),
(
IterCurrentSegmentInfo,
iter_current_segment_info,
indoc! {r#"
current_segment_info = next(bytecode_segments)

is_used = current_segment_info.is_used
is_used = is_segment_used_callback(ids.data_ptr, current_segment_info.segment_length)
ids.is_segment_used = 1 if is_used else 0

is_used_leaf = is_used and isinstance(current_segment_info.inner_structure, BytecodeLeaf)
Expand All @@ -537,6 +546,7 @@ define_hint_enum!(
ids.segment_length = current_segment_info.segment_length
vm_enter_scope(new_scope_locals={
"bytecode_segment_structure": current_segment_info.inner_structure,
"is_segment_used_callback": is_segment_used_callback
})"#
}
),
Expand All @@ -551,45 +561,22 @@ define_hint_enum!(
ValidateCompiledClassFactsPostExecution,
validate_compiled_class_facts_post_execution,
indoc! {r#"
from starkware.cairo.lang.vm.relocatable import RelocatableValue

bytecode_segment_to_length = {}
compiled_hash_to_bytecode_segment = {}
for i in range(ids.n_compiled_class_facts):
fact = ids.compiled_class_facts[i]
bytecode_segment = fact.compiled_class.bytecode_ptr.segment_index
bytecode_segment_to_length[bytecode_segment] = fact.compiled_class.bytecode_length
compiled_hash_to_bytecode_segment[fact.hash] = bytecode_segment

bytecode_segment_to_visited_pcs = {
bytecode_segment: [] for bytecode_segment in bytecode_segment_to_length
}
for addr in iter_accessed_addresses():
if (
isinstance(addr, RelocatableValue)
and addr.segment_index in bytecode_segment_to_visited_pcs
):
bytecode_segment_to_visited_pcs[addr.segment_index].append(addr.offset)

# Sort and remove the program extra data, which is not part of the hash.
for bytecode_segment, visited_pcs in bytecode_segment_to_visited_pcs.items():
visited_pcs.sort()
while (
len(visited_pcs) > 0
and visited_pcs[-1] >= bytecode_segment_to_length[bytecode_segment]
):
visited_pcs.pop()
from starkware.starknet.core.os.contract_class.compiled_class_hash import (
BytecodeAccessOracle,
)

# Build the bytecode segment structures based on the execution info.
# Build the bytecode segment structures.
bytecode_segment_structures = {
compiled_hash: create_bytecode_segment_structure(
bytecode=compiled_class.bytecode,
bytecode_segment_lengths=compiled_class.bytecode_segment_lengths,
visited_pcs=bytecode_segment_to_visited_pcs[
compiled_hash_to_bytecode_segment[compiled_hash]
],
) for compiled_hash, compiled_class in os_input.compiled_classes.items()
}"#}
}
bytecode_segment_access_oracle = BytecodeAccessOracle(is_pc_accessed_callback=is_accessed)
vm_enter_scope({
"bytecode_segment_structures": bytecode_segment_structures,
"is_segment_used_callback": bytecode_segment_access_oracle.is_segment_used
})"#}
),
(
EnterScopeWithAliases,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub(crate) fn bytecode_segment_structure<S: StateReader>(
Scope::BytecodeSegmentStructure.into(),
any_box!(bytecode_segment_structure.clone()),
)]);
// TODO(Nimrod): support is_segment_used_callback.
exec_scopes.enter_scope(new_scope);

Ok(())
Expand Down
Loading