pacha 0.2.5

Model, Data and Recipe Registry with full lineage tracking
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
//! Encryption at rest for model files (spec §3.3)
//!
//! Provides authenticated encryption for model distribution using:
//! - ChaCha20-Poly1305 AEAD (RFC 8439)
//! - Argon2id for password-based key derivation (RFC 9106)
//! - BLAKE3 for content verification
//!
//! ## Security
//!
//! - 256-bit key encryption (ChaCha20-Poly1305)
//! - Memory-hard password hashing (Argon2id)
//! - Authenticated encryption prevents tampering
//!
//! # Example
//!
//! ```no_run
//! use pacha::crypto::{encrypt_model, decrypt_model};
//!
//! // Encrypt a model file
//! let model_data = std::fs::read("model.gguf")?;
//! let encrypted = encrypt_model(&model_data, "my-secret-key")?;
//! std::fs::write("model.gguf.enc", &encrypted)?;
//!
//! // Decrypt at load time
//! let encrypted = std::fs::read("model.gguf.enc")?;
//! let decrypted = decrypt_model(&encrypted, "my-secret-key")?;
//! # Ok::<(), pacha::error::PachaError>(())
//! ```

use crate::error::{PachaError, Result};
use serde::{Deserialize, Serialize};

/// Magic bytes identifying encrypted pacha files
const MAGIC: &[u8; 8] = b"PACHAENC";

/// Current encryption format version
const VERSION: u8 = 1;

/// Salt length for key derivation (32 bytes)
const SALT_LEN: usize = 32;

/// Nonce length for ChaCha20-Poly1305 (12 bytes)
const NONCE_LEN: usize = 12;

/// Authentication tag length (16 bytes)
const TAG_LEN: usize = 16;

/// Header size: magic (8) + version (1) + salt (32) + nonce (12) = 53 bytes
const HEADER_SIZE: usize = 8 + 1 + SALT_LEN + NONCE_LEN;

/// Encrypted file header
#[derive(Debug, Clone)]
pub struct EncryptedHeader {
    /// Format version
    pub version: u8,
    /// Salt for key derivation
    pub salt: [u8; SALT_LEN],
    /// Nonce for encryption
    pub nonce: [u8; NONCE_LEN],
}

impl EncryptedHeader {
    /// Create a new header with random salt and nonce
    #[must_use]
    pub fn new() -> Self {
        #[cfg(feature = "encryption")]
        {
            use rand::rngs::OsRng;
            use rand::RngCore;
            let mut salt = [0u8; SALT_LEN];
            let mut nonce = [0u8; NONCE_LEN];
            OsRng.fill_bytes(&mut salt);
            OsRng.fill_bytes(&mut nonce);
            Self {
                version: VERSION,
                salt,
                nonce,
            }
        }
        #[cfg(not(feature = "encryption"))]
        {
            // Fallback: simple PRNG for salt and nonce
            let seed = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .map(|d| d.as_nanos())
                .unwrap_or(0);

            let mut salt = [0u8; SALT_LEN];
            let mut nonce = [0u8; NONCE_LEN];

            for (i, byte) in salt.iter_mut().enumerate() {
                *byte = ((seed >> (i % 16)) ^ (i as u128 * 7)) as u8;
            }
            for (i, byte) in nonce.iter_mut().enumerate() {
                *byte = ((seed >> ((i + 32) % 16)) ^ (i as u128 * 13)) as u8;
            }

            Self {
                version: VERSION,
                salt,
                nonce,
            }
        }
    }

    /// Serialize header to bytes
    #[must_use]
    pub fn to_bytes(&self) -> Vec<u8> {
        let mut bytes = Vec::with_capacity(HEADER_SIZE);
        bytes.extend_from_slice(MAGIC);
        bytes.push(self.version);
        bytes.extend_from_slice(&self.salt);
        bytes.extend_from_slice(&self.nonce);
        bytes
    }

