miden_crypto/hash/rescue/rpx/
mod.rs

1use core::ops::Range;
2
3use super::{
4    ARK1, ARK2, BINARY_CHUNK_SIZE, CAPACITY_RANGE, CubeExtension, DIGEST_RANGE, ElementHasher,
5    Felt, FieldElement, Hasher, INPUT1_RANGE, INPUT2_RANGE, MDS, NUM_ROUNDS, RATE_RANGE,
6    RATE_WIDTH, STATE_WIDTH, StarkField, ZERO, add_constants, add_constants_and_apply_inv_sbox,
7    add_constants_and_apply_sbox, apply_inv_sbox, apply_mds, apply_sbox,
8};
9use crate::Word;
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#[derive(Debug, Copy, Clone, Eq, PartialEq)]
76pub struct Rpx256();
77
78impl Hasher for Rpx256 {
79    /// Rpx256 collision resistance is 128-bits.
80    const COLLISION_RESISTANCE: u32 = 128;
81
82    type Digest = Word;
83
84    fn hash(bytes: &[u8]) -> Self::Digest {
85        // initialize the state with zeroes
86        let mut state = [ZERO; STATE_WIDTH];
87
88        // determine the number of field elements needed to encode `bytes` when each field element
89        // represents at most 7 bytes.
90        let num_field_elem = bytes.len().div_ceil(BINARY_CHUNK_SIZE);
91
92        // set the first capacity element to `RATE_WIDTH + (num_field_elem % RATE_WIDTH)`. We do
93        // this to achieve:
94        // 1. Domain separating hashing of `[u8]` from hashing of `[Felt]`.
95        // 2. Avoiding collisions at the `[Felt]` representation of the encoded bytes.
96        state[CAPACITY_RANGE.start] =
97            Felt::from((RATE_WIDTH + (num_field_elem % RATE_WIDTH)) as u8);
98
99        // initialize a buffer to receive the little-endian elements.
100        let mut buf = [0_u8; 8];
101
102        // iterate the chunks of bytes, creating a field element from each chunk and copying it
103        // into the state.
104        //
105        // every time the rate range is filled, a permutation is performed. if the final value of
106        // `rate_pos` is not zero, then the chunks count wasn't enough to fill the state range,
107        // and an additional permutation must be performed.
108        let mut current_chunk_idx = 0_usize;
109        // handle the case of an empty `bytes`
110        let last_chunk_idx = if num_field_elem == 0 {
111            current_chunk_idx
112        } else {
113            num_field_elem - 1
114        };
115        let rate_pos = bytes.chunks(BINARY_CHUNK_SIZE).fold(0, |rate_pos, chunk| {
116            // copy the chunk into the buffer
117            if current_chunk_idx != last_chunk_idx {
118                buf[..BINARY_CHUNK_SIZE].copy_from_slice(chunk);
119            } else {
120                // on the last iteration, we pad `buf` with a 1 followed by as many 0's as are
121                // needed to fill it
122                buf.fill(0);
123                buf[..chunk.len()].copy_from_slice(chunk);
124                buf[chunk.len()] = 1;
125            }
126            current_chunk_idx += 1;
127
128            // set the current rate element to the input. since we take at most 7 bytes, we are
129            // guaranteed that the inputs data will fit into a single field element.
130            state[RATE_RANGE.start + rate_pos] = Felt::new(u64::from_le_bytes(buf));
131
132            // proceed filling the range. if it's full, then we apply a permutation and reset the
133            // counter to the beginning of the range.
134            if rate_pos == RATE_WIDTH - 1 {
135                Self::apply_permutation(&mut state);
136                0
137            } else {
138                rate_pos + 1
139            }
140        });
141
142        // if we absorbed some elements but didn't apply a permutation to them (would happen when
143        // the number of elements is not a multiple of RATE_WIDTH), apply the RPX permutation. we
144        // don't need to apply any extra padding because the first capacity element contains a
145        // flag indicating the number of field elements constituting the last block when the latter
146        // is not divisible by `RATE_WIDTH`.
147        if rate_pos != 0 {
148            state[RATE_RANGE.start + rate_pos..RATE_RANGE.end].fill(ZERO);
149            Self::apply_permutation(&mut state);
150        }
151
152        // return the first 4 elements of the rate as hash result.
153        Word::new(state[DIGEST_RANGE].try_into().unwrap())
154    }
155
156    fn merge(values: &[Self::Digest; 2]) -> Self::Digest {
157        // initialize the state by copying the digest elements into the rate portion of the state
158        // (8 total elements), and set the capacity elements to 0.
159        let mut state = [ZERO; STATE_WIDTH];
160        let it = Self::Digest::words_as_elements_iter(values.iter());
161        for (i, v) in it.enumerate() {
162            state[RATE_RANGE.start + i] = *v;
163        }
164
165        // apply the RPX permutation and return the first four elements of the state
166        Self::apply_permutation(&mut state);
167        Word::new(state[DIGEST_RANGE].try_into().unwrap())
168    }
169
170    fn merge_many(values: &[Self::Digest]) -> Self::Digest {
171        Self::hash_elements(Self::Digest::words_as_elements(values))
172    }
173
174    fn merge_with_int(seed: Self::Digest, value: u64) -> Self::Digest {
175        // initialize the state as follows:
176        // - seed is copied into the first 4 elements of the rate portion of the state.
177        // - if the value fits into a single field element, copy it into the fifth rate element and
178        //   set the first capacity element to 5.
179        // - if the value doesn't fit into a single field element, split it into two field elements,
180        //   copy them into rate elements 5 and 6 and set the first capacity element to 6.
181        let mut state = [ZERO; STATE_WIDTH];
182        state[INPUT1_RANGE].copy_from_slice(seed.as_elements());
183        state[INPUT2_RANGE.start] = Felt::new(value);
184        if value < Felt::MODULUS {
185            state[CAPACITY_RANGE.start] = Felt::from(5_u8);
186        } else {
187            state[INPUT2_RANGE.start + 1] = Felt::new(value / Felt::MODULUS);
188            state[CAPACITY_RANGE.start] = Felt::from(6_u8);
189        }
190
191        // apply the RPX permutation and return the first four elements of the rate
192        Self::apply_permutation(&mut state);
193        Word::new(state[DIGEST_RANGE].try_into().unwrap())
194    }
195}
196
197impl ElementHasher for Rpx256 {
198    type BaseField = Felt;
199
200    fn hash_elements<E: FieldElement<BaseField = Self::BaseField>>(elements: &[E]) -> Self::Digest {
201        // convert the elements into a list of base field elements
202        let elements = E::slice_as_base_elements(elements);
203
204        // initialize state to all zeros, except for the first element of the capacity part, which
205        // is set to `elements.len() % RATE_WIDTH`.
206        let mut state = [ZERO; STATE_WIDTH];
207        state[CAPACITY_RANGE.start] = Self::BaseField::from((elements.len() % RATE_WIDTH) as u8);
208
209        // absorb elements into the state one by one until the rate portion of the state is filled
210        // up; then apply the Rescue permutation and start absorbing again; repeat until all
211        // elements have been absorbed
212        let mut i = 0;
213        for &element in elements.iter() {
214            state[RATE_RANGE.start + i] = element;
215            i += 1;
216            if i % RATE_WIDTH == 0 {
217                Self::apply_permutation(&mut state);
218                i = 0;
219            }
220        }
221
222        // if we absorbed some elements but didn't apply a permutation to them (would happen when
223        // the number of elements is not a multiple of RATE_WIDTH), apply the RPX permutation after
224        // padding by as many 0 as necessary to make the input length a multiple of the RATE_WIDTH.
225        if i > 0 {
226            while i != RATE_WIDTH {
227                state[RATE_RANGE.start + i] = ZERO;
228                i += 1;
229            }
230            Self::apply_permutation(&mut state);
231        }
232
233        // return the first 4 elements of the state as hash result
234        Word::new(state[DIGEST_RANGE].try_into().unwrap())
235    }
236}
237
238// HASH FUNCTION IMPLEMENTATION
239// ================================================================================================
240
241impl Rpx256 {
242    // CONSTANTS
243    // --------------------------------------------------------------------------------------------
244
245    /// Sponge state is set to 12 field elements or 768 bytes; 8 elements are reserved for rate and
246    /// the remaining 4 elements are reserved for capacity.
247    pub const STATE_WIDTH: usize = STATE_WIDTH;
248
249    /// The rate portion of the state is located in elements 4 through 11 (inclusive).
250    pub const RATE_RANGE: Range<usize> = RATE_RANGE;
251
252    /// The capacity portion of the state is located in elements 0, 1, 2, and 3.
253    pub const CAPACITY_RANGE: Range<usize> = CAPACITY_RANGE;
254
255    /// The output of the hash function can be read from state elements 4, 5, 6, and 7.
256    pub const DIGEST_RANGE: Range<usize> = DIGEST_RANGE;
257
258    /// MDS matrix used for computing the linear layer in the (FB) and (E) rounds.
259    pub const MDS: [[Felt; STATE_WIDTH]; STATE_WIDTH] = MDS;
260
261    /// Round constants added to the hasher state in the first half of the round.
262    pub const ARK1: [[Felt; STATE_WIDTH]; NUM_ROUNDS] = ARK1;
263
264    /// Round constants added to the hasher state in the second half of the round.
265    pub const ARK2: [[Felt; STATE_WIDTH]; NUM_ROUNDS] = ARK2;
266
267    // TRAIT PASS-THROUGH FUNCTIONS
268    // --------------------------------------------------------------------------------------------
269
270    /// Returns a hash of the provided sequence of bytes.
271    #[inline(always)]
272    pub fn hash(bytes: &[u8]) -> Word {
273        <Self as Hasher>::hash(bytes)
274    }
275
276    /// Returns a hash of two digests. This method is intended for use in construction of
277    /// Merkle trees and verification of Merkle paths.
278    #[inline(always)]
279    pub fn merge(values: &[Word; 2]) -> Word {
280        <Self as Hasher>::merge(values)
281    }
282
283    /// Returns a hash of the provided field elements.
284    #[inline(always)]
285    pub fn hash_elements<E: FieldElement<BaseField = Felt>>(elements: &[E]) -> Word {
286        <Self as ElementHasher>::hash_elements(elements)
287    }
288
289    // DOMAIN IDENTIFIER
290    // --------------------------------------------------------------------------------------------
291
292    /// Returns a hash of two digests and a domain identifier.
293    pub fn merge_in_domain(values: &[Word; 2], domain: Felt) -> Word {
294        // initialize the state by copying the digest elements into the rate portion of the state
295        // (8 total elements), and set the capacity elements to 0.
296        let mut state = [ZERO; STATE_WIDTH];
297        let it = Word::words_as_elements_iter(values.iter());
298        for (i, v) in it.enumerate() {
299            state[RATE_RANGE.start + i] = *v;
300        }
301
302        // set the second capacity element to the domain value. The first capacity element is used
303        // for padding purposes.
304        state[CAPACITY_RANGE.start + 1] = domain;
305
306        // apply the RPX permutation and return the first four elements of the state
307        Self::apply_permutation(&mut state);
308        Word::new(state[DIGEST_RANGE].try_into().unwrap())
309    }
310
311    // RPX PERMUTATION
312    // --------------------------------------------------------------------------------------------
313
314    /// Applies RPX permutation to the provided state.
315    #[inline(always)]
316    pub fn apply_permutation(state: &mut [Felt; STATE_WIDTH]) {
317        Self::apply_fb_round(state, 0);
318        Self::apply_ext_round(state, 1);
319        Self::apply_fb_round(state, 2);
320        Self::apply_ext_round(state, 3);
321        Self::apply_fb_round(state, 4);
322        Self::apply_ext_round(state, 5);
323        Self::apply_final_round(state, 6);
324    }
325
326    // RPX PERMUTATION ROUND FUNCTIONS
327    // --------------------------------------------------------------------------------------------
328
329    /// (FB) round function.
330    #[inline(always)]
331    pub fn apply_fb_round(state: &mut [Felt; STATE_WIDTH], round: usize) {
332        apply_mds(state);
333        if !add_constants_and_apply_sbox(state, &ARK1[round]) {
334            add_constants(state, &ARK1[round]);
335            apply_sbox(state);
336        }
337
338        apply_mds(state);
339        if !add_constants_and_apply_inv_sbox(state, &ARK2[round]) {
340            add_constants(state, &ARK2[round]);
341            apply_inv_sbox(state);
342        }
343    }
344
345    /// (E) round function.
346    #[inline(always)]
347    pub fn apply_ext_round(state: &mut [Felt; STATE_WIDTH], round: usize) {
348        // add constants
349        add_constants(state, &ARK1[round]);
350
351        // decompose the state into 4 elements in the cubic extension field and apply the power 7
352        // map to each of the elements
353        let [s0, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11] = *state;
354        let ext0 = Self::exp7(CubicExtElement::new(s0, s1, s2));
355        let ext1 = Self::exp7(CubicExtElement::new(s3, s4, s5));
356        let ext2 = Self::exp7(CubicExtElement::new(s6, s7, s8));
357        let ext3 = Self::exp7(CubicExtElement::new(s9, s10, s11));
358
359        // decompose the state back into 12 base field elements
360        let arr_ext = [ext0, ext1, ext2, ext3];
361        *state = CubicExtElement::slice_as_base_elements(&arr_ext)
362            .try_into()
363            .expect("shouldn't fail");
364    }
365
366    /// (M) round function.
367    #[inline(always)]
368    pub fn apply_final_round(state: &mut [Felt; STATE_WIDTH], round: usize) {
369        apply_mds(state);
370        add_constants(state, &ARK1[round]);
371    }
372
373    /// Computes an exponentiation to the power 7 in cubic extension field.
374    #[inline(always)]
375    pub fn exp7(x: CubeExtension<Felt>) -> CubeExtension<Felt> {
376        let x2 = x.square();
377        let x4 = x2.square();
378
379        let x3 = x2 * x;
380        x3 * x4
381    }
382}