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
//! # IP Address Encryption
//!
//! This module provides efficient, secure encryption of IP addresses (IPv4 and IPv6)
//! for privacy-preserving storage, logging, and analytics.
//!
//! Unlike truncation (which irreversibly destroys data) or hashing (which prevents
//! decryption), ipcrypt provides reversible encryption with well-defined security
//! properties while maintaining operational utility.
//!
//! ## Use Cases
//!
//! - **Privacy-preserving logs**: Encrypt IP addresses in web server logs while
//!   retaining the ability to decrypt for abuse investigation
//! - **Rate limiting and abuse detection**: Use deterministic mode to identify
//!   repeat clients without storing plaintext IPs
//! - **Analytics without exposure**: Count unique visitors without exposing actual
//!   addresses to third-party analytics services
//! - **Regulatory compliance**: Store IP addresses in encrypted form for GDPR/CCPA
//!   compliance while maintaining lawful interception capability
//!
//! ## Variants
//!
//! | Variant | Key Size | Output Size | Properties |
//! |---------|----------|-------------|------------|
//! | Deterministic | 16 bytes | 16 bytes (IP string) | Same input always produces same output; format-preserving |
//! | ND (non-deterministic) | 16 bytes | 24 bytes (hex) | Different output each time; 8-byte random tweak |
//! | NDX (extended ND) | 32 bytes | 32 bytes (hex) | Different output each time; 16-byte random tweak |
//! | PFX (prefix-preserving) | 32 bytes | 16 bytes (IP string) | Preserves network prefix relationships |
//!
//! ## Choosing the Right Variant
//!
//! - **Deterministic**: Rate limiting, deduplication, database indexing
//! - **ND**: Log archival, third-party analytics, data exports (<4B encryptions/key)
//! - **NDX**: Maximum security, billions of encryptions per key
//! - **PFX**: Network analysis, DDoS research, PCAP anonymization
//!
//! ## IP Address Representation
//!
//! IP addresses can be provided as strings (both IPv4 and IPv6 are supported) or
//! as 16-byte binary arrays:
//! - IPv6: Used directly (16 bytes in network byte order)
//! - IPv4: Encoded as IPv4-mapped IPv6 (`::ffff:a.b.c.d`)
//!
//! Use [`parse_ip`] to convert strings to binary, and [`format_ip`] for the reverse.
//!
//! ## Quick Start (String API)
//!
//! The simplest way to use this module is with the string-based functions:
//!
//! ```rust
//! use libsodium_rs::crypto_ipcrypt;
//!
//! // Deterministic encryption (IP string in, IP string out)
//! let key = crypto_ipcrypt::Key::generate();
//! let encrypted = crypto_ipcrypt::encrypt_str("192.0.2.1", &key).unwrap();
//! let decrypted = crypto_ipcrypt::decrypt_str(&encrypted, &key).unwrap();
//! assert_eq!(decrypted, "192.0.2.1");
//!
//! // Works with IPv6 too
//! let encrypted = crypto_ipcrypt::encrypt_str("2001:db8::1", &key).unwrap();
//! let decrypted = crypto_ipcrypt::decrypt_str(&encrypted, &key).unwrap();
//! assert_eq!(decrypted, "2001:db8::1");
//! ```
//!
//! ## Non-Deterministic Encryption (String API)
//!
//! For ND and NDX variants, output is returned as hex since it exceeds 16 bytes:
//!
//! ```rust
//! use libsodium_rs::crypto_ipcrypt;
//!
//! // ND: 24-byte output as hex (48 characters)
//! let key = crypto_ipcrypt::Key::generate();
//! let encrypted = crypto_ipcrypt::nd::encrypt_str("192.0.2.1", &key).unwrap();
//! assert_eq!(encrypted.len(), 48);  // 24 bytes * 2 for hex
//! let decrypted = crypto_ipcrypt::nd::decrypt_str(&encrypted, &key).unwrap();
//! assert_eq!(decrypted, "192.0.2.1");
//!
//! // NDX: 32-byte output as hex (64 characters)
//! let key = crypto_ipcrypt::ndx::Key::generate();
//! let encrypted = crypto_ipcrypt::ndx::encrypt_str("192.0.2.1", &key).unwrap();
//! assert_eq!(encrypted.len(), 64);  // 32 bytes * 2 for hex
//! let decrypted = crypto_ipcrypt::ndx::decrypt_str(&encrypted, &key).unwrap();
//! assert_eq!(decrypted, "192.0.2.1");
//! ```
//!
//! ## Binary API
//!
//! For maximum control, use the binary API:
//!
//! ```rust
//! use libsodium_rs::crypto_ipcrypt;
//!
//! let key = crypto_ipcrypt::Key::generate();
//!
//! // Parse IP string to binary
//! let ip = crypto_ipcrypt::parse_ip("192.0.2.1").unwrap();
//!
//! // Or construct manually for IPv4
//! let ip = crypto_ipcrypt::ipv4_to_bytes([192, 0, 2, 1]);
//!
//! // Encrypt/decrypt binary
//! let encrypted = crypto_ipcrypt::encrypt(&ip, &key);
//! let decrypted = crypto_ipcrypt::decrypt(&encrypted, &key);
//! assert_eq!(ip, decrypted);
//!
//! // Format back to string
//! let ip_str = crypto_ipcrypt::format_ip(&decrypted);
//! assert_eq!(ip_str, "192.0.2.1");
//! ```
//!
//! ## Hex Encoding for ND/NDX
//!
//! ND and NDX variants provide hex encoding helpers:
//!
//! ```rust
//! use libsodium_rs::crypto_ipcrypt;
//!
//! let key = crypto_ipcrypt::Key::generate();
//! let ip = crypto_ipcrypt::parse_ip("192.0.2.1").unwrap();
//! let tweak = crypto_ipcrypt::nd::Tweak::random();
//!
//! // Encrypt to binary
//! let encrypted = crypto_ipcrypt::nd::encrypt(&ip, &tweak, &key);
//!
//! // Convert to hex for storage/transmission
//! let hex = crypto_ipcrypt::nd::to_hex(&encrypted);
//!
//! // Convert back from hex
//! let recovered = crypto_ipcrypt::nd::from_hex(&hex).unwrap();
//! assert_eq!(encrypted, recovered);
//! ```