    /// Parse header from bytes
    pub fn from_bytes(data: &[u8]) -> Result<Self> {
        if data.len() < HEADER_SIZE {
            return Err(PachaError::InvalidFormat(
                "encrypted file too short".to_string(),
            ));
        }

        // Verify magic
        if &data[0..8] != MAGIC {
            return Err(PachaError::InvalidFormat(
                "not an encrypted pacha file".to_string(),
            ));
        }

        let version = data[8];
        if version != VERSION {
            return Err(PachaError::InvalidFormat(format!(
                "unsupported encryption version: {}",
                version
            )));
        }

        let mut salt = [0u8; SALT_LEN];
        salt.copy_from_slice(&data[9..9 + SALT_LEN]);

        let mut nonce = [0u8; NONCE_LEN];
        nonce.copy_from_slice(&data[9 + SALT_LEN..HEADER_SIZE]);

        Ok(Self {
            version,
            salt,
            nonce,
        })
    }
}

impl Default for EncryptedHeader {
    fn default() -> Self {
        Self::new()
    }
}

/// Encryption configuration for Argon2id
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EncryptionConfig {
    /// Argon2 memory cost in KiB (default: 64MB)
    pub memory_cost_kib: u32,
    /// Argon2 time cost (iterations, default: 3)
    pub time_cost: u32,
    /// Argon2 parallelism (default: 4)
    pub parallelism: u32,
}

impl Default for EncryptionConfig {
    fn default() -> Self {
        Self {
            memory_cost_kib: 65536, // 64 MB
            time_cost: 3,
            parallelism: 4,
        }
    }
}

/// Derive encryption key from password using Argon2id
#[cfg(feature = "encryption")]
fn derive_key(
    password: &str,
    salt: &[u8; SALT_LEN],
    config: &EncryptionConfig,
) -> Result<[u8; 32]> {
    use argon2::{Algorithm, Argon2, Params, Version};

    let params = Params::new(
        config.memory_cost_kib,
        config.time_cost,
        config.parallelism,
        Some(32),
    )
    .map_err(|e| PachaError::Validation(format!("Invalid Argon2 params: {e}")))?;

    let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);

    let mut key = [0u8; 32];
    argon2
        .hash_password_into(password.as_bytes(), salt, &mut key)
        .map_err(|e| PachaError::Validation(format!("Key derivation failed: {e}")))?;

    Ok(key)
}

/// Fallback key derivation when encryption feature is disabled
#[cfg(not(feature = "encryption"))]
fn derive_key(
    password: &str,
    salt: &[u8; SALT_LEN],
    _config: &EncryptionConfig,
) -> Result<[u8; 32]> {
    // Simple key derivation using iterated hashing (NOT SECURE - fallback only)
    let mut key = [0u8; 32];
    let mut state = [0u8; 64];

    for (i, &b) in password.as_bytes().iter().enumerate() {
        state[i % 64] ^= b;
    }
    for (i, &b) in salt.iter().enumerate() {
        state[(i + 32) % 64] ^= b;
    }

    for iteration in 0..10000u32 {
        let iter_bytes = iteration.to_le_bytes();
        for (i, &b) in iter_bytes.iter().enumerate() {
            state[i] ^= b;
        }
        for i in 0..64 {
            state[i] = state[i].wrapping_add(state[(i + 1) % 64]).wrapping_mul(33);
        }
    }

    key.copy_from_slice(&state[0..32]);
    Ok(key)
}

/// Encrypt data using ChaCha20-Poly1305
#[cfg(feature = "encryption")]
fn chacha_encrypt(data: &[u8], key: &[u8; 32], nonce: &[u8; NONCE_LEN]) -> Result<Vec<u8>> {
    use chacha20poly1305::{
        aead::{Aead, KeyInit},
        ChaCha20Poly1305, Nonce,
    };

    let cipher = ChaCha20Poly1305::new_from_slice(key)
        .map_err(|e| PachaError::Validation(format!("Invalid key: {e}")))?;

    let nonce = Nonce::from_slice(nonce);

    cipher
        .encrypt(nonce, data)
        .map_err(|e| PachaError::Validation(format!("Encryption failed: {e}")))
}

