Skip to content

Commit

Permalink
fix: get most tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
whichqua committed Jun 5, 2024
1 parent 051714b commit 418f224
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 149 deletions.
12 changes: 0 additions & 12 deletions scripts/compile-bootloader.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
# to recompile the bootloader when changing the version of cairo-lang.

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
<<<<<<< HEAD
CAIRO_VERSION="0.13.0"
=======
CAIRO_VERSION="0.13.1"
>>>>>>> fb2312f0f9bda782d4d08e8c6bb0bdf77f904ed8

CAIRO_LANG_DIR=$(readlink -f "${SCRIPT_DIR}/../dependencies/cairo-lang")

Expand All @@ -31,14 +27,6 @@ OUTPUT_DIR="$(readlink -f "${SCRIPT_DIR}/../resources")"
mkdir -p "${OUTPUT_DIR}"
COMPILED_BOOTLOADER="${OUTPUT_DIR}/bootloader-${CAIRO_VERSION}.json"

<<<<<<< HEAD
cairo-compile \
"${CAIRO_LANG_DIR}/src/starkware/cairo/bootloaders/bootloader/bootloader.cairo" \
--output "${COMPILED_BOOTLOADER}" \
--cairo_path "${CAIRO_LANG_DIR}"/src \
--proof_mode
=======
cairo-compile "${CAIRO_LANG_DIR}/src/starkware/cairo/bootloaders/bootloader/bootloader.cairo" --output "${COMPILED_BOOTLOADER}" --cairo_path "${CAIRO_LANG_DIR}"/src
>>>>>>> fb2312f0f9bda782d4d08e8c6bb0bdf77f904ed8

