Skip to main content

miden_crypto/
lib.rs

1#![no_std]
2
3#[macro_use]
4extern crate alloc;
5#[cfg(feature = "std")]
6extern crate std;
7
8pub mod aead;
9pub mod dsa;
10pub mod ecdh;
11pub mod hash;
12pub mod ies;
13pub mod merkle;
14pub mod rand;
15pub mod utils;
16pub mod word;
17
18// RE-EXPORTS
19// ================================================================================================
20
21pub use k256::elliptic_curve::zeroize;
22pub use winter_math::{
23    FieldElement, StarkField,
24    fields::{CubeExtension, QuadExtension, f64::BaseElement as Felt},
25};
26pub use word::{Word, WordError};
27
28// TYPE ALIASES
29// ================================================================================================
30
31/// An alias for a key-value map.
32///
33/// By default, this is an alias for the [`alloc::collections::BTreeMap`], however, when the
34/// `hashmaps` feature is enabled, this is an alias for the `hashbrown`'s `HashMap`.
35#[cfg(feature = "hashmaps")]
36pub type Map<K, V> = hashbrown::HashMap<K, V>;
37
38#[cfg(feature = "hashmaps")]
39pub use hashbrown::hash_map::Entry as MapEntry;
40#[cfg(feature = "hashmaps")]
41pub use hashbrown::hash_map::IntoIter as MapIntoIter;
42
43/// An alias for a key-value map.
44///
45/// By default, this is an alias for the [`alloc::collections::BTreeMap`], however, when the
46/// `hashmaps` feature is enabled, this is an alias for the `hashbrown`'s `HashMap`.
47#[cfg(not(feature = "hashmaps"))]
48pub type Map<K, V> = alloc::collections::BTreeMap<K, V>;
49
50#[cfg(not(feature = "hashmaps"))]
51pub use alloc::collections::btree_map::Entry as MapEntry;
52#[cfg(not(feature = "hashmaps"))]
53pub use alloc::collections::btree_map::IntoIter as MapIntoIter;
54
55/// An alias for a simple set.
56///
57/// By default, this is an alias for the [`alloc::collections::BTreeSet`]. However, when the
58/// `hashmaps` feature is enabled, this becomes an alias for hashbrown's HashSet.
59#[cfg(feature = "hashmaps")]
60pub type Set<V> = hashbrown::HashSet<V>;
61
62/// An alias for a simple set.
63///
64/// By default, this is an alias for the [`alloc::collections::BTreeSet`]. However, when the
65/// `hashmaps` feature is enabled, this becomes an alias for hashbrown's HashSet.
66#[cfg(not(feature = "hashmaps"))]
67pub type Set<V> = alloc::collections::BTreeSet<V>;
68
69// CONSTANTS
70// ================================================================================================
71
72/// Number of field elements in a word.
73pub const WORD_SIZE: usize = 4;
74
75/// Field element representing ZERO in the Miden base filed.
76pub const ZERO: Felt = Felt::ZERO;
77
78/// Field element representing ONE in the Miden base filed.
79pub const ONE: Felt = Felt::ONE;
80
81/// Array of field elements representing word of ZEROs in the Miden base field.
82pub const EMPTY_WORD: Word = Word::new([ZERO; WORD_SIZE]);
83
84// TRAITS
85// ================================================================================================
86
87/// Defines how to compute a commitment to an object represented as a sequence of field elements.
88pub trait SequentialCommit {
89    /// A type of the commitment which must be derivable from [Word].
90    type Commitment: From<Word>;
91
92    /// Computes the commitment to the object.
93    ///
94    /// The default implementation of this function uses RPO256 hash function to hash the sequence
95    /// of elements returned from [Self::to_elements()].
96    fn to_commitment(&self) -> Self::Commitment {
97        hash::rpo::Rpo256::hash_elements(&self.to_elements()).into()
98    }
99
100    /// Returns a representation of the object as a sequence of fields elements.
101    fn to_elements(&self) -> alloc::vec::Vec<Felt>;
102}
103
104// TESTS
105// ================================================================================================
106
107#[test]
108#[should_panic]
109fn debug_assert_is_checked() {
110    // enforce the release checks to always have `RUSTFLAGS="-C debug-assertions".
111    //
112    // some upstream tests are performed with `debug_assert`, and we want to assert its correctness
113    // downstream.
114    //
115    // for reference, check
116    // https://github.com/0xMiden/miden-vm/issues/433
117    debug_assert!(false);
118}
119
120#[test]
121#[should_panic]
122#[allow(arithmetic_overflow)]
123fn overflow_panics_for_test() {
124    // overflows might be disabled if tests are performed in release mode. these are critical,
125    // mandatory checks as overflows might be attack vectors.
126    //
127    // to enable overflow checks in release mode, ensure `RUSTFLAGS="-C overflow-checks"`
128    let a = 1_u64;
129    let b = 64;
130    assert_ne!(a << b, 0);
131}