miden_crypto/hash/rescue/rpo/
mod.rs

1use core::ops::Range;
2
3use super::{
4    ARK1, ARK2, BINARY_CHUNK_SIZE, CAPACITY_RANGE, DIGEST_RANGE, ElementHasher, Felt, FieldElement,
5    Hasher, INPUT1_RANGE, INPUT2_RANGE, MDS, NUM_ROUNDS, RATE_RANGE, RATE_WIDTH, STATE_WIDTH,
6    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
14// HASHER IMPLEMENTATION
15// ================================================================================================
16
17/// Implementation of the Rescue Prime Optimized hash function with 256-bit output.
18///
19/// The hash function is implemented according to the Rescue Prime Optimized
20/// [specifications](https://eprint.iacr.org/2022/1577) while the padding rule follows the one
21/// described [here](https://eprint.iacr.org/2023/1045).
22///
23/// The parameters used to instantiate the function are:
24/// * Field: 64-bit prime field with modulus p = 2^64 - 2^32 + 1.
25/// * State width: 12 field elements.
26/// * Rate size: r = 8 field elements.
27/// * Capacity size: c = 4 field elements.
28/// * Number of founds: 7.
29/// * S-Box degree: 7.
30///
31/// The above parameters target a 128-bit security level. The digest consists of four field elements
32/// and it can be serialized into 32 bytes (256 bits).
33///
34/// ## Hash output consistency
35/// Functions [hash_elements()](Rpo256::hash_elements), [merge()](Rpo256::merge), and
36/// [merge_with_int()](Rpo256::merge_with_int) are internally consistent. That is, computing
37/// a hash for the same set of elements using these functions will always produce the same
38/// result. For example, merging two digests using [merge()](Rpo256::merge) will produce the
39/// same result as hashing 8 elements which make up these digests using
40/// [hash_elements()](Rpo256::hash_elements) function.
41///
42/// However, [hash()](Rpo256::hash) function is not consistent with functions mentioned above.
43/// For example, if we take two field elements, serialize them to bytes and hash them using
44/// [hash()](Rpo256::hash), the result will differ from the result obtained by hashing these
45/// elements directly using [hash_elements()](Rpo256::hash_elements) function. The reason for
46/// this difference is that [hash()](Rpo256::hash) function needs to be able to handle
47/// arbitrary binary strings, which may or may not encode valid field elements - and thus,
48/// deserialization procedure used by this function is different from the procedure used to
49/// deserialize valid field elements.
50///
51/// Thus, if the underlying data consists of valid field elements, it might make more sense
52/// to deserialize them into field elements and then hash them using
53/// [hash_elements()](Rpo256::hash_elements) function rather than hashing the serialized bytes
54/// using [hash()](Rpo256::hash) function.
55///
56/// ## Domain separation
57/// [merge_in_domain()](Rpo256::merge_in_domain) hashes two digests into one digest with some domain
58/// identifier and the current implementation sets the second capacity element to the value of
59/// this domain identifier. Using a similar argument to the one formulated for domain separation of
60/// the RPX hash function in Appendix C of its [specification](https://eprint.iacr.org/2023/1045),
61/// one sees that doing so degrades only pre-image resistance, from its initial bound of c.log_2(p),
62/// by as much as the log_2 of the size of the domain identifier space. Since pre-image resistance
63/// becomes the bottleneck for the security bound of the sponge in overwrite-mode only when it is
64/// lower than 2^128, we see that the target 128-bit security level is maintained as long as
65/// the size of the domain identifier space, including for padding, is less than 2^128.
66///
67/// ## Hashing of empty input
68/// The current implementation hashes empty input to the zero digest [0, 0, 0, 0]. This has
69/// the benefit of requiring no calls to the RPO permutation when hashing empty input.
70#[derive(Debug, Copy, Clone, Eq, PartialEq)]
71pub struct Rpo256();
72
73impl Hasher for Rpo256 {
74    /// Rpo256 collision resistance is 128-bits.
75    const COLLISION_RESISTANCE: u32 = 128;
76
77    type Digest = Word;
78
79    fn hash(bytes: &[u8]) -> Self::Digest {
80        // initialize the state with zeroes
81        let mut state = [ZERO; STATE_WIDTH];
82
83        // determine the number of field elements needed to encode `bytes` when each field element
84        // represents at most 7 bytes.
85        let num_field_elem = bytes.len().div_ceil(BINARY_CHUNK_SIZE);
86
87        // set the first capacity element to `RATE_WIDTH + (num_field_elem % RATE_WIDTH)`. We do
88        // this to achieve:
89        // 1. Domain separating hashing of `[u8]` from hashing of `[Felt]`.
90        // 2. Avoiding collisions at the `[Felt]` representation of the encoded bytes.
91        state[CAPACITY_RANGE.start] =
92            Felt::from((RATE_WIDTH + (num_field_elem % RATE_WIDTH)) as u8);
93
94        // initialize a buffer to receive the little-endian elements.
95        let mut buf = [0_u8; 8];
96
97        // iterate the chunks of bytes, creating a field element from each chunk and copying it
98        // into the state.
99        //
100        // every time the rate range is filled, a permutation is performed. if the final value of
101        // `rate_pos` is not zero, then the chunks count wasn't enough to fill the state range,
102        // and an additional permutation must be performed.
103        let mut current_chunk_idx = 0_usize;
104        // handle the case of an empty `bytes`
105        let last_chunk_idx = if num_field_elem == 0 {
106            current_chunk_idx
107        } else {
108            num_field_elem - 1
109        };
110        let rate_pos = bytes.chunks(BINARY_CHUNK_SIZE).fold(0, |rate_pos, chunk| {
111            // copy the chunk into the buffer
112            if current_chunk_idx != last_chunk_idx {
113                buf[..BINARY_CHUNK_SIZE].copy_from_slice(chunk);
114            } else {
115                // on the last iteration, we pad `buf` with a 1 followed by as many 0's as are
116                // needed to fill it
117                buf.fill(0);
118                buf[..chunk.len()].copy_from_slice(chunk);
119                buf[chunk.len()] = 1;
120            }
121            current_chunk_idx += 1;
122
123            // set the current rate element to the input. since we take at most 7 bytes, we are
124            // guaranteed that the inputs data will fit into a single field element.
125            state[RATE_RANGE.start + rate_pos] = Felt::new(u64::from_le_bytes(buf));
126
127            // proceed filling the range. if it's full, then we apply a permutation and reset the
128            // counter to the beginning of the range.
129            if rate_pos == RATE_WIDTH - 1 {
130                Self::apply_permutation(&mut state);
131                0
132            } else {
133                rate_pos + 1
134            }
135        });
136
137        // if we absorbed some elements but didn't apply a permutation to them (would happen when
138        // the number of elements is not a multiple of RATE_WIDTH), apply the RPO permutation. we
139        // don't need to apply any extra padding because the first capacity element contains a
140        // flag indicating the number of field elements constituting the last block when the latter
141        // is not divisible by `RATE_WIDTH`.
142        if rate_pos != 0 {
143            state[RATE_RANGE.start + rate_pos..RATE_RANGE.end].fill(ZERO);
144            Self::apply_permutation(&mut state);
145        }
146
147        // return the first 4 elements of the rate as hash result.
148        Word::new(state[DIGEST_RANGE].try_into().unwrap())
149    }
150
151    fn merge(values: &[Self::Digest; 2]) -> Self::Digest {
152        // initialize the state by copying the digest elements into the rate portion of the state
153        // (8 total elements), and set the capacity elements to 0.
154        let mut state = [ZERO; STATE_WIDTH];
155        let it = Self::Digest::words_as_elements_iter(values.iter());
156        for (i, v) in it.enumerate() {
157            state[RATE_RANGE.start + i] = *v;
158        }
159
160        // apply the RPO permutation and return the first four elements of the state
161        Self::apply_permutation(&mut state);
162        Word::new(state[DIGEST_RANGE].try_into().unwrap())
163    }
164
165    fn merge_many(values: &[Self::Digest]) -> Self::Digest {
166        Self::hash_elements(Self::Digest::words_as_elements(values))
167    }
168
169    fn merge_with_int(seed: Self::Digest, value: u64) -> Self::Digest {
170        // initialize the state as follows:
171        // - seed is copied into the first 4 elements of the rate portion of the state.
172        // - if the value fits into a single field element, copy it into the fifth rate element and
173        //   set the first capacity element to 5.
174        // - if the value doesn't fit into a single field element, split it into two field elements,
175        //   copy them into rate elements 5 and 6 and set the first capacity element to 6.
176        let mut state = [ZERO; STATE_WIDTH];
177        state[INPUT1_RANGE].copy_from_slice(seed.as_elements());
178        state[INPUT2_RANGE.start] = Felt::new(value);
179        if value < Felt::MODULUS {
180            state[CAPACITY_RANGE.start] = Felt::from(5_u8);
181        } else {
182            state[INPUT2_RANGE.start + 1] = Felt::new(value / Felt::MODULUS);
183            state[CAPACITY_RANGE.start] = Felt::from(6_u8);
184        }
185
186        // apply the RPO permutation and return the first four elements of the rate
187        Self::apply_permutation(&mut state);
188        Word::new(state[DIGEST_RANGE].try_into().unwrap())
189    }
190}
191
192impl ElementHasher for Rpo256 {
193    type BaseField = Felt;
194
195    fn hash_elements<E: FieldElement<BaseField = Self::BaseField>>(elements: &[E]) -> Self::Digest {
196        // convert the elements into a list of base field elements
197        let elements = E::slice_as_base_elements(elements);
198
199        // initialize state to all zeros, except for the first element of the capacity part, which
200        // is set to `elements.len() % RATE_WIDTH`.
201        let mut state = [ZERO; STATE_WIDTH];
202        state[CAPACITY_RANGE.start] = Self::BaseField::from((elements.len() % RATE_WIDTH) as u8);
203
204        // absorb elements into the state one by one until the rate portion of the state is filled
205        // up; then apply the Rescue permutation and start absorbing again; repeat until all
206        // elements have been absorbed
207        let mut i = 0;
208        for &element in elements.iter() {
209            state[RATE_RANGE.start + i] = element;
210            i += 1;
211            if i % RATE_WIDTH == 0 {
212                Self::apply_permutation(&mut state);
213                i = 0;
214            }
215        }
216
217        // if we absorbed some elements but didn't apply a permutation to them (would happen when
218        // the number of elements is not a multiple of RATE_WIDTH), apply the RPO permutation after
219        // padding by as many 0 as necessary to make the input length a multiple of the RATE_WIDTH.
220        if i > 0 {
221            while i != RATE_WIDTH {
222                state[RATE_RANGE.start + i] = ZERO;
223                i += 1;
224            }
225            Self::apply_permutation(&mut state);
226        }
227
228        // return the first 4 elements of the state as hash result
229        Word::new(state[DIGEST_RANGE].try_into().unwrap())
230    }
231}
232
233// HASH FUNCTION IMPLEMENTATION
234// ================================================================================================
235
236impl Rpo256 {
237    // CONSTANTS
238    // --------------------------------------------------------------------------------------------
239
240    /// The number of rounds is set to 7 to target 128-bit security level.
241    pub const NUM_ROUNDS: usize = NUM_ROUNDS;
242
243    /// Sponge state is set to 12 field elements or 768 bytes; 8 elements are reserved for rate and
244    /// the remaining 4 elements are reserved for capacity.
245    pub const STATE_WIDTH: usize = STATE_WIDTH;
246
247    /// The rate portion of the state is located in elements 4 through 11 (inclusive).
248    pub const RATE_RANGE: Range<usize> = RATE_RANGE;
249
250    /// The capacity portion of the state is located in elements 0, 1, 2, and 3.
251    pub const CAPACITY_RANGE: Range<usize> = CAPACITY_RANGE;
252
253    /// The output of the hash function can be read from state elements 4, 5, 6, and 7.
254    pub const DIGEST_RANGE: Range<usize> = DIGEST_RANGE;
255
256    /// MDS matrix used for computing the linear layer in a RPO round.
257    pub const MDS: [[Felt; STATE_WIDTH]; STATE_WIDTH] = MDS;
258
259    /// Round constants added to the hasher state in the first half of the RPO round.
260    pub const ARK1: [[Felt; STATE_WIDTH]; NUM_ROUNDS] = ARK1;
261
262    /// Round constants added to the hasher state in the second half of the RPO round.
263    pub const ARK2: [[Felt; STATE_WIDTH]; NUM_ROUNDS] = ARK2;
264
265    // TRAIT PASS-THROUGH FUNCTIONS
266    // --------------------------------------------------------------------------------------------
267
268    /// Returns a hash of the provided sequence of bytes.
269    #[inline(always)]
270    pub fn hash(bytes: &[u8]) -> Word {
271        <Self as Hasher>::hash(bytes)
272    }
273
274    /// Returns a hash of two digests. This method is intended for use in construction of
275    /// Merkle trees and verification of Merkle paths.
276    #[inline(always)]
277    pub fn merge(values: &[Word; 2]) -> Word {
278        <Self as Hasher>::merge(values)
279    }
280
281    /// Returns a hash of the provided field elements.
282    #[inline(always)]
283    pub fn hash_elements<E: FieldElement<BaseField = Felt>>(elements: &[E]) -> Word {
284        <Self as ElementHasher>::hash_elements(elements)
285    }
286
287    // DOMAIN IDENTIFIER
288    // --------------------------------------------------------------------------------------------
289
290    /// Returns a hash of two digests and a domain identifier.
291    pub fn merge_in_domain(values: &[Word; 2], domain: Felt) -> Word {
292        // initialize the state by copying the digest elements into the rate portion of the state
293        // (8 total elements), and set the capacity elements to 0.
294        let mut state = [ZERO; STATE_WIDTH];
295        let it = Word::words_as_elements_iter(values.iter());
296        for (i, v) in it.enumerate() {
297            state[RATE_RANGE.start + i] = *v;
298        }
299
300        // set the second capacity element to the domain value. The first capacity element is used
301        // for padding purposes.
302        state[CAPACITY_RANGE.start + 1] = domain;
303
304        // apply the RPO permutation and return the first four elements of the state
305        Self::apply_permutation(&mut state);
306        Word::new(state[DIGEST_RANGE].try_into().unwrap())
307    }
308
309    // RESCUE PERMUTATION
310    // --------------------------------------------------------------------------------------------
311
312    /// Applies RPO permutation to the provided state.
313    #[inline(always)]
314    pub fn apply_permutation(state: &mut [Felt; STATE_WIDTH]) {
315        for i in 0..NUM_ROUNDS {
316            Self::apply_round(state, i);
317        }
318    }
319
320    /// RPO round function.
321    #[inline(always)]
322    pub fn apply_round(state: &mut [Felt; STATE_WIDTH], round: usize) {
323        // apply first half of RPO round
324        apply_mds(state);
325        if !add_constants_and_apply_sbox(state, &ARK1[round]) {
326            add_constants(state, &ARK1[round]);
327            apply_sbox(state);
328        }
329
330        // apply second half of RPO round
331        apply_mds(state);
332        if !add_constants_and_apply_inv_sbox(state, &ARK2[round]) {
333            add_constants(state, &ARK2[round]);
334            apply_inv_sbox(state);
335        }
336    }
337}