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// EXPORTS
10// ================================================================================================
11
12pub use miden_crypto::{EMPTY_WORD, Felt, ONE, Word, ZERO};
13
14/// The number of field elements in a Miden word.
15pub const WORD_SIZE: usize = Word::NUM_ELEMENTS;
16
17pub mod advice;
18pub mod chiplets;
19pub mod events;
20pub mod mast;
21pub mod operations;
22pub mod precompile;
23pub mod program;
24pub mod proof;
25pub mod utils;
26
27pub mod field {
28    pub use miden_crypto::field::*;
29
30    pub type QuadFelt = BinomialExtensionField<super::Felt, 2>;
31}
32
33pub mod serde {
34    pub use miden_crypto::utils::{
35        BudgetedReader, ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable,
36        SliceReader,
37    };
38
39    /// Reads and validates a serialized length before it is used for allocation.
40    pub fn read_bounded_len<R: ByteReader>(
41        source: &mut R,
42        label: &str,
43        min_element_size: usize,
44    ) -> Result<usize, DeserializationError> {
45        let len = source.read_usize()?;
46        validate_bounded_len(source, label, len, min_element_size)?;
47        Ok(len)
48    }
49
50    /// Validates that a serialized length fits both the reader budget and remaining input.
51    pub fn validate_bounded_len<R: ByteReader>(
52        source: &R,
53        label: &str,
54        len: usize,
55        min_element_size: usize,
56    ) -> Result<(), DeserializationError> {
57        let max_len = source.max_alloc(min_element_size);
58        if len > max_len {
59            return Err(DeserializationError::InvalidValue(alloc::format!(
60                "{label} count {len} exceeds budget {max_len}"
61            )));
62        }
63
64        let min_bytes = len.checked_mul(min_element_size).ok_or_else(|| {
65            DeserializationError::InvalidValue(alloc::format!(
66                "{label} count {len} overflows minimum serialized size {min_element_size}"
67            ))
68        })?;
69        source.check_eor(min_bytes).map_err(|err| match err {
70            DeserializationError::UnexpectedEOF => DeserializationError::InvalidValue(
71                alloc::format!("{label} count {len} exceeds remaining input"),
72            ),
73            err => err,
74        })
75    }
76}
77
78pub mod crypto {
79    pub mod merkle {
80        pub use miden_crypto::merkle::{
81            EmptySubtreeRoots, InnerNodeInfo, MerkleError, MerklePath, MerkleTree, NodeIndex,
82            PartialMerkleTree,
83            mmr::{Mmr, MmrPeaks},
84            smt::{LeafIndex, SMT_DEPTH, SimpleSmt, Smt, SmtProof, SmtProofError},
85            store::{MerkleStore, StoreNode},
86        };
87    }
88
89    pub mod hash {
90        pub use miden_crypto::hash::{
91            blake::{Blake3_256, Blake3Digest},
92            keccak::Keccak256,
93            poseidon2::Poseidon2,
94            rpo::Rpo256,
95            rpx::Rpx256,
96            sha2::{Sha256, Sha512},
97        };
98    }
99
100    pub mod random {
101        pub use miden_crypto::rand::RandomCoin;
102    }
103
104    pub mod dsa {
105        pub use miden_crypto::dsa::{ecdsa_k256_keccak, eddsa_25519_sha512, falcon512_poseidon2};
106    }
107}
108
109pub mod prettier {
110    pub use miden_formatting::{prettier::*, pretty_via_display, pretty_via_to_string};
111}
112
113// CONSTANTS
114// ================================================================================================
115
116/// The initial value for the frame pointer, corresponding to the start address for procedure
117/// locals.
118pub const FMP_INIT_VALUE: Felt = Felt::new_unchecked(2_u64.pow(31));
119
120/// The address where the frame pointer is stored in memory.
121pub const FMP_ADDR: Felt = Felt::new_unchecked(u32::MAX as u64 - 1_u64);