use crate::random;
use crate::SodiumError;

/// Number of bytes in an IP address (16)
pub const BYTES: usize = libsodium_sys::crypto_ipcrypt_BYTES as usize;

/// Maximum length of an IP address string (45 chars + null terminator)
/// IPv6 worst case: "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"
const IP_MAXLEN: usize = 46;

/// Number of bytes in a deterministic key (16)
pub const KEYBYTES: usize = libsodium_sys::crypto_ipcrypt_KEYBYTES as usize;

/// A key for deterministic IP address encryption
///
/// This key is used for the deterministic variant where the same input
/// always produces the same output.
#[derive(Clone, zeroize::Zeroize, zeroize::ZeroizeOnDrop)]
pub struct Key([u8; KEYBYTES]);

impl Key {
    /// Generates a new random key
    pub fn generate() -> Self {
        let mut key = [0u8; KEYBYTES];
        unsafe {
            libsodium_sys::crypto_ipcrypt_keygen(key.as_mut_ptr());
        }
        Key(key)
    }

    /// Creates a key from a byte slice
    ///
    /// # Panics
    ///
    /// Panics if the slice is not exactly `KEYBYTES` bytes long
    pub fn from_slice(slice: &[u8]) -> Self {
        assert_eq!(slice.len(), KEYBYTES, "key must be {KEYBYTES} bytes");
        let mut key = [0u8; KEYBYTES];
        key.copy_from_slice(slice);
        Key(key)
    }

    /// Returns the key as a byte slice
    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }
}

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

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

/// Converts an IPv4 address to the 16-byte IPv4-mapped IPv6 representation
///
/// The result is in the format `::ffff:a.b.c.d`:
/// 10 zero bytes + 0xff 0xff + 4 IPv4 bytes
///
/// # Example
///
/// ```rust
/// use libsodium_rs::crypto_ipcrypt;
///
/// let ip = crypto_ipcrypt::ipv4_to_bytes([192, 0, 2, 1]);
/// assert_eq!(ip[10], 0xff);
/// assert_eq!(ip[11], 0xff);
/// assert_eq!(&ip[12..], &[192, 0, 2, 1]);
/// ```
pub fn ipv4_to_bytes(addr: [u8; 4]) -> [u8; BYTES] {
    let mut bytes = [0u8; BYTES];
    bytes[10] = 0xff;
    bytes[11] = 0xff;
    bytes[12..].copy_from_slice(&addr);
    bytes
}

/// Converts a 16-byte representation back to an IPv4 address if it's IPv4-mapped
///
/// Returns `Some([a, b, c, d])` if the input is an IPv4-mapped address,
/// `None` otherwise.
pub fn bytes_to_ipv4(bytes: &[u8; BYTES]) -> Option<[u8; 4]> {
    // Check for IPv4-mapped prefix (10 zeros + 0xff 0xff)
    if bytes[..10].iter().all(|&b| b == 0) && bytes[10] == 0xff && bytes[11] == 0xff {
        let mut addr = [0u8; 4];
        addr.copy_from_slice(&bytes[12..]);
        Some(addr)
    } else {
        None
    }
}

/// Parses an IP address string into the 16-byte binary representation
///
/// Accepts both IPv4 (e.g., "192.0.2.1") and IPv6 (e.g., "2001:db8::1") addresses.
/// IPv4 addresses are automatically converted to IPv4-mapped format.
/// IPv6 addresses with zone identifiers (e.g., "fe80::1%eth0") are also supported.
///
/// This uses libsodium's `sodium_ip2bin()` function internally.
///
/// # Example
///
/// ```rust
/// use libsodium_rs::crypto_ipcrypt;
///
/// // Parse IPv4
/// let ipv4 = crypto_ipcrypt::parse_ip("192.0.2.1").unwrap();
///
/// // Parse IPv6
/// let ipv6 = crypto_ipcrypt::parse_ip("2001:db8::1").unwrap();
///
/// // Parse IPv6 with zone identifier
/// let ipv6_zone = crypto_ipcrypt::parse_ip("fe80::1%eth0").unwrap();
/// ```
pub fn parse_ip(ip: &str) -> Result<[u8; BYTES], SodiumError> {
    let mut bin = [0u8; BYTES];
    let ret = unsafe {
        libsodium_sys::sodium_ip2bin(
            bin.as_mut_ptr(),
            ip.as_ptr() as *const std::os::raw::c_char,
            ip.len(),
        )
    };
    if ret == 0 {
        Ok(bin)
    } else {
        Err(SodiumError::InvalidInput("invalid IP address".into()))
    }
}

/// Formats a 16-byte binary IP address as a string
///
/// IPv4-mapped addresses (::ffff:a.b.c.d) are converted to dotted-decimal notation.
/// IPv6 addresses are formatted in standard compressed notation.
///
/// This uses libsodium's `sodium_bin2ip()` function internally.
///
/// # Example
///
/// ```rust
/// use libsodium_rs::crypto_ipcrypt;
///
/// let bin = crypto_ipcrypt::parse_ip("192.0.2.1").unwrap();
/// let formatted = crypto_ipcrypt::format_ip(&bin);
/// assert_eq!(formatted, "192.0.2.1");
///
/// let ipv6 = crypto_ipcrypt::parse_ip("2001:db8::1").unwrap();
/// let formatted = crypto_ipcrypt::format_ip(&ipv6);
/// assert_eq!(formatted, "2001:db8::1");
/// ```
pub fn format_ip(bin: &[u8; BYTES]) -> String {
    let mut buf = [0i8; IP_MAXLEN];
    let ptr = unsafe {
        libsodium_sys::sodium_bin2ip(
            buf.as_mut_ptr() as *mut std::os::raw::c_char,
            IP_MAXLEN,
            bin.as_ptr(),
        )
    };
    if ptr.is_null() {
        // This shouldn't happen for valid 16-byte input, but handle it anyway
        String::new()
    } else {
        // Find the null terminator and convert to String
        let len = buf.iter().position(|&c| c == 0).unwrap_or(IP_MAXLEN);
        let bytes: Vec<u8> = buf[..len].iter().map(|&c| c as u8).collect();
        String::from_utf8_lossy(&bytes).into_owned()
    }
}

