libsodium-rs 0.2.4

A comprehensive, idiomatic Rust wrapper for libsodium, providing a safe and ergonomic API for cryptographic operations
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
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
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
//! # Digital Signatures
//!
//! This module provides functions for creating and verifying digital signatures using
//! the Ed25519 signature scheme, which is based on the Edwards-curve Digital Signature
//! Algorithm (`EdDSA`) using the edwards25519 curve.
//!
//! ## Overview
//!
//! Digital signatures provide a way to verify the authenticity and integrity of messages or data.
//! They serve as the digital equivalent of handwritten signatures, but with stronger security
//! properties. When you sign a message with your secret key, anyone with your public key can
//! verify that:
//!
//! 1. The message was signed by someone who possesses the corresponding secret key
//! 2. The message has not been altered since it was signed
//!
//! The Ed25519 signature scheme is based on the Edwards-curve Digital Signature Algorithm (EdDSA)
//! using the edwards25519 curve with a SHA-512 hash function
//!
//! Ed25519 is a modern, high-security, high-performance signature algorithm that is resistant
//! to many types of attacks and side-channel leaks.
//!
//! ## Features
//!
//! - **Fast and secure signatures** with small keys and signatures
//! - **Public key size**: 32 bytes
//! - **Secret key size**: 64 bytes (includes the seed and the public key for optimization)
//! - **Signature size**: 64 bytes
//! - **Batch signature verification** for improved performance
//! - **Protection against side-channel attacks**
//! - **Deterministic signatures** (same message + same key = same signature)
//! - **Collision resistance** against hash function attacks
//! - **No random number generator needed** during signing (prevents RNG-related vulnerabilities)
//!
//! ## Use Cases
//!
//! - **Document signing**: Verify the authenticity of documents
//! - **Software distribution**: Ensure software packages haven't been tampered with
//! - **Secure messaging**: Authenticate the sender of messages
//! - **API authentication**: Verify API requests are coming from authorized clients
//! - **Blockchain transactions**: Sign transactions to prove ownership
//!
//! ## Basic Usage
//!
//! ```rust
//! use libsodium_rs as sodium;
//! use sodium::crypto_sign;
//! use sodium::ensure_init;
//!
//! // Initialize libsodium
//! ensure_init().expect("Failed to initialize libsodium");
//!
//! // Generate a key pair
//! let keypair = crypto_sign::KeyPair::generate().unwrap();
//! let public_key = keypair.public_key;
//! let secret_key = keypair.secret_key;
//!
//! // Message to sign
//! let message = b"Hello, world!";
//!
//! // Sign the message (combined mode)
//! let signed_message = crypto_sign::sign(message, &secret_key).unwrap();
//!
//! // Verify the signature and get the original message
//! let original_message = crypto_sign::verify(&signed_message, &public_key).unwrap();
//! assert_eq!(original_message, message);
//!
//! // Alternatively, use detached signatures
//! let signature = crypto_sign::sign_detached(message, &secret_key).unwrap();
//! assert!(crypto_sign::verify_detached(&signature, message, &public_key));
//! ```
//!
//! ## Combined vs. Detached Signatures
//!
//! This module supports two signature modes:
//!
//! 1. **Combined mode**: The signature is prepended to the message, creating a single byte array
//!    containing both. This is convenient when you want to transmit both together.
//!    - Use `sign()` to create combined signatures
//!    - Use `verify()` to verify combined signatures
//!
//! 2. **Detached mode**: The signature is separate from the message. This is useful when you
//!    want to keep the original message intact or transmit the signature separately.
//!    - Use `sign_detached()` to create detached signatures
//!    - Use `verify_detached()` to verify detached signatures
//!
//! ## Key Management
//!
//! Proper key management is crucial for the security of digital signatures. Here are some
//! best practices:
//!
//! ```rust
//! use libsodium_rs as sodium;
//! use sodium::crypto_sign;
//! use sodium::ensure_init;
//! use std::fs;
//! use std::io::{self, Read, Write};
//!
//! // Initialize libsodium
//! ensure_init().expect("Failed to initialize libsodium");
//!
//! // Generate keys (typically done once)
//! fn generate_and_save_keys() -> io::Result<()> {
//!     let keypair = crypto_sign::KeyPair::generate().unwrap();
//!     let public_key = keypair.public_key;
//!     let secret_key = keypair.secret_key;
//!     
//!     // Save public key (can be shared)
//!     fs::write("public_key.bin", public_key.as_bytes())?;
//!     
//!     // Save secret key (must be kept secure)
//!     // In a real application, you would encrypt this or use a secure key storage solution
//!     fs::write("secret_key.bin", secret_key.as_bytes())?;
//!     
//!     Ok(())
//! }
//!
//! // Load keys for signing
//! fn load_secret_key() -> io::Result<crypto_sign::SecretKey> {
//!     let mut key_data = [0u8; crypto_sign::SECRETKEYBYTES];
//!     let mut file = fs::File::open("secret_key.bin")?;
//!     file.read_exact(&mut key_data)?;
//!     
//!     Ok(crypto_sign::SecretKey::from_bytes(&key_data).unwrap())
//! }
//!
//! // Load keys for verification
//! fn load_public_key() -> io::Result<crypto_sign::PublicKey> {
//!     let mut key_data = [0u8; crypto_sign::PUBLICKEYBYTES];
//!     let mut file = fs::File::open("public_key.bin")?;
//!     file.read_exact(&mut key_data)?;
//!     
//!     Ok(crypto_sign::PublicKey::from_bytes(&key_data).unwrap())
//! }
//!
//! // Sign a document
//! fn sign_document(document: &[u8]) -> io::Result<Vec<u8>> {
//!     let secret_key = load_secret_key()?;
//!     let signature = crypto_sign::sign_detached(document, &secret_key).unwrap();
//!     
//!     // Save or transmit both the document and signature
//!     let mut signed_data = Vec::new();
//!     signed_data.extend_from_slice(&signature);
//!     signed_data.extend_from_slice(document);
//!     
//!     Ok(signed_data)
//! }
//!
//! // Verify a signed document
//! fn verify_document(signed_data: &[u8]) -> io::Result<bool> {
//!     if signed_data.len() < crypto_sign::BYTES {
//!         return Ok(false);
//!     }
//!     
//!     let signature = <[u8; crypto_sign::BYTES]>::try_from(&signed_data[..crypto_sign::BYTES]).unwrap();
//!     let document = &signed_data[crypto_sign::BYTES..];
//!     
//!     let public_key = load_public_key()?;
//!     let result = crypto_sign::verify_detached(&signature, document, &public_key);
//!     
//!     Ok(result)
//! }
//! ```
//!
//! ## Security Considerations
//!
//! - **Secret key protection**: The secret key should be kept confidential at all times. Consider using
//!   hardware security modules (HSMs) or secure enclaves for high-security applications.
//!
//!
//! - **Signature malleability**: Ed25519 signatures are not malleable, meaning an attacker cannot
//!   modify a valid signature to create another valid signature for the same message.
//!
//! - **Forward secrecy**: Digital signatures do not provide forward secrecy. If a secret key is
//!   compromised, all previous signatures created with that key can be attributed to the attacker.
//!
//! - **Deterministic**: Ed25519 is deterministic, meaning the same message signed with the same key
//!   will always produce the same signature. This eliminates the need for a random number generator
//!   during signing, which can be a source of vulnerabilities.
//!
//! - **Cofactor**: Ed25519 has a cofactor of 8, but the signature verification process ensures
//!   that signatures are secure despite this property.
//!
//! - **Quantum resistance**: Ed25519 is not resistant to quantum computing attacks. For long-term
//!   security against quantum computers, consider using post-quantum signature schemes.
//!
//! - **This implementation** uses constant-time operations to prevent timing attacks.

