latticearc 0.8.1

Production-ready post-quantum cryptography. Hybrid ML-KEM+X25519 by default, all 4 NIST standards (FIPS 203–206), and FIPS 140-3 backend — one crate, zero unsafe.
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
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
#![deny(unsafe_code)]
#![deny(missing_docs)]
#![deny(clippy::unwrap_used)]
#![deny(clippy::panic)]

//! Hybrid Digital Signatures Module
//!
//! This module provides hybrid digital signatures that combine post-quantum
//! (ML-DSA) and classical (Ed25519) signature algorithms for enhanced security
//! during the quantum transition period.
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────────────────┐
//! │                    HYBRID SIGNATURE: Signing Flow                       │
//! ├─────────────────────────────────────────────────────────────────────────┤
//! │                                                                         │
//! │  ┌─────────────┐                                                        │
//! │  │   Message   │                                                        │
//! │  │     M       │                                                        │
//! │  └──────┬──────┘                                                        │
//! │         │                                                               │
//! │         ├────────────────────────────────────────┐                      │
//! │         │                                        │                      │
//! │         ▼                                        ▼                      │
//! │  ┌──────────────────────┐              ┌──────────────────────┐         │
//! │  │    ML-DSA-65 Sign    │              │   Ed25519 Sign       │         │
//! │  │                      │              │                      │         │
//! │  │  SK_pq + M ──► σ_pq  │              │  SK_ed + M ──► σ_ed  │         │
//! │  │    (3309 bytes)      │              │    (64 bytes)        │         │
//! │  └──────────┬───────────┘              └──────────┬───────────┘         │
//! │             │                                     │                     │
//! │             └────────────────┬────────────────────┘                     │
//! │                              │                                          │
//! │                              ▼                                          │
//! │                  ┌───────────────────────┐                              │
//! │                  │  Hybrid Signature     │                              │
//! │                  │  σ = σ_pq ║ σ_ed      │                              │
//! │                  │  (3309 + 64 = 3373 B) │                              │
//! │                  └───────────────────────┘                              │
//! │                                                                         │
//! └─────────────────────────────────────────────────────────────────────────┘
//!
//! ┌─────────────────────────────────────────────────────────────────────────┐
//! │                    HYBRID SIGNATURE: Verification Flow                  │
//! ├─────────────────────────────────────────────────────────────────────────┤
//! │                                                                         │
//! │  ┌─────────────┐         ┌───────────────────────┐                      │
//! │  │   Message   │         │  Hybrid Signature     │                      │
//! │  │     M       │         │  σ = σ_pq ║ σ_ed      │                      │
//! │  └──────┬──────┘         └───────────┬───────────┘                      │
//! │         │                            │                                  │
//! │         │  ┌─────────────────────────┴─────────────────────────┐        │
//! │         │  │                                                   │        │
//! │         │  │ Parse: first 3309 bytes = σ_pq, last 64 = σ_ed   │        │
//! │         │  │                                                   │        │
//! │         │  └──────────────────┬────────────────────────────────┘        │
//! │         │                     │                                         │
//! │         ├────────────────────┬┴───────────────────┐                     │
//! │         │                    │                    │                     │
//! │         ▼                    ▼                    ▼                     │
//! │  ┌────────────────┐   ┌────────────────┐   ┌────────────────┐           │
//! │  │ ML-DSA Verify  │   │                │   │ Ed25519 Verify │           │
//! │  │                │   │                │   │                │           │
//! │  │ PK_pq, M, σ_pq │   │     AND        │   │ PK_ed, M, σ_ed │           │
//! │  │       │        │   │                │   │       │        │           │
//! │  └───────┼────────┘   └───────┬────────┘   └───────┼────────┘           │
//! │          │                    │                    │                    │
//! │          ▼                    ▼                    ▼                    │
//! │       ┌─────┐             ┌──────┐             ┌─────┐                  │
//! │       │ OK? │─────────────┤ BOTH ├─────────────│ OK? │                  │
//! │       └─────┘             └──┬───┘             └─────┘                  │
//! │                              │                                          │
//! │                              ▼                                          │
//! │                     ┌────────────────┐                                  │
//! │                     │  Valid = true  │                                  │
//! │                     │  iff BOTH pass │                                  │
//! │                     └────────────────┘                                  │
//! │                                                                         │
//! └─────────────────────────────────────────────────────────────────────────┘
//! ```
//!
//! # Key Sizes Summary
//!
//! | Component   | Public Key | Secret Key | Signature  |
//! |-------------|------------|------------|------------|
//! | ML-DSA-65   | 1952 B     | 4032 B     | 3309 B     |
//! | Ed25519     | 32 B       | 32 B       | 64 B       |
//! | **Hybrid**  | **1984 B** | **4064 B** | **3373 B** |
//!
//! # Security Properties
//!
//! - **EUF-CMA** (Existential Unforgeability under Chosen Message Attack) security
//! - **AND-composition**: Requires breaking BOTH ML-DSA AND Ed25519 to forge
//! - Automatic memory zeroization for secret keys via [`ZeroizeOnDrop`]
//!
//! # Example
//!
//! ```rust,no_run
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! use latticearc::hybrid::sig_hybrid::{generate_keypair, sign, verify};
//!
//! // Generate hybrid keypair
//! let (pk, sk) = generate_keypair()?;
//!
//! // Sign a message (deterministic - no RNG needed)
//! let message = b"Hello, hybrid signatures!";
//! let signature = sign(&sk, message)?;
//!
//! // Verify the signature
//! let is_valid = verify(&pk, message, &signature)?;
//! assert!(is_valid);
//! # Ok(())
//! # }
//! ```
//!
//! [`ZeroizeOnDrop`]: zeroize::ZeroizeOnDrop

use subtle::ConstantTimeEq;
use thiserror::Error;
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};

use crate::log_crypto_operation_error;
use crate::primitives::ec::ed25519::{Ed25519KeyPair, Ed25519Signature as Ed25519SignatureOps};
use crate::primitives::ec::traits::{EcKeyPair, EcSignature};
use crate::primitives::sig::ml_dsa::{
    MlDsaParameterSet, MlDsaPublicKey, MlDsaSecretKey, MlDsaSignature,
    generate_keypair as ml_dsa_generate_keypair,
};
use crate::unified_api::logging::op;

/// Error types for hybrid signature operations.
///
/// This enum captures all possible error conditions that can occur during
/// hybrid signature generation and verification.
// PartialEq is intentionally not derived: crypto error types should be
// inspected by variant (`matches!`) rather than value-compared. The
// `String`-carrying variants would otherwise compare upstream error
// messages, which is too brittle.
#[non_exhaustive]
#[derive(Debug, Clone, Error)]
pub enum HybridSignatureError {
    /// Error during ML-DSA signature operations.
    #[error("ML-DSA error: {0}")]
    MlDsaError(String),
    /// Error during Ed25519 signature operations.
    #[error("Ed25519 error: {0}")]
    Ed25519Error(String),
    /// Signature verification failed for one or both components.
    #[error("Signature verification failed: {0}")]
    VerificationFailed(String),
    /// Invalid key material provided (wrong length, format, etc.).
    #[error("Invalid key material: {0}")]
    InvalidKeyMaterial(String),
    /// General cryptographic operation failure.
    #[error("Cryptographic operation failed: {0}")]
    CryptoError(String),
}

