tidecoin 0.33.0-beta

General purpose library for using and interoperating with Tidecoin.
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
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
// SPDX-License-Identifier: CC0-1.0

//! Post-quantum cryptographic types.
//!
//! Type definitions and crypto wrappers for the five post-quantum signature
//! schemes used by Tidecoin: Falcon-512, Falcon-1024, ML-DSA-44, ML-DSA-65,
//! and ML-DSA-87.
//!
//! These are the product-facing PQ types for normal `tidecoin` users.
//!
//! The lower-level [`consensus_core`] crate also exposes verifier-side PQ types
//! with similar names for use inside the shared validation engine. Most wallet,
//! app, and plumbing code should prefer the `tidecoin` versions from this
//! module.

use core::fmt;

use consensus_core::{
    PqError as ConsensusPqError, PqPublicKey as ConsensusPqPublicKey,
    PqSignature as ConsensusPqSignature,
};
use fips204::traits::{KeyGen, SerDes, Signer};
use hashes::hash160;
use ml_kem::kem::{Decapsulate, FromSeed, Kem, Key, KeyExport};
#[allow(deprecated)]
use ml_kem::ExpandedKeyEncoding;
use ml_kem::{MlKem512, Seed, B32};
use rand_core::{CryptoRng, RngCore};
use tide_fn_dsa_kgen::{
    sign_key_size, vrfy_key_size, KeyPairGeneratorStandard, FN_DSA_LOGN_1024, FN_DSA_LOGN_512,
};
use tide_fn_dsa_sign::{
    FalconProfile, SigningKey as TideSigningKey, SigningKeyStandard, FALCON_NONCE_LEN,
    TIDECOIN_LEGACY_FALCON512_SIG_BODY_MAX,
};
use zeroize::{Zeroize, Zeroizing};

use crate::network::Params;
use crate::prelude::Vec;

pub use primitives::PqScheme;

const TIDECOIN_LEGACY_FALCON512_SIG_MAX: usize =
    1 + FALCON_NONCE_LEN + TIDECOIN_LEGACY_FALCON512_SIG_BODY_MAX;

type MlKem512DecapsulationKey = <MlKem512 as Kem>::DecapsulationKey;
type MlKem512EncapsulationKey = <MlKem512 as Kem>::EncapsulationKey;

#[cfg(test)]
pub(crate) struct DeterministicTestRng(u64);

#[cfg(test)]
impl DeterministicTestRng {
    pub(crate) const fn new(seed: u64) -> Self {
        Self(seed)
    }
}

#[cfg(test)]
impl RngCore for DeterministicTestRng {
    fn next_u32(&mut self) -> u32 {
        self.next_u64() as u32
    }

    fn next_u64(&mut self) -> u64 {
        self.0 = self.0.wrapping_mul(0x9e37_79b9_7f4a_7c15).wrapping_add(0xbf58_476d_1ce4_e5b9);
        self.0 ^ (self.0 >> 29)
    }

    fn fill_bytes(&mut self, dest: &mut [u8]) {
        let mut offset = 0;
        while offset < dest.len() {
            let bytes = self.next_u64().to_le_bytes();
            let n = core::cmp::min(bytes.len(), dest.len() - offset);
            dest[offset..offset + n].copy_from_slice(&bytes[..n]);
            offset += n;
        }
    }

    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
        self.fill_bytes(dest);
        Ok(())
    }
}

#[cfg(test)]
impl CryptoRng for DeterministicTestRng {}

/// High-level cryptographic operations for Tidecoin PQ schemes.
pub trait PqSchemeCryptoExt {
    /// Returns whether this scheme is allowed at the given chain height.
    fn is_allowed_by_params_at_height(&self, height: u32, params: impl AsRef<Params>) -> bool;

    /// Generates a fresh PQ keypair for this scheme using caller-provided randomness.
    fn generate_keypair_with_rng<R>(
        self,
        rng: &mut R,
    ) -> Result<(PqPublicKey, PqSecretKey), PqError>
    where
        R: RngCore + CryptoRng;

    /// Generates a fresh PQ keypair for this scheme.
    #[cfg(feature = "rand")]
    fn generate_keypair(self) -> (PqPublicKey, PqSecretKey);

    /// Generates a deterministic PQ keypair from the scheme-specific seed bytes.
    fn generate_keypair_from_seed(self, seed: &[u8])
        -> Result<(PqPublicKey, PqSecretKey), PqError>;
}

impl PqSchemeCryptoExt for PqScheme {
    /// Returns whether this scheme is allowed at the given chain height.
    ///
    /// Tidecoin permits only Falcon-512 before auxpow activation. Once auxpow is active,
    /// all current PQ schemes are allowed.
    fn is_allowed_by_params_at_height(&self, height: u32, params: impl AsRef<Params>) -> bool {
        Self::is_allowed_at_height(
            *self,
            height,
            params.as_ref().auxpow_start_height.map(|height| height.to_u32()),
        )
    }

    /// Generates a fresh PQ keypair for this scheme using caller-provided randomness.
    fn generate_keypair_with_rng<R>(
        self,
        rng: &mut R,
    ) -> Result<(PqPublicKey, PqSecretKey), PqError>
    where
        R: RngCore + CryptoRng,
    {
        Ok(match self {
            Self::Falcon512 => {
                let mut seed = Zeroizing::new([0u8; 48]);
                rng.fill_bytes(seed.as_mut());
                let mut sk_buf = Zeroizing::new(vec![
                    0u8;
                    sign_key_size(FN_DSA_LOGN_512)
                        .map_err(|_| PqError::BackendFailure)?
                ]);
                let mut vk_buf =
                    vec![0u8; vrfy_key_size(FN_DSA_LOGN_512).map_err(|_| PqError::BackendFailure)?];
                KeyPairGeneratorStandard::default()
                    .keygen_from_seed_pqclean(
                        FN_DSA_LOGN_512,
                        &seed[..],
                        &mut sk_buf[..],
                        &mut vk_buf[..],
                    )
                    .map_err(|_| PqError::BackendFailure)?;
                (PqPublicKey::from_backend(self, &vk_buf), PqSecretKey::from_backend(self, &sk_buf))
            }
            Self::Falcon1024 => {
                let mut seed = Zeroizing::new([0u8; 48]);
                rng.fill_bytes(seed.as_mut());
                let mut sk_buf = Zeroizing::new(vec![
                    0u8;
                    sign_key_size(FN_DSA_LOGN_1024)
                        .map_err(|_| PqError::BackendFailure)?
                ]);
                let mut vk_buf = vec![
                    0u8;
                    vrfy_key_size(FN_DSA_LOGN_1024)
                        .map_err(|_| PqError::BackendFailure)?
                ];
                KeyPairGeneratorStandard::default()
                    .keygen_from_seed_pqclean(
                        FN_DSA_LOGN_1024,
                        &seed[..],
                        &mut sk_buf[..],
                        &mut vk_buf[..],
                    )
                    .map_err(|_| PqError::BackendFailure)?;
                (PqPublicKey::from_backend(self, &vk_buf), PqSecretKey::from_backend(self, &sk_buf))
            }
            Self::MlDsa44 => {
                let (pk, sk) = fips204::ml_dsa_44::try_keygen_with_rng(rng)
                    .map_err(|_| PqError::BackendFailure)?;
                let sk_bytes = Zeroizing::new(sk.into_bytes());
                (
                    PqPublicKey::from_backend(self, &pk.into_bytes()),
                    PqSecretKey::from_backend(self, sk_bytes.as_ref()),
                )
            }
            Self::MlDsa65 => {
                let (pk, sk) = fips204::ml_dsa_65::try_keygen_with_rng(rng)
                    .map_err(|_| PqError::BackendFailure)?;
                let sk_bytes = Zeroizing::new(sk.into_bytes());
                (
                    PqPublicKey::from_backend(self, &pk.into_bytes()),
                    PqSecretKey::from_backend(self, sk_bytes.as_ref()),
                )
            }
            Self::MlDsa87 => {
                let (pk, sk) = fips204::ml_dsa_87::try_keygen_with_rng(rng)
                    .map_err(|_| PqError::BackendFailure)?;
                let sk_bytes = Zeroizing::new(sk.into_bytes());
                (
                    PqPublicKey::from_backend(self, &pk.into_bytes()),
                    PqSecretKey::from_backend(self, sk_bytes.as_ref()),
                )
            }
        })
    }

