poseidon_paramgen/
v2.rs

1use ark_ff::PrimeField;
2
3mod external;
4mod internal;
5
6use crate::{alpha, input::InputParameters, round_constants, rounds};
7use poseidon_parameters::v2::{PoseidonParameters, SquareMatrix};
8
9/// For generating parameters at build time.
10pub mod poseidon_build {
11    pub use crate::poseidon_build::v2_compile as compile;
12}
13
14/// Generate a Poseidon2 instance mapped over Fp given a choice of:
15///
16/// * M, the desired security level (in bits),
17/// * t, the width of the desired hash function, e.g. $t=3$ corresponds to 2-to-1 hash.
18/// * p, the prime modulus,
19/// * `allow_inverse`, whether or not to allow an inverse alpha.
20pub fn generate<F: PrimeField>(
21    M: usize,
22    t: usize,
23    p: F::BigInt,
24    allow_inverse: bool,
25) -> PoseidonParameters<F> {
26    let input = InputParameters::generate(M, t, p, allow_inverse);
27    let alpha = alpha::generate::<F>(p, allow_inverse);
28    let rounds = rounds::v2_generate(&input, &alpha);
29    let arc = round_constants::v2_generate(&input, rounds, alpha);
30    let m_i: SquareMatrix<F> = internal::generate(t);
31
32    // We use the internal matrix also for the external rounds if t < 4.
33    if t < 4 {
34        PoseidonParameters::<F> {
35            M: input.M,
36            t: input.t,
37            alpha,
38            rounds,
39            arc,
40            m_i: m_i.clone(),
41            m_e: m_i,
42        }
43    } else {
44        let m_e = external::generate(t);
45        PoseidonParameters::<F> {
46            M: input.M,
47            t: input.t,
48            alpha,
49            rounds,
50            arc,
51            m_e,
52            m_i,
53        }
54    }
55}