Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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(),
Expand Down
44 changes: 23 additions & 21 deletions crates/starknet_committer_and_os_cli/src/os_cli/tests/bls_field.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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())
}

Expand All @@ -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())),
Expand All @@ -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,
Expand All @@ -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);
Expand All @@ -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,
Expand All @@ -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(),
Expand All @@ -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,
Expand All @@ -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();

Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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::<Vec<Felt>>();

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] = [
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Felt> = serde_json::from_str(Self::non_optional_input(input)?)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub static DEFAULT_PRIME: LazyLock<BigInt> = 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],
Expand All @@ -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,
Expand Down
7 changes: 4 additions & 3 deletions crates/starknet_os/src/test_utils/cairo_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Program> {
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<String, Value> =
serde_json::from_str(program_str).map_err(Cairo0EntryPointRunnerError::ProgramSerde)?;
program_dict.insert(
Expand Down Expand Up @@ -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],
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions crates/starknet_os/src/test_utils/cairo_runner_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down Expand Up @@ -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],
&[],
Expand Down Expand Up @@ -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],
&[],
Expand Down Expand Up @@ -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()],
&[
Expand Down
4 changes: 2 additions & 2 deletions crates/starknet_os/src/test_utils/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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,
Expand Down
Loading