miden_crypto/hash/algebraic_sponge/poseidon2/
mod.rs

1use super::{
2    AlgebraicSponge, CAPACITY_RANGE, DIGEST_RANGE, ElementHasher, Felt, FieldElement, Hasher,
3    RATE_RANGE, Range, STATE_WIDTH, Word, ZERO,
4};
5
6mod constants;
7use constants::*;
8
9#[cfg(test)]
10mod test;
11
12/// Implementation of the Poseidon2 hash function with 256-bit output.
13///
14/// The implementation follows the original [specification](https://eprint.iacr.org/2023/323) and
15/// its accompanying reference [implementation](https://github.com/HorizenLabs/poseidon2).
16///
17/// The parameters used to instantiate the function are:
18/// * Field: 64-bit prime field with modulus 2^64 - 2^32 + 1.
19/// * State width: 12 field elements.
20/// * Capacity size: 4 field elements.
21/// * S-Box degree: 7.
22/// * Rounds: There are 2 different types of rounds, called internal and external, and are
23///   structured as follows:
24/// - Initial External rounds (IE): `add_constants` → `apply_sbox` → `apply_matmul_external`.
25/// - Internal rounds: `add_constants` → `apply_sbox` → `apply_matmul_internal`, where the constant
26///   addition and sbox application apply only to the first entry of the state.
27/// - Terminal External rounds (TE): `add_constants` → `apply_sbox` → `apply_matmul_external`.
28/// - An additional `apply_matmul_external` is inserted at the beginning in order to protect against
29///   some recent attacks.
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()](Poseidon2::hash_elements), [merge()](Poseidon2::merge), and
36/// [merge_with_int()](Poseidon2::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()](Poseidon2::merge) will produce the
39/// same result as hashing 8 elements which make up these digests using
40/// [hash_elements()](Poseidon2::hash_elements) function.
41///
42/// However, [hash()](Poseidon2::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()](Poseidon2::hash), the result will differ from the result obtained by hashing these
45/// elements directly using [hash_elements()](Poseidon2::hash_elements) function. The reason for
46/// this difference is that [hash()](Poseidon2::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()](Poseidon2::hash_elements) function rather than hashing the serialized bytes
54/// using [hash()](Poseidon2::hash) function.
55///
56/// ## Domain separation
57/// [merge_in_domain()](Poseidon2::merge_in_domain) hashes two digests into one digest with some
58/// domain identifier and the current implementation sets the second capacity element to the value
59/// of this domain identifier. Using a similar argument to the one formulated for domain separation
60/// in Appendix C of the [specifications](https://eprint.iacr.org/2023/1045), one sees that doing
61/// so degrades only pre-image resistance, from its initial bound of c.log_2(p), by as much as
62/// the log_2 of the size of the domain identifier space. Since pre-image resistance becomes
63/// 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 Poseidon2 permutation when hashing empty input.
70#[allow(rustdoc::private_intra_doc_links)]
71#[derive(Debug, Copy, Clone, Eq, PartialEq)]
72pub struct Poseidon2();
73
74impl AlgebraicSponge for Poseidon2 {
75    fn apply_permutation(state: &mut [Felt; STATE_WIDTH]) {
76        // 1. Apply (external) linear layer to the input
77        Self::apply_matmul_external(state);
78
79        // 2. Apply initial external rounds to the state
80        Self::initial_external_rounds(state);
81
82        // 3. Apply internal rounds to the state
83        Self::internal_rounds(state);
84
85        // 4. Apply terminal external rounds to the state
86        Self::terminal_external_rounds(state);
87    }
88}
89
90impl Hasher for Poseidon2 {
91    const COLLISION_RESISTANCE: u32 = 128;
92
93    type Digest = Word;
94
95    fn hash(bytes: &[u8]) -> Self::Digest {
96        <Self as AlgebraicSponge>::hash(bytes)
97    }
98
99    fn merge(values: &[Self::Digest; 2]) -> Self::Digest {
100        <Self as AlgebraicSponge>::merge(values)
101    }
102
103    fn merge_many(values: &[Self::Digest]) -> Self::Digest {
104        <Self as AlgebraicSponge>::merge_many(values)
105    }
106
107    fn merge_with_int(seed: Self::Digest, value: u64) -> Self::Digest {
108        <Self as AlgebraicSponge>::merge_with_int(seed, value)
109    }
110}
111
112impl ElementHasher for Poseidon2 {
113    type BaseField = Felt;
114
115    fn hash_elements<E: FieldElement<BaseField = Self::BaseField>>(elements: &[E]) -> Self::Digest {
116        <Self as AlgebraicSponge>::hash_elements(elements)
117    }
118}
119
120impl Poseidon2 {
121    // CONSTANTS
122    // --------------------------------------------------------------------------------------------
123
124    /// Number of initial or terminal external rounds.
125    pub const NUM_EXTERNAL_ROUNDS_HALF: usize = NUM_EXTERNAL_ROUNDS_HALF;
126    /// Number of internal rounds.
127    pub const NUM_INTERNAL_ROUNDS: usize = NUM_INTERNAL_ROUNDS;
128
129    /// Sponge state is set to 12 field elements or 768 bytes; 8 elements are reserved for rate and
130    /// the remaining 4 elements are reserved for capacity.
131    pub const STATE_WIDTH: usize = STATE_WIDTH;
132
133    /// The rate portion of the state is located in elements 4 through 11 (inclusive).
134    pub const RATE_RANGE: Range<usize> = RATE_RANGE;
135
136    /// The capacity portion of the state is located in elements 0, 1, 2, and 3.
137    pub const CAPACITY_RANGE: Range<usize> = CAPACITY_RANGE;
138
139    /// The output of the hash function can be read from state elements 4, 5, 6, and 7.
140    pub const DIGEST_RANGE: Range<usize> = DIGEST_RANGE;
141
142    /// Matrix used for computing the linear layers of internal rounds.
143    pub const MAT_DIAG: [Felt; STATE_WIDTH] = MAT_DIAG;
144
145    /// Round constants added to the hasher state.
146    pub const ARK_EXT_INITIAL: [[Felt; STATE_WIDTH]; NUM_EXTERNAL_ROUNDS_HALF] = ARK_EXT_INITIAL;
147    pub const ARK_EXT_TERMINAL: [[Felt; STATE_WIDTH]; NUM_EXTERNAL_ROUNDS_HALF] = ARK_EXT_TERMINAL;
148    pub const ARK_INT: [Felt; NUM_INTERNAL_ROUNDS] = ARK_INT;
149
150    // TRAIT PASS-THROUGH FUNCTIONS
151    // --------------------------------------------------------------------------------------------
152
153    /// Returns a hash of the provided sequence of bytes.
154    #[inline(always)]
155    pub fn hash(bytes: &[u8]) -> Word {
156        <Self as Hasher>::hash(bytes)
157    }
158
159    /// Returns a hash of two digests. This method is intended for use in construction of
160    /// Merkle trees and verification of Merkle paths.
161    #[inline(always)]
162    pub fn merge(values: &[Word; 2]) -> Word {
163        <Self as Hasher>::merge(values)
164    }
165
166    /// Returns a hash of the provided field elements.
167    #[inline(always)]
168    pub fn hash_elements<E: FieldElement<BaseField = Felt>>(elements: &[E]) -> Word {
169        <Self as ElementHasher>::hash_elements(elements)
170    }
171
172    /// Returns a hash of two digests and a domain identifier.
173    #[inline(always)]
174    pub fn merge_in_domain(values: &[Word; 2], domain: Felt) -> Word {
175        <Self as AlgebraicSponge>::merge_in_domain(values, domain)
176    }
177
178    // POSEIDON2 PERMUTATION
179    // --------------------------------------------------------------------------------------------
180
181    /// Applies the initial external rounds of the permutation.
182    #[allow(clippy::needless_range_loop)]
183    #[inline(always)]
184    fn initial_external_rounds(state: &mut [Felt; STATE_WIDTH]) {
185        for r in 0..NUM_EXTERNAL_ROUNDS_HALF {
186            Self::add_rc(state, &ARK_EXT_INITIAL[r]);
187            Self::apply_sbox(state);
188            Self::apply_matmul_external(state);
189        }
190    }
191
192    /// Applies the internal rounds of the permutation.
193    #[allow(clippy::needless_range_loop)]
194    #[inline(always)]
195    fn internal_rounds(state: &mut [Felt; STATE_WIDTH]) {
196        for r in 0..NUM_INTERNAL_ROUNDS {
197            state[0] += ARK_INT[r];
198            state[0] = state[0].exp7();
199            Self::matmul_internal(state, MAT_DIAG);
200        }
201    }
202
203    /// Applies the terminal external rounds of the permutation.
204    #[inline(always)]
205    #[allow(clippy::needless_range_loop)]
206    fn terminal_external_rounds(state: &mut [Felt; STATE_WIDTH]) {
207        for r in 0..NUM_EXTERNAL_ROUNDS_HALF {
208            Self::add_rc(state, &ARK_EXT_TERMINAL[r]);
209            Self::apply_sbox(state);
210            Self::apply_matmul_external(state);
211        }
212    }
213
214    /// Applies the M_E linear layer to the state.
215    ///
216    /// This basically takes any 4 x 4 MDS matrix M and computes the matrix-vector product with
217    /// the matrix defined by `[[2M, M, ..., M], [M, 2M, ..., M], ..., [M, M, ..., 2M]]`.
218    ///
219    /// Given the structure of the above matrix, we can compute the product of the state with
220    /// matrix `[M, M, ..., M]` and compute the final result using a few addition.
221    #[inline(always)]
222    fn apply_matmul_external(state: &mut [Felt; STATE_WIDTH]) {
223        // multiply the state by `[M, M, ..., M]` block-wise
224        Self::matmul_m4(state);
225
226        // accumulate column-wise sums
227        let number_blocks = STATE_WIDTH / 4;
228        let mut stored = [ZERO; 4];
229        for j in 0..number_blocks {
230            let base = j * 4;
231            for l in 0..4 {
232                stored[l] += state[base + l];
233            }
234        }
235
236        // add stored column-sums to each element
237        for (i, val) in state.iter_mut().enumerate() {
238            *val += stored[i % 4];
239        }
240    }
241
242    /// Multiplies the state block-wise with a 4 x 4 MDS matrix.
243    #[inline(always)]
244    fn matmul_m4(state: &mut [Felt; STATE_WIDTH]) {
245        let t4 = STATE_WIDTH / 4;
246
247        for i in 0..t4 {
248            let idx = i * 4;
249
250            let a = state[idx];
251            let b = state[idx + 1];
252            let c = state[idx + 2];
253            let d = state[idx + 3];
254
255            let t0 = a + b;
256            let t1 = c + d;
257            let two_b = b.double();
258            let two_d = d.double();
259
260            let t2 = two_b + t1;
261            let t3 = two_d + t0;
262
263            let t4 = t1.mul_small(4) + t3;
264            let t5 = t0.mul_small(4) + t2;
265
266            let t6 = t3 + t5;
267            let t7 = t2 + t4;
268
269            state[idx] = t6;
270            state[idx + 1] = t5;
271            state[idx + 2] = t7;
272            state[idx + 3] = t4;
273        }
274    }
275
276    /// Applies the M_I linear layer to the state.
277    ///
278    /// The matrix is given by its diagonal entries with the remaining entries set equal to 1.
279    /// Hence, given the sum of the state entries, the matrix-vector product is computed using
280    /// a multiply-and-add per state entry.
281    #[inline(always)]
282    fn matmul_internal(state: &mut [Felt; STATE_WIDTH], mat_diag: [Felt; 12]) {
283        let mut sum = ZERO;
284        for s in state.iter().take(STATE_WIDTH) {
285            sum += *s
286        }
287
288        for i in 0..state.len() {
289            state[i] = state[i] * mat_diag[i] + sum;
290        }
291    }
292
293    /// Adds the round-constants to the state during external rounds.
294    #[inline(always)]
295    fn add_rc(state: &mut [Felt; STATE_WIDTH], ark: &[Felt; 12]) {
296        state.iter_mut().zip(ark).for_each(|(s, &k)| *s += k);
297    }
298
299    /// Applies the sbox entry-wise to the state.
300    #[inline(always)]
301    fn apply_sbox(state: &mut [Felt; STATE_WIDTH]) {
302        state[0] = state[0].exp7();
303        state[1] = state[1].exp7();
304        state[2] = state[2].exp7();
305        state[3] = state[3].exp7();
306        state[4] = state[4].exp7();
307        state[5] = state[5].exp7();
308        state[6] = state[6].exp7();
309        state[7] = state[7].exp7();
310        state[8] = state[8].exp7();
311        state[9] = state[9].exp7();
312        state[10] = state[10].exp7();
313        state[11] = state[11].exp7();
314    }
315}