    /// Generates a fresh PQ keypair for this scheme.
    #[cfg(feature = "rand")]
    fn generate_keypair(self) -> (PqPublicKey, PqSecretKey) {
        self.generate_keypair_with_rng(&mut rand_core::OsRng).expect("OS random PQ keygen")
    }

    /// Generates a deterministic PQ keypair from a scheme-specific seed.
    fn generate_keypair_from_seed(
        self,
        seed: &[u8],
    ) -> Result<(PqPublicKey, PqSecretKey), PqError> {
        let expected = self.deterministic_seed_len();
        if seed.len() != expected {
            return Err(PqError::InvalidSeedLength { expected, got: seed.len() });
        }

        match self {
            Self::Falcon512 | Self::Falcon1024 => {
                let logn = match self {
                    Self::Falcon512 => FN_DSA_LOGN_512,
                    _ => FN_DSA_LOGN_1024,
                };
                let mut sk_buf = Zeroizing::new(vec![
                    0u8;
                    sign_key_size(logn)
                        .map_err(|_| PqError::BackendFailure)?
                ]);
                let mut vk_buf =
                    vec![0u8; vrfy_key_size(logn).map_err(|_| PqError::BackendFailure)?];
                if KeyPairGeneratorStandard::default()
                    .keygen_from_seed_pqclean(logn, seed, &mut sk_buf[..], &mut vk_buf[..])
                    .is_err()
                {
                    return Err(PqError::BackendFailure);
                }
                Ok((
                    PqPublicKey::from_backend(self, &vk_buf),
                    PqSecretKey::from_backend(self, &sk_buf),
                ))
            }
            Self::MlDsa44 => {
                let xi: &[u8; 32] = seed.try_into().map_err(|_| PqError::BackendFailure)?;
                let (pk, sk) = fips204::ml_dsa_44::KG::keygen_from_seed(xi);
                let sk_bytes = Zeroizing::new(sk.into_bytes());
                Ok((
                    PqPublicKey { scheme: self, data: pk.into_bytes().to_vec() },
                    PqSecretKey { scheme: self, data: sk_bytes[..].to_vec() },
                ))
            }
            Self::MlDsa65 => {
                let xi: &[u8; 32] = seed.try_into().map_err(|_| PqError::BackendFailure)?;
                let (pk, sk) = fips204::ml_dsa_65::KG::keygen_from_seed(xi);
                let sk_bytes = Zeroizing::new(sk.into_bytes());
                Ok((
                    PqPublicKey { scheme: self, data: pk.into_bytes().to_vec() },
                    PqSecretKey { scheme: self, data: sk_bytes[..].to_vec() },
                ))
            }
            Self::MlDsa87 => {
                let xi: &[u8; 32] = seed.try_into().map_err(|_| PqError::BackendFailure)?;
                let (pk, sk) = fips204::ml_dsa_87::KG::keygen_from_seed(xi);
                let sk_bytes = Zeroizing::new(sk.into_bytes());
                Ok((
                    PqPublicKey { scheme: self, data: pk.into_bytes().to_vec() },
                    PqSecretKey { scheme: self, data: sk_bytes[..].to_vec() },
                ))
            }
        }
    }
}

/// A post-quantum public key.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct PqPublicKey {
    scheme: PqScheme,
    data: Vec<u8>,
}

impl PqPublicKey {
    fn from_backend(scheme: PqScheme, data: &[u8]) -> Self {
        Self { scheme, data: data.to_vec() }
    }

    /// Derives the public key matching the given secret key.
    pub fn from_secret_key(sk: &PqSecretKey) -> Result<Self, PqError> {
        let scheme = sk.scheme();
        let data = match scheme {
            PqScheme::Falcon512 | PqScheme::Falcon1024 => {
                let logn = match scheme {
                    PqScheme::Falcon512 => FN_DSA_LOGN_512,
                    _ => FN_DSA_LOGN_1024,
                };
                let signing_key =
                    SigningKeyStandard::decode(sk.as_bytes()).ok_or(PqError::BackendFailure)?;
                let mut vk = vec![0u8; vrfy_key_size(logn).map_err(|_| PqError::BackendFailure)?];
                signing_key.to_verifying_key(&mut vk).map_err(|_| PqError::BackendFailure)?;
                vk
            }
            _ => {
                let result: Result<Vec<u8>, PqError> = match scheme {
                    PqScheme::MlDsa44 => {
                        let sk_arr = Zeroizing::new(
                            sk.as_bytes().try_into().map_err(|_| PqError::BackendFailure)?,
                        );
                        let sk_obj = fips204::ml_dsa_44::PrivateKey::try_from_bytes(*sk_arr)
                            .map_err(|_| PqError::BackendFailure)?;
                        Ok(sk_obj.get_public_key().into_bytes().to_vec())
                    }
                    PqScheme::MlDsa65 => {
                        let sk_arr = Zeroizing::new(
                            sk.as_bytes().try_into().map_err(|_| PqError::BackendFailure)?,
                        );
                        let sk_obj = fips204::ml_dsa_65::PrivateKey::try_from_bytes(*sk_arr)
                            .map_err(|_| PqError::BackendFailure)?;
                        Ok(sk_obj.get_public_key().into_bytes().to_vec())
                    }
                    PqScheme::MlDsa87 => {
                        let sk_arr = Zeroizing::new(
                            sk.as_bytes().try_into().map_err(|_| PqError::BackendFailure)?,
                        );
                        let sk_obj = fips204::ml_dsa_87::PrivateKey::try_from_bytes(*sk_arr)
                            .map_err(|_| PqError::BackendFailure)?;
                        Ok(sk_obj.get_public_key().into_bytes().to_vec())
                    }
                    _ => unreachable!(),
                };
                result?
            }
        };

        Ok(Self { scheme, data })
    }

    /// Parses a prefixed public key: `[scheme_prefix_byte][raw_pubkey_bytes]`.
    pub fn from_prefixed_slice(data: &[u8]) -> Result<Self, PqError> {
        let (&prefix, raw) = data.split_first().ok_or(PqError::EmptyData)?;
        let scheme = PqScheme::from_prefix(prefix).ok_or(PqError::UnknownScheme(prefix))?;
        let expected = scheme.pubkey_len();
        if raw.len() != expected {
            return Err(PqError::InvalidKeyLength { expected, got: raw.len() });
        }
        Ok(Self { scheme, data: raw.to_vec() })
    }

    /// Serializes as `[scheme_prefix_byte][raw_pubkey_bytes]`.
    pub fn to_prefixed_bytes(&self) -> Vec<u8> {
        let mut buf = Vec::with_capacity(1 + self.data.len());
        buf.push(self.scheme.prefix());
        buf.extend_from_slice(&self.data);
        buf
    }

    /// Returns the scheme this key belongs to.
    pub fn scheme(&self) -> PqScheme {
        self.scheme
    }

    /// Raw public key bytes without the scheme prefix.
    pub fn as_bytes(&self) -> &[u8] {
        &self.data
    }

    /// HASH160 of the prefixed public key, suitable for P2PKH / P2WPKH addresses.
    pub fn key_id(&self) -> hash160::Hash {
        hash160::Hash::hash(&self.to_prefixed_bytes())
    }
}

/// A post-quantum secret key.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PqSecretKey {
    scheme: PqScheme,
    data: Vec<u8>,
}

impl Drop for PqSecretKey {
    fn drop(&mut self) {
        self.zeroize();
    }
}

impl Zeroize for PqSecretKey {
    fn zeroize(&mut self) {
        self.data.zeroize();
    }
}

