miden_processor/host/mast_forest_store.rs
1use alloc::{collections::BTreeMap, sync::Arc};
2
3use miden_core::{Word, mast::MastForest};
4
5/// A set of [`MastForest`]s available to the prover that programs may refer to (by means of an
6/// [`miden_core::mast::ExternalNode`]).
7///
8/// For example, a program's kernel and standard library would most likely not be compiled directly
9/// with the program, and instead be provided separately to the prover. This has the benefit of
10/// reducing program binary size. The store could also be much more complex, such as accessing a
11/// centralized registry of [`MastForest`]s when it doesn't find one locally.
12pub trait MastForestStore {
13 /// Returns a [`MastForest`] which is guaranteed to contain a procedure with the provided
14 /// procedure hash as one of its procedure, if any.
15 fn get(&self, procedure_hash: &Word) -> Option<Arc<MastForest>>;
16}
17
18/// A simple [`MastForestStore`] where all known [`MastForest`]s are held in memory.
19#[derive(Debug, Default, Clone)]
20pub struct MemMastForestStore {
21 mast_forests: BTreeMap<Word, Arc<MastForest>>,
22}
23
24impl MemMastForestStore {
25 /// Inserts all the procedures of the provided MAST forest in the store.
26 pub fn insert(&mut self, mast_forest: Arc<MastForest>) {
27 // only register the procedures which are local to this forest
28 for proc_digest in mast_forest.local_procedure_digests() {
29 self.mast_forests.insert(proc_digest, mast_forest.clone());
30 }
31 }
32}
33
34impl MastForestStore for MemMastForestStore {
35 fn get(&self, procedure_hash: &Word) -> Option<Arc<MastForest>> {
36 self.mast_forests.get(procedure_hash).cloned()
37 }
38}