/// Hybrid public key combining ML-DSA and Ed25519 public keys.
///
/// Carries the [`MlDsaParameterSet`] that the ML-DSA half was generated
/// at, so [`verify`] can reconstruct the right ML-DSA public-key shape.
/// Earlier revisions hardcoded `MlDsa65` in `verify`, which silently
/// rejected keys produced by [`generate_keypair_with_parameter_set`]
/// when that function was called with `MlDsa44` or `MlDsa87` — see the
/// `test_hybrid_sig_all_parameter_sets_roundtrip` regression.
///
/// Both component signatures must verify for the hybrid signature to be valid.
#[derive(Debug, Clone)]
pub struct HybridSigPublicKey {
    /// ML-DSA parameter set this PK was generated at.
    parameter_set: MlDsaParameterSet,
    /// ML-DSA public key bytes (size depends on `parameter_set`).
    ml_dsa_pk: Vec<u8>,
    /// Ed25519 public key bytes (32 bytes).
    ed25519_pk: Vec<u8>,
}

impl HybridSigPublicKey {
    /// Construct a `HybridSigPublicKey` from its components, validating
    /// that each PK matches its parameter set's expected length.
    ///
    /// # Errors
    /// Returns `LatticeArcError::InvalidParameter` if `ml_dsa_pk.len()`
    /// does not match `parameter_set.public_key_size()`, or if
    /// `ed25519_pk.len() != 32`.
    pub fn new(
        parameter_set: MlDsaParameterSet,
        ml_dsa_pk: Vec<u8>,
        ed25519_pk: Vec<u8>,
    ) -> Result<Self, crate::prelude::LatticeArcError> {
        let expected_ml_dsa = parameter_set.public_key_size();
        if ml_dsa_pk.len() != expected_ml_dsa {
            return Err(crate::prelude::LatticeArcError::InvalidParameter(format!(
                "ML-DSA public key for {:?}: expected {} bytes, got {}",
                parameter_set,
                expected_ml_dsa,
                ml_dsa_pk.len()
            )));
        }
        if ed25519_pk.len() != 32 {
            return Err(crate::prelude::LatticeArcError::InvalidParameter(format!(
                "Ed25519 public key: expected 32 bytes, got {}",
                ed25519_pk.len()
            )));
        }
        Ok(Self { parameter_set, ml_dsa_pk, ed25519_pk })
    }

    /// Returns the ML-DSA parameter set this public key was generated at.
    #[must_use]
    pub fn parameter_set(&self) -> MlDsaParameterSet {
        self.parameter_set
    }

    /// Returns the ML-DSA public key bytes.
    #[must_use]
    pub fn ml_dsa_pk(&self) -> &[u8] {
        &self.ml_dsa_pk
    }

    /// Returns the Ed25519 public key bytes (32 bytes).
    #[must_use]
    pub fn ed25519_pk(&self) -> &[u8] {
        &self.ed25519_pk
    }
}

/// Hybrid secret key combining ML-DSA and Ed25519
///
/// # Security Guarantees
///
/// This struct implements automatic memory zeroization via the [`ZeroizeOnDrop`] derive.
/// When a `HybridSigSecretKey` is dropped (goes out of scope), all secret
/// key material is immediately overwritten with zeros using constant-time volatile writes.
/// This prevents secret material from remaining in memory after use.
///
/// # Zeroization Implementation
///
/// The [`ZeroizeOnDrop`] derive automatically calls [`Zeroize::zeroize()`]
/// on all fields when the struct is dropped. This happens using volatile
/// operations that prevent compiler optimization and ensure constant-time execution.
///
/// # Cloning
///
/// **Important**: This type does NOT implement [`Clone`] to prevent accidental
/// copying of secret keys. If you need to clone, you must implement it
/// explicitly with proper security considerations, including zeroizing the copy.
///
/// # Memory Safety
///
/// - All secret fields are wrapped in `Zeroizing<Vec<u8>>` for explicit zeroization
/// - Drop implementation ensures zeroization even on panic
/// - Constant-time operations prevent timing side-channels
///
/// # Example
///
/// ```rust,no_run
/// use latticearc::hybrid::sig_hybrid::generate_keypair;
///
/// // Generate keypair
/// let (pk, sk) = generate_keypair().expect("keypair generation failed");
///
/// //.. use sk for cryptographic operations..
///
/// // Drop secret key - automatically zeroized
/// drop(sk);  // Secret material automatically zeroized
/// ```
#[derive(Zeroize, ZeroizeOnDrop)]
pub struct HybridSigSecretKey {
    /// ML-DSA parameter set this SK was generated at. Read by [`sign`]
    /// to construct the right-sized `MlDsaSecretKey`. `MlDsaParameterSet`
    /// is `Copy` and contains no secret material, so `#[zeroize(skip)]`.
    #[zeroize(skip)]
    parameter_set: MlDsaParameterSet,
    /// ML-DSA secret key bytes (size depends on parameter set), automatically zeroized on drop.
    ml_dsa_sk: Zeroizing<Vec<u8>>,
    /// Ed25519 secret key bytes (32 bytes), automatically zeroized on drop.
    ed25519_sk: Zeroizing<Vec<u8>>,
}

impl std::fmt::Debug for HybridSigSecretKey {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // `parameter_set` is non-secret (algorithm identifier, identical to
        // what `HybridSigPublicKey`'s derived Debug surfaces). Surfacing it
        // is what makes parameter-set-mismatch debugging tractable — exactly
        // the bug class the v0.8.0 generic-over-MlDsaParameterSet break
        // was meant to make visible.
        f.debug_struct("HybridSigSecretKey")
            .field("parameter_set", &self.parameter_set)
            .field("data", &"[REDACTED]")
            .finish()
    }
}

impl ConstantTimeEq for HybridSigSecretKey {
    fn ct_eq(&self, other: &Self) -> subtle::Choice {
        // Parameter-set match is a precondition for equality. ML-DSA-44
        // SK bytes have a different length from ML-DSA-87 SK bytes, so a
        // length-mismatch slice ct_eq would already return Choice(0) —
        // but checking the parameter set explicitly fails earlier and
        // makes the contract loud.
        //
        // Use the canonical `(self as u8).ct_eq(...)` pattern (per
        // DESIGN_PATTERNS.md:793, Constant-Time Equality reference)
        // rather than `==`. The parameter set is a public parameter,
        // so the timing isn't security-sensitive HERE — but the
        // canonical pattern keeps the call shape uniform with the
        // secret-bearing legs below, so a future developer copying
        // this shape into a secret context starts from a CT-safe
        // primitive. `#[repr(u8)]` on MlDsaParameterSet makes the
        // cast well-defined.
        let param_eq = (self.parameter_set as u8).ct_eq(&(other.parameter_set as u8));
        param_eq
            & self.ml_dsa_sk.as_slice().ct_eq(other.ml_dsa_sk.as_slice())
            & self.ed25519_sk.as_slice().ct_eq(other.ed25519_sk.as_slice())
    }
}

impl HybridSigSecretKey {
    /// Construct a `HybridSigSecretKey` from its raw component bytes.
    ///
    /// `parameter_set` MUST match the ML-DSA strength used to generate
    /// `ml_dsa_sk`; [`sign`] reads it back to construct the right-sized
    /// `MlDsaSecretKey`. A mismatch surfaces at sign time as the opaque
    /// `MlDsaError` (no panic, no incorrect signing).
    ///
    /// Both component-byte arguments are wrapped in [`Zeroizing`] and
    /// will be wiped from memory when the returned [`HybridSigSecretKey`]
    /// is dropped.
    #[must_use]
    pub fn new(
        parameter_set: MlDsaParameterSet,
        ml_dsa_sk: Zeroizing<Vec<u8>>,
        ed25519_sk: Zeroizing<Vec<u8>>,
    ) -> Self {
        Self { parameter_set, ml_dsa_sk, ed25519_sk }
    }

    /// Returns the ML-DSA parameter set this secret key was generated at.
    #[must_use]
    pub fn parameter_set(&self) -> MlDsaParameterSet {
        self.parameter_set
    }

