miden_crypto/
lib.rs

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