use crate::{Result, SodiumError};
use std::convert::TryFrom;
use std::fmt;

/// Number of bytes in a public key (32)
///
/// The public key is used to verify signatures and can be shared publicly.
pub const PUBLICKEYBYTES: usize = libsodium_sys::crypto_sign_PUBLICKEYBYTES as usize;

/// Number of bytes in a secret key (64)
///
/// The secret key is used to create signatures and should be kept private.
/// Note that the secret key contains both the secret scalar and the public key.
pub const SECRETKEYBYTES: usize = libsodium_sys::crypto_sign_SECRETKEYBYTES as usize;

/// Number of bytes in a signature (64)
///
/// Ed25519 signatures are 64 bytes long, consisting of an R value (32 bytes)
/// and an S value (32 bytes) concatenated together.
pub const BYTES: usize = libsodium_sys::crypto_sign_BYTES as usize;

/// Number of bytes in a seed (32)
///
/// The seed is used to deterministically generate a keypair.
pub const SEEDBYTES: usize = libsodium_sys::crypto_sign_SEEDBYTES as usize;

/// Maximum message length in bytes
///
/// This is the maximum length of a message that can be signed.
pub fn messagebytes_max() -> usize {
    unsafe { libsodium_sys::crypto_sign_messagebytes_max() }
}

/// Size of the state for multi-part signatures
pub const STATEBYTES: usize = std::mem::size_of::<libsodium_sys::crypto_sign_state>();

/// The primitive used by this module ("ed25519")
pub const PRIMITIVE: &str = "ed25519";

/// A public key for Ed25519 digital signatures
///
/// Used to verify signatures created with the corresponding `SecretKey`.
/// The public key is derived from the secret key and can be shared publicly.
///
/// ## Properties
///
/// - Size: 32 bytes (256 bits)
/// - Can be safely shared with anyone
/// - Used to verify the authenticity of signed messages
/// - Represents a point on the edwards25519 elliptic curve
///
/// ## Usage
///
/// Public keys are typically distributed to anyone who needs to verify signatures.
/// They can be safely shared over insecure channels and stored without special protection.
///
/// ```rust
/// use libsodium_rs as sodium;
/// use sodium::crypto_sign;
/// use sodium::ensure_init;
/// use std::convert::TryFrom;
///
/// // Initialize libsodium
/// ensure_init().expect("Failed to initialize libsodium");
///
/// // Generate a keypair
/// let keypair = crypto_sign::KeyPair::generate().unwrap();
/// let public_key = keypair.public_key;
///
/// // Get the raw bytes of the public key (for storage or transmission)
/// let key_bytes = public_key.as_bytes();
///
/// // Later, reconstruct the public key from bytes
/// let reconstructed_key = crypto_sign::PublicKey::from_bytes(key_bytes).unwrap();
/// // Or using TryFrom with owned array
/// let reconstructed_key2 = crypto_sign::PublicKey::try_from(*key_bytes).unwrap();
///
/// assert_eq!(public_key, reconstructed_key);
/// assert_eq!(public_key, reconstructed_key2);
/// ```
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct PublicKey([u8; PUBLICKEYBYTES]);

/// A secret key for Ed25519 digital signatures
///
/// Used to create signatures that can be verified with the corresponding `PublicKey`.
/// The secret key must be kept private to maintain security.
///
/// ## Properties
///
/// - Size: 64 bytes (512 bits)
/// - Contains both the secret scalar (32 bytes) and the public key (32 bytes)
/// - Must be kept confidential
/// - Used to sign messages
///
/// ## Security Considerations
///
/// The secret key is the most sensitive component in the digital signature system.
/// If compromised, an attacker can create valid signatures for any message, impersonating
/// the legitimate owner of the key.
///
/// Best practices for secret key management:
///
/// - Store secret keys in secure, encrypted storage
/// - Consider using hardware security modules (HSMs) for high-security applications
/// - Implement proper access controls to limit who can use the signing key
/// - Rotate keys periodically according to your security policy
/// - Have a revocation plan in case a key is compromised
///
/// ## Usage
///
/// ```rust
/// use libsodium_rs as sodium;
/// use sodium::crypto_sign;
/// use sodium::ensure_init;
///
/// // Initialize libsodium
/// ensure_init().expect("Failed to initialize libsodium");
///
/// // Generate a key pair
/// let keypair = crypto_sign::KeyPair::generate().unwrap();
/// let secret_key = keypair.secret_key;
///
/// // Sign a message
/// let message = b"Important document";
/// let signature = crypto_sign::sign_detached(message, &secret_key).unwrap();
///
/// // In a real application, you would securely store the secret key
/// // and implement proper key management procedures
/// ```
#[derive(Debug, Clone, Eq, PartialEq, zeroize::Zeroize, zeroize::ZeroizeOnDrop)]
pub struct SecretKey([u8; SECRETKEYBYTES]);

/// A key pair for digital signatures
///
/// Contains both a public key and a secret key for use with `crypto_sign` functions.
pub struct KeyPair {
    /// Public key
    pub public_key: PublicKey,
    /// Secret key
    pub secret_key: SecretKey,
}

impl PublicKey {
    /// Generate a new public key from bytes
    ///
    /// # Arguments
    /// * `bytes` - The bytes to create the public key from
    ///
    /// # Returns
    /// * `Result<Self>` - The public key or an error if the input is invalid
    ///
    /// # Errors
    /// Returns an error if the input is not exactly `PUBLICKEYBYTES` bytes long
    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
        if bytes.len() != PUBLICKEYBYTES {
            return Err(SodiumError::InvalidInput(format!(
                "public key must be exactly {} bytes, got {}",
                PUBLICKEYBYTES,
                bytes.len()
            )));
        }

