jf_poseidon2/
constants.rs

1//! Poseidon2 Constants copied from <https://github.com/HorizenLabs/poseidon2/blob/main/plain_implementations/src/poseidon2/>
2
3use ark_ff::PrimeField;
4use hex::FromHex;
5
6#[cfg(feature = "bls12-381")]
7pub mod bls12_381;
8#[cfg(feature = "bn254")]
9pub mod bn254;
10
11#[allow(dead_code)]
12#[inline]
13pub(crate) fn from_hex<F: PrimeField>(s: &str) -> F {
14    F::from_be_bytes_mod_order(&<[u8; 32]>::from_hex(s).expect("Invalid HexStr"))
15}
16
17/// macros to derive instances that implements `trait Poseidon2Params`
18#[macro_export]
19macro_rules! define_poseidon2_params {
20    (
21        $struct_name:ident,
22        $state_size:expr,
23        $sbox_size:expr,
24        $ext_rounds:expr,
25        $int_rounds:expr,
26        $rc_ext:ident,
27        $rc_int:ident,
28        $mat_diag_m_1:ident
29    ) => {
30        /// Poseidon parameters for Bls12-381 scalar field, with
31        /// - state size = $state_size
32        /// - sbox size = $sbox_size
33        /// - external rounds = $ext_rounds
34        /// - internal rounds = $int_rounds
35        #[derive(Clone, Debug)]
36        pub struct $struct_name;
37
38        impl Poseidon2Params<Fr, $state_size> for $struct_name {
39            const T: usize = $state_size;
40            const D: usize = $sbox_size;
41            const EXT_ROUNDS: usize = $ext_rounds;
42            const INT_ROUNDS: usize = $int_rounds;
43
44            fn external_rc() -> &'static [[Fr; $state_size]] {
45                &*$rc_ext
46            }
47
48            fn internal_rc() -> &'static [Fr] {
49                &*$rc_int
50            }
51
52            fn internal_mat_diag_m_1() -> &'static [Fr; $state_size] {
53                &$mat_diag_m_1
54            }
55        }
56    };
57}