miden_crypto/hash/algebraic_sponge/poseidon2/mod.rs
1use once_cell::sync::Lazy;
2use p3_goldilocks::Goldilocks;
3use p3_symmetric::Permutation;
4
5use super::{
6 AlgebraicSponge, CAPACITY_RANGE, DIGEST_RANGE, Felt, RATE_RANGE, RATE0_RANGE, RATE1_RANGE,
7 Range, STATE_WIDTH, Word,
8};
9use crate::{
10 ZERO,
11 hash::algebraic_sponge::poseidon2::constants::{
12 ARK_EXT_INITIAL, ARK_EXT_TERMINAL, ARK_INT, MAT_DIAG,
13 },
14};
15
16mod constants;
17use constants::{NUM_EXTERNAL_ROUNDS_HALF, NUM_INTERNAL_ROUNDS};
18
19#[cfg(test)]
20mod test;
21
22static P3_POSEIDON2: Lazy<p3_goldilocks::Poseidon2Goldilocks<12>> =
23 Lazy::new(p3_goldilocks::default_goldilocks_poseidon2_12);
24
25/// Applies Plonky3's optimized Poseidon2 permutation to a `[Felt; 12]` state.
26///
27/// `Felt` is `#[repr(transparent)]` over `Goldilocks`, so the transmute is safe.
28/// A process-global lazy static holds the permutation so round constants are not reallocated on
29/// every call (including `no_std`, via `once_cell` and the `critical-section` crate).
30#[inline(always)]
31fn p3_permute(state: &mut [Felt; STATE_WIDTH]) {
32 // SAFETY: Felt is #[repr(transparent)] over Goldilocks.
33 let gl_state =
34 unsafe { &mut *(state as *mut [Felt; STATE_WIDTH] as *mut [Goldilocks; STATE_WIDTH]) };
35
36 P3_POSEIDON2.permute_mut(gl_state);
37}
38
39/// Applies Plonky3's optimized Poseidon2 permutation to a packed `[PackedFelt; 12]` state,
40/// running one independent sponge state per SIMD lane.
41#[cfg(any(
42 all(target_arch = "x86_64", target_feature = "avx2"),
43 all(target_arch = "aarch64", target_feature = "neon"),
44 all(target_arch = "wasm32", target_feature = "simd128"),
45))]
46#[inline(always)]
47pub(super) fn p3_permute_packed(state: &mut [miden_field::PackedFelt; STATE_WIDTH]) {
48 P3_POSEIDON2.permute_mut(miden_field::PackedFelt::as_goldilocks_array_mut(state));
49}
50
51/// Implementation of the Poseidon2 hash function with 256-bit output.
52///
53/// The permutation is delegated to Plonky3's optimized `Poseidon2Goldilocks<12>`, which provides
54/// hardware-accelerated implementations on aarch64 (NEON inline assembly) and an optimized generic
55/// implementation on other architectures. The internal MDS diagonal uses small special values
56/// (-2, 1, 2, 1/2, 3, 4, ...) that enable multiplication via shifts and halves rather than full
57/// field multiplications.
58///
59/// The parameters used to instantiate the function are:
60/// * Field: 64-bit prime field with modulus 2^64 - 2^32 + 1.
61/// * State width: 12 field elements.
62/// * Capacity size: 4 field elements.
63/// * S-Box degree: 7.
64/// * Rounds: There are 2 different types of rounds, called internal and external, and are
65/// structured as follows:
66/// - Initial External rounds (IE): `add_constants` → `apply_sbox` → `apply_matmul_external`.
67/// - Internal rounds: `add_constants` → `apply_sbox` → `apply_matmul_internal`, where the constant
68/// addition and sbox application apply only to the first entry of the state.
69/// - Terminal External rounds (TE): `add_constants` → `apply_sbox` → `apply_matmul_external`.
70/// - An additional `apply_matmul_external` is inserted at the beginning in order to protect against
71/// some recent attacks.
72///
73/// The above parameters target a 128-bit security level. The digest consists of four field elements
74/// and it can be serialized into 32 bytes (256 bits).
75///
76/// ## Hash output consistency
77/// Functions [hash_elements()](Poseidon2::hash_elements), and [merge()](Poseidon2::merge), are
78/// internally consistent. That is, computing a hash for the same set of elements using these
79/// functions will always produce the same result. For example, merging two digests using
80/// [merge()](Poseidon2::merge) will produce the same result as hashing 8 elements which make up
81/// these digests using [hash_elements()](Poseidon2::hash_elements) function.
82///
83/// However, [hash()](Poseidon2::hash) function is not consistent with functions mentioned above.
84/// For example, if we take two field elements, serialize them to bytes and hash them using
85/// [hash()](Poseidon2::hash), the result will differ from the result obtained by hashing these
86/// elements directly using [hash_elements()](Poseidon2::hash_elements) function. The reason for
87/// this difference is that [hash()](Poseidon2::hash) function needs to be able to handle
88/// arbitrary binary strings, which may or may not encode valid field elements - and thus,
89/// deserialization procedure used by this function is different from the procedure used to
90/// deserialize valid field elements.
91///
92/// Thus, if the underlying data consists of valid field elements, it might make more sense
93/// to deserialize them into field elements and then hash them using
94/// [hash_elements()](Poseidon2::hash_elements) function rather than hashing the serialized bytes
95/// using [hash()](Poseidon2::hash) function.
96///
97/// ## Domain separation
98/// [merge_in_domain()](Poseidon2::merge_in_domain) hashes two digests into one digest with some
99/// domain identifier and the current implementation sets the second capacity element to the value
100/// of this domain identifier. Using a similar argument to the one formulated for domain separation
101/// in Appendix C of the [specifications](https://eprint.iacr.org/2023/1045), one sees that doing
102/// so degrades only pre-image resistance, from its initial bound of c.log_2(p), by as much as
103/// the log_2 of the size of the domain identifier space. Since pre-image resistance becomes
104/// the bottleneck for the security bound of the sponge in overwrite-mode only when it is
105/// lower than 2^128, we see that the target 128-bit security level is maintained as long as
106/// the size of the domain identifier space, including for padding, is less than 2^128.
107///
108/// ## Hashing of empty input
109/// The current implementation hashes empty input to the zero digest [0, 0, 0, 0]. This has
110/// the benefit of requiring no calls to the Poseidon2 permutation when hashing empty input.
111#[allow(rustdoc::private_intra_doc_links)]
112#[derive(Debug, Copy, Clone, Eq, PartialEq)]
113pub struct Poseidon2();
114
115impl AlgebraicSponge for Poseidon2 {
116 fn apply_permutation(state: &mut [Felt; STATE_WIDTH]) {
117 p3_permute(state);
118 }
119}
120
121impl Poseidon2 {
122 // CONSTANTS
123 // --------------------------------------------------------------------------------------------
124
125 /// Target collision resistance level in bits.
126 pub const COLLISION_RESISTANCE: u32 = 128;
127
128 /// Number of initial or terminal external rounds.
129 pub const NUM_EXTERNAL_ROUNDS_HALF: usize = NUM_EXTERNAL_ROUNDS_HALF;
130 /// Number of internal rounds.
131 pub const NUM_INTERNAL_ROUNDS: usize = NUM_INTERNAL_ROUNDS;
132
133 /// Sponge state is set to 12 field elements, or 96 bytes / 768 bits; 8 elements are
134 /// reserved for the rate and the remaining 4 elements are reserved for the capacity.
135 ///
136 /// # Examples
137 ///
138 /// ```
139 /// use miden_crypto::{Word, hash::poseidon2::Poseidon2};
140 ///
141 /// const FELT_SERIALIZED_SIZE: usize = Word::SERIALIZED_SIZE / Word::NUM_ELEMENTS;
142 ///
143 /// assert_eq!(Poseidon2::STATE_WIDTH * FELT_SERIALIZED_SIZE, 96);
144 /// assert_eq!(Poseidon2::RATE_RANGE.len(), 8);
145 /// assert_eq!(Poseidon2::CAPACITY_RANGE.len(), 4);
146 /// ```
147 pub const STATE_WIDTH: usize = STATE_WIDTH;
148
149 /// The rate portion of the state is located in elements 0 through 7 (inclusive).
150 pub const RATE_RANGE: Range<usize> = RATE_RANGE;
151
152 /// The first 4-element word of the rate portion.
153 pub const RATE0_RANGE: Range<usize> = RATE0_RANGE;
154
155 /// The second 4-element word of the rate portion.
156 pub const RATE1_RANGE: Range<usize> = RATE1_RANGE;
157
158 /// The capacity portion of the state is located in elements 8, 9, 10, and 11.
159 pub const CAPACITY_RANGE: Range<usize> = CAPACITY_RANGE;
160
161 /// The output of the hash function can be read from state elements 0, 1, 2, and 3 (the first
162 /// word of the state).
163 pub const DIGEST_RANGE: Range<usize> = DIGEST_RANGE;
164
165 /// Matrix used for computing the linear layers of internal rounds.
166 pub const MAT_DIAG: [Felt; STATE_WIDTH] = MAT_DIAG;
167
168 /// Round constants added to the hasher state.
169 pub const ARK_EXT_INITIAL: [[Felt; STATE_WIDTH]; NUM_EXTERNAL_ROUNDS_HALF] = ARK_EXT_INITIAL;
170 pub const ARK_EXT_TERMINAL: [[Felt; STATE_WIDTH]; NUM_EXTERNAL_ROUNDS_HALF] = ARK_EXT_TERMINAL;
171 pub const ARK_INT: [Felt; NUM_INTERNAL_ROUNDS] = ARK_INT;
172
173 // HASH FUNCTIONS
174 // --------------------------------------------------------------------------------------------
175
176 /// Returns a hash of the provided sequence of bytes.
177 #[inline(always)]
178 pub fn hash(bytes: &[u8]) -> Word {
179 <Self as AlgebraicSponge>::hash(bytes)
180 }
181
182 /// Applies the Poseidon2 permutation to the provided state in-place.
183 #[inline(always)]
184 pub fn apply_permutation(state: &mut [Felt; STATE_WIDTH]) {
185 <Self as AlgebraicSponge>::apply_permutation(state);
186 }
187
188 /// Returns a hash of the provided field elements.
189 #[inline(always)]
190 pub fn hash_elements<E: BasedVectorSpace<Felt>>(elements: &[E]) -> Word {
191 <Self as AlgebraicSponge>::hash_elements(elements)
192 }
193
194 /// Returns a hash of two digests. This method is intended for use in construction of
195 /// Merkle trees and verification of Merkle paths.
196 #[inline(always)]
197 pub fn merge(values: &[Word; 2]) -> Word {
198 <Self as AlgebraicSponge>::merge(values)
199 }
200
201 /// Returns a hash of multiple digests.
202 #[inline(always)]
203 pub fn merge_many(values: &[Word]) -> Word {
204 <Self as AlgebraicSponge>::merge_many(values)
205 }
206
207 /// Returns a hash of two digests and a domain identifier.
208 #[inline(always)]
209 pub fn merge_in_domain(values: &[Word; 2], domain: Felt) -> Word {
210 <Self as AlgebraicSponge>::merge_in_domain(values, domain)
211 }
212
213 /// Returns a hash of the provided `elements` and a domain identifier.
214 #[inline(always)]
215 pub fn hash_elements_in_domain<E: BasedVectorSpace<Felt>>(
216 elements: &[E],
217 domain: Felt,
218 ) -> Word {
219 <Self as AlgebraicSponge>::hash_elements_in_domain(elements, domain)
220 }
221
222 // POSEIDON2 PERMUTATION
223 // --------------------------------------------------------------------------------------------
224
225 /// Applies the M_E (external) linear layer to the state in-place.
226 ///
227 /// This basically takes any 4 x 4 MDS matrix M and computes the matrix-vector product with
228 /// the matrix defined by `[[2M, M, ..., M], [M, 2M, ..., M], ..., [M, M, ..., 2M]]`.
229 ///
230 /// Given the structure of the above matrix, we can compute the product of the state with
231 /// matrix `[M, M, ..., M]` and compute the final result using a few addition.
232 #[inline(always)]
233 pub fn apply_matmul_external(state: &mut [Felt; STATE_WIDTH]) {
234 // multiply the state by `[M, M, ..., M]` block-wise
235 Self::matmul_m4(state);
236
237 // accumulate column-wise sums
238 let number_blocks = STATE_WIDTH / 4;
239 let mut stored = [ZERO; 4];
240 for j in 0..number_blocks {
241 let base = j * 4;
242 for l in 0..4 {
243 stored[l] += state[base + l];
244 }
245 }
246
247 // add stored column-sums to each element
248 for (i, val) in state.iter_mut().enumerate() {
249 *val += stored[i % 4];
250 }
251 }
252
253 /// Multiply a 4-element vector x by:
254 /// [ 2 3 1 1 ]
255 /// [ 1 2 3 1 ]
256 /// [ 1 1 2 3 ]
257 /// [ 3 1 1 2 ].
258 #[inline(always)]
259 fn matmul_m4(state: &mut [Felt; STATE_WIDTH]) {
260 const N_CHUNKS: usize = STATE_WIDTH / 4;
261
262 for i in 0..N_CHUNKS {
263 let base = i * 4;
264 let x = &mut state[base..base + 4];
265
266 let t01 = x[0] + x[1];
267 let t23 = x[2] + x[3];
268 let t0123 = t01 + t23;
269 let t01123 = t0123 + x[1];
270 let t01233 = t0123 + x[3];
271
272 // The order here is important. Need to overwrite x[0] and x[2] after x[1] and x[3].
273 x[3] = t01233 + x[0].double(); // 3*x[0] + x[1] + x[2] + 2*x[3]
274 x[1] = t01123 + x[2].double(); // x[0] + 2*x[1] + 3*x[2] + x[3]
275 x[0] = t01123 + t01; // 2*x[0] + 3*x[1] + x[2] + x[3]
276 x[2] = t01233 + t23; // x[0] + x[1] + 2*x[2] + 3*x[3]
277 }
278 }
279
280 /// Applies the M_I (internal) linear layer to the state in-place.
281 ///
282 /// The matrix is given by its diagonal entries with the remaining entries set equal to 1.
283 /// Hence, given the sum of the state entries, the matrix-vector product is computed using
284 /// a multiply-and-add per state entry.
285 #[inline(always)]
286 pub fn matmul_internal(state: &mut [Felt; STATE_WIDTH], mat_diag: [Felt; 12]) {
287 let mut sum = ZERO;
288 for s in state.iter().take(STATE_WIDTH) {
289 sum += *s
290 }
291
292 for i in 0..state.len() {
293 state[i] = state[i] * mat_diag[i] + sum;
294 }
295 }
296
297 /// Adds the round constants to the state in-place.
298 #[inline(always)]
299 pub fn add_rc(state: &mut [Felt; STATE_WIDTH], ark: &[Felt; 12]) {
300 state.iter_mut().zip(ark).for_each(|(s, &k)| *s += k);
301 }
302
303 /// Applies the S-box (x^7) to each element of the state in-place.
304 #[inline(always)]
305 pub fn apply_sbox(state: &mut [Felt; STATE_WIDTH]) {
306 state[0] = state[0].exp_const_u64::<7>();
307 state[1] = state[1].exp_const_u64::<7>();
308 state[2] = state[2].exp_const_u64::<7>();
309 state[3] = state[3].exp_const_u64::<7>();
310 state[4] = state[4].exp_const_u64::<7>();
311 state[5] = state[5].exp_const_u64::<7>();
312 state[6] = state[6].exp_const_u64::<7>();
313 state[7] = state[7].exp_const_u64::<7>();
314 state[8] = state[8].exp_const_u64::<7>();
315 state[9] = state[9].exp_const_u64::<7>();
316 state[10] = state[10].exp_const_u64::<7>();
317 state[11] = state[11].exp_const_u64::<7>();
318 }
319}
320
321// PLONKY3 INTEGRATION
322// ================================================================================================
323
324use p3_challenger::DuplexChallenger;
325use p3_symmetric::{CryptographicPermutation, PaddingFreeSponge, TruncatedPermutation};
326
327use crate::field::BasedVectorSpace;
328
329/// Plonky3-compatible Poseidon2 permutation.
330///
331/// This zero-sized wrapper delegates to Plonky3's optimized `Poseidon2Goldilocks<12>` and
332/// implements the `Permutation` and `CryptographicPermutation` traits.
333///
334/// The permutation operates on a state of 12 field elements (STATE_WIDTH = 12), with:
335/// - Rate: 8 elements (positions 0-7)
336/// - Capacity: 4 elements (positions 8-11)
337/// - Digest output: 4 elements (positions 0-3)
338#[derive(Debug, Copy, Clone, Eq, PartialEq)]
339pub struct Poseidon2Permutation256;
340
341impl Poseidon2Permutation256 {
342 // CONSTANTS
343 // --------------------------------------------------------------------------------------------
344
345 /// Number of initial or terminal external rounds.
346 pub const NUM_EXTERNAL_ROUNDS_HALF: usize = Poseidon2::NUM_EXTERNAL_ROUNDS_HALF;
347
348 /// Number of internal rounds.
349 pub const NUM_INTERNAL_ROUNDS: usize = Poseidon2::NUM_INTERNAL_ROUNDS;
350
351 /// Sponge state is set to 12 field elements, or 96 bytes / 768 bits; 8 elements are
352 /// reserved for rate and the remaining 4 elements are reserved for capacity.
353 pub const STATE_WIDTH: usize = STATE_WIDTH;
354
355 /// The rate portion of the state is located in elements 0 through 7 (inclusive).
356 pub const RATE_RANGE: Range<usize> = Poseidon2::RATE_RANGE;
357
358 /// The capacity portion of the state is located in elements 8, 9, 10, and 11.
359 pub const CAPACITY_RANGE: Range<usize> = Poseidon2::CAPACITY_RANGE;
360
361 /// The output of the hash function can be read from state elements 0, 1, 2, and 3.
362 pub const DIGEST_RANGE: Range<usize> = Poseidon2::DIGEST_RANGE;
363
364 // POSEIDON2 PERMUTATION
365 // --------------------------------------------------------------------------------------------
366
367 /// Applies Poseidon2 permutation to the provided state.
368 ///
369 /// This delegates to the Poseidon2 implementation.
370 #[inline(always)]
371 pub fn apply_permutation(state: &mut [Felt; STATE_WIDTH]) {
372 Poseidon2::apply_permutation(state);
373 }
374}
375
376// PLONKY3 TRAIT IMPLEMENTATIONS
377// ================================================================================================
378
379impl Permutation<[Felt; STATE_WIDTH]> for Poseidon2Permutation256 {
380 fn permute_mut(&self, state: &mut [Felt; STATE_WIDTH]) {
381 p3_permute(state);
382 }
383}
384
385impl CryptographicPermutation<[Felt; STATE_WIDTH]> for Poseidon2Permutation256 {}
386
387// TYPE ALIASES FOR PLONKY3 INTEGRATION
388// ================================================================================================
389
390/// Poseidon2-based hasher using Plonky3's PaddingFreeSponge.
391///
392/// This provides a sponge-based hash function with:
393/// - WIDTH: 12 field elements (total state size)
394/// - RATE: 8 field elements (input/output rate)
395/// - OUT: 4 field elements (digest size)
396pub type Poseidon2Hasher = PaddingFreeSponge<Poseidon2Permutation256, 12, 8, 4>;
397
398/// Poseidon2-based compression function using Plonky3's TruncatedPermutation.
399///
400/// This provides a 2-to-1 compression function for Merkle tree construction with:
401/// - CHUNK: 2 (number of input chunks - i.e., 2 digests of 4 elements each = 8 elements)
402/// - N: 4 (output size in field elements)
403/// - WIDTH: 12 (total state size)
404///
405/// The compression function takes 8 field elements (2 digests) as input and produces
406/// 4 field elements (1 digest) as output.
407pub type Poseidon2Compression = TruncatedPermutation<Poseidon2Permutation256, 2, 4, 12>;
408
409/// Poseidon2-based challenger using Plonky3's DuplexChallenger.
410///
411/// This provides a Fiat-Shamir transform implementation for interactive proof protocols,
412/// with:
413/// - F: Generic field type (typically the same as Felt)
414/// - WIDTH: 12 field elements (sponge state size)
415/// - RATE: 8 field elements (rate of absorption/squeezing)
416pub type Poseidon2Challenger<F> = DuplexChallenger<F, Poseidon2Permutation256, 12, 8>;