    /// Returns the ML-DSA secret key bytes wrapped in `Zeroizing`.
    ///
    /// Prefer [`expose_ml_dsa_secret`](Self::expose_ml_dsa_secret) (a borrowed
    /// `&[u8]`) for read-only access — it avoids an allocation. This owned
    /// accessor exists for sign/serialize paths and for tests that need to
    /// call `.zeroize()` on a fresh buffer.
    #[must_use]
    pub fn ml_dsa_sk_bytes(&self) -> Zeroizing<Vec<u8>> {
        Zeroizing::new((*self.ml_dsa_sk).clone())
    }

    /// Returns the Ed25519 secret key bytes wrapped in `Zeroizing`.
    ///
    /// Prefer [`expose_ed25519_secret`](Self::expose_ed25519_secret) (a
    /// borrowed `&[u8]`) for read-only access — it avoids an allocation.
    /// This owned accessor exists for sign/serialize paths and for tests
    /// that need to call `.zeroize()` on a fresh buffer.
    #[must_use]
    pub fn ed25519_sk_bytes(&self) -> Zeroizing<Vec<u8>> {
        Zeroizing::new((*self.ed25519_sk).clone())
    }

    /// Expose the ML-DSA secret key bytes.
    ///
    /// Sealed accessor per Secret Type Invariant I-8
    /// (`docs/SECRET_TYPE_INVARIANTS.md`). For multi-secret composites like
    /// `HybridSigSecretKey`, each secret component gets its own `expose_*_secret()`
    /// method so every secret access is still grep-able. Prefer
    /// [`ml_dsa_sk_bytes`](Self::ml_dsa_sk_bytes) when you need an owned,
    /// zeroizing copy.
    #[must_use]
    pub fn expose_ml_dsa_secret(&self) -> &[u8] {
        &self.ml_dsa_sk
    }

    /// Expose the Ed25519 secret key bytes.
    ///
    /// Sealed accessor per Secret Type Invariant I-8
    /// (`docs/SECRET_TYPE_INVARIANTS.md`). Prefer
    /// [`ed25519_sk_bytes`](Self::ed25519_sk_bytes) when you need an owned,
    /// zeroizing copy.
    #[must_use]
    pub fn expose_ed25519_secret(&self) -> &[u8] {
        &self.ed25519_sk
    }
}

/// Hybrid signature combining ML-DSA and Ed25519 signatures.
///
/// Both component signatures must be present and verify against their
/// respective public keys for the hybrid signature to be considered valid.
/// The signature data can be manually zeroized using the [`Zeroize`] trait.
///
/// [`Zeroize`]: zeroize::Zeroize
#[derive(Debug, Clone, Zeroize)]
pub struct HybridSignature {
    /// ML-DSA signature bytes (size depends on parameter set).
    ml_dsa_sig: Vec<u8>,
    /// Ed25519 signature bytes (64 bytes).
    ed25519_sig: Vec<u8>,
}

impl HybridSignature {
    /// Construct a hybrid signature from its component byte slices.
    #[must_use]
    pub fn new(ml_dsa_sig: Vec<u8>, ed25519_sig: Vec<u8>) -> Self {
        Self { ml_dsa_sig, ed25519_sig }
    }

    /// Borrow the ML-DSA signature component.
    #[must_use]
    pub fn ml_dsa_sig(&self) -> &[u8] {
        &self.ml_dsa_sig
    }

    /// Borrow the Ed25519 signature component.
    #[must_use]
    pub fn ed25519_sig(&self) -> &[u8] {
        &self.ed25519_sig
    }
}

/// Generate hybrid keypair at the default ML-DSA parameter set (`MlDsa65`,
/// NIST Level 3 / 192-bit security).
///
/// For other parameter sets (NIST Level 1 / `MlDsa44`, or NIST Level 5 /
/// `MlDsa87` — the latter required for CNSA 2.0), use
/// [`generate_keypair_with_parameter_set`].
///
/// # Errors
///
/// Returns an error if ML-DSA keypair generation fails or Ed25519 keypair
/// generation (including its pairwise consistency test) fails.
///
/// # Entropy source
/// ML-DSA and Ed25519 key generation route through the primitives layer,
/// which uses `OsRng` internally — callers cannot supply an external RNG.
#[must_use = "generated keypair must be stored or used"]
pub fn generate_keypair() -> Result<(HybridSigPublicKey, HybridSigSecretKey), HybridSignatureError>
{
    generate_keypair_with_parameter_set(MlDsaParameterSet::MlDsa65)
}

/// Generate a hybrid keypair at a specified ML-DSA parameter set.
///
/// `parameter_set` selects the ML-DSA strength of the post-quantum half of
/// the hybrid keypair. The Ed25519 half is unchanged across parameter sets
/// (Ed25519 has a single fixed strength).
///
/// | `parameter_set` | NIST level | Use case |
/// |-----------------|------------|----------|
/// | `MlDsa44`       | 1 (~128-bit) | size-constrained, lowest security tier |
/// | `MlDsa65`       | 3 (~192-bit) | default, balanced |
/// | `MlDsa87`       | 5 (~256-bit) | CNSA 2.0, highest security |
///
/// # Errors
///
/// Returns an error if ML-DSA keypair generation fails for the requested
/// parameter set, or if Ed25519 keypair generation (including its pairwise
/// consistency test) fails.
#[must_use = "generated keypair must be stored or used"]
pub fn generate_keypair_with_parameter_set(
    parameter_set: MlDsaParameterSet,
) -> Result<(HybridSigPublicKey, HybridSigSecretKey), HybridSignatureError> {
    let (ml_dsa_pk, ml_dsa_sk) = ml_dsa_generate_keypair(parameter_set)
        .map_err(|e| HybridSignatureError::MlDsaError(e.to_string()))?;

    // Generate Ed25519 keypair through the primitives wrapper so all
    // Ed25519 operations go through a single entry point. The wrapper
    // performs a pairwise consistency test before returning.
    let ed25519_kp = Ed25519KeyPair::generate()
        .map_err(|e| HybridSignatureError::Ed25519Error(e.to_string()))?;

    let ed25519_pk = ed25519_kp.public_key_bytes();
    // secret_key_bytes() returns Zeroizing<Vec<u8>>; move the inner buffer
    // into the HybridSigSecretKey so the wrapping Zeroizing is preserved.
    let ed25519_sk_zeroizing = ed25519_kp.secret_key_bytes();

    // route construction through the validating
    // `HybridSigPublicKey::new` instead of building
    // the struct literal directly. Direct construction here
    // bypassed the length-check L4 added — the production keygen
    // was the most-common construction path and the only one that
    // skipped the validator. The bytes here come from real
    // generate-keypair output so the validator never rejects in
    // practice; the `expect` documents the invariant.
    let pk = HybridSigPublicKey::new(
        parameter_set,
        ml_dsa_pk.as_bytes().to_vec(),
        ed25519_pk,
    )
    .map_err(|e| HybridSignatureError::InvalidKeyMaterial(format!(
        "internal invariant violated: generate produced a PK that failed length validation: {e}"
    )))?;

    let sk = HybridSigSecretKey {
        parameter_set,
        ml_dsa_sk: Zeroizing::new(ml_dsa_sk.expose_secret().to_vec()),
        ed25519_sk: ed25519_sk_zeroizing,
    };

    Ok((pk, sk))
}