/// Encrypts an IP address using deterministic encryption
///
/// The same address with the same key always produces the same ciphertext.
/// This is useful for rate limiting, deduplication, and database indexing.
///
/// # Arguments
///
/// * `input` - The 16-byte IP address to encrypt
/// * `key` - The 16-byte encryption key
///
/// # Returns
///
/// The 16-byte encrypted IP address
pub fn encrypt(input: &[u8; BYTES], key: &Key) -> [u8; BYTES] {
    let mut output = [0u8; BYTES];
    unsafe {
        libsodium_sys::crypto_ipcrypt_encrypt(output.as_mut_ptr(), input.as_ptr(), key.0.as_ptr());
    }
    output
}

/// Decrypts an IP address encrypted with deterministic encryption
///
/// # Arguments
///
/// * `input` - The 16-byte encrypted IP address
/// * `key` - The 16-byte encryption key
///
/// # Returns
///
/// The 16-byte decrypted IP address
pub fn decrypt(input: &[u8; BYTES], key: &Key) -> [u8; BYTES] {
    let mut output = [0u8; BYTES];
    unsafe {
        libsodium_sys::crypto_ipcrypt_decrypt(output.as_mut_ptr(), input.as_ptr(), key.0.as_ptr());
    }
    output
}

/// Encrypts an IP address string using deterministic encryption
///
/// This is a convenience function that parses the IP, encrypts it, and returns
/// the encrypted address as a formatted IP string.
///
/// # Example
///
/// ```rust
/// use libsodium_rs::crypto_ipcrypt;
///
/// let key = crypto_ipcrypt::Key::generate();
/// let encrypted = crypto_ipcrypt::encrypt_str("192.0.2.1", &key).unwrap();
/// let decrypted = crypto_ipcrypt::decrypt_str(&encrypted, &key).unwrap();
/// assert_eq!(decrypted, "192.0.2.1");
/// ```
pub fn encrypt_str(ip: &str, key: &Key) -> Result<String, SodiumError> {
    let bin = parse_ip(ip)?;
    let encrypted = encrypt(&bin, key);
    Ok(format_ip(&encrypted))
}

/// Decrypts an IP address string encrypted with deterministic encryption
///
/// # Example
///
/// ```rust
/// use libsodium_rs::crypto_ipcrypt;
///
/// let key = crypto_ipcrypt::Key::generate();
/// let encrypted = crypto_ipcrypt::encrypt_str("2001:db8::1", &key).unwrap();
/// let decrypted = crypto_ipcrypt::decrypt_str(&encrypted, &key).unwrap();
/// assert_eq!(decrypted, "2001:db8::1");
/// ```
pub fn decrypt_str(ip: &str, key: &Key) -> Result<String, SodiumError> {
    let bin = parse_ip(ip)?;
    let decrypted = decrypt(&bin, key);
    Ok(format_ip(&decrypted))
}

/// Non-deterministic IP address encryption (ND variant)
///
/// Uses a random 8-byte tweak to ensure the same address produces different
/// ciphertexts each time. Good for log archival and third-party analytics.
pub mod nd {
    use super::*;

    /// Number of bytes in an ND key (16)
    pub const KEYBYTES: usize = libsodium_sys::crypto_ipcrypt_ND_KEYBYTES as usize;

    /// Number of bytes in an ND tweak (8)
    pub const TWEAKBYTES: usize = libsodium_sys::crypto_ipcrypt_ND_TWEAKBYTES as usize;

    /// Number of bytes in ND input (16)
    pub const INPUTBYTES: usize = libsodium_sys::crypto_ipcrypt_ND_INPUTBYTES as usize;

    /// Number of bytes in ND output (24)
    pub const OUTPUTBYTES: usize = libsodium_sys::crypto_ipcrypt_ND_OUTPUTBYTES as usize;

    /// A tweak for non-deterministic encryption
    #[derive(Clone, Copy)]
    pub struct Tweak([u8; TWEAKBYTES]);

    impl Tweak {
        /// Generates a random tweak
        pub fn random() -> Self {
            let mut tweak = [0u8; TWEAKBYTES];
            random::fill_bytes(&mut tweak);
            Tweak(tweak)
        }

        /// Creates a tweak from a byte slice
        pub fn from_slice(slice: &[u8]) -> Self {
            assert_eq!(slice.len(), TWEAKBYTES, "tweak must be {TWEAKBYTES} bytes");
            let mut tweak = [0u8; TWEAKBYTES];
            tweak.copy_from_slice(slice);
            Tweak(tweak)
        }

        /// Returns the tweak as a byte slice
        pub fn as_bytes(&self) -> &[u8] {
            &self.0
        }
    }

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

    /// Encrypts an IP address using non-deterministic encryption
    ///
    /// The output includes the tweak prepended to the ciphertext, so no separate
    /// tweak storage is needed for decryption.
    ///
    /// # Arguments
    ///
    /// * `input` - The 16-byte IP address to encrypt
    /// * `tweak` - The 8-byte random tweak
    /// * `key` - The 16-byte encryption key
    ///
    /// # Returns
    ///
    /// The 24-byte encrypted IP address (tweak + ciphertext)
    pub fn encrypt(input: &[u8; INPUTBYTES], tweak: &Tweak, key: &super::Key) -> [u8; OUTPUTBYTES] {
        let mut output = [0u8; OUTPUTBYTES];
        unsafe {
            libsodium_sys::crypto_ipcrypt_nd_encrypt(
                output.as_mut_ptr(),
                input.as_ptr(),
                tweak.0.as_ptr(),
                key.0.as_ptr(),
            );
        }
        output
    }

