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