/// Decrypt data using ChaCha20-Poly1305
#[cfg(feature = "encryption")]
fn chacha_decrypt(ciphertext: &[u8], key: &[u8; 32], nonce: &[u8; NONCE_LEN]) -> Result<Vec<u8>> {
    use chacha20poly1305::{
        aead::{Aead, KeyInit},
        ChaCha20Poly1305, Nonce,
    };

    let cipher = ChaCha20Poly1305::new_from_slice(key)
        .map_err(|e| PachaError::Validation(format!("Invalid key: {e}")))?;

    let nonce = Nonce::from_slice(nonce);

    cipher.decrypt(nonce, ciphertext).map_err(|_| {
        PachaError::InvalidFormat(
            "decryption failed: invalid password or corrupted data".to_string(),
        )
    })
}

/// Fallback XOR-based encryption (NOT SECURE - only for when feature disabled)
#[cfg(not(feature = "encryption"))]
fn chacha_encrypt(data: &[u8], key: &[u8; 32], nonce: &[u8; NONCE_LEN]) -> Result<Vec<u8>> {
    let mut output = data.to_vec();
    let mut keystream = [0u8; 64];

    for (block_idx, chunk) in output.chunks_mut(64).enumerate() {
        for (i, ks) in keystream.iter_mut().enumerate() {
            *ks = key[i % 32]
                .wrapping_add(nonce[i % NONCE_LEN])
                .wrapping_add(block_idx as u8)
                .wrapping_mul(i as u8 + 1);
        }
        for (i, byte) in chunk.iter_mut().enumerate() {
            *byte ^= keystream[i];
        }
    }

    // Append simplified tag
    let tag = compute_fallback_tag(&output, key);
    output.extend_from_slice(&tag);

    Ok(output)
}

#[cfg(not(feature = "encryption"))]
fn chacha_decrypt(ciphertext: &[u8], key: &[u8; 32], nonce: &[u8; NONCE_LEN]) -> Result<Vec<u8>> {
    if ciphertext.len() < TAG_LEN {
        return Err(PachaError::InvalidFormat(
            "ciphertext too short".to_string(),
        ));
    }

    let data = &ciphertext[..ciphertext.len() - TAG_LEN];
    let stored_tag = &ciphertext[ciphertext.len() - TAG_LEN..];

    // Verify tag
    let computed_tag = compute_fallback_tag(data, key);
    if computed_tag != stored_tag {
        return Err(PachaError::InvalidFormat(
            "decryption failed: invalid password or corrupted data".to_string(),
        ));
    }

    // Decrypt
    let mut output = data.to_vec();
    let mut keystream = [0u8; 64];

    for (block_idx, chunk) in output.chunks_mut(64).enumerate() {
        for (i, ks) in keystream.iter_mut().enumerate() {
            *ks = key[i % 32]
                .wrapping_add(nonce[i % NONCE_LEN])
                .wrapping_add(block_idx as u8)
                .wrapping_mul(i as u8 + 1);
        }
        for (i, byte) in chunk.iter_mut().enumerate() {
            *byte ^= keystream[i];
        }
    }

    Ok(output)
}

#[cfg(not(feature = "encryption"))]
fn compute_fallback_tag(ciphertext: &[u8], key: &[u8; 32]) -> [u8; TAG_LEN] {
    let mut tag = [0u8; TAG_LEN];
    let mut state = [0u64; 4];

    for (i, &b) in key.iter().enumerate() {
        state[i % 4] ^= (b as u64) << ((i * 8) % 64);
    }

    for (i, &b) in ciphertext.iter().enumerate() {
        state[i % 4] = state[i % 4]
            .wrapping_add(b as u64)
            .wrapping_mul(0x100000001b3);
    }

    for (i, byte) in tag.iter_mut().enumerate() {
        *byte = (state[i % 4] >> ((i % 8) * 8)) as u8;
    }

    tag
}

/// Encrypt model data with password
///
/// Uses ChaCha20-Poly1305 for authenticated encryption and Argon2id for
/// key derivation. Returns encrypted data with header.
pub fn encrypt_model(data: &[u8], password: &str) -> Result<Vec<u8>> {
    encrypt_model_with_config(data, password, &EncryptionConfig::default())
}