/// Sign using hybrid signature scheme
///
/// Both ML-DSA and Ed25519 signing are deterministic, so no RNG is required.
///
/// # Errors
///
/// Returns an error if:
/// - The Ed25519 secret key is not exactly 32 bytes.
/// - ML-DSA secret key construction or signing fails.
/// - The Ed25519 secret key format is invalid for conversion.
pub fn sign(
    sk: &HybridSigSecretKey,
    message: &[u8],
) -> Result<HybridSignature, HybridSignatureError> {
    // Borrow for the length check instead of `ed25519_sk_bytes()` — the
    // latter allocates and zeroizes a fresh `Zeroizing<Vec<u8>>` on every
    // call just to read `.len()`, and this is the hot sign() path.
    if sk.expose_ed25519_secret().len() != 32 {
        return Err(HybridSignatureError::InvalidKeyMaterial(
            "Ed25519 secret key must be 32 bytes".to_string(),
        ));
    }

    // Sign with ML-DSA
    let ml_dsa_sk_bytes = sk.ml_dsa_sk_bytes();
    // Sign-side opacity (defense-in-depth per Pattern 6). SK is caller-side
    // state; failures here indicate a programmer / storage bug, but keep the
    // public error uniform to avoid exposing upstream detail.
    //
    // the prior attempt at this
    // wrapped the clone in `Zeroizing<Vec<u8>>` but then immediately
    // re-cloned to pass into `MlDsaSecretKey::new(.. Vec<u8>)`,
    // creating a second bare copy that would leak on the `new()` error
    // path. The proper fix is structural: `MlDsaSecretKey::new()` now
    // wraps its `Vec<u8>` argument in `Zeroizing` on entry, so a single
    // bare clone here is consumed-and-wiped on both success and error
    // paths. (The struct's `data` field is also `Zeroizing<Vec<u8>>`.)
    let ml_dsa_sk_struct = MlDsaSecretKey::new(sk.parameter_set, (*ml_dsa_sk_bytes).clone())
        .map_err(|_e| {
            log_crypto_operation_error!(op::HYBRID_SIGN, "ML-DSA SK init failed");
            HybridSignatureError::MlDsaError("signing failed".to_string())
        })?;
    let ml_dsa_sig = ml_dsa_sk_struct
        .sign(message, &[])
        .map_err(|_e| {
            log_crypto_operation_error!(op::HYBRID_SIGN, "ML-DSA sign failed");
            HybridSignatureError::MlDsaError("signing failed".to_string())
        })?
        .as_bytes()
        .to_vec();

    // Sign with Ed25519 via the primitives wrapper.
    let ed25519_sk_zeroizing = sk.ed25519_sk_bytes();
    let ed25519_keypair = Ed25519KeyPair::from_secret_key(ed25519_sk_zeroizing.as_slice())
        .map_err(|_e| {
            log_crypto_operation_error!(op::HYBRID_SIGN, "Ed25519 SK init failed");
            HybridSignatureError::Ed25519Error("signing failed".to_string())
        })?;
    // `Ed25519KeyPair::sign` is now fallible
    // (validate_signature_size). Message length is already gated by
    // the hybrid sig API above; this `?` is the boundary safety net.
    let ed25519_signature = ed25519_keypair.sign(message).map_err(|_e| {
        log_crypto_operation_error!(op::HYBRID_SIGN, "Ed25519 sign rejected");
        HybridSignatureError::Ed25519Error("signing failed".to_string())
    })?;
    let ed25519_sig = Ed25519SignatureOps::signature_bytes(&ed25519_signature);

    Ok(HybridSignature { ml_dsa_sig, ed25519_sig })
}