impl PqSecretKey {
    fn from_backend(scheme: PqScheme, data: &[u8]) -> Self {
        Self { scheme, data: data.to_vec() }
    }

    /// Creates a secret key from raw bytes for the given scheme.
    pub fn from_slice(scheme: PqScheme, data: &[u8]) -> Result<Self, PqError> {
        let expected = scheme.seckey_len();
        if data.len() != expected {
            return Err(PqError::InvalidKeyLength { expected, got: data.len() });
        }
        if !is_valid_secret_key_encoding(scheme, data) {
            return Err(PqError::InvalidSecretKeyEncoding(scheme));
        }
        Ok(Self { scheme, data: data.to_vec() })
    }

    /// Parses a prefixed secret key: `[scheme_prefix_byte][raw_seckey_bytes]`.
    pub fn from_prefixed_slice(data: &[u8]) -> Result<Self, PqError> {
        let (&prefix, raw) = data.split_first().ok_or(PqError::EmptyData)?;
        let scheme = PqScheme::from_prefix(prefix).ok_or(PqError::UnknownScheme(prefix))?;
        let expected = scheme.seckey_len();
        if raw.len() != expected {
            return Err(PqError::InvalidKeyLength { expected, got: raw.len() });
        }
        Self::from_slice(scheme, raw)
    }

    /// Decodes a secret key from prefixed bytes or, when allowed, a legacy raw secret key.
    pub fn decode_slice(data: &[u8], allow_legacy: bool) -> Result<Self, PqError> {
        if data.is_empty() {
            return Err(PqError::EmptyData);
        }

        if let Some(scheme) = PqScheme::from_prefix(data[0]) {
            if data.len() == scheme.prefixed_seckey_len() {
                return Self::from_prefixed_slice(data);
            }
        }

        if !allow_legacy {
            return Err(PqError::InvalidKeyLength { expected: 0, got: data.len() });
        }

        for scheme in [
            PqScheme::Falcon512,
            PqScheme::Falcon1024,
            PqScheme::MlDsa44,
            PqScheme::MlDsa65,
            PqScheme::MlDsa87,
        ] {
            if data.len() == scheme.seckey_len() && is_valid_secret_key_encoding(scheme, data) {
                return Ok(Self { scheme, data: data.to_vec() });
            }
        }

        Err(PqError::InvalidSecretKeyEncodingForLength(data.len()))
    }

    /// Serializes as `[scheme_prefix_byte][raw_seckey_bytes]`.
    pub fn to_prefixed_bytes(&self) -> Zeroizing<Vec<u8>> {
        let mut buf = Vec::with_capacity(1 + self.data.len());
        buf.push(self.scheme.prefix());
        buf.extend_from_slice(&self.data);
        Zeroizing::new(buf)
    }

    /// Returns the scheme this key belongs to.
    pub fn scheme(&self) -> PqScheme {
        self.scheme
    }

    /// Raw secret key bytes.
    pub fn as_bytes(&self) -> &[u8] {
        &self.data
    }
}

/// A raw post-quantum signature (without the sighash type byte).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PqSignature {
    data: Vec<u8>,
}

impl PqSignature {
    /// Wraps raw signature bytes.
    pub fn from_slice(data: &[u8]) -> Self {
        Self { data: data.to_vec() }
    }

    /// Raw signature bytes.
    pub fn as_bytes(&self) -> &[u8] {
        &self.data
    }

    /// Length of the raw signature in bytes.
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Returns whether the signature is empty.
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// Signs a 32-byte message digest with the given secret key.
    #[cfg(feature = "rand")]
    pub fn sign_msg32(msg: &[u8; 32], sk: &PqSecretKey) -> Result<Self, PqError> {
        Self::sign_msg32_with_rng(msg, sk, &mut rand_core::OsRng)
    }

    /// Signs a 64-byte message digest with the given secret key.
    #[cfg(feature = "rand")]
    pub fn sign_msg64(msg: &[u8; 64], sk: &PqSecretKey) -> Result<Self, PqError> {
        Self::sign_msg64_with_rng(msg, sk, &mut rand_core::OsRng)
    }

    /// Signs a 32-byte message digest with caller-provided randomness.
    pub fn sign_msg32_with_rng<R>(
        msg: &[u8; 32],
        sk: &PqSecretKey,
        rng: &mut R,
    ) -> Result<Self, PqError>
    where
        R: RngCore + CryptoRng,
    {
        Self::sign_message_with_rng(msg, sk, rng)
    }

    /// Signs a 64-byte message digest with caller-provided randomness.
    pub fn sign_msg64_with_rng<R>(
        msg: &[u8; 64],
        sk: &PqSecretKey,
        rng: &mut R,
    ) -> Result<Self, PqError>
    where
        R: RngCore + CryptoRng,
    {
        Self::sign_message_with_rng(msg, sk, rng)
    }

    /// Verifies a 32-byte message digest against the given public key.
    pub fn verify_msg32(&self, msg: &[u8; 32], pk: &PqPublicKey) -> Result<(), PqError> {
        self.as_consensus_signature().verify_msg32(msg, &pk.as_consensus_key()?).map_err(Into::into)
    }

    /// Verifies a 64-byte message digest against the given public key.
    pub fn verify_msg64(&self, msg: &[u8; 64], pk: &PqPublicKey) -> Result<(), PqError> {
        self.as_consensus_signature().verify_msg64(msg, &pk.as_consensus_key()?).map_err(Into::into)
    }

    /// Signs a 32-byte message digest using the node's Falcon-512 legacy format when applicable.
    #[cfg(feature = "rand")]
    pub fn sign_msg32_legacy(msg: &[u8; 32], sk: &PqSecretKey) -> Result<Self, PqError> {
        Self::sign_msg32_legacy_with_rng(msg, sk, &mut rand_core::OsRng)
    }

    /// Signs a 64-byte message digest using the node's Falcon-512 legacy format when applicable.
    #[cfg(feature = "rand")]
    pub fn sign_msg64_legacy(msg: &[u8; 64], sk: &PqSecretKey) -> Result<Self, PqError> {
        Self::sign_msg64_legacy_with_rng(msg, sk, &mut rand_core::OsRng)
    }

    /// Signs a 32-byte message digest using caller-provided randomness and the node's
    /// Falcon-512 legacy format when applicable.
    pub fn sign_msg32_legacy_with_rng<R>(
        msg: &[u8; 32],
        sk: &PqSecretKey,
        rng: &mut R,
    ) -> Result<Self, PqError>
    where
        R: RngCore + CryptoRng,
    {
        Self::sign_message_legacy_with_rng(msg, sk, rng)
    }

    /// Signs a 64-byte message digest using caller-provided randomness and the node's
    /// Falcon-512 legacy format when applicable.
    pub fn sign_msg64_legacy_with_rng<R>(
        msg: &[u8; 64],
        sk: &PqSecretKey,
        rng: &mut R,
    ) -> Result<Self, PqError>
    where
        R: RngCore + CryptoRng,
    {
        Self::sign_message_legacy_with_rng(msg, sk, rng)
    }

    /// Verifies a 32-byte message digest using the node's Falcon-512 legacy format when applicable.
    pub fn verify_msg32_legacy(&self, msg: &[u8; 32], pk: &PqPublicKey) -> Result<(), PqError> {
        self.as_consensus_signature()
            .verify_msg32_legacy(msg, &pk.as_consensus_key()?)
            .map_err(Into::into)
    }

    /// Verifies a 64-byte message digest using the node's Falcon-512 legacy format when applicable.
    pub fn verify_msg64_legacy(&self, msg: &[u8; 64], pk: &PqPublicKey) -> Result<(), PqError> {
        self.as_consensus_signature()
            .verify_msg64_legacy(msg, &pk.as_consensus_key()?)
            .map_err(Into::into)
    }