echo "Compiled the bootloader to '${COMPILED_BOOTLOADER}'."
19 changes: 11 additions & 8 deletions src/hints/bootloader_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,14 +434,16 @@ mod tests {
use num_traits::ToPrimitive;

use crate::hints::codes::{
BOOTLOADER_GUESS_PRE_IMAGE_OF_SUBTASKS_OUTPUT_HASH, BOOTLOADER_SAVE_OUTPUT_POINTER, BOOTLOADER_SAVE_PACKED_OUTPUTS, BOOTLOADER_SET_PACKED_OUTPUT_TO_SUBTASKS, EXECUTE_TASK_ASSERT_PROGRAM_ADDRESS
BOOTLOADER_GUESS_PRE_IMAGE_OF_SUBTASKS_OUTPUT_HASH, BOOTLOADER_SAVE_OUTPUT_POINTER,
BOOTLOADER_SAVE_PACKED_OUTPUTS, BOOTLOADER_SET_PACKED_OUTPUT_TO_SUBTASKS,
EXECUTE_TASK_ASSERT_PROGRAM_ADDRESS,
};
use crate::hints::types::{BootloaderConfig, SimpleBootloaderInput};
use crate::{add_segments, define_segments, ids_data, run_hint, segments, vm, MinimalBootloaderHintProcessor};
use assert_matches::assert_matches;
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::{
BuiltinHintProcessor, HintProcessorData,
use crate::{
add_segments, define_segments, ids_data, run_hint, vm, MinimalBootloaderHintProcessor,
};
use assert_matches::assert_matches;
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::get_maybe_relocatable_from_var_name;
use cairo_vm::hint_processor::hint_processor_definition::HintProcessorLogic;
use cairo_vm::serde::deserialize_program::OffsetValue;
Expand Down Expand Up @@ -564,7 +566,7 @@ mod tests {
fn test_restore_bootloader_output() {
let mut vm: VirtualMachine = vm!();
// The VM must have an existing output segment
let output_segment = vm.add_memory_segment();
vm.add_memory_segment();
vm.builtin_runners = vec![OutputBuiltinRunner::new(true).into()];

let mut exec_scopes = ExecutionScopes::new();
Expand Down Expand Up @@ -686,8 +688,9 @@ mod tests {
// Set n_subtasks to 2
vm.set_fp(1);
define_segments!(vm, 2, [((1, 0), 2)]);
let ids_data = ids_data!["n_subtasks"];

// let ids_data = ids_data!["n_subtasks"];
let ids_data = HashMap::from([("n_subtasks".to_string(), HintReference::new_simple(-1))]);
dbg!(&ids_data);
let ap_tracking = ApTracking::default();

let mut exec_scopes = ExecutionScopes::new();
Expand Down
78 changes: 43 additions & 35 deletions src/hints/execute_task_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,27 +485,24 @@ mod util {
#[cfg(test)]
mod tests {
use std::path::Path;
use std::sync::Arc;

use assert_matches::assert_matches;
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::{
BuiltinHintProcessor, HintProcessorData,
};
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::get_maybe_relocatable_from_var_name;
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;

use cairo_vm::any_box;
use cairo_vm::hint_processor::hint_processor_definition::HintProcessorLogic;
use cairo_vm::serde::deserialize_program::{OffsetValue, ReferenceManager};
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;
use cairo_vm::vm::runners::builtin_runner::BuiltinRunner;
use cairo_vm::vm::runners::cairo_pie::{BuiltinAdditionalData, OutputBuiltinAdditionalData, PublicMemoryPage};
use cairo_vm::{any_box, relocatable, Felt252};
use num_traits::ToPrimitive;
use cairo_vm::vm::runners::cairo_pie::{BuiltinAdditionalData, PublicMemoryPage};

use rstest::{fixture, rstest};

use crate::hints::codes::EXECUTE_TASK_CALL_TASK;
use crate::hints::types::{BootloaderConfig, SimpleBootloaderInput};
use crate::{add_segments, define_segments, ids_data, non_continuous_ids_data, run_hint, segments, vm};

use crate::{add_segments, define_segments, ids_data, non_continuous_ids_data, run_hint, vm};

use super::*;

Expand Down Expand Up @@ -550,23 +547,25 @@ mod tests {
#[fixture]
fn fibonacci() -> Program {
let program_content =
include_bytes!("../cairo-programs/fibonacci.json").to_vec();
include_bytes!("/home/geoff/Desktop/cairo-programs/cairo0/fibonacci/fibonacci.json")
.to_vec();

Program::from_bytes(&program_content, Some("main"))
.expect("Loading example program failed unexpectedly")
}

#[fixture]
fn fibonacci_pie() -> CairoPie {
let pie_file =
Path::new("../cairo-programs/pie.zip");
let pie_file = Path::new("/home/geoff/Desktop/cairo-vm/pie.zip");
CairoPie::read_zip_file(pie_file).expect("Failed to load the program PIE")
}

#[fixture]
fn field_arithmetic_program() -> Program {
let program_content =
include_bytes!("../cairo-programs/fibonacci.json").to_vec();
let program_content = include_bytes!(
"/home/geoff/Desktop/cairo-programs/cairo0/field-arithmetic/field_arithmetic.json"
)
.to_vec();

Program::from_bytes(&program_content, Some("main"))
.expect("Loading example program failed unexpectedly")
Expand Down Expand Up @@ -677,7 +676,8 @@ mod tests {
identifiers,
Default::default(),
Default::default(),
).unwrap();
)
.unwrap();

program
}
Expand All @@ -691,10 +691,13 @@ mod tests {
// the Bootloader Cairo code. Our code only uses the first felt (`output` field in the
// struct). Finally, we put the mocked output of `select_input_builtins` in the next
// memory address and increase the AP register accordingly.
define_segments!(vm, 3, [((1, 0), (2, 0)), ((1, 1), (4, 0)), ((1, 9), (4, 42))]);
define_segments!(
vm,
3,
[((1, 0), (2, 0)), ((1, 1), (4, 0)), ((1, 9), (4, 42))]
);
vm.set_ap(10);
vm.set_fp(9);
add_segments!(vm, 3);

let program_header_ptr = Relocatable::from((2, 0));
let ids_data = non_continuous_ids_data![
Expand Down Expand Up @@ -752,7 +755,7 @@ mod tests {
(2, PublicMemoryPage { start: 7, size: 3 }),
]),
attributes: HashMap::from([("gps_fact_topology".to_string(), tree_structure.clone())]),
base: 0
base: 0,
};
let mut output_builtin = OutputBuiltinRunner::new(true);
output_builtin.set_state(program_output_data.clone());
Expand All @@ -769,9 +772,10 @@ mod tests {

let mut exec_scopes = ExecutionScopes::new();

let output_runner_data = OutputBuiltinAdditionalData {
let output_runner_data = OutputBuiltinState {
pages: HashMap::new(),
attributes: HashMap::new(),
base: 0,
};
exec_scopes.insert_value(vars::OUTPUT_RUNNER_DATA, Some(output_runner_data.clone()));
exec_scopes.insert_value(vars::TASK, task);
Expand All @@ -793,7 +797,7 @@ mod tests {
vm.get_output_builtin_mut().unwrap().get_additional_data();
assert!(matches!(
output_builtin_additional_data,
BuiltinAdditionalData::Output(data) if data == output_runner_data,
BuiltinAdditionalData::Output(data) if data.pages == output_runner_data.pages && data.attributes == output_runner_data.attributes,
));
}

Expand All @@ -809,19 +813,23 @@ mod tests {
// Initialize the used builtins to {range_check: 30, bitwise: 50} as these two
// are used by the field arithmetic program. Note that the used builtins list
// does not contain empty elements (i.e. offsets are 8 and 9 instead of 10 and 12).
define_segments!(vm, 2, [
((1, 0), (2, 1)),
((1, 1), (2, 2)),
((1, 2), (2, 3)),
((1, 3), (2, 4)),
((1, 4), (2, 5)),
((1, 5), (2, 6)),
((1, 6), (2, 7)),
((1, 7), (2, 8)),
((1, 8), (2, 30)),
((1, 9), (2, 50)),
((1, 24), (1, 8)),
]);
define_segments!(
vm,
2,
[
((1, 0), (2, 1)),
((1, 1), (2, 2)),
((1, 2), (2, 3)),
((1, 3), (2, 4)),
((1, 4), (2, 5)),
((1, 5), (2, 6)),
((1, 6), (2, 7)),
((1, 7), (2, 8)),
((1, 8), (2, 30)),
((1, 9), (2, 50)),
((1, 24), (1, 8)),
]
);
vm.set_fp(25);
add_segments!(vm, 1);

Expand Down
2 changes: 1 addition & 1 deletion src/hints/inner_select_builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ mod tests {
use cairo_vm::Felt252;
use rstest::rstest;

use crate::{add_segments, define_segments, ids_data, segments, vm};
use crate::{add_segments, define_segments, ids_data, vm};

use super::*;

Expand Down
12 changes: 6 additions & 6 deletions src/hints/program_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,16 @@ mod tests {
#[rstest]
// Expected hashes generated with `cairo-hash-program`
#[case::fibonacci(
"../cairo_programs/fibonacci.json",
"0x43b17e9592f33142246af4c06cd2b574b460dd1f718d76b51341175a62b220f"
"/home/geoff/Desktop/cairo-programs/cairo0/fibonacci/fibonacci.json",
"0x6fc56a47599a5cc20bb3c6d4c5397f872bb6269f036e383f4c13986d4020952"
)]
#[case::field_arithmetic(
"../cairo_programs/field_arithmetic.json",
"0x1031772ca86e618b058101af9c9a3277bac90712b750bcea1cc69d6c7cad8a7"
"/home/geoff/Desktop/cairo-programs/cairo0/field-arithmetic/field_arithmetic.json",
"0xdc5a7432daec36bb707aa9f8cbcd60a2c5a4f5b16dbe7a4b6d96d5bfdd2a43"
)]
#[case::keccak_copy_inputs(
"../cairo_programs/keccak_copy_inputs.json",
"0x49484fdc8e7a85061f9f21b7e21fe276d8a88c8e96681101a2518809e686c6c"
"/home/geoff/Desktop/cairo-programs/cairo0/keccak-copy-inputs/keccak_copy_inputs.json",
"0x79e69539b9bbcc863519fb17f864c3439277cd851146f30d1ce0232fb358632"
)]
fn test_compute_program_hash_chain(
#[case] program_path: PathBuf,
Expand Down
12 changes: 7 additions & 5 deletions src/hints/program_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,19 @@ impl<'vm> ProgramLoader<'vm> {

#[cfg(test)]
mod tests {
use std::any::Any;


use cairo_vm::types::builtin_name::BuiltinName;
use cairo_vm::types::program::Program;
use cairo_vm::types::relocatable::Relocatable;
use cairo_vm::vm::runners::cairo_pie::StrippedProgram;
use cairo_vm::vm::vm_memory::memory_segments::MemorySegmentManager;
use cairo_vm::Felt252;
use num_traits::ToPrimitive;

use rstest::{fixture, rstest};
use serde::Serialize;


use crate::hints::types::BootloaderVersion;
use crate::{add_segments, hints::types::BootloaderVersion};

use super::*;

Expand Down Expand Up @@ -242,7 +242,7 @@ mod tests {
#[fixture]
fn fibonacci() -> Program {
let program_content =
include_bytes!("../cairo-programs/fibonacci.json").to_vec();
include_bytes!("/home/geoff/Desktop/test-cairo/fibonacci.json").to_vec();

Program::from_bytes(&program_content, Some("main"))
.expect("Loading example program failed unexpectedly")
Expand Down Expand Up @@ -282,6 +282,7 @@ mod tests {
let program = fibonacci.get_stripped_program().unwrap();

let mut vm = VirtualMachine::new(false);
add_segments!(vm, 2);
let mut segments = MemorySegmentManager::new();
let base_address = segments.add();

Expand Down Expand Up @@ -318,6 +319,7 @@ mod tests {
let program = fibonacci.get_stripped_program().unwrap();

let mut vm = VirtualMachine::new(false);
add_segments!(vm, 2);
let mut segments = MemorySegmentManager::new();
let base_address = segments.add();

Expand Down
2 changes: 1 addition & 1 deletion src/hints/select_builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ mod tests {
use cairo_vm::vm::vm_core::VirtualMachine;
use std::collections::HashMap;

use crate::{define_segments, ids_data, segments, vm};
use crate::{define_segments, ids_data, vm};

use super::*;

Expand Down
Loading

0 comments on commit 418f224

Please sign in to comment.