Skip to content

Commit 5d24d07

Browse files
nimrod-starkwarevictorkstarkware
authored andcommitted
starknet_os: remove program input for python tests (#7672)
1 parent 453aa25 commit 5d24d07

File tree

7 files changed

+42
-38
lines changed

7 files changed

+42
-38
lines changed

crates/starknet_committer_and_os_cli/src/os_cli/tests/aliases.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::collections::HashMap;
22

3+
use apollo_starknet_os_program::test_programs::ALIASES_TEST_BYTES;
34
use starknet_os::test_utils::cairo_runner::EntryPointRunnerConfig;
45
use tracing::info;
56

@@ -8,21 +9,21 @@ use crate::os_cli::tests::utils::test_cairo_function;
89

910
// TODO(Amos): This test is incomplete. Add the rest of the test cases and remove this todo.
1011
#[allow(clippy::result_large_err)]
11-
pub(crate) fn aliases_test(input: &str) -> OsPythonTestResult {
12+
pub(crate) fn aliases_test() -> OsPythonTestResult {
1213
info!("Testing `test_constants`...");
13-
test_constants(input)?;
14+
test_constants()?;
1415
Ok("".to_string())
1516
}
1617

1718
#[allow(clippy::result_large_err)]
18-
fn test_constants(input: &str) -> OsPythonTestResult {
19+
fn test_constants() -> OsPythonTestResult {
1920
let max_non_compressed_contract_address = 15;
2021
let alias_counter_storage_key = 0;
2122
let initial_available_alias = 128;
2223
let alias_contract_address = 2;
2324
test_cairo_function(
2425
&EntryPointRunnerConfig::default(),
25-
input,
26+
ALIASES_TEST_BYTES,
2627
"test_constants",
2728
&[
2829
max_non_compressed_contract_address.into(),

crates/starknet_committer_and_os_cli/src/os_cli/tests/bls_field.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::array;
22
use std::collections::HashMap;
33

4+
use apollo_starknet_os_program::OS_PROGRAM_BYTES;
45
use cairo_vm::types::builtin_name::BuiltinName;
56
use cairo_vm::types::layout_name::LayoutName;
67
use cairo_vm::types::program::Program;
@@ -32,20 +33,21 @@ use crate::shared_utils::types::PythonTestError;
3233

3334
const REDUCED_MUL_LIMB_BOUND: i128 = 2_i128.pow(104);
3435

36+
// TODO(Nimrod): Move this to the starknet_os crate and run it directly.
3537
#[allow(clippy::result_large_err)]
36-
pub(crate) fn test_bls_field(input: &str) -> OsPythonTestResult {
38+
pub(crate) fn test_bls_field() -> OsPythonTestResult {
3739
info!("Testing `test_bigint3_to_uint256`...");
38-
test_bigint3_to_uint256(input)?;
40+
test_bigint3_to_uint256()?;
3941
info!("Testing `test_felt_to_bigint3`...");
40-
test_felt_to_bigint3(input)?;
42+
test_felt_to_bigint3()?;
4143
info!("Testing `test_horner_eval`...");
42-
test_horner_eval(input)?;
44+
test_horner_eval()?;
4345
info!("Testing `test_reduced_mul_random`...");
44-
test_reduced_mul_random(input)?;
46+
test_reduced_mul_random()?;
4547
info!("Testing `test_reduced_mul_parameterized`...");
46-
test_reduced_mul_parameterized(input)?;
48+
test_reduced_mul_parameterized()?;
4749
info!("Testing `test_bls_prime_value`...");
48-
test_bls_prime_value(input)?;
50+
test_bls_prime_value()?;
4951
Ok("".to_string())
5052
}
5153

@@ -58,7 +60,7 @@ fn get_entrypoint_runner_config() -> EntryPointRunnerConfig {
5860
}
5961

6062
#[allow(clippy::result_large_err)]
61-
fn run_reduced_mul_test(input: &str, a_split: &[Felt], b_split: &[Felt]) -> OsPythonTestResult {
63+
fn run_reduced_mul_test(a_split: &[Felt], b_split: &[Felt]) -> OsPythonTestResult {
6264
let explicit_args = [
6365
EndpointArg::Value(ValueArg::Array(a_split.to_vec())),
6466
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
7274
let expected_explicit_args = [EndpointArg::Value(ValueArg::Array(expected_result.to_vec()))];
7375
test_cairo_function(
7476
&get_entrypoint_runner_config(),
75-
input,
77+
OS_PROGRAM_BYTES,
7678
"starkware.starknet.core.os.data_availability.bls_field.reduced_mul",
7779
&explicit_args,
7880
&implicit_args,
@@ -84,7 +86,7 @@ fn run_reduced_mul_test(input: &str, a_split: &[Felt], b_split: &[Felt]) -> OsPy
8486
}
8587

8688
#[allow(clippy::result_large_err)]
87-
fn test_bigint3_to_uint256(input: &str) -> OsPythonTestResult {
89+
fn test_bigint3_to_uint256() -> OsPythonTestResult {
8890
let mut rng = seeded_random_prng();
8991
let random_u256_big_uint: BigUint = rng.sample(RandomBits::new(256));
9092
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 {
104106
let entrypoint_runner_config = get_entrypoint_runner_config();
105107
test_cairo_function(
106108
&entrypoint_runner_config,
107-
input,
109+
OS_PROGRAM_BYTES,
108110
"starkware.starknet.core.os.data_availability.bls_field.bigint3_to_uint256",
109111
&explicit_args,
110112
&implicit_args,
@@ -116,7 +118,7 @@ fn test_bigint3_to_uint256(input: &str) -> OsPythonTestResult {
116118
}
117119

118120
#[allow(clippy::result_large_err)]
119-
fn test_felt_to_bigint3(input: &str) -> OsPythonTestResult {
121+
fn test_felt_to_bigint3() -> OsPythonTestResult {
120122
let values: [BigInt; 9] = [
121123
0.into(),
122124
1.into(),
@@ -140,7 +142,7 @@ fn test_felt_to_bigint3(input: &str) -> OsPythonTestResult {
140142

141143
test_cairo_function(
142144
&entrypoint_runner_config,
143-
input,
145+
OS_PROGRAM_BYTES,
144146
"starkware.starknet.core.os.data_availability.bls_field.felt_to_bigint3",
145147
&explicit_args,
146148
&implicit_args,
@@ -153,7 +155,7 @@ fn test_felt_to_bigint3(input: &str) -> OsPythonTestResult {
153155
}
154156

155157
#[allow(clippy::result_large_err)]
156-
fn test_horner_eval(input: &str) -> OsPythonTestResult {
158+
fn test_horner_eval() -> OsPythonTestResult {
157159
let mut rng = seeded_random_prng();
158160
let entrypoint_runner_config = get_entrypoint_runner_config();
159161

@@ -175,7 +177,7 @@ fn test_horner_eval(input: &str) -> OsPythonTestResult {
175177

176178
let (_, explicit_retdata, _) = run_cairo_0_entry_point(
177179
&entrypoint_runner_config,
178-
input,
180+
OS_PROGRAM_BYTES,
179181
"starkware.starknet.core.os.data_availability.bls_field.horner_eval",
180182
&explicit_args,
181183
&implicit_args,
@@ -237,7 +239,7 @@ fn test_horner_eval(input: &str) -> OsPythonTestResult {
237239

238240
#[allow(dead_code)]
239241
#[allow(clippy::result_large_err)]
240-
fn test_reduced_mul_random(input: &str) -> OsPythonTestResult {
242+
fn test_reduced_mul_random() -> OsPythonTestResult {
241243
// Generate a,b in (-REDUCED_MUL_LIMB_LIMIT, REDUCED_MUL_LIMB_LIMIT).
242244
let mut rng = seeded_random_prng();
243245
let a_split = (0..3)
@@ -247,12 +249,12 @@ fn test_reduced_mul_random(input: &str) -> OsPythonTestResult {
247249
.map(|_| rng.gen_range(-REDUCED_MUL_LIMB_BOUND + 1..REDUCED_MUL_LIMB_BOUND).into())
248250
.collect::<Vec<Felt>>();
249251

250-
run_reduced_mul_test(input, &a_split, &b_split)
252+
run_reduced_mul_test(&a_split, &b_split)
251253
}
252254

253255
#[allow(dead_code)]
254256
#[allow(clippy::result_large_err)]
255-
fn test_reduced_mul_parameterized(input: &str) -> OsPythonTestResult {
257+
fn test_reduced_mul_parameterized() -> OsPythonTestResult {
256258
let max_value = Felt::from(REDUCED_MUL_LIMB_BOUND - 1);
257259
let min_value = Felt::from(-REDUCED_MUL_LIMB_BOUND + 1);
258260
let values: [([Felt; 3], [Felt; 3]); 4] = [
@@ -263,16 +265,16 @@ fn test_reduced_mul_parameterized(input: &str) -> OsPythonTestResult {
263265
];
264266
for (a_split, b_split) in values {
265267
info!("Testing `reduced_mul` with a = {a_split:?}, b = {b_split:?}");
266-
run_reduced_mul_test(input, &a_split, &b_split)?;
268+
run_reduced_mul_test(&a_split, &b_split)?;
267269
}
268270

269271
Ok("".to_string())
270272
}
271273

272274
#[allow(clippy::result_large_err)]
273-
fn test_bls_prime_value(input: &str) -> OsPythonTestResult {
275+
fn test_bls_prime_value() -> OsPythonTestResult {
274276
let entrypoint = None;
275-
let program = Program::from_bytes(input.as_bytes(), entrypoint).unwrap();
277+
let program = Program::from_bytes(OS_PROGRAM_BYTES, entrypoint).unwrap();
276278
let actual_split_bls_prime: [Felt; 3] = array::from_fn(|i| {
277279
*program
278280
.constants

crates/starknet_committer_and_os_cli/src/os_cli/tests/python_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ impl PythonTestRunner for OsPythonTestRunner {
3636
#[allow(clippy::result_large_err)]
3737
async fn run(&self, input: Option<&str>) -> OsPythonTestResult {
3838
match self {
39-
Self::AliasesTest => aliases_test(Self::non_optional_input(input)?),
40-
Self::BlsFieldTest => test_bls_field(Self::non_optional_input(input)?),
39+
Self::AliasesTest => aliases_test(),
40+
Self::BlsFieldTest => test_bls_field(),
4141
Self::InputDeserialization => input_deserialization(Self::non_optional_input(input)?),
4242
Self::EncodeFelts => {
4343
let felts: Vec<Felt> = serde_json::from_str(Self::non_optional_input(input)?)?;

crates/starknet_committer_and_os_cli/src/os_cli/tests/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub static DEFAULT_PRIME: LazyLock<BigInt> = LazyLock::new(|| {
2727
#[allow(clippy::result_large_err)]
2828
pub(crate) fn test_cairo_function(
2929
runner_config: &EntryPointRunnerConfig,
30-
program_str: &str,
30+
program_bytes: &[u8],
3131
function_name: &str,
3232
explicit_args: &[EndpointArg],
3333
implicit_args: &[ImplicitArg],
@@ -37,7 +37,7 @@ pub(crate) fn test_cairo_function(
3737
) -> OsPythonTestResult {
3838
run_cairo_function_and_check_result(
3939
runner_config,
40-
program_str,
40+
program_bytes,
4141
function_name,
4242
explicit_args,
4343
implicit_args,

crates/starknet_os/src/test_utils/cairo_runner.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,11 @@ fn extract_builtins_from_implicit_args(
301301
// TODO(Amos): Add builtins properly once the VM allows loading an entrypoint's builtins.
302302
// In addition, pass program as struct and add hint processor as param.
303303
fn inject_builtins(
304-
program_str: &str,
304+
program_bytes: &[u8],
305305
implicit_args: &[ImplicitArg],
306306
) -> Cairo0EntryPointRunnerResult<Program> {
307307
let program_builtins = extract_builtins_from_implicit_args(implicit_args)?;
308+
let program_str = std::str::from_utf8(program_bytes).unwrap();
308309
let mut program_dict: HashMap<String, Value> =
309310
serde_json::from_str(program_str).map_err(Cairo0EntryPointRunnerError::ProgramSerde)?;
310311
program_dict.insert(
@@ -554,7 +555,7 @@ fn get_return_values(
554555
/// usage, unless the builtin is the output builtin, in which case the arg is the output.
555556
pub fn run_cairo_0_entry_point(
556557
runner_config: &EntryPointRunnerConfig,
557-
program_str: &str,
558+
program_bytes: &[u8],
558559
entrypoint: &str,
559560
explicit_args: &[EndpointArg],
560561
implicit_args: &[ImplicitArg],
@@ -567,7 +568,7 @@ pub fn run_cairo_0_entry_point(
567568
entrypoint = format!("__main__.{entrypoint}");
568569
}
569570

570-
let program = inject_builtins(program_str, implicit_args)?;
571+
let program = inject_builtins(program_bytes, implicit_args)?;
571572
info!("Successfully injected builtins into program.");
572573

573574
let (state_reader, os_hints_config, os_state_input) = (None, None, None);

crates/starknet_os/src/test_utils/cairo_runner_test.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ use crate::test_utils::utils::run_cairo_function_and_check_result;
7070
/// let sum = number_1 + number_2;
7171
/// return (res=sum);
7272
/// }
73-
const COMPILED_DUMMY_FUNCTION: &str = include_str!("compiled_dummy_function.json");
73+
const COMPILED_DUMMY_FUNCTION_BYTES: &[u8] = include_bytes!("compiled_dummy_function.json");
7474

7575
#[test]
7676
fn test_felt_and_pointers() -> Cairo0EntryPointRunnerResult<()> {
@@ -117,7 +117,7 @@ fn test_felt_and_pointers() -> Cairo0EntryPointRunnerResult<()> {
117117
]));
118118
run_cairo_function_and_check_result(
119119
&EntryPointRunnerConfig::default(),
120-
COMPILED_DUMMY_FUNCTION,
120+
COMPILED_DUMMY_FUNCTION_BYTES,
121121
"pass_felt_and_pointers",
122122
&[number.into(), array, tuple, simple_struct, compound_struct],
123123
&[],
@@ -172,7 +172,7 @@ fn test_tuples_and_structs() -> Cairo0EntryPointRunnerResult<()> {
172172
]));
173173
run_cairo_function_and_check_result(
174174
&EntryPointRunnerConfig::default(),
175-
COMPILED_DUMMY_FUNCTION,
175+
COMPILED_DUMMY_FUNCTION_BYTES,
176176
"pass_structs_and_tuples",
177177
&[tuple, named_tuple, simple_struct, compound_struct],
178178
&[],
@@ -204,7 +204,7 @@ fn test_implicit_args() -> Cairo0EntryPointRunnerResult<()> {
204204
EntryPointRunnerConfig { layout: LayoutName::all_cairo, ..Default::default() };
205205
run_cairo_function_and_check_result(
206206
&entrypoint_runner_config,
207-
COMPILED_DUMMY_FUNCTION,
207+
COMPILED_DUMMY_FUNCTION_BYTES,
208208
"pass_implicit_args",
209209
&[number_1.into(), number_2.into()],
210210
&[

crates/starknet_os/src/test_utils/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::test_utils::cairo_runner::{
1616
#[allow(clippy::too_many_arguments)]
1717
pub fn run_cairo_function_and_check_result(
1818
runner_config: &EntryPointRunnerConfig,
19-
program_str: &str,
19+
program_bytes: &[u8],
2020
function_name: &str,
2121
explicit_args: &[EndpointArg],
2222
implicit_args: &[ImplicitArg],
@@ -26,7 +26,7 @@ pub fn run_cairo_function_and_check_result(
2626
) -> Cairo0EntryPointRunnerResult<()> {
2727
let (actual_implicit_retdata, actual_explicit_retdata, _) = run_cairo_0_entry_point(
2828
runner_config,
29-
program_str,
29+
program_bytes,
3030
function_name,
3131
explicit_args,
3232
implicit_args,

0 commit comments

Comments
 (0)