Skip to content

Commit 1c0a5da

Browse files
starknet_os: os resources test - support virtual builtins, add sha256 (#14141)
1 parent e273e9a commit 1c0a5da

11 files changed

Lines changed: 81 additions & 43 deletions

File tree

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/constants.cairo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_1 = (
7272
0x06ecb73d21c7d98ddd4148f5bcd91cc2747c65364245fbf32a63b05cca1685c2
7373
);
7474
const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_2 = (
75-
0x0552611434744d19772e04249521f0a3d00d85906c9a600082aaa9339788f8d5
75+
0x04373ade67a35a6ae0466730e5807b828a5db813010d6070bb1db85f99682dcb
7676
);
7777
const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_LEN = 3;
7878

@@ -137,7 +137,7 @@ const SECP256R1_NEW_GAS_COST = 61630;
137137

138138
const KECCAK_GAS_COST = 10000;
139139
const KECCAK_ROUND_COST_GAS_COST = 165137;
140-
const SHA256_PROCESS_BLOCK_GAS_COST = 841295;
140+
const SHA256_PROCESS_BLOCK_GAS_COST = 839995;
141141
const SHA512_PROCESS_BLOCK_GAS_COST = 2413810;
142142

143143
// Cairo 1.0 error codes.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"os": "0x30d60f71b7401f270197f0d50658849403bc9d786c2f7b38535cf7f9a389f66",
3-
"virtual_os": "0x552611434744d19772e04249521f0a3d00d85906c9a600082aaa9339788f8d5",
2+
"os": "0x3b23e69c999017f0a08ca8080de329bf80922aaa8bd364f1bf3b5445b38f989",
3+
"virtual_os": "0x4373ade67a35a6ae0466730e5807b828a5db813010d6070bb1db85f99682dcb",
44
"aggregator": "0x700786d51b3854af43d8e12180380bda3029be6c1767e007858de6ca2edac40",
55
"aggregator_with_prefix": "0xe08d300e3f5996e43d6d7cc5a20068e0e58cf1309089f2348317ac580f6c1f"
66
}

