arnaucube
arnaucube/miden-cryptoPublic Self-hosted repository
Merge pull request #96 from 0xPolygonMiden/hacka-check-root-in-storage

bugfix: check if the requested root is in the storage
Augusto Hack hack.augusto@gmail.com
Thu, 16 Mar 2023 23:30:56 +0100
commit

5703fef2262ff6c5d44d7c9864b6d5b458c709ce

parent

931bcc3cc3c4993ece988f84c4595d00be431184

1 files changed, 6 insertions(+), 12 deletions(-)

jump to
M src/merkle/store.rs → src/merkle/store.rs

@@ -235,6 +235,12 @@ ///

/// This will return `NodeNotInStorage` if the element is not present in the store. pub fn get_node(&self, root: Word, index: NodeIndex) -> Result<Word, MerkleError> { let mut hash: RpoDigest = root.into(); + + // Check the root is in the storage when called with `NodeIndex::root()` + self.nodes + .get(&hash) + .ok_or(MerkleError::NodeNotInStorage(hash.into(), index))?; + for bit in index.bit_iterator().rev() { let node = self .nodes

@@ -383,18 +389,6 @@ .get_node(mtree.root(), NodeIndex::new(mtree.depth(), 3))

.expect("node 3 must be in tree"); Ok(()) - } - - #[test] - fn test_get_node_returns_self_for_root() { - let store = MerkleStore::default(); - let root_idx = NodeIndex::new(0, 0); - - // the root does not need any lookups in the storage itself, so the value is just returned - assert_eq!(store.get_node(LEAVES4[0], root_idx).unwrap(), LEAVES4[0]); - assert_eq!(store.get_node(LEAVES4[1], root_idx).unwrap(), LEAVES4[1]); - assert_eq!(store.get_node(LEAVES4[2], root_idx).unwrap(), LEAVES4[2]); - assert_eq!(store.get_node(LEAVES4[3], root_idx).unwrap(), LEAVES4[3]); } #[test]