Skip to content

Commit b1e68fa

Browse files
blockifier: add virtual os program hash validations in the gateway and blockifier
1 parent 8700e78 commit b1e68fa

22 files changed

+116
-33
lines changed

crates/apollo_consensus_orchestrator/resources/central_invoke_tx_client_side_proving.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"account_deployment_data": [],
3535
"proof_facts": [
3636
"0x5649525455414c5f534e4f53",
37-
"0x4",
37+
"0x030206a40921880628605041292e995870334451179c63090221210893986a2",
3838
"0x3",
3939
"0x2",
4040
"0x1"

crates/apollo_http_server/resources/deprecated_gateway/invoke_tx_client_side_proving.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"account_deployment_data": [],
2929
"proof_facts": [
3030
"0x5649525455414c5f534e4f53",
31-
"0x4",
31+
"0x030206a40921880628605041292e995870334451179c63090221210893986a2",
3232
"0x3",
3333
"0x2",
3434
"0x1"

crates/apollo_starknet_client/resources/reader/invoke_v3_client_side_proving.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"type": "INVOKE_FUNCTION",
3535
"proof_facts": [
3636
"0x5649525455414c5f534e4f53",
37-
"0x4",
37+
"0x030206a40921880628605041292e995870334451179c63090221210893986a2",
3838
"0x3",
3939
"0x2",
4040
"0x1"

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ const RESERVED_CONTRACT_ADDRESS = 0x3;
6464
// The block number -> block hash mapping is written for the current block number minus this number.
6565
const STORED_BLOCK_HASH_BUFFER = 10;
6666

67+
// Allowed virtual OS program hashes for client-side proving.
68+
const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_0 = (
69+
0x0030206a40921880628605041292e995870334451179c63090221210893986a2
70+
);
71+
const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_LEN = 1;
72+
6773
// Gas constants.
6874

6975
const STEP_GAS_COST = 100;

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/constants_template.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ const RESERVED_CONTRACT_ADDRESS = {RESERVED_CONTRACT_ADDRESS};
5252
// The block number -> block hash mapping is written for the current block number minus this number.
5353
const STORED_BLOCK_HASH_BUFFER = {STORED_BLOCK_HASH_BUFFER};
5454

55+
// Allowed virtual OS program hashes for client-side proving.
56+
{ALLOWED_VIRTUAL_OS_PROGRAM_HASHES}
57+
5558
// Gas constants.
5659

5760
const STEP_GAS_COST = {STEP_GAS_COST};

crates/apollo_starknet_os_program/src/constants_test.rs

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use apollo_infra_utils::cairo0_compiler::cairo0_format;
44
use apollo_infra_utils::compile_time_cargo_manifest_dir;
55
use blockifier::blockifier_versioned_constants::{OsConstants, VersionedConstants};
66
use blockifier::execution::syscalls::vm_syscall_utils::SyscallSelector;
7-
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector};
7+
use starknet_api::core::{ContractAddress, EntryPointSelector};
88
use starknet_api::versioned_constants_logic::VersionedConstantsTrait;
99
use starknet_types_core::felt::Felt;
1010

@@ -30,32 +30,49 @@ fn base_only_syscall_cost(selector: SyscallSelector, os_constants: &OsConstants)
3030
fn quote_string(s: &str) -> String {
3131
format!("'{s}'")
3232
}
33-
34-
/// Create constants from a list of class hashes. Example:
33+
/// Create Rust `const` definitions from a list of hashes (rendered as `Felt`s).
34+
///
35+
/// Example:
36+
/// ```
37+
/// let hashes = [ClassHash(1), ClassHash(2)];
38+
///
39+
/// let s = format_hash_consts("X", &hashes, |h| &h.0);
40+
///
41+
/// let expected = "\
42+
/// const X_0 = 0x0000000000000000000000000000000000000000000000000000000000000001;\n
43+
/// const X_1 = 0x0000000000000000000000000000000000000000000000000000000000000002;\n
44+
/// const X_LEN = 2;";
45+
///
46+
/// assert_eq!(s, expected);
3547
/// ```
36-
/// let expected = #"
37-
/// X_0 = 0x1;
38-
/// X_1 = 0x2;
39-
/// X_LEN = 2;
40-
/// "#;
41-
/// assert_eq!(stringify_class_hash_list("X", &[ClassHash(1), ClassHash(2)]), expected);
48+
///
49+
/// Conceptual output (simplified for readability):
50+
/// ```text
51+
/// const X_0 = 0x1;
52+
/// const X_1 = 0x2;
53+
/// const X_LEN = 2;
4254
/// ```
43-
fn stringify_class_hash_list(name: &str, class_hashes: &[ClassHash]) -> String {
44-
class_hashes
55+
fn format_hash_consts<T, F>(name: &str, hash_list: &[T], to_felt: F) -> String
56+
where
57+
F: Fn(&T) -> &Felt,
58+
{
59+
hash_list
4560
.iter()
4661
.enumerate()
47-
.map(|(i, class_hash)| {
48-
// If the line ends up longer than 100 chars, wrap the value in parenthesis, so the
49-
// formatter can split the lines.
50-
let line = format!("const {name}_{i} = {:#066x};", class_hash.0);
51-
if line.len() > 100 {
52-
format!("const {name}_{i} = ({:#066x});", class_hash.0)
62+
.map(|(i, hash)| {
63+
let felt = to_felt(hash);
64+
65+
// If the line ends up longer than 100 chars, wrap the value in parentheses so the
66+
// formatter can split the line.
67+
let const_def = format!("const {name}_{i} = {:#066x};", felt);
68+
if const_def.len() > 100 {
69+
format!("const {name}_{i} = ({:#066x});", felt)
5370
} else {
54-
line
71+
const_def
5572
}
5673
})
57-
.chain(std::iter::once(format!("const {name}_LEN = {};", class_hashes.len())))
58-
.collect::<Vec<String>>()
74+
.chain(std::iter::once(format!("const {name}_LEN = {};", hash_list.len())))
75+
.collect::<Vec<_>>()
5976
.join("\n")
6077
}
6178

@@ -97,6 +114,11 @@ fn generate_constants_file() -> String {
97114
RESERVED_CONTRACT_ADDRESS = contract_address_to_hex(
98115
&os_constants.os_contract_addresses.reserved_contract_address()
99116
),
117+
ALLOWED_VIRTUAL_OS_PROGRAM_HASHES = stringify_hash_list(
118+
"ALLOWED_VIRTUAL_OS_PROGRAM_HASHES",
119+
&os_constants.allowed_virtual_os_program_hashes,
120+
|felt| felt
121+
),
100122
// Base costs.
101123
STEP_GAS_COST = os_constants.gas_costs.base.step_gas_cost,
102124
MEMORY_HOLE_GAS_COST = os_constants.gas_costs.base.memory_hole_gas_cost,
@@ -191,18 +213,20 @@ fn generate_constants_file() -> String {
191213
VALIDATE_TIMESTAMP_ROUNDING =
192214
os_constants.validate_rounding_consts.validate_timestamp_rounding,
193215
// Backward compatibility accounts.
194-
V1_BOUND_ACCOUNTS_CAIRO0 = stringify_class_hash_list(
216+
V1_BOUND_ACCOUNTS_CAIRO0 = stringify_hash_list(
195217
"V1_BOUND_ACCOUNTS_CAIRO0",
196-
&os_constants.v1_bound_accounts_cairo0
218+
&os_constants.v1_bound_accounts_cairo0,
219+
|class_hash| &class_hash.0
197220
),
198-
V1_BOUND_ACCOUNTS_CAIRO1 = stringify_class_hash_list(
221+
V1_BOUND_ACCOUNTS_CAIRO1 = stringify_hash_list(
199222
"V1_BOUND_ACCOUNTS_CAIRO1",
200-
&os_constants.v1_bound_accounts_cairo1
223+
&os_constants.v1_bound_accounts_cairo1,
224+
|class_hash| &class_hash.0
201225
),
202226
V1_BOUND_ACCOUNTS_MAX_TIP =
203227
format!("{:#?}", Felt::from(os_constants.v1_bound_accounts_max_tip)),
204228
DATA_GAS_ACCOUNTS =
205-
stringify_class_hash_list("DATA_GAS_ACCOUNTS", &os_constants.data_gas_accounts),
229+
stringify_hash_list("DATA_GAS_ACCOUNTS", &os_constants.data_gas_accounts, |ch| &ch.0),
206230
);
207231

208232
// Format and return.

crates/blockifier/resources/blockifier_versioned_constants_0_13_0.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"max_recursion_depth": 50,
5858
"min_sierra_version_for_sierra_gas": "100.0.0",
5959
"os_constants": {
60+
"allowed_virtual_os_program_hashes": [],
6061
"builtin_gas_costs": {
6162
"add_mod": 0,
6263
"bitwise": 0,

crates/blockifier/resources/blockifier_versioned_constants_0_13_1.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"max_recursion_depth": 50,
5858
"min_sierra_version_for_sierra_gas": "100.0.0",
5959
"os_constants": {
60+
"allowed_virtual_os_program_hashes": [],
6061
"builtin_gas_costs": {
6162
"add_mod": 0,
6263
"bitwise": 0,

crates/blockifier/resources/blockifier_versioned_constants_0_13_1_1.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"max_recursion_depth": 50,
5858
"min_sierra_version_for_sierra_gas": "100.0.0",
5959
"os_constants": {
60+
"allowed_virtual_os_program_hashes": [],
6061
"builtin_gas_costs": {
6162
"add_mod": 0,
6263
"bitwise": 0,

crates/blockifier/resources/blockifier_versioned_constants_0_13_2.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"max_recursion_depth": 50,
5858
"min_sierra_version_for_sierra_gas": "100.0.0",
5959
"os_constants": {
60+
"allowed_virtual_os_program_hashes": [],
6061
"builtin_gas_costs": {
6162
"add_mod": 0,
6263
"bitwise": 594,

0 commit comments

Comments
 (0)