p3-goldilocks 0.5.3

An implementation of the Goldilocks prime field F_p, where p = 2^64 - 2^32 + 1.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
//! Fused Poseidon1 permutation for Goldilocks on aarch64.

use alloc::vec::Vec;

use p3_poseidon1::{
    FullRoundConstants, PartialRoundConstants, full_round_initial_permute_state,
    full_round_terminal_permute_state, partial_permute_state,
};
use p3_symmetric::{CryptographicPermutation, Permutation};

use super::mds::{MdsNeonGoldilocks, mds_neon_w8, mds_neon_w12};
use super::packing::PackedGoldilocksNeon;
use super::poseidon1_asm::*;
use super::poseidon2_asm::{sbox_layer_asm, sbox_layer_dual_asm};
use super::utils::{pack_lanes, unpack_lanes};
use crate::Goldilocks;

/// Fused Poseidon1 permutation for Goldilocks.
///
/// Holds the pre-extracted raw `u64` constants from the optimized Poseidon1
/// sparse-matrix decomposition. Storing raw values avoids field-element
/// overhead in the hot inner loop.
#[derive(Clone, Debug)]
pub struct Poseidon1GoldilocksFused<const WIDTH: usize> {
    /// Round constants for the initial full rounds (RF/2 vectors).
    initial_constants_raw: Vec<[u64; WIDTH]>,
    /// Round constants for the terminal full rounds (RF/2 vectors).
    terminal_constants_raw: Vec<[u64; WIDTH]>,
    /// Full-width constant vector for the first partial round.
    first_round_constants_raw: [u64; WIDTH],
    /// Dense transition matrix applied once before entering the partial-round loop.
    m_i_raw: [[u64; WIDTH]; WIDTH],
    /// Per-round first row of the sparse matrix (one per partial round).
    sparse_first_row_raw: Vec<[u64; WIDTH]>,
    /// Per-round sub-diagonal vector for the sparse matmul (one per partial round).
    v_raw: Vec<[u64; WIDTH]>,
    /// Scalar round constants for partial rounds 0 through RP-2.
    ///
    /// The last partial round has no scalar constant (it ends with the S-box only).
    round_constants_raw: Vec<u64>,
}

impl<const WIDTH: usize> Poseidon1GoldilocksFused<WIDTH> {
    /// Create from pre-computed full and partial round constants.
    ///
    /// Extracts the raw `u64` representation from each Goldilocks field
    /// element, building the flat arrays that the ASM kernels consume.
    pub fn new(
        full: &FullRoundConstants<Goldilocks, WIDTH>,
        partial: &PartialRoundConstants<Goldilocks, WIDTH>,
    ) -> Self {
        // Extract raw u64 values from full-round constant matrices.
        let initial_constants_raw = full
            .initial
            .iter()
            .map(|rc| core::array::from_fn(|i| rc[i].value))
            .collect();
        let terminal_constants_raw = full
            .terminal
            .iter()
            .map(|rc| core::array::from_fn(|i| rc[i].value))
            .collect();

        // Extract the first partial-round constant vector.
        let first_round_constants_raw =
            core::array::from_fn(|i| partial.first_round_constants[i].value);

        // Extract the dense transition matrix.
        let m_i_raw = core::array::from_fn(|i| core::array::from_fn(|j| partial.m_i[i][j].value));

        // Extract per-round sparse matrix data.
        let sparse_first_row_raw = partial
            .sparse_first_row
            .iter()
            .map(|r| core::array::from_fn(|i| r[i].value))
            .collect();
        let v_raw = partial
            .v
            .iter()
            .map(|r| core::array::from_fn(|i| r[i].value))
            .collect();

        // Extract scalar round constants for partial rounds.
        let round_constants_raw = partial.round_constants.iter().map(|c| c.value).collect();

        Self {
            initial_constants_raw,
            terminal_constants_raw,
            first_round_constants_raw,
            m_i_raw,
            sparse_first_row_raw,
            v_raw,
            round_constants_raw,
        }
    }
}

