miden_crypto/hash/algebraic_sponge/rescue/rpx/
mod.rs

1use super::{
2    ARK1, ARK2, CAPACITY_RANGE, CubeExtension, DIGEST_RANGE, ElementHasher, Felt, FieldElement,
3    Hasher, MDS, NUM_ROUNDS, RATE_RANGE, Range, STATE_WIDTH, Word, add_constants,
4    add_constants_and_apply_ext_round, add_constants_and_apply_inv_sbox,
5    add_constants_and_apply_sbox, apply_inv_sbox, apply_mds, apply_sbox,
6};
7#[cfg(test)]
8use super::{StarkField, ZERO};
9use crate::hash::algebraic_sponge::AlgebraicSponge;
10
11#[cfg(test)]
12mod tests;
13
14pub type CubicExtElement = CubeExtension<Felt>;
15
16// HASHER IMPLEMENTATION
17// ================================================================================================
18
19/// Implementation of the Rescue Prime eXtension hash function with 256-bit output.
20///
21/// The hash function is based on the XHash12 construction in [specifications](https://eprint.iacr.org/2023/1045)
22///
23/// The parameters used to instantiate the function are:
24/// * Field: 64-bit prime field with modulus 2^64 - 2^32 + 1.
25/// * State width: 12 field elements.
26/// * Capacity size: 4 field elements.
27/// * S-Box degree: 7.
28/// * Rounds: There are 3 different types of rounds:
29/// - (FB): `apply_mds` → `add_constants` → `apply_sbox` → `apply_mds` → `add_constants` →
30///   `apply_inv_sbox`.
31/// - (E): `add_constants` → `ext_sbox` (which is raising to power 7 in the degree 3 extension
32///   field).
33/// - (M): `apply_mds` → `add_constants`.
34/// * Permutation: (FB) (E) (FB) (E) (FB) (E) (M).
35///
36/// The above parameters target a 128-bit security level. The digest consists of four field elements
37/// and it can be serialized into 32 bytes (256 bits).
38///
39/// ## Hash output consistency
40/// Functions [hash_elements()](Rpx256::hash_elements), [merge()](Rpx256::merge), and
41/// [merge_with_int()](Rpx256::merge_with_int) are internally consistent. That is, computing
42/// a hash for the same set of elements using these functions will always produce the same
43/// result. For example, merging two digests using [merge()](Rpx256::merge) will produce the
44/// same result as hashing 8 elements which make up these digests using
45/// [hash_elements()](Rpx256::hash_elements) function.
46///
47/// However, [hash()](Rpx256::hash) function is not consistent with functions mentioned above.
48/// For example, if we take two field elements, serialize them to bytes and hash them using
49/// [hash()](Rpx256::hash), the result will differ from the result obtained by hashing these
50/// elements directly using [hash_elements()](Rpx256::hash_elements) function. The reason for
51/// this difference is that [hash()](Rpx256::hash) function needs to be able to handle
52/// arbitrary binary strings, which may or may not encode valid field elements - and thus,
53/// deserialization procedure used by this function is different from the procedure used to
54/// deserialize valid field elements.
55///
56/// Thus, if the underlying data consists of valid field elements, it might make more sense
57/// to deserialize them into field elements and then hash them using
58/// [hash_elements()](Rpx256::hash_elements) function rather than hashing the serialized bytes
59/// using [hash()](Rpx256::hash) function.
60///
61/// ## Domain separation
62/// [merge_in_domain()](Rpx256::merge_in_domain) hashes two digests into one digest with some domain
63/// identifier and the current implementation sets the second capacity element to the value of
64/// this domain identifier. Using a similar argument to the one formulated for domain separation
65/// in Appendix C of the [specifications](https://eprint.iacr.org/2023/1045), one sees that doing
66/// so degrades only pre-image resistance, from its initial bound of c.log_2(p), by as much as
67/// the log_2 of the size of the domain identifier space. Since pre-image resistance becomes
68/// the bottleneck for the security bound of the sponge in overwrite-mode only when it is
69/// lower than 2^128, we see that the target 128-bit security level is maintained as long as
70/// the size of the domain identifier space, including for padding, is less than 2^128.
71///
72/// ## Hashing of empty input
73/// The current implementation hashes empty input to the zero digest [0, 0, 0, 0]. This has
74/// the benefit of requiring no calls to the RPX permutation when hashing empty input.
75#[allow(rustdoc::private_intra_doc_links)]
76#[derive(Debug, Copy, Clone, Eq, PartialEq)]
77pub struct Rpx256();
78
79impl AlgebraicSponge for Rpx256 {
80    /// Applies RPX permutation to the provided state.
81    #[inline(always)]
82    fn apply_permutation(state: &mut [Felt; STATE_WIDTH]) {
83        Self::apply_fb_round(state, 0);
84        Self::apply_ext_round(state, 1);
85        Self::apply_fb_round(state, 2);
86        Self::apply_ext_round(state, 3);
87        Self::apply_fb_round(state, 4);
88        Self::apply_ext_round(state, 5);
89        Self::apply_final_round(state, 6);
90    }
91}
92
93impl Rpx256 {
94    // CONSTANTS
95    // --------------------------------------------------------------------------------------------
96
97    /// Sponge state is set to 12 field elements or 768 bytes; 8 elements are reserved for rate and
98    /// the remaining 4 elements are reserved for capacity.
99    pub const STATE_WIDTH: usize = STATE_WIDTH;
100
101    /// The rate portion of the state is located in elements 4 through 11 (inclusive).
102    pub const RATE_RANGE: Range<usize> = RATE_RANGE;
103
104    /// The capacity portion of the state is located in elements 0, 1, 2, and 3.
105    pub const CAPACITY_RANGE: Range<usize> = CAPACITY_RANGE;
106
107    /// The output of the hash function can be read from state elements 4, 5, 6, and 7.
108    pub const DIGEST_RANGE: Range<usize> = DIGEST_RANGE;
109
110    /// MDS matrix used for computing the linear layer in the (FB) and (E) rounds.
111    pub const MDS: [[Felt; STATE_WIDTH]; STATE_WIDTH] = MDS;
112
113    /// Round constants added to the hasher state in the first half of the round.
114    pub const ARK1: [[Felt; STATE_WIDTH]; NUM_ROUNDS] = ARK1;
115
116    /// Round constants added to the hasher state in the second half of the round.
117    pub const ARK2: [[Felt; STATE_WIDTH]; NUM_ROUNDS] = ARK2;
118
119    // TRAIT PASS-THROUGH FUNCTIONS
120    // --------------------------------------------------------------------------------------------
121
122    /// Returns a hash of the provided sequence of bytes.
123    #[inline(always)]
124    pub fn hash(bytes: &[u8]) -> Word {
125        <Self as Hasher>::hash(bytes)
126    }
127
128    /// Returns a hash of two digests. This method is intended for use in construction of
129    /// Merkle trees and verification of Merkle paths.
130    #[inline(always)]
131    pub fn merge(values: &[Word; 2]) -> Word {
132        <Self as Hasher>::merge(values)
133    }
134
135    /// Returns a hash of the provided field elements.
136    #[inline(always)]
137    pub fn hash_elements<E: FieldElement<BaseField = Felt>>(elements: &[E]) -> Word {
138        <Self as ElementHasher>::hash_elements(elements)
139    }
140
141    /// Returns a hash of two digests and a domain identifier.
142    #[inline(always)]
143    pub fn merge_in_domain(values: &[Word; 2], domain: Felt) -> Word {
144        <Self as AlgebraicSponge>::merge_in_domain(values, domain)
145    }
146
147    // RPX PERMUTATION
148    // --------------------------------------------------------------------------------------------
149
150    /// Applies RPX permutation to the provided state.
151    #[inline(always)]
152    pub fn apply_permutation(state: &mut [Felt; STATE_WIDTH]) {
153        Self::apply_fb_round(state, 0);
154        Self::apply_ext_round(state, 1);
155        Self::apply_fb_round(state, 2);
156        Self::apply_ext_round(state, 3);
157        Self::apply_fb_round(state, 4);
158        Self::apply_ext_round(state, 5);
159        Self::apply_final_round(state, 6);
160    }
161
162    // RPX PERMUTATION ROUND FUNCTIONS
163    // --------------------------------------------------------------------------------------------
164
165    /// (FB) round function.
166    #[inline(always)]
167    pub fn apply_fb_round(state: &mut [Felt; STATE_WIDTH], round: usize) {
168        apply_mds(state);
169        if !add_constants_and_apply_sbox(state, &ARK1[round]) {
170            add_constants(state, &ARK1[round]);
171            apply_sbox(state);
172        }
173
174        apply_mds(state);
175        if !add_constants_and_apply_inv_sbox(state, &ARK2[round]) {
176            add_constants(state, &ARK2[round]);
177            apply_inv_sbox(state);
178        }
179    }
180
181    /// (E) round function.
182    ///
183    /// It first attempts to run the optimized (SIMD-accelerated) implementation.
184    /// If SIMD acceleration is not available for the current target it falls
185    /// back to the scalar reference implementation (`apply_ext_round_ref`).
186    #[inline(always)]
187    pub fn apply_ext_round(state: &mut [Felt; STATE_WIDTH], round: usize) {
188        if !add_constants_and_apply_ext_round(state, &ARK1[round]) {
189            Self::apply_ext_round_ref(state, round);
190        }
191    }
192
193    /// Scalar (reference) implementation of the (E) round function.
194    ///
195    /// This version performs the round without SIMD acceleration and is used
196    /// as a fallback when optimized implementations are not available.
197    #[inline(always)]
198    fn apply_ext_round_ref(state: &mut [Felt; STATE_WIDTH], round: usize) {
199        // add constants
200        add_constants(state, &ARK1[round]);
201
202        // decompose the state into 4 elements in the cubic extension field and apply the power 7
203        // map to each of the elements
204        let [s0, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11] = *state;
205        let ext0 = Self::exp7(CubicExtElement::new(s0, s1, s2));
206        let ext1 = Self::exp7(CubicExtElement::new(s3, s4, s5));
207        let ext2 = Self::exp7(CubicExtElement::new(s6, s7, s8));
208        let ext3 = Self::exp7(CubicExtElement::new(s9, s10, s11));
209
210        // decompose the state back into 12 base field elements
211        let arr_ext = [ext0, ext1, ext2, ext3];
212        *state = CubicExtElement::slice_as_base_elements(&arr_ext)
213            .try_into()
214            .expect("shouldn't fail");
215    }
216
217    /// (M) round function.
218    #[inline(always)]
219    pub fn apply_final_round(state: &mut [Felt; STATE_WIDTH], round: usize) {
220        apply_mds(state);
221        add_constants(state, &ARK1[round]);
222    }
223
224    /// Computes an exponentiation to the power 7 in cubic extension field.
225    #[inline(always)]
226    pub fn exp7(x: CubeExtension<Felt>) -> CubeExtension<Felt> {
227        let x2 = x.square();
228        let x4 = x2.square();
229
230        let x3 = x2 * x;
231        x3 * x4
232    }
233}
234
235impl Hasher for Rpx256 {
236    const COLLISION_RESISTANCE: u32 = 128;
237
238    type Digest = Word;
239
240    fn hash(bytes: &[u8]) -> Self::Digest {
241        <Self as AlgebraicSponge>::hash(bytes)
242    }
243
244    fn merge(values: &[Self::Digest; 2]) -> Self::Digest {
245        <Self as AlgebraicSponge>::merge(values)
246    }
247
248    fn merge_many(values: &[Self::Digest]) -> Self::Digest {
249        <Self as AlgebraicSponge>::merge_many(values)
250    }
251
252    fn merge_with_int(seed: Self::Digest, value: u64) -> Self::Digest {
253        <Self as AlgebraicSponge>::merge_with_int(seed, value)
254    }
255}
256
257impl ElementHasher for Rpx256 {
258    type BaseField = Felt;
259
260    fn hash_elements<E: FieldElement<BaseField = Self::BaseField>>(elements: &[E]) -> Self::Digest {
261        <Self as AlgebraicSponge>::hash_elements(elements)
262    }
263}