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