    /// Decrypts an IP address encrypted with non-deterministic encryption
    ///
    /// The tweak is extracted from the ciphertext automatically.
    ///
    /// # Arguments
    ///
    /// * `input` - The 24-byte encrypted IP address
    /// * `key` - The 16-byte encryption key
    ///
    /// # Returns
    ///
    /// The 16-byte decrypted IP address
    pub fn decrypt(input: &[u8; OUTPUTBYTES], key: &super::Key) -> [u8; INPUTBYTES] {
        let mut output = [0u8; INPUTBYTES];
        unsafe {
            libsodium_sys::crypto_ipcrypt_nd_decrypt(
                output.as_mut_ptr(),
                input.as_ptr(),
                key.0.as_ptr(),
            );
        }
        output
    }

    /// Encrypts an IP address string using non-deterministic encryption
    ///
    /// Returns the 24-byte ciphertext as a hex string (48 characters).
    ///
    /// # Example
    ///
    /// ```rust
    /// use libsodium_rs::crypto_ipcrypt;
    ///
    /// let key = crypto_ipcrypt::Key::generate();
    /// let encrypted = crypto_ipcrypt::nd::encrypt_str("192.0.2.1", &key).unwrap();
    /// assert_eq!(encrypted.len(), 48); // 24 bytes as hex
    ///
    /// let decrypted = crypto_ipcrypt::nd::decrypt_str(&encrypted, &key).unwrap();
    /// assert_eq!(decrypted, "192.0.2.1");
    /// ```
    pub fn encrypt_str(ip: &str, key: &super::Key) -> Result<String, SodiumError> {
        let bin = super::parse_ip(ip)?;
        let tweak = Tweak::random();
        let encrypted = encrypt(&bin, &tweak, key);
        Ok(crate::utils::bin2hex(&encrypted))
    }

    /// Decrypts a hex-encoded ND ciphertext and returns the IP address as a string
    ///
    /// # Example
    ///
    /// ```rust
    /// use libsodium_rs::crypto_ipcrypt;
    ///
    /// let key = crypto_ipcrypt::Key::generate();
    /// let encrypted = crypto_ipcrypt::nd::encrypt_str("2001:db8::1", &key).unwrap();
    /// let decrypted = crypto_ipcrypt::nd::decrypt_str(&encrypted, &key).unwrap();
    /// assert_eq!(decrypted, "2001:db8::1");
    /// ```
    pub fn decrypt_str(hex: &str, key: &super::Key) -> Result<String, SodiumError> {
        let bytes = crate::utils::hex2bin(hex)?;
        if bytes.len() != OUTPUTBYTES {
            return Err(SodiumError::InvalidInput(format!(
                "expected {} bytes, got {}",
                OUTPUTBYTES,
                bytes.len()
            )));
        }
        let mut input = [0u8; OUTPUTBYTES];
        input.copy_from_slice(&bytes);
        let decrypted = decrypt(&input, key);
        Ok(super::format_ip(&decrypted))
    }

    /// Encodes a 24-byte ND ciphertext as a hex string
    pub fn to_hex(ciphertext: &[u8; OUTPUTBYTES]) -> String {
        crate::utils::bin2hex(ciphertext)
    }

    /// Decodes a hex string to a 24-byte ND ciphertext
    pub fn from_hex(hex: &str) -> Result<[u8; OUTPUTBYTES], SodiumError> {
        let bytes = crate::utils::hex2bin(hex)?;
        if bytes.len() != OUTPUTBYTES {
            return Err(SodiumError::InvalidInput(format!(
                "expected {} bytes, got {}",
                OUTPUTBYTES,
                bytes.len()
            )));
        }
        let mut output = [0u8; OUTPUTBYTES];
        output.copy_from_slice(&bytes);
        Ok(output)
    }
}

/// Extended non-deterministic IP address encryption (NDX variant)
///
/// Uses a 32-byte key and 16-byte tweak for maximum security. The larger tweak
/// space provides a higher birthday bound (~2^64), suitable for billions of
/// encryptions per key.
pub mod ndx {
    use super::*;

    /// Number of bytes in an NDX key (32)
    pub const KEYBYTES: usize = libsodium_sys::crypto_ipcrypt_NDX_KEYBYTES as usize;

    /// Number of bytes in an NDX tweak (16)
    pub const TWEAKBYTES: usize = libsodium_sys::crypto_ipcrypt_NDX_TWEAKBYTES as usize;

    /// Number of bytes in NDX input (16)
    pub const INPUTBYTES: usize = libsodium_sys::crypto_ipcrypt_NDX_INPUTBYTES as usize;

    /// Number of bytes in NDX output (32)
    pub const OUTPUTBYTES: usize = libsodium_sys::crypto_ipcrypt_NDX_OUTPUTBYTES as usize;

    /// A key for NDX encryption
    #[derive(Clone, zeroize::Zeroize, zeroize::ZeroizeOnDrop)]
    pub struct Key([u8; KEYBYTES]);

    impl Key {
        /// Generates a new random key
        pub fn generate() -> Self {
            let mut key = [0u8; KEYBYTES];
            unsafe {
                libsodium_sys::crypto_ipcrypt_ndx_keygen(key.as_mut_ptr());
            }
            Key(key)
        }

        /// Creates a key from a byte slice
        pub fn from_slice(slice: &[u8]) -> Self {
            assert_eq!(slice.len(), KEYBYTES, "key must be {KEYBYTES} bytes");
            let mut key = [0u8; KEYBYTES];
            key.copy_from_slice(slice);
            Key(key)
        }

