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/// An alias for a key-value map.
40///
41/// By default, this is an alias for the [`alloc::collections::BTreeMap`], however, when the
42/// `hashmaps` feature is enabled, this is an alias for the `hashbrown`'s `HashMap`.
43#[cfg(not(feature = "hashmaps"))]
44pub type Map<K, V> = alloc::collections::BTreeMap<K, V>;
45
46// CONSTANTS
47// ================================================================================================
48
49/// Number of field elements in a word.
50pub const WORD_SIZE: usize = 4;
51
52/// Field element representing ZERO in the Miden base filed.
53pub const ZERO: Felt = Felt::ZERO;
54
55/// Field element representing ONE in the Miden base filed.
56pub const ONE: Felt = Felt::ONE;
57
58/// Array of field elements representing word of ZEROs in the Miden base field.
59pub const EMPTY_WORD: Word = Word::new([ZERO; WORD_SIZE]);
60
61// TRAITS
62// ================================================================================================
63
64/// Defines how to compute a commitment to an object represented as a sequence of field elements.
65pub trait SequentialCommit {
66    /// A type of the commitment which must be derivable from [Word].
67    type Commitment: From<Word>;
68
69    /// Computes the commitment to the object.
70    ///
71    /// The default implementation of this function uses RPO256 hash function to hash the sequence
72    /// of elements returned from [Self::to_elements()].
73    fn to_commitment(&self) -> Self::Commitment {
74        hash::rpo::Rpo256::hash_elements(&self.to_elements()).into()
75    }
76
77    /// Returns a representation of the object as a sequence of fields elements.
78    fn to_elements(&self) -> alloc::vec::Vec<Felt>;
79}
80
81// TESTS
82// ================================================================================================
83
84#[test]
85#[should_panic]
86fn debug_assert_is_checked() {
87    // enforce the release checks to always have `RUSTFLAGS="-C debug-assertions".
88    //
89    // some upstream tests are performed with `debug_assert`, and we want to assert its correctness
90    // downstream.
91    //
92    // for reference, check
93    // https://github.com/0xMiden/miden-vm/issues/433
94    debug_assert!(false);
95}
96
97#[test]
98#[should_panic]
99#[allow(arithmetic_overflow)]
100fn overflow_panics_for_test() {
101    // overflows might be disabled if tests are performed in release mode. these are critical,
102    // mandatory checks as overflows might be attack vectors.
103    //
104    // to enable overflow checks in release mode, ensure `RUSTFLAGS="-C overflow-checks"`
105    let a = 1_u64;
106    let b = 64;
107    assert_ne!(a << b, 0);
108}