Skip to content

Commit 31449a8

Browse files
blockifier: add virtual os program hash validations in the gateway and blockifier
1 parent 442f676 commit 31449a8

23 files changed

+121
-35
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+
"0x130206a40921880628605041292e995870334451179c63090221210893986a2",
3838
"0x3",
3939
"0x2",
4040
"0x1"

crates/apollo_consensus_orchestrator/resources/central_preconfirmed_block.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
],
6060
"proof_facts": [
6161
"0x5649525455414c5f534e4f53",
62-
"0x4",
62+
"0x130206a40921880628605041292e995870334451179c63090221210893986a2",
6363
"0x3",
6464
"0x2",
6565
"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+
"0x130206a40921880628605041292e995870334451179c63090221210893986a2",
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+
"0x130206a40921880628605041292e995870334451179c63090221210893986a2",
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+
0x0130206a40921880628605041292e995870334451179c63090221210893986a2
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: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ 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;
77
use expect_test::expect_file;
8-
use starknet_api::core::{ClassHash, ContractAddress, EntryPointSelector};
8+
use starknet_api::core::{ContractAddress, EntryPointSelector};
99
use starknet_api::versioned_constants_logic::VersionedConstantsTrait;
1010
use starknet_types_core::felt::Felt;
1111

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

@@ -96,6 +113,11 @@ fn generate_constants_file() -> String {
96113
RESERVED_CONTRACT_ADDRESS = contract_address_to_hex(
97114
&os_constants.os_contract_addresses.reserved_contract_address()
98115
),
116+
ALLOWED_VIRTUAL_OS_PROGRAM_HASHES = format_hash_consts(
117+
"ALLOWED_VIRTUAL_OS_PROGRAM_HASHES",
118+
&os_constants.allowed_virtual_os_program_hashes,
119+
|felt| felt
120+
),
99121
// Base costs.
100122
STEP_GAS_COST = os_constants.gas_costs.base.step_gas_cost,
101123
MEMORY_HOLE_GAS_COST = os_constants.gas_costs.base.memory_hole_gas_cost,
@@ -190,18 +212,23 @@ fn generate_constants_file() -> String {
190212
VALIDATE_TIMESTAMP_ROUNDING =
191213
os_constants.validate_rounding_consts.validate_timestamp_rounding,
192214
// Backward compatibility accounts.
193-
V1_BOUND_ACCOUNTS_CAIRO0 = stringify_class_hash_list(
215+
V1_BOUND_ACCOUNTS_CAIRO0 = format_hash_consts(
194216
"V1_BOUND_ACCOUNTS_CAIRO0",
195-
&os_constants.v1_bound_accounts_cairo0
217+
&os_constants.v1_bound_accounts_cairo0,
218+
|class_hash| &class_hash.0
196219
),
197-
V1_BOUND_ACCOUNTS_CAIRO1 = stringify_class_hash_list(
220+
V1_BOUND_ACCOUNTS_CAIRO1 = format_hash_consts(
198221
"V1_BOUND_ACCOUNTS_CAIRO1",
199-
&os_constants.v1_bound_accounts_cairo1
222+
&os_constants.v1_bound_accounts_cairo1,
223+
|class_hash| &class_hash.0
200224
),
201225
V1_BOUND_ACCOUNTS_MAX_TIP =
202226
format!("{:#?}", Felt::from(os_constants.v1_bound_accounts_max_tip)),
203-
DATA_GAS_ACCOUNTS =
204-
stringify_class_hash_list("DATA_GAS_ACCOUNTS", &os_constants.data_gas_accounts),
227+
DATA_GAS_ACCOUNTS = format_hash_consts(
228+
"DATA_GAS_ACCOUNTS",
229+
&os_constants.data_gas_accounts,
230+
|class_hash| &class_hash.0
231+
),
205232
);
206233

207234
// 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,

0 commit comments

Comments
 (0)