/// Verify using hybrid signature scheme
///
/// # Errors
///
/// Returns an error if:
/// - The Ed25519 public key is not exactly 32 bytes.
/// - The Ed25519 signature is not exactly 64 bytes.
/// - ML-DSA public key or signature construction fails.
/// - ML-DSA signature verification fails.
/// - The Ed25519 public key is invalid or signature verification fails.
pub fn verify(
    pk: &HybridSigPublicKey,
    message: &[u8],
    sig: &HybridSignature,
) -> Result<bool, HybridSignatureError> {
    // SECURITY: Verify BOTH components unconditionally with bitwise AND combination.
    // This preserves AND-security: a partial break of one component must not leak which
    // one failed, because distinct error paths (or early exit) turn AND into OR.
    //
    // Rules:
    // 1. No early return on a single component failure.
    // 2. No `&&` (short-circuit) — use `&` (bitwise) so both calls always execute.
    // 3. A SINGLE opaque error string regardless of which component failed.
    // 4. Structural parse failures collapse to bit=0 (same as verify-fail),
    //    matching FIPS 204 §5.3 Verify-returns-bool shape and the Ed25519
    //    sibling below. This removes the early-exit timing / variant oracle
    //    that would otherwise distinguish "malformed ML-DSA" from "Ed25519
    //    failed".
    //
    // No length pre-checks are performed here. Earlier revisions had four
    // distinguishable `InvalidKeyMaterial` returns for the four buffer
    // lengths; that surface let an adversary varying wire-supplied PK/sig
    // bytes enumerate which length got rejected. The match arms below
    // already collapse parse failures (which include length mismatches) to
    // bit = 0, which is indistinguishable from a verify failure — exactly
    // what Pattern 6 §"adversary-reachable paths" requires.

    // Timing equalizer — see `crate::hybrid::verify_equalizer` for the
    // full rationale. Briefly: select shape-correct bytes (real or
    // zero-byte dummy) and run `from_bytes` + `verify` once on the
    // selection so the wall-clock cost is identical for shape-fail
    // and verify-fail.
    //
    // Per-stage `tracing` was previously emitted under four distinct
    // sub-stage tags; that made the Pattern 6 returned-error opacity
    // reconstructable from the debug log. Operators still get a
    // single "verify failure occurred" event for alerting; the
    // granular sub-stage detail is intentionally dropped (see
    // `docs/DESIGN_PATTERNS.md` Pattern 6).
    let dummy = crate::hybrid::verify_equalizer::hybrid_verify_dummy_material(pk.parameter_set);

    let pq_pk_len = pk.parameter_set.public_key_size();
    let pq_sig_len = pk.parameter_set.signature_size();
    let pq_shape_ok = pk.ml_dsa_pk.len() == pq_pk_len && sig.ml_dsa_sig.len() == pq_sig_len;
    let (pq_pk_bytes, pq_sig_bytes): (&[u8], &[u8]) = if pq_shape_ok {
        (&pk.ml_dsa_pk, &sig.ml_dsa_sig)
    } else {
        (dummy.pq_pk.as_slice(), dummy.pq_sig.as_slice())
    };
    // Match-on-parse, never `?`-propagate: if `from_bytes` ever fails
    // (today unreachable for our zero-byte dummies of correct length,
    // but a future `fips204` release adding content validation could
    // change that), we still want the verify pipeline to run so the
    // wall-clock cost stays equal between shape-fail and verify-fail.
    //
    // When parse fails on the substituted bytes, we DO still run a
    // real verify against the equalizer's pre-parsed valid material
    // (`dummy.parsed`, post-85e2bd79e M1). This guarantees
    // verify-pipeline execution regardless of whether `from_bytes`
    // adds content validation downstream. The pre-parsed PK + sig
    // verify against an internal test message — content-dependent
    // verify timing is content-independent of the caller, which is
    // exactly the property we want from the equalizer.
    //
    // If `dummy.parsed` is `None` (init keygen+sign failed at module
    // load — extremely rare RNG/PCT path), we fall back to the
    // legacy `Ok(false)` skip. The bit is then computed via the
    // shape-check AND parse-check AND verify-check, so a degraded
    // equalizer never affects correctness — only the strength of the
    // timing-oracle countermeasure for the degraded parameter set.
    let parse_ok =
        MlDsaPublicKey::from_bytes(pq_pk_bytes, pk.parameter_set).and_then(|parsed_pk| {
            MlDsaSignature::from_bytes(pq_sig_bytes, pk.parameter_set)
                .map(|parsed_sig| (parsed_pk, parsed_sig))
        });
    // the previous shape was
    //   `Ok(parsed) => parsed.verify(..)`,
    //   `Err(_) => dummy.verify(..)`.
    // But `MlDsaPublicKey::verify` internally calls
    // `ml_dsa_NN::PublicKey::try_from_bytes`, which short-circuits with
    // `Err(VerificationError)` on structurally-invalid PKs (e.g.
    // all-zero) before any actual ML-DSA verify runs. So a shape-pass-
    // but-inner-parse-fail input paid only the cheap parse-fail cost
    // while a shape-pass-and-inner-parse-pass input paid the full
    // ML-DSA verify cost — a measurable timing oracle on the difference.
    // Now: ANY path that doesn't reach a successful real verify falls
    // through to the dummy verify so the wall-clock cost is equal
    // across all reject reasons.
    // pre-parsed material now goes through `parsed_or_init()`
    // which retries init when prior attempts produced None. The result is
    // a clone (cheap — public bytes only); we own it locally.
    let dummy_parsed = dummy.parsed_or_init();
    let ml_dsa_verify_result = match &parse_ok {
        Ok((parsed_pk, parsed_sig)) => {
            let inner = parsed_pk.verify(message, parsed_sig, &[]);
            match &inner {
                Ok(_) => inner,
                // Inner parse failed (structurally-invalid PK reached
                // the short-circuit in `try_from_bytes`). Run the
                // equalizer dummy verify to spend verify-time budget;
                // discard its result.
                Err(_) => match &dummy_parsed {
                    Some(parsed) => {
                        let _ = parsed.pq_pk.verify(&parsed.pq_test_message, &parsed.pq_sig, &[]);
                        // Bubble the original Err so `matches!(., Ok(true))`
                        // below still rejects the substituted input.
                        inner
                    }
                    None => inner,
                },
            }
        }
        Err(_) => match &dummy_parsed {
            // Equalizer fallback: run verify against pre-validated
            // material so the wall-clock cost stays equal between
            // shape-fail-then-parse-fail and shape-good-then-verify.
            // The result is discarded by the AND with `parse_ok.is_ok()`
            // below — its only purpose is to consume verify-time
            // wall-clock budget.
            Some(parsed) => parsed.pq_pk.verify(&parsed.pq_test_message, &parsed.pq_sig, &[]),
            // Init keygen failed (extremely rare; retries
            // on every call). Equalizer degraded; legacy fast-fail
            // behavior. Not a correctness regression because the bit
            // is computed below via AND with multiple guards.
            None => Ok(false),
        },
    };
    // Bit is 1 only when the real input passed shape-check, both
    // `from_bytes` calls succeeded, AND the verify returned Ok(true).
    // When shape failed, the verify still ran (against the dummy or
    // against pre-parsed material) for timing equalization; its
    // result is discarded by the AND with `pq_shape_ok`.
    let ml_dsa_valid: u8 =
        if pq_shape_ok && parse_ok.is_ok() && matches!(ml_dsa_verify_result, Ok(true)) {
            1u8
        } else {
            0u8
        };

    // the Ed25519 leg now has a real verify-time
    // equalizer. The previous shape claimed parse cost was "in the
    // same order of magnitude as verify" — empirically wrong (parse
    // is a 64-byte length check, verify is one EC scalar mul; ~3
    // orders of magnitude apart). On parse-fail, run verify against
    // pre-parsed cached material so the wall-clock cost matches a
    // real verify, mirroring the ML-DSA equalizer above.
    let ed_dummy = crate::hybrid::verify_equalizer::ed25519_verify_dummy_material();
    let ed_dummy_parsed = ed_dummy.parsed_or_init();
    let ed25519_valid: u8 = if let Ok(ed25519_signature) =
        Ed25519SignatureOps::signature_from_bytes(sig.ed25519_sig.as_slice())
    {
        // Wrong-length PK short-circuits inside
        // `Ed25519SignatureOps::verify` before any scalar mul runs,
        // distinguishing "32-byte PK, bad sig" from "wrong-length PK"
        // by wall-clock. Run the dummy verify against parsed dummy
        // material on that path so the wall-clock matches the
        // valid-PK code path.
        let pk_bytes = pk.ed25519_pk.as_slice();
        let result = Ed25519SignatureOps::verify(pk_bytes, message, &ed25519_signature);
        if pk_bytes.len() != 32
            && let Some(parsed) = &ed_dummy_parsed
            && let Ok(parsed_sig) =
                Ed25519SignatureOps::signature_from_bytes(parsed.ed_sig.as_slice())
        {
            // Discarded; runs the EC scalar mul.
            let _ = Ed25519SignatureOps::verify(
                parsed.ed_pk.as_slice(),
                parsed.ed_test_message.as_slice(),
                &parsed_sig,
            );
        }
        match result {
            Ok(()) => 1u8,
            _ => 0u8,
        }
    } else {
        // Parse failure on caller-supplied bytes: still spend a
        // verify-cost worth of cycles against the dummy material so
        // the wall-clock between parse-fail and verify-fail is equal.
        // Discard the result — `0u8` is the verdict because the
        // caller's input was structurally wrong.
        if let Some(parsed) = &ed_dummy_parsed
            && let Ok(parsed_sig) =
                Ed25519SignatureOps::signature_from_bytes(parsed.ed_sig.as_slice())
        {
            // Discarded; runs the EC scalar mul.
            let _ = Ed25519SignatureOps::verify(
                parsed.ed_pk.as_slice(),
                parsed.ed_test_message.as_slice(),
                &parsed_sig,
            );
        }
        // If `ed_dummy_parsed` is None (RNG/PCT init failure —
        // retries on every call), fall through to the
        // legacy fast-fail. Bit is computed below via AND with
        // `pq_shape_ok` and `parse_ok.is_ok()`, so a degraded
        // equalizer cannot affect correctness.
        0u8
    };

    // One generic event on aggregate failure — preserves the "something
    // went wrong with hybrid verify" alerting signal without leaking
    // which component(s) failed.
    if (ml_dsa_valid & ed25519_valid) != 1 {
        log_crypto_operation_error!(op::HYBRID_VERIFY, "hybrid signature verification failed");
    }

    // The two component bits are combined via bitwise AND (not short-circuit
    // `&&`) so verification work is identical regardless of which component
    // failed first. The branch below on `both_valid` does not leak anything
    // the return value doesn't already carry — pass/fail is observable from
    // the caller either way.
    let both_valid: u8 = ml_dsa_valid & ed25519_valid;
    if both_valid != 1 {
        return Err(HybridSignatureError::VerificationFailed(
            "hybrid signature verification failed".to_string(),
        ));
    }

    // Both signatures verified successfully
    Ok(true)
}

#[cfg(test)]
#[expect(clippy::unwrap_used, reason = "Tests use unwrap for simplicity")]
#[expect(clippy::expect_used, reason = "Tests use expect for simplicity")]
#[expect(clippy::indexing_slicing, reason = "Tests use direct indexing for simplicity")]
#[expect(clippy::single_match, reason = "Match with comment is clearer than if-let in tests")]
mod tests {
    use super::*;

    #[test]
    fn test_hybrid_secret_key_zeroization_succeeds() {
        let (_pk, mut sk) = generate_keypair().unwrap();

        let ml_dsa_sk_before = sk.ml_dsa_sk_bytes().to_vec();
        let ed25519_sk_before = sk.ed25519_sk_bytes().to_vec();

        assert!(
            !ml_dsa_sk_before.iter().all(|&b| b == 0),
            "ML-DSA secret should contain non-zero data"
        );
        assert!(
            !ed25519_sk_before.iter().all(|&b| b == 0),
            "Ed25519 secret should contain non-zero data"
        );

        sk.zeroize();

        assert!(sk.ml_dsa_sk_bytes().iter().all(|&b| b == 0), "ML-DSA secret should be zeroized");
        assert!(sk.ed25519_sk_bytes().iter().all(|&b| b == 0), "Ed25519 secret should be zeroized");
    }

