miden_crypto/hash/rescue/rpo/
mod.rs

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