Skip to content

Commit 65993f8

Browse files
starknet_os: os resources test - add keccak
1 parent 4bdb20a commit 65993f8

6 files changed

Lines changed: 58 additions & 25 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-
0x06e0e4653c9bf533daba4c89a969a78f082fd2b7ebd38912ca8138441a897bea
75+
0x050a1cd46f1a35628c7569f85218b7b8b01510cb33834c3bd00885183c5770a5
7676
);
7777
const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_LEN = 3;
7878

@@ -136,7 +136,7 @@ const SECP256R1_MUL_GAS_COST = 13511870;
136136
const SECP256R1_NEW_GAS_COST = 61630;
137137

138138
const KECCAK_GAS_COST = 10000;
139-
const KECCAK_ROUND_COST_GAS_COST = 171707;
139+
const KECCAK_ROUND_COST_GAS_COST = 175207;
140140
const SHA256_PROCESS_BLOCK_GAS_COST = 841295;
141141
const SHA512_PROCESS_BLOCK_GAS_COST = 2413810;
142142

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"os": "0x6953c7279f84f553ce738d580de5df707feef830c3ca1840829b6bbdf79db4e",
3-
"virtual_os": "0x6e0e4653c9bf533daba4c89a969a78f082fd2b7ebd38912ca8138441a897bea",
2+
"os": "0x195ad0b42c896cb80d029d349cc1b7fe182acb20c540c3dcb68256ab87ea41",
3+
"virtual_os": "0x50a1cd46f1a35628c7569f85218b7b8b01510cb33834c3bd00885183c5770a5",
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
@@ -128,7 +128,7 @@
128128
"allowed_virtual_os_program_hashes": [
129129
"0x3e98c2d7703b03a7edb73ed7f075f97f1dcbaa8f717cdf6e1a57bf058265473",
130130
"0x6ecb73d21c7d98ddd4148f5bcd91cc2747c65364245fbf32a63b05cca1685c2",
131-
"0x6e0e4653c9bf533daba4c89a969a78f082fd2b7ebd38912ca8138441a897bea"
131+
"0x50a1cd46f1a35628c7569f85218b7b8b01510cb33834c3bd00885183c5770a5"
132132
],
133133
"constructor_entry_point_selector": "0x28ffe4ff0f226a9107253e17a904099aa4f63a02a5621de0576e5aa71bc5194",
134134
"default_entry_point_selector": "0x0",
@@ -337,7 +337,7 @@
337337
"builtin_instance_counter": {}
338338
},
339339
"KeccakRound": {
340-
"n_steps": 281,
340+
"n_steps": 316,
341341
"n_memory_holes": 0,
342342
"builtin_instance_counter": {
343343
"range_check_builtin": 56,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
+ /os_constants/allowed_virtual_os_program_hashes/2: "0x6e0e4653c9bf533daba4c89a969a78f082fd2b7ebd38912ca8138441a897bea"
1+
+ /os_constants/allowed_virtual_os_program_hashes/2: "0x50a1cd46f1a35628c7569f85218b7b8b01510cb33834c3bd00885183c5770a5"
22
~ /os_resources/execute_syscalls/CallContract/n_steps: 901
33
~ /os_resources/execute_syscalls/Deploy/constant/builtin_instance_counter/pedersen_builtin: 8
44
~ /os_resources/execute_syscalls/Deploy/constant/n_steps: 1183

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ mod OsResourcesTestContract {
55
use starknet::info::SyscallResultTrait;
66
use starknet::syscalls::{
77
call_contract_syscall, deploy_syscall, emit_event_syscall, get_execution_info_v2_syscall,
8-
library_call_syscall,
8+
keccak_syscall, library_call_syscall,
99
};
1010
use starknet::{ClassHash, ContractAddress, get_block_hash_syscall, get_class_hash_at_syscall};
1111

@@ -89,5 +89,14 @@ mod OsResourcesTestContract {
8989

9090
// get execution info syscall.
9191
get_execution_info_v2_syscall().unwrap_syscall();
92+
93+
// keccak syscall. Second call is to measure the keccak round syscall.
94+
keccak_syscall(array![].span()).unwrap_syscall();
95+
// Exactly 17 input u64s are required to measure a single round.
96+
let mut input = array![];
97+
for _ in 0_u8..17 {
98+
input.append(1_u64);
99+
}
100+
keccak_syscall(input.span()).unwrap_syscall();
92101
}
93102
}

crates/starknet_os_flow_tests/src/os_resources_test.rs

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::collections::HashSet;
22

33
use blockifier::blockifier_versioned_constants::{
4+
RawStepGasCost,
45
RawVersionedConstants,
56
ResourcesParams,
67
VariableCallDataFactor,
@@ -43,7 +44,7 @@ use crate::test_manager::{
4344
use crate::tests::NON_TRIVIAL_RESOURCE_BOUNDS;
4445

4546
// TODO(Dori): Delete this, or at least reduce it to a minimal set of unmeasurable syscalls.
46-
const UNMEASURABLE_SYSCALLS: [Selector; 28] = [
47+
const UNMEASURABLE_SYSCALLS: [Selector; 26] = [
4748
Selector::DelegateCall,
4849
Selector::DelegateL1Handler,
4950
Selector::GetBlockNumber,
@@ -53,8 +54,6 @@ const UNMEASURABLE_SYSCALLS: [Selector; 28] = [
5354
Selector::GetSequencerAddress,
5455
Selector::GetTxInfo,
5556
Selector::GetTxSignature,
56-
Selector::Keccak,
57-
Selector::KeccakRound,
5857
Selector::Sha256ProcessBlock,
5958
Selector::Sha512ProcessBlock,
6059
Selector::LibraryCallL1Handler,
@@ -74,7 +73,10 @@ const UNMEASURABLE_SYSCALLS: [Selector; 28] = [
7473
Selector::StorageWrite,
7574
];
7675

77-
const SYSCALLS_WITH_LINEAR_FACTOR: [Selector; 2] = [Selector::Deploy, Selector::MetaTxV0];
76+
/// Keccak does not store the linear factor in the same entry in the versioned constants, but it
77+
/// does have a measurable linear factor stored under [Selector::KeccakRound].
78+
const SYSCALLS_WITH_LINEAR_FACTOR: [Selector; 3] =
79+
[Selector::Deploy, Selector::Keccak, Selector::MetaTxV0];
7880

7981
/// Expected syscalls in the fee transfer call. Should be removed from the list of syscalls during
8082
/// measurement iteration - only the syscalls called during __execute__ should be measured.
@@ -159,6 +161,9 @@ async fn test_fee_transfer_syscalls() {
159161
#[tokio::test]
160162
async fn test_os_resources_regression() {
161163
let os_resources_contract = FeatureContract::OsResourcesTest(RunnableCairo1::Casm);
164+
let version = StarknetVersion::LATEST;
165+
let mut raw_vc: RawVersionedConstants =
166+
serde_json::from_str(VersionedConstants::json_str(&version).unwrap()).unwrap();
162167

163168
// Setup the test initial state and test builder.
164169
// Need to explicitly set up the state to be able to override the minimal sierra version for gas
@@ -319,7 +324,7 @@ async fn test_os_resources_regression() {
319324

320325
// If this if a syscall with a linear factor, the next syscall should be the linear cost.
321326
// Otherwise, this syscall has a constant cost.
322-
let syscall_cost = if SYSCALLS_WITH_LINEAR_FACTOR.contains(&selector) {
327+
if SYSCALLS_WITH_LINEAR_FACTOR.contains(&selector) {
323328
let next_syscall_trace = syscalls_iter.next().unwrap();
324329
assert_eq!(
325330
selector,
@@ -333,16 +338,38 @@ async fn test_os_resources_regression() {
333338
- &next_inner_overhead)
334339
.filter_unused_builtins();
335340
let linear_factor_resources = (&next_resources - &resources).filter_unused_builtins();
336-
VariableResourceParams::WithFactor(ResourcesParams {
337-
constant: resources,
338-
// Syscalls with a linear factor have an unscaled linear factor cost.
339-
calldata_factor: VariableCallDataFactor::Unscaled(linear_factor_resources),
340-
})
341+
// Keccak is a special case - we store the linear cost as a separate syscall.
342+
if selector == Selector::Keccak {
343+
// TODO(Dori): Currently, the Keccak base cost is enforced in the OS to equal the
344+
// syscall base cost. If and when this is no longer the case, no need to replace
345+
// `resources` (measured keccak base cost) with the syscall base cost, and no need
346+
// to recompute the linear factor.
347+
let RawStepGasCost { step_gas_cost: n_steps } =
348+
raw_vc.os_constants.syscall_base_gas_cost.clone();
349+
let resources = ExecutionResources {
350+
n_steps: n_steps.0.try_into().unwrap(),
351+
..Default::default()
352+
};
353+
let linear_factor_resources =
354+
(&next_resources - &resources).filter_unused_builtins();
355+
measurements.insert(Selector::Keccak, VariableResourceParams::Constant(resources));
356+
measurements.insert(
357+
Selector::KeccakRound,
358+
VariableResourceParams::Constant(linear_factor_resources),
359+
);
360+
} else {
361+
measurements.insert(
362+
selector,
363+
VariableResourceParams::WithFactor(ResourcesParams {
364+
constant: resources,
365+
// Syscalls with a linear factor have an unscaled linear factor cost.
366+
calldata_factor: VariableCallDataFactor::Unscaled(linear_factor_resources),
367+
}),
368+
);
369+
}
341370
} else {
342-
VariableResourceParams::Constant(resources)
343-
};
344-
345-
measurements.insert(selector, syscall_cost);
371+
measurements.insert(selector, VariableResourceParams::Constant(resources));
372+
}
346373
}
347374

348375
// Make sure we covered all syscalls we expect to.
@@ -363,9 +390,6 @@ async fn test_os_resources_regression() {
363390
);
364391

365392
// Compare the measurements with the expected values on the latest VC.
366-
let version = StarknetVersion::LATEST;
367-
let mut raw_vc: RawVersionedConstants =
368-
serde_json::from_str(VersionedConstants::json_str(&version).unwrap()).unwrap();
369393
for (syscall, resources) in measurements {
370394
raw_vc.os_resources.execute_syscalls.insert(syscall, resources);
371395
}

0 commit comments

Comments
 (0)