    #[test]
    fn test_hybrid_secret_key_drop_zeroization_succeeds() {
        let test_ml_data = vec![0x77; 4032];
        let test_ed25519_data = vec![0x66; 32];

        {
            let sk = HybridSigSecretKey {
                parameter_set: MlDsaParameterSet::MlDsa65,
                ml_dsa_sk: Zeroizing::new(test_ml_data),
                ed25519_sk: Zeroizing::new(test_ed25519_data),
            };

            assert!(
                !sk.ml_dsa_sk_bytes().iter().all(|&b| b == 0),
                "ML-DSA secret should contain non-zero data"
            );
            assert!(
                !sk.ed25519_sk_bytes().iter().all(|&b| b == 0),
                "Ed25519 secret should contain non-zero data"
            );
        }
    }

    #[test]
    fn test_hybrid_signature_after_zeroization_succeeds() {
        let (pk, mut sk) = generate_keypair().unwrap();
        let message = b"Test message";

        let signature_before = sign(&sk, message).expect("Should sign before zeroization");
        let valid_before =
            verify(&pk, message, &signature_before).expect("Should verify before zeroization");
        assert!(valid_before, "Signature should be valid before zeroization");

        sk.zeroize();

        let result = sign(&sk, message);
        assert!(result.is_err(), "Signing should fail after zeroization");
    }

    #[test]
    fn test_hybrid_signature_keypair_generation_succeeds() {
        let (pk, sk) = generate_keypair().unwrap();

        assert!(!pk.ml_dsa_pk.is_empty(), "ML-DSA public key should not be empty");
        assert_eq!(pk.ed25519_pk.len(), 32, "Ed25519 public key should be 32 bytes");
        assert!(!sk.ml_dsa_sk.is_empty(), "ML-DSA secret key should not be empty");
        assert_eq!(sk.ed25519_sk.len(), 32, "Ed25519 secret key should be 32 bytes");

        assert!(!pk.ml_dsa_pk.iter().all(|&x| x == 0), "ML-DSA PK should not be all zeros");
        assert!(!pk.ed25519_pk.iter().all(|&x| x == 0), "Ed25519 PK should not be all zeros");
    }

    #[test]
    fn test_hybrid_signature_signing_and_verification_succeeds() {
        let (pk, sk) = generate_keypair().unwrap();

        let message = b"Hello, hybrid signature!";
        let sig = sign(&sk, message);
        assert!(sig.is_ok(), "Signing should succeed");

        let sig = sig.unwrap();
        assert!(!sig.ml_dsa_sig.is_empty(), "ML-DSA signature should not be empty");
        assert_eq!(sig.ed25519_sig.len(), 64, "Ed25519 signature should be 64 bytes");

        let valid = verify(&pk, message, &sig);
        assert!(valid.is_ok(), "Verification should succeed");
        assert!(valid.unwrap(), "Signature should be valid");
    }

    #[test]
    fn test_invalid_key_and_signature_lengths_all_return_error_fails() {
        let (pk, _sk) = generate_keypair().unwrap();

        // Test with invalid Ed25519 public key length
        let mut invalid_pk = pk.clone();
        invalid_pk.ed25519_pk = vec![1u8; 31]; // Wrong length
        let sig = HybridSignature { ml_dsa_sig: vec![1u8; 100], ed25519_sig: vec![1u8; 64] };
        let result = verify(&invalid_pk, b"test", &sig);
        assert!(result.is_err(), "Should reject invalid public key length");

        // Test with invalid signature length
        let invalid_sig = HybridSignature {
            ml_dsa_sig: vec![1u8; 100],
            ed25519_sig: vec![1u8; 63], // Wrong length
        };
        let result = verify(&pk, b"test", &invalid_sig);
        assert!(result.is_err(), "Should reject invalid signature length");
    }

    #[test]
    fn test_ed25519_signature_properties_are_correct() {
        // Exercise the primitives-layer Ed25519 wrapper end-to-end to keep
        // this module's tests self-contained without reaching into dalek.
        let keypair = Ed25519KeyPair::generate().expect("keypair generation should succeed");
        let public_key_bytes = keypair.public_key_bytes();

        let message = b"Test message";
        let signature = keypair.sign(message).expect("sign should succeed");

        // Valid signature should verify
        let result = Ed25519SignatureOps::verify(&public_key_bytes, message, &signature);
        assert!(result.is_ok(), "Valid signature should verify");

        // Wrong message should not verify
        let wrong_message = b"Wrong message";
        let result = Ed25519SignatureOps::verify(&public_key_bytes, wrong_message, &signature);
        assert!(result.is_err(), "Wrong message should not verify");
    }

    #[test]
    fn test_hybrid_signature_zeroization_succeeds() {
        let ml_dsa_sig_data = vec![0x77; 2420];
        let ed25519_sig_data = vec![0x88; 64];

        let mut signature =
            HybridSignature { ml_dsa_sig: ml_dsa_sig_data, ed25519_sig: ed25519_sig_data };

        assert!(
            !signature.ml_dsa_sig.iter().all(|&b| b == 0),
            "ML-DSA signature should contain non-zero data"
        );
        assert!(
            !signature.ed25519_sig.iter().all(|&b| b == 0),
            "Ed25519 signature should contain non-zero data"
        );

        signature.zeroize();

        assert!(
            signature.ml_dsa_sig.iter().all(|&b| b == 0),
            "ML-DSA signature should be zeroized"
        );
        assert!(
            signature.ed25519_sig.iter().all(|&b| b == 0),
            "Ed25519 signature should be zeroized"
        );
    }

    #[test]
    fn test_hybrid_keypair_zeroization_succeeds() {
        let (_public_key, secret_key) = generate_keypair().expect("Should generate hybrid keypair");

        assert!(
            !secret_key.ml_dsa_sk.iter().all(|&b| b == 0),
            "Keypair ML-DSA secret should contain non-zero data"
        );
        assert!(
            !secret_key.ed25519_sk.iter().all(|&b| b == 0),
            "Keypair Ed25519 secret should contain non-zero data"
        );

        let mut secret_key_clone = HybridSigSecretKey {
            parameter_set: secret_key.parameter_set,
            ml_dsa_sk: secret_key.ml_dsa_sk_bytes(),
            ed25519_sk: secret_key.ed25519_sk_bytes(),
        };

        secret_key_clone.zeroize();

        assert!(
            secret_key_clone.ml_dsa_sk.iter().all(|&b| b == 0),
            "Cloned ML-DSA secret should be zeroized"
        );
        assert!(
            secret_key_clone.ed25519_sk.iter().all(|&b| b == 0),
            "Cloned Ed25519 secret should be zeroized"
        );
    }

