Skip to content

Commit c2df2ac

Browse files
starknet_os: migrate test_compiled_class_hash_basic (starkware-libs#9780)
Signed-off-by: Dori Medini <[email protected]>
1 parent 9d36b94 commit c2df2ac

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

Cargo.lock

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

crates/starknet_os/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ apollo_starknet_os_program = { workspace = true, features = ["test_programs"] }
6565
assert_matches.workspace = true
6666
blockifier = { workspace = true, features = ["testing"] }
6767
blockifier_test_utils.workspace = true
68+
cairo-lang-utils.workspace = true
6869
ethnum.workspace = true
6970
expect-test.workspace = true
7071
itertools.workspace = true

crates/starknet_os/src/hints/hint_implementation/compiled_class/compiled_class_test.rs

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@ use blockifier::execution::contract_class::{EntryPointV1, EntryPointsByType, Nes
99
use blockifier::test_utils::contracts::FeatureContractTrait;
1010
use blockifier_test_utils::cairo_versions::{CairoVersion, RunnableCairo1};
1111
use blockifier_test_utils::contracts::FeatureContract;
12-
use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass;
12+
use cairo_lang_starknet_classes::casm_contract_class::{
13+
CasmContractClass,
14+
CasmContractEntryPoint,
15+
CasmContractEntryPoints,
16+
};
1317
use cairo_lang_starknet_classes::NestedIntList;
18+
use cairo_lang_utils::bigint::BigUintAsHex;
1419
use cairo_vm::any_box;
1520
use cairo_vm::types::builtin_name::BuiltinName;
1621
use cairo_vm::types::layout_name::LayoutName;
1722
use cairo_vm::types::relocatable::MaybeRelocatable;
1823
use cairo_vm::vm::runners::cairo_runner::ExecutionResources;
1924
use expect_test::{expect, Expect};
2025
use log::info;
26+
use num_bigint::BigUint;
2127
use rstest::rstest;
2228
use starknet_api::contract_class::compiled_class_hash::{
2329
HashVersion,
@@ -37,6 +43,7 @@ use crate::test_utils::cairo_runner::{
3743
ImplicitArg,
3844
ValueArg,
3945
};
46+
use crate::test_utils::utils::DEFAULT_PRIME;
4047
use crate::vm_utils::LoadCairoObject;
4148

4249
// V1 (Poseidon) HASH CONSTS
@@ -180,6 +187,42 @@ impl HashVersionTestSpec for HashVersion {
180187
}
181188
}
182189

190+
fn get_dummy_compiled_class(contract_segmentation: bool) -> CasmContractClass {
191+
CasmContractClass {
192+
prime: DEFAULT_PRIME.clone().to_biguint().unwrap(),
193+
compiler_version: "".into(),
194+
bytecode: (1u8..=10).map(BigUintAsHex::from).collect(),
195+
bytecode_segment_lengths: Some(if contract_segmentation {
196+
NestedIntList::Node(vec![
197+
NestedIntList::Leaf(3),
198+
NestedIntList::Node(vec![
199+
NestedIntList::Leaf(1),
200+
NestedIntList::Leaf(1),
201+
NestedIntList::Node(vec![NestedIntList::Leaf(1)]),
202+
]),
203+
NestedIntList::Leaf(4),
204+
])
205+
} else {
206+
NestedIntList::Leaf(10)
207+
}),
208+
entry_points_by_type: CasmContractEntryPoints {
209+
external: vec![CasmContractEntryPoint {
210+
selector: BigUint::from(1u8),
211+
offset: 1,
212+
builtins: vec!["237".into()],
213+
}],
214+
constructor: vec![CasmContractEntryPoint {
215+
selector: BigUint::from(5u8),
216+
offset: 0,
217+
builtins: vec![],
218+
}],
219+
l1_handler: vec![],
220+
},
221+
hints: vec![],
222+
pythonic_hints: None,
223+
}
224+
}
225+
183226
/// Runs the compiled class hash entry point for the given contract class,
184227
/// with the specified load_full_contract flag and hash version.
185228
/// Returns the execution resources and the computed hash.
@@ -267,6 +310,32 @@ fn run_compiled_class_hash_entry_point(
267310
(actual_execution_resources, hash_computed_by_cairo)
268311
}
269312

313+
#[rstest]
314+
#[case::no_segmentation(
315+
false,
316+
"0xB268995DD0EE80DEBFB8718852750B5FD22082D0C729121C48A0487A4D2F64",
317+
16
318+
)]
319+
#[case::segmentation(true, "0x5517AD8471C9AA4D1ADD31837240DEAD9DC6653854169E489A813DB4376BE9C", 28)]
320+
fn test_compiled_class_hash_basic(
321+
#[case] segmentation: bool,
322+
#[case] expected_hash: &str,
323+
#[case] expected_n_poseidons: usize,
324+
) {
325+
let (resources, compiled_class_hash) = run_compiled_class_hash_entry_point(
326+
&get_dummy_compiled_class(segmentation),
327+
// TODO(Dori): Instead of setting load_full_contract to true, we should mark all segments
328+
// as accessed.
329+
true,
330+
&HashVersion::V1,
331+
);
332+
assert_eq!(compiled_class_hash, Felt::from_hex_unchecked(expected_hash));
333+
assert_eq!(
334+
*resources.builtin_instance_counter.get(&BuiltinName::poseidon).unwrap(),
335+
expected_n_poseidons
336+
);
337+
}
338+
270339
#[rstest]
271340
fn test_compiled_class_hash(
272341
#[values(true, false)] load_full_contract: bool,

0 commit comments

Comments
 (0)