        let mut key = [0u8; PUBLICKEYBYTES];
        key.copy_from_slice(bytes);
        Ok(Self(key))
    }

    /// Extract the public key from a secret key
    ///
    /// This function extracts the public key that is embedded in an Ed25519 secret key.
    /// In the Ed25519 implementation, the secret key (64 bytes) actually contains both
    /// the secret seed (32 bytes) and the public key (32 bytes) for optimization purposes.
    ///
    /// The public key is deterministically derived from the secret seed, but storing it
    /// as part of the secret key allows for faster signing operations by avoiding the
    /// need to recompute the public key for each signature.
    ///
    /// # Arguments
    /// * `secret_key` - The secret key to extract the public key from
    ///
    /// # Returns
    /// * `Result<PublicKey>` - The extracted public key or an error
    ///
    /// # Errors
    /// Returns an error if the extraction fails (extremely rare)
    ///
    /// # Security Considerations
    ///
    /// - This operation does not compromise the security of the secret key
    /// - The public key is safe to share publicly
    /// - The same public key will always be extracted from the same secret key
    ///
    /// # Example
    ///
    /// ```rust
    /// use libsodium_rs as sodium;
    /// use sodium::crypto_sign::{KeyPair, PublicKey};
    /// use sodium::ensure_init;
    ///
    /// // Initialize libsodium
    /// ensure_init().expect("Failed to initialize libsodium");
    ///
    /// // Generate a key pair
    /// let keypair = KeyPair::generate().unwrap();
    /// let secret_key = keypair.secret_key;
    ///
    /// // Extract the public key from the secret key
    /// let extracted_pk = PublicKey::from_secret_key(&secret_key).unwrap();
    ///
    /// // The extracted public key should match the original public key
    /// assert_eq!(extracted_pk, keypair.public_key);
    /// ```
    pub fn from_secret_key(secret_key: &SecretKey) -> Result<Self> {
        let mut pk = [0u8; PUBLICKEYBYTES];

        let result = unsafe {
            libsodium_sys::crypto_sign_ed25519_sk_to_pk(
                pk.as_mut_ptr(),
                secret_key.as_bytes().as_ptr(),
            )
        };

        if result != 0 {
            return Err(SodiumError::OperationError(
                "Failed to extract public key from secret key".into(),
            ));
        }

        Ok(PublicKey(pk))
    }

    /// Get the bytes of the public key
    ///
    /// # Returns
    /// * `&[u8; PUBLICKEYBYTES]` - A reference to the public key bytes
    pub fn as_bytes(&self) -> &[u8; PUBLICKEYBYTES] {
        &self.0
    }

    /// Create a public key from a fixed-size byte array
    ///
    /// # Arguments
    /// * `bytes` - Byte array of exactly PUBLICKEYBYTES length
    ///
    /// # Returns
    /// * `Self` - A new public key
    pub const fn from_bytes_exact(bytes: [u8; PUBLICKEYBYTES]) -> Self {
        Self(bytes)
    }
}

impl AsRef<[u8]> for PublicKey {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

impl From<[u8; PUBLICKEYBYTES]> for PublicKey {
    fn from(bytes: [u8; PUBLICKEYBYTES]) -> Self {
        Self(bytes)
    }
}

impl From<PublicKey> for [u8; PUBLICKEYBYTES] {
    fn from(key: PublicKey) -> [u8; PUBLICKEYBYTES] {
        key.0
    }
}

impl SecretKey {
    /// Generate a new secret key from bytes
    ///
    /// # Arguments
    /// * `bytes` - The bytes to create the secret key from
    ///
    /// # Returns
    /// * `Result<Self>` - The secret key or an error if the input is invalid
    ///
    /// # Errors
    /// Returns an error if the input is not exactly `SECRETKEYBYTES` bytes long
    pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
        if bytes.len() != SECRETKEYBYTES {
            return Err(SodiumError::InvalidInput(format!(
                "secret key must be exactly {} bytes, got {}",
                SECRETKEYBYTES,
                bytes.len()
            )));
        }

        let mut key = [0u8; SECRETKEYBYTES];
        key.copy_from_slice(bytes);
        Ok(SecretKey(key))
    }

    /// Get the bytes of the secret key
    ///
    /// # Returns
    /// * `&[u8; SECRETKEYBYTES]` - A reference to the secret key bytes
    pub fn as_bytes(&self) -> &[u8; SECRETKEYBYTES] {
        &self.0
    }

    /// Create a secret key from a fixed-size byte array
    ///
    /// # Arguments
    /// * `bytes` - Byte array of exactly SECRETKEYBYTES length
    ///
    /// # Returns
    /// * `Self` - A new secret key
    pub const fn from_bytes_exact(bytes: [u8; SECRETKEYBYTES]) -> Self {
        Self(bytes)
    }
}

impl AsRef<[u8]> for SecretKey {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

impl From<[u8; SECRETKEYBYTES]> for SecretKey {
    fn from(bytes: [u8; SECRETKEYBYTES]) -> Self {
        Self(bytes)
    }
}

impl From<SecretKey> for [u8; SECRETKEYBYTES] {
    fn from(key: SecretKey) -> [u8; SECRETKEYBYTES] {
        key.0
    }
}

impl KeyPair {
    /// Generate a new Ed25519 key pair for digital signatures
    ///
    /// This function generates a new random Ed25519 key pair suitable for creating and verifying
    /// digital signatures. The key pair consists of a public key that can be shared and a secret
    /// key that must be kept private.
    ///
    /// The key generation process uses a cryptographically secure random number generator to
    /// create a 32-byte seed, which is then used to deterministically derive both the secret
    /// and public keys according to the Ed25519 algorithm specification.
    ///
    /// ## Key Properties
    ///
    /// - **Public Key**: 32 bytes, can be safely shared with anyone
    /// - **Secret Key**: 64 bytes, contains both the seed (32 bytes) and public key (32 bytes)
    /// - **Security Level**: Equivalent to 128-bit symmetric encryption (highly secure)
    ///
    /// ## Security Considerations
    ///
    /// - The secret key must be kept confidential to maintain security
    /// - The public key can be freely distributed
    /// - Key generation is non-deterministic due to the use of a random seed
    /// - For deterministic key generation, use `KeyPair::from_seed` instead
    /// - The generated keys use the Ed25519 curve, which has strong security properties
    ///   including resistance to many side-channel attacks
    ///
    /// # Example
    ///
    /// ```rust
    /// use libsodium_rs as sodium;
    /// use sodium::crypto_sign;
    /// use sodium::ensure_init;
    ///
    /// // Initialize libsodium
    /// ensure_init().expect("Failed to initialize libsodium");
    ///
    /// // Generate a key pair
    /// let keypair = crypto_sign::KeyPair::generate().unwrap();
    /// let public_key = keypair.public_key;
    /// let secret_key = keypair.secret_key;
    ///
    /// // Use the keys for signing and verification
    /// let message = b"Hello, world!";
    /// let signature = crypto_sign::sign_detached(message, &secret_key).unwrap();
    /// assert!(crypto_sign::verify_detached(&signature, message, &public_key));
    ///
    /// // In a real application, you would securely store the secret key
    /// // and distribute the public key to verifiers
    /// ```
    ///
    /// # Returns
    /// * `Result<KeyPair>` - A newly generated key pair or an error
    ///
    /// # Errors
    /// Returns an error if key generation fails (extremely rare, typically only due to system issues)
    /// such as random number generator failure
    pub fn generate() -> Result<Self> {
        crate::ensure_init()?;

        let mut pk = [0u8; PUBLICKEYBYTES];
        let mut sk = [0u8; SECRETKEYBYTES];

        unsafe {
            let ret = libsodium_sys::crypto_sign_keypair(pk.as_mut_ptr(), sk.as_mut_ptr());
            if ret != 0 {
                return Err(SodiumError::OperationError("key generation failed".into()));
            }
        }

        Ok(Self {
            public_key: PublicKey(pk),
            secret_key: SecretKey(sk),
        })
    }

