Skip to content

Commit

Permalink
fix bootloader identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
odesenfans committed Jun 5, 2024
1 parent 1481c3c commit ef1e51f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 26 deletions.
2 changes: 2 additions & 0 deletions src/hints/bootloader_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::hints::types::{BootloaderInput, CompositePackedOutput, PackedOutput};
use crate::hints::vars;

/// Implements
/// ```no-run
/// %{
/// from starkware.cairo.bootloaders.bootloader.objects import BootloaderInput
/// bootloader_input = BootloaderInput.Schema().load(program_input)
Expand All @@ -33,6 +34,7 @@ use crate::hints::vars;
/// output_builtin_state = output_builtin.get_state()
/// output_builtin.new_state(base=ids.simple_bootloader_output_start)
/// %}
/// ```
pub fn prepare_simple_bootloader_output_segment(
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
Expand Down
35 changes: 13 additions & 22 deletions src/hints/execute_task_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,13 @@ pub fn write_return_builtins_hint(
Ok(())
}

fn get_bootloader_program(exec_scopes: &ExecutionScopes) -> Result<&ProgramIdentifiers, HintError> {
if let Some(boxed_program) = exec_scopes.data[0].get(vars::BOOTLOADER_PROGRAM_IDENTIFIERS) {
if let Some(program) = boxed_program.downcast_ref::<ProgramIdentifiers>() {
fn get_bootloader_identifiers(
exec_scopes: &ExecutionScopes,
) -> Result<&ProgramIdentifiers, HintError> {
if let Some(bootloader_identifiers) =
exec_scopes.data[0].get(vars::BOOTLOADER_PROGRAM_IDENTIFIERS)
{
if let Some(program) = bootloader_identifiers.downcast_ref::<ProgramIdentifiers>() {
return Ok(program);
}
}
Expand Down Expand Up @@ -413,7 +417,7 @@ pub fn call_task(
let program_address: Relocatable = exec_scopes.get("program_address")?;

// ret_pc = ids.ret_pc_label.instruction_offset_ - ids.call_task.instruction_offset_ + pc
let bootloader_identifiers = get_bootloader_program(exec_scopes)?;
let bootloader_identifiers = get_bootloader_identifiers(exec_scopes)?;
let ret_pc_label = get_identifier(bootloader_identifiers, "starkware.cairo.bootloaders.simple_bootloader.execute_task.execute_task.ret_pc_label")?;
let call_task = get_identifier(
bootloader_identifiers,
Expand Down Expand Up @@ -489,7 +493,6 @@ mod tests {

use cairo_vm::any_box;
use cairo_vm::hint_processor::hint_processor_definition::HintProcessorLogic;
use cairo_vm::serde::deserialize_program::ReferenceManager;
use cairo_vm::types::errors::math_errors::MathError;
use cairo_vm::types::program::Program;
use cairo_vm::types::relocatable::MaybeRelocatable;
Expand Down Expand Up @@ -649,7 +652,7 @@ mod tests {
/// * a `HasIdentifiers` trait cannot be used as exec_scopes requires to cast to `Box<dyn Any>`,
/// making casting back to the trait impossible.
/// * using an enum requires defining test-only variants.
fn mock_program_with_identifiers(symbols: HashMap<String, usize>) -> Program {
fn mock_program_identifiers(symbols: HashMap<String, usize>) -> ProgramIdentifiers {
let identifiers = symbols
.into_iter()
.map(|(name, pc)| {
Expand All @@ -667,19 +670,7 @@ mod tests {
})
.collect();

let program = Program::new(
vec![],
Default::default(),
Default::default(),
Default::default(),
ReferenceManager::default(),
identifiers,
Default::default(),
Default::default(),
)
.unwrap();

program
identifiers
}

#[rstest]
Expand All @@ -693,7 +684,7 @@ mod tests {
// memory address and increase the AP register accordingly.
define_segments!(
vm,
3,
4,
[((1, 0), (2, 0)), ((1, 1), (4, 0)), ((1, 9), (4, 42))]
);
vm.set_ap(10);
Expand Down Expand Up @@ -721,9 +712,9 @@ mod tests {
("starkware.cairo.bootloaders.simple_bootloader.execute_task.execute_task.call_task".to_string(), 8usize)
]
);
let bootloader_program = mock_program_with_identifiers(bootloader_identifiers);
let program_identifiers = mock_program_identifiers(bootloader_identifiers);
exec_scopes.insert_value(vars::PROGRAM_DATA_BASE, program_header_ptr.clone());
exec_scopes.insert_value(vars::BOOTLOADER_PROGRAM_IDENTIFIERS, bootloader_program);
exec_scopes.insert_value(vars::BOOTLOADER_PROGRAM_IDENTIFIERS, program_identifiers);

// Load the program in memory
load_program_hint(&mut vm, &mut exec_scopes, &ids_data, &ap_tracking)
Expand Down
2 changes: 2 additions & 0 deletions src/hints/fact_topologies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ pub fn compute_fact_topologies<'a>(
/// * `output_start`: Start of the output range for this fact topology.
///
/// Reimplements the following Python code:
/// ```no-run
/// offset = 0
/// for i, page_size in enumerate(fact_topology.page_sizes):
/// output_builtin.add_page(
Expand All @@ -162,6 +163,7 @@ pub fn compute_fact_topologies<'a>(
/// offset += page_size
///
/// return len(fact_topology.page_sizes)
/// ```
fn add_consecutive_output_pages(
fact_topology: &FactTopology,
output_builtin: &mut OutputBuiltinRunner,
Expand Down
9 changes: 5 additions & 4 deletions src/hints/program_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,18 @@ fn maybe_relocatable_to_field_element(
felt_to_field_element(felt)
}

#[allow(dead_code)] // TODO: remove
/// Computes the Pedersen hash of a program.
///
/// Reimplements this Python function:
/// ```no-run
/// def compute_program_hash_chain(program: ProgramBase, bootloader_version=0):
/// builtin_list = [from_bytes(builtin.encode("ascii")) for builtin in program.builtins]
/// # The program header below is missing the data length, which is later added to the data_chain.
/// program_header = [bootloader_version, program.main, len(program.builtins)] + builtin_list
/// data_chain = program_header + program.data
///
/// return compute_hash_chain([len(data_chain)] + data_chain)
/// ```
pub fn compute_program_hash_chain(
program: &StrippedProgram,
bootloader_version: usize,
Expand Down Expand Up @@ -168,15 +169,15 @@ mod tests {
#[rstest]
// Expected hashes generated with `cairo-hash-program`
#[case::fibonacci(
"./test-programs/cairo0/fibonacci/fibonacci.json",
"./dependencies/test-programs/cairo0/fibonacci/fibonacci.json",
"0x6fc56a47599a5cc20bb3c6d4c5397f872bb6269f036e383f4c13986d4020952"
)]
#[case::field_arithmetic(
"./test-programs/cairo0/field-arithmetic/field_arithmetic.json",
"./dependencies/test-programs/cairo0/field-arithmetic/field_arithmetic.json",
"0xdc5a7432daec36bb707aa9f8cbcd60a2c5a4f5b16dbe7a4b6d96d5bfdd2a43"
)]
#[case::keccak_copy_inputs(
"./test-programs/cairo0/keccak-copy-inputs/keccak_copy_inputs.json",
"./dependencies/test-programs/cairo0/keccak-copy-inputs/keccak_copy_inputs.json",
"0x79e69539b9bbcc863519fb17f864c3439277cd851146f30d1ce0232fb358632"
)]
fn test_compute_program_hash_chain(
Expand Down

0 comments on commit ef1e51f

Please sign in to comment.