/// Run the initial or terminal full rounds on a raw width-8 state.
///
/// Each full round applies: add constants, S-box on all elements, NEON MDS.
#[inline]
fn full_rounds_scalar_w8(raw: &mut [u64; 8], constants: &[[u64; 8]]) {
    for rc in constants {
        unsafe {
            add_rc_asm(raw, rc);
            sbox_layer_asm(raw);
        }
        *raw = unsafe { mds_neon_w8(raw) };
    }
}

/// Run the initial or terminal full rounds on a raw width-12 state.
///
/// Each full round applies: add constants, S-box on all elements, NEON MDS.
#[inline]
fn full_rounds_scalar_w12(raw: &mut [u64; 12], constants: &[[u64; 12]]) {
    for rc in constants {
        unsafe {
            add_rc_asm(raw, rc);
            sbox_layer_asm(raw);
        }
        *raw = unsafe { mds_neon_w12(raw) };
    }
}

/// Run all partial rounds on a raw width-8 state.
///
/// The partial-round sequence is:
/// 1. Add the first-round full-width constant vector.
/// 2. Apply the dense transition matrix once.
/// 3. For each partial round (except the last):
///    S-box on first element, add scalar constant, sparse matmul.
/// 4. Last partial round: S-box on first element, sparse matmul (no constant).
#[inline]
fn partial_rounds_scalar_w8(
    raw: &mut [u64; 8],
    first_rc: &[u64; 8],
    m_i: &[[u64; 8]; 8],
    sparse_first_row: &[[u64; 8]],
    v: &[[u64; 8]],
    round_constants: &[u64],
) {
    // Add the first-round full-width constant vector.
    unsafe {
        add_rc_asm(raw, first_rc);
    }

    // Apply the dense transition matrix once.
    dense_matmul_asm_w8(raw, m_i);

    // Main partial-round loop: S-box + scalar constant + sparse matmul.
    let rounds_p = sparse_first_row.len();
    for r in 0..rounds_p - 1 {
        unsafe {
            sbox_s0_asm(raw);
            add_scalar_s0_asm(raw, round_constants[r]);
            cheap_matmul_asm_w8(raw, &sparse_first_row[r], &v[r]);
        }
    }

    // Last partial round: no scalar constant.
    unsafe {
        sbox_s0_asm(raw);
        cheap_matmul_asm_w8(raw, &sparse_first_row[rounds_p - 1], &v[rounds_p - 1]);
    }
}

/// Run all partial rounds on a raw width-12 state.
///
/// Same structure as the width-8 variant.
#[inline]
fn partial_rounds_scalar_w12(
    raw: &mut [u64; 12],
    first_rc: &[u64; 12],
    m_i: &[[u64; 12]; 12],
    sparse_first_row: &[[u64; 12]],
    v: &[[u64; 12]],
    round_constants: &[u64],
) {
    unsafe {
        add_rc_asm(raw, first_rc);
    }
    dense_matmul_asm_w12(raw, m_i);

    let rounds_p = sparse_first_row.len();
    for r in 0..rounds_p - 1 {
        unsafe {
            sbox_s0_asm(raw);
            add_scalar_s0_asm(raw, round_constants[r]);
            cheap_matmul_asm_w12(raw, &sparse_first_row[r], &v[r]);
        }
    }
    unsafe {
        sbox_s0_asm(raw);
        cheap_matmul_asm_w12(raw, &sparse_first_row[rounds_p - 1], &v[rounds_p - 1]);
    }
}

/// Run the initial or terminal full rounds on two raw width-8 lanes.
///
/// Uses dual-lane ASM primitives for add_rc and S-box, then NEON MDS per lane.
#[inline]
fn full_rounds_dual_w8(lane0: &mut [u64; 8], lane1: &mut [u64; 8], constants: &[[u64; 8]]) {
    for rc in constants {
        unsafe {
            add_rc_dual_asm(lane0, lane1, rc);
            sbox_layer_dual_asm(lane0, lane1);
        }
        *lane0 = unsafe { mds_neon_w8(lane0) };
        *lane1 = unsafe { mds_neon_w8(lane1) };
    }
}

