Skip to content

Commit 32d0226

Browse files
starknet_os: run tests directly (#7675)
1 parent 6ecf38b commit 32d0226

File tree

12 files changed

+135
-200
lines changed

12 files changed

+135
-200
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/starknet_committer_and_os_cli/Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ cairo-vm = { workspace = true, features = [
2828
"cairo-0-data-availability-hints",
2929
"cairo-0-secp-hints",
3030
] }
31-
# Should be moved under `testing` feature, when it exists.
32-
num-bigint = { workspace = true, features = ["rand"] }
33-
# Should be moved under `testing` feature, when it exists.
34-
num-integer.workspace = true
3531
clap = { workspace = true, features = ["cargo", "derive"] }
3632
derive_more.workspace = true
3733
ethnum.workspace = true
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
pub mod aliases;
2-
pub mod bls_field;
31
pub mod python_tests;
42
pub mod types;
5-
pub mod utils;

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@ use starknet_os::test_utils::errors::OsSpecificTestError;
33
use starknet_types_core::felt::Felt;
44

55
use crate::os_cli::commands::{validate_os_input, OsCliInput};
6-
use crate::os_cli::tests::aliases::aliases_test;
7-
use crate::os_cli::tests::bls_field::test_bls_field;
86
use crate::os_cli::tests::types::{OsPythonTestError, OsPythonTestResult};
97
use crate::shared_utils::types::{PythonTestError, PythonTestRunner};
108

119
// Enum representing different Python tests.
1210
pub enum OsPythonTestRunner {
13-
AliasesTest,
14-
BlsFieldTest,
1511
InputDeserialization,
1612
EncodeFelts,
1713
}
@@ -22,8 +18,6 @@ impl TryFrom<String> for OsPythonTestRunner {
2218

2319
fn try_from(value: String) -> Result<Self, Self::Error> {
2420
match value.as_str() {
25-
"aliases_test" => Ok(Self::AliasesTest),
26-
"bls_field_test" => Ok(Self::BlsFieldTest),
2721
"input_deserialization" => Ok(Self::InputDeserialization),
2822
"encode_felts" => Ok(Self::EncodeFelts),
2923
_ => Err(PythonTestError::UnknownTestName(value)),
@@ -36,8 +30,6 @@ impl PythonTestRunner for OsPythonTestRunner {
3630
#[allow(clippy::result_large_err)]
3731
async fn run(&self, input: Option<&str>) -> OsPythonTestResult {
3832
match self {
39-
Self::AliasesTest => aliases_test(),
40-
Self::BlsFieldTest => test_bls_field(),
4133
Self::InputDeserialization => input_deserialization(Self::non_optional_input(input)?),
4234
Self::EncodeFelts => {
4335
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: 0 additions & 81 deletions
This file was deleted.

crates/starknet_os/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,11 @@ strum_macros.workspace = true
5454
thiserror.workspace = true
5555

5656
[dev-dependencies]
57+
apollo_starknet_os_program = { workspace = true, features = ["test_programs"] }
5758
assert_matches.workspace = true
5859
blockifier = { workspace = true, features = ["testing"] }
5960
blockifier_test_utils.workspace = true
61+
ethnum.workspace = true
6062
rand.workspace = true
6163
rstest.workspace = true
6264
starknet_patricia = { workspace = true, features = ["testing"] }

crates/starknet_os/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ pub mod runner;
77
pub mod syscall_handler_utils;
88
#[cfg(any(test, feature = "testing"))]
99
pub mod test_utils;
10+
#[cfg(test)]
11+
pub(crate) mod tests;
1012
pub mod vm_utils;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod cairo_runner;
22
pub mod errors;
3+
#[cfg(test)]
34
pub mod utils;

crates/starknet_os/src/test_utils/utils.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
use std::any::Any;
22
use std::collections::HashMap;
3+
use std::sync::LazyLock;
34

5+
use ethnum::U256;
6+
use num_bigint::{BigInt, Sign};
7+
use rand::rngs::StdRng;
8+
use rand::SeedableRng;
49
use starknet_types_core::felt::Felt;
510

11+
use crate::hints::hint_implementation::kzg::utils::BASE;
612
use crate::test_utils::cairo_runner::{
713
run_cairo_0_entry_point,
814
Cairo0EntryPointRunnerResult,
@@ -55,3 +61,65 @@ pub fn create_squashed_cairo_dict(
5561
}
5662
PointerArg::Composed(squashed_dict)
5763
}
64+
65+
// 2**251 + 17 * 2**192 + 1
66+
pub static DEFAULT_PRIME: LazyLock<BigInt> = LazyLock::new(|| {
67+
BigInt::from_bytes_be(
68+
Sign::Plus,
69+
&(U256::from(2_u32).pow(251) + 17 * U256::from(2_u32).pow(192) + 1).to_be_bytes(),
70+
)
71+
});
72+
73+
#[allow(clippy::too_many_arguments, dead_code)]
74+
pub(crate) fn test_cairo_function(
75+
runner_config: &EntryPointRunnerConfig,
76+
program_bytes: &[u8],
77+
function_name: &str,
78+
explicit_args: &[EndpointArg],
79+
implicit_args: &[ImplicitArg],
80+
expected_explicit_retdata: &[EndpointArg],
81+
expected_implicit_retdata: &[EndpointArg],
82+
hint_locals: HashMap<String, Box<dyn Any>>,
83+
) {
84+
run_cairo_function_and_check_result(
85+
runner_config,
86+
program_bytes,
87+
function_name,
88+
explicit_args,
89+
implicit_args,
90+
expected_explicit_retdata,
91+
expected_implicit_retdata,
92+
hint_locals,
93+
)
94+
.unwrap();
95+
}
96+
97+
#[allow(dead_code)]
98+
pub(crate) fn seeded_random_prng() -> StdRng {
99+
StdRng::seed_from_u64(42)
100+
}
101+
102+
/// Returns the lift of the given field element, val, as a `BigInt` in the range
103+
/// (-prime/2, prime/2).
104+
// TODO(Amos): Use cairo VM version if it is made public:
105+
// https://github.com/lambdaclass/cairo-vm/blob/052e7cef977b336305c869fccbf24e1794b116ff/vm/src/hint_processor/builtin_hint_processor/kzg_da/mod.rs#L90
106+
fn as_int(val: &Felt, prime: &BigInt) -> BigInt {
107+
let val = val.to_bigint();
108+
if val < (prime / BigInt::from(2)) {
109+
return val.clone();
110+
}
111+
val - prime
112+
}
113+
114+
/// Takes a BigInt3 struct represented by the limbs (d0, d1, d2) of
115+
/// and reconstructs the corresponding integer (see split_bigint3()).
116+
/// Note that the limbs do not have to be in the range [0, BASE).
117+
/// Prime is used to handle negative values of the limbs.
118+
// TODO(Amos): Use cairo VM version if it is made public:
119+
// https://github.com/lambdaclass/cairo-vm/blob/052e7cef977b336305c869fccbf24e1794b116ff/vm/src/hint_processor/builtin_hint_processor/kzg_da/mod.rs#L99
120+
pub fn pack_bigint3(limbs: &[Felt]) -> BigInt {
121+
assert!(limbs.len() == 3, "Expected 3 limbs, got {}", limbs.len());
122+
limbs.iter().enumerate().fold(BigInt::ZERO, |acc, (i, &limb)| {
123+
acc + as_int(&limb, &DEFAULT_PRIME) * BASE.pow(i.try_into().unwrap())
124+
})
125+
}

crates/starknet_os/src/tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub(crate) mod aliases;
2+
pub(crate) mod bls_field;

0 commit comments

Comments
 (0)