p3_poseidon2/round_numbers.rs
1//! As the security analysis of Poseidon2 is identical to that of Poseidon,
2//! the relevant constraints regarding the number of full/partial rounds required can be found in
3//! the original paper: https://eprint.iacr.org/2019/458.pdf and the associated codebase:
4//! https://extgit.iaik.tugraz.at/krypto/hadeshash (See generate_params_poseidon.sage)
5//!
6//! These constraints are broken down into 6 equations:
7//! statistical, interpolation, groebner 1, 2, 3 and
8//! an extra constraint coming from the paper https://eprint.iacr.org/2023/537.pdf.
9//!
10//! For our parameters (M = 128, p > 2^30, WIDTH = t >= 8, D = alpha < 12),
11//! the statistical constraint always simplifies to requiring RF >= 6.
12//! Additionally p does not appear in Groebner 3 or the constraint coming from https://eprint.iacr.org/2023/537.pdf.
13//! The remaining 3 constraints all can be rearranged into the form:
14//! F(RF, RP) >= G(p) where G is a function which is non-decreasing with respect to p.
15//!
16//! Thus, if some tuple (M, p, WIDTH, D, RF, RP) satisfies all constraints, then so will
17//! the tuple (M, q, WIDTH, D, RF, RP) for any 2^30 < q < p.
18//! Moreover if RF, RP are the "optimal" round numbers (Optimal meaning minimising the number of S-box operations we need to perform)
19//! for two tuples (M, p, WIDTH, D) and (M, q, WIDTH, D), then
20//! they will also be optimal for (M, r, WIDTH, D) for any q < r < p.
21//!
22//! We compute the optimal required number of external (full) and internal (partial) rounds using:
23//! https://github.com/0xPolygonZero/hash-constants/blob/master/calc_round_numbers.py
24//! Using the above analysis we can conclude that the round numbers are equal
25//! for all 31 bit primes and 64 bit primes respectively.
26
27use gcd::Gcd;
28use p3_field::PrimeField64;
29
30/// Given a field, a width and an D return the number of full and partial rounds needed to achieve 128 bit security.
31pub fn poseidon2_round_numbers_128<F: PrimeField64>(width: usize, d: u64) -> (usize, usize) {
32 // Start by checking that d is a valid permutation.
33 assert_eq!(d.gcd(F::ORDER_U64 - 1), 1);
34
35 // Next compute the number of bits in p.
36 let prime_bit_number = F::ORDER_U64.ilog2() + 1;
37
38 match prime_bit_number {
39 31 => match (width, d) {
40 (16, 3) => (8, 20),
41 (16, 5) => (8, 14),
42 (16, 7) => (8, 13),
43 (16, 9) => (8, 13),
44 (16, 11) => (8, 13),
45 (24, 3) => (8, 23),
46 (24, 5) => (8, 22),
47 (24, 7) => (8, 21),
48 (24, 9) => (8, 21),
49 (24, 11) => (8, 21),
50 _ => panic!("The given pair of width and D has not been checked for these fields"),
51 },
52 64 => match (width, d) {
53 (8, 3) => (8, 41),
54 (8, 5) => (8, 27),
55 (8, 7) => (8, 22),
56 (8, 9) => (8, 19),
57 (8, 11) => (8, 17),
58 (12, 3) => (8, 42),
59 (12, 5) => (8, 27),
60 (12, 7) => (8, 22),
61 (12, 9) => (8, 20),
62 (12, 11) => (8, 18),
63 (16, 3) => (8, 42),
64 (16, 5) => (8, 27),
65 (16, 7) => (8, 22),
66 (16, 9) => (8, 20),
67 (16, 11) => (8, 18),
68 _ => panic!("The given pair of width and D has not been checked for these fields"),
69 },
70 _ => panic!("The optimal parameters for that size of prime have not been computed."),
71 }
72}