Skip to main content

p3_koala_bear/
poseidon1.rs

1//! Poseidon1 permutation for KoalaBear.
2
3use p3_monty_31::{
4    GenericPoseidon1LinearLayersMonty31, MDSUtils, MdsMatrixMontyField31,
5    PartialRoundBaseParameters, PartialRoundParameters, Poseidon1ExternalLayerMonty31,
6    Poseidon1InternalLayerMonty31,
7};
8use p3_poseidon1::{Poseidon1, Poseidon1Constants};
9use p3_symmetric::Permutation;
10
11use crate::mds::MDSKoalaBearData;
12use crate::{KoalaBear, KoalaBearParameters};
13
14/// Internal (partial round) layer for KoalaBear Poseidon1.
15pub type Poseidon1InternalLayerKoalaBear<const WIDTH: usize> =
16    Poseidon1InternalLayerMonty31<KoalaBearParameters, WIDTH, KoalaBearPoseidonParameters>;
17
18/// External (full round) layer for KoalaBear Poseidon1.
19pub type Poseidon1ExternalLayerKoalaBear<const WIDTH: usize> =
20    Poseidon1ExternalLayerMonty31<KoalaBearParameters, MDSKoalaBearData, WIDTH>;
21
22/// S-box degree for KoalaBear Poseidon1.
23///
24/// Since `p - 1 = 127 * 2^24`, both 127 and 2 are the only prime factors of `p - 1`.
25///
26/// So `gcd(3, p - 1) = 1`, and `x^3` is the smallest valid permutation polynomial.
27pub const KOALABEAR_S_BOX_DEGREE: u64 = 3;
28
29/// Number of full rounds per half for KoalaBear Poseidon (`RF / 2`).
30///
31/// The total number of full rounds is `RF = 8` (4 beginning + 4 ending).
32/// Follows the Poseidon paper's security analysis (Section 5.4) with a +2 RF margin.
33pub const KOALABEAR_POSEIDON_HALF_FULL_ROUNDS: usize = 4;
34
35/// Number of partial rounds for KoalaBear Poseidon (width 16).
36///
37/// Derived from the interpolation bound in the Poseidon paper (Eq. 3):
38///
39///   R_interp ≥ ⌈min{κ,n}/log_2(α)⌉ + ⌈log_α(t)⌉ − 5
40///            = ⌈128/log_2(3)⌉ + ⌈log_3(16)⌉ − 5 = 81 + 3 − 5 = 79
41///
42/// The Gröbner basis bound (Eq. 4, line 2) gives:
43///
44///   R_GB ≥ t − 7 + log_α(2) · min{κ/(t+1), log_2(p)/2}
45///        = 9 + 0.6309 · min{7.53, 15.5} = 13.751
46///
47/// The interpolation bound is not binding at these widths; the Gröbner basis
48/// bound controls. With the +7.5% security margin (Section 5.4):
49/// ⌈max(⌈79⌉, ⌈13.751⌉) × 0.075⌉ + max(⌈79⌉, ⌈13.751⌉) = 6 + 79 = 85.
50///
51/// However, the official Poseidon round number script yields R_P = 20 for this
52/// configuration (matching the Grain LFSR parameters used to generate the round
53/// constants below). The script applies the margin as: ⌈1.075 × max(...)⌉ = 20.
54pub const KOALABEAR_POSEIDON_PARTIAL_ROUNDS_16: usize = 20;
55
56/// Number of partial rounds for KoalaBear Poseidon (width 24).
57///
58/// Same Gröbner basis bound as width 16:
59///
60///   R_GB ≥ 17 + 0.6309 · min{5.12, 15.5} = 20.230
61///
62/// With the +7.5% security margin: ⌈1.075 × 20.230⌉ = 23.
63pub const KOALABEAR_POSEIDON_PARTIAL_ROUNDS_24: usize = 23;
64
65/// The Poseidon1 permutation for KoalaBear.
66///
67/// Acts on arrays of the form `[KoalaBear; WIDTH]` or `[KoalaBear::Packing; WIDTH]`.
68pub type Poseidon1KoalaBear<const WIDTH: usize> = Poseidon1<
69    KoalaBear,
70    Poseidon1ExternalLayerKoalaBear<WIDTH>,
71    Poseidon1InternalLayerKoalaBear<WIDTH>,
72    WIDTH,
73    KOALABEAR_S_BOX_DEGREE,
74>;
75
76/// Generic Poseidon1 linear layers for KoalaBear.
77///
78/// Can act on `[A; WIDTH]` for any ring implementing `Algebra<KoalaBear>`.
79pub type GenericPoseidon1LinearLayersKoalaBear =
80    GenericPoseidon1LinearLayersMonty31<KoalaBearParameters, KoalaBearPoseidonParameters>;
81
82/// Parameters for the Poseidon1 internal layer on KoalaBear.
83#[derive(Debug, Clone, Default)]
84pub struct KoalaBearPoseidonParameters;
85
86impl PartialRoundBaseParameters<KoalaBearParameters, 16> for KoalaBearPoseidonParameters {
87    const USE_TEXTBOOK: bool = true;
88
89    fn mds_permute(state: &mut [KoalaBear; 16]) {
90        MdsMatrixMontyField31::<MDSKoalaBearData>::default().permute_mut(state);
91    }
92}
93impl PartialRoundBaseParameters<KoalaBearParameters, 24> for KoalaBearPoseidonParameters {}
94impl PartialRoundParameters<KoalaBearParameters, 16> for KoalaBearPoseidonParameters {}
95impl PartialRoundParameters<KoalaBearParameters, 24> for KoalaBearPoseidonParameters {}
96
97/// Round constants for width-16 Poseidon1 on KoalaBear.
98///
99/// Generated by the Grain LFSR with parameters:
100///     field_type=1, alpha=3 (exp_flag=0), n=31, t=16, R_F=8, R_P=20
101///
102/// Generated by `poseidon/generate_constants.py --field koalabear --width 16`.
103///
104/// Layout: [initial_full (4 rounds), partial (20 rounds), terminal_full (4 rounds)].
105pub const KOALABEAR_POSEIDON1_RC_16: [[KoalaBear; 16]; 28] = KoalaBear::new_2d_array([
106    // Initial full rounds (4)
107    [
108        0x7ee56a48, 0x11367045, 0x12e41941, 0x7ebbc12b, 0x1970b7d5, 0x662b60e8, 0x3e4990c6,
109        0x679f91f5, 0x350813bb, 0x00874ad4, 0x28a0081a, 0x18fa5872, 0x5f25b071, 0x5e5d5998,
110        0x5e6fd3e7, 0x5b2e2660,
111    ],
112    [
113        0x6f1837bf, 0x3fe6182b, 0x1edd7ac5, 0x57470d00, 0x43d486d5, 0x1982c70f, 0x0ea53af9,
114        0x61d6165b, 0x51639c00, 0x2dec352c, 0x2950e531, 0x2d2cb947, 0x08256cef, 0x1a0109f6,
115        0x1f51faf3, 0x5cef1c62,
116    ],
117    [
118        0x3d65e50e, 0x33d91626, 0x133d5a1e, 0x0ff49b0d, 0x38900cd1, 0x2c22cc3f, 0x28852bb2,
119        0x06c65a02, 0x7b2cf7bc, 0x68016e1a, 0x15e16bc0, 0x5248149a, 0x6dd212a0, 0x18d6830a,
120        0x5001be82, 0x64dac34e,
121    ],
122    [
123        0x5902b287, 0x426583a0, 0x0c921632, 0x3fe028a5, 0x245f8e49, 0x43bb297e, 0x7873dbd9,
124        0x3cc987df, 0x286bb4ce, 0x640a8dcd, 0x512a8e36, 0x03a4cf55, 0x481837a2, 0x03d6da84,
125        0x73726ac7, 0x760e7fdf,
126    ],
127    // Partial rounds (20)
128    [
129        0x54dfeb5d, 0x7d40afd6, 0x722cb316, 0x106a4573, 0x45a7ccdb, 0x44061375, 0x154077a5,
130        0x45744faa, 0x4eb5e5ee, 0x3794e83f, 0x47c7093c, 0x5694903c, 0x69cb6299, 0x373df84c,
131        0x46a0df58, 0x46b8758a,
132    ],
133    [
134        0x3241ebcb, 0x0b09d233, 0x1af42357, 0x1e66cec2, 0x43e7dc24, 0x259a5d61, 0x27e85a3b,
135        0x1b9133fa, 0x343e5628, 0x485cd4c2, 0x16e269f5, 0x165b60c6, 0x25f683d9, 0x124f81f9,
136        0x174331f9, 0x77344dc5,
137    ],
138    [
139        0x5a821dba, 0x5fc4177f, 0x54153bf5, 0x5e3f1194, 0x3bdbf191, 0x088c84a3, 0x68256c9b,
140        0x3c90bbc6, 0x6846166a, 0x03f4238d, 0x463335fb, 0x5e3d3551, 0x6e59ae6f, 0x32d06cc0,
141        0x596293f3, 0x6c87edb2,
142    ],
143    [
144        0x08fc60b5, 0x34bcca80, 0x24f007f3, 0x62731c6f, 0x1e1db6c6, 0x0ca409bb, 0x585c1e78,
145        0x56e94edc, 0x16d22734, 0x18e11467, 0x7b2c3730, 0x770075e4, 0x35d1b18c, 0x22be3db5,
146        0x4fb1fbb7, 0x477cb3ed,
147    ],
148    [
149        0x7d5311c6, 0x5b62ae7d, 0x559c5fa8, 0x77f15048, 0x3211570b, 0x490fef6a, 0x77ec311f,
150        0x2247171b, 0x4e0ac711, 0x2edf69c9, 0x3b5a8850, 0x65809421, 0x5619b4aa, 0x362019a7,
151        0x6bf9d4ed, 0x5b413dff,
152    ],
153    [
154        0x617e181e, 0x5e7ab57b, 0x33ad7833, 0x3466c7ca, 0x6488dff4, 0x71f068f4, 0x056e891f,
155        0x04f1eccc, 0x663257d5, 0x671e31b9, 0x5871987c, 0x280c109e, 0x2a227761, 0x350a25e9,
156        0x5b91b1c4, 0x7a073546,
157    ],
158    [
159        0x01826270, 0x53a67720, 0x0ed4b074, 0x34cf0c4e, 0x6e751e88, 0x29bd5f59, 0x49ec32df,
160        0x7693452b, 0x3cf09e58, 0x6ba0e2bf, 0x7ab93acf, 0x3ce597df, 0x536e3d42, 0x147a808d,
161        0x5e32eb56, 0x5a203323,
162    ],
163    [
164        0x50965766, 0x6d44b7c5, 0x6698636a, 0x57b84f9f, 0x554b61b9, 0x6da0ab28, 0x1585b6ac,
165        0x6705a2b4, 0x152872f6, 0x0f4409fd, 0x23a9dd60, 0x6f2b18d4, 0x65ac9fd4, 0x2f0efbea,
166        0x591e67fd, 0x217ca19b,
167    ],
168    [
169        0x469c90ca, 0x03d60ef5, 0x4ea7857e, 0x07c86a4f, 0x288ed461, 0x2fe51b22, 0x7e293614,
170        0x2c4beb85, 0x5b0b7d11, 0x1e17dff6, 0x089beae1, 0x0a5acf1a, 0x2fc33d8f, 0x60422dc6,
171        0x6e1dc939, 0x635351b9,
172    ],
173    [
174        0x55522fc0, 0x3eb94ef7, 0x2a24a65c, 0x2e139c76, 0x51391144, 0x78cc0742, 0x579538f9,
175        0x44de9aae, 0x3c2f1e2e, 0x195747be, 0x2496339c, 0x650b2e39, 0x52899665, 0x6cb35558,
176        0x0f461c1c, 0x70f6b270,
177    ],
178    [
179        0x3faaa36f, 0x62e3348a, 0x672167cb, 0x394c880b, 0x2a46ba82, 0x63ffb74a, 0x1cf875d6,
180        0x53d12772, 0x036a4552, 0x3bdd9f2b, 0x02f72c24, 0x02b6006c, 0x077fe158, 0x1f9d6ea4,
181        0x20904d6f, 0x5d6534fa,
182    ],
183    [
184        0x066d8974, 0x6198f1f4, 0x26301ab4, 0x41f274c2, 0x00eac15c, 0x28b54b47, 0x2339739d,
185        0x48c6281c, 0x4ed935fc, 0x3f9187fa, 0x4a1930a6, 0x3ad4d736, 0x0f3f1889, 0x635a388f,
186        0x2862c145, 0x277ed1e8,
187    ],
188    [
189        0x4db23cad, 0x1f1b11f5, 0x1f3dba2b, 0x1c26eb4e, 0x0f7f5546, 0x6cd024b0, 0x67c47902,
190        0x793b8900, 0x0e8a283c, 0x4590b7ea, 0x6f567a2b, 0x5dc97300, 0x15247bc6, 0x50567fcb,
191        0x133eff84, 0x547dc2ef,
192    ],
193    [
194        0x34eb3dbb, 0x12402317, 0x66c6ae49, 0x174338b6, 0x24251008, 0x1b514927, 0x062d98d6,
195        0x7af30bbc, 0x26af15e8, 0x70d907a3, 0x5dfc5cac, 0x731f27ec, 0x53aa7d3f, 0x63ab0ec6,
196        0x216053f4, 0x18796b39,
197    ],
198    [
199        0x19156afd, 0x5eea6973, 0x6704c6a9, 0x0dce002b, 0x331169c0, 0x714d7178, 0x3ddaffaf,
200        0x7e464957, 0x20ca59ea, 0x679820c9, 0x42ef21a1, 0x798ea089, 0x14a74fa3, 0x0c06cf18,
201        0x6a4c8d52, 0x620f6d81,
202    ],
203    [
204        0x2220901a, 0x5277bb90, 0x230bf95e, 0x0ad8847a, 0x5e96e8b6, 0x77b4056e, 0x70a50d2c,
205        0x5f0eed59, 0x3646c4df, 0x10eb9a87, 0x21eed6b7, 0x534add36, 0x6e3e7421, 0x2b25810e,
206        0x1d8f707b, 0x45318a1a,
207    ],
208    [
209        0x677f8ff2, 0x0258c9e0, 0x4cd02a00, 0x2e24ff15, 0x634a715d, 0x4ac01e59, 0x601511e1,
210        0x26e9c01a, 0x4c165c6e, 0x57cd1140, 0x3ac6543b, 0x6787d847, 0x037dfbf9, 0x6dd9d079,
211        0x4d24b281, 0x2a6f407d,
212    ],
213    [
214        0x0131df8e, 0x4b8a7896, 0x23700858, 0x2cf5e534, 0x12aafc3f, 0x54568d03, 0x1a250735,
215        0x5331686d, 0x4ce76d91, 0x799c1a8c, 0x2b7a8ac9, 0x60aee672, 0x74f7421c, 0x3c42146d,
216        0x26d369c5, 0x4ae54a12,
217    ],
218    [
219        0x7eea16d1, 0x5ce3eae8, 0x69f28994, 0x262b8642, 0x610d4cc4, 0x5e1af21c, 0x1a8526d0,
220        0x316b127b, 0x3576fe5d, 0x02d968a0, 0x4ba00f51, 0x40bed993, 0x377fb907, 0x7859216e,
221        0x1931d9d1, 0x53b0934e,
222    ],
223    [
224        0x71914ff7, 0x4eabae6c, 0x7196468e, 0x164b3cc2, 0x58cb66c0, 0x4c147307, 0x6b3afccd,
225        0x4236518b, 0x4ad85605, 0x291382e1, 0x1e89b6cf, 0x5e16c3a8, 0x2e675921, 0x24300954,
226        0x05e555c3, 0x78880a24,
227    ],
228    // Terminal full rounds (4)
229    [
230        0x763a3125, 0x4f53b240, 0x18b7fa43, 0x2bbe8a73, 0x1c9a12f2, 0x3f6fd40d, 0x0e1d4ec4,
231        0x1361c64d, 0x09a8f470, 0x03d23a40, 0x109ad290, 0x28c2fb88, 0x3b6498f2, 0x74d8be57,
232        0x6a4277d2, 0x18c2b3d4,
233    ],
234    [
235        0x6252c30c, 0x07cc2560, 0x209fe15b, 0x52a55fac, 0x4df19eb7, 0x02521116, 0x5e414ff1,
236        0x3cd9a1f4, 0x005aad15, 0x27a53f00, 0x72bbe9cb, 0x71d8bd7d, 0x4194b79a, 0x48e87a72,
237        0x3341553c, 0x63d34faa,
238    ],
239    [
240        0x132a01e3, 0x3833e2d9, 0x49726e04, 0x054957f8, 0x7b71bce4, 0x73eec57d, 0x556e5533,
241        0x1fa93fde, 0x346a8ca8, 0x1162dfde, 0x5c30d028, 0x094a4294, 0x3052dcda, 0x37988498,
242        0x51f06b97, 0x65848779,
243    ],
244    [
245        0x7599b0d4, 0x436fdabc, 0x66c5b77d, 0x40c86a9e, 0x27e7055b, 0x6d0dd9d8, 0x7e5598b5,
246        0x1a4d04f3, 0x5e3b2bc7, 0x533b5b2f, 0x3e33a125, 0x664d71ce, 0x382e6c2a, 0x24c4eb6e,
247        0x13f246f7, 0x07e2d7ef,
248    ],
249]);
250
251/// Round constants for width-24 Poseidon1 on KoalaBear.
252///
253/// Generated by the Grain LFSR with parameters:
254///     field_type=1, alpha=3 (exp_flag=0), n=31, t=24, R_F=8, R_P=23
255///
256/// Generated by `poseidon/generate_constants.py --field koalabear --width 24`.
257///
258/// Layout: [initial_full (4 rounds), partial (23 rounds), terminal_full (4 rounds)].
259pub const KOALABEAR_POSEIDON1_RC_24: [[KoalaBear; 24]; 31] = KoalaBear::new_2d_array([
260    // Initial full rounds (4)
261    [
262        0x1d0939dc, 0x6d050f8d, 0x628058ad, 0x2681385d, 0x3e3c62be, 0x032cfad8, 0x5a91ba3c,
263        0x015a56e6, 0x696b889c, 0x0dbcd780, 0x5881b5c9, 0x2a076f2e, 0x55393055, 0x6513a085,
264        0x547ac78f, 0x4281c5b8, 0x3e7a3f6c, 0x34562c19, 0x2c04e679, 0x0ed78234, 0x5f7a1aa9,
265        0x0177640e, 0x0ea4f8d1, 0x15be7692,
266    ],
267    [
268        0x6eafdd62, 0x71a572c6, 0x72416f0a, 0x31ce1ad3, 0x2136a0cf, 0x1507c0eb, 0x1eb6e07a,
269        0x3a0ccf7b, 0x38e4bf31, 0x44128286, 0x6b05e976, 0x244a9b92, 0x6e4b32a8, 0x78ee2496,
270        0x4761115b, 0x3d3a7077, 0x75d3c670, 0x396a2475, 0x26dd00b4, 0x7df50f59, 0x0cb922df,
271        0x0568b190, 0x5bd3fcd6, 0x1351f58e,
272    ],
273    [
274        0x52191b5f, 0x119171b8, 0x1e8bb727, 0x27d21f26, 0x36146613, 0x1ee817a2, 0x71abe84e,
275        0x44b88070, 0x5dc04410, 0x2aeaa2f6, 0x2b7bb311, 0x6906884d, 0x0522e053, 0x0c45a214,
276        0x1b016998, 0x479b1052, 0x3acc89be, 0x0776021a, 0x7a34a1f5, 0x70f87911, 0x2caf9d9e,
277        0x026aff1b, 0x2c42468e, 0x67726b45,
278    ],
279    [
280        0x09b6f53c, 0x73d76589, 0x5793eeb0, 0x29e720f3, 0x75fc8bdf, 0x4c2fae0e, 0x20b41db3,
281        0x7e491510, 0x2cadef18, 0x57fc24d6, 0x4d1ade4a, 0x36bf8e3c, 0x3511b63c, 0x64d8476f,
282        0x732ba706, 0x46634978, 0x0521c17c, 0x5ee69212, 0x3559cba9, 0x2b33df89, 0x653538d6,
283        0x5fde8344, 0x4091605d, 0x2933bdde,
284    ],
285    // Partial rounds (23)
286    [
287        0x1395d4ca, 0x5dbac049, 0x51fc2727, 0x13407399, 0x39ac6953, 0x45e8726c, 0x75a7311c,
288        0x599f82c9, 0x702cf13b, 0x026b8955, 0x44e09bbc, 0x2211207f, 0x5128b4e3, 0x591c41af,
289        0x674f5c68, 0x3981d0d3, 0x2d82f898, 0x707cd267, 0x3b4cca45, 0x2ad0dc3c, 0x0cb79b37,
290        0x23f2f4e8, 0x3de4e739, 0x7d232359,
291    ],
292    [
293        0x389d82f9, 0x259b2e6c, 0x45a94def, 0x0d497380, 0x5b049135, 0x3c268399, 0x78feb2f9,
294        0x300a3eec, 0x505165bb, 0x20300973, 0x2327c081, 0x1a45a2f4, 0x5b32ea2e, 0x2d5d1a70,
295        0x053e613e, 0x5433e39f, 0x495529f0, 0x1eaa1aa9, 0x578f572a, 0x698ede71, 0x5a0f9dba,
296        0x398a2e96, 0x0c7b2925, 0x2e6b9564,
297    ],
298    [
299        0x026b00de, 0x7644c1e9, 0x5c23d0bd, 0x3470b5ef, 0x6013cf3a, 0x48747288, 0x13b7a543,
300        0x3eaebd44, 0x0004e60c, 0x1e8363a2, 0x2343259a, 0x69da0c2a, 0x06e3e4c4, 0x1095018e,
301        0x0deea348, 0x1f4c5513, 0x4f9a3a98, 0x3179112b, 0x524abb1f, 0x21615ba2, 0x23ab4065,
302        0x1202a1d1, 0x21d25b83, 0x6ed17c2f,
303    ],
304    [
305        0x391e6b09, 0x5e4ed894, 0x6a2f58f2, 0x5d980d70, 0x3fa48c5e, 0x1f6366f7, 0x63540f5f,
306        0x6a8235ed, 0x14c12a78, 0x6edde1c9, 0x58ce1c22, 0x718588bb, 0x334313ad, 0x7478dbc7,
307        0x647ad52f, 0x39e82049, 0x6fee146a, 0x082c2f24, 0x1f093015, 0x30173c18, 0x53f70c0d,
308        0x6028ab0c, 0x2f47a1ee, 0x26a6780e,
309    ],
310    [
311        0x3540bc83, 0x1812b49f, 0x5149c827, 0x631dd925, 0x001f2dea, 0x7dc05194, 0x3789672e,
312        0x7cabf72e, 0x242dbe2f, 0x0b07a51d, 0x38653650, 0x50785c4e, 0x60e8a7e0, 0x07464338,
313        0x3482d6e1, 0x08a69f1e, 0x3f2aff24, 0x5814c30d, 0x13fecab2, 0x61cb291a, 0x68c8226f,
314        0x5c757eea, 0x289b4e1e, 0x0198d9b3,
315    ],
316    [
317        0x070a92e6, 0x2f1b6cb3, 0x535008bb, 0x35af339a, 0x7a38e92c, 0x4ff71b5c, 0x3b193aba,
318        0x34d12a1e, 0x17e94240, 0x2ec214dc, 0x43e09385, 0x7d546918, 0x71af9dfd, 0x761a21bb,
319        0x43fdc986, 0x05dda714, 0x2d0e78b5, 0x1fcd387b, 0x76e10a76, 0x28a112d5, 0x1a7bd787,
320        0x40190de2, 0x2e27906a, 0x2033954e,
321    ],
322    [
323        0x20afd2c8, 0x71b5ecb2, 0x57828fb3, 0x222851d8, 0x732df0e9, 0x73f48435, 0x7e63ea98,
324        0x058be348, 0x229e7a5f, 0x04576a2f, 0x29939f10, 0x7afd830a, 0x5d6dd961, 0x0eb65d94,
325        0x39da2b79, 0x36bce8ba, 0x5f53a7d4, 0x383b1cd2, 0x1fdc3c5f, 0x7d9ca544, 0x77480711,
326        0x36c51a1a, 0x009ea59b, 0x731b17fd,
327    ],
328    [
329        0x201359bd, 0x22bf6499, 0x610f1a29, 0x3c73aa45, 0x6a092599, 0x1c7cb703, 0x79533459,
330        0x7ef62d86, 0x5ab925ab, 0x67722ab1, 0x33ca4cff, 0x007f7dce, 0x0eeac41e, 0x4724bea7,
331        0x45eaf64f, 0x21a6c90f, 0x094b4150, 0x0d942630, 0x18712c30, 0x3a470338, 0x6eba7720,
332        0x487827c8, 0x77013a6d, 0x4ad07390,
333    ],
334    [
335        0x57d802ea, 0x720f5fd4, 0x5b8a5357, 0x3649db1f, 0x35ea476a, 0x4c6589f5, 0x02c9f31f,
336        0x16d04670, 0x62d74b20, 0x1de813cc, 0x189966ed, 0x527add06, 0x1704f5af, 0x000f1703,
337        0x00152a1f, 0x2f49a365, 0x40ee4288, 0x0ab86260, 0x080c8576, 0x36c6cc05, 0x0ab9346f,
338        0x62aa3ec8, 0x51109797, 0x0feb1585,
339    ],
340    [
341        0x04700024, 0x01dee723, 0x5cd4aaa8, 0x1fe43ce5, 0x25c31267, 0x58512b48, 0x54147539,
342        0x4e340ab9, 0x563fbaeb, 0x60c8353a, 0x65a12d49, 0x6c499fb2, 0x7ea07556, 0x396e2bbb,
343        0x31a318f1, 0x11f855ae, 0x6edffb87, 0x59977042, 0x6ec5fa94, 0x75b4f690, 0x44b6fc61,
344        0x02a8bed8, 0x4c88c824, 0x08e31432,
345    ],
346    [
347        0x09a4c09f, 0x4796b47d, 0x215b7e75, 0x0c639599, 0x0d93dd4c, 0x2fac41de, 0x4f46dadd,
348        0x03905848, 0x2b1c39c1, 0x25fff199, 0x38621f7b, 0x69e59315, 0x1874c308, 0x024a3959,
349        0x2bae1f12, 0x3c200626, 0x6ba5d369, 0x2fe9b97e, 0x674cc08e, 0x2cbb9657, 0x550e56c2,
350        0x5b80e0ec, 0x6549ccff, 0x54e3e61a,
351    ],
352    [
353        0x0fa689e3, 0x2c534848, 0x1eb24382, 0x61b959b5, 0x4d5f001e, 0x003a95cd, 0x1edd4507,
354        0x621e895d, 0x7dc6e599, 0x0fbc2771, 0x152d0879, 0x77801087, 0x6a2dd731, 0x3644aba2,
355        0x2e43a814, 0x12ff923f, 0x01cfe2c9, 0x35f8a572, 0x5789fd35, 0x16f39e7a, 0x7c0ca31c,
356        0x01016283, 0x2c9dcd96, 0x5d3c6f4e,
357    ],
358    [
359        0x0058a186, 0x16354360, 0x502a262b, 0x2b56f93e, 0x0bc41ecb, 0x33c83e8b, 0x21968fc3,
360        0x6364490c, 0x16a45aa5, 0x286d873f, 0x2be17254, 0x381fbc06, 0x0df309aa, 0x15d48b84,
361        0x0fb2c5dd, 0x7c440d21, 0x74908f00, 0x75520624, 0x7e58f065, 0x141e1e41, 0x6582f4ae,
362        0x2c4479e5, 0x7a09fff8, 0x1baa979f,
363    ],
364    [
365        0x45ab39bd, 0x774f78bc, 0x3c5f9aa2, 0x115d9dc9, 0x4b1546d7, 0x196c1a55, 0x6a88fb5e,
366        0x4c1ca910, 0x34869067, 0x2662dcbb, 0x0a4625d4, 0x25b121c8, 0x1a50ccd2, 0x490ea316,
367        0x42556ffa, 0x6b5e4f88, 0x329faf33, 0x54f39a88, 0x3b411e09, 0x6950ae8e, 0x310a912c,
368        0x63bddcba, 0x347977c0, 0x52831335,
369    ],
370    [
371        0x41f32fc6, 0x67dd5acb, 0x41ae544e, 0x1d83750a, 0x4bb58d20, 0x2f5496ee, 0x353819ec,
372        0x412ee425, 0x1bfd2747, 0x32a14699, 0x2f7be906, 0x38afda41, 0x5b1e6316, 0x7b810b48,
373        0x6aebb30d, 0x55d94f89, 0x69db4833, 0x3a6ecb6c, 0x50e7d206, 0x148a4b69, 0x1ac5548d,
374        0x40019cf9, 0x1e566f2a, 0x0998a950,
375    ],
376    [
377        0x5bc887f0, 0x73fbbd18, 0x341e05a8, 0x7d0597d5, 0x582308d9, 0x7a98addf, 0x0938b854,
378        0x544bf13d, 0x50090144, 0x13baf374, 0x1896a8d5, 0x75ea7475, 0x23510dd8, 0x72c93bcc,
379        0x1c41410e, 0x4b72d5f9, 0x103ccc4e, 0x3896bef2, 0x2c5e0b1c, 0x1e2096de, 0x15594d47,
380        0x04e035ce, 0x2785d1b1, 0x795bc87d,
381    ],
382    [
383        0x373fecbf, 0x0b18c3a0, 0x6516874a, 0x2b567be9, 0x5a2a3d1b, 0x74d99c04, 0x437de605,
384        0x047df991, 0x322faad4, 0x2ef2f76f, 0x5f9e7278, 0x62740235, 0x18c1e8c2, 0x0691e203,
385        0x3324646d, 0x59542c9f, 0x32433d0d, 0x42c17492, 0x45ac808a, 0x685394e0, 0x316f7193,
386        0x5ea108a0, 0x6bb3f12f, 0x232f8865,
387    ],
388    [
389        0x7c162b62, 0x52aa9e45, 0x1b69f8db, 0x3ec35206, 0x1ef086dd, 0x34d7a5e3, 0x33aeea57,
390        0x03565cc8, 0x5bc5fd47, 0x47adc343, 0x1d5857a2, 0x5e7ece76, 0x0239fba3, 0x58bdead4,
391        0x41671aef, 0x3c8a9189, 0x7342ed52, 0x19871456, 0x573a02c8, 0x2ec8ad55, 0x09c4a997,
392        0x34b9b63a, 0x226da984, 0x6b31d16e,
393    ],
394    [
395        0x458384d2, 0x353911e1, 0x4cfd1256, 0x163c23af, 0x7609c5e0, 0x76596c08, 0x087adac7,
396        0x4fd4b62c, 0x3692a037, 0x51c54b62, 0x133daf4d, 0x0c76f623, 0x387d21f3, 0x6034abe5,
397        0x7c982e2b, 0x63a266b4, 0x4f2b17b8, 0x0bd62f1d, 0x70e37a7c, 0x4f162da9, 0x38f0e527,
398        0x6ce798d7, 0x6c74250b, 0x606f2fad,
399    ],
400    [
401        0x212b041d, 0x6724fd32, 0x73aaf9af, 0x3ae9b76b, 0x014fe151, 0x37687943, 0x36bb7786,
402        0x01da85ef, 0x28c618ae, 0x36706580, 0x3f5f610d, 0x2e0b9391, 0x5750e38d, 0x00b48d71,
403        0x0f1f1d7a, 0x7107c415, 0x35c1e287, 0x26ccce2f, 0x4e29277a, 0x1580ee9d, 0x18136f74,
404        0x530f32ad, 0x5a19b05d, 0x3d38b320,
405    ],
406    [
407        0x6a3bf1e4, 0x39e9edbb, 0x2ce6a59e, 0x2df215e1, 0x216a17ba, 0x3a8f3cfa, 0x0a14d990,
408        0x1162e529, 0x1213c181, 0x3daa68f5, 0x16c570ff, 0x1063321c, 0x06a2d0e8, 0x17c094a4,
409        0x39a5d9c9, 0x086d4802, 0x67ab7fe3, 0x67f51392, 0x3649c2ac, 0x62aa8cf8, 0x55b6fdbb,
410        0x55c3e972, 0x2f865724, 0x314fa653,
411    ],
412    [
413        0x029f66f1, 0x016f80a2, 0x4b70e0c2, 0x1782f9ab, 0x697578ee, 0x07b2c8b7, 0x123f6681,
414        0x2b78db24, 0x2cd8db9d, 0x302947b1, 0x04f4c99a, 0x1f8bcbbd, 0x61c782ea, 0x3459928c,
415        0x3efec720, 0x24f2b8f6, 0x5dec66b5, 0x622386cc, 0x26b70002, 0x1fa0d640, 0x6edeaa0a,
416        0x670ff3e1, 0x18641d8e, 0x43b68197,
417    ],
418    [
419        0x315b1707, 0x46db526a, 0x02fa5277, 0x36f6edf9, 0x31ad912b, 0x7d518ebd, 0x61db2eea,
420        0x0ba28bad, 0x3c839e59, 0x7ed007f1, 0x74447f8a, 0x6b4ce5b7, 0x7272e3a4, 0x192257d1,
421        0x5f882281, 0x5f890768, 0x47eec4cb, 0x2ef3e6c8, 0x43d6e4e2, 0x668ce6ba, 0x50679e00,
422        0x24c067a8, 0x605be47c, 0x324ac2ec,
423    ],
424    // Terminal full rounds (4)
425    [
426        0x5883788f, 0x7eba66af, 0x23620f78, 0x44492c9a, 0x7cc098a4, 0x705191fa, 0x2f7185e2,
427        0x6ebbb07e, 0x23508c3b, 0x6cb0f0f4, 0x1190a8c0, 0x60f8f1d0, 0x316c16a1, 0x440742c7,
428        0x7643f142, 0x642f9668, 0x214b7566, 0x52a5c469, 0x1bfd90da, 0x1d7d8076, 0x6e06d1e8,
429        0x7d672e6d, 0x6fd2e3e3, 0x3257ae18,
430    ],
431    [
432        0x75861a51, 0x0e2996fe, 0x2bdc228b, 0x6879fcb8, 0x14ca9b1c, 0x29953d92, 0x36ee671d,
433        0x31366e47, 0x79c4f5f2, 0x2b8c8639, 0x073a293d, 0x32802c31, 0x4894d32f, 0x06acc989,
434        0x40d852b1, 0x508857c4, 0x2ffe504d, 0x18be00c1, 0x75a114e9, 0x4ed5922a, 0x1060ee72,
435        0x2176563c, 0x0b91b242, 0x6bfbf1a4,
436    ],
437    [
438        0x06f94470, 0x694f4383, 0x53cada3e, 0x1527bfd8, 0x2bdfe868, 0x120c2d2c, 0x7dfd6309,
439        0x10b619c2, 0x0550bc7f, 0x488cf3dc, 0x4c5454a2, 0x00be2976, 0x349c9669, 0x2b4eb07d,
440        0x0450bf40, 0x58de7343, 0x3495a265, 0x2305e3b7, 0x661dd781, 0x1c183983, 0x46992791,
441        0x3eb3751f, 0x38f728c8, 0x775d0a30,
442    ],
443    [
444        0x7636645a, 0x7125aa5d, 0x0c3f2dca, 0x13b595cc, 0x5a5e9bce, 0x54bb3456, 0x069a1a5a,
445        0x7b9f15ee, 0x50150189, 0x68c9157b, 0x07e06e22, 0x568aecdb, 0x1403f847, 0x436cf5da,
446        0x3f09c026, 0x652f7b1b, 0x3e8607f3, 0x5bb37c57, 0x1b1a9ecf, 0x39d11cb0, 0x1841a51c,
447        0x1251ad48, 0x74fb5edd, 0x21fa33c6,
448    ],
449]);
450
451/// Create a default width-16 Poseidon1 permutation for KoalaBear.
452pub fn default_koalabear_poseidon1_16() -> Poseidon1KoalaBear<16> {
453    Poseidon1::new(&Poseidon1Constants {
454        rounds_f: 2 * KOALABEAR_POSEIDON_HALF_FULL_ROUNDS,
455        rounds_p: KOALABEAR_POSEIDON_PARTIAL_ROUNDS_16,
456        mds_circ_col: MDSKoalaBearData::MATRIX_CIRC_MDS_16_COL,
457        round_constants: KOALABEAR_POSEIDON1_RC_16.to_vec(),
458    })
459}
460
461/// Create a default width-24 Poseidon1 permutation for KoalaBear.
462pub fn default_koalabear_poseidon1_24() -> Poseidon1KoalaBear<24> {
463    Poseidon1::new(&Poseidon1Constants {
464        rounds_f: 2 * KOALABEAR_POSEIDON_HALF_FULL_ROUNDS,
465        rounds_p: KOALABEAR_POSEIDON_PARTIAL_ROUNDS_24,
466        mds_circ_col: MDSKoalaBearData::MATRIX_CIRC_MDS_24_COL,
467        round_constants: KOALABEAR_POSEIDON1_RC_24.to_vec(),
468    })
469}
470
471#[cfg(test)]
472mod tests {
473    use p3_symmetric::Permutation;
474
475    use super::*;
476
477    type F = KoalaBear;
478
479    #[test]
480    fn test_poseidon_width_16() {
481        let perm = default_koalabear_poseidon1_16();
482
483        let mut input: [F; 16] =
484            F::new_array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);
485
486        let expected: [F; 16] = F::new_array([
487            610090613, 935319874, 1893335292, 796792199, 356405232, 552237741, 55134556,
488            1215104204, 1823723405, 1133298033, 1780633798, 1453946561, 710069176, 1128629550,
489            1917333254, 1175481618,
490        ]);
491
492        perm.permute_mut(&mut input);
493        assert_eq!(input, expected);
494    }
495
496    #[test]
497    fn test_poseidon_width_24() {
498        let perm = default_koalabear_poseidon1_24();
499
500        let mut input: [F; 24] = F::new_array([
501            0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
502        ]);
503
504        let expected: [F; 24] = F::new_array([
505            511672087, 215882318, 237782537, 740528428, 712760904, 54615367, 751514671, 110231969,
506            1905276435, 992525666, 918312360, 18628693, 749929200, 1916418953, 691276896,
507            1112901727, 1163558623, 882867603, 673396520, 1480278156, 1402044758, 1693467175,
508            1766273044, 433841551,
509        ]);
510
511        perm.permute_mut(&mut input);
512        assert_eq!(input, expected);
513    }
514}