Skip to content

Commit

Permalink
refactor(katana-trie): use pedersen hash in all tries (#2822)
Browse files Browse the repository at this point in the history
change to pedersen hash for all tries
  • Loading branch information
kariy committed Dec 19, 2024
1 parent f668faf commit e3e375d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion crates/katana/rpc/rpc/tests/proofs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ async fn classes_proofs() {

// the returned data is the list of values corresponds to the [key]
let results = classes_proof
.verify_proof::<hash::Poseidon>(global_roots.classes_tree_root, [key], 251)
.verify_proof::<hash::Pedersen>(global_roots.classes_tree_root, [key], 251)
.collect::<Result<Vec<_>, _>>()
.expect("failed to verify proofs");

Expand Down
1 change: 1 addition & 0 deletions crates/katana/storage/provider/src/traits/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::ProviderResult;
pub trait StateRootProvider: Send + Sync {
/// Retrieves the root of the global state trie.
fn state_root(&self) -> ProviderResult<Felt> {
// https://docs.starknet.io/architecture-and-concepts/network-architecture/starknet-state/#state_commitment
Ok(starknet_types_core::hash::Poseidon::hash_array(&[
short_string!("STARKNET_STATE_V0"),
self.contracts_root()?,
Expand Down
3 changes: 2 additions & 1 deletion crates/katana/trie/src/classes.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use bonsai_trie::{BonsaiDatabase, BonsaiPersistentDatabase, MultiProof};
use katana_primitives::block::BlockNumber;
use katana_primitives::class::{ClassHash, CompiledClassHash};
use katana_primitives::hash::Pedersen;
use katana_primitives::Felt;
use starknet::macros::short_string;
use starknet_types_core::hash::{Poseidon, StarkHash};
Expand All @@ -9,7 +10,7 @@ use crate::id::CommitId;

#[derive(Debug)]
pub struct ClassesTrie<DB: BonsaiDatabase> {
trie: crate::BonsaiTrie<DB>,
trie: crate::BonsaiTrie<DB, Pedersen>,
}

impl<DB: BonsaiDatabase> ClassesTrie<DB> {
Expand Down
3 changes: 2 additions & 1 deletion crates/katana/trie/src/contracts.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use bonsai_trie::{BonsaiDatabase, BonsaiPersistentDatabase, MultiProof};
use katana_primitives::block::BlockNumber;
use katana_primitives::hash::Pedersen;
use katana_primitives::{ContractAddress, Felt};

use crate::id::CommitId;

#[derive(Debug)]
pub struct ContractsTrie<DB: BonsaiDatabase> {
trie: crate::BonsaiTrie<DB>,
trie: crate::BonsaiTrie<DB, Pedersen>,
}

impl<DB: BonsaiDatabase> ContractsTrie<DB> {
Expand Down
36 changes: 29 additions & 7 deletions crates/katana/trie/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use bonsai_trie::{BonsaiDatabase, BonsaiPersistentDatabase};
use bonsai_trie::{BonsaiStorage, BonsaiStorageConfig};
use katana_primitives::class::ClassHash;
use katana_primitives::Felt;
use starknet_types_core::hash::{Pedersen, Poseidon, StarkHash};
use starknet_types_core::hash::{Pedersen, StarkHash};
pub use {bitvec, bonsai_trie as bonsai};

mod classes;
Expand All @@ -26,12 +26,19 @@ pub use storages::StoragesTrie;
/// having to handle how to transform the keys into the internal keys used by the trie.
/// This struct is not meant to be used directly, and instead use the specific tries that have
/// been derived from it, [`ClassesTrie`], [`ContractsTrie`], or [`StoragesTrie`].
#[derive(Debug)]
pub(crate) struct BonsaiTrie<DB: BonsaiDatabase> {
storage: BonsaiStorage<CommitId, DB, Poseidon>,
pub(crate) struct BonsaiTrie<DB, Hash = Pedersen>
where
DB: BonsaiDatabase,
Hash: StarkHash + Send + Sync,
{
storage: BonsaiStorage<CommitId, DB, Hash>,
}

impl<DB: BonsaiDatabase> BonsaiTrie<DB> {
impl<DB, Hash> BonsaiTrie<DB, Hash>
where
DB: BonsaiDatabase,
Hash: StarkHash + Send + Sync,
{
pub fn new(db: DB) -> Self {
let config = BonsaiStorageConfig {
max_saved_trie_logs: Some(usize::MAX),
Expand All @@ -43,7 +50,11 @@ impl<DB: BonsaiDatabase> BonsaiTrie<DB> {
}
}

impl<DB: BonsaiDatabase> BonsaiTrie<DB> {
impl<DB, Hash> BonsaiTrie<DB, Hash>
where
DB: BonsaiDatabase,
Hash: StarkHash + Send + Sync,
{
pub fn root(&self, id: &[u8]) -> Felt {
self.storage.root_hash(id).expect("failed to get trie root")
}
Expand All @@ -59,9 +70,10 @@ impl<DB: BonsaiDatabase> BonsaiTrie<DB> {
}
}

impl<DB> BonsaiTrie<DB>
impl<DB, Hash> BonsaiTrie<DB, Hash>
where
DB: BonsaiDatabase + BonsaiPersistentDatabase<CommitId>,
Hash: StarkHash + Send + Sync,
{
pub fn insert(&mut self, id: &[u8], key: Felt, value: Felt) {
let key: BitVec<u8, Msb0> = key.to_bytes_be().as_bits()[5..].to_owned();
Expand All @@ -73,6 +85,16 @@ where
}
}

impl<DB, Hash> std::fmt::Debug for BonsaiTrie<DB, Hash>
where
DB: BonsaiDatabase,
Hash: StarkHash + Send + Sync,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("BonsaiTrie").field("storage", &self.storage).finish()
}
}

pub fn compute_merkle_root<H>(values: &[Felt]) -> anyhow::Result<Felt>
where
H: StarkHash + Send + Sync,
Expand Down
3 changes: 2 additions & 1 deletion crates/katana/trie/src/storages.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use bonsai_trie::{BonsaiDatabase, BonsaiPersistentDatabase, MultiProof};
use katana_primitives::block::BlockNumber;
use katana_primitives::contract::{StorageKey, StorageValue};
use katana_primitives::hash::Pedersen;
use katana_primitives::{ContractAddress, Felt};

use crate::id::CommitId;

#[derive(Debug)]
pub struct StoragesTrie<DB: BonsaiDatabase> {
trie: crate::BonsaiTrie<DB>,
trie: crate::BonsaiTrie<DB, Pedersen>,
}

impl<DB: BonsaiDatabase> StoragesTrie<DB> {
Expand Down

0 comments on commit e3e375d

Please sign in to comment.