    /// Generate a new Ed25519 key pair from a seed.
    ///
    /// The seed must be exactly 32 bytes long.
    ///
    /// # Example
    ///
    /// ```rust
    /// use libsodium_rs::{ensure_init, crypto_sign, random};
    ///
    /// // Initialize libsodium
    /// ensure_init().expect("Failed to initialize libsodium");
    ///
    /// // Generate a random seed
    /// let mut seed = [0u8; 32];
    /// random::fill_bytes(&mut seed);
    ///
    /// // Generate a keypair from the seed
    /// let keypair = crypto_sign::KeyPair::from_seed(&seed).unwrap();
    ///
    /// // The same seed will always produce the same keypair
    /// let keypair2 = crypto_sign::KeyPair::from_seed(&seed).unwrap();
    /// assert_eq!(keypair.public_key, keypair2.public_key);
    /// assert_eq!(keypair.secret_key, keypair2.secret_key);
    /// ```
    pub fn from_seed(seed: &[u8]) -> Result<Self> {
        crate::ensure_init()?;

        if seed.len() != SEEDBYTES {
            return Err(SodiumError::InvalidInput(format!(
                "invalid seed length: expected {}, got {}",
                SEEDBYTES,
                seed.len()
            )));
        }

        let mut pk = [0u8; PUBLICKEYBYTES];
        let mut sk = [0u8; SECRETKEYBYTES];

        unsafe {
            let ret = libsodium_sys::crypto_sign_seed_keypair(
                pk.as_mut_ptr(),
                sk.as_mut_ptr(),
                seed.as_ptr(),
            );
            if ret != 0 {
                return Err(SodiumError::OperationError(
                    "Failed to generate keypair from seed".into(),
                ));
            }
        }

        Ok(Self {
            public_key: PublicKey(pk),
            secret_key: SecretKey(sk),
        })
    }

