miden_core_lib/lib.rs
1#![no_std]
2
3#[cfg(feature = "std")]
4extern crate std;
5
6#[cfg(any(feature = "constraints-tools", all(test, feature = "std")))]
7pub mod constraints_regen;
8pub mod dsa;
9pub mod handlers;
10
11extern crate alloc;
12
13use alloc::{sync::Arc, vec, vec::Vec};
14
15use miden_core::{
16 events::EventName, mast::MastForest, precompile::PrecompileVerifierRegistry,
17 serde::Deserializable,
18};
19use miden_mast_package::Package;
20use miden_processor::{HostLibrary, event::EventHandler};
21use miden_utils_sync::LazyLock;
22
23use crate::handlers::{
24 aead_decrypt::{AEAD_DECRYPT_EVENT_NAME, handle_aead_decrypt},
25 debug::default_debug_handlers,
26 ecdsa::{ECDSA_VERIFY_EVENT_NAME, EcdsaPrecompile},
27 eddsa_ed25519::{EDDSA25519_VERIFY_EVENT_NAME, EddsaPrecompile},
28 falcon_div::{FALCON_DIV_EVENT_NAME, handle_falcon_div},
29 keccak256::{KECCAK_HASH_BYTES_EVENT_NAME, KeccakPrecompile},
30 sha512::{SHA512_HASH_BYTES_EVENT_NAME, Sha512Precompile},
31 smt_peek::{SMT_PEEK_EVENT_NAME, handle_smt_peek},
32 sorted_array::{
33 LOWERBOUND_ARRAY_EVENT_NAME, LOWERBOUND_KEY_VALUE_EVENT_NAME, handle_lowerbound_array,
34 handle_lowerbound_key_value,
35 },
36 u64_div::{U64_DIV_EVENT_NAME, handle_u64_div},
37 u128_div::{U128_DIV_EVENT_NAME, handle_u128_div},
38 u256_div::{U256_DIV_EVENT_NAME, handle_u256_div},
39};
40
41// CORE LIBRARY
42// ================================================================================================
43
44/// The Miden core library, providing a set of optimized procedures for Miden programs.
45///
46/// This library wraps a [`Package`] containing highly-optimized and battle-tested implementations
47/// of commonly-used primitives. When the core library is dynamically linked during assembly time,
48/// procedures can be called from any Miden program and are serialized as 32 bytes, reducing the
49/// amount of code that needs to be shared between parties for proving and verifying program
50/// execution.
51///
52/// # Contents
53///
54/// The core library provides several categories of functionality:
55///
56/// - **Cryptographic primitives**: Hash functions (Keccak256, SHA-512), digital signature
57/// verification (ECDSA, EdDSA-Ed25519, Falcon), and authenticated encryption (AEAD decryption).
58/// - **Mathematical operations**: Division operations for u64, u128, and u256.
59/// - **Data structures**: Sparse Merkle Tree operations, Merkle Mountain Range (MMR), and sorted
60/// array utilities with lower-bound search capabilities.
61/// - **Memory operations**: Efficient hashing and "un-hashing" of large amounts of data.
62///
63/// Many of these operations are implemented as **precompiles** - special procedures that execute
64/// outside the Miden VM but are verified as part of the proof. Precompiles allow for efficient
65/// execution of complex operations that would be expensive to compute directly in the VM, while
66/// maintaining the security guarantees of the Miden proof system. The core library includes
67/// precompiles for cryptographic operations like hash functions and signature verification.
68///
69/// # Usage
70///
71/// The core library is typically used with the assembler to enable core library procedures
72/// in compiled programs:
73///
74/// ```rust,ignore
75/// use miden_assembly::{Assembler, Linkage};
76/// use miden_core_lib::CoreLibrary;
77///
78/// let core_lib = CoreLibrary::default();
79/// let assembler = Assembler::new(source_manager)
80/// .with_package(core_lib.package(), Linkage::Dynamic)
81/// .unwrap();
82/// ```
83///
84/// For program execution, you'll also need to register the event handlers:
85///
86/// ```rust,ignore
87/// # let core_lib = CoreLibrary::default();
88/// let handlers = core_lib.handlers();
89/// // Register handlers with your host...
90/// ```
91///
92/// Stack and memory print-style debug handlers are registered with stdout writers by default.
93/// These handlers can print private values if a program moves witness data onto the operand stack
94/// or into memory. Privacy-sensitive hosts should replace or unregister these handlers. Advice
95/// debug handlers can expose witness data directly, so hosts must opt into those explicitly.
96///
97/// For proof verification, use [`verifier_registry()`](Self::verifier_registry) to get the
98/// precompile verifiers required to validate core library precompile requests.
99///
100/// [`Package`]: miden_mast_package::Package
101#[derive(Clone)]
102pub struct CoreLibrary(Arc<Package>);
103
104impl AsRef<Package> for CoreLibrary {
105 fn as_ref(&self) -> &Package {
106 &self.0
107 }
108}
109
110impl From<&CoreLibrary> for HostLibrary {
111 fn from(core_lib: &CoreLibrary) -> Self {
112 Self {
113 mast_forest: core_lib.mast_forest().clone(),
114 package_debug_info: Ok(None),
115 handlers: core_lib.handlers(),
116 }
117 }
118}
119
120impl CoreLibrary {
121 /// Serialized representation of the Miden `core` package.
122 pub const SERIALIZED: &'static [u8] =
123 include_bytes!(concat!(env!("OUT_DIR"), "/assets/miden-core.masp"));
124
125 /// Returns a reference to the [MastForest] underlying the Miden core library.
126 pub fn mast_forest(&self) -> &Arc<MastForest> {
127 self.0.mast_forest()
128 }
129
130 /// Returns a reference to the underlying [`Arc<Package>`].
131 pub fn package(&self) -> Arc<Package> {
132 self.0.clone()
133 }
134
135 /// Returns the default event handlers required by the core library.
136 ///
137 /// Stack and memory print-style debug handlers write to stdout by default. These handlers can
138 /// print private values if a program moves witness data onto the operand stack or into memory.
139 /// Hosts can replace those handlers to route output to a UI, log, no-op handler, or other sink.
140 /// Advice debug handlers can expose witness data directly, so hosts must opt into those
141 /// explicitly by extending this handler set with
142 /// [`crate::handlers::debug::advice_debug_handlers`].
143 pub fn handlers(&self) -> Vec<(EventName, Arc<dyn EventHandler>)> {
144 let mut handlers: Vec<(EventName, Arc<dyn EventHandler>)> = vec![
145 (KECCAK_HASH_BYTES_EVENT_NAME, Arc::new(KeccakPrecompile)),
146 (SHA512_HASH_BYTES_EVENT_NAME, Arc::new(Sha512Precompile)),
147 (ECDSA_VERIFY_EVENT_NAME, Arc::new(EcdsaPrecompile)),
148 (EDDSA25519_VERIFY_EVENT_NAME, Arc::new(EddsaPrecompile)),
149 (SMT_PEEK_EVENT_NAME, Arc::new(handle_smt_peek)),
150 (U64_DIV_EVENT_NAME, Arc::new(handle_u64_div)),
151 (U128_DIV_EVENT_NAME, Arc::new(handle_u128_div)),
152 (U256_DIV_EVENT_NAME, Arc::new(handle_u256_div)),
153 (FALCON_DIV_EVENT_NAME, Arc::new(handle_falcon_div)),
154 (LOWERBOUND_ARRAY_EVENT_NAME, Arc::new(handle_lowerbound_array)),
155 (LOWERBOUND_KEY_VALUE_EVENT_NAME, Arc::new(handle_lowerbound_key_value)),
156 (AEAD_DECRYPT_EVENT_NAME, Arc::new(handle_aead_decrypt)),
157 ];
158 handlers.extend(default_debug_handlers());
159 handlers
160 }
161
162 /// Returns a [`PrecompileVerifierRegistry`] containing all verifiers required to validate
163 /// core library precompile requests.
164 pub fn verifier_registry(&self) -> PrecompileVerifierRegistry {
165 PrecompileVerifierRegistry::new()
166 .with_verifier(&KECCAK_HASH_BYTES_EVENT_NAME, Arc::new(KeccakPrecompile))
167 .with_verifier(&SHA512_HASH_BYTES_EVENT_NAME, Arc::new(Sha512Precompile))
168 .with_verifier(&ECDSA_VERIFY_EVENT_NAME, Arc::new(EcdsaPrecompile))
169 .with_verifier(&EDDSA25519_VERIFY_EVENT_NAME, Arc::new(EddsaPrecompile))
170 }
171}
172
173impl Default for CoreLibrary {
174 fn default() -> Self {
175 static CORELIB: LazyLock<CoreLibrary> = LazyLock::new(|| {
176 let contents = Package::read_from_bytes(CoreLibrary::SERIALIZED)
177 .expect("failed to read core package!");
178 CoreLibrary(Arc::new(contents))
179 });
180 CORELIB.clone()
181 }
182}
183
184// TESTS
185// ================================================================================================
186
187#[cfg(test)]
188mod tests {
189 use super::*;
190
191 #[test]
192 fn test_compile() {
193 let core_lib = CoreLibrary::default();
194 let exists = core_lib
195 .0
196 .get_procedure_root_by_path("::miden::core::math::u64::overflowing_add")
197 .is_some();
198
199 assert!(exists);
200 }
201}