arkworks_utils/poseidon_params/
mod.rs

1use super::{parse_matrix, parse_vec, Bytes, Curve, FromHexError};
2
3pub use ark_std::vec::Vec;
4
5pub struct PoseidonData {
6	pub mds: Vec<Vec<Bytes>>,
7	pub rounds: Vec<Bytes>,
8	pub full_rounds: u8,
9	pub partial_rounds: u8,
10	pub width: u8,
11	pub exp: i8,
12}
13
14impl PoseidonData {
15	pub fn new(
16		mds: Vec<Vec<Bytes>>,
17		rounds: Vec<Bytes>,
18		full_rounds: u8,
19		partial_rounds: u8,
20		width: u8,
21		exp: i8,
22	) -> Self {
23		Self {
24			mds,
25			rounds,
26			full_rounds,
27			partial_rounds,
28			exp,
29			width,
30		}
31	}
32}
33
34pub fn setup_poseidon_params(
35	curve: Curve,
36	exp: i8,
37	width: u8,
38) -> Result<PoseidonData, FromHexError> {
39	// Making params for poseidon in merkle tree
40	match (curve, exp, width) {
41		#[cfg(feature = "poseidon_bls381_x3_3")]
42		(Curve::Bls381, 3, 3) => {
43			#[path = "./bls381_x3_3.rs"]
44			mod bls381_x3_3;
45			use bls381_x3_3::{
46				EXPONENTIATION, FULL_ROUNDS, MDS_ENTRIES, PARTIAL_ROUNDS, ROUND_CONSTS, WIDTH,
47			};
48			get_poseidon_data(
49				ROUND_CONSTS,
50				MDS_ENTRIES,
51				FULL_ROUNDS,
52				PARTIAL_ROUNDS,
53				WIDTH,
54				EXPONENTIATION,
55			)
56		}
57		#[cfg(feature = "poseidon_bn254_x3_3")]
58		(Curve::Bn254, 3, 3) => {
59			#[path = "./bn254_x3_3.rs"]
60			pub mod bn254_x3_3;
61			use bn254_x3_3::{
62				EXPONENTIATION, FULL_ROUNDS, MDS_ENTRIES, PARTIAL_ROUNDS, ROUND_CONSTS, WIDTH,
63			};
64			get_poseidon_data(
65				ROUND_CONSTS,
66				MDS_ENTRIES,
67				FULL_ROUNDS,
68				PARTIAL_ROUNDS,
69				WIDTH,
70				EXPONENTIATION,
71			)
72		}
73		#[cfg(feature = "poseidon_bls381_x3_5")]
74		(Curve::Bls381, 3, 5) => {
75			#[path = "./bls381_x3_5.rs"]
76			pub mod bls381_x3_5;
77			use bls381_x3_5::{
78				EXPONENTIATION, FULL_ROUNDS, MDS_ENTRIES, PARTIAL_ROUNDS, ROUND_CONSTS, WIDTH,
79			};
80			get_poseidon_data(
81				ROUND_CONSTS,
82				MDS_ENTRIES,
83				FULL_ROUNDS,
84				PARTIAL_ROUNDS,
85				WIDTH,
86				EXPONENTIATION,
87			)
88		}
89		#[cfg(feature = "poseidon_bn254_x3_5")]
90		(Curve::Bn254, 3, 5) => {
91			#[path = "./bn254_x3_5.rs"]
92			pub mod bn254_x3_5;
93			use bn254_x3_5::{
94				EXPONENTIATION, FULL_ROUNDS, MDS_ENTRIES, PARTIAL_ROUNDS, ROUND_CONSTS, WIDTH,
95			};
96			get_poseidon_data(
97				ROUND_CONSTS,
98				MDS_ENTRIES,
99				FULL_ROUNDS,
100				PARTIAL_ROUNDS,
101				WIDTH,
102				EXPONENTIATION,
103			)
104		}
105		#[cfg(feature = "poseidon_bls381_x5_3")]
106		(Curve::Bls381, 5, 3) => {
107			#[path = "./bls381_x5_3.rs"]
108			pub mod bls381_x5_3;
109			use bls381_x5_3::{
110				EXPONENTIATION, FULL_ROUNDS, MDS_ENTRIES, PARTIAL_ROUNDS, ROUND_CONSTS, WIDTH,
111			};
112			get_poseidon_data(
113				ROUND_CONSTS,
114				MDS_ENTRIES,
115				FULL_ROUNDS,
116				PARTIAL_ROUNDS,
117				WIDTH,
118				EXPONENTIATION,
119			)
120		}
121		#[cfg(feature = "poseidon_bn254_x5_3")]
122		(Curve::Bn254, 5, 3) => {
123			#[path = "./bn254_x5_3.rs"]
124			pub mod bn254_x5_3;
125			use bn254_x5_3::{
126				EXPONENTIATION, FULL_ROUNDS, MDS_ENTRIES, PARTIAL_ROUNDS, ROUND_CONSTS, WIDTH,
127			};
128			get_poseidon_data(
129				ROUND_CONSTS,
130				MDS_ENTRIES,
131				FULL_ROUNDS,
132				PARTIAL_ROUNDS,
133				WIDTH,
134				EXPONENTIATION,
135			)
136		}
137		#[cfg(feature = "poseidon_bn254_x5_2")]
138		(Curve::Bn254, 5, 2) => {
139			#[path = "./bn254_x5_2.rs"]
140			pub mod bn254_x5_2;
141			use bn254_x5_2::{
142				EXPONENTIATION, FULL_ROUNDS, MDS_ENTRIES, PARTIAL_ROUNDS, ROUND_CONSTS, WIDTH,
143			};
144			get_poseidon_data(
145				ROUND_CONSTS,
146				MDS_ENTRIES,
147				FULL_ROUNDS,
148				PARTIAL_ROUNDS,
149				WIDTH,
150				EXPONENTIATION,
151			)
152		}
153		#[cfg(feature = "poseidon_bn254_x5_4")]
154		(Curve::Bn254, 5, 4) => {
155			#[path = "./bn254_x5_4.rs"]
156			pub mod bn254_x5_4;
157			use bn254_x5_4::{
158				EXPONENTIATION, FULL_ROUNDS, MDS_ENTRIES, PARTIAL_ROUNDS, ROUND_CONSTS, WIDTH,
159			};
160			get_poseidon_data(
161				ROUND_CONSTS,
162				MDS_ENTRIES,
163				FULL_ROUNDS,
164				PARTIAL_ROUNDS,
165				WIDTH,
166				EXPONENTIATION,
167			)
168		}
169		#[cfg(feature = "poseidon_bls381_x5_5")]
170		(Curve::Bls381, 5, 5) => {
171			#[path = "./bls381_x5_5.rs"]
172			pub mod bls381_x5_5;
173			use bls381_x5_5::{
174				EXPONENTIATION, FULL_ROUNDS, MDS_ENTRIES, PARTIAL_ROUNDS, ROUND_CONSTS, WIDTH,
175			};
176			get_poseidon_data(
177				ROUND_CONSTS,
178				MDS_ENTRIES,
179				FULL_ROUNDS,
180				PARTIAL_ROUNDS,
181				WIDTH,
182				EXPONENTIATION,
183			)
184		}
185		#[cfg(feature = "poseidon_bn254_x5_5")]
186		(Curve::Bn254, 5, 5) => {
187			#[path = "./bn254_x5_5.rs"]
188			pub mod bn254_x5_5;
189			use bn254_x5_5::{
190				EXPONENTIATION, FULL_ROUNDS, MDS_ENTRIES, PARTIAL_ROUNDS, ROUND_CONSTS, WIDTH,
191			};
192			get_poseidon_data(
193				ROUND_CONSTS,
194				MDS_ENTRIES,
195				FULL_ROUNDS,
196				PARTIAL_ROUNDS,
197				WIDTH,
198				EXPONENTIATION,
199			)
200		}
201		#[cfg(feature = "poseidon_bls381_x17_3")]
202		(Curve::Bls381, 17, 3) => {
203			#[path = "./bls381_x17_3.rs"]
204			pub mod bls381_x17_3;
205			use bls381_x17_3::{
206				EXPONENTIATION, FULL_ROUNDS, MDS_ENTRIES, PARTIAL_ROUNDS, ROUND_CONSTS, WIDTH,
207			};
208			get_poseidon_data(
209				ROUND_CONSTS,
210				MDS_ENTRIES,
211				FULL_ROUNDS,
212				PARTIAL_ROUNDS,
213				WIDTH,
214				EXPONENTIATION,
215			)
216		}
217		#[cfg(feature = "poseidon_bn254_x17_3")]
218		(Curve::Bn254, 17, 3) => {
219			#[path = "./bn254_x17_3.rs"]
220			pub mod bn254_x17_3;
221			use bn254_x17_3::{
222				EXPONENTIATION, FULL_ROUNDS, MDS_ENTRIES, PARTIAL_ROUNDS, ROUND_CONSTS, WIDTH,
223			};
224			get_poseidon_data(
225				ROUND_CONSTS,
226				MDS_ENTRIES,
227				FULL_ROUNDS,
228				PARTIAL_ROUNDS,
229				WIDTH,
230				EXPONENTIATION,
231			)
232		}
233		#[cfg(feature = "poseidon_bls381_x17_5")]
234		(Curve::Bls381, 17, 5) => {
235			#[path = "./bls381_x17_5.rs"]
236			pub mod bls381_x17_5;
237			use bls381_x17_5::{
238				EXPONENTIATION, FULL_ROUNDS, MDS_ENTRIES, PARTIAL_ROUNDS, ROUND_CONSTS, WIDTH,
239			};
240			get_poseidon_data(
241				ROUND_CONSTS,
242				MDS_ENTRIES,
243				FULL_ROUNDS,
244				PARTIAL_ROUNDS,
245				WIDTH,
246				EXPONENTIATION,
247			)
248		}
249		#[cfg(feature = "poseidon_bn254_x17_5")]
250		(Curve::Bn254, 17, 5) => {
251			#[path = "./bn254_x17_5.rs"]
252			pub mod bn254_x17_5;
253			use bn254_x17_5::{
254				EXPONENTIATION, FULL_ROUNDS, MDS_ENTRIES, PARTIAL_ROUNDS, ROUND_CONSTS, WIDTH,
255			};
256			get_poseidon_data(
257				ROUND_CONSTS,
258				MDS_ENTRIES,
259				FULL_ROUNDS,
260				PARTIAL_ROUNDS,
261				WIDTH,
262				EXPONENTIATION,
263			)
264		}
265		_ => unimplemented!(),
266	}
267}
268
269pub fn get_poseidon_result(curve: Curve, exp: i8, width: u8) -> Result<Vec<Bytes>, FromHexError> {
270	match (curve, exp, width) {
271		#[cfg(feature = "poseidon_bn254_x5_5")]
272		(Curve::Bn254, 5, 5) => {
273			#[path = "./bn254_x5_5_result.rs"]
274			pub mod bn254_x5_5_result;
275			parse_vec(bn254_x5_5_result::RESULT.to_vec())
276		}
277		#[cfg(feature = "poseidon_bn254_x5_3")]
278		(Curve::Bn254, 5, 3) => {
279			#[path = "./bn254_x5_3_result.rs"]
280			pub mod bn254_x5_3_result;
281			parse_vec(bn254_x5_3_result::RESULT.to_vec())
282		}
283		_ => unimplemented!(),
284	}
285}
286
287pub fn get_poseidon_data(
288	rounds: &[&str],
289	mds: &[&[&str]],
290	full_rounds: u8,
291	partial_rounds: u8,
292	width: u8,
293	exp: i8,
294) -> Result<PoseidonData, FromHexError> {
295	let rounds = parse_vec(rounds.to_vec())?;
296	let mds = parse_matrix(mds.iter().map(|x| x.to_vec()).collect::<Vec<_>>())?;
297	Ok(PoseidonData::new(
298		mds,
299		rounds,
300		full_rounds,
301		partial_rounds,
302		width,
303		exp,
304	))
305}