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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
//! Poseidon1 permutation for Goldilocks.
//!
//! # Overview
//!
//! This module provides the Poseidon1 hash permutation instantiated for the
//! Goldilocks field (p = 2^64 - 2^32 + 1). The public API is a single type
//! alias that transparently dispatches to the best available implementation.
//!
//! # Platform Dispatch
//!
//! On **aarch64**, the type alias resolves to a dual-dispatch wrapper:
//! scalar permutations use NEON-accelerated MDS for full rounds with
//! LLVM-optimized sparse partial rounds, while packed NEON permutations
//! use the fused dual-lane ASM path (w8) or per-lane scalar path (w12).
//!
//! On **all other platforms**, it resolves to the generic Poseidon1
//! implementation with Karatsuba MDS convolution.
//!
//! No `#[cfg]` is needed in calling code.
//!
//! # MDS Matrix
//!
//! The MDS matrix is a **circulant** matrix sourced from the MDS crate.
//! At runtime, it is applied via fast Karatsuba convolution (sub-O(t^2)).
//! During initialization only, it is expanded to dense form for the
//! sparse matrix decomposition of partial rounds.
//!
//! # Round Constants
//!
//! Generated by the Grain LFSR (Poseidon1 paper, Appendix E) with SBOX=0 (x^alpha encoding).

use p3_poseidon1::{
    Poseidon1, Poseidon1Constants, Poseidon1ExternalLayerGeneric, Poseidon1InternalLayerGeneric,
};

use crate::mds::{MATRIX_CIRC_MDS_8_COL, MATRIX_CIRC_MDS_12_COL};
use crate::{Goldilocks, MdsMatrixGoldilocks};

/// S-box degree for Goldilocks Poseidon1.
///
/// The S-box raises each element to this power. The Goldilocks prime
/// factors as `p - 1 = 2^32 * 3 * 5 * 17 * 257 * 65537`. Neither 3 nor 5
/// are coprime to `p - 1`, so the smallest valid exponent is 7.
pub const GOLDILOCKS_S_BOX_DEGREE: u64 = 7;

/// Number of full rounds per half for Goldilocks Poseidon (`RF / 2`).
///
/// The total number of full rounds is `RF = 8` (4 beginning + 4 ending).
/// Follows the Poseidon paper's security analysis (Section 5.4) with a +2 RF margin.
pub const GOLDILOCKS_POSEIDON_HALF_FULL_ROUNDS: usize = 4;

/// Number of partial rounds for Goldilocks Poseidon (width 8).
///
/// Derived from the interpolation bound in the Poseidon paper (Eq. 3):
///
///   R_interp ≥ ⌈min{κ,n}/log_2(α)⌉ + ⌈log_α(t)⌉ − 5
///            = ⌈64/log_2(7)⌉ + ⌈log_7(8)⌉ − 5 = 23 + 2 − 5 = 20
///
/// With the +7.5% security margin (Section 5.4): ⌈1.075 × 20⌉ = 22.
pub const GOLDILOCKS_POSEIDON_PARTIAL_ROUNDS_8: usize = 22;

/// Number of partial rounds for Goldilocks Poseidon (width 12).
///
/// Same interpolation bound as width 8:
///
///   R_interp ≥ ⌈64/log_2(7)⌉ + ⌈log_7(12)⌉ − 5 = 23 + 2 − 5 = 20
///
/// With the +7.5% security margin: ⌈1.075 × 20⌉ = 22.
pub const GOLDILOCKS_POSEIDON_PARTIAL_ROUNDS_12: usize = 22;

/// Generic (non-fused) Poseidon1 permutation for Goldilocks.
///
/// Uses the platform-independent Poseidon1 implementation with Karatsuba
/// MDS convolution. Used directly for widths not supported by the fused
/// type (e.g. 16, 24) and as the non-aarch64 fallback for widths 8 and 12.
pub type Poseidon1GoldilocksGeneric<const WIDTH: usize> = Poseidon1<
    Goldilocks,
    Poseidon1ExternalLayerGeneric<Goldilocks, MdsMatrixGoldilocks, WIDTH>,
    Poseidon1InternalLayerGeneric<Goldilocks, WIDTH>,
    WIDTH,
    GOLDILOCKS_S_BOX_DEGREE,
>;

/// Unified Poseidon1 permutation for Goldilocks.
///
/// On aarch64, resolves to a dual-dispatch wrapper: scalar permutations
/// use NEON MDS for full rounds with sparse partial rounds, packed NEON
/// permutations use fused dual-lane ASM (w8) or per-lane scalar (w12).
///
/// On all other platforms, resolves to the generic implementation with
/// Karatsuba MDS convolution.
///
/// Supports both scalar and packed state representations transparently.
#[cfg(target_arch = "aarch64")]
pub type Poseidon1Goldilocks<const WIDTH: usize> = crate::Poseidon1GoldilocksDispatch<WIDTH>;