/// Encrypt model data with password and custom config
pub fn encrypt_model_with_config(
    data: &[u8],
    password: &str,
    config: &EncryptionConfig,
) -> Result<Vec<u8>> {
    if password.is_empty() {
        return Err(PachaError::InvalidFormat(
            "encryption password cannot be empty".to_string(),
        ));
    }

    let header = EncryptedHeader::new();
    let key = derive_key(password, &header.salt, config)?;

    // Encrypt data (includes auth tag for real implementation)
    let ciphertext = chacha_encrypt(data, &key, &header.nonce)?;

    // Assemble output: header + ciphertext (tag is included in ciphertext for chacha20poly1305)
    let mut output = header.to_bytes();
    output.extend_from_slice(&ciphertext);

    Ok(output)
}

/// Decrypt model data with password
pub fn decrypt_model(encrypted_data: &[u8], password: &str) -> Result<Vec<u8>> {
    decrypt_model_with_config(encrypted_data, password, &EncryptionConfig::default())
}

/// Decrypt model data with password and custom config
pub fn decrypt_model_with_config(
    encrypted_data: &[u8],
    password: &str,
    config: &EncryptionConfig,
) -> Result<Vec<u8>> {
    if encrypted_data.len() < HEADER_SIZE + TAG_LEN {
        return Err(PachaError::InvalidFormat(
            "encrypted data too short".to_string(),
        ));
    }

    // Parse header
    let header = EncryptedHeader::from_bytes(encrypted_data)?;

    // Extract ciphertext (includes auth tag)
    let ciphertext = &encrypted_data[HEADER_SIZE..];

    // Derive key
    let key = derive_key(password, &header.salt, config)?;

    // Decrypt and verify (ChaCha20-Poly1305 verifies tag internally)
    chacha_decrypt(ciphertext, &key, &header.nonce)
}

/// Check if data appears to be encrypted
#[must_use]
pub fn is_encrypted(data: &[u8]) -> bool {
    data.len() >= 8 && &data[0..8] == MAGIC
}

/// Get encryption format version from encrypted data
pub fn get_version(data: &[u8]) -> Result<u8> {
    if data.len() < 9 {
        return Err(PachaError::InvalidFormat(
            "data too short for version check".to_string(),
        ));
    }
    if &data[0..8] != MAGIC {
        return Err(PachaError::InvalidFormat(
            "not an encrypted pacha file".to_string(),
        ));
    }
    Ok(data[8])
}