        /// Returns the key as a byte slice
        pub fn as_bytes(&self) -> &[u8] {
            &self.0
        }
    }

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

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

    /// A tweak for NDX encryption
    #[derive(Clone, Copy)]
    pub struct Tweak([u8; TWEAKBYTES]);

    impl Tweak {
        /// Generates a random tweak
        pub fn random() -> Self {
            let mut tweak = [0u8; TWEAKBYTES];
            random::fill_bytes(&mut tweak);
            Tweak(tweak)
        }

        /// Creates a tweak from a byte slice
        pub fn from_slice(slice: &[u8]) -> Self {
            assert_eq!(slice.len(), TWEAKBYTES, "tweak must be {TWEAKBYTES} bytes");
            let mut tweak = [0u8; TWEAKBYTES];
            tweak.copy_from_slice(slice);
            Tweak(tweak)
        }

        /// Returns the tweak as a byte slice
        pub fn as_bytes(&self) -> &[u8] {
            &self.0
        }
    }

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

    /// Encrypts an IP address using extended non-deterministic encryption
    ///
    /// # Arguments
    ///
    /// * `input` - The 16-byte IP address to encrypt
    /// * `tweak` - The 16-byte random tweak
    /// * `key` - The 32-byte encryption key
    ///
    /// # Returns
    ///
    /// The 32-byte encrypted IP address
    pub fn encrypt(input: &[u8; INPUTBYTES], tweak: &Tweak, key: &Key) -> [u8; OUTPUTBYTES] {
        let mut output = [0u8; OUTPUTBYTES];
        unsafe {
            libsodium_sys::crypto_ipcrypt_ndx_encrypt(
                output.as_mut_ptr(),
                input.as_ptr(),
                tweak.0.as_ptr(),
                key.0.as_ptr(),
            );
        }
        output
    }

    /// Decrypts an IP address encrypted with extended non-deterministic encryption
    ///
    /// # Arguments
    ///
    /// * `input` - The 32-byte encrypted IP address
    /// * `key` - The 32-byte encryption key
    ///
    /// # Returns
    ///
    /// The 16-byte decrypted IP address
    pub fn decrypt(input: &[u8; OUTPUTBYTES], key: &Key) -> [u8; INPUTBYTES] {
        let mut output = [0u8; INPUTBYTES];
        unsafe {
            libsodium_sys::crypto_ipcrypt_ndx_decrypt(
                output.as_mut_ptr(),
                input.as_ptr(),
                key.0.as_ptr(),
            );
        }
        output
    }

    /// Encrypts an IP address string using extended non-deterministic encryption
    ///
    /// Returns the 32-byte ciphertext as a hex string (64 characters).
    ///
    /// # Example
    ///
    /// ```rust
    /// use libsodium_rs::crypto_ipcrypt;
    ///
    /// let key = crypto_ipcrypt::ndx::Key::generate();
    /// let encrypted = crypto_ipcrypt::ndx::encrypt_str("192.0.2.1", &key).unwrap();
    /// assert_eq!(encrypted.len(), 64); // 32 bytes as hex
    ///
    /// let decrypted = crypto_ipcrypt::ndx::decrypt_str(&encrypted, &key).unwrap();
    /// assert_eq!(decrypted, "192.0.2.1");
    /// ```
    pub fn encrypt_str(ip: &str, key: &Key) -> Result<String, SodiumError> {
        let bin = super::parse_ip(ip)?;
        let tweak = Tweak::random();
        let encrypted = encrypt(&bin, &tweak, key);
        Ok(crate::utils::bin2hex(&encrypted))
    }

    /// Decrypts a hex-encoded NDX ciphertext and returns the IP address as a string
    ///
    /// # Example
    ///
    /// ```rust
    /// use libsodium_rs::crypto_ipcrypt;
    ///
    /// let key = crypto_ipcrypt::ndx::Key::generate();
    /// let encrypted = crypto_ipcrypt::ndx::encrypt_str("2001:db8::1", &key).unwrap();
    /// let decrypted = crypto_ipcrypt::ndx::decrypt_str(&encrypted, &key).unwrap();
    /// assert_eq!(decrypted, "2001:db8::1");
    /// ```
    pub fn decrypt_str(hex: &str, key: &Key) -> Result<String, SodiumError> {
        let bytes = crate::utils::hex2bin(hex)?;
        if bytes.len() != OUTPUTBYTES {
            return Err(SodiumError::InvalidInput(format!(
                "expected {} bytes, got {}",
                OUTPUTBYTES,
                bytes.len()
            )));
        }
        let mut input = [0u8; OUTPUTBYTES];
        input.copy_from_slice(&bytes);
        let decrypted = decrypt(&input, key);
        Ok(super::format_ip(&decrypted))
    }

    /// Encodes a 32-byte NDX ciphertext as a hex string
    pub fn to_hex(ciphertext: &[u8; OUTPUTBYTES]) -> String {
        crate::utils::bin2hex(ciphertext)
    }

    /// Decodes a hex string to a 32-byte NDX ciphertext
    pub fn from_hex(hex: &str) -> Result<[u8; OUTPUTBYTES], SodiumError> {
        let bytes = crate::utils::hex2bin(hex)?;
        if bytes.len() != OUTPUTBYTES {
            return Err(SodiumError::InvalidInput(format!(
                "expected {} bytes, got {}",
                OUTPUTBYTES,
                bytes.len()
            )));
        }
        let mut output = [0u8; OUTPUTBYTES];
        output.copy_from_slice(&bytes);
        Ok(output)
    }
}

/// Prefix-preserving IP address encryption (PFX variant)
///
/// Preserves network prefix relationships: addresses sharing a common prefix
/// will have ciphertexts sharing a corresponding (different) prefix.
///
/// Use cases:
/// - Network research and academic datasets
/// - DDoS attack analysis
/// - PCAP and NetFlow anonymization
/// - ISP and CDN traffic analysis
pub mod pfx {