    /// Verifies a 32-byte message digest, accepting either strict or legacy Falcon-512 signatures.
    pub fn verify_msg32_allow_legacy(
        &self,
        msg: &[u8; 32],
        pk: &PqPublicKey,
    ) -> Result<(), PqError> {
        self.as_consensus_signature()
            .verify_msg32_allow_legacy(msg, &pk.as_consensus_key()?)
            .map_err(Into::into)
    }

    /// Verifies a 64-byte message digest, accepting either strict or legacy Falcon-512 signatures.
    pub fn verify_msg64_allow_legacy(
        &self,
        msg: &[u8; 64],
        pk: &PqPublicKey,
    ) -> Result<(), PqError> {
        self.as_consensus_signature()
            .verify_msg64_allow_legacy(msg, &pk.as_consensus_key()?)
            .map_err(Into::into)
    }

    fn sign_message_with_rng<R>(msg: &[u8], sk: &PqSecretKey, rng: &mut R) -> Result<Self, PqError>
    where
        R: RngCore + CryptoRng,
    {
        Ok(match sk.scheme() {
            PqScheme::Falcon512 | PqScheme::Falcon1024 => {
                let mut signing_key =
                    SigningKeyStandard::decode(sk.as_bytes()).ok_or(PqError::BackendFailure)?;
                let mut sig = vec![0u8; sk.scheme().max_sig_len()];
                let sig_len = signing_key
                    .sign_falcon(rng, FalconProfile::PqClean, msg, &mut sig)
                    .map_err(|_| PqError::BackendFailure)?;
                sig.truncate(sig_len);
                Self::from_slice(&sig)
            }
            PqScheme::MlDsa44 => {
                let sk_arr =
                    Zeroizing::new(sk.as_bytes().try_into().map_err(|_| PqError::BackendFailure)?);
                let sk_obj = fips204::ml_dsa_44::PrivateKey::try_from_bytes(*sk_arr)
                    .map_err(|_| PqError::BackendFailure)?;
                let sig =
                    sk_obj.try_sign_with_rng(rng, msg, &[]).map_err(|_| PqError::BackendFailure)?;
                Self::from_slice(&sig)
            }
            PqScheme::MlDsa65 => {
                let sk_arr =
                    Zeroizing::new(sk.as_bytes().try_into().map_err(|_| PqError::BackendFailure)?);
                let sk_obj = fips204::ml_dsa_65::PrivateKey::try_from_bytes(*sk_arr)
                    .map_err(|_| PqError::BackendFailure)?;
                let sig =
                    sk_obj.try_sign_with_rng(rng, msg, &[]).map_err(|_| PqError::BackendFailure)?;
                Self::from_slice(&sig)
            }
            PqScheme::MlDsa87 => {
                let sk_arr =
                    Zeroizing::new(sk.as_bytes().try_into().map_err(|_| PqError::BackendFailure)?);
                let sk_obj = fips204::ml_dsa_87::PrivateKey::try_from_bytes(*sk_arr)
                    .map_err(|_| PqError::BackendFailure)?;
                let sig =
                    sk_obj.try_sign_with_rng(rng, msg, &[]).map_err(|_| PqError::BackendFailure)?;
                Self::from_slice(&sig)
            }
        })
    }

    fn sign_message_legacy_with_rng<R>(
        msg: &[u8],
        sk: &PqSecretKey,
        rng: &mut R,
    ) -> Result<Self, PqError>
    where
        R: RngCore + CryptoRng,
    {
        if sk.scheme() != PqScheme::Falcon512 {
            return Self::sign_message_with_rng(msg, sk, rng);
        }
        let mut signing_key =
            SigningKeyStandard::decode(sk.as_bytes()).ok_or(PqError::BackendFailure)?;
        let mut sig = vec![0u8; TIDECOIN_LEGACY_FALCON512_SIG_MAX];
        let sig_len = signing_key
            .sign_falcon(rng, FalconProfile::TidecoinLegacyFalcon512, msg, &mut sig)
            .map_err(|_| PqError::BackendFailure)?;
        sig.truncate(sig_len);
        Ok(Self { data: sig })
    }

    fn as_consensus_signature(&self) -> ConsensusPqSignature {
        ConsensusPqSignature::from_slice(self.as_bytes())
    }
}

/// ML-KEM-512 public-key size in bytes.
pub const MLKEM512_PUBLICKEY_BYTES: usize = 800;
/// ML-KEM-512 secret-key size in bytes.
pub const MLKEM512_SECRETKEY_BYTES: usize = 1632;
/// ML-KEM-512 ciphertext size in bytes.
pub const MLKEM512_CIPHERTEXT_BYTES: usize = 768;
/// ML-KEM-512 shared-secret size in bytes.
pub const MLKEM512_SHARED_SECRET_BYTES: usize = 32;
/// Tidecoin-node deterministic ML-KEM-512 keypair coin length.
pub const MLKEM512_KEYPAIR_COINS_BYTES: usize = 2 * MLKEM512_SHARED_SECRET_BYTES;

/// An ML-KEM-512 public key.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MlKem512PublicKey {
    data: Vec<u8>,
}

impl MlKem512PublicKey {
    /// Parses a public key from raw bytes.
    pub fn from_slice(data: &[u8]) -> Result<Self, PqError> {
        if data.len() != MLKEM512_PUBLICKEY_BYTES {
            return Err(PqError::InvalidKeyLength {
                expected: MLKEM512_PUBLICKEY_BYTES,
                got: data.len(),
            });
        }
        Ok(Self { data: data.to_vec() })
    }

    /// Returns the raw public key bytes.
    pub fn as_bytes(&self) -> &[u8] {
        &self.data
    }
}

/// An ML-KEM-512 secret key.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MlKem512SecretKey {
    data: Vec<u8>,
}

impl Drop for MlKem512SecretKey {
    fn drop(&mut self) {
        self.zeroize();
    }
}

impl Zeroize for MlKem512SecretKey {
    fn zeroize(&mut self) {
        self.data.zeroize();
    }
}

impl MlKem512SecretKey {
    /// Parses a secret key from raw bytes.
    pub fn from_slice(data: &[u8]) -> Result<Self, PqError> {
        if data.len() != MLKEM512_SECRETKEY_BYTES {
            return Err(PqError::InvalidKeyLength {
                expected: MLKEM512_SECRETKEY_BYTES,
                got: data.len(),
            });
        }
        Ok(Self { data: data.to_vec() })
    }

    /// Returns the raw secret key bytes.
    pub fn as_bytes(&self) -> &[u8] {
        &self.data
    }
}

/// An ML-KEM-512 ciphertext.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MlKem512Ciphertext {
    data: Vec<u8>,
}

impl MlKem512Ciphertext {
    /// Parses a ciphertext from raw bytes.
    pub fn from_slice(data: &[u8]) -> Result<Self, PqError> {
        if data.len() != MLKEM512_CIPHERTEXT_BYTES {
            return Err(PqError::InvalidKeyLength {
                expected: MLKEM512_CIPHERTEXT_BYTES,
                got: data.len(),
            });
        }
        Ok(Self { data: data.to_vec() })
    }

    /// Returns the raw ciphertext bytes.
    pub fn as_bytes(&self) -> &[u8] {
        &self.data
    }
}

/// An ML-KEM-512 shared secret.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct MlKem512SharedSecret {
    data: [u8; MLKEM512_SHARED_SECRET_BYTES],
}

impl Drop for MlKem512SharedSecret {
    fn drop(&mut self) {
        self.zeroize();
    }
}

impl Zeroize for MlKem512SharedSecret {
    fn zeroize(&mut self) {
        self.data.zeroize();
    }
}

impl MlKem512SharedSecret {
    /// Parses a shared secret from raw bytes.
    pub fn from_slice(data: &[u8]) -> Result<Self, PqError> {
        if data.len() != MLKEM512_SHARED_SECRET_BYTES {
            return Err(PqError::InvalidKeyLength {
                expected: MLKEM512_SHARED_SECRET_BYTES,
                got: data.len(),
            });
        }
        let mut buf = [0_u8; MLKEM512_SHARED_SECRET_BYTES];
        buf.copy_from_slice(data);
        Ok(Self { data: buf })
    }

