Skip to content

Commit 7ed8117

Browse files
feat(starknet_os): deserialize commitment facts (#5854)
1 parent 5a0c775 commit 7ed8117

3 files changed

Lines changed: 63 additions & 5 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
use num_bigint::BigUint;
2+
use starknet_patricia::patricia_merkle_tree::node_data::errors::{
3+
EdgePathError,
4+
PathToBottomError,
5+
};
6+
use starknet_types_core::felt::Felt;
27

38
use crate::hints::hint_implementation::patricia::utils::Preimage;
49

510
#[derive(Debug, thiserror::Error)]
611
pub enum PatriciaError {
12+
#[error(transparent)]
13+
EdgePath(#[from] EdgePathError),
714
#[error("Expected a binary node, found: {0:?}")]
815
ExpectedBinary(Preimage),
16+
#[error("Invalid raw preimage: {0:?}, length should be 2 or 3.")]
17+
InvalidRawPreimage(Vec<Felt>),
918
#[error("Exceeded the max index: {0:?}")]
1019
MaxLayerIndexExceeded(BigUint),
20+
#[error(transparent)]
21+
PathToBottom(#[from] PathToBottomError),
1122
}

crates/starknet_os/src/hints/hint_implementation/patricia/utils.rs

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ use std::collections::{HashMap, HashSet};
22

33
use num_bigint::BigUint;
44
use starknet_patricia::hash::hash_trait::HashOutput;
5-
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{BinaryData, EdgeData};
5+
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
6+
BinaryData,
7+
EdgeData,
8+
EdgePathLength,
9+
PathToBottom,
10+
};
611
use starknet_patricia::patricia_merkle_tree::types::SubTreeHeight;
12+
use starknet_types_core::felt::Felt;
713

814
use crate::hints::hint_implementation::patricia::error::PatriciaError;
915

@@ -20,17 +26,20 @@ pub enum Preimage {
2026
pub type PreimageMap = HashMap<HashOutput, Preimage>;
2127

2228
impl Preimage {
29+
pub(crate) const BINARY_LENGTH: u8 = 2;
30+
pub(crate) const EDGE_LENGTH: u8 = 3;
31+
2332
pub fn length(&self) -> u8 {
2433
match self {
25-
Preimage::Binary(_) => 2,
26-
Preimage::Edge(_) => 3,
34+
Self::Binary(_) => Self::BINARY_LENGTH,
35+
Self::Edge(_) => Self::EDGE_LENGTH,
2736
}
2837
}
2938

3039
fn get_binary(&self) -> Result<&BinaryData, PatriciaError> {
3140
match self {
32-
Preimage::Binary(binary) => Ok(binary),
33-
Preimage::Edge(_) => Err(PatriciaError::ExpectedBinary(self.clone())),
41+
Self::Binary(binary) => Ok(binary),
42+
Self::Edge(_) => Err(PatriciaError::ExpectedBinary(self.clone())),
3443
}
3544
}
3645
}
@@ -173,3 +182,35 @@ pub(crate) fn build_update_tree(
173182
.remove(&LayerIndex::FIRST_LEAF)
174183
.expect("There should be a root node since modifications are not empty."))
175184
}
185+
186+
/// Deserializes the preimage mapping from the commitment facts.
187+
fn create_preimage_mapping(
188+
commitment_facts: &HashMap<HashOutput, Vec<Felt>>,
189+
) -> Result<PreimageMap, PatriciaError> {
190+
let mut preimage_mapping = PreimageMap::new();
191+
for (hash, raw_preimage) in commitment_facts.iter() {
192+
match raw_preimage.as_slice() {
193+
[left, right] => {
194+
let binary_data =
195+
BinaryData { left_hash: HashOutput(*left), right_hash: HashOutput(*right) };
196+
preimage_mapping.insert(*hash, Preimage::Binary(binary_data));
197+
}
198+
[length, path, bottom] => {
199+
let edge_data = EdgeData {
200+
bottom_hash: HashOutput(*bottom),
201+
path_to_bottom: PathToBottom::new(
202+
(*path).into(),
203+
EdgePathLength::new((*length).try_into().map_err(|_| {
204+
PatriciaError::InvalidRawPreimage(raw_preimage.clone())
205+
})?)?,
206+
)?,
207+
};
208+
preimage_mapping.insert(*hash, Preimage::Edge(edge_data));
209+
}
210+
_ => {
211+
return Err(PatriciaError::InvalidRawPreimage(raw_preimage.clone()));
212+
}
213+
}
214+
}
215+
Ok(preimage_mapping)
216+
}

crates/starknet_patricia/src/patricia_merkle_tree/node_data/inner_node.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ impl From<u128> for EdgePath {
5252
}
5353
}
5454

55+
impl From<Felt> for EdgePath {
56+
fn from(value: Felt) -> Self {
57+
U256::from_be_bytes(value.to_bytes_be()).into()
58+
}
59+
}
60+
5561
impl From<&EdgePath> for Felt {
5662
fn from(path: &EdgePath) -> Self {
5763
Self::from_bytes_be(&path.0.to_be_bytes())

0 commit comments

Comments
 (0)