Skip to main content

p3_poseidon1/
lib.rs

1//! The Poseidon1 permutation.
2//!
3//! # Overview
4//!
5//! Poseidon1 is a cryptographic hash function designed for efficient verification inside
6//! zero-knowledge proof systems (SNARKs, STARKs, etc). It operates natively over
7//! prime fields, avoiding the overhead of bit-decomposition required by traditional hashes
8//! like SHA-256 or Keccak.
9//!
10//! This crate provides [`Poseidon1`], an optimized implementation that uses the **sparse matrix
11//! decomposition** from the original paper (Appendix B) to factor the dense MDS matrix into
12//! sparse components. This reduces the cost of each partial round from O(t^2) to O(t), where
13//! t = `WIDTH` is the state width.
14//!
15//! # Round Structure
16//!
17//! Poseidon1 alternates between two types of rounds:
18//!
19//! ```text
20//!   ┌──────────────────┐   ┌───────────────────┐   ┌──────────────────┐
21//!   │ RF/2 full rounds │ → │ RP partial rounds │ → │ RF/2 full rounds │
22//!   │ (S-box on all)   │   │ (S-box on s[0])   │   │ (S-box on all)   │
23//!   └──────────────────┘   └───────────────────┘   └──────────────────┘
24//! ```
25//!
26//! - **Full rounds** (RF total, split equally): apply the S-box x^D to every state element,
27//!   then multiply by the dense MDS matrix. These provide resistance against statistical
28//!   attacks (differential, linear, rebound).
29//!
30//! - **Partial rounds** (RP total): apply the S-box only to `state[0]`, then multiply by a
31//!   **sparse** matrix derived from the MDS factorization. These efficiently increase the
32//!   algebraic degree to resist interpolation and Gröbner basis attacks.
33//!
34//! Each round follows the sequence: **AddRoundConstants → S-box → MixLayer**.
35//!
36//! # References
37//!
38//! - Grassi et al., "Poseidon1: A New Hash Function for Zero-Knowledge Proof Systems",
39//!   USENIX Security 2021. <https://eprint.iacr.org/2019/458>
40//! - HorizenLabs reference implementation: <https://github.com/HorizenLabs/poseidon2>
41
42#![no_std]
43
44extern crate alloc;
45
46pub mod external;
47pub mod generic;
48pub mod internal;
49pub mod utils;
50
51use alloc::vec::Vec;
52use core::marker::PhantomData;
53
54pub use external::*;
55pub use generic::*;
56pub use internal::*;
57use p3_field::{Algebra, InjectiveMonomial, PrimeField};
58use p3_symmetric::{CryptographicPermutation, Permutation};
59use rand::distr::{Distribution, StandardUniform};
60use rand::{Rng, RngExt};
61
62/// Raw Poseidon1 parameters before the sparse matrix optimization.
63///
64/// These are the "textbook" parameters as generated by the Grain LFSR or any
65/// other parameter-generation script. They are transformed into the optimized
66/// sparse form used at runtime.
67///
68/// # Round Constant Layout
69///
70/// Constants are stored in a flat array with three consecutive sections:
71///
72/// ```text
73///   ┌──────────────┬──────────────────┬──────────────────┐
74///   │ initial full  │  partial rounds  │  terminal full   │
75///   │ (RF/2 items)  │  (RP items)      │  (RF/2 items)    │
76///   └──────────────┴──────────────────┴──────────────────┘
77/// ```
78///
79/// Each entry is a WIDTH-sized vector (one constant per state element per round).
80#[derive(Clone, Debug)]
81pub struct Poseidon1Constants<F, const WIDTH: usize> {
82    /// Total number of full rounds (split equally between initial and terminal).
83    pub rounds_f: usize,
84
85    /// Number of partial rounds.
86    pub rounds_p: usize,
87
88    /// First column of the circulant MDS matrix, stored as signed integers.
89    ///
90    /// This matches the representation used by the MDS crate, so concrete
91    /// fields can pass their verified constants directly without duplication.
92    /// During initialization, the dense form is expanded once for the sparse
93    /// decomposition, then discarded.
94    pub mds_circ_col: [i64; WIDTH],
95
96    /// Round constants, one WIDTH-sized vector per round.
97    ///
98    /// Total length = rounds_f + rounds_p.
99    pub round_constants: Vec<[F; WIDTH]>,
100}
101
102impl<F: PrimeField, const WIDTH: usize> Poseidon1Constants<F, WIDTH> {
103    /// Compute the optimized sparse-form constants from these raw parameters.
104    ///
105    /// This performs two transformations:
106    ///
107    /// 1. **Sparse matrix decomposition**: factors the dense MDS matrix into one
108    ///    dense transition matrix and several sparse matrices. Each sparse matrix
109    ///    is parameterized by two vectors of length WIDTH-1.
110    ///
111    /// 2. **Round constant compression**: via backward substitution through the
112    ///    inverse MDS matrix, reduces each partial round's full constant vector
113    ///    to a single scalar, except for the first partial round.
114    pub fn to_optimized(
115        &self,
116    ) -> (
117        FullRoundConstants<F, WIDTH>,
118        PartialRoundConstants<F, WIDTH>,
119    ) {
120        let half_f = self.rounds_f / 2;
121        let rounds_p = self.rounds_p;
122
123        // Split the flat round-constant array into three sections.
124        //
125        // Layout: [initial_full (RF/2) | partial (RP) | terminal_full (RF/2)]
126        let initial_rc: Vec<[F; WIDTH]> = self.round_constants[..half_f].to_vec();
127        let partial_rc: Vec<[F; WIDTH]> = self.round_constants[half_f..half_f + rounds_p].to_vec();
128        let terminal_rc: Vec<[F; WIDTH]> = self.round_constants[half_f + rounds_p..].to_vec();
129
130        // Expand circulant first column into dense MDS matrix for the sparse decomposition.
131        let mds: [[F; WIDTH]; WIDTH] = utils::circulant_to_dense(&self.mds_circ_col);
132
133        // Compress round constants and factor MDS into sparse matrices.
134        let (first_round_constants, opt_partial_rc, m_i, sparse_v, sparse_first_row) =
135            utils::compute_optimized_constants::<F, WIDTH>(&mds, rounds_p, &partial_rc);
136
137        // Compute textbook path constants (forward constant substitution).
138        let (textbook_scalar_constants, textbook_residual) =
139            utils::forward_constant_substitution::<F, WIDTH>(&mds, &partial_rc);
140
141        let full_constants = FullRoundConstants {
142            initial: initial_rc,
143            terminal: terminal_rc,
144            dense_mds: mds,
145        };
146
147        let partial_constants = PartialRoundConstants {
148            first_round_constants,
149            m_i,
150            sparse_first_row,
151            v: sparse_v,
152            round_constants: opt_partial_rc,
153            textbook_scalar_constants,
154            textbook_residual,
155        };
156
157        (full_constants, partial_constants)
158    }
159}
160
161/// The optimized Poseidon1 permutation.
162///
163/// Holds the pre-computed full and partial round layers and applies them
164/// in sequence: initial full rounds, then partial rounds, then terminal
165/// full rounds.
166#[derive(Clone, Debug)]
167pub struct Poseidon1<F, FullRoundPerm, PartialRoundPerm, const WIDTH: usize, const D: u64> {
168    /// The initial and terminal full round layers.
169    ///
170    /// Full rounds apply the S-box to every state element, then multiply
171    /// by the dense MDS matrix.
172    full_round_layer: FullRoundPerm,
173
174    /// The partial round layer using sparse matrix decomposition.
175    ///
176    /// Partial rounds apply the S-box only to the first state element,
177    /// then multiply by a sparse matrix in O(t) operations.
178    partial_round_layer: PartialRoundPerm,
179
180    _phantom: PhantomData<F>,
181}
182
183impl<F, FullRoundPerm, PartialRoundPerm, const WIDTH: usize, const D: u64>
184    Poseidon1<F, FullRoundPerm, PartialRoundPerm, WIDTH, D>
185where
186    F: PrimeField,
187    FullRoundPerm: FullRoundLayerConstructor<F, WIDTH>,
188    PartialRoundPerm: PartialRoundLayerConstructor<F, WIDTH>,
189{
190    /// Create a new optimized Poseidon1 from raw constants.
191    ///
192    /// Internally computes the sparse matrix decomposition and the
193    /// optimized round constants. This is the typical entry point.
194    pub fn new(raw: &Poseidon1Constants<F, WIDTH>) -> Self {
195        let (full_constants, partial_constants) = raw.to_optimized();
196        Self::new_from_precomputed(full_constants, partial_constants)
197    }
198
199    /// Create a new optimized Poseidon1 from pre-computed constants.
200    ///
201    /// Use this when the sparse matrix decomposition has already been performed
202    /// (e.g., constants loaded from a file or hardcoded).
203    fn new_from_precomputed(
204        full_constants: FullRoundConstants<F, WIDTH>,
205        partial_constants: PartialRoundConstants<F, WIDTH>,
206    ) -> Self {
207        Self {
208            full_round_layer: FullRoundPerm::new_from_constants(full_constants),
209            partial_round_layer: PartialRoundPerm::new_from_constants(partial_constants),
210            _phantom: PhantomData,
211        }
212    }
213
214    /// Create a new Poseidon1 with random round constants and a given MDS permutation.
215    ///
216    /// Builds the dense MDS matrix by applying the permutation to unit vectors,
217    /// generates random round constants, and computes the sparse matrix decomposition.
218    ///
219    /// Primarily useful for testing.
220    pub fn new_from_rng(
221        half_num_full_rounds: usize,
222        num_partial_rounds: usize,
223        mds: &impl Permutation<[F; WIDTH]>,
224        rng: &mut impl Rng,
225    ) -> Self
226    where
227        StandardUniform: Distribution<F>,
228    {
229        let rounds_f = 2 * half_num_full_rounds;
230        let num_rounds = rounds_f + num_partial_rounds;
231
232        // Generate random round constants.
233        let round_constants: Vec<[F; WIDTH]> = (0..num_rounds)
234            .map(|_| core::array::from_fn(|_| rng.sample(StandardUniform)))
235            .collect();
236
237        // Build dense MDS by applying the permutation to unit vectors.
238        let columns: [[F; WIDTH]; WIDTH] = core::array::from_fn(|j| {
239            let mut e = [F::ZERO; WIDTH];
240            e[j] = F::ONE;
241            mds.permute(e)
242        });
243        // Transpose to row-major format.
244        let dense_mds: [[F; WIDTH]; WIDTH] =
245            core::array::from_fn(|i| core::array::from_fn(|j| columns[j][i]));
246
247        // Split round constants into three sections.
248        let half_f = half_num_full_rounds;
249        let initial_rc = round_constants[..half_f].to_vec();
250        let partial_rc = round_constants[half_f..half_f + num_partial_rounds].to_vec();
251        let terminal_rc = round_constants[half_f + num_partial_rounds..].to_vec();
252
253        // Compute optimized sparse constants.
254        let (first_round_constants, opt_partial_rc, m_i, sparse_v, sparse_first_row) =
255            utils::compute_optimized_constants::<F, WIDTH>(
256                &dense_mds,
257                num_partial_rounds,
258                &partial_rc,
259            );
260
261        // Compute textbook path constants (forward constant substitution).
262        let (textbook_scalar_constants, textbook_residual) =
263            utils::forward_constant_substitution::<F, WIDTH>(&dense_mds, &partial_rc);
264
265        let full_constants = FullRoundConstants {
266            initial: initial_rc,
267            terminal: terminal_rc,
268            dense_mds,
269        };
270
271        let partial_constants = PartialRoundConstants {
272            first_round_constants,
273            m_i,
274            sparse_first_row,
275            v: sparse_v,
276            round_constants: opt_partial_rc,
277            textbook_scalar_constants,
278            textbook_residual,
279        };
280
281        Self::new_from_precomputed(full_constants, partial_constants)
282    }
283}
284
285impl<F, A, FullRoundPerm, PartialRoundPerm, const WIDTH: usize, const D: u64>
286    Permutation<[A; WIDTH]> for Poseidon1<F, FullRoundPerm, PartialRoundPerm, WIDTH, D>
287where
288    F: PrimeField + InjectiveMonomial<D>,
289    A: Algebra<F> + Sync + InjectiveMonomial<D>,
290    FullRoundPerm: FullRoundLayer<A, WIDTH, D>,
291    PartialRoundPerm: PartialRoundLayer<A, WIDTH, D>,
292{
293    fn permute_mut(&self, state: &mut [A; WIDTH]) {
294        // RF/2 full rounds (S-box on all elements + dense MDS).
295        self.full_round_layer.permute_state_initial(state);
296        // RP partial rounds (S-box on state[0] only + sparse matrix).
297        self.partial_round_layer.permute_state(state);
298        // RF/2 full rounds (S-box on all elements + dense MDS).
299        self.full_round_layer.permute_state_terminal(state);
300    }
301}
302
303impl<F, A, FullRoundPerm, PartialRoundPerm, const WIDTH: usize, const D: u64>
304    CryptographicPermutation<[A; WIDTH]> for Poseidon1<F, FullRoundPerm, PartialRoundPerm, WIDTH, D>
305where
306    F: PrimeField + InjectiveMonomial<D>,
307    A: Algebra<F> + Sync + InjectiveMonomial<D>,
308    FullRoundPerm: FullRoundLayer<A, WIDTH, D>,
309    PartialRoundPerm: PartialRoundLayer<A, WIDTH, D>,
310{
311}