    /// Returns the raw shared-secret bytes.
    pub fn as_bytes(&self) -> &[u8; MLKEM512_SHARED_SECRET_BYTES] {
        &self.data
    }
}

/// An initialized ML-KEM-512 keypair.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MlKem512Keypair {
    public_key: MlKem512PublicKey,
    secret_key: MlKem512SecretKey,
}

impl MlKem512Keypair {
    /// Generates a fresh random ML-KEM-512 keypair.
    #[cfg(feature = "rand")]
    pub fn generate() -> Self {
        Self::generate_with_rng(&mut rand_core::OsRng)
    }

    /// Generates a fresh ML-KEM-512 keypair using caller-provided randomness.
    pub fn generate_with_rng<R>(rng: &mut R) -> Self
    where
        R: RngCore + CryptoRng,
    {
        let mut coins = [0_u8; MLKEM512_KEYPAIR_COINS_BYTES];
        rng.fill_bytes(&mut coins);
        let keypair = Self::generate_deterministic(&coins).expect("valid ML-KEM-512 coin length");
        coins.zeroize();
        keypair
    }

    /// Generates a deterministic ML-KEM-512 keypair from Tidecoin-node coins.
    pub fn generate_deterministic(coins: &[u8]) -> Result<Self, PqError> {
        if coins.len() != MLKEM512_KEYPAIR_COINS_BYTES {
            return Err(PqError::InvalidSeedLength {
                expected: MLKEM512_KEYPAIR_COINS_BYTES,
                got: coins.len(),
            });
        }

        let mut seed = Seed::try_from(coins).map_err(|_| PqError::BackendFailure)?;
        let (dk, ek) = MlKem512::from_seed(&seed);
        #[allow(deprecated)]
        let mut dk_bytes = dk.to_expanded_bytes();
        let secret_key = MlKem512SecretKey { data: dk_bytes.as_slice().to_vec() };
        dk_bytes.as_mut_slice().zeroize();
        seed.as_mut_slice().zeroize();
        Ok(Self {
            public_key: MlKem512PublicKey { data: ek.to_bytes().as_slice().to_vec() },
            secret_key,
        })
    }

    /// Constructs a keypair from raw public and secret key bytes.
    pub fn from_slices(public_key: &[u8], secret_key: &[u8]) -> Result<Self, PqError> {
        Ok(Self {
            public_key: MlKem512PublicKey::from_slice(public_key)?,
            secret_key: MlKem512SecretKey::from_slice(secret_key)?,
        })
    }

    /// Returns the public key.
    pub fn public_key(&self) -> &MlKem512PublicKey {
        &self.public_key
    }

    /// Returns the secret key.
    pub fn secret_key(&self) -> &MlKem512SecretKey {
        &self.secret_key
    }

    /// Consumes the keypair and returns both parts.
    pub fn into_parts(self) -> (MlKem512PublicKey, MlKem512SecretKey) {
        (self.public_key, self.secret_key)
    }
}

impl MlKem512PublicKey {
    /// Encapsulates to this public key.
    #[cfg(feature = "rand")]
    pub fn encapsulate(&self) -> Result<(MlKem512Ciphertext, MlKem512SharedSecret), PqError> {
        self.encapsulate_with_rng(&mut rand_core::OsRng)
    }

    /// Encapsulates to this public key using caller-provided randomness.
    pub fn encapsulate_with_rng<R>(
        &self,
        rng: &mut R,
    ) -> Result<(MlKem512Ciphertext, MlKem512SharedSecret), PqError>
    where
        R: RngCore + CryptoRng,
    {
        let mut coins = [0_u8; MLKEM512_SHARED_SECRET_BYTES];
        rng.fill_bytes(&mut coins);
        let result = self.encapsulate_deterministic(&coins);
        coins.zeroize();
        result
    }

    /// Deterministically encapsulates using Tidecoin-node coins.
    pub fn encapsulate_deterministic(
        &self,
        coins: &[u8],
    ) -> Result<(MlKem512Ciphertext, MlKem512SharedSecret), PqError> {
        if coins.len() != MLKEM512_SHARED_SECRET_BYTES {
            return Err(PqError::InvalidSeedLength {
                expected: MLKEM512_SHARED_SECRET_BYTES,
                got: coins.len(),
            });
        }

        let ek_arr = Key::<MlKem512EncapsulationKey>::try_from(self.as_bytes())
            .map_err(|_| PqError::BackendFailure)?;
        let ek = MlKem512EncapsulationKey::new(&ek_arr).map_err(|_| PqError::BackendFailure)?;
        let mut m = B32::try_from(coins).map_err(|_| PqError::BackendFailure)?;
        let (ct, mut ss) = ek.encapsulate_deterministic(&m);
        let shared_secret = MlKem512SharedSecret::from_slice(ss.as_slice())?;
        m.as_mut_slice().zeroize();
        ss.as_mut_slice().zeroize();
        Ok((MlKem512Ciphertext { data: ct.as_slice().to_vec() }, shared_secret))
    }
}

impl MlKem512SecretKey {
    /// Decapsulates an ML-KEM-512 ciphertext with this secret key.
    pub fn decapsulate(
        &self,
        ciphertext: &MlKem512Ciphertext,
    ) -> Result<MlKem512SharedSecret, PqError> {
        let mut dk_arr = ml_kem::ExpandedDecapsulationKey::<MlKem512>::try_from(self.as_bytes())
            .map_err(|_| PqError::BackendFailure)?;
        #[allow(deprecated)]
        let dk = MlKem512DecapsulationKey::from_expanded_bytes(&dk_arr)
            .map_err(|_| PqError::BackendFailure)?;
        let ct_arr = <ml_kem::Ciphertext<MlKem512>>::try_from(ciphertext.as_bytes())
            .map_err(|_| PqError::BackendFailure)?;
        let mut ss = dk.decapsulate(&ct_arr);
        let shared_secret = MlKem512SharedSecret::from_slice(ss.as_slice())?;
        dk_arr.as_mut_slice().zeroize();
        ss.as_mut_slice().zeroize();
        Ok(shared_secret)
    }
}

/// Errors related to post-quantum key and signature handling.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PqError {
    /// The prefix byte does not map to a known PQ scheme.
    UnknownScheme(u8),
    /// Key data has the wrong length.
    InvalidKeyLength {
        /// Expected number of bytes.
        expected: usize,
        /// Actual number of bytes received.
        got: usize,
    },
    /// Deterministic key material has the wrong length.
    InvalidSeedLength {
        /// Expected number of bytes.
        expected: usize,
        /// Actual number of bytes received.
        got: usize,
    },
    /// Signature data exceeds the maximum allowed length.
    InvalidSignatureLength {
        /// Maximum allowed bytes for this scheme.
        max: usize,
        /// Actual number of bytes received.
        got: usize,
    },
    /// Secret key bytes do not use the scheme's required encoding.
    InvalidSecretKeyEncoding(PqScheme),
    /// Secret key bytes do not match any known scheme encoding for their length.
    InvalidSecretKeyEncodingForLength(usize),
    /// Signature verification failed.
    VerificationFailed,
    /// Backend key/signature operation failed.
    BackendFailure,
    /// The input slice was empty.
    EmptyData,
}

impl fmt::Display for PqError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnknownScheme(b) => write!(f, "unknown PQ scheme prefix: 0x{:02x}", b),
            Self::InvalidKeyLength { expected, got } => {
                write!(f, "invalid key length: expected {} bytes, got {}", expected, got)
            }
            Self::InvalidSeedLength { expected, got } => write!(
                f,
                "invalid deterministic seed length: expected {} bytes, got {}",
                expected, got
            ),
            Self::InvalidSignatureLength { max, got } => {
                write!(f, "invalid signature length: max {} bytes, got {}", max, got)
            }
            Self::InvalidSecretKeyEncoding(scheme) => {
                write!(f, "invalid secret key encoding for {:?}", scheme)
            }
            Self::InvalidSecretKeyEncodingForLength(len) => {
                write!(f, "invalid secret key encoding for {}-byte key", len)
            }
            Self::VerificationFailed => write!(f, "signature verification failed"),
            Self::BackendFailure => write!(f, "backend PQ operation failed"),
            Self::EmptyData => write!(f, "empty data"),
        }
    }
}

