liminal_ark_pnbr_poseidon_paramgen/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2#![allow(non_snake_case)]
3#![deny(missing_docs)]
4//! Module for generating parameters for the Poseidon SNARK-friendly hash function.
5//!
6//! This crate will, given a choice of:
7//!
8//! * M, the desired security level (in bits),
9//! * t, the width of the desired hash function, e.g. $t=3$ corresponds to 2-to-1 hash.
10//! * p, the prime modulus,
11//! * `allow_inverse`, whether or not to allow an inverse alpha for the Sbox layer.
12//!
13//! generate the best choice of parameters, for both the unoptimized version of Poseidon
14//! specified in the [Poseidon paper], as well as the optimizations described in Appendix
15//! B.
16//!
17//! [Poseidon paper]: https://eprint.iacr.org/2019/458.pdf
18
19#[cfg(not(feature = "std"))]
20extern crate alloc;
21
22mod alpha;
23mod appendix_g;
24pub(crate) mod input;
25mod mds;
26mod round_constants;
27mod rounds;
28mod transcript;
29mod utils;
30
31/// For generating parameters at build time.
32#[cfg(feature = "std")]
33pub mod poseidon_build;
34
35use ark_ff::PrimeField;
36use poseidon_parameters::PoseidonParameters;
37use utils::log2;
38
39/// Generate a Poseidon instance mapped over Fp given a choice of:
40///
41/// * M, the desired security level (in bits),
42/// * t, the width of the desired hash function, e.g. $t=3$ corresponds to 2-to-1 hash.
43/// * p, the prime modulus,
44/// * `allow_inverse`, whether or not to allow an inverse alpha.
45pub fn generate<F: PrimeField>(
46    M: usize,
47    t: usize,
48    p: F::BigInt,
49    allow_inverse: bool,
50) -> PoseidonParameters<F> {
51    let input = input::generate(M, t, p, allow_inverse);
52    let alpha = alpha::generate::<F>(p, allow_inverse);
53    let rounds = rounds::generate(&input, &alpha);
54    let mds = mds::generate(&input);
55    let arc = round_constants::generate(&input, rounds, alpha);
56    let optimized_mds = mds::generate_optimized(&mds, t, &rounds);
57    let optimized_arc = round_constants::generate_optimized(&arc, &mds, &rounds);
58
59    PoseidonParameters::<F> {
60        M: input.M,
61        t: input.t,
62        alpha,
63        rounds,
64        mds,
65        arc,
66        optimized_mds,
67        optimized_arc,
68    }
69}