descriptor-encrypt 0.1.2

Encrypt Bitcoin wallet descriptors such that only authorized spenders can decrypt
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
use anyhow::{Result, anyhow};
use chacha20::{
    ChaCha20,
    cipher::{KeyIvInit, StreamCipher},
};
use chacha20poly1305::{
    ChaCha20Poly1305,
    aead::{Aead, KeyInit},
};
use miniscript::descriptor::{DescriptorPublicKey, SinglePubKey};
use sha2::{Digest, Sha256};

/// A trait to construct a cipher
pub trait KeyCipher {
    /// Returns an encrypted payload
    fn encrypt_payload(
        &self,
        payload: Vec<u8>,
        encryption_key: [u8; 32],
        nonce: [u8; 12],
    ) -> Result<Vec<u8>>;

    /// Returns a decrypted payload
    fn decrypt_payload(
        &self,
        encrypted_payload: Vec<u8>,
        encryption_key: [u8; 32],
        nonce: [u8; 12],
    ) -> Result<Vec<u8>>;

    /// Returns a share encrypted using a public key, a derivation hash, and an index
    fn encrypt_share(
        &self,
        share: Vec<u8>,
        pk: &DescriptorPublicKey,
        hash: &[u8; 32],
        index: usize,
    ) -> Result<Vec<u8>>;

    /// Returns plaintext decrypted using a set of public keys, a derivation hash, and an index
    fn decrypt_share(
        &self,
        encrypted_share: Vec<u8>,
        pks: &[Option<&DescriptorPublicKey>],
        hash: &[u8; 32],
        index: usize,
    ) -> Result<Vec<u8>>;
}

pub struct AuthenticatedCipher {}

pub struct UnauthenticatedCipher {}

impl KeyCipher for AuthenticatedCipher {
    fn encrypt_payload(
        &self,
        payload: Vec<u8>,
        encryption_key: [u8; 32],
        nonce: [u8; 12],
    ) -> Result<Vec<u8>> {
        Ok(apply_chacha20(payload, encryption_key, nonce))
    }

    fn decrypt_payload(
        &self,
        encrypted_payload: Vec<u8>,
        encryption_key: [u8; 32],
        nonce: [u8; 12],
    ) -> Result<Vec<u8>> {
        Ok(apply_chacha20(encrypted_payload, encryption_key, nonce))
    }

    fn encrypt_share(
        &self,
        share: Vec<u8>,
        pk: &DescriptorPublicKey,
        hash: &[u8; 32],
        index: usize,
    ) -> Result<Vec<u8>> {
        let encryption_key = get_encryption_key(pk, hash, index);
        // We can safely use a zero nonce because the key is unique to the ciphertext and index
        let nonce = [0u8; 12];
        let (nonce, cipher) = get_chacha20_poly1305_cipher(encryption_key, nonce)?;
        let encrypted_share = cipher
            .encrypt(&nonce, share.as_ref())
            .map_err(|e| anyhow::anyhow!("ChaCha20Poly1305 encryption error: {:?}", e))?;

        Ok(encrypted_share.as_slice().into())
    }

    fn decrypt_share(
        &self,
        encrypted_share: Vec<u8>,
        pks: &[Option<&DescriptorPublicKey>],
        hash: &[u8; 32],
        index: usize,
    ) -> Result<Vec<u8>> {
        for pk in pks {
            let Some(pk) = pk else {
                continue;
            };
            let encryption_key = get_encryption_key(pk, hash, index);
            let nonce = [0u8; 12];
            let Ok((nonce, cipher)) = get_chacha20_poly1305_cipher(encryption_key, nonce) else {
                continue;
            };
            let Ok(share) = cipher.decrypt(&nonce, encrypted_share.as_ref()) else {
                continue;
            };

            return Ok(share);
        }

        Err(anyhow!("Failed to decrypt"))
    }
}