    /// Convert the KeyPair into a tuple of (PublicKey, SecretKey)
    pub fn into_tuple(self) -> (PublicKey, SecretKey) {
        (self.public_key, self.secret_key)
    }
}

/// Sign a message using a secret key (combined mode)
///
/// This function signs a message using the provided secret key and prepends the signature
/// to the message. The resulting signed message contains both the signature and the original
/// message concatenated together.
///
/// ## Combined Mode
///
/// This is known as "combined mode" because the signature and message are combined into
/// a single byte array. For detached signatures (where the signature is separate from the
/// message), use the `sign_detached` function instead.
///
/// ## Algorithm Details
///
/// Ed25519 signing works as follows:
/// 1. Compute a deterministic nonce from the secret key and message using SHA-512
/// 2. Compute point R = nonce * G (where G is the base point of the edwards25519 curve)
/// 3. Compute S = nonce + (hash(R || public_key || message) * secret_scalar)
/// 4. The signature is the concatenation of R and S
///
/// ## Example
///
/// ```rust
/// use libsodium_rs as sodium;
/// use sodium::crypto_sign;
/// use sodium::ensure_init;
///
/// // Initialize libsodium
/// ensure_init().expect("Failed to initialize libsodium");
///
/// // Generate a key pair
/// let keypair = crypto_sign::KeyPair::generate().unwrap();
/// let public_key = keypair.public_key;
/// let secret_key = keypair.secret_key;
///
/// // Sign a message
/// let message = b"Hello, world!";
/// let signed_message = crypto_sign::sign(message, &secret_key).unwrap();
///
/// // The signed message contains both the signature and the original message
/// assert_eq!(signed_message.len(), message.len() + crypto_sign::BYTES);
///
/// // Verify the signature and extract the original message
/// let original_message = crypto_sign::verify(&signed_message, &public_key).unwrap();
/// assert_eq!(original_message, message);
///
/// // Tamper with the signed message - verification should fail
/// let mut tampered = signed_message.clone();
/// tampered[0] ^= 1; // Flip a bit in the signature
/// assert!(crypto_sign::verify(&tampered, &public_key).is_none());
/// ```
///
/// # Arguments
/// * `message` - The message to sign
/// * `secret_key` - The secret key to sign with
///
/// # Returns
/// * `Result<Vec<u8>>` - The signed message (signature + original message) or an error
///
/// # Errors
/// Returns an error if signing fails (extremely rare, typically only due to system issues)
pub fn sign(message: &[u8], secret_key: &SecretKey) -> Result<Vec<u8>> {
    let mut signed_message = vec![0u8; message.len() + BYTES];
    let mut signed_len = 0u64;

    let result = unsafe {
        libsodium_sys::crypto_sign(
            signed_message.as_mut_ptr(),
            &mut signed_len,
            message.as_ptr(),
            message.len() as u64,
            secret_key.as_bytes().as_ptr(),
        )
    };

    if result != 0 {
        return Err(SodiumError::OperationError("Ed25519 signing failed".into()));
    }

    signed_message.truncate(signed_len as usize);
    Ok(signed_message)
}

/// Verify a signed message using a public key (combined mode)
///
/// This function verifies a signed message using the provided public key and extracts
/// the original message if the signature is valid. The signed message must have been
/// created using the `sign` function.
///
/// ## Combined Mode
///
/// This function works with "combined mode" signatures, where the signature and message
/// are combined into a single byte array. For verifying detached signatures, use the
/// `verify_detached` function instead.
///
/// ## Algorithm Details
///
/// Ed25519 verification works as follows:
/// 1. Extract R and S from the signature
/// 2. Compute h = hash(R || public_key || message)
/// 3. Verify that R == S*G - h*public_key
///
/// ## Security Considerations
///
/// - The verification is performed in constant time to prevent timing attacks
/// - If verification fails, no part of the message is considered authentic
///
/// ## Example
///
/// ```rust
/// use libsodium_rs as sodium;
/// use sodium::crypto_sign;
/// use sodium::ensure_init;
///
/// // Initialize libsodium
/// ensure_init().expect("Failed to initialize libsodium");
///
/// // Generate a key pair
/// let keypair = crypto_sign::KeyPair::generate().unwrap();
/// let public_key = keypair.public_key;
/// let secret_key = keypair.secret_key;
///
/// // Sign a message
/// let message = b"Hello, world!";
/// let signed_message = crypto_sign::sign(message, &secret_key).unwrap();
///
/// // Verify the signature and extract the original message
/// let original_message = crypto_sign::verify(&signed_message, &public_key).unwrap();
/// assert_eq!(original_message, message);
///
/// // Tamper with the signed message - verification should fail
/// let mut tampered = signed_message.clone();
/// tampered[0] ^= 1; // Flip a bit in the signature
/// assert!(crypto_sign::verify(&tampered, &public_key).is_none());
/// ```
///
/// # Arguments
/// * `signed_message` - The signed message to verify
/// * `public_key` - The public key to verify with
///
/// # Returns
/// * `Option<Vec<u8>>` - The original message if verification succeeds, or `None` if verification fails
pub fn verify(signed_message: &[u8], public_key: &PublicKey) -> Option<Vec<u8>> {
    if signed_message.len() < BYTES {
        return None;
    }

    let mut message = vec![0u8; signed_message.len() - BYTES];
    let mut message_len = 0u64;

    let result = unsafe {
        libsodium_sys::crypto_sign_open(
            message.as_mut_ptr(),
            &mut message_len,
            signed_message.as_ptr(),
            signed_message.len() as u64,
            public_key.as_bytes().as_ptr(),
        )
    };

    if result != 0 {
        return None;
    }

    message.truncate(message_len as usize);
    Some(message)
}

/// Create a detached signature for a message
///
/// This function creates a signature for a message using the provided secret key, but
/// unlike `sign`, it returns only the signature, not the message with the signature.
/// This is useful when you want to transmit the signature separately from the message.
///
/// ## Detached Mode
///
/// This is known as "detached mode" because the signature is separate from the message.
/// For combined signatures (where the signature is prepended to the message), use the
/// `sign` function instead.
///
/// ## Signature Format
///
/// The signature is a 64-byte array containing:
/// - An R value (32 bytes): A point on the Edwards curve derived from a deterministic nonce
/// - An S value (32 bytes): A scalar value that proves knowledge of the secret key
///
/// ## Security Properties
///
/// - **Deterministic**: The same message signed with the same key always produces the same signature
/// - **Non-malleable**: Signatures cannot be modified to create new valid signatures
/// - **Forward secure**: Even if a signature is compromised, the secret key remains secure
/// - **Collision resistant**: Finding two different messages that produce the same signature is computationally infeasible
///
/// ## Use Cases
///
/// - **Document signing**: When you need to keep the original document intact
/// - **Large data**: When the data being signed is too large to duplicate in memory
/// - **Separate storage**: When you want to store or transmit signatures separately from the data
/// - **Signature databases**: When maintaining a database of signatures for verification
///
/// # Example
///
/// ```rust
/// use libsodium_rs as sodium;
/// use sodium::crypto_sign;
/// use sodium::ensure_init;
///
/// // Initialize libsodium
/// ensure_init().expect("Failed to initialize libsodium");
///
/// // Generate a key pair
/// let keypair = crypto_sign::KeyPair::generate().unwrap();
/// let public_key = keypair.public_key;
/// let secret_key = keypair.secret_key;
///
/// // Sign a message
/// let message = b"Hello, world!";
/// let signature = crypto_sign::sign_detached(message, &secret_key).unwrap();
///
/// // Verify the signature
/// assert!(crypto_sign::verify_detached(&signature, message, &public_key));
///
/// // The signature can be stored or transmitted separately from the message
/// // For example, you might store it in a database or send it in a separate packet
/// let signature_hex = signature.iter().map(|b| format!("{:02x}", b)).collect::<String>();
/// println!("Signature: {}", signature_hex);
/// ```
///
/// # Arguments
/// * `message` - The message to sign
/// * `secret_key` - The secret key to sign with
///
/// # Returns
/// * `Result<[u8; BYTES]>` - The 64-byte signature or an error
///
/// # Errors
/// Returns an error if signing fails (extremely rare, typically only due to system issues)
///
/// # Performance Considerations
///
/// - Ed25519 signatures are designed to be fast to verify
/// - Signing is more computationally intensive than verification
/// - For large messages, the performance is dominated by the SHA-512 hashing of the message
pub fn sign_detached(message: &[u8], secret_key: &SecretKey) -> Result<[u8; BYTES]> {
    let mut signature = [0u8; BYTES];
    let mut signature_len = 0u64;

    let result = unsafe {
        libsodium_sys::crypto_sign_detached(
            signature.as_mut_ptr(),
            &mut signature_len,
            message.as_ptr(),
            message.len() as u64,
            secret_key.as_bytes().as_ptr(),
        )
    };

    if result != 0 {
        return Err(SodiumError::OperationError(
            "Ed25519 detached signing failed".into(),
        ));
    }

    Ok(signature)
}

/// Verify a detached signature
///
/// This function verifies a detached signature for a message using the provided public key.
/// The signature must have been created using the `sign_detached` function with the
/// corresponding secret key.
///
/// ## Detached Mode
///
/// This function works with "detached mode" signatures, where the signature is separate
/// from the message. For verifying combined signatures, use the `verify` function instead.
///
/// ## Security Considerations
///
/// - Verification is **constant-time** with respect to the public key and signature,
///   protecting against timing attacks
/// - The function will return false for any invalid input (wrong signature, wrong message,
///   or wrong public key)
/// - Ed25519 is resistant to many side-channel attacks
/// - Batch verification (verifying multiple signatures at once) can be more efficient
///   for high-throughput applications
///
/// ## Common Verification Scenarios
///
/// - **Document verification**: Ensuring a document hasn't been tampered with
/// - **Software updates**: Verifying the authenticity of software packages
/// - **API authentication**: Verifying that API requests come from authorized sources
/// - **Certificate validation**: Verifying certificate signatures in PKI systems
///
/// ## Example
///
/// ```rust
/// use libsodium_rs as sodium;
/// use sodium::crypto_sign;
/// use sodium::ensure_init;
///
/// // Initialize libsodium
/// ensure_init().expect("Failed to initialize libsodium");
///
/// // Generate a key pair
/// let keypair = crypto_sign::KeyPair::generate().unwrap();
/// let public_key = keypair.public_key;
/// let secret_key = keypair.secret_key;
///
/// // Sign a message
/// let message = b"Hello, world!";
/// let signature = crypto_sign::sign_detached(message, &secret_key).unwrap();
///
/// // Verify the signature
/// let is_valid = crypto_sign::verify_detached(&signature, message, &public_key);
/// assert!(is_valid);
///
/// // Verification with a modified message should fail
/// let wrong_message = b"Modified message";
/// assert!(!crypto_sign::verify_detached(&signature, wrong_message, &public_key));
///
/// // Verification with a wrong public key should fail
/// let wrong_keypair = crypto_sign::KeyPair::generate().unwrap();
/// let wrong_public_key = wrong_keypair.public_key;
/// assert!(!crypto_sign::verify_detached(&signature, message, &wrong_public_key));
/// ```
///
/// # Arguments
/// * `signature` - The 64-byte signature to verify
/// * `message` - The message that was signed
/// * `public_key` - The public key to verify with
///
/// # Returns
/// * `bool` - `true` if the signature is valid, `false` otherwise
pub fn verify_detached(signature: &[u8; BYTES], message: &[u8], public_key: &PublicKey) -> bool {
    let result = unsafe {
        libsodium_sys::crypto_sign_verify_detached(
            signature.as_ptr(),
            message.as_ptr(),
            message.len() as u64,
            public_key.as_bytes().as_ptr(),
        )
    };

    result == 0
}

// Add implementation of Display for PublicKey and SecretKey for easier debugging
impl fmt::Display for PublicKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // Show a prefix of the key for debugging, but not the entire key
        let bytes = self.as_bytes();
        write!(
            f,
            "PublicKey({:02x}{:02x}..{:02x}{:02x})",
            bytes[0],
            bytes[1],
            bytes[PUBLICKEYBYTES - 2],
            bytes[PUBLICKEYBYTES - 1]
        )
    }
}

impl fmt::Display for SecretKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        // For security reasons, don't show any part of the secret key
        // Even in debug output, it's better to be cautious
        write!(f, "SecretKey(*****)")
    }
}

// Add implementation of TryFrom for PublicKey and SecretKey for more idiomatic conversions
impl TryFrom<&[u8]> for PublicKey {
    type Error = SodiumError;

    fn try_from(bytes: &[u8]) -> std::result::Result<Self, Self::Error> {
        PublicKey::from_bytes(bytes)
    }
}

impl TryFrom<&[u8]> for SecretKey {
    type Error = SodiumError;

    fn try_from(bytes: &[u8]) -> std::result::Result<Self, Self::Error> {
        SecretKey::from_bytes(bytes)
    }
}

