Skip to main content

miden_core/
lib.rs

1#![no_std]
2
3#[macro_use]
4extern crate alloc;
5
6#[cfg(feature = "std")]
7extern crate std;
8
9// ASSERT MATCHES MACRO
10// ================================================================================================
11
12/// This is an implementation of `std::assert_matches::assert_matches`
13/// so it can be removed when that feature stabilizes upstream
14#[macro_export]
15macro_rules! assert_matches {
16    ($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )? $(,)?) => {
17        match $left {
18            $( $pattern )|+ $( if $guard )? => {}
19            ref left_val => {
20                panic!(r#"
21assertion failed: `(left matches right)`
22    left: `{:?}`,
23    right: `{}`"#, left_val, stringify!($($pattern)|+ $(if $guard)?));
24            }
25        }
26    };
27
28    ($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )?, $msg:literal $(,)?) => {
29        match $left {
30            $( $pattern )|+ $( if $guard )? => {}
31            ref left_val => {
32                panic!(concat!(r#"
33assertion failed: `(left matches right)`
34    left: `{:?}`,
35    right: `{}`
36"#, $msg), left_val, stringify!($($pattern)|+ $(if $guard)?));
37            }
38        }
39    };
40
41    ($left:expr, $(|)? $( $pattern:pat_param )|+ $( if $guard: expr )?, $msg:literal, $($arg:tt)+) => {
42        match $left {
43            $( $pattern )|+ $( if $guard )? => {}
44            ref left_val => {
45                panic!(concat!(r#"
46assertion failed: `(left matches right)`
47    left: `{:?}`,
48    right: `{}`
49"#, $msg), left_val, stringify!($($pattern)|+ $(if $guard)?), $($arg)+);
50            }
51        }
52    }
53}
54
55// EXPORTS
56// ================================================================================================
57
58pub use miden_crypto::{EMPTY_WORD, Felt, ONE, Word, ZERO};
59
60/// The number of field elements in a Miden word.
61pub const WORD_SIZE: usize = Word::NUM_ELEMENTS;
62
63pub mod advice;
64pub mod chiplets;
65pub mod events;
66pub mod mast;
67pub mod operations;
68pub mod precompile;
69pub mod program;
70pub mod proof;
71pub mod utils;
72
73pub mod field {
74    pub use miden_crypto::field::*;
75
76    pub type QuadFelt = BinomialExtensionField<super::Felt, 2>;
77}
78
79pub mod serde {
80    pub use miden_crypto::utils::{
81        BudgetedReader, ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable,
82        SliceReader,
83    };
84}
85
86pub mod crypto {
87    pub mod merkle {
88        pub use miden_crypto::merkle::{
89            EmptySubtreeRoots, InnerNodeInfo, MerkleError, MerklePath, MerkleTree, NodeIndex,
90            PartialMerkleTree,
91            mmr::{Mmr, MmrPeaks},
92            smt::{LeafIndex, SMT_DEPTH, SimpleSmt, Smt, SmtProof, SmtProofError},
93            store::{MerkleStore, StoreNode},
94        };
95    }
96
97    pub mod hash {
98        pub use miden_crypto::hash::{
99            blake::{Blake3_256, Blake3Digest},
100            keccak::Keccak256,
101            poseidon2::Poseidon2,
102            rpo::Rpo256,
103            rpx::Rpx256,
104            sha2::{Sha256, Sha512},
105        };
106    }
107
108    pub mod random {
109        pub use miden_crypto::rand::RandomCoin;
110    }
111
112    pub mod dsa {
113        pub use miden_crypto::dsa::{ecdsa_k256_keccak, eddsa_25519_sha512, falcon512_poseidon2};
114    }
115}
116
117pub mod prettier {
118    pub use miden_formatting::{prettier::*, pretty_via_display, pretty_via_to_string};
119
120    /// Pretty-print a list of [PrettyPrint] values as comma-separated items.
121    pub fn pretty_print_csv<'a, T>(items: impl IntoIterator<Item = &'a T>) -> Document
122    where
123        T: PrettyPrint + 'a,
124    {
125        let mut doc = Document::Empty;
126        for (i, item) in items.into_iter().enumerate() {
127            if i > 0 {
128                doc += const_text(", ");
129            }
130            doc += item.render();
131        }
132        doc
133    }
134}
135
136// CONSTANTS
137// ================================================================================================
138
139/// The initial value for the frame pointer, corresponding to the start address for procedure
140/// locals.
141pub const FMP_INIT_VALUE: Felt = Felt::new_unchecked(2_u64.pow(31));
142
143/// The address where the frame pointer is stored in memory.
144pub const FMP_ADDR: Felt = Felt::new_unchecked(u32::MAX as u64 - 1_u64);