/// Run the initial or terminal full rounds on two raw width-12 lanes.
///
/// Uses dual-lane ASM primitives for add_rc and S-box, then NEON MDS per lane.
#[inline]
fn full_rounds_dual_w12(lane0: &mut [u64; 12], lane1: &mut [u64; 12], constants: &[[u64; 12]]) {
    for rc in constants {
        unsafe {
            add_rc_dual_asm(lane0, lane1, rc);
            sbox_layer_dual_asm(lane0, lane1);
        }
        *lane0 = unsafe { mds_neon_w12(lane0) };
        *lane1 = unsafe { mds_neon_w12(lane1) };
    }
}

/// Run all partial rounds on two width-8 lanes simultaneously.
///
/// Uses dual-lane S-box and sparse matmul primitives to keep the
/// pipeline full. The scalar constant is added to each lane separately
/// (no dual variant needed for a single-element addition).
#[inline]
fn partial_rounds_dual_w8(
    lane0: &mut [u64; 8],
    lane1: &mut [u64; 8],
    first_rc: &[u64; 8],
    m_i: &[[u64; 8]; 8],
    sparse_first_row: &[[u64; 8]],
    v: &[[u64; 8]],
    round_constants: &[u64],
) {
    // Add the first-round constant to both lanes.
    unsafe {
        add_rc_dual_asm(lane0, lane1, first_rc);
    }

    // Dense transition matrix on both lanes.
    dense_matmul_dual_asm_w8(lane0, lane1, m_i);

    // Main partial-round loop.
    let rounds_p = sparse_first_row.len();
    for r in 0..rounds_p - 1 {
        unsafe {
            sbox_s0_dual_asm(lane0, lane1);
            add_scalar_s0_asm(lane0, round_constants[r]);
            add_scalar_s0_asm(lane1, round_constants[r]);
            cheap_matmul_dual_asm_w8(lane0, lane1, &sparse_first_row[r], &v[r]);
        }
    }

    // Last partial round: no scalar constant.
    unsafe {
        sbox_s0_dual_asm(lane0, lane1);
        cheap_matmul_dual_asm_w8(
            lane0,
            lane1,
            &sparse_first_row[rounds_p - 1],
            &v[rounds_p - 1],
        );
    }
}

/// Run all partial rounds on two width-12 lanes simultaneously.
///
/// Same structure as the width-8 dual variant.
#[inline]
fn partial_rounds_dual_w12(
    lane0: &mut [u64; 12],
    lane1: &mut [u64; 12],
    first_rc: &[u64; 12],
    m_i: &[[u64; 12]; 12],
    sparse_first_row: &[[u64; 12]],
    v: &[[u64; 12]],
    round_constants: &[u64],
) {
    unsafe {
        add_rc_dual_asm(lane0, lane1, first_rc);
    }
    dense_matmul_dual_asm_w12(lane0, lane1, m_i);

    let rounds_p = sparse_first_row.len();
    for r in 0..rounds_p - 1 {
        unsafe {
            sbox_s0_dual_asm(lane0, lane1);
            add_scalar_s0_asm(lane0, round_constants[r]);
            add_scalar_s0_asm(lane1, round_constants[r]);
            cheap_matmul_dual_asm_w12(lane0, lane1, &sparse_first_row[r], &v[r]);
        }
    }
    unsafe {
        sbox_s0_dual_asm(lane0, lane1);
        cheap_matmul_dual_asm_w12(
            lane0,
            lane1,
            &sparse_first_row[rounds_p - 1],
            &v[rounds_p - 1],
        );
    }
}

impl Permutation<[Goldilocks; 8]> for Poseidon1GoldilocksFused<8> {
    fn permute_mut(&self, state: &mut [Goldilocks; 8]) {
        // Zero-cost transmute: Goldilocks is repr(transparent) over u64.
        let raw = unsafe { &mut *(state as *mut [Goldilocks; 8] as *mut [u64; 8]) };

        // Initial full rounds, then partial rounds, then terminal full rounds.
        full_rounds_scalar_w8(raw, &self.initial_constants_raw);
        partial_rounds_scalar_w8(
            raw,
            &self.first_round_constants_raw,
            &self.m_i_raw,
            &self.sparse_first_row_raw,
            &self.v_raw,
            &self.round_constants_raw,
        );
        full_rounds_scalar_w8(raw, &self.terminal_constants_raw);
    }
}