/// Generate a new Ed25519 key pair from a seed
///
/// This function deterministically generates an Ed25519 key pair from a seed. The same seed
/// will always produce the same key pair. This is useful for key derivation or when you need
/// reproducible keys.
///
/// ## Security Considerations
///
/// - The seed must be kept as secret as the secret key itself
/// - The seed should be high-entropy (ideally from a CSPRNG)
/// ```rust
/// use libsodium_rs::{ensure_init, crypto_sign};
///
/// // Initialize libsodium
/// ensure_init().expect("Failed to initialize libsodium");
///
/// ```
///
/// # Arguments
///
/// * `seed` - The seed to generate the key pair from
///
/// # Returns
///
/// * `Result<KeyPair>` - The generated key pair or an error
///
/// # Errors
///
/// Returns an error if key pair generation fails (extremely rare)
pub fn keypair_from_seed(seed: &[u8; SEEDBYTES]) -> Result<KeyPair> {
    let mut public_key = [0u8; PUBLICKEYBYTES];
    let mut secret_key = [0u8; SECRETKEYBYTES];

    let result = unsafe {
        libsodium_sys::crypto_sign_seed_keypair(
            public_key.as_mut_ptr(),
            secret_key.as_mut_ptr(),
            seed.as_ptr(),
        )
    };

    if result != 0 {
        return Err(SodiumError::OperationError(
            "Ed25519 key pair generation failed".into(),
        ));
    }

    Ok(KeyPair {
        public_key: PublicKey::from_bytes(&public_key).unwrap(),
        secret_key: SecretKey::from_bytes(&secret_key).unwrap(),
    })
}

/// Extract the seed from a secret key
///
/// This function extracts the seed that was used to generate an Ed25519 secret key.
/// The seed is the first 32 bytes of the secret key.
///
/// ## Security Considerations
///
/// - The seed is as sensitive as the secret key itself and must be kept confidential
/// - This is mainly useful for key derivation or when you need to reconstruct keys
///
/// ## Example
///
/// ```rust
/// use libsodium_rs as sodium;
/// use sodium::crypto_sign;
/// use sodium::random;
/// use sodium::ensure_init;
///
/// // Initialize libsodium
/// ensure_init().expect("Failed to initialize libsodium");
///
/// // Generate a random seed
/// let mut original_seed = [0u8; crypto_sign::SEEDBYTES];
/// random::fill_bytes(&mut original_seed);
///
/// // Generate a keypair from the seed
/// let keypair = crypto_sign::KeyPair::from_seed(&original_seed).unwrap();
/// let secret_key = keypair.secret_key;
///
/// // Extract the seed from the secret key
/// let extracted_seed = crypto_sign::secret_key_to_seed(&secret_key).unwrap();
///
/// // The extracted seed should match the original
/// assert_eq!(original_seed, extracted_seed);
/// ```
///
/// # Arguments
///
/// * `secret_key` - The secret key to extract the seed from
///
/// # Returns
///
/// * `Result<[u8; SEEDBYTES]>` - The extracted seed or an error
///
/// # Errors
///
/// Returns an error if the extraction fails (extremely rare)
pub fn secret_key_to_seed(secret_key: &SecretKey) -> Result<[u8; SEEDBYTES]> {
    let mut seed = [0u8; SEEDBYTES];

    let result = unsafe {
        libsodium_sys::crypto_sign_ed25519_sk_to_seed(
            seed.as_mut_ptr(),
            secret_key.as_bytes().as_ptr(),
        )
    };

    if result != 0 {
        return Err(SodiumError::OperationError(
            "Failed to extract seed from secret key".into(),
        ));
    }

    Ok(seed)
}

/// Convert an Ed25519 public key to a Curve25519 public key
///
/// This function converts an Ed25519 public key (used for signatures) to a Curve25519
/// public key (used for key exchange). This allows using the same key pair for both
/// signatures and key exchange.
///
/// ## Security Considerations
///
/// - Not all Ed25519 public keys can be converted to Curve25519 public keys
/// - This conversion is primarily useful when you want to use the same key pair for
///   both signatures and key exchange
///
/// ## Example
///
/// ```rust
/// use libsodium_rs as sodium;
/// use sodium::crypto_sign;
/// use sodium::ensure_init;
///
/// // Initialize libsodium
/// ensure_init().expect("Failed to initialize libsodium");
///
/// // Generate an Ed25519 keypair
/// let keypair = crypto_sign::KeyPair::generate().unwrap();
/// let ed25519_pk = keypair.public_key;
///
/// // Convert the Ed25519 public key to a Curve25519 public key
/// let curve25519_pk = crypto_sign::ed25519_pk_to_curve25519(&ed25519_pk).unwrap();
///
/// // The Curve25519 public key can now be used for key exchange
/// assert_eq!(curve25519_pk.len(), 32);
/// ```
///
/// # Arguments
///
/// * `ed25519_pk` - The Ed25519 public key to convert
///
/// # Returns
///
/// * `Result<[u8; 32]>` - The converted Curve25519 public key or an error
///
/// # Errors
///
/// Returns an error if the conversion fails (which can happen for some Ed25519 public keys)
pub fn ed25519_pk_to_curve25519(ed25519_pk: &PublicKey) -> Result<[u8; 32]> {
    let mut curve25519_pk = [0u8; 32];

    let result = unsafe {
        libsodium_sys::crypto_sign_ed25519_pk_to_curve25519(
            curve25519_pk.as_mut_ptr(),
            ed25519_pk.as_bytes().as_ptr(),
        )
    };

    if result != 0 {
        return Err(SodiumError::OperationError(
            "Failed to convert Ed25519 public key to Curve25519".into(),
        ));
    }

    Ok(curve25519_pk)
}

/// Convert an Ed25519 secret key to a Curve25519 secret key
///
/// This function converts an Ed25519 secret key (used for signatures) to a Curve25519
/// secret key (used for key exchange). This allows using the same key pair for both
/// signatures and key exchange.
///
/// ## Security Considerations
///
/// - The resulting Curve25519 secret key is as sensitive as the Ed25519 secret key
///   and must be kept confidential
/// - This conversion is primarily useful when you want to use the same key pair for
///   both signatures and key exchange
///
/// ## Example
///
/// ```rust
/// use libsodium_rs as sodium;
/// use sodium::crypto_sign;
/// use sodium::ensure_init;
///
/// // Initialize libsodium
/// ensure_init().expect("Failed to initialize libsodium");
///
/// // Generate an Ed25519 keypair
/// let keypair = crypto_sign::KeyPair::generate().unwrap();
/// let ed25519_sk = keypair.secret_key;
///
/// // Convert the Ed25519 secret key to a Curve25519 secret key
/// let curve25519_sk = crypto_sign::ed25519_sk_to_curve25519(&ed25519_sk).unwrap();
///
/// // The Curve25519 secret key can now be used for key exchange
/// assert_eq!(curve25519_sk.len(), 32);
/// ```
///
/// # Arguments
///
/// * `ed25519_sk` - The Ed25519 secret key to convert
///
/// # Returns
///
/// * `Result<[u8; 32]>` - The converted Curve25519 secret key or an error
///
/// # Errors
///
/// Returns an error if the conversion fails (extremely rare)
pub fn ed25519_sk_to_curve25519(ed25519_sk: &SecretKey) -> Result<[u8; 32]> {
    let mut curve25519_sk = [0u8; 32];

    let result = unsafe {
        libsodium_sys::crypto_sign_ed25519_sk_to_curve25519(
            curve25519_sk.as_mut_ptr(),
            ed25519_sk.as_bytes().as_ptr(),
        )
    };

    if result != 0 {
        return Err(SodiumError::OperationError(
            "Failed to convert Ed25519 secret key to Curve25519".into(),
        ));
    }

    Ok(curve25519_sk)
}

