miden_lib/
lib.rs

1#![no_std]
2
3use alloc::sync::Arc;
4
5#[macro_use]
6extern crate alloc;
7
8#[cfg(feature = "std")]
9extern crate std;
10
11use miden_objects::assembly::Library;
12use miden_objects::assembly::mast::MastForest;
13use miden_objects::utils::serde::Deserializable;
14use miden_objects::utils::sync::LazyLock;
15
16mod auth;
17pub use auth::AuthScheme;
18
19pub mod account;
20pub mod errors;
21pub mod note;
22pub mod transaction;
23pub mod utils;
24
25#[cfg(any(feature = "testing", test))]
26pub mod testing;
27
28// RE-EXPORTS
29// ================================================================================================
30pub use miden_stdlib::StdLibrary;
31
32// CONSTANTS
33// ================================================================================================
34
35const MIDEN_LIB_BYTES: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/assets/miden.masl"));
36
37// MIDEN LIBRARY
38// ================================================================================================
39
40#[derive(Clone)]
41pub struct MidenLib(Library);
42
43impl MidenLib {
44    /// Returns a reference to the [`MastForest`] of the inner [`Library`].
45    pub fn mast_forest(&self) -> &Arc<MastForest> {
46        self.0.mast_forest()
47    }
48}
49
50impl AsRef<Library> for MidenLib {
51    fn as_ref(&self) -> &Library {
52        &self.0
53    }
54}
55
56impl From<MidenLib> for Library {
57    fn from(value: MidenLib) -> Self {
58        value.0
59    }
60}
61
62impl Default for MidenLib {
63    fn default() -> Self {
64        static MIDEN_LIB: LazyLock<MidenLib> = LazyLock::new(|| {
65            let contents =
66                Library::read_from_bytes(MIDEN_LIB_BYTES).expect("failed to read miden lib masl!");
67            MidenLib(contents)
68        });
69        MIDEN_LIB.clone()
70    }
71}
72
73// TESTS
74// ================================================================================================
75
76// NOTE: Most kernel-related tests can be found under /miden-tx/kernel_tests
77#[cfg(all(test, feature = "std"))]
78mod tests {
79    use miden_objects::assembly::LibraryPath;
80
81    use super::MidenLib;
82
83    #[test]
84    fn test_compile() {
85        let path = "miden::account::get_id".parse::<LibraryPath>().unwrap();
86        let miden = MidenLib::default();
87        let exists = miden.0.module_infos().any(|module| {
88            module
89                .procedures()
90                .any(|(_, proc)| module.path().clone().append(&proc.name).unwrap() == path)
91        });
92
93        assert!(exists);
94    }
95}