impl KeyCipher for UnauthenticatedCipher {
    fn encrypt_payload(
        &self,
        payload: Vec<u8>,
        encryption_key: [u8; 32],
        nonce: [u8; 12],
    ) -> Result<Vec<u8>> {
        let (nonce, cipher) = get_chacha20_poly1305_cipher(encryption_key, nonce)?;
        let encrypted_payload = cipher
            .encrypt(&nonce, payload.as_ref())
            .map_err(|e| anyhow::anyhow!("ChaCha20Poly1305 encryption error: {:?}", e))?;

        Ok(encrypted_payload.as_slice().into())
    }

    fn decrypt_payload(
        &self,
        encrypted_payload: Vec<u8>,
        encryption_key: [u8; 32],
        nonce: [u8; 12],
    ) -> Result<Vec<u8>> {
        let (nonce, cipher) = get_chacha20_poly1305_cipher(encryption_key, nonce)?;
        cipher
            .decrypt(&nonce, encrypted_payload.as_ref())
            .map_err(|e| anyhow::anyhow!("ChaCha20Poly1305 decryption error: {:?}", e))
    }

    fn encrypt_share(
        &self,
        share: Vec<u8>,
        pk: &DescriptorPublicKey,
        hash: &[u8; 32],
        index: usize,
    ) -> Result<Vec<u8>> {
        let encryption_key = get_encryption_key(pk, hash, index);
        let nonce = [0u8; 12];

        Ok(apply_chacha20(share, encryption_key, nonce))
    }

    fn decrypt_share(
        &self,
        encrypted_share: Vec<u8>,
        pks: &[Option<&DescriptorPublicKey>],
        hash: &[u8; 32],
        index: usize,
    ) -> Result<Vec<u8>> {
        if index >= pks.len() {
            return Err(anyhow!("Insufficient keys for index {}", index));
        }

        let Some(pk) = pks[index] else {
            return Err(anyhow!("No key exists at index {}", index));
        };

        let encryption_key = get_encryption_key(pk, hash, index);
        let nonce = [0u8; 12];

        Ok(apply_chacha20(encrypted_share, encryption_key, nonce))
    }
}

fn apply_chacha20(plaintext: Vec<u8>, encryption_key: [u8; 32], nonce: [u8; 12]) -> Vec<u8> {
    let mut cipher = ChaCha20::new(&encryption_key.into(), &nonce.into());
    let mut buffer = plaintext.clone();
    cipher.apply_keystream(&mut buffer);
    buffer
}

fn get_chacha20_poly1305_cipher(
    encryption_key: [u8; 32],
    nonce: [u8; 12],
) -> Result<(chacha20poly1305::Nonce, ChaCha20Poly1305)> {
    let nonce = chacha20poly1305::Nonce::from_slice(&nonce);
    let cipher = ChaCha20Poly1305::new_from_slice(&encryption_key)
        .map_err(|e| anyhow::anyhow!("ChaCha20Poly1305 key error: {:?}", e))?;

    Ok((*nonce, cipher))
}