/// Unified Poseidon1 permutation for Goldilocks.
///
/// On aarch64, resolves to the fused ASM-optimized implementation that
/// uses inline assembly and dual-lane NEON processing.
///
/// On all other platforms, resolves to the generic implementation with
/// Karatsuba MDS convolution.
///
/// Supports both scalar and packed state representations transparently.
#[cfg(not(target_arch = "aarch64"))]
pub type Poseidon1Goldilocks<const WIDTH: usize> = Poseidon1GoldilocksGeneric<WIDTH>;

/// Round constants for width-8 Poseidon1 on Goldilocks.
///
/// Generated by the Grain LFSR with parameters:
///     field_type=1, alpha=7 (exp_flag=0), n=64, t=8, R_F=8, R_P=22
///
/// Generated by `poseidon/generate_constants.py --field goldilocks --width 8`.
///
/// Layout: [initial_full (4 rounds), partial (22 rounds), terminal_full (4 rounds)].
pub const GOLDILOCKS_POSEIDON1_RC_8: [[Goldilocks; 8]; 30] = Goldilocks::new_2d_array([
    // Initial full rounds (4)
    [
        0xdd5743e7f2a5a5d9,
        0xcb3a864e58ada44b,
        0xffa2449ed32f8cdc,
        0x42025f65d6bd13ee,
        0x7889175e25506323,
        0x34b98bb03d24b737,
        0xbdcc535ecc4faa2a,
        0x5b20ad869fc0d033,
    ],
    [
        0xf1dda5b9259dfcb4,
        0x27515210be112d59,
        0x4227d1718c766c3f,
        0x26d333161a5bd794,
        0x49b938957bf4b026,
        0x4a56b5938b213669,
        0x1120426b48c8353d,
        0x6b323c3f10a56cad,
    ],
    [
        0xce57d6245ddca6b2,
        0xb1fc8d402bba1eb1,
        0xb5c5096ca959bd04,
        0x6db55cd306d31f7f,
        0xc49d293a81cb9641,
        0x1ce55a4fe979719f,
        0xa92e60a9d178a4d1,
        0x002cc64973bcfd8c,
    ],
    [
        0xcea721cce82fb11b,
        0xe5b55eb8098ece81,
        0x4e30525c6f1ddd66,
        0x43c6702827070987,
        0xaca68430a7b5762a,
        0x3674238634df9c93,
        0x88cee1c825e33433,
        0xde99ae8d74b57176,
    ],
    // Partial rounds (22)
    [
        0x488897d85ff51f56,
        0x1140737ccb162218,
        0xa7eeb9215866ed35,
        0x9bd2976fee49fcc9,
        0xc0c8f0de580a3fcc,
        0x4fb2dae6ee8fc793,
        0x343a89f35f37395b,
        0x223b525a77ca72c8,
    ],
    [
        0x56ccb62574aaa918,
        0xc4d507d8027af9ed,
        0xa080673cf0b7e95c,
        0xf0184884eb70dcf8,
        0x044f10b0cb3d5c69,
        0xe9e3f7993938f186,
        0x1b761c80e772f459,
        0x606cec607a1b5fac,
    ],
    [
        0x14a0c2e1d45f03cd,
        0x4eace8855398574f,
        0xf905ca7103eff3e6,
        0xf8c8f8d20862c059,
        0xb524fe8bdd678e5a,
        0xfbb7865901a1ec41,
        0x014ef1197d341346,
        0x9725e20825d07394,
    ],
    [
        0xfdb25aef2c5bae3b,
        0xbe5402dc598c971e,
        0x93a5711f04cdca3d,
        0xc45a9a5b2f8fb97b,
        0xfe8946a924933545,
        0x2af997a27369091c,
        0xaa62c88e0b294011,
        0x058eb9d810ce9f74,
    ],
    [
        0xb3cb23eced349ae4,
        0xa3648177a77b4a84,
        0x43153d905992d95d,
        0xf4e2a97cda44aa4b,
        0x5baa2702b908682f,
        0x082923bdf4f750d1,
        0x98ae09a325893803,
        0xf8a6475077968838,
    ],
    [
        0xceb0735bf00b2c5f,
        0x0a1a5d953888e072,
        0x2fcb190489f94475,
        0xb5be06270dec69fc,
        0x739cb934b09acf8b,
        0x537750b75ec7f25b,
        0xe9dd318bae1f3961,
        0xf7462137299efe1a,
    ],
    [
        0xb1f6b8eee9adb940,
        0xbdebcc8a809dfe6b,
        0x40fc1f791b178113,
        0x3ac1c3362d014864,
        0x9a016184bdb8aeba,
        0x95f2394459fbc25e,
        0xe3f34a07a76a66c2,
        0x8df25f9ad98b1b96,
    ],
    [
        0x85ffc27171439d9d,
        0xddcb9a2dcfd26910,
        0x26b5ba4bf3afb94e,
        0xffff9cc7c7651e2f,
        0x8c88364698280b55,
        0xebc114167b910501,
        0x2d77b4d89ecfb516,
        0x332e0828eba151f2,
    ],
    [
        0x46fa6a6450dd4735,
        0xd00db7dd92384a33,
        0x5fd4fb751f3a5fc5,
        0x496fb90c0bb65ea2,
        0xf3baec0bb87cc5c7,
        0x862a3c0a7d4c7713,
        0xbf5f38336a3f47d8,
        0x41ad9dbc1394a20c,
    ],
    [
        0xcc535945b7dbf0f7,
        0x82af2bc93685bcec,
        0x8e4c8d0c8cebfccd,
        0x17cb39417e84597e,
        0xd4a965a8c749b232,
        0xa2cab040f33f3ee5,
        0xa98811a1fed4e3a6,
        0x1cc48b54f377e2a1,
    ],
    [
        0xe40cd4f6c5609a27,
        0x11de79ebca97a4a4,
        0x9177c73d8b7e929d,
        0x2a6fe8085797e792,
        0x3de6e93329f8d5ae,
        0x3f7af9125da962ff,
        0xd710682cfc77d3ac,
        0x48faf05f3b053cf4,
    ],
    [
        0x287db8630da89c8b,
        0x4d0de32053cb30e9,
        0x8b37a4f20c5ada7b,
        0xe7cc6ebe78c84ecf,
        0x240bdc0a66a2610d,
        0x8299e7f02caa1650,
        0x380a53fefb6e754e,
        0x684a1d8cf8eb6810,
    ],
    [
        0xe839452eb4b8a5e1,
        0xb03fa62e90626af4,
        0x11a688602fbc5efc,
        0x30dda75c355a2d62,
        0x0f712adcb73810de,
        0xffdc1102187f1ae1,
        0x40c34f398254b99c,
        0xede021b9dc289a4a,
    ],
    [
        0x8b7b05225c4e7dad,
        0x3bc794346f9d9ff9,
        0xfccb5a57f2ca86ff,
        0xbb1502015a7da9d4,
        0xd7e0a35d4352a015,
        0x27af7a44f8160931,
        0xc37442f6782f4615,
        0xbdf392a9bd095dcb,
    ],
    [
        0xc17f55037cf00de9,
        0xbcffedd34c71a874,
        0x5eb45d2a8133d1f2,
        0xbabe251e1612ebdf,
        0x3efeb9fbe438c536,
        0x2d7cef97b4afe1cf,
        0xe5de1b4660016c0b,
        0xcdcc26c332f5657c,
    ],
    [
        0xe01dd653daf15809,
        0xb0a6bdd4b41094b5,
        0x27eac858b0b03a05,
        0x51d43b5e93adbdc0,
        0x8b89a23b0fea5fc9,
        0xdc8ac3b14f7f2fc1,
        0xe793f82f1efec039,
        0x9f6f2cf8969e7b80,
    ],
    [
        0x49d45382e0f21d4a,
        0x5f4ad1797cd72786,
        0x4dc3dbebfd45f795,
        0x03a3ef84dba6e1bc,
        0x204bc9b3d3fc4c01,
        0x9ad706081e89b9ba,
        0x638bfb4d840e9f89,
        0x5ef2938cd095ae35,
    ],
    [
        0x42cca18ebeb265c8,
        0xb7b2ec5c29aecbf8,
        0x0d84f9535dc78f0f,
        0x04e64ad942e77b8c,
        0xb4880dffffc9da0b,
        0x16db16d9c29adeb1,
        0x09bbaf2a0590cd1e,
        0x76460e74961fcf8d,
    ],
    [
        0xed12a2276dfa1553,
        0x0b5acec5de0436fd,
        0x3c6cfea033a1f0a8,
        0x2b5ecefe546cac15,
        0x6e2d82884cd3bf6f,
        0xc134878d1add7b83,
        0x997963422eb7a280,
        0x5e834537ac648cf6,
    ],
    [
        0x89e779214737c0b7,
        0x1a8c05e8581ad95b,
        0x8d18b72796437cf7,
        0xe7252c949e04b106,
        0x53267c4fd174585a,
        0xa16ef5d9c81dad47,
        0xda65191937270a46,
        0xcb2a5b55f2df664c,
    ],
    [
        0x854aee2dc1924137,
        0xf37013c9d479ece6,
        0x0e163bc0630c4696,
        0x384ee64955048f76,
        0xf65d814e28ee4ec5,
        0xe57bc564fd82f1b1,
        0x4b338937b6876614,
        0x66ee0b04ed43cd8d,
    ],
    [
        0x49884bf25f4ef15d,
        0xeb51fe28de1c6f54,
        0x2cd64e84fce8dfcc,
        0x29164a96a541a013,
        0x173ce7558f4cacb8,
        0xeb5b1ce5877c89e9,
        0x5faff4b0f5217bf6,
        0xac42d0b1c20f205e,
    ],
    // Terminal full rounds (4)
    [
        0xfb1d6bf0ca43221b,
        0x97b0a1b01d6a2955,
        0x08c60bd622952b30,
        0x43f2be0f9e24147c,
        0xfa7268b7d3730f5d,
        0x43a6c419a23983bb,
        0xcd77c1f7b29b113c,
        0xcfa43c9db8eec29f,
    ],
    [
        0xcaaa95a6c7365dec,
        0x0a91193f798f3be0,
        0x1104497652735dc6,
        0x35aecb93663b515e,
        0x8dbc9916065aa858,
        0xada8f7a0266579ed,
        0x524dee7bec1ea789,
        0xa93aee9dd5af9521,
    ],
    [
        0x9d1f1b54750d707e,
        0x7c9feab87096d5dc,
        0xa2e1fb19f9d4261b,
        0xb714deb448de6346,
        0x225d1f0d011c5403,
        0x1549b7f1d28cedc0,
        0xaef3e46f97d43942,
        0x6dfc7ffe0b38bf08,
    ],
    [
        0x7de853fdc542b663,
        0xa68ecc96610657b2,
        0xe88bb5428af289b1,
        0xd7cfa1504c5569f5,
        0x78a9aad0d642d30a,
        0xd68315f2353dce52,
        0x46e56300f86fcfd5,
        0x323d95332b145fd6,
    ],
]);

