Skip to content

Commit

Permalink
Fix failing tests (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
whichqua authored Jun 6, 2024
1 parent ca65f84 commit 9b3715c
Show file tree
Hide file tree
Showing 15 changed files with 330 additions and 216 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: "true"
- run: rustup show
- run: rustup component add rustfmt
- uses: Swatinem/rust-cache@v2
Expand All @@ -20,6 +22,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: "true"
- run: rustup show
- uses: Swatinem/rust-cache@v2
- run: cargo clippy --all -- -D warnings
Expand All @@ -32,24 +36,20 @@ jobs:
submodules: "true"
- run: rustup show
- uses: Swatinem/rust-cache@v2
- uses: actions/setup-python@v4
with:
python-version: '3.11'
# - name: "Unit tests"
# run: |
# RUSTFLAGS="-D warnings" cargo test
- name: "Test examples"
- name: Run Rust tests
run: |
RUSTFLAGS="-D warnings" cargo run --example run_program
RUSTFLAGS="-D warnings" cargo test
udeps:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: "true"
- run: rustup toolchain install nightly --profile minimal
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true
- run: cargo install cargo-udeps --locked
# - run: cargo +nightly udeps --all-targets
- run: cargo +nightly udeps
- name: Run udeps
run: cargo +nightly udeps --all-targets
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "dependencies/cairo-lang"]
path = dependencies/cairo-lang
url = https://github.com/starkware-libs/cairo-lang
[submodule "test-programs"]
path = dependencies/test-programs
url = https://github.com/Moonsong-Labs/cairo-programs
2 changes: 1 addition & 1 deletion dependencies/cairo-lang
Submodule cairo-lang updated 225 files
1 change: 1 addition & 0 deletions dependencies/test-programs
Submodule test-programs added at b3a798
117 changes: 62 additions & 55 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 Expand Up @@ -433,16 +435,24 @@ pub fn assert_program_address(
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,
};
use crate::hints::types::{BootloaderConfig, SimpleBootloaderInput};
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;
use cairo_vm::vm::runners::builtin_runner::{BuiltinRunner, OutputBuiltinState};
use cairo_vm::vm::runners::cairo_pie::PublicMemoryPage;
use cairo_vm::vm::runners::builtin_runner::{
BuiltinRunner, OutputBuiltinRunner, OutputBuiltinState,
};
use cairo_vm::vm::runners::cairo_pie::{BuiltinAdditionalData, PublicMemoryPage};
use cairo_vm::{any_box, relocatable, Felt252};
use rstest::{fixture, rstest};