    /// Number of bytes in a PFX key (32)
    pub const KEYBYTES: usize = libsodium_sys::crypto_ipcrypt_PFX_KEYBYTES as usize;

    /// Number of bytes in PFX input/output (16)
    pub const BYTES: usize = libsodium_sys::crypto_ipcrypt_PFX_BYTES as usize;

    /// A key for prefix-preserving encryption
    #[derive(Clone, zeroize::Zeroize, zeroize::ZeroizeOnDrop)]
    pub struct Key([u8; KEYBYTES]);

    impl Key {
        /// Generates a new random key
        pub fn generate() -> Self {
            let mut key = [0u8; KEYBYTES];
            unsafe {
                libsodium_sys::crypto_ipcrypt_pfx_keygen(key.as_mut_ptr());
            }
            Key(key)
        }

        /// Creates a key from a byte slice
        pub fn from_slice(slice: &[u8]) -> Self {
            assert_eq!(slice.len(), KEYBYTES, "key must be {KEYBYTES} bytes");
            let mut key = [0u8; KEYBYTES];
            key.copy_from_slice(slice);
            Key(key)
        }

        /// Returns the key as a byte slice
        pub fn as_bytes(&self) -> &[u8] {
            &self.0
        }
    }

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

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

    /// Encrypts an IP address using prefix-preserving encryption
    ///
    /// Addresses in the same subnet will produce ciphertexts in a corresponding
    /// (encrypted) subnet. The address family is preserved: IPv4 addresses
    /// encrypt to IPv4 addresses.
    ///
    /// # Arguments
    ///
    /// * `input` - The 16-byte IP address to encrypt
    /// * `key` - The 32-byte encryption key
    ///
    /// # Returns
    ///
    /// The 16-byte encrypted IP address
    pub fn encrypt(input: &[u8; BYTES], key: &Key) -> [u8; BYTES] {
        let mut output = [0u8; BYTES];
        unsafe {
            libsodium_sys::crypto_ipcrypt_pfx_encrypt(
                output.as_mut_ptr(),
                input.as_ptr(),
                key.0.as_ptr(),
            );
        }
        output
    }

    /// Decrypts an IP address encrypted with prefix-preserving encryption
    ///
    /// # Arguments
    ///
    /// * `input` - The 16-byte encrypted IP address
    /// * `key` - The 32-byte encryption key
    ///
    /// # Returns
    ///
    /// The 16-byte decrypted IP address
    pub fn decrypt(input: &[u8; BYTES], key: &Key) -> [u8; BYTES] {
        let mut output = [0u8; BYTES];
        unsafe {
            libsodium_sys::crypto_ipcrypt_pfx_decrypt(
                output.as_mut_ptr(),
                input.as_ptr(),
                key.0.as_ptr(),
            );
        }
        output
    }

    /// Encrypts an IP address string using prefix-preserving encryption
    ///
    /// Returns the encrypted address as a formatted IP string.
    ///
    /// # Example
    ///
    /// ```rust
    /// use libsodium_rs::crypto_ipcrypt;
    ///
    /// let key = crypto_ipcrypt::pfx::Key::generate();
    /// let encrypted = crypto_ipcrypt::pfx::encrypt_str("192.0.2.1", &key).unwrap();
    /// let decrypted = crypto_ipcrypt::pfx::decrypt_str(&encrypted, &key).unwrap();
    /// assert_eq!(decrypted, "192.0.2.1");
    /// ```
    pub fn encrypt_str(ip: &str, key: &Key) -> Result<String, super::SodiumError> {
        let bin = super::parse_ip(ip)?;
        let encrypted = encrypt(&bin, key);
        Ok(super::format_ip(&encrypted))
    }

    /// Decrypts an IP address string encrypted with prefix-preserving encryption
    ///
    /// # Example
    ///
    /// ```rust
    /// use libsodium_rs::crypto_ipcrypt;
    ///
    /// let key = crypto_ipcrypt::pfx::Key::generate();
    /// let encrypted = crypto_ipcrypt::pfx::encrypt_str("2001:db8::1", &key).unwrap();
    /// let decrypted = crypto_ipcrypt::pfx::decrypt_str(&encrypted, &key).unwrap();
    /// assert_eq!(decrypted, "2001:db8::1");
    /// ```
    pub fn decrypt_str(ip: &str, key: &Key) -> Result<String, super::SodiumError> {
        let bin = super::parse_ip(ip)?;
        let decrypted = decrypt(&bin, key);
        Ok(super::format_ip(&decrypted))
    }
}

#[cfg(test)]
mod tests {
    use super::{
        bytes_to_ipv4, decrypt, encrypt, ipv4_to_bytes, nd, ndx, pfx, Key, BYTES, KEYBYTES,
    };

    #[test]
    fn test_ipv4_conversion() {
        let ipv4 = [192, 0, 2, 1];
        let bytes = ipv4_to_bytes(ipv4);

        // Check IPv4-mapped format
        assert!(bytes[..10].iter().all(|&b| b == 0));
        assert_eq!(bytes[10], 0xff);
        assert_eq!(bytes[11], 0xff);
        assert_eq!(&bytes[12..], &ipv4);

        // Convert back
        let recovered = bytes_to_ipv4(&bytes).unwrap();
        assert_eq!(recovered, ipv4);
    }

    #[test]
    fn test_deterministic_encryption() {
        let key = Key::generate();
        let ip = ipv4_to_bytes([192, 0, 2, 1]);

        let encrypted = encrypt(&ip, &key);
        let decrypted = decrypt(&encrypted, &key);

        assert_eq!(ip, decrypted);
        assert_ne!(ip, encrypted);
    }

    #[test]
    fn test_deterministic_same_input_same_output() {
        let key = Key::generate();
        let ip = ipv4_to_bytes([10, 0, 0, 1]);

        let encrypted1 = encrypt(&ip, &key);
        let encrypted2 = encrypt(&ip, &key);

        assert_eq!(encrypted1, encrypted2);
    }