/// Round constants for width-12 Poseidon1 on Goldilocks.
///
/// Generated by the Grain LFSR with parameters:
///     field_type=1, alpha=7 (exp_flag=0), n=64, t=12, R_F=8, R_P=22
///
/// Generated by `poseidon/generate_constants.py --field goldilocks --width 12`.
///
/// Layout: [initial_full (4 rounds), partial (22 rounds), terminal_full (4 rounds)].
pub const GOLDILOCKS_POSEIDON1_RC_12: [[Goldilocks; 12]; 30] = Goldilocks::new_2d_array([
    // Initial full rounds (4)
    [
        0x13dcf33aba214f46,
        0x30b3b654a1da6d83,
        0x1fc634ada6159b56,
        0x937459964dc03466,
        0xedd2ef2ca7949924,
        0xede9affde0e22f68,
        0x8515b9d6bac9282d,
        0x6b5c07b4e9e900d8,
        0x1ec66368838c8a08,
        0x9042367d80d1fbab,
        0x400283564a3c3799,
        0x4a00be0466bca75e,
    ],
    [
        0x7913beee58e3817f,
        0xf545e88532237d90,
        0x22f8cb8736042005,
        0x6f04990e247a2623,
        0xfe22e87ba37c38cd,
        0xd20e32c85ffe2815,
        0x117227674048fe73,
        0x4e9fb7ea98a6b145,
        0xe0866c232b8af08b,
        0x00bbc77916884964,
        0x7031c0fb990d7116,
        0x240a9e87cf35108f,
    ],
    [
        0x2e6363a5a12244b3,
        0x5e1c3787d1b5011c,
        0x4132660e2a196e8b,
        0x3a013b648d3d4327,
        0xf79839f49888ea43,
        0xfe85658ebafe1439,
        0xb6889825a14240bd,
        0x578453605541382b,
        0x4508cda8f6b63ce9,
        0x9c3ef35848684c91,
        0x0812bde23c87178c,
        0xfe49638f7f722c14,
    ],
    [
        0x8e3f688ce885cbf5,
        0xb8e110acf746a87d,
        0xb4b2e8973a6dabef,
        0x9e714c5da3d462ec,
        0x6438f9033d3d0c15,
        0x24312f7cf1a27199,
        0x23f843bb47acbf71,
        0x9183f11a34be9f01,
        0x839062fbb9d45dbf,
        0x24b56e7e6c2e43fa,
        0xe1683da61c962a72,
        0xa95c63971a19bfa7,
    ],
    // Partial rounds (22)
    [
        0x4adf842aa75d4316,
        0xf8fbb871aa4ab4eb,
        0x68e85b6eb2dd6aeb,
        0x07a0b06b2d270380,
        0xd94e0228bd282de4,
        0x8bdd91d3250c5278,
        0x209c68b88bba778f,
        0xb5e18cdab77f3877,
        0xb296a3e808da93fa,
        0x8370ecbda11a327e,
        0x3f9075283775dad8,
        0xb78095bb23c6aa84,
    ],
    [
        0x3f36b9fe72ad4e5f,
        0x69bc96780b10b553,
        0x3f1d341f2eb7b881,
        0x4e939e9815838818,
        0xda366b3ae2a31604,
        0xbc89db1e7287d509,
        0x6102f411f9ef5659,
        0x58725c5e7ac1f0ab,
        0x0df5856c798883e7,
        0xf7bb62a8da4c961b,
        0xc68be7c94882a24d,
        0xaf996d5d5cdaedd9,
    ],
    [
        0x9717f025e7daf6a5,
        0x6436679e6e7216f4,
        0x8a223d99047af267,
        0xbb512e35a133ba9a,
        0xfbbf44097671aa03,
        0xf04058ebf6811e61,
        0x5cca84703fac7ffb,
        0x9b55c7945de6469f,
        0x8e05bf09808e934f,
        0x2ea900de876307d7,
        0x7748fff2b38dfb89,
        0x6b99a676dd3b5d81,
    ],
    [
        0xac4bb7c627cf7c13,
        0xadb6ebe5e9e2f5ba,
        0x2d33378cafa24ae3,
        0x1e5b73807543f8c2,
        0x09208814bfebb10f,
        0x782e64b6bb5b93dd,
        0xadd5a48eac90b50f,
        0xadd4c54c736ea4b1,
        0xd58dbb86ed817fd8,
        0x6d5ed1a533f34ddd,
        0x28686aa3e36b7cb9,
        0x591abd3476689f36,
    ],
    [
        0x047d766678f13875,
        0xa2a11112625f5b49,
        0x21fd10a3f8304958,
        0xf9b40711443b0280,
        0xd2697eb8b2bde88e,
        0x3493790b51731b3f,
        0x11caf9dd73764023,
        0x7acfb8f72878164e,
        0x744ec4db23cefc26,
        0x1e00e58f422c6340,
        0x21dd28d906a62dda,
        0xf32a46ab5f465b5f,
    ],
    [
        0xbfce13201f3f7e6b,
        0xf30d2e7adb5304e2,
        0xecdf4ee4abad48e9,
        0xf94e82182d395019,
        0x4ee52e3744d887c5,
        0xa1341c7cac0083b2,
        0x2302fb26c30c834a,
        0xaea3c587273bf7d3,
        0xf798e24961823ec7,
        0x962deba3e9a2cd94,
        0xb36ee79485ca4707,
        0xd380199eddd2de52,
    ],
    [
        0x70971fc4e6f85305,
        0x8e722f6e5dc32699,
        0xa0883df133052b92,
        0x8f86c6a3eb7d01a4,
        0x763649c8b670bdc5,
        0x830d5c82b808759b,
        0xaa1da8bb91da02e7,
        0x9bc9bf629e211c4d,
        0x0f0a899b10a4dea8,
        0xb883bdcee7c6b356,
        0x78c7101e7496ae1e,
        0x2fd6c5a8bf1e5ca6,
    ],
    [
        0xe2a6e06e61fcec9c,
        0xebfce7d5c5b3dbd5,
        0xca2eeca4bb485d85,
        0xc2b875537c42eb69,
        0x6faf849976873328,
        0xfc3fcb6e81ad4cc3,
        0x180dd95503955a28,
        0xd40f19a3c9fe1520,
        0x49d178ddbf7fd96d,
        0x3950bee2e10e0297,
        0x437b90cf295be062,
        0xa5cd126edffad23b,
    ],
    [
        0xdf58134c134491c2,
        0x0677eca229d9f7bd,
        0x492200a1f7d83a3c,
        0xafb58c9810a43645,
        0x7659077c5a9c208e,
        0x30b4bc83706995cd,
        0xc98fa77bbbef3a3b,
        0x84a82905750b3109,
        0x72f2a02326aeb69b,
        0x8d27a2a2d73a848a,
        0xaa9e30a80bde4b68,
        0x63abb1415e050474,
    ],
    [
        0x1c4bd1e816050a7e,
        0x15d1502e4f469dfd,
        0x53989d594b0c4cd8,
        0x7a1a4c83cb7e377e,
        0x1b52f8a9944e480e,
        0xeb7b03f76a91a79e,
        0x0073a4fc9328c69e,
        0x2c7b16f8620d9de4,
        0x950d052963e46bc4,
        0x8d201ba1a9c89fac,
        0xd3502941bdf35503,
        0x7c6dfcd5af8676fb,
    ],
    [
        0xf8a6cd02e92cdb0b,
        0x6e7500f3a5464b22,
        0x07637eabba4bdd20,
        0x88b82717beee0e14,
        0xbaa2b1cd3dd4c79a,
        0xdfecc3aebec4cfa6,
        0x7561087b0cff0166,
        0x538fcac317a703a6,
        0xd7d6c6eeeeeeea19,
        0xd647b1ee441658a0,
        0xdf4442110236c546,
        0x559ef2c6dd73ec15,
    ],
    [
        0x4c0f5fc6c0dda3d1,
        0x685010cc3100cea7,
        0x2fb6ba8aa0344440,
        0xb515f0a3ca75f1fb,
        0x886887eaecb87c10,
        0xf03ec3fd710abb04,
        0xd3b4763e17f543ef,
        0x50d9e5716e78083a,
        0x0bce2385cf8d74ff,
        0xaf23032cd5f0e04b,
        0xd366aa112b6159d9,
        0x810a3ad3ac7979db,
    ],
    [
        0x0a4a11d794be40a2,
        0xeebf0cf23b668a3f,
        0x600873fb011d761b,
        0x0bfb5591a02ff618,
        0xa16e2a528910af52,
        0xf6553653e2878421,
        0xccbe7c7a601a30c0,
        0xb18b214fe489f5b3,
        0xe21017ab9e153425,
        0x586099ede17af9a6,
        0x385078b514f50647,
        0xc02b3a9afb89883d,
    ],
    [
        0x6d3fbd3b4a9f1de6,
        0x4b4d40a41b0f473c,
        0x838f1887b8f31711,
        0x9396895be5c58a41,
        0x6247a479d66fc2e3,
        0x13fe228a98f2d0a2,
        0x5ba5fde765f9481e,
        0xafb89fa62267e117,
        0xfa4dc1bebcaa6333,
        0xdbab590882b87289,
        0xc3b6c08e23ba9301,
        0xd84b5de94a324fb7,
    ],
    [
        0x0d0c371c5b35b850,
        0x7964f570e7188038,
        0x5daf18bbd996604c,
        0x6743bc47b9595258,
        0x5528b9362c59bb71,
        0xac45e25b7127b68c,
        0xa2077d7dfbb606b6,
        0xf3faac6faee378af,
        0x0c6388b51545e884,
        0xd27dbb6944917b61,
        0x89bcac584344c104,
        0x856bab802ce7402d,
    ],
    [
        0x2cff3000be1fcd0a,
        0x765f2977fa72a917,
        0x1443711329f5f9d5,
        0xd35cd0261af2f951,
        0x2a1bb986084ec281,
        0x2334a54b758f23f2,
        0xa9b8cb612caf706b,
        0xb6ba11c4ab1a1017,
        0xde96b0824b4b46e2,
        0xc59d4272c6d92e2c,
        0x389bb5107611754d,
        0x23647fbc77657372,
    ],
    [
        0xd5ef60d6f76a42fa,
        0xebb406bb79ac9819,
        0x55faccc709a2f423,
        0xd9d6ea97490091cd,
        0xef3ce5069647a7e4,
        0xdf31625d3fa78464,
        0x242e60fd68f10f66,
        0x39c966cc815f084d,
        0x20e2e22e02bae3f7,
        0xb38919d3f1173d7c,
        0xf17769f6c77084d9,
        0xcc051d8094cac41f,
    ],
    [
        0x942069f5d6eece7e,
        0x8d61d3e6f141c572,
        0xc5cef9d85dd605f4,
        0x938f2ac2bf885997,
        0x23bddbace7c48f6c,
        0xc90a6c5ba98537e4,
        0x0be6ee2cca90f6ae,
        0xa026175394ae0e90,
        0x29fca3e314c77628,
        0x2aa2aa8738ab7b77,
        0xe11bbd31fbb8cac6,
        0xb5bbbef1b78a23af,
    ],
    [
        0x8b62a5551e9a9797,
        0x3f91073d4d491c80,
        0x4cfa44976396424a,
        0xf8dcb2dfb3aa1b44,
        0x3849409eba1a95f5,
        0x070845799f234380,
        0x184c0093667da1ba,
        0xbd66aafccd51601e,
        0xee6d14e92155b490,
        0x626f2ec1865bc544,
        0x1bd2854bf6485986,
        0x368b8497472f12ef,
    ],
    [
        0x4f88cdcdfb791921,
        0xe2c0acfeda9ae781,
        0x9739bc21773469b3,
        0x00ce3ad64dc4bb8f,
        0xaab85a321ee7a4c8,
        0xd5de825be97004f4,
        0x48d676d3a043b1c6,
        0x9c6180b1ff643097,
        0x34882a89dd590b09,
        0xae7e6b0d249c3b1d,
        0x8c016908a04885a1,
        0x83ebaaebc9ae0721,
    ],
    [
        0xab21b42e0f642307,
        0xdb46631f62bb29c1,
        0xef29f0399e09b5d9,
        0x5b52fbb3613b8ba1,
        0x57e129fcc96922e6,
        0xcdeb14c9d9204b3a,
        0x1341ef0da8536e34,
        0xd7e3400f2bacde63,
        0x6911eeb42f70d7e5,
        0xc3a2a910a4679767,
        0x1773cbe4a0f6bb28,
        0xe17b0d53e843eab5,
    ],
    [
        0x587fa39990b62800,
        0x0d5d32788135879d,
        0x277f7b31fd3a4cdb,
        0xa435290ee56d7efa,
        0xea6f40be35159925,
        0xcb73377a506171cb,
        0xe43c367ce731d82a,
        0x6eb305031ca10c43,
        0xc019a8c622cc84cb,
        0xd5614f5658c612e6,
        0x7b1ecbe957c3ff98,
        0x60db6ee9651a8478,
    ],
    // Terminal full rounds (4)
    [
        0x9271d450fc9b4117,
        0xcffeea06b6e3aac1,
        0xfa4a44c748d1cd8e,
        0xe64db01ba569b469,
        0xd31005160e4045fe,
        0x39e0fa013e025f79,
        0xe243be574196a956,
        0x205b2a681e3d2642,
        0x79cae5ad93486bab,
        0xfdf567844e32c295,
        0x331679589bfb7189,
        0xaf06ee32297b89c2,
    ],
    [
        0xa6bcae311e498491,
        0x9d16f52c96ac8b3e,
        0x48a674b59393fa35,
        0x0f9e65da3fde3796,
        0x1e098310fc84578c,
        0x559ae5fab1ae8dad,
        0x56bd4d624078881d,
        0xfd8bbbf8fbe817b5,
        0x82d30695c44df534,
        0x3ec0a97bc41127c5,
        0x1eb8b64adaa22078,
        0x82c45e418d60c983,
    ],
    [
        0xb092280f484d55bf,
        0xcd317c9537697939,
        0xd3be2e352feb79f3,
        0xca6d866539a390e5,
        0xb5efb1a494e55ee6,
        0xfa9013ac89756e9e,
        0xaeb88efd1e981242,
        0x13ee477cdab6e0dc,
        0xce7df902c40da2d3,
        0xf3fbaf0d4e6f5f34,
        0xf96354ada6785f38,
        0x13b5692812406886,
    ],
    [
        0xf03cae030a0f4418,
        0x7d3172887aa98e1a,
        0x8a2c2644f2faf7b9,
        0x80d721abee696d00,
        0x27c8b903a4d68267,
        0xaf0b7b12f90291b8,
        0x00acd08cfdff3817,
        0x4659ee496c634328,
        0xf5b25c10730dbff1,
        0xdde3a153297329c2,
        0x50c0b70d6910a44b,
        0x23c7426af725a6a0,
    ],
]);