/// State for multi-part (streaming) signature creation and verification
///
/// This struct is used for creating or verifying signatures when the message is too large
/// to fit in memory at once, or when the message is being received in chunks.
#[derive(Debug, Clone)]
pub struct State {
    state: libsodium_sys::crypto_sign_state,
}

impl State {
    /// Create a new signature state
    ///
    /// This function initializes a new state for multi-part signature creation or verification.
    ///
    /// ## Example
    ///
    /// ```rust
    /// use libsodium_rs as sodium;
    /// use sodium::crypto_sign;
    /// use sodium::ensure_init;
    ///
    /// // Initialize libsodium
    /// ensure_init().expect("Failed to initialize libsodium");
    ///
    /// // Create a new signature state
    /// let state = crypto_sign::State::new().unwrap();
    /// ```
    ///
    /// # Returns
    /// * `Result<State>` - A new signature state or an error
    ///
    /// # Errors
    /// Returns an error if initialization fails (extremely rare)
    pub fn new() -> Result<Self> {
        let mut state = unsafe { std::mem::zeroed() };
        let result = unsafe { libsodium_sys::crypto_sign_init(&mut state) };

        if result != 0 {
            return Err(SodiumError::OperationError(
                "Failed to initialize signature state".into(),
            ));
        }

        Ok(State { state })
    }

    /// Update the signature state with a message chunk
    ///
    /// This function updates the signature state with a chunk of the message to be signed
    /// or verified. It can be called multiple times to process a message in chunks.
    ///
    /// ## Example
    ///
    /// ```rust
    /// use libsodium_rs as sodium;
    /// use sodium::crypto_sign;
    /// use sodium::ensure_init;
    ///
    /// // Initialize libsodium
    /// ensure_init().expect("Failed to initialize libsodium");
    ///
    /// // Create a new signature state
    /// let mut state = crypto_sign::State::new().unwrap();
    ///
    /// // Update the state with message chunks
    /// state.update(b"Hello, ").unwrap();
    /// state.update(b"world!").unwrap();
    /// ```
    ///
    /// # Arguments
    /// * `message_chunk` - A chunk of the message to update the state with
    ///
    /// # Returns
    /// * `Result<()>` - Success or an error
    ///
    /// # Errors
    /// Returns an error if the update fails (extremely rare)
    pub fn update(&mut self, message_chunk: &[u8]) -> Result<()> {
        let result = unsafe {
            libsodium_sys::crypto_sign_update(
                &mut self.state,
                message_chunk.as_ptr(),
                message_chunk.len() as u64,
            )
        };

        if result != 0 {
            return Err(SodiumError::OperationError(
                "Failed to update signature state".into(),
            ));
        }

        Ok(())
    }

    /// Finalize the signature creation process
    ///
    /// This function finalizes the signature creation process and returns the signature.
    /// It should be called after all message chunks have been processed with `update()`.
    ///
    /// ## Example
    ///
    /// ```rust
    /// use libsodium_rs as sodium;
    /// use sodium::crypto_sign;
    /// use sodium::ensure_init;
    ///
    /// // Initialize libsodium
    /// ensure_init().expect("Failed to initialize libsodium");
    ///
    /// // Generate a keypair
    /// let keypair = crypto_sign::KeyPair::generate().unwrap();
    /// let secret_key = keypair.secret_key;
    ///
    /// // Create a multi-part signature
    /// let mut state = crypto_sign::State::new().unwrap();
    /// state.update(b"Hello, ").unwrap();
    /// state.update(b"world!").unwrap();
    ///
    /// // Finalize and get the signature
    /// let signature = state.finalize_create(&secret_key).unwrap();
    /// assert_eq!(signature.len(), crypto_sign::BYTES);
    /// ```
    ///
    /// # Arguments
    /// * `secret_key` - The secret key to sign with
    ///
    /// # Returns
    /// * `Result<[u8; BYTES]>` - The signature or an error
    ///
    /// # Errors
    /// Returns an error if finalization fails (extremely rare)
    pub fn finalize_create(&mut self, secret_key: &SecretKey) -> Result<[u8; BYTES]> {
        let mut sig = [0u8; BYTES];
        let mut sig_len: u64 = 0;

        let result = unsafe {
            libsodium_sys::crypto_sign_final_create(
                &mut self.state,
                sig.as_mut_ptr(),
                &mut sig_len,
                secret_key.as_bytes().as_ptr(),
            )
        };

        if result != 0 {
            return Err(SodiumError::OperationError(
                "Failed to finalize signature creation".into(),
            ));
        }

        Ok(sig)
    }

    /// Finalize the signature verification process
    ///
    /// This function finalizes the signature verification process. It should be called
    /// after all message chunks have been processed with `update()`.
    ///
    /// ## Example
    ///
    /// ```rust
    /// use libsodium_rs as sodium;
    /// use sodium::crypto_sign;
    /// use sodium::ensure_init;
    ///
    /// // Initialize libsodium
    /// ensure_init().expect("Failed to initialize libsodium");
    ///
    /// // Generate a keypair
    /// let keypair = crypto_sign::KeyPair::generate().unwrap();
    /// let public_key = keypair.public_key;
    /// let secret_key = keypair.secret_key;
    ///
    /// // Create a multi-part signature
    /// let mut sign_state = crypto_sign::State::new().unwrap();
    /// sign_state.update(b"Hello, ").unwrap();
    /// sign_state.update(b"world!").unwrap();
    /// let signature = sign_state.finalize_create(&secret_key).unwrap();
    ///
    /// // Verify the signature
    /// let mut verify_state = crypto_sign::State::new().unwrap();
    /// verify_state.update(b"Hello, ").unwrap();
    /// verify_state.update(b"world!").unwrap();
    /// assert!(verify_state.finalize_verify(&signature, &public_key));
    /// ```
    ///
    /// # Arguments
    /// * `signature` - The signature to verify
    /// * `public_key` - The public key to verify with
    ///
    /// # Returns
    /// * `bool` - `true` if the signature is valid, `false` otherwise
    pub fn finalize_verify(&mut self, signature: &[u8; BYTES], public_key: &PublicKey) -> bool {
        let result = unsafe {
            libsodium_sys::crypto_sign_final_verify(
                &mut self.state,
                signature.as_ptr(),
                public_key.as_bytes().as_ptr(),
            )
        };

        result == 0
    }
}

