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
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions crates/starknet_committer_and_os_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ cairo-vm = { workspace = true, features = [
"cairo-0-data-availability-hints",
"cairo-0-secp-hints",
] }
# Should be moved under `testing` feature, when it exists.
num-bigint = { workspace = true, features = ["rand"] }
# Should be moved under `testing` feature, when it exists.
num-integer.workspace = true
clap = { workspace = true, features = ["cargo", "derive"] }
derive_more.workspace = true
ethnum.workspace = true
Expand Down
3 changes: 0 additions & 3 deletions crates/starknet_committer_and_os_cli/src/os_cli/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
pub mod aliases;
pub mod bls_field;
pub mod python_tests;
pub mod types;
pub mod utils;
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ use starknet_os::test_utils::errors::OsSpecificTestError;
use starknet_types_core::felt::Felt;

use crate::os_cli::commands::{validate_os_input, OsCliInput};
use crate::os_cli::tests::aliases::aliases_test;
use crate::os_cli::tests::bls_field::test_bls_field;
use crate::os_cli::tests::types::{OsPythonTestError, OsPythonTestResult};
use crate::shared_utils::types::{PythonTestError, PythonTestRunner};

// Enum representing different Python tests.
pub enum OsPythonTestRunner {
AliasesTest,
BlsFieldTest,
InputDeserialization,
EncodeFelts,
}
Expand All @@ -22,8 +18,6 @@ impl TryFrom<String> for OsPythonTestRunner {

fn try_from(value: String) -> Result<Self, Self::Error> {
match value.as_str() {
"aliases_test" => Ok(Self::AliasesTest),
"bls_field_test" => Ok(Self::BlsFieldTest),
"input_deserialization" => Ok(Self::InputDeserialization),
"encode_felts" => Ok(Self::EncodeFelts),
_ => Err(PythonTestError::UnknownTestName(value)),
Expand All @@ -36,8 +30,6 @@ impl PythonTestRunner for OsPythonTestRunner {
#[allow(clippy::result_large_err)]
async fn run(&self, input: Option<&str>) -> OsPythonTestResult {
match self {
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
81 changes: 0 additions & 81 deletions crates/starknet_committer_and_os_cli/src/os_cli/tests/utils.rs

This file was deleted.

2 changes: 2 additions & 0 deletions crates/starknet_os/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ strum_macros.workspace = true
thiserror.workspace = true

[dev-dependencies]
apollo_starknet_os_program = { workspace = true, features = ["test_programs"] }
assert_matches.workspace = true
blockifier = { workspace = true, features = ["testing"] }
blockifier_test_utils.workspace = true
ethnum.workspace = true
rand.workspace = true
rstest.workspace = true
starknet_patricia = { workspace = true, features = ["testing"] }
Expand Down
2 changes: 2 additions & 0 deletions crates/starknet_os/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ pub mod runner;
pub mod syscall_handler_utils;
#[cfg(any(test, feature = "testing"))]
pub mod test_utils;
#[cfg(test)]
pub(crate) mod tests;
pub mod vm_utils;
1 change: 1 addition & 0 deletions crates/starknet_os/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod cairo_runner;
pub mod errors;
#[cfg(test)]
pub mod utils;
68 changes: 68 additions & 0 deletions crates/starknet_os/src/test_utils/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
use std::any::Any;
use std::collections::HashMap;
use std::sync::LazyLock;

use ethnum::U256;
use num_bigint::{BigInt, Sign};
use rand::rngs::StdRng;
use rand::SeedableRng;
use starknet_types_core::felt::Felt;

use crate::hints::hint_implementation::kzg::utils::BASE;
use crate::test_utils::cairo_runner::{
run_cairo_0_entry_point,
Cairo0EntryPointRunnerResult,
Expand Down Expand Up @@ -55,3 +61,65 @@ pub fn create_squashed_cairo_dict(
}
PointerArg::Composed(squashed_dict)
}

// 2**251 + 17 * 2**192 + 1
pub static DEFAULT_PRIME: LazyLock<BigInt> = LazyLock::new(|| {
BigInt::from_bytes_be(
Sign::Plus,
&(U256::from(2_u32).pow(251) + 17 * U256::from(2_u32).pow(192) + 1).to_be_bytes(),
)
});

#[allow(clippy::too_many_arguments, dead_code)]
pub(crate) fn test_cairo_function(
runner_config: &EntryPointRunnerConfig,
program_bytes: &[u8],
function_name: &str,
explicit_args: &[EndpointArg],
implicit_args: &[ImplicitArg],
expected_explicit_retdata: &[EndpointArg],
expected_implicit_retdata: &[EndpointArg],
hint_locals: HashMap<String, Box<dyn Any>>,
) {
run_cairo_function_and_check_result(
runner_config,
program_bytes,
function_name,
explicit_args,
implicit_args,
expected_explicit_retdata,
expected_implicit_retdata,
hint_locals,
)
.unwrap();
}

#[allow(dead_code)]
pub(crate) fn seeded_random_prng() -> StdRng {
StdRng::seed_from_u64(42)
}

/// Returns the lift of the given field element, val, as a `BigInt` in the range
/// (-prime/2, prime/2).
// TODO(Amos): Use cairo VM version if it is made public:
// https://github.com/lambdaclass/cairo-vm/blob/052e7cef977b336305c869fccbf24e1794b116ff/vm/src/hint_processor/builtin_hint_processor/kzg_da/mod.rs#L90
fn as_int(val: &Felt, prime: &BigInt) -> BigInt {
let val = val.to_bigint();
if val < (prime / BigInt::from(2)) {
return val.clone();
}
val - prime
}

/// Takes a BigInt3 struct represented by the limbs (d0, d1, d2) of
/// and reconstructs the corresponding integer (see split_bigint3()).
/// Note that the limbs do not have to be in the range [0, BASE).
/// Prime is used to handle negative values of the limbs.
// TODO(Amos): Use cairo VM version if it is made public:
// https://github.com/lambdaclass/cairo-vm/blob/052e7cef977b336305c869fccbf24e1794b116ff/vm/src/hint_processor/builtin_hint_processor/kzg_da/mod.rs#L99
pub fn pack_bigint3(limbs: &[Felt]) -> BigInt {
assert!(limbs.len() == 3, "Expected 3 limbs, got {}", limbs.len());
limbs.iter().enumerate().fold(BigInt::ZERO, |acc, (i, &limb)| {
acc + as_int(&limb, &DEFAULT_PRIME) * BASE.pow(i.try_into().unwrap())
})
}
2 changes: 2 additions & 0 deletions crates/starknet_os/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub(crate) mod aliases;
pub(crate) mod bls_field;
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
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;

use crate::os_cli::tests::types::OsPythonTestResult;
use crate::os_cli::tests::utils::test_cairo_function;

use crate::test_utils::cairo_runner::EntryPointRunnerConfig;
use crate::test_utils::utils::test_cairo_function;
// TODO(Nimrod): Move this next to the stateful compression hints implementation.
// 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() -> OsPythonTestResult {
info!("Testing `test_constants`...");
test_constants()?;
Ok("".to_string())
}

#[allow(clippy::result_large_err)]
fn test_constants() -> OsPythonTestResult {
#[test]
fn test_constants() {
let max_non_compressed_contract_address = 15;
let alias_counter_storage_key = 0;
let initial_available_alias = 128;
Expand Down
Loading
Loading