// ============================================================================
// Tests - Extreme TDD
// ============================================================================

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

    // -------------------------------------------------------------------------
    // Core Encryption/Decryption Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_encrypt_decrypt_roundtrip() {
        let original = b"Hello, this is test model data!";
        let password = "my-secret-password";

        let encrypted = encrypt_model(original, password).unwrap();
        let decrypted = decrypt_model(&encrypted, password).unwrap();

        assert_eq!(original.as_slice(), decrypted.as_slice());
    }

    #[test]
    fn test_encrypt_decrypt_large_data() {
        let original: Vec<u8> = (0..10000).map(|i| (i % 256) as u8).collect();
        let password = "test-password-123";

        let encrypted = encrypt_model(&original, password).unwrap();
        let decrypted = decrypt_model(&encrypted, password).unwrap();

        assert_eq!(original, decrypted);
    }

    #[test]
    fn test_encrypt_decrypt_1mb_data() {
        let original: Vec<u8> = (0..1024 * 1024).map(|i| (i % 256) as u8).collect();
        let password = "strong-password";

        let encrypted = encrypt_model(&original, password).unwrap();
        let decrypted = decrypt_model(&encrypted, password).unwrap();

        assert_eq!(original.len(), decrypted.len());
        assert_eq!(original, decrypted);
    }

    #[test]
    fn test_empty_data_encrypt() {
        let original: &[u8] = &[];
        let password = "password";

        let encrypted = encrypt_model(original, password).unwrap();
        let decrypted = decrypt_model(&encrypted, password).unwrap();

        assert!(decrypted.is_empty());
    }

    // -------------------------------------------------------------------------
    // Authentication Tests (Tampering Detection)
    // -------------------------------------------------------------------------

    #[test]
    fn test_wrong_password_fails() {
        let original = b"Secret model data";
        let password = "correct-password";
        let wrong_password = "wrong-password";

        let encrypted = encrypt_model(original, password).unwrap();
        let result = decrypt_model(&encrypted, wrong_password);

        assert!(result.is_err());
    }

    #[test]
    fn test_empty_password_rejected() {
        let data = b"test data";
        let result = encrypt_model(data, "");

        assert!(result.is_err());
    }

    #[test]
    fn test_corrupted_ciphertext_fails() {
        let original = b"Test data for corruption test";
        let password = "password";

        let mut encrypted = encrypt_model(original, password).unwrap();

        // Corrupt a byte in the ciphertext
        if encrypted.len() > HEADER_SIZE + 5 {
            encrypted[HEADER_SIZE + 5] ^= 0xFF;
        }

        let result = decrypt_model(&encrypted, password);
        assert!(result.is_err(), "Should detect ciphertext corruption");
    }

    #[test]
    fn test_corrupted_tag_fails() {
        let original = b"Test data";
        let password = "password";

        let mut encrypted = encrypt_model(original, password).unwrap();

        // Corrupt the last byte (part of auth tag)
        let len = encrypted.len();
        encrypted[len - 1] ^= 0xFF;

        let result = decrypt_model(&encrypted, password);
        assert!(result.is_err(), "Should detect tag corruption");
    }

    #[test]
    fn test_truncated_data_fails() {
        let original = b"Test data";
        let password = "password";

        let encrypted = encrypt_model(original, password).unwrap();
        let truncated = &encrypted[..encrypted.len() - 10];

        let result = decrypt_model(truncated, password);
        assert!(result.is_err());
    }

    // -------------------------------------------------------------------------
    // Header Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_is_encrypted() {
        let original = b"Plain data";
        let password = "password";

        assert!(!is_encrypted(original));

        let encrypted = encrypt_model(original, password).unwrap();
        assert!(is_encrypted(&encrypted));
    }

    #[test]
    fn test_get_version() {
        let original = b"Test";
        let password = "pwd";

        let encrypted = encrypt_model(original, password).unwrap();
        let version = get_version(&encrypted).unwrap();

        assert_eq!(version, VERSION);
    }

    #[test]
    fn test_header_serialization() {
        let header = EncryptedHeader::new();
        let bytes = header.to_bytes();
        let parsed = EncryptedHeader::from_bytes(&bytes).unwrap();

        assert_eq!(header.version, parsed.version);
        assert_eq!(header.salt, parsed.salt);
        assert_eq!(header.nonce, parsed.nonce);
    }

    #[test]
    fn test_invalid_magic() {
        let mut data = vec![0u8; 100];
        data[0..8].copy_from_slice(b"NOTMAGIC");

        let result = EncryptedHeader::from_bytes(&data);
        assert!(result.is_err());
    }

    #[test]
    fn test_unsupported_version() {
        let mut data = vec![0u8; 100];
        data[0..8].copy_from_slice(MAGIC);
        data[8] = 99; // Unsupported version

        let result = EncryptedHeader::from_bytes(&data);
        assert!(result.is_err());
    }

    // -------------------------------------------------------------------------
    // Configuration Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_encryption_config_default() {
        let config = EncryptionConfig::default();

        assert_eq!(config.memory_cost_kib, 65536);
        assert_eq!(config.time_cost, 3);
        assert_eq!(config.parallelism, 4);
    }

    #[test]
    fn test_encrypt_with_custom_config() {
        let original = b"Custom config test";
        let password = "password";

        let config = EncryptionConfig {
            memory_cost_kib: 32768,
            time_cost: 2,
            parallelism: 2,
        };

        let encrypted = encrypt_model_with_config(original, password, &config).unwrap();
        let decrypted = decrypt_model_with_config(&encrypted, password, &config).unwrap();

        assert_eq!(original.as_slice(), decrypted.as_slice());
    }

    // -------------------------------------------------------------------------
    // Password Edge Cases
    // -------------------------------------------------------------------------

    #[test]
    fn test_special_characters_in_password() {
        let original = b"Test data";
        let password = "p@$$w0rd!#$%^&*()_+-=[]{}|;':\",./<>?";

        let encrypted = encrypt_model(original, password).unwrap();
        let decrypted = decrypt_model(&encrypted, password).unwrap();

        assert_eq!(original.as_slice(), decrypted.as_slice());
    }

    #[test]
    fn test_unicode_password() {
        let original = b"Test data";
        let password = "密码🔐пароль";

        let encrypted = encrypt_model(original, password).unwrap();
        let decrypted = decrypt_model(&encrypted, password).unwrap();

        assert_eq!(original.as_slice(), decrypted.as_slice());
    }

    #[test]
    fn test_very_long_password() {
        let original = b"Test data";
        let password = "a".repeat(10000);

        let encrypted = encrypt_model(original, &password).unwrap();
        let decrypted = decrypt_model(&encrypted, &password).unwrap();

        assert_eq!(original.as_slice(), decrypted.as_slice());
    }

    // -------------------------------------------------------------------------
    // Randomness/Uniqueness Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_different_encryptions_produce_different_ciphertext() {
        let original = b"Same data";
        let password = "same-password";

        let encrypted1 = encrypt_model(original, password).unwrap();
        let encrypted2 = encrypt_model(original, password).unwrap();

        // Different salt/nonce means different ciphertext
        assert_ne!(encrypted1, encrypted2);

        // But both decrypt correctly
        let decrypted1 = decrypt_model(&encrypted1, password).unwrap();
        let decrypted2 = decrypt_model(&encrypted2, password).unwrap();
        assert_eq!(decrypted1, decrypted2);
    }

    #[test]
    fn test_different_passwords_produce_different_ciphertext() {
        let original = b"Same data";

        let encrypted1 = encrypt_model(original, "password1").unwrap();
        let encrypted2 = encrypt_model(original, "password2").unwrap();

        assert_ne!(encrypted1, encrypted2);
    }

    // -------------------------------------------------------------------------
    // Size Tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_encryption_overhead() {
        let original = b"Test data for size check";
        let password = "password";

        let encrypted = encrypt_model(original, password).unwrap();

        // Overhead = header (53) + tag (16) = 69 bytes
        let min_overhead = HEADER_SIZE + TAG_LEN;
        assert!(encrypted.len() >= original.len() + min_overhead);
    }

    // -------------------------------------------------------------------------
    // Edge Cases
    // -------------------------------------------------------------------------

    #[test]
    fn test_single_byte_data() {
        let original = &[0x42u8];
        let password = "password";

        let encrypted = encrypt_model(original, password).unwrap();
        let decrypted = decrypt_model(&encrypted, password).unwrap();

        assert_eq!(original.as_slice(), decrypted.as_slice());
    }

    #[test]
    fn test_binary_data_with_nulls() {
        let original: Vec<u8> = vec![0, 0, 0, 1, 2, 3, 0, 0, 0];
        let password = "password";

        let encrypted = encrypt_model(&original, password).unwrap();
        let decrypted = decrypt_model(&encrypted, password).unwrap();

        assert_eq!(original, decrypted);
    }

    #[test]
    fn test_all_zeros_data() {
        let original = vec![0u8; 1000];
        let password = "password";

        let encrypted = encrypt_model(&original, password).unwrap();
        let decrypted = decrypt_model(&encrypted, password).unwrap();

        assert_eq!(original, decrypted);
    }

    #[test]
    fn test_all_ones_data() {
        let original = vec![0xFFu8; 1000];
        let password = "password";

        let encrypted = encrypt_model(&original, password).unwrap();
        let decrypted = decrypt_model(&encrypted, password).unwrap();

        assert_eq!(original, decrypted);
    }
}