impl CryptographicPermutation<[Goldilocks; 8]> for Poseidon1GoldilocksFused<8> {}

impl Permutation<[PackedGoldilocksNeon; 8]> for Poseidon1GoldilocksFused<8> {
    fn permute_mut(&self, state: &mut [PackedGoldilocksNeon; 8]) {
        // Unpack the two lanes from the packed representation.
        let (mut lane0, mut lane1) = unpack_lanes(state);

        // Run the full permutation on both lanes simultaneously.
        full_rounds_dual_w8(&mut lane0, &mut lane1, &self.initial_constants_raw);
        partial_rounds_dual_w8(
            &mut lane0,
            &mut lane1,
            &self.first_round_constants_raw,
            &self.m_i_raw,
            &self.sparse_first_row_raw,
            &self.v_raw,
            &self.round_constants_raw,
        );
        full_rounds_dual_w8(&mut lane0, &mut lane1, &self.terminal_constants_raw);

        // Repack both lanes into the packed representation.
        pack_lanes(state, &lane0, &lane1);
    }
}

impl CryptographicPermutation<[PackedGoldilocksNeon; 8]> for Poseidon1GoldilocksFused<8> {}

impl Permutation<[Goldilocks; 12]> for Poseidon1GoldilocksFused<12> {
    fn permute_mut(&self, state: &mut [Goldilocks; 12]) {
        let raw = unsafe { &mut *(state as *mut [Goldilocks; 12] as *mut [u64; 12]) };

        full_rounds_scalar_w12(raw, &self.initial_constants_raw);
        partial_rounds_scalar_w12(
            raw,
            &self.first_round_constants_raw,
            &self.m_i_raw,
            &self.sparse_first_row_raw,
            &self.v_raw,
            &self.round_constants_raw,
        );
        full_rounds_scalar_w12(raw, &self.terminal_constants_raw);
    }
}

impl CryptographicPermutation<[Goldilocks; 12]> for Poseidon1GoldilocksFused<12> {}

impl Permutation<[PackedGoldilocksNeon; 12]> for Poseidon1GoldilocksFused<12> {
    fn permute_mut(&self, state: &mut [PackedGoldilocksNeon; 12]) {
        let (mut lane0, mut lane1) = unpack_lanes(state);

        full_rounds_dual_w12(&mut lane0, &mut lane1, &self.initial_constants_raw);
        partial_rounds_dual_w12(
            &mut lane0,
            &mut lane1,
            &self.first_round_constants_raw,
            &self.m_i_raw,
            &self.sparse_first_row_raw,
            &self.v_raw,
            &self.round_constants_raw,
        );
        full_rounds_dual_w12(&mut lane0, &mut lane1, &self.terminal_constants_raw);

        pack_lanes(state, &lane0, &lane1);
    }
}

impl CryptographicPermutation<[PackedGoldilocksNeon; 12]> for Poseidon1GoldilocksFused<12> {}

/// Dual-dispatch wrapper for Goldilocks Poseidon1.
///
/// **Scalar** permutations use the NEON-accelerated MDS for full rounds
/// and LLVM-optimized sparse matrix decomposition for partial rounds.
/// This avoids sequential inline ASM that would prevent LLVM's
/// instruction scheduling optimizations on wide out-of-order cores.
///
/// **Packed** permutations delegate to the fused dual-lane ASM path
/// with NEON MDS for full rounds and sparse matrix for partial rounds
/// (dual-lane interleaving hides multiply latency).
#[derive(Clone, Debug)]
pub struct Poseidon1GoldilocksDispatch<const WIDTH: usize> {
    /// Fused dual-lane path — used for packed permutations.
    fused: Poseidon1GoldilocksFused<WIDTH>,
    /// Pre-computed full round constants for NEON MDS.
    full_constants: FullRoundConstants<Goldilocks, WIDTH>,
    /// Pre-computed partial round constants (textbook path for scalar, sparse for packed).
    partial_constants: PartialRoundConstants<Goldilocks, WIDTH>,
}

