diff --git a/crates/starknet_committer_and_os_cli/src/os_cli/tests/aliases.rs b/crates/starknet_committer_and_os_cli/src/os_cli/tests/aliases.rs index ae23d98c278..9589f3627bb 100644 --- a/crates/starknet_committer_and_os_cli/src/os_cli/tests/aliases.rs +++ b/crates/starknet_committer_and_os_cli/src/os_cli/tests/aliases.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; +use apollo_starknet_os_program::test_programs::ALIASES_TEST_BYTES; use starknet_os::test_utils::cairo_runner::EntryPointRunnerConfig; use tracing::info; @@ -8,21 +9,21 @@ use crate::os_cli::tests::utils::test_cairo_function; // TODO(Amos): This test is incomplete. Add the rest of the test cases and remove this todo. #[allow(clippy::result_large_err)] -pub(crate) fn aliases_test(input: &str) -> OsPythonTestResult { +pub(crate) fn aliases_test() -> OsPythonTestResult { info!("Testing `test_constants`..."); - test_constants(input)?; + test_constants()?; Ok("".to_string()) } #[allow(clippy::result_large_err)] -fn test_constants(input: &str) -> OsPythonTestResult { +fn test_constants() -> OsPythonTestResult { let max_non_compressed_contract_address = 15; let alias_counter_storage_key = 0; let initial_available_alias = 128; let alias_contract_address = 2; test_cairo_function( &EntryPointRunnerConfig::default(), - input, + ALIASES_TEST_BYTES, "test_constants", &[ max_non_compressed_contract_address.into(), diff --git a/crates/starknet_committer_and_os_cli/src/os_cli/tests/bls_field.rs b/crates/starknet_committer_and_os_cli/src/os_cli/tests/bls_field.rs index 312d979f0f2..0dc59cec8a5 100644 --- a/crates/starknet_committer_and_os_cli/src/os_cli/tests/bls_field.rs +++ b/crates/starknet_committer_and_os_cli/src/os_cli/tests/bls_field.rs @@ -1,6 +1,7 @@ use std::array; use std::collections::HashMap; +use apollo_starknet_os_program::OS_PROGRAM_BYTES; use cairo_vm::types::builtin_name::BuiltinName; use cairo_vm::types::layout_name::LayoutName; use cairo_vm::types::program::Program; @@ -32,20 +33,21 @@ use crate::shared_utils::types::PythonTestError; const REDUCED_MUL_LIMB_BOUND: i128 = 2_i128.pow(104); +// TODO(Nimrod): Move this to the starknet_os crate and run it directly. #[allow(clippy::result_large_err)] -pub(crate) fn test_bls_field(input: &str) -> OsPythonTestResult { +pub(crate) fn test_bls_field() -> OsPythonTestResult { info!("Testing `test_bigint3_to_uint256`..."); - test_bigint3_to_uint256(input)?; + test_bigint3_to_uint256()?; info!("Testing `test_felt_to_bigint3`..."); - test_felt_to_bigint3(input)?; + test_felt_to_bigint3()?; info!("Testing `test_horner_eval`..."); - test_horner_eval(input)?; + test_horner_eval()?; info!("Testing `test_reduced_mul_random`..."); - test_reduced_mul_random(input)?; + test_reduced_mul_random()?; info!("Testing `test_reduced_mul_parameterized`..."); - test_reduced_mul_parameterized(input)?; + test_reduced_mul_parameterized()?; info!("Testing `test_bls_prime_value`..."); - test_bls_prime_value(input)?; + test_bls_prime_value()?; Ok("".to_string()) } @@ -58,7 +60,7 @@ fn get_entrypoint_runner_config() -> EntryPointRunnerConfig { } #[allow(clippy::result_large_err)] -fn run_reduced_mul_test(input: &str, a_split: &[Felt], b_split: &[Felt]) -> OsPythonTestResult { +fn run_reduced_mul_test(a_split: &[Felt], b_split: &[Felt]) -> OsPythonTestResult { let explicit_args = [ EndpointArg::Value(ValueArg::Array(a_split.to_vec())), EndpointArg::Value(ValueArg::Array(b_split.to_vec())), @@ -72,7 +74,7 @@ fn run_reduced_mul_test(input: &str, a_split: &[Felt], b_split: &[Felt]) -> OsPy let expected_explicit_args = [EndpointArg::Value(ValueArg::Array(expected_result.to_vec()))]; test_cairo_function( &get_entrypoint_runner_config(), - input, + OS_PROGRAM_BYTES, "starkware.starknet.core.os.data_availability.bls_field.reduced_mul", &explicit_args, &implicit_args, @@ -84,7 +86,7 @@ fn run_reduced_mul_test(input: &str, a_split: &[Felt], b_split: &[Felt]) -> OsPy } #[allow(clippy::result_large_err)] -fn test_bigint3_to_uint256(input: &str) -> OsPythonTestResult { +fn test_bigint3_to_uint256() -> OsPythonTestResult { let mut rng = seeded_random_prng(); let random_u256_big_uint: BigUint = rng.sample(RandomBits::new(256)); let random_u256_bigint = BigInt::from_biguint(Sign::Plus, random_u256_big_uint); @@ -104,7 +106,7 @@ fn test_bigint3_to_uint256(input: &str) -> OsPythonTestResult { let entrypoint_runner_config = get_entrypoint_runner_config(); test_cairo_function( &entrypoint_runner_config, - input, + OS_PROGRAM_BYTES, "starkware.starknet.core.os.data_availability.bls_field.bigint3_to_uint256", &explicit_args, &implicit_args, @@ -116,7 +118,7 @@ fn test_bigint3_to_uint256(input: &str) -> OsPythonTestResult { } #[allow(clippy::result_large_err)] -fn test_felt_to_bigint3(input: &str) -> OsPythonTestResult { +fn test_felt_to_bigint3() -> OsPythonTestResult { let values: [BigInt; 9] = [ 0.into(), 1.into(), @@ -140,7 +142,7 @@ fn test_felt_to_bigint3(input: &str) -> OsPythonTestResult { test_cairo_function( &entrypoint_runner_config, - input, + OS_PROGRAM_BYTES, "starkware.starknet.core.os.data_availability.bls_field.felt_to_bigint3", &explicit_args, &implicit_args, @@ -153,7 +155,7 @@ fn test_felt_to_bigint3(input: &str) -> OsPythonTestResult { } #[allow(clippy::result_large_err)] -fn test_horner_eval(input: &str) -> OsPythonTestResult { +fn test_horner_eval() -> OsPythonTestResult { let mut rng = seeded_random_prng(); let entrypoint_runner_config = get_entrypoint_runner_config(); @@ -175,7 +177,7 @@ fn test_horner_eval(input: &str) -> OsPythonTestResult { let (_, explicit_retdata, _) = run_cairo_0_entry_point( &entrypoint_runner_config, - input, + OS_PROGRAM_BYTES, "starkware.starknet.core.os.data_availability.bls_field.horner_eval", &explicit_args, &implicit_args, @@ -237,7 +239,7 @@ fn test_horner_eval(input: &str) -> OsPythonTestResult { #[allow(dead_code)] #[allow(clippy::result_large_err)] -fn test_reduced_mul_random(input: &str) -> OsPythonTestResult { +fn test_reduced_mul_random() -> OsPythonTestResult { // Generate a,b in (-REDUCED_MUL_LIMB_LIMIT, REDUCED_MUL_LIMB_LIMIT). let mut rng = seeded_random_prng(); let a_split = (0..3) @@ -247,12 +249,12 @@ fn test_reduced_mul_random(input: &str) -> OsPythonTestResult { .map(|_| rng.gen_range(-REDUCED_MUL_LIMB_BOUND + 1..REDUCED_MUL_LIMB_BOUND).into()) .collect::>(); - run_reduced_mul_test(input, &a_split, &b_split) + run_reduced_mul_test(&a_split, &b_split) } #[allow(dead_code)] #[allow(clippy::result_large_err)] -fn test_reduced_mul_parameterized(input: &str) -> OsPythonTestResult { +fn test_reduced_mul_parameterized() -> OsPythonTestResult { let max_value = Felt::from(REDUCED_MUL_LIMB_BOUND - 1); let min_value = Felt::from(-REDUCED_MUL_LIMB_BOUND + 1); let values: [([Felt; 3], [Felt; 3]); 4] = [ @@ -263,16 +265,16 @@ fn test_reduced_mul_parameterized(input: &str) -> OsPythonTestResult { ]; for (a_split, b_split) in values { info!("Testing `reduced_mul` with a = {a_split:?}, b = {b_split:?}"); - run_reduced_mul_test(input, &a_split, &b_split)?; + run_reduced_mul_test(&a_split, &b_split)?; } Ok("".to_string()) } #[allow(clippy::result_large_err)] -fn test_bls_prime_value(input: &str) -> OsPythonTestResult { +fn test_bls_prime_value() -> OsPythonTestResult { let entrypoint = None; - let program = Program::from_bytes(input.as_bytes(), entrypoint).unwrap(); + let program = Program::from_bytes(OS_PROGRAM_BYTES, entrypoint).unwrap(); let actual_split_bls_prime: [Felt; 3] = array::from_fn(|i| { *program .constants diff --git a/crates/starknet_committer_and_os_cli/src/os_cli/tests/python_tests.rs b/crates/starknet_committer_and_os_cli/src/os_cli/tests/python_tests.rs index 0ec662ae9e5..b7006268ada 100644 --- a/crates/starknet_committer_and_os_cli/src/os_cli/tests/python_tests.rs +++ b/crates/starknet_committer_and_os_cli/src/os_cli/tests/python_tests.rs @@ -36,8 +36,8 @@ impl PythonTestRunner for OsPythonTestRunner { #[allow(clippy::result_large_err)] async fn run(&self, input: Option<&str>) -> OsPythonTestResult { match self { - Self::AliasesTest => aliases_test(Self::non_optional_input(input)?), - Self::BlsFieldTest => test_bls_field(Self::non_optional_input(input)?), + Self::AliasesTest => aliases_test(), + Self::BlsFieldTest => test_bls_field(), Self::InputDeserialization => input_deserialization(Self::non_optional_input(input)?), Self::EncodeFelts => { let felts: Vec = serde_json::from_str(Self::non_optional_input(input)?)?; diff --git a/crates/starknet_committer_and_os_cli/src/os_cli/tests/utils.rs b/crates/starknet_committer_and_os_cli/src/os_cli/tests/utils.rs index 244f95df125..09c38b53724 100644 --- a/crates/starknet_committer_and_os_cli/src/os_cli/tests/utils.rs +++ b/crates/starknet_committer_and_os_cli/src/os_cli/tests/utils.rs @@ -27,7 +27,7 @@ pub static DEFAULT_PRIME: LazyLock = LazyLock::new(|| { #[allow(clippy::result_large_err)] pub(crate) fn test_cairo_function( runner_config: &EntryPointRunnerConfig, - program_str: &str, + program_bytes: &[u8], function_name: &str, explicit_args: &[EndpointArg], implicit_args: &[ImplicitArg], @@ -37,7 +37,7 @@ pub(crate) fn test_cairo_function( ) -> OsPythonTestResult { run_cairo_function_and_check_result( runner_config, - program_str, + program_bytes, function_name, explicit_args, implicit_args, diff --git a/crates/starknet_os/src/test_utils/cairo_runner.rs b/crates/starknet_os/src/test_utils/cairo_runner.rs index 808ef468b48..0de82502370 100644 --- a/crates/starknet_os/src/test_utils/cairo_runner.rs +++ b/crates/starknet_os/src/test_utils/cairo_runner.rs @@ -301,10 +301,11 @@ fn extract_builtins_from_implicit_args( // TODO(Amos): Add builtins properly once the VM allows loading an entrypoint's builtins. // In addition, pass program as struct and add hint processor as param. fn inject_builtins( - program_str: &str, + program_bytes: &[u8], implicit_args: &[ImplicitArg], ) -> Cairo0EntryPointRunnerResult { let program_builtins = extract_builtins_from_implicit_args(implicit_args)?; + let program_str = std::str::from_utf8(program_bytes).unwrap(); let mut program_dict: HashMap = serde_json::from_str(program_str).map_err(Cairo0EntryPointRunnerError::ProgramSerde)?; program_dict.insert( @@ -554,7 +555,7 @@ fn get_return_values( /// usage, unless the builtin is the output builtin, in which case the arg is the output. pub fn run_cairo_0_entry_point( runner_config: &EntryPointRunnerConfig, - program_str: &str, + program_bytes: &[u8], entrypoint: &str, explicit_args: &[EndpointArg], implicit_args: &[ImplicitArg], @@ -567,7 +568,7 @@ pub fn run_cairo_0_entry_point( entrypoint = format!("__main__.{entrypoint}"); } - let program = inject_builtins(program_str, implicit_args)?; + let program = inject_builtins(program_bytes, implicit_args)?; info!("Successfully injected builtins into program."); let (state_reader, os_hints_config, os_state_input) = (None, None, None); diff --git a/crates/starknet_os/src/test_utils/cairo_runner_test.rs b/crates/starknet_os/src/test_utils/cairo_runner_test.rs index b81e56874a2..952466ccecf 100644 --- a/crates/starknet_os/src/test_utils/cairo_runner_test.rs +++ b/crates/starknet_os/src/test_utils/cairo_runner_test.rs @@ -70,7 +70,7 @@ use crate::test_utils::utils::run_cairo_function_and_check_result; /// let sum = number_1 + number_2; /// return (res=sum); /// } -const COMPILED_DUMMY_FUNCTION: &str = include_str!("compiled_dummy_function.json"); +const COMPILED_DUMMY_FUNCTION_BYTES: &[u8] = include_bytes!("compiled_dummy_function.json"); #[test] fn test_felt_and_pointers() -> Cairo0EntryPointRunnerResult<()> { @@ -117,7 +117,7 @@ fn test_felt_and_pointers() -> Cairo0EntryPointRunnerResult<()> { ])); run_cairo_function_and_check_result( &EntryPointRunnerConfig::default(), - COMPILED_DUMMY_FUNCTION, + COMPILED_DUMMY_FUNCTION_BYTES, "pass_felt_and_pointers", &[number.into(), array, tuple, simple_struct, compound_struct], &[], @@ -172,7 +172,7 @@ fn test_tuples_and_structs() -> Cairo0EntryPointRunnerResult<()> { ])); run_cairo_function_and_check_result( &EntryPointRunnerConfig::default(), - COMPILED_DUMMY_FUNCTION, + COMPILED_DUMMY_FUNCTION_BYTES, "pass_structs_and_tuples", &[tuple, named_tuple, simple_struct, compound_struct], &[], @@ -204,7 +204,7 @@ fn test_implicit_args() -> Cairo0EntryPointRunnerResult<()> { EntryPointRunnerConfig { layout: LayoutName::all_cairo, ..Default::default() }; run_cairo_function_and_check_result( &entrypoint_runner_config, - COMPILED_DUMMY_FUNCTION, + COMPILED_DUMMY_FUNCTION_BYTES, "pass_implicit_args", &[number_1.into(), number_2.into()], &[ diff --git a/crates/starknet_os/src/test_utils/utils.rs b/crates/starknet_os/src/test_utils/utils.rs index 775f9d6aaa5..7bd6abb9483 100644 --- a/crates/starknet_os/src/test_utils/utils.rs +++ b/crates/starknet_os/src/test_utils/utils.rs @@ -16,7 +16,7 @@ use crate::test_utils::cairo_runner::{ #[allow(clippy::too_many_arguments)] pub fn run_cairo_function_and_check_result( runner_config: &EntryPointRunnerConfig, - program_str: &str, + program_bytes: &[u8], function_name: &str, explicit_args: &[EndpointArg], implicit_args: &[ImplicitArg], @@ -26,7 +26,7 @@ pub fn run_cairo_function_and_check_result( ) -> Cairo0EntryPointRunnerResult<()> { let (actual_implicit_retdata, actual_explicit_retdata, _) = run_cairo_0_entry_point( runner_config, - program_str, + program_bytes, function_name, explicit_args, implicit_args,