impl From<ConsensusPqError> for PqError {
    fn from(err: ConsensusPqError) -> Self {
        match err {
            ConsensusPqError::UnknownScheme(prefix) => Self::UnknownScheme(prefix),
            ConsensusPqError::InvalidKeyLength { expected, got } => {
                Self::InvalidKeyLength { expected, got }
            }
            ConsensusPqError::VerificationFailed => Self::VerificationFailed,
            ConsensusPqError::BackendFailure => Self::BackendFailure,
            ConsensusPqError::EmptyData => Self::EmptyData,
        }
    }
}

impl PqPublicKey {
    fn as_consensus_key(&self) -> Result<ConsensusPqPublicKey, PqError> {
        ConsensusPqPublicKey::from_scheme_and_bytes(self.scheme, self.as_bytes())
            .map_err(Into::into)
    }
}

fn is_valid_secret_key_encoding(scheme: PqScheme, data: &[u8]) -> bool {
    if data.len() != scheme.seckey_len() {
        return false;
    }
    match scheme {
        PqScheme::Falcon512 => data.first().copied() == Some(0x59),
        PqScheme::Falcon1024 => data.first().copied() == Some(0x5A),
        PqScheme::MlDsa44 | PqScheme::MlDsa65 | PqScheme::MlDsa87 => true,
    }
}

#[cfg(feature = "std")]
impl std::error::Error for PqError {}

#[cfg(test)]
mod tests {
    use alloc::string::ToString;

    use super::*;

    fn tagged_seed(scheme: PqScheme, tag: u8) -> Vec<u8> {
        (0..scheme.deterministic_seed_len()).map(|i| tag ^ (i as u8).wrapping_mul(131)).collect()
    }

    fn deterministic_keypair(scheme: PqScheme, tag: u8) -> (PqPublicKey, PqSecretKey) {
        scheme.generate_keypair_from_seed(&tagged_seed(scheme, tag)).unwrap()
    }

    fn all_signature_schemes() -> [PqScheme; 5] {
        [
            PqScheme::Falcon512,
            PqScheme::Falcon1024,
            PqScheme::MlDsa44,
            PqScheme::MlDsa65,
            PqScheme::MlDsa87,
        ]
    }

    fn test_rng(tag: u64) -> DeterministicTestRng {
        DeterministicTestRng::new(tag)
    }

    #[test]
    fn scheme_round_trip() {
        for scheme in [
            PqScheme::Falcon512,
            PqScheme::Falcon1024,
            PqScheme::MlDsa44,
            PqScheme::MlDsa65,
            PqScheme::MlDsa87,
        ] {
            let prefix = scheme.prefix();
            let parsed = PqScheme::from_prefix(prefix).unwrap();
            assert_eq!(parsed, scheme);
            assert_eq!(scheme.prefix(), prefix);
            assert_eq!(scheme.prefixed_pubkey_len(), scheme.pubkey_len() + 1);
        }
        assert!(PqScheme::from_prefix(0x00).is_none());
    }

    #[test]
    fn pubkey_parse_and_serialize_all_schemes() {
        for scheme in [
            PqScheme::Falcon512,
            PqScheme::Falcon1024,
            PqScheme::MlDsa44,
            PqScheme::MlDsa65,
            PqScheme::MlDsa87,
        ] {
            let raw = vec![scheme.prefix(); scheme.pubkey_len()];
            let mut prefixed = vec![scheme.prefix()];
            prefixed.extend_from_slice(&raw);

            let pk = PqPublicKey::from_prefixed_slice(&prefixed).unwrap();
            assert_eq!(pk.scheme(), scheme);
            assert_eq!(pk.as_bytes(), &raw[..]);
            assert_eq!(pk.to_prefixed_bytes(), prefixed);
            assert_eq!(pk.key_id(), hash160::Hash::hash(&prefixed));
        }
    }

    #[test]
    fn pubkey_key_id_changes_with_prefix() {
        let raw_512 = vec![0xAB; PqScheme::Falcon512.pubkey_len()];
        let mut prefixed_512 = vec![PqScheme::Falcon512.prefix()];
        prefixed_512.extend_from_slice(&raw_512);
        let pk_512 = PqPublicKey::from_prefixed_slice(&prefixed_512).unwrap();

        let raw_1024 = vec![0xAB; PqScheme::Falcon1024.pubkey_len()];
        let mut prefixed_1024 = vec![PqScheme::Falcon1024.prefix()];
        prefixed_1024.extend_from_slice(&raw_1024);
        let pk_1024 = PqPublicKey::from_prefixed_slice(&prefixed_1024).unwrap();

        assert_eq!(pk_512.key_id(), hash160::Hash::hash(&prefixed_512));
        assert_eq!(pk_1024.key_id(), hash160::Hash::hash(&prefixed_1024));
        assert_ne!(pk_512.key_id(), pk_1024.key_id());
    }

    #[test]
    fn pubkey_wrong_length() {
        let err = PqPublicKey::from_prefixed_slice(&[0x07, 0x01, 0x02]).unwrap_err();
        assert_eq!(err, PqError::InvalidKeyLength { expected: 897, got: 2 });
        assert_eq!(err.to_string(), "invalid key length: expected 897 bytes, got 2");
    }

    #[test]
    fn pubkey_empty() {
        assert_eq!(PqPublicKey::from_prefixed_slice(&[]).unwrap_err(), PqError::EmptyData);
    }

    #[test]
    fn pubkey_unknown_scheme() {
        let data = vec![0xFF; 100];
        assert_eq!(
            PqPublicKey::from_prefixed_slice(&data).unwrap_err(),
            PqError::UnknownScheme(0xFF)
        );
    }

    #[test]
    fn seckey_parse_all_schemes() {
        for scheme in [
            PqScheme::Falcon512,
            PqScheme::Falcon1024,
            PqScheme::MlDsa44,
            PqScheme::MlDsa65,
            PqScheme::MlDsa87,
        ] {
            let fill = match scheme {
                PqScheme::Falcon512 => 0x59,
                PqScheme::Falcon1024 => 0x5A,
                _ => scheme.prefix(),
            };
            let raw = vec![fill; scheme.seckey_len()];
            let sk = PqSecretKey::from_slice(scheme, &raw).unwrap();
            assert_eq!(sk.scheme(), scheme);
            assert_eq!(sk.as_bytes(), &raw[..]);
        }
    }

    #[test]
    fn seckey_wrong_length() {
        let err = PqSecretKey::from_slice(PqScheme::MlDsa44, &[0; 10]).unwrap_err();
        assert_eq!(err, PqError::InvalidKeyLength { expected: 2560, got: 10 });
        assert_eq!(err.to_string(), "invalid key length: expected 2560 bytes, got 10");
    }

    #[test]
    fn seckey_length_table_matches_node_expectations() {
        let cases = [
            (PqScheme::Falcon512, 897, 1281, 752),
            (PqScheme::Falcon1024, 1793, 2305, 1462),
            (PqScheme::MlDsa44, 1312, 2560, 2420),
            (PqScheme::MlDsa65, 1952, 4032, 3309),
            (PqScheme::MlDsa87, 2592, 4896, 4627),
        ];

        for (scheme, pub_len, sec_len, sig_len) in cases {
            assert_eq!(scheme.pubkey_len(), pub_len);
            assert_eq!(scheme.seckey_len(), sec_len);
            assert_eq!(scheme.max_sig_len(), sig_len);
        }
    }