impl<const WIDTH: usize> Poseidon1GoldilocksDispatch<WIDTH> {
    /// Create from fused and pre-computed constants.
    pub const fn new(
        fused: Poseidon1GoldilocksFused<WIDTH>,
        full_constants: FullRoundConstants<Goldilocks, WIDTH>,
        partial_constants: PartialRoundConstants<Goldilocks, WIDTH>,
    ) -> Self {
        Self {
            fused,
            full_constants,
            partial_constants,
        }
    }
}

// --- Width 8 ---

impl Permutation<[Goldilocks; 8]> for Poseidon1GoldilocksDispatch<8> {
    fn permute_mut(&self, state: &mut [Goldilocks; 8]) {
        let mds = MdsNeonGoldilocks;
        full_round_initial_permute_state::<_, _, _, 8, 7>(state, &self.full_constants, &mds);
        partial_permute_state::<_, _, 8, 7>(state, &self.partial_constants);
        full_round_terminal_permute_state::<_, _, _, 8, 7>(state, &self.full_constants, &mds);
    }
}

impl CryptographicPermutation<[Goldilocks; 8]> for Poseidon1GoldilocksDispatch<8> {}

impl Permutation<[PackedGoldilocksNeon; 8]> for Poseidon1GoldilocksDispatch<8> {
    fn permute_mut(&self, state: &mut [PackedGoldilocksNeon; 8]) {
        self.fused.permute_mut(state);
    }
}

impl CryptographicPermutation<[PackedGoldilocksNeon; 8]> for Poseidon1GoldilocksDispatch<8> {}

// --- Width 12 ---

impl Permutation<[Goldilocks; 12]> for Poseidon1GoldilocksDispatch<12> {
    fn permute_mut(&self, state: &mut [Goldilocks; 12]) {
        let mds = MdsNeonGoldilocks;
        full_round_initial_permute_state::<_, _, _, 12, 7>(state, &self.full_constants, &mds);
        partial_permute_state::<_, _, 12, 7>(state, &self.partial_constants);
        full_round_terminal_permute_state::<_, _, _, 12, 7>(state, &self.full_constants, &mds);
    }
}

impl CryptographicPermutation<[Goldilocks; 12]> for Poseidon1GoldilocksDispatch<12> {}

impl Permutation<[PackedGoldilocksNeon; 12]> for Poseidon1GoldilocksDispatch<12> {
    fn permute_mut(&self, state: &mut [PackedGoldilocksNeon; 12]) {
        // Extract both lanes, run the optimized scalar path on each, repack.
        // Directly inline the scalar logic (NEON MDS full rounds + sparse partial
        // rounds) to avoid trait-dispatch overhead and enable cross-call inlining.
        let mut lane0: [Goldilocks; 12] = core::array::from_fn(|i| state[i].0[0]);
        let mut lane1: [Goldilocks; 12] = core::array::from_fn(|i| state[i].0[1]);

        let mds = MdsNeonGoldilocks;
        full_round_initial_permute_state::<_, _, _, 12, 7>(&mut lane0, &self.full_constants, &mds);
        partial_permute_state::<_, _, 12, 7>(&mut lane0, &self.partial_constants);
        full_round_terminal_permute_state::<_, _, _, 12, 7>(&mut lane0, &self.full_constants, &mds);

        full_round_initial_permute_state::<_, _, _, 12, 7>(&mut lane1, &self.full_constants, &mds);
        partial_permute_state::<_, _, 12, 7>(&mut lane1, &self.partial_constants);
        full_round_terminal_permute_state::<_, _, _, 12, 7>(&mut lane1, &self.full_constants, &mds);

        for i in 0..12 {
            state[i] = PackedGoldilocksNeon([lane0[i], lane1[i]]);
        }
    }
}

impl CryptographicPermutation<[PackedGoldilocksNeon; 12]> for Poseidon1GoldilocksDispatch<12> {}

#[cfg(test)]
mod tests {
    use p3_field::{PrimeCharacteristicRing, PrimeField64};
    use p3_poseidon1::Poseidon1Constants;
    use p3_symmetric::Permutation;
    use rand::rngs::SmallRng;
    use rand::{RngExt, SeedableRng};