    #[test]
    fn test_hybrid_zeroization_order_is_correct() {
        let mut secret_key1 = HybridSigSecretKey {
            parameter_set: MlDsaParameterSet::MlDsa44,
            ml_dsa_sk: Zeroizing::new(vec![0x11; 2560]),
            ed25519_sk: Zeroizing::new(vec![0x22; 32]),
        };

        let mut secret_key2 = HybridSigSecretKey {
            parameter_set: MlDsaParameterSet::MlDsa44,
            ml_dsa_sk: Zeroizing::new(vec![0x33; 2560]),
            ed25519_sk: Zeroizing::new(vec![0x44; 32]),
        };

        assert!(
            !secret_key1.ml_dsa_sk.iter().all(|&b| b == 0),
            "Key1 ML-DSA secret should contain non-zero data"
        );
        assert!(
            !secret_key1.ed25519_sk.iter().all(|&b| b == 0),
            "Key1 Ed25519 secret should contain non-zero data"
        );
        assert!(
            !secret_key2.ml_dsa_sk.iter().all(|&b| b == 0),
            "Key2 ML-DSA secret should contain non-zero data"
        );
        assert!(
            !secret_key2.ed25519_sk.iter().all(|&b| b == 0),
            "Key2 Ed25519 secret should contain non-zero data"
        );

        secret_key1.ml_dsa_sk.zeroize();

        assert!(
            secret_key1.ml_dsa_sk.iter().all(|&b| b == 0),
            "Key1 ML-DSA secret should be zeroized first"
        );
        assert!(
            !secret_key1.ed25519_sk.iter().all(|&b| b == 0),
            "Key1 Ed25519 secret should still contain data"
        );
        assert!(
            !secret_key2.ml_dsa_sk.iter().all(|&b| b == 0),
            "Key2 ML-DSA secret should still contain data"
        );

        secret_key1.ed25519_sk.zeroize();

        assert!(
            secret_key1.ed25519_sk.iter().all(|&b| b == 0),
            "Key1 Ed25519 secret should be zeroized second"
        );
        assert!(
            !secret_key2.ml_dsa_sk.iter().all(|&b| b == 0),
            "Key2 ML-DSA secret should still contain data"
        );

        secret_key2.ml_dsa_sk.zeroize();
        secret_key2.ed25519_sk.zeroize();

        assert!(
            secret_key2.ml_dsa_sk.iter().all(|&b| b == 0),
            "Key2 ML-DSA secret should be zeroized"
        );
        assert!(
            secret_key2.ed25519_sk.iter().all(|&b| b == 0),
            "Key2 Ed25519 secret should be zeroized"
        );
    }

    #[test]
    fn test_hybrid_concurrent_zeroization_succeeds() {
        use std::sync::Arc;
        use std::thread;

        let ml_dsa_data = Arc::new(vec![0x99; 2560]);
        let ed25519_data = Arc::new(vec![0xAA; 32]);
        let mut handles = vec![];

        for i in 0..4 {
            let ml_dsa_clone = Arc::clone(&ml_dsa_data);
            let ed25519_clone = Arc::clone(&ed25519_data);

            let handle = thread::spawn(move || {
                let mut secret_key = HybridSigSecretKey {
                    parameter_set: MlDsaParameterSet::MlDsa65,
                    ml_dsa_sk: Zeroizing::new((*ml_dsa_clone).clone()),
                    ed25519_sk: Zeroizing::new((*ed25519_clone).clone()),
                };

                assert!(
                    !secret_key.ml_dsa_sk.iter().all(|&b| b == 0),
                    "Thread {} ML-DSA secret should contain non-zero data",
                    i
                );
                assert!(
                    !secret_key.ed25519_sk.iter().all(|&b| b == 0),
                    "Thread {} Ed25519 secret should contain non-zero data",
                    i
                );

                secret_key.zeroize();

                let ml_dsa_zeroized = secret_key.ml_dsa_sk.iter().all(|&b| b == 0);
                let ed25519_zeroized = secret_key.ed25519_sk.iter().all(|&b| b == 0);

                (i, ml_dsa_zeroized, ed25519_zeroized)
            });

            handles.push(handle);
        }

        for handle in handles {
            let (thread_id, ml_dsa_zeroized, ed25519_zeroized) =
                handle.join().expect("Thread should complete");
            assert!(ml_dsa_zeroized, "Thread {} ML-DSA secret should be zeroized", thread_id);
            assert!(ed25519_zeroized, "Thread {} Ed25519 secret should be zeroized", thread_id);
        }
    }

    // --- Additional coverage tests ---

    #[test]
    fn test_hybrid_verify_wrong_message_fails() {
        let (pk, sk) = generate_keypair().unwrap();

        let sig = sign(&sk, b"Original message").unwrap();
        let result = verify(&pk, b"Different message", &sig);
        // ML-DSA verify will return error for wrong message
        assert!(result.is_err(), "Wrong message should fail verification");
    }

    #[test]
    fn test_hybrid_verify_wrong_key_fails() {
        let (_pk1, sk1) = generate_keypair().unwrap();
        let (pk2, _sk2) = generate_keypair().unwrap();

        let sig = sign(&sk1, b"Test").unwrap();
        let result = verify(&pk2, b"Test", &sig);
        assert!(result.is_err(), "Wrong public key should fail verification");
    }