    #[test]
    fn seckey_prefixed_roundtrip_all_schemes() {
        for scheme in [
            PqScheme::Falcon512,
            PqScheme::Falcon1024,
            PqScheme::MlDsa44,
            PqScheme::MlDsa65,
            PqScheme::MlDsa87,
        ] {
            let (_, sk) = deterministic_keypair(scheme, scheme.prefix());
            let prefixed = sk.to_prefixed_bytes();
            let reparsed = PqSecretKey::from_prefixed_slice(&prefixed).unwrap();
            assert_eq!(reparsed, sk);
            assert_eq!(prefixed.len(), scheme.prefixed_seckey_len());
        }
    }

    #[test]
    fn seckey_legacy_decode_roundtrip_all_schemes() {
        for scheme in [
            PqScheme::Falcon512,
            PqScheme::Falcon1024,
            PqScheme::MlDsa44,
            PqScheme::MlDsa65,
            PqScheme::MlDsa87,
        ] {
            let (_, sk) = deterministic_keypair(scheme, scheme.prefix());
            let reparsed = PqSecretKey::decode_slice(sk.as_bytes(), true).unwrap();
            assert_eq!(reparsed, sk);
        }
    }

    #[test]
    fn seckey_legacy_decode_disallowed_when_prefix_missing() {
        let (_, sk) = deterministic_keypair(PqScheme::Falcon512, 0x51);
        assert_eq!(
            PqSecretKey::decode_slice(sk.as_bytes(), false).unwrap_err(),
            PqError::InvalidKeyLength { expected: 0, got: sk.as_bytes().len() }
        );
    }

    #[test]
    fn falcon_seckey_validates_scheme_header_byte() {
        let (_, sk_512) = deterministic_keypair(PqScheme::Falcon512, 0x52);
        let mut bad_512 = sk_512.as_bytes().to_vec();
        bad_512[0] = 0x58;
        assert_eq!(
            PqSecretKey::from_slice(PqScheme::Falcon512, &bad_512).unwrap_err(),
            PqError::InvalidSecretKeyEncoding(PqScheme::Falcon512)
        );

        let (_, sk_1024) = deterministic_keypair(PqScheme::Falcon1024, 0x53);
        let mut bad_1024 = sk_1024.as_bytes().to_vec();
        bad_1024[0] = 0x59;
        assert_eq!(
            PqSecretKey::from_slice(PqScheme::Falcon1024, &bad_1024).unwrap_err(),
            PqError::InvalidSecretKeyEncoding(PqScheme::Falcon1024)
        );
    }

    #[test]
    fn signature_round_trip() {
        let raw = vec![0x42; 700];
        let sig = PqSignature::from_slice(&raw);
        assert_eq!(sig.as_bytes(), &raw[..]);
        assert_eq!(sig.len(), 700);
        assert!(!sig.is_empty());
    }

    #[test]
    fn signature_empty_and_boundary() {
        let empty = PqSignature::from_slice(&[]);
        assert!(empty.is_empty());
        assert_eq!(empty.len(), 0);

        let boundary = PqSignature::from_slice(&vec![0x11; PqScheme::Falcon512.max_sig_len()]);
        assert_eq!(boundary.len(), PqScheme::Falcon512.max_sig_len());
        assert!(!boundary.is_empty());
    }

    #[test]
    fn signature_error_display() {
        let err = PqError::InvalidSignatureLength { max: 752, got: 753 };
        assert_eq!(err.to_string(), "invalid signature length: max 752 bytes, got 753");
    }

    #[test]
    fn generate_keypair_matches_length_table() {
        for scheme in [
            PqScheme::Falcon512,
            PqScheme::Falcon1024,
            PqScheme::MlDsa44,
            PqScheme::MlDsa65,
            PqScheme::MlDsa87,
        ] {
            let mut rng = test_rng(u64::from(scheme.prefix()));
            let (pk, sk) = scheme.generate_keypair_with_rng(&mut rng).unwrap();
            assert_eq!(pk.scheme(), scheme);
            assert_eq!(sk.scheme(), scheme);
            assert_eq!(pk.as_bytes().len(), scheme.pubkey_len());
            assert_eq!(sk.as_bytes().len(), scheme.seckey_len());
        }
    }

    #[test]
    fn derive_public_key_from_secret_all_schemes() {
        for scheme in [
            PqScheme::Falcon512,
            PqScheme::Falcon1024,
            PqScheme::MlDsa44,
            PqScheme::MlDsa65,
            PqScheme::MlDsa87,
        ] {
            let (pk, sk) = deterministic_keypair(scheme, scheme.prefix());
            let derived = PqPublicKey::from_secret_key(&sk).unwrap();
            assert_eq!(derived, pk);
        }
    }

    #[test]
    fn deterministic_keypair_generation_is_stable_all_schemes() {
        let seed48 = [0x42_u8; 48];
        let seed32 = [0x24_u8; 32];

        for scheme in [
            PqScheme::Falcon512,
            PqScheme::Falcon1024,
            PqScheme::MlDsa44,
            PqScheme::MlDsa65,
            PqScheme::MlDsa87,
        ] {
            let seed = match scheme.deterministic_seed_len() {
                48 => &seed48[..],
                32 => &seed32[..],
                _ => unreachable!(),
            };

            let (pk_a, sk_a) = scheme.generate_keypair_from_seed(seed).unwrap();
            let (pk_b, sk_b) = scheme.generate_keypair_from_seed(seed).unwrap();
            assert_eq!(pk_a, pk_b);
            assert_eq!(sk_a, sk_b);
            assert_eq!(PqPublicKey::from_secret_key(&sk_a).unwrap(), pk_a);
        }
    }

    #[test]
    fn sign_and_verify_msg32_all_schemes() {
        let msg = [0x5Au8; 32];

        for scheme in [
            PqScheme::Falcon512,
            PqScheme::Falcon1024,
            PqScheme::MlDsa44,
            PqScheme::MlDsa65,
            PqScheme::MlDsa87,
        ] {
            let (pk, sk) = deterministic_keypair(scheme, scheme.prefix());
            let mut rng = test_rng(u64::from(scheme.prefix()));
            let sig = PqSignature::sign_msg32_with_rng(&msg, &sk, &mut rng).unwrap();
            assert!(sig.len() <= scheme.max_sig_len());
            sig.verify_msg32(&msg, &pk).unwrap();

            let wrong_msg = [0xA5u8; 32];
            assert_eq!(sig.verify_msg32(&wrong_msg, &pk).unwrap_err(), PqError::VerificationFailed);
        }
    }

    #[test]
    fn sign_and_verify_msg64_all_schemes() {
        let msg = [0x33u8; 64];

        for scheme in [
            PqScheme::Falcon512,
            PqScheme::Falcon1024,
            PqScheme::MlDsa44,
            PqScheme::MlDsa65,
            PqScheme::MlDsa87,
        ] {
            let (pk, sk) = deterministic_keypair(scheme, scheme.prefix());
            let mut rng = test_rng(0x64 + u64::from(scheme.prefix()));
            let sig = PqSignature::sign_msg64_with_rng(&msg, &sk, &mut rng).unwrap();
            assert!(sig.len() <= scheme.max_sig_len());
            sig.verify_msg64(&msg, &pk).unwrap();

            let wrong_msg = [0xCCu8; 64];
            assert_eq!(sig.verify_msg64(&wrong_msg, &pk).unwrap_err(), PqError::VerificationFailed);
        }
    }

    #[test]
    fn corrupted_signatures_are_rejected_for_all_schemes() {
        let msg32 = [0x5Au8; 32];
        let msg64 = [0x33u8; 64];

        for scheme in all_signature_schemes() {
            let (pk, sk) = deterministic_keypair(scheme, scheme.prefix());

            let mut rng32 = test_rng(0x5100 + u64::from(scheme.prefix()));
            let sig32 = PqSignature::sign_msg32_with_rng(&msg32, &sk, &mut rng32).unwrap();
            let mut corrupted32 = sig32.as_bytes().to_vec();
            let last32 = corrupted32.last_mut().expect("signature should not be empty");
            *last32 ^= 0x01;
            let corrupted32 = PqSignature::from_slice(&corrupted32);
            assert!(
                corrupted32.verify_msg32(&msg32, &pk).is_err(),
                "{scheme:?} corrupted 32-byte-message signature verified"
            );

            let mut rng64 = test_rng(0x6400 + u64::from(scheme.prefix()));
            let sig64 = PqSignature::sign_msg64_with_rng(&msg64, &sk, &mut rng64).unwrap();
            let mut corrupted64 = sig64.as_bytes().to_vec();
            let last64 = corrupted64.last_mut().expect("signature should not be empty");
            *last64 ^= 0x01;
            let corrupted64 = PqSignature::from_slice(&corrupted64);
            assert!(
                corrupted64.verify_msg64(&msg64, &pk).is_err(),
                "{scheme:?} corrupted 64-byte-message signature verified"
            );
        }
    }