    use super::*;
    use crate::mds::{MATRIX_CIRC_MDS_8_COL, MATRIX_CIRC_MDS_12_COL};
    use crate::poseidon1::{
        GOLDILOCKS_POSEIDON_HALF_FULL_ROUNDS, GOLDILOCKS_POSEIDON_PARTIAL_ROUNDS_8,
        GOLDILOCKS_POSEIDON_PARTIAL_ROUNDS_12, GOLDILOCKS_POSEIDON1_RC_8,
        GOLDILOCKS_POSEIDON1_RC_12, default_goldilocks_poseidon1_8,
        default_goldilocks_poseidon1_12,
    };

    type F = Goldilocks;

    /// Build a width-8 fused permutation from the fixed round constants.
    fn make_fused_w8() -> Poseidon1GoldilocksFused<8> {
        let raw = Poseidon1Constants {
            rounds_f: 2 * GOLDILOCKS_POSEIDON_HALF_FULL_ROUNDS,
            rounds_p: GOLDILOCKS_POSEIDON_PARTIAL_ROUNDS_8,
            mds_circ_col: MATRIX_CIRC_MDS_8_COL,
            round_constants: GOLDILOCKS_POSEIDON1_RC_8.to_vec(),
        };
        let (full, partial) = raw.to_optimized();
        Poseidon1GoldilocksFused::new(&full, &partial)
    }

    /// Build a width-12 fused permutation from the fixed round constants.
    fn make_fused_w12() -> Poseidon1GoldilocksFused<12> {
        let raw = Poseidon1Constants {
            rounds_f: 2 * GOLDILOCKS_POSEIDON_HALF_FULL_ROUNDS,
            rounds_p: GOLDILOCKS_POSEIDON_PARTIAL_ROUNDS_12,
            mds_circ_col: MATRIX_CIRC_MDS_12_COL,
            round_constants: GOLDILOCKS_POSEIDON1_RC_12.to_vec(),
        };
        let (full, partial) = raw.to_optimized();
        Poseidon1GoldilocksFused::new(&full, &partial)
    }

    /// Verify that the fused width-8 implementation matches the generic one
    /// on both zero and random inputs.
    #[test]
    fn test_fused_matches_generic_w8() {
        let generic = default_goldilocks_poseidon1_8();
        let fused = make_fused_w8();
        let mut rng = SmallRng::seed_from_u64(42);

        // Zero input.
        let mut g_state = [F::ZERO; 8];
        let mut f_state = [F::ZERO; 8];
        generic.permute_mut(&mut g_state);
        fused.permute_mut(&mut f_state);
        for i in 0..8 {
            assert_eq!(
                f_state[i].as_canonical_u64(),
                g_state[i].as_canonical_u64(),
                "Fused vs generic mismatch at index {i} (zero input, w8)"
            );
        }

        // Random input.
        let mut g_state: [F; 8] = rng.random();
        let mut f_state = g_state;
        generic.permute_mut(&mut g_state);
        fused.permute_mut(&mut f_state);
        for i in 0..8 {
            assert_eq!(
                f_state[i].as_canonical_u64(),
                g_state[i].as_canonical_u64(),
                "Fused vs generic mismatch at index {i} (random input, w8)"
            );
        }
    }

    /// Same fused-vs-generic verification for width 12.
    #[test]
    fn test_fused_matches_generic_w12() {
        let generic = default_goldilocks_poseidon1_12();
        let fused = make_fused_w12();
        let mut rng = SmallRng::seed_from_u64(42);

        let mut g_state = [F::ZERO; 12];
        let mut f_state = [F::ZERO; 12];
        generic.permute_mut(&mut g_state);
        fused.permute_mut(&mut f_state);
        for i in 0..12 {
            assert_eq!(
                f_state[i].as_canonical_u64(),
                g_state[i].as_canonical_u64(),
                "Fused vs generic mismatch at index {i} (zero input, w12)"
            );
        }

        let mut g_state: [F; 12] = rng.random();
        let mut f_state = g_state;
        generic.permute_mut(&mut g_state);
        fused.permute_mut(&mut f_state);
        for i in 0..12 {
            assert_eq!(
                f_state[i].as_canonical_u64(),
                g_state[i].as_canonical_u64(),
                "Fused vs generic mismatch at index {i} (random input, w12)"
            );
        }
    }