crates/blockifier/resources/blockifier_versioned_constants_0_14_4.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@
129129
"allowed_virtual_os_program_hashes": [
130130
"0x3e98c2d7703b03a7edb73ed7f075f97f1dcbaa8f717cdf6e1a57bf058265473",
131131
"0x6ecb73d21c7d98ddd4148f5bcd91cc2747c65364245fbf32a63b05cca1685c2",
132-
"0x552611434744d19772e04249521f0a3d00d85906c9a600082aaa9339788f8d5"
132+
"0x4373ade67a35a6ae0466730e5807b828a5db813010d6070bb1db85f99682dcb"
133133
],
134134
"constructor_entry_point_selector": "0x28ffe4ff0f226a9107253e17a904099aa4f63a02a5621de0576e5aa71bc5194",
135135
"default_entry_point_selector": "0x0",
@@ -462,7 +462,7 @@
462462
}
463463
},
464464
"Sha256ProcessBlock": {
465-
"n_steps": 1867,
465+
"n_steps": 1854,
466466
"n_memory_holes": 0,
467467
"builtin_instance_counter": {
468468
"range_check_builtin": 65,

crates/blockifier/resources/versioned_constants_diff_regression/0.14.3_0.14.4.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
+ /os_constants/allowed_virtual_os_program_hashes/2: "0x552611434744d19772e04249521f0a3d00d85906c9a600082aaa9339788f8d5"
1+
+ /os_constants/allowed_virtual_os_program_hashes/2: "0x4373ade67a35a6ae0466730e5807b828a5db813010d6070bb1db85f99682dcb"
22
~ /os_resources/execute_syscalls/CallContract/n_steps: 901
33
~ /os_resources/execute_syscalls/EmitEvent/n_steps: 47
44
~ /os_resources/execute_syscalls/GetBlockHash/builtin_instance_counter/range_check_builtin: 3
@@ -9,3 +9,4 @@
99
~ /os_resources/execute_syscalls/KeccakRound/n_steps: 223
1010
~ /os_resources/execute_syscalls/LibraryCall/n_steps: 874
1111
~ /os_resources/execute_syscalls/MetaTxV0/constant/n_steps: 1307
12+
~ /os_resources/execute_syscalls/Sha256ProcessBlock/n_steps: 1854

crates/blockifier/src/execution/syscalls/syscall_tests/sha256.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn test_sha256(runnable_version: RunnableCairo1) {
3737
l2_to_l1_messages: [],
3838
cairo_native: false,
3939
failed: false,
40-
gas_consumed: 851745,
40+
gas_consumed: 850445,
4141
}
4242
"#]]
4343
.assert_debug_eq(&execution);

crates/blockifier/src/versioned_constants_test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ fn test_latest_no_panic() {
229229
fn test_syscall_gas_cost_calculation() {
230230
const EXPECTED_CALL_CONTRACT_GAS_COST: Expect = expect!["91360"];
231231
const EXPECTED_SECP256K1MUL_GAS_COST: Expect = expect!["8143850"];
232-
const EXPECTED_SHA256PROCESSBLOCK_GAS_COST: Expect = expect!["841295"];
232+
const EXPECTED_SHA256PROCESSBLOCK_GAS_COST: Expect = expect!["839995"];
233233

234234
let versioned_constants = VersionedConstants::latest_constants().clone();
235235

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
// Contract for measuring per-syscall OS resource costs.
22
#[starknet::contract(account)]
33
mod OsResourcesTestContract {
4+
use box::BoxTrait;
5+
use core::sha256::{SHA256_INITIAL_STATE, sha256_state_handle_init};
46
use starknet::info::SyscallResultTrait;
57
use starknet::syscalls::{
68
call_contract_syscall, deploy_syscall, emit_event_syscall, get_execution_info_v3_syscall,
7-
keccak_syscall, library_call_syscall,
9+
keccak_syscall, library_call_syscall, sha256_process_block_syscall,
810
};
911
use starknet::{ClassHash, ContractAddress, get_block_hash_syscall, get_class_hash_at_syscall};
1012

@@ -115,5 +117,10 @@ mod OsResourcesTestContract {
115117
input.append(1_u64);
116118
}
117119
keccak_syscall(input.span()).unwrap_syscall();
120+
121+
// sha256.
122+
let mut input = BoxTrait::new([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
123+
let mut state = sha256_state_handle_init(BoxTrait::new(SHA256_INITIAL_STATE));
124+
let _ = sha256_process_block_syscall(state, input).unwrap_syscall();
118125
}
119126
}

crates/starknet_os/src/hint_processor/constants.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@ use std::sync::LazyLock;
33

44
use cairo_vm::types::builtin_name::BuiltinName;
55

6-
pub(crate) static BUILTIN_INSTANCE_SIZES: LazyLock<HashMap<BuiltinName, usize>> =
7-
LazyLock::new(|| {
8-
HashMap::from([
9-
(BuiltinName::pedersen, 3),
10-
(BuiltinName::range_check, 1),
11-
(BuiltinName::ecdsa, 2),
12-
(BuiltinName::bitwise, 5),
13-
(BuiltinName::ec_op, 7),
14-
(BuiltinName::poseidon, 6),
15-
(BuiltinName::segment_arena, 3),
16-
(BuiltinName::range_check96, 1),
17-
(BuiltinName::add_mod, 7),
18-
(BuiltinName::mul_mod, 7),
19-
(BuiltinName::keccak, 16),
20-
])
21-
});
6+
pub static BUILTIN_INSTANCE_SIZES: LazyLock<HashMap<BuiltinName, usize>> = LazyLock::new(|| {
7+
HashMap::from([
8+
(BuiltinName::pedersen, 3),
9+
(BuiltinName::range_check, 1),
10+
(BuiltinName::ecdsa, 2),
11+
(BuiltinName::bitwise, 5),
12+
(BuiltinName::ec_op, 7),
13+
(BuiltinName::poseidon, 6),
14+
(BuiltinName::segment_arena, 3),
15+
(BuiltinName::range_check96, 1),
16+
(BuiltinName::add_mod, 7),
17+
(BuiltinName::mul_mod, 7),
18+
(BuiltinName::keccak, 16),
19+
])
20+
});

crates/starknet_os/src/resource_utils_test.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ use crate::test_utils::cairo_runner::{
1717
ImplicitArg,
1818
ValueArg,
1919
};
20-
use crate::test_utils::{SHA256_BATCH_RESOURCES_CONSTANT, SHA256_BATCH_RESOURCES_LINEAR};
20+
use crate::test_utils::{
21+
SHA256_BATCH_RESOURCES_CONSTANT,
22+
SHA256_BATCH_RESOURCES_LINEAR,
23+
SHA256_BATCH_SIZE,
24+
};
2125

2226
/// SHA-256 block compression: takes 8 u32 state words and 16 u32 message words, returns the
2327
/// new 8 u32 state. Wraps `sha2::compress256` (message words are big-endian per the SHA-256 spec).
@@ -98,11 +102,10 @@ fn run_finalize_sha256(number_of_blocks: usize) -> ExecutionResources {
98102
fn test_finalize_sha256() {
99103
// Sha256 batching resources has a factor that is linear in the number of rounds, and a constant
100104
// factor. Sample the execution at two points to compute both factors.
101-
let blocks_in_round = 7_usize;
102105
let number_of_blocks_1 = 8_usize;
103-
let number_of_rounds_1 = (number_of_blocks_1 - 1) / blocks_in_round + 1;
104-
let number_of_blocks_2 = number_of_blocks_1 + blocks_in_round;
105-
let number_of_rounds_2 = (number_of_blocks_2 - 1) / blocks_in_round + 1;
106+
let number_of_rounds_1 = (number_of_blocks_1 - 1) / SHA256_BATCH_SIZE + 1;
107+
let number_of_blocks_2 = number_of_blocks_1 + SHA256_BATCH_SIZE;
108+
let number_of_rounds_2 = (number_of_blocks_2 - 1) / SHA256_BATCH_SIZE + 1;
106109
let resources_1 = run_finalize_sha256(number_of_blocks_1);
107110
let resources_2 = run_finalize_sha256(number_of_blocks_2);
108111

crates/starknet_os/src/test_utils.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,25 @@ pub mod validations;
1919
mod resource_utils_test;
2020

2121
// Resources consumed by the SHA-256 batch phase, separated into linear and constant factors.
22-
pub static SHA256_BATCH_RESOURCES_LINEAR: LazyLock<ExecutionResources> =
22+
pub const SHA256_BATCH_SIZE: usize = 7;
23+
pub static SHA256_BATCH_RESOURCES_LINEAR_UNSCALED: LazyLock<ExecutionResources> =
2324
LazyLock::new(|| ExecutionResources {
2425
n_steps: 11822,
2526
n_memory_holes: 0,
2627
builtin_instance_counter: BTreeMap::from([
27-
(
28-
BuiltinName::bitwise,
29-
7800 / BUILTIN_INSTANCE_SIZES.get(&BuiltinName::bitwise).unwrap(),
30-
),
31-
(
32-
BuiltinName::range_check,
33-
448 / BUILTIN_INSTANCE_SIZES.get(&BuiltinName::range_check).unwrap(),
34-
),
28+
(BuiltinName::bitwise, 7800),
29+
(BuiltinName::range_check, 448),
3530
]),
3631
});
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+
});
3741
pub static SHA256_BATCH_RESOURCES_CONSTANT: LazyLock<ExecutionResources> =
3842
LazyLock::new(|| ExecutionResources {
3943
n_steps: 49,

0 commit comments

Comments
 (0)