    #[test]
    fn test_hybrid_sign_invalid_ed25519_sk_length_returns_error() {
        let sk = HybridSigSecretKey {
            parameter_set: MlDsaParameterSet::MlDsa65,
            ml_dsa_sk: Zeroizing::new(vec![0u8; 4032]),
            ed25519_sk: Zeroizing::new(vec![0u8; 16]), // Wrong: should be 32
        };
        let result = sign(&sk, b"test");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, HybridSignatureError::InvalidKeyMaterial(_)));
    }

    #[test]
    fn test_hybrid_verify_invalid_ed25519_pk_length_returns_error() {
        let pk = HybridSigPublicKey {
            parameter_set: MlDsaParameterSet::MlDsa65,
            ml_dsa_pk: vec![0u8; 1952],
            ed25519_pk: vec![0u8; 16], // Wrong: should be 32
        };
        let sig = HybridSignature { ml_dsa_sig: vec![0u8; 3309], ed25519_sig: vec![0u8; 64] };
        let result = verify(&pk, b"test", &sig);
        // Pattern 6 (#52): verify no longer distinguishes length errors
        // from verify failures; both collapse to VerificationFailed via
        // the bit=0 path. Assert the surviving variant.
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(matches!(err, HybridSignatureError::VerificationFailed(_)));
    }

    #[test]
    fn test_hybrid_verify_invalid_ed25519_sig_length_returns_error() {
        let (pk, _sk) = generate_keypair().unwrap();

        let sig = HybridSignature {
            ml_dsa_sig: vec![0u8; 3309],
            ed25519_sig: vec![0u8; 32], // Wrong: should be 64
        };
        let result = verify(&pk, b"test", &sig);
        assert!(result.is_err());
        let err = result.unwrap_err();
        // Pattern 6 (#52): see test above — length mismatches collapse into
        // the bit=0 verify path, so the surviving variant is VerificationFailed.
        assert!(matches!(err, HybridSignatureError::VerificationFailed(_)));
    }

    #[test]
    fn test_hybrid_signature_error_display_all_variants_have_correct_format_fails() {
        let err1 = HybridSignatureError::MlDsaError("dsa fail".to_string());
        assert!(err1.to_string().contains("dsa fail"));

        let err2 = HybridSignatureError::Ed25519Error("ed fail".to_string());
        assert!(err2.to_string().contains("ed fail"));

        let err3 = HybridSignatureError::VerificationFailed("verify fail".to_string());
        assert!(err3.to_string().contains("verify fail"));

        let err4 = HybridSignatureError::InvalidKeyMaterial("bad key".to_string());
        assert!(err4.to_string().contains("bad key"));

        let err5 = HybridSignatureError::CryptoError("crypto fail".to_string());
        assert!(err5.to_string().contains("crypto fail"));
    }

    #[test]
    fn test_hybrid_signature_error_clone_round_trips() {
        let err1 = HybridSignatureError::MlDsaError("test".to_string());
        let err2 = err1.clone();
        assert_eq!(err1.to_string(), err2.to_string());
        assert!(matches!(err2, HybridSignatureError::MlDsaError(_)));
        assert!(!matches!(err1, HybridSignatureError::Ed25519Error(_)));
    }

    #[test]
    fn test_hybrid_public_key_clone_debug_succeeds() {
        let (pk, _sk) = generate_keypair().unwrap();

        let pk2 = pk.clone();
        assert_eq!(pk.ml_dsa_pk, pk2.ml_dsa_pk);
        assert_eq!(pk.ed25519_pk, pk2.ed25519_pk);

        let debug = format!("{:?}", pk);
        assert!(debug.contains("HybridSigPublicKey"));
    }

    #[test]
    fn test_hybrid_secret_key_debug_has_correct_format() {
        let (_pk, sk) = generate_keypair().unwrap();

        let debug = format!("{:?}", sk);
        assert!(debug.contains("HybridSigSecretKey"));
    }

    #[test]
    fn test_hybrid_signature_clone_debug_succeeds() {
        let sig = HybridSignature { ml_dsa_sig: vec![1, 2, 3], ed25519_sig: vec![4, 5, 6] };
        let sig2 = sig.clone();
        assert_eq!(sig.ml_dsa_sig, sig2.ml_dsa_sig);
        assert_eq!(sig.ed25519_sig, sig2.ed25519_sig);

        let debug = format!("{:?}", sig);
        assert!(debug.contains("HybridSignature"));
    }

    #[test]
    fn test_sign_same_key_consistent_ed25519_succeeds() {
        let (_pk, sk) = generate_keypair().unwrap();

        let message = b"Consistency test";
        let sig1 = sign(&sk, message).unwrap();
        let sig2 = sign(&sk, message).unwrap();

        // Ed25519 signing is deterministic (RFC 8032)
        assert_eq!(sig1.ed25519_sig, sig2.ed25519_sig, "Ed25519 sig should be deterministic");
        // ML-DSA uses hedged randomness — signatures may differ but both verify
        assert!(!sig1.ml_dsa_sig.is_empty());
        assert!(!sig2.ml_dsa_sig.is_empty());
    }

    #[test]
    fn test_sign_different_messages_produces_unique_sigs_are_unique() {
        let (_pk, sk) = generate_keypair().unwrap();

        let sig1 = sign(&sk, b"Message A").unwrap();
        let sig2 = sign(&sk, b"Message B").unwrap();

        assert_ne!(sig1.ml_dsa_sig, sig2.ml_dsa_sig);
        assert_ne!(sig1.ed25519_sig, sig2.ed25519_sig);
    }

    #[test]
    fn test_sign_empty_message_succeeds() {
        let (pk, sk) = generate_keypair().unwrap();

        let sig = sign(&sk, b"").unwrap();
        let valid = verify(&pk, b"", &sig).unwrap();
        assert!(valid, "Empty message signature should verify");
    }

    #[test]
    fn test_sign_large_message_succeeds() {
        let (pk, sk) = generate_keypair().unwrap();

        // 50 KiB: large enough to exercise multi-block message handling,
        // below the default max_signature_size_bytes (64 KiB) resource cap
        // enforced by the primitive sign path.
        let large_message = vec![0xABu8; 50 * 1024];
        let sig = sign(&sk, &large_message).unwrap();
        let valid = verify(&pk, &large_message, &sig).unwrap();
        assert!(valid, "Large message signature should verify");
    }

    // ========================================================================
    // Additional coverage: verify error paths
    // ========================================================================

    #[test]
    fn test_verify_wrong_key_pair_ml_dsa_fails() {
        let (pk1, _sk1) = generate_keypair().unwrap();
        let (_pk2, sk2) = generate_keypair().unwrap();

        // Sign with sk2, verify with pk1 — ML-DSA component should fail
        let sig = sign(&sk2, b"test msg").unwrap();
        let result = verify(&pk1, b"test msg", &sig);
        // ML-DSA verify returns Err for wrong key pair
        assert!(result.is_err(), "Verification with wrong key pair must fail");
    }

    #[test]
    fn test_verify_corrupted_ed25519_signature_fails() {
        let (pk, sk) = generate_keypair().unwrap();
        let message = b"Test message";

        let mut sig = sign(&sk, message).unwrap();
        // Corrupt only the Ed25519 signature (leave ML-DSA intact)
        sig.ed25519_sig[0] ^= 0xFF;

        let result = verify(&pk, message, &sig);
        // Should fail: either error or false
        match result {
            Ok(valid) => assert!(!valid, "Corrupted Ed25519 sig must not verify"),
            Err(_) => {} // Error is also acceptable
        }
    }

    #[test]
    fn test_verify_invalid_ml_dsa_sig_length_returns_error() {
        let (pk, _sk) = generate_keypair().unwrap();

        let sig = HybridSignature {
            ml_dsa_sig: vec![0u8; 100], // Wrong length for MlDsa65
            ed25519_sig: vec![0u8; 64],
        };

        let result = verify(&pk, b"test", &sig);
        assert!(result.is_err());
    }

    #[test]
    fn test_verify_invalid_ml_dsa_pk_length_returns_error() {
        let (_pk, sk) = generate_keypair().unwrap();
        let sig = sign(&sk, b"test").unwrap();

        let bad_pk = HybridSigPublicKey {
            parameter_set: MlDsaParameterSet::MlDsa65,
            ml_dsa_pk: vec![0u8; 100], // Wrong length
            ed25519_pk: vec![0u8; 32],
        };

        let result = verify(&bad_pk, b"test", &sig);
        assert!(result.is_err());
    }

    /// Regression test for the parameter-set propagation bug: prior to
    /// 0.8.0 the hardcoded `MlDsaParameterSet::MlDsa65` in `sign` and
    /// `verify` silently broke any keypair generated via
    /// `generate_keypair_with_parameter_set(MlDsa44)` or `(MlDsa87)` —
    /// the public API advertised three parameter sets but only one
    /// actually round-tripped. This test pins all three working.
    #[test]
    fn test_hybrid_sig_all_parameter_sets_roundtrip() {
        for param_set in
            [MlDsaParameterSet::MlDsa44, MlDsaParameterSet::MlDsa65, MlDsaParameterSet::MlDsa87]
        {
            let (pk, sk) = generate_keypair_with_parameter_set(param_set)
                .expect("keypair gen must succeed for every advertised parameter set");
            assert_eq!(pk.parameter_set(), param_set);
            assert_eq!(sk.parameter_set(), param_set);
            let message = b"parameter-set propagation regression";
            let sig = sign(&sk, message).expect("sign must succeed");
            let valid = verify(&pk, message, &sig).expect("verify must succeed");
            assert!(valid, "round-trip must verify for {param_set:?}");
        }
    }

    #[test]
    fn test_sign_verify_multiple_messages_same_key_succeeds() {
        let (pk, sk) = generate_keypair().unwrap();

        let messages: Vec<&[u8]> = vec![b"msg1", b"msg2", b"msg3"];
        let sigs: Vec<_> = messages.iter().map(|m| sign(&sk, m).unwrap()).collect();

        // Each signature should verify with its own message
        for (msg, sig) in messages.iter().zip(sigs.iter()) {
            assert!(verify(&pk, msg, sig).unwrap());
        }

        // Cross-verify should fail
        assert!(!verify(&pk, b"msg1", &sigs[1]).unwrap_or(false));
    }

    #[test]
    fn test_hybrid_signature_clone_succeeds() {
        let (_pk, sk) = generate_keypair().unwrap();
        let sig = sign(&sk, b"clone test").unwrap();
        let cloned = sig.clone();
        assert_eq!(sig.ml_dsa_sig, cloned.ml_dsa_sig);
        assert_eq!(sig.ed25519_sig, cloned.ed25519_sig);
    }

    #[test]
    fn test_hybrid_public_key_ml_dsa_has_correct_size() {
        let (pk, _sk) = generate_keypair().unwrap();
        assert_eq!(pk.ml_dsa_pk.len(), 1952); // MlDsa65 public key size
        assert_eq!(pk.ed25519_pk.len(), 32);
    }
}