/// Create the default width-8 Poseidon1 permutation for Goldilocks.
///
/// Returns the platform-optimal implementation: dual-dispatch on aarch64
/// (generic for scalar, fused ASM for packed), generic Karatsuba on all
/// other platforms.
#[cfg(target_arch = "aarch64")]
pub fn default_goldilocks_poseidon1_8() -> Poseidon1Goldilocks<8> {
    let constants = 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) = constants.to_optimized();
    let fused = crate::Poseidon1GoldilocksFused::new(&full, &partial);
    crate::Poseidon1GoldilocksDispatch::new(fused, full, partial)
}

/// Create the default width-8 Poseidon1 permutation for Goldilocks.
///
/// Returns the platform-optimal implementation: fused ASM on aarch64,
/// generic Karatsuba on all other platforms.
#[cfg(not(target_arch = "aarch64"))]
pub fn default_goldilocks_poseidon1_8() -> Poseidon1Goldilocks<8> {
    Poseidon1::new(&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(),
    })
}

/// Create the default width-12 Poseidon1 permutation for Goldilocks.
///
/// Returns the platform-optimal implementation: dual-dispatch on aarch64
/// (generic for scalar, fused ASM for packed), generic Karatsuba on all
/// other platforms.
#[cfg(target_arch = "aarch64")]
pub fn default_goldilocks_poseidon1_12() -> Poseidon1Goldilocks<12> {
    let constants = 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) = constants.to_optimized();
    let fused = crate::Poseidon1GoldilocksFused::new(&full, &partial);
    crate::Poseidon1GoldilocksDispatch::new(fused, full, partial)
}

