Skip to content

Commit 7e5cf81

Browse files
apollo_starknet_os_program: address audit comments, part 2
1 parent 8368fde commit 7e5cf81

File tree

7 files changed

+37
-33
lines changed

7 files changed

+37
-33
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ const STORED_BLOCK_HASH_BUFFER = 10;
6666

6767
// Allowed virtual OS program hashes for client-side proving.
6868
const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_0 = (
69-
0x0391095dffec88985e40bfa640aa05fd05ed050fcee5b79c27f492de3a68b77f
69+
0x09743416d2d92b680d47338cb89f3def2e77ba772bbc2e568aeb48425e6c450
7070
);
7171
const ALLOWED_VIRTUAL_OS_PROGRAM_HASHES_LEN = 1;
7272

crates/apollo_starknet_os_program/src/cairo/starkware/starknet/core/os/execution/execution_constraints.cairo

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ from starkware.starknet.core.os.virtual_os_output import (
1313
PROOF_VERSION,
1414
VIRTUAL_OS_OUTPUT_VERSION,
1515
VIRTUAL_SNOS,
16+
ProofHeader,
1617
VirtualOsOutputHeader,
1718
)
1819

@@ -39,15 +40,21 @@ func check_proof_facts{range_check_ptr, contract_state_changes: DictAccess*}(
3940
if (proof_facts_size == 0) {
4041
return ();
4142
}
42-
alloc_locals;
43-
assert_le(VirtualOsOutputHeader.SIZE + 3, proof_facts_size);
44-
let proof_version = proof_facts[0];
45-
assert proof_version = PROOF_VERSION;
46-
let proof_type = proof_facts[1];
47-
assert proof_type = VIRTUAL_SNOS;
48-
let program_hash = proof_facts[2];
49-
assert is_program_hash_allowed(program_hash) = TRUE;
50-
let os_output_header = cast(&proof_facts[3], VirtualOsOutputHeader*);
43+
44+
assert_le(ProofHeader.SIZE + VirtualOsOutputHeader.SIZE, proof_facts_size);
45+
46+
// Validate the proof header.
47+
let proof_header = cast(proof_facts, ProofHeader*);
48+
assert is_program_hash_allowed(proof_header.program_hash) = TRUE;
49+
// Proof version and variant are for future compatibility.
50+
assert [proof_header] = ProofHeader(
51+
proof_version=PROOF_VERSION,
52+
proof_variant=VIRTUAL_SNOS,
53+
program_hash=proof_header.program_hash,
54+
);
55+
56+
// Validate the virtual OS output header.
57+
let os_output_header = cast(&proof_facts[ProofHeader.SIZE], VirtualOsOutputHeader*);
5158

5259
with_attr error_message("Virtual OS output version is not supported") {
5360
assert os_output_header.output_version = VIRTUAL_OS_OUTPUT_VERSION;

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

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,27 @@ from starkware.starknet.core.os.virtual_os_output import (
1111
VirtualOsOutputHeader,
1212
)
1313

14-
// Recursively hashes each L2-to-L1 message separately and writes them.
15-
func hash_messages_to_l1_recursive{output_ptr: felt*, poseidon_ptr: PoseidonBuiltin*}(
16-
messages_ptr_start: MessageToL1Header*, messages_ptr_end: MessageToL1Header*
14+
// Hashes each L2-to-L1 message separately and writes the hash to the output.
15+
func output_message_to_l1_hashes{output_ptr: felt*, poseidon_ptr: PoseidonBuiltin*}(
16+
messages_ptr_start: felt*, messages_ptr_end: felt*
1717
) {
18-
alloc_locals;
19-
2018
if (messages_ptr_start == messages_ptr_end) {
2119
return ();
2220
}
2321

2422
// Read the message header.
25-
let message_header = [messages_ptr_start];
26-
let payload_size = message_header.payload_size;
23+
let message_header = cast(messages_ptr_start, MessageToL1Header*);
2724

2825
// Hash the message (header + payload).
29-
// The message consists of: from_address, to_address, payload_size, ...payload.
30-
local message_size = MessageToL1Header.SIZE + payload_size;
31-
let (message_hash) = poseidon_hash_many(
32-
n=message_size, elements=cast(messages_ptr_start, felt*)
33-
);
26+
let message_size = MessageToL1Header.SIZE + message_header.payload_size;
27+
let (message_hash) = poseidon_hash_many(n=message_size, elements=messages_ptr_start);
3428

3529
// Store the hash and advance output_ptr.
3630
assert output_ptr[0] = message_hash;
3731
let output_ptr = &output_ptr[1];
3832

39-
// Move to the next message.
40-
let next_message_ptr = cast(messages_ptr_start + message_size, MessageToL1Header*);
41-
42-
// Recursively process the remaining messages.
43-
return hash_messages_to_l1_recursive(
44-
messages_ptr_start=next_message_ptr, messages_ptr_end=messages_ptr_end
33+
return output_message_to_l1_hashes(
34+
messages_ptr_start=&messages_ptr_start[message_size], messages_ptr_end=messages_ptr_end
4535
);
4636
}
4737

@@ -102,7 +92,7 @@ func process_os_output{
10292
let output_ptr = output_ptr + VirtualOsOutputHeader.SIZE;
10393
let messages_to_l1_hashes_ptr_start: felt* = output_ptr;
10494

105-
hash_messages_to_l1_recursive(
95+
output_message_to_l1_hashes(
10696
messages_ptr_start=os_output.initial_carried_outputs.messages_to_l1,
10797
messages_ptr_end=os_output.final_carried_outputs.messages_to_l1,
10898
);

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ const PROOF_VERSION = 'PROOF0';
77
// The version of the virtual OS output.
88
const VIRTUAL_OS_OUTPUT_VERSION = 'VIRTUAL_SNOS0';
99

10+
// The header of the proof facts, preceding the virtual OS output.
11+
struct ProofHeader {
12+
proof_version: felt,
13+
proof_variant: felt,
14+
program_hash: felt,
15+
}
16+
1017
// The header of the virtual OS output.
1118
struct VirtualOsOutputHeader {
1219
output_version: felt,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"os": "0x2784c2ff1846d5949f32d0af52b285a9dbe84224c1e0f24324aabc196fca674",
3-
"virtual_os": "0x391095dffec88985e40bfa640aa05fd05ed050fcee5b79c27f492de3a68b77f",
2+
"os": "0x7c5db5bc9e55d9027353a9daae7cbbd546dd10bada122ccfd2adb3cd3d67ac0",
3+
"virtual_os": "0x9743416d2d92b680d47338cb89f3def2e77ba772bbc2e568aeb48425e6c450",
44
"aggregator": "0x4a50066445fdceb87b7ed1211dbff1f544ad4fa879a678a79355ed812dea346",
55
"aggregator_with_prefix": "0x5af6c0904619be30798f335358f96ba742a8f23c7b0df1080d0c24e2224c6f6"
66
}

crates/blockifier/resources/blockifier_versioned_constants_0_14_2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"segment_arena_cells": false,
6868
"os_constants": {
6969
"allowed_virtual_os_program_hashes": [
70-
"0x391095dffec88985e40bfa640aa05fd05ed050fcee5b79c27f492de3a68b77f"
70+
"0x9743416d2d92b680d47338cb89f3def2e77ba772bbc2e568aeb48425e6c450"
7171
],
7272
"constructor_entry_point_selector": "0x28ffe4ff0f226a9107253e17a904099aa4f63a02a5621de0576e5aa71bc5194",
7373
"default_entry_point_selector": "0x0",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
~ /archival_data_gas_costs/gas_per_proof/0: 1000000000
22
~ /enable_casm_hash_migration: false
33
~ /gateway/max_proof_size: 600000
4-
+ /os_constants/allowed_virtual_os_program_hashes/0: "0x391095dffec88985e40bfa640aa05fd05ed050fcee5b79c27f492de3a68b77f"
4+
+ /os_constants/allowed_virtual_os_program_hashes/0: "0x9743416d2d92b680d47338cb89f3def2e77ba772bbc2e568aeb48425e6c450"
55
~ /os_constants/builtin_gas_costs/blake: 3334

0 commit comments

Comments
 (0)