Expand All @@ -469,17 +479,21 @@ mod tests {

#[rstest]
fn test_prepare_simple_bootloader_output_segment(bootloader_input: BootloaderInput) {
let mut vm = vm!();
vm.segments.add();
vm.run_context.fp = 1;
let mut vm = VirtualMachine::new(false);
vm.add_memory_segment();
vm.add_memory_segment();
vm.set_fp(1);

let mut output_builtin = OutputBuiltinRunner::new(true);
output_builtin.initialize_segments(&mut vm.segments);
vm.builtin_runners
.push(BuiltinRunner::Output(output_builtin.clone()));

let mut exec_scopes = ExecutionScopes::new();
let ids_data = ids_data!["simple_bootloader_output_start"];
let ids_data = HashMap::from([(
"simple_bootloader_output_start".to_string(),
HintReference::new_simple(-1),
)]);
let ap_tracking = ApTracking::new();

exec_scopes.insert_value(vars::BOOTLOADER_INPUT, bootloader_input);
Expand All @@ -492,18 +506,16 @@ mod tests {
.expect("Hint failed unexpectedly");

let current_output_builtin = vm
.get_output_builtin()
.get_output_builtin_mut()
.expect("The VM should have an output builtin")
.clone();
let stored_output_builtin: OutputBuiltinState = exec_scopes
.get(vars::OUTPUT_BUILTIN_STATE)
.expect("The output builtin is not stored in the execution scope as expected");

// Check the content of the stored output builtin
assert_ne!(current_output_builtin.base(), stored_output_builtin.base());
assert_eq!(stored_output_builtin.base(), output_builtin.base());
assert_eq!(stored_output_builtin.stop_ptr, output_builtin.stop_ptr);
assert_eq!(stored_output_builtin.included, output_builtin.included);
assert_ne!(current_output_builtin.base(), stored_output_builtin.base);
assert_eq!(stored_output_builtin.base, output_builtin.base());

let simple_bootloader_output_start = get_maybe_relocatable_from_var_name(
"simple_bootloader_output_start",
Expand Down Expand Up @@ -557,12 +569,18 @@ mod tests {
let mut vm: VirtualMachine = vm!();
// The VM must have an existing output segment
let output_segment = vm.add_memory_segment();
vm.builtin_runners = vec![OutputBuiltinRunner::from_segment(&output_segment, true).into()];
let output_builtin = {
let mut builtin = OutputBuiltinRunner::new(true);
builtin.new_state(output_segment.segment_index.try_into().unwrap(), true);
builtin
};

vm.builtin_runners = vec![output_builtin.into()];

let mut exec_scopes = ExecutionScopes::new();
let new_segment = vm.add_memory_segment();
let output_builtin_state = OutputBuiltinState {
base: new_segment.segment_index,
base: new_segment.segment_index.try_into().unwrap(),
pages: Default::default(),
attributes: Default::default(),
};
Expand All @@ -573,7 +591,7 @@ mod tests {
assert_eq!(vm.builtin_runners.len(), 1);
match &vm.builtin_runners[0] {
BuiltinRunner::Output(output_builtin) => {
assert_eq!(output_builtin.base(), output_builtin_state.base());
assert_eq!(output_builtin.base(), output_builtin_state.base);
}
other => panic!("Expected an output builtin, found {:?}", other),
}
Expand All @@ -585,7 +603,7 @@ mod tests {

let mut vm = vm!();
add_segments!(vm, 2);
vm.run_context.fp = 2;
vm.set_fp(2);

let mut exec_scopes = ExecutionScopes::new();
let ids_data = ids_data!["bootloader_config"];
Expand All @@ -598,10 +616,7 @@ mod tests {

let bootloader_config_segment =
get_ptr_from_var_name("bootloader_config", &mut vm, &ids_data, &ap_tracking).unwrap();

let config_segment = vm
.segments
.memory
.get_continuous_range(bootloader_config_segment, 3)
.unwrap();

Expand All @@ -622,8 +637,6 @@ mod tests {
match programs_segment {
MaybeRelocatable::RelocatableValue(relocatable) => {
let program_hashes: Vec<Felt252> = vm
.segments
.memory
.get_integer_range(relocatable.clone(), expected_nb_programs)
.unwrap()
.iter()
Expand All @@ -643,6 +656,7 @@ mod tests {

#[rstest]
fn test_gen_arg() {
use std::ops::Add;
let mut vm = vm!();

let mut nested_args = Vec::<Box<dyn Any>>::new();
Expand All @@ -657,8 +671,6 @@ mod tests {
let args_base: Relocatable = gen_arg(&mut vm, &args).expect("gen_args failed unexpectedly");

let values = vm
.segments
.memory
.get_integer_range(args_base, 2)
.expect("Loading values failed");

Expand All @@ -667,14 +679,10 @@ mod tests {

let nested_args_address: Relocatable = args_base.add(2i32).unwrap();
let nested_args_base = vm
.segments
.memory
.get_relocatable(nested_args_address)
.expect("Nested vector should be here");

let nested_values = vm
.segments
.memory
.get_integer_range(nested_args_base, 2)
.expect("Loading nested values failed");

Expand All @@ -686,10 +694,9 @@ mod tests {
fn test_enter_packed_output_scope() {
let mut vm = vm!();
// Set n_subtasks to 2
vm.run_context.fp = 1;
vm.segments = segments![((1, 0), 2)];
let ids_data = ids_data!["n_subtasks"];

vm.set_fp(1);
define_segments!(vm, 2, [((1, 0), 2)]);
let ids_data = HashMap::from([("n_subtasks".to_string(), HintReference::new_simple(-1))]);
let ap_tracking = ApTracking::default();

let mut exec_scopes = ExecutionScopes::new();
Expand Down Expand Up @@ -729,11 +736,7 @@ mod tests {
) -> bool {
exec_scopes.insert_value(vars::PACKED_OUTPUT, packed_output);
is_plain_packed_output(vm, exec_scopes).expect("Hint failed unexpectedly");
let result = vm
.segments
.memory
.get_integer(vm.run_context.get_ap())
.unwrap();
let result = vm.get_integer(vm.get_ap()).unwrap();

result.into_owned() != Felt252::from(0)
}
Expand All @@ -744,7 +747,8 @@ mod tests {
assert!(is_plain(&mut vm, &mut exec_scopes, plain_packed_output));

// Increment AP to avoid an inconsistent memory error writing in the same slot
vm.run_context.ap += 1;
let new_ap = &vm.get_ap().offset + 1usize;
vm.set_ap(new_ap);
assert!(!is_plain(
&mut vm,
&mut exec_scopes,
Expand All @@ -755,18 +759,16 @@ mod tests {
#[test]
fn test_save_output_pointer() {
let mut vm = vm!();
vm.segments = segments![((1, 0), (0, 0))];
define_segments!(vm, 2, [((1, 0), (0, 0))]);
let mut hint_ref = HintReference::new(0, 0, true, false);
hint_ref.offset2 = OffsetValue::Value(2);
let ids_data = HashMap::from([("output_ptr".to_string(), hint_ref)]);

let mut exec_scopes = ExecutionScopes::new();

let hint_data = HintProcessorData::new_default(
String::from(hint_code::BOOTLOADER_SAVE_OUTPUT_POINTER),
ids_data,
);
let mut hint_processor = BuiltinHintProcessor::new_empty();
let hint_data =
HintProcessorData::new_default(String::from(BOOTLOADER_SAVE_OUTPUT_POINTER), ids_data);
let mut hint_processor = MinimalBootloaderHintProcessor::new();
assert_matches!(
hint_processor.execute_hint(
&mut vm,
Expand Down Expand Up @@ -811,10 +813,10 @@ mod tests {
exec_scopes.insert_box("bootloader_input", Box::new(bootloader_input.clone()));

let hint_data = HintProcessorData::new_default(
String::from(hint_code::BOOTLOADER_SAVE_PACKED_OUTPUTS),
String::from(BOOTLOADER_SAVE_PACKED_OUTPUTS),
HashMap::new(),
);
let mut hint_processor = BuiltinHintProcessor::new_empty();
let mut hint_processor = MinimalBootloaderHintProcessor::new();
assert_matches!(
hint_processor.execute_hint(
&mut vm,
Expand Down Expand Up @@ -877,8 +879,13 @@ mod tests {
offset: 18
}
);
let pages = match vm.get_output_builtin_mut().unwrap().get_additional_data() {
BuiltinAdditionalData::Output(o) => o.pages,
_ => unreachable!("Type should be Output"),
};

assert_eq!(
vm.get_output_builtin().unwrap().pages,
pages,
HashMap::from([
(1, PublicMemoryPage { start: 2, size: 3 }),
(2, PublicMemoryPage { start: 5, size: 1 }),
Expand All @@ -905,10 +912,10 @@ mod tests {
);

let hint_data = HintProcessorData::new_default(
String::from(hint_code::BOOTLOADER_SET_PACKED_OUTPUT_TO_SUBTASKS),
String::from(BOOTLOADER_SET_PACKED_OUTPUT_TO_SUBTASKS),
HashMap::new(),
);
let mut hint_processor = BuiltinHintProcessor::new_empty();
let mut hint_processor = MinimalBootloaderHintProcessor::new();
assert_matches!(
hint_processor.execute_hint(
&mut vm,
Expand All @@ -927,7 +934,7 @@ mod tests {
fn test_guess_pre_image_of_subtasks_output_hash() {
let mut vm = vm!();
add_segments!(vm, 2);
vm.run_context.fp = 2;
vm.set_fp(2);

let ids_data = ids_data!["nested_subtasks_output_len", "nested_subtasks_output"];

Expand All @@ -947,15 +954,15 @@ mod tests {
run_hint!(
vm,
ids_data.clone(),
hint_code::BOOTLOADER_GUESS_PRE_IMAGE_OF_SUBTASKS_OUTPUT_HASH,
BOOTLOADER_GUESS_PRE_IMAGE_OF_SUBTASKS_OUTPUT_HASH,
&mut exec_scopes
),
Ok(())
);
let nested_subtasks_output_len =
get_integer_from_var_name("nested_subtasks_output_len", &vm, &ids_data, &ap_tracking)
.expect("nested_subtasks_output_len should be set")
.into_owned();
.to_owned();
assert_eq!(nested_subtasks_output_len, 1.into());

let nested_subtasks_output =
Expand Down Expand Up @@ -988,7 +995,7 @@ mod tests {
let mut vm = vm!();

add_segments!(vm, 2);
vm.run_context.fp = 2;
vm.set_fp(2);

let ids_data = ids_data!(vars::PROGRAM_ADDRESS);
let ap_tracking = ApTracking::new();
Expand Down Expand Up @@ -1019,7 +1026,7 @@ mod tests {
let result = run_hint!(
vm,
ids_data.clone(),
hint_code::EXECUTE_TASK_ASSERT_PROGRAM_ADDRESS,
EXECUTE_TASK_ASSERT_PROGRAM_ADDRESS,
&mut exec_scopes
);

Expand Down
Loading

0 comments on commit 9b3715c

Please sign in to comment.