/// Create the default width-12 Poseidon1 permutation for Goldilocks.
///
/// Returns the platform-optimal implementation: fused ASM on aarch64,
/// generic Karatsuba on all other platforms.
#[cfg(not(target_arch = "aarch64"))]
pub fn default_goldilocks_poseidon1_12() -> Poseidon1Goldilocks<12> {
    Poseidon1::new(&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(),
    })
}

#[cfg(test)]
mod tests {
    use p3_symmetric::Permutation;
    use rand::SeedableRng;
    use rand::rngs::SmallRng;

    use super::*;

    type F = Goldilocks;

    /// Known-answer test for width 8 (sequential 0..7 input).
    #[test]
    fn test_poseidon_goldilocks_width_8() {
        let perm = default_goldilocks_poseidon1_8();

        let mut input: [F; 8] = F::new_array([0, 1, 2, 3, 4, 5, 6, 7]);
        perm.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_poseidon_goldilocks_width_12() {
        let perm = default_goldilocks_poseidon1_12();

        let mut input: [F; 12] = F::new_array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);
        perm.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);
    }

    /// Smoke test for width 16 with random constants.
    /// Uses the generic type directly since the fused type only supports 8 and 12.
    #[test]
    fn test_poseidon_goldilocks_width_16() {
        let mut rng = SmallRng::seed_from_u64(1);
        let poseidon = Poseidon1GoldilocksGeneric::<16>::new_from_rng(
            GOLDILOCKS_POSEIDON_HALF_FULL_ROUNDS,
            GOLDILOCKS_POSEIDON_PARTIAL_ROUNDS_8,
            &MdsMatrixGoldilocks,
            &mut rng,
        );
        let input: [F; 16] = rand::RngExt::random(&mut rng);
        let output = poseidon.permute(input);
        assert_ne!(output, input);
    }

    /// Smoke test for width 24 with random constants.
    #[test]
    fn test_poseidon_goldilocks_width_24() {
        let mut rng = SmallRng::seed_from_u64(1);
        let poseidon = Poseidon1GoldilocksGeneric::<24>::new_from_rng(
            GOLDILOCKS_POSEIDON_HALF_FULL_ROUNDS,
            GOLDILOCKS_POSEIDON_PARTIAL_ROUNDS_8,
            &MdsMatrixGoldilocks,
            &mut rng,
        );
        let input: [F; 24] = rand::RngExt::random(&mut rng);
        let output = poseidon.permute(input);
        assert_ne!(output, input);
    }

    #[cfg(all(target_arch = "x86_64", target_feature = "avx512f"))]
    mod avx512 {
        use super::*;
        use crate::PackedGoldilocksAVX512;

        #[test]
        fn test_avx512_poseidon_width_16() {
            let mut rng = SmallRng::seed_from_u64(1);
            let poseidon = Poseidon1GoldilocksGeneric::<16>::new_from_rng(
                GOLDILOCKS_POSEIDON_HALF_FULL_ROUNDS,
                GOLDILOCKS_POSEIDON_PARTIAL_ROUNDS_8,
                &MdsMatrixGoldilocks,
                &mut rng,
            );
            let input: [F; 16] = rand::RngExt::random(&mut rng);

            let mut expected = input;
            poseidon.permute_mut(&mut expected);

            let mut avx512_input = input.map(Into::<PackedGoldilocksAVX512>::into);
            poseidon.permute_mut(&mut avx512_input);

            let avx512_output = avx512_input.map(|x| x.0[0]);
            assert_eq!(avx512_output, expected);
        }

        #[test]
        fn test_avx512_poseidon_width_24() {
            let mut rng = SmallRng::seed_from_u64(1);
            let poseidon = Poseidon1GoldilocksGeneric::<24>::new_from_rng(
                GOLDILOCKS_POSEIDON_HALF_FULL_ROUNDS,
                GOLDILOCKS_POSEIDON_PARTIAL_ROUNDS_8,
                &MdsMatrixGoldilocks,
                &mut rng,
            );
            let input: [F; 24] = rand::RngExt::random(&mut rng);

            let mut expected = input;
            poseidon.permute_mut(&mut expected);

            let mut avx512_input = input.map(Into::<PackedGoldilocksAVX512>::into);
            poseidon.permute_mut(&mut avx512_input);

            let avx512_output = avx512_input.map(|x| x.0[0]);
            assert_eq!(avx512_output, expected);
        }
    }

    #[cfg(all(
        target_arch = "x86_64",
        target_feature = "avx2",
        not(target_feature = "avx512f")
    ))]
    mod avx2 {
        use super::*;
        use crate::PackedGoldilocksAVX2;

        #[test]
        fn test_avx2_poseidon_width_16() {
            let mut rng = SmallRng::seed_from_u64(1);
            let poseidon = Poseidon1GoldilocksGeneric::<16>::new_from_rng(
                GOLDILOCKS_POSEIDON_HALF_FULL_ROUNDS,
                GOLDILOCKS_POSEIDON_PARTIAL_ROUNDS_8,
                &MdsMatrixGoldilocks,
                &mut rng,
            );
            let input: [F; 16] = rand::RngExt::random(&mut rng);

            let mut expected = input;
            poseidon.permute_mut(&mut expected);

            let mut avx2_input = input.map(Into::<PackedGoldilocksAVX2>::into);
            poseidon.permute_mut(&mut avx2_input);

            let avx2_output = avx2_input.map(|x| x.0[0]);
            assert_eq!(avx2_output, expected);
        }

        #[test]
        fn test_avx2_poseidon_width_24() {
            let mut rng = SmallRng::seed_from_u64(1);
            let poseidon = Poseidon1GoldilocksGeneric::<24>::new_from_rng(
                GOLDILOCKS_POSEIDON_HALF_FULL_ROUNDS,
                GOLDILOCKS_POSEIDON_PARTIAL_ROUNDS_8,
                &MdsMatrixGoldilocks,
                &mut rng,
            );
            let input: [F; 24] = rand::RngExt::random(&mut rng);

            let mut expected = input;
            poseidon.permute_mut(&mut expected);

            let mut avx2_input = input.map(Into::<PackedGoldilocksAVX2>::into);
            poseidon.permute_mut(&mut avx2_input);

            let avx2_output = avx2_input.map(|x| x.0[0]);
            assert_eq!(avx2_output, expected);
        }
    }

    #[cfg(target_arch = "aarch64")]
    mod neon {
        use super::*;
        use crate::PackedGoldilocksNeon;

        #[test]
        fn test_neon_poseidon_width_8() {
            let perm = default_goldilocks_poseidon1_8();
            let input: [F; 8] = F::new_array([0, 1, 2, 3, 4, 5, 6, 7]);

            let mut expected = input;
            perm.permute_mut(&mut expected);

            let mut neon_input = input.map(Into::<PackedGoldilocksNeon>::into);
            perm.permute_mut(&mut neon_input);

            let neon_output = neon_input.map(|x| x.0[0]);
            assert_eq!(neon_output, expected);
        }

        #[test]
        fn test_neon_poseidon_width_12() {
            let perm = default_goldilocks_poseidon1_12();
            let input: [F; 12] = F::new_array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);

            let mut expected = input;
            perm.permute_mut(&mut expected);

            let mut neon_input = input.map(Into::<PackedGoldilocksNeon>::into);
            perm.permute_mut(&mut neon_input);

            let neon_output = neon_input.map(|x| x.0[0]);
            assert_eq!(neon_output, expected);
        }
    }
}