Skip to content

Commit 8149407

Browse files
committed
starknet_committer: compute and store accessed keys digest
1 parent 6ee8f1d commit 8149407

3 files changed

Lines changed: 55 additions & 0 deletions

File tree

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/starknet_committer/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ license.workspace = true
77
description = "Computes and manages Starknet state."
88

99
[features]
10+
os_input = ["dep:bincode", "dep:blake2", "dep:digest"]
1011
testing = ["starknet_patricia/testing"]
1112

1213
[dependencies]
14+
bincode = { workspace = true, optional = true }
15+
blake2 = { workspace = true, optional = true }
16+
digest = { workspace = true, optional = true }
1317
apollo_config.workspace = true
1418
async-trait.workspace = true
1519
derive_more = { workspace = true, features = ["as_ref", "from", "into"] }

crates/starknet_committer/src/db/serde_db_utils.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
#[cfg(feature = "os_input")]
2+
use blake2::Blake2s256;
3+
#[cfg(feature = "os_input")]
4+
use digest::Digest;
15
use serde::{Deserialize, Serialize};
26
use starknet_api::block::BlockNumber;
7+
#[cfg(feature = "os_input")]
8+
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
39
use starknet_patricia_storage::storage_trait::DbValue;
410
use starknet_types_core::felt::Felt;
511

12+
#[cfg(feature = "os_input")]
13+
use crate::patricia_merkle_tree::tree::SortedLeavesRequest;
14+
615
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Hash, Serialize)]
716
pub struct DbBlockNumber(pub BlockNumber);
817

@@ -23,3 +32,42 @@ pub fn serialize_felt_no_packing(felt: Felt) -> DbValue {
2332
pub fn deserialize_felt_no_packing(value: &DbValue) -> Felt {
2433
Felt::from_bytes_be_slice(&value.0)
2534
}
35+
36+
/// BLAKE2s-256 digest over a deterministic encoding of [`SortedLeavesRequest`].
37+
#[cfg(feature = "os_input")]
38+
pub fn accessed_keys_digest(sorted: &SortedLeavesRequest<'_>) -> [u8; 32] {
39+
let mut payload = Vec::new();
40+
41+
let class = sorted.class_sorted.get_indices();
42+
payload.extend_from_slice(&encode_usize(class.len()));
43+
for idx in class {
44+
payload.extend_from_slice(&idx.0.to_be_bytes());
45+
}
46+
47+
let contract = sorted.contract_sorted.get_indices();
48+
payload.extend_from_slice(&encode_usize(contract.len()));
49+
for idx in contract {
50+
payload.extend_from_slice(&idx.0.to_be_bytes());
51+
}
52+
53+
let mut contract_indices: Vec<NodeIndex> = sorted.storage_sorted.keys().copied().collect();
54+
contract_indices.sort_unstable();
55+
56+
payload.extend_from_slice(&encode_usize(contract_indices.len()));
57+
for contract_idx in contract_indices {
58+
let sorted_slots = sorted.storage_sorted.get(&contract_idx).unwrap();
59+
payload.extend_from_slice(&contract_idx.0.to_be_bytes());
60+
let slot_indices = sorted_slots.get_indices();
61+
payload.extend_from_slice(&encode_usize(slot_indices.len()));
62+
for slot in slot_indices {
63+
payload.extend_from_slice(&slot.0.to_be_bytes());
64+
}
65+
}
66+
67+
Blake2s256::digest(&payload).into()
68+
}
69+
70+
#[cfg(feature = "os_input")]
71+
fn encode_usize(n: usize) -> [u8; 8] {
72+
(n as u64).to_be_bytes()
73+
}

0 commit comments

Comments
 (0)