    /// Verify that the packed (dual-lane) width-8 path matches running
    /// two independent scalar permutations.
    #[test]
    fn test_packed_matches_scalar_w8() {
        let fused = make_fused_w8();
        let mut rng = SmallRng::seed_from_u64(123);

        // Two independent random scalar inputs.
        let scalar_a: [F; 8] = rng.random();
        let scalar_b: [F; 8] = rng.random();

        // Pack them into a single packed state and permute.
        let mut packed: [PackedGoldilocksNeon; 8] =
            core::array::from_fn(|i| PackedGoldilocksNeon([scalar_a[i], scalar_b[i]]));
        fused.permute_mut(&mut packed);

        // Compute the expected result by running scalar on each independently.
        let mut expected_a = scalar_a;
        let mut expected_b = scalar_b;
        fused.permute_mut(&mut expected_a);
        fused.permute_mut(&mut expected_b);

        // Lane 0 must match the first scalar, lane 1 must match the second.
        for i in 0..8 {
            assert_eq!(
                packed[i].0[0].as_canonical_u64(),
                expected_a[i].as_canonical_u64(),
                "Packed lane0 mismatch at index {i} (w8)"
            );
            assert_eq!(
                packed[i].0[1].as_canonical_u64(),
                expected_b[i].as_canonical_u64(),
                "Packed lane1 mismatch at index {i} (w8)"
            );
        }
    }

    /// Same packed-vs-scalar verification for width 12.
    #[test]
    fn test_packed_matches_scalar_w12() {
        let fused = make_fused_w12();
        let mut rng = SmallRng::seed_from_u64(123);

        let scalar_a: [F; 12] = rng.random();
        let scalar_b: [F; 12] = rng.random();

        let mut packed: [PackedGoldilocksNeon; 12] =
            core::array::from_fn(|i| PackedGoldilocksNeon([scalar_a[i], scalar_b[i]]));
        fused.permute_mut(&mut packed);

        let mut expected_a = scalar_a;
        let mut expected_b = scalar_b;
        fused.permute_mut(&mut expected_a);
        fused.permute_mut(&mut expected_b);

        for i in 0..12 {
            assert_eq!(
                packed[i].0[0].as_canonical_u64(),
                expected_a[i].as_canonical_u64(),
                "Packed lane0 mismatch at index {i} (w12)"
            );
            assert_eq!(
                packed[i].0[1].as_canonical_u64(),
                expected_b[i].as_canonical_u64(),
                "Packed lane1 mismatch at index {i} (w12)"
            );
        }
    }

    /// Known-answer test for width 8 (sequential 0..7 input).
    #[test]
    fn test_fused_kat_w8() {
        let fused = make_fused_w8();
        let mut input: [F; 8] = F::new_array([0, 1, 2, 3, 4, 5, 6, 7]);
        fused.permute_mut(&mut input);

        let expected: [F; 8] = F::new_array([
            2431226948502761687,
            9427563026145807618,
            6827549936272051660,
            16907684411084503785,
            10131745626715172913,
            17448305483431576765,
            9066501914269485014,
            12095238468458521303,
        ]);
        assert_eq!(input, expected);
    }

    /// Known-answer test for width 12 (sequential 0..11 input).
    #[test]
    fn test_fused_kat_w12() {
        let fused = make_fused_w12();
        let mut input: [F; 12] = F::new_array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
        fused.permute_mut(&mut input);

        let expected: [F; 12] = F::new_array([
            15595088881848875364,
            9564850329150784619,
            13607005230761744521,
            12117102595842533385,
            2814257411756993122,
            11640647689983397089,
            14363867760831937423,
            13323891071259596526,
            11219803511311150468,
            9221595262780869902,
            5898229059046891887,
            18181291031484020550,
        ]);
        assert_eq!(input, expected);
    }
}