fn get_encryption_key(pk: &DescriptorPublicKey, hash: &[u8; 32], leaf_index: usize) -> [u8; 32] {
    let mut key_material = match pk {
        DescriptorPublicKey::Single(single_pub) => match single_pub.key {
            SinglePubKey::FullKey(full_pk) => full_pk.inner.serialize().to_vec(),
            SinglePubKey::XOnly(xpk) => xpk.serialize().to_vec(),
        },
        DescriptorPublicKey::XPub(xkey) => xkey.xkey.encode().to_vec(),
        DescriptorPublicKey::MultiXPub(multi_xkey) => multi_xkey.xkey.encode().to_vec(),
    };
    key_material.extend(*hash);
    key_material.extend(leaf_index.to_le_bytes().to_vec());

    let mut hasher = Sha256::new();
    hasher.update(key_material);
    let encryption_key = hasher.finalize();

    let mut result = [0u8; 32];
    result.copy_from_slice(encryption_key.as_slice());
    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use bitcoin::{
        PublicKey,
        secp256k1::{PublicKey as SecpPublicKey, Secp256k1, SecretKey},
    };
    use miniscript::descriptor::SinglePub;

    // Helper function to create a DescriptorPublicKey for testing
    fn create_test_pk(seed_val: u32) -> DescriptorPublicKey {
        let secp = Secp256k1::new();
        let mut sk_bytes = [0u8; 32];
        sk_bytes[0..4].copy_from_slice(&seed_val.to_be_bytes());

        let secret_key = SecretKey::from_slice(&sk_bytes)
            .unwrap_or_else(|_| panic!("Failed to create secret key from seed {}", seed_val));

        let pk_inner = SecpPublicKey::from_secret_key(&secp, &secret_key);

        let full_pk = PublicKey {
            inner: pk_inner,
            compressed: true,
        };

        DescriptorPublicKey::Single(SinglePub {
            key: SinglePubKey::FullKey(full_pk),
            origin: None,
        })
    }

    // Helper function to create a dummy 32-byte hash
    fn create_dummy_hash(seed: u8) -> [u8; 32] {
        let mut seed_data = [0u8; 32];
        seed_data[0] = seed;
        Sha256::digest(seed_data).into()
    }

    #[test]
    fn test_encrypt_decrypt_cycle_succeeds() {
        let cipher = AuthenticatedCipher {};
        let pk = create_test_pk(1);
        let hash = create_dummy_hash(1);
        let index = 0_usize;
        let plaintext = b"this is a secret message".to_vec();

        let ciphertext = cipher
            .encrypt_share(plaintext.clone(), &pk, &hash, index)
            .unwrap();

        let decrypted_plaintext = cipher.decrypt_share(ciphertext, &[Some(&pk)], &hash, index);

        assert_eq!(
            decrypted_plaintext.unwrap(),
            plaintext,
            "Decrypted plaintext should match original."
        );
    }

    #[test]
    fn test_decrypt_with_wrong_public_key_fails() {
        let cipher = AuthenticatedCipher {};
        let pk1 = create_test_pk(1);
        let pk2 = create_test_pk(2);
        let hash = create_dummy_hash(1);
        let index = 0_usize;
        let plaintext = b"another secret".to_vec();

        let ciphertext = cipher
            .encrypt_share(plaintext.clone(), &pk1, &hash, index)
            .unwrap();

        let decrypted_plaintext = cipher.decrypt_share(ciphertext, &[Some(&pk2)], &hash, index);

        assert!(
            decrypted_plaintext.is_err(),
            "Decryption should fail with the wrong public key."
        );
    }

    #[test]
    fn test_decrypt_with_wrong_hash_fails() {
        let cipher = AuthenticatedCipher {};
        let pk = create_test_pk(1);
        let hash1 = create_dummy_hash(1);
        let hash2 = create_dummy_hash(2);
        let index = 0_usize;
        let plaintext = b"secret with hash".to_vec();

        let ciphertext = cipher
            .encrypt_share(plaintext.clone(), &pk, &hash1, index)
            .unwrap();

        let decrypted_plaintext = cipher.decrypt_share(ciphertext, &[Some(&pk)], &hash2, index);

        assert!(
            decrypted_plaintext.is_err(),
            "Decryption should fail with the wrong hash."
        );
    }

    #[test]
    fn test_decrypt_with_wrong_index_fails() {
        let cipher = AuthenticatedCipher {};
        let pk = create_test_pk(1);
        let hash = create_dummy_hash(1);
        let index1 = 0_usize;
        let index2 = 1_usize;
        let plaintext = b"secret with index".to_vec();

        let ciphertext = cipher
            .encrypt_share(plaintext.clone(), &pk, &hash, index1)
            .unwrap();

        let decrypted_plaintext = cipher.decrypt_share(ciphertext, &[Some(&pk)], &hash, index2);

        assert!(
            decrypted_plaintext.is_err(),
            "Decryption should fail with the wrong index."
        );
    }

    #[test]
    fn test_decrypt_with_list_of_pks_correct_key_present_succeeds() {
        let cipher = AuthenticatedCipher {};
        let pk_correct = create_test_pk(10);
        let pk_wrong1 = create_test_pk(11);
        let pk_wrong2 = create_test_pk(12);
        let hash = create_dummy_hash(5);
        let index = 3_usize;
        let plaintext = b"find the right key!".to_vec();

        let ciphertext = cipher
            .encrypt_share(plaintext.clone(), &pk_correct, &hash, index)
            .unwrap();

        let pks_list = vec![Some(&pk_wrong1), Some(&pk_correct), Some(&pk_wrong2)];
        let decrypted_plaintext = cipher.decrypt_share(ciphertext, &pks_list, &hash, index);

        assert_eq!(
            decrypted_plaintext.unwrap(),
            plaintext,
            "Decryption should succeed if the correct key is in the list."
        );
    }

    #[test]
    fn test_decrypt_with_list_of_pks_correct_key_absent_fails() {
        let cipher = AuthenticatedCipher {};
        let pk_correct = create_test_pk(20);
        let pk_wrong1 = create_test_pk(21);
        let pk_wrong2 = create_test_pk(22);
        let hash = create_dummy_hash(6);
        let index = 4_usize;
        let plaintext = b"key not here".to_vec();

        let ciphertext = cipher
            .encrypt_share(plaintext.clone(), &pk_correct, &hash, index)
            .unwrap();

        let pks_list = vec![Some(&pk_wrong1), Some(&pk_wrong2)];
        let decrypted_plaintext = cipher.decrypt_share(ciphertext, &pks_list, &hash, index);

        assert!(
            decrypted_plaintext.is_err(),
            "Decryption should fail if the correct key is not in the list."
        );
    }

    #[test]
    fn test_empty_plaintext_encrypt_decrypt_succeeds() {
        let cipher = AuthenticatedCipher {};
        let pk = create_test_pk(30);
        let hash = create_dummy_hash(7);
        let index = 5_usize;
        let plaintext = Vec::new();

        let ciphertext = cipher
            .encrypt_share(plaintext.clone(), &pk, &hash, index)
            .unwrap();

        let decrypted_plaintext = cipher.decrypt_share(ciphertext, &[Some(&pk)], &hash, index);

        assert_eq!(
            decrypted_plaintext.unwrap(),
            plaintext,
            "Encryption/decryption of empty plaintext should work."
        );
    }

    #[test]
    fn test_decrypt_with_empty_pk_list_fails() {
        let cipher = AuthenticatedCipher {};
        let pk_correct = create_test_pk(40);
        let hash = create_dummy_hash(8);
        let index = 6_usize;
        let plaintext = b"no keys to try".to_vec();

        let ciphertext = cipher
            .encrypt_share(plaintext.clone(), &pk_correct, &hash, index)
            .unwrap();

        let pks_list_empty = Vec::new();
        let decrypted_plaintext = cipher.decrypt_share(ciphertext, &pks_list_empty, &hash, index);

        assert!(
            decrypted_plaintext.is_err(),
            "Decryption should fail if the list of public keys is empty."
        );
    }

    #[test]
    fn test_different_pks_produce_different_ciphertexts() {
        let cipher = AuthenticatedCipher {};
        let pk1 = create_test_pk(51);
        let pk2 = create_test_pk(52);
        let hash = create_dummy_hash(9);
        let index = 7_usize;
        let plaintext = b"same data, different key".to_vec();

        let ciphertext1 = cipher
            .encrypt_share(plaintext.clone(), &pk1, &hash, index)
            .unwrap();
        let ciphertext2 = cipher
            .encrypt_share(plaintext.clone(), &pk2, &hash, index)
            .unwrap();

        assert_ne!(
            ciphertext1, ciphertext2,
            "Ciphertexts should differ if public keys differ."
        );
    }

    #[test]
    fn test_different_hashes_produce_different_ciphertexts() {
        let cipher = AuthenticatedCipher {};
        let pk = create_test_pk(60);
        let hash1 = create_dummy_hash(10);
        let hash2 = create_dummy_hash(11);
        let index = 8_usize;
        let plaintext = b"same data, different hash".to_vec();

        let ciphertext1 = cipher
            .encrypt_share(plaintext.clone(), &pk, &hash1, index)
            .unwrap();
        let ciphertext2 = cipher
            .encrypt_share(plaintext.clone(), &pk, &hash2, index)
            .unwrap();

        assert_ne!(
            ciphertext1, ciphertext2,
            "Ciphertexts should differ if hashes differ."
        );
    }

    #[test]
    fn test_different_indices_produce_different_ciphertexts() {
        let cipher = AuthenticatedCipher {};
        let pk = create_test_pk(70);
        let hash = create_dummy_hash(12);
        let index1 = 9_usize;
        let index2 = 10_usize;
        let plaintext = b"same data, different index".to_vec();

        let ciphertext1 = cipher
            .encrypt_share(plaintext.clone(), &pk, &hash, index1)
            .unwrap();
        let ciphertext2 = cipher
            .encrypt_share(plaintext.clone(), &pk, &hash, index2)
            .unwrap();

        assert_ne!(
            ciphertext1, ciphertext2,
            "Ciphertexts should differ if indices differ."
        );
    }

    // UnauthenticatedCipher Tests

    #[test]
    fn test_encrypt_decrypt_cycle_succeeds_unauthenticated() {
        let cipher = UnauthenticatedCipher {};
        let pk = create_test_pk(101);
        let hash = create_dummy_hash(101);
        let index = 0_usize;
        let plaintext = b"unauth secret message".to_vec();

        let ciphertext = cipher
            .encrypt_share(plaintext.clone(), &pk, &hash, index)
            .unwrap();

        let decrypted_plaintext = cipher
            .decrypt_share(ciphertext, &[Some(&pk)], &hash, index)
            .unwrap();

        assert_eq!(
            decrypted_plaintext, plaintext,
            "Unauthenticated decrypted plaintext should match original."
        );
    }

    #[test]
    fn test_decrypt_unauthenticated_with_wrong_public_key_at_index_garbles() {
        let cipher = UnauthenticatedCipher {};
        let pk1_enc = create_test_pk(102);
        let pk2_dec = create_test_pk(103);
        let hash = create_dummy_hash(102);
        let index = 0_usize;
        let plaintext = b"unauth wrong pk".to_vec();

        let ciphertext = cipher
            .encrypt_share(plaintext.clone(), &pk1_enc, &hash, index)
            .unwrap();

        let decrypted_plaintext = cipher
            .decrypt_share(ciphertext, &[Some(&pk2_dec)], &hash, index)
            .unwrap();

        assert_ne!(
            decrypted_plaintext, plaintext,
            "Unauthenticated decryption with wrong PK at index should produce different data."
        );
    }

    #[test]
    fn test_decrypt_unauthenticated_with_wrong_hash_garbles() {
        let cipher = UnauthenticatedCipher {};
        let pk = create_test_pk(104);
        let hash1_enc = create_dummy_hash(103);
        let hash2_dec = create_dummy_hash(104);
        let index = 0_usize;
        let plaintext = b"unauth wrong hash".to_vec();

        let ciphertext = cipher
            .encrypt_share(plaintext.clone(), &pk, &hash1_enc, index)
            .unwrap();

        let decrypted_plaintext = cipher
            .decrypt_share(ciphertext, &[Some(&pk)], &hash2_dec, index)
            .unwrap();

        assert_ne!(
            decrypted_plaintext, plaintext,
            "Unauthenticated decryption with wrong hash should produce different data."
        );
    }

    #[test]
    fn test_decrypt_unauthenticated_with_wrong_index_param_garbles() {
        let cipher = UnauthenticatedCipher {};
        let pk = create_test_pk(105);
        let hash = create_dummy_hash(105);
        let index1_enc = 0_usize;
        let index2_dec = 1_usize;
        let plaintext = b"unauth wrong index param".to_vec();

        let ciphertext = cipher
            .encrypt_share(plaintext.clone(), &pk, &hash, index1_enc)
            .unwrap();

        let pks_for_decryption = if index2_dec == 0 {
            vec![Some(&pk)]
        } else {
            vec![None, Some(&pk)]
        };

        let decrypted_plaintext = cipher
            .decrypt_share(ciphertext, &pks_for_decryption, &hash, index2_dec)
            .unwrap();

        assert_ne!(
            decrypted_plaintext, plaintext,
            "Unauthenticated decryption with wrong index parameter should produce different data."
        );
    }

    #[test]
    fn test_decrypt_unauthenticated_with_correct_pk_at_specified_index_succeeds() {
        let cipher = UnauthenticatedCipher {};
        let pk_correct = create_test_pk(110);
        let pk_other = create_test_pk(111);
        let hash = create_dummy_hash(110);
        let encrypt_idx = 1_usize;
        let decrypt_idx = 1_usize;
        let plaintext = b"unauth correct pk at index".to_vec();

        let ciphertext = cipher
            .encrypt_share(plaintext.clone(), &pk_correct, &hash, encrypt_idx)
            .unwrap();

        let pks_list = vec![Some(&pk_other), Some(&pk_correct), Some(&pk_other)];
        let decrypted_plaintext = cipher
            .decrypt_share(ciphertext, &pks_list, &hash, decrypt_idx)
            .unwrap();

        assert_eq!(
            decrypted_plaintext, plaintext,
            "Unauthenticated decryption should succeed if correct PK is at specified index."
        );
    }

    #[test]
    fn test_decrypt_unauthenticated_with_wrong_pk_at_specified_index_garbles() {
        let cipher = UnauthenticatedCipher {};
        let pk_encrypt = create_test_pk(112);
        let pk_decrypt_wrong = create_test_pk(113);
        let pk_other = create_test_pk(114);
        let hash = create_dummy_hash(111);
        let encrypt_idx = 0_usize;
        let decrypt_idx = 0_usize;
        let plaintext = b"unauth wrong pk at index".to_vec();

        let ciphertext = cipher
            .encrypt_share(plaintext.clone(), &pk_encrypt, &hash, encrypt_idx)
            .unwrap();

        let pks_list = vec![Some(&pk_decrypt_wrong), Some(&pk_other)];
        let decrypted_plaintext = cipher
            .decrypt_share(ciphertext, &pks_list, &hash, decrypt_idx)
            .unwrap();

        assert_ne!(
            decrypted_plaintext, plaintext,
            "Unauthenticated decryption with wrong PK at specified index should garble."
        );
    }

    #[test]
    fn test_empty_plaintext_encrypt_decrypt_succeeds_unauthenticated() {
        let cipher = UnauthenticatedCipher {};
        let pk = create_test_pk(130);
        let hash = create_dummy_hash(130);
        let index = 0_usize;
        let plaintext = Vec::new();

        let ciphertext = cipher
            .encrypt_share(plaintext.clone(), &pk, &hash, index)
            .unwrap();

        let decrypted_plaintext = cipher
            .decrypt_share(ciphertext, &[Some(&pk)], &hash, index)
            .unwrap();

        assert_eq!(
            decrypted_plaintext, plaintext,
            "Unauthenticated encryption/decryption of empty plaintext should work."
        );
    }

    #[test]
    fn test_decrypt_unauthenticated_with_empty_pk_list_fails() {
        let cipher = UnauthenticatedCipher {};
        let pk_correct = create_test_pk(140);
        let hash = create_dummy_hash(140);
        let index_enc = 0_usize;
        let index_dec = 0_usize;
        let plaintext = b"unauth no keys".to_vec();

        let ciphertext = cipher
            .encrypt_share(plaintext.clone(), &pk_correct, &hash, index_enc)
            .unwrap();

        let pks_list_empty: Vec<Option<&DescriptorPublicKey>> = Vec::new();
        let result = cipher.decrypt_share(ciphertext, &pks_list_empty, &hash, index_dec);

        assert!(
            result.is_err(),
            "Unauthenticated decryption should fail if PK list is empty and index is accessed."
        );
        assert_eq!(
            result.unwrap_err().to_string(),
            format!("Insufficient keys for index {}", index_dec)
        );
    }

    #[test]
    fn test_decrypt_unauthenticated_with_out_of_bounds_index_fails() {
        let cipher = UnauthenticatedCipher {};
        let pk = create_test_pk(141);
        let hash = create_dummy_hash(141);
        let index_enc = 0_usize;
        let index_dec = 1_usize;
        let plaintext = b"unauth out of bounds".to_vec();

        let ciphertext = cipher
            .encrypt_share(plaintext.clone(), &pk, &hash, index_enc)
            .unwrap();

        let pks_list = vec![Some(&pk)];
        let result = cipher.decrypt_share(ciphertext, &pks_list, &hash, index_dec);

        assert!(
            result.is_err(),
            "Unauthenticated decryption should fail for out-of-bounds index."
        );
        assert_eq!(
            result.unwrap_err().to_string(),
            format!("Insufficient keys for index {}", index_dec)
        );
    }

    #[test]
    fn test_decrypt_unauthenticated_with_none_pk_at_index_fails() {
        let cipher = UnauthenticatedCipher {};
        let pk_enc = create_test_pk(142);
        let hash = create_dummy_hash(142);
        let index_enc = 0_usize;
        let index_dec = 0_usize;
        let plaintext = b"unauth none pk".to_vec();

        let ciphertext = cipher
            .encrypt_share(plaintext.clone(), &pk_enc, &hash, index_enc)
            .unwrap();

        let pks_list = vec![None];
        let result = cipher.decrypt_share(ciphertext, &pks_list, &hash, index_dec);

        assert!(
            result.is_err(),
            "Unauthenticated decryption should fail if PK at index is None."
        );
        assert_eq!(
            result.unwrap_err().to_string(),
            format!("No key exists at index {}", index_dec)
        );
    }

    #[test]
    fn test_different_pks_produce_different_ciphertexts_unauthenticated() {
        let cipher = UnauthenticatedCipher {};
        let pk1 = create_test_pk(151);
        let pk2 = create_test_pk(152);
        let hash = create_dummy_hash(150);
        let index = 0_usize;
        let plaintext = b"unauth same data, diff key".to_vec();

        let ciphertext1 = cipher
            .encrypt_share(plaintext.clone(), &pk1, &hash, index)
            .unwrap();
        let ciphertext2 = cipher
            .encrypt_share(plaintext.clone(), &pk2, &hash, index)
            .unwrap();

        assert_ne!(
            ciphertext1, ciphertext2,
            "Unauthenticated ciphertexts should differ if public keys differ."
        );
    }

    #[test]
    fn test_different_hashes_produce_different_ciphertexts_unauthenticated() {
        let cipher = UnauthenticatedCipher {};
        let pk = create_test_pk(160);
        let hash1 = create_dummy_hash(160);
        let hash2 = create_dummy_hash(161);
        let index = 0_usize;
        let plaintext = b"unauth same data, diff hash".to_vec();

        let ciphertext1 = cipher
            .encrypt_share(plaintext.clone(), &pk, &hash1, index)
            .unwrap();
        let ciphertext2 = cipher
            .encrypt_share(plaintext.clone(), &pk, &hash2, index)
            .unwrap();

        assert_ne!(
            ciphertext1, ciphertext2,
            "Unauthenticated ciphertexts should differ if hashes differ."
        );
    }

    #[test]
    fn test_different_indices_produce_different_ciphertexts_unauthenticated() {
        let cipher = UnauthenticatedCipher {};
        let pk = create_test_pk(170);
        let hash = create_dummy_hash(170);
        let index1 = 0_usize;
        let index2 = 1_usize;
        let plaintext = b"unauth same data, diff index".to_vec();

        let ciphertext1 = cipher
            .encrypt_share(plaintext.clone(), &pk, &hash, index1)
            .unwrap();
        let ciphertext2 = cipher
            .encrypt_share(plaintext.clone(), &pk, &hash, index2)
            .unwrap();

        assert_ne!(
            ciphertext1, ciphertext2,
            "Unauthenticated ciphertexts should differ if indices differ."
        );
    }
}