    #[test]
    fn test_nd_encryption() {
        let key = Key::generate();
        let ip = ipv4_to_bytes([192, 168, 1, 1]);
        let tweak = nd::Tweak::random();

        let encrypted = nd::encrypt(&ip, &tweak, &key);
        let decrypted = nd::decrypt(&encrypted, &key);

        assert_eq!(ip, decrypted);
        assert_eq!(encrypted.len(), nd::OUTPUTBYTES);
    }

    #[test]
    fn test_nd_different_tweaks() {
        let key = Key::generate();
        let ip = ipv4_to_bytes([192, 168, 1, 1]);

        let tweak1 = nd::Tweak::random();
        let tweak2 = nd::Tweak::random();

        let encrypted1 = nd::encrypt(&ip, &tweak1, &key);
        let encrypted2 = nd::encrypt(&ip, &tweak2, &key);

        // Different tweaks should produce different ciphertexts
        assert_ne!(encrypted1, encrypted2);

        // Both should decrypt to the same value
        let decrypted1 = nd::decrypt(&encrypted1, &key);
        let decrypted2 = nd::decrypt(&encrypted2, &key);

        assert_eq!(decrypted1, ip);
        assert_eq!(decrypted2, ip);
    }

    #[test]
    fn test_ndx_encryption() {
        let key = ndx::Key::generate();
        let ip = ipv4_to_bytes([172, 16, 0, 1]);
        let tweak = ndx::Tweak::random();

        let encrypted = ndx::encrypt(&ip, &tweak, &key);
        let decrypted = ndx::decrypt(&encrypted, &key);

        assert_eq!(ip, decrypted);
        assert_eq!(encrypted.len(), ndx::OUTPUTBYTES);
    }

    #[test]
    fn test_ndx_different_tweaks() {
        let key = ndx::Key::generate();
        let ip = ipv4_to_bytes([172, 16, 0, 1]);

        let tweak1 = ndx::Tweak::random();
        let tweak2 = ndx::Tweak::random();

        let encrypted1 = ndx::encrypt(&ip, &tweak1, &key);
        let encrypted2 = ndx::encrypt(&ip, &tweak2, &key);

        assert_ne!(encrypted1, encrypted2);

        assert_eq!(ndx::decrypt(&encrypted1, &key), ip);
        assert_eq!(ndx::decrypt(&encrypted2, &key), ip);
    }

    #[test]
    fn test_pfx_encryption() {
        let key = pfx::Key::generate();
        let ip = ipv4_to_bytes([10, 0, 0, 1]);

        let encrypted = pfx::encrypt(&ip, &key);
        let decrypted = pfx::decrypt(&encrypted, &key);

        assert_eq!(ip, decrypted);
        assert_eq!(encrypted.len(), pfx::BYTES);
    }

    #[test]
    fn test_pfx_preserves_relationship() {
        let key = pfx::Key::generate();

        // Two addresses in the same /24
        let ip1 = ipv4_to_bytes([10, 0, 0, 1]);
        let ip2 = ipv4_to_bytes([10, 0, 0, 100]);

        let enc1 = pfx::encrypt(&ip1, &key);
        let enc2 = pfx::encrypt(&ip2, &key);

        // They should both decrypt correctly
        assert_eq!(pfx::decrypt(&enc1, &key), ip1);
        assert_eq!(pfx::decrypt(&enc2, &key), ip2);

        // The encrypted addresses should share a common prefix
        // (We can't easily verify the exact prefix length, but we can
        // at least verify they're different)
        assert_ne!(enc1, enc2);
    }

    #[test]
    fn test_different_keys_different_results() {
        let key1 = Key::generate();
        let key2 = Key::generate();
        let ip = ipv4_to_bytes([192, 0, 2, 1]);

        let encrypted1 = encrypt(&ip, &key1);
        let encrypted2 = encrypt(&ip, &key2);

        assert_ne!(encrypted1, encrypted2);
    }

    #[test]
    fn test_key_from_slice() {
        let bytes = [0x42u8; KEYBYTES];
        let key = Key::from_slice(&bytes);
        assert_eq!(key.as_bytes(), &bytes);
    }

    #[test]
    fn test_ndx_key_from_slice() {
        let bytes = [0x42u8; ndx::KEYBYTES];
        let key = ndx::Key::from_slice(&bytes);
        assert_eq!(key.as_bytes(), &bytes);
    }

    #[test]
    fn test_pfx_key_from_slice() {
        let bytes = [0x42u8; pfx::KEYBYTES];
        let key = pfx::Key::from_slice(&bytes);
        assert_eq!(key.as_bytes(), &bytes);
    }

    #[test]
    fn test_ipv6_encryption() {
        let key = Key::generate();

        // An IPv6 address (2001:db8::1)
        let ip: [u8; BYTES] = [
            0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x01,
        ];

        let encrypted = encrypt(&ip, &key);
        let decrypted = decrypt(&encrypted, &key);

        assert_eq!(ip, decrypted);

        // Should not be detected as IPv4-mapped
        assert!(bytes_to_ipv4(&ip).is_none());
    }

    #[test]
    fn test_constants() {
        assert_eq!(BYTES, 16);
        assert_eq!(KEYBYTES, 16);

        assert_eq!(nd::KEYBYTES, 16);
        assert_eq!(nd::TWEAKBYTES, 8);
        assert_eq!(nd::INPUTBYTES, 16);
        assert_eq!(nd::OUTPUTBYTES, 24);

        assert_eq!(ndx::KEYBYTES, 32);
        assert_eq!(ndx::TWEAKBYTES, 16);
        assert_eq!(ndx::INPUTBYTES, 16);
        assert_eq!(ndx::OUTPUTBYTES, 32);

        assert_eq!(pfx::KEYBYTES, 32);
        assert_eq!(pfx::BYTES, 16);
    }