    #[test]
    fn cross_scheme_signatures_are_rejected_for_all_schemes() {
        let msg = [0xA7u8; 32];
        let schemes = all_signature_schemes();

        for signing_scheme in schemes {
            let (_, sk) = deterministic_keypair(signing_scheme, signing_scheme.prefix());
            let mut rng = test_rng(0x7700 + u64::from(signing_scheme.prefix()));
            let sig = PqSignature::sign_msg32_with_rng(&msg, &sk, &mut rng).unwrap();

            for verifying_scheme in schemes {
                if verifying_scheme == signing_scheme {
                    continue;
                }
                let (wrong_pk, _) =
                    deterministic_keypair(verifying_scheme, verifying_scheme.prefix() ^ 0x55);
                assert!(
                    sig.verify_msg32(&msg, &wrong_pk).is_err(),
                    "{signing_scheme:?} signature verified against {verifying_scheme:?} key"
                );
            }
        }
    }

    #[test]
    fn falcon512_legacy_signatures_roundtrip_for_msg32() {
        let msg = [0x42_u8; 32];
        let (pk, sk) = deterministic_keypair(PqScheme::Falcon512, 0x54);

        let strict = PqSignature::sign_msg32_with_rng(&msg, &sk, &mut test_rng(0x54)).unwrap();
        let legacy =
            PqSignature::sign_msg32_legacy_with_rng(&msg, &sk, &mut test_rng(0x55)).unwrap();

        assert_eq!(legacy.as_bytes().first().copied(), Some(0x39));
        assert!(legacy.len() <= 690);
        assert_ne!(legacy, strict);
        legacy.verify_msg32_legacy(&msg, &pk).unwrap();
        legacy.verify_msg32_allow_legacy(&msg, &pk).unwrap();
    }

    #[test]
    fn falcon512_legacy_signatures_roundtrip_for_msg64() {
        let msg = [0x24_u8; 64];
        let (pk, sk) = deterministic_keypair(PqScheme::Falcon512, 0x56);

        let legacy =
            PqSignature::sign_msg64_legacy_with_rng(&msg, &sk, &mut test_rng(0x56)).unwrap();

        assert_eq!(legacy.as_bytes().first().copied(), Some(0x39));
        assert!(legacy.len() <= 690);
        legacy.verify_msg64_legacy(&msg, &pk).unwrap();
        legacy.verify_msg64_allow_legacy(&msg, &pk).unwrap();
    }

    #[test]
    fn scheme_auxpow_gate_matches_node_policy() {
        let mut params = Params::MAINNET;
        params.auxpow_start_height = Some(crate::BlockHeight::from_u32(100));

        assert!(PqScheme::Falcon512.is_allowed_by_params_at_height(0, &params));
        assert!(!PqScheme::MlDsa44.is_allowed_by_params_at_height(0, &params));
        assert!(PqScheme::MlDsa44.is_allowed_by_params_at_height(100, &params));

        let no_auxpow = Params::MAINNET;
        assert!(PqScheme::Falcon512.is_allowed_by_params_at_height(1_000_000, &no_auxpow));
        assert!(!PqScheme::MlDsa87.is_allowed_by_params_at_height(1_000_000, &no_auxpow));
    }

    #[test]
    fn mlkem512_roundtrip() {
        let keypair = MlKem512Keypair::generate_with_rng(&mut test_rng(0x57));
        let (ciphertext, shared_secret) =
            keypair.public_key().encapsulate_with_rng(&mut test_rng(0x58)).unwrap();
        let recovered = keypair.secret_key().decapsulate(&ciphertext).unwrap();
        assert_eq!(recovered, shared_secret);
        assert_eq!(shared_secret.as_bytes().len(), MLKEM512_SHARED_SECRET_BYTES);
    }

    #[test]
    fn mlkem512_deterministic_keygen_matches() {
        let coins = [0x11_u8; MLKEM512_KEYPAIR_COINS_BYTES];
        let a = MlKem512Keypair::generate_deterministic(&coins).unwrap();
        let b = MlKem512Keypair::generate_deterministic(&coins).unwrap();
        assert_eq!(a, b);
    }

    #[test]
    fn mlkem512_deterministic_encapsulation_matches() {
        let coins = [0x22_u8; MLKEM512_KEYPAIR_COINS_BYTES];
        let enc_coins = [0x33_u8; MLKEM512_SHARED_SECRET_BYTES];
        let keypair = MlKem512Keypair::generate_deterministic(&coins).unwrap();

        let (ct1, ss1) = keypair.public_key().encapsulate_deterministic(&enc_coins).unwrap();
        let (ct2, ss2) = keypair.public_key().encapsulate_deterministic(&enc_coins).unwrap();
        let recovered = keypair.secret_key().decapsulate(&ct1).unwrap();

        assert_eq!(ct1, ct2);
        assert_eq!(ss1, ss2);
        assert_eq!(recovered, ss1);
    }

    #[cfg(feature = "tidecoin-node-validation")]
    #[test]
    fn mlkem512_deterministic_paths_match_node() {
        let harness = match node_parity::TidecoinNodeHarness::from_env() {
            Ok(harness) => harness,
            Err(err) => {
                std::eprintln!("skipping ML-KEM node-backed validation test: {err}");
                return;
            }
        };
        let coins = [0x44_u8; MLKEM512_KEYPAIR_COINS_BYTES];
        let enc_coins = [0x45_u8; MLKEM512_SHARED_SECRET_BYTES];

        let rust_pair = MlKem512Keypair::generate_deterministic(&coins).unwrap();
        let (node_pk, node_sk) = harness
            .mlkem512_keypair_from_coins(&coins, MLKEM512_PUBLICKEY_BYTES, MLKEM512_SECRETKEY_BYTES)
            .unwrap();
        assert_eq!(rust_pair.public_key().as_bytes(), node_pk.as_slice());
        assert_eq!(rust_pair.secret_key().as_bytes(), node_sk.as_slice());

        let (rust_ct, rust_ss) =
            rust_pair.public_key().encapsulate_deterministic(&enc_coins).unwrap();
        let (node_ct, node_ss) = harness
            .mlkem512_encaps_deterministic(
                rust_pair.public_key().as_bytes(),
                &enc_coins,
                MLKEM512_CIPHERTEXT_BYTES,
                MLKEM512_SHARED_SECRET_BYTES,
            )
            .unwrap();
        assert_eq!(rust_ct.as_bytes(), node_ct.as_slice());
        assert_eq!(rust_ss.as_bytes().as_slice(), node_ss.as_slice());

        let rust_recovered = rust_pair.secret_key().decapsulate(&rust_ct).unwrap();
        let node_recovered = harness
            .mlkem512_decaps(
                rust_ct.as_bytes(),
                rust_pair.secret_key().as_bytes(),
                MLKEM512_SHARED_SECRET_BYTES,
            )
            .unwrap();
        assert_eq!(rust_recovered.as_bytes().as_slice(), node_recovered.as_slice());
    }

    #[test]
    fn secret_types_require_drop_for_zeroization() {
        assert!(core::mem::needs_drop::<PqSecretKey>());
        assert!(core::mem::needs_drop::<MlKem512SecretKey>());
        assert!(core::mem::needs_drop::<MlKem512SharedSecret>());
    }
}