Skip to content

Commit b4da5e8

Browse files
starknet_os: os resources test - add sha512
1 parent fe05b95 commit b4da5e8

3 files changed

Lines changed: 36 additions & 23 deletions

File tree

crates/blockifier_test_utils/resources/feature_contracts/cairo1/os_resources_test_contract.cairo

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
mod OsResourcesTestContract {
44
use box::BoxTrait;
55
use core::sha256::{SHA256_INITIAL_STATE, sha256_state_handle_init};
6+
use core::sha512::{SHA512_INITIAL_STATE, sha512_state_handle_init};
67
use starknet::info::SyscallResultTrait;
78
use starknet::secp256_trait::Secp256Trait;
89
use starknet::secp256k1::{
@@ -16,7 +17,7 @@ mod OsResourcesTestContract {
1617
use starknet::syscalls::{
1718
call_contract_syscall, deploy_syscall, emit_event_syscall, get_execution_info_v3_syscall,
1819
keccak_syscall, library_call_syscall, replace_class_syscall, send_message_to_l1_syscall,
19-
sha256_process_block_syscall,
20+
sha256_process_block_syscall, sha512_process_block_syscall,
2021
};
2122
use starknet::{ClassHash, ContractAddress, get_block_hash_syscall, get_class_hash_at_syscall};
2223

@@ -136,6 +137,11 @@ mod OsResourcesTestContract {
136137
let mut state = sha256_state_handle_init(BoxTrait::new(SHA256_INITIAL_STATE));
137138
let _ = sha256_process_block_syscall(state, input).unwrap_syscall();
138139

140+
// sha256.
141+
let mut input = BoxTrait::new([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
142+
let mut state = sha512_state_handle_init(BoxTrait::new(SHA512_INITIAL_STATE));
143+
let _ = sha512_process_block_syscall(state, input).unwrap_syscall();
144+
139145
// replace class syscall.
140146
replace_class_syscall(stable_class_hash).unwrap_syscall();
141147

crates/starknet_os/src/test_utils.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ pub mod validations;
1818
#[path = "resource_utils_test.rs"]
1919
mod resource_utils_test;
2020

21+
fn scale_builtin_cells(resources: &ExecutionResources) -> ExecutionResources {
22+
let mut scaled_resources = resources.clone();
23+
scaled_resources.builtin_instance_counter = scaled_resources
24+
.builtin_instance_counter
25+
.iter()
26+
.map(|(builtin, count)| (*builtin, count / BUILTIN_INSTANCE_SIZES.get(builtin).unwrap()))
27+
.collect();
28+
scaled_resources
29+
}
30+
2131
// Resources consumed by the SHA-256 batch phase, separated into linear and constant factors.
2232
pub const SHA256_BATCH_SIZE: usize = 7;
2333
pub static SHA256_BATCH_RESOURCES_LINEAR_UNSCALED: LazyLock<ExecutionResources> =
@@ -29,15 +39,8 @@ pub static SHA256_BATCH_RESOURCES_LINEAR_UNSCALED: LazyLock<ExecutionResources>
2939
(BuiltinName::range_check, 448),
3040
]),
3141
});
32-
pub static SHA256_BATCH_RESOURCES_LINEAR: LazyLock<ExecutionResources> = LazyLock::new(|| {
33-
let mut resources = SHA256_BATCH_RESOURCES_LINEAR_UNSCALED.clone();
34-
resources.builtin_instance_counter = resources
35-
.builtin_instance_counter
36-
.iter()
37-
.map(|(builtin, count)| (*builtin, count / BUILTIN_INSTANCE_SIZES.get(builtin).unwrap()))
38-
.collect();
39-
resources
40-
});
42+
pub static SHA256_BATCH_RESOURCES_LINEAR: LazyLock<ExecutionResources> =
43+
LazyLock::new(|| scale_builtin_cells(&*SHA256_BATCH_RESOURCES_LINEAR_UNSCALED));
4144
pub static SHA256_BATCH_RESOURCES_CONSTANT: LazyLock<ExecutionResources> =
4245
LazyLock::new(|| ExecutionResources {
4346
n_steps: 49,
@@ -50,21 +53,17 @@ pub static SHA256_BATCH_RESOURCES_CONSTANT: LazyLock<ExecutionResources> =
5053

5154
// Resources consumed by the SHA-512 batch phase, separated into linear and constant factors.
5255
pub const SHA512_BATCH_SIZE: usize = 3;
53-
pub static SHA512_BATCH_RESOURCES_LINEAR: LazyLock<ExecutionResources> =
56+
pub static SHA512_BATCH_RESOURCES_LINEAR_UNSCALED: LazyLock<ExecutionResources> =
5457
LazyLock::new(|| ExecutionResources {
5558
n_steps: 13710,
5659
n_memory_holes: 0,
5760
builtin_instance_counter: BTreeMap::from([
58-
(
59-
BuiltinName::bitwise,
60-
9960 / BUILTIN_INSTANCE_SIZES.get(&BuiltinName::bitwise).unwrap(),
61-
),
62-
(
63-
BuiltinName::range_check,
64-
192 / BUILTIN_INSTANCE_SIZES.get(&BuiltinName::range_check).unwrap(),
65-
),
61+
(BuiltinName::bitwise, 9960),
62+
(BuiltinName::range_check, 192),
6663
]),
6764
});
65+
pub static SHA512_BATCH_RESOURCES_LINEAR: LazyLock<ExecutionResources> =
66+
LazyLock::new(|| scale_builtin_cells(&*SHA512_BATCH_RESOURCES_LINEAR_UNSCALED));
6867
pub static SHA512_BATCH_RESOURCES_CONSTANT: LazyLock<ExecutionResources> =
6968
LazyLock::new(|| ExecutionResources {
7069
n_steps: 49,

crates/starknet_os_flow_tests/src/os_resources_test.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ use starknet_api::transaction::{L2ToL1Payload, MessageToL1};
2828
use starknet_api::versioned_constants_logic::VersionedConstantsTrait;
2929
use starknet_api::{calldata, declare_tx_args, invoke_tx_args};
3030
use starknet_os::hint_processor::os_logger::ResourceFinalizer;
31-
use starknet_os::test_utils::{SHA256_BATCH_RESOURCES_LINEAR_UNSCALED, SHA256_BATCH_SIZE};
31+
use starknet_os::test_utils::{
32+
SHA256_BATCH_RESOURCES_LINEAR_UNSCALED,
33+
SHA256_BATCH_SIZE,
34+
SHA512_BATCH_RESOURCES_LINEAR_UNSCALED,
35+
SHA512_BATCH_SIZE,
36+
};
3237
use starknet_types_core::felt::Felt;
3338
use strum::IntoEnumIterator;
3439

@@ -49,7 +54,7 @@ use crate::test_manager::{
4954
use crate::tests::NON_TRIVIAL_RESOURCE_BOUNDS;
5055

5156
// TODO(Dori): Delete this, or at least reduce it to a minimal set of unmeasurable syscalls.
52-
const UNMEASURABLE_SYSCALLS: [Selector; 13] = [
57+
const UNMEASURABLE_SYSCALLS: [Selector; 12] = [
5358
Selector::DelegateCall,
5459
Selector::DelegateL1Handler,
5560
Selector::GetBlockNumber,
@@ -59,7 +64,6 @@ const UNMEASURABLE_SYSCALLS: [Selector; 13] = [
5964
Selector::GetSequencerAddress,
6065
Selector::GetTxInfo,
6166
Selector::GetTxSignature,
62-
Selector::Sha512ProcessBlock,
6367
Selector::LibraryCallL1Handler,
6468
Selector::StorageRead,
6569
Selector::StorageWrite,
@@ -87,7 +91,8 @@ static SYSCALLS_WITH_LINEAR_FACTOR: LazyLock<HashMap<Selector, usize>> = LazyLoc
8791
/// Syscalls that are implemented using virtual builtins. Such syscalls have their "heavy lifting"
8892
/// executed after the execute_syscalls part of the OS, so the consumed resources are not captured
8993
/// by the OsLogger.
90-
const SYSCALLS_WITH_VIRTUAL_BUILTINS: [Selector; 1] = [Selector::Sha256ProcessBlock];
94+
const SYSCALLS_WITH_VIRTUAL_BUILTINS: [Selector; 2] =
95+
[Selector::Sha256ProcessBlock, Selector::Sha512ProcessBlock];
9196

9297
/// Expected syscalls in the fee transfer call. Should be removed from the list of syscalls during
9398
/// measurement iteration - only the syscalls called during __execute__ should be measured.
@@ -117,6 +122,9 @@ fn update_resources_for_virtual_builtin_syscall(
117122
Selector::Sha256ProcessBlock => (&measured_base
118123
+ &SHA256_BATCH_RESOURCES_LINEAR_UNSCALED.div_ceil(SHA256_BATCH_SIZE))
119124
.filter_unused_builtins(),
125+
Selector::Sha512ProcessBlock => (&measured_base
126+
+ &SHA512_BATCH_RESOURCES_LINEAR_UNSCALED.div_ceil(SHA512_BATCH_SIZE))
127+
.filter_unused_builtins(),
120128
_ => panic!("Resource update not implemented for virtual builtin syscall: {selector:?}."),
121129
}
122130
}

0 commit comments

Comments
 (0)