lambdaworks_crypto/hash/poseidon/
parameters.rs1use alloc::vec::Vec;
2use lambdaworks_math::field::{element::FieldElement as FE, traits::IsPrimeField};
3
4pub trait PermutationParameters {
12 type F: IsPrimeField + 'static;
13
14 const RATE: usize;
15 const CAPACITY: usize;
16 const ALPHA: u32;
17 const N_FULL_ROUNDS: usize;
18 const N_PARTIAL_ROUNDS: usize;
19 const STATE_SIZE: usize = Self::RATE + Self::CAPACITY;
20
21 const MDS_MATRIX: &'static [FE<Self::F>];
22 const N_MDS_MATRIX_ROWS: usize;
23 const N_MDS_MATRIX_COLS: usize;
24
25 const ROUND_CONSTANTS: &'static [FE<Self::F>];
26 const N_ROUND_CONSTANTS_ROWS: usize;
27 const N_ROUND_CONSTANTS_COLS: usize;
28
29 fn mix(state: &mut [FE<Self::F>]) {
33 let mut new_state: Vec<FE<Self::F>> = Vec::with_capacity(Self::STATE_SIZE);
34 for i in 0..Self::STATE_SIZE {
35 let mut new_e = FE::zero();
36 for (j, current_state) in state.iter().enumerate() {
37 let mut mij = Self::MDS_MATRIX[i * Self::N_MDS_MATRIX_COLS + j].clone();
38 mij *= current_state;
39 new_e += mij;
40 }
41 new_state.push(new_e);
42 }
43 state.clone_from_slice(&new_state[0..Self::STATE_SIZE]);
44 }
45}