    #[test]
    fn test_parse_ip_ipv4() {
        use super::{format_ip, parse_ip};

        let bin = parse_ip("192.0.2.1").unwrap();
        assert_eq!(format_ip(&bin), "192.0.2.1");

        let bin = parse_ip("10.0.0.1").unwrap();
        assert_eq!(format_ip(&bin), "10.0.0.1");

        let bin = parse_ip("255.255.255.255").unwrap();
        assert_eq!(format_ip(&bin), "255.255.255.255");
    }

    #[test]
    fn test_parse_ip_ipv6() {
        use super::{format_ip, parse_ip};

        let bin = parse_ip("2001:db8::1").unwrap();
        assert_eq!(format_ip(&bin), "2001:db8::1");

        let bin = parse_ip("::1").unwrap();
        assert_eq!(format_ip(&bin), "::1");

        let bin = parse_ip("fe80::1").unwrap();
        assert_eq!(format_ip(&bin), "fe80::1");

        // Full IPv6 address
        let bin = parse_ip("2001:0db8:85a3:0000:0000:8a2e:0370:7334").unwrap();
        // libsodium compresses it
        let formatted = format_ip(&bin);
        assert!(formatted.contains("2001:"));
    }

    #[test]
    fn test_parse_ip_invalid() {
        use super::parse_ip;

        assert!(parse_ip("invalid").is_err());
        assert!(parse_ip("256.0.0.1").is_err());
        assert!(parse_ip("").is_err());
    }

    #[test]
    fn test_encrypt_str_deterministic() {
        use super::{decrypt_str, encrypt_str, Key};

        let key = Key::generate();

        // IPv4
        let encrypted = encrypt_str("192.0.2.1", &key).unwrap();
        let decrypted = decrypt_str(&encrypted, &key).unwrap();
        assert_eq!(decrypted, "192.0.2.1");

        // IPv6
        let encrypted = encrypt_str("2001:db8::1", &key).unwrap();
        let decrypted = decrypt_str(&encrypted, &key).unwrap();
        assert_eq!(decrypted, "2001:db8::1");
    }

    #[test]
    fn test_nd_encrypt_str() {
        let key = Key::generate();

        let encrypted = nd::encrypt_str("192.0.2.1", &key).unwrap();
        assert_eq!(encrypted.len(), nd::OUTPUTBYTES * 2); // hex encoded

        let decrypted = nd::decrypt_str(&encrypted, &key).unwrap();
        assert_eq!(decrypted, "192.0.2.1");

        // IPv6
        let encrypted = nd::encrypt_str("2001:db8::1", &key).unwrap();
        let decrypted = nd::decrypt_str(&encrypted, &key).unwrap();
        assert_eq!(decrypted, "2001:db8::1");
    }

    #[test]
    fn test_nd_hex_encoding() {
        let key = Key::generate();
        let ip = ipv4_to_bytes([192, 0, 2, 1]);
        let tweak = nd::Tweak::random();

        let encrypted = nd::encrypt(&ip, &tweak, &key);
        let hex = nd::to_hex(&encrypted);
        assert_eq!(hex.len(), nd::OUTPUTBYTES * 2);

        let decoded = nd::from_hex(&hex).unwrap();
        assert_eq!(encrypted, decoded);
    }

    #[test]
    fn test_ndx_encrypt_str() {
        let key = ndx::Key::generate();

        let encrypted = ndx::encrypt_str("192.0.2.1", &key).unwrap();
        assert_eq!(encrypted.len(), ndx::OUTPUTBYTES * 2); // hex encoded

        let decrypted = ndx::decrypt_str(&encrypted, &key).unwrap();
        assert_eq!(decrypted, "192.0.2.1");

        // IPv6
        let encrypted = ndx::encrypt_str("2001:db8::1", &key).unwrap();
        let decrypted = ndx::decrypt_str(&encrypted, &key).unwrap();
        assert_eq!(decrypted, "2001:db8::1");
    }

    #[test]
    fn test_ndx_hex_encoding() {
        let key = ndx::Key::generate();
        let ip = ipv4_to_bytes([192, 0, 2, 1]);
        let tweak = ndx::Tweak::random();

        let encrypted = ndx::encrypt(&ip, &tweak, &key);
        let hex = ndx::to_hex(&encrypted);
        assert_eq!(hex.len(), ndx::OUTPUTBYTES * 2);

        let decoded = ndx::from_hex(&hex).unwrap();
        assert_eq!(encrypted, decoded);
    }

    #[test]
    fn test_pfx_encrypt_str() {
        let key = pfx::Key::generate();

        let encrypted = pfx::encrypt_str("192.0.2.1", &key).unwrap();
        let decrypted = pfx::decrypt_str(&encrypted, &key).unwrap();
        assert_eq!(decrypted, "192.0.2.1");

        // IPv6
        let encrypted = pfx::encrypt_str("2001:db8::1", &key).unwrap();
        let decrypted = pfx::decrypt_str(&encrypted, &key).unwrap();
        assert_eq!(decrypted, "2001:db8::1");
    }

    #[test]
    fn test_format_ip_consistency_with_manual_conversion() {
        use super::{bytes_to_ipv4, format_ip, ipv4_to_bytes, parse_ip};

        // Test that parse_ip produces the same result as ipv4_to_bytes for IPv4
        let manual = ipv4_to_bytes([192, 0, 2, 1]);
        let parsed = parse_ip("192.0.2.1").unwrap();
        assert_eq!(manual, parsed);

        // Test that format_ip can recover the original
        let formatted = format_ip(&manual);
        assert_eq!(formatted, "192.0.2.1");

        // Test bytes_to_ipv4 on result from parse_ip
        let ipv4 = bytes_to_ipv4(&parsed).unwrap();
        assert_eq!(ipv4, [192, 0, 2, 1]);
    }
}