/// Returns the name of the primitive used by this module
///
/// This function returns the string "ed25519", which is the name of the primitive
/// used by this module.
///
/// ## Example
///
/// ```rust
/// use libsodium_rs as sodium;
/// use sodium::crypto_sign;
///
/// assert_eq!(crypto_sign::primitive(), "ed25519");
/// ```
///
/// # Returns
/// * `&'static str` - The name of the primitive ("ed25519")
pub fn primitive() -> &'static str {
    PRIMITIVE
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_keypair_generation() {
        // Generate a new keypair
        let keypair = KeyPair::generate().unwrap();
        let pk = keypair.public_key;
        let sk = keypair.secret_key;

        // Verify the key sizes
        assert_eq!(pk.as_bytes().len(), PUBLICKEYBYTES);
        assert_eq!(sk.as_bytes().len(), SECRETKEYBYTES);
    }

    #[test]
    fn test_sign_verify() {
        // Generate a new keypair
        let keypair = KeyPair::generate().unwrap();
        let pk = keypair.public_key;
        let sk = keypair.secret_key;
        let message = b"Hello, World!";

        // Sign the message
        let signed = sign(message, &sk).unwrap();

        // Verify the signature and extract the original message
        let verified = verify(&signed, &pk).unwrap();

        // The extracted message should match the original
        assert_eq!(message, &verified[..]);

        // Verify that the signed message is longer than the original
        assert!(signed.len() > message.len());
        assert_eq!(signed.len(), message.len() + BYTES);

        // Tamper with the signed message - verification should fail
        let mut tampered = signed.clone();
        tampered[0] ^= 1; // Flip a bit in the signature
        assert!(verify(&tampered, &pk).is_none());
    }

    #[test]
    fn test_verify_detached() {
        // Generate a keypair
        let keypair = KeyPair::generate().unwrap();
        let pk = keypair.public_key;
        let sk = keypair.secret_key;

        // Sign a message
        let message = b"Test message";
        let signature = sign_detached(message, &sk).unwrap();

        // Verify the signature with the correct message
        assert!(verify_detached(&signature, message, &pk));

        // Verify with wrong message - should fail
        let wrong_message = b"Wrong message";
        assert!(!verify_detached(&signature, wrong_message, &pk));

        // Generate a new keypair and verify with wrong public key - should fail
        let wrong_keypair = KeyPair::generate().unwrap();
        let wrong_pk = wrong_keypair.public_key;
        assert!(!verify_detached(&signature, message, &wrong_pk));
    }

    #[test]
    fn test_seed_keypair() {
        // Generate a random seed
        let mut seed = [0u8; SEEDBYTES];
        crate::random::fill_bytes(&mut seed);

        // Generate two keypairs from the same seed
        let keypair1 = KeyPair::from_seed(&seed).unwrap();
        let pk1 = keypair1.public_key;
        let sk1 = keypair1.secret_key;

        // The same seed should produce the same keypair
        let keypair2 = KeyPair::from_seed(&seed).unwrap();
        let pk2 = keypair2.public_key;
        let sk2 = keypair2.secret_key;
        assert_eq!(pk1, pk2);
        assert_eq!(sk1, sk2);

        // Invalid seed length should fail
        let invalid_seed = [0u8; SEEDBYTES - 1];
        assert!(KeyPair::from_seed(&invalid_seed).is_err());

        // Test deterministic key generation
        let seed = [0u8; SEEDBYTES];
        let keypair = KeyPair::from_seed(&seed).unwrap();
        let determ_pk = keypair.public_key;
        let determ_sk = keypair.secret_key;
        assert_ne!(determ_pk.0, [0u8; PUBLICKEYBYTES]);
        assert_ne!(determ_sk.0, [0u8; SECRETKEYBYTES]);
    }

    #[test]
    fn test_key_conversions() {
        // Generate a keypair
        let keypair = KeyPair::generate().unwrap();
        let pk = keypair.public_key;
        let sk = keypair.secret_key;

        // Extract public key from secret key
        let extracted_pk = PublicKey::from_secret_key(&sk).unwrap();
        assert_eq!(pk, extracted_pk);

        // Test seed extraction (for deterministic keypairs)
        let seed = [0u8; SEEDBYTES];
        let keypair = KeyPair::from_seed(&seed).unwrap();
        let sk = keypair.secret_key;
        let extracted_seed = secret_key_to_seed(&sk).unwrap();
        assert_eq!(seed, extracted_seed);

        // Test conversion to Curve25519 keys
        let curve_pk = ed25519_pk_to_curve25519(&pk).unwrap();
        let curve_sk = ed25519_sk_to_curve25519(&sk).unwrap();

        assert_eq!(curve_pk.len(), 32);
        assert_eq!(curve_sk.len(), 32);
    }

    #[test]
    fn test_multipart_signing() {
        // Generate a keypair
        let keypair = KeyPair::generate().unwrap();
        let pk = keypair.public_key;
        let sk = keypair.secret_key;

        // Create a message in parts
        let part1 = b"Hello, ";
        let part2 = b"world!";

        // Sign using multi-part API
        let mut sign_state = State::new().unwrap();
        sign_state.update(part1).unwrap();
        sign_state.update(part2).unwrap();
        let signature = sign_state.finalize_create(&sk).unwrap();

        // Verify using multi-part API
        let mut verify_state = State::new().unwrap();
        verify_state.update(part1).unwrap();
        verify_state.update(part2).unwrap();
        assert!(verify_state.finalize_verify(&signature, &pk));

        // Test with different message - should fail
        let mut verify_state2 = State::new().unwrap();
        verify_state2.update(b"Different message").unwrap();
        assert!(!verify_state2.finalize_verify(&signature, &pk));

        // Test with wrong public key - should fail
        let wrong_keypair = KeyPair::generate().unwrap();
        let wrong_pk = wrong_keypair.public_key;
        let mut verify_state3 = State::new().unwrap();
        verify_state3.update(part1).unwrap();
        verify_state3.update(part2).unwrap();
        assert!(!verify_state3.finalize_verify(&signature, &wrong_pk));
    }

    #[test]
    fn test_publickey_traits() {
        let keypair = KeyPair::generate().unwrap();
        let pk = keypair.public_key;

        // Test AsRef<[u8]>
        let bytes_ref: &[u8] = pk.as_ref();
        assert_eq!(bytes_ref.len(), PUBLICKEYBYTES);
        assert_eq!(bytes_ref, pk.as_bytes());

        // Test From<[u8; N]> for PublicKey
        let bytes: [u8; PUBLICKEYBYTES] = pk.clone().into();
        let pk2 = PublicKey::from(bytes);
        assert_eq!(pk.as_bytes(), pk2.as_bytes());

        // Test From<PublicKey> for [u8; N]
        let extracted: [u8; PUBLICKEYBYTES] = pk.into();
        assert_eq!(extracted, bytes);
    }

    #[test]
    fn test_secretkey_traits() {
        let keypair = KeyPair::generate().unwrap();
        let sk = keypair.secret_key;

        // Test AsRef<[u8]>
        let bytes_ref: &[u8] = sk.as_ref();
        assert_eq!(bytes_ref.len(), SECRETKEYBYTES);
        assert_eq!(bytes_ref, sk.as_bytes());

        // Test From<[u8; N]> for SecretKey
        let bytes: [u8; SECRETKEYBYTES] = sk.clone().into();
        let sk2 = SecretKey::from(bytes);
        assert_eq!(sk.as_bytes(), sk2.as_bytes());

        // Test From<SecretKey> for [u8; N]
        let extracted: [u8; SECRETKEYBYTES] = sk.into();
        assert_eq!(extracted, bytes);
    }
}