-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
641c399
commit 94edaba
Showing
11 changed files
with
1,915 additions
and
134 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod types; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
use crate::types::Felt; | ||
|
||
#[allow(dead_code)] | ||
pub(crate) struct HashInputPair(pub Felt, pub Felt); | ||
|
||
#[allow(dead_code)] | ||
pub(crate) struct HashOutput(pub Felt); | ||
|
||
#[allow(dead_code)] | ||
impl HashOutput { | ||
pub(crate) const ZERO: HashOutput = HashOutput(Felt::ZERO); | ||
} | ||
|
||
pub(crate) trait HashFunction { | ||
/// Computes the hash of given input. | ||
async fn compute_hash(i: HashInputPair) -> HashOutput; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,3 @@ | ||
use pathfinder_crypto::Felt; | ||
use patricia_merkle_tree::{ | ||
hash::{HashInput, HashOutput}, | ||
node::NodeIndex, | ||
}; | ||
|
||
pub mod hash; | ||
pub mod patricia_merkle_tree; | ||
|
||
// TODO(Dori, 3/3/2024): Delete this dummy code once rust allows it. | ||
pub fn dummy() -> Felt { | ||
let felt = Felt::from_u64(0_u64); | ||
let hash_input = HashInput(felt, felt, felt); | ||
let hash_output = HashOutput(felt); | ||
let node_index = NodeIndex(felt); | ||
// Rust requires that every field will be used. | ||
hash_input.0 + hash_input.1 + hash_input.2 + hash_output.0 + node_index.0 | ||
} | ||
|
||
#[cfg(test)] | ||
pub mod test { | ||
use super::dummy; | ||
use pathfinder_crypto::Felt; | ||
use pretty_assertions::assert_eq; | ||
|
||
#[test] | ||
fn test_dummy() { | ||
assert_eq!(dummy(), Felt::from_u64(0_u64)); | ||
} | ||
} | ||
pub mod types; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
pub mod hash; | ||
pub mod errors; | ||
pub mod node; | ||
pub mod tree; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// TODO(Amos, 01/04/2024): Add error types. | ||
#[derive(Debug)] | ||
pub enum NodeHashComputationError {} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,75 @@ | ||
use pathfinder_crypto::Felt; | ||
use starknet_api::core::{ClassHash, Nonce}; | ||
|
||
use super::hash::HashFunction; | ||
use crate::types::Felt; | ||
|
||
use crate::hash::types::{HashFunction, HashOutput}; | ||
|
||
use super::errors::NodeHashComputationError; | ||
|
||
// TODO(Amos, 01/05/2024): Implement types for NodeIndex, EdgePath, EdgePathLength | ||
#[allow(dead_code)] | ||
pub(crate) struct NodeIndex(pub Felt); | ||
|
||
// TODO(Amos, 26/3/2024): Define trait's methods. | ||
pub(crate) trait Node<H: HashFunction> {} | ||
#[allow(dead_code)] | ||
pub(crate) struct EdgePath(pub Felt); | ||
|
||
#[allow(dead_code)] | ||
pub(crate) struct EdgePathLength(pub Felt); | ||
|
||
#[allow(dead_code)] | ||
pub(crate) enum NodeValue<LeafVal> { | ||
Binary(Option<BinaryValue>), | ||
Edge { | ||
bottom_value: Option<BottomValue>, | ||
path_to_bottom: PathToBottom, | ||
}, | ||
Leaf(LeafVal), | ||
Sibling, | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub(crate) enum BinaryValue { | ||
Left(HashOutput), | ||
Right(HashOutput), | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub(crate) enum BottomValue { | ||
BottomBinaryValue(BinaryValue), | ||
BottomLeafValue(LeafValue), | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub(crate) struct PathToBottom { | ||
pub path: EdgePath, | ||
pub length: EdgePathLength, | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub(crate) enum LeafValue { | ||
StorageValue(Felt), | ||
CompiledClassHash(Felt), | ||
StateTreeValue { | ||
class_hash: ClassHash, | ||
contract_state_root_hash: Felt, | ||
nonce: Nonce, | ||
}, | ||
} | ||
|
||
pub(crate) trait Node<H: HashFunction, LeafVal> { | ||
/// Computes and sets sub-tree's hashes and values, if possible. | ||
/// If successful or if hash and value of node are already set - returns hash of node. | ||
fn compute_and_set_subtree_hashes_and_values( | ||
&mut self, | ||
) -> Result<HashOutput, NodeHashComputationError>; | ||
|
||
/// Computes and returns hash of node, if value is set. | ||
/// If node is None - returns hash of empty tree. | ||
fn compute_hash(node: Option<&Self>) -> Result<HashOutput, NodeHashComputationError>; | ||
|
||
/// Returns node's value. If value was not set some fields may be empty. | ||
fn get_value(&self) -> NodeValue<LeafVal>; | ||
|
||
/// Returns node's hash if it was set. | ||
fn get_hash(&self) -> Option<HashOutput>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
use crate::patricia_merkle_tree::node::Node; | ||
use std::iter::Map; | ||
|
||
use super::{hash::HashFunction, node::NodeIndex}; | ||
use crate::patricia_merkle_tree::node::{Node, NodeIndex}; | ||
|
||
pub(crate) trait Tree<H: HashFunction, N: Node<H>> { | ||
/// Returns the node with given full (Merkle) index, if it exists. | ||
fn get_node(full_index: NodeIndex) -> Option<N>; | ||
use crate::hash::types::HashFunction; | ||
|
||
pub(crate) trait Tree<H: HashFunction, LeafVal, N: Node<H, LeafVal>> { | ||
/// Returns the root if the tree is not empty. | ||
fn get_root() -> Option<N>; | ||
fn get_root(&self) -> Option<&mut N>; | ||
|
||
/// Updates tree in place with given leaves, and returns all modified (and new) nodes. | ||
fn update_and_get_modified_nodes( | ||
&mut self, | ||
index_to_updated_leaf: Map<NodeIndex, &N>, | ||
) -> Map<NodeIndex, &N>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
use starknet_types_core::felt::Felt as StarknetTypesFelt; | ||
|
||
pub(crate) type Felt = StarknetTypesFelt; |